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 # Full FFI support for the compiler
24 private var foreign_callbacks
= new ForeignCallbackSet
26 redef fun finalize_ffi
(compiler
: AbstractCompiler)
28 if not uses_ffi
then return
32 compiled_callbacks
.clear
33 #Do not reset `foreign_callbacks` and `ffi_callbacks` because they are computed in previous phases
36 private var compiled_callbacks
= new Array[NitniCallback]
38 # Returns true if callbacks has yet to be generated and register it as being generated
39 private fun check_callback_compilation
(cb
: NitniCallback): Bool
41 var compiled
= compiled_callbacks
.has
(cb
)
42 if not compiled
then compiled_callbacks
.add
(cb
)
47 redef class AMethPropdef
48 redef fun compile_ffi_support_to_c
(v
)
52 var mmodule
= mpropdef
.mclassdef
.mmodule
53 var mainmodule
= v
.compiler
.mainmodule
54 var ccu
= mmodule
.nitni_ccu
.as(not null)
56 for mtype
in foreign_callbacks
.types
do
57 if not mtype
.is_cprimitive
then
58 mtype
.compile_extern_type
(v
, ccu
)
60 # has callbacks already been compiled? (this may very well happen with global compilation)
61 mtype
.compile_extern_helper_functions
(v
, ccu
, mmodule
.check_callback_compilation
(mtype
))
66 for cb
in foreign_callbacks
.callbacks
do
67 cb
.compile_extern_callback
(v
, ccu
, mainmodule
.check_callback_compilation
(cb
))
70 for cb
in foreign_callbacks
.supers
do
71 cb
.compile_extern_callback
(v
, ccu
, mainmodule
.check_callback_compilation
(cb
))
74 for cb
in foreign_callbacks
.casts
do
75 cb
.compile_extern_callbacks
(v
, ccu
, mainmodule
.check_callback_compilation
(cb
))
78 # manage nitni callback set
79 mmodule
.foreign_callbacks
.join
(foreign_callbacks
)
83 redef class MExplicitCall
84 private fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit, compile_implementation_too
: Bool)
86 var mproperty
= mproperty
87 assert mproperty
isa MMethod
89 # In nitni files, declare internal function as extern
90 var full_friendly_csignature
= mproperty
.build_csignature
(recv_mtype
, v
.compiler
.mainmodule
, null, long_signature
, internal_call_context
)
91 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
93 if not compile_implementation_too
then return
95 # Internally, implement internal function
96 var nitni_visitor
= v
.compiler
.new_visitor
97 nitni_visitor
.frame
= v
.frame
98 var msignature
= mproperty
.lookup_first_definition
(v
.compiler
.mainmodule
, recv_mtype
).msignature
99 var csignature_blind
= mproperty
.build_csignature
(recv_mtype
, v
.compiler
.mainmodule
, null, long_signature
, internal_call_context
)
101 nitni_visitor
.add_decl
("/* nitni callback for {mproperty.full_name} */")
102 nitni_visitor
.add_decl
("{csignature_blind} \{")
104 var vars
= new Array[RuntimeVariable]
105 var mtype
: MType = recv_mtype
107 if mproperty
.is_init
then
108 var recv_mtype
= recv_mtype
109 recv_var
= nitni_visitor
.init_instance_or_extern
(recv_mtype
)
110 nitni_visitor
.add
("{mtype.ctype} self /* var self: {mtype} */;")
111 nitni_visitor
.add
("self = {recv_var};")
113 mtype
= mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
114 recv_var
= nitni_visitor
.var_from_c
("self", mtype
)
115 recv_var
= nitni_visitor
.box_extern
(recv_var
, mtype
)
120 for p
in msignature
.mparameters
do
121 var arg_mtype
= p
.mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
122 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
123 arg
= nitni_visitor
.box_extern
(arg
, arg_mtype
)
127 var ret_var
= nitni_visitor
.send
(mproperty
, vars
)
129 var return_mtype
= msignature
.return_mtype
130 if mproperty
.is_init
then
131 if recv_mtype
.mclass
.kind
!= extern_kind
then ret_var
= recv_var
132 return_mtype
= recv_mtype
134 if return_mtype
!= null then
135 assert ret_var
!= null
136 return_mtype
= return_mtype
.anchor_to
(v
.compiler
.mainmodule
, recv_mtype
)
137 ret_var
= nitni_visitor
.autobox
(ret_var
, return_mtype
)
138 ret_var
= nitni_visitor
.unbox_extern
(ret_var
, return_mtype
)
139 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
141 nitni_visitor
.add
("\}")
146 private fun compile_extern_helper_functions
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit, compile_implementation_too
: Bool)
148 # actually, we do not need to do anything when using the bohem garbage collector
149 var call_context
= from_c_call_context
152 ccu
.header_decl
.add
"#ifndef {mangled_cname}_incr_ref\n"
153 ccu
.header_decl
.add
" #define {mangled_cname}_incr_ref(from) nitni_global_ref_incr(({call_context.name_mtype(self)})(from))\n"
154 ccu
.header_decl
.add
"#endif\n"
157 ccu
.header_decl
.add
"#ifndef {mangled_cname}_decr_ref\n"
158 ccu
.header_decl
.add
" #define {mangled_cname}_decr_ref(from) nitni_global_ref_decr(({call_context.name_mtype(self)})(from))\n"
159 ccu
.header_decl
.add
"#endif\n"
163 redef class MNullableType
164 redef fun compile_extern_helper_functions
(v
, ccu
, compile_implementation_too
)
168 var base_cname
= "null_{mtype.mangled_cname}"
169 var full_cname
= "NIT_NULL___{base_cname}"
171 # In nitni files, declare internal function as extern
172 var full_friendly_csignature
= "{cname_blind} {full_cname}()"
173 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
175 # In nitni files, #define friendly as extern
176 ccu
.header_decl
.add
("#define {base_cname} {full_cname}\n")
178 if not compile_implementation_too
then return
180 # FIXME: This is ugly an broke the separate compilation principle
181 # The real function MUST be compiled only once, #define pragma only protect the compiler, not the loader
182 # However, I am not sure of the right approach here (eg. week refs are ugly)
183 if v
.compiler
.compiled_null_types
.has
(self) then return
184 v
.compiler
.compiled_null_types
.add
self
186 # Internally, implement internal function
187 var nitni_visitor
= v
.compiler
.new_visitor
188 nitni_visitor
.frame
= v
.frame
189 var full_internal_csignature
= "{cname_blind} {full_cname}()"
190 nitni_visitor
.add
("{full_internal_csignature} \{")
191 nitni_visitor
.add
("struct nitni_instance* ret_for_c;")
192 nitni_visitor
.add
("ret_for_c = nit_alloc(sizeof(struct nitni_instance));")
193 nitni_visitor
.add
("ret_for_c->value = NULL;")
194 nitni_visitor
.add
("return ret_for_c;")
195 nitni_visitor
.add
("\}")
199 redef class MExplicitSuper
200 private fun compile_extern_callback
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit, compile_implementation_too
: Bool)
202 var mproperty
= from
.mproperty
203 assert mproperty
isa MMethod
204 var mclass_type
= from
.mclassdef
.mclass
.mclass_type
206 # In nitni files, declare internal function as extern
207 var internal_csignature
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
208 ccu
.header_decl
.add
("extern {internal_csignature};\n")
210 # In nitni files, #define friendly as extern
211 var friendly_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", short_signature
)
212 var internal_cname
= mproperty
.build_cname
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
)
213 ccu
.header_decl
.add
("#define {friendly_cname} {internal_cname}\n")
215 if not compile_implementation_too
then return
217 # Internally, implement internal function
218 var nitni_visitor
= v
.compiler
.new_visitor
219 nitni_visitor
.frame
= v
.frame
220 var msignature
= mproperty
.lookup_first_definition
(v
.compiler
.mainmodule
, mclass_type
).msignature
222 var csignature_blind
= mproperty
.build_csignature
(mclass_type
, v
.compiler
.mainmodule
, "___super", long_signature
, internal_call_context
)
224 nitni_visitor
.add_decl
("/* nitni callback to super for {mproperty.full_name} */")
225 nitni_visitor
.add_decl
("{csignature_blind} \{")
227 var vars
= new Array[RuntimeVariable]
229 var recv_var
= nitni_visitor
.var_from_c
("self", mclass_type
)
230 recv_var
= nitni_visitor
.box_extern
(recv_var
, mclass_type
)
233 for p
in msignature
.mparameters
do
234 var arg_mtype
= v
.anchor
(p
.mtype
)
235 var arg
= nitni_visitor
.var_from_c
(p
.name
, arg_mtype
)
236 arg
= nitni_visitor
.box_extern
(arg
, arg_mtype
)
240 var ret_var
= nitni_visitor
.supercall
(from
.as(MMethodDef), mclass_type
, vars
)
242 var return_mtype
= msignature
.return_mtype
243 if return_mtype
!= null then
244 assert ret_var
!= null
245 return_mtype
= v
.anchor
(return_mtype
)
246 ret_var
= nitni_visitor
.autobox
(ret_var
, return_mtype
)
247 ret_var
= nitni_visitor
.unbox_extern
(ret_var
, return_mtype
)
248 nitni_visitor
.ret_to_c
(ret_var
, return_mtype
)
250 nitni_visitor
.add
("\}")
254 redef class MExplicitCast
255 private fun compile_extern_callbacks
(v
: AbstractCompilerVisitor, ccu
: CCompilationUnit, compile_implementation_too
: Bool)
264 # In nitni files, declare internal function as extern
265 var full_friendly_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({from.cname_blind})"
266 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
268 # In nitni files, #define friendly as extern
269 ccu
.header_decl
.add
("#define {check_cname} {v.compiler.mainmodule.name}___{check_cname}\n")
271 if compile_implementation_too
then
272 # Internally, implement internal function
273 var nitni_visitor
= v
.compiler
.new_visitor
274 nitni_visitor
.frame
= v
.frame
276 var full_internal_csignature
= "int {v.compiler.mainmodule.name }___{from.mangled_cname}_is_a_{to.mangled_cname}({internal_call_context.name_mtype(from)} from)"
278 nitni_visitor
.add_decl
("/* nitni check for {from} to {to} */")
279 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
281 var from_var
= nitni_visitor
.var_from_c
("from", from
)
282 from_var
= nitni_visitor
.box_extern
(from_var
, from
)
283 var recv_var
= nitni_visitor
.type_test
(from_var
, to
, "FFI isa")
284 nitni_visitor
.add
("return {recv_var};")
286 nitni_visitor
.add
("\}")
290 if from
== to
.as_nullable
then
292 ccu
.header_decl
.add
("#define {from.mangled_cname}_is_null !{from.mangled_cname}_is_a_{to.mangled_cname}\n")
299 # In nitni files, declare internal function as extern
300 full_friendly_csignature
= "{to.cname_blind} {v.compiler.mainmodule.name }___{from.mangled_cname}_as_{to.mangled_cname}({from.cname_blind})"
301 ccu
.header_decl
.add
("extern {full_friendly_csignature};\n")
303 # In nitni files, #define friendly as extern
304 ccu
.header_decl
.add
("#define {cast_cname} {v.compiler.mainmodule.name}___{cast_cname}\n")
306 if compile_implementation_too
then
307 # Internally, implement internal function
308 var nitni_visitor
= v
.compiler
.new_visitor
309 nitni_visitor
.frame
= v
.frame
311 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)"
312 nitni_visitor
.add_decl
("/* nitni cast for {from} to {to} */")
313 nitni_visitor
.add_decl
("{full_internal_csignature} \{")
315 var from_var
= nitni_visitor
.var_from_c
("from", from
)
316 from_var
= nitni_visitor
.box_extern
(from_var
, from
)
319 var check
= nitni_visitor
.type_test
(from_var
, to
, "FFI cast")
320 nitni_visitor
.add
("if (!{check}) \{")
321 nitni_visitor
.add_abort
("FFI cast failed")
322 nitni_visitor
.add
("\}")
325 var recv_var
= nitni_visitor
.autobox
(from_var
, to
)
326 recv_var
= nitni_visitor
.unbox_extern
(recv_var
, to
)
328 nitni_visitor
.ret_to_c
(recv_var
, to
)
330 nitni_visitor
.add
("\}")
334 if from
.as_nullable
== to
then
335 # format A_as_nullable
336 ccu
.header_decl
.add
("#define {from.mangled_cname}_as_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")
339 if from
== to
.as_nullable
then
340 # format A_as_nullable
341 ccu
.header_decl
.add
("#define {to.mangled_cname}_as_not_nullable {from.mangled_cname}_as_{to.mangled_cname}\n")