ni_nitdoc: added fast copy past utility to signatures.
[nit.git] / src / abstract_compiler.nit
index 36d7ed1..20ebe59 100644 (file)
@@ -102,12 +102,13 @@ redef class ModelBuilder
                # A single C file regroups many compiled rumtime functions
                # Note that we do not try to be clever an a small change in a Nit source file may change the content of all the generated .c files
                var time0 = get_time
+               self.toolcontext.info("*** WRITING C ***", 1)
 
                ".nit_compile".mkdir
 
                var outname = self.toolcontext.opt_output.value
                if outname == null then
-                       outname = "{mainmodule.name}.bin"
+                       outname = "{mainmodule.name}"
                end
 
                var hfilename = compiler.header.file.name + ".h"
@@ -181,16 +182,21 @@ redef class ModelBuilder
                        #p = "..".join_path(p)
                        cc_includes += " -I \"" + p + "\""
                end
-               makefile.write("CC = ccache cc\nCFLAGS = -g -O2{cc_includes}\nLDFLAGS ?= \nLDLIBS  ?= -lm -lgc\n\n")
+               makefile.write("CC = ccache cc\nCFLAGS = -g -O2\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS  ?= -lm -lgc\n\n")
                makefile.write("all: {outname}\n\n")
 
                var ofiles = new Array[String]
                # Compile each generated file
                for f in cfiles do
                        var o = f.strip_extension(".c") + ".o"
-                       makefile.write("{o}: {f}\n\t$(CC) $(CFLAGS) -D NONITCNI -c -o {o} {f}\n\n")
+                       makefile.write("{o}: {f}\n\t$(CC) $(CFLAGS) $(CINCL) -D NONITCNI -c -o {o} {f}\n\n")
                        ofiles.add(o)
                end
+
+               # Add gc_choser.h to aditionnal bodies
+               var gc_chooser = new ExternCFile("{cc_paths.first}/gc_chooser.c", "-DWITH_LIBGC")
+               compiler.extern_bodies.add(gc_chooser)
+
                # Compile each required extern body into a specific .o
                for f in compiler.extern_bodies do
                        var basename = f.filename.basename(".c")
@@ -207,7 +213,7 @@ redef class ModelBuilder
                self.toolcontext.info("Generated makefile: {makename}", 2)
 
                var time1 = get_time
-               self.toolcontext.info("*** END COMPILING TO C: {time1-time0} ***", 2)
+               self.toolcontext.info("*** END WRITING C: {time1-time0} ***", 2)
 
                # Execute the Makefile
 
@@ -238,8 +244,12 @@ end
 abstract class AbstractCompiler
        type VISITOR: AbstractCompilerVisitor
 
-       # The main module of the program
-       var mainmodule: MModule protected writable
+       # The main module of the program currently compiled
+       # Is assigned during the separate compilation
+       var mainmodule: MModule writable
+
+       # The real main module of the program
+       var realmainmodule: MModule
 
        # The modeulbuilder used to know the model and the AST
        var modelbuilder: ModelBuilder protected writable
@@ -250,6 +260,7 @@ abstract class AbstractCompiler
        init(mainmodule: MModule, modelbuilder: ModelBuilder)
        do
                self.mainmodule = mainmodule
+               self.realmainmodule = mainmodule
                self.modelbuilder = modelbuilder
        end
 
@@ -290,16 +301,7 @@ abstract class AbstractCompiler
                self.header.add_decl("#include <stdlib.h>")
                self.header.add_decl("#include <stdio.h>")
                self.header.add_decl("#include <string.h>")
-               self.header.add_decl("#ifndef NOBOEHM")
-               self.header.add_decl("#include <gc/gc.h>")
-               self.header.add_decl("#ifdef NOBOEHM_ATOMIC")
-               self.header.add_decl("#undef GC_MALLOC_ATOMIC")
-               self.header.add_decl("#define GC_MALLOC_ATOMIC(x) GC_MALLOC(x)")
-               self.header.add_decl("#endif /*NOBOEHM_ATOMIC*/")
-               self.header.add_decl("#else /*NOBOEHM*/")
-               self.header.add_decl("#define GC_MALLOC(x) calloc(1, (x))")
-               self.header.add_decl("#define GC_MALLOC_ATOMIC(x) calloc(1, (x))")
-               self.header.add_decl("#endif /*NOBOEHM*/")
+               self.header.add_decl("#include <gc_chooser.h>")
 
                compile_header_structs
 
@@ -336,6 +338,7 @@ abstract class AbstractCompiler
                end
                v.add_decl("int main(int argc, char** argv) \{")
                v.add("glob_argc = argc; glob_argv = argv;")
+               v.add("initialize_gc_option();")
                var main_type = mainmodule.sys_type
                if main_type != null then
                        var mainmodule = v.compiler.mainmodule
@@ -1609,7 +1612,7 @@ redef class AInternMethPropdef
                        v.ret(v.new_expr("glob_sys", ret.as(not null)))
                        return
                else if pname == "calloc_string" then
-                       v.ret(v.new_expr("(char*)GC_MALLOC_ATOMIC({arguments[1]})", ret.as(not null)))
+                       v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
                        return
                else if pname == "calloc_array" then
                        v.calloc_array(ret.as(not null), arguments)
@@ -1629,7 +1632,13 @@ redef class AInternMethPropdef
                        v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
                        return
                else if pname == "force_garbage_collection" then
-                       v.add("GC_gcollect();")
+                       v.add("nit_gcollect();")
+                       return
+               else if pname == "native_argc" then
+                       v.ret(v.new_expr("glob_argc", ret.as(not null)))
+                       return
+               else if pname == "native_argv" then
+                       v.ret(v.new_expr("glob_argv[{arguments[1]}]", ret.as(not null)))
                        return
                end
                v.add("printf(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at {location.to_s}\\n\");")
@@ -2365,19 +2374,7 @@ end
 
 # Utils
 
-redef class HashSet[E]
-       init from(elements: Collection[E]) do
-               init
-               self.add_all(elements)
-       end
-end
-
 redef class Array[E]
-       init from(elements: Collection[E]) do
-               init
-               self.add_all(elements)
-       end
-
        # Return a new Array with the elements only contened in 'self' and not in 'o'
        fun -(o: Array[E]): Array[E] do
                var res = new Array[E]