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
84 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
88 self.class_tables
= class_coloring
.build_type_tables
(mmbuilder
.model
.mclasses
, class_colors
)
91 for mclass
in class_colors
.keys
do
92 self.class_ids
[mclass
] = self.class_ids
.length
95 # for the class_name and output_class_name methods
96 self.compile_class_names
97 self.compile_box_kinds
100 redef fun compile_class_names
do
101 # Build type names table
102 var type_array
= new Array[nullable MClass]
103 for t
, i
in class_ids
do
104 if i
>= type_array
.length
then
110 var v
= self.new_visitor
111 v
.add
("const char const * class_names[] = \{")
112 for t
in type_array
do
114 v
.add
("NULL, /* empty */")
122 redef fun compile_class_to_c
(mclass
: MClass)
124 var mtype
= mclass
.intro
.bound_mtype
125 var c_name
= mclass
.c_name
127 var vft
= self.method_tables
[mclass
]
128 var attrs
= self.attr_tables
[mclass
]
129 var class_table
= self.class_tables
[mclass
]
130 var v
= self.new_visitor
132 v
.add_decl
("/* runtime class {c_name} */")
133 var idnum
= classids
.length
134 var idname
= "ID_" + c_name
135 self.classids
[mtype
] = idname
136 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
138 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
139 self.header
.add_decl
("struct class_{c_name} \{")
140 self.header
.add_decl
("int id;")
141 self.header
.add_decl
("int box_kind;")
142 self.header
.add_decl
("int color;")
143 self.header
.add_decl
("const struct vts_table *vts_table;")
144 self.header
.add_decl
("struct type_table *type_table;")
145 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
146 self.header
.add_decl
("\};")
148 # extern const struct vts_table_X vts_table_X
149 self.header
.add_decl
("extern const struct vts_table_{c_name} vts_table_{c_name};")
150 self.header
.add_decl
("struct vts_table_{c_name} \{")
151 self.header
.add_decl
("struct vts_entry vts[{self.vt_tables[mclass].length}];")
152 self.header
.add_decl
("\};")
155 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
156 v
.add_decl
("{self.class_ids[mclass]},")
157 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
158 v
.add_decl
("{self.class_colors[mclass]},")
159 v
.add_decl
("(const struct vts_table*) &vts_table_{c_name},")
160 v
.add_decl
("(struct type_table*) &type_table_{c_name},")
162 for i
in [0 .. vft
.length
[ do
163 var mpropdef
= vft
[i
]
164 if mpropdef
== null then
165 v
.add_decl
("NULL, /* empty */")
167 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
168 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
170 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
177 build_class_vts_table
(mclass
, v
.as(SeparateErasureCompilerVisitor))
179 # Build class type table
180 self.header
.add_decl
("extern const struct type_table_{c_name} type_table_{c_name};")
181 self.header
.add_decl
("struct type_table_{c_name} \{")
182 self.header
.add_decl
("int size;")
183 self.header
.add_decl
("int table[{class_table.length}];")
184 self.header
.add_decl
("\};")
186 v
.add_decl
("const struct type_table_{c_name} type_table_{c_name} = \{")
187 v
.add_decl
("{class_table.length},")
189 for msuper
in class_table
do
190 if msuper
== null then
191 v
.add_decl
("-1, /* empty */")
193 v
.add_decl
("{self.class_ids[msuper]}, /* {msuper} */")
199 #Build instance struct
200 if mtype
.ctype
!= "val*" then
201 self.header
.add_decl
("struct instance_{c_name} \{")
202 self.header
.add_decl
("const struct class *class;")
203 self.header
.add_decl
("{mtype.ctype} value;")
204 self.header
.add_decl
("\};")
206 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
207 v
.add_decl
("/* allocate {mtype} */")
208 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
209 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
210 v
.add
("res->class = (struct class*) &class_{c_name};")
211 v
.add
("res->value = value;")
212 v
.add
("return (val*)res;")
217 var is_native_array
= mclass
.name
== "NativeArray"
220 if is_native_array
then
226 #Build instance struct
227 #extern const struct instance_array__NativeArray instance_array__NativeArray;
228 self.header
.add_decl
("struct instance_{c_name} \{")
229 self.header
.add_decl
("const struct class *class;")
230 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
231 if is_native_array
then
232 # NativeArrays are just a instance header followed by an array of values
233 self.header
.add_decl
("val* values[0];")
235 self.header
.add_decl
("\};")
238 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
239 v
.add_decl
("/* allocate {mtype} */")
240 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
241 var res
= v
.new_named_var
(mtype
, "self")
243 if is_native_array
then
244 var mtype_elt
= mtype
.arguments
.first
245 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
247 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
249 v
.add
("{res}->class = (struct class*) &class_{c_name};")
251 self.generate_init_attr
(v
, res
, mtype
)
252 v
.add
("return {res};")
255 generate_check_init_instance
(mtype
)
258 private fun build_class_vts_table
(mclass
: MClass, v
: SeparateCompilerVisitor) do
259 v
.add_decl
("const struct vts_table_{mclass.c_name} vts_table_{mclass.c_name} = \{")
262 for vt
in self.vt_tables
[mclass
] do
264 v
.add_decl
("NULL, /* empty */")
267 var bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, vt
.bound
)
268 while bound
isa MNullableType do
269 bound
= retrieve_vt_bound
(mclass
.intro
.bound_mtype
, bound
.mtype
)
272 v
.add_decl
("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
279 private fun retrieve_vt_bound
(anchor
: MClassType, mtype
: nullable MType): MType do
280 if mtype
== null then
281 print
"NOT YET IMPLEMENTED: retrieve_vt_bound on null"
284 if mtype
isa MVirtualType then
285 return mtype
.anchor_to
(mainmodule
, anchor
)
286 else if mtype
isa MParameterType then
287 return mtype
.anchor_to
(mainmodule
, anchor
)
293 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
296 class SeparateErasureCompilerVisitor
297 super SeparateCompilerVisitor
299 redef fun init_instance
(mtype
)
301 return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
304 redef fun type_test
(value
, mtype
)
306 self.add
("/* type test for {value.inspect} isa {mtype} */")
308 var res
= self.new_var
(bool_type
)
310 var cltype
= self.get_name
("cltype")
311 self.add_decl
("int {cltype};")
312 var idtype
= self.get_name
("idtype")
313 self.add_decl
("int {idtype};")
314 var is_nullable
= self.get_name
("is_nullable")
315 self.add_decl
("short int {is_nullable};")
316 var is_null
= self.get_name
("is_null")
317 self.add_decl
("short int {is_null};")
320 if mtype
isa MNullableType then
323 self.add
("{is_nullable} = 1;")
325 if mtype
isa MParameterType then
326 # Here we get the bound of the the formal type (eh, erasure...)
327 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
328 if mtype
isa MNullableType then
331 self.add
("{is_nullable} = 1;")
334 if mtype
isa MVirtualType then
335 # FIXME virtual types should not be erased but got from the class table of the current receiver (self.frame.arguments.first)
336 #mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, true)
337 #if mtype isa MNullableType then
338 # mtype = mtype.mtype
343 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
344 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
350 if value
.mtype
.ctype
== "val*" then
351 class_ptr
= "{value}->class->"
352 self.add
("{is_null} = {value} == NULL;")
354 var mclass
= value
.mtype
.as(MClassType).mclass
355 class_ptr
= "class_{mclass.c_name}."
356 self.add
("{is_null} = 0;")
359 if mtype
isa MClassType then
360 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
361 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
362 if maybe_null
== 0 then
363 self.add
("{is_nullable} = 0;")
365 else if mtype
isa MVirtualType then
366 var recv
= self.frame
.arguments
.first
367 var recv_boxed
= self.autobox
(recv
, self.object_type
)
368 self.add
("{cltype} = {recv_boxed}->class->vts_table->vts[{mtype.mproperty.const_color}].class->color;")
369 self.add
("{idtype} = {recv_boxed}->class->vts_table->vts[{mtype.mproperty.const_color}].class->id;")
370 if maybe_null
== 0 then
371 self.add
("{is_nullable} = {recv_boxed}->class->vts_table->vts[{mtype.mproperty.const_color}].is_nullable;")
374 self.debug
("type_test({value.inspect}, {mtype})")
378 # check color is in table
379 self.add
("if({is_null}) \{")
380 self.add
("{res} = {is_nullable};")
381 self.add
("\} else \{")
382 self.add
("if({cltype} >= {class_ptr}type_table->size) \{")
383 self.add
("{res} = 0;")
384 self.add
("\} else \{")
385 self.add
("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
392 redef fun class_name_string
(value
)
394 var res
= self.get_name
("var_class_name")
395 self.add_decl
("const char *{res};")
396 self.add
("{res} = class_names[{value}->class->id];")
400 redef fun array_instance
(array
, elttype
)
402 var nclass
= self.get_class
("NativeArray")
403 elttype
= self.anchor
(elttype
)
404 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
405 var res
= self.init_instance
(arraytype
)
406 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
407 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
409 self.add
("{nat} = NEW_{nclass.c_name}({array.length});")
410 for i
in [0..array
.length
[ do
411 var r
= self.autobox
(array
[i
], self.object_type
)
412 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
414 var length
= self.int_instance
(array
.length
)
415 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
416 self.check_init_instance
(res
, arraytype
)
421 redef fun calloc_array
(ret_type
, arguments
)
423 var ret
= ret_type
.as(MClassType)
424 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type
))