if opt_no_check_all.value then
opt_no_check_erasure_cast.value = true
end
+
+ # Temporary disabled. TODO: implement tagging in the erasure compiler.
+ if opt_erasure.value then
+ opt_no_tag_primitives.value = true
+ end
end
var erasure_compiler_phase = new ErasureCompilerPhase(self, null)
self.toolcontext.info("*** GENERATING C ***", 1)
var compiler = new SeparateErasureCompiler(mainmodule, self, runtime_type_analysis)
- compiler.compile_header
-
- var c_name = mainmodule.c_name
-
- # compile class structures
- self.toolcontext.info("Property coloring", 2)
- compiler.new_file("{c_name}.tables")
- compiler.do_property_coloring
- for m in mainmodule.in_importation.greaters do
- for mclass in m.intro_mclasses do
- compiler.compile_class_to_c(mclass)
- end
- end
- compiler.compile_color_consts(compiler.vt_colors)
-
- # The main function of the C
- compiler.new_file("{c_name}.main")
- compiler.compile_nitni_global_ref_functions
- compiler.compile_main_function
-
- # compile methods
- for m in mainmodule.in_importation.greaters do
- self.toolcontext.info("Generate C for module {m.full_name}", 2)
- compiler.new_file("{m.c_name}.sep")
- compiler.compile_module_to_c(m)
- end
-
+ compiler.do_compilation
compiler.display_stats
-
var time1 = get_time
self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
write_and_make(compiler)
var mtype = mclass.intro.bound_mtype
var c_name = mclass.c_name
- var vft = self.method_tables[mclass]
- var attrs = self.attr_tables[mclass]
var class_table = self.class_tables[mclass]
var v = self.new_visitor
var rta = runtime_type_analysis
var is_dead = false # mclass.kind == abstract_kind or mclass.kind == interface_kind
- if not is_dead and rta != null and not rta.live_classes.has(mclass) and mtype.ctype == "val*" and mclass.name != "NativeArray" then
+ 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
is_dead = true
end
end
v.add_decl("&type_table_{c_name},")
v.add_decl("\{")
- for i in [0 .. vft.length[ do
+ var vft = self.method_tables.get_or_null(mclass)
+ if vft != null then for i in [0 .. vft.length[ do
var mpropdef = vft[i]
if mpropdef == null then
v.add_decl("NULL, /* empty */")
v.add_decl("NULL, /* DEAD {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
continue
end
- if true or mpropdef.mclassdef.bound_mtype.ctype != "val*" then
- v.require_declaration("VIRTUAL_{mpropdef.c_name}")
- v.add_decl("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
- else
- v.require_declaration("{mpropdef.c_name}")
- v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
- end
+ var rf = mpropdef.virtual_runtime_function
+ v.require_declaration(rf.c_name)
+ v.add_decl("(nitmethod_t){rf.c_name}, /* pointer to {mpropdef.full_name} */")
end
end
v.add_decl("\}")
v.add_decl("\}")
v.add_decl("\};")
- if mtype.ctype != "val*" or mtype.mclass.name == "Pointer" then
+ if mtype.is_c_primitive or mtype.mclass.name == "Pointer" then
#Build instance struct
self.header.add_decl("struct instance_{c_name} \{")
self.header.add_decl("const struct class *class;")
self.header.add_decl("\};")
#Build BOX
- self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype});")
+ self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype_extern});")
v.add_decl("/* allocate {mtype} */")
- v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
+ v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype_extern} value) \{")
v.add("struct instance_{c_name}*res = nit_alloc(sizeof(struct instance_{c_name}));")
v.require_declaration("class_{c_name}")
v.add("res->class = &class_{c_name};")
var res = v.new_named_var(mtype, "self")
res.is_exact = true
- v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
+ var attrs = self.attr_tables.get_or_null(mclass)
+ if attrs == null then
+ v.add("{res} = nit_alloc(sizeof(struct instance));")
+ else
+ v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
+ end
v.require_declaration("class_{c_name}")
v.add("{res}->class = &class_{c_name};")
- self.generate_init_attr(v, res, mtype)
- v.set_finalizer res
+ if attrs != null then
+ self.generate_init_attr(v, res, mtype)
+ v.set_finalizer res
+ end
v.add("return {res};")
end
v.add("\}")
end
end
+ redef fun compile_types
+ do
+ compile_color_consts(vt_colors)
+ end
+
redef fun new_visitor do return new SeparateErasureCompilerVisitor(self)
# Stats
end
var class_ptr
- if value.mtype.ctype == "val*" then
+ if not value.mtype.is_c_primitive then
class_ptr = "{value}->class->"
else
var mclass = value.mtype.as(MClassType).mclass
else if mtype isa MVirtualType then
var recv = self.frame.arguments.first
var recv_ptr
- if recv.mtype.ctype == "val*" then
+ if not recv.mtype.is_c_primitive then
recv_ptr = "{recv}->class->"
else
var mclass = recv.mtype.as(MClassType).mclass
var res = self.new_var(mtype)
if compiler.runtime_type_analysis != null and not compiler.runtime_type_analysis.live_types.has(value.mtype.as(MClassType)) then
self.add("/*no boxing of {value.mtype}: {value.mtype} is not live! */")
- self.add("PRINT_ERROR(\"Dead code executed!\\n\"); show_backtrace(1);")
+ self.add("PRINT_ERROR(\"Dead code executed!\\n\"); fatal_exit(1);")
return res
end
self.require_declaration("BOX_{valtype.c_name}")
do
var res = self.get_name("var_class_name")
self.add_decl("const char* {res};")
- if value.mtype.ctype == "val*" then
+ if not value.mtype.is_c_primitive then
self.add "{res} = {value} == NULL ? \"null\" : {value}->class->name;"
else
self.require_declaration("class_{value.mtype.c_name}")
redef fun native_array_instance(elttype, length)
do
- var nclass = self.get_class("NativeArray")
+ var nclass = mmodule.native_array_class
var mtype = nclass.get_mtype([elttype])
var res = self.new_var(mtype)
res.is_exact = true