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
(v
.compiler
.modelbuilder
.compile_dir
, 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, 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 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 if not v
.compiler
.supports_ffi
then
132 amodule
.mmodule
.uses_ffi
= true
134 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
136 # Outgoing code in compiler
137 var externname
= mpropdef
.mproperty
.build_cname
(mpropdef
.mclassdef
.bound_mtype
, mmodule
, "___impl", long_signature
)
138 var recv_var
: nullable RuntimeVariable = null
139 var return_mtype
= mpropdef
.msignature
.return_mtype
140 if return_mtype
!= null then
141 return_mtype
= return_mtype
.anchor_to
(mmodule
, mclass_type
)
142 recv_var
= v
.new_var
(return_mtype
)
145 v
.adapt_signature
(mpropdef
, arguments
)
147 var arguments_for_c
= new Array[String]
148 for a
in [0..arguments
.length
[ do
149 var arg
= arguments
[a
]
150 var param_mtype
: MType
152 param_mtype
= mpropdef
.mclassdef
.mclass
.mclass_type
153 else param_mtype
= mpropdef
.msignature
.mparameters
[a-1
].mtype
155 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
157 if param_mtype
.is_cprimitive
then
158 arguments_for_c
.add
(arg
.name
)
160 v
.add
("struct nitni_instance* var_for_c_{a};")
161 v
.add
("var_for_c_{a} = malloc(sizeof(struct nitni_instance));")
162 v
.add
("var_for_c_{a}->value = {arg.name};")
163 arguments_for_c
.add
("var_for_c_{a}")
167 if recv_var
== null then
168 v
.add
("{externname}({arguments_for_c.join(", ")});")
170 assert return_mtype
!= null
171 if return_mtype
.is_cprimitive
then
172 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
174 v
.add
("struct nitni_instance* ret_var;")
175 v
.add
("ret_var = {externname}({arguments_for_c.join(", ")});")
176 v
.add
("{recv_var} = ret_var->value;")
181 compile_ffi_support_to_c
(v
)
185 redef class AExternInitPropdef
186 redef fun compile_to_c
(v
, mpropdef
, arguments
)
188 var mmodule
= mpropdef
.mclassdef
.mmodule
189 var amodule
= v
.compiler
.modelbuilder
.mmodule2nmodule
[mmodule
]
191 # if using the old native interface fallback on previous implementation
192 var nextern
= self.n_extern
193 if nextern
!= null then
194 amodule
.uses_legacy_ni
= true
199 if not v
.compiler
.supports_ffi
then
204 amodule
.mmodule
.uses_ffi
= true
206 var mclass_type
= mpropdef
.mclassdef
.bound_mtype
208 var externname
= mpropdef
.mproperty
.build_cname
(mpropdef
.mclassdef
.bound_mtype
, mmodule
, "___impl", long_signature
)
209 var return_mtype
= arguments
.first
.mtype
210 var recv_var
= v
.new_var
(return_mtype
)
212 v
.adapt_signature
(mpropdef
, arguments
)
216 var arguments_for_c
= new Array[String]
217 for a
in [0..arguments
.length
[ do
218 var arg
= arguments
[a
]
219 var param_mtype
: MType
220 param_mtype
= mpropdef
.msignature
.mparameters
[a
].mtype
221 param_mtype
= param_mtype
.anchor_to
(mmodule
, mclass_type
)
223 if param_mtype
.is_cprimitive
then
224 arguments_for_c
.add
(arg
.name
)
226 v
.add
("struct nitni_instance* var_for_c_{a};")
227 v
.add
("var_for_c_{a} = malloc(sizeof(struct nitni_instance));")
228 v
.add
("var_for_c_{a}->value = {arg.name};")
229 arguments_for_c
.add
("var_for_c_{a}")
233 if return_mtype
.is_cprimitive
then
234 v
.add
("{recv_var} = {externname}({arguments_for_c.join(", ")});")
236 v
.add
("struct nitni_instance* ret_var;")
237 v
.add
("ret_var = {externname}({arguments_for_c.join(", ")});")
238 v
.add
("{recv_var} = ret_var->value;")
242 compile_ffi_support_to_c
(v
)
246 redef class CCompilationUnit
247 fun write_as_nitni
(amodule
: AModule, compdir
: String)
249 var base_name
= "{amodule.mmodule.name}._nitni"
251 var h_file
= "{base_name}.h"
252 write_header_to_file
( amodule
, "{compdir}/{h_file}", new Array[String],
253 "{amodule.cname.to_s.to_upper}_NITG_NITNI_H")
255 var c_file
= "{base_name}.c"
256 write_body_to_file
( amodule
, "{compdir}/{c_file}", ["\"{h_file}\
""] )
258 files
.add
( "{compdir}/{c_file}" )
262 redef class AbstractCompilerVisitor
263 # Create a `RuntimeVariable` for this C variable originating from C user code
264 private fun var_from_c
(name
: String, mtype
: MType): RuntimeVariable
266 if mtype
.is_cprimitive
then
267 return new RuntimeVariable(name
, mtype
, mtype
)
269 return new RuntimeVariable("{name}->value", mtype
, mtype
)
273 # Return a `RuntimeVarible` to C user code
274 private fun ret_to_c
(src
: RuntimeVariable, mtype
: MType)
276 if mtype
.is_cprimitive
then
279 add
("struct nitni_instance* ret_for_c;")
280 add
("ret_for_c = malloc(sizeof(struct nitni_instance));")
281 add
("ret_for_c->value = {src};")
282 add
("return ret_for_c;")
288 fun compile_extern_type
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
290 assert not is_cprimitive
292 # define friendly type
293 ccu
.header_c_types
.add
("#ifndef NIT_TYPE_{cname}\n")
294 ccu
.header_c_types
.add
("#define NIT_TYPE_{cname} 1\n")
295 ccu
.header_c_types
.add
("typedef struct nitni_instance *{cname};\n")
296 ccu
.header_c_types
.add
("#endif\n")
299 fun compile_extern_helper_functions
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
301 # actually, we do not need to do anything when using the bohem garbage collector
304 var nitni_visitor
= v
.compiler
.new_visitor
305 ccu
.header_decl
.add
("#define {mangled_cname}_incr_ref(from) while(0)\{\}\n")
308 nitni_visitor
= v
.compiler
.new_visitor
309 ccu
.header_decl
.add
("#define {mangled_cname}_decr_ref(from) while(0)\{\}\n")
313 redef class MNullableType
314 redef fun compile_extern_helper_functions
(v
, ccu
)
318 # In nitni files, declare internal function as extern
319 var full_friendly_csignature
= "{cname} {v.compiler.mainmodule.name}___null_{mtype.mangled_cname}()"
320 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
322 # In nitni files, #define friendly as extern
323 var base_cname
= "null_{mtype.mangled_cname}"
324 ccu
.header_decl
.add
("#define {base_cname} {v.compiler.mainmodule.name}___{base_cname}\n")
326 # Internally, implement internal function
327 var nitni_visitor
= v
.compiler
.new_visitor
328 nitni_visitor
.frame
= v
.frame
329 var full_internal_csignature
= "{cname_blind} {v.compiler.mainmodule.name}___null_{mtype.mangled_cname}()"
330 nitni_visitor
.add
("#ifndef NIT_NULL_null_{mtype.mangled_cname}")
331 nitni_visitor
.add
("#define NIT_NULL_null_{mtype.mangled_cname}")
332 nitni_visitor
.add
("{full_internal_csignature} \{")
333 nitni_visitor
.add
("struct nitni_instance* ret_for_c;")
334 nitni_visitor
.add
("ret_for_c = malloc(sizeof(struct nitni_instance));")
335 nitni_visitor
.add
("ret_for_c->value = NULL;")
336 nitni_visitor
.add
("return ret_for_c;")
337 nitni_visitor
.add
("\}")
338 nitni_visitor
.add
("#endif")
342 redef class MExplicitCall
343 fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
345 var mproperty
= mproperty
346 assert mproperty
isa MMethod
348 # In nitni files, declare internal function as extern
349 var full_friendly_csignature
= mproperty
.build_csignature
(recv_mtype
, v
.compiler
.mainmodule
, null, long_signature
, internal_call_context
)
350 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
352 # Internally, implement internal function
353 var nitni_visitor
= v
.compiler
.new_visitor
354 nitni_visitor
.frame
= v
.frame
355 var msignature
= mproperty
.intro
.msignature
356 var csignature_blind
= mproperty
.build_csignature
(recv_mtype
, v
.compiler
.mainmodule
, null, long_signature
, internal_call_context
)
358 nitni_visitor
.add_decl
("/* nitni callback for {mproperty.full_name} */")
359 nitni_visitor
.add_decl
("{csignature_blind} \{")
361 var vars
= new Array[RuntimeVariable]
362 var mtype
: MType = recv_mtype
364 if mproperty
.is_init
then
365 if recv_mtype
.mclass
.kind
== extern_kind
then
366 recv_var
= nitni_visitor
.new_var
(mtype
)
368 var recv_mtype
= recv_mtype
369 recv_var
= nitni_visitor
.init_instance
(recv_mtype
)
370 nitni_visitor
.add
("{mtype.ctype} recv /* var self: {mtype} */;")
371 nitni_visitor
.add
("recv = {recv_var};")
374 mtype
= mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
375 recv_var
= nitni_visitor
.var_from_c
("recv", mtype
)
380 for p
in msignature
.mparameters
do
381 var arg_mtype
= p
.mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
382 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
386 var ret_var
= nitni_visitor
.send
(mproperty
, vars
)
388 var return_mtype
= msignature
.return_mtype
389 if mproperty
.is_init
then
390 if recv_mtype
.mclass
.kind
!= extern_kind
then ret_var
= recv_var
391 return_mtype
= recv_mtype
393 if return_mtype
!= null then
394 assert ret_var
!= null
395 return_mtype
= return_mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
396 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
398 nitni_visitor
.add
("\}")
402 redef class MExplicitSuper
403 fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
405 var mproperty
= from
.mproperty
406 assert mproperty
isa MMethod
407 var mclass_type
= from
.mclassdef
.mclass
.mclass_type
408 var mmodule
= from
.mclassdef
.mmodule
410 # In nitni files, declare internal function as extern
411 var internal_csignature
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
412 ccu
.header_decl
.add
("extern {internal_csignature};\n")
414 # In nitni files, #define friendly as extern
415 var friendly_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", short_signature
)
416 var internal_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
)
417 ccu
.header_decl
.add
("#define {friendly_cname} {internal_cname}\n")
419 # Internally, implement internal function
420 var nitni_visitor
= v
.compiler
.new_visitor
421 nitni_visitor
.frame
= v
.frame
422 var msignature
= mproperty
.intro
.msignature
424 var csignature_blind
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
426 nitni_visitor
.add_decl
("/* nitni callback to super for {mproperty.full_name} */")
427 nitni_visitor
.add_decl
("{csignature_blind} \{")
429 var vars
= new Array[RuntimeVariable]
431 var recv_var
= nitni_visitor
.var_from_c
("recv", mclass_type
)
434 for p
in msignature
.mparameters
do
435 var arg_mtype
= v
.anchor
(p
.mtype
)
436 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
440 var ret_var
= nitni_visitor
.supercall
(from
.as(MMethodDef), mclass_type
, vars
)
442 var return_mtype
= msignature
.return_mtype
443 if return_mtype
!= null then
444 assert ret_var
!= null
445 return_mtype
= v
.anchor
(return_mtype
)
446 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
448 nitni_visitor
.add
("\}")
452 redef class MExplicitCast
453 fun compile_extern_callbacks
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit)
462 # In nitni files, declare internal function as extern
463 var full_friendly_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
464 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
466 # In nitni files, #define friendly as extern
467 ccu
.header_decl
.add
("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
469 # Internally, implement internal function
470 var nitni_visitor
= v
.compiler
.new_visitor
471 nitni_visitor
.frame
= v
.frame
473 var full_internal_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind} from)"
474 nitni_visitor
.add_decl
("/* nitni check for {from} to {to} */")
475 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
477 var from_var
= new RuntimeVariable("from->value", from
, from
)
478 var recv_var
= nitni_visitor
.type_test
(from_var
, to
, "FFI isa")
479 nitni_visitor
.add
("return {recv_var};")
481 nitni_visitor
.add
("\}")
484 if from
== to
.as_nullable
then
486 ccu
.header_decl
.add
("#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n")
493 # In nitni files, declare internal function as extern
494 full_friendly_csignature
= "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
495 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
497 # In nitni files, #define friendly as extern
498 ccu
.header_decl
.add
("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
500 # Internally, implement internal function
501 nitni_visitor
= v
.compiler
.new_visitor
502 nitni_visitor
.frame
= v
.frame
504 full_internal_csignature
= "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind} from)"
505 nitni_visitor
.add_decl
("/* nitni cast for {from} to {to} */")
506 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
508 from_var
= nitni_visitor
.var_from_c
("from", from
)
511 var check
= nitni_visitor
.type_test
(from_var
, to
, "FFI cast")
512 nitni_visitor
.add
("if (!{check}) \{")
513 nitni_visitor
.add_abort
("FFI cast failed")
514 nitni_visitor
.add
("\}")
517 recv_var
= nitni_visitor
.autobox
(from_var
, to
)
519 nitni_visitor
.ret_to_c
(recv_var
, to
)
521 nitni_visitor
.add
("\}")
524 if from
.as_nullable
== to
then
525 # format A_as_nullable
526 ccu
.header_decl
.add
("#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
529 if from
== to
.as_nullable
then
530 # format A_as_nullable
531 ccu
.header_decl
.add
("#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")