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