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