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 redef var uses_legacy_ni
: Bool = false
30 redef fun finalize_ffi
(v
: AbstractCompilerVisitor, modelbuilder
: ModelBuilder)
32 finalize_ffi_wrapper
(".nit_compile", v
.compiler
.mainmodule
)
33 for file
in ffi_files
do v
.compiler
.extern_bodies
.add
(file
)
36 fun ensure_compile_nitni_base
(v
: AbstractCompilerVisitor)
38 if nitni_ccu
!= null then return
40 nitni_ccu
= new CCompilationUnit
43 redef fun finalize_nitni
(v
: AbstractCompilerVisitor)
45 ensure_compile_nitni_base
(v
)
47 nitni_ccu
.header_c_types
.add
("#include \"{mmodule.name}._ffi
.h\
"\n")
49 nitni_ccu
.write_as_nitni
(self, ".nit_compile")
51 for file
in nitni_ccu
.files
do
52 v
.compiler
.extern_bodies
.add
(new ExternCFile(file
, c_compiler_options
))
56 var compiled_callbacks
: Array[NitniCallback] = new Array[NitniCallback]
58 # Returns true if callbacks has yet to be generated and register it as being generated
59 fun check_callback_compilation
(cb
: NitniCallback): Bool
61 var compiled
= compiled_callbacks
.has
(cb
)
62 if not compiled
then compiled_callbacks
.add
(cb
)
67 redef class AExternPropdef
68 fun compile_ffi_support_to_c
(v
: AbstractCompilerVisitor)
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
76 var csignature
= mpropdef
.mproperty
.build_csignature
(mclass_type
, mmodule
, "___impl", long_signature
, internal_call_context
)
77 v
.declare_once
("{csignature};")
80 compile_ffi_method
(amodule
)
82 # nitni - Compile missing callbacks
83 amodule
.ensure_compile_nitni_base
(v
)
84 var ccu
= amodule
.nitni_ccu
.as(not null)
86 for mtype
in foreign_callbacks
.types
do
87 if not mtype
.is_cprimitive
then
88 mtype
.compile_extern_type
(v
, ccu
)
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
)
96 for cb
in foreign_callbacks
.callbacks
do if amainmodule
.check_callback_compilation
(cb
) then
97 cb
.compile_extern_callback
(v
, ccu
)
100 for cb
in foreign_callbacks
.supers
do if amainmodule
.check_callback_compilation
(cb
) then
101 cb
.compile_extern_callback
(v
, ccu
)
104 for cb
in foreign_callbacks
.casts
do if amainmodule
.check_callback_compilation
(cb
) then
105 cb
.compile_extern_callbacks
(v
, ccu
)
108 # manage nitni callback set
109 amodule
.foreign_callbacks
.join
(foreign_callbacks
)
113 redef class AExternMethPropdef
114 redef fun compile_to_c
(v
, mpropdef
, arguments
)
116 var mmodule
= mpropdef
.mclassdef
.mmodule
117 var amodule
= v
.compiler
.modelbuilder
.mmodule2nmodule
[mmodule
]
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
127 amodule
.mmodule
.uses_ffi
= true
129 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
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
)
140 v
.adapt_signature
(mpropdef
, arguments
)
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
147 param_mtype
= mpropdef
.mclassdef
.mclass
.mclass_type
148 else param_mtype
= mpropdef
.msignature
.mparameters
[a-1
].mtype
150 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
152 if param_mtype
.is_cprimitive
then
153 arguments_for_c
.add
(arg
.name
)
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}")
162 if recv_var
== null then
163 v
.add
("{externname}({arguments_for_c.join(", ")});")
165 assert return_mtype
!= null
166 if return_mtype
.is_cprimitive
then
167 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
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;")
176 compile_ffi_support_to_c
(v
)
180 redef class AExternInitPropdef
181 redef fun compile_to_c
(v
, mpropdef
, arguments
)
183 var mmodule
= mpropdef
.mclassdef
.mmodule
184 var amodule
= v
.compiler
.modelbuilder
.mmodule2nmodule
[mmodule
]
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
194 amodule
.mmodule
.uses_ffi
= true
196 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
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
)
202 v
.adapt_signature
(mpropdef
, arguments
)
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
)
213 if param_mtype
.is_cprimitive
then
214 arguments_for_c
.add
(arg
.name
)
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}")
223 if return_mtype
.is_cprimitive
then
224 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
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;")
232 compile_ffi_support_to_c
(v
)
236 redef class CCompilationUnit
237 fun write_as_nitni
(amodule
: AModule, compdir
: String)
239 var base_name
= "{amodule.mmodule.name}._nitni"
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")
245 var c_file
= "{base_name}.c"
246 write_body_to_file
( amodule
, "{compdir}/{c_file}", ["\"{h_file}\
""] )
248 files
.add
( "{compdir}/{c_file}" )
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
256 if mtype
.is_cprimitive
then
257 return new RuntimeVariable(name
, mtype
, mtype
)
259 return new RuntimeVariable("{name}->value", mtype
, mtype
)
263 # Return a `RuntimeVarible` to C user code
264 private fun ret_to_c
(src
: RuntimeVariable, mtype
: MType)
266 if mtype
.is_cprimitive
then
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;")
278 fun compile_extern_type
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
280 assert not is_cprimitive
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")
289 fun compile_extern_helper_functions
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
291 # actually, we do not need to do anything when using the bohem garbage collector
294 var nitni_visitor
= v
.compiler
.new_visitor
295 ccu
.header_decl
.add
("#define {mangled_cname}_incr_ref(from) while(0)\{\}\n")
298 nitni_visitor
= v
.compiler
.new_visitor
299 ccu
.header_decl
.add
("#define {mangled_cname}_decr_ref(from) while(0)\{\}\n")
303 redef class MNullableType
304 redef fun compile_extern_helper_functions
(v
, ccu
)
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")
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")
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")
332 redef class MExplicitCall
333 fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
335 var mproperty
= mproperty
336 assert mproperty
isa MMethod
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")
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
)
348 nitni_visitor
.add_decl
("/* nitni callback for {mproperty.full_name} */")
349 nitni_visitor
.add_decl
("{csignature_blind} \{")
351 var vars
= new Array[RuntimeVariable]
352 var mtype
: MType = recv_mtype
354 if mproperty
.is_init
then
355 if recv_mtype
.mclass
.kind
== extern_kind
then
356 recv_var
= nitni_visitor
.new_var
(mtype
)
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};")
364 mtype
= mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
365 recv_var
= nitni_visitor
.var_from_c
("recv", mtype
)
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
)
376 var ret_var
= nitni_visitor
.send
(mproperty
, vars
)
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
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
)
388 nitni_visitor
.add
("\}")
392 redef class MExplicitSuper
393 fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
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
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")
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")
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
414 var csignature_blind
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
416 nitni_visitor
.add_decl
("/* nitni callback to super for {mproperty.full_name} */")
417 nitni_visitor
.add_decl
("{csignature_blind} \{")
419 var vars
= new Array[RuntimeVariable]
421 var recv_var
= nitni_visitor
.var_from_c
("recv", mclass_type
)
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
)
430 var ret_var
= nitni_visitor
.supercall
(from
.as(MMethodDef), mclass_type
, vars
)
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
)
438 nitni_visitor
.add
("\}")
442 redef class MExplicitCast
443 fun compile_extern_callbacks
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
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")
456 # In nitni files, #define friendly as extern
457 ccu
.header_decl
.add
("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
459 # Internally, implement internal function
460 var nitni_visitor
= v
.compiler
.new_visitor
461 nitni_visitor
.frame
= v
.frame
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} \{")
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};")
471 nitni_visitor
.add
("\}")
474 if from
== to
.as_nullable
then
476 ccu
.header_decl
.add
("#define {from.mangled_cname}_is_null {from.mangled_cname}_is_a_{to.mangled_cname}\n")
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")
487 # In nitni files, #define friendly as extern
488 ccu
.header_decl
.add
("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
490 # Internally, implement internal function
491 nitni_visitor
= v
.compiler
.new_visitor
492 nitni_visitor
.frame
= v
.frame
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} \{")
498 from_var
= nitni_visitor
.var_from_c
("from", from
)
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
("\}")
507 recv_var
= nitni_visitor
.autobox
(from_var
, to
)
509 nitni_visitor
.ret_to_c
(recv_var
, to
)
511 nitni_visitor
.add
("\}")
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")
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")