package compiling
import compiling_base
-private import compiling_methods
private import compiling_global
-private import syntax
+private import compiling_icode
-redef class MMSrcModule
+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)
do
- for m in mhe.greaters_and_self do
- assert m isa MMSrcModule
+ 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)
end
+ tc.info("Merging all tables",2)
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")
+ tc.info("Generating C code for module: {m.name}",2)
m.compile_separate_module(tc, ga)
- var native_name = m.filename.strip_extension(".nit")
+ var native_name = m.location.file.strip_extension(".nit")
if (native_name + "_nit.h").file_exists then
includes.add("-I {native_name.dirname}")
end
if native_name.file_exists then files.add(native_name)
end
- files.add("{tc.compdir}/{name}._tables.c")
+ tc.info("Generating main, tables and makefile ...",1)
+ files.add("{tc.compdir}/{module.name}._tables.c")
compile_main(tc, ga)
- 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 fun compile_main(tc: ToolContext, ga: GlobalAnalysis)
do
- var v = new GlobalCompilerVisitor(self, tc, ga)
+ var v = new GlobalCompilerVisitor(module, tc, ga)
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 fun compile_separate_module(tc: ToolContext, ga: GlobalAnalysis)
do
var v = new GlobalCompilerVisitor(self, tc, ga)
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)
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