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