1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Separate compilation of a Nit program with generic type erasure
16 module separate_erasure_compiler
18 intrude import separate_compiler
20 # Add separate erased compiler specific options
21 redef class ToolContext
23 var opt_erasure
= new OptionBool("Erase generic types", "--erasure")
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")
32 self.option_context
.add_option
(self.opt_erasure
, self.opt_no_check_erasure_cast
, opt_rta
)
35 redef fun process_options
(args
)
39 if opt_no_check_all
.value
then
40 opt_no_check_erasure_cast
.value
= true
43 # Temporary disabled. TODO: implement tagging in the erasure compiler.
44 if opt_erasure
.value
then
45 opt_no_tag_primitives
.value
= true
49 var erasure_compiler_phase
= new ErasureCompilerPhase(self, null)
52 class ErasureCompilerPhase
54 redef fun process_mainmodule
(mainmodule
, given_mmodules
) do
55 if not toolcontext
.opt_erasure
.value
then return
57 var modelbuilder
= toolcontext
.modelbuilder
59 if toolcontext
.opt_rta
.value
then
60 analysis
= modelbuilder
.do_rapid_type_analysis
(mainmodule
)
62 modelbuilder
.run_separate_erasure_compiler
(mainmodule
, analysis
)
66 redef class ModelBuilder
67 fun run_separate_erasure_compiler
(mainmodule
: MModule, runtime_type_analysis
: nullable RapidTypeAnalysis)
70 self.toolcontext
.info
("*** GENERATING C ***", 1)
72 var compiler
= new SeparateErasureCompiler(mainmodule
, self, runtime_type_analysis
)
73 compiler
.do_compilation
74 compiler
.display_stats
76 self.toolcontext
.info
("*** END GENERATING C: {time1-time0} ***", 2)
77 write_and_make
(compiler
)
81 class SeparateErasureCompiler
82 super SeparateCompiler
84 private var class_ids
: Map[MClass, Int] is noinit
85 private var class_colors
: Map[MClass, Int] is noinit
86 protected var vt_colors
: Map[MVirtualTypeProp, Int] is noinit
91 var poset
= mainmodule
.flatten_mclass_hierarchy
92 var mclasses
= new HashSet[MClass].from
(poset
)
93 var colorer
= new POSetColorer[MClass]
94 colorer
.colorize
(poset
)
95 class_ids
= colorer
.ids
96 class_colors
= colorer
.colors
97 class_tables
= self.build_class_typing_tables
(mclasses
)
99 # lookup vt to build layout with
100 var vts
= new HashMap[MClass, Set[MVirtualTypeProp]]
101 for mclass
in mclasses
do
102 vts
[mclass
] = new HashSet[MVirtualTypeProp]
103 for mprop
in self.mainmodule
.properties
(mclass
) do
104 if mprop
isa MVirtualTypeProp then
105 vts
[mclass
].add
(mprop
)
111 var vt_colorer
= new POSetBucketsColorer[MClass, MVirtualTypeProp](poset
, colorer
.conflicts
)
112 vt_colors
= vt_colorer
.colorize
(vts
)
113 vt_tables
= build_vt_tables
(mclasses
)
116 fun build_vt_tables
(mclasses
: Set[MClass]): Map[MClass, Array[nullable MPropDef]] do
117 var tables
= new HashMap[MClass, Array[nullable MPropDef]]
118 for mclass
in mclasses
do
119 var table
= new Array[nullable MPropDef]
120 # first, fill table from parents by reverse linearization order
121 var parents
= new Array[MClass]
122 if mainmodule
.flatten_mclass_hierarchy
.has
(mclass
) then
123 parents
= mclass
.in_hierarchy
(mainmodule
).greaters
.to_a
124 self.mainmodule
.linearize_mclasses
(parents
)
126 for parent
in parents
do
127 if parent
== mclass
then continue
128 for mproperty
in self.mainmodule
.properties
(parent
) do
129 if not mproperty
isa MVirtualTypeProp then continue
130 var color
= vt_colors
[mproperty
]
131 if table
.length
<= color
then
132 for i
in [table
.length
.. color
[ do
136 for mpropdef
in mproperty
.mpropdefs
do
137 if mpropdef
.mclassdef
.mclass
== parent
then
138 table
[color
] = mpropdef
144 # then override with local properties
145 for mproperty
in self.mainmodule
.properties
(mclass
) do
146 if not mproperty
isa MVirtualTypeProp then continue
147 var color
= vt_colors
[mproperty
]
148 if table
.length
<= color
then
149 for i
in [table
.length
.. color
[ do
153 for mpropdef
in mproperty
.mpropdefs
do
154 if mpropdef
.mclassdef
.mclass
== mclass
then
155 table
[color
] = mpropdef
159 tables
[mclass
] = table
165 fun build_class_typing_tables
(mclasses
: Set[MClass]): Map[MClass, Array[nullable MClass]] do
166 var tables
= new HashMap[MClass, Array[nullable MClass]]
167 for mclass
in mclasses
do
168 var table
= new Array[nullable MClass]
169 var supers
= new Array[MClass]
170 if mainmodule
.flatten_mclass_hierarchy
.has
(mclass
) then
171 supers
= mclass
.in_hierarchy
(mainmodule
).greaters
.to_a
174 var color
= class_colors
[sup
]
175 if table
.length
<= color
then
176 for i
in [table
.length
.. color
[ do
182 tables
[mclass
] = table
187 redef fun compile_header_structs
do
188 self.header
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
189 self.compile_header_attribute_structs
190 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. */")
191 self.header
.add_decl
("struct type_table \{ int size; int table[]; \}; /* colorized type table. */")
192 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. */")
193 self.header
.add_decl
("struct vts_table \{ int dummy; const struct vts_entry vts[]; \}; /* vts list of a C type representation. */")
194 self.header
.add_decl
("typedef struct instance \{ const struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
197 redef fun compile_class_to_c
(mclass
: MClass)
199 var mtype
= mclass
.intro
.bound_mtype
200 var c_name
= mclass
.c_name
202 var class_table
= self.class_tables
[mclass
]
203 var v
= self.new_visitor
205 var rta
= runtime_type_analysis
206 var is_dead
= false # mclass.kind == abstract_kind or mclass.kind == interface_kind
207 if not is_dead
and rta
!= null and not rta
.live_classes
.has
(mclass
) and not mtype
.is_c_primitive
and mclass
.name
!= "NativeArray" then
211 v
.add_decl
("/* runtime class {c_name} */")
213 self.provide_declaration
("class_{c_name}", "extern const struct class class_{c_name};")
214 v
.add_decl
("extern const struct type_table type_table_{c_name};")
217 v
.add_decl
("const struct class class_{c_name} = \{")
218 v
.add_decl
("{class_ids[mclass]},")
219 v
.add_decl
("\"{mclass.name}\
", /* class_name_string */")
220 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
221 v
.add_decl
("{class_colors[mclass]},")
223 if build_class_vts_table
(mclass
) then
224 v
.require_declaration
("vts_table_{c_name}")
225 v
.add_decl
("&vts_table_{c_name},")
229 v
.add_decl
("&type_table_{c_name},")
231 var vft
= self.method_tables
.get_or_null
(mclass
)
232 if vft
!= null then for i
in [0 .. vft
.length
[ do
233 var mpropdef
= vft
[i
]
234 if mpropdef
== null then
235 v
.add_decl
("NULL, /* empty */")
237 assert mpropdef
isa MMethodDef
238 if rta
!= null and not rta
.live_methoddefs
.has
(mpropdef
) then
239 v
.add_decl
("NULL, /* DEAD {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
242 var rf
= mpropdef
.virtual_runtime_function
243 v
.require_declaration
(rf
.c_name
)
244 v
.add_decl
("(nitmethod_t){rf.c_name}, /* pointer to {mpropdef.full_name} */")
251 # Build class type table
253 v
.add_decl
("const struct type_table type_table_{c_name} = \{")
254 v
.add_decl
("{class_table.length},")
256 for msuper
in class_table
do
257 if msuper
== null then
258 v
.add_decl
("-1, /* empty */")
260 v
.add_decl
("{self.class_ids[msuper]}, /* {msuper} */")
266 if mtype
.is_c_primitive
or mtype
.mclass
.name
== "Pointer" then
267 #Build instance struct
268 self.header
.add_decl
("struct instance_{c_name} \{")
269 self.header
.add_decl
("const struct class *class;")
270 self.header
.add_decl
("{mtype.ctype} value;")
271 self.header
.add_decl
("\};")
274 self.provide_declaration
("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype_extern});")
275 v
.add_decl
("/* allocate {mtype} */")
276 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype_extern} value) \{")
277 v
.add
("struct instance_{c_name}*res = nit_alloc(sizeof(struct instance_{c_name}));")
278 v
.require_declaration
("class_{c_name}")
279 v
.add
("res->class = &class_{c_name};")
280 v
.add
("res->value = value;")
281 v
.add
("return (val*)res;")
284 if mtype
.mclass
.name
!= "Pointer" then return
287 self.provide_declaration
("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
288 v
.add_decl
("/* allocate {mtype} */")
289 v
.add_decl
("{mtype.ctype} NEW_{c_name}() \{")
291 v
.add_abort
("{mclass} is DEAD")
293 var res
= v
.new_named_var
(mtype
, "self")
295 v
.add
("{res} = nit_alloc(sizeof(struct instance_{mtype.c_name}));")
296 v
.require_declaration
("class_{c_name}")
297 v
.add
("{res}->class = &class_{c_name};")
298 v
.add
("((struct instance_{mtype.c_name}*){res})->value = NULL;")
299 v
.add
("return {res};")
303 else if mclass
.name
== "NativeArray" then
304 #Build instance struct
305 self.header
.add_decl
("struct instance_{c_name} \{")
306 self.header
.add_decl
("const struct class *class;")
307 self.header
.add_decl
("int length;")
308 self.header
.add_decl
("val* values[];")
309 self.header
.add_decl
("\};")
312 self.provide_declaration
("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(int length);")
313 v
.add_decl
("/* allocate {mtype} */")
314 v
.add_decl
("{mtype.ctype} NEW_{c_name}(int length) \{")
315 var res
= v
.get_name
("self")
316 v
.add_decl
("struct instance_{c_name} *{res};")
317 var mtype_elt
= mtype
.arguments
.first
318 v
.add
("{res} = nit_alloc(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
319 v
.require_declaration
("class_{c_name}")
320 v
.add
("{res}->class = &class_{c_name};")
321 v
.add
("{res}->length = length;")
322 v
.add
("return (val*){res};")
325 else if mclass
.name
== "RoutineRef" then
326 self.header
.add_decl
("struct instance_{c_name} \{")
327 self.header
.add_decl
("const struct class *class;")
328 self.header
.add_decl
("val* recv;")
329 self.header
.add_decl
("nitmethod_t method;")
330 self.header
.add_decl
("\};")
332 self.provide_declaration
("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(val* recv, nitmethod_t method, const struct class* class);")
333 v
.add_decl
("/* allocate {mtype} */")
334 v
.add_decl
("{mtype.ctype} NEW_{c_name}(val* recv, nitmethod_t method, const struct class* class)\{")
335 var res
= v
.get_name
("self")
336 v
.add_decl
("struct instance_{c_name} *{res};")
337 var alloc
= v
.nit_alloc
("sizeof(struct instance_{c_name})", mclass
.full_name
)
338 v
.add
("{res} = {alloc};")
339 v
.add
("{res}->class = class;")
340 v
.add
("{res}->recv = recv;")
341 v
.add
("{res}->method = method;")
342 v
.add
("return (val*){res};")
345 else if mtype
.mclass
.kind
== extern_kind
and mtype
.mclass
.name
!= "CString" then
346 var pointer_type
= mainmodule
.pointer_type
348 self.provide_declaration
("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
349 v
.add_decl
("/* allocate {mtype} */")
350 v
.add_decl
("{mtype.ctype} NEW_{c_name}() \{")
352 v
.add_abort
("{mclass} is DEAD")
354 var res
= v
.new_named_var
(mtype
, "self")
356 v
.add
("{res} = nit_alloc(sizeof(struct instance_{pointer_type.c_name}));")
357 #v.add("{res}->type = type;")
358 v
.require_declaration
("class_{c_name}")
359 v
.add
("{res}->class = &class_{c_name};")
360 v
.add
("((struct instance_{pointer_type.c_name}*){res})->value = NULL;")
361 v
.add
("return {res};")
368 self.provide_declaration
("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(void);")
369 v
.add_decl
("/* allocate {mtype} */")
370 v
.add_decl
("{mtype.ctype} NEW_{c_name}(void) \{")
372 v
.add_abort
("{mclass} is DEAD")
375 var res
= v
.new_named_var
(mtype
, "self")
377 var attrs
= self.attr_tables
.get_or_null
(mclass
)
378 if attrs
== null then
379 v
.add
("{res} = nit_alloc(sizeof(struct instance));")
381 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 if attrs
!= null then
386 self.generate_init_attr
(v
, res
, mtype
)
389 v
.add
("return {res};")
394 private fun build_class_vts_table
(mclass
: MClass): Bool do
395 if self.vt_tables
[mclass
].is_empty
then return false
397 self.provide_declaration
("vts_table_{mclass.c_name}", "extern const struct vts_table vts_table_{mclass.c_name};")
400 v
.add_decl
("const struct vts_table vts_table_{mclass.c_name} = \{")
401 v
.add_decl
("0, /* dummy */")
404 for vt
in self.vt_tables
[mclass
] do
406 v
.add_decl
("\{-1, NULL\}, /* empty */")
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
)
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} */")
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"
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
)
438 redef fun compile_types
440 compile_color_consts
(vt_colors
)
443 redef fun new_visitor
do return new SeparateErasureCompilerVisitor(self)
447 private var class_tables
: Map[MClass, Array[nullable MClass]] is noinit
448 private var vt_tables
: Map[MClass, Array[nullable MPropDef]] is noinit
450 redef fun display_sizes
452 print
"# size of subtyping tables"
453 print
"\ttotal \tholes"
456 for t
, table
in class_tables
do
457 total
+= table
.length
458 for e
in table
do if e
== null then holes
+= 1
460 print
"\t{total}\t{holes}"
462 print
"# size of resolution tables"
463 print
"\ttotal \tholes"
466 for t
, table
in vt_tables
do
467 total
+= table
.length
468 for e
in table
do if e
== null then holes
+= 1
470 print
"\t{total}\t{holes}"
472 print
"# size of methods tables"
473 print
"\ttotal \tholes"
476 for t
, table
in method_tables
do
477 total
+= table
.length
478 for e
in table
do if e
== null then holes
+= 1
480 print
"\t{total}\t{holes}"
482 print
"# size of attributes tables"
483 print
"\ttotal \tholes"
486 for t
, table
in attr_tables
do
487 total
+= table
.length
488 for e
in table
do if e
== null then holes
+= 1
490 print
"\t{total}\t{holes}"
494 class SeparateErasureCompilerVisitor
495 super SeparateCompilerVisitor
497 redef fun compile_callsite
(callsite
, arguments
)
500 if callsite
.erasure_cast
and not self.compiler
.as(SeparateErasureCompiler).modelbuilder
.toolcontext
.opt_no_check_erasure_cast
.value
then
502 var mtype
= callsite
.msignature
.return_mtype
504 self.add
("/* Erasure cast for return {res} isa {mtype} */")
505 var cond
= self.type_test
(res
, mtype
, "erasure")
506 self.add
("if (!{cond}) \{")
507 #var x = self.class_name_string(res)
508 #var y = self.class_name_string(arguments.first)
509 #self.add("PRINT_ERROR(\"Erasure cast: expected {mtype} (self is %s), got %s for {res}\\n\", {y}, {x});")
510 self.add_abort
("Cast failed")
516 redef fun init_instance
(mtype
)
518 self.require_declaration
("NEW_{mtype.mclass.c_name}")
519 return self.new_expr
("NEW_{mtype.mclass.c_name}()", mtype
)
522 redef fun type_test
(value
, mtype
, tag
)
524 self.add
("/* type test for {value.inspect} isa {mtype} */")
526 var res
= self.new_var
(bool_type
)
528 var cltype
= self.get_name
("cltype")
529 self.add_decl
("int {cltype};")
530 var idtype
= self.get_name
("idtype")
531 self.add_decl
("int {idtype};")
533 var maybe_null
= self.maybe_null
(value
)
534 var accept_null
= "0"
535 if mtype
isa MNullableType then
539 if mtype
isa MParameterType then
540 # Here we get the bound of the the formal type (eh, erasure...)
541 mtype
= mtype
.resolve_for
(self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, self.frame
.mpropdef
.mclassdef
.mmodule
, false)
542 if mtype
isa MNullableType then
548 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
549 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
550 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
551 self.compiler
.count_type_test_skipped
[tag
] += 1
552 self.add
("count_type_test_skipped_{tag}++;")
558 if not value
.mtype
.is_c_primitive
then
559 class_ptr
= "{value}->class->"
561 var mclass
= value
.mtype
.as(MClassType).mclass
562 self.require_declaration
("class_{mclass.c_name}")
563 class_ptr
= "class_{mclass.c_name}."
566 if mtype
isa MClassType then
567 self.require_declaration
("class_{mtype.mclass.c_name}")
568 self.add
("{cltype} = class_{mtype.mclass.c_name}.color;")
569 self.add
("{idtype} = class_{mtype.mclass.c_name}.id;")
570 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
571 self.compiler
.count_type_test_resolved
[tag
] += 1
572 self.add
("count_type_test_resolved_{tag}++;")
574 else if mtype
isa MVirtualType then
575 var recv
= self.frame
.arguments
.first
577 if not recv
.mtype
.is_c_primitive
then
578 recv_ptr
= "{recv}->class->"
580 var mclass
= recv
.mtype
.as(MClassType).mclass
581 self.require_declaration
("class_{mclass.c_name}")
582 recv_ptr
= "class_{mclass.c_name}."
584 var entry
= self.get_name
("entry")
585 self.add
("struct vts_entry {entry};")
586 self.require_declaration
(mtype
.mproperty
.const_color
)
587 self.add
("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
588 self.add
("{cltype} = {entry}.class->color;")
589 self.add
("{idtype} = {entry}.class->id;")
590 if maybe_null
and accept_null
== "0" then
591 var is_nullable
= self.get_name
("is_nullable")
592 self.add_decl
("short int {is_nullable};")
593 self.add
("{is_nullable} = {entry}.is_nullable;")
594 accept_null
= is_nullable
.to_s
596 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
597 self.compiler
.count_type_test_unresolved
[tag
] += 1
598 self.add
("count_type_test_unresolved_{tag}++;")
601 self.debug
("type_test({value.inspect}, {mtype})")
605 # check color is in table
607 self.add
("if({value} == NULL) \{")
608 self.add
("{res} = {accept_null};")
609 self.add
("\} else \{")
611 self.add
("if({cltype} >= {class_ptr}type_table->size) \{")
612 self.add
("{res} = 0;")
613 self.add
("\} else \{")
614 self.add
("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
623 redef fun unbox_extern
(value
, mtype
)
625 if mtype
isa MClassType and mtype
.mclass
.kind
== extern_kind
and
626 mtype
.mclass
.name
!= "CString" then
627 var pointer_type
= compiler
.mainmodule
.pointer_type
628 var res
= self.new_var_extern
(mtype
)
629 self.add
"{res} = ((struct instance_{pointer_type.c_name}*){value})->value; /* unboxing {value.mtype} */"
636 redef fun box_extern
(value
, mtype
)
638 if mtype
isa MClassType and mtype
.mclass
.kind
== extern_kind
and
639 mtype
.mclass
.name
!= "CString" then
640 var valtype
= compiler
.mainmodule
.pointer_type
641 var res
= self.new_var
(mtype
)
642 if compiler
.runtime_type_analysis
!= null and not compiler
.runtime_type_analysis
.live_types
.has
(value
.mtype
.as(MClassType)) then
643 self.add
("/*no boxing of {value.mtype}: {value.mtype} is not live! */")
644 self.add
("PRINT_ERROR(\"Dead code executed
!\\n\
"); fatal_exit(1);")
647 self.require_declaration
("BOX_{valtype.c_name}")
648 self.add
("{res} = BOX_{valtype.c_name}({value}); /* boxing {value.mtype} */")
649 self.require_declaration
("class_{mtype.c_name}")
650 self.add
("{res}->class = &class_{mtype.c_name};")
657 redef fun class_name_string
(value
)
659 var res
= self.get_name
("var_class_name")
660 self.add_decl
("const char* {res};")
661 if not value
.mtype
.is_c_primitive
then
662 self.add
"{res} = {value} == NULL ? \"null\
" : {value}->class->name;"
664 self.require_declaration
("class_{value.mtype.c_name}")
665 self.add
"{res} = class_{value.mtype.c_name}.name;"
670 redef fun native_array_instance
(elttype
, length
)
672 var nclass
= mmodule
.native_array_class
673 var mtype
= nclass
.get_mtype
([elttype
])
674 var res
= self.new_var
(mtype
)
676 self.require_declaration
("NEW_{nclass.c_name}")
677 length
= autobox
(length
, compiler
.mainmodule
.int_type
)
678 self.add
("{res} = NEW_{nclass.c_name}({length});")
682 redef fun routine_ref_instance
(routine_type
, recv
, callsite
)
684 var mmethoddef
= callsite
.mpropdef
685 #debug "ENTER ref_instance"
686 var mmethod
= mmethoddef
.mproperty
687 # routine_mclass is the specialized one, e.g: FunRef1, ProcRef2, etc..
688 var routine_mclass
= routine_type
.mclass
690 var nclasses
= mmodule
.model
.get_mclasses_by_name
("RoutineRef").as(not null)
691 var base_routine_mclass
= nclasses
.first
693 # All routine classes use the same `NEW` constructor.
694 # However, they have different declared `class` and `type` value.
695 self.require_declaration
("NEW_{base_routine_mclass.c_name}")
697 var recv_class_cname
= recv
.mcasttype
.as(MClassType).mclass
.c_name
700 if recv
.mtype
.is_c_primitive
then
701 my_recv
= autobox
(recv
, mmodule
.object_type
)
703 var my_recv_mclass_type
= my_recv
.mtype
.as(MClassType)
705 # The class of the concrete Routine must exist (e.g ProcRef0, FunRef0, etc.)
706 self.require_declaration
("class_{routine_mclass.c_name}")
708 self.require_declaration
(mmethoddef
.c_name
)
710 var thunk_function
= mmethoddef
.callref_thunk
(my_recv_mclass_type
)
711 var runtime_function
= mmethoddef
.virtual_runtime_function
713 var is_c_equiv
= runtime_function
.msignature
.c_equiv
(thunk_function
.msignature
)
715 var c_ref
= thunk_function
.c_ref
717 var const_color
= mmethoddef
.mproperty
.const_color
718 c_ref
= "{class_info(my_recv)}->vft[{const_color}]"
719 self.require_declaration
(const_color
)
721 self.require_declaration
(thunk_function
.c_name
)
722 compiler
.thunk_todo
(thunk_function
)
724 compiler
.thunk_todo
(thunk_function
)
726 # Each RoutineRef points to a receiver AND a callref_thunk
727 var res
= self.new_expr
("NEW_{base_routine_mclass.c_name}({my_recv}, (nitmethod_t){c_ref}, &class_{routine_mclass.c_name})", routine_type
)
728 #debug "LEAVING ref_instance"