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