From 70a211e67a3e83ed0bbe6bea439b17824b0d4479 Mon Sep 17 00:00:00 2001 From: Jean-Sebastien Gelinas Date: Fri, 4 Dec 2009 01:14:11 -0500 Subject: [PATCH] compile: add 'icode' as output type Signed-off-by: Jean-Sebastien Gelinas Signed-off-by: Jean Privat --- src/compiling/compiling.nit | 12 +++- src/compiling/compiling_icode.nit | 2 - src/compiling/icode_generator.nit | 109 +++++++++++++++++++++++++++++++++++++ src/nitc.nit | 2 +- 4 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 src/compiling/icode_generator.nit diff --git a/src/compiling/compiling.nit b/src/compiling/compiling.nit index 9b75317..5391fb0 100644 --- a/src/compiling/compiling.nit +++ b/src/compiling/compiling.nit @@ -19,6 +19,7 @@ package compiling import table_computation import compiling_base +import icode_generator private import compiling_global private import compiling_icode @@ -31,8 +32,15 @@ redef class Program do if output_format == "none" then # Nothing to do - else if output_format == "C" then - compile_prog_to_c + else + # Optimize all iroutines + with_each_iroutines !action(i, m) do i.optimize(m) + + if output_format == "C" then + compile_prog_to_c + else if output_format == "icode" then + generate_icode_files + end end end diff --git a/src/compiling/compiling_icode.nit b/src/compiling/compiling_icode.nit index 8ca8a7a..5374ac8 100644 --- a/src/compiling/compiling_icode.nit +++ b/src/compiling/compiling_icode.nit @@ -336,11 +336,9 @@ redef class IRoutine end # Full compilation of the routine - # Including optimization and other stuff. # cv must be in the correct function fun compile_to_c(cv: CompilerVisitor, cname: String, args: Array[String]): nullable String do - optimize(cv.module) var v = new I2CCompilerVisitor(cv, self, cname) return compile_inside_to_c(v, args) end diff --git a/src/compiling/icode_generator.nit b/src/compiling/icode_generator.nit new file mode 100644 index 0000000..04d479c --- /dev/null +++ b/src/compiling/icode_generator.nit @@ -0,0 +1,109 @@ +# 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. + +# Generates ICode as an output format +package icode_generator + +import compiling_base +import program +import analysis + +redef class Program + # Generates ICode for the whole program + fun generate_icode_files do + tc.compdir.mkdir + + with_each_live_local_classes !action(c) do + c.generate_icode_file(tc.compdir.as(not null)) + end + end +end + +# A class to dump ICode to a file +class FileICodeDumper +special ICodeDumper + var _file: OFStream + + init(f: OFStream) do + # We don't want to output line numbers and locations + super(false, false) + _file = f + end + + redef fun write(s) do + for i in [0..indent_level[ do + _file.write(" ") + end + _file.write(s) + _file.write("\n") + end +end + +redef class MMLocalClass + # Generates ICode for all properties of this class in a file + fun generate_icode_file(dir: String) do + var file = new OFStream.open("{dir}/{self}.icode") + if primitive_info == null then + # Initialization methods + if init_var_iroutine != null then + var icd = new FileICodeDumper(file) + icd.indent + file.write("Init var iroutine::\n") + init_var_iroutine.dump(icd) + file.write("\n\n") + end + if checknew_iroutine != null then + var icd = new FileICodeDumper(file) + icd.indent + file.write("Check new instance iroutine::\n") + checknew_iroutine.dump(icd) + file.write("\n\n") + end + + # 'new' methods + for pg in global_properties do + if not pg.is_init_for(self) then continue + var p = self[pg] + assert p isa MMMethod + var icd = new FileICodeDumper(file) + icd.indent + file.write("New instance:: {p.full_name}\n") + new_instance_iroutine[p].dump(icd) + file.write("\n\n") + end + end + + # Other methods + for pg in global_properties do + var p = self[pg] + if p.local_class == self and p isa MMMethod then + p.generate_icode(file) + end + end + file.close + end +end + +redef class MMMethod + # Generates ICode for this method with a little header + fun generate_icode(file: OFStream) do + var icd = new FileICodeDumper(file) + icd.indent + file.write("Method:: {full_name}\n") + iroutine.dump(icd) + file.write("\n\n") + end +end diff --git a/src/nitc.nit b/src/nitc.nit index 6f2bd3e..fc5d7c8 100644 --- a/src/nitc.nit +++ b/src/nitc.nit @@ -41,7 +41,7 @@ special AbstractCompiler readable var _opt_compdir: OptionString = new OptionString("Intermediate compilation directory", "--compdir") readable var _opt_extension_prefix: OptionString = new OptionString("Append prefix to file extension", "-p", "--extension-prefix") readable var _opt_dump: OptionBool = new OptionBool("Dump intermediate code", "--dump") - readable var _opt_output_format: OptionEnum = new OptionEnum(["none", "C"], "The type of code we want to be generated", 1, "--output-format") + readable var _opt_output_format: OptionEnum = new OptionEnum(["none", "C", "icode"], "The type of code we want to be generated", 1, "--output-format") init do -- 1.7.9.5