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 for this class
172 var min_visibility
= public_visibility
173 for p
in parent
.intro_mproperties
(min_visibility
) do
174 if p
isa MMethod then
179 ids
.push
(parent
.vtable
.id
)
180 nb_methods
.push
(count
)
183 # When all super-classes have their identifiers and vtables, allocate current one
184 allocate_vtable
(v
, ids
, nb_methods
)
186 # The virtual table now needs to be filled with pointer to methods
189 # Allocate a single vtable
190 # ids : Array of superclasses identifiers
191 # nb_methods : Array which contain the number of methods for each class in ids
192 private fun allocate_vtable
(v
: VirtualMachine, ids
: Array[Int], nb_methods
: Array[Int])
195 var idc
= new Array[Int]
197 vtable
.mask
= v
.ph
.pnand
(ids
, 1, idc
) - 1
199 vtable
.classname
= name
201 # Add current id to Array of super-ids
202 var ids_total
= new Array[Int]
203 ids_total
.add_all
(ids
)
204 ids_total
.push
(vtable
.id
)
206 var nb_methods_total
= new Array[Int]
208 var min_visibility
= public_visibility
209 for p
in intro_mproperties
(min_visibility
) do
210 if p
isa MMethod then
214 nb_methods_total
.add_all
(nb_methods
)
215 nb_methods_total
.push
(count
)
217 vtable
.internal_vtable
= v
.memory_manager
.init_vtable
(ids_total
, nb_methods_total
, vtable
.mask
)
221 # A VTable contains the virtual method table for the dispatch
222 # and informations to perform subtyping tests
224 # The mask to perform perfect hashing
227 # Unique identifier given by perfect hashing
230 # Pointer to the c-allocated area, represents the virtual table
231 var internal_vtable
: Pointer
233 # The short classname of this class
234 var classname
: String
241 var vtable
: nullable VTable
247 # An instance is associated to its class virtual table
248 if mt
isa MClassType then
249 vtable
= mt
.mclass
.vtable
254 # Handle memory, used for allocate virtual table and associated structures
257 # Allocate and fill a virtual table
258 fun init_vtable
(ids
: Array[Int], nb_methods
: Array[Int], mask
: Int): Pointer
260 # Allocate in C current virtual table
261 var res
= intern_init_vtable
(ids
, nb_methods
, mask
)
266 # Construct virtual tables with a bi-dimensional layout
267 private fun intern_init_vtable
(ids
: Array[Int], nb_methods
: Array[Int], mask
: Int): Pointer
268 import Array[Int].length
, Array[Int].[] `{
270 // Allocate and fill current virtual table
272 int total_size = 0; // total size of this virtual table
273 int nb_classes = Array_of_Int_length(nb_methods);
274 for(i = 0; i<nb_classes; i++) {
275 /* - One for each method of this class
276 * - One for the delta (pointer to attributes)
279 total_size += Array_of_Int__index(nb_methods, i);
283 // And the size of the perfect hashtable
284 total_size += mask+1;
285 long unsigned int* vtable = malloc(sizeof(long unsigned int)*total_size);
287 // Initialisation to the first position of the virtual table (ie : Object)
288 long unsigned int *init = vtable + mask + 2;
289 for(i=0; i<total_size; i++)
290 vtable[i] = (long unsigned int)init;
292 // Set the virtual table to its position 0
293 // ie: after the hashtable
294 vtable = vtable + mask + 1;
296 int current_size = 1;
297 for(i = 0; i < nb_classes; i++) {
299 vtable[hv] contains a pointer to the group of introducted methods
300 For each superclasse we have in virtual table :
301 (id | delta (attributes) | introduced methods)
303 int hv = mask & Array_of_Int__index(ids, i);
305 vtable[current_size] = Array_of_Int__index(ids, i);
306 vtable[-hv] = (long unsigned int)&(vtable[current_size]);
309 current_size += Array_of_Int__index(nb_methods, i);