compile: add 'icode' as output type
authorJean-Sebastien Gelinas <calestar@gmail.com>
Fri, 4 Dec 2009 06:14:11 +0000 (01:14 -0500)
committerJean Privat <jean@pryen.org>
Mon, 11 Jan 2010 21:52:34 +0000 (16:52 -0500)
Signed-off-by: Jean-Sebastien Gelinas <calestar@gmail.com>
Signed-off-by: Jean Privat <jean@pryen.org>

src/compiling/compiling.nit
src/compiling/compiling_icode.nit
src/compiling/icode_generator.nit [new file with mode: 0644]
src/nitc.nit

index 9b75317..5391fb0 100644 (file)
@@ -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
 
index 8ca8a7a..5374ac8 100644 (file)
@@ -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 (file)
index 0000000..04d479c
--- /dev/null
@@ -0,0 +1,109 @@
+# 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.
+
+# 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
index 6f2bd3e..fc5d7c8 100644 (file)
@@ -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