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