nitg-s: rename SeparateCompiler:do_global_type_coloring to do_type_coloring
[nit.git] / src / separate_erasure_compiler.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Separate compilation of a Nit program with generic type erasure
16 module separate_erasure_compiler
17
18
19 import separate_compiler
20 intrude import coloring
21
22 redef class ToolContext
23 # --erasure
24 var opt_erasure: OptionBool = new OptionBool("Erase generic types", "--erasure")
25
26 redef init
27 do
28 super
29 self.option_context.add_option(self.opt_erasure)
30 end
31 end
32
33 redef class ModelBuilder
34 fun run_separate_erasure_compiler(mainmodule: MModule, runtime_type_analysis: RapidTypeAnalysis)
35 do
36 var time0 = get_time
37 self.toolcontext.info("*** COMPILING TO C ***", 1)
38
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[];")
53
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;")
58
59 # The main function of the C
60 compiler.compile_main_function
61
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)
66 end
67 end
68
69 # compile methods
70 for m in mainmodule.in_importation.greaters do
71 compiler.compile_module_to_c(m)
72 end
73
74 write_and_make(compiler)
75 end
76 end
77
78 class SeparateErasureCompiler
79 super SeparateCompiler
80
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]]
85
86 init(mainmodule: MModule, runtime_type_analysis: RapidTypeAnalysis, mmbuilder: ModelBuilder) do
87 self.header = self.new_visitor
88 # classes coloration
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)
92
93 # methods coloration
94 var method_coloring = new MethodColoring(class_coloring)
95 self.method_colors = method_coloring.colorize
96 self.method_tables = method_coloring.build_property_tables
97
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
102
103 # vt coloration
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
107
108 # set type unique id
109 for mclass in class_colors.keys do
110 self.class_ids[mclass] = self.class_ids.length
111 end
112
113 # for the class_name and output_class_name methods
114 self.compile_class_names
115 self.compile_box_kinds
116 end
117
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
123 type_array[i] = null
124 end
125 type_array[i] = t
126 end
127
128 var v = self.new_visitor
129 v.add("const char const * class_names[] = \{")
130 for t in type_array do
131 if t == null then
132 v.add("NULL, /* empty */")
133 else
134 v.add("\"{t}\",")
135 end
136 end
137 v.add("\};")
138 end
139
140 redef fun compile_class_to_c(mclass: MClass)
141 do
142 var mtype = mclass.intro.bound_mtype
143 var c_name = mclass.c_name
144
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
149
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} */")
155
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("\};")
165
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("\};")
171
172 # Build class vft
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},")
179 v.add_decl("\{")
180 for i in [0 .. vft.length[ do
181 var mpropdef = vft[i]
182 if mpropdef == null then
183 v.add_decl("NULL, /* empty */")
184 else
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} */")
187 else
188 v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
189 end
190 end
191 end
192 v.add_decl("\}")
193 v.add_decl("\};")
194
195 build_class_vts_table(mclass, v.as(SeparateErasureCompilerVisitor))
196
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("\};")
203
204 v.add_decl("const struct type_table_{c_name} type_table_{c_name} = \{")
205 v.add_decl("{class_table.length},")
206 v.add_decl("\{")
207 for msuper in class_table do
208 if msuper == null then
209 v.add_decl("-1, /* empty */")
210 else
211 v.add_decl("{self.class_ids[msuper]}, /* {msuper} */")
212 end
213 end
214 v.add_decl("\}")
215 v.add_decl("\};")
216
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("\};")
223
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;")
231 v.add("\}")
232 return
233 end
234
235 var is_native_array = mclass.name == "NativeArray"
236
237 var sig
238 if is_native_array then
239 sig = "int length"
240 else
241 sig = ""
242 end
243
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];")
252 end
253 self.header.add_decl("\};")
254
255
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")
260 res.is_exact = true
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}));")
264 else
265 v.add("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
266 end
267 v.add("{res}->class = (struct class*) &class_{c_name};")
268
269 self.generate_init_attr(v, res, mtype)
270 v.add("return {res};")
271 v.add("\}")
272
273 generate_check_init_instance(mtype)
274 end
275
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} = \{")
278 v.add_decl("\{")
279
280 for vt in self.class_vts_tables[mclass] do
281 if vt == null then
282 v.add_decl("NULL, /* empty */")
283 else
284 var is_null = 0
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)
288 is_null = 1
289 end
290 v.add_decl("\{{is_null}, (struct class*)&class_{bound.as(MClassType).mclass.c_name}\}, /* {vt} */")
291 end
292 end
293 v.add_decl("\},")
294 v.add_decl("\};")
295 end
296
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"
300 abort
301 end
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)
306 else
307 return mtype
308 end
309 end
310
311 redef fun new_visitor do return new SeparateErasureCompilerVisitor(self)
312 end
313
314 class SeparateErasureCompilerVisitor
315 super SeparateCompilerVisitor
316
317 redef fun init_instance(mtype)
318 do
319 return self.new_expr("NEW_{mtype.mclass.c_name}()", mtype)
320 end
321
322 redef fun type_test(value, mtype)
323 do
324 self.add("/* type test for {value.inspect} isa {mtype} */")
325
326 var res = self.new_var(bool_type)
327
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};")
336
337 var maybe_null = 0
338 if mtype isa MNullableType then
339 mtype = mtype.mtype
340 maybe_null = 1
341 self.add("{is_nullable} = 1;")
342 end
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
347 mtype = mtype.mtype
348 maybe_null = 1
349 self.add("{is_nullable} = 1;")
350 end
351 end
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
357 # maybe_null = true
358 #end
359 end
360
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}*/")
363 return res
364 end
365
366 var class_ptr
367 var type_table
368 if value.mtype.ctype == "val*" then
369 class_ptr = "{value}->class->"
370 self.add("{is_null} = {value} == NULL;")
371 else
372 var mclass = value.mtype.as(MClassType).mclass
373 class_ptr = "class_{mclass.c_name}."
374 self.add("{is_null} = 0;")
375 end
376
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;")
382 end
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;")
391 end
392 else
393 self.debug("type_test({value.inspect}, {mtype})")
394 abort
395 end
396
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};")
405 self.add("\}")
406 self.add("\}")
407
408 return res
409 end
410
411 redef fun class_name_string(value)
412 do
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];")
416 return res
417 end
418
419 redef fun array_instance(array, elttype)
420 do
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]))
427 nat.is_exact = true
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};")
432 end
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)
436 self.add("\}")
437 return res
438 end
439
440 redef fun calloc_array(ret_type, arguments)
441 do
442 var ret = ret_type.as(MClassType)
443 self.ret(self.new_expr("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type))
444 end
445 end