b9a37c4df7311eff92ec46523ccab6a1c395ee9f
[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 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 interpreter.start(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 allocated in C
45 var memory_manager: MemoryManager = new MemoryManager
46
47 # The unique instance of the `MInit` value
48 var initialization_value: Instance is noinit
49
50 init
51 do
52 var init_type = new MInitType(mainmodule.model)
53 initialization_value = new MutableInstance(init_type)
54 super
55 end
56
57 # Subtyping test for the virtual machine
58 redef fun is_subtype(sub, sup: MType): Bool
59 do
60 var anchor = self.frame.arguments.first.mtype.as(MClassType)
61 var sup_accept_null = false
62 if sup isa MNullableType then
63 sup_accept_null = true
64 sup = sup.mtype
65 else if sup isa MNullType then
66 sup_accept_null = true
67 end
68
69 # Can `sub` provides null or not?
70 # Thus we can match with `sup_accept_null`
71 # Also discard the nullable marker if it exists
72 if sub isa MNullableType then
73 if not sup_accept_null then return false
74 sub = sub.mtype
75 else if sub isa MNullType then
76 return sup_accept_null
77 end
78 # Now the case of direct null and nullable is over
79
80 # An unfixed formal type can only accept itself
81 if sup isa MParameterType or sup isa MVirtualType then
82 return sub == sup
83 end
84
85 if sub isa MParameterType or sub isa MVirtualType then
86 sub = sub.anchor_to(mainmodule, anchor)
87 # Manage the second layer of null/nullable
88 if sub isa MNullableType then
89 if not sup_accept_null then return false
90 sub = sub.mtype
91 else if sub isa MNullType then
92 return sup_accept_null
93 end
94 end
95
96 assert sub isa MClassType
97
98 # `sup` accepts only null
99 if sup isa MNullType then return false
100
101 assert sup isa MClassType
102
103 # Create the sup vtable if not create
104 if not sup.mclass.loaded then create_class(sup.mclass)
105
106 # Sub can be discovered inside a Generic type during the subtyping test
107 if not sub.mclass.loaded then create_class(sub.mclass)
108
109 if sup isa MGenericType then
110 var sub2 = sub.supertype_to(mainmodule, anchor, sup.mclass)
111 assert sub2.mclass == sup.mclass
112
113 for i in [0..sup.mclass.arity[ do
114 var sub_arg = sub2.arguments[i]
115 var sup_arg = sup.arguments[i]
116 var res = is_subtype(sub_arg, sup_arg)
117
118 if res == false then return false
119 end
120 return true
121 end
122
123 var super_id = sup.mclass.vtable.id
124 var mask = sub.mclass.vtable.mask
125
126 # For now, we always use perfect hashing for subtyping test
127 return inter_is_subtype_ph(super_id, mask, sub.mclass.vtable.internal_vtable)
128 end
129
130 # Subtyping test with perfect hashing
131 private fun inter_is_subtype_ph(id: Int, mask:Int, vtable: Pointer): Bool `{
132 // hv is the position in hashtable
133 int hv = id & mask;
134
135 // Follow the pointer to somewhere in the vtable
136 long unsigned int *offset = (long unsigned int*)(((long int *)vtable)[-hv]);
137
138 // If the pointed value is corresponding to the identifier, the test is true, otherwise false
139 return *offset == id;
140 `}
141
142 # Subtyping test with Cohen test (direct access)
143 private fun inter_is_subtype_sst(id: Int, position: Int, vtable: Pointer): Bool `{
144 // Direct access to the position given in parameter
145 int tableid = (long unsigned int)((long int *)vtable)[position];
146
147 return id == tableid;
148 `}
149
150 # Redef init_instance to simulate the loading of a class
151 redef fun init_instance(recv: Instance)
152 do
153 if not recv.mtype.as(MClassType).mclass.loaded then create_class(recv.mtype.as(MClassType).mclass)
154
155 recv.vtable = recv.mtype.as(MClassType).mclass.vtable
156
157 assert recv isa MutableInstance
158
159 recv.internal_attributes = init_internal_attributes(initialization_value, recv.mtype.as(MClassType).mclass.all_mattributes(mainmodule, none_visibility).length)
160 super
161 end
162
163 # Associate a `PrimitiveInstance` to its `VTable`
164 redef fun init_instance_primitive(recv: Instance)
165 do
166 if not recv.mtype.as(MClassType).mclass.loaded then create_class(recv.mtype.as(MClassType).mclass)
167
168 recv.vtable = recv.mtype.as(MClassType).mclass.vtable
169 end
170
171 # Create a virtual table for this `MClass` if not already done
172 redef fun get_primitive_class(name: String): MClass
173 do
174 var mclass = super
175
176 if not mclass.loaded then create_class(mclass)
177
178 return mclass
179 end
180
181 # Initialize the internal representation of an object (its attribute values)
182 # `init_instance` is the initial value of attributes
183 private fun init_internal_attributes(init_instance: Instance, size: Int): Pointer
184 import Array[Instance].length, Array[Instance].[] `{
185
186 Instance* attributes = malloc(sizeof(Instance) * size);
187
188 int i;
189 for(i=0; i<size; i++)
190 attributes[i] = init_instance;
191
192 Instance_incr_ref(init_instance);
193 return attributes;
194 `}
195
196 # Creates the runtime structures for this class
197 fun create_class(mclass: MClass) do mclass.make_vt(self)
198
199 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
200 redef fun send(mproperty: MMethod, args: Array[Instance]): nullable Instance
201 do
202 var recv = args.first
203 var mtype = recv.mtype
204 var ret = send_commons(mproperty, args, mtype)
205 if ret != null then return ret
206
207 var propdef = method_dispatch(mproperty, recv.vtable.as(not null), recv)
208
209 return self.call(propdef, args)
210 end
211
212 # Method dispatch, for a given global method `mproperty`
213 # returns the most specific local method in the class corresponding to `vtable`
214 private fun method_dispatch(mproperty: MMethod, vtable: VTable, recv: Instance): MMethodDef
215 do
216 if mproperty.intro_mclassdef.mclass.positions_methods[recv.mtype.as(MClassType).mclass] != -1 then
217 return method_dispatch_sst(vtable.internal_vtable, mproperty.absolute_offset)
218 else
219 return method_dispatch_ph(vtable.internal_vtable, vtable.mask,
220 mproperty.intro_mclassdef.mclass.vtable.id, mproperty.offset)
221 end
222 end
223
224 # Execute a method dispatch with perfect hashing
225 private fun method_dispatch_ph(vtable: Pointer, mask: Int, id: Int, offset: Int): MMethodDef `{
226 // Perfect hashing position
227 int hv = mask & id;
228 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
229
230 // pointer+2 is the position where methods are
231 // Add the offset of property and get the method implementation
232 MMethodDef propdef = (MMethodDef)*(pointer + 2 + offset);
233
234 return propdef;
235 `}
236
237 # Execute a method dispatch with direct access and return the appropriate `MMethodDef`
238 # `vtable` : Pointer to the internal pointer of the class
239 # `absolute_offset` : Absolute offset from the beginning of the virtual table
240 private fun method_dispatch_sst(vtable: Pointer, absolute_offset: Int): MMethodDef `{
241 // pointer+2 is the position where methods are
242 // Add the offset of property and get the method implementation
243 MMethodDef propdef = (MMethodDef)((long int *)vtable)[absolute_offset];
244
245 return propdef;
246 `}
247
248 # Return the value of the attribute `mproperty` for the object `recv`
249 redef fun read_attribute(mproperty: MAttribute, recv: Instance): Instance
250 do
251 assert recv isa MutableInstance
252
253 var i: Instance
254
255 if mproperty.intro_mclassdef.mclass.positions_attributes[recv.mtype.as(MClassType).mclass] != -1 then
256 # if this attribute class has an unique position for this receiver, then use direct access
257 i = read_attribute_sst(recv.internal_attributes, mproperty.absolute_offset)
258 else
259 # Otherwise, read the attribute value with perfect hashing
260 var id = mproperty.intro_mclassdef.mclass.vtable.id
261
262 i = read_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
263 recv.vtable.mask, id, mproperty.offset)
264 end
265
266 # If we get a `MInit` value, throw an error
267 if i == initialization_value then
268 fatal("Uninitialized attribute {mproperty.name}")
269 abort
270 end
271
272 return i
273 end
274
275 # Return the attribute value in `instance` with a sequence of perfect_hashing
276 # * `instance` is the attributes array of the receiver
277 # * `vtable` is the pointer to the virtual table of the class (of the receiver)
278 # * `mask` is the perfect hashing mask of the class
279 # * `id` is the identifier of the class
280 # * `offset` is the relative offset of this attribute
281 private fun read_attribute_ph(instance: Pointer, vtable: Pointer, mask: Int, id: Int, offset: Int): Instance `{
282 // Perfect hashing position
283 int hv = mask & id;
284 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
285
286 // pointer+1 is the position where the delta of the class is
287 int absolute_offset = *(pointer + 1);
288
289 Instance res = ((Instance *)instance)[absolute_offset + offset];
290
291 return res;
292 `}
293
294 # Return the attribute value in `instance` with a direct access (SST)
295 # * `instance` is the attributes array of the receiver
296 # * `offset` is the absolute offset of this attribute
297 private fun read_attribute_sst(instance: Pointer, offset: Int): Instance `{
298 /* We can make a direct access to the attribute value
299 because this attribute is always at the same position
300 for the class of this receiver */
301 Instance res = ((Instance *)instance)[offset];
302
303 return res;
304 `}
305
306 # Replace in `recv` the value of the attribute `mproperty` by `value`
307 redef fun write_attribute(mproperty: MAttribute, recv: Instance, value: Instance)
308 do
309 assert recv isa MutableInstance
310
311 # Replace the old value of mproperty in recv
312 if mproperty.intro_mclassdef.mclass.positions_attributes[recv.mtype.as(MClassType).mclass] != -1 then
313 # if this attribute class has an unique position for this receiver, then use direct access
314 write_attribute_sst(recv.internal_attributes, mproperty.absolute_offset, value)
315 else
316 # Otherwise, use perfect hashing to replace the old value
317 var id = mproperty.intro_mclassdef.mclass.vtable.id
318
319 write_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
320 recv.vtable.mask, id, mproperty.offset, value)
321 end
322 end
323
324 # Replace the value of an attribute in an instance
325 # * `instance` is the attributes array of the receiver
326 # * `vtable` is the pointer to the virtual table of the class (of the receiver)
327 # * `mask` is the perfect hashing mask of the class
328 # * `id` is the identifier of the class
329 # * `offset` is the relative offset of this attribute
330 # * `value` is the new value for this attribute
331 private fun write_attribute_ph(instance: Pointer, vtable: Pointer, mask: Int, id: Int, offset: Int, value: Instance) `{
332 // Perfect hashing position
333 int hv = mask & id;
334 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
335
336 // pointer+1 is the position where the delta of the class is
337 int absolute_offset = *(pointer + 1);
338
339 ((Instance *)instance)[absolute_offset + offset] = value;
340 Instance_incr_ref(value);
341 `}
342
343 # Replace the value of an attribute in an instance with direct access
344 # * `instance` is the attributes array of the receiver
345 # * `offset` is the absolute offset of this attribute
346 # * `value` is the new value for this attribute
347 private fun write_attribute_sst(instance: Pointer, offset: Int, value: Instance) `{
348 // Direct access to the position with the absolute offset
349 ((Instance *)instance)[offset] = value;
350 Instance_incr_ref(value);
351 `}
352
353 # Is the attribute `mproperty` initialized in the instance `recv`?
354 redef fun isset_attribute(mproperty: MAttribute, recv: Instance): Bool
355 do
356 assert recv isa MutableInstance
357
358 # Read the attribute value with internal perfect hashing read
359 # because we do not want to throw an error if the value is `initialization_value`
360 var id = mproperty.intro_mclassdef.mclass.vtable.id
361
362 var i = read_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
363 recv.vtable.mask, id, mproperty.offset)
364
365 return i != initialization_value
366 end
367 end
368
369 redef class MClass
370 # A reference to the virtual table of this class
371 var vtable: nullable VTable
372
373 # True when the class is effectively loaded by the vm, false otherwise
374 var loaded: Bool = false
375
376 # Color for Cohen subtyping test : the absolute position of the id
377 # of this class in virtual tables
378 var color: Int
379
380 # For each loaded subclass, keep the position of the group of attributes
381 # introduced by self class in the object
382 var positions_attributes: HashMap[MClass, Int] = new HashMap[MClass, Int]
383
384 # For each loaded subclass, keep the position of the group of methods
385 # introduced by self class in the vtable
386 var positions_methods: HashMap[MClass, Int] = new HashMap[MClass, Int]
387
388 # Allocates a VTable for this class and gives it an id
389 private fun make_vt(v: VirtualMachine)
390 do
391 if loaded then return
392
393 # `superclasses` contains the order of superclasses for virtual tables
394 var superclasses = superclasses_ordering(v)
395 superclasses.remove(self)
396
397 # Make_vt for super-classes
398 var ids = new Array[Int]
399 var nb_methods = new Array[Int]
400 var nb_attributes = new Array[Int]
401
402 # Absolute offset of attribute from the beginning of the attributes table
403 var offset_attributes = 0
404
405 # Absolute offset of method from the beginning of the methods table,
406 # is initialize to 3 because the first position is empty in the virtual table
407 # and the second and third are respectively class id and delta
408 var offset_methods = 3
409
410 # The previous element in `superclasses`
411 var previous_parent: nullable MClass = null
412 if superclasses.length > 0 then previous_parent = superclasses[0]
413 for parent in superclasses do
414 if not parent.loaded then parent.make_vt(v)
415
416 # Get the number of introduced methods and attributes for this class
417 var methods = 0
418 var attributes = 0
419
420 for p in parent.intro_mproperties(none_visibility) do
421 if p isa MMethod then methods += 1
422 if p isa MAttribute then attributes += 1
423 end
424
425 ids.push(parent.vtable.id)
426 nb_methods.push(methods)
427 nb_attributes.push(attributes)
428
429 # Update `positions_attributes` and `positions_methods` in `parent`.
430 # If the position is invariant for this parent, store this position
431 # else store a special value (-1)
432 var pos_attr = -1
433 var pos_meth = -1
434
435 if previous_parent.as(not null).positions_attributes[parent] == offset_attributes then pos_attr = offset_attributes
436 if previous_parent.as(not null).positions_methods[parent] == offset_methods then pos_meth = offset_methods
437
438 parent.update_positions(pos_attr, pos_meth, self)
439
440 offset_attributes += attributes
441 offset_methods += methods
442 offset_methods += 2 # Because each block starts with an id and the delta
443 end
444
445 # When all super-classes have their identifiers and vtables, allocate current one
446 allocate_vtable(v, ids, nb_methods, nb_attributes, offset_attributes, offset_methods)
447 loaded = true
448
449 # Set the absolute position of the identifier of this class in the virtual table
450 color = offset_methods - 2
451
452 # The virtual table now needs to be filled with pointer to methods
453 superclasses.add(self)
454 for cl in superclasses do
455 fill_vtable(v, vtable.as(not null), cl)
456 end
457 end
458
459 # Allocate a single vtable
460 # * `ids : Array of superclasses identifiers
461 # * `nb_methods : Array which contain the number of introduced methods for each class in ids
462 # * `nb_attributes : Array which contain the number of introduced attributes for each class in ids
463 # * `offset_attributes : Offset from the beginning of the table of the group of attributes
464 # * `offset_methods : Offset from the beginning of the table of the group of methods
465 private fun allocate_vtable(v: VirtualMachine, ids: Array[Int], nb_methods: Array[Int], nb_attributes: Array[Int],
466 offset_attributes: Int, offset_methods: Int)
467 do
468 vtable = new VTable
469 var idc = new Array[Int]
470
471 vtable.mask = v.ph.pnand(ids, 1, idc) - 1
472 vtable.id = idc[0]
473 vtable.classname = name
474
475 # Add current id to Array of super-ids
476 var ids_total = new Array[Int]
477 ids_total.add_all(ids)
478 ids_total.push(vtable.id)
479
480 var nb_methods_total = new Array[Int]
481 var nb_attributes_total = new Array[Int]
482
483 var self_methods = 0
484 var nb_introduced_attributes = 0
485
486 # Fixing offsets for self attributes and methods
487 var relative_offset_attr = 0
488 var relative_offset_meth = 0
489 for p in intro_mproperties(none_visibility) do
490 if p isa MMethod then
491 self_methods += 1
492 p.offset = relative_offset_meth
493 p.absolute_offset = offset_methods + relative_offset_meth
494 relative_offset_meth += 1
495 end
496 if p isa MAttribute then
497 nb_introduced_attributes += 1
498 p.offset = relative_offset_attr
499 p.absolute_offset = offset_attributes + relative_offset_attr
500 relative_offset_attr += 1
501 end
502 end
503
504 nb_methods_total.add_all(nb_methods)
505 nb_methods_total.push(self_methods)
506
507 nb_attributes_total.add_all(nb_attributes)
508 nb_attributes_total.push(nb_introduced_attributes)
509
510 # Save the offsets of self class
511 update_positions(offset_attributes, offset_methods, self)
512
513 # Since we have the number of attributes for each class, calculate the delta
514 var deltas = calculate_delta(nb_attributes_total)
515 vtable.internal_vtable = v.memory_manager.init_vtable(ids_total, nb_methods_total, deltas, vtable.mask)
516 end
517
518 # Fill the vtable with methods of `self` class
519 # * `v` : Current instance of the VirtualMachine
520 # * `table` : the table of self class, will be filled with its methods
521 private fun fill_vtable(v:VirtualMachine, table: VTable, cl: MClass)
522 do
523 var methods = new Array[MMethodDef]
524 for m in cl.intro_mproperties(none_visibility) do
525 if m isa MMethod then
526 # `propdef` is the most specific implementation for this MMethod
527 var propdef = m.lookup_first_definition(v.mainmodule, self.intro.bound_mtype)
528 methods.push(propdef)
529 end
530 end
531
532 # Call a method in C to put propdefs of self methods in the vtables
533 v.memory_manager.put_methods(vtable.internal_vtable, vtable.mask, cl.vtable.id, methods)
534 end
535
536 # Computes delta for each class
537 # A delta represents the offset for this group of attributes in the object
538 # *`nb_attributes` : number of attributes for each class (classes are linearized from Object to current)
539 # * return deltas for each class
540 private fun calculate_delta(nb_attributes: Array[Int]): Array[Int]
541 do
542 var deltas = new Array[Int]
543
544 var total = 0
545 for nb in nb_attributes do
546 deltas.push(total)
547 total += nb
548 end
549
550 return deltas
551 end
552
553 # Order superclasses of self
554 # Return the order of superclasses in runtime structures of this class
555 private fun superclasses_ordering(v: VirtualMachine): Array[MClass]
556 do
557 var superclasses = new Array[MClass]
558 superclasses.add_all(ancestors)
559
560 var res = new Array[MClass]
561 if superclasses.length > 1 then
562 # Starting at self
563 var ordering = self.dfs(v, res)
564
565 return ordering
566 else
567 # There is no super-class, self is Object
568 return superclasses
569 end
570 end
571
572 # A kind of Depth-First-Search for superclasses ordering
573 # *`v` : the current executed instance of VirtualMachine
574 # * `res` : Result Array, ie current superclasses ordering
575 private fun dfs(v: VirtualMachine, res: Array[MClass]): Array[MClass]
576 do
577 # Add this class at the beginning
578 res.insert(self, 0)
579
580 var direct_parents = self.in_hierarchy(v.mainmodule).direct_greaters.to_a
581
582 if direct_parents.length > 1 then
583 # Prefix represents the class which has the most properties
584 # we try to choose it in first to reduce the number of potential recompilations
585 var prefix = null
586 var max = -1
587 for cl in direct_parents do
588 # If we never have visited this class
589 if not res.has(cl) then
590 var properties_length = cl.all_mproperties(v.mainmodule, none_visibility).length
591 if properties_length > max then
592 max = properties_length
593 prefix = cl
594 end
595 end
596 end
597
598 if prefix != null then
599 # Add the prefix class ordering at the beginning of our sequence
600 var prefix_res = new Array[MClass]
601 prefix_res = prefix.dfs(v, prefix_res)
602
603 # Then we recurse on other classes
604 for cl in direct_parents do
605 if cl != prefix then
606 res = new Array[MClass]
607 res = cl.dfs(v, res)
608
609 for cl_res in res do
610 if not prefix_res.has(cl_res) then prefix_res.push(cl_res)
611 end
612 end
613 end
614 res = prefix_res
615 end
616
617 res.push(self)
618 else
619 if direct_parents.length > 0 then
620 res = direct_parents.first.dfs(v, res)
621 end
622 end
623
624 if not res.has(self) then res.push(self)
625
626 return res
627 end
628
629 # Update positions of the class `cl`
630 # * `attributes_offset`: absolute offset of introduced attributes
631 # * `methods_offset`: absolute offset of introduced methods
632 private fun update_positions(attributes_offsets: Int, methods_offset:Int, cl: MClass)
633 do
634 positions_attributes[cl] = attributes_offsets
635 positions_methods[cl] = methods_offset
636 end
637 end
638
639 redef class MAttribute
640 # Relative offset of this attribute in the runtime instance
641 # (beginning of the block of its introducing class)
642 var offset: Int
643
644 # Absolute offset of this attribute in the runtime instance (beginning of the attribute table)
645 var absolute_offset: Int
646 end
647
648 redef class MMethod
649 # Relative offset of this method in the virtual table (from the beginning of the block)
650 var offset: Int
651
652 # Absolute offset of this method in the virtual table (from the beginning of the vtable)
653 var absolute_offset: Int
654 end
655
656 # Redef MutableInstance to improve implementation of attributes in objects
657 redef class MutableInstance
658
659 # C-array to store pointers to attributes of this Object
660 var internal_attributes: Pointer
661 end
662
663 # Redef to associate an `Instance` to its `VTable`
664 redef class Instance
665
666 # Associate a runtime instance to its virtual table which contains methods, types etc.
667 var vtable: nullable VTable
668 end
669
670 # Is the type of the initial value inside attributes
671 class MInitType
672 super MType
673
674 redef var model: Model
675
676 redef fun to_s do return "InitType"
677 redef fun as_nullable do return self
678 redef fun need_anchor do return false
679 redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do return self
680 redef fun can_resolve_for(mtype, anchor, mmodule) do return true
681
682 redef fun collect_mclassdefs(mmodule) do return new HashSet[MClassDef]
683
684 redef fun collect_mclasses(mmodule) do return new HashSet[MClass]
685
686 redef fun collect_mtypes(mmodule) do return new HashSet[MClassType]
687 end
688
689 # A VTable contains the virtual method table for the dispatch
690 # and informations to perform subtyping tests
691 class VTable
692 # The mask to perform perfect hashing
693 var mask: Int is noinit
694
695 # Unique identifier given by perfect hashing
696 var id: Int is noinit
697
698 # Pointer to the c-allocated area, represents the virtual table
699 var internal_vtable: Pointer is noinit
700
701 # The short classname of this class
702 var classname: String is noinit
703 end
704
705 # Handle memory, used for allocate virtual table and associated structures
706 class MemoryManager
707
708 # Allocate and fill a virtual table
709 fun init_vtable(ids: Array[Int], nb_methods: Array[Int], nb_attributes: Array[Int], mask: Int): Pointer
710 do
711 # Allocate in C current virtual table
712 var res = intern_init_vtable(ids, nb_methods, nb_attributes, mask)
713
714 return res
715 end
716
717 # Construct virtual tables with a bi-dimensional layout
718 private fun intern_init_vtable(ids: Array[Int], nb_methods: Array[Int], deltas: Array[Int], mask: Int): Pointer
719 import Array[Int].length, Array[Int].[] `{
720
721 // Allocate and fill current virtual table
722 int i;
723 int total_size = 0; // total size of this virtual table
724 int nb_classes = Array_of_Int_length(nb_methods);
725 for(i = 0; i<nb_classes; i++) {
726 /* - One for each method of this class
727 * - One for the delta (offset of this group of attributes in objects)
728 * - One for the id
729 */
730 total_size += Array_of_Int__index(nb_methods, i);
731 total_size += 2;
732 }
733
734 // Add the size of the perfect hashtable (mask +1)
735 // Add one because we start to fill the vtable at position 1 (0 is the init position)
736 total_size += mask+2;
737 long unsigned int* vtable = malloc(sizeof(long unsigned int)*total_size);
738
739 // Initialisation to the first position of the virtual table (ie : Object)
740 long unsigned int *init = vtable + mask + 2;
741 for(i=0; i<total_size; i++)
742 vtable[i] = (long unsigned int)init;
743
744 // Set the virtual table to its position 0
745 // ie: after the hashtable
746 vtable = vtable + mask + 1;
747
748 int current_size = 1;
749 for(i = 0; i < nb_classes; i++) {
750 /*
751 vtable[hv] contains a pointer to the group of introduced methods
752 For each superclasse we have in virtual table :
753 (id | delta | introduced methods)
754 */
755 int hv = mask & Array_of_Int__index(ids, i);
756
757 vtable[current_size] = Array_of_Int__index(ids, i);
758 vtable[current_size + 1] = Array_of_Int__index(deltas, i);
759 vtable[-hv] = (long unsigned int)&(vtable[current_size]);
760
761 current_size += 2;
762 current_size += Array_of_Int__index(nb_methods, i);
763 }
764
765 return vtable;
766 `}
767
768 # Put implementation of methods of a class in `vtable`
769 # * `vtable` : Pointer to the C-virtual table
770 # * `mask` : perfect-hashing mask of the class corresponding to the vtable
771 # * `id` : id of the target class
772 # * `methods` : array of MMethodDef of the target class
773 fun put_methods(vtable: Pointer, mask: Int, id: Int, methods: Array[MMethodDef])
774 import Array[MMethodDef].length, Array[MMethodDef].[] `{
775
776 // Get the area to fill with methods by a sequence of perfect hashing
777 int hv = mask & id;
778 long unsigned int *pointer = (long unsigned int*)(((long unsigned int *)vtable)[-hv]);
779
780 // pointer+2 is the beginning of the area for methods implementation
781 int length = Array_of_MMethodDef_length(methods);
782 long unsigned int *area = (pointer + 2);
783 int i;
784
785 for(i=0; i<length; i++)
786 {
787 MMethodDef method = Array_of_MMethodDef__index(methods, i);
788 area[i] = (long unsigned int)method;
789 MMethodDef_incr_ref(method);
790 }
791 `}
792 end