3e4f4af62993be0c5d701061b87e8bf82ec2432b
[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 (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 var instance = new PrimitiveInstance[Pointer](static_type, self.pointer)
155 v.init_instance_primitive instance
156 return instance
157 else
158 return self.instance
159 end
160 end
161 end
162
163 # Handle to foreign code library
164 private extern class ForeignCodeLib
165 # Open and load the library at `path`
166 new dlopen(path: NativeString) `{
167 return dlopen(path, RTLD_LOCAL | RTLD_NOW);
168 `}
169
170 # Find the `ForeignCodeEntry` at `symbol_name`
171 fun dlsym(symbol_name: NativeString): ForeignCodeEntry `{
172 return dlsym(self, symbol_name);
173 `}
174 end
175
176 private fun dlerror: NativeString `{ return dlerror(); `}
177
178 # Handle to an implementation function in a `ForeignCodeLib`
179 private extern class ForeignCodeEntry`{ nit_foreign_lib_entry `}
180
181 # Invoke the implementation function by passing `args` and receive the return value
182 fun call(argc: Int, args, ret: CallArg): Bool `{
183 return ((nit_foreign_lib_entry)self)(argc, args, ret);
184 `}
185 end
186
187 redef class AMethPropdef
188 # Handle to the entrypoint of this method in the foreign code library
189 private var foreign_entry_cache: nullable ForeignCodeEntry = null
190
191 redef fun call_extern(v, mpropdef, args, frame)
192 do
193 # Fallback the default error if this method is not supported
194 if not supported_by_dynamic_ffi then return super
195
196 var entry = foreign_entry_cache
197 if entry == null then
198 # Get handle to foreign code lib
199 var amodule = v.modelbuilder.mmodule2node(mpropdef.mclassdef.mmodule)
200 assert amodule != null
201
202 var lib = amodule.foreign_code_lib(v)
203 if lib == null then return v.error_instance
204
205 # Get handle to implementation function
206 entry = lib.dlsym(mpropdef.foreign_lib_entry_cname.to_cstring)
207 if entry.address_is_null then
208 print mpropdef.foreign_lib_entry_cname
209 v.fatal "FFI Error: Cannot find method {mpropdef.name} in foreign code library."
210 return v.error_instance
211 end
212
213 foreign_entry_cache = entry
214 end
215
216 # Prepare to send args to foreign code lib
217 var is_init = mpropdef.mproperty.is_init
218 if is_init then args.shift
219 var native_args_length = args.length
220
221 var native_args = new CallArg(args.length)
222 var a = 0
223 if not is_init then
224 var arg = args[a]
225 var native_arg = native_args[a]
226 native_arg.from_static_type(arg, mpropdef.mclassdef.mclass.mclass_type)
227 a += 1
228 end
229 for param in mpropdef.msignature.mparameters do
230 var arg = args[a]
231 var native_arg = native_args[a]
232 native_arg.from_static_type(arg, param.mtype)
233 a += 1
234 end
235
236 # Allocate memory for the return value
237 var native_return = new CallArg(1)
238 var error = entry.call(native_args_length, native_args, native_return)
239
240 if error then
241 v.fatal "FFI Error: Native code library reported an error"
242 return null
243 end
244
245 # Get the result
246 var return_mtype = mpropdef.msignature.return_mtype
247 if is_init then return_mtype = mpropdef.mclassdef.mclass.mclass_type
248
249 var return_value
250 if return_mtype == null then
251 return_value = null
252 else
253 return_value = native_return.to_instance(return_mtype, v)
254 end
255
256 native_args.free
257 native_return.free
258
259 return return_value
260 end
261 end
262
263 redef class AModule
264
265 private var foreign_code_lib_cache: nullable ForeignCodeLib = null
266
267 # Handle to the external library with FFI code
268 private fun foreign_code_lib(v: NaiveInterpreter): nullable ForeignCodeLib
269 do
270 var lib = foreign_code_lib_cache
271 if lib != null then return lib
272
273 var mmodule = mmodule
274 assert mmodule != null
275
276 var foreign_code_lib_path = v.foreign_code_lib_path(mmodule)
277
278 # Compile lib
279 compile_foreign_lib v
280
281 lib = new ForeignCodeLib.dlopen(foreign_code_lib_path.to_cstring)
282 if lib.address_is_null then
283 v.fatal "FFI Error: Cannot load foreign code library for {mmodule.name}: {dlerror.to_s}"
284 return null
285 end
286
287 foreign_code_lib_cache = lib
288 return lib
289 end
290 end