nitc: fix calling extern constructors from extern code in separate compiler
[nit.git] / src / compiler / 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 # FFI support for the compilers
18 module compiler_ffi
19
20 intrude import abstract_compiler
21 intrude import ffi
22 import nitni
23
24 redef class MModule
25 private var foreign_callbacks = new ForeignCallbackSet
26 var nitni_ccu: nullable CCompilationUnit = null
27
28 private fun nmodule(v: AbstractCompilerVisitor): nullable AModule
29 do
30 return v.compiler.modelbuilder.mmodule2node(self)
31 end
32
33 redef fun finalize_ffi(compiler: AbstractCompiler)
34 do
35 if not uses_ffi then return
36
37 var v = compiler.new_visitor
38 var n = nmodule(v)
39 if n == null then return
40 n.ensure_compile_ffi_wrapper
41 finalize_ffi_wrapper(v.compiler.modelbuilder.compile_dir, v.compiler.mainmodule)
42 for file in ffi_files do v.compiler.extern_bodies.add(file)
43
44 ensure_compile_nitni_base(v)
45
46 nitni_ccu.header_c_types.add("#include \"{c_name}._ffi.h\"\n")
47 nitni_ccu.header_c_types.add """
48 extern void nitni_global_ref_incr(void*);
49 extern void nitni_global_ref_decr(void*);
50 """
51
52 var cflags = self.cflags[""].join(" ")
53 nitni_ccu.write_as_nitni(self, v.compiler.modelbuilder.compile_dir)
54
55 for file in nitni_ccu.files do
56 var f = new ExternCFile(file, cflags)
57 f.pkgconfigs.add_all pkgconfigs
58 v.compiler.extern_bodies.add(f)
59 end
60
61 # reset FFI things so the next compilation job, if any, starts with a clean context
62 # FIXME clean and rationalize this
63 nitni_ccu = null
64 compiled_ffi_methods.clear
65 ffi_ccu = null
66 ffi_files.clear
67 compiled_callbacks.clear
68 #Do not reset `foreign_callbacks` and `ffi_callbacks` because they are computed in previous phases
69 end
70
71 private fun ensure_compile_nitni_base(v: AbstractCompilerVisitor)
72 do
73 if nitni_ccu != null then return
74
75 nitni_ccu = new CCompilationUnit
76 end
77
78 redef fun collect_linker_libs
79 do
80 if not self.ldflags.keys.has("") then return null
81 return self.ldflags[""]
82 end
83
84 private var compiled_callbacks = new Array[NitniCallback]
85
86 # Returns true if callbacks has yet to be generated and register it as being generated
87 private fun check_callback_compilation(cb: NitniCallback): Bool
88 do
89 var compiled = compiled_callbacks.has(cb)
90 if not compiled then compiled_callbacks.add(cb)
91 return not compiled
92 end
93 end
94
95 redef class AMethPropdef
96 private fun compile_ffi_support_to_c(v: AbstractCompilerVisitor)
97 do
98 var mmodule = mpropdef.mclassdef.mmodule
99 var mainmodule = v.compiler.mainmodule
100 var amodule = v.compiler.modelbuilder.mmodule2node(mmodule)
101 var mclass_type = mpropdef.mclassdef.bound_mtype
102
103 # Declare as extern
104 var csignature = mpropdef.mproperty.build_csignature(mclass_type, mmodule, "___impl", long_signature, internal_call_context)
105 v.declare_once("{csignature};")
106
107 # FFI part
108 amodule.ensure_compile_ffi_wrapper
109 compile_ffi_method(mmodule)
110
111 # nitni - Compile missing callbacks
112 mmodule.ensure_compile_nitni_base(v)
113 var ccu = mmodule.nitni_ccu.as(not null)
114
115 for mtype in foreign_callbacks.types do
116 if not mtype.is_cprimitive then
117 mtype.compile_extern_type(v, ccu)
118
119 # has callbacks already been compiled? (this may very well happen with global compilation)
120 mtype.compile_extern_helper_functions(v, ccu, mmodule.check_callback_compilation(mtype))
121 end
122 end
123
124 # compile callbacks
125 for cb in foreign_callbacks.callbacks do
126 cb.compile_extern_callback(v, ccu, mainmodule.check_callback_compilation(cb))
127 end
128
129 for cb in foreign_callbacks.supers do
130 cb.compile_extern_callback(v, ccu, mainmodule.check_callback_compilation(cb))
131 end
132
133 for cb in foreign_callbacks.casts do
134 cb.compile_extern_callbacks(v, ccu, mainmodule.check_callback_compilation(cb))
135 end
136
137 # manage nitni callback set
138 mmodule.foreign_callbacks.join(foreign_callbacks)
139 end
140
141 redef fun compile_externmeth_to_c(v, mpropdef, arguments)
142 do
143 # if using the old native interface fallback on previous implementation
144 if n_extern_code_block == null then return super
145
146 var mmodule = mpropdef.mclassdef.mmodule
147 mmodule.uses_ffi = true
148
149 var mclass_type = mpropdef.mclassdef.bound_mtype
150
151 # Outgoing code in compiler
152 var externname = mpropdef.mproperty.build_cname(mpropdef.mclassdef.bound_mtype, mmodule, "___impl", long_signature)
153 var recv_var: nullable RuntimeVariable = null
154 var return_mtype = mpropdef.msignature.return_mtype
155 if return_mtype != null then
156 return_mtype = return_mtype.anchor_to(mmodule, mclass_type)
157 recv_var = v.new_var(return_mtype)
158 end
159
160 v.adapt_signature(mpropdef, arguments)
161 v.unbox_signature_extern(mpropdef, arguments)
162
163 var arguments_for_c = new Array[String]
164 for a in [0..arguments.length[ do
165 var arg = arguments[a]
166 var param_mtype: MType
167 if a == 0 then
168 param_mtype = mpropdef.mclassdef.mclass.mclass_type
169 else param_mtype = mpropdef.msignature.mparameters[a-1].mtype
170
171 param_mtype = param_mtype.anchor_to(mmodule, mclass_type)
172
173 if param_mtype.is_cprimitive then
174 arguments_for_c.add(arg.name)
175 else
176 v.add("struct nitni_instance* var_for_c_{a};")
177 v.add("var_for_c_{a} = nit_alloc(sizeof(struct nitni_instance));")
178 v.add("var_for_c_{a}->value = {arg.name};")
179 arguments_for_c.add("var_for_c_{a}")
180 end
181 end
182
183 if recv_var == null then
184 v.add("{externname}({arguments_for_c.join(", ")});")
185 else
186 assert return_mtype != null
187 if return_mtype.is_cprimitive then
188 v.add("{recv_var} = {externname}({arguments_for_c.join(", ")});")
189 else
190 v.add("struct nitni_instance* ret_var;")
191 v.add("ret_var = {externname}({arguments_for_c.join(", ")});")
192 v.add("{recv_var} = ret_var->value;")
193 end
194 recv_var = v.box_extern(recv_var, return_mtype)
195 v.ret(recv_var)
196 end
197
198 compile_ffi_support_to_c(v)
199 return true
200 end
201
202 redef fun compile_externinit_to_c(v, mpropdef, arguments)
203 do
204 # if using the old native interface fallback on previous implementation
205 if n_extern_code_block == null then return super
206
207 var mmodule = mpropdef.mclassdef.mmodule
208 mmodule.uses_ffi = true
209
210 var mclass_type = mpropdef.mclassdef.bound_mtype
211
212 var externname = mpropdef.mproperty.build_cname(mpropdef.mclassdef.bound_mtype, mmodule, "___impl", long_signature)
213 var return_mtype = arguments.first.mtype
214 var recv_var = v.new_var(return_mtype)
215
216 v.adapt_signature(mpropdef, arguments)
217 v.unbox_signature_extern(mpropdef, arguments)
218
219 arguments.shift
220
221 var arguments_for_c = new Array[String]
222 for a in [0..arguments.length[ do
223 var arg = arguments[a]
224 var param_mtype: MType
225 param_mtype = mpropdef.msignature.mparameters[a].mtype
226 param_mtype = param_mtype.anchor_to(mmodule, mclass_type)
227
228 if param_mtype.is_cprimitive then
229 arguments_for_c.add(arg.name)
230 else
231 v.add("struct nitni_instance* var_for_c_{a};")
232 v.add("var_for_c_{a} = nit_alloc(sizeof(struct nitni_instance));")
233 v.add("var_for_c_{a}->value = {arg.name};")
234 arguments_for_c.add("var_for_c_{a}")
235 end
236 end
237
238 if return_mtype.is_cprimitive then
239 v.add("{recv_var} = {externname}({arguments_for_c.join(", ")});")
240 else
241 v.add("struct nitni_instance* ret_var;")
242 v.add("ret_var = {externname}({arguments_for_c.join(", ")});")
243 v.add("{recv_var} = ret_var->value;")
244 end
245 recv_var = v.box_extern(recv_var, return_mtype)
246 v.ret(recv_var)
247
248 compile_ffi_support_to_c(v)
249 return true
250 end
251 end
252
253 redef class CCompilationUnit
254 fun write_as_nitni(mmodule: MModule, compdir: String)
255 do
256 var base_name = "{mmodule.c_name}._nitni"
257
258 var h_file = "{base_name}.h"
259 write_header_to_file( mmodule, "{compdir}/{h_file}", new Array[String],
260 "{mmodule.c_name.to_s.to_upper}_NITG_NITNI_H")
261
262 var c_file = "{base_name}.c"
263 write_body_to_file( mmodule, "{compdir}/{c_file}", ["\"{h_file}\""] )
264
265 files.add( "{compdir}/{c_file}" )
266 end
267 end
268
269 redef class AbstractCompiler
270 # Cache to avoid multiple compilation of NULL values
271 # see FIXME in `MNullableType#compile_extern_helper_functions`
272 private var compiled_null_types = new Array[MNullableType]
273 end
274
275 redef class AbstractCompilerVisitor
276 # Create a `RuntimeVariable` for this C variable originating from C user code
277 private fun var_from_c(name: String, mtype: MType): RuntimeVariable
278 do
279 if mtype.is_cprimitive then
280 return new RuntimeVariable(name, mtype, mtype)
281 else
282 return new RuntimeVariable("{name}->value", mtype, mtype)
283 end
284 end
285
286 # Return a `RuntimeVarible` to C user code
287 private fun ret_to_c(src: RuntimeVariable, mtype: MType)
288 do
289 if mtype.is_cprimitive then
290 add("return {src};")
291 else
292 add("struct nitni_instance* ret_for_c;")
293 add("ret_for_c = nit_alloc(sizeof(struct nitni_instance));")
294 add("ret_for_c->value = {src};")
295 add("return ret_for_c;")
296 end
297 end
298 end
299
300 redef class MType
301 private fun compile_extern_type(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
302 do
303 assert not is_cprimitive
304
305 # define friendly type
306 ccu.header_c_types.add("#ifndef NIT_TYPE_{cname}\n")
307 ccu.header_c_types.add("#define NIT_TYPE_{cname} 1\n")
308 ccu.header_c_types.add("typedef struct nitni_instance *{cname};\n")
309 ccu.header_c_types.add("#endif\n")
310 end
311
312 private fun compile_extern_helper_functions(v: AbstractCompilerVisitor, ccu: CCompilationUnit, compile_implementation_too: Bool)
313 do
314 # actually, we do not need to do anything when using the bohem garbage collector
315 var call_context = from_c_call_context
316
317 # incr_ref
318 ccu.header_decl.add "#ifndef {mangled_cname}_incr_ref\n"
319 ccu.header_decl.add " #define {mangled_cname}_incr_ref(from) nitni_global_ref_incr(({call_context.name_mtype(self)})(from))\n"
320 ccu.header_decl.add "#endif\n"
321
322 # decr_ref
323 ccu.header_decl.add "#ifndef {mangled_cname}_decr_ref\n"
324 ccu.header_decl.add " #define {mangled_cname}_decr_ref(from) nitni_global_ref_decr(({call_context.name_mtype(self)})(from))\n"
325 ccu.header_decl.add "#endif\n"
326 end
327 end
328
329 redef class MNullableType
330 redef fun compile_extern_helper_functions(v, ccu, compile_implementation_too)
331 do
332 super
333
334 var base_cname = "null_{mtype.mangled_cname}"
335 var full_cname = "NIT_NULL___{base_cname}"
336
337 # In nitni files, declare internal function as extern
338 var full_friendly_csignature = "{cname_blind} {full_cname}()"
339 ccu.header_decl.add("extern {full_friendly_csignature};\n")
340
341 # In nitni files, #define friendly as extern
342 ccu.header_decl.add("#define {base_cname} {full_cname}\n")
343
344 if not compile_implementation_too then return
345
346 # FIXME: This is ugly an broke the separate compilation principle
347 # The real function MUST be compiled only once, #define pragma only protect the compiler, not the loader
348 # However, I am not sure of the right approach here (eg. week refs are ugly)
349 if v.compiler.compiled_null_types.has(self) then return
350 v.compiler.compiled_null_types.add self
351
352 # Internally, implement internal function
353 var nitni_visitor = v.compiler.new_visitor
354 nitni_visitor.frame = v.frame
355 var full_internal_csignature = "{cname_blind} {full_cname}()"
356 nitni_visitor.add("{full_internal_csignature} \{")
357 nitni_visitor.add("struct nitni_instance* ret_for_c;")
358 nitni_visitor.add("ret_for_c = nit_alloc(sizeof(struct nitni_instance));")
359 nitni_visitor.add("ret_for_c->value = NULL;")
360 nitni_visitor.add("return ret_for_c;")
361 nitni_visitor.add("\}")
362 end
363 end
364
365 redef class MExplicitCall
366 private fun compile_extern_callback(v: AbstractCompilerVisitor, ccu: CCompilationUnit, compile_implementation_too: Bool)
367 do
368 var mproperty = mproperty
369 assert mproperty isa MMethod
370
371 # In nitni files, declare internal function as extern
372 var full_friendly_csignature = mproperty.build_csignature(recv_mtype, v.compiler.mainmodule, null, long_signature, internal_call_context)
373 ccu.header_decl.add("extern {full_friendly_csignature};\n")
374
375 if not compile_implementation_too then return
376
377 # Internally, implement internal function
378 var nitni_visitor = v.compiler.new_visitor
379 nitni_visitor.frame = v.frame
380 var msignature = mproperty.lookup_first_definition(v.compiler.mainmodule, recv_mtype).msignature
381 var csignature_blind = mproperty.build_csignature(recv_mtype, v.compiler.mainmodule, null, long_signature, internal_call_context)
382
383 nitni_visitor.add_decl("/* nitni callback for {mproperty.full_name} */")
384 nitni_visitor.add_decl("{csignature_blind} \{")
385
386 var vars = new Array[RuntimeVariable]
387 var mtype: MType = recv_mtype
388 var recv_var = null
389 if mproperty.is_init then
390 var recv_mtype = recv_mtype
391 recv_var = nitni_visitor.init_instance_or_extern(recv_mtype)
392 nitni_visitor.add("{mtype.ctype} recv /* var self: {mtype} */;")
393 nitni_visitor.add("recv = {recv_var};")
394 else
395 mtype = mtype.anchor_to(v.compiler.mainmodule, recv_mtype)
396 recv_var = nitni_visitor.var_from_c("recv", mtype)
397 recv_var = nitni_visitor.box_extern(recv_var, mtype)
398 end
399
400 vars.add(recv_var)
401
402 for p in msignature.mparameters do
403 var arg_mtype = p.mtype.anchor_to(v.compiler.mainmodule, recv_mtype)
404 var arg = nitni_visitor.var_from_c(p.name, arg_mtype)
405 arg = nitni_visitor.box_extern(arg, arg_mtype)
406 vars.add(arg)
407 end
408
409 var ret_var = nitni_visitor.send(mproperty, vars)
410
411 var return_mtype = msignature.return_mtype
412 if mproperty.is_init then
413 if recv_mtype.mclass.kind != extern_kind then ret_var = recv_var
414 return_mtype = recv_mtype
415 end
416 if return_mtype != null then
417 assert ret_var != null
418 return_mtype = return_mtype.anchor_to(v.compiler.mainmodule, recv_mtype)
419 ret_var = nitni_visitor.autobox(ret_var, return_mtype)
420 ret_var = nitni_visitor.unbox_extern(ret_var, return_mtype)
421 nitni_visitor.ret_to_c(ret_var, return_mtype)
422 end
423 nitni_visitor.add("\}")
424 end
425 end
426
427 redef class MExplicitSuper
428 private fun compile_extern_callback(v: AbstractCompilerVisitor, ccu: CCompilationUnit, compile_implementation_too: Bool)
429 do
430 var mproperty = from.mproperty
431 assert mproperty isa MMethod
432 var mclass_type = from.mclassdef.mclass.mclass_type
433
434 # In nitni files, declare internal function as extern
435 var internal_csignature = mproperty.build_csignature(mclass_type, v.compiler.mainmodule, "___super", long_signature, internal_call_context)
436 ccu.header_decl.add("extern {internal_csignature};\n")
437
438 # In nitni files, #define friendly as extern
439 var friendly_cname = mproperty.build_cname(mclass_type, v.compiler.mainmodule, "___super", short_signature)
440 var internal_cname = mproperty.build_cname(mclass_type, v.compiler.mainmodule, "___super", long_signature)
441 ccu.header_decl.add("#define {friendly_cname} {internal_cname}\n")
442
443 if not compile_implementation_too then return
444
445 # Internally, implement internal function
446 var nitni_visitor = v.compiler.new_visitor
447 nitni_visitor.frame = v.frame
448 var msignature = mproperty.lookup_first_definition(v.compiler.mainmodule, mclass_type).msignature
449
450 var csignature_blind = mproperty.build_csignature(mclass_type, v.compiler.mainmodule, "___super", long_signature, internal_call_context)
451
452 nitni_visitor.add_decl("/* nitni callback to super for {mproperty.full_name} */")
453 nitni_visitor.add_decl("{csignature_blind} \{")
454
455 var vars = new Array[RuntimeVariable]
456
457 var recv_var = nitni_visitor.var_from_c("recv", mclass_type)
458 recv_var = nitni_visitor.box_extern(recv_var, mclass_type)
459 vars.add(recv_var)
460
461 for p in msignature.mparameters do
462 var arg_mtype = v.anchor(p.mtype)
463 var arg = nitni_visitor.var_from_c(p.name, arg_mtype)
464 arg = nitni_visitor.box_extern(arg, arg_mtype)
465 vars.add(arg)
466 end
467
468 var ret_var = nitni_visitor.supercall(from.as(MMethodDef), mclass_type, vars)
469
470 var return_mtype = msignature.return_mtype
471 if return_mtype != null then
472 assert ret_var != null
473 return_mtype = v.anchor(return_mtype)
474 ret_var = nitni_visitor.autobox(ret_var, return_mtype)
475 ret_var = nitni_visitor.unbox_extern(ret_var, return_mtype)
476 nitni_visitor.ret_to_c(ret_var, return_mtype)
477 end
478 nitni_visitor.add("\}")
479 end
480 end
481
482 redef class MExplicitCast
483 private fun compile_extern_callbacks(v: AbstractCompilerVisitor, ccu: CCompilationUnit, compile_implementation_too: Bool)
484 do
485 var from = from
486 var to = to
487
488 #
489 ## check type
490 #
491
492 # In nitni files, declare internal function as extern
493 var full_friendly_csignature = "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
494 ccu.header_decl.add("extern {full_friendly_csignature};\n")
495
496 # In nitni files, #define friendly as extern
497 ccu.header_decl.add("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
498
499 if compile_implementation_too then
500 # Internally, implement internal function
501 var nitni_visitor = v.compiler.new_visitor
502 nitni_visitor.frame = v.frame
503
504 var full_internal_csignature = "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
505
506 nitni_visitor.add_decl("/* nitni check for {from} to {to} */")
507 nitni_visitor.add_decl("{full_internal_csignature} \{")
508
509 var from_var = nitni_visitor.var_from_c("from", from)
510 from_var = nitni_visitor.box_extern(from_var, from)
511 var recv_var = nitni_visitor.type_test(from_var, to, "FFI isa")
512 nitni_visitor.add("return {recv_var};")
513
514 nitni_visitor.add("\}")
515 end
516
517 # special checks
518 if from == to.as_nullable then
519 # format A_is_null
520 ccu.header_decl.add("#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n")
521 end
522
523 #
524 ## cast
525 #
526
527 # In nitni files, declare internal function as extern
528 full_friendly_csignature = "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
529 ccu.header_decl.add("extern {full_friendly_csignature};\n")
530
531 # In nitni files, #define friendly as extern
532 ccu.header_decl.add("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
533
534 if compile_implementation_too then
535 # Internally, implement internal function
536 var nitni_visitor = v.compiler.new_visitor
537 nitni_visitor.frame = v.frame
538
539 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)"
540 nitni_visitor.add_decl("/* nitni cast for {from} to {to} */")
541 nitni_visitor.add_decl("{full_internal_csignature} \{")
542
543 var from_var = nitni_visitor.var_from_c("from", from)
544 from_var = nitni_visitor.box_extern(from_var, from)
545
546 ## test type
547 var check = nitni_visitor.type_test(from_var, to, "FFI cast")
548 nitni_visitor.add("if (!{check}) \{")
549 nitni_visitor.add_abort("FFI cast failed")
550 nitni_visitor.add("\}")
551
552 ## internal cast
553 var recv_var = nitni_visitor.autobox(from_var, to)
554 recv_var = nitni_visitor.unbox_extern(recv_var, to)
555
556 nitni_visitor.ret_to_c(recv_var, to)
557
558 nitni_visitor.add("\}")
559 end
560
561 # special casts
562 if from.as_nullable == to then
563 # format A_as_nullable
564 ccu.header_decl.add("#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
565 end
566
567 if from == to.as_nullable then
568 # format A_as_nullable
569 ccu.header_decl.add("#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
570 end
571 end
572 end