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
25 private var foreign_callbacks
= new ForeignCallbackSet
26 var nitni_ccu
: nullable CCompilationUnit = null
28 private fun nmodule
(v
: AbstractCompilerVisitor): nullable AModule
30 return v
.compiler
.modelbuilder
.mmodule2node
(self)
33 redef fun finalize_ffi
(compiler
: AbstractCompiler)
35 if not uses_ffi
then return
37 var v
= compiler
.new_visitor
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
)
44 ensure_compile_nitni_base
(v
)
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*);
52 var cflags
= self.cflags
[""].join
(" ")
53 nitni_ccu
.write_as_nitni
(self, v
.compiler
.modelbuilder
.compile_dir
)
55 for file
in nitni_ccu
.files
do
56 var f
= new ExternCFile(file
, cflags
)
57 f
.pkgconfigs
.add_all pkgconfigs
58 v
.compiler
.extern_bodies
.add
(f
)
61 # reset FFI things so the next compilation job, if any, starts with a clean context
62 # FIXME clean and rationalize this
64 compiled_ffi_methods
.clear
67 compiled_callbacks
.clear
68 #Do not reset `foreign_callbacks` and `ffi_callbacks` because they are computed in previous phases
71 private fun ensure_compile_nitni_base
(v
: AbstractCompilerVisitor)
73 if nitni_ccu
!= null then return
75 nitni_ccu
= new CCompilationUnit
78 redef fun collect_linker_libs
80 if not self.ldflags
.keys
.has
("") then return null
81 return self.ldflags
[""]
84 private var compiled_callbacks
= new Array[NitniCallback]
86 # Returns true if callbacks has yet to be generated and register it as being generated
87 private fun check_callback_compilation
(cb
: NitniCallback): Bool
89 var compiled
= compiled_callbacks
.has
(cb
)
90 if not compiled
then compiled_callbacks
.add
(cb
)
95 redef class AMethPropdef
96 private fun compile_ffi_support_to_c
(v
: AbstractCompilerVisitor)
98 var mmodule
= mpropdef
.mclassdef
.mmodule
99 var mainmodule
= v
.compiler
.mainmodule
100 var amodule
= v
.compiler
.modelbuilder
.mmodule2node
(mmodule
)
101 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
104 var csignature
= mpropdef
.mproperty
.build_csignature
(mclass_type
, mmodule
, "___impl", long_signature
, internal_call_context
)
105 v
.declare_once
("{csignature};")
108 amodule
.ensure_compile_ffi_wrapper
109 compile_ffi_method
(mmodule
)
111 # nitni - Compile missing callbacks
112 mmodule
.ensure_compile_nitni_base
(v
)
113 var ccu
= mmodule
.nitni_ccu
.as(not null)
115 for mtype
in foreign_callbacks
.types
do
116 if not mtype
.is_cprimitive
then
117 mtype
.compile_extern_type
(v
, ccu
)
119 # has callbacks already been compiled? (this may very well happen with global compilation)
120 mtype
.compile_extern_helper_functions
(v
, ccu
, mmodule
.check_callback_compilation
(mtype
))
125 for cb
in foreign_callbacks
.callbacks
do
126 cb
.compile_extern_callback
(v
, ccu
, mainmodule
.check_callback_compilation
(cb
))
129 for cb
in foreign_callbacks
.supers
do
130 cb
.compile_extern_callback
(v
, ccu
, mainmodule
.check_callback_compilation
(cb
))
133 for cb
in foreign_callbacks
.casts
do
134 cb
.compile_extern_callbacks
(v
, ccu
, mainmodule
.check_callback_compilation
(cb
))
137 # manage nitni callback set
138 mmodule
.foreign_callbacks
.join
(foreign_callbacks
)
141 redef fun compile_externmeth_to_c
(v
, mpropdef
, arguments
)
143 # if using the old native interface fallback on previous implementation
144 if n_extern_code_block
== null then return super
146 var mmodule
= mpropdef
.mclassdef
.mmodule
147 mmodule
.uses_ffi
= true
149 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
151 # Outgoing code in compiler
152 var externname
= mpropdef
.mproperty
.build_cname
(mpropdef
.mclassdef
.bound_mtype
, mmodule
, "___impl", long_signature
)
153 var recv_var
: nullable RuntimeVariable = null
154 var return_mtype
= mpropdef
.msignature
.return_mtype
155 if return_mtype
!= null then
156 return_mtype
= return_mtype
.anchor_to
(mmodule
, mclass_type
)
157 recv_var
= v
.new_var
(return_mtype
)
160 v
.adapt_signature
(mpropdef
, arguments
)
161 v
.unbox_signature_extern
(mpropdef
, arguments
)
163 var arguments_for_c
= new Array[String]
164 for a
in [0..arguments
.length
[ do
165 var arg
= arguments
[a
]
166 var param_mtype
: MType
168 param_mtype
= mpropdef
.mclassdef
.mclass
.mclass_type
169 else param_mtype
= mpropdef
.msignature
.mparameters
[a-1
].mtype
171 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
173 if param_mtype
.is_cprimitive
then
174 arguments_for_c
.add
(arg
.name
)
176 v
.add
("struct nitni_instance* var_for_c_{a};")
177 v
.add
("var_for_c_{a} = nit_alloc(sizeof(struct nitni_instance));")
178 v
.add
("var_for_c_{a}->value = {arg.name};")
179 arguments_for_c
.add
("var_for_c_{a}")
183 if recv_var
== null then
184 v
.add
("{externname}({arguments_for_c.join(", ")});")
186 assert return_mtype
!= null
187 if return_mtype
.is_cprimitive
then
188 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
190 v
.add
("struct nitni_instance* ret_var;")
191 v
.add
("ret_var = {externname}({arguments_for_c.join(", ")});")
192 v
.add
("{recv_var} = ret_var->value;")
194 recv_var
= v
.box_extern
(recv_var
, return_mtype
)
198 compile_ffi_support_to_c
(v
)
202 redef fun compile_externinit_to_c
(v
, mpropdef
, arguments
)
204 # if using the old native interface fallback on previous implementation
205 if n_extern_code_block
== null then return super
207 var mmodule
= mpropdef
.mclassdef
.mmodule
208 mmodule
.uses_ffi
= true
210 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
212 var externname
= mpropdef
.mproperty
.build_cname
(mpropdef
.mclassdef
.bound_mtype
, mmodule
, "___impl", long_signature
)
213 var return_mtype
= arguments
.first
.mtype
214 var recv_var
= v
.new_var
(return_mtype
)
216 v
.adapt_signature
(mpropdef
, arguments
)
217 v
.unbox_signature_extern
(mpropdef
, arguments
)
221 var arguments_for_c
= new Array[String]
222 for a
in [0..arguments
.length
[ do
223 var arg
= arguments
[a
]
224 var param_mtype
: MType
225 param_mtype
= mpropdef
.msignature
.mparameters
[a
].mtype
226 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
228 if param_mtype
.is_cprimitive
then
229 arguments_for_c
.add
(arg
.name
)
231 v
.add
("struct nitni_instance* var_for_c_{a};")
232 v
.add
("var_for_c_{a} = nit_alloc(sizeof(struct nitni_instance));")
233 v
.add
("var_for_c_{a}->value = {arg.name};")
234 arguments_for_c
.add
("var_for_c_{a}")
238 if return_mtype
.is_cprimitive
then
239 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
241 v
.add
("struct nitni_instance* ret_var;")
242 v
.add
("ret_var = {externname}({arguments_for_c.join(", ")});")
243 v
.add
("{recv_var} = ret_var->value;")
245 recv_var
= v
.box_extern
(recv_var
, return_mtype
)
248 compile_ffi_support_to_c
(v
)
253 redef class CCompilationUnit
254 fun write_as_nitni
(mmodule
: MModule, compdir
: String)
256 var base_name
= "{mmodule.c_name}._nitni"
258 var h_file
= "{base_name}.h"
259 write_header_to_file
( mmodule
, "{compdir}/{h_file}", new Array[String],
260 "{mmodule.c_name.to_s.to_upper}_NITG_NITNI_H")
262 var c_file
= "{base_name}.c"
263 write_body_to_file
( mmodule
, "{compdir}/{c_file}", ["\"{h_file}\
""] )
265 files
.add
( "{compdir}/{c_file}" )
269 redef class AbstractCompiler
270 # Cache to avoid multiple compilation of NULL values
271 # see FIXME in `MNullableType#compile_extern_helper_functions`
272 private var compiled_null_types
= new Array[MNullableType]
275 redef class AbstractCompilerVisitor
276 # Create a `RuntimeVariable` for this C variable originating from C user code
277 private fun var_from_c
(name
: String, mtype
: MType): RuntimeVariable
279 if mtype
.is_cprimitive
then
280 return new RuntimeVariable(name
, mtype
, mtype
)
282 return new RuntimeVariable("{name}->value", mtype
, mtype
)
286 # Return a `RuntimeVarible` to C user code
287 private fun ret_to_c
(src
: RuntimeVariable, mtype
: MType)
289 if mtype
.is_cprimitive
then
292 add
("struct nitni_instance* ret_for_c;")
293 add
("ret_for_c = nit_alloc(sizeof(struct nitni_instance));")
294 add
("ret_for_c->value = {src};")
295 add
("return ret_for_c;")
301 private fun compile_extern_type
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
303 assert not is_cprimitive
305 # define friendly type
306 ccu
.header_c_types
.add
("#ifndef NIT_TYPE_{cname}\n")
307 ccu
.header_c_types
.add
("#define NIT_TYPE_{cname} 1\n")
308 ccu
.header_c_types
.add
("typedef struct nitni_instance *{cname};\n")
309 ccu
.header_c_types
.add
("#endif\n")
312 private fun compile_extern_helper_functions
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit, compile_implementation_too
: Bool)
314 # actually, we do not need to do anything when using the bohem garbage collector
315 var call_context
= from_c_call_context
318 ccu
.header_decl
.add
"#ifndef {mangled_cname}_incr_ref\n"
319 ccu
.header_decl
.add
" #define {mangled_cname}_incr_ref(from) nitni_global_ref_incr(({call_context.name_mtype(self)})(from))\n"
320 ccu
.header_decl
.add
"#endif\n"
323 ccu
.header_decl
.add
"#ifndef {mangled_cname}_decr_ref\n"
324 ccu
.header_decl
.add
" #define {mangled_cname}_decr_ref(from) nitni_global_ref_decr(({call_context.name_mtype(self)})(from))\n"
325 ccu
.header_decl
.add
"#endif\n"
329 redef class MNullableType
330 redef fun compile_extern_helper_functions
(v
, ccu
, compile_implementation_too
)
334 var base_cname
= "null_{mtype.mangled_cname}"
335 var full_cname
= "NIT_NULL___{base_cname}"
337 # In nitni files, declare internal function as extern
338 var full_friendly_csignature
= "{cname_blind} {full_cname}()"
339 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
341 # In nitni files, #define friendly as extern
342 ccu
.header_decl
.add
("#define {base_cname} {full_cname}\n")
344 if not compile_implementation_too
then return
346 # FIXME: This is ugly an broke the separate compilation principle
347 # The real function MUST be compiled only once, #define pragma only protect the compiler, not the loader
348 # However, I am not sure of the right approach here (eg. week refs are ugly)
349 if v
.compiler
.compiled_null_types
.has
(self) then return
350 v
.compiler
.compiled_null_types
.add
self
352 # Internally, implement internal function
353 var nitni_visitor
= v
.compiler
.new_visitor
354 nitni_visitor
.frame
= v
.frame
355 var full_internal_csignature
= "{cname_blind} {full_cname}()"
356 nitni_visitor
.add
("{full_internal_csignature} \{")
357 nitni_visitor
.add
("struct nitni_instance* ret_for_c;")
358 nitni_visitor
.add
("ret_for_c = nit_alloc(sizeof(struct nitni_instance));")
359 nitni_visitor
.add
("ret_for_c->value = NULL;")
360 nitni_visitor
.add
("return ret_for_c;")
361 nitni_visitor
.add
("\}")
365 redef class MExplicitCall
366 private fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit, compile_implementation_too
: Bool)
368 var mproperty
= mproperty
369 assert mproperty
isa MMethod
371 # In nitni files, declare internal function as extern
372 var full_friendly_csignature
= mproperty
.build_csignature
(recv_mtype
, v
.compiler
.mainmodule
, null, long_signature
, internal_call_context
)
373 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
375 if not compile_implementation_too
then return
377 # Internally, implement internal function
378 var nitni_visitor
= v
.compiler
.new_visitor
379 nitni_visitor
.frame
= v
.frame
380 var msignature
= mproperty
.lookup_first_definition
(v
.compiler
.mainmodule
, recv_mtype
).msignature
381 var csignature_blind
= mproperty
.build_csignature
(recv_mtype
, v
.compiler
.mainmodule
, null, long_signature
, internal_call_context
)
383 nitni_visitor
.add_decl
("/* nitni callback for {mproperty.full_name} */")
384 nitni_visitor
.add_decl
("{csignature_blind} \{")
386 var vars
= new Array[RuntimeVariable]
387 var mtype
: MType = recv_mtype
389 if mproperty
.is_init
then
390 var recv_mtype
= recv_mtype
391 recv_var
= nitni_visitor
.init_instance_or_extern
(recv_mtype
)
392 nitni_visitor
.add
("{mtype.ctype} recv /* var self: {mtype} */;")
393 nitni_visitor
.add
("recv = {recv_var};")
395 mtype
= mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
396 recv_var
= nitni_visitor
.var_from_c
("recv", mtype
)
397 recv_var
= nitni_visitor
.box_extern
(recv_var
, mtype
)
402 for p
in msignature
.mparameters
do
403 var arg_mtype
= p
.mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
404 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
405 arg
= nitni_visitor
.box_extern
(arg
, arg_mtype
)
409 var ret_var
= nitni_visitor
.send
(mproperty
, vars
)
411 var return_mtype
= msignature
.return_mtype
412 if mproperty
.is_init
then
413 if recv_mtype
.mclass
.kind
!= extern_kind
then ret_var
= recv_var
414 return_mtype
= recv_mtype
416 if return_mtype
!= null then
417 assert ret_var
!= null
418 return_mtype
= return_mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
419 ret_var
= nitni_visitor
.autobox
(ret_var
, return_mtype
)
420 ret_var
= nitni_visitor
.unbox_extern
(ret_var
, return_mtype
)
421 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
423 nitni_visitor
.add
("\}")
427 redef class MExplicitSuper
428 private fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit, compile_implementation_too
: Bool)
430 var mproperty
= from
.mproperty
431 assert mproperty
isa MMethod
432 var mclass_type
= from
.mclassdef
.mclass
.mclass_type
434 # In nitni files, declare internal function as extern
435 var internal_csignature
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
436 ccu
.header_decl
.add
("extern {internal_csignature};\n")
438 # In nitni files, #define friendly as extern
439 var friendly_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", short_signature
)
440 var internal_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
)
441 ccu
.header_decl
.add
("#define {friendly_cname} {internal_cname}\n")
443 if not compile_implementation_too
then return
445 # Internally, implement internal function
446 var nitni_visitor
= v
.compiler
.new_visitor
447 nitni_visitor
.frame
= v
.frame
448 var msignature
= mproperty
.lookup_first_definition
(v
.compiler
.mainmodule
, mclass_type
).msignature
450 var csignature_blind
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
452 nitni_visitor
.add_decl
("/* nitni callback to super for {mproperty.full_name} */")
453 nitni_visitor
.add_decl
("{csignature_blind} \{")
455 var vars
= new Array[RuntimeVariable]
457 var recv_var
= nitni_visitor
.var_from_c
("recv", mclass_type
)
458 recv_var
= nitni_visitor
.box_extern
(recv_var
, mclass_type
)
461 for p
in msignature
.mparameters
do
462 var arg_mtype
= v
.anchor
(p
.mtype
)
463 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
464 arg
= nitni_visitor
.box_extern
(arg
, arg_mtype
)
468 var ret_var
= nitni_visitor
.supercall
(from
.as(MMethodDef), mclass_type
, vars
)
470 var return_mtype
= msignature
.return_mtype
471 if return_mtype
!= null then
472 assert ret_var
!= null
473 return_mtype
= v
.anchor
(return_mtype
)
474 ret_var
= nitni_visitor
.autobox
(ret_var
, return_mtype
)
475 ret_var
= nitni_visitor
.unbox_extern
(ret_var
, return_mtype
)
476 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
478 nitni_visitor
.add
("\}")
482 redef class MExplicitCast
483 private fun compile_extern_callbacks
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit, compile_implementation_too
: Bool)
492 # In nitni files, declare internal function as extern
493 var full_friendly_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
494 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
496 # In nitni files, #define friendly as extern
497 ccu
.header_decl
.add
("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
499 if compile_implementation_too
then
500 # Internally, implement internal function
501 var nitni_visitor
= v
.compiler
.new_visitor
502 nitni_visitor
.frame
= v
.frame
504 var full_internal_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
506 nitni_visitor
.add_decl
("/* nitni check for {from} to {to} */")
507 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
509 var from_var
= nitni_visitor
.var_from_c
("from", from
)
510 from_var
= nitni_visitor
.box_extern
(from_var
, from
)
511 var recv_var
= nitni_visitor
.type_test
(from_var
, to
, "FFI isa")
512 nitni_visitor
.add
("return {recv_var};")
514 nitni_visitor
.add
("\}")
518 if from
== to
.as_nullable
then
520 ccu
.header_decl
.add
("#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n")
527 # In nitni files, declare internal function as extern
528 full_friendly_csignature
= "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
529 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
531 # In nitni files, #define friendly as extern
532 ccu
.header_decl
.add
("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
534 if compile_implementation_too
then
535 # Internally, implement internal function
536 var nitni_visitor
= v
.compiler
.new_visitor
537 nitni_visitor
.frame
= v
.frame
539 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)"
540 nitni_visitor
.add_decl
("/* nitni cast for {from} to {to} */")
541 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
543 var from_var
= nitni_visitor
.var_from_c
("from", from
)
544 from_var
= nitni_visitor
.box_extern
(from_var
, from
)
547 var check
= nitni_visitor
.type_test
(from_var
, to
, "FFI cast")
548 nitni_visitor
.add
("if (!{check}) \{")
549 nitni_visitor
.add_abort
("FFI cast failed")
550 nitni_visitor
.add
("\}")
553 var recv_var
= nitni_visitor
.autobox
(from_var
, to
)
554 recv_var
= nitni_visitor
.unbox_extern
(recv_var
, to
)
556 nitni_visitor
.ret_to_c
(recv_var
, to
)
558 nitni_visitor
.add
("\}")
562 if from
.as_nullable
== to
then
563 # format A_as_nullable
564 ccu
.header_decl
.add
("#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
567 if from
== to
.as_nullable
then
568 # format A_as_nullable
569 ccu
.header_decl
.add
("#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")