src: update most tools to new constructors
[nit.git] / src / compiler / separate_erasure_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 with generic type erasure
16 module separate_erasure_compiler
17
18 intrude import separate_compiler
19
20 # Add separate erased compiler specific options
21 redef class ToolContext
22 # --erasure
23 var opt_erasure = new OptionBool("Erase generic types", "--erasure")
24 # --rta
25 var opt_rta = new OptionBool("Activate RTA (implicit with --global and --separate)", "--rta")
26 # --no-check-erasure-cast
27 var opt_no_check_erasure_cast = new OptionBool("Disable implicit casts on unsafe return with erasure-typing policy (dangerous)", "--no-check-erasure-cast")
28
29 redef init
30 do
31 super
32 self.option_context.add_option(self.opt_erasure, self.opt_no_check_erasure_cast, opt_rta)
33 end
34
35 redef fun process_options(args)
36 do
37 super
38
39 if opt_no_check_all.value then
40 opt_no_check_erasure_cast.value = true
41 end
42 end
43
44 var erasure_compiler_phase = new ErasureCompilerPhase(self, null)
45 end
46
47 class ErasureCompilerPhase
48 super Phase
49 redef fun process_mainmodule(mainmodule, given_mmodules) do
50 if not toolcontext.opt_erasure.value then return
51
52 var modelbuilder = toolcontext.modelbuilder
53 var analysis = null
54 if toolcontext.opt_rta.value then
55 analysis = modelbuilder.do_rapid_type_analysis(mainmodule)
56 end
57 modelbuilder.run_separate_erasure_compiler(mainmodule, analysis)
58 end
59 end
60
61 redef class ModelBuilder
62 fun run_separate_erasure_compiler(mainmodule: MModule, runtime_type_analysis: nullable RapidTypeAnalysis)
63 do
64 var time0 = get_time
65 self.toolcontext.info("*** GENERATING C ***", 1)
66
67 var compiler = new SeparateErasureCompiler(mainmodule, self, runtime_type_analysis)
68 compiler.compile_header
69
70 # compile class structures
71 self.toolcontext.info("Property coloring", 2)
72 compiler.new_file("{mainmodule.name}.tables")
73 compiler.do_property_coloring
74 for m in mainmodule.in_importation.greaters do
75 for mclass in m.intro_mclasses do
76 compiler.compile_class_to_c(mclass)
77 end
78 end
79 compiler.compile_color_consts(compiler.vt_colors)
80
81 # The main function of the C
82 compiler.new_file("{mainmodule.name}.main")
83 compiler.compile_nitni_global_ref_functions
84 compiler.compile_main_function
85
86 # compile methods
87 for m in mainmodule.in_importation.greaters do
88 self.toolcontext.info("Generate C for module {m}", 2)
89 compiler.new_file("{m.name}.sep")
90 compiler.compile_module_to_c(m)
91 end
92
93 compiler.display_stats
94
95 var time1 = get_time
96 self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
97 write_and_make(compiler)
98 end
99 end
100
101 class SeparateErasureCompiler
102 super SeparateCompiler
103
104 private var class_ids: Map[MClass, Int] is noinit
105 private var class_colors: Map[MClass, Int] is noinit
106 protected var vt_colors: Map[MVirtualTypeProp, Int] is noinit
107
108 init do
109
110 # Class coloring
111 var poset = mainmodule.flatten_mclass_hierarchy
112 var mclasses = new HashSet[MClass].from(poset)
113 var colorer = new POSetColorer[MClass]
114 colorer.colorize(poset)
115 class_ids = colorer.ids
116 class_colors = colorer.colors
117 class_tables = self.build_class_typing_tables(mclasses)
118
119 # lookup vt to build layout with
120 var vts = new HashMap[MClass, Set[MVirtualTypeProp]]
121 for mclass in mclasses do
122 vts[mclass] = new HashSet[MVirtualTypeProp]
123 for mprop in self.mainmodule.properties(mclass) do
124 if mprop isa MVirtualTypeProp then
125 vts[mclass].add(mprop)
126 end
127 end
128 end
129
130 # vt coloration
131 var vt_colorer = new POSetBucketsColorer[MClass, MVirtualTypeProp](poset, colorer.conflicts)
132 vt_colors = vt_colorer.colorize(vts)
133 vt_tables = build_vt_tables(mclasses)
134 end
135
136 fun build_vt_tables(mclasses: Set[MClass]): Map[MClass, Array[nullable MPropDef]] do
137 var tables = new HashMap[MClass, Array[nullable MPropDef]]
138 for mclass in mclasses do
139 var table = new Array[nullable MPropDef]
140 # first, fill table from parents by reverse linearization order
141 var parents = new Array[MClass]
142 if mainmodule.flatten_mclass_hierarchy.has(mclass) then
143 parents = mclass.in_hierarchy(mainmodule).greaters.to_a
144 self.mainmodule.linearize_mclasses(parents)
145 end
146 for parent in parents do
147 if parent == mclass then continue
148 for mproperty in self.mainmodule.properties(parent) do
149 if not mproperty isa MVirtualTypeProp then continue
150 var color = vt_colors[mproperty]
151 if table.length <= color then
152 for i in [table.length .. color[ do
153 table[i] = null
154 end
155 end
156 for mpropdef in mproperty.mpropdefs do
157 if mpropdef.mclassdef.mclass == parent then
158 table[color] = mpropdef
159 end
160 end
161 end
162 end
163
164 # then override with local properties
165 for mproperty in self.mainmodule.properties(mclass) do
166 if not mproperty isa MVirtualTypeProp then continue
167 var color = vt_colors[mproperty]
168 if table.length <= color then
169 for i in [table.length .. color[ do
170 table[i] = null
171 end
172 end
173 for mpropdef in mproperty.mpropdefs do
174 if mpropdef.mclassdef.mclass == mclass then
175 table[color] = mpropdef
176 end
177 end
178 end
179 tables[mclass] = table
180 end
181 return tables
182 end
183
184 # Build class tables
185 fun build_class_typing_tables(mclasses: Set[MClass]): Map[MClass, Array[nullable MClass]] do
186 var tables = new HashMap[MClass, Array[nullable MClass]]
187 for mclass in mclasses do
188 var table = new Array[nullable MClass]
189 var supers = new Array[MClass]
190 if mainmodule.flatten_mclass_hierarchy.has(mclass) then
191 supers = mclass.in_hierarchy(mainmodule).greaters.to_a
192 end
193 for sup in supers do
194 var color = class_colors[sup]
195 if table.length <= color then
196 for i in [table.length .. color[ do
197 table[i] = null
198 end
199 end
200 table[color] = sup
201 end
202 tables[mclass] = table
203 end
204 return tables
205 end
206
207 redef fun compile_header_structs do
208 self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
209 self.compile_header_attribute_structs
210 self.header.add_decl("struct class \{ int id; const char *name; int box_kind; int color; const struct vts_table *vts_table; const struct type_table *type_table; nitmethod_t vft[]; \}; /* general C type representing a Nit class. */")
211 self.header.add_decl("struct type_table \{ int size; int table[]; \}; /* colorized type table. */")
212 self.header.add_decl("struct vts_entry \{ short int is_nullable; const struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
213 self.header.add_decl("struct vts_table \{ int dummy; const struct vts_entry vts[]; \}; /* vts list of a C type representation. */")
214 self.header.add_decl("typedef struct instance \{ const struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
215 end
216
217 redef fun compile_class_to_c(mclass: MClass)
218 do
219 var mtype = mclass.intro.bound_mtype
220 var c_name = mclass.c_name
221
222 var vft = self.method_tables[mclass]
223 var attrs = self.attr_tables[mclass]
224 var class_table = self.class_tables[mclass]
225 var v = self.new_visitor
226
227 var rta = runtime_type_analysis
228 var is_dead = false # mclass.kind == abstract_kind or mclass.kind == interface_kind
229 if not is_dead and rta != null and not rta.live_classes.has(mclass) and mtype.ctype == "val*" and mclass.name != "NativeArray" then
230 is_dead = true
231 end
232
233 v.add_decl("/* runtime class {c_name} */")
234
235 self.provide_declaration("class_{c_name}", "extern const struct class class_{c_name};")
236 v.add_decl("extern const struct type_table type_table_{c_name};")
237
238 # Build class vft
239 v.add_decl("const struct class class_{c_name} = \{")
240 v.add_decl("{class_ids[mclass]},")
241 v.add_decl("\"{mclass.name}\", /* class_name_string */")
242 v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
243 v.add_decl("{class_colors[mclass]},")
244 if not is_dead then
245 if build_class_vts_table(mclass) then
246 v.require_declaration("vts_table_{c_name}")
247 v.add_decl("&vts_table_{c_name},")
248 else
249 v.add_decl("NULL,")
250 end
251 v.add_decl("&type_table_{c_name},")
252 v.add_decl("\{")
253 for i in [0 .. vft.length[ do
254 var mpropdef = vft[i]
255 if mpropdef == null then
256 v.add_decl("NULL, /* empty */")
257 else
258 assert mpropdef isa MMethodDef
259 if rta != null and not rta.live_methoddefs.has(mpropdef) then
260 v.add_decl("NULL, /* DEAD {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
261 continue
262 end
263 if true or mpropdef.mclassdef.bound_mtype.ctype != "val*" then
264 v.require_declaration("VIRTUAL_{mpropdef.c_name}")
265 v.add_decl("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
266 else
267 v.require_declaration("{mpropdef.c_name}")
268 v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
269 end
270 end
271 end
272 v.add_decl("\}")
273 end
274 v.add_decl("\};")
275
276 # Build class type table
277
278 v.add_decl("const struct type_table type_table_{c_name} = \{")
279 v.add_decl("{class_table.length},")
280 v.add_decl("\{")
281 for msuper in class_table do
282 if msuper == null then
283 v.add_decl("-1, /* empty */")
284 else
285 v.add_decl("{self.class_ids[msuper]}, /* {msuper} */")
286 end
287 end
288 v.add_decl("\}")
289 v.add_decl("\};")
290
291 if mtype.ctype != "val*" or mtype.mclass.name == "Pointer" then
292 #Build instance struct
293 self.header.add_decl("struct instance_{c_name} \{")
294 self.header.add_decl("const struct class *class;")
295 self.header.add_decl("{mtype.ctype} value;")
296 self.header.add_decl("\};")
297
298 #Build BOX
299 self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype});")
300 v.add_decl("/* allocate {mtype} */")
301 v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
302 v.add("struct instance_{c_name}*res = nit_alloc(sizeof(struct instance_{c_name}));")
303 v.require_declaration("class_{c_name}")
304 v.add("res->class = &class_{c_name};")
305 v.add("res->value = value;")
306 v.add("return (val*)res;")
307 v.add("\}")
308
309 if mtype.mclass.name != "Pointer" then return
310
311 v = new_visitor
312 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
313 v.add_decl("/* allocate {mtype} */")
314 v.add_decl("{mtype.ctype} NEW_{c_name}() \{")
315 if is_dead then
316 v.add_abort("{mclass} is DEAD")
317 else
318 var res = v.new_named_var(mtype, "self")
319 res.is_exact = true
320 v.add("{res} = nit_alloc(sizeof(struct instance_{mtype.c_name}));")
321 v.require_declaration("class_{c_name}")
322 v.add("{res}->class = &class_{c_name};")
323 v.add("((struct instance_{mtype.c_name}*){res})->value = NULL;")
324 v.add("return {res};")
325 end
326 v.add("\}")
327 return
328 else if mclass.name == "NativeArray" then
329 #Build instance struct
330 self.header.add_decl("struct instance_{c_name} \{")
331 self.header.add_decl("const struct class *class;")
332 self.header.add_decl("int length;")
333 self.header.add_decl("val* values[];")
334 self.header.add_decl("\};")
335
336 #Build NEW
337 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(int length);")
338 v.add_decl("/* allocate {mtype} */")
339 v.add_decl("{mtype.ctype} NEW_{c_name}(int length) \{")
340 var res = v.get_name("self")
341 v.add_decl("struct instance_{c_name} *{res};")
342 var mtype_elt = mtype.arguments.first
343 v.add("{res} = nit_alloc(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
344 v.require_declaration("class_{c_name}")
345 v.add("{res}->class = &class_{c_name};")
346 v.add("{res}->length = length;")
347 v.add("return (val*){res};")
348 v.add("\}")
349 return
350 else if mtype.mclass.kind == extern_kind and mtype.mclass.name != "NativeString" then
351 var pointer_type = mainmodule.pointer_type
352
353 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
354 v.add_decl("/* allocate {mtype} */")
355 v.add_decl("{mtype.ctype} NEW_{c_name}() \{")
356 if is_dead then
357 v.add_abort("{mclass} is DEAD")
358 else
359 var res = v.new_named_var(mtype, "self")
360 res.is_exact = true
361 v.add("{res} = nit_alloc(sizeof(struct instance_{pointer_type.c_name}));")
362 #v.add("{res}->type = type;")
363 v.require_declaration("class_{c_name}")
364 v.add("{res}->class = &class_{c_name};")
365 v.add("((struct instance_{pointer_type.c_name}*){res})->value = NULL;")
366 v.add("return {res};")
367 end
368 v.add("\}")
369 return
370 end
371
372 #Build NEW
373 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(void);")
374 v.add_decl("/* allocate {mtype} */")
375 v.add_decl("{mtype.ctype} NEW_{c_name}(void) \{")
376 if is_dead then
377 v.add_abort("{mclass} is DEAD")
378 else
379
380 var res = v.new_named_var(mtype, "self")
381 res.is_exact = true
382 v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
383 v.require_declaration("class_{c_name}")
384 v.add("{res}->class = &class_{c_name};")
385 self.generate_init_attr(v, res, mtype)
386 v.set_finalizer res
387 v.add("return {res};")
388 end
389 v.add("\}")
390 end
391
392 private fun build_class_vts_table(mclass: MClass): Bool do
393 if self.vt_tables[mclass].is_empty then return false
394
395 self.provide_declaration("vts_table_{mclass.c_name}", "extern const struct vts_table vts_table_{mclass.c_name};")
396
397 var v = new_visitor
398 v.add_decl("const struct vts_table vts_table_{mclass.c_name} = \{")
399 v.add_decl("0, /* dummy */")
400 v.add_decl("\{")
401
402 for vt in self.vt_tables[mclass] do
403 if vt == null then
404 v.add_decl("\{-1, NULL\}, /* empty */")
405 else
406 var is_null = 0
407 var bound = retrieve_vt_bound(mclass.intro.bound_mtype, vt.as(MVirtualTypeDef).bound)
408 while bound isa MNullableType do
409 bound = retrieve_vt_bound(mclass.intro.bound_mtype, bound.mtype)
410 is_null = 1
411 end
412 var vtclass = bound.as(MClassType).mclass
413 v.require_declaration("class_{vtclass.c_name}")
414 v.add_decl("\{{is_null}, &class_{vtclass.c_name}\}, /* {vt} */")
415 end
416 end
417 v.add_decl("\},")
418 v.add_decl("\};")
419 return true
420 end
421
422 private fun retrieve_vt_bound(anchor: MClassType, mtype: nullable MType): MType do
423 if mtype == null then
424 print "NOT YET IMPLEMENTED: retrieve_vt_bound on null"
425 abort
426 end
427 if mtype isa MVirtualType then
428 return mtype.anchor_to(mainmodule, anchor)
429 else if mtype isa MParameterType then
430 return mtype.anchor_to(mainmodule, anchor)
431 else
432 return mtype
433 end
434 end
435
436 redef fun new_visitor do return new SeparateErasureCompilerVisitor(self)
437
438 # Stats
439
440 private var class_tables: Map[MClass, Array[nullable MClass]] is noinit
441 private var vt_tables: Map[MClass, Array[nullable MPropDef]] is noinit
442
443 redef fun display_sizes
444 do
445 print "# size of subtyping tables"
446 print "\ttotal \tholes"
447 var total = 0
448 var holes = 0
449 for t, table in class_tables do
450 total += table.length
451 for e in table do if e == null then holes += 1
452 end
453 print "\t{total}\t{holes}"
454
455 print "# size of resolution tables"
456 print "\ttotal \tholes"
457 total = 0
458 holes = 0
459 for t, table in vt_tables do
460 total += table.length
461 for e in table do if e == null then holes += 1
462 end
463 print "\t{total}\t{holes}"
464
465 print "# size of methods tables"
466 print "\ttotal \tholes"
467 total = 0
468 holes = 0
469 for t, table in method_tables do
470 total += table.length
471 for e in table do if e == null then holes += 1
472 end
473 print "\t{total}\t{holes}"
474
475 print "# size of attributes tables"
476 print "\ttotal \tholes"
477 total = 0
478 holes = 0
479 for t, table in attr_tables do
480 total += table.length
481 for e in table do if e == null then holes += 1
482 end
483 print "\t{total}\t{holes}"
484 end
485 end
486
487 class SeparateErasureCompilerVisitor
488 super SeparateCompilerVisitor
489
490 redef fun compile_callsite(callsite, arguments)
491 do
492 var res = super
493 if callsite.erasure_cast and not self.compiler.as(SeparateErasureCompiler).modelbuilder.toolcontext.opt_no_check_erasure_cast.value then
494 assert res != null
495 var mtype = callsite.msignature.return_mtype
496 assert mtype != null
497 self.add("/* Erasure cast for return {res} isa {mtype} */")
498 var cond = self.type_test(res, mtype, "erasure")
499 self.add("if (!{cond}) \{")
500 #var x = self.class_name_string(res)
501 #var y = self.class_name_string(arguments.first)
502 #self.add("PRINT_ERROR(\"Erasure cast: expected {mtype} (self is %s), got %s for {res}\\n\", {y}, {x});")
503 self.add_abort("Cast failed")
504 self.add("\}")
505 end
506 return res
507 end
508
509 redef fun init_instance(mtype)
510 do
511 self.require_declaration("NEW_{mtype.mclass.c_name}")
512 return self.new_expr("NEW_{mtype.mclass.c_name}()", mtype)
513 end
514
515 redef fun type_test(value, mtype, tag)
516 do
517 self.add("/* type test for {value.inspect} isa {mtype} */")
518
519 var res = self.new_var(bool_type)
520
521 var cltype = self.get_name("cltype")
522 self.add_decl("int {cltype};")
523 var idtype = self.get_name("idtype")
524 self.add_decl("int {idtype};")
525
526 var maybe_null = self.maybe_null(value)
527 var accept_null = "0"
528 if mtype isa MNullableType then
529 mtype = mtype.mtype
530 accept_null = "1"
531 end
532 if mtype isa MParameterType then
533 # Here we get the bound of the the formal type (eh, erasure...)
534 mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, false)
535 if mtype isa MNullableType then
536 mtype = mtype.mtype
537 accept_null = "1"
538 end
539 end
540
541 if value.mcasttype.is_subtype(self.frame.mpropdef.mclassdef.mmodule, self.frame.mpropdef.mclassdef.bound_mtype, mtype) then
542 self.add("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
543 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
544 self.compiler.count_type_test_skipped[tag] += 1
545 self.add("count_type_test_skipped_{tag}++;")
546 end
547 return res
548 end
549
550 var class_ptr
551 if value.mtype.ctype == "val*" then
552 class_ptr = "{value}->class->"
553 else
554 var mclass = value.mtype.as(MClassType).mclass
555 self.require_declaration("class_{mclass.c_name}")
556 class_ptr = "class_{mclass.c_name}."
557 end
558
559 if mtype isa MClassType then
560 self.require_declaration("class_{mtype.mclass.c_name}")
561 self.add("{cltype} = class_{mtype.mclass.c_name}.color;")
562 self.add("{idtype} = class_{mtype.mclass.c_name}.id;")
563 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
564 self.compiler.count_type_test_resolved[tag] += 1
565 self.add("count_type_test_resolved_{tag}++;")
566 end
567 else if mtype isa MVirtualType then
568 var recv = self.frame.arguments.first
569 var recv_ptr
570 if recv.mtype.ctype == "val*" then
571 recv_ptr = "{recv}->class->"
572 else
573 var mclass = recv.mtype.as(MClassType).mclass
574 self.require_declaration("class_{mclass.c_name}")
575 recv_ptr = "class_{mclass.c_name}."
576 end
577 var entry = self.get_name("entry")
578 self.add("struct vts_entry {entry};")
579 self.require_declaration(mtype.mproperty.const_color)
580 self.add("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
581 self.add("{cltype} = {entry}.class->color;")
582 self.add("{idtype} = {entry}.class->id;")
583 if maybe_null and accept_null == "0" then
584 var is_nullable = self.get_name("is_nullable")
585 self.add_decl("short int {is_nullable};")
586 self.add("{is_nullable} = {entry}.is_nullable;")
587 accept_null = is_nullable.to_s
588 end
589 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
590 self.compiler.count_type_test_unresolved[tag] += 1
591 self.add("count_type_test_unresolved_{tag}++;")
592 end
593 else
594 self.debug("type_test({value.inspect}, {mtype})")
595 abort
596 end
597
598 # check color is in table
599 if maybe_null then
600 self.add("if({value} == NULL) \{")
601 self.add("{res} = {accept_null};")
602 self.add("\} else \{")
603 end
604 self.add("if({cltype} >= {class_ptr}type_table->size) \{")
605 self.add("{res} = 0;")
606 self.add("\} else \{")
607 self.add("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
608 self.add("\}")
609 if maybe_null then
610 self.add("\}")
611 end
612
613 return res
614 end
615
616 redef fun unbox_extern(value, mtype)
617 do
618 if mtype isa MClassType and mtype.mclass.kind == extern_kind and
619 mtype.mclass.name != "NativeString" then
620 var pointer_type = compiler.mainmodule.pointer_type
621 var res = self.new_var_extern(mtype)
622 self.add "{res} = ((struct instance_{pointer_type.c_name}*){value})->value; /* unboxing {value.mtype} */"
623 return res
624 else
625 return value
626 end
627 end
628
629 redef fun box_extern(value, mtype)
630 do
631 if mtype isa MClassType and mtype.mclass.kind == extern_kind and
632 mtype.mclass.name != "NativeString" then
633 var valtype = compiler.mainmodule.pointer_type
634 var res = self.new_var(mtype)
635 if compiler.runtime_type_analysis != null and not compiler.runtime_type_analysis.live_types.has(value.mtype.as(MClassType)) then
636 self.add("/*no boxing of {value.mtype}: {value.mtype} is not live! */")
637 self.add("PRINT_ERROR(\"Dead code executed!\\n\"); show_backtrace(1);")
638 return res
639 end
640 self.require_declaration("BOX_{valtype.c_name}")
641 self.add("{res} = BOX_{valtype.c_name}({value}); /* boxing {value.mtype} */")
642 self.require_declaration("class_{mtype.c_name}")
643 self.add("{res}->class = &class_{mtype.c_name};")
644 return res
645 else
646 return value
647 end
648 end
649
650 redef fun class_name_string(value)
651 do
652 var res = self.get_name("var_class_name")
653 self.add_decl("const char* {res};")
654 if value.mtype.ctype == "val*" then
655 self.add "{res} = {value} == NULL ? \"null\" : {value}->class->name;"
656 else
657 self.require_declaration("class_{value.mtype.c_name}")
658 self.add "{res} = class_{value.mtype.c_name}.name;"
659 end
660 return res
661 end
662
663 redef fun native_array_instance(elttype, length)
664 do
665 var nclass = self.get_class("NativeArray")
666 var mtype = nclass.get_mtype([elttype])
667 var res = self.new_var(mtype)
668 res.is_exact = true
669 self.require_declaration("NEW_{nclass.c_name}")
670 self.add("{res} = NEW_{nclass.c_name}({length});")
671 return res
672 end
673
674 redef fun calloc_array(ret_type, arguments)
675 do
676 var ret = ret_type.as(MClassType)
677 self.require_declaration("NEW_{ret.mclass.c_name}")
678 self.ret(self.new_expr("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type))
679 end
680 end