private import compiling_global
private import compiling_icode
-redef class MMModule
+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
tc.info("Building tables",1)
- for m in mhe.greaters_and_self do
+ for m in module.mhe.greaters_and_self do
tc.info("Building tables for module: {m.name}",2)
m.local_analysis(tc)
end
files.add("$CLIBDIR/gc.c")
files.add("$CLIBDIR/gc_static_objects_list.c")
tc.info("Generating C code",1)
- for m in mhe.greaters_and_self do
+ 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)
end
tc.info("Generating main, tables and makefile ...",1)
- files.add("{tc.compdir}/{name}._tables.c")
+ 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 = ""
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 {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")
# 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
# Compute and generate tables for classes and modules.
package compiling_global
+import program
private import compiling_icode
# Something that store color of table elements
end
end
-redef class MMModule
- # The local table of the module (refers things introduced in the module)
- var _local_table: Array[ModuleTableElt] = new Array[ModuleTableElt]
-
- # Builds the local tables and local classes layouts
- fun local_analysis(tc: ToolContext)
- do
- for c in local_classes do
- if c isa MMConcreteClass then
- c.build_layout_in(tc, _local_table)
- end
- end
- end
-
+redef class Program
# Do the complete global analysis
fun global_analysis(cctx: ToolContext): GlobalAnalysis
do
- #print "Do the complete global analysis"
- var ga = new GlobalAnalysis(self)
+ var ga = new GlobalAnalysis(module)
var smallest_classes = new Array[MMLocalClass]
var global_properties = new HashSet[MMGlobalProperty]
var ctab = new Array[TableElt]
# We have to work on ALL the classes of the module
var classes = new Array[MMLocalClass]
- for c in local_classes do
+ for c in module.local_classes do
c.compute_super_classes
classes.add(c)
end
return ga
end
- private fun append_to_table(cc: ColorContext, table: Array[nullable TableElt], cmp: TableEltComposite)
- do
- for j in [0..cmp.length[ do
- var e = cmp.item(j)
- cc.color(e) = table.length
- table.add(e)
- end
- end
-
- private fun build_tables_in(table: Array[nullable TableElt], ga: GlobalAnalysis, c: MMLocalClass, elts: Array[TableElt])
- do
- var tab = new HashMap[Int, TableElt]
- var len = 0
- for e in elts do
- if e.is_related_to(c) then
- var col = ga.color(e)
- var l = col + e.length
- tab[col] = e
- if len < l then
- len = l
- end
- end
- end
- var i = 0
- while i < len do
- if tab.has_key(i) then
- var e = tab[i]
- for j in [0..e.length[ do
- table[i] = e.item(j)
- i = i + 1
- end
- else
- table[i] = null
- i = i + 1
- end
- end
- end
-
# Perform coloring
fun colorize(ga: GlobalAnalysis, elts: Array[TableElt], classes: Collection[MMLocalClass], startcolor: Int)
do
return true
end
+ private fun append_to_table(cc: ColorContext, table: Array[nullable TableElt], cmp: TableEltComposite)
+ do
+ for j in [0..cmp.length[ do
+ var e = cmp.item(j)
+ cc.color(e) = table.length
+ table.add(e)
+ end
+ end
+
+ private fun build_tables_in(table: Array[nullable TableElt], ga: GlobalAnalysis, c: MMLocalClass, elts: Array[TableElt])
+ do
+ var tab = new HashMap[Int, TableElt]
+ var len = 0
+ for e in elts do
+ if e.is_related_to(c) then
+ var col = ga.color(e)
+ var l = col + e.length
+ tab[col] = e
+ if len < l then
+ len = l
+ end
+ end
+ end
+ var i = 0
+ while i < len do
+ if tab.has_key(i) then
+ var e = tab[i]
+ for j in [0..e.length[ do
+ table[i] = e.item(j)
+ i = i + 1
+ end
+ else
+ table[i] = null
+ i = i + 1
+ end
+ end
+ end
+
# Compile module and class tables
fun compile_tables_to_c(v: GlobalCompilerVisitor)
do
- for m in mhe.greaters_and_self do
+ for m in module.mhe.greaters_and_self do
m.compile_local_table_to_c(v)
end
- for c in local_classes do
+ for c in module.local_classes do
c.compile_tables_to_c(v)
end
var s = new Buffer.from("classtable_t TAG2VFT[4] = \{NULL")
for t in ["Int","Char","Bool"] do
- if has_global_class_named(t.to_symbol) then
+ if module.has_global_class_named(t.to_symbol) then
s.append(", (const classtable_t)VFT_{t}")
else
s.append(", NULL")
v.add_instr(s.to_s)
end
- # Declare class table (for _sep.h)
- fun declare_class_tables_to_c(v: GlobalCompilerVisitor)
- do
- for c in local_classes do
- if c.global.module == self then
- c.declare_tables_to_c(v)
- end
- end
- end
-
# Compile main part (for _table.c)
fun compile_main_part(v: GlobalCompilerVisitor)
do
v.add_instr("prepare_signals();")
v.add_instr("glob_argc = argc; glob_argv = argv;")
var sysname = once "Sys".to_symbol
- if not has_global_class_named(sysname) then
+ if not module.has_global_class_named(sysname) then
print("No main")
else
- var sys = class_by_name(sysname)
+ var sys = module.class_by_name(sysname)
var name = once "main".to_symbol
if not sys.has_global_property_by_name(name) then
print("No main")
v.unindent
v.add_instr("}")
end
+end
+
+redef class MMModule
+ # The local table of the module (refers things introduced in the module)
+ var _local_table: Array[ModuleTableElt] = new Array[ModuleTableElt]
+
+ # Builds the local tables and local classes layouts
+ fun local_analysis(tc: ToolContext)
+ do
+ for c in local_classes do
+ if c isa MMConcreteClass then
+ c.build_layout_in(tc, _local_table)
+ end
+ end
+ end
+
+ # Declare class table (for _sep.h)
+ fun declare_class_tables_to_c(v: GlobalCompilerVisitor)
+ do
+ for c in local_classes do
+ if c.global.module == self then
+ c.declare_tables_to_c(v)
+ end
+ end
+ end
# Compile sep files
fun compile_mod_to_c(v: GlobalCompilerVisitor)
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Common things to represent a Nit program/library
+package program
+
+import metamodel
+
+# Instances of this class represent a program/library that will
+# be analyzed/compiled by nitc
+class Program
+ # This module is the 'main' module, the one where we find the 'main' method
+ readable var _module: MMModule
+
+ init(m: MMModule) do
+ _module = m
+ end
+end