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_array = mmodule.ldflags[""]
132 if ldflags_array.has("-lrt") and system("sh -c 'uname -s 2>/dev/null || echo not' | grep Darwin >/dev/null") == 0 then
133 # Remove -lrt on OS X
134 ldflags_array.remove "-lrt"
136 var ldflags = ldflags_array.join(" ")
139 var pkgconfigs = mmodule.pkgconfigs
141 if not pkgconfigs.is_empty then
142 var cmd = "which pkg-config >/dev/null"
143 if system(cmd) != 0 then
144 v.fatal "FFI Error: Command `pkg-config
` not found. Please install it"
148 for p in pkgconfigs do
149 cmd = "pkg-config --exists '{p}'"
150 if system(cmd) != 0 then
151 v.fatal "FFI Error: package {p} is not found by `pkg-config
`. Please install it."
156 pkg_cflags = "`pkg-config
--cflags
{pkgconfigs.join(" ")}`"
157 ldflags += " `pkg-config
--libs
{pkgconfigs.join(" ")}`"
160 # Compile each source file to an object file (or equivalent)
161 var object_files = new Array[String]
163 f.compile(v, mmodule, object_files, pkg_cflags)
166 # Link everything in a shared library
167 var cmd = "{v.c_compiler} -Wall -shared -o {foreign_code_lib_path} {object_files.join(" ")} {ldflags}"
168 if system(cmd) != 0 then
169 v.fatal "FFI Error: Failed to link native code using `{cmd}`"
176 # Compile the standard API of the `.so
` file
178 # * The shared structure `nit_call_arg
`.
179 # * Standardized implementation functions entrypoints that relay calls
180 # to the FFI implementation functions.
181 private fun compile_foreign_lib_api(compdir: String): CCompilationUnit
183 var mmodule = mmodule
184 assert mmodule != null
186 # ready extern code compiler
187 var ecc = new CCompilationUnit
189 ecc.body_decl.add """
193 #include <inttypes.h>
195 // C structure behind `CallArg` from the interpreter
196 typedef union nit_call_arg {
203 uint16_t value_UInt16;
205 uint32_t value_UInt32;
213 var used_types = collect_mtypes
214 for t in used_types do
215 if not t.is_cprimitive then
216 ecc.header_c_types.add "typedef void* {t.cname};\n"
220 # TODO callbacks & casts
222 for nclassdef in n_classdefs do for npropdef in nclassdef.n_propdefs do
223 if npropdef isa AMethPropdef and npropdef.supported_by_dynamic_ffi then
224 npropdef.mpropdef.compile_foreign_code_entry ecc
228 ecc.write_as_foreign_lib_api(mmodule, compdir)
233 # Collect all `MType` use in extern methods within this module
234 private fun collect_mtypes: Set[MType]
236 var used_types = new HashSet[MType]
239 for nclassdef in n_classdefs do for npropdef in nclassdef.n_propdefs do
240 if npropdef isa AMethPropdef and npropdef.supported_by_dynamic_ffi then
241 var fcs = npropdef.foreign_callbacks
242 used_types.add_all fcs.types
250 redef class CCompilationUnit
251 # Write this compilation unit as the API of a foreign code library
252 private fun write_as_foreign_lib_api(mmodule: MModule, compdir: String)
254 # The FFI expects the support header to end with `._nitni
.h
`
255 var base_name = mmodule.c_name + "._nitni"
256 var guard = mmodule.c_name.to_s.to_upper + "_API_H"
257 var header_comment = """
259 Public API to foreign code of the Nit module {{{mmodule.name}}}
264 var h_file = base_name+".h"
265 var stream = new FileWriter.open(compdir/h_file)
266 stream.write header_comment
271 compile_header_core stream
279 var c_file = base_name+".c"
280 stream = new FileWriter.open(compdir/c_file)
281 stream.write header_comment
283 #include "{{{h_file}}}"
285 compile_body_core stream
288 # Only the C files needs compiling
289 files.add compdir / c_file
293 redef class MMethodDef
294 # Name of the entry point to the implementation function in the foreign lib
295 fun foreign_lib_entry_cname: String do return "entry__{cname}"
297 # Compile the standardized entry point as part of the foreign lib API
298 private fun compile_foreign_code_entry(ecc: CCompilationUnit)
300 var msignature = msignature
301 if msignature == null then return
304 var return_mtype = msignature.return_mtype
305 if mproperty.is_init then return_mtype = mclassdef.mclass.mclass_type
308 if return_mtype != null then
309 c_return_type = return_mtype.cname_blind
310 else c_return_type = "void"
312 var is_init = mproperty.is_init
315 var params = new Array[String]
316 if not is_init then params.add mclassdef.mclass.mclass_type.cname_blind
317 for param in msignature.mparameters do params.add param.mtype.cname_blind
319 # Declare the implementation function as extern
320 var impl_cname = mproperty.build_cname(mclassdef.bound_mtype,
321 mclassdef.mmodule, "___impl", long_signature)
322 ecc.body_decl.add "extern {c_return_type} {impl_cname}({params.join(", ")});\n"
324 # Declare the entry function
325 var foreign_lib_entry_cname = "int {foreign_lib_entry_cname}(int argc, nit_call_arg *argv, nit_call_arg *result)"
326 var fc = new CFunction(foreign_lib_entry_cname)
328 # Check argument count on the library side
330 # This may detect inconsistencies between the interpreter and the generated code.
331 var expected_argc = msignature.arity
332 if not is_init then expected_argc += 1
335 if (argc != {{{expected_argc}}}) {
336 printf("Invalid argument count in `{{{mproperty.full_name}}}`, expected %d, got %d.\\n",
337 argc, {{{expected_argc}}});
342 # Unpack and prepare args for the user code
344 var args_for_call = new Array[String]
346 var mtype = mclassdef.mclass.mclass_type
347 var arg_name = "arg___self"
349 fc.decls.add " {mtype.cname_blind} {arg_name};\n"
350 fc.exprs.add " {arg_name} = argv[{k}].{mtype.call_arg_field};\n"
351 args_for_call.add arg_name
355 for param in msignature.mparameters do
356 var mtype = param.mtype
357 var arg_name = "arg__"+param.name
359 fc.decls.add " {mtype.cname_blind} {arg_name};\n"
360 fc.exprs.add " {arg_name} = argv[{k}].{mtype.call_arg_field};\n"
361 args_for_call.add arg_name
366 # Call implementation function
367 var args_compressed = args_for_call.join(", ")
368 var method_call = "{impl_cname}({args_compressed})"
369 if return_mtype != null then
371 {{{return_mtype.cname_blind}}} return_value;
374 return_value = {{{method_call}}};
375 result->{{{return_mtype.call_arg_field}}} = return_value;
378 fc.exprs.add " {method_call};\n"
381 fc.exprs.add " return 0;\n"
383 ecc.add_exported_function fc
388 # The interpreter FFI use `void
*` to represent intern data types
389 redef fun cname_blind do return "void*"
391 # Field to store this type in the C structure `nit_call_arg
`
392 private fun call_arg_field: String do return "value_Pointer"
395 redef class MClassType
396 redef fun call_arg_field
398 if is_cprimitive and mclass.kind != extern_kind then
399 return "value_{name}"
404 redef class ExternFile
405 # Compile this source file
406 private fun compile(v: NaiveInterpreter, mmodule: MModule,
407 object_files: Array[String], pkg_cflags: String): Bool is abstract
410 redef class ExternCFile
411 redef fun compile(v, mmodule, object_files, pkg_cflags)
413 var compile_dir = v.compile_dir
414 var cflags = mmodule.cflags[""].join(" ") + " " + pkg_cflags
415 var obj = compile_dir / filename.basename(".c") + ".o"
417 var cmd = "{v.c_compiler} -Wall -c -fPIC -I {compile_dir} -g -o {obj} {filename} {cflags}"
418 if sys.system(cmd) != 0 then
419 v.fatal "FFI Error: Failed to compile C code using `{cmd}`"