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