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 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 # Subtyping test for the virtual machine
48 redef fun is_subtype
(sub
, sup
: MType): Bool
50 var anchor
= self.frame
.arguments
.first
.mtype
.as(MClassType)
51 var sup_accept_null
= false
52 if sup
isa MNullableType then
53 sup_accept_null
= true
55 else if sup
isa MNullType then
56 sup_accept_null
= true
59 # Can `sub` provides null or not?
60 # Thus we can match with `sup_accept_null`
61 # Also discard the nullable marker if it exists
62 if sub
isa MNullableType then
63 if not sup_accept_null
then return false
65 else if sub
isa MNullType then
66 return sup_accept_null
68 # Now the case of direct null and nullable is over
70 # An unfixed formal type can only accept itself
71 if sup
isa MParameterType or sup
isa MVirtualType then
75 if sub
isa MParameterType or sub
isa MVirtualType then
76 sub
= sub
.anchor_to
(mainmodule
, anchor
)
77 # Manage the second layer of null/nullable
78 if sub
isa MNullableType then
79 if not sup_accept_null
then return false
81 else if sub
isa MNullType then
82 return sup_accept_null
86 assert sub
isa MClassType
88 # `sup` accepts only null
89 if sup
isa MNullType then return false
91 assert sup
isa MClassType
93 # Create the sup vtable if not create
94 if not sup
.mclass
.loaded
then create_class
(sup
.mclass
)
96 # Sub can be discovered inside a Generic type during the subtyping test
97 if not sub
.mclass
.loaded
then create_class
(sub
.mclass
)
99 if anchor
== null then anchor
= sub
100 if sup
isa MGenericType then
101 var sub2
= sub
.supertype_to
(mainmodule
, anchor
, sup
.mclass
)
102 assert sub2
.mclass
== sup
.mclass
104 for i
in [0..sup
.mclass
.arity
[ do
105 var sub_arg
= sub2
.arguments
[i
]
106 var sup_arg
= sup
.arguments
[i
]
107 var res
= is_subtype
(sub_arg
, sup_arg
)
109 if res
== false then return false
114 var super_id
= sup
.mclass
.vtable
.id
115 var mask
= sub
.mclass
.vtable
.mask
117 return inter_is_subtype
(super_id
, mask
, sub
.mclass
.vtable
.internal_vtable
)
120 # Subtyping test with perfect hashing
121 private fun inter_is_subtype
(id
: Int, mask
:Int, vtable
: Pointer): Bool `{
122 // hv is the position in hashtable
125 // Follow the pointer to somewhere in the vtable
126 long unsigned int *offset = (long unsigned int*)(((long int *)vtable)[-hv]);
128 // If the pointed value is corresponding to the identifier, the test is true, otherwise false
129 return *offset == id;
132 # Redef init_instance to simulate the loading of a class
133 redef fun init_instance
(recv
: Instance)
135 if not recv
.mtype
.as(MClassType).mclass
.loaded
then create_class
(recv
.mtype
.as(MClassType).mclass
)
138 recv
.vtable
= recv
.mtype
.as(MClassType).mclass
.vtable
141 # Creates the runtime structures for this class
142 fun create_class
(mclass
: MClass) do mclass
.make_vt
(self)
146 # A reference to the virtual table of this class
147 var vtable
: nullable VTable
149 # True when the class is effectively loaded by the vm, false otherwise
150 var loaded
: Bool = false
152 # Allocates a VTable for this class and gives it an id
153 private fun make_vt
(v
: VirtualMachine)
155 if loaded
then return
157 # Superclasses contains all superclasses except self
158 var superclasses
= new Array[MClass]
159 superclasses
.add_all
(ancestors
)
160 superclasses
.remove
(self)
161 v
.mainmodule
.linearize_mclasses
(superclasses
)
163 # Make_vt for super-classes
164 var ids
= new Array[Int]
165 var nb_methods
= new Array[Int]
167 for parent
in superclasses
do
168 if parent
.vtable
== null then parent
.make_vt
(v
)
170 # Get the number of introduced methods and attributes for this class
173 for p
in parent
.intro_mproperties
(none_visibility
) do
174 if p
isa MMethod then methods
+= 1
177 ids
.push
(parent
.vtable
.id
)
178 nb_methods
.push
(methods
)
181 # When all super-classes have their identifiers and vtables, allocate current one
182 allocate_vtable
(v
, ids
, nb_methods
)
184 # The virtual table now needs to be filled with pointer to methods
187 # Allocate a single vtable
188 # ids : Array of superclasses identifiers
189 # nb_methods : Array which contain the number of introducted methods for each class in ids
190 private fun allocate_vtable
(v
: VirtualMachine, ids
: Array[Int], nb_methods
: Array[Int])
193 var idc
= new Array[Int]
195 vtable
.mask
= v
.ph
.pnand
(ids
, 1, idc
) - 1
197 vtable
.classname
= name
199 # Add current id to Array of super-ids
200 var ids_total
= new Array[Int]
201 ids_total
.add_all
(ids
)
202 ids_total
.push
(vtable
.id
)
204 var nb_methods_total
= new Array[Int]
206 for p
in intro_mproperties
(none_visibility
) do
207 if p
isa MMethod then methods
+= 1
209 nb_methods_total
.add_all
(nb_methods
)
210 nb_methods_total
.push
(methods
)
212 vtable
.internal_vtable
= v
.memory_manager
.init_vtable
(ids_total
, nb_methods_total
, vtable
.mask
)
216 # A VTable contains the virtual method table for the dispatch
217 # and informations to perform subtyping tests
219 # The mask to perform perfect hashing
222 # Unique identifier given by perfect hashing
225 # Pointer to the c-allocated area, represents the virtual table
226 var internal_vtable
: Pointer
228 # The short classname of this class
229 var classname
: String
235 var vtable
: nullable VTable
238 # Handle memory, used for allocate virtual table and associated structures
241 # Allocate and fill a virtual table
242 fun init_vtable
(ids
: Array[Int], nb_methods
: Array[Int], mask
: Int): Pointer
244 # Allocate in C current virtual table
245 var res
= intern_init_vtable
(ids
, nb_methods
, mask
)
250 # Construct virtual tables with a bi-dimensional layout
251 private fun intern_init_vtable
(ids
: Array[Int], nb_methods
: Array[Int], mask
: Int): Pointer
252 import Array[Int].length
, Array[Int].[] `{
254 // Allocate and fill current virtual table
256 int total_size = 0; // total size of this virtual table
257 int nb_classes = Array_of_Int_length(nb_methods);
258 for(i = 0; i<nb_classes; i++) {
259 /* - One for each method of this class
260 * - One for the delta (offset of this group of attributes in objects)
263 total_size += Array_of_Int__index(nb_methods, i);
267 // Add the size of the perfect hashtable
268 total_size += mask+1;
269 long unsigned int* vtable = malloc(sizeof(long unsigned int)*total_size);
271 // Initialisation to the first position of the virtual table (ie : Object)
272 long unsigned int *init = vtable + mask + 2;
273 for(i=0; i<total_size; i++)
274 vtable[i] = (long unsigned int)init;
276 // Set the virtual table to its position 0
277 // ie: after the hashtable
278 vtable = vtable + mask + 1;
280 int current_size = 1;
281 for(i = 0; i < nb_classes; i++) {
283 vtable[hv] contains a pointer to the group of introducted methods
284 For each superclasse we have in virtual table :
285 (id | delta | introduced methods)
287 int hv = mask & Array_of_Int__index(ids, i);
289 vtable[current_size] = Array_of_Int__index(ids, i);
290 vtable[-hv] = (long unsigned int)&(vtable[current_size]);
293 current_size += Array_of_Int__index(nb_methods, i);