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 protected var vt_layout
: nullable PropertyLayout[MVirtualTypeProp]
74 protected var vt_tables
: Map[MClass, Array[nullable MPropDef]]
76 init(mainmodule
: MModule, mmbuilder
: ModelBuilder, runtime_type_analysis
: RapidTypeAnalysis) do
79 var mclasses
= new HashSet[MClass].from
(mmbuilder
.model
.mclasses
)
81 var layout_builder
: TypingLayoutBuilder[MClass]
82 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
then
83 layout_builder
= new PHClassLayoutBuilder(mainmodule
, new PHModOperator)
84 self.header
.add_decl
("#define HASH(mask, id) ((mask)%(id))")
85 else if modelbuilder
.toolcontext
.opt_phand_typing
.value
then
86 layout_builder
= new PHClassLayoutBuilder(mainmodule
, new PHAndOperator)
87 self.header
.add_decl
("#define HASH(mask, id) ((mask)&(id))")
88 else if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
89 layout_builder
= new BMClassLayoutBuilder(mainmodule
)
91 layout_builder
= new CLClassLayoutBuilder(mainmodule
)
93 self.class_layout
= layout_builder
.build_layout
(mclasses
)
94 self.class_tables
= self.build_class_typing_tables
(mclasses
)
97 var vt_coloring
= new CLPropertyLayoutBuilder[MVirtualTypeProp](mainmodule
)
98 var vt_layout
= vt_coloring
.build_layout
(mclasses
)
99 self.vt_tables
= build_vt_tables
(mclasses
, vt_layout
)
100 self.compile_color_consts
(vt_layout
.pos
)
101 self.vt_layout
= vt_layout
104 fun build_vt_tables
(mclasses
: Set[MClass], layout
: PropertyLayout[MProperty]): Map[MClass, Array[nullable MPropDef]] do
105 var tables
= new HashMap[MClass, Array[nullable MPropDef]]
106 for mclass
in mclasses
do
107 var table
= new Array[nullable MPropDef]
108 # first, fill table from parents by reverse linearization order
109 var parents
= self.mainmodule
.super_mclasses
(mclass
)
110 var lin
= self.mainmodule
.reverse_linearize_mclasses
(parents
)
112 for mproperty
in self.mainmodule
.properties
(parent
) do
113 if not mproperty
isa MVirtualTypeProp then continue
114 var color
= layout
.pos
[mproperty
]
115 if table
.length
<= color
then
116 for i
in [table
.length
.. color
[ do
120 for mpropdef
in mproperty
.mpropdefs
do
121 if mpropdef
.mclassdef
.mclass
== parent
then
122 table
[color
] = mpropdef
128 # then override with local properties
129 for mproperty
in self.mainmodule
.properties
(mclass
) do
130 if not mproperty
isa MVirtualTypeProp then continue
131 var color
= layout
.pos
[mproperty
]
132 if table
.length
<= color
then
133 for i
in [table
.length
.. color
[ do
137 for mpropdef
in mproperty
.mpropdefs
do
138 if mpropdef
.mclassdef
.mclass
== mclass
then
139 table
[color
] = mpropdef
143 tables
[mclass
] = table
149 fun build_class_typing_tables
(mclasses
: Set[MClass]): Map[MClass, Array[nullable MClass]] do
150 var tables
= new HashMap[MClass, Array[nullable MClass]]
151 var layout
= self.class_layout
152 for mclass
in mclasses
do
153 var table
= new Array[nullable MClass]
154 var supers
= new HashSet[MClass]
155 supers
.add_all
(self.mainmodule
.super_mclasses
(mclass
))
159 if layout
isa PHTypingLayout[MClass] then
160 color
= layout
.hashes
[mclass
][sup
]
162 color
= layout
.pos
[sup
]
164 if table
.length
<= color
then
165 for i
in [table
.length
.. color
[ do
171 tables
[mclass
] = table
176 redef fun compile_header_structs
do
177 self.header
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
178 self.compile_header_attribute_structs
179 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. */")
180 self.header
.add_decl
("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
181 self.header
.add_decl
("struct vts_entry \{ short int is_nullable; struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
183 if self.vt_layout
isa PHPropertyLayoutBuilder[MVirtualTypeProp] then
184 self.header
.add_decl
("struct vts_table \{ int mask; struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
186 self.header
.add_decl
("struct vts_table \{ struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
189 self.header
.add_decl
("typedef struct val \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
192 redef fun compile_class_to_c
(mclass
: MClass)
194 var mtype
= mclass
.intro
.bound_mtype
195 var c_name
= mclass
.c_name
197 var vft
= self.method_tables
[mclass
]
198 var attrs
= self.attr_tables
[mclass
]
199 var class_table
= self.class_tables
[mclass
]
200 var v
= self.new_visitor
202 v
.add_decl
("/* runtime class {c_name} */")
204 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
205 self.header
.add_decl
("struct class_{c_name} \{")
206 self.header
.add_decl
("int id;")
207 self.header
.add_decl
("const char *name;")
208 self.header
.add_decl
("int box_kind;")
209 self.header
.add_decl
("int color;")
210 self.header
.add_decl
("const struct vts_table *vts_table;")
211 self.header
.add_decl
("struct type_table *type_table;")
212 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
213 self.header
.add_decl
("\};")
216 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
217 v
.add_decl
("{self.class_layout.ids[mclass]},")
218 v
.add_decl
("\"{mclass.name}\
", /* class_name_string */")
219 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
220 var layout
= self.class_layout
221 if layout
isa PHTypingLayout[MClass] then
222 v
.add_decl
("{layout.masks[mclass]},")
224 v
.add_decl
("{layout.pos[mclass]},")
226 if build_class_vts_table
(mclass
) then
227 v
.add_decl
("(const struct vts_table*) &vts_table_{c_name},")
231 v
.add_decl
("(struct type_table*) &type_table_{c_name},")
233 for i
in [0 .. vft
.length
[ do
234 var mpropdef
= vft
[i
]
235 if mpropdef
== null then
236 v
.add_decl
("NULL, /* empty */")
238 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
239 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
241 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
248 # Build class type table
249 self.header
.add_decl
("extern const struct type_table_{c_name} type_table_{c_name};")
250 self.header
.add_decl
("struct type_table_{c_name} \{")
251 self.header
.add_decl
("int size;")
252 self.header
.add_decl
("int table[{class_table.length}];")
253 self.header
.add_decl
("\};")
255 v
.add_decl
("const struct type_table_{c_name} type_table_{c_name} = \{")
256 v
.add_decl
("{class_table.length},")
258 for msuper
in class_table
do
259 if msuper
== null then
260 v
.add_decl
("-1, /* empty */")
262 v
.add_decl
("{self.class_layout.ids[msuper]}, /* {msuper} */")
268 #Build instance struct
269 if mtype
.ctype
!= "val*" then
270 self.header
.add_decl
("struct instance_{c_name} \{")
271 self.header
.add_decl
("const struct class *class;")
272 self.header
.add_decl
("{mtype.ctype} value;")
273 self.header
.add_decl
("\};")
275 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
276 v
.add_decl
("/* allocate {mtype} */")
277 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
278 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
279 v
.add
("res->class = (struct class*) &class_{c_name};")
280 v
.add
("res->value = value;")
281 v
.add
("return (val*)res;")
286 var is_native_array
= mclass
.name
== "NativeArray"
289 if is_native_array
then
295 #Build instance struct
296 #extern const struct instance_array__NativeArray instance_array__NativeArray;
297 self.header
.add_decl
("struct instance_{c_name} \{")
298 self.header
.add_decl
("const struct class *class;")
299 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
300 if is_native_array
then
301 # NativeArrays are just a instance header followed by an array of values
302 self.header
.add_decl
("val* values[0];")
304 self.header
.add_decl
("\};")
307 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
308 v
.add_decl
("/* allocate {mtype} */")
309 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
310 var res
= v
.new_named_var
(mtype
, "self")
312 if is_native_array
then
313 var mtype_elt
= mtype
.arguments
.first
314 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
316 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
318 v
.add
("{res}->class = (struct class*) &class_{c_name};")
320 self.generate_init_attr
(v
, res
, mtype
)
321 v
.add
("return {res};")
324 generate_check_init_instance
(mtype
)
327 private fun build_class_vts_table
(mclass
: MClass): Bool do
328 if self.vt_tables
[mclass
].is_empty
then return false
330 self.header
.add_decl
("extern const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name};")
331 self.header
.add_decl
("struct vts_table_{mclass.c_name} \{")
332 if self.vt_layout
isa PHPropertyLayoutBuilder[MVirtualTypeProp] then
333 self.header
.add_decl
("int mask;")
335 self.header
.add_decl
("struct vts_entry vts[{self.vt_tables[mclass].length}];")
336 self.header
.add_decl
("\};")
339 v
.add_decl
("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
340 if self.vt_layout
isa PHPropertyLayoutBuilder[MVirtualTypeProp] then
341 #TODO redo this when PHPropertyLayoutBuilder will be implemented
342 #v.add_decl("{vt_masks[mclass]},")
346 for vt
in self.vt_tables
[mclass
] do
348 v
.add_decl
("\{-1, NULL\}, /* empty */")
351 var bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, vt
.as(MVirtualTypeDef).bound
)
352 while bound
isa MNullableType do
353 bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, bound
.mtype
)
356 v
.add_decl
("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
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"
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
)
378 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
382 redef fun display_sizes
384 print
"# size of tables"
385 print
"\trs size\trs hole\tst size\tst hole"
390 var rtables
= vt_tables
391 for unanch
, table
in rtables
do
392 rt_table
+= table
.length
393 for e
in table
do if e
== null then rt_holes
+= 1
396 var ttables
= class_tables
397 for t
, table
in ttables
do
398 st_table
+= table
.length
399 for e
in table
do if e
== null then st_holes
+= 1
401 print
"\t{rt_table}\t{rt_holes}\t{st_table}\t{st_holes}"
405 class SeparateErasureCompilerVisitor
406 super SeparateCompilerVisitor
408 redef fun compile_callsite
(callsite
, arguments
)
411 if callsite
.erasure_cast
and not self.compiler
.as(SeparateErasureCompiler).modelbuilder
.toolcontext
.opt_no_check_erasure_cast
.value
then
413 var mtype
= callsite
.msignature
.return_mtype
415 self.add
("/* Erasure cast for return {res} isa {mtype} */")
416 var cond
= self.type_test
(res
, mtype
, "erasure")
417 self.add
("if (!{cond}) \{")
418 #var x = self.class_name_string(res)
419 #var y = self.class_name_string(arguments.first)
420 #self.add("fprintf(stderr, \"Erasure cast: expected {mtype} (self is %s), got %s for {res}\\n\", {y}, {x});")
421 self.add_abort
("Cast failed")
427 redef fun init_instance
(mtype
) do return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
429 redef fun type_test
(value
, mtype
, tag
)
431 self.add
("/* type test for {value.inspect} isa {mtype} */")
433 var res
= self.new_var
(bool_type
)
435 var cltype
= self.get_name
("cltype")
436 self.add_decl
("int {cltype};")
437 var idtype
= self.get_name
("idtype")
438 self.add_decl
("int {idtype};")
440 var maybe_null
= self.maybe_null
(value
)
441 var accept_null
= "0"
442 if mtype
isa MNullableType then
446 if mtype
isa MParameterType then
447 # Here we get the bound of the the formal type (eh, erasure...)
448 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
449 if mtype
isa MNullableType then
455 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
456 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
457 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
458 self.compiler
.count_type_test_skipped
[tag
] += 1
459 self.add
("count_type_test_skipped_{tag}++;")
466 if value
.mtype
.ctype
== "val*" then
467 class_ptr
= "{value}->class->"
469 var mclass
= value
.mtype
.as(MClassType).mclass
470 class_ptr
= "class_{mclass.c_name}."
473 if mtype
isa MClassType then
474 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
475 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
476 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
477 self.compiler
.count_type_test_resolved
[tag
] += 1
478 self.add
("count_type_test_resolved_{tag}++;")
480 else if mtype
isa MVirtualType then
481 var recv
= self.frame
.arguments
.first
483 if recv
.mtype
.ctype
== "val*" then
484 recv_ptr
= "{recv}->class->"
486 var mclass
= recv
.mtype
.as(MClassType).mclass
487 recv_ptr
= "class_{mclass.c_name}."
489 var entry
= self.get_name
("entry")
490 self.add
("struct vts_entry {entry};")
491 if self.compiler
.as(SeparateErasureCompiler).vt_layout
isa PHPropertyLayoutBuilder[MVirtualTypeProp] then
492 self.add
("{entry} = {recv_ptr}vts_table->vts[HASH({recv_ptr}vts_table->mask, {mtype.mproperty.const_color})];")
494 self.add
("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
496 self.add
("{cltype} = {entry}.class->color;")
497 self.add
("{idtype} = {entry}.class->id;")
498 if maybe_null
and accept_null
== "0" then
499 var is_nullable
= self.get_name
("is_nullable")
500 self.add_decl
("short int {is_nullable};")
501 self.add
("{is_nullable} = {entry}.is_nullable;")
502 accept_null
= is_nullable
.to_s
504 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
505 self.compiler
.count_type_test_unresolved
[tag
] += 1
506 self.add
("count_type_test_unresolved_{tag}++;")
509 self.debug
("type_test({value.inspect}, {mtype})")
513 # check color is in table
515 self.add
("if({value} == NULL) \{")
516 self.add
("{res} = {accept_null};")
517 self.add
("\} else \{")
519 if self.compiler
.as(SeparateErasureCompiler).class_layout
isa PHTypingLayout[MClass] then
520 self.add
("{cltype} = HASH({class_ptr}color, {idtype});")
522 self.add
("if({cltype} >= {class_ptr}type_table->size) \{")
523 self.add
("{res} = 0;")
524 self.add
("\} else \{")
525 self.add
("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
534 redef fun class_name_string
(value
)
536 var res
= self.get_name
("var_class_name")
537 self.add_decl
("const char* {res};")
538 if value
.mtype
.ctype
== "val*" then
539 self.add
"{res} = {value} == NULL ? \"null\
" : {value}->class->name;"
541 self.add
"{res} = class_{value.mtype.c_name}.name;"
546 redef fun array_instance
(array
, elttype
)
548 var nclass
= self.get_class
("NativeArray")
549 elttype
= self.anchor
(elttype
)
550 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
551 var res
= self.init_instance
(arraytype
)
552 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
553 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
555 self.add
("{nat} = NEW_{nclass.c_name}({array.length});")
556 for i
in [0..array
.length
[ do
557 var r
= self.autobox
(array
[i
], self.object_type
)
558 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
560 var length
= self.int_instance
(array
.length
)
561 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
562 self.check_init_instance
(res
, arraytype
)
567 redef fun calloc_array
(ret_type
, arguments
)
569 var ret
= ret_type
.as(MClassType)
570 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type
))