nitg*: extern classes a polymorph in Nit, and unboxed only for extern methods
[nit.git] / src / separate_erasure_compiler.nit
index 4ff60ac..af3a43c 100644 (file)
@@ -21,13 +21,40 @@ intrude import separate_compiler
 redef class ToolContext
        # --erasure
        var opt_erasure: OptionBool = new OptionBool("Erase generic types", "--erasure")
+       # --rta
+       var opt_rta = new OptionBool("Activate RTA (implicit with --global and --separate)", "--rta")
        # --no-check-erasure-cast
        var opt_no_check_erasure_cast: OptionBool = new OptionBool("Disable implicit casts on unsafe return with erasure-typing policy (dangerous)", "--no-check-erasure-cast")
 
        redef init
        do
                super
-               self.option_context.add_option(self.opt_erasure, self.opt_no_check_erasure_cast)
+               self.option_context.add_option(self.opt_erasure, self.opt_no_check_erasure_cast, opt_rta)
+       end
+
+       redef fun process_options(args)
+       do
+               super
+
+               if opt_no_check_all.value then
+                       opt_no_check_erasure_cast.value = true
+               end
+       end
+
+       var erasure_compiler_phase = new ErasureCompilerPhase(self, null)
+end
+
+class ErasureCompilerPhase
+       super Phase
+       redef fun process_mainmodule(mainmodule, given_mmodules) do
+               if not toolcontext.opt_erasure.value then return
+
+               var modelbuilder = toolcontext.modelbuilder
+               var analysis = null
+               if toolcontext.opt_rta.value then
+                       analysis = modelbuilder.do_rapid_type_analysis(mainmodule)
+               end
+               modelbuilder.run_separate_erasure_compiler(mainmodule, analysis)
        end
 end
 
@@ -49,10 +76,11 @@ redef class ModelBuilder
                                compiler.compile_class_to_c(mclass)
                        end
                end
-               compiler.compile_color_consts(compiler.vt_layout.pos)
+               compiler.compile_color_consts(compiler.vt_colors)
 
                # The main function of the C
                compiler.new_file("{mainmodule.name}.main")
+               compiler.compile_nitni_global_ref_functions
                compiler.compile_main_function
 
                # compile methods
@@ -73,30 +101,21 @@ end
 class SeparateErasureCompiler
        super SeparateCompiler
 
-       private var class_layout: nullable Layout[MClass]
-       protected var vt_layout: nullable Layout[MVirtualTypeProp]
+       private var class_ids: Map[MClass, Int]
+       private var class_colors: Map[MClass, Int]
+       protected var vt_colors: Map[MVirtualTypeProp, Int]
 
        init(mainmodule: MModule, mmbuilder: ModelBuilder, runtime_type_analysis: nullable RapidTypeAnalysis) do
                super
 
-               var mclasses = new HashSet[MClass].from(mmbuilder.model.mclasses)
-
-               var layout_builder: TypingLayoutBuilder[MClass]
-               var class_colorer = new MClassColorer(mainmodule)
-               if modelbuilder.toolcontext.opt_phmod_typing.value then
-                       layout_builder = new MClassHasher(new PHModOperator, mainmodule)
-                       class_colorer.build_layout(mclasses)
-               else if modelbuilder.toolcontext.opt_phand_typing.value then
-                       layout_builder = new MClassHasher(new PHAndOperator, mainmodule)
-                       class_colorer.build_layout(mclasses)
-               else if modelbuilder.toolcontext.opt_bm_typing.value then
-                       layout_builder = new MClassBMizer(mainmodule)
-                       class_colorer.build_layout(mclasses)
-               else
-                       layout_builder = class_colorer
-               end
-               self.class_layout = layout_builder.build_layout(mclasses)
-               self.class_tables = self.build_class_typing_tables(mclasses)
+               # 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]]
@@ -110,13 +129,12 @@ class SeparateErasureCompiler
                end
 
                # vt coloration
-               var vt_coloring = new MPropertyColorer[MVirtualTypeProp](mainmodule, class_colorer)
-               var vt_layout = vt_coloring.build_layout(vts)
-               self.vt_tables = build_vt_tables(mclasses, vt_layout)
-               self.vt_layout = vt_layout
+               var vt_colorer = new POSetBucketsColorer[MClass, MVirtualTypeProp](poset, colorer.conflicts)
+               vt_colors = vt_colorer.colorize(vts)
+               vt_tables = build_vt_tables(mclasses)
        end
 
-       fun build_vt_tables(mclasses: Set[MClass], layout: Layout[MProperty]): Map[MClass, Array[nullable MPropDef]] do
+       fun build_vt_tables(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]
@@ -130,7 +148,7 @@ class SeparateErasureCompiler
                                if parent == mclass then continue
                                for mproperty in self.mainmodule.properties(parent) do
                                        if not mproperty isa MVirtualTypeProp then continue
-                                       var color = layout.pos[mproperty]
+                                       var color = vt_colors[mproperty]
                                        if table.length <= color then
                                                for i in [table.length .. color[ do
                                                        table[i] = null
@@ -147,7 +165,7 @@ class SeparateErasureCompiler
                        # then override with local properties
                        for mproperty in self.mainmodule.properties(mclass) do
                                if not mproperty isa MVirtualTypeProp then continue
-                               var color = layout.pos[mproperty]
+                               var color = vt_colors[mproperty]
                                if table.length <= color then
                                        for i in [table.length .. color[ do
                                                table[i] = null
@@ -167,7 +185,6 @@ class SeparateErasureCompiler
        # 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]]
-               var layout = self.class_layout
                for mclass in mclasses do
                        var table = new Array[nullable MClass]
                        var supers = new Array[MClass]
@@ -175,12 +192,7 @@ class SeparateErasureCompiler
                                supers = mclass.in_hierarchy(mainmodule).greaters.to_a
                        end
                        for sup in supers do
-                               var color: Int
-                               if layout isa PHLayout[MClass, MClass] then
-                                       color = layout.hashes[mclass][sup]
-                               else
-                                       color = layout.pos[sup]
-                               end
+                               var color = class_colors[sup]
                                if table.length <= color then
                                        for i in [table.length .. color[ do
                                                table[i] = null
@@ -199,19 +211,7 @@ class SeparateErasureCompiler
                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. */")
                self.header.add_decl("struct type_table \{ int size; int table[]; \}; /* colorized type table. */")
                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. */")
-
-               if self.vt_layout isa PHLayout[MClass, MVirtualTypeProp] then
-                       self.header.add_decl("struct vts_table \{ int mask; const struct vts_entry vts[]; \}; /* vts list of a C type representation. */")
-               else
-                       self.header.add_decl("struct vts_table \{ int dummy; const struct vts_entry vts[]; \}; /* vts list of a C type representation. */")
-               end
-
-               if modelbuilder.toolcontext.opt_phmod_typing.value then
-                       self.header.add_decl("#define HASH(mask, id) ((mask)%(id))")
-               else if modelbuilder.toolcontext.opt_phand_typing.value then
-                       self.header.add_decl("#define HASH(mask, id) ((mask)&(id))")
-               end
-
+               self.header.add_decl("struct vts_table \{ int dummy; const struct vts_entry vts[]; \}; /* vts list of a C type representation. */")
                self.header.add_decl("typedef struct instance \{ const struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
        end
 
@@ -219,13 +219,18 @@ class SeparateErasureCompiler
        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]
                var class_table = self.class_tables[mclass]
                var v = self.new_visitor
 
+               var rta = runtime_type_analysis
+               var is_dead = 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
+                       is_dead = true
+               end
+
                v.add_decl("/* runtime class {c_name} */")
 
                self.provide_declaration("class_{c_name}", "extern const struct class class_{c_name};")
@@ -233,38 +238,40 @@ class SeparateErasureCompiler
 
                # Build class vft
                v.add_decl("const struct class class_{c_name} = \{")
-               v.add_decl("{self.class_layout.ids[mclass]},")
+               v.add_decl("{class_ids[mclass]},")
                v.add_decl("\"{mclass.name}\", /* class_name_string */")
                v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
-               var layout = self.class_layout
-               if layout isa PHLayout[MClass, MClass] then
-                       v.add_decl("{layout.masks[mclass]},")
-               else
-                       v.add_decl("{layout.pos[mclass]},")
-               end
-               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("\{")
-               for i in [0 .. vft.length[ do
-                       var mpropdef = vft[i]
-                       if mpropdef == null then
-                               v.add_decl("NULL, /* empty */")
+               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
-                               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} */")
+                               v.add_decl("NULL,")
+                       end
+                       v.add_decl("&type_table_{c_name},")
+                       v.add_decl("\{")
+                       for i in [0 .. vft.length[ do
+                               var mpropdef = vft[i]
+                               if mpropdef == null then
+                                       v.add_decl("NULL, /* empty */")
                                else
-                                       v.require_declaration("{mpropdef.c_name}")
-                                       v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
+                                       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
+                                       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
                                end
                        end
+                       v.add_decl("\}")
                end
-               v.add_decl("\}")
                v.add_decl("\};")
 
                # Build class type table
@@ -276,36 +283,54 @@ class SeparateErasureCompiler
                        if msuper == null then
                                v.add_decl("-1, /* empty */")
                        else
-                               v.add_decl("{self.class_layout.ids[msuper]}, /* {msuper} */")
+                               v.add_decl("{self.class_ids[msuper]}, /* {msuper} */")
                        end
                end
                v.add_decl("\}")
                v.add_decl("\};")
 
-               if mtype.ctype != "val*" then
-                       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 class *class;")
-                               self.header.add_decl("{mtype.ctype} value;")
-                               self.header.add_decl("\};")
-                       end
+               if mtype.ctype != "val*" 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});")
                        v.add_decl("/* allocate {mtype} */")
                        v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
-                       v.add("struct instance_{c_instance_name}*res = nit_alloc(sizeof(struct instance_{c_instance_name}));")
+                       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
+
+                       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
                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("\};")
 
@@ -313,13 +338,34 @@ class SeparateErasureCompiler
                        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.new_named_var(mtype, "self")
-                       res.is_exact = true
+                       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("return {res};")
+                       v.add("{res}->length = length;")
+                       v.add("return (val*){res};")
+                       v.add("\}")
+                       return
+               else if mtype.mclass.kind == extern_kind and mtype.mclass.name != "NativeString" 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
                end
@@ -328,13 +374,19 @@ class SeparateErasureCompiler
                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) \{")
-               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.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.require_declaration("class_{c_name}")
+                       v.add("{res}->class = &class_{c_name};")
+                       self.generate_init_attr(v, res, mtype)
+                       v.set_finalizer res
+                       v.add("return {res};")
+               end
                v.add("\}")
        end
 
@@ -345,12 +397,7 @@ class SeparateErasureCompiler
 
                var v = new_visitor
                v.add_decl("const struct vts_table vts_table_{mclass.c_name} = \{")
-               if self.vt_layout isa PHLayout[MClass, MVirtualTypeProp] then
-                       #TODO redo this when PHPropertyLayoutBuilder will be implemented
-                       #v.add_decl("{vt_masks[mclass]},")
-               else
-                       v.add_decl("0, /* dummy */")
-               end
+               v.add_decl("0, /* dummy */")
                v.add_decl("\{")
 
                for vt in self.vt_tables[mclass] do
@@ -532,11 +579,7 @@ class SeparateErasureCompilerVisitor
                        var entry = self.get_name("entry")
                        self.add("struct vts_entry {entry};")
                        self.require_declaration(mtype.mproperty.const_color)
-                       if self.compiler.as(SeparateErasureCompiler).vt_layout isa PHLayout[MClass, MVirtualTypeProp] then
-                               self.add("{entry} = {recv_ptr}vts_table->vts[HASH({recv_ptr}vts_table->mask, {mtype.mproperty.const_color})];")
-                       else
-                               self.add("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
-                       end
+                       self.add("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
                        self.add("{cltype} = {entry}.class->color;")
                        self.add("{idtype} = {entry}.class->id;")
                        if maybe_null and accept_null == "0" then
@@ -560,9 +603,6 @@ class SeparateErasureCompilerVisitor
                        self.add("{res} = {accept_null};")
                        self.add("\} else \{")
                end
-               if self.compiler.as(SeparateErasureCompiler).class_layout isa PHLayout[MClass, MClass] then
-                       self.add("{cltype} = HASH({class_ptr}color, {idtype});")
-               end
                self.add("if({cltype} >= {class_ptr}type_table->size) \{")
                self.add("{res} = 0;")
                self.add("\} else \{")
@@ -575,6 +615,40 @@ class SeparateErasureCompilerVisitor
                return res
        end
 
+       redef fun unbox_extern(value, mtype)
+       do
+               if mtype isa MClassType and mtype.mclass.kind == extern_kind and
+                  mtype.mclass.name != "NativeString" then
+                       var pointer_type = compiler.mainmodule.pointer_type
+                       var res = self.new_var_extern(mtype)
+                       self.add "{res} = ((struct instance_{pointer_type.c_name}*){value})->value; /* unboxing {value.mtype} */"
+                       return res
+               else
+                       return value
+               end
+       end
+
+       redef fun box_extern(value, mtype)
+       do
+               if mtype isa MClassType and mtype.mclass.kind == extern_kind and
+                  mtype.mclass.name != "NativeString" then
+                       var valtype = compiler.mainmodule.pointer_type
+                       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);")
+                               return res
+                       end
+                       self.require_declaration("BOX_{valtype.c_name}")
+                       self.add("{res} = BOX_{valtype.c_name}({value}); /* boxing {value.mtype} */")
+                       self.require_declaration("class_{mtype.c_name}")
+                       self.add("{res}->class = &class_{mtype.c_name};")
+                       return res
+               else
+                       return value
+               end
+       end
+
        redef fun class_name_string(value)
        do
                var res = self.get_name("var_class_name")
@@ -588,24 +662,14 @@ class SeparateErasureCompilerVisitor
                return res
        end
 
-       redef fun array_instance(array, elttype)
+       redef fun native_array_instance(elttype, length)
        do
                var nclass = self.get_class("NativeArray")
-               elttype = self.anchor(elttype)
-               var arraytype = self.get_class("Array").get_mtype([elttype])
-               var res = self.init_instance(arraytype)
-               self.add("\{ /* {res} = array_instance Array[{elttype}] */")
-               var nat = self.new_var(self.get_class("NativeArray").get_mtype([elttype]))
-               nat.is_exact = true
+               var mtype = nclass.get_mtype([elttype])
+               var res = self.new_var(mtype)
+               res.is_exact = true
                self.require_declaration("NEW_{nclass.c_name}")
-               self.add("{nat} = NEW_{nclass.c_name}({array.length});")
-               for i in [0..array.length[ do
-                       var r = self.autobox(array[i], self.object_type)
-                       self.add("((struct instance_{nclass.c_instance_name}*){nat})->values[{i}] = (val*) {r};")
-               end
-               var length = self.int_instance(array.length)
-               self.send(self.get_property("with_native", arraytype), [res, nat, length])
-               self.add("\}")
+               self.add("{res} = NEW_{nclass.c_name}({length});")
                return res
        end