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