1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Compiles extern code within a module to a static library, as needed
16 module on_demand_compiler
22 import naive_interpreter
24 import debugger_socket
# To linearize `ToolContext::init`
26 redef class ToolContext
29 var opt_compile_dir
= new OptionString("Directory used to generate temporary files", "--compile-dir")
31 init do option_context
.add_option opt_compile_dir
34 redef class AMethPropdef
35 # Does this method definition use the FFI and is it supported by the interpreter?
37 # * Must use the nested foreign code block of the FFI.
38 # * Must not have callbacks.
39 # * Must be implemented in C.
40 # * Must not have a parameter or return typed with a Nit standard class.
41 fun supported_by_dynamic_ffi
: Bool
43 # If the user specfied `is light_ffi`, it must be supported
44 var nats
= get_annotations
("light_ffi")
45 if nats
.not_empty
then return true
47 var n_extern_code_block
= n_extern_code_block
48 if not (n_extern_calls
== null and n_extern_code_block
!= null and
49 n_extern_code_block
.is_c
) then return false
51 for mparam
in mpropdef
.msignature
.mparameters
do
52 if not mparam
.mtype
.is_cprimitive
then
57 var return_mtype
= mpropdef
.msignature
.return_mtype
58 if return_mtype
!= null and not return_mtype
.is_cprimitive
then
66 redef class NaiveInterpreter
67 redef fun start
(mainmodule
)
71 # Delete temporary files
72 var compile_dir
= compile_dir
73 if compile_dir
.file_exists
then compile_dir
.rmdir
76 # Where to store generated C and extracted code
77 private var compile_dir
: String is lazy
do
78 # Prioritize the user supplied directory
79 var opt
= modelbuilder
.toolcontext
.opt_compile_dir
.value
80 if opt
!= null then return opt
81 return "/tmp/niti_ffi_{process_id}"
84 # Identifier for this process, unique between running interpreters
85 private fun process_id
: Int `{ return getpid(); `}
87 # Path of the compiled foreign code library
89 # TODO change the ".so" extension per platform.
90 fun foreign_code_lib_path(mmodule: MModule): String
92 return compile_dir / mmodule.c_name + ".so"
95 # External compiler used to generate the foreign code library
96 private var c_compiler = "cc"
101 # Compile user FFI code and a standardized API into a `.so
` file
103 # Returns `true` on success.
104 fun compile_foreign_lib(v: NaiveInterpreter): Bool
106 var mmodule = mmodule
107 assert mmodule != null
109 var compile_dir = v.compile_dir
110 var foreign_code_lib_path = v.foreign_code_lib_path(mmodule)
112 if not compile_dir.file_exists then compile_dir.mkdir(0o700)
114 # Compile the common FFI part
115 ensure_compile_ffi_wrapper
116 for mclassdef in mmodule.mclassdefs do for mpropdef in mclassdef.mpropdefs do
117 var anode = v.modelbuilder.mpropdef2node(mpropdef)
118 if mpropdef isa MMethodDef and anode isa AMethPropdef and anode.supported_by_dynamic_ffi then
119 anode.compile_ffi_method(mmodule)
122 mmodule.finalize_ffi_wrapper(compile_dir, mmodule)
124 # Compile the standard API and its implementation for the .so file
125 var ccu = compile_foreign_lib_api(compile_dir)
127 var srcs = [for file in ccu.files do new ExternCFile(file, ""): ExternFile]
128 srcs.add_all mmodule.ffi_files
130 # Compiler options specific to this module
131 var ldflags = mmodule.ldflags[""].join(" ")
134 var pkgconfigs = mmodule.pkgconfigs
136 if not pkgconfigs.is_empty then
137 var cmd = "which pkg-config >/dev/null"
138 if system(cmd) != 0 then
139 v.fatal "FFI Error: Command `pkg-config
` not found. Please install it"
143 for p in pkgconfigs do
144 cmd = "pkg-config --exists '{p}'"
145 if system(cmd) != 0 then
146 v.fatal "FFI Error: package {p} is not found by `pkg-config
`. Please install it."
151 pkg_cflags = "`pkg-config
--cflags
{pkgconfigs.join(" ")}`"
152 ldflags += " `pkg-config
--libs
{pkgconfigs.join(" ")}`"
155 # Compile each source file to an object file (or equivalent)
156 var object_files = new Array[String]
158 f.compile(v, mmodule, object_files, pkg_cflags)
161 # Link everything in a shared library
162 var cmd = "{v.c_compiler} -Wall -shared -o {foreign_code_lib_path} {object_files.join(" ")} {ldflags}"
163 if system(cmd) != 0 then
164 v.fatal "FFI Error: Failed to link native code using `{cmd}`"
171 # Compile the standard API of the `.so
` file
173 # * The shared structure `nit_call_arg
`.
174 # * Standardized implementation functions entrypoints that relay calls
175 # to the FFI implementation functions.
176 private fun compile_foreign_lib_api(compdir: String): CCompilationUnit
178 var mmodule = mmodule
179 assert mmodule != null
181 # ready extern code compiler
182 var ecc = new CCompilationUnit
184 ecc.body_decl.add """
188 #include <inttypes.h>
190 // C structure behind `CallArg` from the interpreter
191 typedef union nit_call_arg {
198 uint16_t value_UInt16;
200 uint32_t value_UInt32;
208 var used_types = collect_mtypes
209 for t in used_types do
210 if not t.is_cprimitive then
211 ecc.header_c_types.add "typedef void* {t.cname};\n"
215 # TODO callbacks & casts
217 for nclassdef in n_classdefs do for npropdef in nclassdef.n_propdefs do
218 if npropdef isa AMethPropdef and npropdef.supported_by_dynamic_ffi then
219 npropdef.mpropdef.compile_foreign_code_entry ecc
223 ecc.write_as_foreign_lib_api(mmodule, compdir)
228 # Collect all `MType` use in extern methods within this module
229 private fun collect_mtypes: Set[MType]
231 var used_types = new HashSet[MType]
234 for nclassdef in n_classdefs do for npropdef in nclassdef.n_propdefs do
235 if npropdef isa AMethPropdef and npropdef.supported_by_dynamic_ffi then
236 var fcs = npropdef.foreign_callbacks
237 used_types.add_all fcs.types
245 redef class CCompilationUnit
246 # Write this compilation unit as the API of a foreign code library
247 private fun write_as_foreign_lib_api(mmodule: MModule, compdir: String)
249 # The FFI expects the support header to end with `._nitni
.h
`
250 var base_name = mmodule.c_name + "._nitni"
251 var guard = mmodule.c_name.to_s.to_upper + "_API_H"
252 var header_comment = """
254 Public API to foreign code of the Nit module {{{mmodule.name}}}
259 var h_file = base_name+".h"
260 var stream = new FileWriter.open(compdir/h_file)
261 stream.write header_comment
266 compile_header_core stream
274 var c_file = base_name+".c"
275 stream = new FileWriter.open(compdir/c_file)
276 stream.write header_comment
278 #include "{{{h_file}}}"
280 compile_body_core stream
283 # Only the C files needs compiling
284 files.add compdir / c_file
288 redef class MMethodDef
289 # Name of the entry point to the implementation function in the foreign lib
290 fun foreign_lib_entry_cname: String do return "entry__{cname}"
292 # Compile the standardized entry point as part of the foreign lib API
293 private fun compile_foreign_code_entry(ecc: CCompilationUnit)
295 var msignature = msignature
296 if msignature == null then return
299 var return_mtype = msignature.return_mtype
300 if mproperty.is_init then return_mtype = mclassdef.mclass.mclass_type
303 if return_mtype != null then
304 c_return_type = return_mtype.cname_blind
305 else c_return_type = "void"
307 var is_init = mproperty.is_init
310 var params = new Array[String]
311 if not is_init then params.add mclassdef.mclass.mclass_type.cname_blind
312 for param in msignature.mparameters do params.add param.mtype.cname_blind
314 # Declare the implementation function as extern
315 var impl_cname = mproperty.build_cname(mclassdef.bound_mtype,
316 mclassdef.mmodule, "___impl", long_signature)
317 ecc.body_decl.add "extern {c_return_type} {impl_cname}({params.join(", ")});\n"
319 # Declare the entry function
320 var foreign_lib_entry_cname = "int {foreign_lib_entry_cname}(int argc, nit_call_arg *argv, nit_call_arg *result)"
321 var fc = new CFunction(foreign_lib_entry_cname)
323 # Check argument count on the library side
325 # This may detect inconsistencies between the interpreter and the generated code.
326 var expected_argc = msignature.arity
327 if not is_init then expected_argc += 1
330 if (argc != {{{expected_argc}}}) {
331 printf("Invalid argument count in `{{{mproperty.full_name}}}`, expected %d, got %d.\\n",
332 argc, {{{expected_argc}}});
337 # Unpack and prepare args for the user code
339 var args_for_call = new Array[String]
341 var mtype = mclassdef.mclass.mclass_type
342 var arg_name = "arg___self"
344 fc.decls.add " {mtype.cname_blind} {arg_name};\n"
345 fc.exprs.add " {arg_name} = argv[{k}].{mtype.call_arg_field};\n"
346 args_for_call.add arg_name
350 for param in msignature.mparameters do
351 var mtype = param.mtype
352 var arg_name = "arg__"+param.name
354 fc.decls.add " {mtype.cname_blind} {arg_name};\n"
355 fc.exprs.add " {arg_name} = argv[{k}].{mtype.call_arg_field};\n"
356 args_for_call.add arg_name
361 # Call implementation function
362 var args_compressed = args_for_call.join(", ")
363 var method_call = "{impl_cname}({args_compressed})"
364 if return_mtype != null then
366 {{{return_mtype.cname_blind}}} return_value;
369 return_value = {{{method_call}}};
370 result->{{{return_mtype.call_arg_field}}} = return_value;
373 fc.exprs.add " {method_call};\n"
376 fc.exprs.add " return 0;\n"
378 ecc.add_exported_function fc
383 # The interpreter FFI use `void
*` to represent intern data types
384 redef fun cname_blind do return "void*"
386 # Field to store this type in the C structure `nit_call_arg
`
387 private fun call_arg_field: String do return "value_Pointer"
390 redef class MClassType
391 redef fun call_arg_field
393 if is_cprimitive and mclass.kind != extern_kind then
394 return "value_{name}"
399 redef class ExternFile
400 # Compile this source file
401 private fun compile(v: NaiveInterpreter, mmodule: MModule,
402 object_files: Array[String], pkg_cflags: String): Bool is abstract
405 redef class ExternCFile
406 redef fun compile(v, mmodule, object_files, pkg_cflags)
408 var compile_dir = v.compile_dir
409 var cflags = mmodule.cflags[""].join(" ") + " " + pkg_cflags
410 var obj = compile_dir / filename.basename(".c") + ".o"
412 var cmd = "{v.c_compiler} -Wall -c -fPIC -I {compile_dir} -g -o {obj} {filename} {cflags}"
413 if sys.system(cmd) != 0 then
414 v.fatal "FFI Error: Failed to compile C code using `{cmd}`"