all: fix broken markdown comments with missing or unwanted code blocks
[nit.git] / src / vm.nit
index a135d0f..75868c6 100644 (file)
@@ -17,7 +17,7 @@
 # Implementation of the Nit virtual machine
 module vm
 
-intrude import naive_interpreter
+import interpreter::naive_interpreter
 import model_utils
 import perfect_hashing
 
@@ -28,7 +28,7 @@ redef class ModelBuilder
                self.toolcontext.info("*** NITVM STARTING ***", 1)
 
                var interpreter = new VirtualMachine(self, mainmodule, arguments)
-               init_naive_interpreter(interpreter, mainmodule)
+               interpreter.start(mainmodule)
 
                var time1 = get_time
                self.toolcontext.info("*** NITVM STOPPING : {time1-time0} ***", 2)
@@ -44,6 +44,16 @@ class VirtualMachine super NaiveInterpreter
        # Handles memory and garbage collection
        var memory_manager: MemoryManager = new MemoryManager
 
+       # The unique instance of the `MInit` value
+       var initialization_value: Instance
+
+       init(modelbuilder: ModelBuilder, mainmodule: MModule, arguments: Array[String])
+       do
+               var init_type = new MInitType(mainmodule.model)
+               initialization_value = new MutableInstance(init_type)
+               super
+       end
+
        # Subtyping test for the virtual machine
        redef fun is_subtype(sub, sup: MType): Bool
        do
@@ -96,7 +106,6 @@ class VirtualMachine super NaiveInterpreter
                # Sub can be discovered inside a Generic type during the subtyping test
                if not sub.mclass.loaded then create_class(sub.mclass)
 
-               if anchor == null then anchor = sub
                if sup isa MGenericType then
                        var sub2 = sub.supertype_to(mainmodule, anchor, sup.mclass)
                        assert sub2.mclass == sup.mclass
@@ -136,29 +145,82 @@ class VirtualMachine super NaiveInterpreter
 
                recv.vtable = recv.mtype.as(MClassType).mclass.vtable
 
-               assert(recv isa MutableInstance)
+               assert recv isa MutableInstance
 
-               recv.internal_attributes = init_internal_attributes(null_instance, recv.mtype.as(MClassType).mclass.all_mattributes(mainmodule, none_visibility).length)
+               recv.internal_attributes = init_internal_attributes(initialization_value, recv.mtype.as(MClassType).mclass.all_mattributes(mainmodule, none_visibility).length)
                super
        end
 
+       # Associate a `PrimitiveInstance` to its `VTable`
+       redef fun init_instance_primitive(recv: Instance)
+       do
+               if not recv.mtype.as(MClassType).mclass.loaded then create_class(recv.mtype.as(MClassType).mclass)
+
+               recv.vtable = recv.mtype.as(MClassType).mclass.vtable
+       end
+
+       # Create a virtual table for this `MClass` if not already done
+       redef fun get_primitive_class(name: String): MClass
+       do
+               var mclass = super
+
+               if not mclass.loaded then create_class(mclass)
+
+               return mclass
+       end
+
        # Initialize the internal representation of an object (its attribute values)
-       private fun init_internal_attributes(null_instance: Instance, size: Int): Pointer
+       # `init_instance` is the initial value of attributes
+       private fun init_internal_attributes(init_instance: Instance, size: Int): Pointer
                import Array[Instance].length, Array[Instance].[] `{
 
                Instance* attributes = malloc(sizeof(Instance) * size);
 
                int i;
                for(i=0; i<size; i++)
-                       attributes[i] = null_instance;
+                       attributes[i] = init_instance;
 
-               Instance_incr_ref(null_instance);
+               Instance_incr_ref(init_instance);
                return attributes;
        `}
 
        # Creates the runtime structures for this class
        fun create_class(mclass: MClass) do     mclass.make_vt(self)
 
+       # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
+       redef fun send(mproperty: MMethod, args: Array[Instance]): nullable Instance
+       do
+               var recv = args.first
+               var mtype = recv.mtype
+               var ret = send_commons(mproperty, args, mtype)
+               if ret != null then return ret
+
+               var propdef = method_dispatch(mproperty, recv.vtable.as(not null))
+
+               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
+       do
+               return method_dispatch_ph(vtable.internal_vtable, vtable.mask,
+                               mproperty.intro_mclassdef.mclass.vtable.id, mproperty.offset)
+       end
+
+       # Execute a method dispatch with perfect hashing
+       private fun method_dispatch_ph(vtable: Pointer, mask: Int, id: Int, offset: Int): MMethodDef `{
+               // Perfect hashing position
+               int hv = mask & id;
+               long unsigned int *pointer = (long unsigned int*)(((long int *)vtable)[-hv]);
+
+               // pointer+2 is the position where methods are
+               // Add the offset of property and get the method implementation
+               MMethodDef propdef = (MMethodDef)*(pointer + 2 + offset);
+
+               return propdef;
+       `}
+
        # Return the value of the attribute `mproperty` for the object `recv`
        redef fun read_attribute(mproperty: MAttribute, recv: Instance): Instance
        do
@@ -170,15 +232,21 @@ class VirtualMachine super NaiveInterpreter
                var i = read_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
                                        recv.vtable.mask, id, mproperty.offset)
 
+               # If we get a `MInit` value, throw an error
+               if i == initialization_value then
+                       fatal("Uninitialized attribute {mproperty.name}")
+                       abort
+               end
+
                return i
        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;
@@ -205,12 +273,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;
@@ -222,6 +290,21 @@ class VirtualMachine super NaiveInterpreter
                ((Instance *)instance)[absolute_offset + offset] = value;
                Instance_incr_ref(value);
        `}
+
+       # Is the attribute `mproperty` initialized in the instance `recv`?
+       redef fun isset_attribute(mproperty: MAttribute, recv: Instance): Bool
+       do
+               assert recv isa MutableInstance
+
+               # Read the attribute value with internal perfect hashing read
+               # because we do not want to throw an error if the value is `initialization_value`
+               var id = mproperty.intro_mclassdef.mclass.vtable.id
+
+               var i = read_attribute_ph(recv.internal_attributes, recv.vtable.internal_vtable,
+                                       recv.vtable.mask, id, mproperty.offset)
+
+               return i != initialization_value
+       end
 end
 
 redef class MClass
@@ -286,15 +369,20 @@ redef class MClass
                # 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
+
                # The virtual table now needs to be filled with pointer to methods
+               superclasses.add(self)
+               for cl in superclasses do
+                       fill_vtable(v, vtable.as(not null), cl)
+               end
        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
@@ -316,14 +404,19 @@ redef class MClass
                var self_methods = 0
                var nb_introduced_attributes = 0
 
-               # For self attributes, fixing offsets
-               var relative_offset = 0
+               # Fixing offsets for self attributes and methods
+               var relative_offset_attr = 0
+               var relative_offset_meth = 0
                for p in intro_mproperties(none_visibility) do
-                       if p isa MMethod then self_methods += 1
+                       if p isa MMethod then
+                               self_methods += 1
+                               p.offset = relative_offset_meth
+                               relative_offset_meth += 1
+                       end
                        if p isa MAttribute then
                                nb_introduced_attributes += 1
-                               p.offset = relative_offset
-                               relative_offset += 1
+                               p.offset = relative_offset_attr
+                               relative_offset_attr += 1
                        end
                end
 
@@ -343,10 +436,28 @@ redef class MClass
                vtable.internal_vtable = v.memory_manager.init_vtable(ids_total, nb_methods_total, d, vtable.mask)
        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
+       private fun fill_vtable(v:VirtualMachine, table: VTable, cl: MClass)
+       do
+               var methods = new Array[MMethodDef]
+               for m in cl.intro_mproperties(none_visibility) do
+                       if m isa MMethod then
+                               # `propdef` is the most specific implementation for this MMethod
+                               var propdef = m.lookup_first_definition(v.mainmodule, self.intro.bound_mtype)
+                               methods.push(propdef)
+                       end
+               end
+
+               # Call a method in C to put propdefs of self methods in the vtables
+               v.memory_manager.put_methods(vtable.internal_vtable, vtable.mask, cl.vtable.id, methods)
+       end
+
        # 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]
@@ -380,8 +491,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
@@ -437,8 +548,8 @@ redef class MClass
        end
 
        # Update positions of self class in `parent`
-       #     `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, parent: MClass)
        do
                parent.positions_attributes[self] = attributes_offsets
@@ -451,6 +562,11 @@ redef class MAttribute
        var offset: Int
 end
 
+redef class MMethod
+       # Represents the relative offset of this attribute in the runtime instance
+       var offset: Int
+end
+
 # Redef MutableInstance to improve implementation of attributes in objects
 redef class MutableInstance
 
@@ -458,6 +574,34 @@ redef class MutableInstance
        var internal_attributes: Pointer
 end
 
+# Redef to associate an `Instance` to its `VTable`
+redef class Instance
+       var vtable: nullable VTable
+end
+
+# Is the type of the initial value inside attributes
+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
+       redef fun need_anchor do return false
+       redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do return self
+       redef fun can_resolve_for(mtype, anchor, mmodule) do return true
+
+       redef fun collect_mclassdefs(mmodule) do return new HashSet[MClassDef]
+
+       redef fun collect_mclasses(mmodule) do return new HashSet[MClass]
+
+       redef fun collect_mtypes(mmodule) do return new HashSet[MClassType]
+end
+
 # A VTable contains the virtual method table for the dispatch
 # and informations to perform subtyping tests
 class VTable
@@ -474,10 +618,6 @@ class VTable
        var classname: String is noinit
 end
 
-redef class Instance
-       var vtable: nullable VTable
-end
-
 # Handle memory, used for allocate virtual table and associated structures
 class MemoryManager
 
@@ -540,4 +680,29 @@ class MemoryManager
 
                return vtable;
        `}
+
+       # 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
+       fun put_methods(vtable: Pointer, mask: Int, id: Int, methods: Array[MMethodDef])
+               import Array[MMethodDef].length, Array[MMethodDef].[] `{
+
+               // Get the area to fill with methods by a sequence of perfect hashing
+               int hv = mask & id;
+               long unsigned int *pointer = (long unsigned int*)(((long unsigned int *)vtable)[-hv]);
+
+               // pointer+2 is the beginning of the area for methods implementation
+               int length = Array_of_MMethodDef_length(methods);
+               long unsigned int *area = (pointer + 2);
+               int i;
+
+               for(i=0; i<length; i++)
+               {
+                       MMethodDef method = Array_of_MMethodDef__index(methods, i);
+                       area[i] = (long unsigned int)method;
+                       MMethodDef_incr_ref(method);
+               }
+       `}
 end