compile: generate classes init iroutines sooner to insert global analysis
[nit.git] / src / compiling / compiling.nit
index 5af27e8..72e7e10 100644 (file)
 # Compute and generate tables for classes and modules.
 package compiling
 
+import table_computation
 import compiling_base
-private import compiling_methods
 private import compiling_global
-private import syntax
+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
 
-redef class MMSrcModule
        # 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
-       meth compile_prog_to_c(tc: ToolContext)
+       fun compile_prog_to_c(tc: ToolContext)
        do
-               for m in mhe.greaters_and_self do
-                       assert m isa MMSrcModule
-                       m.local_analysis(tc)
-               end
-
-               var ga = global_analysis(tc)
-
                tc.compdir.mkdir
 
                var files = new Array[String]
                var includes = new ArraySet[String]
                files.add("$CLIBDIR/nit_main.c")
-               for m in mhe.greaters_and_self do
-                       assert m isa MMSrcModule
+               files.add("$CLIBDIR/gc.c")
+               files.add("$CLIBDIR/gc_static_objects_list.c")
+               tc.info("Generating C code",1)
+               for m in module.mhe.greaters_and_self do
                        files.add("{tc.compdir}/{m.name}._sep.c")
-                       m.compile_separate_module(tc, ga)
-                       var native_name = m.filename.strip_extension(".nit")
+                       tc.info("Generating C code for module: {m.name}",2)
+                       m.compile_separate_module(tc, self)
+                       var native_name = m.location.file.strip_extension(".nit")
                        if (native_name + "_nit.h").file_exists then
                                includes.add("-I {native_name.dirname}")
                        end
@@ -52,57 +56,64 @@ redef class MMSrcModule
                        if native_name.file_exists then files.add(native_name)
                end
 
-               files.add("{tc.compdir}/{name}._tables.c")
-               compile_main(tc, ga)
+               tc.info("Generating main, tables and makefile ...",1)
+               files.add("{tc.compdir}/{module.name}._tables.c")
+               compile_main(tc)
 
-               var fn = "{tc.compdir}/{name}._build.sh"
+               var fn = "{tc.compdir}/{module.name}._build.sh"
                var f = new OFStream.open(fn)
+               var verbose = ""
+
+               if tc.verbose_level > 0 then
+                       verbose = "-"
+                       for i in [1..tc.verbose_level] do verbose = verbose + "v"
+               end
+
                f.write("#!/bin/sh\n")
-               f.write("# This shell script is generated by NIT to compile the program {name}.\n")
+               f.write("# This shell script is generated by NIT to compile the program {module.name}.\n")
                f.write("CLIBDIR=\"{tc.clibdir}\"\n")
-               f.write("{tc.bindir}/gccx -d {tc.compdir} -I $CLIBDIR {includes.join(" ")}")
+               f.write("{tc.bindir}/gccx {verbose} -d {tc.compdir} -I $CLIBDIR {includes.join(" ")}")
                if tc.output_file != null then 
                        f.write(" -o {tc.output_file}")
                else if tc.ext_prefix.is_empty then
-                       f.write(" -o {name}")
+                       f.write(" -o {module.name}")
                else
-                       f.write(" -o {name}_{tc.ext_prefix}")
+                       f.write(" -o {module.name}_{tc.ext_prefix}")
                end
                if tc.boost then f.write(" -O")
                f.write(" \"$@\" \\\n  {files.join("\\\n  ")}\n")
                f.close
 
                if not tc.no_cc then 
+                       tc.info("Building",1)
                        sys.system("sh {fn}")
                end
        end
 
        # Compile the main file
-       private meth compile_main(tc: ToolContext, ga: GlobalAnalysis)
+       private fun compile_main(tc: ToolContext)
        do
-               var v = new CompilerVisitor(self)
-               v.tc = tc
-               v.global_analysis = ga
+               var v = new GlobalCompilerVisitor(module, tc, self)
                v.add_decl("#include <nit_common.h>")
                compile_tables_to_c(v)
                compile_main_part(v)
-               var f = new OFStream.open("{tc.compdir}/{name}._tables.c")
-               f.write("/* This C file is generated by NIT to compile program {name}. */\n")
-               for m in mhe.greaters_and_self do
+               var f = new OFStream.open("{tc.compdir}/{module.name}._tables.c")
+               f.write("/* This C file is generated by NIT to compile program {module.name}. */\n")
+               for m in module.mhe.greaters_and_self do
                        f.write("#include \"{m.name}._sep.h\"\n")
                end
                f.write(v.to_s)
                f.close
        end
+end
 
+redef class MMModule
        # Compile the sep files (of the current module only)
-       private meth compile_separate_module(tc: ToolContext, ga: GlobalAnalysis)
+       private fun compile_separate_module(tc: ToolContext, program: Program)
        do
-               var v = new CompilerVisitor(self)
-               v.tc = tc
-               v.global_analysis = ga
+               var v = new GlobalCompilerVisitor(self, tc, program)
                v.add_decl("#include <nit_common.h>")
-               var native_name = filename.strip_extension(".nit")
+               var native_name = location.file.strip_extension(".nit")
                native_name += ("_nit.h")
                if native_name.file_exists then v.add_decl("#include <{native_name.basename("")}>")
                declare_class_tables_to_c(v)
@@ -112,14 +123,18 @@ redef class MMSrcModule
                f.write("#ifndef {name}_sep\n")
                f.write("#define {name}_sep\n")
                for m in mhe.direct_greaters do f.write("#include \"{m.name}._sep.h\"\n")
-               f.write(v.ctx.decls.join("\n"))
-               f.write("\n#endif\n")
+               for s in v.ctx.decls do
+                       f.write(s)
+               end
+               f.write("#endif\n")
                f.close
-               var f = new OFStream.open("{tc.compdir}/{name}._sep.c")
+
+               f = new OFStream.open("{tc.compdir}/{name}._sep.c")
                f.write("/* This C file is generated by NIT to compile module {name}. */\n")
                f.write("#include \"{name}._sep.h\"\n")
-               f.write(v.ctx.instrs.join("\n"))
-               f.write("\n")
+               for s in v.ctx.instrs do
+                       f.write(s)
+               end
                f.close
        end
 end