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")
28 self.option_context
.add_option
(self.opt_erasure
)
32 redef class ModelBuilder
33 fun run_separate_erasure_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
36 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
38 var compiler
= new SeparateErasureCompiler(mainmodule
, runtime_type_analysis
, self)
40 # compile class structures
41 for m
in mainmodule
.in_importation
.greaters
do
42 for mclass
in m
.intro_mclasses
do
43 compiler
.compile_class_to_c
(mclass
)
47 # The main function of the C
48 compiler
.compile_main_function
51 for m
in mainmodule
.in_importation
.greaters
do
52 compiler
.compile_module_to_c
(m
)
55 write_and_make
(compiler
)
59 class SeparateErasureCompiler
60 super SeparateCompiler
62 private var class_ids
: HashMap[MClass, Int] = new HashMap[MClass, Int]
63 private var class_colors
: Map[MClass, Int]
64 private var class_tables
: Map[MClass, Array[nullable MClass]]
66 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
67 var mclasses
= new HashSet[MClass]
68 mclasses
.add_all
(mmbuilder
.model
.mclasses
)
71 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
then
73 for mclass
in mclasses
do
74 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
77 var class_coloring
= new ClassModPerfectHashing(mainmodule
)
78 self.class_colors
= class_coloring
.compute_masks
(mclasses
, class_ids
)
79 self.class_tables
= class_coloring
.hash_type_tables
(mclasses
, class_ids
, class_colors
)
81 self.header
.add_decl
("int HASH(int, int);")
83 v
.add_decl
("int HASH(int mask, int id) \{")
84 v
.add_decl
("return mask % id;")
86 else if modelbuilder
.toolcontext
.opt_phand_typing
.value
then
88 for mclass
in mclasses
do
89 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
92 var class_coloring
= new ClassAndPerfectHashing(mainmodule
)
93 self.class_colors
= class_coloring
.compute_masks
(mclasses
, class_ids
)
94 self.class_tables
= class_coloring
.hash_type_tables
(mclasses
, class_ids
, class_colors
)
96 self.header
.add_decl
("int HASH(int, int);")
98 v
.add_decl
("int HASH(int mask, int id) \{")
99 v
.add_decl
("return mask & id;")
103 if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
104 class_coloring
= new NaiveClassColoring(mainmodule
)
106 class_coloring
= new ClassColoring(mainmodule
)
109 for mclass
in mclasses
do
110 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
112 self.class_colors
= class_coloring
.colorize
(modelbuilder
.model
.mclasses
)
113 self.class_tables
= class_coloring
.build_type_tables
(modelbuilder
.model
.mclasses
, class_colors
)
116 # for the class_name and output_class_name methods
117 self.compile_class_names
120 redef fun compile_header_structs
do
121 self.header
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
122 self.header
.add_decl
("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
123 self.header
.add_decl
("struct class \{ int id; 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. */")
124 self.header
.add_decl
("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
125 self.header
.add_decl
("struct vts_entry \{ short int is_nullable; struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
127 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
128 self.header
.add_decl
("struct vts_table \{ int mask; struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
130 self.header
.add_decl
("struct vts_table \{ struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
133 self.header
.add_decl
("typedef struct \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
134 self.header
.add_decl
("extern const char const * class_names[];")
137 redef fun compile_class_names
do
138 # Build type names table
139 var type_array
= new Array[nullable MClass]
140 for t
, id
in class_ids
do
141 if id
>= type_array
.length
then
142 for i
in [type_array
.length
..id
[ do
149 var v
= self.new_visitor
150 v
.add
("const char const * class_names[] = \{")
151 for t
in type_array
do
153 v
.add
("NULL, /* empty */")
161 redef fun compile_class_to_c
(mclass
: MClass)
163 var mtype
= mclass
.intro
.bound_mtype
164 var c_name
= mclass
.c_name
166 var vft
= self.method_tables
[mclass
]
167 var attrs
= self.attr_tables
[mclass
]
168 var class_table
= self.class_tables
[mclass
]
169 var v
= self.new_visitor
171 v
.add_decl
("/* runtime class {c_name} */")
172 var idnum
= classids
.length
173 var idname
= "ID_" + c_name
174 self.classids
[mtype
] = idname
175 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
177 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
178 self.header
.add_decl
("struct class_{c_name} \{")
179 self.header
.add_decl
("int id;")
180 self.header
.add_decl
("int box_kind;")
181 self.header
.add_decl
("int color;")
182 self.header
.add_decl
("const struct vts_table *vts_table;")
183 self.header
.add_decl
("struct type_table *type_table;")
184 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
185 self.header
.add_decl
("\};")
188 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
189 v
.add_decl
("{self.class_ids[mclass]},")
190 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
191 v
.add_decl
("{self.class_colors[mclass]},")
192 if build_class_vts_table
(mclass
) then
193 v
.add_decl
("(const struct vts_table*) &vts_table_{c_name},")
197 v
.add_decl
("(struct type_table*) &type_table_{c_name},")
199 for i
in [0 .. vft
.length
[ do
200 var mpropdef
= vft
[i
]
201 if mpropdef
== null then
202 v
.add_decl
("NULL, /* empty */")
204 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
205 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
207 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
214 # Build class type table
215 self.header
.add_decl
("extern const struct type_table_{c_name} type_table_{c_name};")
216 self.header
.add_decl
("struct type_table_{c_name} \{")
217 self.header
.add_decl
("int size;")
218 self.header
.add_decl
("int table[{class_table.length}];")
219 self.header
.add_decl
("\};")
221 v
.add_decl
("const struct type_table_{c_name} type_table_{c_name} = \{")
222 v
.add_decl
("{class_table.length},")
224 for msuper
in class_table
do
225 if msuper
== null then
226 v
.add_decl
("-1, /* empty */")
228 v
.add_decl
("{self.class_ids[msuper]}, /* {msuper} */")
234 #Build instance struct
235 if mtype
.ctype
!= "val*" then
236 self.header
.add_decl
("struct instance_{c_name} \{")
237 self.header
.add_decl
("const struct class *class;")
238 self.header
.add_decl
("{mtype.ctype} value;")
239 self.header
.add_decl
("\};")
241 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
242 v
.add_decl
("/* allocate {mtype} */")
243 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
244 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
245 v
.add
("res->class = (struct class*) &class_{c_name};")
246 v
.add
("res->value = value;")
247 v
.add
("return (val*)res;")
252 var is_native_array
= mclass
.name
== "NativeArray"
255 if is_native_array
then
261 #Build instance struct
262 #extern const struct instance_array__NativeArray instance_array__NativeArray;
263 self.header
.add_decl
("struct instance_{c_name} \{")
264 self.header
.add_decl
("const struct class *class;")
265 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
266 if is_native_array
then
267 # NativeArrays are just a instance header followed by an array of values
268 self.header
.add_decl
("val* values[0];")
270 self.header
.add_decl
("\};")
273 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
274 v
.add_decl
("/* allocate {mtype} */")
275 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
276 var res
= v
.new_named_var
(mtype
, "self")
278 if is_native_array
then
279 var mtype_elt
= mtype
.arguments
.first
280 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
282 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
284 v
.add
("{res}->class = (struct class*) &class_{c_name};")
286 self.generate_init_attr
(v
, res
, mtype
)
287 v
.add
("return {res};")
290 generate_check_init_instance
(mtype
)
293 private fun build_class_vts_table
(mclass
: MClass): Bool do
294 if self.vt_tables
[mclass
].is_empty
then return false
296 self.header
.add_decl
("extern const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name};")
297 self.header
.add_decl
("struct vts_table_{mclass.c_name} \{")
298 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
299 self.header
.add_decl
("int mask;")
301 self.header
.add_decl
("struct vts_entry vts[{self.vt_tables[mclass].length}];")
302 self.header
.add_decl
("\};")
305 v
.add_decl
("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
306 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
307 v
.add_decl
("{vt_masks[mclass]},")
311 for vt
in self.vt_tables
[mclass
] do
313 v
.add_decl
("\{-1, NULL\}, /* empty */")
316 var bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, vt
.bound
)
317 while bound
isa MNullableType do
318 bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, bound
.mtype
)
321 v
.add_decl
("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
329 private fun retrieve_vt_bound
(anchor
: MClassType, mtype
: nullable MType): MType do
330 if mtype
== null then
331 print
"NOT YET IMPLEMENTED: retrieve_vt_bound on null"
334 if mtype
isa MVirtualType then
335 return mtype
.anchor_to
(mainmodule
, anchor
)
336 else if mtype
isa MParameterType then
337 return mtype
.anchor_to
(mainmodule
, anchor
)
343 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
346 class SeparateErasureCompilerVisitor
347 super SeparateCompilerVisitor
349 redef fun init_instance
(mtype
)
351 return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
354 redef fun type_test
(value
, mtype
)
356 self.add
("/* type test for {value.inspect} isa {mtype} */")
358 var res
= self.new_var
(bool_type
)
360 var cltype
= self.get_name
("cltype")
361 self.add_decl
("int {cltype};")
362 var idtype
= self.get_name
("idtype")
363 self.add_decl
("int {idtype};")
364 var is_nullable
= self.get_name
("is_nullable")
365 self.add_decl
("short int {is_nullable};")
366 var is_null
= self.get_name
("is_null")
367 self.add_decl
("short int {is_null};")
370 if mtype
isa MNullableType then
373 self.add
("{is_nullable} = 1;")
375 if mtype
isa MParameterType then
376 # Here we get the bound of the the formal type (eh, erasure...)
377 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
378 if mtype
isa MNullableType then
381 self.add
("{is_nullable} = 1;")
384 if mtype
isa MVirtualType then
385 # FIXME virtual types should not be erased but got from the class table of the current receiver (self.frame.arguments.first)
386 #mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, true)
387 #if mtype isa MNullableType then
388 # mtype = mtype.mtype
393 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
394 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
400 if value
.mtype
.ctype
== "val*" then
401 class_ptr
= "{value}->class->"
402 self.add
("{is_null} = {value} == NULL;")
404 var mclass
= value
.mtype
.as(MClassType).mclass
405 class_ptr
= "class_{mclass.c_name}."
406 self.add
("{is_null} = 0;")
409 if mtype
isa MClassType then
410 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
411 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
412 if maybe_null
== 0 then
413 self.add
("{is_nullable} = 0;")
415 else if mtype
isa MVirtualType then
416 var recv
= self.frame
.arguments
.first
417 var recv_boxed
= self.autobox
(recv
, self.object_type
)
418 var entry
= self.get_name
("entry")
419 self.add
("struct vts_entry {entry};")
420 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
421 self.add
("{entry} = {recv_boxed}->class->vts_table->vts[HASH({recv_boxed}->class->vts_table->mask, {mtype.mproperty.const_color})];")
423 self.add
("{entry} = {recv_boxed}->class->vts_table->vts[{mtype.mproperty.const_color}];")
425 self.add
("{cltype} = {entry}.class->color;")
426 self.add
("{idtype} = {entry}.class->id;")
427 if maybe_null
== 0 then
428 self.add
("{is_nullable} = {entry}.is_nullable;")
431 self.debug
("type_test({value.inspect}, {mtype})")
435 # check color is in table
436 self.add
("if({is_null}) \{")
437 self.add
("{res} = {is_nullable};")
438 self.add
("\} else \{")
439 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
440 self.add
("{cltype} = HASH({class_ptr}color, {idtype});")
442 self.add
("if({cltype} >= {class_ptr}type_table->size) \{")
443 self.add
("{res} = 0;")
444 self.add
("\} else \{")
445 self.add
("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
452 redef fun class_name_string
(value
)
454 var res
= self.get_name
("var_class_name")
455 self.add_decl
("const char *{res};")
456 self.add
("{res} = class_names[{value}->class->id];")
460 redef fun array_instance
(array
, elttype
)
462 var nclass
= self.get_class
("NativeArray")
463 elttype
= self.anchor
(elttype
)
464 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
465 var res
= self.init_instance
(arraytype
)
466 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
467 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
469 self.add
("{nat} = NEW_{nclass.c_name}({array.length});")
470 for i
in [0..array
.length
[ do
471 var r
= self.autobox
(array
[i
], self.object_type
)
472 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
474 var length
= self.int_instance
(array
.length
)
475 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
476 self.check_init_instance
(res
, arraytype
)
481 redef fun calloc_array
(ret_type
, arguments
)
483 var ret
= ret_type
.as(MClassType)
484 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type
))