sep_comp: do not rely on RTA in autobox
[nit.git] / src / compiler / 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 import coloring
20 import rapid_type_analysis
21
22 # Add separate compiler specific options
23 redef class ToolContext
24 # --separate
25 var opt_separate = new OptionBool("Use separate compilation", "--separate")
26 # --no-inline-intern
27 var opt_no_inline_intern = new OptionBool("Do not inline call to intern methods", "--no-inline-intern")
28 # --no-union-attribute
29 var opt_no_union_attribute = 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 = new OptionBool("Always call == in a polymorphic way", "--no-shortcut-equal")
32 # --no-tag-primitives
33 var opt_no_tag_primitives = new OptionBool("Use only boxes for primitive types", "--no-tag-primitives")
34
35 # --colors-are-symbols
36 var opt_colors_are_symbols = new OptionBool("Store colors as symbols (link-boost)", "--colors-are-symbols")
37 # --trampoline-call
38 var opt_trampoline_call = new OptionBool("Use an indirection when calling", "--trampoline-call")
39 # --guard-call
40 var opt_guard_call = new OptionBool("Guard VFT calls with a direct call", "--guard-call")
41 # --substitute-monomorph
42 var opt_substitute_monomorph = new OptionBool("Replace monomorph trampoline with direct call (link-boost)", "--substitute-monomorph")
43 # --link-boost
44 var opt_link_boost = new OptionBool("Enable all link-boost optimizations", "--link-boost")
45
46 # --inline-coloring-numbers
47 var opt_inline_coloring_numbers = new OptionBool("Inline colors and ids (semi-global)", "--inline-coloring-numbers")
48 # --inline-some-methods
49 var opt_inline_some_methods = new OptionBool("Allow the separate compiler to inline some methods (semi-global)", "--inline-some-methods")
50 # --direct-call-monomorph
51 var opt_direct_call_monomorph = new OptionBool("Allow the separate compiler to direct call monomorph sites (semi-global)", "--direct-call-monomorph")
52 # --direct-call-monomorph0
53 var opt_direct_call_monomorph0 = new OptionBool("Allow the separate compiler to direct call monomorph sites (semi-global)", "--direct-call-monomorph0")
54 # --skip-dead-methods
55 var opt_skip_dead_methods = new OptionBool("Do not compile dead methods (semi-global)", "--skip-dead-methods")
56 # --semi-global
57 var opt_semi_global = new OptionBool("Enable all semi-global optimizations", "--semi-global")
58 # --no-colo-dead-methods
59 var opt_colo_dead_methods = new OptionBool("Force colorization of dead methods", "--colo-dead-methods")
60 # --tables-metrics
61 var opt_tables_metrics = new OptionBool("Enable static size measuring of tables used for vft, typing and resolution", "--tables-metrics")
62 # --type-poset
63 var opt_type_poset = new OptionBool("Build a poset of types to create more condensed tables.", "--type-poset")
64
65 redef init
66 do
67 super
68 self.option_context.add_option(self.opt_separate)
69 self.option_context.add_option(self.opt_no_inline_intern)
70 self.option_context.add_option(self.opt_no_union_attribute)
71 self.option_context.add_option(self.opt_no_shortcut_equate)
72 self.option_context.add_option(self.opt_no_tag_primitives)
73 self.option_context.add_option(opt_colors_are_symbols, opt_trampoline_call, opt_guard_call, opt_direct_call_monomorph0, opt_substitute_monomorph, opt_link_boost)
74 self.option_context.add_option(self.opt_inline_coloring_numbers, opt_inline_some_methods, opt_direct_call_monomorph, opt_skip_dead_methods, opt_semi_global)
75 self.option_context.add_option(self.opt_colo_dead_methods)
76 self.option_context.add_option(self.opt_tables_metrics)
77 self.option_context.add_option(self.opt_type_poset)
78 end
79
80 redef fun process_options(args)
81 do
82 super
83
84 var tc = self
85 if tc.opt_semi_global.value then
86 tc.opt_inline_coloring_numbers.value = true
87 tc.opt_inline_some_methods.value = true
88 tc.opt_direct_call_monomorph.value = true
89 tc.opt_skip_dead_methods.value = true
90 end
91 if tc.opt_link_boost.value then
92 tc.opt_colors_are_symbols.value = true
93 tc.opt_substitute_monomorph.value = true
94 end
95 if tc.opt_substitute_monomorph.value then
96 tc.opt_trampoline_call.value = true
97 end
98 end
99
100 var separate_compiler_phase = new SeparateCompilerPhase(self, null)
101 end
102
103 class SeparateCompilerPhase
104 super Phase
105 redef fun process_mainmodule(mainmodule, given_mmodules) do
106 if not toolcontext.opt_separate.value then return
107
108 var modelbuilder = toolcontext.modelbuilder
109 var analysis = modelbuilder.do_rapid_type_analysis(mainmodule)
110 modelbuilder.run_separate_compiler(mainmodule, analysis)
111 end
112 end
113
114 redef class ModelBuilder
115 fun run_separate_compiler(mainmodule: MModule, runtime_type_analysis: nullable RapidTypeAnalysis)
116 do
117 var time0 = get_time
118 self.toolcontext.info("*** GENERATING C ***", 1)
119
120 var compiler = new SeparateCompiler(mainmodule, self, runtime_type_analysis)
121 compiler.do_compilation
122 compiler.display_stats
123
124 var time1 = get_time
125 self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
126 write_and_make(compiler)
127 end
128
129 # Count number of invocations by VFT
130 private var nb_invok_by_tables = 0
131 # Count number of invocations by direct call
132 private var nb_invok_by_direct = 0
133 # Count number of invocations by inlining
134 private var nb_invok_by_inline = 0
135 end
136
137 # Singleton that store the knowledge about the separate compilation process
138 class SeparateCompiler
139 super AbstractCompiler
140
141 redef type VISITOR: SeparateCompilerVisitor
142
143 # The result of the RTA (used to know live types and methods)
144 var runtime_type_analysis: nullable RapidTypeAnalysis
145
146 private var undead_types: Set[MType] = new HashSet[MType]
147 private var live_unresolved_types: Map[MClassDef, Set[MType]] = new HashMap[MClassDef, HashSet[MType]]
148
149 private var type_ids: Map[MType, Int] is noinit
150 private var type_colors: Map[MType, Int] is noinit
151 private var opentype_colors: Map[MType, Int] is noinit
152
153 init do
154 var file = new_file("nit.common")
155 self.header = new CodeWriter(file)
156 self.compile_box_kinds
157 end
158
159 redef fun do_compilation
160 do
161 var compiler = self
162 compiler.compile_header
163
164 var c_name = mainmodule.c_name
165
166 # compile class structures
167 modelbuilder.toolcontext.info("Property coloring", 2)
168 compiler.new_file("{c_name}.classes")
169 compiler.do_property_coloring
170 compiler.compile_class_infos
171 for m in mainmodule.in_importation.greaters do
172 for mclass in m.intro_mclasses do
173 #if mclass.kind == abstract_kind or mclass.kind == interface_kind then continue
174 compiler.compile_class_to_c(mclass)
175 end
176 end
177
178 # The main function of the C
179 compiler.new_file("{c_name}.main")
180 compiler.compile_nitni_global_ref_functions
181 compiler.compile_main_function
182 compiler.compile_finalizer_function
183 compiler.link_mmethods
184
185 # compile methods
186 for m in mainmodule.in_importation.greaters do
187 modelbuilder.toolcontext.info("Generate C for module {m.full_name}", 2)
188 compiler.new_file("{m.c_name}.sep")
189 compiler.compile_module_to_c(m)
190 end
191
192 # compile live & cast type structures
193 modelbuilder.toolcontext.info("Type coloring", 2)
194 compiler.new_file("{c_name}.types")
195 compiler.compile_types
196 end
197
198 # Color and compile type structures and cast information
199 fun compile_types
200 do
201 var compiler = self
202
203 var mtypes = compiler.do_type_coloring
204 for t in mtypes do
205 compiler.compile_type_to_c(t)
206 end
207 # compile remaining types structures (useless but needed for the symbol resolution at link-time)
208 for t in compiler.undead_types do
209 if mtypes.has(t) then continue
210 compiler.compile_type_to_c(t)
211 end
212
213 end
214
215 redef fun compile_header_structs do
216 self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
217 self.compile_header_attribute_structs
218 self.header.add_decl("struct class \{ int box_kind; nitmethod_t vft[]; \}; /* general C type representing a Nit class. */")
219
220 # With resolution_table_table, all live type resolution are stored in a big table: resolution_table
221 self.header.add_decl("struct type \{ int id; const char *name; int color; short int is_nullable; const struct types *resolution_table; int table_size; int type_table[]; \}; /* general C type representing a Nit type. */")
222 self.header.add_decl("struct instance \{ const struct type *type; const struct class *class; nitattribute_t attrs[]; \}; /* general C type representing a Nit instance. */")
223 self.header.add_decl("struct types \{ int dummy; const struct type *types[]; \}; /* a list types (used for vts, fts and unresolved lists). */")
224 self.header.add_decl("typedef struct instance val; /* general C type representing a Nit instance. */")
225
226 if not modelbuilder.toolcontext.opt_no_tag_primitives.value then
227 self.header.add_decl("extern const struct class *class_info[];")
228 self.header.add_decl("extern const struct type *type_info[];")
229 end
230 end
231
232 fun compile_header_attribute_structs
233 do
234 if modelbuilder.toolcontext.opt_no_union_attribute.value then
235 self.header.add_decl("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
236 else
237 self.header.add_decl("typedef union \{")
238 self.header.add_decl("void* val;")
239 for c, v in self.box_kinds do
240 var t = c.mclass_type
241
242 # `Pointer` reuse the `val` field
243 if t.mclass.name == "Pointer" then continue
244
245 self.header.add_decl("{t.ctype_extern} {t.ctypename};")
246 end
247 self.header.add_decl("\} nitattribute_t; /* general C type representing a Nit attribute. */")
248 end
249 end
250
251 fun compile_box_kinds
252 do
253 # Collect all bas box class
254 # FIXME: this is not completely fine with a separate compilation scheme
255 for classname in ["Int", "Bool", "Byte", "Char", "Float", "NativeString", "Pointer"] do
256 var classes = self.mainmodule.model.get_mclasses_by_name(classname)
257 if classes == null then continue
258 assert classes.length == 1 else print classes.join(", ")
259 self.box_kinds[classes.first] = self.box_kinds.length + 1
260 end
261 end
262
263 var box_kinds = new HashMap[MClass, Int]
264
265 fun box_kind_of(mclass: MClass): Int
266 do
267 #var pointer_type = self.mainmodule.pointer_type
268 #if mclass.mclass_type.ctype == "val*" or mclass.mclass_type.is_subtype(self.mainmodule, mclass.mclass_type pointer_type) then
269 if mclass.mclass_type.ctype_extern == "val*" then
270 return 0
271 else if mclass.kind == extern_kind and mclass.name != "NativeString" then
272 return self.box_kinds[self.mainmodule.pointer_type.mclass]
273 else
274 return self.box_kinds[mclass]
275 end
276
277 end
278
279 fun compile_color_consts(colors: Map[Object, Int]) do
280 var v = new_visitor
281 for m, c in colors do
282 compile_color_const(v, m, c)
283 end
284 end
285
286 fun compile_color_const(v: SeparateCompilerVisitor, m: Object, color: Int) do
287 if color_consts_done.has(m) then return
288 if m isa MEntity then
289 if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
290 self.provide_declaration(m.const_color, "#define {m.const_color} {color}")
291 else if not modelbuilder.toolcontext.opt_colors_are_symbols.value or not v.compiler.target_platform.supports_linker_script then
292 self.provide_declaration(m.const_color, "extern const int {m.const_color};")
293 v.add("const int {m.const_color} = {color};")
294 else
295 # The color 'C' is the ``address'' of a false static variable 'XC'
296 self.provide_declaration(m.const_color, "#define {m.const_color} ((long)&X{m.const_color})\nextern const void X{m.const_color};")
297 if color == -1 then color = 0 # Symbols cannot be negative, so just use 0 for dead things
298 # Teach the linker that the address of 'XC' is `color`.
299 linker_script.add("X{m.const_color} = {color};")
300 end
301 else
302 abort
303 end
304 color_consts_done.add(m)
305 end
306
307 private var color_consts_done = new HashSet[Object]
308
309 # The conflict graph of classes used for coloration
310 var class_conflict_graph: POSetConflictGraph[MClass] is noinit
311
312 # colorize classe properties
313 fun do_property_coloring do
314
315 var rta = runtime_type_analysis
316
317 # Class graph
318 var mclasses = mainmodule.flatten_mclass_hierarchy
319 class_conflict_graph = mclasses.to_conflict_graph
320
321 # Prepare to collect elements to color and build layout with
322 var mmethods = new HashMap[MClass, Set[PropertyLayoutElement]]
323 var mattributes = new HashMap[MClass, Set[MAttribute]]
324
325 # The dead methods and super-call, still need to provide a dead color symbol
326 var dead_methods = new Array[PropertyLayoutElement]
327
328 for mclass in mclasses do
329 mmethods[mclass] = new HashSet[PropertyLayoutElement]
330 mattributes[mclass] = new HashSet[MAttribute]
331 end
332
333 # Pre-collect known live things
334 if rta != null then
335 for m in rta.live_methods do
336 mmethods[m.intro_mclassdef.mclass].add m
337 end
338 for m in rta.live_super_sends do
339 var mclass = m.mclassdef.mclass
340 mmethods[mclass].add m
341 end
342 end
343
344 for m in mainmodule.in_importation.greaters do for cd in m.mclassdefs do
345 var mclass = cd.mclass
346 # Collect methods ad attributes
347 for p in cd.intro_mproperties do
348 if p isa MMethod then
349 if rta == null then
350 mmethods[mclass].add p
351 else if not rta.live_methods.has(p) then
352 dead_methods.add p
353 end
354 else if p isa MAttribute then
355 mattributes[mclass].add p
356 end
357 end
358
359 # Collect all super calls (dead or not)
360 for mpropdef in cd.mpropdefs do
361 if not mpropdef isa MMethodDef then continue
362 if mpropdef.has_supercall then
363 if rta == null then
364 mmethods[mclass].add mpropdef
365 else if not rta.live_super_sends.has(mpropdef) then
366 dead_methods.add mpropdef
367 end
368 end
369 end
370 end
371
372 # methods coloration
373 var meth_colorer = new POSetGroupColorer[MClass, PropertyLayoutElement](class_conflict_graph, mmethods)
374 var method_colors = meth_colorer.colors
375 compile_color_consts(method_colors)
376
377 # give null color to dead methods and supercalls
378 for mproperty in dead_methods do compile_color_const(new_visitor, mproperty, -1)
379
380 # attribute coloration
381 var attr_colorer = new POSetGroupColorer[MClass, MAttribute](class_conflict_graph, mattributes)
382 var attr_colors = attr_colorer.colors#ize(poset, mattributes)
383 compile_color_consts(attr_colors)
384
385 # Build method and attribute tables
386 method_tables = new HashMap[MClass, Array[nullable MPropDef]]
387 attr_tables = new HashMap[MClass, Array[nullable MProperty]]
388 for mclass in mclasses do
389 if not mclass.has_new_factory and (mclass.kind == abstract_kind or mclass.kind == interface_kind) then continue
390 if rta != null and not rta.live_classes.has(mclass) then continue
391
392 var mtype = mclass.intro.bound_mtype
393
394 # Resolve elements in the layout to get the final table
395 var meth_layout = meth_colorer.build_layout(mclass)
396 var meth_table = new Array[nullable MPropDef].with_capacity(meth_layout.length)
397 method_tables[mclass] = meth_table
398 for e in meth_layout do
399 if e == null then
400 meth_table.add null
401 else if e isa MMethod then
402 # Standard method call of `e`
403 meth_table.add e.lookup_first_definition(mainmodule, mtype)
404 else if e isa MMethodDef then
405 # Super-call in the methoddef `e`
406 meth_table.add e.lookup_next_definition(mainmodule, mtype)
407 else
408 abort
409 end
410 end
411
412 # Do not need to resolve attributes as only the position is used
413 attr_tables[mclass] = attr_colorer.build_layout(mclass)
414 end
415
416
417 end
418
419 # colorize live types of the program
420 private fun do_type_coloring: Collection[MType] do
421 # Collect types to colorize
422 var live_types = runtime_type_analysis.live_types
423 var live_cast_types = runtime_type_analysis.live_cast_types
424
425 var res = new HashSet[MType]
426 res.add_all live_types
427 res.add_all live_cast_types
428
429 if modelbuilder.toolcontext.opt_type_poset.value then
430 # Compute colors with a type poset
431 var poset = poset_from_mtypes(live_types, live_cast_types)
432 var colorer = new POSetColorer[MType]
433 colorer.colorize(poset)
434 type_ids = colorer.ids
435 type_colors = colorer.colors
436 type_tables = build_type_tables(poset)
437 else
438 # Compute colors using the class poset
439 # Faster to compute but the number of holes can degenerate
440 compute_type_test_layouts(live_types, live_cast_types)
441
442 type_ids = new HashMap[MType, Int]
443 for x in res do type_ids[x] = type_ids.length + 1
444 end
445
446 # VT and FT are stored with other unresolved types in the big resolution_tables
447 self.compute_resolution_tables(live_types)
448
449 return res
450 end
451
452 private fun poset_from_mtypes(mtypes, cast_types: Set[MType]): POSet[MType] do
453 var poset = new POSet[MType]
454
455 # Instead of doing the full matrix mtypes X cast_types,
456 # a grouping is done by the base classes of the type so
457 # that we compare only types whose base classes are in inheritance.
458
459 var mtypes_by_class = new MultiHashMap[MClass, MType]
460 for e in mtypes do
461 var c = e.undecorate.as(MClassType).mclass
462 mtypes_by_class[c].add(e)
463 poset.add_node(e)
464 end
465
466 var casttypes_by_class = new MultiHashMap[MClass, MType]
467 for e in cast_types do
468 var c = e.undecorate.as(MClassType).mclass
469 casttypes_by_class[c].add(e)
470 poset.add_node(e)
471 end
472
473 for c1, ts1 in mtypes_by_class do
474 for c2 in c1.in_hierarchy(mainmodule).greaters do
475 var ts2 = casttypes_by_class[c2]
476 for e in ts1 do
477 for o in ts2 do
478 if e == o then continue
479 if e.is_subtype(mainmodule, null, o) then
480 poset.add_edge(e, o)
481 end
482 end
483 end
484 end
485 end
486 return poset
487 end
488
489 # Build type tables
490 fun build_type_tables(mtypes: POSet[MType]): Map[MType, Array[nullable MType]] do
491 var tables = new HashMap[MType, Array[nullable MType]]
492 for mtype in mtypes do
493 var table = new Array[nullable MType]
494 for sup in mtypes[mtype].greaters do
495 var color = type_colors[sup]
496 if table.length <= color then
497 for i in [table.length .. color[ do
498 table[i] = null
499 end
500 end
501 table[color] = sup
502 end
503 tables[mtype] = table
504 end
505 return tables
506 end
507
508
509 private fun compute_type_test_layouts(mtypes: Set[MClassType], cast_types: Set[MType]) do
510 # Group cast_type by their classes
511 var bucklets = new HashMap[MClass, Set[MType]]
512 for e in cast_types do
513 var c = e.undecorate.as(MClassType).mclass
514 if not bucklets.has_key(c) then
515 bucklets[c] = new HashSet[MType]
516 end
517 bucklets[c].add(e)
518 end
519
520 # Colorize cast_types from the class hierarchy
521 var colorer = new POSetGroupColorer[MClass, MType](class_conflict_graph, bucklets)
522 type_colors = colorer.colors
523
524 var layouts = new HashMap[MClass, Array[nullable MType]]
525 for c in runtime_type_analysis.live_classes do
526 layouts[c] = colorer.build_layout(c)
527 end
528
529 # Build the table for each live type
530 for t in mtypes do
531 # A live type use the layout of its class
532 var c = t.mclass
533 var layout = layouts[c]
534 var table = new Array[nullable MType].with_capacity(layout.length)
535 type_tables[t] = table
536
537 # For each potential super-type in the layout
538 for sup in layout do
539 if sup == null then
540 table.add null
541 else if t.is_subtype(mainmodule, null, sup) then
542 table.add sup
543 else
544 table.add null
545 end
546 end
547 end
548 end
549
550 # resolution_tables is used to perform a type resolution at runtime in O(1)
551 private fun compute_resolution_tables(mtypes: Set[MType]) do
552 # During the visit of the body of classes, live_unresolved_types are collected
553 # and associated to
554 # Collect all live_unresolved_types (visited in the body of classes)
555
556 # Determinate fo each livetype what are its possible requested anchored types
557 var mtype2unresolved = new HashMap[MClass, Set[MType]]
558 for mtype in self.runtime_type_analysis.live_types do
559 var mclass = mtype.mclass
560 var set = mtype2unresolved.get_or_null(mclass)
561 if set == null then
562 set = new HashSet[MType]
563 mtype2unresolved[mclass] = set
564 end
565 for cd in mtype.collect_mclassdefs(self.mainmodule) do
566 if self.live_unresolved_types.has_key(cd) then
567 set.add_all(self.live_unresolved_types[cd])
568 end
569 end
570 end
571
572 # Compute the table layout with the prefered method
573 var colorer = new BucketsColorer[MClass, MType]
574
575 opentype_colors = colorer.colorize(mtype2unresolved)
576 resolution_tables = self.build_resolution_tables(self.runtime_type_analysis.live_types, mtype2unresolved)
577
578 # Compile a C constant for each collected unresolved type.
579 # Either to a color, or to -1 if the unresolved type is dead (no live receiver can require it)
580 var all_unresolved = new HashSet[MType]
581 for t in self.live_unresolved_types.values do
582 all_unresolved.add_all(t)
583 end
584 var all_unresolved_types_colors = new HashMap[MType, Int]
585 for t in all_unresolved do
586 if opentype_colors.has_key(t) then
587 all_unresolved_types_colors[t] = opentype_colors[t]
588 else
589 all_unresolved_types_colors[t] = -1
590 end
591 end
592 self.compile_color_consts(all_unresolved_types_colors)
593
594 #print "tables"
595 #for k, v in unresolved_types_tables.as(not null) do
596 # print "{k}: {v.join(", ")}"
597 #end
598 #print ""
599 end
600
601 fun build_resolution_tables(elements: Set[MClassType], map: Map[MClass, Set[MType]]): Map[MClassType, Array[nullable MType]] do
602 var tables = new HashMap[MClassType, Array[nullable MType]]
603 for mclasstype in elements do
604 var mtypes = map[mclasstype.mclass]
605 var table = new Array[nullable MType]
606 for mtype in mtypes do
607 var color = opentype_colors[mtype]
608 if table.length <= color then
609 for i in [table.length .. color[ do
610 table[i] = null
611 end
612 end
613 table[color] = mtype
614 end
615 tables[mclasstype] = table
616 end
617 return tables
618 end
619
620 # Separately compile all the method definitions of the module
621 fun compile_module_to_c(mmodule: MModule)
622 do
623 var old_module = self.mainmodule
624 self.mainmodule = mmodule
625 for cd in mmodule.mclassdefs do
626 for pd in cd.mpropdefs do
627 if not pd isa MMethodDef then continue
628 if pd.msignature == null then continue # Skip broken method
629 var rta = runtime_type_analysis
630 if modelbuilder.toolcontext.opt_skip_dead_methods.value and rta != null and not rta.live_methoddefs.has(pd) then continue
631 #print "compile {pd} @ {cd} @ {mmodule}"
632 var r = pd.separate_runtime_function
633 r.compile_to_c(self)
634 var r2 = pd.virtual_runtime_function
635 if r2 != r then r2.compile_to_c(self)
636
637 # Generate trampolines
638 if modelbuilder.toolcontext.opt_trampoline_call.value then
639 r2.compile_trampolines(self)
640 end
641 end
642 end
643 self.mainmodule = old_module
644 end
645
646 # Process all introduced methods and compile some linking information (if needed)
647 fun link_mmethods
648 do
649 if not modelbuilder.toolcontext.opt_substitute_monomorph.value and not modelbuilder.toolcontext.opt_guard_call.value then return
650
651 for mmodule in mainmodule.in_importation.greaters do
652 for cd in mmodule.mclassdefs do
653 for m in cd.intro_mproperties do
654 if not m isa MMethod then continue
655 link_mmethod(m)
656 end
657 end
658 end
659 end
660
661 # Compile some linking information (if needed)
662 fun link_mmethod(m: MMethod)
663 do
664 var n2 = "CALL_" + m.const_color
665
666 # Replace monomorphic call by a direct call to the virtual implementation
667 var md = is_monomorphic(m)
668 if md != null then
669 linker_script.add("{n2} = {md.virtual_runtime_function.c_name};")
670 end
671
672 # If opt_substitute_monomorph then a trampoline is used, else a weak symbol is used
673 if modelbuilder.toolcontext.opt_guard_call.value then
674 var r = m.intro.virtual_runtime_function
675 provide_declaration(n2, "{r.c_ret} {n2}{r.c_sig} __attribute__((weak));")
676 end
677 end
678
679 # The single mmethodef called in case of monomorphism.
680 # Returns nul if dead or polymorphic.
681 fun is_monomorphic(m: MMethod): nullable MMethodDef
682 do
683 var rta = runtime_type_analysis
684 if rta == null then
685 # Without RTA, monomorphic means alone (uniq name)
686 if m.mpropdefs.length == 1 then
687 return m.mpropdefs.first
688 else
689 return null
690 end
691 else
692 # With RTA, monomorphic means only live methoddef
693 var res: nullable MMethodDef = null
694 for md in m.mpropdefs do
695 if rta.live_methoddefs.has(md) then
696 if res != null then return null
697 res = md
698 end
699 end
700 return res
701 end
702 end
703
704 # Globaly compile the type structure of a live type
705 fun compile_type_to_c(mtype: MType)
706 do
707 assert not mtype.need_anchor
708 var is_live = mtype isa MClassType and runtime_type_analysis.live_types.has(mtype)
709 var is_cast_live = runtime_type_analysis.live_cast_types.has(mtype)
710 var c_name = mtype.c_name
711 var v = new SeparateCompilerVisitor(self)
712 v.add_decl("/* runtime type {mtype} */")
713
714 # extern const struct type_X
715 self.provide_declaration("type_{c_name}", "extern const struct type type_{c_name};")
716
717 # const struct type_X
718 v.add_decl("const struct type type_{c_name} = \{")
719
720 # type id (for cast target)
721 if is_cast_live then
722 v.add_decl("{type_ids[mtype]},")
723 else
724 v.add_decl("-1, /*CAST DEAD*/")
725 end
726
727 # type name
728 v.add_decl("\"{mtype}\", /* class_name_string */")
729
730 # type color (for cast target)
731 if is_cast_live then
732 v.add_decl("{type_colors[mtype]},")
733 else
734 v.add_decl("-1, /*CAST DEAD*/")
735 end
736
737 # is_nullable bit
738 if mtype isa MNullableType then
739 v.add_decl("1,")
740 else
741 v.add_decl("0,")
742 end
743
744 # resolution table (for receiver)
745 if is_live then
746 var mclass_type = mtype.undecorate
747 assert mclass_type isa MClassType
748 if resolution_tables[mclass_type].is_empty then
749 v.add_decl("NULL, /*NO RESOLUTIONS*/")
750 else
751 compile_type_resolution_table(mtype)
752 v.require_declaration("resolution_table_{c_name}")
753 v.add_decl("&resolution_table_{c_name},")
754 end
755 else
756 v.add_decl("NULL, /*DEAD*/")
757 end
758
759 # cast table (for receiver)
760 if is_live then
761 v.add_decl("{self.type_tables[mtype].length},")
762 v.add_decl("\{")
763 for stype in self.type_tables[mtype] do
764 if stype == null then
765 v.add_decl("-1, /* empty */")
766 else
767 v.add_decl("{type_ids[stype]}, /* {stype} */")
768 end
769 end
770 v.add_decl("\},")
771 else
772 # Use -1 to indicate dead type, the info is used by --hardening
773 v.add_decl("-1, \{\}, /*DEAD TYPE*/")
774 end
775 v.add_decl("\};")
776 end
777
778 fun compile_type_resolution_table(mtype: MType) do
779
780 var mclass_type = mtype.undecorate.as(MClassType)
781
782 # extern const struct resolution_table_X resolution_table_X
783 self.provide_declaration("resolution_table_{mtype.c_name}", "extern const struct types resolution_table_{mtype.c_name};")
784
785 # const struct fts_table_X fts_table_X
786 var v = new_visitor
787 v.add_decl("const struct types resolution_table_{mtype.c_name} = \{")
788 v.add_decl("0, /* dummy */")
789 v.add_decl("\{")
790 for t in self.resolution_tables[mclass_type] do
791 if t == null then
792 v.add_decl("NULL, /* empty */")
793 else
794 # The table stores the result of the type resolution
795 # Therefore, for a receiver `mclass_type`, and a unresolved type `t`
796 # the value stored is tv.
797 var tv = t.resolve_for(mclass_type, mclass_type, self.mainmodule, true)
798 # FIXME: What typeids means here? How can a tv not be live?
799 if type_ids.has_key(tv) then
800 v.require_declaration("type_{tv.c_name}")
801 v.add_decl("&type_{tv.c_name}, /* {t}: {tv} */")
802 else
803 v.add_decl("NULL, /* empty ({t}: {tv} not a live type) */")
804 end
805 end
806 end
807 v.add_decl("\}")
808 v.add_decl("\};")
809 end
810
811 # Globally compile the table of the class mclass
812 # In a link-time optimisation compiler, tables are globally computed
813 # In a true separate compiler (a with dynamic loading) you cannot do this unfortnally
814 fun compile_class_to_c(mclass: MClass)
815 do
816 var mtype = mclass.intro.bound_mtype
817 var c_name = mclass.c_name
818
819 var v = new_visitor
820
821 var rta = runtime_type_analysis
822 var is_dead = rta != null and not rta.live_classes.has(mclass)
823 # While the class may be dead, some part of separately compiled code may use symbols associated to the class, so
824 # in order to compile and link correctly the C code, these symbols should be declared and defined.
825 var need_corpse = is_dead and mtype.is_c_primitive or mclass.kind == extern_kind or mclass.kind == enum_kind
826
827 v.add_decl("/* runtime class {c_name}: {mclass.full_name} (dead={is_dead}; need_corpse={need_corpse})*/")
828
829 # Build class vft
830 if not is_dead or need_corpse then
831 self.provide_declaration("class_{c_name}", "extern const struct class class_{c_name};")
832 v.add_decl("const struct class class_{c_name} = \{")
833 v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
834 v.add_decl("\{")
835 var vft = self.method_tables.get_or_null(mclass)
836 if vft != null then for i in [0 .. vft.length[ do
837 var mpropdef = vft[i]
838 if mpropdef == null then
839 v.add_decl("NULL, /* empty */")
840 else
841 assert mpropdef isa MMethodDef
842 if rta != null and not rta.live_methoddefs.has(mpropdef) then
843 v.add_decl("NULL, /* DEAD {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
844 continue
845 end
846 var rf = mpropdef.virtual_runtime_function
847 v.require_declaration(rf.c_name)
848 v.add_decl("(nitmethod_t){rf.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
849 end
850 end
851 v.add_decl("\}")
852 v.add_decl("\};")
853 end
854
855 if mtype.is_c_primitive or mtype.mclass.name == "Pointer" then
856 # Is a primitive type or the Pointer class, not any other extern class
857
858 if mtype.is_tagged then return
859
860 #Build instance struct
861 self.header.add_decl("struct instance_{c_name} \{")
862 self.header.add_decl("const struct type *type;")
863 self.header.add_decl("const struct class *class;")
864 self.header.add_decl("{mtype.ctype_extern} value;")
865 self.header.add_decl("\};")
866
867 # Pointer is needed by extern types, live or not
868 if is_dead and mtype.mclass.name != "Pointer" then return
869
870 #Build BOX
871 self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype_extern});")
872 v.add_decl("/* allocate {mtype} */")
873 v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype_extern} value) \{")
874 v.add("struct instance_{c_name}*res = nit_alloc(sizeof(struct instance_{c_name}));")
875 v.compiler.undead_types.add(mtype)
876 v.require_declaration("type_{c_name}")
877 v.add("res->type = &type_{c_name};")
878 v.require_declaration("class_{c_name}")
879 v.add("res->class = &class_{c_name};")
880 v.add("res->value = value;")
881 v.add("return (val*)res;")
882 v.add("\}")
883
884 # A Pointer class also need its constructor
885 if mtype.mclass.name != "Pointer" then return
886
887 v = new_visitor
888 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(const struct type* type);")
889 v.add_decl("/* allocate {mtype} */")
890 v.add_decl("{mtype.ctype} NEW_{c_name}(const struct type* type) \{")
891 if is_dead then
892 v.add_abort("{mclass} is DEAD")
893 else
894 var res = v.new_named_var(mtype, "self")
895 res.is_exact = true
896 v.add("{res} = nit_alloc(sizeof(struct instance_{mtype.c_name}));")
897 v.add("{res}->type = type;")
898 hardening_live_type(v, "type")
899 v.require_declaration("class_{c_name}")
900 v.add("{res}->class = &class_{c_name};")
901 v.add("((struct instance_{mtype.c_name}*){res})->value = NULL;")
902 v.add("return {res};")
903 end
904 v.add("\}")
905 return
906 else if mclass.name == "NativeArray" then
907 #Build instance struct
908 self.header.add_decl("struct instance_{c_name} \{")
909 self.header.add_decl("const struct type *type;")
910 self.header.add_decl("const struct class *class;")
911 # NativeArrays are just a instance header followed by a length and an array of values
912 self.header.add_decl("int length;")
913 self.header.add_decl("val* values[0];")
914 self.header.add_decl("\};")
915
916 #Build NEW
917 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(int length, const struct type* type);")
918 v.add_decl("/* allocate {mtype} */")
919 v.add_decl("{mtype.ctype} NEW_{c_name}(int length, const struct type* type) \{")
920 var res = v.get_name("self")
921 v.add_decl("struct instance_{c_name} *{res};")
922 var mtype_elt = mtype.arguments.first
923 v.add("{res} = nit_alloc(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
924 v.add("{res}->type = type;")
925 hardening_live_type(v, "type")
926 v.require_declaration("class_{c_name}")
927 v.add("{res}->class = &class_{c_name};")
928 v.add("{res}->length = length;")
929 v.add("return (val*){res};")
930 v.add("\}")
931 return
932 else if mtype.mclass.kind == extern_kind and mtype.mclass.name != "NativeString" then
933 # Is an extern class (other than Pointer and NativeString)
934 # Pointer is caught in a previous `if`, and NativeString is internal
935
936 var pointer_type = mainmodule.pointer_type
937
938 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(const struct type* type);")
939 v.add_decl("/* allocate extern {mtype} */")
940 v.add_decl("{mtype.ctype} NEW_{c_name}(const struct type* type) \{")
941 if is_dead then
942 v.add_abort("{mclass} is DEAD")
943 else
944 var res = v.new_named_var(mtype, "self")
945 res.is_exact = true
946 v.add("{res} = nit_alloc(sizeof(struct instance_{pointer_type.c_name}));")
947 v.add("{res}->type = type;")
948 hardening_live_type(v, "type")
949 v.require_declaration("class_{c_name}")
950 v.add("{res}->class = &class_{c_name};")
951 v.add("((struct instance_{pointer_type.c_name}*){res})->value = NULL;")
952 v.add("return {res};")
953 end
954 v.add("\}")
955 return
956 end
957
958 #Build NEW
959 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(const struct type* type);")
960 v.add_decl("/* allocate {mtype} */")
961 v.add_decl("{mtype.ctype} NEW_{c_name}(const struct type* type) \{")
962 if is_dead then
963 v.add_abort("{mclass} is DEAD")
964 else
965 var res = v.new_named_var(mtype, "self")
966 res.is_exact = true
967 var attrs = self.attr_tables.get_or_null(mclass)
968 if attrs == null then
969 v.add("{res} = nit_alloc(sizeof(struct instance));")
970 else
971 v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
972 end
973 v.add("{res}->type = type;")
974 hardening_live_type(v, "type")
975 v.require_declaration("class_{c_name}")
976 v.add("{res}->class = &class_{c_name};")
977 if attrs != null then
978 self.generate_init_attr(v, res, mtype)
979 v.set_finalizer res
980 end
981 v.add("return {res};")
982 end
983 v.add("\}")
984 end
985
986 # Compile structures used to map tagged primitive values to their classes and types.
987 # This method also determines which class will be tagged.
988 fun compile_class_infos
989 do
990 if modelbuilder.toolcontext.opt_no_tag_primitives.value then return
991
992 # Note: if you change the tagging scheme, do not forget to update
993 # `autobox` and `extract_tag`
994 var class_info = new Array[nullable MClass].filled_with(null, 4)
995 for t in box_kinds.keys do
996 # Note: a same class can be associated to multiple slots if one want to
997 # use some Huffman coding.
998 if t.name == "Int" then
999 class_info[1] = t
1000 else if t.name == "Char" then
1001 class_info[2] = t
1002 else if t.name == "Bool" then
1003 class_info[3] = t
1004 else
1005 continue
1006 end
1007 t.mclass_type.is_tagged = true
1008 end
1009
1010 # Compile the table for classes. The tag is used as an index
1011 var v = self.new_visitor
1012 v.add_decl "const struct class *class_info[4] = \{"
1013 for t in class_info do
1014 if t == null then
1015 v.add_decl("NULL,")
1016 else
1017 var s = "class_{t.c_name}"
1018 v.require_declaration(s)
1019 v.add_decl("&{s},")
1020 end
1021 end
1022 v.add_decl("\};")
1023
1024 # Compile the table for types. The tag is used as an index
1025 v.add_decl "const struct type *type_info[4] = \{"
1026 for t in class_info do
1027 if t == null then
1028 v.add_decl("NULL,")
1029 else
1030 var s = "type_{t.c_name}"
1031 undead_types.add(t.mclass_type)
1032 v.require_declaration(s)
1033 v.add_decl("&{s},")
1034 end
1035 end
1036 v.add_decl("\};")
1037 end
1038
1039 # Add a dynamic test to ensure that the type referenced by `t` is a live type
1040 fun hardening_live_type(v: VISITOR, t: String)
1041 do
1042 if not v.compiler.modelbuilder.toolcontext.opt_hardening.value then return
1043 v.add("if({t} == NULL) \{")
1044 v.add_abort("type null")
1045 v.add("\}")
1046 v.add("if({t}->table_size < 0) \{")
1047 v.add("PRINT_ERROR(\"Insantiation of a dead type: %s\\n\", {t}->name);")
1048 v.add_abort("type dead")
1049 v.add("\}")
1050 end
1051
1052 redef fun new_visitor do return new SeparateCompilerVisitor(self)
1053
1054 # Stats
1055
1056 private var type_tables: Map[MType, Array[nullable MType]] = new HashMap[MType, Array[nullable MType]]
1057 private var resolution_tables: Map[MClassType, Array[nullable MType]] = new HashMap[MClassType, Array[nullable MType]]
1058 protected var method_tables: Map[MClass, Array[nullable MPropDef]] = new HashMap[MClass, Array[nullable MPropDef]]
1059 protected var attr_tables: Map[MClass, Array[nullable MProperty]] = new HashMap[MClass, Array[nullable MProperty]]
1060
1061 redef fun display_stats
1062 do
1063 super
1064 if self.modelbuilder.toolcontext.opt_tables_metrics.value then
1065 display_sizes
1066 end
1067 if self.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
1068 display_isset_checks
1069 end
1070 var tc = self.modelbuilder.toolcontext
1071 tc.info("# implementation of method invocation",2)
1072 var nb_invok_total = modelbuilder.nb_invok_by_tables + modelbuilder.nb_invok_by_direct + modelbuilder.nb_invok_by_inline
1073 tc.info("total number of invocations: {nb_invok_total}",2)
1074 tc.info("invocations by VFT send: {modelbuilder.nb_invok_by_tables} ({div(modelbuilder.nb_invok_by_tables,nb_invok_total)}%)",2)
1075 tc.info("invocations by direct call: {modelbuilder.nb_invok_by_direct} ({div(modelbuilder.nb_invok_by_direct,nb_invok_total)}%)",2)
1076 tc.info("invocations by inlining: {modelbuilder.nb_invok_by_inline} ({div(modelbuilder.nb_invok_by_inline,nb_invok_total)}%)",2)
1077 end
1078
1079 fun display_sizes
1080 do
1081 print "# size of subtyping tables"
1082 print "\ttotal \tholes"
1083 var total = 0
1084 var holes = 0
1085 for t, table in type_tables do
1086 total += table.length
1087 for e in table do if e == null then holes += 1
1088 end
1089 print "\t{total}\t{holes}"
1090
1091 print "# size of resolution tables"
1092 print "\ttotal \tholes"
1093 total = 0
1094 holes = 0
1095 for t, table in resolution_tables do
1096 total += table.length
1097 for e in table do if e == null then holes += 1
1098 end
1099 print "\t{total}\t{holes}"
1100
1101 print "# size of methods tables"
1102 print "\ttotal \tholes"
1103 total = 0
1104 holes = 0
1105 for t, table in method_tables do
1106 total += table.length
1107 for e in table do if e == null then holes += 1
1108 end
1109 print "\t{total}\t{holes}"
1110
1111 print "# size of attributes tables"
1112 print "\ttotal \tholes"
1113 total = 0
1114 holes = 0
1115 for t, table in attr_tables do
1116 total += table.length
1117 for e in table do if e == null then holes += 1
1118 end
1119 print "\t{total}\t{holes}"
1120 end
1121
1122 protected var isset_checks_count = 0
1123 protected var attr_read_count = 0
1124
1125 fun display_isset_checks do
1126 print "# total number of compiled attribute reads"
1127 print "\t{attr_read_count}"
1128 print "# total number of compiled isset-checks"
1129 print "\t{isset_checks_count}"
1130 end
1131
1132 redef fun compile_nitni_structs
1133 do
1134 self.header.add_decl """
1135 struct nitni_instance \{
1136 struct nitni_instance *next,
1137 *prev; /* adjacent global references in global list */
1138 int count; /* number of time this global reference has been marked */
1139 struct instance *value;
1140 \};
1141 """
1142 super
1143 end
1144
1145 redef fun finalize_ffi_for_module(mmodule)
1146 do
1147 var old_module = self.mainmodule
1148 self.mainmodule = mmodule
1149 super
1150 self.mainmodule = old_module
1151 end
1152 end
1153
1154 # A visitor on the AST of property definition that generate the C code of a separate compilation process.
1155 class SeparateCompilerVisitor
1156 super AbstractCompilerVisitor
1157
1158 redef type COMPILER: SeparateCompiler
1159
1160 redef fun adapt_signature(m, args)
1161 do
1162 var msignature = m.msignature.resolve_for(m.mclassdef.bound_mtype, m.mclassdef.bound_mtype, m.mclassdef.mmodule, true)
1163 var recv = args.first
1164 if recv.mtype.ctype != m.mclassdef.mclass.mclass_type.ctype then
1165 args.first = self.autobox(args.first, m.mclassdef.mclass.mclass_type)
1166 end
1167 for i in [0..msignature.arity[ do
1168 var t = msignature.mparameters[i].mtype
1169 if i == msignature.vararg_rank then
1170 t = args[i+1].mtype
1171 end
1172 args[i+1] = self.autobox(args[i+1], t)
1173 end
1174 end
1175
1176 redef fun unbox_signature_extern(m, args)
1177 do
1178 var msignature = m.msignature.resolve_for(m.mclassdef.bound_mtype, m.mclassdef.bound_mtype, m.mclassdef.mmodule, true)
1179 if not m.mproperty.is_init and m.is_extern then
1180 args.first = self.unbox_extern(args.first, m.mclassdef.mclass.mclass_type)
1181 end
1182 for i in [0..msignature.arity[ do
1183 var t = msignature.mparameters[i].mtype
1184 if i == msignature.vararg_rank then
1185 t = args[i+1].mtype
1186 end
1187 if m.is_extern then args[i+1] = self.unbox_extern(args[i+1], t)
1188 end
1189 end
1190
1191 redef fun autobox(value, mtype)
1192 do
1193 if value.mtype == mtype then
1194 return value
1195 else if not value.mtype.is_c_primitive and not mtype.is_c_primitive then
1196 return value
1197 else if not value.mtype.is_c_primitive then
1198 if mtype.is_tagged then
1199 if mtype.name == "Int" then
1200 return self.new_expr("(long)({value})>>2", mtype)
1201 else if mtype.name == "Char" then
1202 return self.new_expr("(uint32_t)((long)({value})>>2)", mtype)
1203 else if mtype.name == "Bool" then
1204 return self.new_expr("(short int)((long)({value})>>2)", mtype)
1205 else
1206 abort
1207 end
1208 end
1209 return self.new_expr("((struct instance_{mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype)
1210 else if not mtype.is_c_primitive then
1211 if value.mtype.is_tagged then
1212 if value.mtype.name == "Int" then
1213 return self.new_expr("(val*)({value}<<2|1)", mtype)
1214 else if value.mtype.name == "Char" then
1215 return self.new_expr("(val*)((long)({value})<<2|2)", mtype)
1216 else if value.mtype.name == "Bool" then
1217 return self.new_expr("(val*)((long)({value})<<2|3)", mtype)
1218 else
1219 abort
1220 end
1221 end
1222 var valtype = value.mtype.as(MClassType)
1223 if mtype isa MClassType and mtype.mclass.kind == extern_kind and mtype.mclass.name != "NativeString" then
1224 valtype = compiler.mainmodule.pointer_type
1225 end
1226 var res = self.new_var(mtype)
1227 self.require_declaration("BOX_{valtype.c_name}")
1228 self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
1229 return res
1230 else if (value.mtype.ctype == "void*" and mtype.ctype == "void*") or
1231 (value.mtype.ctype == "char*" and mtype.ctype == "void*") or
1232 (value.mtype.ctype == "void*" and mtype.ctype == "char*") then
1233 return value
1234 else
1235 # Bad things will appen!
1236 var res = self.new_var(mtype)
1237 self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
1238 self.add("PRINT_ERROR(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); fatal_exit(1);")
1239 return res
1240 end
1241 end
1242
1243 redef fun unbox_extern(value, mtype)
1244 do
1245 if mtype isa MClassType and mtype.mclass.kind == extern_kind and
1246 mtype.mclass.name != "NativeString" then
1247 var pointer_type = compiler.mainmodule.pointer_type
1248 var res = self.new_var_extern(mtype)
1249 self.add "{res} = ((struct instance_{pointer_type.c_name}*){value})->value; /* unboxing {value.mtype} */"
1250 return res
1251 else
1252 return value
1253 end
1254 end
1255
1256 redef fun box_extern(value, mtype)
1257 do
1258 if mtype isa MClassType and mtype.mclass.kind == extern_kind and
1259 mtype.mclass.name != "NativeString" then
1260 var valtype = compiler.mainmodule.pointer_type
1261 var res = self.new_var(mtype)
1262 compiler.undead_types.add(mtype)
1263 self.require_declaration("BOX_{valtype.c_name}")
1264 self.add("{res} = BOX_{valtype.c_name}({value}); /* boxing {value.mtype} */")
1265 self.require_declaration("type_{mtype.c_name}")
1266 self.add("{res}->type = &type_{mtype.c_name};")
1267 self.require_declaration("class_{mtype.c_name}")
1268 self.add("{res}->class = &class_{mtype.c_name};")
1269 return res
1270 else
1271 return value
1272 end
1273 end
1274
1275 # Returns a C expression containing the tag of the value as a long.
1276 #
1277 # If the C expression is evaluated to 0, it means there is no tag.
1278 # Thus the expression can be used as a condition.
1279 fun extract_tag(value: RuntimeVariable): String
1280 do
1281 assert not value.mtype.is_c_primitive
1282 return "((long){value}&3)" # Get the two low bits
1283 end
1284
1285 # Returns a C expression of the runtime class structure of the value.
1286 # The point of the method is to work also with primitive types.
1287 fun class_info(value: RuntimeVariable): String
1288 do
1289 if not value.mtype.is_c_primitive then
1290 if can_be_primitive(value) and not compiler.modelbuilder.toolcontext.opt_no_tag_primitives.value then
1291 var tag = extract_tag(value)
1292 return "({tag}?class_info[{tag}]:{value}->class)"
1293 end
1294 return "{value}->class"
1295 else
1296 compiler.undead_types.add(value.mtype)
1297 self.require_declaration("class_{value.mtype.c_name}")
1298 return "(&class_{value.mtype.c_name})"
1299 end
1300 end
1301
1302 # Returns a C expression of the runtime type structure of the value.
1303 # The point of the method is to work also with primitive types.
1304 fun type_info(value: RuntimeVariable): String
1305 do
1306 if not value.mtype.is_c_primitive then
1307 if can_be_primitive(value) and not compiler.modelbuilder.toolcontext.opt_no_tag_primitives.value then
1308 var tag = extract_tag(value)
1309 return "({tag}?type_info[{tag}]:{value}->type)"
1310 end
1311 return "{value}->type"
1312 else
1313 compiler.undead_types.add(value.mtype)
1314 self.require_declaration("type_{value.mtype.c_name}")
1315 return "(&type_{value.mtype.c_name})"
1316 end
1317 end
1318
1319 redef fun compile_callsite(callsite, args)
1320 do
1321 var rta = compiler.runtime_type_analysis
1322 # TODO: Inlining of new-style constructors with initializers
1323 if compiler.modelbuilder.toolcontext.opt_direct_call_monomorph.value and rta != null and callsite.mpropdef.initializers.is_empty then
1324 var tgs = rta.live_targets(callsite)
1325 if tgs.length == 1 then
1326 return direct_call(tgs.first, args)
1327 end
1328 end
1329 # Shortcut intern methods as they are not usually redefinable
1330 if callsite.mpropdef.is_intern and callsite.mproperty.name != "object_id" then
1331 # `object_id` is the only redefined intern method, so it can not be directly called.
1332 # TODO find a less ugly approach?
1333 return direct_call(callsite.mpropdef, args)
1334 end
1335 return super
1336 end
1337
1338 # Fully and directly call a mpropdef
1339 #
1340 # This method is used by `compile_callsite`
1341 private fun direct_call(mpropdef: MMethodDef, args: Array[RuntimeVariable]): nullable RuntimeVariable
1342 do
1343 var res0 = before_send(mpropdef.mproperty, args)
1344 var res = call(mpropdef, mpropdef.mclassdef.bound_mtype, args)
1345 if res0 != null then
1346 assert res != null
1347 self.assign(res0, res)
1348 res = res0
1349 end
1350 add("\}") # close the before_send
1351 return res
1352 end
1353 redef fun send(mmethod, arguments)
1354 do
1355 if arguments.first.mcasttype.is_c_primitive then
1356 # In order to shortcut the primitive, we need to find the most specific method
1357 # Howverr, because of performance (no flattening), we always work on the realmainmodule
1358 var m = self.compiler.mainmodule
1359 self.compiler.mainmodule = self.compiler.realmainmodule
1360 var res = self.monomorphic_send(mmethod, arguments.first.mcasttype, arguments)
1361 self.compiler.mainmodule = m
1362 return res
1363 end
1364
1365 return table_send(mmethod, arguments, mmethod)
1366 end
1367
1368 # Handle common special cases before doing the effective method invocation
1369 # This methods handle the `==` and `!=` methods and the case of the null receiver.
1370 # Note: a { is open in the generated C, that enclose and protect the effective method invocation.
1371 # Client must not forget to close the } after them.
1372 #
1373 # The value returned is the result of the common special cases.
1374 # If not null, client must compile it with the result of their own effective method invocation.
1375 #
1376 # If `before_send` can shortcut the whole message sending, a dummy `if(0){`
1377 # is generated to cancel the effective method invocation that will follow
1378 # TODO: find a better approach
1379 private fun before_send(mmethod: MMethod, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1380 do
1381 var res: nullable RuntimeVariable = null
1382 var recv = arguments.first
1383 var consider_null = not self.compiler.modelbuilder.toolcontext.opt_no_check_null.value or mmethod.name == "==" or mmethod.name == "!="
1384 var maybenull = (recv.mcasttype isa MNullableType or recv.mcasttype isa MNullType) and consider_null
1385 if maybenull then
1386 self.add("if ({recv} == NULL) \{")
1387 if mmethod.name == "==" or mmethod.name == "is_same_instance" then
1388 res = self.new_var(bool_type)
1389 var arg = arguments[1]
1390 if arg.mcasttype isa MNullableType then
1391 self.add("{res} = ({arg} == NULL);")
1392 else if arg.mcasttype isa MNullType then
1393 self.add("{res} = 1; /* is null */")
1394 else
1395 self.add("{res} = 0; /* {arg.inspect} cannot be null */")
1396 end
1397 else if mmethod.name == "!=" then
1398 res = self.new_var(bool_type)
1399 var arg = arguments[1]
1400 if arg.mcasttype isa MNullableType then
1401 self.add("{res} = ({arg} != NULL);")
1402 else if arg.mcasttype isa MNullType then
1403 self.add("{res} = 0; /* is null */")
1404 else
1405 self.add("{res} = 1; /* {arg.inspect} cannot be null */")
1406 end
1407 else
1408 self.add_abort("Receiver is null")
1409 end
1410 self.add("\} else \{")
1411 else
1412 self.add("\{")
1413 end
1414 if not self.compiler.modelbuilder.toolcontext.opt_no_shortcut_equate.value and (mmethod.name == "==" or mmethod.name == "!=" or mmethod.name == "is_same_instance") then
1415 # Recv is not null, thus if arg is, it is easy to conclude (and respect the invariants)
1416 var arg = arguments[1]
1417 if arg.mcasttype isa MNullType then
1418 if res == null then res = self.new_var(bool_type)
1419 if mmethod.name == "!=" then
1420 self.add("{res} = 1; /* arg is null and recv is not */")
1421 else # `==` and `is_same_instance`
1422 self.add("{res} = 0; /* arg is null but recv is not */")
1423 end
1424 self.add("\}") # closes the null case
1425 self.add("if (0) \{") # what follow is useless, CC will drop it
1426 end
1427 end
1428 return res
1429 end
1430
1431 private fun table_send(mmethod: MMethod, arguments: Array[RuntimeVariable], mentity: MEntity): nullable RuntimeVariable
1432 do
1433 compiler.modelbuilder.nb_invok_by_tables += 1
1434 if compiler.modelbuilder.toolcontext.opt_invocation_metrics.value then add("count_invoke_by_tables++;")
1435
1436 assert arguments.length == mmethod.intro.msignature.arity + 1 else debug("Invalid arity for {mmethod}. {arguments.length} arguments given.")
1437
1438 var res0 = before_send(mmethod, arguments)
1439
1440 var runtime_function = mmethod.intro.virtual_runtime_function
1441 var msignature = runtime_function.called_signature
1442
1443 adapt_signature(mmethod.intro, arguments)
1444
1445 var res: nullable RuntimeVariable
1446 var ret = msignature.return_mtype
1447 if ret == null then
1448 res = null
1449 else
1450 res = self.new_var(ret)
1451 end
1452
1453 var ss = arguments.join(", ")
1454
1455 var const_color = mentity.const_color
1456 var ress
1457 if res != null then
1458 ress = "{res} = "
1459 else
1460 ress = ""
1461 end
1462 if mentity isa MMethod and compiler.modelbuilder.toolcontext.opt_direct_call_monomorph0.value then
1463 # opt_direct_call_monomorph0 is used to compare the efficiency of the alternative lookup implementation, ceteris paribus.
1464 # The difference with the non-zero option is that the monomorphism is looked-at on the mmethod level and not at the callsite level.
1465 # TODO: remove this mess and use per callsite service to detect monomorphism in a single place.
1466 var md = compiler.is_monomorphic(mentity)
1467 if md != null then
1468 var callsym = md.virtual_runtime_function.c_name
1469 self.require_declaration(callsym)
1470 self.add "{ress}{callsym}({ss}); /* {mmethod} on {arguments.first.inspect}*/"
1471 else
1472 self.require_declaration(const_color)
1473 self.add "{ress}(({runtime_function.c_funptrtype})({class_info(arguments.first)}->vft[{const_color}]))({ss}); /* {mmethod} on {arguments.first.inspect}*/"
1474 end
1475 else if mentity isa MMethod and compiler.modelbuilder.toolcontext.opt_guard_call.value then
1476 var callsym = "CALL_" + const_color
1477 self.require_declaration(callsym)
1478 self.add "if (!{callsym}) \{"
1479 self.require_declaration(const_color)
1480 self.add "{ress}(({runtime_function.c_funptrtype})({class_info(arguments.first)}->vft[{const_color}]))({ss}); /* {mmethod} on {arguments.first.inspect}*/"
1481 self.add "\} else \{"
1482 self.add "{ress}{callsym}({ss}); /* {mmethod} on {arguments.first.inspect}*/"
1483 self.add "\}"
1484 else if mentity isa MMethod and compiler.modelbuilder.toolcontext.opt_trampoline_call.value then
1485 var callsym = "CALL_" + const_color
1486 self.require_declaration(callsym)
1487 self.add "{ress}{callsym}({ss}); /* {mmethod} on {arguments.first.inspect}*/"
1488 else
1489 self.require_declaration(const_color)
1490 self.add "{ress}(({runtime_function.c_funptrtype})({class_info(arguments.first)}->vft[{const_color}]))({ss}); /* {mmethod} on {arguments.first.inspect}*/"
1491 end
1492
1493 if res0 != null then
1494 assert res != null
1495 assign(res0,res)
1496 res = res0
1497 end
1498
1499 self.add("\}") # closes the null case
1500
1501 return res
1502 end
1503
1504 redef fun call(mmethoddef, recvtype, arguments)
1505 do
1506 assert arguments.length == mmethoddef.msignature.arity + 1 else debug("Invalid arity for {mmethoddef}. {arguments.length} arguments given.")
1507
1508 var res: nullable RuntimeVariable
1509 var ret = mmethoddef.msignature.return_mtype
1510 if ret == null then
1511 res = null
1512 else
1513 ret = ret.resolve_for(mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.mmodule, true)
1514 res = self.new_var(ret)
1515 end
1516
1517 if (mmethoddef.is_intern and not compiler.modelbuilder.toolcontext.opt_no_inline_intern.value) or
1518 (compiler.modelbuilder.toolcontext.opt_inline_some_methods.value and mmethoddef.can_inline(self)) then
1519 compiler.modelbuilder.nb_invok_by_inline += 1
1520 if compiler.modelbuilder.toolcontext.opt_invocation_metrics.value then add("count_invoke_by_inline++;")
1521 var frame = new StaticFrame(self, mmethoddef, recvtype, arguments)
1522 frame.returnlabel = self.get_name("RET_LABEL")
1523 frame.returnvar = res
1524 var old_frame = self.frame
1525 self.frame = frame
1526 self.add("\{ /* Inline {mmethoddef} ({arguments.join(",")}) on {arguments.first.inspect} */")
1527 mmethoddef.compile_inside_to_c(self, arguments)
1528 self.add("{frame.returnlabel.as(not null)}:(void)0;")
1529 self.add("\}")
1530 self.frame = old_frame
1531 return res
1532 end
1533 compiler.modelbuilder.nb_invok_by_direct += 1
1534 if compiler.modelbuilder.toolcontext.opt_invocation_metrics.value then add("count_invoke_by_direct++;")
1535
1536 # Autobox arguments
1537 self.adapt_signature(mmethoddef, arguments)
1538
1539 self.require_declaration(mmethoddef.c_name)
1540 if res == null then
1541 self.add("{mmethoddef.c_name}({arguments.join(", ")}); /* Direct call {mmethoddef} on {arguments.first.inspect}*/")
1542 return null
1543 else
1544 self.add("{res} = {mmethoddef.c_name}({arguments.join(", ")});")
1545 end
1546
1547 return res
1548 end
1549
1550 redef fun supercall(m: MMethodDef, recvtype: MClassType, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1551 do
1552 if arguments.first.mcasttype.is_c_primitive then
1553 # In order to shortcut the primitive, we need to find the most specific method
1554 # However, because of performance (no flattening), we always work on the realmainmodule
1555 var main = self.compiler.mainmodule
1556 self.compiler.mainmodule = self.compiler.realmainmodule
1557 var res = self.monomorphic_super_send(m, recvtype, arguments)
1558 self.compiler.mainmodule = main
1559 return res
1560 end
1561 return table_send(m.mproperty, arguments, m)
1562 end
1563
1564 redef fun vararg_instance(mpropdef, recv, varargs, elttype)
1565 do
1566 # A vararg must be stored into an new array
1567 # The trick is that the dymaic type of the array may depends on the receiver
1568 # of the method (ie recv) if the static type is unresolved
1569 # This is more complex than usual because the unresolved type must not be resolved
1570 # with the current receiver (ie self).
1571 # Therefore to isolate the resolution from self, a local StaticFrame is created.
1572 # One can see this implementation as an inlined method of the receiver whose only
1573 # job is to allocate the array
1574 var old_frame = self.frame
1575 var frame = new StaticFrame(self, mpropdef, mpropdef.mclassdef.bound_mtype, [recv])
1576 self.frame = frame
1577 #print "required Array[{elttype}] for recv {recv.inspect}. bound=Array[{self.resolve_for(elttype, recv)}]. selfvar={frame.arguments.first.inspect}"
1578 var res = self.array_instance(varargs, elttype)
1579 self.frame = old_frame
1580 return res
1581 end
1582
1583 redef fun isset_attribute(a, recv)
1584 do
1585 self.check_recv_notnull(recv)
1586 var res = self.new_var(bool_type)
1587
1588 # What is the declared type of the attribute?
1589 var mtype = a.intro.static_mtype.as(not null)
1590 var intromclassdef = a.intro.mclassdef
1591 mtype = mtype.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1592
1593 if mtype isa MNullableType then
1594 self.add("{res} = 1; /* easy isset: {a} on {recv.inspect} */")
1595 return res
1596 end
1597
1598 self.require_declaration(a.const_color)
1599 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
1600 self.add("{res} = {recv}->attrs[{a.const_color}] != NULL; /* {a} on {recv.inspect}*/")
1601 else
1602
1603 if not mtype.is_c_primitive and not mtype.is_tagged then
1604 self.add("{res} = {recv}->attrs[{a.const_color}].val != NULL; /* {a} on {recv.inspect} */")
1605 else
1606 self.add("{res} = 1; /* NOT YET IMPLEMENTED: isset of primitives: {a} on {recv.inspect} */")
1607 end
1608 end
1609 return res
1610 end
1611
1612 redef fun read_attribute(a, recv)
1613 do
1614 self.check_recv_notnull(recv)
1615
1616 # What is the declared type of the attribute?
1617 var ret = a.intro.static_mtype.as(not null)
1618 var intromclassdef = a.intro.mclassdef
1619 ret = ret.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1620
1621 if self.compiler.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
1622 self.compiler.attr_read_count += 1
1623 self.add("count_attr_reads++;")
1624 end
1625
1626 self.require_declaration(a.const_color)
1627 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
1628 # Get the attribute or a box (ie. always a val*)
1629 var cret = self.object_type.as_nullable
1630 var res = self.new_var(cret)
1631 res.mcasttype = ret
1632
1633 self.add("{res} = {recv}->attrs[{a.const_color}]; /* {a} on {recv.inspect} */")
1634
1635 # Check for Uninitialized attribute
1636 if not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_attr_isset.value then
1637 self.add("if (unlikely({res} == NULL)) \{")
1638 self.add_abort("Uninitialized attribute {a.name}")
1639 self.add("\}")
1640
1641 if self.compiler.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
1642 self.compiler.isset_checks_count += 1
1643 self.add("count_isset_checks++;")
1644 end
1645 end
1646
1647 # Return the attribute or its unboxed version
1648 # Note: it is mandatory since we reuse the box on write, we do not whant that the box escapes
1649 return self.autobox(res, ret)
1650 else
1651 var res = self.new_var(ret)
1652 self.add("{res} = {recv}->attrs[{a.const_color}].{ret.ctypename}; /* {a} on {recv.inspect} */")
1653
1654 # Check for Uninitialized attribute
1655 if not ret.is_c_primitive and not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_attr_isset.value then
1656 self.add("if (unlikely({res} == NULL)) \{")
1657 self.add_abort("Uninitialized attribute {a.name}")
1658 self.add("\}")
1659 if self.compiler.modelbuilder.toolcontext.opt_isset_checks_metrics.value then
1660 self.compiler.isset_checks_count += 1
1661 self.add("count_isset_checks++;")
1662 end
1663 end
1664
1665 return res
1666 end
1667 end
1668
1669 redef fun write_attribute(a, recv, value)
1670 do
1671 self.check_recv_notnull(recv)
1672
1673 # What is the declared type of the attribute?
1674 var mtype = a.intro.static_mtype.as(not null)
1675 var intromclassdef = a.intro.mclassdef
1676 mtype = mtype.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1677
1678 # Adapt the value to the declared type
1679 value = self.autobox(value, mtype)
1680
1681 self.require_declaration(a.const_color)
1682 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
1683 var attr = "{recv}->attrs[{a.const_color}]"
1684 if mtype.is_tagged then
1685 # The attribute is not primitive, thus store it as tagged
1686 var tv = autobox(value, compiler.mainmodule.object_type)
1687 self.add("{attr} = {tv}; /* {a} on {recv.inspect} */")
1688 else if mtype.is_c_primitive then
1689 assert mtype isa MClassType
1690 # The attribute is primitive, thus we store it in a box
1691 # The trick is to create the box the first time then resuse the box
1692 self.add("if ({attr} != NULL) \{")
1693 self.add("((struct instance_{mtype.c_name}*){attr})->value = {value}; /* {a} on {recv.inspect} */")
1694 self.add("\} else \{")
1695 value = self.autobox(value, self.object_type.as_nullable)
1696 self.add("{attr} = {value}; /* {a} on {recv.inspect} */")
1697 self.add("\}")
1698 else
1699 # The attribute is not primitive, thus store it direclty
1700 self.add("{attr} = {value}; /* {a} on {recv.inspect} */")
1701 end
1702 else
1703 self.add("{recv}->attrs[{a.const_color}].{mtype.ctypename} = {value}; /* {a} on {recv.inspect} */")
1704 end
1705 end
1706
1707 # Check that mtype is a live open type
1708 fun hardening_live_open_type(mtype: MType)
1709 do
1710 if not compiler.modelbuilder.toolcontext.opt_hardening.value then return
1711 self.require_declaration(mtype.const_color)
1712 var col = mtype.const_color
1713 self.add("if({col} == -1) \{")
1714 self.add("PRINT_ERROR(\"Resolution of a dead open type: %s\\n\", \"{mtype.to_s.escape_to_c}\");")
1715 self.add_abort("open type dead")
1716 self.add("\}")
1717 end
1718
1719 # Check that mtype it a pointer to a live cast type
1720 fun hardening_cast_type(t: String)
1721 do
1722 if not compiler.modelbuilder.toolcontext.opt_hardening.value then return
1723 add("if({t} == NULL) \{")
1724 add_abort("cast type null")
1725 add("\}")
1726 add("if({t}->id == -1 || {t}->color == -1) \{")
1727 add("PRINT_ERROR(\"Try to cast on a dead cast type: %s\\n\", {t}->name);")
1728 add_abort("cast type dead")
1729 add("\}")
1730 end
1731
1732 redef fun init_instance(mtype)
1733 do
1734 self.require_declaration("NEW_{mtype.mclass.c_name}")
1735 var compiler = self.compiler
1736 if mtype isa MGenericType and mtype.need_anchor then
1737 hardening_live_open_type(mtype)
1738 link_unresolved_type(self.frame.mpropdef.mclassdef, mtype)
1739 var recv = self.frame.arguments.first
1740 var recv_type_info = self.type_info(recv)
1741 self.require_declaration(mtype.const_color)
1742 return self.new_expr("NEW_{mtype.mclass.c_name}({recv_type_info}->resolution_table->types[{mtype.const_color}])", mtype)
1743 end
1744 compiler.undead_types.add(mtype)
1745 self.require_declaration("type_{mtype.c_name}")
1746 return self.new_expr("NEW_{mtype.mclass.c_name}(&type_{mtype.c_name})", mtype)
1747 end
1748
1749 redef fun type_test(value, mtype, tag)
1750 do
1751 self.add("/* {value.inspect} isa {mtype} */")
1752 var compiler = self.compiler
1753
1754 var recv = self.frame.arguments.first
1755 var recv_type_info = self.type_info(recv)
1756
1757 var res = self.new_var(bool_type)
1758
1759 var cltype = self.get_name("cltype")
1760 self.add_decl("int {cltype};")
1761 var idtype = self.get_name("idtype")
1762 self.add_decl("int {idtype};")
1763
1764 var maybe_null = self.maybe_null(value)
1765 var accept_null = "0"
1766 var ntype = mtype
1767 if ntype isa MNullableType then
1768 ntype = ntype.mtype
1769 accept_null = "1"
1770 end
1771
1772 if value.mcasttype.is_subtype(self.frame.mpropdef.mclassdef.mmodule, self.frame.mpropdef.mclassdef.bound_mtype, mtype) then
1773 self.add("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
1774 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1775 self.compiler.count_type_test_skipped[tag] += 1
1776 self.add("count_type_test_skipped_{tag}++;")
1777 end
1778 return res
1779 end
1780
1781 if ntype.need_anchor then
1782 var type_struct = self.get_name("type_struct")
1783 self.add_decl("const struct type* {type_struct};")
1784
1785 # Either with resolution_table with a direct resolution
1786 hardening_live_open_type(mtype)
1787 link_unresolved_type(self.frame.mpropdef.mclassdef, mtype)
1788 self.require_declaration(mtype.const_color)
1789 self.add("{type_struct} = {recv_type_info}->resolution_table->types[{mtype.const_color}];")
1790 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1791 self.compiler.count_type_test_unresolved[tag] += 1
1792 self.add("count_type_test_unresolved_{tag}++;")
1793 end
1794 hardening_cast_type(type_struct)
1795 self.add("{cltype} = {type_struct}->color;")
1796 self.add("{idtype} = {type_struct}->id;")
1797 if maybe_null and accept_null == "0" then
1798 var is_nullable = self.get_name("is_nullable")
1799 self.add_decl("short int {is_nullable};")
1800 self.add("{is_nullable} = {type_struct}->is_nullable;")
1801 accept_null = is_nullable.to_s
1802 end
1803 else if ntype isa MClassType then
1804 compiler.undead_types.add(mtype)
1805 self.require_declaration("type_{mtype.c_name}")
1806 hardening_cast_type("(&type_{mtype.c_name})")
1807 self.add("{cltype} = type_{mtype.c_name}.color;")
1808 self.add("{idtype} = type_{mtype.c_name}.id;")
1809 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1810 self.compiler.count_type_test_resolved[tag] += 1
1811 self.add("count_type_test_resolved_{tag}++;")
1812 end
1813 else
1814 self.add("PRINT_ERROR(\"NOT YET IMPLEMENTED: type_test(%s, {mtype}).\\n\", \"{value.inspect}\"); fatal_exit(1);")
1815 end
1816
1817 # check color is in table
1818 if maybe_null then
1819 self.add("if({value} == NULL) \{")
1820 self.add("{res} = {accept_null};")
1821 self.add("\} else \{")
1822 end
1823 var value_type_info = self.type_info(value)
1824 self.add("if({cltype} >= {value_type_info}->table_size) \{")
1825 self.add("{res} = 0;")
1826 self.add("\} else \{")
1827 self.add("{res} = {value_type_info}->type_table[{cltype}] == {idtype};")
1828 self.add("\}")
1829 if maybe_null then
1830 self.add("\}")
1831 end
1832
1833 return res
1834 end
1835
1836 redef fun is_same_type_test(value1, value2)
1837 do
1838 var res = self.new_var(bool_type)
1839 # Swap values to be symetric
1840 if value2.mtype.is_c_primitive and not value1.mtype.is_c_primitive then
1841 var tmp = value1
1842 value1 = value2
1843 value2 = tmp
1844 end
1845 if value1.mtype.is_c_primitive then
1846 if value2.mtype == value1.mtype then
1847 self.add("{res} = 1; /* is_same_type_test: compatible types {value1.mtype} vs. {value2.mtype} */")
1848 else if value2.mtype.is_c_primitive then
1849 self.add("{res} = 0; /* is_same_type_test: incompatible types {value1.mtype} vs. {value2.mtype}*/")
1850 else
1851 var mtype1 = value1.mtype.as(MClassType)
1852 self.require_declaration("class_{mtype1.c_name}")
1853 self.add("{res} = ({value2} != NULL) && ({value2}->class == &class_{mtype1.c_name}); /* is_same_type_test */")
1854 end
1855 else
1856 self.add("{res} = ({value1} == {value2}) || ({value1} != NULL && {value2} != NULL && {class_info(value1)} == {class_info(value2)}); /* is_same_type_test */")
1857 end
1858 return res
1859 end
1860
1861 redef fun class_name_string(value)
1862 do
1863 var res = self.get_name("var_class_name")
1864 self.add_decl("const char* {res};")
1865 if not value.mtype.is_c_primitive then
1866 self.add "{res} = {value} == NULL ? \"null\" : {type_info(value)}->name;"
1867 else if value.mtype isa MClassType and value.mtype.as(MClassType).mclass.kind == extern_kind and
1868 value.mtype.as(MClassType).name != "NativeString" then
1869 self.add "{res} = \"{value.mtype.as(MClassType).mclass}\";"
1870 else
1871 self.require_declaration("type_{value.mtype.c_name}")
1872 self.add "{res} = type_{value.mtype.c_name}.name;"
1873 end
1874 return res
1875 end
1876
1877 redef fun equal_test(value1, value2)
1878 do
1879 var res = self.new_var(bool_type)
1880 if value2.mtype.is_c_primitive and not value1.mtype.is_c_primitive then
1881 var tmp = value1
1882 value1 = value2
1883 value2 = tmp
1884 end
1885 if value1.mtype.is_c_primitive then
1886 if value2.mtype == value1.mtype then
1887 self.add("{res} = {value1} == {value2};")
1888 else if value2.mtype.is_c_primitive then
1889 self.add("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1890 else if value1.mtype.is_tagged then
1891 self.add("{res} = ({value2} != NULL) && ({self.autobox(value2, value1.mtype)} == {value1});")
1892 else
1893 var mtype1 = value1.mtype.as(MClassType)
1894 self.require_declaration("class_{mtype1.c_name}")
1895 self.add("{res} = ({value2} != NULL) && ({value2}->class == &class_{mtype1.c_name});")
1896 self.add("if ({res}) \{")
1897 self.add("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1898 self.add("\}")
1899 end
1900 return res
1901 end
1902 var maybe_null = true
1903 var test = new Array[String]
1904 var t1 = value1.mcasttype
1905 if t1 isa MNullableType then
1906 test.add("{value1} != NULL")
1907 t1 = t1.mtype
1908 else
1909 maybe_null = false
1910 end
1911 var t2 = value2.mcasttype
1912 if t2 isa MNullableType then
1913 test.add("{value2} != NULL")
1914 t2 = t2.mtype
1915 else
1916 maybe_null = false
1917 end
1918
1919 var incompatible = false
1920 var primitive
1921 if t1.is_c_primitive then
1922 primitive = t1
1923 if t1 == t2 then
1924 # No need to compare class
1925 else if t2.is_c_primitive then
1926 incompatible = true
1927 else if can_be_primitive(value2) then
1928 if t1.is_tagged then
1929 self.add("{res} = {value1} == {value2};")
1930 return res
1931 end
1932 if not compiler.modelbuilder.toolcontext.opt_no_tag_primitives.value then
1933 test.add("(!{extract_tag(value2)})")
1934 end
1935 test.add("{value1}->class == {value2}->class")
1936 else
1937 incompatible = true
1938 end
1939 else if t2.is_c_primitive then
1940 primitive = t2
1941 if can_be_primitive(value1) then
1942 if t2.is_tagged then
1943 self.add("{res} = {value1} == {value2};")
1944 return res
1945 end
1946 if not compiler.modelbuilder.toolcontext.opt_no_tag_primitives.value then
1947 test.add("(!{extract_tag(value1)})")
1948 end
1949 test.add("{value1}->class == {value2}->class")
1950 else
1951 incompatible = true
1952 end
1953 else
1954 primitive = null
1955 end
1956
1957 if incompatible then
1958 if maybe_null then
1959 self.add("{res} = {value1} == {value2}; /* incompatible types {t1} vs. {t2}; but may be NULL*/")
1960 return res
1961 else
1962 self.add("{res} = 0; /* incompatible types {t1} vs. {t2}; cannot be NULL */")
1963 return res
1964 end
1965 end
1966 if primitive != null then
1967 if primitive.is_tagged then
1968 self.add("{res} = {value1} == {value2};")
1969 return res
1970 end
1971 test.add("((struct instance_{primitive.c_name}*){value1})->value == ((struct instance_{primitive.c_name}*){value2})->value")
1972 else if can_be_primitive(value1) and can_be_primitive(value2) then
1973 if not compiler.modelbuilder.toolcontext.opt_no_tag_primitives.value then
1974 test.add("(!{extract_tag(value1)}) && (!{extract_tag(value2)})")
1975 end
1976 test.add("{value1}->class == {value2}->class")
1977 var s = new Array[String]
1978 for t, v in self.compiler.box_kinds do
1979 if t.mclass_type.is_tagged then continue
1980 s.add "({value1}->class->box_kind == {v} && ((struct instance_{t.c_name}*){value1})->value == ((struct instance_{t.c_name}*){value2})->value)"
1981 end
1982 if s.is_empty then
1983 self.add("{res} = {value1} == {value2};")
1984 return res
1985 end
1986 test.add("({s.join(" || ")})")
1987 else
1988 self.add("{res} = {value1} == {value2};")
1989 return res
1990 end
1991 self.add("{res} = {value1} == {value2} || ({test.join(" && ")});")
1992 return res
1993 end
1994
1995 fun can_be_primitive(value: RuntimeVariable): Bool
1996 do
1997 var t = value.mcasttype.undecorate
1998 if not t isa MClassType then return false
1999 var k = t.mclass.kind
2000 return k == interface_kind or t.is_c_primitive
2001 end
2002
2003 fun maybe_null(value: RuntimeVariable): Bool
2004 do
2005 var t = value.mcasttype
2006 return t isa MNullableType or t isa MNullType
2007 end
2008
2009 redef fun array_instance(array, elttype)
2010 do
2011 var nclass = mmodule.native_array_class
2012 var arrayclass = mmodule.array_class
2013 var arraytype = arrayclass.get_mtype([elttype])
2014 var res = self.init_instance(arraytype)
2015 self.add("\{ /* {res} = array_instance Array[{elttype}] */")
2016 var length = self.int_instance(array.length)
2017 var nat = native_array_instance(elttype, length)
2018 for i in [0..array.length[ do
2019 var r = self.autobox(array[i], self.object_type)
2020 self.add("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
2021 end
2022 self.send(self.get_property("with_native", arrayclass.intro.bound_mtype), [res, nat, length])
2023 self.add("\}")
2024 return res
2025 end
2026
2027 redef fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable
2028 do
2029 var mtype = mmodule.native_array_type(elttype)
2030 self.require_declaration("NEW_{mtype.mclass.c_name}")
2031 assert mtype isa MGenericType
2032 var compiler = self.compiler
2033 length = autobox(length, compiler.mainmodule.int_type)
2034 if mtype.need_anchor then
2035 hardening_live_open_type(mtype)
2036 link_unresolved_type(self.frame.mpropdef.mclassdef, mtype)
2037 var recv = self.frame.arguments.first
2038 var recv_type_info = self.type_info(recv)
2039 self.require_declaration(mtype.const_color)
2040 return self.new_expr("NEW_{mtype.mclass.c_name}({length}, {recv_type_info}->resolution_table->types[{mtype.const_color}])", mtype)
2041 end
2042 compiler.undead_types.add(mtype)
2043 self.require_declaration("type_{mtype.c_name}")
2044 return self.new_expr("NEW_{mtype.mclass.c_name}({length}, &type_{mtype.c_name})", mtype)
2045 end
2046
2047 redef fun native_array_def(pname, ret_type, arguments)
2048 do
2049 var elttype = arguments.first.mtype
2050 var nclass = mmodule.native_array_class
2051 var recv = "((struct instance_{nclass.c_name}*){arguments[0]})->values"
2052 if pname == "[]" then
2053 # Because the objects are boxed, return the box to avoid unnecessary (or broken) unboxing/reboxing
2054 var res = self.new_expr("{recv}[{arguments[1]}]", compiler.mainmodule.object_type)
2055 res.mcasttype = ret_type.as(not null)
2056 self.ret(res)
2057 return
2058 else if pname == "[]=" then
2059 self.add("{recv}[{arguments[1]}]={arguments[2]};")
2060 return
2061 else if pname == "length" then
2062 self.ret(self.new_expr("((struct instance_{nclass.c_name}*){arguments[0]})->length", ret_type.as(not null)))
2063 return
2064 else if pname == "copy_to" then
2065 var recv1 = "((struct instance_{nclass.c_name}*){arguments[1]})->values"
2066 self.add("memmove({recv1}, {recv}, {arguments[2]}*sizeof({elttype.ctype}));")
2067 return
2068 end
2069 end
2070
2071 redef fun native_array_get(nat, i)
2072 do
2073 var nclass = mmodule.native_array_class
2074 var recv = "((struct instance_{nclass.c_name}*){nat})->values"
2075 # Because the objects are boxed, return the box to avoid unnecessary (or broken) unboxing/reboxing
2076 var res = self.new_expr("{recv}[{i}]", compiler.mainmodule.object_type)
2077 return res
2078 end
2079
2080 redef fun native_array_set(nat, i, val)
2081 do
2082 var nclass = mmodule.native_array_class
2083 var recv = "((struct instance_{nclass.c_name}*){nat})->values"
2084 self.add("{recv}[{i}]={val};")
2085 end
2086
2087 fun link_unresolved_type(mclassdef: MClassDef, mtype: MType) do
2088 assert mtype.need_anchor
2089 var compiler = self.compiler
2090 if not compiler.live_unresolved_types.has_key(self.frame.mpropdef.mclassdef) then
2091 compiler.live_unresolved_types[self.frame.mpropdef.mclassdef] = new HashSet[MType]
2092 end
2093 compiler.live_unresolved_types[self.frame.mpropdef.mclassdef].add(mtype)
2094 end
2095 end
2096
2097 redef class MMethodDef
2098 # The C function associated to a mmethoddef
2099 fun separate_runtime_function: SeparateRuntimeFunction
2100 do
2101 var res = self.separate_runtime_function_cache
2102 if res == null then
2103 var recv = mclassdef.bound_mtype
2104 var msignature = msignature.resolve_for(recv, recv, mclassdef.mmodule, true)
2105 res = new SeparateRuntimeFunction(self, recv, msignature, c_name)
2106 self.separate_runtime_function_cache = res
2107 end
2108 return res
2109 end
2110 private var separate_runtime_function_cache: nullable SeparateRuntimeFunction
2111
2112 # The C function associated to a mmethoddef, that can be stored into a VFT of a class
2113 # The first parameter (the reciever) is always typed by val* in order to accept an object value
2114 # The C-signature is always compatible with the intro
2115 fun virtual_runtime_function: SeparateRuntimeFunction
2116 do
2117 var res = self.virtual_runtime_function_cache
2118 if res == null then
2119 # Because the function is virtual, the signature must match the one of the original class
2120 var intromclassdef = mproperty.intro.mclassdef
2121 var recv = intromclassdef.bound_mtype
2122
2123 res = separate_runtime_function
2124 if res.called_recv == recv then
2125 self.virtual_runtime_function_cache = res
2126 return res
2127 end
2128
2129 var msignature = mproperty.intro.msignature.resolve_for(recv, recv, intromclassdef.mmodule, true)
2130
2131 if recv.ctype == res.called_recv.ctype and msignature.c_equiv(res.called_signature) then
2132 self.virtual_runtime_function_cache = res
2133 return res
2134 end
2135
2136 res = new SeparateRuntimeFunction(self, recv, msignature, "VIRTUAL_{c_name}")
2137 self.virtual_runtime_function_cache = res
2138 res.is_thunk = true
2139 end
2140 return res
2141 end
2142 private var virtual_runtime_function_cache: nullable SeparateRuntimeFunction
2143 end
2144
2145 redef class MSignature
2146 # Does the C-version of `self` the same than the C-version of `other`?
2147 fun c_equiv(other: MSignature): Bool
2148 do
2149 if self == other then return true
2150 if arity != other.arity then return false
2151 for i in [0..arity[ do
2152 if mparameters[i].mtype.ctype != other.mparameters[i].mtype.ctype then return false
2153 end
2154 if return_mtype != other.return_mtype then
2155 if return_mtype == null or other.return_mtype == null then return false
2156 if return_mtype.ctype != other.return_mtype.ctype then return false
2157 end
2158 return true
2159 end
2160 end
2161
2162 # The C function associated to a methoddef separately compiled
2163 class SeparateRuntimeFunction
2164 super AbstractRuntimeFunction
2165
2166 # The call-side static receiver
2167 var called_recv: MType
2168
2169 # The call-side static signature
2170 var called_signature: MSignature
2171
2172 # The name on the compiled method
2173 redef var build_c_name: String
2174
2175 # Statically call the original body instead
2176 var is_thunk = false
2177
2178 redef fun to_s do return self.mmethoddef.to_s
2179
2180 # The C return type (something or `void`)
2181 var c_ret: String is lazy do
2182 var ret = called_signature.return_mtype
2183 if ret != null then
2184 return ret.ctype
2185 else
2186 return "void"
2187 end
2188 end
2189
2190 # The C signature (only the parmeter part)
2191 var c_sig: String is lazy do
2192 var sig = new FlatBuffer
2193 sig.append("({called_recv.ctype} self")
2194 for i in [0..called_signature.arity[ do
2195 var mtype = called_signature.mparameters[i].mtype
2196 if i == called_signature.vararg_rank then
2197 mtype = mmethoddef.mclassdef.mmodule.array_type(mtype)
2198 end
2199 sig.append(", {mtype.ctype} p{i}")
2200 end
2201 sig.append(")")
2202 return sig.to_s
2203 end
2204
2205 # The C type for the function pointer.
2206 var c_funptrtype: String is lazy do return "{c_ret}(*){c_sig}"
2207
2208 redef fun compile_to_c(compiler)
2209 do
2210 var mmethoddef = self.mmethoddef
2211
2212 var sig = "{c_ret} {c_name}{c_sig}"
2213 compiler.provide_declaration(self.c_name, "{sig} __attribute__((weak));")
2214
2215 var rta = compiler.as(SeparateCompiler).runtime_type_analysis
2216 if rta != null and not rta.live_mmodules.has(mmethoddef.mclassdef.mmodule) then
2217 return
2218 end
2219
2220 var recv = self.mmethoddef.mclassdef.bound_mtype
2221 var v = compiler.new_visitor
2222 var selfvar = new RuntimeVariable("self", called_recv, recv)
2223 var arguments = new Array[RuntimeVariable]
2224 var frame = new StaticFrame(v, mmethoddef, recv, arguments)
2225 v.frame = frame
2226
2227 var msignature = called_signature
2228 var ret = called_signature.return_mtype
2229
2230 var comment = new FlatBuffer
2231 comment.append("({selfvar}: {selfvar.mtype}")
2232 arguments.add(selfvar)
2233 for i in [0..msignature.arity[ do
2234 var mtype = msignature.mparameters[i].mtype
2235 if i == msignature.vararg_rank then
2236 mtype = v.mmodule.array_type(mtype)
2237 end
2238 comment.append(", {mtype}")
2239 var argvar = new RuntimeVariable("p{i}", mtype, mtype)
2240 arguments.add(argvar)
2241 end
2242 comment.append(")")
2243 if ret != null then
2244 comment.append(": {ret}")
2245 end
2246
2247 v.add_decl("/* method {self} for {comment} */")
2248 v.add_decl("{sig} \{")
2249 if ret != null then
2250 frame.returnvar = v.new_var(ret)
2251 end
2252 frame.returnlabel = v.get_name("RET_LABEL")
2253
2254 if is_thunk then
2255 var subret = v.call(mmethoddef, recv, arguments)
2256 if ret != null then
2257 assert subret != null
2258 v.assign(frame.returnvar.as(not null), subret)
2259 end
2260 else
2261 mmethoddef.compile_inside_to_c(v, arguments)
2262 end
2263
2264 v.add("{frame.returnlabel.as(not null)}:;")
2265 if ret != null then
2266 v.add("return {frame.returnvar.as(not null)};")
2267 end
2268 v.add("\}")
2269 compiler.names[self.c_name] = "{mmethoddef.full_name} ({mmethoddef.location.file.filename}:{mmethoddef.location.line_start})"
2270 end
2271
2272 # Compile the trampolines used to implement late-binding.
2273 #
2274 # See `opt_trampoline_call`.
2275 fun compile_trampolines(compiler: SeparateCompiler)
2276 do
2277 var recv = self.mmethoddef.mclassdef.bound_mtype
2278 var selfvar = new RuntimeVariable("self", called_recv, recv)
2279 var ret = called_signature.return_mtype
2280 var arguments = ["self"]
2281 for i in [0..called_signature.arity[ do arguments.add "p{i}"
2282
2283 if mmethoddef.is_intro and not recv.is_c_primitive then
2284 var m = mmethoddef.mproperty
2285 var n2 = "CALL_" + m.const_color
2286 compiler.provide_declaration(n2, "{c_ret} {n2}{c_sig};")
2287 var v2 = compiler.new_visitor
2288 v2.add "{c_ret} {n2}{c_sig} \{"
2289 v2.require_declaration(m.const_color)
2290 var call = "(({c_funptrtype})({v2.class_info(selfvar)}->vft[{m.const_color}]))({arguments.join(", ")});"
2291 if ret != null then
2292 v2.add "return {call}"
2293 else
2294 v2.add call
2295 end
2296
2297 v2.add "\}"
2298
2299 end
2300 if mmethoddef.has_supercall and not recv.is_c_primitive then
2301 var m = mmethoddef
2302 var n2 = "CALL_" + m.const_color
2303 compiler.provide_declaration(n2, "{c_ret} {n2}{c_sig};")
2304 var v2 = compiler.new_visitor
2305 v2.add "{c_ret} {n2}{c_sig} \{"
2306 v2.require_declaration(m.const_color)
2307 var call = "(({c_funptrtype})({v2.class_info(selfvar)}->vft[{m.const_color}]))({arguments.join(", ")});"
2308 if ret != null then
2309 v2.add "return {call}"
2310 else
2311 v2.add call
2312 end
2313
2314 v2.add "\}"
2315 end
2316 end
2317 end
2318
2319 redef class MType
2320 # Are values of `self` tagged?
2321 # If false, it means that the type is not primitive, or is boxed.
2322 var is_tagged = false
2323 end
2324
2325 redef class MEntity
2326 var const_color: String is lazy do return "COLOR_{c_name}"
2327 end
2328
2329 interface PropertyLayoutElement end
2330
2331 redef class MProperty
2332 super PropertyLayoutElement
2333 end
2334
2335 redef class MPropDef
2336 super PropertyLayoutElement
2337 end
2338
2339 redef class AMethPropdef
2340 # The semi-global compilation does not support inlining calls to extern news
2341 redef fun can_inline
2342 do
2343 var m = mpropdef
2344 if m != null and m.mproperty.is_init and m.is_extern then return false
2345 return super
2346 end
2347 end
2348
2349 redef class AAttrPropdef
2350 redef fun init_expr(v, recv)
2351 do
2352 super
2353 if is_lazy and v.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
2354 var guard = self.mlazypropdef.mproperty
2355 v.write_attribute(guard, recv, v.bool_instance(false))
2356 end
2357 end
2358 end