1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Separate compilation of a Nit program with generic type erasure
16 module separate_erasure_compiler
19 import separate_compiler
21 redef class ToolContext
23 var opt_erasure
: OptionBool = new OptionBool("Erase generic types", "--erasure")
25 # --no-check-erasure-cast
26 var opt_no_check_erasure_cast
: OptionBool = new OptionBool("Disable implicit casts on unsafe return with erasure-typing policy (dangerous)", "--no-check-erasure-cast")
31 self.option_context
.add_option
(self.opt_erasure
, self.opt_no_check_erasure_cast
)
35 redef class ModelBuilder
36 fun run_separate_erasure_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
39 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
41 var compiler
= new SeparateErasureCompiler(mainmodule
, runtime_type_analysis
, self)
42 compiler
.compile_header
44 # compile class structures
45 for m
in mainmodule
.in_importation
.greaters
do
46 for mclass
in m
.intro_mclasses
do
47 compiler
.compile_class_to_c
(mclass
)
51 # The main function of the C
52 compiler
.compile_main_function
55 for m
in mainmodule
.in_importation
.greaters
do
56 compiler
.compile_module_to_c
(m
)
59 compiler
.display_stats
61 write_and_make
(compiler
)
65 class SeparateErasureCompiler
66 super SeparateCompiler
68 private var class_ids
: HashMap[MClass, Int] = new HashMap[MClass, Int]
69 private var class_colors
: Map[MClass, Int]
70 private var class_tables
: Map[MClass, Array[nullable MClass]]
72 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
73 var mclasses
= new HashSet[MClass]
74 mclasses
.add_all
(mmbuilder
.model
.mclasses
)
77 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
then
79 for mclass
in mclasses
do
80 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
83 var class_coloring
= new ClassModPerfectHashing(mainmodule
)
84 self.class_colors
= class_coloring
.compute_masks
(mclasses
, class_ids
)
85 self.class_tables
= class_coloring
.hash_type_tables
(mclasses
, class_ids
, class_colors
)
87 self.header
.add_decl
("#define HASH(mask, id) ((mask)%(id))")
88 else if modelbuilder
.toolcontext
.opt_phand_typing
.value
then
90 for mclass
in mclasses
do
91 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
94 var class_coloring
= new ClassAndPerfectHashing(mainmodule
)
95 self.class_colors
= class_coloring
.compute_masks
(mclasses
, class_ids
)
96 self.class_tables
= class_coloring
.hash_type_tables
(mclasses
, class_ids
, class_colors
)
98 self.header
.add_decl
("#define HASH(mask, id) ((mask)&(id))")
101 if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
102 class_coloring
= new NaiveClassColoring(mainmodule
)
104 class_coloring
= new ClassColoring(mainmodule
)
107 for mclass
in mclasses
do
108 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
110 self.class_colors
= class_coloring
.colorize
(modelbuilder
.model
.mclasses
)
111 self.class_tables
= class_coloring
.build_type_tables
(modelbuilder
.model
.mclasses
, class_colors
)
115 redef fun compile_header_structs
do
116 self.header
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
117 self.compile_header_attribute_structs
118 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. */")
119 self.header
.add_decl
("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
120 self.header
.add_decl
("struct vts_entry \{ short int is_nullable; struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
122 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
123 self.header
.add_decl
("struct vts_table \{ int mask; struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
125 self.header
.add_decl
("struct vts_table \{ struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
128 self.header
.add_decl
("typedef struct val \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
131 redef fun compile_class_to_c
(mclass
: MClass)
133 var mtype
= mclass
.intro
.bound_mtype
134 var c_name
= mclass
.c_name
136 var vft
= self.method_tables
[mclass
]
137 var attrs
= self.attr_tables
[mclass
]
138 var class_table
= self.class_tables
[mclass
]
139 var v
= self.new_visitor
141 v
.add_decl
("/* runtime class {c_name} */")
142 var idnum
= classids
.length
143 var idname
= "ID_" + c_name
144 self.classids
[mtype
] = idname
145 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
147 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
148 self.header
.add_decl
("struct class_{c_name} \{")
149 self.header
.add_decl
("int id;")
150 self.header
.add_decl
("const char *name;")
151 self.header
.add_decl
("int box_kind;")
152 self.header
.add_decl
("int color;")
153 self.header
.add_decl
("const struct vts_table *vts_table;")
154 self.header
.add_decl
("struct type_table *type_table;")
155 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
156 self.header
.add_decl
("\};")
159 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
160 v
.add_decl
("{self.class_ids[mclass]},")
161 v
.add_decl
("\"{mclass.name}\
", /* class_name_string */")
162 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
163 v
.add_decl
("{self.class_colors[mclass]},")
164 if build_class_vts_table
(mclass
) then
165 v
.add_decl
("(const struct vts_table*) &vts_table_{c_name},")
169 v
.add_decl
("(struct type_table*) &type_table_{c_name},")
171 for i
in [0 .. vft
.length
[ do
172 var mpropdef
= vft
[i
]
173 if mpropdef
== null then
174 v
.add_decl
("NULL, /* empty */")
176 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
177 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
179 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
186 # Build class type table
187 self.header
.add_decl
("extern const struct type_table_{c_name} type_table_{c_name};")
188 self.header
.add_decl
("struct type_table_{c_name} \{")
189 self.header
.add_decl
("int size;")
190 self.header
.add_decl
("int table[{class_table.length}];")
191 self.header
.add_decl
("\};")
193 v
.add_decl
("const struct type_table_{c_name} type_table_{c_name} = \{")
194 v
.add_decl
("{class_table.length},")
196 for msuper
in class_table
do
197 if msuper
== null then
198 v
.add_decl
("-1, /* empty */")
200 v
.add_decl
("{self.class_ids[msuper]}, /* {msuper} */")
206 #Build instance struct
207 if mtype
.ctype
!= "val*" then
208 self.header
.add_decl
("struct instance_{c_name} \{")
209 self.header
.add_decl
("const struct class *class;")
210 self.header
.add_decl
("{mtype.ctype} value;")
211 self.header
.add_decl
("\};")
213 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
214 v
.add_decl
("/* allocate {mtype} */")
215 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
216 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
217 v
.add
("res->class = (struct class*) &class_{c_name};")
218 v
.add
("res->value = value;")
219 v
.add
("return (val*)res;")
224 var is_native_array
= mclass
.name
== "NativeArray"
227 if is_native_array
then
233 #Build instance struct
234 #extern const struct instance_array__NativeArray instance_array__NativeArray;
235 self.header
.add_decl
("struct instance_{c_name} \{")
236 self.header
.add_decl
("const struct class *class;")
237 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
238 if is_native_array
then
239 # NativeArrays are just a instance header followed by an array of values
240 self.header
.add_decl
("val* values[0];")
242 self.header
.add_decl
("\};")
245 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
246 v
.add_decl
("/* allocate {mtype} */")
247 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
248 var res
= v
.new_named_var
(mtype
, "self")
250 if is_native_array
then
251 var mtype_elt
= mtype
.arguments
.first
252 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
254 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
256 v
.add
("{res}->class = (struct class*) &class_{c_name};")
258 self.generate_init_attr
(v
, res
, mtype
)
259 v
.add
("return {res};")
262 generate_check_init_instance
(mtype
)
265 private fun build_class_vts_table
(mclass
: MClass): Bool do
266 if self.vt_tables
[mclass
].is_empty
then return false
268 self.header
.add_decl
("extern const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name};")
269 self.header
.add_decl
("struct vts_table_{mclass.c_name} \{")
270 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
271 self.header
.add_decl
("int mask;")
273 self.header
.add_decl
("struct vts_entry vts[{self.vt_tables[mclass].length}];")
274 self.header
.add_decl
("\};")
277 v
.add_decl
("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
278 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
279 v
.add_decl
("{vt_masks[mclass]},")
283 for vt
in self.vt_tables
[mclass
] do
285 v
.add_decl
("\{-1, NULL\}, /* empty */")
288 var bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, vt
.bound
)
289 while bound
isa MNullableType do
290 bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, bound
.mtype
)
293 v
.add_decl
("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
301 private fun retrieve_vt_bound
(anchor
: MClassType, mtype
: nullable MType): MType do
302 if mtype
== null then
303 print
"NOT YET IMPLEMENTED: retrieve_vt_bound on null"
306 if mtype
isa MVirtualType then
307 return mtype
.anchor_to
(mainmodule
, anchor
)
308 else if mtype
isa MParameterType then
309 return mtype
.anchor_to
(mainmodule
, anchor
)
315 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
318 class SeparateErasureCompilerVisitor
319 super SeparateCompilerVisitor
321 redef fun compile_callsite
(callsite
, arguments
)
324 if callsite
.erasure_cast
and not self.compiler
.as(SeparateErasureCompiler).modelbuilder
.toolcontext
.opt_no_check_erasure_cast
.value
then
326 var mtype
= callsite
.msignature
.return_mtype
328 self.add
("/* Erasure cast for return {res} isa {mtype} */")
329 var cond
= self.type_test
(res
, mtype
, "erasure")
330 self.add
("if (!{cond}) \{")
331 #var x = self.class_name_string(res)
332 #var y = self.class_name_string(arguments.first)
333 #self.add("fprintf(stderr, \"Erasure cast: expected {mtype} (self is %s), got %s for {res}\\n\", {y}, {x});")
334 self.add_abort
("Cast failed")
340 redef fun init_instance
(mtype
)
342 return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
345 redef fun type_test
(value
, mtype
, tag
)
347 self.add
("/* type test for {value.inspect} isa {mtype} */")
349 var res
= self.new_var
(bool_type
)
351 var cltype
= self.get_name
("cltype")
352 self.add_decl
("int {cltype};")
353 var idtype
= self.get_name
("idtype")
354 self.add_decl
("int {idtype};")
355 var is_nullable
= self.get_name
("is_nullable")
356 self.add_decl
("short int {is_nullable};")
357 var is_null
= self.get_name
("is_null")
358 self.add_decl
("short int {is_null};")
361 if mtype
isa MNullableType then
364 self.add
("{is_nullable} = 1;")
366 if mtype
isa MParameterType then
367 # Here we get the bound of the the formal type (eh, erasure...)
368 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
369 if mtype
isa MNullableType then
372 self.add
("{is_nullable} = 1;")
375 if mtype
isa MVirtualType then
376 # FIXME virtual types should not be erased but got from the class table of the current receiver (self.frame.arguments.first)
377 #mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, true)
378 #if mtype isa MNullableType then
379 # mtype = mtype.mtype
384 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
385 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
386 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
387 self.compiler
.count_type_test_skipped
[tag
] += 1
388 self.add
("count_type_test_skipped_{tag}++;")
395 if value
.mtype
.ctype
== "val*" then
396 class_ptr
= "{value}->class->"
397 self.add
("{is_null} = {value} == NULL;")
399 var mclass
= value
.mtype
.as(MClassType).mclass
400 class_ptr
= "class_{mclass.c_name}."
401 self.add
("{is_null} = 0;")
404 if mtype
isa MClassType then
405 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
406 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
407 if maybe_null
== 0 then
408 self.add
("{is_nullable} = 0;")
410 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
411 self.compiler
.count_type_test_resolved
[tag
] += 1
412 self.add
("count_type_test_resolved_{tag}++;")
414 else if mtype
isa MVirtualType then
415 var recv
= self.frame
.arguments
.first
417 if recv
.mtype
.ctype
== "val*" then
418 recv_ptr
= "{recv}->class->"
420 var mclass
= recv
.mtype
.as(MClassType).mclass
421 recv_ptr
= "class_{mclass.c_name}."
423 var entry
= self.get_name
("entry")
424 self.add
("struct vts_entry {entry};")
425 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
426 self.add
("{entry} = {recv_ptr}vts_table->vts[HASH({recv_ptr}vts_table->mask, {mtype.mproperty.const_color})];")
428 self.add
("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
430 self.add
("{cltype} = {entry}.class->color;")
431 self.add
("{idtype} = {entry}.class->id;")
432 if maybe_null
== 0 then
433 self.add
("{is_nullable} = {entry}.is_nullable;")
435 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
436 self.compiler
.count_type_test_unresolved
[tag
] += 1
437 self.add
("count_type_test_unresolved_{tag}++;")
440 self.debug
("type_test({value.inspect}, {mtype})")
444 # check color is in table
445 self.add
("if({is_null}) \{")
446 self.add
("{res} = {is_nullable};")
447 self.add
("\} else \{")
448 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
449 self.add
("{cltype} = HASH({class_ptr}color, {idtype});")
451 self.add
("if({cltype} >= {class_ptr}type_table->size) \{")
452 self.add
("{res} = 0;")
453 self.add
("\} else \{")
454 self.add
("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
461 redef fun class_name_string
(value
)
463 var res
= self.get_name
("var_class_name")
464 self.add_decl
("const char* {res};")
465 if value
.mtype
.ctype
== "val*" then
466 self.add
"{res} = {value} == NULL ? \"null\
" : {value}->class->name;"
468 self.add
"{res} = class_{value.mtype.c_name}.name;"
473 redef fun array_instance
(array
, elttype
)
475 var nclass
= self.get_class
("NativeArray")
476 elttype
= self.anchor
(elttype
)
477 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
478 var res
= self.init_instance
(arraytype
)
479 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
480 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
482 self.add
("{nat} = NEW_{nclass.c_name}({array.length});")
483 for i
in [0..array
.length
[ do
484 var r
= self.autobox
(array
[i
], self.object_type
)
485 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
487 var length
= self.int_instance
(array
.length
)
488 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
489 self.check_init_instance
(res
, arraytype
)
494 redef fun calloc_array
(ret_type
, arguments
)
496 var ret
= ret_type
.as(MClassType)
497 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type
))