9c675ec51cbbe34acf6fef5ef93484267f17a13e
[nit.git] / src / separate_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
16 module separate_compiler
17
18 import abstract_compiler
19 intrude import coloring
20 import rapid_type_analysis
21
22 # Add separate compiler specific options
23 redef class ToolContext
24 # --separate
25 var opt_separate: OptionBool = new OptionBool("Use separate compilation", "--separate")
26 # --no-inline-intern
27 var opt_no_inline_intern: OptionBool = new OptionBool("Do not inline call to intern methods", "--no-inline-intern")
28 # --no-union-attribute
29 var opt_no_union_attribute: OptionBool = new OptionBool("Put primitive attibutes in a box instead of an union", "--no-union-attribute")
30 # --no-shortcut-equate
31 var opt_no_shortcut_equate: OptionBool = new OptionBool("Always call == in a polymorphic way", "--no-shortcut-equal")
32 # --inline-coloring-numbers
33 var opt_inline_coloring_numbers: OptionBool = new OptionBool("Inline colors and ids", "--inline-coloring-numbers")
34 # --use-naive-coloring
35 var opt_bm_typing: OptionBool = new OptionBool("Colorize items incrementaly, used to simulate binary matrix typing", "--bm-typing")
36 # --use-mod-perfect-hashing
37 var opt_phmod_typing: OptionBool = new OptionBool("Replace coloration by perfect hashing (with mod operator)", "--phmod-typing")
38 # --use-and-perfect-hashing
39 var opt_phand_typing: OptionBool = new OptionBool("Replace coloration by perfect hashing (with and operator)", "--phand-typing")
40 # --generic-resolution-tree
41 var opt_typing_table_metrics: OptionBool = new OptionBool("Enable static size measuring of tables used for typing and resolution", "--typing-table-metrics")
42
43 redef init
44 do
45 super
46 self.option_context.add_option(self.opt_separate)
47 self.option_context.add_option(self.opt_no_inline_intern)
48 self.option_context.add_option(self.opt_no_union_attribute)
49 self.option_context.add_option(self.opt_no_shortcut_equate)
50 self.option_context.add_option(self.opt_inline_coloring_numbers)
51 self.option_context.add_option(self.opt_bm_typing)
52 self.option_context.add_option(self.opt_phmod_typing)
53 self.option_context.add_option(self.opt_phand_typing)
54 self.option_context.add_option(self.opt_typing_table_metrics)
55 end
56 end
57
58 redef class ModelBuilder
59 fun run_separate_compiler(mainmodule: MModule, runtime_type_analysis: RapidTypeAnalysis)
60 do
61 var time0 = get_time
62 self.toolcontext.info("*** COMPILING TO C ***", 1)
63
64 var compiler = new SeparateCompiler(mainmodule, self, runtime_type_analysis)
65 compiler.compile_header
66
67 # compile class structures
68 for m in mainmodule.in_importation.greaters do
69 for mclass in m.intro_mclasses do
70 compiler.compile_class_to_c(mclass)
71 end
72 end
73
74 # The main function of the C
75 compiler.new_file
76 compiler.compile_main_function
77
78 # compile methods
79 for m in mainmodule.in_importation.greaters do
80 compiler.new_file
81 compiler.compile_module_to_c(m)
82 end
83
84 # compile live & cast type structures
85 compiler.new_file
86 var mtypes = compiler.do_type_coloring
87 for t in mtypes do
88 compiler.compile_type_to_c(t)
89 end
90
91 compiler.display_stats
92
93 write_and_make(compiler)
94 end
95 end
96
97 # Singleton that store the knowledge about the separate compilation process
98 class SeparateCompiler
99 super AbstractCompiler
100
101 # Cache for classid
102 protected var classids: HashMap[MClassType, String] = new HashMap[MClassType, String]
103
104 # The result of the RTA (used to know live types and methods)
105 var runtime_type_analysis: RapidTypeAnalysis
106
107 private var undead_types: Set[MType] = new HashSet[MType]
108 private var partial_types: Set[MType] = new HashSet[MType]
109
110 private var type_layout_builder: TypingLayoutBuilder[MType]
111 private var type_layout: nullable TypingLayout[MType]
112 private var type_tables: nullable Map[MType, Array[nullable MType]] = null
113
114 private var live_unanchored_types: Map[MClassDef, Set[MType]] = new HashMap[MClassDef, HashSet[MType]]
115
116 private var unanchored_types_colors: nullable Map[MType, Int]
117 private var unanchored_types_tables: nullable Map[MClassType, Array[nullable MType]]
118 private var unanchored_types_masks: nullable Map[MClassType, Int]
119
120 protected var method_colors: Map[MMethod, Int]
121 protected var method_tables: Map[MClass, Array[nullable MPropDef]]
122
123 protected var attr_colors: Map[MAttribute, Int]
124 protected var attr_tables: Map[MClass, Array[nullable MPropDef]]
125
126 protected var vt_colors: Map[MVirtualTypeProp, Int]
127 protected var vt_tables: Map[MClass, Array[nullable MPropDef]]
128 protected var vt_masks: nullable Map[MClass, Int]
129
130 init(mainmodule: MModule, mmbuilder: ModelBuilder, runtime_type_analysis: RapidTypeAnalysis) do
131 super
132 self.header = new_visitor
133 self.init_layout_builders
134 self.runtime_type_analysis = runtime_type_analysis
135 self.do_property_coloring
136 self.compile_box_kinds
137 end
138
139 protected fun init_layout_builders do
140 # Typing Layout
141 if modelbuilder.toolcontext.opt_bm_typing.value then
142 self.type_layout_builder = new BMTypeLayoutBuilder(self.mainmodule)
143 else if modelbuilder.toolcontext.opt_phmod_typing.value then
144 self.type_layout_builder = new PHTypeLayoutBuilder(self.mainmodule, new PHModOperator)
145 self.header.add_decl("#define HASH(mask, id) ((mask)%(id))")
146 else if modelbuilder.toolcontext.opt_phand_typing.value then
147 self.type_layout_builder = new PHTypeLayoutBuilder(self.mainmodule, new PHAndOperator)
148 self.header.add_decl("#define HASH(mask, id) ((mask)&(id))")
149 else
150 self.type_layout_builder = new CLTypeLayoutBuilder(self.mainmodule)
151 end
152 end
153
154 redef fun compile_header_structs do
155 self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
156 self.compile_header_attribute_structs
157 self.header.add_decl("struct class \{ int box_kind; nitmethod_t vft[1]; \}; /* general C type representing a Nit class. */")
158
159 # With unanchored_table, all live type resolution are stored in a big table: unanchored_table
160 self.header.add_decl("struct type \{ int id; const char *name; int color; short int is_nullable; struct types *unanchored_table; int table_size; int type_table[1]; \}; /* general C type representing a Nit type. */")
161
162 if modelbuilder.toolcontext.opt_phmod_typing.value or modelbuilder.toolcontext.opt_phand_typing.value then
163 self.header.add_decl("struct types \{ int mask; struct type *types[1]; \}; /* a list types (used for vts, fts and unanchored lists). */")
164 else
165 self.header.add_decl("struct types \{ struct type *types[1]; \}; /* a list types (used for vts, fts and unanchored lists). */")
166 end
167
168 self.header.add_decl("typedef struct \{ struct type *type; struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
169 end
170
171 fun compile_header_attribute_structs
172 do
173 if modelbuilder.toolcontext.opt_no_union_attribute.value then
174 self.header.add_decl("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
175 else
176 self.header.add_decl("typedef union \{")
177 self.header.add_decl("void* val;")
178 for c, v in self.box_kinds do
179 var t = c.mclass_type
180 self.header.add_decl("{t.ctype} {t.ctypename};")
181 end
182 self.header.add_decl("\} nitattribute_t; /* general C type representing a Nit attribute. */")
183 end
184 end
185
186 fun compile_box_kinds
187 do
188 # Collect all bas box class
189 # FIXME: this is not completely fine with a separate compilation scheme
190 for classname in ["Int", "Bool", "Char", "Float", "NativeString", "Pointer"] do
191 var classes = self.mainmodule.model.get_mclasses_by_name(classname)
192 if classes == null then continue
193 assert classes.length == 1 else print classes.join(", ")
194 self.box_kinds[classes.first] = self.box_kinds.length + 1
195 end
196 end
197
198 var box_kinds = new HashMap[MClass, Int]
199
200 fun box_kind_of(mclass: MClass): Int
201 do
202 if mclass.mclass_type.ctype == "val*" then
203 return 0
204 else if mclass.kind == extern_kind then
205 return self.box_kinds[self.mainmodule.get_primitive_class("Pointer")]
206 else
207 return self.box_kinds[mclass]
208 end
209
210 end
211
212 fun compile_color_consts(colors: Map[Object, Int]) do
213 for m, c in colors do
214 if m isa MProperty then
215 if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
216 self.header.add_decl("#define {m.const_color} {c}")
217 else
218 self.header.add_decl("extern const int {m.const_color};")
219 self.header.add("const int {m.const_color} = {c};")
220 end
221 else if m isa MType then
222 if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
223 self.header.add_decl("#define {m.const_color} {c}")
224 else
225 self.header.add_decl("extern const int {m.const_color};")
226 self.header.add("const int {m.const_color} = {c};")
227 end
228 end
229 end
230 end
231
232 # colorize classe properties
233 fun do_property_coloring do
234
235 # classes coloration
236 var mclasses = new HashSet[MClass].from(modelbuilder.model.mclasses)
237 var class_coloring = new ClassColoring(mainmodule)
238 class_coloring.colorize(mclasses)
239
240 # methods coloration
241 var method_coloring = new MethodColoring(mainmodule, class_coloring)
242 self.method_colors = method_coloring.colorize
243 self.method_tables = build_property_tables(method_coloring, class_coloring)
244 self.compile_color_consts(self.method_colors)
245
246 # attributes coloration
247 var attribute_coloring = new AttributeColoring(mainmodule, class_coloring)
248 self.attr_colors = attribute_coloring.colorize
249 self.attr_tables = build_property_tables(method_coloring, class_coloring)
250 self.compile_color_consts(self.attr_colors)
251
252 # vt coloration
253 if modelbuilder.toolcontext.opt_bm_typing.value then
254 var vt_coloring = new NaiveVTColoring(mainmodule, class_coloring)
255 self.vt_colors = vt_coloring.colorize
256 self.vt_tables = build_property_tables(vt_coloring, class_coloring)
257 else if modelbuilder.toolcontext.opt_phmod_typing.value then
258 var vt_coloring = new VTModPerfectHashing(mainmodule, class_coloring)
259 self.vt_colors = vt_coloring.colorize
260 self.vt_masks = vt_coloring.compute_masks
261 self.vt_tables = build_property_tables(vt_coloring, class_coloring)
262 else if modelbuilder.toolcontext.opt_phand_typing.value then
263 var vt_coloring = new VTAndPerfectHashing(mainmodule, class_coloring)
264 self.vt_colors = vt_coloring.colorize
265 self.vt_masks = vt_coloring.compute_masks
266 self.vt_tables = build_property_tables(vt_coloring, class_coloring)
267 else
268 var vt_coloring = new VTColoring(mainmodule, class_coloring)
269 self.vt_colors = vt_coloring.colorize
270 self.vt_tables = build_property_tables(vt_coloring, class_coloring)
271 end
272 self.compile_color_consts(self.vt_colors)
273 end
274
275 fun build_property_tables(prop_coloring: PropertyColoring, class_coloring: ClassColoring): Map[MClass, Array[nullable MPropDef]] do
276 var tables = new HashMap[MClass, Array[nullable MPropDef]]
277 var mclasses = class_coloring.coloration_result.keys
278 for mclass in mclasses do
279 var table = new Array[nullable MPropDef]
280 # first, fill table from parents by reverse linearization order
281 var parents = class_coloring.mmodule.super_mclasses(mclass)
282 var lin = class_coloring.reverse_linearize(parents)
283 for parent in lin do
284 for mproperty in prop_coloring.properties(parent) do
285 var color = prop_coloring.coloration_result[mproperty]
286 if table.length <= color then
287 for i in [table.length .. color[ do
288 table[i] = null
289 end
290 end
291 for mpropdef in mproperty.mpropdefs do
292 if mpropdef.mclassdef.mclass == parent then
293 table[color] = mpropdef
294 end
295 end
296 end
297 end
298
299 # then override with local properties
300 for mproperty in prop_coloring.properties(mclass) do
301 var color = prop_coloring.coloration_result[mproperty]
302 if table.length <= color then
303 for i in [table.length .. color[ do
304 table[i] = null
305 end
306 end
307 for mpropdef in mproperty.mpropdefs do
308 if mpropdef.mclassdef.mclass == mclass then
309 table[color] = mpropdef
310 end
311 end
312 end
313 tables[mclass] = table
314 end
315 return tables
316 end
317
318 # colorize live types of the program
319 private fun do_type_coloring: Set[MType] do
320 var mtypes = new HashSet[MType]
321 mtypes.add_all(self.runtime_type_analysis.live_types)
322 mtypes.add_all(self.runtime_type_analysis.live_cast_types)
323 mtypes.add_all(self.undead_types)
324 for c in self.box_kinds.keys do
325 mtypes.add(c.mclass_type)
326 end
327
328 for mtype in mtypes do
329 retieve_live_partial_types(mtype)
330 end
331 mtypes.add_all(self.partial_types)
332
333 # VT and FT are stored with other unresolved types in the big unanchored_tables
334 self.compile_unanchored_tables(mtypes)
335
336 # colorize types
337 self.type_layout = self.type_layout_builder.build_layout(mtypes)
338 self.type_tables = self.build_type_tables(mtypes)
339 return mtypes
340 end
341
342 # Build type tables
343 fun build_type_tables(mtypes: Set[MType]): Map[MType, Array[nullable MType]] do
344 var tables = new HashMap[MType, Array[nullable MType]]
345 var layout = self.type_layout
346 for mtype in mtypes do
347 var table = new Array[nullable MType]
348 var supers = new HashSet[MType]
349 supers.add_all(self.mainmodule.super_mtypes(mtype, mtypes))
350 supers.add(mtype)
351 for sup in supers do
352 var color: Int
353 if layout isa PHTypingLayout[MType] then
354 color = layout.hashes[mtype][sup]
355 else
356 color = layout.pos[sup]
357 end
358 if table.length <= color then
359 for i in [table.length .. color[ do
360 table[i] = null
361 end
362 end
363 table[color] = sup
364 end
365 tables[mtype] = table
366 end
367 return tables
368 end
369
370 protected fun compile_unanchored_tables(mtypes: Set[MType]) do
371 # Unanchored_tables is used to perform a type resolution at runtime in O(1)
372
373 # During the visit of the body of classes, live_unanchored_types are collected
374 # and associated to
375 # Collect all live_unanchored_types (visited in the body of classes)
376
377 # Determinate fo each livetype what are its possible requested anchored types
378 var mtype2unanchored = new HashMap[MClassType, Set[MType]]
379 for mtype in self.runtime_type_analysis.live_types do
380 var set = new HashSet[MType]
381 for cd in mtype.collect_mclassdefs(self.mainmodule) do
382 if self.live_unanchored_types.has_key(cd) then
383 set.add_all(self.live_unanchored_types[cd])
384 end
385 end
386 mtype2unanchored[mtype] = set
387 end
388
389 # Compute the table layout with the prefered method
390 if modelbuilder.toolcontext.opt_bm_typing.value then
391 var unanchored_type_coloring = new NaiveUnanchoredTypeColoring
392 self.unanchored_types_colors = unanchored_type_coloring.colorize(mtype2unanchored)
393 self.unanchored_types_tables = unanchored_type_coloring.build_tables(mtype2unanchored)
394 else if modelbuilder.toolcontext.opt_phmod_typing.value then
395 var unanchored_type_coloring = new UnanchoredTypeModPerfectHashing
396 self.unanchored_types_colors = unanchored_type_coloring.colorize(mtype2unanchored)
397 self.unanchored_types_masks = unanchored_type_coloring.compute_masks(mtype2unanchored)
398 self.unanchored_types_tables = unanchored_type_coloring.build_tables(mtype2unanchored)
399 else if modelbuilder.toolcontext.opt_phand_typing.value then
400 var unanchored_type_coloring = new UnanchoredTypeAndPerfectHashing
401 self.unanchored_types_colors = unanchored_type_coloring.colorize(mtype2unanchored)
402 self.unanchored_types_masks = unanchored_type_coloring.compute_masks(mtype2unanchored)
403 self.unanchored_types_tables = unanchored_type_coloring.build_tables(mtype2unanchored)
404 else
405 var unanchored_type_coloring = new UnanchoredTypeColoring
406 self.unanchored_types_colors = unanchored_type_coloring.colorize(mtype2unanchored)
407 self.unanchored_types_tables = unanchored_type_coloring.build_tables(mtype2unanchored)
408 end
409
410 # Compile a C constant for each collected unanchored type.
411 # Either to a color, or to -1 if the unanchored type is dead (no live receiver can require it)
412 var all_unanchored = new HashSet[MType]
413 for t in self.live_unanchored_types.values do
414 all_unanchored.add_all(t)
415 end
416 var all_unanchored_types_colors = new HashMap[MType, Int]
417 for t in all_unanchored do
418 if unanchored_types_colors.has_key(t) then
419 all_unanchored_types_colors[t] = unanchored_types_colors[t]
420 else
421 all_unanchored_types_colors[t] = -1
422 end
423 end
424 self.compile_color_consts(all_unanchored_types_colors)
425
426 #print "tables"
427 #for k, v in unanchored_types_tables.as(not null) do
428 # print "{k}: {v.join(", ")}"
429 #end
430 #print ""
431 end
432
433 fun retieve_live_partial_types(mtype: MType) do
434 # add formal types arguments to mtypes
435 if mtype isa MGenericType then
436 for ft in mtype.arguments do
437 if ft.need_anchor then
438 print("Why do we need anchor here ?")
439 abort
440 end
441 self.partial_types.add(ft)
442 retieve_live_partial_types(ft)
443 end
444 end
445 var mclass_type: MClassType
446 if mtype isa MNullableType then
447 mclass_type = mtype.mtype.as(MClassType)
448 else
449 mclass_type = mtype.as(MClassType)
450 end
451
452 # add virtual types to mtypes
453 for vt in self.vt_tables[mclass_type.mclass] do
454 if vt != null then
455 var anchored = vt.as(MVirtualTypeDef).bound.anchor_to(self.mainmodule, mclass_type)
456 self.partial_types.add(anchored)
457 end
458 end
459 end
460
461 # Separately compile all the method definitions of the module
462 fun compile_module_to_c(mmodule: MModule)
463 do
464 var old_module = self.mainmodule
465 self.mainmodule = mmodule
466 for cd in mmodule.mclassdefs do
467 for pd in cd.mpropdefs do
468 if not pd isa MMethodDef then continue
469 #print "compile {pd} @ {cd} @ {mmodule}"
470 var r = new SeparateRuntimeFunction(pd)
471 r.compile_to_c(self)
472 if true or cd.bound_mtype.ctype != "val*" then
473 var r2 = new VirtualRuntimeFunction(pd)
474 r2.compile_to_c(self)
475 end
476 end
477 end
478 self.mainmodule = old_module
479 end
480
481 # Globaly compile the type structure of a live type
482 fun compile_type_to_c(mtype: MType)
483 do
484 var c_name = mtype.c_name
485 var v = new SeparateCompilerVisitor(self)
486 v.add_decl("/* runtime type {mtype} */")
487
488 # extern const struct type_X
489 self.header.add_decl("extern const struct type_{c_name} type_{c_name};")
490 self.header.add_decl("struct type_{c_name} \{")
491 self.header.add_decl("int id;")
492 self.header.add_decl("const char *name;")
493 self.header.add_decl("int color;")
494 self.header.add_decl("short int is_nullable;")
495 self.header.add_decl("const struct types *unanchored_table;")
496 self.header.add_decl("int table_size;")
497 self.header.add_decl("int type_table[{self.type_tables[mtype].length}];")
498 self.header.add_decl("\};")
499
500 # const struct type_X
501 v.add_decl("const struct type_{c_name} type_{c_name} = \{")
502 v.add_decl("{self.type_layout.ids[mtype]},")
503 v.add_decl("\"{mtype}\", /* class_name_string */")
504 var layout = self.type_layout
505 if layout isa PHTypingLayout[MType] then
506 v.add_decl("{layout.masks[mtype]},")
507 else
508 v.add_decl("{layout.pos[mtype]},")
509 end
510 if mtype isa MNullableType then
511 v.add_decl("1,")
512 else
513 v.add_decl("0,")
514 end
515 if compile_type_unanchored_table(mtype) then
516 v.add_decl("(struct types*) &unanchored_table_{c_name},")
517 else
518 v.add_decl("NULL,")
519 end
520 v.add_decl("{self.type_tables[mtype].length},")
521 v.add_decl("\{")
522 for stype in self.type_tables[mtype] do
523 if stype == null then
524 v.add_decl("-1, /* empty */")
525 else
526 v.add_decl("{self.type_layout.ids[stype]}, /* {stype} */")
527 end
528 end
529 v.add_decl("\},")
530 v.add_decl("\};")
531 end
532
533 fun compile_type_unanchored_table(mtype: MType): Bool do
534
535 var mclass_type: MClassType
536 if mtype isa MNullableType then
537 mclass_type = mtype.mtype.as(MClassType)
538 else
539 mclass_type = mtype.as(MClassType)
540 end
541 if not self.unanchored_types_tables.has_key(mclass_type) then return false
542
543 # extern const struct unanchored_table_X unanchored_table_X
544 self.header.add_decl("extern const struct unanchored_table_{mtype.c_name} unanchored_table_{mtype.c_name};")
545
546 self.header.add_decl("struct unanchored_table_{mtype.c_name} \{")
547 if modelbuilder.toolcontext.opt_phmod_typing.value or modelbuilder.toolcontext.opt_phand_typing.value then
548 self.header.add_decl("int mask;")
549 end
550 self.header.add_decl("struct type *types[{self.unanchored_types_tables[mclass_type].length}];")
551 self.header.add_decl("\};")
552
553 # const struct fts_table_X fts_table_X
554 var v = new_visitor
555 v.add_decl("const struct unanchored_table_{mtype.c_name} unanchored_table_{mtype.c_name} = \{")
556 if modelbuilder.toolcontext.opt_phmod_typing.value or modelbuilder.toolcontext.opt_phand_typing.value then
557 v.add_decl("{self.unanchored_types_masks[mclass_type]},")
558 end
559 v.add_decl("\{")
560 for t in self.unanchored_types_tables[mclass_type] do
561 if t == null then
562 v.add_decl("NULL, /* empty */")
563 else
564 # The table stores the result of the type resolution
565 # Therefore, for a receiver `mclass_type`, and a unresolved type `t`
566 # the value stored is tv.
567 var tv = t.resolve_for(mclass_type, mclass_type, self.mainmodule, true)
568 # FIXME: What typeids means here? How can a tv not be live?
569 if self.type_layout.ids.has_key(tv) then
570 v.add_decl("(struct type*)&type_{tv.c_name}, /* {t}: {tv} */")
571 else
572 v.add_decl("NULL, /* empty ({t}: {tv} not a live type) */")
573 end
574 end
575 end
576 v.add_decl("\},")
577 v.add_decl("\};")
578 return true
579 end
580
581 # Globally compile the table of the class mclass
582 # In a link-time optimisation compiler, tables are globally computed
583 # In a true separate compiler (a with dynamic loading) you cannot do this unfortnally
584 fun compile_class_to_c(mclass: MClass)
585 do
586 var mtype = mclass.intro.bound_mtype
587 var c_name = mclass.c_name
588
589 var vft = self.method_tables[mclass]
590 var attrs = self.attr_tables[mclass]
591 var v = new_visitor
592
593 v.add_decl("/* runtime class {c_name} */")
594 var idnum = classids.length
595 var idname = "ID_" + c_name
596 self.classids[mtype] = idname
597 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
598
599 self.header.add_decl("struct class_{c_name} \{")
600 self.header.add_decl("int box_kind;")
601 self.header.add_decl("nitmethod_t vft[{vft.length}];")
602 self.header.add_decl("\};")
603
604 # Build class vft
605 self.header.add_decl("extern const struct class_{c_name} class_{c_name};")
606 v.add_decl("const struct class_{c_name} class_{c_name} = \{")
607 v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
608 v.add_decl("\{")
609 for i in [0 .. vft.length[ do
610 var mpropdef = vft[i]
611 if mpropdef == null then
612 v.add_decl("NULL, /* empty */")
613 else
614 if true or mpropdef.mclassdef.bound_mtype.ctype != "val*" then
615 v.add_decl("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
616 else
617 v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
618 end
619 end
620 end
621 v.add_decl("\}")
622 v.add_decl("\};")
623
624 if mtype.ctype != "val*" then
625 #Build instance struct
626 self.header.add_decl("struct instance_{c_name} \{")
627 self.header.add_decl("const struct type *type;")
628 self.header.add_decl("const struct class *class;")
629 self.header.add_decl("{mtype.ctype} value;")
630 self.header.add_decl("\};")
631
632 if not self.runtime_type_analysis.live_types.has(mtype) then return
633
634 self.header.add_decl("val* BOX_{c_name}({mtype.ctype});")
635 v.add_decl("/* allocate {mtype} */")
636 v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
637 v.add("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
638 v.add("res->type = (struct type*) &type_{c_name};")
639 v.add("res->class = (struct class*) &class_{c_name};")
640 v.add("res->value = value;")
641 v.add("return (val*)res;")
642 v.add("\}")
643 return
644 end
645
646 var is_native_array = mclass.name == "NativeArray"
647
648 var sig
649 if is_native_array then
650 sig = "int length, struct type* type"
651 else
652 sig = "struct type* type"
653 end
654
655 #Build instance struct
656 #extern const struct instance_array__NativeArray instance_array__NativeArray;
657 self.header.add_decl("struct instance_{c_name} \{")
658 self.header.add_decl("const struct type *type;")
659 self.header.add_decl("const struct class *class;")
660 self.header.add_decl("nitattribute_t attrs[{attrs.length}];")
661 if is_native_array then
662 # NativeArrays are just a instance header followed by an array of values
663 self.header.add_decl("val* values[0];")
664 end
665 self.header.add_decl("\};")
666
667
668 self.header.add_decl("{mtype.ctype} NEW_{c_name}({sig});")
669 v.add_decl("/* allocate {mtype} */")
670 v.add_decl("{mtype.ctype} NEW_{c_name}({sig}) \{")
671 var res = v.new_named_var(mtype, "self")
672 res.is_exact = true
673 if is_native_array then
674 var mtype_elt = mtype.arguments.first
675 v.add("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
676 else
677 v.add("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
678 end
679 v.add("{res}->type = type;")
680 if v.compiler.modelbuilder.toolcontext.opt_hardening.value then
681 v.add("if(type == NULL) \{")
682 v.add_abort("type null")
683 v.add("\}")
684 v.add("if(type->unanchored_table == NULL) \{")
685 v.add("fprintf(stderr, \"Insantiation of a dead type: %s\\n\", type->name);")
686 v.add_abort("type dead")
687 v.add("\}")
688 end
689 v.add("{res}->class = (struct class*) &class_{c_name};")
690
691 self.generate_init_attr(v, res, mtype)
692 v.add("return {res};")
693 v.add("\}")
694
695 generate_check_init_instance(mtype)
696 end
697
698 redef fun generate_check_init_instance(mtype)
699 do
700 if self.modelbuilder.toolcontext.opt_no_check_initialization.value then return
701
702 var v = self.new_visitor
703 var c_name = mtype.mclass.c_name
704 var res = new RuntimeVariable("self", mtype, mtype)
705 self.header.add_decl("void CHECK_NEW_{c_name}({mtype.ctype});")
706 v.add_decl("/* allocate {mtype} */")
707 v.add_decl("void CHECK_NEW_{c_name}({mtype.ctype} {res}) \{")
708 self.generate_check_attr(v, res, mtype)
709 v.add("\}")
710 end
711
712 redef fun new_visitor do return new SeparateCompilerVisitor(self)
713
714 # Stats
715
716 redef fun display_stats
717 do
718 super
719 if self.modelbuilder.toolcontext.opt_typing_table_metrics.value then
720 display_sizes
721 end
722 end
723
724 fun display_sizes
725 do
726 print "# size of tables"
727 print "\trs size\trs hole\tst size\tst hole"
728 var rt_table = 0
729 var rt_holes = 0
730 var st_table = 0
731 var st_holes = 0
732 var rtables = unanchored_types_tables
733 if rtables != null then
734 for unanch, table in rtables do
735 rt_table += table.length
736 for e in table do if e == null then rt_holes += 1
737 end
738 end
739
740 var ttables = type_tables
741 if ttables != null then
742 for t, table in ttables do
743 st_table += table.length
744 for e in table do if e == null then st_holes += 1
745 end
746 end
747 print "\t{rt_table}\t{rt_holes}\t{st_table}\t{st_holes}"
748 end
749 end
750
751 # A visitor on the AST of property definition that generate the C code of a separate compilation process.
752 class SeparateCompilerVisitor
753 super AbstractCompilerVisitor
754
755 redef type COMPILER: SeparateCompiler
756
757 redef fun adapt_signature(m, args)
758 do
759 var msignature = m.msignature.resolve_for(m.mclassdef.bound_mtype, m.mclassdef.bound_mtype, m.mclassdef.mmodule, true)
760 var recv = args.first
761 if recv.mtype.ctype != m.mclassdef.mclass.mclass_type.ctype then
762 args.first = self.autobox(args.first, m.mclassdef.mclass.mclass_type)
763 end
764 for i in [0..msignature.arity[ do
765 var t = msignature.mparameters[i].mtype
766 if i == msignature.vararg_rank then
767 t = args[i+1].mtype
768 end
769 args[i+1] = self.autobox(args[i+1], t)
770 end
771 end
772
773 redef fun autobox(value, mtype)
774 do
775 if value.mtype == mtype then
776 return value
777 else if value.mtype.ctype == "val*" and mtype.ctype == "val*" then
778 return value
779 else if value.mtype.ctype == "val*" then
780 return self.new_expr("((struct instance_{mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype)
781 else if mtype.ctype == "val*" then
782 var valtype = value.mtype.as(MClassType)
783 var res = self.new_var(mtype)
784 if not compiler.runtime_type_analysis.live_types.has(valtype) then
785 self.add("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
786 self.add("printf(\"Dead code executed!\\n\"); exit(1);")
787 return res
788 end
789 self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
790 return res
791 else
792 # Bad things will appen!
793 var res = self.new_var(mtype)
794 self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
795 self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); exit(1);")
796 return res
797 end
798 end
799
800 # Return a C expression returning the runtime type structure of the value
801 # The point of the method is to works also with primitives types.
802 fun type_info(value: RuntimeVariable): String
803 do
804 if value.mtype.ctype == "val*" then
805 return "{value}->type"
806 else
807 return "(&type_{value.mtype.c_name})"
808 end
809 end
810
811 redef fun send(mmethod, arguments)
812 do
813 if arguments.first.mcasttype.ctype != "val*" then
814 return self.monomorphic_send(mmethod, arguments.first.mcasttype, arguments)
815 end
816
817 var res: nullable RuntimeVariable
818 var msignature = mmethod.intro.msignature.resolve_for(mmethod.intro.mclassdef.bound_mtype, mmethod.intro.mclassdef.bound_mtype, mmethod.intro.mclassdef.mmodule, true)
819 var ret = msignature.return_mtype
820 if mmethod.is_new then
821 ret = arguments.first.mtype
822 res = self.new_var(ret)
823 else if ret == null then
824 res = null
825 else
826 res = self.new_var(ret)
827 end
828
829 var s = new Buffer
830 var ss = new Buffer
831
832 var recv = arguments.first
833 s.append("val*")
834 ss.append("{recv}")
835 self.varargize(mmethod.intro, mmethod.intro.msignature.as(not null), arguments)
836 for i in [0..msignature.arity[ do
837 var a = arguments[i+1]
838 var t = msignature.mparameters[i].mtype
839 if i == msignature.vararg_rank then
840 t = arguments[i+1].mcasttype
841 end
842 s.append(", {t.ctype}")
843 a = self.autobox(a, t)
844 ss.append(", {a}")
845 end
846
847 var consider_null = not self.compiler.modelbuilder.toolcontext.opt_no_check_other.value or mmethod.name == "==" or mmethod.name == "!="
848 var maybenull = recv.mcasttype isa MNullableType and consider_null
849 if maybenull then
850 self.add("if ({recv} == NULL) \{")
851 if mmethod.name == "==" then
852 assert res != null
853 var arg = arguments[1]
854 if arg.mcasttype isa MNullableType then
855 self.add("{res} = ({arg} == NULL);")
856 else if arg.mcasttype isa MNullType then
857 self.add("{res} = 1; /* is null */")
858 else
859 self.add("{res} = 0; /* {arg.inspect} cannot be null */")
860 end
861 else if mmethod.name == "!=" then
862 assert res != null
863 var arg = arguments[1]
864 if arg.mcasttype isa MNullableType then
865 self.add("{res} = ({arg} != NULL);")
866 else if arg.mcasttype isa MNullType then
867 self.add("{res} = 0; /* is null */")
868 else
869 self.add("{res} = 1; /* {arg.inspect} cannot be null */")
870 end
871 else
872 self.add_abort("Reciever is null")
873 end
874 self.add("\} else \{")
875 end
876 if not self.compiler.modelbuilder.toolcontext.opt_no_shortcut_equate.value and (mmethod.name == "==" or mmethod.name == "!=") then
877 assert res != null
878 # Recv is not null, thus is arg is, it is easy to conclude (and respect the invariants)
879 var arg = arguments[1]
880 if arg.mcasttype isa MNullType then
881 if mmethod.name == "==" then
882 self.add("{res} = 0; /* arg is null but recv is not */")
883 else
884 self.add("{res} = 1; /* arg is null and recv is not */")
885 end
886 if maybenull then
887 self.add("\}")
888 end
889 return res
890 end
891 end
892
893 var r
894 if ret == null then r = "void" else r = ret.ctype
895 var call = "(({r} (*)({s}))({arguments.first}->class->vft[{mmethod.const_color}]))({ss}) /* {mmethod} on {arguments.first.inspect}*/"
896
897 if res != null then
898 self.add("{res} = {call};")
899 else
900 self.add("{call};")
901 end
902
903 if maybenull then
904 self.add("\}")
905 end
906
907 return res
908 end
909
910 redef fun call(mmethoddef, recvtype, arguments)
911 do
912 var res: nullable RuntimeVariable
913 var ret = mmethoddef.msignature.return_mtype
914 if mmethoddef.mproperty.is_new then
915 ret = arguments.first.mtype
916 res = self.new_var(ret)
917 else if ret == null then
918 res = null
919 else
920 ret = ret.resolve_for(mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.mmodule, true)
921 res = self.new_var(ret)
922 end
923
924 if self.compiler.modelbuilder.mpropdef2npropdef.has_key(mmethoddef) and
925 self.compiler.modelbuilder.mpropdef2npropdef[mmethoddef] isa AInternMethPropdef and
926 not compiler.modelbuilder.toolcontext.opt_no_inline_intern.value then
927 var frame = new Frame(self, mmethoddef, recvtype, arguments)
928 frame.returnlabel = self.get_name("RET_LABEL")
929 frame.returnvar = res
930 var old_frame = self.frame
931 self.frame = frame
932 self.add("\{ /* Inline {mmethoddef} ({arguments.join(",")}) */")
933 mmethoddef.compile_inside_to_c(self, arguments)
934 self.add("{frame.returnlabel.as(not null)}:(void)0;")
935 self.add("\}")
936 self.frame = old_frame
937 return res
938 end
939
940 # Autobox arguments
941 self.adapt_signature(mmethoddef, arguments)
942
943 if res == null then
944 self.add("{mmethoddef.c_name}({arguments.join(", ")});")
945 return null
946 else
947 self.add("{res} = {mmethoddef.c_name}({arguments.join(", ")});")
948 end
949
950 return res
951 end
952
953 redef fun vararg_instance(mpropdef, recv, varargs, elttype)
954 do
955 # A vararg must be stored into an new array
956 # The trick is that the dymaic type of the array may depends on the receiver
957 # of the method (ie recv) if the static type is unresolved
958 # This is more complex than usual because the unanchored type must not be resolved
959 # with the current receiver (ie self).
960 # Therefore to isolate the resolution from self, a local Frame is created.
961 # One can see this implementation as an inlined method of the receiver whose only
962 # job is to allocate the array
963 var old_frame = self.frame
964 var frame = new Frame(self, mpropdef, mpropdef.mclassdef.bound_mtype, [recv])
965 self.frame = frame
966 #print "required Array[{elttype}] for recv {recv.inspect}. bound=Array[{self.resolve_for(elttype, recv)}]. selfvar={frame.arguments.first.inspect}"
967 var res = self.array_instance(varargs, elttype)
968 self.frame = old_frame
969 return res
970 end
971
972 redef fun isset_attribute(a, recv)
973 do
974 self.check_recv_notnull(recv)
975 var res = self.new_var(bool_type)
976
977 # What is the declared type of the attribute?
978 var mtype = a.intro.static_mtype.as(not null)
979 var intromclassdef = a.intro.mclassdef
980 mtype = mtype.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
981
982 if mtype isa MNullableType then
983 self.add("{res} = 1; /* easy isset: {a} on {recv.inspect} */")
984 return res
985 end
986
987 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
988 self.add("{res} = {recv}->attrs[{a.const_color}] != NULL; /* {a} on {recv.inspect}*/")
989 else
990
991 if mtype.ctype == "val*" then
992 self.add("{res} = {recv}->attrs[{a.const_color}].val != NULL; /* {a} on {recv.inspect} */")
993 else
994 self.add("{res} = 1; /* NOT YET IMPLEMENTED: isset of primitives: {a} on {recv.inspect} */")
995 end
996 end
997 return res
998 end
999
1000 redef fun read_attribute(a, recv)
1001 do
1002 self.check_recv_notnull(recv)
1003
1004 # What is the declared type of the attribute?
1005 var ret = a.intro.static_mtype.as(not null)
1006 var intromclassdef = a.intro.mclassdef
1007 ret = ret.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1008
1009 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
1010 # Get the attribute or a box (ie. always a val*)
1011 var cret = self.object_type.as_nullable
1012 var res = self.new_var(cret)
1013 res.mcasttype = ret
1014
1015 self.add("{res} = {recv}->attrs[{a.const_color}]; /* {a} on {recv.inspect} */")
1016
1017 # Check for Uninitialized attribute
1018 if not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_initialization.value then
1019 self.add("if ({res} == NULL) \{")
1020 self.add_abort("Uninitialized attribute {a.name}")
1021 self.add("\}")
1022 end
1023
1024 # Return the attribute or its unboxed version
1025 # Note: it is mandatory since we reuse the box on write, we do not whant that the box escapes
1026 return self.autobox(res, ret)
1027 else
1028 var res = self.new_var(ret)
1029 self.add("{res} = {recv}->attrs[{a.const_color}].{ret.ctypename}; /* {a} on {recv.inspect} */")
1030
1031 # Check for Uninitialized attribute
1032 if ret.ctype == "val*" and not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_initialization.value then
1033 self.add("if ({res} == NULL) \{")
1034 self.add_abort("Uninitialized attribute {a.name}")
1035 self.add("\}")
1036 end
1037
1038 return res
1039 end
1040 end
1041
1042 redef fun write_attribute(a, recv, value)
1043 do
1044 self.check_recv_notnull(recv)
1045
1046 # What is the declared type of the attribute?
1047 var mtype = a.intro.static_mtype.as(not null)
1048 var intromclassdef = a.intro.mclassdef
1049 mtype = mtype.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1050
1051 # Adapt the value to the declared type
1052 value = self.autobox(value, mtype)
1053
1054 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
1055 var attr = "{recv}->attrs[{a.const_color}]"
1056 if mtype.ctype != "val*" then
1057 assert mtype isa MClassType
1058 # The attribute is primitive, thus we store it in a box
1059 # The trick is to create the box the first time then resuse the box
1060 self.add("if ({attr} != NULL) \{")
1061 self.add("((struct instance_{mtype.c_name}*){attr})->value = {value}; /* {a} on {recv.inspect} */")
1062 self.add("\} else \{")
1063 value = self.autobox(value, self.object_type.as_nullable)
1064 self.add("{attr} = {value}; /* {a} on {recv.inspect} */")
1065 self.add("\}")
1066 else
1067 # The attribute is not primitive, thus store it direclty
1068 self.add("{attr} = {value}; /* {a} on {recv.inspect} */")
1069 end
1070 else
1071 self.add("{recv}->attrs[{a.const_color}].{mtype.ctypename} = {value}; /* {a} on {recv.inspect} */")
1072 end
1073 end
1074
1075 # Build livetype structure retrieving
1076 # ENSURE: mtype.need_anchor
1077 fun retrieve_anchored_livetype(mtype: MGenericType, buffer: Buffer) do
1078 assert mtype.need_anchor
1079
1080 var compiler = self.compiler
1081 for ft in mtype.arguments do
1082
1083 var ntype = ft
1084 var s: String = ""
1085 if ntype isa MNullableType then
1086 ntype = ntype.mtype
1087 end
1088
1089 var recv = self.frame.arguments.first
1090 var recv_type_info = self.type_info(recv)
1091 if ntype isa MParameterType then
1092 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1093 buffer.append("[{recv_type_info}->fts_table->types[HASH({recv_type_info}->fts_table->mask, {ntype.const_color})]->livecolor]")
1094 else
1095 buffer.append("[{recv_type_info}->fts_table->types[{ntype.const_color}]->livecolor]")
1096 end
1097 else if ntype isa MVirtualType then
1098 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1099 buffer.append("[{recv_type_info}->vts_table->types[HASH({recv_type_info}->vts_table->mask, {ntype.mproperty.const_color})]->livecolor]")
1100 else
1101 buffer.append("[{recv_type_info}->vts_table->types[{ntype.mproperty.const_color}]->livecolor]")
1102 end
1103 else if ntype isa MGenericType and ntype.need_anchor then
1104 var bbuff = new Buffer
1105 retrieve_anchored_livetype(ntype, bbuff)
1106 buffer.append("[livetypes_{ntype.mclass.c_name}{bbuff.to_s}->livecolor]")
1107 else if ntype isa MClassType then
1108 compiler.undead_types.add(ft)
1109 buffer.append("[type_{ft.c_name}.livecolor]")
1110 else
1111 self.add("printf(\"NOT YET IMPLEMENTED: init_instance(%s, {mtype}).\\n\", \"{ft}\"); exit(1);")
1112 end
1113 end
1114 end
1115
1116 redef fun init_instance(mtype)
1117 do
1118 var compiler = self.compiler
1119 if mtype isa MGenericType and mtype.need_anchor then
1120 link_unanchored_type(self.frame.mpropdef.mclassdef, mtype)
1121 var recv = self.frame.arguments.first
1122 var recv_type_info = self.type_info(recv)
1123 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1124 return self.new_expr("NEW_{mtype.mclass.c_name}((struct type *) {recv_type_info}->unanchored_table->types[HASH({recv_type_info}->unanchored_table->mask, {mtype.const_color})])", mtype)
1125 else
1126 return self.new_expr("NEW_{mtype.mclass.c_name}((struct type *) {recv_type_info}->unanchored_table->types[{mtype.const_color}])", mtype)
1127 end
1128 end
1129 compiler.undead_types.add(mtype)
1130 return self.new_expr("NEW_{mtype.mclass.c_name}((struct type *) &type_{mtype.c_name})", mtype)
1131 end
1132
1133 redef fun check_init_instance(value, mtype)
1134 do
1135 if self.compiler.modelbuilder.toolcontext.opt_no_check_initialization.value then return
1136 self.add("CHECK_NEW_{mtype.mclass.c_name}({value});")
1137 end
1138
1139 redef fun type_test(value, mtype, tag)
1140 do
1141 self.add("/* {value.inspect} isa {mtype} */")
1142 var compiler = self.compiler
1143
1144 var recv = self.frame.arguments.first
1145 var recv_type_info = self.type_info(recv)
1146
1147 var res = self.new_var(bool_type)
1148
1149 var cltype = self.get_name("cltype")
1150 self.add_decl("int {cltype};")
1151 var idtype = self.get_name("idtype")
1152 self.add_decl("int {idtype};")
1153
1154 var maybe_null = self.maybe_null(value)
1155 var accept_null = "0"
1156 var ntype = mtype
1157 if ntype isa MNullableType then
1158 ntype = ntype.mtype
1159 accept_null = "1"
1160 end
1161
1162 if value.mcasttype.is_subtype(self.frame.mpropdef.mclassdef.mmodule, self.frame.mpropdef.mclassdef.bound_mtype, mtype) then
1163 self.add("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
1164 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1165 self.compiler.count_type_test_skipped[tag] += 1
1166 self.add("count_type_test_skipped_{tag}++;")
1167 end
1168 return res
1169 end
1170
1171 if ntype.need_anchor then
1172 var type_struct = self.get_name("type_struct")
1173 self.add_decl("struct type* {type_struct};")
1174
1175 # Either with unanchored_table with a direct resolution
1176 link_unanchored_type(self.frame.mpropdef.mclassdef, ntype)
1177 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1178 self.add("{type_struct} = {recv_type_info}->unanchored_table->types[HASH({recv_type_info}->unanchored_table->mask, {ntype.const_color})];")
1179 else
1180 self.add("{type_struct} = {recv_type_info}->unanchored_table->types[{ntype.const_color}];")
1181 end
1182 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1183 self.compiler.count_type_test_unresolved[tag] += 1
1184 self.add("count_type_test_unresolved_{tag}++;")
1185 end
1186 self.add("{cltype} = {type_struct}->color;")
1187 self.add("{idtype} = {type_struct}->id;")
1188 if maybe_null and accept_null == "0" then
1189 var is_nullable = self.get_name("is_nullable")
1190 self.add_decl("short int {is_nullable};")
1191 self.add("{is_nullable} = {type_struct}->is_nullable;")
1192 accept_null = is_nullable.to_s
1193 end
1194 else if ntype isa MClassType then
1195 compiler.undead_types.add(mtype)
1196 self.add("{cltype} = type_{mtype.c_name}.color;")
1197 self.add("{idtype} = type_{mtype.c_name}.id;")
1198 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1199 self.compiler.count_type_test_resolved[tag] += 1
1200 self.add("count_type_test_resolved_{tag}++;")
1201 end
1202 else
1203 self.add("printf(\"NOT YET IMPLEMENTED: type_test(%s, {mtype}).\\n\", \"{value.inspect}\"); exit(1);")
1204 end
1205
1206 # check color is in table
1207 if maybe_null then
1208 self.add("if({value} == NULL) \{")
1209 self.add("{res} = {accept_null};")
1210 self.add("\} else \{")
1211 end
1212 var value_type_info = self.type_info(value)
1213 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1214 self.add("{cltype} = HASH({value_type_info}->color, {idtype});")
1215 end
1216 self.add("if({cltype} >= {value_type_info}->table_size) \{")
1217 self.add("{res} = 0;")
1218 self.add("\} else \{")
1219 self.add("{res} = {value_type_info}->type_table[{cltype}] == {idtype};")
1220 self.add("\}")
1221 if maybe_null then
1222 self.add("\}")
1223 end
1224
1225 return res
1226 end
1227
1228 redef fun is_same_type_test(value1, value2)
1229 do
1230 var res = self.new_var(bool_type)
1231 # Swap values to be symetric
1232 if value2.mtype.ctype != "val*" and value1.mtype.ctype == "val*" then
1233 var tmp = value1
1234 value1 = value2
1235 value2 = tmp
1236 end
1237 if value1.mtype.ctype != "val*" then
1238 if value2.mtype == value1.mtype then
1239 self.add("{res} = 1; /* is_same_type_test: compatible types {value1.mtype} vs. {value2.mtype} */")
1240 else if value2.mtype.ctype != "val*" then
1241 self.add("{res} = 0; /* is_same_type_test: incompatible types {value1.mtype} vs. {value2.mtype}*/")
1242 else
1243 var mtype1 = value1.mtype.as(MClassType)
1244 self.add("{res} = ({value2} != NULL) && ({value2}->class == (struct class*) &class_{mtype1.c_name}); /* is_same_type_test */")
1245 end
1246 else
1247 self.add("{res} = ({value1} == {value2}) || ({value1} != NULL && {value2} != NULL && {value1}->class == {value2}->class); /* is_same_type_test */")
1248 end
1249 return res
1250 end
1251
1252 redef fun class_name_string(value)
1253 do
1254 var res = self.get_name("var_class_name")
1255 self.add_decl("const char* {res};")
1256 if value.mtype.ctype == "val*" then
1257 self.add "{res} = {value} == NULL ? \"null\" : {value}->type->name;"
1258 else
1259 self.add "{res} = type_{value.mtype.c_name}.name;"
1260 end
1261 return res
1262 end
1263
1264 redef fun equal_test(value1, value2)
1265 do
1266 var res = self.new_var(bool_type)
1267 if value2.mtype.ctype != "val*" and value1.mtype.ctype == "val*" then
1268 var tmp = value1
1269 value1 = value2
1270 value2 = tmp
1271 end
1272 if value1.mtype.ctype != "val*" then
1273 if value2.mtype == value1.mtype then
1274 self.add("{res} = {value1} == {value2};")
1275 else if value2.mtype.ctype != "val*" then
1276 self.add("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1277 else
1278 var mtype1 = value1.mtype.as(MClassType)
1279 self.add("{res} = ({value2} != NULL) && ({value2}->class == (struct class*) &class_{mtype1.c_name});")
1280 self.add("if ({res}) \{")
1281 self.add("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1282 self.add("\}")
1283 end
1284 return res
1285 end
1286 var maybe_null = true
1287 var test = new Array[String]
1288 var t1 = value1.mcasttype
1289 if t1 isa MNullableType then
1290 test.add("{value1} != NULL")
1291 t1 = t1.mtype
1292 else
1293 maybe_null = false
1294 end
1295 var t2 = value2.mcasttype
1296 if t2 isa MNullableType then
1297 test.add("{value2} != NULL")
1298 t2 = t2.mtype
1299 else
1300 maybe_null = false
1301 end
1302
1303 var incompatible = false
1304 var primitive
1305 if t1.ctype != "val*" then
1306 primitive = t1
1307 if t1 == t2 then
1308 # No need to compare class
1309 else if t2.ctype != "val*" then
1310 incompatible = true
1311 else if can_be_primitive(value2) then
1312 test.add("{value1}->class == {value2}->class")
1313 else
1314 incompatible = true
1315 end
1316 else if t2.ctype != "val*" then
1317 primitive = t2
1318 if can_be_primitive(value1) then
1319 test.add("{value1}->class == {value2}->class")
1320 else
1321 incompatible = true
1322 end
1323 else
1324 primitive = null
1325 end
1326
1327 if incompatible then
1328 if maybe_null then
1329 self.add("{res} = {value1} == {value2}; /* incompatible types {t1} vs. {t2}; but may be NULL*/")
1330 return res
1331 else
1332 self.add("{res} = 0; /* incompatible types {t1} vs. {t2}; cannot be NULL */")
1333 return res
1334 end
1335 end
1336 if primitive != null then
1337 test.add("((struct instance_{primitive.c_name}*){value1})->value == ((struct instance_{primitive.c_name}*){value2})->value")
1338 else if can_be_primitive(value1) and can_be_primitive(value2) then
1339 test.add("{value1}->class == {value2}->class")
1340 var s = new Array[String]
1341 for t, v in self.compiler.box_kinds do
1342 s.add "({value1}->class->box_kind == {v} && ((struct instance_{t.c_name}*){value1})->value == ((struct instance_{t.c_name}*){value2})->value)"
1343 end
1344 test.add("({s.join(" || ")})")
1345 else
1346 self.add("{res} = {value1} == {value2};")
1347 return res
1348 end
1349 self.add("{res} = {value1} == {value2} || ({test.join(" && ")});")
1350 return res
1351 end
1352
1353 fun can_be_primitive(value: RuntimeVariable): Bool
1354 do
1355 var t = value.mcasttype
1356 if t isa MNullableType then t = t.mtype
1357 if not t isa MClassType then return false
1358 var k = t.mclass.kind
1359 return k == interface_kind or t.ctype != "val*"
1360 end
1361
1362 fun maybe_null(value: RuntimeVariable): Bool
1363 do
1364 var t = value.mcasttype
1365 return t isa MNullableType or t isa MNullType
1366 end
1367
1368 redef fun array_instance(array, elttype)
1369 do
1370 var nclass = self.get_class("NativeArray")
1371 var arrayclass = self.get_class("Array")
1372 var arraytype = arrayclass.get_mtype([elttype])
1373 var res = self.init_instance(arraytype)
1374 self.add("\{ /* {res} = array_instance Array[{elttype}] */")
1375 var length = self.int_instance(array.length)
1376 var nat = native_array_instance(elttype, length)
1377 for i in [0..array.length[ do
1378 var r = self.autobox(array[i], self.object_type)
1379 self.add("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
1380 end
1381 self.send(self.get_property("with_native", arrayclass.intro.bound_mtype), [res, nat, length])
1382 self.check_init_instance(res, arraytype)
1383 self.add("\}")
1384 return res
1385 end
1386
1387 fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable
1388 do
1389 var mtype = self.get_class("NativeArray").get_mtype([elttype])
1390 assert mtype isa MGenericType
1391 var compiler = self.compiler
1392 if mtype.need_anchor then
1393 link_unanchored_type(self.frame.mpropdef.mclassdef, mtype)
1394 var recv = self.frame.arguments.first
1395 var recv_type_info = self.type_info(recv)
1396 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1397 return self.new_expr("NEW_{mtype.mclass.c_name}({length}, (struct type *) {recv_type_info}->unanchored_table->types[HASH({recv_type_info}->unanchored_table->mask, {mtype.const_color})])", mtype)
1398 else
1399 return self.new_expr("NEW_{mtype.mclass.c_name}({length}, (struct type *) {recv_type_info}->unanchored_table->types[{mtype.const_color}])", mtype)
1400 end
1401 end
1402 compiler.undead_types.add(mtype)
1403 return self.new_expr("NEW_{mtype.mclass.c_name}({length}, (struct type *) &type_{mtype.c_name})", mtype)
1404 end
1405
1406 redef fun native_array_def(pname, ret_type, arguments)
1407 do
1408 var elttype = arguments.first.mtype
1409 var nclass = self.get_class("NativeArray")
1410 var recv = "((struct instance_{nclass.c_name}*){arguments[0]})->values"
1411 if pname == "[]" then
1412 self.ret(self.new_expr("{recv}[{arguments[1]}]", ret_type.as(not null)))
1413 return
1414 else if pname == "[]=" then
1415 self.add("{recv}[{arguments[1]}]={arguments[2]};")
1416 return
1417 else if pname == "copy_to" then
1418 var recv1 = "((struct instance_{nclass.c_name}*){arguments[1]})->values"
1419 self.add("memcpy({recv1}, {recv}, {arguments[2]}*sizeof({elttype.ctype}));")
1420 return
1421 end
1422 end
1423
1424 redef fun calloc_array(ret_type, arguments)
1425 do
1426 var mclass = self.get_class("ArrayCapable")
1427 var ft = mclass.mclass_type.arguments.first.as(MParameterType)
1428 var res = self.native_array_instance(ft, arguments[1])
1429 self.ret(res)
1430 end
1431
1432 fun link_unanchored_type(mclassdef: MClassDef, mtype: MType) do
1433 assert mtype.need_anchor
1434 var compiler = self.compiler
1435 if not compiler.live_unanchored_types.has_key(self.frame.mpropdef.mclassdef) then
1436 compiler.live_unanchored_types[self.frame.mpropdef.mclassdef] = new HashSet[MType]
1437 end
1438 compiler.live_unanchored_types[self.frame.mpropdef.mclassdef].add(mtype)
1439 end
1440 end
1441
1442 # The C function associated to a methoddef separately compiled
1443 class SeparateRuntimeFunction
1444 super AbstractRuntimeFunction
1445
1446 redef fun build_c_name: String do return "{mmethoddef.c_name}"
1447
1448 redef fun to_s do return self.mmethoddef.to_s
1449
1450 redef fun compile_to_c(compiler)
1451 do
1452 var mmethoddef = self.mmethoddef
1453
1454 var recv = self.mmethoddef.mclassdef.bound_mtype
1455 var v = compiler.new_visitor
1456 var selfvar = new RuntimeVariable("self", recv, recv)
1457 var arguments = new Array[RuntimeVariable]
1458 var frame = new Frame(v, mmethoddef, recv, arguments)
1459 v.frame = frame
1460
1461 var msignature = mmethoddef.msignature.resolve_for(mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.mmodule, true)
1462
1463 var sig = new Buffer
1464 var comment = new Buffer
1465 var ret = msignature.return_mtype
1466 if ret != null then
1467 sig.append("{ret.ctype} ")
1468 else if mmethoddef.mproperty.is_new then
1469 ret = recv
1470 sig.append("{ret.ctype} ")
1471 else
1472 sig.append("void ")
1473 end
1474 sig.append(self.c_name)
1475 sig.append("({selfvar.mtype.ctype} {selfvar}")
1476 comment.append("(self: {selfvar}")
1477 arguments.add(selfvar)
1478 for i in [0..msignature.arity[ do
1479 var mtype = msignature.mparameters[i].mtype
1480 if i == msignature.vararg_rank then
1481 mtype = v.get_class("Array").get_mtype([mtype])
1482 end
1483 comment.append(", {mtype}")
1484 sig.append(", {mtype.ctype} p{i}")
1485 var argvar = new RuntimeVariable("p{i}", mtype, mtype)
1486 arguments.add(argvar)
1487 end
1488 sig.append(")")
1489 comment.append(")")
1490 if ret != null then
1491 comment.append(": {ret}")
1492 end
1493 compiler.header.add_decl("{sig};")
1494
1495 v.add_decl("/* method {self} for {comment} */")
1496 v.add_decl("{sig} \{")
1497 if ret != null then
1498 frame.returnvar = v.new_var(ret)
1499 end
1500 frame.returnlabel = v.get_name("RET_LABEL")
1501
1502 if recv != arguments.first.mtype then
1503 #print "{self} {recv} {arguments.first}"
1504 end
1505 mmethoddef.compile_inside_to_c(v, arguments)
1506
1507 v.add("{frame.returnlabel.as(not null)}:;")
1508 if ret != null then
1509 v.add("return {frame.returnvar.as(not null)};")
1510 end
1511 v.add("\}")
1512 end
1513 end
1514
1515 # The C function associated to a methoddef on a primitive type, stored into a VFT of a class
1516 # The first parameter (the reciever) is always typed by val* in order to accept an object value
1517 class VirtualRuntimeFunction
1518 super AbstractRuntimeFunction
1519
1520 redef fun build_c_name: String do return "VIRTUAL_{mmethoddef.c_name}"
1521
1522 redef fun to_s do return self.mmethoddef.to_s
1523
1524 redef fun compile_to_c(compiler)
1525 do
1526 var mmethoddef = self.mmethoddef
1527
1528 var recv = self.mmethoddef.mclassdef.bound_mtype
1529 var v = compiler.new_visitor
1530 var selfvar = new RuntimeVariable("self", v.object_type, recv)
1531 var arguments = new Array[RuntimeVariable]
1532 var frame = new Frame(v, mmethoddef, recv, arguments)
1533 v.frame = frame
1534
1535 var sig = new Buffer
1536 var comment = new Buffer
1537
1538 # Because the function is virtual, the signature must match the one of the original class
1539 var intromclassdef = self.mmethoddef.mproperty.intro.mclassdef
1540 var msignature = mmethoddef.mproperty.intro.msignature.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1541 var ret = msignature.return_mtype
1542 if ret != null then
1543 sig.append("{ret.ctype} ")
1544 else if mmethoddef.mproperty.is_new then
1545 ret = recv
1546 sig.append("{ret.ctype} ")
1547 else
1548 sig.append("void ")
1549 end
1550 sig.append(self.c_name)
1551 sig.append("({selfvar.mtype.ctype} {selfvar}")
1552 comment.append("(self: {selfvar}")
1553 arguments.add(selfvar)
1554 for i in [0..msignature.arity[ do
1555 var mtype = msignature.mparameters[i].mtype
1556 if i == msignature.vararg_rank then
1557 mtype = v.get_class("Array").get_mtype([mtype])
1558 end
1559 comment.append(", {mtype}")
1560 sig.append(", {mtype.ctype} p{i}")
1561 var argvar = new RuntimeVariable("p{i}", mtype, mtype)
1562 arguments.add(argvar)
1563 end
1564 sig.append(")")
1565 comment.append(")")
1566 if ret != null then
1567 comment.append(": {ret}")
1568 end
1569 compiler.header.add_decl("{sig};")
1570
1571 v.add_decl("/* method {self} for {comment} */")
1572 v.add_decl("{sig} \{")
1573 if ret != null then
1574 frame.returnvar = v.new_var(ret)
1575 end
1576 frame.returnlabel = v.get_name("RET_LABEL")
1577
1578 if recv != arguments.first.mtype then
1579 #print "{self} {recv} {arguments.first}"
1580 end
1581 mmethoddef.compile_inside_to_c(v, arguments)
1582
1583 v.add("{frame.returnlabel.as(not null)}:;")
1584 if ret != null then
1585 v.add("return {frame.returnvar.as(not null)};")
1586 end
1587 v.add("\}")
1588 end
1589
1590 # TODO ?
1591 redef fun call(v, arguments) do abort
1592 end
1593
1594 redef class MType
1595 fun const_color: String do return "COLOR_{c_name}"
1596 end
1597
1598 redef class MProperty
1599 fun const_color: String do return "COLOR_{c_name}"
1600 end