import abstract_compiler
import layout_builders
import rapid_type_analysis
-import collect_super_sends
import compiler_ffi
# Add separate compiler specific options
end
end
+ # Collect all super calls (dead or not)
+ var all_super_calls = new HashSet[MMethodDef]
+ for mmodule in self.mainmodule.in_importation.greaters do
+ for mclassdef in mmodule.mclassdefs do
+ for mpropdef in mclassdef.mpropdefs do
+ if not mpropdef isa MMethodDef then continue
+ if mpropdef.has_supercall then
+ all_super_calls.add(mpropdef)
+ end
+ end
+ end
+ end
+
# lookup super calls and add it to the list of mmethods to build layout with
var super_calls
if runtime_type_analysis != null then
super_calls = runtime_type_analysis.live_super_sends
else
- super_calls = modelbuilder.collect_super_sends
+ super_calls = all_super_calls
end
+
for mmethoddef in super_calls do
var mclass = mmethoddef.mclassdef.mclass
mmethods[mclass].add(mmethoddef)
self.compile_color_consts(method_layout.pos)
# attribute null color to dead supercalls
- for mmodule in self.mainmodule.in_importation.greaters do
- for mclassdef in mmodule.mclassdefs do
- for mpropdef in mclassdef.mpropdefs do
- if mpropdef.has_supercall then
- compile_color_const(new_visitor, mpropdef, -1)
- end
- end
- end
+ for mpropdef in all_super_calls do
+ if super_calls.has(mpropdef) then continue
+ compile_color_const(new_visitor, mpropdef, -1)
end
# attributes coloration
do
var mtype = mclass.intro.bound_mtype
var c_name = mclass.c_name
+ var c_instance_name = mclass.c_instance_name
var vft = self.method_tables[mclass]
var attrs = self.attr_tables[mclass]
end
if mtype.ctype != "val*" then
- #Build instance struct
- self.header.add_decl("struct instance_{c_name} \{")
- self.header.add_decl("const struct type *type;")
- self.header.add_decl("const struct class *class;")
- self.header.add_decl("{mtype.ctype} value;")
- self.header.add_decl("\};")
+ if mtype.mclass.name == "Pointer" or mtype.mclass.kind != extern_kind then
+ #Build instance struct
+ self.header.add_decl("struct instance_{c_instance_name} \{")
+ self.header.add_decl("const struct type *type;")
+ self.header.add_decl("const struct class *class;")
+ self.header.add_decl("{mtype.ctype} value;")
+ self.header.add_decl("\};")
+ end
if not self.runtime_type_analysis.live_types.has(mtype) then return
#Build BOX
- self.header.add_decl("val* BOX_{c_name}({mtype.ctype});")
+ self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype});")
v.add_decl("/* allocate {mtype} */")
v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
- v.add("struct instance_{c_name}*res = nit_alloc(sizeof(struct instance_{c_name}));")
+ v.add("struct instance_{c_instance_name}*res = nit_alloc(sizeof(struct instance_{c_instance_name}));")
v.require_declaration("type_{c_name}")
v.add("res->type = &type_{c_name};")
v.require_declaration("class_{c_name}")
return
else if mclass.name == "NativeArray" then
#Build instance struct
- self.header.add_decl("struct instance_{c_name} \{")
+ self.header.add_decl("struct instance_{c_instance_name} \{")
self.header.add_decl("const struct type *type;")
self.header.add_decl("const struct class *class;")
# NativeArrays are just a instance header followed by an array of values
var res = v.new_named_var(mtype, "self")
res.is_exact = true
var mtype_elt = mtype.arguments.first
- v.add("{res} = nit_alloc(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
+ v.add("{res} = nit_alloc(sizeof(struct instance_{c_instance_name}) + length*sizeof({mtype_elt.ctype}));")
v.add("{res}->type = type;")
hardening_live_type(v, "type")
v.require_declaration("class_{c_name}")
else if value.mtype.ctype == "val*" and mtype.ctype == "val*" then
return value
else if value.mtype.ctype == "val*" then
- return self.new_expr("((struct instance_{mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype)
+ return self.new_expr("((struct instance_{mtype.c_instance_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype)
else if mtype.ctype == "val*" then
var valtype = value.mtype.as(MClassType)
var res = self.new_var(mtype)
self.add("printf(\"Dead code executed!\\n\"); show_backtrace(1);")
return res
end
+ self.require_declaration("BOX_{valtype.c_name}")
self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
return res
else if value.mtype.cname_blind == "void*" and mtype.cname_blind == "void*" then
if value.mtype.ctype == "val*" then
return "{value}->type"
else
+ compiler.undead_types.add(value.mtype)
self.require_declaration("type_{value.mtype.c_name}")
return "(&type_{value.mtype.c_name})"
end
# The attribute is primitive, thus we store it in a box
# The trick is to create the box the first time then resuse the box
self.add("if ({attr} != NULL) \{")
- self.add("((struct instance_{mtype.c_name}*){attr})->value = {value}; /* {a} on {recv.inspect} */")
+ self.add("((struct instance_{mtype.c_instance_name}*){attr})->value = {value}; /* {a} on {recv.inspect} */")
self.add("\} else \{")
value = self.autobox(value, self.object_type.as_nullable)
self.add("{attr} = {value}; /* {a} on {recv.inspect} */")
end
end
if primitive != null then
- test.add("((struct instance_{primitive.c_name}*){value1})->value == ((struct instance_{primitive.c_name}*){value2})->value")
+ test.add("((struct instance_{primitive.c_instance_name}*){value1})->value == ((struct instance_{primitive.c_instance_name}*){value2})->value")
else if can_be_primitive(value1) and can_be_primitive(value2) then
test.add("{value1}->class == {value2}->class")
var s = new Array[String]
for t, v in self.compiler.box_kinds do
- s.add "({value1}->class->box_kind == {v} && ((struct instance_{t.c_name}*){value1})->value == ((struct instance_{t.c_name}*){value2})->value)"
+ s.add "({value1}->class->box_kind == {v} && ((struct instance_{t.c_instance_name}*){value1})->value == ((struct instance_{t.c_instance_name}*){value2})->value)"
end
test.add("({s.join(" || ")})")
else
do
var elttype = arguments.first.mtype
var nclass = self.get_class("NativeArray")
- var recv = "((struct instance_{nclass.c_name}*){arguments[0]})->values"
+ var recv = "((struct instance_{nclass.c_instance_name}*){arguments[0]})->values"
if pname == "[]" then
self.ret(self.new_expr("{recv}[{arguments[1]}]", ret_type.as(not null)))
return
self.add("{recv}[{arguments[1]}]={arguments[2]};")
return
else if pname == "copy_to" then
- var recv1 = "((struct instance_{nclass.c_name}*){arguments[1]})->values"
+ var recv1 = "((struct instance_{nclass.c_instance_name}*){arguments[1]})->values"
self.add("memcpy({recv1}, {recv}, {arguments[2]}*sizeof({elttype.ctype}));")
return
end
redef class MType
fun const_color: String do return "COLOR_{c_name}"
+
+ # C name of the instance type to use
+ fun c_instance_name: String do return c_name
+end
+
+redef class MClassType
+ redef fun c_instance_name do return mclass.c_instance_name
+end
+
+redef class MClass
+ # Extern classes use the C instance of kernel::Pointer
+ fun c_instance_name: String
+ do
+ if kind == extern_kind then
+ return "kernel__Pointer"
+ else return c_name
+ end
end
redef class MProperty