nitgs: opt_direct_call_monomorph call `before_send` to deal with special cases
[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 import layout_builders
20 import rapid_type_analysis
21 import compiler_ffi
22
23 # Add separate compiler specific options
24 redef class ToolContext
25 # --separate
26 var opt_separate: OptionBool = new OptionBool("Use separate compilation", "--separate")
27 # --no-inline-intern
28 var opt_no_inline_intern: OptionBool = new OptionBool("Do not inline call to intern methods", "--no-inline-intern")
29 # --no-union-attribute
30 var opt_no_union_attribute: OptionBool = new OptionBool("Put primitive attibutes in a box instead of an union", "--no-union-attribute")
31 # --no-shortcut-equate
32 var opt_no_shortcut_equate: OptionBool = new OptionBool("Always call == in a polymorphic way", "--no-shortcut-equal")
33 # --inline-coloring-numbers
34 var opt_inline_coloring_numbers: OptionBool = new OptionBool("Inline colors and ids (semi-global)", "--inline-coloring-numbers")
35 # --inline-some-methods
36 var opt_inline_some_methods: OptionBool = new OptionBool("Allow the separate compiler to inline some methods (semi-global)", "--inline-some-methods")
37 # --direct-call-monomorph
38 var opt_direct_call_monomorph: OptionBool = new OptionBool("Allow the separate compiler to direct call monomorph sites (semi-global)", "--direct-call-monomorph")
39 # --use-naive-coloring
40 var opt_bm_typing: OptionBool = new OptionBool("Colorize items incrementaly, used to simulate binary matrix typing", "--bm-typing")
41 # --use-mod-perfect-hashing
42 var opt_phmod_typing: OptionBool = new OptionBool("Replace coloration by perfect hashing (with mod operator)", "--phmod-typing")
43 # --use-and-perfect-hashing
44 var opt_phand_typing: OptionBool = new OptionBool("Replace coloration by perfect hashing (with and operator)", "--phand-typing")
45 # --tables-metrics
46 var opt_tables_metrics: OptionBool = new OptionBool("Enable static size measuring of tables used for vft, typing and resolution", "--tables-metrics")
47
48 redef init
49 do
50 super
51 self.option_context.add_option(self.opt_separate)
52 self.option_context.add_option(self.opt_no_inline_intern)
53 self.option_context.add_option(self.opt_no_union_attribute)
54 self.option_context.add_option(self.opt_no_shortcut_equate)
55 self.option_context.add_option(self.opt_inline_coloring_numbers, opt_inline_some_methods, opt_direct_call_monomorph)
56 self.option_context.add_option(self.opt_bm_typing)
57 self.option_context.add_option(self.opt_phmod_typing)
58 self.option_context.add_option(self.opt_phand_typing)
59 self.option_context.add_option(self.opt_tables_metrics)
60 end
61 end
62
63 redef class ModelBuilder
64 fun run_separate_compiler(mainmodule: MModule, runtime_type_analysis: nullable RapidTypeAnalysis)
65 do
66 var time0 = get_time
67 self.toolcontext.info("*** GENERATING C ***", 1)
68
69 var compiler = new SeparateCompiler(mainmodule, self, runtime_type_analysis)
70 compiler.compile_header
71
72 # compile class structures
73 self.toolcontext.info("Property coloring", 2)
74 compiler.new_file("{mainmodule.name}.classes")
75 compiler.do_property_coloring
76 for m in mainmodule.in_importation.greaters do
77 for mclass in m.intro_mclasses do
78 if mclass.kind == abstract_kind or mclass.kind == interface_kind then continue
79 compiler.compile_class_to_c(mclass)
80 end
81 end
82
83 # The main function of the C
84 compiler.new_file("{mainmodule.name}.main")
85 compiler.compile_main_function
86
87 # compile methods
88 for m in mainmodule.in_importation.greaters do
89 self.toolcontext.info("Generate C for module {m}", 2)
90 compiler.new_file("{m.name}.sep")
91 compiler.compile_module_to_c(m)
92 end
93
94 # compile live & cast type structures
95 self.toolcontext.info("Type coloring", 2)
96 compiler.new_file("{mainmodule.name}.types")
97 var mtypes = compiler.do_type_coloring
98 for t in mtypes do
99 compiler.compile_type_to_c(t)
100 end
101 # compile remaining types structures (useless but needed for the symbol resolution at link-time)
102 for t in compiler.undead_types do
103 if mtypes.has(t) then continue
104 compiler.compile_type_to_c(t)
105 end
106
107 compiler.display_stats
108
109 var time1 = get_time
110 self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
111 write_and_make(compiler)
112 end
113
114 # Count number of invocations by VFT
115 private var nb_invok_by_tables = 0
116 # Count number of invocations by direct call
117 private var nb_invok_by_direct = 0
118 # Count number of invocations by inlining
119 private var nb_invok_by_inline = 0
120 end
121
122 # Singleton that store the knowledge about the separate compilation process
123 class SeparateCompiler
124 super AbstractCompiler
125
126 redef type VISITOR: SeparateCompilerVisitor
127
128 # The result of the RTA (used to know live types and methods)
129 var runtime_type_analysis: nullable RapidTypeAnalysis
130
131 private var undead_types: Set[MType] = new HashSet[MType]
132 private var live_unresolved_types: Map[MClassDef, Set[MType]] = new HashMap[MClassDef, HashSet[MType]]
133
134 private var type_layout: nullable Layout[MType]
135 private var resolution_layout: nullable Layout[MType]
136 protected var method_layout: nullable Layout[PropertyLayoutElement]
137 protected var attr_layout: nullable Layout[MAttribute]
138
139 init(mainmodule: MModule, mmbuilder: ModelBuilder, runtime_type_analysis: nullable RapidTypeAnalysis) do
140 super(mainmodule, mmbuilder)
141 var file = new_file("nit.common")
142 self.header = new CodeWriter(file)
143 self.runtime_type_analysis = runtime_type_analysis
144 self.compile_box_kinds
145 end
146
147 redef fun compile_header_structs do
148 self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
149 self.compile_header_attribute_structs
150 self.header.add_decl("struct class \{ int box_kind; nitmethod_t vft[]; \}; /* general C type representing a Nit class. */")
151
152 # With resolution_table_table, all live type resolution are stored in a big table: resolution_table
153 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. */")
154 self.header.add_decl("struct instance \{ const struct type *type; const struct class *class; nitattribute_t attrs[]; \}; /* general C type representing a Nit instance. */")
155
156 if modelbuilder.toolcontext.opt_phmod_typing.value or modelbuilder.toolcontext.opt_phand_typing.value then
157 self.header.add_decl("struct types \{ int mask; const struct type *types[]; \}; /* a list types (used for vts, fts and unresolved lists). */")
158 else
159 self.header.add_decl("struct types \{ int dummy; const struct type *types[]; \}; /* a list types (used for vts, fts and unresolved lists). */")
160 end
161
162 if modelbuilder.toolcontext.opt_phmod_typing.value then
163 self.header.add_decl("#define HASH(mask, id) ((mask)%(id))")
164 else if modelbuilder.toolcontext.opt_phand_typing.value then
165 self.header.add_decl("#define HASH(mask, id) ((mask)&(id))")
166 end
167
168 self.header.add_decl("typedef struct instance val; /* general C type representing a Nit instance. */")
169 end
170
171 fun compile_header_attribute_structs
172 do
173 if modelbuilder.toolcontext.opt_no_union_attribute.value then
174 self.header.add_decl("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
175 else
176 self.header.add_decl("typedef union \{")
177 self.header.add_decl("void* val;")
178 for c, v in self.box_kinds do
179 var t = c.mclass_type
180 self.header.add_decl("{t.ctype} {t.ctypename};")
181 end
182 self.header.add_decl("\} nitattribute_t; /* general C type representing a Nit attribute. */")
183 end
184 end
185
186 fun compile_box_kinds
187 do
188 # Collect all bas box class
189 # FIXME: this is not completely fine with a separate compilation scheme
190 for classname in ["Int", "Bool", "Char", "Float", "NativeString", "Pointer"] do
191 var classes = self.mainmodule.model.get_mclasses_by_name(classname)
192 if classes == null then continue
193 assert classes.length == 1 else print classes.join(", ")
194 self.box_kinds[classes.first] = self.box_kinds.length + 1
195 end
196 end
197
198 var box_kinds = new HashMap[MClass, Int]
199
200 fun box_kind_of(mclass: MClass): Int
201 do
202 if mclass.mclass_type.ctype == "val*" then
203 return 0
204 else if mclass.kind == extern_kind then
205 return self.box_kinds[self.mainmodule.get_primitive_class("Pointer")]
206 else
207 return self.box_kinds[mclass]
208 end
209
210 end
211
212 fun compile_color_consts(colors: Map[Object, Int]) do
213 var v = new_visitor
214 for m, c in colors do
215 compile_color_const(v, m, c)
216 end
217 end
218
219 fun compile_color_const(v: SeparateCompilerVisitor, m: Object, color: Int) do
220 if color_consts_done.has(m) then return
221 if m isa MProperty then
222 if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
223 self.provide_declaration(m.const_color, "#define {m.const_color} {color}")
224 else
225 self.provide_declaration(m.const_color, "extern const int {m.const_color};")
226 v.add("const int {m.const_color} = {color};")
227 end
228 else if m isa MPropDef then
229 if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
230 self.provide_declaration(m.const_color, "#define {m.const_color} {color}")
231 else
232 self.provide_declaration(m.const_color, "extern const int {m.const_color};")
233 v.add("const int {m.const_color} = {color};")
234 end
235 else if m isa MType then
236 if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
237 self.provide_declaration(m.const_color, "#define {m.const_color} {color}")
238 else
239 self.provide_declaration(m.const_color, "extern const int {m.const_color};")
240 v.add("const int {m.const_color} = {color};")
241 end
242 end
243 color_consts_done.add(m)
244 end
245
246 private var color_consts_done = new HashSet[Object]
247
248 # colorize classe properties
249 fun do_property_coloring do
250 var mclasses = new HashSet[MClass].from(modelbuilder.model.mclasses)
251
252 # Layouts
253 var method_layout_builder: PropertyLayoutBuilder[PropertyLayoutElement]
254 var attribute_layout_builder: PropertyLayoutBuilder[MAttribute]
255 #FIXME PH and BM layouts too slow for large programs
256 #if modelbuilder.toolcontext.opt_bm_typing.value then
257 # method_layout_builder = new MMethodBMizer(self.mainmodule)
258 # attribute_layout_builder = new MAttributeBMizer(self.mainmodule)
259 #else if modelbuilder.toolcontext.opt_phmod_typing.value then
260 # method_layout_builder = new MMethodHasher(new PHModOperator, self.mainmodule)
261 # attribute_layout_builder = new MAttributeHasher(new PHModOperator, self.mainmodule)
262 #else if modelbuilder.toolcontext.opt_phand_typing.value then
263 # method_layout_builder = new MMethodHasher(new PHAndOperator, self.mainmodule)
264 # attribute_layout_builder = new MAttributeHasher(new PHAndOperator, self.mainmodule)
265 #else
266
267 var class_layout_builder = new MClassColorer(self.mainmodule)
268 class_layout_builder.build_layout(mclasses)
269 method_layout_builder = new MPropertyColorer[PropertyLayoutElement](self.mainmodule, class_layout_builder)
270 attribute_layout_builder = new MPropertyColorer[MAttribute](self.mainmodule, class_layout_builder)
271 #end
272
273 # lookup properties to build layout with
274 var mmethods = new HashMap[MClass, Set[PropertyLayoutElement]]
275 var mattributes = new HashMap[MClass, Set[MAttribute]]
276 for mclass in mclasses do
277 mmethods[mclass] = new HashSet[PropertyLayoutElement]
278 mattributes[mclass] = new HashSet[MAttribute]
279 for mprop in self.mainmodule.properties(mclass) do
280 if mprop isa MMethod then
281 mmethods[mclass].add(mprop)
282 else if mprop isa MAttribute then
283 mattributes[mclass].add(mprop)
284 end
285 end
286 end
287
288 # Collect all super calls (dead or not)
289 var all_super_calls = new HashSet[MMethodDef]
290 for mmodule in self.mainmodule.in_importation.greaters do
291 for mclassdef in mmodule.mclassdefs do
292 for mpropdef in mclassdef.mpropdefs do
293 if not mpropdef isa MMethodDef then continue
294 if mpropdef.has_supercall then
295 all_super_calls.add(mpropdef)
296 end
297 end
298 end
299 end
300
301 # lookup super calls and add it to the list of mmethods to build layout with
302 var super_calls
303 if runtime_type_analysis != null then
304 super_calls = runtime_type_analysis.live_super_sends
305 else
306 super_calls = all_super_calls
307 end
308
309 for mmethoddef in super_calls do
310 var mclass = mmethoddef.mclassdef.mclass
311 mmethods[mclass].add(mmethoddef)
312 for descendant in mclass.in_hierarchy(self.mainmodule).smallers do
313 mmethods[descendant].add(mmethoddef)
314 end
315 end
316
317 # methods coloration
318 self.method_layout = method_layout_builder.build_layout(mmethods)
319 self.method_tables = build_method_tables(mclasses, super_calls)
320 self.compile_color_consts(method_layout.pos)
321
322 # attribute null color to dead supercalls
323 for mpropdef in all_super_calls do
324 if super_calls.has(mpropdef) then continue
325 compile_color_const(new_visitor, mpropdef, -1)
326 end
327
328 # attributes coloration
329 self.attr_layout = attribute_layout_builder.build_layout(mattributes)
330 self.attr_tables = build_attr_tables(mclasses)
331 self.compile_color_consts(attr_layout.pos)
332 end
333
334 fun build_method_tables(mclasses: Set[MClass], super_calls: Set[MMethodDef]): Map[MClass, Array[nullable MPropDef]] do
335 var layout = self.method_layout
336 var tables = new HashMap[MClass, Array[nullable MPropDef]]
337 for mclass in mclasses do
338 var table = new Array[nullable MPropDef]
339 var supercalls = new List[MMethodDef]
340
341 # first, fill table from parents by reverse linearization order
342 var parents = new Array[MClass]
343 if mainmodule.flatten_mclass_hierarchy.has(mclass) then
344 parents = mclass.in_hierarchy(mainmodule).greaters.to_a
345 self.mainmodule.linearize_mclasses(parents)
346 end
347
348 for parent in parents do
349 if parent == mclass then continue
350 for mproperty in self.mainmodule.properties(parent) do
351 if not mproperty isa MMethod then continue
352 var color = layout.pos[mproperty]
353 if table.length <= color then
354 for i in [table.length .. color[ do
355 table[i] = null
356 end
357 end
358 for mpropdef in mproperty.mpropdefs do
359 if mpropdef.mclassdef.mclass == parent then
360 table[color] = mpropdef
361 end
362 end
363 end
364
365 # lookup for super calls in super classes
366 for mmethoddef in super_calls do
367 for mclassdef in parent.mclassdefs do
368 if mclassdef.mpropdefs.has(mmethoddef) then
369 supercalls.add(mmethoddef)
370 end
371 end
372 end
373 end
374
375 # then override with local properties
376 for mproperty in self.mainmodule.properties(mclass) do
377 if not mproperty isa MMethod then continue
378 var color = layout.pos[mproperty]
379 if table.length <= color then
380 for i in [table.length .. color[ do
381 table[i] = null
382 end
383 end
384 for mpropdef in mproperty.mpropdefs do
385 if mpropdef.mclassdef.mclass == mclass then
386 table[color] = mpropdef
387 end
388 end
389 end
390
391 # lookup for super calls in local class
392 for mmethoddef in super_calls do
393 for mclassdef in mclass.mclassdefs do
394 if mclassdef.mpropdefs.has(mmethoddef) then
395 supercalls.add(mmethoddef)
396 end
397 end
398 end
399 # insert super calls in table according to receiver
400 for supercall in supercalls do
401 var color = layout.pos[supercall]
402 if table.length <= color then
403 for i in [table.length .. color[ do
404 table[i] = null
405 end
406 end
407 var mmethoddef = supercall.lookup_next_definition(self.mainmodule, mclass.intro.bound_mtype)
408 table[color] = mmethoddef
409 end
410 tables[mclass] = table
411 end
412 return tables
413 end
414
415 fun build_attr_tables(mclasses: Set[MClass]): Map[MClass, Array[nullable MPropDef]] do
416 var layout = self.attr_layout
417 var tables = new HashMap[MClass, Array[nullable MPropDef]]
418 for mclass in mclasses do
419 var table = new Array[nullable MPropDef]
420 # first, fill table from parents by reverse linearization order
421 var parents = new Array[MClass]
422 if mainmodule.flatten_mclass_hierarchy.has(mclass) then
423 parents = mclass.in_hierarchy(mainmodule).greaters.to_a
424 self.mainmodule.linearize_mclasses(parents)
425 end
426 for parent in parents do
427 if parent == mclass then continue
428 for mproperty in self.mainmodule.properties(parent) do
429 if not mproperty isa MAttribute then continue
430 var color = layout.pos[mproperty]
431 if table.length <= color then
432 for i in [table.length .. color[ do
433 table[i] = null
434 end
435 end
436 for mpropdef in mproperty.mpropdefs do
437 if mpropdef.mclassdef.mclass == parent then
438 table[color] = mpropdef
439 end
440 end
441 end
442 end
443
444 # then override with local properties
445 for mproperty in self.mainmodule.properties(mclass) do
446 if not mproperty isa MAttribute then continue
447 var color = layout.pos[mproperty]
448 if table.length <= color then
449 for i in [table.length .. color[ do
450 table[i] = null
451 end
452 end
453 for mpropdef in mproperty.mpropdefs do
454 if mpropdef.mclassdef.mclass == mclass then
455 table[color] = mpropdef
456 end
457 end
458 end
459 tables[mclass] = table
460 end
461 return tables
462 end
463
464 # colorize live types of the program
465 private fun do_type_coloring: POSet[MType] do
466 var mtypes = new HashSet[MType]
467 mtypes.add_all(self.runtime_type_analysis.live_types)
468 mtypes.add_all(self.runtime_type_analysis.live_cast_types)
469 for c in self.box_kinds.keys do
470 mtypes.add(c.mclass_type)
471 end
472
473 # Typing Layout
474 var layout_builder: TypingLayoutBuilder[MType]
475 if modelbuilder.toolcontext.opt_bm_typing.value then
476 layout_builder = new MTypeBMizer(self.mainmodule)
477 else if modelbuilder.toolcontext.opt_phmod_typing.value then
478 layout_builder = new MTypeHasher(new PHModOperator, self.mainmodule)
479 else if modelbuilder.toolcontext.opt_phand_typing.value then
480 layout_builder = new MTypeHasher(new PHAndOperator, self.mainmodule)
481 else
482 layout_builder = new MTypeColorer(self.mainmodule)
483 end
484
485 # colorize types
486 self.type_layout = layout_builder.build_layout(mtypes)
487 var poset = layout_builder.poset.as(not null)
488 self.type_tables = self.build_type_tables(poset)
489
490 # VT and FT are stored with other unresolved types in the big resolution_tables
491 self.compile_resolution_tables(mtypes)
492
493 return poset
494 end
495
496 # Build type tables
497 fun build_type_tables(mtypes: POSet[MType]): Map[MType, Array[nullable MType]] do
498 var tables = new HashMap[MType, Array[nullable MType]]
499 var layout = self.type_layout
500 for mtype in mtypes do
501 var table = new Array[nullable MType]
502 for sup in mtypes[mtype].greaters do
503 var color: Int
504 if layout isa PHLayout[MType, MType] then
505 color = layout.hashes[mtype][sup]
506 else
507 color = layout.pos[sup]
508 end
509 if table.length <= color then
510 for i in [table.length .. color[ do
511 table[i] = null
512 end
513 end
514 table[color] = sup
515 end
516 tables[mtype] = table
517 end
518 return tables
519 end
520
521 protected fun compile_resolution_tables(mtypes: Set[MType]) do
522 # resolution_tables is used to perform a type resolution at runtime in O(1)
523
524 # During the visit of the body of classes, live_unresolved_types are collected
525 # and associated to
526 # Collect all live_unresolved_types (visited in the body of classes)
527
528 # Determinate fo each livetype what are its possible requested anchored types
529 var mtype2unresolved = new HashMap[MClassType, Set[MType]]
530 for mtype in self.runtime_type_analysis.live_types do
531 var set = new HashSet[MType]
532 for cd in mtype.collect_mclassdefs(self.mainmodule) do
533 if self.live_unresolved_types.has_key(cd) then
534 set.add_all(self.live_unresolved_types[cd])
535 end
536 end
537 mtype2unresolved[mtype] = set
538 end
539
540 # Compute the table layout with the prefered method
541 var resolution_builder: ResolutionLayoutBuilder
542 if modelbuilder.toolcontext.opt_bm_typing.value then
543 resolution_builder = new ResolutionBMizer
544 else if modelbuilder.toolcontext.opt_phmod_typing.value then
545 resolution_builder = new ResolutionHasher(new PHModOperator)
546 else if modelbuilder.toolcontext.opt_phand_typing.value then
547 resolution_builder = new ResolutionHasher(new PHAndOperator)
548 else
549 resolution_builder = new ResolutionColorer
550 end
551 self.resolution_layout = resolution_builder.build_layout(mtype2unresolved)
552 self.resolution_tables = self.build_resolution_tables(mtype2unresolved)
553
554 # Compile a C constant for each collected unresolved type.
555 # Either to a color, or to -1 if the unresolved type is dead (no live receiver can require it)
556 var all_unresolved = new HashSet[MType]
557 for t in self.live_unresolved_types.values do
558 all_unresolved.add_all(t)
559 end
560 var all_unresolved_types_colors = new HashMap[MType, Int]
561 for t in all_unresolved do
562 if self.resolution_layout.pos.has_key(t) then
563 all_unresolved_types_colors[t] = self.resolution_layout.pos[t]
564 else
565 all_unresolved_types_colors[t] = -1
566 end
567 end
568 self.compile_color_consts(all_unresolved_types_colors)
569
570 #print "tables"
571 #for k, v in unresolved_types_tables.as(not null) do
572 # print "{k}: {v.join(", ")}"
573 #end
574 #print ""
575 end
576
577 fun build_resolution_tables(elements: Map[MClassType, Set[MType]]): Map[MClassType, Array[nullable MType]] do
578 var tables = new HashMap[MClassType, Array[nullable MType]]
579 var layout = self.resolution_layout
580 for mclasstype, mtypes in elements do
581 var table = new Array[nullable MType]
582 for mtype in mtypes do
583 var color: Int
584 if layout isa PHLayout[MClassType, MType] then
585 color = layout.hashes[mclasstype][mtype]
586 else
587 color = layout.pos[mtype]
588 end
589 if table.length <= color then
590 for i in [table.length .. color[ do
591 table[i] = null
592 end
593 end
594 table[color] = mtype
595 end
596 tables[mclasstype] = table
597 end
598 return tables
599 end
600
601 # Separately compile all the method definitions of the module
602 fun compile_module_to_c(mmodule: MModule)
603 do
604 var old_module = self.mainmodule
605 self.mainmodule = mmodule
606 for cd in mmodule.mclassdefs do
607 for pd in cd.mpropdefs do
608 if not pd isa MMethodDef then continue
609 #print "compile {pd} @ {cd} @ {mmodule}"
610 var r = pd.separate_runtime_function
611 r.compile_to_c(self)
612 var r2 = pd.virtual_runtime_function
613 r2.compile_to_c(self)
614 end
615 end
616 self.mainmodule = old_module
617 end
618
619 # Globaly compile the type structure of a live type
620 fun compile_type_to_c(mtype: MType)
621 do
622 assert not mtype.need_anchor
623 var layout = self.type_layout
624 var is_live = mtype isa MClassType and runtime_type_analysis.live_types.has(mtype)
625 var is_cast_live = runtime_type_analysis.live_cast_types.has(mtype)
626 var c_name = mtype.c_name
627 var v = new SeparateCompilerVisitor(self)
628 v.add_decl("/* runtime type {mtype} */")
629
630 # extern const struct type_X
631 self.provide_declaration("type_{c_name}", "extern const struct type type_{c_name};")
632
633 # const struct type_X
634 v.add_decl("const struct type type_{c_name} = \{")
635
636 # type id (for cast target)
637 if is_cast_live then
638 v.add_decl("{layout.ids[mtype]},")
639 else
640 v.add_decl("-1, /*CAST DEAD*/")
641 end
642
643 # type name
644 v.add_decl("\"{mtype}\", /* class_name_string */")
645
646 # type color (for cast target)
647 if is_cast_live then
648 if layout isa PHLayout[MType, MType] then
649 v.add_decl("{layout.masks[mtype]},")
650 else
651 v.add_decl("{layout.pos[mtype]},")
652 end
653 else
654 v.add_decl("-1, /*CAST DEAD*/")
655 end
656
657 # is_nullable bit
658 if mtype isa MNullableType then
659 v.add_decl("1,")
660 else
661 v.add_decl("0,")
662 end
663
664 # resolution table (for receiver)
665 if is_live then
666 var mclass_type = mtype
667 if mclass_type isa MNullableType then mclass_type = mclass_type.mtype
668 assert mclass_type isa MClassType
669 if resolution_tables[mclass_type].is_empty then
670 v.add_decl("NULL, /*NO RESOLUTIONS*/")
671 else
672 compile_type_resolution_table(mtype)
673 v.require_declaration("resolution_table_{c_name}")
674 v.add_decl("&resolution_table_{c_name},")
675 end
676 else
677 v.add_decl("NULL, /*DEAD*/")
678 end
679
680 # cast table (for receiver)
681 if is_live then
682 v.add_decl("{self.type_tables[mtype].length},")
683 v.add_decl("\{")
684 for stype in self.type_tables[mtype] do
685 if stype == null then
686 v.add_decl("-1, /* empty */")
687 else
688 v.add_decl("{layout.ids[stype]}, /* {stype} */")
689 end
690 end
691 v.add_decl("\},")
692 else
693 v.add_decl("0, \{\}, /*DEAD TYPE*/")
694 end
695 v.add_decl("\};")
696 end
697
698 fun compile_type_resolution_table(mtype: MType) do
699
700 var mclass_type: MClassType
701 if mtype isa MNullableType then
702 mclass_type = mtype.mtype.as(MClassType)
703 else
704 mclass_type = mtype.as(MClassType)
705 end
706
707 var layout = self.resolution_layout
708
709 # extern const struct resolution_table_X resolution_table_X
710 self.provide_declaration("resolution_table_{mtype.c_name}", "extern const struct types resolution_table_{mtype.c_name};")
711
712 # const struct fts_table_X fts_table_X
713 var v = new_visitor
714 v.add_decl("const struct types resolution_table_{mtype.c_name} = \{")
715 if layout isa PHLayout[MClassType, MType] then
716 v.add_decl("{layout.masks[mclass_type]},")
717 else
718 v.add_decl("0, /* dummy */")
719 end
720 v.add_decl("\{")
721 for t in self.resolution_tables[mclass_type] do
722 if t == null then
723 v.add_decl("NULL, /* empty */")
724 else
725 # The table stores the result of the type resolution
726 # Therefore, for a receiver `mclass_type`, and a unresolved type `t`
727 # the value stored is tv.
728 var tv = t.resolve_for(mclass_type, mclass_type, self.mainmodule, true)
729 # FIXME: What typeids means here? How can a tv not be live?
730 if self.type_layout.ids.has_key(tv) then
731 v.require_declaration("type_{tv.c_name}")
732 v.add_decl("&type_{tv.c_name}, /* {t}: {tv} */")
733 else
734 v.add_decl("NULL, /* empty ({t}: {tv} not a live type) */")
735 end
736 end
737 end
738 v.add_decl("\}")
739 v.add_decl("\};")
740 end
741
742 # Globally compile the table of the class mclass
743 # In a link-time optimisation compiler, tables are globally computed
744 # In a true separate compiler (a with dynamic loading) you cannot do this unfortnally
745 fun compile_class_to_c(mclass: MClass)
746 do
747 var mtype = mclass.intro.bound_mtype
748 var c_name = mclass.c_name
749 var c_instance_name = mclass.c_instance_name
750
751 var vft = self.method_tables[mclass]
752 var attrs = self.attr_tables[mclass]
753 var v = new_visitor
754
755 var is_dead = runtime_type_analysis != null and not runtime_type_analysis.live_classes.has(mclass) and mtype.ctype == "val*" and mclass.name != "NativeArray"
756
757 v.add_decl("/* runtime class {c_name} */")
758
759 # Build class vft
760 if not is_dead then
761 self.provide_declaration("class_{c_name}", "extern const struct class class_{c_name};")
762 v.add_decl("const struct class class_{c_name} = \{")
763 v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
764 v.add_decl("\{")
765 for i in [0 .. vft.length[ do
766 var mpropdef = vft[i]
767 if mpropdef == null then
768 v.add_decl("NULL, /* empty */")
769 else
770 assert mpropdef isa MMethodDef
771 var rf = mpropdef.virtual_runtime_function
772 v.require_declaration(rf.c_name)
773 v.add_decl("(nitmethod_t){rf.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
774 end
775 end
776 v.add_decl("\}")
777 v.add_decl("\};")
778 end
779
780 if mtype.ctype != "val*" then
781 if mtype.mclass.name == "Pointer" or mtype.mclass.kind != extern_kind then
782 #Build instance struct
783 self.header.add_decl("struct instance_{c_instance_name} \{")
784 self.header.add_decl("const struct type *type;")
785 self.header.add_decl("const struct class *class;")
786 self.header.add_decl("{mtype.ctype} value;")
787 self.header.add_decl("\};")
788 end
789
790 if not self.runtime_type_analysis.live_types.has(mtype) then return
791
792 #Build BOX
793 self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype});")
794 v.add_decl("/* allocate {mtype} */")
795 v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
796 v.add("struct instance_{c_instance_name}*res = nit_alloc(sizeof(struct instance_{c_instance_name}));")
797 v.require_declaration("type_{c_name}")
798 v.add("res->type = &type_{c_name};")
799 v.require_declaration("class_{c_name}")
800 v.add("res->class = &class_{c_name};")
801 v.add("res->value = value;")
802 v.add("return (val*)res;")
803 v.add("\}")
804 return
805 else if mclass.name == "NativeArray" then
806 #Build instance struct
807 self.header.add_decl("struct instance_{c_instance_name} \{")
808 self.header.add_decl("const struct type *type;")
809 self.header.add_decl("const struct class *class;")
810 # NativeArrays are just a instance header followed by an array of values
811 self.header.add_decl("val* values[0];")
812 self.header.add_decl("\};")
813
814 #Build NEW
815 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(int length, const struct type* type);")
816 v.add_decl("/* allocate {mtype} */")
817 v.add_decl("{mtype.ctype} NEW_{c_name}(int length, const struct type* type) \{")
818 var res = v.new_named_var(mtype, "self")
819 res.is_exact = true
820 var mtype_elt = mtype.arguments.first
821 v.add("{res} = nit_alloc(sizeof(struct instance_{c_instance_name}) + length*sizeof({mtype_elt.ctype}));")
822 v.add("{res}->type = type;")
823 hardening_live_type(v, "type")
824 v.require_declaration("class_{c_name}")
825 v.add("{res}->class = &class_{c_name};")
826 v.add("return {res};")
827 v.add("\}")
828 return
829 end
830
831 #Build NEW
832 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(const struct type* type);")
833 v.add_decl("/* allocate {mtype} */")
834 v.add_decl("{mtype.ctype} NEW_{c_name}(const struct type* type) \{")
835 if is_dead then
836 v.add_abort("{mclass} is DEAD")
837 else
838 var res = v.new_named_var(mtype, "self")
839 res.is_exact = true
840 v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
841 v.add("{res}->type = type;")
842 hardening_live_type(v, "type")
843 v.require_declaration("class_{c_name}")
844 v.add("{res}->class = &class_{c_name};")
845 self.generate_init_attr(v, res, mtype)
846 v.add("return {res};")
847 end
848 v.add("\}")
849 end
850
851 # Add a dynamic test to ensure that the type referenced by `t` is a live type
852 fun hardening_live_type(v: VISITOR, t: String)
853 do
854 if not v.compiler.modelbuilder.toolcontext.opt_hardening.value then return
855 v.add("if({t} == NULL) \{")
856 v.add_abort("type null")
857 v.add("\}")
858 v.add("if({t}->table_size == 0) \{")
859 v.add("fprintf(stderr, \"Insantiation of a dead type: %s\\n\", {t}->name);")
860 v.add_abort("type dead")
861 v.add("\}")
862 end
863
864 redef fun new_visitor do return new SeparateCompilerVisitor(self)
865
866 # Stats
867
868 private var type_tables: Map[MType, Array[nullable MType]] = new HashMap[MType, Array[nullable MType]]
869 private var resolution_tables: Map[MClassType, Array[nullable MType]] = new HashMap[MClassType, Array[nullable MType]]
870 protected var method_tables: Map[MClass, Array[nullable MPropDef]] = new HashMap[MClass, Array[nullable MPropDef]]
871 protected var attr_tables: Map[MClass, Array[nullable MPropDef]] = new HashMap[MClass, Array[nullable MPropDef]]
872
873 redef fun display_stats
874 do
875 super
876 if self.modelbuilder.toolcontext.opt_tables_metrics.value then
877 display_sizes
878 end
879
880 var tc = self.modelbuilder.toolcontext
881 tc.info("# implementation of method invocation",2)
882 var nb_invok_total = modelbuilder.nb_invok_by_tables + modelbuilder.nb_invok_by_direct + modelbuilder.nb_invok_by_inline
883 tc.info("total number of invocations: {nb_invok_total}",2)
884 tc.info("invocations by VFT send: {modelbuilder.nb_invok_by_tables} ({div(modelbuilder.nb_invok_by_tables,nb_invok_total)}%)",2)
885 tc.info("invocations by direct call: {modelbuilder.nb_invok_by_direct} ({div(modelbuilder.nb_invok_by_direct,nb_invok_total)}%)",2)
886 tc.info("invocations by inlining: {modelbuilder.nb_invok_by_inline} ({div(modelbuilder.nb_invok_by_inline,nb_invok_total)}%)",2)
887 end
888
889 fun display_sizes
890 do
891 print "# size of subtyping tables"
892 print "\ttotal \tholes"
893 var total = 0
894 var holes = 0
895 for t, table in type_tables do
896 total += table.length
897 for e in table do if e == null then holes += 1
898 end
899 print "\t{total}\t{holes}"
900
901 print "# size of resolution tables"
902 print "\ttotal \tholes"
903 total = 0
904 holes = 0
905 for t, table in resolution_tables do
906 total += table.length
907 for e in table do if e == null then holes += 1
908 end
909 print "\t{total}\t{holes}"
910
911 print "# size of methods tables"
912 print "\ttotal \tholes"
913 total = 0
914 holes = 0
915 for t, table in method_tables do
916 total += table.length
917 for e in table do if e == null then holes += 1
918 end
919 print "\t{total}\t{holes}"
920
921 print "# size of attributes tables"
922 print "\ttotal \tholes"
923 total = 0
924 holes = 0
925 for t, table in attr_tables do
926 total += table.length
927 for e in table do if e == null then holes += 1
928 end
929 print "\t{total}\t{holes}"
930 end
931
932 redef fun compile_nitni_structs
933 do
934 self.header.add_decl("struct nitni_instance \{struct instance *value;\};")
935 end
936
937 redef fun finalize_ffi_for_module(nmodule)
938 do
939 var old_module = self.mainmodule
940 self.mainmodule = nmodule.mmodule.as(not null)
941 super
942 self.mainmodule = old_module
943 end
944 end
945
946 # A visitor on the AST of property definition that generate the C code of a separate compilation process.
947 class SeparateCompilerVisitor
948 super AbstractCompilerVisitor
949
950 redef type COMPILER: SeparateCompiler
951
952 redef fun adapt_signature(m, args)
953 do
954 var msignature = m.msignature.resolve_for(m.mclassdef.bound_mtype, m.mclassdef.bound_mtype, m.mclassdef.mmodule, true)
955 var recv = args.first
956 if recv.mtype.ctype != m.mclassdef.mclass.mclass_type.ctype then
957 args.first = self.autobox(args.first, m.mclassdef.mclass.mclass_type)
958 end
959 for i in [0..msignature.arity[ do
960 var t = msignature.mparameters[i].mtype
961 if i == msignature.vararg_rank then
962 t = args[i+1].mtype
963 end
964 args[i+1] = self.autobox(args[i+1], t)
965 end
966 end
967
968 redef fun autobox(value, mtype)
969 do
970 if value.mtype == mtype then
971 return value
972 else if value.mtype.ctype == "val*" and mtype.ctype == "val*" then
973 return value
974 else if value.mtype.ctype == "val*" then
975 return self.new_expr("((struct instance_{mtype.c_instance_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype)
976 else if mtype.ctype == "val*" then
977 var valtype = value.mtype.as(MClassType)
978 var res = self.new_var(mtype)
979 if compiler.runtime_type_analysis != null and not compiler.runtime_type_analysis.live_types.has(valtype) then
980 self.add("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
981 self.add("printf(\"Dead code executed!\\n\"); show_backtrace(1);")
982 return res
983 end
984 self.require_declaration("BOX_{valtype.c_name}")
985 self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
986 return res
987 else if value.mtype.cname_blind == "void*" and mtype.cname_blind == "void*" then
988 return value
989 else
990 # Bad things will appen!
991 var res = self.new_var(mtype)
992 self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
993 self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); show_backtrace(1);")
994 return res
995 end
996 end
997
998 # Return a C expression returning the runtime type structure of the value
999 # The point of the method is to works also with primitives types.
1000 fun type_info(value: RuntimeVariable): String
1001 do
1002 if value.mtype.ctype == "val*" then
1003 return "{value}->type"
1004 else
1005 compiler.undead_types.add(value.mtype)
1006 self.require_declaration("type_{value.mtype.c_name}")
1007 return "(&type_{value.mtype.c_name})"
1008 end
1009 end
1010
1011 redef fun compile_callsite(callsite, args)
1012 do
1013 var rta = compiler.runtime_type_analysis
1014 var recv = args.first.mtype
1015 if compiler.modelbuilder.toolcontext.opt_direct_call_monomorph.value and rta != null then
1016 var tgs = rta.live_targets(callsite)
1017 if tgs.length == 1 then
1018 # DIRECT CALL
1019 var mmethod = callsite.mproperty
1020 self.varargize(mmethod.intro, mmethod.intro.msignature.as(not null), args)
1021 var res0 = before_send(mmethod, args)
1022 var res = call(tgs.first, tgs.first.mclassdef.bound_mtype, args)
1023 if res0 != null then
1024 assert res != null
1025 self.assign(res0, res)
1026 res = res0
1027 end
1028 add("\}") # close the before_send
1029 return res
1030 end
1031 end
1032 return super
1033 end
1034 redef fun send(mmethod, arguments)
1035 do
1036 self.varargize(mmethod.intro, mmethod.intro.msignature.as(not null), arguments)
1037
1038 if arguments.first.mcasttype.ctype != "val*" then
1039 # In order to shortcut the primitive, we need to find the most specific method
1040 # Howverr, because of performance (no flattening), we always work on the realmainmodule
1041 var m = self.compiler.mainmodule
1042 self.compiler.mainmodule = self.compiler.realmainmodule
1043 var res = self.monomorphic_send(mmethod, arguments.first.mcasttype, arguments)
1044 self.compiler.mainmodule = m
1045 return res
1046 end
1047
1048 return table_send(mmethod, arguments, mmethod.const_color)
1049 end
1050
1051 # Handel common special cases before doing the effective method invocation
1052 # This methods handle the `==` and `!=` methods and the case of the null receiver.
1053 # Note: a { is open in the generated C, that enclose and protect the effective method invocation.
1054 # Client must not forget to close the } after them.
1055 #
1056 # The value returned is the result of the common special cases.
1057 # If not null, client must compine it with the result of their own effective method invocation.
1058 #
1059 # If `before_send` can shortcut the whole message sending, a dummy `if(0){`
1060 # is generated to cancel the effective method invocation that will follow
1061 # TODO: find a better approach
1062 private fun before_send(mmethod: MMethod, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1063 do
1064 var res: nullable RuntimeVariable = null
1065 var recv = arguments.first
1066 var consider_null = not self.compiler.modelbuilder.toolcontext.opt_no_check_other.value or mmethod.name == "==" or mmethod.name == "!="
1067 var maybenull = recv.mcasttype isa MNullableType and consider_null
1068 if maybenull then
1069 self.add("if ({recv} == NULL) \{")
1070 if mmethod.name == "==" then
1071 res = self.new_var(bool_type)
1072 var arg = arguments[1]
1073 if arg.mcasttype isa MNullableType then
1074 self.add("{res} = ({arg} == NULL);")
1075 else if arg.mcasttype isa MNullType then
1076 self.add("{res} = 1; /* is null */")
1077 else
1078 self.add("{res} = 0; /* {arg.inspect} cannot be null */")
1079 end
1080 else if mmethod.name == "!=" then
1081 res = self.new_var(bool_type)
1082 var arg = arguments[1]
1083 if arg.mcasttype isa MNullableType then
1084 self.add("{res} = ({arg} != NULL);")
1085 else if arg.mcasttype isa MNullType then
1086 self.add("{res} = 0; /* is null */")
1087 else
1088 self.add("{res} = 1; /* {arg.inspect} cannot be null */")
1089 end
1090 else
1091 self.add_abort("Receiver is null")
1092 end
1093 self.add("\} else \{")
1094 else
1095 self.add("\{")
1096 end
1097 if not self.compiler.modelbuilder.toolcontext.opt_no_shortcut_equate.value and (mmethod.name == "==" or mmethod.name == "!=") then
1098 if res == null then res = self.new_var(bool_type)
1099 # Recv is not null, thus is arg is, it is easy to conclude (and respect the invariants)
1100 var arg = arguments[1]
1101 if arg.mcasttype isa MNullType then
1102 if mmethod.name == "==" then
1103 self.add("{res} = 0; /* arg is null but recv is not */")
1104 else
1105 self.add("{res} = 1; /* arg is null and recv is not */")
1106 end
1107 self.add("\}") # closes the null case
1108 self.add("if (0) \{") # what follow is useless, CC will drop it
1109 end
1110 end
1111 return res
1112 end
1113
1114 private fun table_send(mmethod: MMethod, arguments: Array[RuntimeVariable], const_color: String): nullable RuntimeVariable
1115 do
1116 compiler.modelbuilder.nb_invok_by_tables += 1
1117 if compiler.modelbuilder.toolcontext.opt_invocation_metrics.value then add("count_invoke_by_tables++;")
1118
1119 assert arguments.length == mmethod.intro.msignature.arity + 1 else debug("Invalid arity for {mmethod}. {arguments.length} arguments given.")
1120 var recv = arguments.first
1121
1122 var res0 = before_send(mmethod, arguments)
1123
1124 var res: nullable RuntimeVariable
1125 var msignature = mmethod.intro.msignature.resolve_for(mmethod.intro.mclassdef.bound_mtype, mmethod.intro.mclassdef.bound_mtype, mmethod.intro.mclassdef.mmodule, true)
1126 var ret = msignature.return_mtype
1127 if mmethod.is_new then
1128 ret = arguments.first.mtype
1129 res = self.new_var(ret)
1130 else if ret == null then
1131 res = null
1132 else
1133 res = self.new_var(ret)
1134 end
1135
1136 var s = new Buffer
1137 var ss = new Buffer
1138
1139 s.append("val*")
1140 ss.append("{recv}")
1141 for i in [0..msignature.arity[ do
1142 var a = arguments[i+1]
1143 var t = msignature.mparameters[i].mtype
1144 if i == msignature.vararg_rank then
1145 t = arguments[i+1].mcasttype
1146 end
1147 s.append(", {t.ctype}")
1148 a = self.autobox(a, t)
1149 ss.append(", {a}")
1150 end
1151
1152
1153 var r
1154 if ret == null then r = "void" else r = ret.ctype
1155 self.require_declaration(const_color)
1156 var call = "(({r} (*)({s}))({arguments.first}->class->vft[{const_color}]))({ss}) /* {mmethod} on {arguments.first.inspect}*/"
1157
1158 if res != null then
1159 self.add("{res} = {call};")
1160 else
1161 self.add("{call};")
1162 end
1163
1164 if res0 != null then
1165 assert res != null
1166 assign(res0,res)
1167 res = res0
1168 end
1169
1170 self.add("\}") # closes the null case
1171
1172 return res
1173 end
1174
1175 redef fun call(mmethoddef, recvtype, arguments)
1176 do
1177 assert arguments.length == mmethoddef.msignature.arity + 1 else debug("Invalid arity for {mmethoddef}. {arguments.length} arguments given.")
1178
1179 var res: nullable RuntimeVariable
1180 var ret = mmethoddef.msignature.return_mtype
1181 if mmethoddef.mproperty.is_new then
1182 ret = arguments.first.mtype
1183 res = self.new_var(ret)
1184 else if ret == null then
1185 res = null
1186 else
1187 ret = ret.resolve_for(mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.mmodule, true)
1188 res = self.new_var(ret)
1189 end
1190
1191 if (mmethoddef.is_intern and not compiler.modelbuilder.toolcontext.opt_no_inline_intern.value) or
1192 (compiler.modelbuilder.toolcontext.opt_inline_some_methods.value and mmethoddef.can_inline(self)) then
1193 compiler.modelbuilder.nb_invok_by_inline += 1
1194 if compiler.modelbuilder.toolcontext.opt_invocation_metrics.value then add("count_invoke_by_inline++;")
1195 var frame = new Frame(self, mmethoddef, recvtype, arguments)
1196 frame.returnlabel = self.get_name("RET_LABEL")
1197 frame.returnvar = res
1198 var old_frame = self.frame
1199 self.frame = frame
1200 self.add("\{ /* Inline {mmethoddef} ({arguments.join(",")}) on {arguments.first.inspect} */")
1201 mmethoddef.compile_inside_to_c(self, arguments)
1202 self.add("{frame.returnlabel.as(not null)}:(void)0;")
1203 self.add("\}")
1204 self.frame = old_frame
1205 return res
1206 end
1207 compiler.modelbuilder.nb_invok_by_direct += 1
1208 if compiler.modelbuilder.toolcontext.opt_invocation_metrics.value then add("count_invoke_by_direct++;")
1209
1210 # Autobox arguments
1211 self.adapt_signature(mmethoddef, arguments)
1212
1213 self.require_declaration(mmethoddef.c_name)
1214 if res == null then
1215 self.add("{mmethoddef.c_name}({arguments.join(", ")}); /* Direct call {mmethoddef} on {arguments.first.inspect}*/")
1216 return null
1217 else
1218 self.add("{res} = {mmethoddef.c_name}({arguments.join(", ")});")
1219 end
1220
1221 return res
1222 end
1223
1224 redef fun supercall(m: MMethodDef, recvtype: MClassType, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1225 do
1226 if arguments.first.mcasttype.ctype != "val*" then
1227 # In order to shortcut the primitive, we need to find the most specific method
1228 # However, because of performance (no flattening), we always work on the realmainmodule
1229 var main = self.compiler.mainmodule
1230 self.compiler.mainmodule = self.compiler.realmainmodule
1231 var res = self.monomorphic_super_send(m, recvtype, arguments)
1232 self.compiler.mainmodule = main
1233 return res
1234 end
1235 return table_send(m.mproperty, arguments, m.const_color)
1236 end
1237
1238 redef fun vararg_instance(mpropdef, recv, varargs, elttype)
1239 do
1240 # A vararg must be stored into an new array
1241 # The trick is that the dymaic type of the array may depends on the receiver
1242 # of the method (ie recv) if the static type is unresolved
1243 # This is more complex than usual because the unresolved type must not be resolved
1244 # with the current receiver (ie self).
1245 # Therefore to isolate the resolution from self, a local Frame is created.
1246 # One can see this implementation as an inlined method of the receiver whose only
1247 # job is to allocate the array
1248 var old_frame = self.frame
1249 var frame = new Frame(self, mpropdef, mpropdef.mclassdef.bound_mtype, [recv])
1250 self.frame = frame
1251 #print "required Array[{elttype}] for recv {recv.inspect}. bound=Array[{self.resolve_for(elttype, recv)}]. selfvar={frame.arguments.first.inspect}"
1252 var res = self.array_instance(varargs, elttype)
1253 self.frame = old_frame
1254 return res
1255 end
1256
1257 redef fun isset_attribute(a, recv)
1258 do
1259 self.check_recv_notnull(recv)
1260 var res = self.new_var(bool_type)
1261
1262 # What is the declared type of the attribute?
1263 var mtype = a.intro.static_mtype.as(not null)
1264 var intromclassdef = a.intro.mclassdef
1265 mtype = mtype.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1266
1267 if mtype isa MNullableType then
1268 self.add("{res} = 1; /* easy isset: {a} on {recv.inspect} */")
1269 return res
1270 end
1271
1272 self.require_declaration(a.const_color)
1273 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
1274 self.add("{res} = {recv}->attrs[{a.const_color}] != NULL; /* {a} on {recv.inspect}*/")
1275 else
1276
1277 if mtype.ctype == "val*" then
1278 self.add("{res} = {recv}->attrs[{a.const_color}].val != NULL; /* {a} on {recv.inspect} */")
1279 else
1280 self.add("{res} = 1; /* NOT YET IMPLEMENTED: isset of primitives: {a} on {recv.inspect} */")
1281 end
1282 end
1283 return res
1284 end
1285
1286 redef fun read_attribute(a, recv)
1287 do
1288 self.check_recv_notnull(recv)
1289
1290 # What is the declared type of the attribute?
1291 var ret = a.intro.static_mtype.as(not null)
1292 var intromclassdef = a.intro.mclassdef
1293 ret = ret.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1294
1295 self.require_declaration(a.const_color)
1296 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
1297 # Get the attribute or a box (ie. always a val*)
1298 var cret = self.object_type.as_nullable
1299 var res = self.new_var(cret)
1300 res.mcasttype = ret
1301
1302 self.add("{res} = {recv}->attrs[{a.const_color}]; /* {a} on {recv.inspect} */")
1303
1304 # Check for Uninitialized attribute
1305 if not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_initialization.value then
1306 self.add("if (unlikely({res} == NULL)) \{")
1307 self.add_abort("Uninitialized attribute {a.name}")
1308 self.add("\}")
1309 end
1310
1311 # Return the attribute or its unboxed version
1312 # Note: it is mandatory since we reuse the box on write, we do not whant that the box escapes
1313 return self.autobox(res, ret)
1314 else
1315 var res = self.new_var(ret)
1316 self.add("{res} = {recv}->attrs[{a.const_color}].{ret.ctypename}; /* {a} on {recv.inspect} */")
1317
1318 # Check for Uninitialized attribute
1319 if ret.ctype == "val*" and not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_initialization.value then
1320 self.add("if (unlikely({res} == NULL)) \{")
1321 self.add_abort("Uninitialized attribute {a.name}")
1322 self.add("\}")
1323 end
1324
1325 return res
1326 end
1327 end
1328
1329 redef fun write_attribute(a, recv, value)
1330 do
1331 self.check_recv_notnull(recv)
1332
1333 # What is the declared type of the attribute?
1334 var mtype = a.intro.static_mtype.as(not null)
1335 var intromclassdef = a.intro.mclassdef
1336 mtype = mtype.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1337
1338 # Adapt the value to the declared type
1339 value = self.autobox(value, mtype)
1340
1341 self.require_declaration(a.const_color)
1342 if self.compiler.modelbuilder.toolcontext.opt_no_union_attribute.value then
1343 var attr = "{recv}->attrs[{a.const_color}]"
1344 if mtype.ctype != "val*" then
1345 assert mtype isa MClassType
1346 # The attribute is primitive, thus we store it in a box
1347 # The trick is to create the box the first time then resuse the box
1348 self.add("if ({attr} != NULL) \{")
1349 self.add("((struct instance_{mtype.c_instance_name}*){attr})->value = {value}; /* {a} on {recv.inspect} */")
1350 self.add("\} else \{")
1351 value = self.autobox(value, self.object_type.as_nullable)
1352 self.add("{attr} = {value}; /* {a} on {recv.inspect} */")
1353 self.add("\}")
1354 else
1355 # The attribute is not primitive, thus store it direclty
1356 self.add("{attr} = {value}; /* {a} on {recv.inspect} */")
1357 end
1358 else
1359 self.add("{recv}->attrs[{a.const_color}].{mtype.ctypename} = {value}; /* {a} on {recv.inspect} */")
1360 end
1361 end
1362
1363 # Check that mtype is a live open type
1364 fun hardening_live_open_type(mtype: MType)
1365 do
1366 if not compiler.modelbuilder.toolcontext.opt_hardening.value then return
1367 self.require_declaration(mtype.const_color)
1368 var col = mtype.const_color
1369 self.add("if({col} == -1) \{")
1370 self.add("fprintf(stderr, \"Resolution of a dead open type: %s\\n\", \"{mtype.to_s.escape_to_c}\");")
1371 self.add_abort("open type dead")
1372 self.add("\}")
1373 end
1374
1375 # Check that mtype it a pointer to a live cast type
1376 fun hardening_cast_type(t: String)
1377 do
1378 if not compiler.modelbuilder.toolcontext.opt_hardening.value then return
1379 add("if({t} == NULL) \{")
1380 add_abort("cast type null")
1381 add("\}")
1382 add("if({t}->id == -1 || {t}->color == -1) \{")
1383 add("fprintf(stderr, \"Try to cast on a dead cast type: %s\\n\", {t}->name);")
1384 add_abort("cast type dead")
1385 add("\}")
1386 end
1387
1388 redef fun init_instance(mtype)
1389 do
1390 self.require_declaration("NEW_{mtype.mclass.c_name}")
1391 var compiler = self.compiler
1392 if mtype isa MGenericType and mtype.need_anchor then
1393 hardening_live_open_type(mtype)
1394 link_unresolved_type(self.frame.mpropdef.mclassdef, mtype)
1395 var recv = self.frame.arguments.first
1396 var recv_type_info = self.type_info(recv)
1397 self.require_declaration(mtype.const_color)
1398 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1399 return self.new_expr("NEW_{mtype.mclass.c_name}({recv_type_info}->resolution_table->types[HASH({recv_type_info}->resolution_table->mask, {mtype.const_color})])", mtype)
1400 else
1401 return self.new_expr("NEW_{mtype.mclass.c_name}({recv_type_info}->resolution_table->types[{mtype.const_color}])", mtype)
1402 end
1403 end
1404 compiler.undead_types.add(mtype)
1405 self.require_declaration("type_{mtype.c_name}")
1406 return self.new_expr("NEW_{mtype.mclass.c_name}(&type_{mtype.c_name})", mtype)
1407 end
1408
1409 redef fun type_test(value, mtype, tag)
1410 do
1411 self.add("/* {value.inspect} isa {mtype} */")
1412 var compiler = self.compiler
1413
1414 var recv = self.frame.arguments.first
1415 var recv_type_info = self.type_info(recv)
1416
1417 var res = self.new_var(bool_type)
1418
1419 var cltype = self.get_name("cltype")
1420 self.add_decl("int {cltype};")
1421 var idtype = self.get_name("idtype")
1422 self.add_decl("int {idtype};")
1423
1424 var maybe_null = self.maybe_null(value)
1425 var accept_null = "0"
1426 var ntype = mtype
1427 if ntype isa MNullableType then
1428 ntype = ntype.mtype
1429 accept_null = "1"
1430 end
1431
1432 if value.mcasttype.is_subtype(self.frame.mpropdef.mclassdef.mmodule, self.frame.mpropdef.mclassdef.bound_mtype, mtype) then
1433 self.add("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
1434 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1435 self.compiler.count_type_test_skipped[tag] += 1
1436 self.add("count_type_test_skipped_{tag}++;")
1437 end
1438 return res
1439 end
1440
1441 if ntype.need_anchor then
1442 var type_struct = self.get_name("type_struct")
1443 self.add_decl("const struct type* {type_struct};")
1444
1445 # Either with resolution_table with a direct resolution
1446 hardening_live_open_type(mtype)
1447 link_unresolved_type(self.frame.mpropdef.mclassdef, mtype)
1448 self.require_declaration(mtype.const_color)
1449 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1450 self.add("{type_struct} = {recv_type_info}->resolution_table->types[HASH({recv_type_info}->resolution_table->mask, {mtype.const_color})];")
1451 else
1452 self.add("{type_struct} = {recv_type_info}->resolution_table->types[{mtype.const_color}];")
1453 end
1454 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1455 self.compiler.count_type_test_unresolved[tag] += 1
1456 self.add("count_type_test_unresolved_{tag}++;")
1457 end
1458 hardening_cast_type(type_struct)
1459 self.add("{cltype} = {type_struct}->color;")
1460 self.add("{idtype} = {type_struct}->id;")
1461 if maybe_null and accept_null == "0" then
1462 var is_nullable = self.get_name("is_nullable")
1463 self.add_decl("short int {is_nullable};")
1464 self.add("{is_nullable} = {type_struct}->is_nullable;")
1465 accept_null = is_nullable.to_s
1466 end
1467 else if ntype isa MClassType then
1468 compiler.undead_types.add(mtype)
1469 self.require_declaration("type_{mtype.c_name}")
1470 hardening_cast_type("(&type_{mtype.c_name})")
1471 self.add("{cltype} = type_{mtype.c_name}.color;")
1472 self.add("{idtype} = type_{mtype.c_name}.id;")
1473 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
1474 self.compiler.count_type_test_resolved[tag] += 1
1475 self.add("count_type_test_resolved_{tag}++;")
1476 end
1477 else
1478 self.add("printf(\"NOT YET IMPLEMENTED: type_test(%s, {mtype}).\\n\", \"{value.inspect}\"); show_backtrace(1);")
1479 end
1480
1481 # check color is in table
1482 if maybe_null then
1483 self.add("if({value} == NULL) \{")
1484 self.add("{res} = {accept_null};")
1485 self.add("\} else \{")
1486 end
1487 var value_type_info = self.type_info(value)
1488 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1489 self.add("{cltype} = HASH({value_type_info}->color, {idtype});")
1490 end
1491 self.add("if({cltype} >= {value_type_info}->table_size) \{")
1492 self.add("{res} = 0;")
1493 self.add("\} else \{")
1494 self.add("{res} = {value_type_info}->type_table[{cltype}] == {idtype};")
1495 self.add("\}")
1496 if maybe_null then
1497 self.add("\}")
1498 end
1499
1500 return res
1501 end
1502
1503 redef fun is_same_type_test(value1, value2)
1504 do
1505 var res = self.new_var(bool_type)
1506 # Swap values to be symetric
1507 if value2.mtype.ctype != "val*" and value1.mtype.ctype == "val*" then
1508 var tmp = value1
1509 value1 = value2
1510 value2 = tmp
1511 end
1512 if value1.mtype.ctype != "val*" then
1513 if value2.mtype == value1.mtype then
1514 self.add("{res} = 1; /* is_same_type_test: compatible types {value1.mtype} vs. {value2.mtype} */")
1515 else if value2.mtype.ctype != "val*" then
1516 self.add("{res} = 0; /* is_same_type_test: incompatible types {value1.mtype} vs. {value2.mtype}*/")
1517 else
1518 var mtype1 = value1.mtype.as(MClassType)
1519 self.require_declaration("class_{mtype1.c_name}")
1520 self.add("{res} = ({value2} != NULL) && ({value2}->class == &class_{mtype1.c_name}); /* is_same_type_test */")
1521 end
1522 else
1523 self.add("{res} = ({value1} == {value2}) || ({value1} != NULL && {value2} != NULL && {value1}->class == {value2}->class); /* is_same_type_test */")
1524 end
1525 return res
1526 end
1527
1528 redef fun class_name_string(value)
1529 do
1530 var res = self.get_name("var_class_name")
1531 self.add_decl("const char* {res};")
1532 if value.mtype.ctype == "val*" then
1533 self.add "{res} = {value} == NULL ? \"null\" : {value}->type->name;"
1534 else if value.mtype isa MClassType and value.mtype.as(MClassType).mclass.kind == extern_kind then
1535 self.add "{res} = \"{value.mtype.as(MClassType).mclass}\";"
1536 else
1537 self.require_declaration("type_{value.mtype.c_name}")
1538 self.add "{res} = type_{value.mtype.c_name}.name;"
1539 end
1540 return res
1541 end
1542
1543 redef fun equal_test(value1, value2)
1544 do
1545 var res = self.new_var(bool_type)
1546 if value2.mtype.ctype != "val*" and value1.mtype.ctype == "val*" then
1547 var tmp = value1
1548 value1 = value2
1549 value2 = tmp
1550 end
1551 if value1.mtype.ctype != "val*" then
1552 if value2.mtype == value1.mtype then
1553 self.add("{res} = {value1} == {value2};")
1554 else if value2.mtype.ctype != "val*" then
1555 self.add("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1556 else
1557 var mtype1 = value1.mtype.as(MClassType)
1558 self.require_declaration("class_{mtype1.c_name}")
1559 self.add("{res} = ({value2} != NULL) && ({value2}->class == &class_{mtype1.c_name});")
1560 self.add("if ({res}) \{")
1561 self.add("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1562 self.add("\}")
1563 end
1564 return res
1565 end
1566 var maybe_null = true
1567 var test = new Array[String]
1568 var t1 = value1.mcasttype
1569 if t1 isa MNullableType then
1570 test.add("{value1} != NULL")
1571 t1 = t1.mtype
1572 else
1573 maybe_null = false
1574 end
1575 var t2 = value2.mcasttype
1576 if t2 isa MNullableType then
1577 test.add("{value2} != NULL")
1578 t2 = t2.mtype
1579 else
1580 maybe_null = false
1581 end
1582
1583 var incompatible = false
1584 var primitive
1585 if t1.ctype != "val*" then
1586 primitive = t1
1587 if t1 == t2 then
1588 # No need to compare class
1589 else if t2.ctype != "val*" then
1590 incompatible = true
1591 else if can_be_primitive(value2) then
1592 test.add("{value1}->class == {value2}->class")
1593 else
1594 incompatible = true
1595 end
1596 else if t2.ctype != "val*" then
1597 primitive = t2
1598 if can_be_primitive(value1) then
1599 test.add("{value1}->class == {value2}->class")
1600 else
1601 incompatible = true
1602 end
1603 else
1604 primitive = null
1605 end
1606
1607 if incompatible then
1608 if maybe_null then
1609 self.add("{res} = {value1} == {value2}; /* incompatible types {t1} vs. {t2}; but may be NULL*/")
1610 return res
1611 else
1612 self.add("{res} = 0; /* incompatible types {t1} vs. {t2}; cannot be NULL */")
1613 return res
1614 end
1615 end
1616 if primitive != null then
1617 test.add("((struct instance_{primitive.c_instance_name}*){value1})->value == ((struct instance_{primitive.c_instance_name}*){value2})->value")
1618 else if can_be_primitive(value1) and can_be_primitive(value2) then
1619 test.add("{value1}->class == {value2}->class")
1620 var s = new Array[String]
1621 for t, v in self.compiler.box_kinds do
1622 s.add "({value1}->class->box_kind == {v} && ((struct instance_{t.c_instance_name}*){value1})->value == ((struct instance_{t.c_instance_name}*){value2})->value)"
1623 end
1624 test.add("({s.join(" || ")})")
1625 else
1626 self.add("{res} = {value1} == {value2};")
1627 return res
1628 end
1629 self.add("{res} = {value1} == {value2} || ({test.join(" && ")});")
1630 return res
1631 end
1632
1633 fun can_be_primitive(value: RuntimeVariable): Bool
1634 do
1635 var t = value.mcasttype
1636 if t isa MNullableType then t = t.mtype
1637 if not t isa MClassType then return false
1638 var k = t.mclass.kind
1639 return k == interface_kind or t.ctype != "val*"
1640 end
1641
1642 fun maybe_null(value: RuntimeVariable): Bool
1643 do
1644 var t = value.mcasttype
1645 return t isa MNullableType or t isa MNullType
1646 end
1647
1648 redef fun array_instance(array, elttype)
1649 do
1650 var nclass = self.get_class("NativeArray")
1651 var arrayclass = self.get_class("Array")
1652 var arraytype = arrayclass.get_mtype([elttype])
1653 var res = self.init_instance(arraytype)
1654 self.add("\{ /* {res} = array_instance Array[{elttype}] */")
1655 var length = self.int_instance(array.length)
1656 var nat = native_array_instance(elttype, length)
1657 for i in [0..array.length[ do
1658 var r = self.autobox(array[i], self.object_type)
1659 self.add("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
1660 end
1661 self.send(self.get_property("with_native", arrayclass.intro.bound_mtype), [res, nat, length])
1662 self.add("\}")
1663 return res
1664 end
1665
1666 fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable
1667 do
1668 var mtype = self.get_class("NativeArray").get_mtype([elttype])
1669 self.require_declaration("NEW_{mtype.mclass.c_name}")
1670 assert mtype isa MGenericType
1671 var compiler = self.compiler
1672 if mtype.need_anchor then
1673 hardening_live_open_type(mtype)
1674 link_unresolved_type(self.frame.mpropdef.mclassdef, mtype)
1675 var recv = self.frame.arguments.first
1676 var recv_type_info = self.type_info(recv)
1677 self.require_declaration(mtype.const_color)
1678 if compiler.modelbuilder.toolcontext.opt_phmod_typing.value or compiler.modelbuilder.toolcontext.opt_phand_typing.value then
1679 return self.new_expr("NEW_{mtype.mclass.c_name}({length}, {recv_type_info}->resolution_table->types[HASH({recv_type_info}->resolution_table->mask, {mtype.const_color})])", mtype)
1680 else
1681 return self.new_expr("NEW_{mtype.mclass.c_name}({length}, {recv_type_info}->resolution_table->types[{mtype.const_color}])", mtype)
1682 end
1683 end
1684 compiler.undead_types.add(mtype)
1685 self.require_declaration("type_{mtype.c_name}")
1686 return self.new_expr("NEW_{mtype.mclass.c_name}({length}, &type_{mtype.c_name})", mtype)
1687 end
1688
1689 redef fun native_array_def(pname, ret_type, arguments)
1690 do
1691 var elttype = arguments.first.mtype
1692 var nclass = self.get_class("NativeArray")
1693 var recv = "((struct instance_{nclass.c_instance_name}*){arguments[0]})->values"
1694 if pname == "[]" then
1695 self.ret(self.new_expr("{recv}[{arguments[1]}]", ret_type.as(not null)))
1696 return
1697 else if pname == "[]=" then
1698 self.add("{recv}[{arguments[1]}]={arguments[2]};")
1699 return
1700 else if pname == "copy_to" then
1701 var recv1 = "((struct instance_{nclass.c_instance_name}*){arguments[1]})->values"
1702 self.add("memcpy({recv1}, {recv}, {arguments[2]}*sizeof({elttype.ctype}));")
1703 return
1704 end
1705 end
1706
1707 redef fun calloc_array(ret_type, arguments)
1708 do
1709 var mclass = self.get_class("ArrayCapable")
1710 var ft = mclass.mclass_type.arguments.first.as(MParameterType)
1711 var res = self.native_array_instance(ft, arguments[1])
1712 self.ret(res)
1713 end
1714
1715 fun link_unresolved_type(mclassdef: MClassDef, mtype: MType) do
1716 assert mtype.need_anchor
1717 var compiler = self.compiler
1718 if not compiler.live_unresolved_types.has_key(self.frame.mpropdef.mclassdef) then
1719 compiler.live_unresolved_types[self.frame.mpropdef.mclassdef] = new HashSet[MType]
1720 end
1721 compiler.live_unresolved_types[self.frame.mpropdef.mclassdef].add(mtype)
1722 end
1723 end
1724
1725 redef class MMethodDef
1726 fun separate_runtime_function: AbstractRuntimeFunction
1727 do
1728 var res = self.separate_runtime_function_cache
1729 if res == null then
1730 res = new SeparateRuntimeFunction(self)
1731 self.separate_runtime_function_cache = res
1732 end
1733 return res
1734 end
1735 private var separate_runtime_function_cache: nullable SeparateRuntimeFunction
1736
1737 fun virtual_runtime_function: AbstractRuntimeFunction
1738 do
1739 var res = self.virtual_runtime_function_cache
1740 if res == null then
1741 res = new VirtualRuntimeFunction(self)
1742 self.virtual_runtime_function_cache = res
1743 end
1744 return res
1745 end
1746 private var virtual_runtime_function_cache: nullable VirtualRuntimeFunction
1747 end
1748
1749 # The C function associated to a methoddef separately compiled
1750 class SeparateRuntimeFunction
1751 super AbstractRuntimeFunction
1752
1753 redef fun build_c_name: String do return "{mmethoddef.c_name}"
1754
1755 redef fun to_s do return self.mmethoddef.to_s
1756
1757 redef fun compile_to_c(compiler)
1758 do
1759 var mmethoddef = self.mmethoddef
1760
1761 var recv = self.mmethoddef.mclassdef.bound_mtype
1762 var v = compiler.new_visitor
1763 var selfvar = new RuntimeVariable("self", recv, recv)
1764 var arguments = new Array[RuntimeVariable]
1765 var frame = new Frame(v, mmethoddef, recv, arguments)
1766 v.frame = frame
1767
1768 var msignature = mmethoddef.msignature.resolve_for(mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.mmodule, true)
1769
1770 var sig = new Buffer
1771 var comment = new Buffer
1772 var ret = msignature.return_mtype
1773 if ret != null then
1774 sig.append("{ret.ctype} ")
1775 else if mmethoddef.mproperty.is_new then
1776 ret = recv
1777 sig.append("{ret.ctype} ")
1778 else
1779 sig.append("void ")
1780 end
1781 sig.append(self.c_name)
1782 sig.append("({selfvar.mtype.ctype} {selfvar}")
1783 comment.append("({selfvar}: {selfvar.mtype}")
1784 arguments.add(selfvar)
1785 for i in [0..msignature.arity[ do
1786 var mtype = msignature.mparameters[i].mtype
1787 if i == msignature.vararg_rank then
1788 mtype = v.get_class("Array").get_mtype([mtype])
1789 end
1790 comment.append(", {mtype}")
1791 sig.append(", {mtype.ctype} p{i}")
1792 var argvar = new RuntimeVariable("p{i}", mtype, mtype)
1793 arguments.add(argvar)
1794 end
1795 sig.append(")")
1796 comment.append(")")
1797 if ret != null then
1798 comment.append(": {ret}")
1799 end
1800 compiler.provide_declaration(self.c_name, "{sig};")
1801
1802 v.add_decl("/* method {self} for {comment} */")
1803 v.add_decl("{sig} \{")
1804 if ret != null then
1805 frame.returnvar = v.new_var(ret)
1806 end
1807 frame.returnlabel = v.get_name("RET_LABEL")
1808
1809 if recv != arguments.first.mtype then
1810 #print "{self} {recv} {arguments.first}"
1811 end
1812 mmethoddef.compile_inside_to_c(v, arguments)
1813
1814 v.add("{frame.returnlabel.as(not null)}:;")
1815 if ret != null then
1816 v.add("return {frame.returnvar.as(not null)};")
1817 end
1818 v.add("\}")
1819 if not self.c_name.has_substring("VIRTUAL", 0) then compiler.names[self.c_name] = "{mmethoddef.mclassdef.mmodule.name}::{mmethoddef.mclassdef.mclass.name}::{mmethoddef.mproperty.name} ({mmethoddef.location.file.filename}:{mmethoddef.location.line_start})"
1820 end
1821 end
1822
1823 # The C function associated to a methoddef on a primitive type, stored into a VFT of a class
1824 # The first parameter (the reciever) is always typed by val* in order to accept an object value
1825 class VirtualRuntimeFunction
1826 super AbstractRuntimeFunction
1827
1828 redef fun build_c_name: String do return "VIRTUAL_{mmethoddef.c_name}"
1829
1830 redef fun to_s do return self.mmethoddef.to_s
1831
1832 redef fun compile_to_c(compiler)
1833 do
1834 var mmethoddef = self.mmethoddef
1835
1836 var recv = self.mmethoddef.mclassdef.bound_mtype
1837 var v = compiler.new_visitor
1838 var selfvar = new RuntimeVariable("self", v.object_type, recv)
1839 var arguments = new Array[RuntimeVariable]
1840 var frame = new Frame(v, mmethoddef, recv, arguments)
1841 v.frame = frame
1842
1843 var sig = new Buffer
1844 var comment = new Buffer
1845
1846 # Because the function is virtual, the signature must match the one of the original class
1847 var intromclassdef = self.mmethoddef.mproperty.intro.mclassdef
1848 var msignature = mmethoddef.mproperty.intro.msignature.resolve_for(intromclassdef.bound_mtype, intromclassdef.bound_mtype, intromclassdef.mmodule, true)
1849 var ret = msignature.return_mtype
1850 if ret != null then
1851 sig.append("{ret.ctype} ")
1852 else if mmethoddef.mproperty.is_new then
1853 ret = recv
1854 sig.append("{ret.ctype} ")
1855 else
1856 sig.append("void ")
1857 end
1858 sig.append(self.c_name)
1859 sig.append("({selfvar.mtype.ctype} {selfvar}")
1860 comment.append("({selfvar}: {selfvar.mtype}")
1861 arguments.add(selfvar)
1862 for i in [0..msignature.arity[ do
1863 var mtype = msignature.mparameters[i].mtype
1864 if i == msignature.vararg_rank then
1865 mtype = v.get_class("Array").get_mtype([mtype])
1866 end
1867 comment.append(", {mtype}")
1868 sig.append(", {mtype.ctype} p{i}")
1869 var argvar = new RuntimeVariable("p{i}", mtype, mtype)
1870 arguments.add(argvar)
1871 end
1872 sig.append(")")
1873 comment.append(")")
1874 if ret != null then
1875 comment.append(": {ret}")
1876 end
1877 compiler.provide_declaration(self.c_name, "{sig};")
1878
1879 v.add_decl("/* method {self} for {comment} */")
1880 v.add_decl("{sig} \{")
1881 if ret != null then
1882 frame.returnvar = v.new_var(ret)
1883 end
1884 frame.returnlabel = v.get_name("RET_LABEL")
1885
1886 var subret = v.call(mmethoddef, recv, arguments)
1887 if ret != null then
1888 assert subret != null
1889 v.assign(frame.returnvar.as(not null), subret)
1890 end
1891
1892 v.add("{frame.returnlabel.as(not null)}:;")
1893 if ret != null then
1894 v.add("return {frame.returnvar.as(not null)};")
1895 end
1896 v.add("\}")
1897 if not self.c_name.has_substring("VIRTUAL", 0) then compiler.names[self.c_name] = "{mmethoddef.mclassdef.mmodule.name}::{mmethoddef.mclassdef.mclass.name}::{mmethoddef.mproperty.name} ({mmethoddef.location.file.filename}--{mmethoddef.location.line_start})"
1898 end
1899
1900 # TODO ?
1901 redef fun call(v, arguments) do abort
1902 end
1903
1904 redef class MType
1905 fun const_color: String do return "COLOR_{c_name}"
1906
1907 # C name of the instance type to use
1908 fun c_instance_name: String do return c_name
1909 end
1910
1911 redef class MClassType
1912 redef fun c_instance_name do return mclass.c_instance_name
1913 end
1914
1915 redef class MClass
1916 # Extern classes use the C instance of kernel::Pointer
1917 fun c_instance_name: String
1918 do
1919 if kind == extern_kind then
1920 return "kernel__Pointer"
1921 else return c_name
1922 end
1923 end
1924
1925 redef class MProperty
1926 fun const_color: String do return "COLOR_{c_name}"
1927 end
1928
1929 redef class MPropDef
1930 fun const_color: String do return "COLOR_{c_name}"
1931 end