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