layout_builders: renamed TypingLayout in Layout (for further abstraction)
[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 intrude import separate_compiler
19
20 # Add separate erased compiler specific options
21 redef class ToolContext
22 # --erasure
23 var opt_erasure: OptionBool = new OptionBool("Erase generic types", "--erasure")
24 # --no-check-erasure-cast
25 var opt_no_check_erasure_cast: OptionBool = new OptionBool("Disable implicit casts on unsafe return with erasure-typing policy (dangerous)", "--no-check-erasure-cast")
26
27 redef init
28 do
29 super
30 self.option_context.add_option(self.opt_erasure, self.opt_no_check_erasure_cast)
31 end
32 end
33
34 redef class ModelBuilder
35 fun run_separate_erasure_compiler(mainmodule: MModule, runtime_type_analysis: RapidTypeAnalysis)
36 do
37 var time0 = get_time
38 self.toolcontext.info("*** COMPILING TO C ***", 1)
39
40 var compiler = new SeparateErasureCompiler(mainmodule, self, runtime_type_analysis)
41 compiler.compile_header
42
43 # compile class structures
44 compiler.new_file
45 for m in mainmodule.in_importation.greaters do
46 for mclass in m.intro_mclasses do
47 compiler.compile_class_to_c(mclass)
48 end
49 end
50
51 # The main function of the C
52 compiler.new_file
53 compiler.compile_main_function
54
55 # compile methods
56 for m in mainmodule.in_importation.greaters do
57 compiler.new_file
58 compiler.compile_module_to_c(m)
59 end
60
61 compiler.display_stats
62
63 write_and_make(compiler)
64 end
65 end
66
67 class SeparateErasureCompiler
68 super SeparateCompiler
69
70 private var class_layout: nullable Layout[MClass]
71 protected var vt_layout: nullable PropertyLayout[MVirtualTypeProp]
72
73 init(mainmodule: MModule, mmbuilder: ModelBuilder, runtime_type_analysis: RapidTypeAnalysis) do
74 super
75
76 var mclasses = new HashSet[MClass].from(mmbuilder.model.mclasses)
77
78 var layout_builder: TypingLayoutBuilder[MClass]
79 if modelbuilder.toolcontext.opt_phmod_typing.value then
80 layout_builder = new PHClassLayoutBuilder(mainmodule, new PHModOperator)
81 else if modelbuilder.toolcontext.opt_phand_typing.value then
82 layout_builder = new PHClassLayoutBuilder(mainmodule, new PHAndOperator)
83 else if modelbuilder.toolcontext.opt_bm_typing.value then
84 layout_builder = new BMClassLayoutBuilder(mainmodule)
85 else
86 layout_builder = new CLClassLayoutBuilder(mainmodule)
87 end
88 self.class_layout = layout_builder.build_layout(mclasses)
89 self.class_tables = self.build_class_typing_tables(mclasses)
90
91 # vt coloration
92 var vt_coloring = new CLPropertyLayoutBuilder[MVirtualTypeProp](new MVirtualTypePropColorer(mainmodule))
93 var vt_layout = vt_coloring.build_layout(mclasses)
94 self.vt_tables = build_vt_tables(mclasses, vt_layout)
95 self.compile_color_consts(vt_layout.pos)
96 self.vt_layout = vt_layout
97 end
98
99 fun build_vt_tables(mclasses: Set[MClass], layout: PropertyLayout[MProperty]): Map[MClass, Array[nullable MPropDef]] do
100 var tables = new HashMap[MClass, Array[nullable MPropDef]]
101 for mclass in mclasses do
102 var table = new Array[nullable MPropDef]
103 # first, fill table from parents by reverse linearization order
104 var parents = self.mainmodule.super_mclasses(mclass)
105 var lin = self.mainmodule.reverse_linearize_mclasses(parents)
106 for parent in lin do
107 for mproperty in self.mainmodule.properties(parent) do
108 if not mproperty isa MVirtualTypeProp then continue
109 var color = layout.pos[mproperty]
110 if table.length <= color then
111 for i in [table.length .. color[ do
112 table[i] = null
113 end
114 end
115 for mpropdef in mproperty.mpropdefs do
116 if mpropdef.mclassdef.mclass == parent then
117 table[color] = mpropdef
118 end
119 end
120 end
121 end
122
123 # then override with local properties
124 for mproperty in self.mainmodule.properties(mclass) do
125 if not mproperty isa MVirtualTypeProp then continue
126 var color = layout.pos[mproperty]
127 if table.length <= color then
128 for i in [table.length .. color[ do
129 table[i] = null
130 end
131 end
132 for mpropdef in mproperty.mpropdefs do
133 if mpropdef.mclassdef.mclass == mclass then
134 table[color] = mpropdef
135 end
136 end
137 end
138 tables[mclass] = table
139 end
140 return tables
141 end
142
143 # Build class tables
144 fun build_class_typing_tables(mclasses: Set[MClass]): Map[MClass, Array[nullable MClass]] do
145 var tables = new HashMap[MClass, Array[nullable MClass]]
146 var layout = self.class_layout
147 for mclass in mclasses do
148 var table = new Array[nullable MClass]
149 var supers = new HashSet[MClass]
150 supers.add_all(self.mainmodule.super_mclasses(mclass))
151 supers.add(mclass)
152 for sup in supers do
153 var color: Int
154 if layout isa PHTypingLayout[MClass] then
155 color = layout.hashes[mclass][sup]
156 else
157 color = layout.pos[sup]
158 end
159 if table.length <= color then
160 for i in [table.length .. color[ do
161 table[i] = null
162 end
163 end
164 table[color] = sup
165 end
166 tables[mclass] = table
167 end
168 return tables
169 end
170
171 redef fun compile_header_structs do
172 self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
173 self.compile_header_attribute_structs
174 self.header.add_decl("struct class \{ int id; const char *name; 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. */")
175 self.header.add_decl("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
176 self.header.add_decl("struct vts_entry \{ short int is_nullable; struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
177
178 if self.vt_layout isa PHPropertyLayoutBuilder[MVirtualTypeProp] then
179 self.header.add_decl("struct vts_table \{ int mask; struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
180 else
181 self.header.add_decl("struct vts_table \{ struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
182 end
183
184 if modelbuilder.toolcontext.opt_phmod_typing.value then
185 self.header.add_decl("#define HASH(mask, id) ((mask)%(id))")
186 else if modelbuilder.toolcontext.opt_phand_typing.value then
187 self.header.add_decl("#define HASH(mask, id) ((mask)&(id))")
188 end
189
190 self.header.add_decl("typedef struct val \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
191 end
192
193 redef fun compile_class_to_c(mclass: MClass)
194 do
195 var mtype = mclass.intro.bound_mtype
196 var c_name = mclass.c_name
197
198 var vft = self.method_tables[mclass]
199 var attrs = self.attr_tables[mclass]
200 var class_table = self.class_tables[mclass]
201 var v = self.new_visitor
202
203 v.add_decl("/* runtime class {c_name} */")
204
205 self.header.add_decl("extern const struct class_{c_name} class_{c_name};")
206 self.header.add_decl("struct class_{c_name} \{")
207 self.header.add_decl("int id;")
208 self.header.add_decl("const char *name;")
209 self.header.add_decl("int box_kind;")
210 self.header.add_decl("int color;")
211 self.header.add_decl("const struct vts_table *vts_table;")
212 self.header.add_decl("struct type_table *type_table;")
213 self.header.add_decl("nitmethod_t vft[{vft.length}];")
214 self.header.add_decl("\};")
215
216 # Build class vft
217 v.add_decl("const struct class_{c_name} class_{c_name} = \{")
218 v.add_decl("{self.class_layout.ids[mclass]},")
219 v.add_decl("\"{mclass.name}\", /* class_name_string */")
220 v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
221 var layout = self.class_layout
222 if layout isa PHTypingLayout[MClass] then
223 v.add_decl("{layout.masks[mclass]},")
224 else
225 v.add_decl("{layout.pos[mclass]},")
226 end
227 if build_class_vts_table(mclass) then
228 v.add_decl("(const struct vts_table*) &vts_table_{c_name},")
229 else
230 v.add_decl("NULL,")
231 end
232 v.add_decl("(struct type_table*) &type_table_{c_name},")
233 v.add_decl("\{")
234 for i in [0 .. vft.length[ do
235 var mpropdef = vft[i]
236 if mpropdef == null then
237 v.add_decl("NULL, /* empty */")
238 else
239 if true or mpropdef.mclassdef.bound_mtype.ctype != "val*" then
240 v.add_decl("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
241 else
242 v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
243 end
244 end
245 end
246 v.add_decl("\}")
247 v.add_decl("\};")
248
249 # Build class type table
250 self.header.add_decl("extern const struct type_table_{c_name} type_table_{c_name};")
251 self.header.add_decl("struct type_table_{c_name} \{")
252 self.header.add_decl("int size;")
253 self.header.add_decl("int table[{class_table.length}];")
254 self.header.add_decl("\};")
255
256 v.add_decl("const struct type_table_{c_name} type_table_{c_name} = \{")
257 v.add_decl("{class_table.length},")
258 v.add_decl("\{")
259 for msuper in class_table do
260 if msuper == null then
261 v.add_decl("-1, /* empty */")
262 else
263 v.add_decl("{self.class_layout.ids[msuper]}, /* {msuper} */")
264 end
265 end
266 v.add_decl("\}")
267 v.add_decl("\};")
268
269 #Build instance struct
270 if mtype.ctype != "val*" then
271 self.header.add_decl("struct instance_{c_name} \{")
272 self.header.add_decl("const struct class *class;")
273 self.header.add_decl("{mtype.ctype} value;")
274 self.header.add_decl("\};")
275
276 self.header.add_decl("val* BOX_{c_name}({mtype.ctype});")
277 v.add_decl("/* allocate {mtype} */")
278 v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
279 v.add("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
280 v.add("res->class = (struct class*) &class_{c_name};")
281 v.add("res->value = value;")
282 v.add("return (val*)res;")
283 v.add("\}")
284 return
285 end
286
287 var is_native_array = mclass.name == "NativeArray"
288
289 var sig
290 if is_native_array then
291 sig = "int length"
292 else
293 sig = ""
294 end
295
296 #Build instance struct
297 #extern const struct instance_array__NativeArray instance_array__NativeArray;
298 self.header.add_decl("struct instance_{c_name} \{")
299 self.header.add_decl("const struct class *class;")
300 self.header.add_decl("nitattribute_t attrs[{attrs.length}];")
301 if is_native_array then
302 # NativeArrays are just a instance header followed by an array of values
303 self.header.add_decl("val* values[0];")
304 end
305 self.header.add_decl("\};")
306
307
308 self.header.add_decl("{mtype.ctype} NEW_{c_name}({sig});")
309 v.add_decl("/* allocate {mtype} */")
310 v.add_decl("{mtype.ctype} NEW_{c_name}({sig}) \{")
311 var res = v.new_named_var(mtype, "self")
312 res.is_exact = true
313 if is_native_array then
314 var mtype_elt = mtype.arguments.first
315 v.add("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
316 else
317 v.add("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
318 end
319 v.add("{res}->class = (struct class*) &class_{c_name};")
320
321 self.generate_init_attr(v, res, mtype)
322 v.add("return {res};")
323 v.add("\}")
324
325 generate_check_init_instance(mtype)
326 end
327
328 private fun build_class_vts_table(mclass: MClass): Bool do
329 if self.vt_tables[mclass].is_empty then return false
330
331 self.header.add_decl("extern const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name};")
332 self.header.add_decl("struct vts_table_{mclass.c_name} \{")
333 if self.vt_layout isa PHPropertyLayoutBuilder[MVirtualTypeProp] then
334 self.header.add_decl("int mask;")
335 end
336 self.header.add_decl("struct vts_entry vts[{self.vt_tables[mclass].length}];")
337 self.header.add_decl("\};")
338
339 var v = new_visitor
340 v.add_decl("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
341 if self.vt_layout isa PHPropertyLayoutBuilder[MVirtualTypeProp] then
342 #TODO redo this when PHPropertyLayoutBuilder will be implemented
343 #v.add_decl("{vt_masks[mclass]},")
344 end
345 v.add_decl("\{")
346
347 for vt in self.vt_tables[mclass] do
348 if vt == null then
349 v.add_decl("\{-1, NULL\}, /* empty */")
350 else
351 var is_null = 0
352 var bound = retrieve_vt_bound(mclass.intro.bound_mtype, vt.as(MVirtualTypeDef).bound)
353 while bound isa MNullableType do
354 bound = retrieve_vt_bound(mclass.intro.bound_mtype, bound.mtype)
355 is_null = 1
356 end
357 v.add_decl("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
358 end
359 end
360 v.add_decl("\},")
361 v.add_decl("\};")
362 return true
363 end
364
365 private fun retrieve_vt_bound(anchor: MClassType, mtype: nullable MType): MType do
366 if mtype == null then
367 print "NOT YET IMPLEMENTED: retrieve_vt_bound on null"
368 abort
369 end
370 if mtype isa MVirtualType then
371 return mtype.anchor_to(mainmodule, anchor)
372 else if mtype isa MParameterType then
373 return mtype.anchor_to(mainmodule, anchor)
374 else
375 return mtype
376 end
377 end
378
379 redef fun new_visitor do return new SeparateErasureCompilerVisitor(self)
380
381 # Stats
382
383 private var class_tables: Map[MClass, Array[nullable MClass]]
384 private var vt_tables: Map[MClass, Array[nullable MPropDef]]
385
386 redef fun display_sizes
387 do
388 print "# size of subtyping tables"
389 print "\ttotal \tholes"
390 var total = 0
391 var holes = 0
392 for t, table in class_tables do
393 total += table.length
394 for e in table do if e == null then holes += 1
395 end
396 print "\t{total}\t{holes}"
397
398 print "# size of resolution tables"
399 print "\ttotal \tholes"
400 total = 0
401 holes = 0
402 for t, table in vt_tables do
403 total += table.length
404 for e in table do if e == null then holes += 1
405 end
406 print "\t{total}\t{holes}"
407
408 print "# size of methods tables"
409 print "\ttotal \tholes"
410 total = 0
411 holes = 0
412 for t, table in method_tables do
413 total += table.length
414 for e in table do if e == null then holes += 1
415 end
416 print "\t{total}\t{holes}"
417
418 print "# size of attributes tables"
419 print "\ttotal \tholes"
420 total = 0
421 holes = 0
422 for t, table in attr_tables do
423 total += table.length
424 for e in table do if e == null then holes += 1
425 end
426 print "\t{total}\t{holes}"
427 end
428 end
429
430 class SeparateErasureCompilerVisitor
431 super SeparateCompilerVisitor
432
433 redef fun compile_callsite(callsite, arguments)
434 do
435 var res = super
436 if callsite.erasure_cast and not self.compiler.as(SeparateErasureCompiler).modelbuilder.toolcontext.opt_no_check_erasure_cast.value then
437 assert res != null
438 var mtype = callsite.msignature.return_mtype
439 assert mtype != null
440 self.add("/* Erasure cast for return {res} isa {mtype} */")
441 var cond = self.type_test(res, mtype, "erasure")
442 self.add("if (!{cond}) \{")
443 #var x = self.class_name_string(res)
444 #var y = self.class_name_string(arguments.first)
445 #self.add("fprintf(stderr, \"Erasure cast: expected {mtype} (self is %s), got %s for {res}\\n\", {y}, {x});")
446 self.add_abort("Cast failed")
447 self.add("\}")
448 end
449 return res
450 end
451
452 redef fun init_instance(mtype) do return self.new_expr("NEW_{mtype.mclass.c_name}()", mtype)
453
454 redef fun type_test(value, mtype, tag)
455 do
456 self.add("/* type test for {value.inspect} isa {mtype} */")
457
458 var res = self.new_var(bool_type)
459
460 var cltype = self.get_name("cltype")
461 self.add_decl("int {cltype};")
462 var idtype = self.get_name("idtype")
463 self.add_decl("int {idtype};")
464
465 var maybe_null = self.maybe_null(value)
466 var accept_null = "0"
467 if mtype isa MNullableType then
468 mtype = mtype.mtype
469 accept_null = "1"
470 end
471 if mtype isa MParameterType then
472 # Here we get the bound of the the formal type (eh, erasure...)
473 mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, false)
474 if mtype isa MNullableType then
475 mtype = mtype.mtype
476 accept_null = "1"
477 end
478 end
479
480 if value.mcasttype.is_subtype(self.frame.mpropdef.mclassdef.mmodule, self.frame.mpropdef.mclassdef.bound_mtype, mtype) then
481 self.add("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
482 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
483 self.compiler.count_type_test_skipped[tag] += 1
484 self.add("count_type_test_skipped_{tag}++;")
485 end
486 return res
487 end
488
489 var class_ptr
490 var type_table
491 if value.mtype.ctype == "val*" then
492 class_ptr = "{value}->class->"
493 else
494 var mclass = value.mtype.as(MClassType).mclass
495 class_ptr = "class_{mclass.c_name}."
496 end
497
498 if mtype isa MClassType then
499 self.add("{cltype} = class_{mtype.mclass.c_name}.color;")
500 self.add("{idtype} = class_{mtype.mclass.c_name}.id;")
501 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
502 self.compiler.count_type_test_resolved[tag] += 1
503 self.add("count_type_test_resolved_{tag}++;")
504 end
505 else if mtype isa MVirtualType then
506 var recv = self.frame.arguments.first
507 var recv_ptr
508 if recv.mtype.ctype == "val*" then
509 recv_ptr = "{recv}->class->"
510 else
511 var mclass = recv.mtype.as(MClassType).mclass
512 recv_ptr = "class_{mclass.c_name}."
513 end
514 var entry = self.get_name("entry")
515 self.add("struct vts_entry {entry};")
516 if self.compiler.as(SeparateErasureCompiler).vt_layout isa PHPropertyLayoutBuilder[MVirtualTypeProp] then
517 self.add("{entry} = {recv_ptr}vts_table->vts[HASH({recv_ptr}vts_table->mask, {mtype.mproperty.const_color})];")
518 else
519 self.add("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
520 end
521 self.add("{cltype} = {entry}.class->color;")
522 self.add("{idtype} = {entry}.class->id;")
523 if maybe_null and accept_null == "0" then
524 var is_nullable = self.get_name("is_nullable")
525 self.add_decl("short int {is_nullable};")
526 self.add("{is_nullable} = {entry}.is_nullable;")
527 accept_null = is_nullable.to_s
528 end
529 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
530 self.compiler.count_type_test_unresolved[tag] += 1
531 self.add("count_type_test_unresolved_{tag}++;")
532 end
533 else
534 self.debug("type_test({value.inspect}, {mtype})")
535 abort
536 end
537
538 # check color is in table
539 if maybe_null then
540 self.add("if({value} == NULL) \{")
541 self.add("{res} = {accept_null};")
542 self.add("\} else \{")
543 end
544 if self.compiler.as(SeparateErasureCompiler).class_layout isa PHTypingLayout[MClass] then
545 self.add("{cltype} = HASH({class_ptr}color, {idtype});")
546 end
547 self.add("if({cltype} >= {class_ptr}type_table->size) \{")
548 self.add("{res} = 0;")
549 self.add("\} else \{")
550 self.add("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
551 self.add("\}")
552 if maybe_null then
553 self.add("\}")
554 end
555
556 return res
557 end
558
559 redef fun class_name_string(value)
560 do
561 var res = self.get_name("var_class_name")
562 self.add_decl("const char* {res};")
563 if value.mtype.ctype == "val*" then
564 self.add "{res} = {value} == NULL ? \"null\" : {value}->class->name;"
565 else
566 self.add "{res} = class_{value.mtype.c_name}.name;"
567 end
568 return res
569 end
570
571 redef fun array_instance(array, elttype)
572 do
573 var nclass = self.get_class("NativeArray")
574 elttype = self.anchor(elttype)
575 var arraytype = self.get_class("Array").get_mtype([elttype])
576 var res = self.init_instance(arraytype)
577 self.add("\{ /* {res} = array_instance Array[{elttype}] */")
578 var nat = self.new_var(self.get_class("NativeArray").get_mtype([elttype]))
579 nat.is_exact = true
580 self.add("{nat} = NEW_{nclass.c_name}({array.length});")
581 for i in [0..array.length[ do
582 var r = self.autobox(array[i], self.object_type)
583 self.add("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
584 end
585 var length = self.int_instance(array.length)
586 self.send(self.get_property("with_native", arraytype), [res, nat, length])
587 self.check_init_instance(res, arraytype)
588 self.add("\}")
589 return res
590 end
591
592 redef fun calloc_array(ret_type, arguments)
593 do
594 var ret = ret_type.as(MClassType)
595 self.ret(self.new_expr("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type))
596 end
597 end