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