From: Jean-Sebastien Gelinas Date: Mon, 3 Aug 2009 21:13:21 +0000 (-0400) Subject: compile: add 'Program' class to represent a nit program X-Git-Tag: v0.3~37 X-Git-Url: http://nitlanguage.org compile: add 'Program' class to represent a nit program Signed-off-by: Jean-Sebastien Gelinas Signed-off-by: Jean Privat --- diff --git a/src/compiling/compiling.nit b/src/compiling/compiling.nit index 4f142fd..901100d 100644 --- a/src/compiling/compiling.nit +++ b/src/compiling/compiling.nit @@ -21,14 +21,14 @@ import compiling_base 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 @@ -44,7 +44,7 @@ redef class MMModule 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) @@ -57,10 +57,10 @@ redef class MMModule 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 = "" @@ -70,15 +70,15 @@ redef class MMModule 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") @@ -93,19 +93,21 @@ redef class MMModule # 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 ") 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 diff --git a/src/compiling/compiling_global.nit b/src/compiling/compiling_global.nit index bc69ed1..36708e3 100644 --- a/src/compiling/compiling_global.nit +++ b/src/compiling/compiling_global.nit @@ -17,6 +17,7 @@ # Compute and generate tables for classes and modules. package compiling_global +import program private import compiling_icode # Something that store color of table elements @@ -152,25 +153,11 @@ redef class MMConcreteClass 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] @@ -186,7 +173,7 @@ redef class MMModule # 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 @@ -321,44 +308,6 @@ redef class MMModule 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 @@ -413,19 +362,57 @@ redef class MMModule 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") @@ -435,16 +422,6 @@ redef class MMModule 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 @@ -453,10 +430,10 @@ redef class MMModule 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") @@ -471,6 +448,31 @@ redef class MMModule 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) diff --git a/src/nitc.nit b/src/nitc.nit index debc63e..08a44c2 100644 --- a/src/nitc.nit +++ b/src/nitc.nit @@ -19,6 +19,7 @@ package nitc import abstracttool import analysis +import program private import compiling private import syntax @@ -124,9 +125,9 @@ special AbstractCompiler dump_intermediate_code(mods) end for mod in mods do - mod.compile_prog_to_c(self) + var p = new Program(mod) + p.compile_prog_to_c(self) end - end end diff --git a/src/program.nit b/src/program.nit new file mode 100644 index 0000000..6d9b2b6 --- /dev/null +++ b/src/program.nit @@ -0,0 +1,31 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2009 Jean-Sebastien Gelinas +# +# 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