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 intrude 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 init_naive_interpreter
(interpreter
, 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 and garbage collection
45 var memory_manager
: MemoryManager = new MemoryManager
47 # The unique instance of the `MInit` value
48 var initialization_value
: Instance
50 init(modelbuilder
: ModelBuilder, mainmodule
: MModule, arguments
: Array[String])
53 var init_type
= new MInitType(mainmodule
.model
)
54 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 anchor
== null then anchor
= sub
110 if sup
isa MGenericType then
111 var sub2
= sub
.supertype_to
(mainmodule
, anchor
, sup
.mclass
)
112 assert sub2
.mclass
== sup
.mclass
114 for i
in [0..sup
.mclass
.arity
[ do
115 var sub_arg
= sub2
.arguments
[i
]
116 var sup_arg
= sup
.arguments
[i
]
117 var res
= is_subtype
(sub_arg
, sup_arg
)
119 if res
== false then return false
124 var super_id
= sup
.mclass
.vtable
.id
125 var mask
= sub
.mclass
.vtable
.mask
127 return inter_is_subtype
(super_id
, mask
, sub
.mclass
.vtable
.internal_vtable
)
130 # Subtyping test with perfect hashing
131 private fun inter_is_subtype
(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 # Redef init_instance to simulate the loading of a class
143 redef fun init_instance
(recv
: Instance)
145 if not recv
.mtype
.as(MClassType).mclass
.loaded
then create_class
(recv
.mtype
.as(MClassType).mclass
)
147 recv
.vtable
= recv
.mtype
.as(MClassType).mclass
.vtable
149 assert(recv
isa MutableInstance)
151 recv
.internal_attributes
= init_internal_attributes
(initialization_value
, recv
.mtype
.as(MClassType).mclass
.all_mattributes
(mainmodule
, none_visibility
).length
)
155 # Initialize the internal representation of an object (its attribute values)
156 # `init_instance` is the initial value of attributes
157 private fun init_internal_attributes
(init_instance
: Instance, size
: Int): Pointer
158 import Array[Instance].length
, Array[Instance].[] `{
160 Instance* attributes = malloc(sizeof(Instance) * size);
163 for(i=0; i<size; i++)
164 attributes[i] = init_instance;
166 Instance_incr_ref(init_instance);
170 # Creates the runtime structures for this class
171 fun create_class
(mclass
: MClass) do mclass
.make_vt
(self)
173 # Return the value of the attribute `mproperty` for the object `recv`
174 redef fun read_attribute
(mproperty
: MAttribute, recv
: Instance): Instance
176 assert recv
isa MutableInstance
178 # Read the attribute value with perfect hashing
179 var id
= mproperty
.intro_mclassdef
.mclass
.vtable
.id
181 var i
= read_attribute_ph
(recv
.internal_attributes
, recv
.vtable
.internal_vtable
,
182 recv
.vtable
.mask
, id
, mproperty
.offset
)
184 # If we get a `MInit` value, throw an error
185 if i
== initialization_value
then
186 fatal
("Uninitialized attribute {mproperty.name}")
193 # Return the attribute value in `instance` with a sequence of perfect_hashing
194 # `instance` is the attributes array of the receiver
195 # `vtable` is the pointer to the virtual table of the class (of the receiver)
196 # `mask` is the perfect hashing mask of the class
197 # `id` is the identifier of the class
198 # `offset` is the relative offset of this attribute
199 private fun read_attribute_ph
(instance
: Pointer, vtable
: Pointer, mask
: Int, id
: Int, offset
: Int): Instance `{
200 // Perfect hashing position
202 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
204 // pointer+1 is the position where the delta of the class is
205 int absolute_offset = *(pointer + 1);
207 Instance res = ((Instance *)instance)[absolute_offset + offset];
212 # Replace in `recv` the value of the attribute `mproperty` by `value`
213 redef fun write_attribute
(mproperty
: MAttribute, recv
: Instance, value
: Instance)
215 assert recv
isa MutableInstance
217 var id
= mproperty
.intro_mclassdef
.mclass
.vtable
.id
219 # Replace the old value of mproperty in recv
220 write_attribute_ph
(recv
.internal_attributes
, recv
.vtable
.internal_vtable
,
221 recv
.vtable
.mask
, id
, mproperty
.offset
, value
)
224 # Replace the value of an attribute in an instance
225 # `instance` is the attributes array of the receiver
226 # `vtable` is the pointer to the virtual table of the class (of the receiver)
227 # `mask` is the perfect hashing mask of the class
228 # `id` is the identifier of the class
229 # `offset` is the relative offset of this attribute
230 # `value` is the new value for this attribute
231 private fun write_attribute_ph
(instance
: Pointer, vtable
: Pointer, mask
: Int, id
: Int, offset
: Int, value
: Instance) `{
232 // Perfect hashing position
234 long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
236 // pointer+1 is the position where the delta of the class is
237 int absolute_offset = *(pointer + 1);
239 ((Instance *)instance)[absolute_offset + offset] = value;
240 Instance_incr_ref(value);
245 # A reference to the virtual table of this class
246 var vtable
: nullable VTable
248 # True when the class is effectively loaded by the vm, false otherwise
249 var loaded
: Bool = false
251 # For each loaded subclass, keep the position of the group of attributes
252 # introduced by self class in the object
253 var positions_attributes
: HashMap[MClass, Int] = new HashMap[MClass, Int]
255 # For each loaded subclass, keep the position of the group of methods
256 # introduced by self class in the vtable
257 var positions_methods
: HashMap[MClass, Int] = new HashMap[MClass, Int]
259 # Allocates a VTable for this class and gives it an id
260 private fun make_vt
(v
: VirtualMachine)
262 if loaded
then return
264 # `superclasses` contains the order of superclasses for virtual tables
265 var superclasses
= superclasses_ordering
(v
)
266 superclasses
.remove
(self)
268 # Make_vt for super-classes
269 var ids
= new Array[Int]
270 var nb_methods
= new Array[Int]
271 var nb_attributes
= new Array[Int]
273 # Absolute offset of the beginning of the attributes table
274 var offset_attributes
= 0
275 # Absolute offset of the beginning of the methods table
276 var offset_methods
= 0
278 for parent
in superclasses
do
279 if not parent
.loaded
then parent
.make_vt
(v
)
281 # Get the number of introduced methods and attributes for this class
285 for p
in parent
.intro_mproperties
(none_visibility
) do
286 if p
isa MMethod then methods
+= 1
287 if p
isa MAttribute then
292 ids
.push
(parent
.vtable
.id
)
293 nb_methods
.push
(methods
)
294 nb_attributes
.push
(attributes
)
296 # Update `positions_attributes` and `positions_methods` in `parent`
297 update_positions
(offset_attributes
, offset_methods
, parent
)
299 offset_attributes
+= attributes
300 offset_methods
+= methods
303 # When all super-classes have their identifiers and vtables, allocate current one
304 allocate_vtable
(v
, ids
, nb_methods
, nb_attributes
, offset_attributes
, offset_methods
)
306 # The virtual table now needs to be filled with pointer to methods
309 # Allocate a single vtable
310 # `ids : Array of superclasses identifiers
311 # `nb_methods : Array which contain the number of introduced methods for each class in ids
312 # `nb_attributes : Array which contain the number of introduced attributes for each class in ids
313 # `offset_attributes : Offset from the beginning of the table of the group of attributes
314 # `offset_methods : Offset from the beginning of the table of the group of methods
315 private fun allocate_vtable
(v
: VirtualMachine, ids
: Array[Int], nb_methods
: Array[Int], nb_attributes
: Array[Int],
316 offset_attributes
: Int, offset_methods
: Int)
319 var idc
= new Array[Int]
321 vtable
.mask
= v
.ph
.pnand
(ids
, 1, idc
) - 1
323 vtable
.classname
= name
325 # Add current id to Array of super-ids
326 var ids_total
= new Array[Int]
327 ids_total
.add_all
(ids
)
328 ids_total
.push
(vtable
.id
)
330 var nb_methods_total
= new Array[Int]
331 var nb_attributes_total
= new Array[Int]
334 var nb_introduced_attributes
= 0
336 # For self attributes, fixing offsets
337 var relative_offset
= 0
338 for p
in intro_mproperties
(none_visibility
) do
339 if p
isa MMethod then self_methods
+= 1
340 if p
isa MAttribute then
341 nb_introduced_attributes
+= 1
342 p
.offset
= relative_offset
347 nb_methods_total
.add_all
(nb_methods
)
348 nb_methods_total
.push
(self_methods
)
350 nb_attributes_total
.add_all
(nb_attributes
)
351 nb_attributes_total
.push
(nb_introduced_attributes
)
353 # Save the offsets of self class
354 offset_attributes
+= nb_introduced_attributes
355 offset_methods
+= self_methods
356 update_positions
(offset_attributes
, offset_methods
, self)
358 # Since we have the number of attributes for each class, calculate the delta
359 var d
= calculate_delta
(nb_attributes_total
)
360 vtable
.internal_vtable
= v
.memory_manager
.init_vtable
(ids_total
, nb_methods_total
, d
, vtable
.mask
)
363 # Computes delta for each class
364 # A delta represents the offset for this group of attributes in the object
365 # `nb_attributes` : number of attributes for each class (classes are linearized from Object to current)
366 # return deltas for each class
367 private fun calculate_delta
(nb_attributes
: Array[Int]): Array[Int]
369 var deltas
= new Array[Int]
372 for nb
in nb_attributes
do
380 # Order superclasses of self
381 # Return the order of superclasses in runtime structures of this class
382 private fun superclasses_ordering
(v
: VirtualMachine): Array[MClass]
384 var superclasses
= new Array[MClass]
385 superclasses
.add_all
(ancestors
)
387 var res
= new Array[MClass]
388 if superclasses
.length
> 1 then
390 var ordering
= self.dfs
(v
, res
)
394 # There is no super-class, self is Object
399 # A kind of Depth-First-Search for superclasses ordering
400 # `v` : the current executed instance of VirtualMachine
401 # `res` : Result Array, ie current superclasses ordering
402 private fun dfs
(v
: VirtualMachine, res
: Array[MClass]): Array[MClass]
404 # Add this class at the beginning
407 var direct_parents
= self.in_hierarchy
(v
.mainmodule
).direct_greaters
.to_a
409 if direct_parents
.length
> 1 then
410 # Prefix represents the class which has the most properties
411 # we try to choose it in first to reduce the number of potential recompilations
414 for cl
in direct_parents
do
415 # If we never have visited this class
416 if not res
.has
(cl
) then
417 var properties_length
= cl
.all_mproperties
(v
.mainmodule
, none_visibility
).length
418 if properties_length
> max
then
419 max
= properties_length
425 if prefix
!= null then
426 # Add the prefix class ordering at the beginning of our sequence
427 var prefix_res
= new Array[MClass]
428 prefix_res
= prefix
.dfs
(v
, prefix_res
)
430 # Then we recurse on other classes
431 for cl
in direct_parents
do
433 res
= new Array[MClass]
437 if not prefix_res
.has
(cl_res
) then prefix_res
.push
(cl_res
)
446 if direct_parents
.length
> 0 then
447 res
= direct_parents
.first
.dfs
(v
, res
)
451 if not res
.has
(self) then res
.push
(self)
456 # Update positions of self class in `parent`
457 # `attributes_offset`: absolute offset of introduced attributes
458 # `methods_offset`: absolute offset of introduced methods
459 private fun update_positions
(attributes_offsets
: Int, methods_offset
:Int, parent
: MClass)
461 parent
.positions_attributes
[self] = attributes_offsets
462 parent
.positions_methods
[self] = methods_offset
466 redef class MAttribute
467 # Represents the relative offset of this attribute in the runtime instance
471 # Redef MutableInstance to improve implementation of attributes in objects
472 redef class MutableInstance
474 # C-array to store pointers to attributes of this Object
475 var internal_attributes
: Pointer
478 # Is the type of the initial value inside attributes
482 redef var model
: Model
483 protected init(model
: Model)
488 redef fun to_s
do return "InitType"
489 redef fun as_nullable
do return self
490 redef fun need_anchor
do return false
491 redef fun resolve_for
(mtype
, anchor
, mmodule
, cleanup_virtual
) do return self
492 redef fun can_resolve_for
(mtype
, anchor
, mmodule
) do return true
494 redef fun collect_mclassdefs
(mmodule
) do return new HashSet[MClassDef]
496 redef fun collect_mclasses
(mmodule
) do return new HashSet[MClass]
498 redef fun collect_mtypes
(mmodule
) do return new HashSet[MClassType]
501 # A VTable contains the virtual method table for the dispatch
502 # and informations to perform subtyping tests
504 # The mask to perform perfect hashing
505 var mask
: Int is noinit
507 # Unique identifier given by perfect hashing
508 var id
: Int is noinit
510 # Pointer to the c-allocated area, represents the virtual table
511 var internal_vtable
: Pointer is noinit
513 # The short classname of this class
514 var classname
: String is noinit
518 var vtable
: nullable VTable
521 # Handle memory, used for allocate virtual table and associated structures
524 # Allocate and fill a virtual table
525 fun init_vtable
(ids
: Array[Int], nb_methods
: Array[Int], nb_attributes
: Array[Int], mask
: Int): Pointer
527 # Allocate in C current virtual table
528 var res
= intern_init_vtable
(ids
, nb_methods
, nb_attributes
, mask
)
533 # Construct virtual tables with a bi-dimensional layout
534 private fun intern_init_vtable
(ids
: Array[Int], nb_methods
: Array[Int], deltas
: Array[Int], mask
: Int): Pointer
535 import Array[Int].length
, Array[Int].[] `{
537 // Allocate and fill current virtual table
539 int total_size = 0; // total size of this virtual table
540 int nb_classes = Array_of_Int_length(nb_methods);
541 for(i = 0; i<nb_classes; i++) {
542 /* - One for each method of this class
543 * - One for the delta (offset of this group of attributes in objects)
546 total_size += Array_of_Int__index(nb_methods, i);
550 // Add the size of the perfect hashtable (mask +1)
551 // Add one because we start to fill the vtable at position 1 (0 is the init position)
552 total_size += mask+2;
553 long unsigned int* vtable = malloc(sizeof(long unsigned int)*total_size);
555 // Initialisation to the first position of the virtual table (ie : Object)
556 long unsigned int *init = vtable + mask + 2;
557 for(i=0; i<total_size; i++)
558 vtable[i] = (long unsigned int)init;
560 // Set the virtual table to its position 0
561 // ie: after the hashtable
562 vtable = vtable + mask + 1;
564 int current_size = 1;
565 for(i = 0; i < nb_classes; i++) {
567 vtable[hv] contains a pointer to the group of introduced methods
568 For each superclasse we have in virtual table :
569 (id | delta | introduced methods)
571 int hv = mask & Array_of_Int__index(ids, i);
573 vtable[current_size] = Array_of_Int__index(ids, i);
574 vtable[current_size + 1] = Array_of_Int__index(deltas, i);
575 vtable[-hv] = (long unsigned int)&(vtable[current_size]);
578 current_size += Array_of_Int__index(nb_methods, i);