b5e5bb8c4e48b6adb04187d8979a01663a9d4135
[nit.git] / src / vm.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Julien Pagès <julien.pages@lirmm.fr>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Implementation of the Nit virtual machine
18 module vm
19
20 intrude import interpreter::naive_interpreter
21 import model_utils
22 import perfect_hashing
23
24 redef class ModelBuilder
25 redef fun run_naive_interpreter(mainmodule: MModule, arguments: Array[String])
26 do
27 var time0 = get_time
28 self.toolcontext.info("*** NITVM STARTING ***", 1)
29
30 var interpreter = new VirtualMachine(self, mainmodule, arguments)
31 init_naive_interpreter(interpreter, mainmodule)
32
33 var time1 = get_time
34 self.toolcontext.info("*** NITVM STOPPING : {time1-time0} ***", 2)
35 end
36 end
37
38 # A virtual machine based on the naive_interpreter
39 class VirtualMachine super NaiveInterpreter
40
41 # Perfect hashing and perfect numbering
42 var ph: Perfecthashing = new Perfecthashing
43
44 # Handles memory and garbage collection
45 var memory_manager: MemoryManager = new MemoryManager
46
47 # Subtyping test for the virtual machine
48 redef fun is_subtype(sub, sup: MType): Bool
49 do
50 var anchor = self.frame.arguments.first.mtype.as(MClassType)
51 var sup_accept_null = false
52 if sup isa MNullableType then
53 sup_accept_null = true
54 sup = sup.mtype
55 else if sup isa MNullType then
56 sup_accept_null = true
57 end
58
59 # Can `sub` provides null or not?
60 # Thus we can match with `sup_accept_null`
61 # Also discard the nullable marker if it exists
62 if sub isa MNullableType then
63 if not sup_accept_null then return false
64 sub = sub.mtype
65 else if sub isa MNullType then
66 return sup_accept_null
67 end
68 # Now the case of direct null and nullable is over
69
70 # An unfixed formal type can only accept itself
71 if sup isa MParameterType or sup isa MVirtualType then
72 return sub == sup
73 end
74
75 if sub isa MParameterType or sub isa MVirtualType then
76 sub = sub.anchor_to(mainmodule, anchor)
77 # Manage the second layer of null/nullable
78 if sub isa MNullableType then
79 if not sup_accept_null then return false
80 sub = sub.mtype
81 else if sub isa MNullType then
82 return sup_accept_null
83 end
84 end
85
86 assert sub isa MClassType
87
88 # `sup` accepts only null
89 if sup isa MNullType then return false
90
91 assert sup isa MClassType
92
93 # Create the sup vtable if not create
94 if not sup.mclass.loaded then create_class(sup.mclass)
95
96 # Sub can be discovered inside a Generic type during the subtyping test
97 if not sub.mclass.loaded then create_class(sub.mclass)
98
99 if anchor == null then anchor = sub
100 if sup isa MGenericType then
101 var sub2 = sub.supertype_to(mainmodule, anchor, sup.mclass)
102 assert sub2.mclass == sup.mclass
103
104 for i in [0..sup.mclass.arity[ do
105 var sub_arg = sub2.arguments[i]
106 var sup_arg = sup.arguments[i]
107 var res = is_subtype(sub_arg, sup_arg)
108
109 if res == false then return false
110 end
111 return true
112 end
113
114 var super_id = sup.mclass.vtable.id
115 var mask = sub.mclass.vtable.mask
116
117 return inter_is_subtype(super_id, mask, sub.mclass.vtable.internal_vtable)
118 end
119
120 # Subtyping test with perfect hashing
121 private fun inter_is_subtype(id: Int, mask:Int, vtable: Pointer): Bool `{
122 // hv is the position in hashtable
123 int hv = id & mask;
124
125 // Follow the pointer to somewhere in the vtable
126 long unsigned int *offset = (long unsigned int*)(((long int *)vtable)[-hv]);
127
128 // If the pointed value is corresponding to the identifier, the test is true, otherwise false
129 return *offset == id;
130 `}
131
132 # Redef init_instance to simulate the loading of a class
133 redef fun init_instance(recv: Instance)
134 do
135 if not recv.mtype.as(MClassType).mclass.loaded then create_class(recv.mtype.as(MClassType).mclass)
136
137 recv.vtable = recv.mtype.as(MClassType).mclass.vtable
138
139 assert(recv isa MutableInstance)
140
141 recv.internal_attributes = init_internal_attributes(null_instance, recv.mtype.as(MClassType).mclass.all_mattributes(mainmodule, none_visibility).length)
142 super
143 end
144
145 # Initialize the internal representation of an object (its attribute values)
146 private fun init_internal_attributes(null_instance: Instance, size: Int): Pointer
147 import Array[Instance].length, Array[Instance].[] `{
148
149 Instance* attributes = malloc(sizeof(Instance) * size);
150
151 int i;
152 for(i=0; i<size; i++)
153 attributes[i] = null_instance;
154
155 Instance_incr_ref(null_instance);
156 return attributes;
157 `}
158
159 # Creates the runtime structures for this class
160 fun create_class(mclass: MClass) do mclass.make_vt(self)
161
162 # Return the value of the attribute `mproperty` for the object `recv`
163 redef fun read_attribute(mproperty: MAttribute, recv: Instance): Instance
164 do
165 assert recv isa MutableInstance
166
167 # Read the attribute value with perfect hashing
168 var id = mproperty.intro_mclassdef.mclass.vtable.id
169
170 var i = read_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
171 recv.vtable.mask, id, mproperty.offset)
172
173 return i
174 end
175
176 # Return the attribute value in `instance` with a sequence of perfect_hashing
177 # `instance` is the attributes array of the receiver
178 # `vtable` is the pointer to the virtual table of the class (of the receiver)
179 # `mask` is the perfect hashing mask of the class
180 # `id` is the identifier of the class
181 # `offset` is the relative offset of this attribute
182 private fun read_attribute_ph(instance: Pointer, vtable: Pointer, mask: Int, id: Int, offset: Int): Instance `{
183 // Perfect hashing position
184 int hv = mask & id;
185 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
186
187 // pointer+1 is the position where the delta of the class is
188 int absolute_offset = *(pointer + 1);
189
190 Instance res = ((Instance *)instance)[absolute_offset + offset];
191
192 return res;
193 `}
194
195 # Replace in `recv` the value of the attribute `mproperty` by `value`
196 redef fun write_attribute(mproperty: MAttribute, recv: Instance, value: Instance)
197 do
198 assert recv isa MutableInstance
199
200 var id = mproperty.intro_mclassdef.mclass.vtable.id
201
202 # Replace the old value of mproperty in recv
203 write_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
204 recv.vtable.mask, id, mproperty.offset, value)
205 end
206
207 # Replace the value of an attribute in an instance
208 # `instance` is the attributes array of the receiver
209 # `vtable` is the pointer to the virtual table of the class (of the receiver)
210 # `mask` is the perfect hashing mask of the class
211 # `id` is the identifier of the class
212 # `offset` is the relative offset of this attribute
213 # `value` is the new value for this attribute
214 private fun write_attribute_ph(instance: Pointer, vtable: Pointer, mask: Int, id: Int, offset: Int, value: Instance) `{
215 // Perfect hashing position
216 int hv = mask & id;
217 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
218
219 // pointer+1 is the position where the delta of the class is
220 int absolute_offset = *(pointer + 1);
221
222 ((Instance *)instance)[absolute_offset + offset] = value;
223 Instance_incr_ref(value);
224 `}
225 end
226
227 redef class MClass
228 # A reference to the virtual table of this class
229 var vtable: nullable VTable
230
231 # True when the class is effectively loaded by the vm, false otherwise
232 var loaded: Bool = false
233
234 # For each loaded subclass, keep the position of the group of attributes
235 # introduced by self class in the object
236 var positions_attributes: HashMap[MClass, Int] = new HashMap[MClass, Int]
237
238 # For each loaded subclass, keep the position of the group of methods
239 # introduced by self class in the vtable
240 var positions_methods: HashMap[MClass, Int] = new HashMap[MClass, Int]
241
242 # Allocates a VTable for this class and gives it an id
243 private fun make_vt(v: VirtualMachine)
244 do
245 if loaded then return
246
247 # `superclasses` contains the order of superclasses for virtual tables
248 var superclasses = superclasses_ordering(v)
249 superclasses.remove(self)
250
251 # Make_vt for super-classes
252 var ids = new Array[Int]
253 var nb_methods = new Array[Int]
254 var nb_attributes = new Array[Int]
255
256 # Absolute offset of the beginning of the attributes table
257 var offset_attributes = 0
258 # Absolute offset of the beginning of the methods table
259 var offset_methods = 0
260
261 for parent in superclasses do
262 if not parent.loaded then parent.make_vt(v)
263
264 # Get the number of introduced methods and attributes for this class
265 var methods = 0
266 var attributes = 0
267
268 for p in parent.intro_mproperties(none_visibility) do
269 if p isa MMethod then methods += 1
270 if p isa MAttribute then
271 attributes += 1
272 end
273 end
274
275 ids.push(parent.vtable.id)
276 nb_methods.push(methods)
277 nb_attributes.push(attributes)
278
279 # Update `positions_attributes` and `positions_methods` in `parent`
280 update_positions(offset_attributes, offset_methods, parent)
281
282 offset_attributes += attributes
283 offset_methods += methods
284 end
285
286 # When all super-classes have their identifiers and vtables, allocate current one
287 allocate_vtable(v, ids, nb_methods, nb_attributes, offset_attributes, offset_methods)
288 loaded = true
289 # The virtual table now needs to be filled with pointer to methods
290 end
291
292 # Allocate a single vtable
293 # `ids : Array of superclasses identifiers
294 # `nb_methods : Array which contain the number of introduced methods for each class in ids
295 # `nb_attributes : Array which contain the number of introduced attributes for each class in ids
296 # `offset_attributes : Offset from the beginning of the table of the group of attributes
297 # `offset_methods : Offset from the beginning of the table of the group of methods
298 private fun allocate_vtable(v: VirtualMachine, ids: Array[Int], nb_methods: Array[Int], nb_attributes: Array[Int],
299 offset_attributes: Int, offset_methods: Int)
300 do
301 vtable = new VTable
302 var idc = new Array[Int]
303
304 vtable.mask = v.ph.pnand(ids, 1, idc) - 1
305 vtable.id = idc[0]
306 vtable.classname = name
307
308 # Add current id to Array of super-ids
309 var ids_total = new Array[Int]
310 ids_total.add_all(ids)
311 ids_total.push(vtable.id)
312
313 var nb_methods_total = new Array[Int]
314 var nb_attributes_total = new Array[Int]
315
316 var self_methods = 0
317 var nb_introduced_attributes = 0
318
319 # For self attributes, fixing offsets
320 var relative_offset = 0
321 for p in intro_mproperties(none_visibility) do
322 if p isa MMethod then self_methods += 1
323 if p isa MAttribute then
324 nb_introduced_attributes += 1
325 p.offset = relative_offset
326 relative_offset += 1
327 end
328 end
329
330 nb_methods_total.add_all(nb_methods)
331 nb_methods_total.push(self_methods)
332
333 nb_attributes_total.add_all(nb_attributes)
334 nb_attributes_total.push(nb_introduced_attributes)
335
336 # Save the offsets of self class
337 offset_attributes += nb_introduced_attributes
338 offset_methods += self_methods
339 update_positions(offset_attributes, offset_methods, self)
340
341 # Since we have the number of attributes for each class, calculate the delta
342 var d = calculate_delta(nb_attributes_total)
343 vtable.internal_vtable = v.memory_manager.init_vtable(ids_total, nb_methods_total, d, vtable.mask)
344 end
345
346 # Computes delta for each class
347 # A delta represents the offset for this group of attributes in the object
348 # `nb_attributes` : number of attributes for each class (classes are linearized from Object to current)
349 # return deltas for each class
350 private fun calculate_delta(nb_attributes: Array[Int]): Array[Int]
351 do
352 var deltas = new Array[Int]
353
354 var total = 0
355 for nb in nb_attributes do
356 deltas.push(total)
357 total += nb
358 end
359
360 return deltas
361 end
362
363 # Order superclasses of self
364 # Return the order of superclasses in runtime structures of this class
365 private fun superclasses_ordering(v: VirtualMachine): Array[MClass]
366 do
367 var superclasses = new Array[MClass]
368 superclasses.add_all(ancestors)
369
370 var res = new Array[MClass]
371 if superclasses.length > 1 then
372 # Starting at self
373 var ordering = self.dfs(v, res)
374
375 return ordering
376 else
377 # There is no super-class, self is Object
378 return superclasses
379 end
380 end
381
382 # A kind of Depth-First-Search for superclasses ordering
383 # `v` : the current executed instance of VirtualMachine
384 # `res` : Result Array, ie current superclasses ordering
385 private fun dfs(v: VirtualMachine, res: Array[MClass]): Array[MClass]
386 do
387 # Add this class at the beginning
388 res.insert(self, 0)
389
390 var direct_parents = self.in_hierarchy(v.mainmodule).direct_greaters.to_a
391
392 if direct_parents.length > 1 then
393 # Prefix represents the class which has the most properties
394 # we try to choose it in first to reduce the number of potential recompilations
395 var prefix = null
396 var max = -1
397 for cl in direct_parents do
398 # If we never have visited this class
399 if not res.has(cl) then
400 var properties_length = cl.all_mproperties(v.mainmodule, none_visibility).length
401 if properties_length > max then
402 max = properties_length
403 prefix = cl
404 end
405 end
406 end
407
408 if prefix != null then
409 # Add the prefix class ordering at the beginning of our sequence
410 var prefix_res = new Array[MClass]
411 prefix_res = prefix.dfs(v, prefix_res)
412
413 # Then we recurse on other classes
414 for cl in direct_parents do
415 if cl != prefix then
416 res = new Array[MClass]
417 res = cl.dfs(v, res)
418
419 for cl_res in res do
420 if not prefix_res.has(cl_res) then prefix_res.push(cl_res)
421 end
422 end
423 end
424 res = prefix_res
425 end
426
427 res.push(self)
428 else
429 if direct_parents.length > 0 then
430 res = direct_parents.first.dfs(v, res)
431 end
432 end
433
434 if not res.has(self) then res.push(self)
435
436 return res
437 end
438
439 # Update positions of self class in `parent`
440 # `attributes_offset`: absolute offset of introduced attributes
441 # `methods_offset`: absolute offset of introduced methods
442 private fun update_positions(attributes_offsets: Int, methods_offset:Int, parent: MClass)
443 do
444 parent.positions_attributes[self] = attributes_offsets
445 parent.positions_methods[self] = methods_offset
446 end
447 end
448
449 redef class MAttribute
450 # Represents the relative offset of this attribute in the runtime instance
451 var offset: Int
452 end
453
454 # Redef MutableInstance to improve implementation of attributes in objects
455 redef class MutableInstance
456
457 # C-array to store pointers to attributes of this Object
458 var internal_attributes: Pointer
459 end
460
461 # A VTable contains the virtual method table for the dispatch
462 # and informations to perform subtyping tests
463 class VTable
464 # The mask to perform perfect hashing
465 var mask: Int is noinit
466
467 # Unique identifier given by perfect hashing
468 var id: Int is noinit
469
470 # Pointer to the c-allocated area, represents the virtual table
471 var internal_vtable: Pointer is noinit
472
473 # The short classname of this class
474 var classname: String is noinit
475 end
476
477 redef class Instance
478 var vtable: nullable VTable
479 end
480
481 # Handle memory, used for allocate virtual table and associated structures
482 class MemoryManager
483
484 # Allocate and fill a virtual table
485 fun init_vtable(ids: Array[Int], nb_methods: Array[Int], nb_attributes: Array[Int], mask: Int): Pointer
486 do
487 # Allocate in C current virtual table
488 var res = intern_init_vtable(ids, nb_methods, nb_attributes, mask)
489
490 return res
491 end
492
493 # Construct virtual tables with a bi-dimensional layout
494 private fun intern_init_vtable(ids: Array[Int], nb_methods: Array[Int], deltas: Array[Int], mask: Int): Pointer
495 import Array[Int].length, Array[Int].[] `{
496
497 // Allocate and fill current virtual table
498 int i;
499 int total_size = 0; // total size of this virtual table
500 int nb_classes = Array_of_Int_length(nb_methods);
501 for(i = 0; i<nb_classes; i++) {
502 /* - One for each method of this class
503 * - One for the delta (offset of this group of attributes in objects)
504 * - One for the id
505 */
506 total_size += Array_of_Int__index(nb_methods, i);
507 total_size += 2;
508 }
509
510 // Add the size of the perfect hashtable (mask +1)
511 // Add one because we start to fill the vtable at position 1 (0 is the init position)
512 total_size += mask+2;
513 long unsigned int* vtable = malloc(sizeof(long unsigned int)*total_size);
514
515 // Initialisation to the first position of the virtual table (ie : Object)
516 long unsigned int *init = vtable + mask + 2;
517 for(i=0; i<total_size; i++)
518 vtable[i] = (long unsigned int)init;
519
520 // Set the virtual table to its position 0
521 // ie: after the hashtable
522 vtable = vtable + mask + 1;
523
524 int current_size = 1;
525 for(i = 0; i < nb_classes; i++) {
526 /*
527 vtable[hv] contains a pointer to the group of introduced methods
528 For each superclasse we have in virtual table :
529 (id | delta | introduced methods)
530 */
531 int hv = mask & Array_of_Int__index(ids, i);
532
533 vtable[current_size] = Array_of_Int__index(ids, i);
534 vtable[current_size + 1] = Array_of_Int__index(deltas, i);
535 vtable[-hv] = (long unsigned int)&(vtable[current_size]);
536
537 current_size += 2;
538 current_size += Array_of_Int__index(nb_methods, i);
539 }
540
541 return vtable;
542 `}
543 end