nitvm: Subtyping test is now implemented with direct access
[nit.git] / src / vm.nit
index 65e03a1..b9a37c4 100644 (file)
@@ -45,9 +45,9 @@ class VirtualMachine super NaiveInterpreter
        var memory_manager: MemoryManager = new MemoryManager
 
        # The unique instance of the `MInit` value
-       var initialization_value: Instance
+       var initialization_value: Instance is noinit
 
-       init(modelbuilder: ModelBuilder, mainmodule: MModule, arguments: Array[String])
+       init
        do
                var init_type = new MInitType(mainmodule.model)
                initialization_value = new MutableInstance(init_type)
@@ -123,11 +123,12 @@ class VirtualMachine super NaiveInterpreter
                var super_id = sup.mclass.vtable.id
                var mask = sub.mclass.vtable.mask
 
-               return inter_is_subtype(super_id, mask, sub.mclass.vtable.internal_vtable)
+               # For now, we always use perfect hashing for subtyping test
+               return inter_is_subtype_ph(super_id, mask, sub.mclass.vtable.internal_vtable)
        end
 
        # Subtyping test with perfect hashing
-       private fun inter_is_subtype(id: Int, mask:Int, vtable: Pointer): Bool `{
+       private fun inter_is_subtype_ph(id: Int, mask:Int, vtable: Pointer): Bool `{
                // hv is the position in hashtable
                int hv = id & mask;
 
@@ -138,6 +139,14 @@ class VirtualMachine super NaiveInterpreter
                return *offset == id;
        `}
 
+       # Subtyping test with Cohen test (direct access)
+       private fun inter_is_subtype_sst(id: Int, position: Int, vtable: Pointer): Bool `{
+               // Direct access to the position given in parameter
+               int tableid = (long unsigned int)((long int *)vtable)[position];
+
+               return id == tableid;
+       `}
+
        # Redef init_instance to simulate the loading of a class
        redef fun init_instance(recv: Instance)
        do
@@ -195,17 +204,21 @@ class VirtualMachine super NaiveInterpreter
                var ret = send_commons(mproperty, args, mtype)
                if ret != null then return ret
 
-               var propdef = method_dispatch(mproperty, recv.vtable.as(not null))
+               var propdef = method_dispatch(mproperty, recv.vtable.as(not null), recv)
 
                return self.call(propdef, args)
        end
 
        # Method dispatch, for a given global method `mproperty`
        # returns the most specific local method in the class corresponding to `vtable`
-       private fun method_dispatch(mproperty: MMethod, vtable: VTable): MMethodDef
+       private fun method_dispatch(mproperty: MMethod, vtable: VTable, recv: Instance): MMethodDef
        do
-               return method_dispatch_ph(vtable.internal_vtable, vtable.mask,
+               if mproperty.intro_mclassdef.mclass.positions_methods[recv.mtype.as(MClassType).mclass] != -1 then
+                       return method_dispatch_sst(vtable.internal_vtable, mproperty.absolute_offset)
+               else
+                       return method_dispatch_ph(vtable.internal_vtable, vtable.mask,
                                mproperty.intro_mclassdef.mclass.vtable.id, mproperty.offset)
+               end
        end
 
        # Execute a method dispatch with perfect hashing
@@ -221,6 +234,17 @@ class VirtualMachine super NaiveInterpreter
                return propdef;
        `}
 
+       # Execute a method dispatch with direct access and return the appropriate `MMethodDef`
+       # `vtable` : Pointer to the internal pointer of the class
+       # `absolute_offset` : Absolute offset from the beginning of the virtual table
+       private fun method_dispatch_sst(vtable: Pointer, absolute_offset: Int): MMethodDef `{
+               // pointer+2 is the position where methods are
+               // Add the offset of property and get the method implementation
+               MMethodDef propdef = (MMethodDef)((long int *)vtable)[absolute_offset];
+
+               return propdef;
+       `}
+
        # Return the value of the attribute `mproperty` for the object `recv`
        redef fun read_attribute(mproperty: MAttribute, recv: Instance): Instance
        do
@@ -249,11 +273,11 @@ class VirtualMachine super NaiveInterpreter
        end
 
        # Return the attribute value in `instance` with a sequence of perfect_hashing
-       #     `instance` is the attributes array of the receiver
-       #     `vtable` is the pointer to the virtual table of the class (of the receiver)
-       #     `mask` is the perfect hashing mask of the class
-       #     `id` is the identifier of the class
-       #     `offset` is the relative offset of this attribute
+       # * `instance` is the attributes array of the receiver
+       # * `vtable` is the pointer to the virtual table of the class (of the receiver)
+       # * `mask` is the perfect hashing mask of the class
+       # * `id` is the identifier of the class
+       # * `offset` is the relative offset of this attribute
        private fun read_attribute_ph(instance: Pointer, vtable: Pointer, mask: Int, id: Int, offset: Int): Instance `{
                // Perfect hashing position
                int hv = mask & id;
@@ -268,8 +292,8 @@ class VirtualMachine super NaiveInterpreter
        `}
 
        # Return the attribute value in `instance` with a direct access (SST)
-       #     `instance` is the attributes array of the receiver
-       #     `offset` is the absolute offset of this attribute
+       # * `instance` is the attributes array of the receiver
+       # * `offset` is the absolute offset of this attribute
        private fun read_attribute_sst(instance: Pointer, offset: Int): Instance `{
                /* We can make a direct access to the attribute value
                   because this attribute is always at the same position
@@ -298,12 +322,12 @@ class VirtualMachine super NaiveInterpreter
        end
 
        # Replace the value of an attribute in an instance
-       #     `instance` is the attributes array of the receiver
-       #     `vtable` is the pointer to the virtual table of the class (of the receiver)
-       #     `mask` is the perfect hashing mask of the class
-       #     `id` is the identifier of the class
-       #     `offset` is the relative offset of this attribute
-       #     `value` is the new value for this attribute
+       # * `instance` is the attributes array of the receiver
+       # * `vtable` is the pointer to the virtual table of the class (of the receiver)
+       # * `mask` is the perfect hashing mask of the class
+       # * `id` is the identifier of the class
+       # * `offset` is the relative offset of this attribute
+       # * `value` is the new value for this attribute
        private fun write_attribute_ph(instance: Pointer, vtable: Pointer, mask: Int, id: Int, offset: Int, value: Instance) `{
                // Perfect hashing position
                int hv = mask & id;
@@ -317,9 +341,9 @@ class VirtualMachine super NaiveInterpreter
        `}
 
        # Replace the value of an attribute in an instance with direct access
-       #     `instance` is the attributes array of the receiver
-       #     `offset` is the absolute offset of this attribute
-       #     `value` is the new value for this attribute
+       # * `instance` is the attributes array of the receiver
+       # * `offset` is the absolute offset of this attribute
+       # * `value` is the new value for this attribute
        private fun write_attribute_sst(instance: Pointer, offset: Int, value: Instance) `{
                // Direct access to the position with the absolute offset
                ((Instance *)instance)[offset] = value;
@@ -349,6 +373,10 @@ redef class MClass
        # True when the class is effectively loaded by the vm, false otherwise
        var loaded: Bool = false
 
+       # Color for Cohen subtyping test : the absolute position of the id
+       # of this class in virtual tables
+       var color: Int
+
        # For each loaded subclass, keep the position of the group of attributes
        # introduced by self class in the object
        var positions_attributes: HashMap[MClass, Int] = new HashMap[MClass, Int]
@@ -373,8 +401,11 @@ redef class MClass
 
                # Absolute offset of attribute from the beginning of the attributes table
                var offset_attributes = 0
-               # Absolute offset of method from the beginning of the methods table
-               var offset_methods = 0
+
+               # Absolute offset of method from the beginning of the methods table,
+               # is initialize to 3 because the first position is empty in the virtual table
+               # and the second and third are respectively class id and delta
+               var offset_methods = 3
 
                # The previous element in `superclasses`
                var previous_parent: nullable MClass = null
@@ -408,12 +439,16 @@ redef class MClass
 
                        offset_attributes += attributes
                        offset_methods += methods
+                       offset_methods += 2 # Because each block starts with an id and the delta
                end
 
                # When all super-classes have their identifiers and vtables, allocate current one
                allocate_vtable(v, ids, nb_methods, nb_attributes, offset_attributes, offset_methods)
                loaded = true
 
+               # Set the absolute position of the identifier of this class in the virtual table
+               color = offset_methods - 2
+
                # The virtual table now needs to be filled with pointer to methods
                superclasses.add(self)
                for cl in superclasses do
@@ -422,11 +457,11 @@ redef class MClass
        end
 
        # Allocate a single vtable
-       #     `ids : Array of superclasses identifiers
-       #     `nb_methods : Array which contain the number of introduced methods for each class in ids
-       #     `nb_attributes : Array which contain the number of introduced attributes for each class in ids
-       #     `offset_attributes : Offset from the beginning of the table of the group of attributes
-       #     `offset_methods : Offset from the beginning of the table of the group of methods
+       # * `ids : Array of superclasses identifiers
+       # * `nb_methods : Array which contain the number of introduced methods for each class in ids
+       # * `nb_attributes : Array which contain the number of introduced attributes for each class in ids
+       # * `offset_attributes : Offset from the beginning of the table of the group of attributes
+       # * `offset_methods : Offset from the beginning of the table of the group of methods
        private fun allocate_vtable(v: VirtualMachine, ids: Array[Int], nb_methods: Array[Int], nb_attributes: Array[Int],
                        offset_attributes: Int, offset_methods: Int)
        do
@@ -481,8 +516,8 @@ redef class MClass
        end
 
        # Fill the vtable with methods of `self` class
-       #     `v` : Current instance of the VirtualMachine
-       #     `table` : the table of self class, will be filled with its methods
+       # * `v` : Current instance of the VirtualMachine
+       # * `table` : the table of self class, will be filled with its methods
        private fun fill_vtable(v:VirtualMachine, table: VTable, cl: MClass)
        do
                var methods = new Array[MMethodDef]
@@ -500,8 +535,8 @@ redef class MClass
 
        # Computes delta for each class
        # A delta represents the offset for this group of attributes in the object
-       #     `nb_attributes` : number of attributes for each class (classes are linearized from Object to current)
-       #     return deltas for each class
+       # *`nb_attributes` : number of attributes for each class (classes are linearized from Object to current)
+       # * return deltas for each class
        private fun calculate_delta(nb_attributes: Array[Int]): Array[Int]
        do
                var deltas = new Array[Int]
@@ -535,8 +570,8 @@ redef class MClass
        end
 
        # A kind of Depth-First-Search for superclasses ordering
-       #     `v` : the current executed instance of VirtualMachine
-       #     `res` : Result Array, ie current superclasses ordering
+       # *`v` : the current executed instance of VirtualMachine
+       # * `res` : Result Array, ie current superclasses ordering
        private fun dfs(v: VirtualMachine, res: Array[MClass]): Array[MClass]
        do
                # Add this class at the beginning
@@ -592,8 +627,8 @@ redef class MClass
        end
 
        # Update positions of the class `cl`
-       #     `attributes_offset`: absolute offset of introduced attributes
-       #     `methods_offset`: absolute offset of introduced methods
+       # * `attributes_offset`: absolute offset of introduced attributes
+       # * `methods_offset`: absolute offset of introduced methods
        private fun update_positions(attributes_offsets: Int, methods_offset:Int, cl: MClass)
        do
                positions_attributes[cl] = attributes_offsets
@@ -627,6 +662,8 @@ end
 
 # Redef to associate an `Instance` to its `VTable`
 redef class Instance
+
+       # Associate a runtime instance to its virtual table which contains methods, types etc.
        var vtable: nullable VTable
 end
 
@@ -635,10 +672,6 @@ class MInitType
        super MType
 
        redef var model: Model
-       protected init(model: Model)
-       do
-               self.model = model
-       end
 
        redef fun to_s do return "InitType"
        redef fun as_nullable do return self
@@ -733,10 +766,10 @@ class MemoryManager
        `}
 
        # Put implementation of methods of a class in `vtable`
-       # `vtable` : Pointer to the C-virtual table
-       # `mask` : perfect-hashing mask of the class corresponding to the vtable
-       # `id` : id of the target class
-       # `methods` : array of MMethodDef of the target class
+       # * `vtable` : Pointer to the C-virtual table
+       # * `mask` : perfect-hashing mask of the class corresponding to the vtable
+       # * `id` : id of the target class
+       # * `methods` : array of MMethodDef of the target class
        fun put_methods(vtable: Pointer, mask: Int, id: Int, methods: Array[MMethodDef])
                import Array[MMethodDef].length, Array[MMethodDef].[] `{