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
))
55 # reset FFI things so the next compilation job, if any, starts with a clean context
56 # FIXME clean and rationalize this
58 compiled_ffi_methods
.clear
61 compiled_callbacks
.clear
62 #Do not reset `foreign_callbacks` and `ffi_callbacks` because they are computed in previous phases
65 private fun ensure_compile_nitni_base
(v
: AbstractCompilerVisitor)
67 if nitni_ccu
!= null then return
69 nitni_ccu
= new CCompilationUnit
72 redef fun collect_linker_libs
74 var s
= c_linker_options
75 if s
.is_empty
then return null
76 var res
= new ArraySet[String]
81 private var compiled_callbacks
: Array[NitniCallback] = new Array[NitniCallback]
83 # Returns true if callbacks has yet to be generated and register it as being generated
84 private fun check_callback_compilation
(cb
: NitniCallback): Bool
86 var compiled
= compiled_callbacks
.has
(cb
)
87 if not compiled
then compiled_callbacks
.add
(cb
)
92 redef class AMethPropdef
93 private fun compile_ffi_support_to_c
(v
: AbstractCompilerVisitor)
95 var mmodule
= mpropdef
.mclassdef
.mmodule
96 var mainmodule
= v
.compiler
.mainmodule
97 var amainmodule
= v
.compiler
.modelbuilder
.mmodule2nmodule
[mainmodule
]
98 var amodule
= v
.compiler
.modelbuilder
.mmodule2nmodule
[mmodule
]
99 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
102 var csignature
= mpropdef
.mproperty
.build_csignature
(mclass_type
, mmodule
, "___impl", long_signature
, internal_call_context
)
103 v
.declare_once
("{csignature};")
106 amodule
.ensure_compile_ffi_wrapper
107 compile_ffi_method
(mmodule
)
109 assert self isa AExternPropdef
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 if mmodule
.check_callback_compilation
(mtype
) then mtype
.compile_extern_helper_functions
(v
, ccu
)
125 for cb
in foreign_callbacks
.callbacks
do if mainmodule
.check_callback_compilation
(cb
) then
126 cb
.compile_extern_callback
(v
, ccu
)
129 for cb
in foreign_callbacks
.supers
do if mainmodule
.check_callback_compilation
(cb
) then
130 cb
.compile_extern_callback
(v
, ccu
)
133 for cb
in foreign_callbacks
.casts
do if mainmodule
.check_callback_compilation
(cb
) then
134 cb
.compile_extern_callbacks
(v
, ccu
)
137 # manage nitni callback set
138 mmodule
.foreign_callbacks
.join
(foreign_callbacks
)
141 redef fun compile_externmeth_to_c
(v
, mpropdef
, arguments
)
143 var mmodule
= mpropdef
.mclassdef
.mmodule
145 # if using the old native interface fallback on previous implementation
146 var nextern
= self.n_extern
147 if nextern
!= null then
152 mmodule
.uses_ffi
= true
154 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
156 # Outgoing code in compiler
157 var externname
= mpropdef
.mproperty
.build_cname
(mpropdef
.mclassdef
.bound_mtype
, mmodule
, "___impl", long_signature
)
158 var recv_var
: nullable RuntimeVariable = null
159 var return_mtype
= mpropdef
.msignature
.return_mtype
160 if return_mtype
!= null then
161 return_mtype
= return_mtype
.anchor_to
(mmodule
, mclass_type
)
162 recv_var
= v
.new_var
(return_mtype
)
165 v
.adapt_signature
(mpropdef
, arguments
)
167 var arguments_for_c
= new Array[String]
168 for a
in [0..arguments
.length
[ do
169 var arg
= arguments
[a
]
170 var param_mtype
: MType
172 param_mtype
= mpropdef
.mclassdef
.mclass
.mclass_type
173 else param_mtype
= mpropdef
.msignature
.mparameters
[a-1
].mtype
175 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
177 if param_mtype
.is_cprimitive
then
178 arguments_for_c
.add
(arg
.name
)
180 v
.add
("struct nitni_instance* var_for_c_{a};")
181 v
.add
("var_for_c_{a} = malloc(sizeof(struct nitni_instance));")
182 v
.add
("var_for_c_{a}->value = {arg.name};")
183 arguments_for_c
.add
("var_for_c_{a}")
187 if recv_var
== null then
188 v
.add
("{externname}({arguments_for_c.join(", ")});")
190 assert return_mtype
!= null
191 if return_mtype
.is_cprimitive
then
192 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
194 v
.add
("struct nitni_instance* ret_var;")
195 v
.add
("ret_var = {externname}({arguments_for_c.join(", ")});")
196 v
.add
("{recv_var} = ret_var->value;")
201 compile_ffi_support_to_c
(v
)
204 redef fun compile_externinit_to_c
(v
, mpropdef
, arguments
)
206 var mmodule
= mpropdef
.mclassdef
.mmodule
208 # if using the old native interface fallback on previous implementation
209 var nextern
= self.n_extern
210 if nextern
!= null then
215 mmodule
.uses_ffi
= true
217 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
219 var externname
= mpropdef
.mproperty
.build_cname
(mpropdef
.mclassdef
.bound_mtype
, mmodule
, "___impl", long_signature
)
220 var return_mtype
= arguments
.first
.mtype
221 var recv_var
= v
.new_var
(return_mtype
)
223 v
.adapt_signature
(mpropdef
, arguments
)
227 var arguments_for_c
= new Array[String]
228 for a
in [0..arguments
.length
[ do
229 var arg
= arguments
[a
]
230 var param_mtype
: MType
231 param_mtype
= mpropdef
.msignature
.mparameters
[a
].mtype
232 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
234 if param_mtype
.is_cprimitive
then
235 arguments_for_c
.add
(arg
.name
)
237 v
.add
("struct nitni_instance* var_for_c_{a};")
238 v
.add
("var_for_c_{a} = malloc(sizeof(struct nitni_instance));")
239 v
.add
("var_for_c_{a}->value = {arg.name};")
240 arguments_for_c
.add
("var_for_c_{a}")
244 if return_mtype
.is_cprimitive
then
245 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
247 v
.add
("struct nitni_instance* ret_var;")
248 v
.add
("ret_var = {externname}({arguments_for_c.join(", ")});")
249 v
.add
("{recv_var} = ret_var->value;")
253 compile_ffi_support_to_c
(v
)
257 redef class CCompilationUnit
258 fun write_as_nitni
(mmodule
: MModule, compdir
: String)
260 var base_name
= "{mmodule.name}._nitni"
262 var h_file
= "{base_name}.h"
263 write_header_to_file
( mmodule
, "{compdir}/{h_file}", new Array[String],
264 "{mmodule.cname.to_s.to_upper}_NITG_NITNI_H")
266 var c_file
= "{base_name}.c"
267 write_body_to_file
( mmodule
, "{compdir}/{c_file}", ["\"{h_file}\
""] )
269 files
.add
( "{compdir}/{c_file}" )
273 redef class AbstractCompiler
274 # Cache to avoid multiple compilation of NULL values
275 # see FIXME in `MNullableType#compile_extern_helper_functions`
276 private var compiled_null_types
= new Array[MNullableType]
279 redef class AbstractCompilerVisitor
280 # Create a `RuntimeVariable` for this C variable originating from C user code
281 private fun var_from_c
(name
: String, mtype
: MType): RuntimeVariable
283 if mtype
.is_cprimitive
then
284 return new RuntimeVariable(name
, mtype
, mtype
)
286 return new RuntimeVariable("{name}->value", mtype
, mtype
)
290 # Return a `RuntimeVarible` to C user code
291 private fun ret_to_c
(src
: RuntimeVariable, mtype
: MType)
293 if mtype
.is_cprimitive
then
296 add
("struct nitni_instance* ret_for_c;")
297 add
("ret_for_c = malloc(sizeof(struct nitni_instance));")
298 add
("ret_for_c->value = {src};")
299 add
("return ret_for_c;")
305 private fun compile_extern_type
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
307 assert not is_cprimitive
309 # define friendly type
310 ccu
.header_c_types
.add
("#ifndef NIT_TYPE_{cname}\n")
311 ccu
.header_c_types
.add
("#define NIT_TYPE_{cname} 1\n")
312 ccu
.header_c_types
.add
("typedef struct nitni_instance *{cname};\n")
313 ccu
.header_c_types
.add
("#endif\n")
316 private fun compile_extern_helper_functions
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
318 # actually, we do not need to do anything when using the bohem garbage collector
321 var nitni_visitor
= v
.compiler
.new_visitor
322 ccu
.header_decl
.add
("#define {mangled_cname}_incr_ref(from) while(0)\{\}\n")
325 nitni_visitor
= v
.compiler
.new_visitor
326 ccu
.header_decl
.add
("#define {mangled_cname}_decr_ref(from) while(0)\{\}\n")
330 redef class MNullableType
331 redef fun compile_extern_helper_functions
(v
, ccu
)
335 var base_cname
= "null_{mtype.mangled_cname}"
336 var full_cname
= "NIT_NULL___{base_cname}"
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")
342 # In nitni files, #define friendly as extern
343 ccu
.header_decl
.add
("#define {base_cname} {full_cname}\n")
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
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 = malloc(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
("\}")
364 redef class MExplicitCall
365 private fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
367 var mproperty
= mproperty
368 assert mproperty
isa MMethod
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")
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
)
380 nitni_visitor
.add_decl
("/* nitni callback for {mproperty.full_name} */")
381 nitni_visitor
.add_decl
("{csignature_blind} \{")
383 var vars
= new Array[RuntimeVariable]
384 var mtype
: MType = recv_mtype
386 if mproperty
.is_init
then
387 if recv_mtype
.mclass
.kind
== extern_kind
then
388 recv_var
= nitni_visitor
.new_var
(mtype
)
390 var recv_mtype
= recv_mtype
391 recv_var
= nitni_visitor
.init_instance
(recv_mtype
)
392 nitni_visitor
.add
("{mtype.ctype} recv /* var self: {mtype} */;")
393 nitni_visitor
.add
("recv = {recv_var};")
396 mtype
= mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
397 recv_var
= nitni_visitor
.var_from_c
("recv", 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
)
408 var ret_var
= nitni_visitor
.send
(mproperty
, vars
)
410 var return_mtype
= msignature
.return_mtype
411 if mproperty
.is_init
then
412 if recv_mtype
.mclass
.kind
!= extern_kind
then ret_var
= recv_var
413 return_mtype
= recv_mtype
415 if return_mtype
!= null then
416 assert ret_var
!= null
417 return_mtype
= return_mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
418 ret_var
= nitni_visitor
.autobox
(ret_var
, return_mtype
)
419 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
421 nitni_visitor
.add
("\}")
425 redef class MExplicitSuper
426 private fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
428 var mproperty
= from
.mproperty
429 assert mproperty
isa MMethod
430 var mclass_type
= from
.mclassdef
.mclass
.mclass_type
431 var mmodule
= from
.mclassdef
.mmodule
433 # In nitni files, declare internal function as extern
434 var internal_csignature
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
435 ccu
.header_decl
.add
("extern {internal_csignature};\n")
437 # In nitni files, #define friendly as extern
438 var friendly_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", short_signature
)
439 var internal_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
)
440 ccu
.header_decl
.add
("#define {friendly_cname} {internal_cname}\n")
442 # Internally, implement internal function
443 var nitni_visitor
= v
.compiler
.new_visitor
444 nitni_visitor
.frame
= v
.frame
445 var msignature
= mproperty
.lookup_first_definition
(v
.compiler
.mainmodule
, mclass_type
).msignature
447 var csignature_blind
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
449 nitni_visitor
.add_decl
("/* nitni callback to super for {mproperty.full_name} */")
450 nitni_visitor
.add_decl
("{csignature_blind} \{")
452 var vars
= new Array[RuntimeVariable]
454 var recv_var
= nitni_visitor
.var_from_c
("recv", mclass_type
)
457 for p
in msignature
.mparameters
do
458 var arg_mtype
= v
.anchor
(p
.mtype
)
459 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
463 var ret_var
= nitni_visitor
.supercall
(from
.as(MMethodDef), mclass_type
, vars
)
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 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
471 nitni_visitor
.add
("\}")
475 redef class MExplicitCast
476 private fun compile_extern_callbacks
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
485 # In nitni files, declare internal function as extern
486 var full_friendly_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
487 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
489 # In nitni files, #define friendly as extern
490 ccu
.header_decl
.add
("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
492 # Internally, implement internal function
493 var nitni_visitor
= v
.compiler
.new_visitor
494 nitni_visitor
.frame
= v
.frame
496 var full_internal_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind} from)"
497 nitni_visitor
.add_decl
("/* nitni check for {from} to {to} */")
498 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
500 var from_var
= new RuntimeVariable("from->value", from
, from
)
501 var recv_var
= nitni_visitor
.type_test
(from_var
, to
, "FFI isa")
502 nitni_visitor
.add
("return {recv_var};")
504 nitni_visitor
.add
("\}")
507 if from
== to
.as_nullable
then
509 ccu
.header_decl
.add
("#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n")
516 # In nitni files, declare internal function as extern
517 full_friendly_csignature
= "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
518 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
520 # In nitni files, #define friendly as extern
521 ccu
.header_decl
.add
("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
523 # Internally, implement internal function
524 nitni_visitor
= v
.compiler
.new_visitor
525 nitni_visitor
.frame
= v
.frame
527 full_internal_csignature
= "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind} from)"
528 nitni_visitor
.add_decl
("/* nitni cast for {from} to {to} */")
529 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
531 from_var
= nitni_visitor
.var_from_c
("from", from
)
534 var check
= nitni_visitor
.type_test
(from_var
, to
, "FFI cast")
535 nitni_visitor
.add
("if (!{check}) \{")
536 nitni_visitor
.add_abort
("FFI cast failed")
537 nitni_visitor
.add
("\}")
540 recv_var
= nitni_visitor
.autobox
(from_var
, to
)
542 nitni_visitor
.ret_to_c
(recv_var
, to
)
544 nitni_visitor
.add
("\}")
547 if from
.as_nullable
== to
then
548 # format A_as_nullable
549 ccu
.header_decl
.add
("#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
552 if from
== to
.as_nullable
then
553 # format A_as_nullable
554 ccu
.header_decl
.add
("#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")