compile: generate classes init iroutines sooner to insert global analysis
authorJean-Sebastien Gelinas <calestar@gmail.com>
Mon, 10 Aug 2009 16:25:11 +0000 (12:25 -0400)
committerJean Privat <jean@pryen.org>
Wed, 19 Aug 2009 17:13:40 +0000 (13:13 -0400)
Signed-off-by: Jean-Sebastien Gelinas <calestar@gmail.com>
Signed-off-by: Jean Privat <jean@pryen.org>

src/compiling/compiling.nit
src/compiling/compiling_global.nit
src/nitc.nit

index ad0cd09..72e7e10 100644 (file)
@@ -23,6 +23,14 @@ private import compiling_global
 private import compiling_icode
 
 redef class Program
+       # Generate icode for allocation/construction/validation of classes
+       fun generate_classes_init_to_icode
+       do
+               for c in module.local_classes do
+                       c.generate_allocation_iroutines(self)
+               end
+       end
+
        # Compile the program
        # Generate all sep files (_sep.[ch]), the main file (_table.c) and the build file (_build.sh)
        # Then execute the build.sh
index e7f49fc..66e670b 100644 (file)
@@ -363,6 +363,81 @@ 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)
        do
@@ -412,30 +487,11 @@ redef class MMLocalClass
                else 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
-
                                var cname = "INIT_ATTRIBUTES__{name}"
-                               var args = iroutine.compile_signature_to_c(v, cname, "init attributes of {name}", null, null)
+                               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
-                               iroutine.compile_to_c(v, cname, args)
+                               _init_var_iroutine.compile_to_c(v, cname, args)
                                ctx_old.append(v.ctx)
                                v.ctx = ctx_old
                                v.unindent
@@ -458,25 +514,11 @@ redef class MMLocalClass
                        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
-
                                var cname = "CHECKNEW_{name}"
-                               var args = iroutine.compile_signature_to_c(v, cname, "check new {name}", null, null)
+                               var args = _checknew_iroutine.compile_signature_to_c(v, cname, "check new {name}", null, null)
                                var ctx_old = v.ctx
                                v.ctx = new CContext
-                               iroutine.compile_to_c(v, cname, args)
+                               _checknew_iroutine.compile_to_c(v, cname, args)
                                ctx_old.append(v.ctx)
                                v.ctx = ctx_old
                                v.unindent
@@ -492,31 +534,12 @@ redef class MMLocalClass
                                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
-
                                var cname = "NEW_{self}_{p.global.intro.cname}"
-                               var new_args = iroutine.compile_signature_to_c(v, cname, "new {self} {p.full_name}", null, null)
+                               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
                                v.add_instr(init_table_decl)
-                               var e = iroutine.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
index f52cc14..c0d3133 100644 (file)
@@ -127,6 +127,7 @@ special AbstractCompiler
                for mod in mods do
                        var p = new Program(mod)
                        p.do_table_computation(self)
+                       p.generate_classes_init_to_icode
                        p.compile_prog_to_c(self)
                end
        end