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
)
48 for m
in mainmodule
.in_importation
.greaters
do
49 compiler
.compile_module_to_c
(m
)
52 write_and_make
(compiler
)
56 class SeparateErasureCompiler
57 super SeparateCompiler
59 private var class_ids
: HashMap[MClass, Int] = new HashMap[MClass, Int]
60 private var class_colors
: Map[MClass, Int]
61 private var class_tables
: Map[MClass, Array[nullable MClass]]
63 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
64 var mclasses
= new HashSet[MClass]
65 mclasses
.add_all
(mmbuilder
.model
.mclasses
)
68 if modelbuilder
.toolcontext
.opt_use_mod_perfect_hashing
.value
then
70 for mclass
in mclasses
do
71 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
74 var class_coloring
= new ClassModPerfectHashing(mainmodule
)
75 self.class_colors
= class_coloring
.compute_masks
(mclasses
, class_ids
)
76 self.class_tables
= class_coloring
.hash_type_tables
(mclasses
, class_ids
, class_colors
)
78 self.header
.add_decl
("int HASH(int, int);")
80 v
.add_decl
("int HASH(int mask, int id) \{")
81 v
.add_decl
("return mask % id;")
83 else if modelbuilder
.toolcontext
.opt_use_and_perfect_hashing
.value
then
85 for mclass
in mclasses
do
86 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
89 var class_coloring
= new ClassAndPerfectHashing(mainmodule
)
90 self.class_colors
= class_coloring
.compute_masks
(mclasses
, class_ids
)
91 self.class_tables
= class_coloring
.hash_type_tables
(mclasses
, class_ids
, class_colors
)
93 self.header
.add_decl
("int HASH(int, int);")
95 v
.add_decl
("int HASH(int mask, int id) \{")
96 v
.add_decl
("return mask & id;")
100 if modelbuilder
.toolcontext
.opt_use_naive_coloring
.value
then
101 class_coloring
= new NaiveClassColoring(mainmodule
)
103 class_coloring
= new ClassColoring(mainmodule
)
106 for mclass
in mclasses
do
107 self.class_ids
[mclass
] = self.class_ids
.length
+ 1
109 self.class_colors
= class_coloring
.colorize
(modelbuilder
.model
.mclasses
)
110 self.class_tables
= class_coloring
.build_type_tables
(modelbuilder
.model
.mclasses
, class_colors
)
113 # for the class_name and output_class_name methods
114 self.compile_class_names
117 redef fun compile_header_structs
do
118 self.header
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
119 self.header
.add_decl
("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
120 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. */")
121 self.header
.add_decl
("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
122 self.header
.add_decl
("struct vts_entry \{ short int is_nullable; struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
123 self.header
.add_decl
("struct vts_table \{ struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
124 self.header
.add_decl
("typedef struct \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
125 self.header
.add_decl
("extern const char const * class_names[];")
128 redef fun compile_class_names
do
129 # Build type names table
130 var type_array
= new Array[nullable MClass]
131 for t
, id
in class_ids
do
132 if id
>= type_array
.length
then
133 for i
in [type_array
.length
..id
[ do
140 var v
= self.new_visitor
141 v
.add
("const char const * class_names[] = \{")
142 for t
in type_array
do
144 v
.add
("NULL, /* empty */")
152 redef fun compile_class_to_c
(mclass
: MClass)
154 var mtype
= mclass
.intro
.bound_mtype
155 var c_name
= mclass
.c_name
157 var vft
= self.method_tables
[mclass
]
158 var attrs
= self.attr_tables
[mclass
]
159 var class_table
= self.class_tables
[mclass
]
160 var v
= self.new_visitor
162 v
.add_decl
("/* runtime class {c_name} */")
163 var idnum
= classids
.length
164 var idname
= "ID_" + c_name
165 self.classids
[mtype
] = idname
166 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
168 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
169 self.header
.add_decl
("struct class_{c_name} \{")
170 self.header
.add_decl
("int id;")
171 self.header
.add_decl
("int box_kind;")
172 self.header
.add_decl
("int color;")
173 self.header
.add_decl
("const struct vts_table *vts_table;")
174 self.header
.add_decl
("struct type_table *type_table;")
175 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
176 self.header
.add_decl
("\};")
178 # extern const struct vts_table_X vts_table_X
179 self.header
.add_decl
("extern const struct vts_table_{c_name} vts_table_{c_name};")
180 self.header
.add_decl
("struct vts_table_{c_name} \{")
181 self.header
.add_decl
("struct vts_entry vts[{self.vt_tables[mclass].length}];")
182 self.header
.add_decl
("\};")
185 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
186 v
.add_decl
("{self.class_ids[mclass]},")
187 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
188 v
.add_decl
("{self.class_colors[mclass]},")
189 v
.add_decl
("(const struct vts_table*) &vts_table_{c_name},")
190 v
.add_decl
("(struct type_table*) &type_table_{c_name},")
192 for i
in [0 .. vft
.length
[ do
193 var mpropdef
= vft
[i
]
194 if mpropdef
== null then
195 v
.add_decl
("NULL, /* empty */")
197 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
198 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
200 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
207 build_class_vts_table
(mclass
, v
.as(SeparateErasureCompilerVisitor))
209 # Build class type table
210 self.header
.add_decl
("extern const struct type_table_{c_name} type_table_{c_name};")
211 self.header
.add_decl
("struct type_table_{c_name} \{")
212 self.header
.add_decl
("int size;")
213 self.header
.add_decl
("int table[{class_table.length}];")
214 self.header
.add_decl
("\};")
216 v
.add_decl
("const struct type_table_{c_name} type_table_{c_name} = \{")
217 v
.add_decl
("{class_table.length},")
219 for msuper
in class_table
do
220 if msuper
== null then
221 v
.add_decl
("-1, /* empty */")
223 v
.add_decl
("{self.class_ids[msuper]}, /* {msuper} */")
229 #Build instance struct
230 if mtype
.ctype
!= "val*" then
231 self.header
.add_decl
("struct instance_{c_name} \{")
232 self.header
.add_decl
("const struct class *class;")
233 self.header
.add_decl
("{mtype.ctype} value;")
234 self.header
.add_decl
("\};")
236 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
237 v
.add_decl
("/* allocate {mtype} */")
238 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
239 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
240 v
.add
("res->class = (struct class*) &class_{c_name};")
241 v
.add
("res->value = value;")
242 v
.add
("return (val*)res;")
247 var is_native_array
= mclass
.name
== "NativeArray"
250 if is_native_array
then
256 #Build instance struct
257 #extern const struct instance_array__NativeArray instance_array__NativeArray;
258 self.header
.add_decl
("struct instance_{c_name} \{")
259 self.header
.add_decl
("const struct class *class;")
260 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
261 if is_native_array
then
262 # NativeArrays are just a instance header followed by an array of values
263 self.header
.add_decl
("val* values[0];")
265 self.header
.add_decl
("\};")
268 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
269 v
.add_decl
("/* allocate {mtype} */")
270 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
271 var res
= v
.new_named_var
(mtype
, "self")
273 if is_native_array
then
274 var mtype_elt
= mtype
.arguments
.first
275 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
277 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
279 v
.add
("{res}->class = (struct class*) &class_{c_name};")
281 self.generate_init_attr
(v
, res
, mtype
)
282 v
.add
("return {res};")
285 generate_check_init_instance
(mtype
)
288 private fun build_class_vts_table
(mclass
: MClass, v
: SeparateCompilerVisitor) do
289 v
.add_decl
("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
292 for vt
in self.vt_tables
[mclass
] do
294 v
.add_decl
("\{-1, NULL\}, /* empty */")
297 var bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, vt
.bound
)
298 while bound
isa MNullableType do
299 bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, bound
.mtype
)
302 v
.add_decl
("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
309 private fun retrieve_vt_bound
(anchor
: MClassType, mtype
: nullable MType): MType do
310 if mtype
== null then
311 print
"NOT YET IMPLEMENTED: retrieve_vt_bound on null"
314 if mtype
isa MVirtualType then
315 return mtype
.anchor_to
(mainmodule
, anchor
)
316 else if mtype
isa MParameterType then
317 return mtype
.anchor_to
(mainmodule
, anchor
)
323 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
326 class SeparateErasureCompilerVisitor
327 super SeparateCompilerVisitor
329 redef fun init_instance
(mtype
)
331 return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
334 redef fun type_test
(value
, mtype
)
336 self.add
("/* type test for {value.inspect} isa {mtype} */")
338 var res
= self.new_var
(bool_type
)
340 var cltype
= self.get_name
("cltype")
341 self.add_decl
("int {cltype};")
342 var idtype
= self.get_name
("idtype")
343 self.add_decl
("int {idtype};")
344 var is_nullable
= self.get_name
("is_nullable")
345 self.add_decl
("short int {is_nullable};")
346 var is_null
= self.get_name
("is_null")
347 self.add_decl
("short int {is_null};")
350 if mtype
isa MNullableType then
353 self.add
("{is_nullable} = 1;")
355 if mtype
isa MParameterType then
356 # Here we get the bound of the the formal type (eh, erasure...)
357 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
358 if mtype
isa MNullableType then
361 self.add
("{is_nullable} = 1;")
364 if mtype
isa MVirtualType then
365 # FIXME virtual types should not be erased but got from the class table of the current receiver (self.frame.arguments.first)
366 #mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, true)
367 #if mtype isa MNullableType then
368 # mtype = mtype.mtype
373 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
374 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
380 if value
.mtype
.ctype
== "val*" then
381 class_ptr
= "{value}->class->"
382 self.add
("{is_null} = {value} == NULL;")
384 var mclass
= value
.mtype
.as(MClassType).mclass
385 class_ptr
= "class_{mclass.c_name}."
386 self.add
("{is_null} = 0;")
389 if mtype
isa MClassType then
390 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
391 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
392 if maybe_null
== 0 then
393 self.add
("{is_nullable} = 0;")
395 else if mtype
isa MVirtualType then
396 var recv
= self.frame
.arguments
.first
397 var recv_boxed
= self.autobox
(recv
, self.object_type
)
398 self.add
("{cltype} = {recv_boxed}->class->vts_table->vts[{mtype.mproperty.const_color}].class->color;")
399 self.add
("{idtype} = {recv_boxed}->class->vts_table->vts[{mtype.mproperty.const_color}].class->id;")
400 if maybe_null
== 0 then
401 self.add
("{is_nullable} = {recv_boxed}->class->vts_table->vts[{mtype.mproperty.const_color}].is_nullable;")
404 self.debug
("type_test({value.inspect}, {mtype})")
408 # check color is in table
409 self.add
("if({is_null}) \{")
410 self.add
("{res} = {is_nullable};")
411 self.add
("\} else \{")
412 if compiler
.modelbuilder
.toolcontext
.opt_use_mod_perfect_hashing
.value
or compiler
.modelbuilder
.toolcontext
.opt_use_and_perfect_hashing
.value
then
413 self.add
("{cltype} = HASH({class_ptr}color, {idtype});")
415 self.add
("if({cltype} >= {class_ptr}type_table->size) \{")
416 self.add
("{res} = 0;")
417 self.add
("\} else \{")
418 self.add
("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
425 redef fun class_name_string
(value
)
427 var res
= self.get_name
("var_class_name")
428 self.add_decl
("const char *{res};")
429 self.add
("{res} = class_names[{value}->class->id];")
433 redef fun array_instance
(array
, elttype
)
435 var nclass
= self.get_class
("NativeArray")
436 elttype
= self.anchor
(elttype
)
437 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
438 var res
= self.init_instance
(arraytype
)
439 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
440 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
442 self.add
("{nat} = NEW_{nclass.c_name}({array.length});")
443 for i
in [0..array
.length
[ do
444 var r
= self.autobox
(array
[i
], self.object_type
)
445 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
447 var length
= self.int_instance
(array
.length
)
448 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
449 self.check_init_instance
(res
, arraytype
)
454 redef fun calloc_array
(ret_type
, arguments
)
456 var ret
= ret_type
.as(MClassType)
457 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type
))