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