# 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
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}")
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)
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)
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")
import mmloader
private import utils
import primitive_info
+import program
redef class ToolContext
readable writable var _global: Bool = false
# 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
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)
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
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
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}]"
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
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
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
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
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
# 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]
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
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
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
end
end
- init(m: MMModule) do
+ init(m: MMModule, toolcontext: ToolContext) do
_module = m
+ _tc = toolcontext
end
end