1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # FFI support for the compilers
20 intrude import abstract_compiler
21 intrude import common_ffi
25 private var foreign_callbacks
= new ForeignCallbackSet
26 var nitni_ccu
: nullable CCompilationUnit = null
28 private fun nmodule
(v
: AbstractCompilerVisitor): nullable AModule
30 var m2n
= v
.compiler
.modelbuilder
.mmodule2nmodule
31 return m2n
.get_or_null
(self)
34 redef fun finalize_ffi
(compiler
: AbstractCompiler)
36 if not uses_ffi
then return
38 var v
= compiler
.new_visitor
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
)
45 ensure_compile_nitni_base
(v
)
47 nitni_ccu
.header_c_types
.add
("#include \"{name}._ffi
.h\
"\n")
49 nitni_ccu
.write_as_nitni
(self, v
.compiler
.modelbuilder
.compile_dir
)
51 for file
in nitni_ccu
.files
do
52 v
.compiler
.extern_bodies
.add
(new ExternCFile(file
, c_compiler_options
))
56 private fun ensure_compile_nitni_base
(v
: AbstractCompilerVisitor)
58 if nitni_ccu
!= null then return
60 nitni_ccu
= new CCompilationUnit
63 redef fun collect_linker_libs
65 var s
= c_linker_options
66 if s
.is_empty
then return null
67 var res
= new ArraySet[String]
72 private var compiled_callbacks
: Array[NitniCallback] = new Array[NitniCallback]
74 # Returns true if callbacks has yet to be generated and register it as being generated
75 private fun check_callback_compilation
(cb
: NitniCallback): Bool
77 var compiled
= compiled_callbacks
.has
(cb
)
78 if not compiled
then compiled_callbacks
.add
(cb
)
83 redef class AMethPropdef
84 private fun compile_ffi_support_to_c
(v
: AbstractCompilerVisitor)
86 var mmodule
= mpropdef
.mclassdef
.mmodule
87 var mainmodule
= v
.compiler
.mainmodule
88 var amainmodule
= v
.compiler
.modelbuilder
.mmodule2nmodule
[mainmodule
]
89 var amodule
= v
.compiler
.modelbuilder
.mmodule2nmodule
[mmodule
]
90 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
93 var csignature
= mpropdef
.mproperty
.build_csignature
(mclass_type
, mmodule
, "___impl", long_signature
, internal_call_context
)
94 v
.declare_once
("{csignature};")
97 amodule
.ensure_compile_ffi_wrapper
98 compile_ffi_method
(mmodule
)
100 assert self isa AExternPropdef
102 # nitni - Compile missing callbacks
103 mmodule
.ensure_compile_nitni_base
(v
)
104 var ccu
= mmodule
.nitni_ccu
.as(not null)
106 for mtype
in foreign_callbacks
.types
do
107 if not mtype
.is_cprimitive
then
108 mtype
.compile_extern_type
(v
, ccu
)
110 # has callbacks already been compiled? (this may very well happen with global compilation)
111 if mmodule
.check_callback_compilation
(mtype
) then mtype
.compile_extern_helper_functions
(v
, ccu
)
116 for cb
in foreign_callbacks
.callbacks
do if mainmodule
.check_callback_compilation
(cb
) then
117 cb
.compile_extern_callback
(v
, ccu
)
120 for cb
in foreign_callbacks
.supers
do if mainmodule
.check_callback_compilation
(cb
) then
121 cb
.compile_extern_callback
(v
, ccu
)
124 for cb
in foreign_callbacks
.casts
do if mainmodule
.check_callback_compilation
(cb
) then
125 cb
.compile_extern_callbacks
(v
, ccu
)
128 # manage nitni callback set
129 mmodule
.foreign_callbacks
.join
(foreign_callbacks
)
132 redef fun compile_externmeth_to_c
(v
, mpropdef
, arguments
)
134 var mmodule
= mpropdef
.mclassdef
.mmodule
136 # if using the old native interface fallback on previous implementation
137 var nextern
= self.n_extern
138 if nextern
!= null then
143 mmodule
.uses_ffi
= true
145 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
147 # Outgoing code in compiler
148 var externname
= mpropdef
.mproperty
.build_cname
(mpropdef
.mclassdef
.bound_mtype
, mmodule
, "___impl", long_signature
)
149 var recv_var
: nullable RuntimeVariable = null
150 var return_mtype
= mpropdef
.msignature
.return_mtype
151 if return_mtype
!= null then
152 return_mtype
= return_mtype
.anchor_to
(mmodule
, mclass_type
)
153 recv_var
= v
.new_var
(return_mtype
)
156 v
.adapt_signature
(mpropdef
, arguments
)
158 var arguments_for_c
= new Array[String]
159 for a
in [0..arguments
.length
[ do
160 var arg
= arguments
[a
]
161 var param_mtype
: MType
163 param_mtype
= mpropdef
.mclassdef
.mclass
.mclass_type
164 else param_mtype
= mpropdef
.msignature
.mparameters
[a-1
].mtype
166 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
168 if param_mtype
.is_cprimitive
then
169 arguments_for_c
.add
(arg
.name
)
171 v
.add
("struct nitni_instance* var_for_c_{a};")
172 v
.add
("var_for_c_{a} = malloc(sizeof(struct nitni_instance));")
173 v
.add
("var_for_c_{a}->value = {arg.name};")
174 arguments_for_c
.add
("var_for_c_{a}")
178 if recv_var
== null then
179 v
.add
("{externname}({arguments_for_c.join(", ")});")
181 assert return_mtype
!= null
182 if return_mtype
.is_cprimitive
then
183 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
185 v
.add
("struct nitni_instance* ret_var;")
186 v
.add
("ret_var = {externname}({arguments_for_c.join(", ")});")
187 v
.add
("{recv_var} = ret_var->value;")
192 compile_ffi_support_to_c
(v
)
195 redef fun compile_externinit_to_c
(v
, mpropdef
, arguments
)
197 var mmodule
= mpropdef
.mclassdef
.mmodule
199 # if using the old native interface fallback on previous implementation
200 var nextern
= self.n_extern
201 if nextern
!= null then
206 mmodule
.uses_ffi
= true
208 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
210 var externname
= mpropdef
.mproperty
.build_cname
(mpropdef
.mclassdef
.bound_mtype
, mmodule
, "___impl", long_signature
)
211 var return_mtype
= arguments
.first
.mtype
212 var recv_var
= v
.new_var
(return_mtype
)
214 v
.adapt_signature
(mpropdef
, arguments
)
218 var arguments_for_c
= new Array[String]
219 for a
in [0..arguments
.length
[ do
220 var arg
= arguments
[a
]
221 var param_mtype
: MType
222 param_mtype
= mpropdef
.msignature
.mparameters
[a
].mtype
223 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
225 if param_mtype
.is_cprimitive
then
226 arguments_for_c
.add
(arg
.name
)
228 v
.add
("struct nitni_instance* var_for_c_{a};")
229 v
.add
("var_for_c_{a} = malloc(sizeof(struct nitni_instance));")
230 v
.add
("var_for_c_{a}->value = {arg.name};")
231 arguments_for_c
.add
("var_for_c_{a}")
235 if return_mtype
.is_cprimitive
then
236 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
238 v
.add
("struct nitni_instance* ret_var;")
239 v
.add
("ret_var = {externname}({arguments_for_c.join(", ")});")
240 v
.add
("{recv_var} = ret_var->value;")
244 compile_ffi_support_to_c
(v
)
248 redef class CCompilationUnit
249 fun write_as_nitni
(mmodule
: MModule, compdir
: String)
251 var base_name
= "{mmodule.name}._nitni"
253 var h_file
= "{base_name}.h"
254 write_header_to_file
( mmodule
, "{compdir}/{h_file}", new Array[String],
255 "{mmodule.cname.to_s.to_upper}_NITG_NITNI_H")
257 var c_file
= "{base_name}.c"
258 write_body_to_file
( mmodule
, "{compdir}/{c_file}", ["\"{h_file}\
""] )
260 files
.add
( "{compdir}/{c_file}" )
264 redef class AbstractCompilerVisitor
265 # Create a `RuntimeVariable` for this C variable originating from C user code
266 private fun var_from_c
(name
: String, mtype
: MType): RuntimeVariable
268 if mtype
.is_cprimitive
then
269 return new RuntimeVariable(name
, mtype
, mtype
)
271 return new RuntimeVariable("{name}->value", mtype
, mtype
)
275 # Return a `RuntimeVarible` to C user code
276 private fun ret_to_c
(src
: RuntimeVariable, mtype
: MType)
278 if mtype
.is_cprimitive
then
281 add
("struct nitni_instance* ret_for_c;")
282 add
("ret_for_c = malloc(sizeof(struct nitni_instance));")
283 add
("ret_for_c->value = {src};")
284 add
("return ret_for_c;")
290 private fun compile_extern_type
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
292 assert not is_cprimitive
294 # define friendly type
295 ccu
.header_c_types
.add
("#ifndef NIT_TYPE_{cname}\n")
296 ccu
.header_c_types
.add
("#define NIT_TYPE_{cname} 1\n")
297 ccu
.header_c_types
.add
("typedef struct nitni_instance *{cname};\n")
298 ccu
.header_c_types
.add
("#endif\n")
301 private fun compile_extern_helper_functions
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
303 # actually, we do not need to do anything when using the bohem garbage collector
306 var nitni_visitor
= v
.compiler
.new_visitor
307 ccu
.header_decl
.add
("#define {mangled_cname}_incr_ref(from) while(0)\{\}\n")
310 nitni_visitor
= v
.compiler
.new_visitor
311 ccu
.header_decl
.add
("#define {mangled_cname}_decr_ref(from) while(0)\{\}\n")
315 redef class MNullableType
316 redef fun compile_extern_helper_functions
(v
, ccu
)
320 var base_cname
= "null_{mtype.mangled_cname}"
321 var full_cname
= "NIT_NULL___{base_cname}"
323 # In nitni files, declare internal function as extern
324 var full_friendly_csignature
= "{cname_blind} {full_cname}()"
325 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
327 # In nitni files, #define friendly as extern
328 ccu
.header_decl
.add
("#define {base_cname} {full_cname}\n")
330 # FIXME: This is ugly an broke the separate compilation principle
331 # The real function MUST be compiled only once, #define pragma only protect the compiler, not the loader
332 # However, I am not sure of the right approach here (eg. week refs are ugly)
333 if is_already_compiled
then return
334 is_already_compiled
= true
336 # Internally, implement internal function
337 var nitni_visitor
= v
.compiler
.new_visitor
338 nitni_visitor
.frame
= v
.frame
339 var full_internal_csignature
= "{cname_blind} {full_cname}()"
340 nitni_visitor
.add
("{full_internal_csignature} \{")
341 nitni_visitor
.add
("struct nitni_instance* ret_for_c;")
342 nitni_visitor
.add
("ret_for_c = malloc(sizeof(struct nitni_instance));")
343 nitni_visitor
.add
("ret_for_c->value = NULL;")
344 nitni_visitor
.add
("return ret_for_c;")
345 nitni_visitor
.add
("\}")
348 private var is_already_compiled
= false # FIXME to remove, show above
351 redef class MExplicitCall
352 private fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
354 var mproperty
= mproperty
355 assert mproperty
isa MMethod
357 # In nitni files, declare internal function as extern
358 var full_friendly_csignature
= mproperty
.build_csignature
(recv_mtype
, v
.compiler
.mainmodule
, null, long_signature
, internal_call_context
)
359 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
361 # Internally, implement internal function
362 var nitni_visitor
= v
.compiler
.new_visitor
363 nitni_visitor
.frame
= v
.frame
364 var msignature
= mproperty
.lookup_first_definition
(v
.compiler
.mainmodule
, recv_mtype
).msignature
365 var csignature_blind
= mproperty
.build_csignature
(recv_mtype
, v
.compiler
.mainmodule
, null, long_signature
, internal_call_context
)
367 nitni_visitor
.add_decl
("/* nitni callback for {mproperty.full_name} */")
368 nitni_visitor
.add_decl
("{csignature_blind} \{")
370 var vars
= new Array[RuntimeVariable]
371 var mtype
: MType = recv_mtype
373 if mproperty
.is_init
then
374 if recv_mtype
.mclass
.kind
== extern_kind
then
375 recv_var
= nitni_visitor
.new_var
(mtype
)
377 var recv_mtype
= recv_mtype
378 recv_var
= nitni_visitor
.init_instance
(recv_mtype
)
379 nitni_visitor
.add
("{mtype.ctype} recv /* var self: {mtype} */;")
380 nitni_visitor
.add
("recv = {recv_var};")
383 mtype
= mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
384 recv_var
= nitni_visitor
.var_from_c
("recv", mtype
)
389 for p
in msignature
.mparameters
do
390 var arg_mtype
= p
.mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
391 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
395 var ret_var
= nitni_visitor
.send
(mproperty
, vars
)
397 var return_mtype
= msignature
.return_mtype
398 if mproperty
.is_init
then
399 if recv_mtype
.mclass
.kind
!= extern_kind
then ret_var
= recv_var
400 return_mtype
= recv_mtype
402 if return_mtype
!= null then
403 assert ret_var
!= null
404 return_mtype
= return_mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
405 ret_var
= nitni_visitor
.autobox
(ret_var
, return_mtype
)
406 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
408 nitni_visitor
.add
("\}")
412 redef class MExplicitSuper
413 private fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
415 var mproperty
= from
.mproperty
416 assert mproperty
isa MMethod
417 var mclass_type
= from
.mclassdef
.mclass
.mclass_type
418 var mmodule
= from
.mclassdef
.mmodule
420 # In nitni files, declare internal function as extern
421 var internal_csignature
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
422 ccu
.header_decl
.add
("extern {internal_csignature};\n")
424 # In nitni files, #define friendly as extern
425 var friendly_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", short_signature
)
426 var internal_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
)
427 ccu
.header_decl
.add
("#define {friendly_cname} {internal_cname}\n")
429 # Internally, implement internal function
430 var nitni_visitor
= v
.compiler
.new_visitor
431 nitni_visitor
.frame
= v
.frame
432 var msignature
= mproperty
.lookup_first_definition
(v
.compiler
.mainmodule
, mclass_type
).msignature
434 var csignature_blind
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
436 nitni_visitor
.add_decl
("/* nitni callback to super for {mproperty.full_name} */")
437 nitni_visitor
.add_decl
("{csignature_blind} \{")
439 var vars
= new Array[RuntimeVariable]
441 var recv_var
= nitni_visitor
.var_from_c
("recv", mclass_type
)
444 for p
in msignature
.mparameters
do
445 var arg_mtype
= v
.anchor
(p
.mtype
)
446 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
450 var ret_var
= nitni_visitor
.supercall
(from
.as(MMethodDef), mclass_type
, vars
)
452 var return_mtype
= msignature
.return_mtype
453 if return_mtype
!= null then
454 assert ret_var
!= null
455 return_mtype
= v
.anchor
(return_mtype
)
456 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
458 nitni_visitor
.add
("\}")
462 redef class MExplicitCast
463 private fun compile_extern_callbacks
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
472 # In nitni files, declare internal function as extern
473 var full_friendly_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
474 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
476 # In nitni files, #define friendly as extern
477 ccu
.header_decl
.add
("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
479 # Internally, implement internal function
480 var nitni_visitor
= v
.compiler
.new_visitor
481 nitni_visitor
.frame
= v
.frame
483 var full_internal_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind} from)"
484 nitni_visitor
.add_decl
("/* nitni check for {from} to {to} */")
485 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
487 var from_var
= new RuntimeVariable("from->value", from
, from
)
488 var recv_var
= nitni_visitor
.type_test
(from_var
, to
, "FFI isa")
489 nitni_visitor
.add
("return {recv_var};")
491 nitni_visitor
.add
("\}")
494 if from
== to
.as_nullable
then
496 ccu
.header_decl
.add
("#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n")
503 # In nitni files, declare internal function as extern
504 full_friendly_csignature
= "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
505 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
507 # In nitni files, #define friendly as extern
508 ccu
.header_decl
.add
("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
510 # Internally, implement internal function
511 nitni_visitor
= v
.compiler
.new_visitor
512 nitni_visitor
.frame
= v
.frame
514 full_internal_csignature
= "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind} from)"
515 nitni_visitor
.add_decl
("/* nitni cast for {from} to {to} */")
516 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
518 from_var
= nitni_visitor
.var_from_c
("from", from
)
521 var check
= nitni_visitor
.type_test
(from_var
, to
, "FFI cast")
522 nitni_visitor
.add
("if (!{check}) \{")
523 nitni_visitor
.add_abort
("FFI cast failed")
524 nitni_visitor
.add
("\}")
527 recv_var
= nitni_visitor
.autobox
(from_var
, to
)
529 nitni_visitor
.ret_to_c
(recv_var
, to
)
531 nitni_visitor
.add
("\}")
534 if from
.as_nullable
== to
then
535 # format A_as_nullable
536 ccu
.header_decl
.add
("#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
539 if from
== to
.as_nullable
then
540 # format A_as_nullable
541 ccu
.header_decl
.add
("#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")