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
.new_visitor
42 v
.add_decl
("#include <stdlib.h>")
43 v
.add_decl
("#include <stdio.h>")
44 v
.add_decl
("#include <string.h>")
45 v
.add_decl
("#include <gc/gc.h>")
46 v
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
47 v
.add_decl
("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
48 v
.add_decl
("struct class \{ int id; int color; struct type_table *type_table; nitmethod_t vft[1]; \}; /* general C type representing a Nit class. */")
49 v
.add_decl
("struct type_table \{ int size; int table[1]; \}; /* colorized type table. */")
50 v
.add_decl
("typedef struct \{ struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
51 v
.add_decl
("extern const char const * class_names[];")
53 # Declare global instances
54 v
.add_decl
("extern int glob_argc;")
55 v
.add_decl
("extern char **glob_argv;")
56 v
.add_decl
("extern val *glob_sys;")
58 # The main function of the C
59 compiler
.compile_main_function
61 # compile class structures
62 for m
in mainmodule
.in_importation
.greaters
do
63 for mclass
in m
.intro_mclasses
do
64 compiler
.compile_class_to_c
(mclass
)
69 for m
in mainmodule
.in_importation
.greaters
do
70 compiler
.compile_module_to_c
(m
)
73 write_and_make
(compiler
)
77 class SeparateErasureCompiler
78 super SeparateCompiler
80 private var class_ids
: HashMap[MClass, Int] = new HashMap[MClass, Int]
81 private var class_tables
: nullable Map[MClass, Array[nullable MClass]] = null
83 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
85 var class_coloring
= new ClassColoring(mainmodule
)
86 self.class_colors
= class_coloring
.colorize
(mmbuilder
.model
.mclasses
)
87 self.class_tables
= class_coloring
.build_type_tables
(mmbuilder
.model
.mclasses
, class_colors
)
90 var method_coloring
= new MethodColoring(class_coloring
)
91 self.method_colors
= method_coloring
.colorize
92 self.method_tables
= method_coloring
.build_property_tables
94 # attributes coloration
95 var attribute_coloring
= new AttributeColoring(class_coloring
)
96 self.attr_colors
= attribute_coloring
.colorize
97 self.attr_tables
= attribute_coloring
.build_property_tables
100 for mclass
in class_colors
.keys
do
101 self.class_ids
[mclass
] = self.class_ids
.length
104 # for the class_name and output_class_name methods
105 self.compile_class_names
108 redef fun compile_class_names
do
109 # Build type names table
110 var type_array
= new Array[nullable MClass]
111 for t
, i
in class_ids
do
112 if i
>= type_array
.length
then
118 var v
= self.new_visitor
119 v
.add
("const char const * class_names[] = \{")
120 for t
in type_array
do
122 v
.add
("NULL, /* empty */")
130 redef fun compile_class_to_c
(mclass
: MClass)
132 var mtype
= mclass
.intro
.bound_mtype
133 var c_name
= mclass
.c_name
135 var vft
= self.method_tables
[mclass
]
136 var attrs
= self.attr_tables
[mclass
]
137 var class_table
= self.class_tables
[mclass
]
138 var v
= self.new_visitor
140 v
.add_decl
("/* runtime class {c_name} */")
141 var idnum
= classids
.length
142 var idname
= "ID_" + c_name
143 self.classids
[mtype
] = idname
144 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
146 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
147 self.header
.add_decl
("struct class_{c_name} \{")
148 self.header
.add_decl
("int id;")
149 self.header
.add_decl
("int color;")
150 self.header
.add_decl
("struct type_table *type_table;")
151 self.header
.add_decl
("nitmethod_t vft[{vft.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.class_colors[mclass]},")
158 v
.add_decl
("(struct type_table*) &type_table_{c_name},")
160 for i
in [0 .. vft
.length
[ do
161 var mpropdef
= vft
[i
]
162 if mpropdef
== null then
163 v
.add_decl
("NULL, /* empty */")
165 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
166 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
168 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
175 # Build class type table
176 self.header
.add_decl
("extern const struct type_table_{c_name} type_table_{c_name};")
177 self.header
.add_decl
("struct type_table_{c_name} \{")
178 self.header
.add_decl
("int size;")
179 self.header
.add_decl
("int table[{class_table.length}];")
180 self.header
.add_decl
("\};")
182 v
.add_decl
("const struct type_table_{c_name} type_table_{c_name} = \{")
183 v
.add_decl
("{class_table.length},")
185 for msuper
in class_table
do
186 if msuper
== null then
187 v
.add_decl
("-1, /* empty */")
189 v
.add_decl
("{self.class_ids[msuper]}, /* {msuper} */")
195 #Build instance struct
196 if mtype
.ctype
!= "val*" then
197 self.header
.add_decl
("struct instance_{c_name} \{")
198 self.header
.add_decl
("const struct class *class;")
199 self.header
.add_decl
("{mtype.ctype} value;")
200 self.header
.add_decl
("\};")
202 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
203 v
.add_decl
("/* allocate {mtype} */")
204 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
205 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
206 v
.add
("res->class = (struct class*) &class_{c_name};")
207 v
.add
("res->value = value;")
208 v
.add
("return (val*)res;")
213 var is_native_array
= mclass
.name
== "NativeArray"
216 if is_native_array
then
222 #Build instance struct
223 #extern const struct instance_array__NativeArray instance_array__NativeArray;
224 self.header
.add_decl
("struct instance_{c_name} \{")
225 self.header
.add_decl
("const struct class *class;")
226 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
227 if is_native_array
then
228 # NativeArrays are just a instance header followed by an array of values
229 self.header
.add_decl
("val* values[0];")
231 self.header
.add_decl
("\};")
234 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
235 v
.add_decl
("/* allocate {mtype} */")
236 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
237 var res
= v
.new_named_var
(mtype
, "self")
239 if is_native_array
then
240 var mtype_elt
= mtype
.arguments
.first
241 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
243 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
245 v
.add
("{res}->class = (struct class*) &class_{c_name};")
247 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
249 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
250 for npropdef
in n
.n_propdefs
do
251 if npropdef
isa AAttrPropdef then
252 npropdef
.init_expr
(v
, res
)
256 v
.add
("return {res};")
260 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
263 class SeparateErasureCompilerVisitor
264 super SeparateCompilerVisitor
266 redef fun init_instance
(mtype
)
268 return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
271 redef fun type_test
(value
, mtype
)
273 var res
= self.new_var
(bool_type
)
275 var cltype
= self.get_name
("cltype")
276 self.add_decl
("int {cltype};")
277 var idtype
= self.get_name
("idtype")
278 self.add_decl
("int {idtype};")
280 var maybe_null
= false
281 if mtype
isa MNullableType then
285 if mtype
isa MParameterType then
286 # Here we get the bound of the the formal type (eh, erasure...)
287 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
288 if mtype
isa MNullableType then
293 if mtype
isa MVirtualType then
294 # FIXME virtual types should not be erased but got from the class table of the current receiver (self.frame.arguments.first)
295 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, true)
296 if mtype
isa MNullableType then
302 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
303 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
308 if value
.mtype
.ctype
== "val*" then
309 type_table
= "{value}->class->type_table"
311 var mclass
= value
.mtype
.as(MClassType).mclass
312 type_table
= "type_table_{mclass.c_name}"
315 if mtype
isa MClassType then
316 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
317 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
319 self.debug
("type_test({value.inspect}, {mtype})")
325 s
= "{value} == NULL ||"
327 s
= "{value} != NULL &&"
329 # check color is in table
330 self.add
("if({value} != NULL && {cltype} >= {type_table}->size) \{")
331 self.add
("{res} = 0;")
332 self.add
("\} else \{")
333 self.add
("{res} = {s} {type_table}->table[{cltype}] == {idtype};")
339 redef fun class_name_string
(value1
)
341 var res
= self.get_name
("var_class_name")
342 self.add_decl
("const char* {res} = class_names[self->class->id];")
346 redef fun array_instance
(array
, elttype
)
348 var nclass
= self.get_class
("NativeArray")
349 elttype
= self.anchor
(elttype
)
350 var arraytype
= self.get_class
("Array").get_mtype
([elttype
])
351 var res
= self.init_instance
(arraytype
)
352 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
353 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
355 self.add
("{nat} = NEW_{nclass.c_name}({array.length});")
356 for i
in [0..array
.length
[ do
357 var r
= self.autobox
(array
[i
], self.object_type
)
358 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
360 var length
= self.int_instance
(array
.length
)
361 self.send
(self.get_property
("with_native", arraytype
), [res
, nat
, length
])
362 self.check_init_instance
(res
)
367 redef fun calloc_array
(ret_type
, arguments
)
369 var ret
= ret_type
.as(MClassType)
370 self.ret
(self.new_expr
("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type
))