nitc: fix --typing-test-metrics by using correct tags inf FFI
[nit.git] / src / compiler / compiler_ffi / compiler_ffi.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Full FFI support for the compiler
18 module compiler_ffi
19
20 intrude import light
21 import nitni
22
23 redef class MModule
24 private var foreign_callbacks = new ForeignCallbackSet
25
26 redef fun finalize_ffi(compiler: AbstractCompiler)
27 do
28 if not uses_ffi then return
29
30 super
31
32 compiled_callbacks.clear
33 #Do not reset `foreign_callbacks` and `ffi_callbacks` because they are computed in previous phases
34 end
35
36 private var compiled_callbacks = new Array[NitniCallback]
37
38 # Returns true if callbacks has yet to be generated and register it as being generated
39 private fun check_callback_compilation(cb: NitniCallback): Bool
40 do
41 var compiled = compiled_callbacks.has(cb)
42 if not compiled then compiled_callbacks.add(cb)
43 return not compiled
44 end
45 end
46
47 redef class AMethPropdef
48 redef fun compile_ffi_support_to_c(v)
49 do
50 super
51
52 var mmodule = mpropdef.mclassdef.mmodule
53 var mainmodule = v.compiler.mainmodule
54 var ccu = mmodule.nitni_ccu.as(not null)
55
56 for mtype in foreign_callbacks.types do
57 if not mtype.is_cprimitive then
58 mtype.compile_extern_type(v, ccu)
59
60 # has callbacks already been compiled? (this may very well happen with global compilation)
61 mtype.compile_extern_helper_functions(v, ccu, mmodule.check_callback_compilation(mtype))
62 end
63 end
64
65 # compile callbacks
66 for cb in foreign_callbacks.callbacks do
67 cb.compile_extern_callback(v, ccu, mainmodule.check_callback_compilation(cb))
68 end
69
70 for cb in foreign_callbacks.supers do
71 cb.compile_extern_callback(v, ccu, mainmodule.check_callback_compilation(cb))
72 end
73
74 for cb in foreign_callbacks.casts do
75 cb.compile_extern_callbacks(v, ccu, mainmodule.check_callback_compilation(cb))
76 end
77
78 # manage nitni callback set
79 mmodule.foreign_callbacks.join(foreign_callbacks)
80 end
81 end
82
83 redef class MExplicitCall
84 private fun compile_extern_callback(v: AbstractCompilerVisitor, ccu: CCompilationUnit, compile_implementation_too: Bool)
85 do
86 var mproperty = mproperty
87 assert mproperty isa MMethod
88
89 # In nitni files, declare internal function as extern
90 var full_friendly_csignature = mproperty.build_csignature(recv_mtype, v.compiler.mainmodule, null, long_signature, internal_call_context)
91 ccu.header_decl.add("extern {full_friendly_csignature};\n")
92
93 if not compile_implementation_too then return
94
95 # Internally, implement internal function
96 var nitni_visitor = v.compiler.new_visitor
97 nitni_visitor.frame = v.frame
98 var msignature = mproperty.lookup_first_definition(v.compiler.mainmodule, recv_mtype).msignature
99 var csignature_blind = mproperty.build_csignature(recv_mtype, v.compiler.mainmodule, null, long_signature, internal_call_context)
100
101 nitni_visitor.add_decl("/* nitni callback for {mproperty.full_name} */")
102 nitni_visitor.add_decl("{csignature_blind} \{")
103
104 var vars = new Array[RuntimeVariable]
105 var mtype: MType = recv_mtype
106 var recv_var = null
107 if mproperty.is_init then
108 var recv_mtype = recv_mtype
109 recv_var = nitni_visitor.init_instance_or_extern(recv_mtype)
110 nitni_visitor.add("{mtype.ctype} self /* var self: {mtype} */;")
111 nitni_visitor.add("self = {recv_var};")
112 else
113 mtype = mtype.anchor_to(v.compiler.mainmodule, recv_mtype)
114 recv_var = nitni_visitor.var_from_c("self", mtype)
115 recv_var = nitni_visitor.box_extern(recv_var, mtype)
116 end
117
118 vars.add(recv_var)
119
120 for p in msignature.mparameters do
121 var arg_mtype = p.mtype.anchor_to(v.compiler.mainmodule, recv_mtype)
122 var arg = nitni_visitor.var_from_c(p.name, arg_mtype)
123 arg = nitni_visitor.box_extern(arg, arg_mtype)
124 vars.add(arg)
125 end
126
127 var ret_var = nitni_visitor.send(mproperty, vars)
128
129 var return_mtype = msignature.return_mtype
130 if mproperty.is_init then
131 if recv_mtype.mclass.kind != extern_kind then ret_var = recv_var
132 return_mtype = recv_mtype
133 end
134 if return_mtype != null then
135 assert ret_var != null
136 return_mtype = return_mtype.anchor_to(v.compiler.mainmodule, recv_mtype)
137 ret_var = nitni_visitor.autobox(ret_var, return_mtype)
138 ret_var = nitni_visitor.unbox_extern(ret_var, return_mtype)
139 nitni_visitor.ret_to_c(ret_var, return_mtype)
140 end
141 nitni_visitor.add("\}")
142 end
143 end
144
145 redef class MType
146 private fun compile_extern_helper_functions(v: AbstractCompilerVisitor, ccu: CCompilationUnit, compile_implementation_too: Bool)
147 do
148 # actually, we do not need to do anything when using the bohem garbage collector
149 var call_context = from_c_call_context
150
151 # incr_ref
152 ccu.header_decl.add "#ifndef {mangled_cname}_incr_ref\n"
153 ccu.header_decl.add " #define {mangled_cname}_incr_ref(from) nitni_global_ref_incr(({call_context.name_mtype(self)})(from))\n"
154 ccu.header_decl.add "#endif\n"
155
156 # decr_ref
157 ccu.header_decl.add "#ifndef {mangled_cname}_decr_ref\n"
158 ccu.header_decl.add " #define {mangled_cname}_decr_ref(from) nitni_global_ref_decr(({call_context.name_mtype(self)})(from))\n"
159 ccu.header_decl.add "#endif\n"
160 end
161 end
162
163 redef class MNullableType
164 redef fun compile_extern_helper_functions(v, ccu, compile_implementation_too)
165 do
166 super
167
168 var base_cname = "null_{mtype.mangled_cname}"
169 var full_cname = "NIT_NULL___{base_cname}"
170
171 # In nitni files, declare internal function as extern
172 var full_friendly_csignature = "{cname_blind} {full_cname}()"
173 ccu.header_decl.add("extern {full_friendly_csignature};\n")
174
175 # In nitni files, #define friendly as extern
176 ccu.header_decl.add("#define {base_cname} {full_cname}\n")
177
178 if not compile_implementation_too then return
179
180 # FIXME: This is ugly an broke the separate compilation principle
181 # The real function MUST be compiled only once, #define pragma only protect the compiler, not the loader
182 # However, I am not sure of the right approach here (eg. week refs are ugly)
183 if v.compiler.compiled_null_types.has(self) then return
184 v.compiler.compiled_null_types.add self
185
186 # Internally, implement internal function
187 var nitni_visitor = v.compiler.new_visitor
188 nitni_visitor.frame = v.frame
189 var full_internal_csignature = "{cname_blind} {full_cname}()"
190 nitni_visitor.add("{full_internal_csignature} \{")
191 nitni_visitor.add("struct nitni_instance* ret_for_c;")
192 nitni_visitor.add("ret_for_c = nit_alloc(sizeof(struct nitni_instance));")
193 nitni_visitor.add("ret_for_c->value = NULL;")
194 nitni_visitor.add("return ret_for_c;")
195 nitni_visitor.add("\}")
196 end
197 end
198
199 redef class MExplicitSuper
200 private fun compile_extern_callback(v: AbstractCompilerVisitor, ccu: CCompilationUnit, compile_implementation_too: Bool)
201 do
202 var mproperty = from.mproperty
203 assert mproperty isa MMethod
204 var mclass_type = from.mclassdef.mclass.mclass_type
205
206 # In nitni files, declare internal function as extern
207 var internal_csignature = mproperty.build_csignature(mclass_type, v.compiler.mainmodule, "___super", long_signature, internal_call_context)
208 ccu.header_decl.add("extern {internal_csignature};\n")
209
210 # In nitni files, #define friendly as extern
211 var friendly_cname = mproperty.build_cname(mclass_type, v.compiler.mainmodule, "___super", short_signature)
212 var internal_cname = mproperty.build_cname(mclass_type, v.compiler.mainmodule, "___super", long_signature)
213 ccu.header_decl.add("#define {friendly_cname} {internal_cname}\n")
214
215 if not compile_implementation_too then return
216
217 # Internally, implement internal function
218 var nitni_visitor = v.compiler.new_visitor
219 nitni_visitor.frame = v.frame
220 var msignature = mproperty.lookup_first_definition(v.compiler.mainmodule, mclass_type).msignature
221
222 var csignature_blind = mproperty.build_csignature(mclass_type, v.compiler.mainmodule, "___super", long_signature, internal_call_context)
223
224 nitni_visitor.add_decl("/* nitni callback to super for {mproperty.full_name} */")
225 nitni_visitor.add_decl("{csignature_blind} \{")
226
227 var vars = new Array[RuntimeVariable]
228
229 var recv_var = nitni_visitor.var_from_c("self", mclass_type)
230 recv_var = nitni_visitor.box_extern(recv_var, mclass_type)
231 vars.add(recv_var)
232
233 for p in msignature.mparameters do
234 var arg_mtype = v.anchor(p.mtype)
235 var arg = nitni_visitor.var_from_c(p.name, arg_mtype)
236 arg = nitni_visitor.box_extern(arg, arg_mtype)
237 vars.add(arg)
238 end
239
240 var ret_var = nitni_visitor.supercall(from.as(MMethodDef), mclass_type, vars)
241
242 var return_mtype = msignature.return_mtype
243 if return_mtype != null then
244 assert ret_var != null
245 return_mtype = v.anchor(return_mtype)
246 ret_var = nitni_visitor.autobox(ret_var, return_mtype)
247 ret_var = nitni_visitor.unbox_extern(ret_var, return_mtype)
248 nitni_visitor.ret_to_c(ret_var, return_mtype)
249 end
250 nitni_visitor.add("\}")
251 end
252 end
253
254 redef class MExplicitCast
255 private fun compile_extern_callbacks(v: AbstractCompilerVisitor, ccu: CCompilationUnit, compile_implementation_too: Bool)
256 do
257 var from = from
258 var to = to
259
260 #
261 ## check type
262 #
263
264 # In nitni files, declare internal function as extern
265 var full_friendly_csignature = "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
266 ccu.header_decl.add("extern {full_friendly_csignature};\n")
267
268 # In nitni files, #define friendly as extern
269 ccu.header_decl.add("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
270
271 if compile_implementation_too then
272 # Internally, implement internal function
273 var nitni_visitor = v.compiler.new_visitor
274 nitni_visitor.frame = v.frame
275
276 var full_internal_csignature = "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
277
278 nitni_visitor.add_decl("/* nitni check for {from} to {to} */")
279 nitni_visitor.add_decl("{full_internal_csignature} \{")
280
281 var from_var = nitni_visitor.var_from_c("from", from)
282 from_var = nitni_visitor.box_extern(from_var, from)
283 var recv_var = nitni_visitor.type_test(from_var, to, "isa")
284 nitni_visitor.add("return {recv_var};")
285
286 nitni_visitor.add("\}")
287 end
288
289 # special checks
290 if from == to.as_nullable then
291 # format A_is_null
292 ccu.header_decl.add("#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n")
293 end
294
295 #
296 ## cast
297 #
298
299 # In nitni files, declare internal function as extern
300 full_friendly_csignature = "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
301 ccu.header_decl.add("extern {full_friendly_csignature};\n")
302
303 # In nitni files, #define friendly as extern
304 ccu.header_decl.add("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
305
306 if compile_implementation_too then
307 # Internally, implement internal function
308 var nitni_visitor = v.compiler.new_visitor
309 nitni_visitor.frame = v.frame
310
311 var full_internal_csignature = "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
312 nitni_visitor.add_decl("/* nitni cast for {from} to {to} */")
313 nitni_visitor.add_decl("{full_internal_csignature} \{")
314
315 var from_var = nitni_visitor.var_from_c("from", from)
316 from_var = nitni_visitor.box_extern(from_var, from)
317
318 ## test type
319 var check = nitni_visitor.type_test(from_var, to, "as")
320 nitni_visitor.add("if (!{check}) \{")
321 nitni_visitor.add_abort("FFI cast failed")
322 nitni_visitor.add("\}")
323
324 ## internal cast
325 var recv_var = nitni_visitor.autobox(from_var, to)
326 recv_var = nitni_visitor.unbox_extern(recv_var, to)
327
328 nitni_visitor.ret_to_c(recv_var, to)
329
330 nitni_visitor.add("\}")
331 end
332
333 # special casts
334 if from.as_nullable == to then
335 # format A_as_nullable
336 ccu.header_decl.add("#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
337 end
338
339 if from == to.as_nullable then
340 # format A_as_nullable
341 ccu.header_decl.add("#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
342 end
343 end
344 end