class
super SeparateCompiler
private var class_ids: Map[MClass, Int] is noinit
private var class_colors: Map[MClass, Int] is noinit
protected var : Map[MVirtualTypeProp, Int] is noinit
init do
# Class coloring
var poset = mainmodule.flatten_mclass_hierarchy
var mclasses = new HashSet[MClass].from(poset)
var colorer = new POSetColorer[MClass]
colorer.colorize(poset)
class_ids = colorer.ids
class_colors = colorer.colors
class_tables = self.build_class_typing_tables(mclasses)
# lookup vt to build layout with
var vts = new HashMap[MClass, Set[MVirtualTypeProp]]
for mclass in mclasses do
vts[mclass] = new HashSet[MVirtualTypeProp]
for mprop in self.mainmodule.properties(mclass) do
if mprop isa MVirtualTypeProp then
vts[mclass].add(mprop)
end
end
end
# vt coloration
var vt_colorer = new POSetBucketsColorer[MClass, MVirtualTypeProp](poset, colorer.conflicts)
vt_colors = vt_colorer.colorize(vts)
vt_tables = build_vt_tables(mclasses)
end
fun (mclasses: Set[MClass]): Map[MClass, Array[nullable MPropDef]] do
var tables = new HashMap[MClass, Array[nullable MPropDef]]
for mclass in mclasses do
var table = new Array[nullable MPropDef]
# 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
if not mproperty isa MVirtualTypeProp then continue
var color = vt_colors[mproperty]
if table.length <= color then
for i in [table.length .. color[ do
table[i] = null
end
end
for mpropdef in mproperty.mpropdefs do
if mpropdef.mclassdef.mclass == parent then
table[color] = mpropdef
end
end
end
end
# then override with local properties
for mproperty in self.mainmodule.properties(mclass) do
if not mproperty isa MVirtualTypeProp then continue
var color = vt_colors[mproperty]
if table.length <= color then
for i in [table.length .. color[ do
table[i] = null
end
end
for mpropdef in mproperty.mpropdefs do
if mpropdef.mclassdef.mclass == mclass then
table[color] = mpropdef
end
end
end
tables[mclass] = table
end
return tables
end
# Build class tables
fun build_class_typing_tables(mclasses: Set[MClass]): Map[MClass, Array[nullable MClass]] do
var tables = new HashMap[MClass, Array[nullable MClass]]
for mclass in mclasses do
var table = new Array[nullable MClass]
var supers = new Array[MClass]
if mainmodule.flatten_mclass_hierarchy.has(mclass) then
supers = mclass.in_hierarchy(mainmodule).greaters.to_a
end
for sup in supers do
var color = class_colors[sup]
if table.length <= color then
for i in [table.length .. color[ do
table[i] = null
end
end
table[color] = sup
end
tables[mclass] = table
end
return tables
end
redef fun compile_class_if_universal(ccinfo, v)
do
var mclass = ccinfo.mclass
var mtype = ccinfo.mtype
var c_name = mclass.c_name
var is_dead = ccinfo.is_dead
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("{mtype.ctype} value;")
self.header.add_decl("\};")
#Build BOX
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_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};")
v.add("res->value = value;")
v.add("return (val*)res;")
v.add("\}")
if mtype.mclass.name != "Pointer" then return true
v = new_visitor
self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
v.add_decl("/* allocate {mtype} */")
v.add_decl("{mtype.ctype} NEW_{c_name}() \{")
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_{mtype.c_name}));")
v.require_declaration("class_{c_name}")
v.add("{res}->class = &class_{c_name};")
v.add("((struct instance_{mtype.c_name}*){res})->value = NULL;")
v.add("return {res};")
end
v.add("\}")
return true
else if mclass.name == "NativeArray" then
#Build instance struct
self.header.add_decl("struct instance_{c_name} \{")
self.header.add_decl("const struct class *class;")
self.header.add_decl("int length;")
self.header.add_decl("val* values[];")
self.header.add_decl("\};")
#Build NEW
self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(int length);")
v.add_decl("/* allocate {mtype} */")
v.add_decl("{mtype.ctype} NEW_{c_name}(int length) \{")
var res = v.get_name("self")
v.add_decl("struct instance_{c_name} *{res};")
var mtype_elt = mtype.arguments.first
v.add("{res} = nit_alloc(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
v.require_declaration("class_{c_name}")
v.add("{res}->class = &class_{c_name};")
v.add("{res}->length = length;")
v.add("return (val*){res};")
v.add("\}")
return true
else if mclass.name == "RoutineRef" then
self.header.add_decl("struct instance_{c_name} \{")
self.header.add_decl("const struct class *class;")
self.header.add_decl("val* recv;")
self.header.add_decl("nitmethod_t method;")
self.header.add_decl("\};")
self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(val* recv, nitmethod_t method, const struct class* class);")
v.add_decl("/* allocate {mtype} */")
v.add_decl("{mtype.ctype} NEW_{c_name}(val* recv, nitmethod_t method, const struct class* class)\{")
var res = v.get_name("self")
v.add_decl("struct instance_{c_name} *{res};")
var alloc = v.nit_alloc("sizeof(struct instance_{c_name})", mclass.full_name)
v.add("{res} = {alloc};")
v.add("{res}->class = class;")
v.add("{res}->recv = recv;")
v.add("{res}->method = method;")
v.add("return (val*){res};")
v.add("\}")
return true
else if mtype.mclass.kind == extern_kind and mtype.mclass.name != "CString" then
var pointer_type = mainmodule.pointer_type
self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
v.add_decl("/* allocate {mtype} */")
v.add_decl("{mtype.ctype} NEW_{c_name}() \{")
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_{pointer_type.c_name}));")
#v.add("{res}->type = type;")
v.require_declaration("class_{c_name}")
v.add("{res}->class = &class_{c_name};")
v.add("((struct instance_{pointer_type.c_name}*){res})->value = NULL;")
v.add("return {res};")
end
v.add("\}")
return true
end
return false
end
redef fun compile_class_vft(ccinfo, v)
do
var mclass = ccinfo.mclass
var = ccinfo.mtype
var c_name = mclass.c_name
var is_dead = ccinfo.is_dead
var rta = runtime_type_analysis
# 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("{class_ids[mclass]},")
v.add_decl("\"{mclass.name}\", /* class_name_string */")
v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
v.add_decl("{class_colors[mclass]},")
if not is_dead then
if build_class_vts_table(mclass) then
v.require_declaration("vts_table_{c_name}")
v.add_decl("&vts_table_{c_name},")
else
v.add_decl("NULL,")
end
v.add_decl("&type_table_{c_name},")
v.add_decl("\{")
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 */")
else
assert mpropdef isa MMethodDef
if rta != null and not rta.live_methoddefs.has(mpropdef) then
v.add_decl("NULL, /* DEAD {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
continue
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("\}")
end
v.add_decl("\};")
end
protected fun (ccinfo: ClassCompilationInfo, v: SeparateCompilerVisitor)
do
var mclass = ccinfo.mclass
var c_name = mclass.c_name
var class_table = self.class_tables[mclass]
# Build class type table
v.add_decl("const struct type_table type_table_{c_name} = \{")
v.add_decl("{class_table.length},")
v.add_decl("\{")
for msuper in class_table do
if msuper == null then
v.add_decl("-1, /* empty */")
else
v.add_decl("{self.class_ids[msuper]}, /* {msuper} */")
end
end
v.add_decl("\}")
v.add_decl("\};")
end
redef fun compile_default_new(ccinfo, v)
do
var mclass = ccinfo.mclass
var mtype = ccinfo.mtype
var c_name = mclass.c_name
var is_dead = ccinfo.is_dead
#Build NEW
self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(void);")
v.add_decl("/* allocate {mtype} */")
v.add_decl("{mtype.ctype} NEW_{c_name}(void) \{")
if is_dead then
v.add_abort("{mclass} is DEAD")
else
var res = v.new_named_var(mtype, "self")
res.is_exact = true
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};")
if attrs != null then
self.generate_init_attr(v, res, mtype)
v.set_finalizer res
end
v.add("return {res};")
end
v.add("\}")
end
redef fun build_class_compilation_info(mclass)
do
var ccinfo = super
var mtype = ccinfo.mtype
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 not mtype.is_c_primitive and mclass.name != "NativeArray" then
is_dead = true
end
ccinfo.is_dead = is_dead
return ccinfo
end
redef fun compile_class_to_c(mclass: )
do
var ccinfo = build_class_compilation_info(mclass)
var v = new_visitor
v.add_decl("/* runtime class {mclass.c_name} */")
self.provide_declaration("class_{mclass.c_name}", "extern const struct class class_{mclass.c_name};")
v.add_decl("extern const struct type_table type_table_{mclass.c_name};")
self.compile_class_vft(ccinfo, v)
self.compile_class_type_table(ccinfo, v)
if not self.compile_class_if_universal(ccinfo, v) then
self.compile_default_new(ccinfo, v)
end
end
private fun build_class_vts_table(mclass: MClass): Bool do
if self.vt_tables[mclass].is_empty then return false
self.provide_declaration("vts_table_{mclass.c_name}", "extern const struct vts_table vts_table_{mclass.c_name};")
var v = new_visitor
v.add_decl("const struct vts_table vts_table_{mclass.c_name} = \{")
v.add_decl("0, /* dummy */")
v.add_decl("\{")
for vt in self.vt_tables[mclass] do
if vt == null then
v.add_decl("\{-1, NULL\}, /* empty */")
else
var is_null = 0
var bound = retrieve_vt_bound(mclass.intro.bound_mtype, vt.as(MVirtualTypeDef).bound)
while bound isa MNullableType do
bound = retrieve_vt_bound(mclass.intro.bound_mtype, bound.mtype)
is_null = 1
end
var vtclass = bound.as(MClassType).mclass
v.require_declaration("class_{vtclass.c_name}")
v.add_decl("\{{is_null}, &class_{vtclass.c_name}\}, /* {vt} */")
end
end
v.add_decl("\},")
v.add_decl("\};")
return true
end
private fun retrieve_vt_bound(anchor: MClassType, mtype: nullable MType): MType do
if mtype == null then
print "NOT YET IMPLEMENTED: retrieve_vt_bound on null"
abort
end
if mtype isa MVirtualType then
return mtype.anchor_to(mainmodule, anchor)
else if mtype isa MParameterType then
return mtype.anchor_to(mainmodule, anchor)
else
return mtype
end
end
redef fun compile_types
do
compile_color_consts(vt_colors)
end
redef fun new_visitor do return new SeparateErasureCompilerVisitor(self)
# Stats
private var class_tables: Map[MClass, Array[nullable MClass]] is noinit
private var vt_tables: Map[MClass, Array[nullable MPropDef]] is noinit
redef fun display_sizes
do
print "# size of subtyping tables"
print "\ttotal \tholes"
var total = 0
var holes = 0
for t, table in class_tables do
total += table.length
for e in table do if e == null then holes += 1
end
print "\t{total}\t{holes}"
print "# size of resolution tables"
print "\ttotal \tholes"
total = 0
holes = 0
for t, table in vt_tables do
total += table.length
for e in table do if e == null then holes += 1
end
print "\t{total}\t{holes}"
print "# size of methods tables"
print "\ttotal \tholes"
total = 0
holes = 0
for t, table in method_tables do
total += table.length
for e in table do if e == null then holes += 1
end
print "\t{total}\t{holes}"
print "# size of attributes tables"
print "\ttotal \tholes"
total = 0
holes = 0
for t, table in attr_tables do
total += table.length
for e in table do if e == null then holes += 1
end
print "\t{total}\t{holes}"
end
end
src/compiler/separate_erasure_compiler.nit:81,1--533,3