compile: add ToolContext to Program
authorJean-Sebastien Gelinas <calestar@gmail.com>
Thu, 27 Aug 2009 18:33:12 +0000 (14:33 -0400)
committerJean Privat <jean@pryen.org>
Tue, 1 Sep 2009 18:15:23 +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_base.nit
src/compiling/compiling_global.nit
src/compiling/table_computation.nit
src/nitc.nit
src/program.nit

index ad0cd09..445fc50 100644 (file)
@@ -26,7 +26,7 @@ redef class Program
        # 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
-       fun compile_prog_to_c(tc: ToolContext)
+       fun compile_prog_to_c
        do
                tc.compdir.mkdir
 
@@ -39,7 +39,7 @@ redef class Program
                for m in module.mhe.greaters_and_self do
                        files.add("{tc.compdir}/{m.name}._sep.c")
                        tc.info("Generating C code for module: {m.name}",2)
-                       m.compile_separate_module(tc, self)
+                       m.compile_separate_module(self)
                        var native_name = m.location.file.strip_extension(".nit")
                        if (native_name + "_nit.h").file_exists then
                                includes.add("-I {native_name.dirname}")
@@ -50,7 +50,7 @@ redef class Program
 
                tc.info("Generating main, tables and makefile ...",1)
                files.add("{tc.compdir}/{module.name}._tables.c")
-               compile_main(tc)
+               compile_main
 
                var fn = "{tc.compdir}/{module.name}._build.sh"
                var f = new OFStream.open(fn)
@@ -83,9 +83,9 @@ redef class Program
        end
 
        # Compile the main file
-       private fun compile_main(tc: ToolContext)
+       private fun compile_main
        do
-               var v = new GlobalCompilerVisitor(module, tc, self)
+               var v = new CompilerVisitor(module, self)
                v.add_decl("#include <nit_common.h>")
                compile_tables_to_c(v)
                compile_main_part(v)
@@ -101,9 +101,10 @@ end
 
 redef class MMModule
        # Compile the sep files (of the current module only)
-       private fun compile_separate_module(tc: ToolContext, program: Program)
+       private fun compile_separate_module(program: Program)
        do
-               var v = new GlobalCompilerVisitor(self, tc, program)
+               var tc = program.tc
+               var v = new CompilerVisitor(self, program)
                v.add_decl("#include <nit_common.h>")
                var native_name = location.file.strip_extension(".nit")
                native_name += ("_nit.h")
index ec63914..f873be4 100644 (file)
@@ -20,6 +20,7 @@ package compiling_base
 import mmloader
 private import utils
 import primitive_info
+import program
 
 redef class ToolContext
        readable writable var _global: Bool = false
@@ -113,14 +114,14 @@ class CompilerVisitor
        # The current indent lever
        readable writable var _indent_level: Int = 0
 
-       # The ToolContext info
-       readable var _tc: ToolContext
+       # The program we are compiling
+       readable var _program: Program
 
        # Create a new CompilerVisitor based on a module
-       init(module: MMModule, tc: ToolContext)
+       init(module: MMModule, p: Program)
        do
                _module = module
-               _tc = tc
+               _program = p
        end
 end
 
index 1d6ab55..afe203f 100644 (file)
@@ -20,20 +20,9 @@ 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
                        m.compile_local_table_to_c(v)
@@ -55,7 +44,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
@@ -76,7 +65,7 @@ end
 
 redef class MMModule
        # Declare class table (for _sep.h)
-       fun declare_class_tables_to_c(v: GlobalCompilerVisitor)
+       fun declare_class_tables_to_c(v: CompilerVisitor)
        do
                for c in local_classes do
                        if c.global.module == self then
@@ -86,16 +75,16 @@ redef class MMModule
        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.global 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.global then
                                value = "{e.value(v.program)}"
                        else
                                value = "SFT_{name}[{i}]"
@@ -123,11 +112,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.global or local_table.is_empty then
                        return
                end
 
@@ -145,12 +134,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
@@ -328,7 +317,7 @@ end
 
 redef class MMLocalClass
        # 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
@@ -347,7 +336,7 @@ redef class MMLocalClass
        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
index 24e5fb2..1da4f6d 100644 (file)
@@ -93,7 +93,7 @@ redef class MMConcreteClass
        readable var _instance_layout: Array[TableElt] = new Array[TableElt]
 
        # Build the local layout of the class and feed the module table
-       private fun build_layout_in(tc: ToolContext, module_table: Array[ModuleTableElt])
+       private fun build_layout_in(module_table: Array[ModuleTableElt])
        do
                var clt = _class_layout
                var ilt = _instance_layout
@@ -140,20 +140,20 @@ redef class Program
        # Associate global classes to compiled classes
        readable var _compiled_classes: HashMap[MMGlobalClass, CompiledClass] = new HashMap[MMGlobalClass, CompiledClass]
 
-       fun do_table_computation(tc: ToolContext)
+       fun do_table_computation
        do
                tc.info("Building tables",1)
                for m in module.mhe.greaters_and_self do
                        tc.info("Building tables for module: {m.name}",2)
-                       m.local_analysis(tc)
+                       m.local_analysis
                end
 
                tc.info("Merging all tables",2)
-               do_global_table_analysis(tc)
+               do_global_table_analysis
        end
 
        # Do the complete global analysis
-       private fun do_global_table_analysis(cctx: ToolContext)
+       private fun do_global_table_analysis
        do
                #print "Do the complete global analysis"
                var smallest_classes = new Array[MMLocalClass]
@@ -402,11 +402,11 @@ redef class MMModule
        readable var _local_table: Array[ModuleTableElt] = new Array[ModuleTableElt]
 
        # Builds the local tables and local classes layouts
-       private fun local_analysis(tc: ToolContext)
+       private fun local_analysis
        do
                for c in local_classes do
                        if c isa MMConcreteClass then
-                               c.build_layout_in(tc, _local_table)
+                               c.build_layout_in(_local_table)
                        end
                end
        end
index 928397a..cb9b5b1 100644 (file)
@@ -125,11 +125,11 @@ special AbstractCompiler
                        dump_intermediate_code(mods)
                end
                for mod in mods do
-                       var p = new Program(mod)
+                       var p = new Program(mod, self)
                        p.compute_main_method
-                       p.do_table_computation(self)
+                       p.do_table_computation
                        p.generate_allocation_iroutines
-                       p.compile_prog_to_c(self)
+                       p.compile_prog_to_c
                end
        end
 end
index 4bb6ea5..05214aa 100644 (file)
@@ -20,10 +20,15 @@ package program
 import metamodel
 import icode
 import primitive_info
+import mmloader
 
 # Instances of this class represent a program/library that will
 # be analyzed/compiled by nitc
 class Program
+       # This is the ToolContext associated with this Program
+       # It contains (amongts other things) the command line options
+       readable var _tc: ToolContext
+
        # This module is the 'main' module, the one where we find the 'main' method
        readable var _module: MMModule
 
@@ -122,8 +127,9 @@ class Program
                end
        end
 
-       init(m: MMModule) do
+       init(m: MMModule, toolcontext: ToolContext) do
                _module = m
+               _tc = toolcontext
        end
 end