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