# Implementation of the Nit virtual machine
module vm
-intrude import interpreter::naive_interpreter
+import interpreter::naive_interpreter
import model_utils
import perfect_hashing
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)
# 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
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
# 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;
`}
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
((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