Merge branch 'package2module' into wip
[nit.git] / src / compiling / compiling_global.nit
index 4e43f9a..da63476 100644 (file)
@@ -20,31 +20,22 @@ package compiling_global
 import table_computation
 private import compiling_icode
 
-class GlobalCompilerVisitor
-special CompilerVisitor
-       # The global analysis result
-       readable var _program: Program
-       init(m: MMModule, tc: ToolContext, prog: Program)
-       do
-               super(m, tc)
-               _program = prog
-       end
-end
-
 redef class Program
        # Compile module and class tables
-       fun compile_tables_to_c(v: GlobalCompilerVisitor)
+       fun compile_tables_to_c(v: CompilerVisitor)
        do
-               for m in module.mhe.greaters_and_self do
+               for m in main_module.mhe.greaters_and_self do
                        m.compile_local_table_to_c(v)
                end
 
-               for c in module.local_classes do
+               with_each_live_local_classes !action(c) do
+                       if c.global.is_abstract or c.global.is_interface then continue
                        c.compile_tables_to_c(v)
                end
+
                var s = new Buffer.from("classtable_t TAG2VFT[4] = \{NULL")
                for t in ["Int","Char","Bool"] do
-                       if module.has_global_class_named(t.to_symbol) then
+                       if main_module.has_global_class_named(t.to_symbol) then
                                s.append(", (const classtable_t)VFT_{t}")
                        else
                                s.append(", NULL")
@@ -55,7 +46,7 @@ redef class Program
        end
 
        # Compile main part (for _table.c)
-       fun compile_main_part(v: GlobalCompilerVisitor)
+       fun compile_main_part(v: CompilerVisitor)
        do
                v.add_instr("int main(int argc, char **argv) \{")
                v.indent
@@ -75,27 +66,27 @@ redef class Program
 end
 
 redef class MMModule
-       # Declare class table (for _sep.h)
-       fun declare_class_tables_to_c(v: GlobalCompilerVisitor)
+       # Declare class table (for _sep.h or _glob.h)
+       fun declare_class_tables_to_c(v: CompilerVisitor)
        do
                for c in local_classes do
-                       if c.global.module == self then
+                       if c.global.mmmodule == self then
                                c.declare_tables_to_c(v)
                        end
                end
        end
 
        # Compile sep files
-       fun compile_mod_to_c(v: GlobalCompilerVisitor)
+       fun compile_mod_to_c(v: CompilerVisitor)
        do
                v.add_decl("extern const char *LOCATE_{name};")
-               if not v.tc.global then
+               if not v.program.tc.use_SFT_optimization then
                        v.add_decl("extern const int SFT_{name}[];")
                end
                var i = 0
                for e in local_table do
                        var value: String
-                       if v.tc.global then
+                       if v.program.tc.use_SFT_optimization then
                                value = "{e.value(v.program)}"
                        else
                                value = "SFT_{name}[{i}]"
@@ -123,11 +114,11 @@ redef class MMModule
        end
 
        # Compile module file for the current module
-       fun compile_local_table_to_c(v: GlobalCompilerVisitor)
+       fun compile_local_table_to_c(v: CompilerVisitor)
        do
                v.add_instr("const char *LOCATE_{name} = \"{location.file}\";")
 
-               if v.tc.global or local_table.is_empty then
+               if v.program.tc.use_SFT_optimization or local_table.is_empty then
                        return
                end
 
@@ -145,12 +136,12 @@ end
 
 redef class AbsTableElt
        # Compile the macro needed to use the element and other related elements
-       fun compile_macros(v: GlobalCompilerVisitor, value: String) is abstract
+       fun compile_macros(v: CompilerVisitor, value: String) is abstract
 end
 
 redef class TableElt
        # Return the value of the element for a given class
-       fun compile_to_c(v: GlobalCompilerVisitor, c: MMLocalClass): String is abstract
+       fun compile_to_c(v: CompilerVisitor, c: MMLocalClass): String is abstract
 end
 
 redef class ModuleTableElt
@@ -327,26 +318,13 @@ end
 ###############################################################################
 
 redef class MMLocalClass
-       # IRoutine for the initialization of the default attributes (called by IInitAttributes)
-       var _init_var_iroutine: nullable IRoutine = null
-       # IRoutine to validate the instance after initialization (called by ICheckInstance)
-       var _checknew_iroutine: nullable IRoutine = null
-       # IRoutines to call to create a new valid instance (memory allocated, object initialized and validated)
-       # These iroutines will call: IAllocateInstance, IInitAttributes, some init function and ICheckInstance
-       # These routines will be called by INew
-       var _new_instance_iroutine: HashMap[MMMethod, IRoutine] = new HashMap[MMMethod, IRoutine]
-
        # Declaration and macros related to the class table
-       fun declare_tables_to_c(v: GlobalCompilerVisitor)
+       fun declare_tables_to_c(v: CompilerVisitor)
        do
                v.add_decl("")
                var pi = primitive_info
                v.add_decl("extern const classtable_elt_t VFT_{name}[];")
-               if name == "NativeArray".to_symbol then
-                       v.add_decl("val_t NEW_NativeArray(size_t length, size_t size);")
-               else if pi == null then
-                       # v.add_decl("val_t NEW_{name}(void);")
-               else if not pi.tagged then
+               if pi != null and not pi.tagged then
                        var t = pi.cname
                        var tbox = "struct TBOX_{name}"
                        v.add_decl("{tbox} \{ const classtable_elt_t * vft; bigint object_id; {t} val;};")
@@ -355,83 +333,8 @@ redef class MMLocalClass
                end
        end
 
-       # Generation of allocation function of this class
-       fun generate_allocation_iroutines(prog: Program)
-       do
-               var cc = prog.compiled_classes[self.global]
-
-               var pi = primitive_info
-               if pi == null then
-                       do
-                               # Generate INIT_ATTRIBUTES routine
-                               var iself = new IRegister(get_type)
-                               var iselfa = [iself]
-                               var iroutine = new IRoutine(iselfa, null)
-                               var icb = new ICodeBuilder(module, iroutine)
-
-                               for g in global_properties do
-                                       var p = self[g]
-                                       var t = p.signature.return_type
-                                       if p isa MMAttribute and t != null then
-                                               var ir = p.iroutine
-                                               if ir == null then continue
-                                               # FIXME: Not compatible with sep compilation
-                                               var e = icb.inline_routine(ir, iselfa, null).as(not null)
-                                               icb.stmt(new IAttrWrite(p, iself, e))
-                                       end
-                               end
-
-                               _init_var_iroutine = iroutine
-                       end
-                       do
-                               # Compile CHECKNAME
-                               var iself = new IRegister(get_type)
-                               var iselfa = [iself]
-                               var iroutine = new IRoutine(iselfa, null)
-                               var icb = new ICodeBuilder(module, iroutine)
-                               for g in global_properties do
-                                       var p = self[g]
-                                       var t = p.signature.return_type
-                                       if p isa MMAttribute and t != null and not t.is_nullable then
-                                               icb.add_attr_check(p, iself)
-                                       end
-                               end
-
-                               _checknew_iroutine = iroutine
-                       end
-
-                       var init_table_size = cshe.greaters.length + 1
-
-                       for g in global_properties do
-                               var p = self[g]
-                               # FIXME skip invisible constructors
-                               if not p.global.is_init_for(self) then continue
-                               assert p isa MMMethod
-
-                               var iself = new IRegister(get_type)
-                               var iparams = new Array[IRegister]
-                               for i in [0..p.signature.arity[ do iparams.add(new IRegister(p.signature[i]))
-                               var iroutine = new IRoutine(iparams, iself)
-                               iroutine.location = p.iroutine.location
-                               var icb = new ICodeBuilder(module, iroutine)
-
-                               var inew = new IAllocateInstance(get_type)
-                               inew.result = iself
-                               icb.stmt(inew)
-                               var iargs = [iself]
-                               iargs.add_all(iparams)
-
-                               icb.stmt(new IInitAttributes(get_type, iself))
-                               icb.stmt(new IStaticCall(p, iargs))
-                               icb.stmt(new ICheckInstance(get_type, iself))
-
-                               _new_instance_iroutine[p] = iroutine
-                       end
-               end
-       end
-
        # Compilation of table and new (or box)
-       fun compile_tables_to_c(v: GlobalCompilerVisitor)
+       fun compile_tables_to_c(v: CompilerVisitor)
        do
                var cc = v.program.compiled_classes[self.global]
                var ctab = cc.class_table
@@ -480,12 +383,11 @@ redef class MMLocalClass
                        do
                                # Generate INIT_ATTRIBUTES routine
                                var cname = "INIT_ATTRIBUTES__{name}"
-                               var args = _init_var_iroutine.compile_signature_to_c(v, cname, "init var of {name}", null, null)
-                               var ctx_old = v.ctx
-                               v.ctx = new CContext
-                               _init_var_iroutine.compile_to_c(v, cname, args)
-                               ctx_old.append(v.ctx)
-                               v.ctx = ctx_old
+                               var args = init_var_iroutine.compile_signature_to_c(v, cname, "init var of {name}", null, null)
+                               var decl_writer_old = v.decl_writer
+                               v.decl_writer = v.writer.sub
+                               init_var_iroutine.compile_to_c(v, cname, args)
+                               v.decl_writer = decl_writer_old
                                v.unindent
                                v.add_instr("}")
                        end
@@ -507,12 +409,11 @@ redef class MMLocalClass
                        do
                                # Compile CHECKNAME
                                var cname = "CHECKNEW_{name}"
-                               var args = _checknew_iroutine.compile_signature_to_c(v, cname, "check new {name}", null, null)
-                               var ctx_old = v.ctx
-                               v.ctx = new CContext
-                               _checknew_iroutine.compile_to_c(v, cname, args)
-                               ctx_old.append(v.ctx)
-                               v.ctx = ctx_old
+                               var args = checknew_iroutine.compile_signature_to_c(v, cname, "check new {name}", null, null)
+                               var decl_writer_old = v.decl_writer
+                               v.decl_writer = v.writer.sub
+                               checknew_iroutine.compile_to_c(v, cname, args)
+                               v.decl_writer = decl_writer_old
                                v.unindent
                                v.add_instr("}")
                        end
@@ -527,14 +428,13 @@ redef class MMLocalClass
                                assert p isa MMMethod
 
                                var cname = "NEW_{self}_{p.global.intro.cname}"
-                               var new_args = _new_instance_iroutine[p].compile_signature_to_c(v, cname, "new {self} {p.full_name}", null, null)
-                               var ctx_old = v.ctx
-                               v.ctx = new CContext
+                               var new_args = new_instance_iroutine[p].compile_signature_to_c(v, cname, "new {self} {p.full_name}", null, null)
+                               var decl_writer_old = v.decl_writer
+                               v.decl_writer = v.writer.sub
                                v.add_instr(init_table_decl)
-                               var e = _new_instance_iroutine[p].compile_to_c(v, cname, new_args).as(not null)
+                               var e = new_instance_iroutine[p].compile_to_c(v, cname, new_args).as(not null)
                                v.add_instr("return {e};")
-                               ctx_old.append(v.ctx)
-                               v.ctx = ctx_old
+                               v.decl_writer = decl_writer_old
                                v.unindent
                                v.add_instr("}")
                        end
@@ -564,10 +464,10 @@ redef class MMMethod
                var more_params: nullable String = null
                if global.is_init then more_params = "int* init_table"
                var args = ir.compile_signature_to_c(v, cname, full_name, null, more_params)
-               var ctx_old = v.ctx
-               v.ctx = new CContext
-
-               v.out_contexts.clear
+               var writer_old = v.writer
+               v.writer = v.writer.sub
+               var decl_writer_old = v.decl_writer
+               v.decl_writer = v.writer.sub
 
                var itpos: nullable String = null
                if global.is_init then
@@ -584,15 +484,13 @@ redef class MMMethod
                if s == null then
                        v.add_instr("return;")
                else
-                       v.add_instr("return ", s, ";")
+                       v.add_instr("return {s};")
                end
-
-               ctx_old.append(v.ctx)
-               v.ctx = ctx_old
                v.unindent
                v.add_instr("}")
 
-               for ctx in v.out_contexts do v.ctx.merge(ctx)
+               v.writer = writer_old
+               v.decl_writer = decl_writer_old
        end
 end