nitg-e: avoid automatic boxing in vt resolution
[nit.git] / src / separate_erasure_compiler.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Separate compilation of a Nit program with generic type erasure
16 module separate_erasure_compiler
17
18
19 import separate_compiler
20
21 redef class ToolContext
22 # --erasure
23 var opt_erasure: OptionBool = new OptionBool("Erase generic types", "--erasure")
24
25 redef init
26 do
27 super
28 self.option_context.add_option(self.opt_erasure)
29 end
30 end
31
32 redef class ModelBuilder
33 fun run_separate_erasure_compiler(mainmodule: MModule, runtime_type_analysis: RapidTypeAnalysis)
34 do
35 var time0 = get_time
36 self.toolcontext.info("*** COMPILING TO C ***", 1)
37
38 var compiler = new SeparateErasureCompiler(mainmodule, runtime_type_analysis, self)
39
40 # compile class structures
41 for m in mainmodule.in_importation.greaters do
42 for mclass in m.intro_mclasses do
43 compiler.compile_class_to_c(mclass)
44 end
45 end
46
47 # The main function of the C
48 compiler.compile_main_function
49
50 # compile methods
51 for m in mainmodule.in_importation.greaters do
52 compiler.compile_module_to_c(m)
53 end
54
55 write_and_make(compiler)
56 end
57 end
58
59 class SeparateErasureCompiler
60 super SeparateCompiler
61
62 private var class_ids: HashMap[MClass, Int] = new HashMap[MClass, Int]
63 private var class_colors: Map[MClass, Int]
64 private var class_tables: Map[MClass, Array[nullable MClass]]
65
66 init(mainmodule: MModule, runtime_type_analysis: RapidTypeAnalysis, mmbuilder: ModelBuilder) do
67 var mclasses = new HashSet[MClass]
68 mclasses.add_all(mmbuilder.model.mclasses)
69
70 # classes coloration
71 if modelbuilder.toolcontext.opt_phmod_typing.value then
72 # set type unique id
73 for mclass in mclasses do
74 self.class_ids[mclass] = self.class_ids.length + 1
75 end
76
77 var class_coloring = new ClassModPerfectHashing(mainmodule)
78 self.class_colors = class_coloring.compute_masks(mclasses, class_ids)
79 self.class_tables = class_coloring.hash_type_tables(mclasses, class_ids, class_colors)
80
81 self.header.add_decl("int HASH(int, int);")
82 var v = new_visitor
83 v.add_decl("int HASH(int mask, int id) \{")
84 v.add_decl("return mask % id;")
85 v.add_decl("\}")
86 else if modelbuilder.toolcontext.opt_phand_typing.value then
87 # set type unique id
88 for mclass in mclasses do
89 self.class_ids[mclass] = self.class_ids.length + 1
90 end
91
92 var class_coloring = new ClassAndPerfectHashing(mainmodule)
93 self.class_colors = class_coloring.compute_masks(mclasses, class_ids)
94 self.class_tables = class_coloring.hash_type_tables(mclasses, class_ids, class_colors)
95
96 self.header.add_decl("int HASH(int, int);")
97 var v = new_visitor
98 v.add_decl("int HASH(int mask, int id) \{")
99 v.add_decl("return mask & id;")
100 v.add_decl("\}")
101 else
102 var class_coloring
103 if modelbuilder.toolcontext.opt_bm_typing.value then
104 class_coloring = new NaiveClassColoring(mainmodule)
105 else
106 class_coloring = new ClassColoring(mainmodule)
107 end
108 # set type unique id
109 for mclass in mclasses do
110 self.class_ids[mclass] = self.class_ids.length + 1
111 end
112 self.class_colors = class_coloring.colorize(modelbuilder.model.mclasses)
113 self.class_tables = class_coloring.build_type_tables(modelbuilder.model.mclasses, class_colors)
114 end
115
116 # for the class_name and output_class_name methods
117 self.compile_class_names
118 end
119
120 redef fun compile_header_structs do
121 self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
122 self.header.add_decl("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
123 self.header.add_decl("struct class \{ int id; int box_kind; int color; struct vts_table *vts_table; struct type_table *type_table; nitmethod_t vft[1]; \}; /* general C type representing a Nit class. */")
124 self.header.add_decl("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
125 self.header.add_decl("struct vts_entry \{ short int is_nullable; struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
126
127 if modelbuilder.toolcontext.opt_phmod_typing.value or modelbuilder.toolcontext.opt_phand_typing.value then
128 self.header.add_decl("struct vts_table \{ int mask; struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
129 else
130 self.header.add_decl("struct vts_table \{ struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
131 end
132
133 self.header.add_decl("typedef struct \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
134 self.header.add_decl("extern const char const * class_names[];")
135 end
136
137 redef fun compile_class_names do
138 # Build type names table
139 var type_array = new Array[nullable MClass]
140 for t, id in class_ids do
141 if id >= type_array.length then
142 for i in [type_array.length..id[ do
143 type_array[i] = null
144 end
145 end
146 type_array[id] = t
147 end
148
149 var v = self.new_visitor
150 v.add("const char const * class_names[] = \{")
151 for t in type_array do
152 if t == null then
153 v.add("NULL, /* empty */")
154 else
155 v.add("\"{t}\",")
156 end
157 end
158 v.add("\};")
159 end
160
161 redef fun compile_class_to_c(mclass: MClass)
162 do
163 var mtype = mclass.intro.bound_mtype
164 var c_name = mclass.c_name
165
166 var vft = self.method_tables[mclass]
167 var attrs = self.attr_tables[mclass]
168 var class_table = self.class_tables[mclass]
169 var v = self.new_visitor
170
171 v.add_decl("/* runtime class {c_name} */")
172 var idnum = classids.length
173 var idname = "ID_" + c_name
174 self.classids[mtype] = idname
175 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
176
177 self.header.add_decl("extern const struct class_{c_name} class_{c_name};")
178 self.header.add_decl("struct class_{c_name} \{")
179 self.header.add_decl("int id;")
180 self.header.add_decl("int box_kind;")
181 self.header.add_decl("int color;")
182 self.header.add_decl("const struct vts_table *vts_table;")
183 self.header.add_decl("struct type_table *type_table;")
184 self.header.add_decl("nitmethod_t vft[{vft.length}];")
185 self.header.add_decl("\};")
186
187 # Build class vft
188 v.add_decl("const struct class_{c_name} class_{c_name} = \{")
189 v.add_decl("{self.class_ids[mclass]},")
190 v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
191 v.add_decl("{self.class_colors[mclass]},")
192 if build_class_vts_table(mclass) then
193 v.add_decl("(const struct vts_table*) &vts_table_{c_name},")
194 else
195 v.add_decl("NULL,")
196 end
197 v.add_decl("(struct type_table*) &type_table_{c_name},")
198 v.add_decl("\{")
199 for i in [0 .. vft.length[ do
200 var mpropdef = vft[i]
201 if mpropdef == null then
202 v.add_decl("NULL, /* empty */")
203 else
204 if true or mpropdef.mclassdef.bound_mtype.ctype != "val*" then
205 v.add_decl("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
206 else
207 v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
208 end
209 end
210 end
211 v.add_decl("\}")
212 v.add_decl("\};")
213
214 # Build class type table
215 self.header.add_decl("extern const struct type_table_{c_name} type_table_{c_name};")
216 self.header.add_decl("struct type_table_{c_name} \{")
217 self.header.add_decl("int size;")
218 self.header.add_decl("int table[{class_table.length}];")
219 self.header.add_decl("\};")
220
221 v.add_decl("const struct type_table_{c_name} type_table_{c_name} = \{")
222 v.add_decl("{class_table.length},")
223 v.add_decl("\{")
224 for msuper in class_table do
225 if msuper == null then
226 v.add_decl("-1, /* empty */")
227 else
228 v.add_decl("{self.class_ids[msuper]}, /* {msuper} */")
229 end
230 end
231 v.add_decl("\}")
232 v.add_decl("\};")
233
234 #Build instance struct
235 if mtype.ctype != "val*" then
236 self.header.add_decl("struct instance_{c_name} \{")
237 self.header.add_decl("const struct class *class;")
238 self.header.add_decl("{mtype.ctype} value;")
239 self.header.add_decl("\};")
240
241 self.header.add_decl("val* BOX_{c_name}({mtype.ctype});")
242 v.add_decl("/* allocate {mtype} */")
243 v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
244 v.add("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
245 v.add("res->class = (struct class*) &class_{c_name};")
246 v.add("res->value = value;")
247 v.add("return (val*)res;")
248 v.add("\}")
249 return
250 end
251
252 var is_native_array = mclass.name == "NativeArray"
253
254 var sig
255 if is_native_array then
256 sig = "int length"
257 else
258 sig = ""
259 end
260
261 #Build instance struct
262 #extern const struct instance_array__NativeArray instance_array__NativeArray;
263 self.header.add_decl("struct instance_{c_name} \{")
264 self.header.add_decl("const struct class *class;")
265 self.header.add_decl("nitattribute_t attrs[{attrs.length}];")
266 if is_native_array then
267 # NativeArrays are just a instance header followed by an array of values
268 self.header.add_decl("val* values[0];")
269 end
270 self.header.add_decl("\};")
271
272
273 self.header.add_decl("{mtype.ctype} NEW_{c_name}({sig});")
274 v.add_decl("/* allocate {mtype} */")
275 v.add_decl("{mtype.ctype} NEW_{c_name}({sig}) \{")
276 var res = v.new_named_var(mtype, "self")
277 res.is_exact = true
278 if is_native_array then
279 var mtype_elt = mtype.arguments.first
280 v.add("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
281 else
282 v.add("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
283 end
284 v.add("{res}->class = (struct class*) &class_{c_name};")
285
286 self.generate_init_attr(v, res, mtype)
287 v.add("return {res};")
288 v.add("\}")
289
290 generate_check_init_instance(mtype)
291 end
292
293 private fun build_class_vts_table(mclass: MClass): Bool do
294 if self.vt_tables[mclass].is_empty then return false
295
296 self.header.add_decl("extern const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name};")
297 self.header.add_decl("struct vts_table_{mclass.c_name} \{")
298 if modelbuilder.toolcontext.opt_phmod_typing.value or modelbuilder.toolcontext.opt_phand_typing.value then
299 self.header.add_decl("int mask;")
300 end
301 self.header.add_decl("struct vts_entry vts[{self.vt_tables[mclass].length}];")
302 self.header.add_decl("\};")
303
304 var v = new_visitor
305 v.add_decl("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
306 if modelbuilder.toolcontext.opt_phmod_typing.value or modelbuilder.toolcontext.opt_phand_typing.value then
307 v.add_decl("{vt_masks[mclass]},")
308 end
309 v.add_decl("\{")
310
311 for vt in self.vt_tables[mclass] do
312 if vt == null then
313 v.add_decl("\{-1, NULL\}, /* empty */")
314 else
315 var is_null = 0
316 var bound = retrieve_vt_bound(mclass.intro.bound_mtype, vt.bound)
317 while bound isa MNullableType do
318 bound = retrieve_vt_bound(mclass.intro.bound_mtype, bound.mtype)
319 is_null = 1
320 end
321 v.add_decl("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
322 end
323 end
324 v.add_decl("\},")
325 v.add_decl("\};")
326 return true
327 end
328
329 private fun retrieve_vt_bound(anchor: MClassType, mtype: nullable MType): MType do
330 if mtype == null then
331 print "NOT YET IMPLEMENTED: retrieve_vt_bound on null"
332 abort
333 end
334 if mtype isa MVirtualType then
335 return mtype.anchor_to(mainmodule, anchor)
336 else if mtype isa MParameterType then
337 return mtype.anchor_to(mainmodule, anchor)
338 else
339 return mtype
340 end
341 end
342
343 redef fun new_visitor do return new SeparateErasureCompilerVisitor(self)
344 end
345
346 class SeparateErasureCompilerVisitor
347 super SeparateCompilerVisitor
348
349 redef fun init_instance(mtype)
350 do
351 return self.new_expr("NEW_{mtype.mclass.c_name}()", mtype)
352 end
353
354 redef fun type_test(value, mtype)
355 do
356 self.add("/* type test for {value.inspect} isa {mtype} */")
357
358 var res = self.new_var(bool_type)
359
360 var cltype = self.get_name("cltype")
361 self.add_decl("int {cltype};")
362 var idtype = self.get_name("idtype")
363 self.add_decl("int {idtype};")
364 var is_nullable = self.get_name("is_nullable")
365 self.add_decl("short int {is_nullable};")
366 var is_null = self.get_name("is_null")
367 self.add_decl("short int {is_null};")
368
369 var maybe_null = 0
370 if mtype isa MNullableType then
371 mtype = mtype.mtype
372 maybe_null = 1
373 self.add("{is_nullable} = 1;")
374 end
375 if mtype isa MParameterType then
376 # Here we get the bound of the the formal type (eh, erasure...)
377 mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, false)
378 if mtype isa MNullableType then
379 mtype = mtype.mtype
380 maybe_null = 1
381 self.add("{is_nullable} = 1;")
382 end
383 end
384 if mtype isa MVirtualType then
385 # FIXME virtual types should not be erased but got from the class table of the current receiver (self.frame.arguments.first)
386 #mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, true)
387 #if mtype isa MNullableType then
388 # mtype = mtype.mtype
389 # maybe_null = true
390 #end
391 end
392
393 if value.mcasttype.is_subtype(self.frame.mpropdef.mclassdef.mmodule, self.frame.mpropdef.mclassdef.bound_mtype, mtype) then
394 self.add("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
395 return res
396 end
397
398 var class_ptr
399 var type_table
400 if value.mtype.ctype == "val*" then
401 class_ptr = "{value}->class->"
402 self.add("{is_null} = {value} == NULL;")
403 else
404 var mclass = value.mtype.as(MClassType).mclass
405 class_ptr = "class_{mclass.c_name}."
406 self.add("{is_null} = 0;")
407 end
408
409 if mtype isa MClassType then
410 self.add("{cltype} = class_{mtype.mclass.c_name}.color;")
411 self.add("{idtype} = class_{mtype.mclass.c_name}.id;")
412 if maybe_null == 0 then
413 self.add("{is_nullable} = 0;")
414 end
415 else if mtype isa MVirtualType then
416 var recv = self.frame.arguments.first
417 var recv_ptr
418 if recv.mtype.ctype == "val*" then
419 recv_ptr = "{recv}->class->"
420 else
421 var mclass = recv.mtype.as(MClassType).mclass
422 recv_ptr = "class_{mclass.c_name}."
423 end
424 var entry = self.get_name("entry")
425 self.add("struct vts_entry {entry};")
426 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
427 self.add("{entry} = {recv_ptr}vts_table->vts[HASH({recv_ptr}vts_table->mask, {mtype.mproperty.const_color})];")
428 else
429 self.add("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
430 end
431 self.add("{cltype} = {entry}.class->color;")
432 self.add("{idtype} = {entry}.class->id;")
433 if maybe_null == 0 then
434 self.add("{is_nullable} = {entry}.is_nullable;")
435 end
436 else
437 self.debug("type_test({value.inspect}, {mtype})")
438 abort
439 end
440
441 # check color is in table
442 self.add("if({is_null}) \{")
443 self.add("{res} = {is_nullable};")
444 self.add("\} else \{")
445 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
446 self.add("{cltype} = HASH({class_ptr}color, {idtype});")
447 end
448 self.add("if({cltype} >= {class_ptr}type_table->size) \{")
449 self.add("{res} = 0;")
450 self.add("\} else \{")
451 self.add("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
452 self.add("\}")
453 self.add("\}")
454
455 return res
456 end
457
458 redef fun class_name_string(value)
459 do
460 var res = self.get_name("var_class_name")
461 self.add_decl("const char *{res};")
462 self.add("{res} = class_names[{value}->class->id];")
463 return res
464 end
465
466 redef fun array_instance(array, elttype)
467 do
468 var nclass = self.get_class("NativeArray")
469 elttype = self.anchor(elttype)
470 var arraytype = self.get_class("Array").get_mtype([elttype])
471 var res = self.init_instance(arraytype)
472 self.add("\{ /* {res} = array_instance Array[{elttype}] */")
473 var nat = self.new_var(self.get_class("NativeArray").get_mtype([elttype]))
474 nat.is_exact = true
475 self.add("{nat} = NEW_{nclass.c_name}({array.length});")
476 for i in [0..array.length[ do
477 var r = self.autobox(array[i], self.object_type)
478 self.add("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
479 end
480 var length = self.int_instance(array.length)
481 self.send(self.get_property("with_native", arraytype), [res, nat, length])
482 self.check_init_instance(res, arraytype)
483 self.add("\}")
484 return res
485 end
486
487 redef fun calloc_array(ret_type, arguments)
488 do
489 var ret = ret_type.as(MClassType)
490 self.ret(self.new_expr("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type))
491 end
492 end