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
20 intrude import coloring
22 redef class ToolContext
24 var opt_erasure
: OptionBool = new OptionBool("Erase generic types", "--erasure")
29 self.option_context
.add_option
(self.opt_erasure
)
33 redef class ModelBuilder
34 fun run_separate_erasure_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
37 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
39 var compiler
= new SeparateErasureCompiler(mainmodule
, runtime_type_analysis
, self)
40 var v
= compiler
.header
41 v
.add_decl
("#include <stdlib.h>")
42 v
.add_decl
("#include <stdio.h>")
43 v
.add_decl
("#include <string.h>")
44 v
.add_decl
("#include <gc/gc.h>")
45 v
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
46 v
.add_decl
("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
47 v
.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. */")
48 v
.add_decl
("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
49 v
.add_decl
("struct vts_entry \{ short int is_nullable; struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
50 v
.add_decl
("struct vts_table \{ struct vts_entry vts[1]; \}; /* vts list of a C type representation. */")
51 v
.add_decl
("typedef struct \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
52 v
.add_decl
("extern const char const * class_names[];")
54 # Declare global instances
55 v
.add_decl
("extern int glob_argc;")
56 v
.add_decl
("extern char **glob_argv;")
57 v
.add_decl
("extern val *glob_sys;")
59 # The main function of the C
60 compiler
.compile_main_function
62 # compile class structures
63 for m
in mainmodule
.in_importation
.greaters
do
64 for mclass
in m
.intro_mclasses
do
65 compiler
.compile_class_to_c
(mclass
)
70 for m
in mainmodule
.in_importation
.greaters
do
71 compiler
.compile_module_to_c
(m
)
74 write_and_make
(compiler
)
78 class SeparateErasureCompiler
79 super SeparateCompiler
81 private var class_ids
: HashMap[MClass, Int] = new HashMap[MClass, Int]
82 private var class_tables
: nullable Map[MClass, Array[nullable MClass]] = null
83 private var class_vts_colors
: Map[MVirtualTypeProp, Int]
84 private var class_vts_tables
: Map[MClass, Array[nullable MVirtualTypeDef]]
86 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
87 self.header
= self.new_visitor
89 var class_coloring
= new ClassColoring(mainmodule
)
90 self.class_colors
= class_coloring
.colorize
(mmbuilder
.model
.mclasses
)
91 self.class_tables
= class_coloring
.build_type_tables
(mmbuilder
.model
.mclasses
, class_colors
)
94 var method_coloring
= new MethodColoring(class_coloring
)
95 self.method_colors
= method_coloring
.colorize
96 self.method_tables
= method_coloring
.build_property_tables
98 # attributes coloration
99 var attribute_coloring
= new AttributeColoring(class_coloring
)
100 self.attr_colors
= attribute_coloring
.colorize
101 self.attr_tables
= attribute_coloring
.build_property_tables
104 var vt_coloring
= new VTColoring(class_coloring
)
105 self.class_vts_colors
= vt_coloring
.colorize
106 self.class_vts_tables
= vt_coloring
.build_property_tables
109 for mclass
in class_colors
.keys
do
110 self.class_ids
[mclass
] = self.class_ids
.length
113 # for the class_name and output_class_name methods
114 self.compile_class_names
115 self.compile_box_kinds
118 redef fun compile_class_names
do
119 # Build type names table
120 var type_array
= new Array[nullable MClass]
121 for t
, i
in class_ids
do
122 if i
>= type_array
.length
then
128 var v
= self.new_visitor
129 v
.add
("const char const * class_names[] = \{")
130 for t
in type_array
do
132 v
.add
("NULL, /* empty */")
140 redef fun compile_class_to_c
(mclass
: MClass)
142 var mtype
= mclass
.intro
.bound_mtype
143 var c_name
= mclass
.c_name
145 var vft
= self.method_tables
[mclass
]
146 var attrs
= self.attr_tables
[mclass
]
147 var class_table
= self.class_tables
[mclass
]
148 var v
= self.new_visitor
150 v
.add_decl
("/* runtime class {c_name} */")
151 var idnum
= classids
.length
152 var idname
= "ID_" + c_name
153 self.classids
[mtype
] = idname
154 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
156 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
157 self.header
.add_decl
("struct class_{c_name} \{")
158 self.header
.add_decl
("int id;")
159 self.header
.add_decl
("int box_kind;")
160 self.header
.add_decl
("int color;")
161 self.header
.add_decl
("const struct vts_table *vts_table;")
162 self.header
.add_decl
("struct type_table *type_table;")
163 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
164 self.header
.add_decl
("\};")
166 # extern const struct vts_table_X vts_table_X
167 self.header
.add_decl
("extern const struct vts_table_{c_name} vts_table_{c_name};")
168 self.header
.add_decl
("struct vts_table_{c_name} \{")
169 self.header
.add_decl
("struct vts_entry vts[{self.class_vts_tables[mclass].length}];")
170 self.header
.add_decl
("\};")
173 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
174 v
.add_decl
("{self.class_ids[mclass]},")
175 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
176 v
.add_decl
("{self.class_colors[mclass]},")
177 v
.add_decl
("(const struct vts_table*) &vts_table_{c_name},")
178 v
.add_decl
("(struct type_table*) &type_table_{c_name},")
180 for i
in [0 .. vft
.length
[ do
181 var mpropdef
= vft
[i
]
182 if mpropdef
== null then
183 v
.add_decl
("NULL, /* empty */")
185 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
186 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
188 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
195 build_class_vts_table
(mclass
, v
.as(SeparateErasureCompilerVisitor))
197 # Build class type table
198 self.header
.add_decl
("extern const struct type_table_{c_name} type_table_{c_name};")
199 self.header
.add_decl
("struct type_table_{c_name} \{")
200 self.header
.add_decl
("int size;")
201 self.header
.add_decl
("int table[{class_table.length}];")
202 self.header
.add_decl
("\};")
204 v
.add_decl
("const struct type_table_{c_name} type_table_{c_name} = \{")
205 v
.add_decl
("{class_table.length},")
207 for msuper
in class_table
do
208 if msuper
== null then
209 v
.add_decl
("-1, /* empty */")
211 v
.add_decl
("{self.class_ids[msuper]}, /* {msuper} */")
217 #Build instance struct
218 if mtype
.ctype
!= "val*" then
219 self.header
.add_decl
("struct instance_{c_name} \{")
220 self.header
.add_decl
("const struct class *class;")
221 self.header
.add_decl
("{mtype.ctype} value;")
222 self.header
.add_decl
("\};")
224 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
225 v
.add_decl
("/* allocate {mtype} */")
226 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
227 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
228 v
.add
("res->class = (struct class*) &class_{c_name};")
229 v
.add
("res->value = value;")
230 v
.add
("return (val*)res;")
235 var is_native_array
= mclass
.name
== "NativeArray"
238 if is_native_array
then
244 #Build instance struct
245 #extern const struct instance_array__NativeArray instance_array__NativeArray;
246 self.header
.add_decl
("struct instance_{c_name} \{")
247 self.header
.add_decl
("const struct class *class;")
248 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
249 if is_native_array
then
250 # NativeArrays are just a instance header followed by an array of values
251 self.header
.add_decl
("val* values[0];")
253 self.header
.add_decl
("\};")
256 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
257 v
.add_decl
("/* allocate {mtype} */")
258 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
259 var res
= v
.new_named_var
(mtype
, "self")
261 if is_native_array
then
262 var mtype_elt
= mtype
.arguments
.first
263 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
265 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
267 v
.add
("{res}->class = (struct class*) &class_{c_name};")
269 self.generate_init_attr
(v
, res
, mtype
)
270 v
.add
("return {res};")
273 generate_check_init_instance
(mtype
)
276 private fun build_class_vts_table
(mclass
: MClass, v
: SeparateCompilerVisitor) do
277 v
.add_decl
("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
280 for vt
in self.class_vts_tables
[mclass
] do
282 v
.add_decl
("NULL, /* empty */")
285 var bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, vt
.bound
)
286 while bound
isa MNullableType do
287 bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, bound
.mtype
)
290 v
.add_decl
("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
297 private fun retrieve_vt_bound
(anchor
: MClassType, mtype
: nullable MType): MType do
298 if mtype
== null then
299 print
"NOT YET IMPLEMENTED: retrieve_vt_bound on null"
302 if mtype
isa MVirtualType then
303 return mtype
.anchor_to
(mainmodule
, anchor
)
304 else if mtype
isa MParameterType then
305 return mtype
.anchor_to
(mainmodule
, anchor
)
311 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
314 class SeparateErasureCompilerVisitor
315 super SeparateCompilerVisitor
317 redef fun init_instance
(mtype
)
319 return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
322 redef fun type_test
(value
, mtype
)
324 self.add
("/* type test for {value.inspect} isa {mtype} */")
326 var res
= self.new_var
(bool_type
)
328 var cltype
= self.get_name
("cltype")
329 self.add_decl
("int {cltype};")
330 var idtype
= self.get_name
("idtype")
331 self.add_decl
("int {idtype};")
332 var is_nullable
= self.get_name
("is_nullable")
333 self.add_decl
("short int {is_nullable};")
334 var is_null
= self.get_name
("is_null")
335 self.add_decl
("short int {is_null};")
338 if mtype
isa MNullableType then
341 self.add
("{is_nullable} = 1;")
343 if mtype
isa MParameterType then
344 # Here we get the bound of the the formal type (eh, erasure...)
345 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
346 if mtype
isa MNullableType then
349 self.add
("{is_nullable} = 1;")
352 if mtype
isa MVirtualType then
353 # FIXME virtual types should not be erased but got from the class table of the current receiver (self.frame.arguments.first)
354 #mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, true)
355 #if mtype isa MNullableType then
356 # mtype = mtype.mtype
361 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
362 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
368 if value
.mtype
.ctype
== "val*" then
369 class_ptr
= "{value}->class->"
370 self.add
("{is_null} = {value} == NULL;")
372 var mclass
= value
.mtype
.as(MClassType).mclass
373 class_ptr
= "class_{mclass.c_name}."
374 self.add
("{is_null} = 0;")
377 if mtype
isa MClassType then
378 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
379 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
380 if maybe_null
== 0 then
381 self.add
("{is_nullable} = 0;")
383 else if mtype
isa MVirtualType then
384 var vtcolor
= compiler
.as(SeparateErasureCompiler).class_vts_colors
[mtype
.mproperty
.as(MVirtualTypeProp)]
385 var recv
= self.frame
.arguments
.first
386 var recv_boxed
= self.autobox
(recv
, self.object_type
)
387 self.add
("{cltype} = {recv_boxed}->class->vts_table->vts[{vtcolor}].class->color;")
388 self.add
("{idtype} = {recv_boxed}->class->vts_table->vts[{vtcolor}].class->id;")
389 if maybe_null
== 0 then
390 self.add
("{is_nullable} = {recv_boxed}->class->vts_table->vts[{vtcolor}].is_nullable;")
393 self.debug
("type_test({value.inspect}, {mtype})")
397 # check color is in table
398 self.add
("if({is_null}) \{")
399 self.add
("{res} = {is_nullable};")
400 self.add
("\} else \{")
401 self.add
("if({cltype} >= {class_ptr}type_table->size) \{")
402 self.add
("{res} = 0;")
403 self.add
("\} else \{")
404 self.add
("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
411 redef fun class_name_string
(value
)
413 var res
= self.get_name
("var_class_name")
414 self.add_decl
("const char *{res};")
415 self.add
("{res} = class_names[{value}->class->id];")
419 redef fun array_instance
(array
, elttype
)
421 var nclass
= self.get_class
("NativeArray")
422 elttype
= self.anchor
(elttype
)
423 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
424 var res
= self.init_instance
(arraytype
)
425 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
426 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
428 self.add
("{nat} = NEW_{nclass.c_name}({array.length});")
429 for i
in [0..array
.length
[ do
430 var r
= self.autobox
(array
[i
], self.object_type
)
431 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
433 var length
= self.int_instance
(array
.length
)
434 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
435 self.check_init_instance
(res
, arraytype
)
440 redef fun calloc_array
(ret_type
, arguments
)
442 var ret
= ret_type
.as(MClassType)
443 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type
))