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