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
18 intrude import separate_compiler
20 # Add separate erased compiler specific options
21 redef class ToolContext
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")
30 self.option_context
.add_option
(self.opt_erasure
, self.opt_no_check_erasure_cast
)
34 redef class ModelBuilder
35 fun run_separate_erasure_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
38 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
40 var compiler
= new SeparateErasureCompiler(mainmodule
, self, runtime_type_analysis
)
41 compiler
.compile_header
43 # 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
53 compiler
.compile_main_function
56 for m
in mainmodule
.in_importation
.greaters
do
58 compiler
.compile_module_to_c
(m
)
61 compiler
.display_stats
63 write_and_make
(compiler
)
67 class SeparateErasureCompiler
68 super SeparateCompiler
70 private var class_layout
: nullable TypingLayout[MClass]
71 private var class_tables
: Map[MClass, Array[nullable MClass]]
73 init(mainmodule
: MModule, mmbuilder
: ModelBuilder, runtime_type_analysis
: RapidTypeAnalysis) do
76 var mclasses
= new HashSet[MClass].from
(mmbuilder
.model
.mclasses
)
78 var layout_builder
: ClassLayoutBuilder
79 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
then
80 layout_builder
= new PHClassLayoutBuilder(mainmodule
, new PHModOperator)
81 self.header
.add_decl
("#define HASH(mask, id) ((mask)%(id))")
82 else if modelbuilder
.toolcontext
.opt_phand_typing
.value
then
83 layout_builder
= new PHClassLayoutBuilder(mainmodule
, new PHAndOperator)
84 self.header
.add_decl
("#define HASH(mask, id) ((mask)&(id))")
85 else if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
86 layout_builder
= new BMClassLayoutBuilder(mainmodule
)
88 layout_builder
= new CLClassLayoutBuilder(mainmodule
)
90 self.class_layout
= layout_builder
.build_layout
(mclasses
)
91 self.class_tables
= self.build_class_typing_tables
(mclasses
)
95 fun build_class_typing_tables
(mclasses
: Set[MClass]): Map[MClass, Array[nullable MClass]] do
96 var tables
= new HashMap[MClass, Array[nullable MClass]]
97 var layout
= self.class_layout
98 for mclass
in mclasses
do
99 var table
= new Array[nullable MClass]
100 var supers
= new HashSet[MClass]
101 supers
.add_all
(self.mainmodule
.super_mclasses
(mclass
))
105 if layout
isa PHTypingLayout[MClass] then
106 color
= layout
.hashes
[mclass
][sup
]
108 color
= layout
.pos
[sup
]
110 if table
.length
<= color
then
111 for i
in [table
.length
.. color
[ do
117 tables
[mclass
] = table
122 redef fun compile_header_structs
do
123 self.header
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
124 self.compile_header_attribute_structs
125 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. */")
126 self.header
.add_decl
("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
127 self.header
.add_decl
("struct vts_entry \{ short int is_nullable; struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
129 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
130 self.header
.add_decl
("struct vts_table \{ int mask; struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
132 self.header
.add_decl
("struct vts_table \{ struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
135 self.header
.add_decl
("typedef struct val \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
138 redef fun compile_class_to_c
(mclass
: MClass)
140 var mtype
= mclass
.intro
.bound_mtype
141 var c_name
= mclass
.c_name
143 var vft
= self.method_tables
[mclass
]
144 var attrs
= self.attr_tables
[mclass
]
145 var class_table
= self.class_tables
[mclass
]
146 var v
= self.new_visitor
148 v
.add_decl
("/* runtime class {c_name} */")
149 var idnum
= classids
.length
150 var idname
= "ID_" + c_name
151 self.classids
[mtype
] = idname
152 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
154 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
155 self.header
.add_decl
("struct class_{c_name} \{")
156 self.header
.add_decl
("int id;")
157 self.header
.add_decl
("const char *name;")
158 self.header
.add_decl
("int box_kind;")
159 self.header
.add_decl
("int color;")
160 self.header
.add_decl
("const struct vts_table *vts_table;")
161 self.header
.add_decl
("struct type_table *type_table;")
162 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
163 self.header
.add_decl
("\};")
166 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
167 v
.add_decl
("{self.class_layout.ids[mclass]},")
168 v
.add_decl
("\"{mclass.name}\
", /* class_name_string */")
169 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
170 var layout
= self.class_layout
171 if layout
isa PHTypingLayout[MClass] then
172 v
.add_decl
("{layout.masks[mclass]},")
174 v
.add_decl
("{layout.pos[mclass]},")
176 if build_class_vts_table
(mclass
) then
177 v
.add_decl
("(const struct vts_table*) &vts_table_{c_name},")
181 v
.add_decl
("(struct type_table*) &type_table_{c_name},")
183 for i
in [0 .. vft
.length
[ do
184 var mpropdef
= vft
[i
]
185 if mpropdef
== null then
186 v
.add_decl
("NULL, /* empty */")
188 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
189 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
191 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
198 # Build class type table
199 self.header
.add_decl
("extern const struct type_table_{c_name} type_table_{c_name};")
200 self.header
.add_decl
("struct type_table_{c_name} \{")
201 self.header
.add_decl
("int size;")
202 self.header
.add_decl
("int table[{class_table.length}];")
203 self.header
.add_decl
("\};")
205 v
.add_decl
("const struct type_table_{c_name} type_table_{c_name} = \{")
206 v
.add_decl
("{class_table.length},")
208 for msuper
in class_table
do
209 if msuper
== null then
210 v
.add_decl
("-1, /* empty */")
212 v
.add_decl
("{self.class_layout.ids[msuper]}, /* {msuper} */")
218 #Build instance struct
219 if mtype
.ctype
!= "val*" then
220 self.header
.add_decl
("struct instance_{c_name} \{")
221 self.header
.add_decl
("const struct class *class;")
222 self.header
.add_decl
("{mtype.ctype} value;")
223 self.header
.add_decl
("\};")
225 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
226 v
.add_decl
("/* allocate {mtype} */")
227 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
228 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
229 v
.add
("res->class = (struct class*) &class_{c_name};")
230 v
.add
("res->value = value;")
231 v
.add
("return (val*)res;")
236 var is_native_array
= mclass
.name
== "NativeArray"
239 if is_native_array
then
245 #Build instance struct
246 #extern const struct instance_array__NativeArray instance_array__NativeArray;
247 self.header
.add_decl
("struct instance_{c_name} \{")
248 self.header
.add_decl
("const struct class *class;")
249 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
250 if is_native_array
then
251 # NativeArrays are just a instance header followed by an array of values
252 self.header
.add_decl
("val* values[0];")
254 self.header
.add_decl
("\};")
257 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
258 v
.add_decl
("/* allocate {mtype} */")
259 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
260 var res
= v
.new_named_var
(mtype
, "self")
262 if is_native_array
then
263 var mtype_elt
= mtype
.arguments
.first
264 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
266 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
268 v
.add
("{res}->class = (struct class*) &class_{c_name};")
270 self.generate_init_attr
(v
, res
, mtype
)
271 v
.add
("return {res};")
274 generate_check_init_instance
(mtype
)
277 private fun build_class_vts_table
(mclass
: MClass): Bool do
278 if self.vt_tables
[mclass
].is_empty
then return false
280 self.header
.add_decl
("extern const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name};")
281 self.header
.add_decl
("struct vts_table_{mclass.c_name} \{")
282 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
283 self.header
.add_decl
("int mask;")
285 self.header
.add_decl
("struct vts_entry vts[{self.vt_tables[mclass].length}];")
286 self.header
.add_decl
("\};")
289 v
.add_decl
("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
290 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
291 v
.add_decl
("{vt_masks[mclass]},")
295 for vt
in self.vt_tables
[mclass
] do
297 v
.add_decl
("\{-1, NULL\}, /* empty */")
300 var bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, vt
.bound
)
301 while bound
isa MNullableType do
302 bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, bound
.mtype
)
305 v
.add_decl
("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
313 private fun retrieve_vt_bound
(anchor
: MClassType, mtype
: nullable MType): MType do
314 if mtype
== null then
315 print
"NOT YET IMPLEMENTED: retrieve_vt_bound on null"
318 if mtype
isa MVirtualType then
319 return mtype
.anchor_to
(mainmodule
, anchor
)
320 else if mtype
isa MParameterType then
321 return mtype
.anchor_to
(mainmodule
, anchor
)
327 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
331 redef fun display_sizes
333 print
"# size of tables"
334 print
"\trs size\trs hole\tst size\tst hole"
339 var rtables
= vt_tables
340 for unanch
, table
in rtables
do
341 rt_table
+= table
.length
342 for e
in table
do if e
== null then rt_holes
+= 1
345 var ttables
= class_tables
346 for t
, table
in ttables
do
347 st_table
+= table
.length
348 for e
in table
do if e
== null then st_holes
+= 1
350 print
"\t{rt_table}\t{rt_holes}\t{st_table}\t{st_holes}"
354 class SeparateErasureCompilerVisitor
355 super SeparateCompilerVisitor
357 redef fun compile_callsite
(callsite
, arguments
)
360 if callsite
.erasure_cast
and not self.compiler
.as(SeparateErasureCompiler).modelbuilder
.toolcontext
.opt_no_check_erasure_cast
.value
then
362 var mtype
= callsite
.msignature
.return_mtype
364 self.add
("/* Erasure cast for return {res} isa {mtype} */")
365 var cond
= self.type_test
(res
, mtype
, "erasure")
366 self.add
("if (!{cond}) \{")
367 #var x = self.class_name_string(res)
368 #var y = self.class_name_string(arguments.first)
369 #self.add("fprintf(stderr, \"Erasure cast: expected {mtype} (self is %s), got %s for {res}\\n\", {y}, {x});")
370 self.add_abort
("Cast failed")
376 redef fun init_instance
(mtype
) do return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
378 redef fun type_test
(value
, mtype
, tag
)
380 self.add
("/* type test for {value.inspect} isa {mtype} */")
382 var res
= self.new_var
(bool_type
)
384 var cltype
= self.get_name
("cltype")
385 self.add_decl
("int {cltype};")
386 var idtype
= self.get_name
("idtype")
387 self.add_decl
("int {idtype};")
389 var maybe_null
= self.maybe_null
(value
)
390 var accept_null
= "0"
391 if mtype
isa MNullableType then
395 if mtype
isa MParameterType then
396 # Here we get the bound of the the formal type (eh, erasure...)
397 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
398 if mtype
isa MNullableType then
404 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
405 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
406 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
407 self.compiler
.count_type_test_skipped
[tag
] += 1
408 self.add
("count_type_test_skipped_{tag}++;")
415 if value
.mtype
.ctype
== "val*" then
416 class_ptr
= "{value}->class->"
418 var mclass
= value
.mtype
.as(MClassType).mclass
419 class_ptr
= "class_{mclass.c_name}."
422 if mtype
isa MClassType then
423 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
424 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
425 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
426 self.compiler
.count_type_test_resolved
[tag
] += 1
427 self.add
("count_type_test_resolved_{tag}++;")
429 else if mtype
isa MVirtualType then
430 var recv
= self.frame
.arguments
.first
432 if recv
.mtype
.ctype
== "val*" then
433 recv_ptr
= "{recv}->class->"
435 var mclass
= recv
.mtype
.as(MClassType).mclass
436 recv_ptr
= "class_{mclass.c_name}."
438 var entry
= self.get_name
("entry")
439 self.add
("struct vts_entry {entry};")
440 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
441 self.add
("{entry} = {recv_ptr}vts_table->vts[HASH({recv_ptr}vts_table->mask, {mtype.mproperty.const_color})];")
443 self.add
("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
445 self.add
("{cltype} = {entry}.class->color;")
446 self.add
("{idtype} = {entry}.class->id;")
447 if maybe_null
and accept_null
== "0" then
448 var is_nullable
= self.get_name
("is_nullable")
449 self.add_decl
("short int {is_nullable};")
450 self.add
("{is_nullable} = {entry}.is_nullable;")
451 accept_null
= is_nullable
.to_s
453 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
454 self.compiler
.count_type_test_unresolved
[tag
] += 1
455 self.add
("count_type_test_unresolved_{tag}++;")
458 self.debug
("type_test({value.inspect}, {mtype})")
462 # check color is in table
464 self.add
("if({value} == NULL) \{")
465 self.add
("{res} = {accept_null};")
466 self.add
("\} else \{")
468 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
469 self.add
("{cltype} = HASH({class_ptr}color, {idtype});")
471 self.add
("if({cltype} >= {class_ptr}type_table->size) \{")
472 self.add
("{res} = 0;")
473 self.add
("\} else \{")
474 self.add
("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
483 redef fun class_name_string
(value
)
485 var res
= self.get_name
("var_class_name")
486 self.add_decl
("const char* {res};")
487 if value
.mtype
.ctype
== "val*" then
488 self.add
"{res} = {value} == NULL ? \"null\
" : {value}->class->name;"
490 self.add
"{res} = class_{value.mtype.c_name}.name;"
495 redef fun array_instance
(array
, elttype
)
497 var nclass
= self.get_class
("NativeArray")
498 elttype
= self.anchor
(elttype
)
499 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
500 var res
= self.init_instance
(arraytype
)
501 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
502 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
504 self.add
("{nat} = NEW_{nclass.c_name}({array.length});")
505 for i
in [0..array
.length
[ do
506 var r
= self.autobox
(array
[i
], self.object_type
)
507 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
509 var length
= self.int_instance
(array
.length
)
510 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
511 self.check_init_instance
(res
, arraytype
)
516 redef fun calloc_array
(ret_type
, arguments
)
518 var ret
= ret_type
.as(MClassType)
519 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type
))