compiler.do_property_coloring
for m in mainmodule.in_importation.greaters do
for mclass in m.intro_mclasses do
+ if mclass.kind == abstract_kind or mclass.kind == interface_kind then continue
compiler.compile_class_to_c(mclass)
end
end
class SeparateCompiler
super AbstractCompiler
+ redef type VISITOR: SeparateCompilerVisitor
+
# The result of the RTA (used to know live types and methods)
var runtime_type_analysis: RapidTypeAnalysis
private var type_layout: nullable Layout[MType]
private var resolution_layout: nullable Layout[MType]
- protected var method_layout: nullable Layout[MMethod]
+ protected var method_layout: nullable Layout[PropertyLayoutElement]
protected var attr_layout: nullable Layout[MAttribute]
init(mainmodule: MModule, mmbuilder: ModelBuilder, runtime_type_analysis: RapidTypeAnalysis) do
fun compile_color_consts(colors: Map[Object, Int]) do
var v = new_visitor
for m, c in colors do
- if color_consts_done.has(m) then continue
- if m isa MProperty then
- if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
- self.provide_declaration(m.const_color, "#define {m.const_color} {c}")
- else
- self.provide_declaration(m.const_color, "extern const int {m.const_color};")
- v.add("const int {m.const_color} = {c};")
- end
- else if m isa MType then
- if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
- self.provide_declaration(m.const_color, "#define {m.const_color} {c}")
- else
- self.provide_declaration(m.const_color, "extern const int {m.const_color};")
- v.add("const int {m.const_color} = {c};")
- end
+ compile_color_const(v, m, c)
+ end
+ end
+
+ fun compile_color_const(v: SeparateCompilerVisitor, m: Object, color: Int) do
+ if color_consts_done.has(m) then return
+ if m isa MProperty then
+ if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
+ self.provide_declaration(m.const_color, "#define {m.const_color} {color}")
+ else
+ self.provide_declaration(m.const_color, "extern const int {m.const_color};")
+ v.add("const int {m.const_color} = {color};")
+ end
+ else if m isa MPropDef then
+ if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
+ self.provide_declaration(m.const_color, "#define {m.const_color} {color}")
+ else
+ self.provide_declaration(m.const_color, "extern const int {m.const_color};")
+ v.add("const int {m.const_color} = {color};")
+ end
+ else if m isa MType then
+ if modelbuilder.toolcontext.opt_inline_coloring_numbers.value then
+ self.provide_declaration(m.const_color, "#define {m.const_color} {color}")
+ else
+ self.provide_declaration(m.const_color, "extern const int {m.const_color};")
+ v.add("const int {m.const_color} = {color};")
end
- color_consts_done.add(m)
end
+ color_consts_done.add(m)
end
private var color_consts_done = new HashSet[Object]
var mclasses = new HashSet[MClass].from(modelbuilder.model.mclasses)
# Layouts
- var method_layout_builder: PropertyLayoutBuilder[MMethod]
+ var method_layout_builder: PropertyLayoutBuilder[PropertyLayoutElement]
var attribute_layout_builder: PropertyLayoutBuilder[MAttribute]
#FIXME PH and BM layouts too slow for large programs
#if modelbuilder.toolcontext.opt_bm_typing.value then
var class_layout_builder = new MClassColorer(self.mainmodule)
class_layout_builder.build_layout(mclasses)
- method_layout_builder = new MMethodColorer(self.mainmodule, class_layout_builder)
- attribute_layout_builder = new MAttributeColorer(self.mainmodule, class_layout_builder)
+ method_layout_builder = new MPropertyColorer[PropertyLayoutElement](self.mainmodule, class_layout_builder)
+ attribute_layout_builder = new MPropertyColorer[MAttribute](self.mainmodule, class_layout_builder)
#end
+ # lookup properties to build layout with
+ var mmethods = new HashMap[MClass, Set[PropertyLayoutElement]]
+ var mattributes = new HashMap[MClass, Set[MAttribute]]
+ for mclass in mclasses do
+ mmethods[mclass] = new HashSet[PropertyLayoutElement]
+ mattributes[mclass] = new HashSet[MAttribute]
+ for mprop in self.mainmodule.properties(mclass) do
+ if mprop isa MMethod then
+ mmethods[mclass].add(mprop)
+ else if mprop isa MAttribute then
+ mattributes[mclass].add(mprop)
+ end
+ end
+ end
+
+ # lookup super calls and add it to the list of mmethods to build layout with
+ var super_calls = runtime_type_analysis.live_super_sends
+ for mmethoddef in super_calls do
+ var mclass = mmethoddef.mclassdef.mclass
+ mmethods[mclass].add(mmethoddef)
+ for descendant in mclass.in_hierarchy(self.mainmodule).smallers do
+ mmethods[descendant].add(mmethoddef)
+ end
+ end
+
# methods coloration
- var method_layout = method_layout_builder.build_layout(mclasses)
- self.method_tables = build_method_tables(mclasses, method_layout)
+ self.method_layout = method_layout_builder.build_layout(mmethods)
+ self.method_tables = build_method_tables(mclasses, super_calls)
self.compile_color_consts(method_layout.pos)
- self.method_layout = method_layout
+
+ # 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
+ end
# attributes coloration
- var attr_layout = attribute_layout_builder.build_layout(mclasses)
- self.attr_tables = build_attr_tables(mclasses, attr_layout)
+ self.attr_layout = attribute_layout_builder.build_layout(mattributes)
+ self.attr_tables = build_attr_tables(mclasses)
self.compile_color_consts(attr_layout.pos)
- self.attr_layout = attr_layout
end
- fun build_method_tables(mclasses: Set[MClass], layout: Layout[MProperty]): Map[MClass, Array[nullable MPropDef]] do
+ fun build_method_tables(mclasses: Set[MClass], super_calls: Set[MMethodDef]): Map[MClass, Array[nullable MPropDef]] do
+ var layout = self.method_layout
var tables = new HashMap[MClass, Array[nullable MPropDef]]
for mclass in mclasses do
var table = new Array[nullable MPropDef]
+ var supercalls = new List[MMethodDef]
+
# first, fill table from parents by reverse linearization order
var parents = new Array[MClass]
if mainmodule.flatten_mclass_hierarchy.has(mclass) then
parents = mclass.in_hierarchy(mainmodule).greaters.to_a
self.mainmodule.linearize_mclasses(parents)
end
+
for parent in parents do
if parent == mclass then continue
for mproperty in self.mainmodule.properties(parent) do
end
end
end
+
+ # lookup for super calls in super classes
+ for mmethoddef in super_calls do
+ for mclassdef in parent.mclassdefs do
+ if mclassdef.mpropdefs.has(mmethoddef) then
+ supercalls.add(mmethoddef)
+ end
+ end
+ end
end
# then override with local properties
end
end
end
+
+ # lookup for super calls in local class
+ for mmethoddef in super_calls do
+ for mclassdef in mclass.mclassdefs do
+ if mclassdef.mpropdefs.has(mmethoddef) then
+ supercalls.add(mmethoddef)
+ end
+ end
+ end
+ # insert super calls in table according to receiver
+ for supercall in supercalls do
+ var color = layout.pos[supercall]
+ if table.length <= color then
+ for i in [table.length .. color[ do
+ table[i] = null
+ end
+ end
+ var mmethoddef = supercall.lookup_next_definition(self.mainmodule, mclass.intro.bound_mtype)
+ table[color] = mmethoddef
+ end
tables[mclass] = table
end
return tables
end
- fun build_attr_tables(mclasses: Set[MClass], layout: Layout[MProperty]): Map[MClass, Array[nullable MPropDef]] do
+ fun build_attr_tables(mclasses: Set[MClass]): Map[MClass, Array[nullable MPropDef]] do
+ var layout = self.attr_layout
var tables = new HashMap[MClass, Array[nullable MPropDef]]
for mclass in mclasses do
var table = new Array[nullable MPropDef]
var attrs = self.attr_tables[mclass]
var v = new_visitor
+ var is_dead = not runtime_type_analysis.live_classes.has(mclass) and mtype.ctype == "val*" and mclass.name != "NativeArray"
+
v.add_decl("/* runtime class {c_name} */")
# Build class vft
- self.provide_declaration("class_{c_name}", "extern const struct class class_{c_name};")
- v.add_decl("const struct class class_{c_name} = \{")
- v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
- v.add_decl("\{")
- for i in [0 .. vft.length[ do
- var mpropdef = vft[i]
- if mpropdef == null then
- v.add_decl("NULL, /* empty */")
- else
- assert mpropdef isa MMethodDef
- var rf = mpropdef.virtual_runtime_function
- v.require_declaration(rf.c_name)
- v.add_decl("(nitmethod_t){rf.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
+ if not is_dead then
+ self.provide_declaration("class_{c_name}", "extern const struct class class_{c_name};")
+ v.add_decl("const struct class class_{c_name} = \{")
+ v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
+ v.add_decl("\{")
+ for i in [0 .. vft.length[ do
+ var mpropdef = vft[i]
+ if mpropdef == null then
+ v.add_decl("NULL, /* empty */")
+ else
+ assert mpropdef isa MMethodDef
+ var rf = mpropdef.virtual_runtime_function
+ v.require_declaration(rf.c_name)
+ v.add_decl("(nitmethod_t){rf.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
+ end
end
+ v.add_decl("\}")
+ v.add_decl("\};")
end
- v.add_decl("\}")
- v.add_decl("\};")
if mtype.ctype != "val*" then
#Build instance struct
self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(const struct type* type);")
v.add_decl("/* allocate {mtype} */")
v.add_decl("{mtype.ctype} NEW_{c_name}(const struct type* type) \{")
- 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));")
- v.add("{res}->type = type;")
- hardening_live_type(v, "type")
- v.require_declaration("class_{c_name}")
- v.add("{res}->class = &class_{c_name};")
- self.generate_init_attr(v, res, mtype)
- v.add("return {res};")
+ if is_dead then
+ v.add_abort("{mclass} is DEAD")
+ else
+ 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));")
+ v.add("{res}->type = type;")
+ hardening_live_type(v, "type")
+ v.require_declaration("class_{c_name}")
+ v.add("{res}->class = &class_{c_name};")
+ self.generate_init_attr(v, res, mtype)
+ v.add("return {res};")
+ end
v.add("\}")
generate_check_init_instance(mtype)
self.provide_declaration("CHECK_NEW_{c_name}", "void CHECK_NEW_{c_name}({mtype.ctype});")
v.add_decl("/* allocate {mtype} */")
v.add_decl("void CHECK_NEW_{c_name}({mtype.ctype} {res}) \{")
- self.generate_check_attr(v, res, mtype)
+ if runtime_type_analysis.live_classes.has(mtype.mclass) then
+ self.generate_check_attr(v, res, mtype)
+ else
+ v.add_abort("{mtype.mclass} is DEAD")
+ end
v.add("\}")
end
var res = self.new_var(mtype)
if not compiler.runtime_type_analysis.live_types.has(valtype) then
self.add("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
- self.add("printf(\"Dead code executed!\\n\"); exit(1);")
+ self.add("printf(\"Dead code executed!\\n\"); show_backtrace(1);")
return res
end
self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
# Bad things will appen!
var res = self.new_var(mtype)
self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
- self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); exit(1);")
+ self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); show_backtrace(1);")
return res
end
end
redef fun send(mmethod, arguments)
do
+ self.varargize(mmethod.intro, mmethod.intro.msignature.as(not null), arguments)
+ return table_send(mmethod, arguments, mmethod.const_color)
+ end
+
+ private fun table_send(mmethod: MMethod, arguments: Array[RuntimeVariable], const_color: String): nullable RuntimeVariable
+ do
if arguments.first.mcasttype.ctype != "val*" then
# In order to shortcut the primitive, we need to find the most specific method
# Howverr, because of performance (no flattening), we always work on the realmainmodule
var recv = arguments.first
s.append("val*")
ss.append("{recv}")
- self.varargize(mmethod.intro, mmethod.intro.msignature.as(not null), arguments)
for i in [0..msignature.arity[ do
var a = arguments[i+1]
var t = msignature.mparameters[i].mtype
var r
if ret == null then r = "void" else r = ret.ctype
- self.require_declaration(mmethod.const_color)
- var call = "(({r} (*)({s}))({arguments.first}->class->vft[{mmethod.const_color}]))({ss}) /* {mmethod} on {arguments.first.inspect}*/"
+ self.require_declaration(const_color)
+ var call = "(({r} (*)({s}))({arguments.first}->class->vft[{const_color}]))({ss}) /* {mmethod} on {arguments.first.inspect}*/"
if res != null then
self.add("{res} = {call};")
return res
end
- redef fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable
+ redef fun supercall(m: MMethodDef, recvtype: MClassType, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
do
- # FIXME implements a polymorphic access in tables
- m = m.lookup_next_definition(m.mclassdef.mmodule, m.mclassdef.bound_mtype)
- return self.call(m, recvtype, args)
+ return table_send(m.mproperty, arguments, m.const_color)
end
redef fun vararg_instance(mpropdef, recv, varargs, elttype)
self.add("count_type_test_resolved_{tag}++;")
end
else
- self.add("printf(\"NOT YET IMPLEMENTED: type_test(%s, {mtype}).\\n\", \"{value.inspect}\"); exit(1);")
+ self.add("printf(\"NOT YET IMPLEMENTED: type_test(%s, {mtype}).\\n\", \"{value.inspect}\"); show_backtrace(1);")
end
# check color is in table
redef class MProperty
fun const_color: String do return "COLOR_{c_name}"
end
+
+redef class MPropDef
+ fun const_color: String do return "COLOR_{c_name}"
+end