src: intro an FFI for the interpreter
[nit.git] / src / interpreter / dynamic_loading_ffi / dynamic_loading_ffi.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Execute FFI code by creating and loading shared libraries
16 #
17 # Triggers the compilation of the library when needed.
18 module dynamic_loading_ffi is ldflags "-ldl"
19
20 import naive_interpreter
21
22 import on_demand_compiler
23 import ffi
24
25 in "C Header" `{
26 #include <dlfcn.h>
27 #include <inttypes.h>
28
29 // C structure behind `CallArg`
30 typedef union nit_call_arg {
31 long value_Int;
32 int value_Bool;
33 uint32_t value_Char;
34 uint8_t value_Byte;
35 double value_Float;
36 void* value_Pointer;
37 } nit_call_arg;
38
39 // Signature shared by all entry points
40 typedef int (*nit_foreign_lib_entry)(int, nit_call_arg*, nit_call_arg*);
41 `}
42
43 # Single cell in the list of arguments sent to foreign code (and received)
44 #
45 # Each cell can hold all primitive types and pointers to Nit class instances.
46 #
47 # Since this is a C pointer, it acts as both a single cell and an array.
48 private extern class CallArg `{ nit_call_arg* `}
49
50 # Initialize an array of `CallArg` of `length` elements
51 new (length: Int) `{ return calloc(length, sizeof(nit_call_arg)); `}
52
53 # Get the element at `index` after `self`
54 fun [](index: Int): CallArg `{ return self + index; `}
55
56 # The `Int` held by this cell
57 fun int: Int `{ return self->value_Int; `}
58
59 # The `Int` held by this cell
60 fun int=(value: Int) `{ self->value_Int = value; `}
61
62 # The `Bool` held by this cell
63 fun bool: Bool `{ return self->value_Bool; `}
64
65 # The `Bool` held by this cell
66 fun bool=(value: Bool) `{ self->value_Bool = value; `}
67
68 # The `Char` held by this cell
69 fun char: Char `{ return self->value_Char; `}
70
71 # The `Char` held by this cell
72 fun char=(value: Char) `{ self->value_Char = value; `}
73
74 # The `Byte` held by this cell
75 fun byte: Byte `{ return self->value_Byte; `}
76
77 # The `Byte` held by this cell
78 fun byte=(value: Byte) `{ self->value_Byte = value; `}
79
80 # The `Float` held by this cell
81 fun float: Float `{ return self->value_Float; `}
82
83 # The `Float` held by this cell
84 fun float=(value: Float) `{ self->value_Float = value; `}
85
86 # The `Pointer` held by this cell
87 fun pointer: Pointer `{ return self->value_Pointer; `}
88
89 # The `Pointer` held by this cell
90 fun pointer=(value: Pointer) `{ self->value_Pointer = value; `}
91
92 # The `Instance` held by this cell
93 fun instance: Instance `{ return (Instance)self->value_Pointer; `}
94
95 # The `Instance` held by this cell
96 fun instance=(value: Instance) `{ self->value_Pointer = value; `}
97
98 # The `NativeString` held by this cell
99 fun native_string: NativeString `{ return (unsigned char*)self->value_Pointer; `}
100
101 # Set the content of this cell according to `static_type`
102 #
103 # Opposite of `to_instance`.
104 fun from_static_type(value: Instance, static_type: MType)
105 do
106 if static_type.name == "Int" then
107 assert value isa PrimitiveInstance[Int]
108 self.int = value.val
109 else if static_type.name == "Bool" then
110 assert value isa PrimitiveInstance[Bool]
111 self.bool = value.val
112 else if static_type.name == "Char" then
113 assert value isa PrimitiveInstance[Char]
114 self.char = value.val
115 else if static_type.name == "Byte" then
116 assert value isa PrimitiveInstance[Byte]
117 self.byte = value.val
118 else if static_type.name == "Float" then
119 assert value isa PrimitiveInstance[Float]
120 self.float = value.val
121 else if static_type.name == "NativeString" then
122 assert value isa PrimitiveInstance[Buffer]
123 self.pointer = value.val.to_cstring
124 else if static_type isa MClassType and static_type.mclass.kind == extern_kind then
125 assert value isa PrimitiveInstance[Pointer] else print value.class_name
126 self.pointer = value.val
127 else
128 self.instance = value
129 end
130 end
131
132 # Get the content of this cell as an `Instance` of the `static_type`
133 #
134 # Opposite of `from_static_type`.
135 fun to_instance(static_type: MType, v: NaiveInterpreter): Instance
136 do
137 var name = static_type.name
138 if name == "Int" then
139 return v.int_instance(self.int)
140 else if name == "Bool" then
141 return if self.bool then
142 v.true_instance
143 else v.false_instance
144 else if name == "Char" then
145 return v.char_instance(self.char)
146 else if name == "Byte" then
147 return v.byte_instance(self.byte)
148 else if name == "Float" then
149 return v.float_instance(self.float)
150 else if name == "NativeString" then
151 return v.native_string_instance(self.native_string.to_s)
152 else if static_type isa MClassType and static_type.mclass.kind == extern_kind then
153 # We tag it with the most precise known type
154 return new PrimitiveInstance[Pointer](static_type, self.pointer)
155 else
156 return self.instance
157 end
158 end
159 end
160
161 # Handle to foreign code library
162 private extern class ForeignCodeLib
163 # Open and load the library at `path`
164 new dlopen(path: NativeString) `{
165 return dlopen(path, RTLD_LOCAL | RTLD_NOW);
166 `}
167
168 # Find the `ForeignCodeEntry` at `symbol_name`
169 fun dlsym(symbol_name: NativeString): ForeignCodeEntry `{
170 return dlsym(self, symbol_name);
171 `}
172 end
173
174 private fun dlerror: NativeString `{ return dlerror(); `}
175
176 # Handle to an implementation function in a `ForeignCodeLib`
177 private extern class ForeignCodeEntry`{ nit_foreign_lib_entry `}
178
179 # Invoke the implementation function by passing `args` and receive the return value
180 fun call(argc: Int, args, ret: CallArg): Bool `{
181 return ((nit_foreign_lib_entry)self)(argc, args, ret);
182 `}
183 end
184
185 redef class AMethPropdef
186 # Handle to the entrypoint of this method in the foreign code library
187 private var foreign_entry_cache: nullable ForeignCodeEntry = null
188
189 redef fun call_extern(v, mpropdef, args, frame)
190 do
191 # Fallback the default error if this method is not supported
192 if not supported_by_dynamic_ffi then return super
193
194 var entry = foreign_entry_cache
195 if entry == null then
196 # Get handle to foreign code lib
197 var amodule = v.modelbuilder.mmodule2node(mpropdef.mclassdef.mmodule)
198 assert amodule != null
199
200 var lib = amodule.foreign_code_lib(v)
201 if lib == null then return v.error_instance
202
203 # Get handle to implementation function
204 entry = lib.dlsym(mpropdef.foreign_lib_entry_cname.to_cstring)
205 if entry.address_is_null then
206 print mpropdef.foreign_lib_entry_cname
207 v.fatal "FFI Error: Cannot find method {mpropdef.name} in foreign code library."
208 return v.error_instance
209 end
210
211 foreign_entry_cache = entry
212 end
213
214 # Prepare to send args to foreign code lib
215 var is_init = mpropdef.mproperty.is_init
216 if is_init then args.shift
217 var native_args_length = args.length
218
219 var native_args = new CallArg(args.length)
220 var a = 0
221 if not is_init then
222 var arg = args[a]
223 var native_arg = native_args[a]
224 native_arg.from_static_type(arg, mpropdef.mclassdef.mclass.mclass_type)
225 a += 1
226 end
227 for param in mpropdef.msignature.mparameters do
228 var arg = args[a]
229 var native_arg = native_args[a]
230 native_arg.from_static_type(arg, param.mtype)
231 a += 1
232 end
233
234 # Allocate memory for the return value
235 var native_return = new CallArg(1)
236 var error = entry.call(native_args_length, native_args, native_return)
237
238 if error then
239 v.fatal "FFI Error: Native code library reported an error"
240 return null
241 end
242
243 # Get the result
244 var return_mtype = mpropdef.msignature.return_mtype
245 if is_init then return_mtype = mpropdef.mclassdef.mclass.mclass_type
246
247 var return_value
248 if return_mtype == null then
249 return_value = null
250 else
251 return_value = native_return.to_instance(return_mtype, v)
252 end
253
254 native_args.free
255 native_return.free
256
257 return return_value
258 end
259 end
260
261 redef class AModule
262
263 private var foreign_code_lib_cache: nullable ForeignCodeLib = null
264
265 # Handle to the external library with FFI code
266 private fun foreign_code_lib(v: NaiveInterpreter): nullable ForeignCodeLib
267 do
268 var lib = foreign_code_lib_cache
269 if lib != null then return lib
270
271 var mmodule = mmodule
272 assert mmodule != null
273
274 var foreign_code_lib_path = v.foreign_code_lib_path(mmodule)
275
276 # Compile lib
277 compile_foreign_lib v
278
279 lib = new ForeignCodeLib.dlopen(foreign_code_lib_path.to_cstring)
280 if lib.address_is_null then
281 v.fatal "FFI Error: Cannot load foreign code library for {mmodule.name}: {dlerror.to_s}"
282 return null
283 end
284
285 foreign_code_lib_cache = lib
286 return lib
287 end
288 end