b9a37c4df7311eff92ec46523ccab6a1c395ee9f
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Julien Pagès <julien.pages@lirmm.fr>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Implementation of the Nit virtual machine
20 import interpreter
::naive_interpreter
22 import perfect_hashing
24 redef class ModelBuilder
25 redef fun run_naive_interpreter
(mainmodule
: MModule, arguments
: Array[String])
28 self.toolcontext
.info
("*** NITVM STARTING ***", 1)
30 var interpreter
= new VirtualMachine(self, mainmodule
, arguments
)
31 interpreter
.start
(mainmodule
)
34 self.toolcontext
.info
("*** NITVM STOPPING : {time1-time0} ***", 2)
38 # A virtual machine based on the naive_interpreter
39 class VirtualMachine super NaiveInterpreter
41 # Perfect hashing and perfect numbering
42 var ph
: Perfecthashing = new Perfecthashing
44 # Handles memory allocated in C
45 var memory_manager
: MemoryManager = new MemoryManager
47 # The unique instance of the `MInit` value
48 var initialization_value
: Instance is noinit
52 var init_type
= new MInitType(mainmodule
.model
)
53 initialization_value
= new MutableInstance(init_type
)
57 # Subtyping test for the virtual machine
58 redef fun is_subtype
(sub
, sup
: MType): Bool
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
65 else if sup
isa MNullType then
66 sup_accept_null
= true
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
75 else if sub
isa MNullType then
76 return sup_accept_null
78 # Now the case of direct null and nullable is over
80 # An unfixed formal type can only accept itself
81 if sup
isa MParameterType or sup
isa MVirtualType then
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
91 else if sub
isa MNullType then
92 return sup_accept_null
96 assert sub
isa MClassType
98 # `sup` accepts only null
99 if sup
isa MNullType then return false
101 assert sup
isa MClassType
103 # Create the sup vtable if not create
104 if not sup
.mclass
.loaded
then create_class
(sup
.mclass
)
106 # Sub can be discovered inside a Generic type during the subtyping test
107 if not sub
.mclass
.loaded
then create_class
(sub
.mclass
)
109 if sup
isa MGenericType then
110 var sub2
= sub
.supertype_to
(mainmodule
, anchor
, sup
.mclass
)
111 assert sub2
.mclass
== sup
.mclass
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
)
118 if res
== false then return false
123 var super_id
= sup
.mclass
.vtable
.id
124 var mask
= sub
.mclass
.vtable
.mask
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
)
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
135 // Follow the pointer to somewhere in the vtable
136 long unsigned int *offset = (long unsigned int*)(((long int *)vtable)[-hv]);
138 // If the pointed value is corresponding to the identifier, the test is true, otherwise false
139 return *offset == id;
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];
147 return id == tableid;
150 # Redef init_instance to simulate the loading of a class
151 redef fun init_instance
(recv
: Instance)
153 if not recv
.mtype
.as(MClassType).mclass
.loaded
then create_class
(recv
.mtype
.as(MClassType).mclass
)
155 recv
.vtable
= recv
.mtype
.as(MClassType).mclass
.vtable
157 assert recv
isa MutableInstance
159 recv
.internal_attributes
= init_internal_attributes
(initialization_value
, recv
.mtype
.as(MClassType).mclass
.all_mattributes
(mainmodule
, none_visibility
).length
)
163 # Associate a `PrimitiveInstance` to its `VTable`
164 redef fun init_instance_primitive
(recv
: Instance)
166 if not recv
.mtype
.as(MClassType).mclass
.loaded
then create_class
(recv
.mtype
.as(MClassType).mclass
)
168 recv
.vtable
= recv
.mtype
.as(MClassType).mclass
.vtable
171 # Create a virtual table for this `MClass` if not already done
172 redef fun get_primitive_class
(name
: String): MClass
176 if not mclass
.loaded
then create_class
(mclass
)
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].[] `{
186 Instance* attributes = malloc(sizeof(Instance) * size);
189 for(i=0; i<size; i++)
190 attributes[i] = init_instance;
192 Instance_incr_ref(init_instance);
196 # Creates the runtime structures for this class
197 fun create_class
(mclass
: MClass) do mclass
.make_vt
(self)
199 # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
200 redef fun send
(mproperty
: MMethod, args
: Array[Instance]): nullable Instance
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
207 var propdef
= method_dispatch
(mproperty
, recv
.vtable
.as(not null), recv
)
209 return self.call
(propdef
, args
)
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
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
)
219 return method_dispatch_ph
(vtable
.internal_vtable
, vtable
.mask
,
220 mproperty
.intro_mclassdef
.mclass
.vtable
.id
, mproperty
.offset
)
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
228 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
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);
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];
248 # Return the value of the attribute `mproperty` for the object `recv`
249 redef fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
251 assert recv
isa MutableInstance
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
)
259 # Otherwise, read the attribute value with perfect hashing
260 var id
= mproperty
.intro_mclassdef
.mclass
.vtable
.id
262 i
= read_attribute_ph
(recv
.internal_attributes
, recv
.vtable
.internal_vtable
,
263 recv
.vtable
.mask
, id
, mproperty
.offset
)
266 # If we get a `MInit` value, throw an error
267 if i
== initialization_value
then
268 fatal
("Uninitialized attribute {mproperty.name}")
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
284 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
286 // pointer+1 is the position where the delta of the class is
287 int absolute_offset = *(pointer + 1);
289 Instance res = ((Instance *)instance)[absolute_offset + offset];
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];
306 # Replace in `recv` the value of the attribute `mproperty` by `value`
307 redef fun write_attribute
(mproperty
: MAttribute, recv
: Instance, value
: Instance)
309 assert recv
isa MutableInstance
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
)
316 # Otherwise, use perfect hashing to replace the old value
317 var id
= mproperty
.intro_mclassdef
.mclass
.vtable
.id
319 write_attribute_ph
(recv
.internal_attributes
, recv
.vtable
.internal_vtable
,
320 recv
.vtable
.mask
, id
, mproperty
.offset
, value
)
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
334 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
336 // pointer+1 is the position where the delta of the class is
337 int absolute_offset = *(pointer + 1);
339 ((Instance *)instance)[absolute_offset + offset] = value;
340 Instance_incr_ref(value);
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);
353 # Is the attribute `mproperty` initialized in the instance `recv`?
354 redef fun isset_attribute
(mproperty
: MAttribute, recv
: Instance): Bool
356 assert recv
isa MutableInstance
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
362 var i
= read_attribute_ph
(recv
.internal_attributes
, recv
.vtable
.internal_vtable
,
363 recv
.vtable
.mask
, id
, mproperty
.offset
)
365 return i
!= initialization_value
370 # A reference to the virtual table of this class
371 var vtable
: nullable VTable
373 # True when the class is effectively loaded by the vm, false otherwise
374 var loaded
: Bool = false
376 # Color for Cohen subtyping test : the absolute position of the id
377 # of this class in virtual tables
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]
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]
388 # Allocates a VTable for this class and gives it an id
389 private fun make_vt
(v
: VirtualMachine)
391 if loaded
then return
393 # `superclasses` contains the order of superclasses for virtual tables
394 var superclasses
= superclasses_ordering
(v
)
395 superclasses
.remove
(self)
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]
402 # Absolute offset of attribute from the beginning of the attributes table
403 var offset_attributes
= 0
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
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
)
416 # Get the number of introduced methods and attributes for this class
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
425 ids
.push
(parent
.vtable
.id
)
426 nb_methods
.push
(methods
)
427 nb_attributes
.push
(attributes
)
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)
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
438 parent
.update_positions
(pos_attr
, pos_meth
, self)
440 offset_attributes
+= attributes
441 offset_methods
+= methods
442 offset_methods
+= 2 # Because each block starts with an id and the delta
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
)
449 # Set the absolute position of the identifier of this class in the virtual table
450 color
= offset_methods
- 2
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
)
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)
469 var idc
= new Array[Int]
471 vtable
.mask
= v
.ph
.pnand
(ids
, 1, idc
) - 1
473 vtable
.classname
= name
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
)
480 var nb_methods_total
= new Array[Int]
481 var nb_attributes_total
= new Array[Int]
484 var nb_introduced_attributes
= 0
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
492 p
.offset
= relative_offset_meth
493 p
.absolute_offset
= offset_methods
+ relative_offset_meth
494 relative_offset_meth
+= 1
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
504 nb_methods_total
.add_all
(nb_methods
)
505 nb_methods_total
.push
(self_methods
)
507 nb_attributes_total
.add_all
(nb_attributes
)
508 nb_attributes_total
.push
(nb_introduced_attributes
)
510 # Save the offsets of self class
511 update_positions
(offset_attributes
, offset_methods
, self)
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
)
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)
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
)
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
)
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]
542 var deltas
= new Array[Int]
545 for nb
in nb_attributes
do
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]
557 var superclasses
= new Array[MClass]
558 superclasses
.add_all
(ancestors
)
560 var res
= new Array[MClass]
561 if superclasses
.length
> 1 then
563 var ordering
= self.dfs
(v
, res
)
567 # There is no super-class, self is Object
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]
577 # Add this class at the beginning
580 var direct_parents
= self.in_hierarchy
(v
.mainmodule
).direct_greaters
.to_a
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
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
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
)
603 # Then we recurse on other classes
604 for cl
in direct_parents
do
606 res
= new Array[MClass]
610 if not prefix_res
.has
(cl_res
) then prefix_res
.push
(cl_res
)
619 if direct_parents
.length
> 0 then
620 res
= direct_parents
.first
.dfs
(v
, res
)
624 if not res
.has
(self) then res
.push
(self)
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)
634 positions_attributes
[cl
] = attributes_offsets
635 positions_methods
[cl
] = methods_offset
639 redef class MAttribute
640 # Relative offset of this attribute in the runtime instance
641 # (beginning of the block of its introducing class)
644 # Absolute offset of this attribute in the runtime instance (beginning of the attribute table)
645 var absolute_offset
: Int
649 # Relative offset of this method in the virtual table (from the beginning of the block)
652 # Absolute offset of this method in the virtual table (from the beginning of the vtable)
653 var absolute_offset
: Int
656 # Redef MutableInstance to improve implementation of attributes in objects
657 redef class MutableInstance
659 # C-array to store pointers to attributes of this Object
660 var internal_attributes
: Pointer
663 # Redef to associate an `Instance` to its `VTable`
666 # Associate a runtime instance to its virtual table which contains methods, types etc.
667 var vtable
: nullable VTable
670 # Is the type of the initial value inside attributes
674 redef var model
: Model
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
682 redef fun collect_mclassdefs
(mmodule
) do return new HashSet[MClassDef]
684 redef fun collect_mclasses
(mmodule
) do return new HashSet[MClass]
686 redef fun collect_mtypes
(mmodule
) do return new HashSet[MClassType]
689 # A VTable contains the virtual method table for the dispatch
690 # and informations to perform subtyping tests
692 # The mask to perform perfect hashing
693 var mask
: Int is noinit
695 # Unique identifier given by perfect hashing
696 var id
: Int is noinit
698 # Pointer to the c-allocated area, represents the virtual table
699 var internal_vtable
: Pointer is noinit
701 # The short classname of this class
702 var classname
: String is noinit
705 # Handle memory, used for allocate virtual table and associated structures
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
711 # Allocate in C current virtual table
712 var res
= intern_init_vtable
(ids
, nb_methods
, nb_attributes
, mask
)
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].[] `{
721 // Allocate and fill current virtual table
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)
730 total_size += Array_of_Int__index(nb_methods, i);
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);
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;
744 // Set the virtual table to its position 0
745 // ie: after the hashtable
746 vtable = vtable + mask + 1;
748 int current_size = 1;
749 for(i = 0; i < nb_classes; i++) {
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)
755 int hv = mask & Array_of_Int__index(ids, i);
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]);
762 current_size += Array_of_Int__index(nb_methods, i);
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].[] `{
776 // Get the area to fill with methods by a sequence of perfect hashing
778 long unsigned int *pointer = (long unsigned int*)(((long unsigned int *)vtable)[-hv]);
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);
785 for(i=0; i<length; i++)
787 MMethodDef method = Array_of_MMethodDef__index(methods, i);
788 area[i] = (long unsigned int)method;
789 MMethodDef_incr_ref(method);