compile: move management and generation of iroutines in MMLocalClass to Program
authorJean-Sebastien Gelinas <calestar@gmail.com>
Fri, 21 Aug 2009 19:29:53 +0000 (15:29 -0400)
committerJean Privat <jean@pryen.org>
Tue, 1 Sep 2009 18:15:22 +0000 (14:15 -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
src/program.nit

index 72e7e10..ad0cd09 100644 (file)
@@ -23,14 +23,6 @@ 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 4e43f9a..1d6ab55 100644 (file)
@@ -327,15 +327,6 @@ 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)
        do
@@ -355,81 +346,6 @@ 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
@@ -480,10 +396,10 @@ 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 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)
+                               init_var_iroutine.compile_to_c(v, cname, args)
                                ctx_old.append(v.ctx)
                                v.ctx = ctx_old
                                v.unindent
@@ -507,10 +423,10 @@ 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 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)
+                               checknew_iroutine.compile_to_c(v, cname, args)
                                ctx_old.append(v.ctx)
                                v.ctx = ctx_old
                                v.unindent
@@ -527,11 +443,11 @@ 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 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 = _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
index a3ef6cf..928397a 100644 (file)
@@ -128,7 +128,7 @@ special AbstractCompiler
                        var p = new Program(mod)
                        p.compute_main_method
                        p.do_table_computation(self)
-                       p.generate_classes_init_to_icode
+                       p.generate_allocation_iroutines
                        p.compile_prog_to_c(self)
                end
        end
index 686ff23..4bb6ea5 100644 (file)
@@ -18,6 +18,8 @@
 package program
 
 import metamodel
+import icode
+import primitive_info
 
 # Instances of this class represent a program/library that will
 # be analyzed/compiled by nitc
@@ -47,7 +49,91 @@ class Program
                _main_class = sys
        end
 
+       # Generation of allocation function of this class
+       fun generate_allocation_iroutines
+       do
+               for c in module.local_classes do
+                       var pi = c.primitive_info
+                       if pi == null then
+                               do
+                                       # Generate INIT_ATTRIBUTES routine
+                                       var iself = new IRegister(c.get_type)
+                                       var iselfa = [iself]
+                                       var iroutine = new IRoutine(iselfa, null)
+                                       var icb = new ICodeBuilder(module, iroutine)
+
+                                       for g in c.global_properties do
+                                               var p = c[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
+
+                                       c.init_var_iroutine = iroutine
+                               end
+                               do
+                                       # Compile CHECKNAME
+                                       var iself = new IRegister(c.get_type)
+                                       var iselfa = [iself]
+                                       var iroutine = new IRoutine(iselfa, null)
+                                       var icb = new ICodeBuilder(module, iroutine)
+                                       for g in c.global_properties do
+                                               var p = c[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
+
+                                       c.checknew_iroutine = iroutine
+                               end
+
+                               for g in c.global_properties do
+                                       var p = c[g]
+                                       # FIXME skip invisible constructors
+                                       if not p.global.is_init_for(c) then continue
+                                       assert p isa MMMethod
+
+                                       var iself = new IRegister(c.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(c.get_type)
+                                       inew.result = iself
+                                       icb.stmt(inew)
+                                       var iargs = [iself]
+                                       iargs.add_all(iparams)
+
+                                       icb.stmt(new IInitAttributes(c.get_type, iself))
+                                       icb.stmt(new IStaticCall(p, iargs))
+                                       icb.stmt(new ICheckInstance(c.get_type, iself))
+
+                                       c.new_instance_iroutine[p] = iroutine
+                               end
+                       end
+               end
+       end
+
        init(m: MMModule) do
                _module = m
        end
 end
+
+redef class MMLocalClass
+       # IRoutine for the initialization of the default attributes (called by IInitAttributes)
+       readable writable var _init_var_iroutine: nullable IRoutine = null
+       # IRoutine to validate the instance after initialization (called by ICheckInstance)
+       readable writable 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
+       readable var _new_instance_iroutine: HashMap[MMMethod, IRoutine] = new HashMap[MMMethod, IRoutine]
+end