parser: `Visitor::visit` does not accepts `null`
[nit.git] / src / separate_compiler.nit
index b13f14d..dcd7cc7 100644 (file)
@@ -59,16 +59,18 @@ redef class ModelBuilder
        fun run_separate_compiler(mainmodule: MModule, runtime_type_analysis: RapidTypeAnalysis)
        do
                var time0 = get_time
-               self.toolcontext.info("*** COMPILING TO C ***", 1)
+               self.toolcontext.info("*** GENERATING C ***", 1)
 
                var compiler = new SeparateCompiler(mainmodule, self, runtime_type_analysis)
                compiler.compile_header
 
                # compile class structures
+               self.toolcontext.info("Property coloring", 2)
                compiler.new_file("{mainmodule.name}.classes")
                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
@@ -79,11 +81,13 @@ redef class ModelBuilder
 
                # compile methods
                for m in mainmodule.in_importation.greaters do
+                       self.toolcontext.info("Generate C for module {m}", 2)
                        compiler.new_file("{m.name}.sep")
                        compiler.compile_module_to_c(m)
                end
 
                # compile live & cast type structures
+               self.toolcontext.info("Type coloring", 2)
                compiler.new_file("{mainmodule.name}.types")
                var mtypes = compiler.do_type_coloring
                for t in mtypes do
@@ -92,6 +96,8 @@ redef class ModelBuilder
 
                compiler.display_stats
 
+               var time1 = get_time
+               self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
                write_and_make(compiler)
        end
 end
@@ -217,13 +223,23 @@ class SeparateCompiler
                # Layouts
                var method_layout_builder: PropertyLayoutBuilder[MMethod]
                var attribute_layout_builder: PropertyLayoutBuilder[MAttribute]
-               if modelbuilder.toolcontext.opt_bm_typing.value then
-                       method_layout_builder = new MMethodBMizer(self.mainmodule)
-                       attribute_layout_builder = new MAttributeBMizer(self.mainmodule)
-               else
-                       method_layout_builder = new MMethodColorer(self.mainmodule)
-                       attribute_layout_builder = new MAttributeColorer(self.mainmodule)
-               end
+               #FIXME PH and BM layouts too slow for large programs
+               #if modelbuilder.toolcontext.opt_bm_typing.value then
+               #       method_layout_builder = new MMethodBMizer(self.mainmodule)
+               #       attribute_layout_builder = new MAttributeBMizer(self.mainmodule)
+               #else if modelbuilder.toolcontext.opt_phmod_typing.value then
+               #       method_layout_builder = new MMethodHasher(new PHModOperator, self.mainmodule)
+               #       attribute_layout_builder = new MAttributeHasher(new PHModOperator, self.mainmodule)
+               #else if modelbuilder.toolcontext.opt_phand_typing.value then
+               #       method_layout_builder = new MMethodHasher(new PHAndOperator, self.mainmodule)
+               #       attribute_layout_builder = new MAttributeHasher(new PHAndOperator, self.mainmodule)
+               #else
+
+               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)
+               #end
 
                # methods coloration
                var method_layout = method_layout_builder.build_layout(mclasses)
@@ -243,9 +259,13 @@ class SeparateCompiler
                for mclass in mclasses do
                        var table = new Array[nullable MPropDef]
                        # first, fill table from parents by reverse linearization order
-                       var parents = self.mainmodule.super_mclasses(mclass)
-                       var lin = self.mainmodule.reverse_linearize_mclasses(parents)
-                       for parent in lin do
+                       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 MMethod then continue
                                        var color = layout.pos[mproperty]
@@ -287,9 +307,13 @@ class SeparateCompiler
                for mclass in mclasses do
                        var table = new Array[nullable MPropDef]
                        # first, fill table from parents by reverse linearization order
-                       var parents = self.mainmodule.super_mclasses(mclass)
-                       var lin = self.mainmodule.reverse_linearize_mclasses(parents)
-                       for parent in lin do
+                       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 MAttribute then continue
                                        var color = layout.pos[mproperty]
@@ -327,7 +351,7 @@ class SeparateCompiler
        end
 
        # colorize live types of the program
-       private fun do_type_coloring: Set[MType] do
+       private fun do_type_coloring: POSet[MType] do
                var mtypes = new HashSet[MType]
                mtypes.add_all(self.runtime_type_analysis.live_types)
                mtypes.add_all(self.runtime_type_analysis.live_cast_types)
@@ -337,7 +361,7 @@ class SeparateCompiler
                end
 
                for mtype in mtypes do
-                       retieve_live_partial_types(mtype)
+                       retrieve_partial_types(mtype)
                end
                mtypes.add_all(self.partial_types)
 
@@ -355,24 +379,22 @@ class SeparateCompiler
 
                # colorize types
                self.type_layout = layout_builder.build_layout(mtypes)
-               self.type_tables = self.build_type_tables(mtypes)
+               var poset = layout_builder.poset.as(not null)
+               self.type_tables = self.build_type_tables(poset)
 
                # VT and FT are stored with other unresolved types in the big resolution_tables
                self.compile_resolution_tables(mtypes)
 
-               return mtypes
+               return poset
        end
 
        # Build type tables
-       fun build_type_tables(mtypes: Set[MType]): Map[MType, Array[nullable MType]] do
+       fun build_type_tables(mtypes: POSet[MType]): Map[MType, Array[nullable MType]] do
                var tables = new HashMap[MType, Array[nullable MType]]
                var layout = self.type_layout
                for mtype in mtypes do
                        var table = new Array[nullable MType]
-                       var supers = new HashSet[MType]
-                       supers.add_all(self.mainmodule.super_mtypes(mtype, mtypes))
-                       supers.add(mtype)
-                       for sup in supers do
+                       for sup in mtypes[mtype].greaters do
                                var color: Int
                                if layout isa PHLayout[MType, MType] then
                                        color = layout.hashes[mtype][sup]
@@ -471,7 +493,7 @@ class SeparateCompiler
                return tables
        end
 
-       fun retieve_live_partial_types(mtype: MType) do
+       fun retrieve_partial_types(mtype: MType) do
                # add formal types arguments to mtypes
                if mtype isa MGenericType then
                        for ft in mtype.arguments do
@@ -480,7 +502,7 @@ class SeparateCompiler
                                        abort
                                end
                                self.partial_types.add(ft)
-                               retieve_live_partial_types(ft)
+                               retrieve_partial_types(ft)
                        end
                end
                var mclass_type: MClassType
@@ -508,12 +530,10 @@ class SeparateCompiler
                        for pd in cd.mpropdefs do
                                if not pd isa MMethodDef then continue
                                #print "compile {pd} @ {cd} @ {mmodule}"
-                               var r = new SeparateRuntimeFunction(pd)
+                               var r = pd.separate_runtime_function
                                r.compile_to_c(self)
-                               if true or cd.bound_mtype.ctype != "val*" then
-                                       var r2 = new VirtualRuntimeFunction(pd)
-                                       r2.compile_to_c(self)
-                               end
+                               var r2 = pd.virtual_runtime_function
+                               r2.compile_to_c(self)
                        end
                end
                self.mainmodule = old_module
@@ -621,29 +641,30 @@ class SeparateCompiler
                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
-                               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} */")
+               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
-                                       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
+                                       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
@@ -659,7 +680,7 @@ class SeparateCompiler
                        self.header.add_decl("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 = GC_MALLOC(sizeof(struct instance_{c_name}));")
+                       v.add("struct instance_{c_name}*res = nit_alloc(sizeof(struct instance_{c_name}));")
                        v.require_declaration("type_{c_name}")
                        v.add("res->type = &type_{c_name};")
                        v.require_declaration("class_{c_name}")
@@ -684,7 +705,7 @@ class SeparateCompiler
                        var res = v.new_named_var(mtype, "self")
                        res.is_exact = true
                        var mtype_elt = mtype.arguments.first
-                       v.add("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
+                       v.add("{res} = nit_alloc(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
                        v.add("{res}->type = type;")
                        hardening_live_type(v, "type")
                        v.require_declaration("class_{c_name}")
@@ -698,15 +719,19 @@ class SeparateCompiler
                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} = GC_MALLOC(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)
@@ -735,7 +760,11 @@ class SeparateCompiler
                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
 
@@ -864,7 +893,13 @@ class SeparateCompilerVisitor
        redef fun send(mmethod, arguments)
        do
                if arguments.first.mcasttype.ctype != "val*" then
-                       return self.monomorphic_send(mmethod, arguments.first.mcasttype, arguments)
+                       # 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 m = self.compiler.mainmodule
+                       self.compiler.mainmodule = self.compiler.realmainmodule
+                       var res = self.monomorphic_send(mmethod, arguments.first.mcasttype, arguments)
+                       self.compiler.mainmodule = m
+                       return res
                end
 
                var res: nullable RuntimeVariable
@@ -1475,6 +1510,30 @@ class SeparateCompilerVisitor
        end
 end
 
+redef class MMethodDef
+       fun separate_runtime_function: AbstractRuntimeFunction
+       do
+               var res = self.separate_runtime_function_cache
+               if res == null then
+                       res = new SeparateRuntimeFunction(self)
+                       self.separate_runtime_function_cache = res
+               end
+               return res
+       end
+       private var separate_runtime_function_cache: nullable SeparateRuntimeFunction
+
+       fun virtual_runtime_function: AbstractRuntimeFunction
+       do
+               var res = self.virtual_runtime_function_cache
+               if res == null then
+                       res = new VirtualRuntimeFunction(self)
+                       self.virtual_runtime_function_cache = res
+               end
+               return res
+       end
+       private var virtual_runtime_function_cache: nullable VirtualRuntimeFunction
+end
+
 # The C function associated to a methoddef separately compiled
 class SeparateRuntimeFunction
        super AbstractRuntimeFunction
@@ -1611,10 +1670,11 @@ class VirtualRuntimeFunction
                end
                frame.returnlabel = v.get_name("RET_LABEL")
 
-               if recv != arguments.first.mtype then
-                       #print "{self} {recv} {arguments.first}"
+               var subret = v.call(mmethoddef, recv, arguments)
+               if ret != null then
+                       assert subret != null
+                       v.assign(frame.returnvar.as(not null), subret)
                end
-               mmethoddef.compile_inside_to_c(v, arguments)
 
                v.add("{frame.returnlabel.as(not null)}:;")
                if ret != null then