icode: introduce intermediate code representation
[nit.git] / src / compiling / compiling.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Compute and generate tables for classes and modules.
18 package compiling
19
20 import compiling_base
21 private import compiling_global
22 private import compiling_icode
23
24 redef class MMModule
25 # Compile the program
26 # Generate all sep files (_sep.[ch]), the main file (_table.c) and the build file (_build.sh)
27 # Then execute the build.sh
28 fun compile_prog_to_c(tc: ToolContext)
29 do
30 tc.info("Building tables",1)
31 for m in mhe.greaters_and_self do
32 tc.info("Building tables for module: {m.name}",2)
33 m.local_analysis(tc)
34 end
35
36 tc.info("Merging all tables",2)
37 var ga = global_analysis(tc)
38
39 tc.compdir.mkdir
40
41 var files = new Array[String]
42 var includes = new ArraySet[String]
43 files.add("$CLIBDIR/nit_main.c")
44 tc.info("Generating C code",1)
45 for m in mhe.greaters_and_self do
46 files.add("{tc.compdir}/{m.name}._sep.c")
47 tc.info("Generating C code for module: {m.name}",2)
48 m.compile_separate_module(tc, ga)
49 var native_name = m.location.file.strip_extension(".nit")
50 if (native_name + "_nit.h").file_exists then
51 includes.add("-I {native_name.dirname}")
52 end
53 native_name += "_nit.c"
54 if native_name.file_exists then files.add(native_name)
55 end
56
57 tc.info("Generating main, tables and makefile ...",1)
58 files.add("{tc.compdir}/{name}._tables.c")
59 compile_main(tc, ga)
60
61 var fn = "{tc.compdir}/{name}._build.sh"
62 var f = new OFStream.open(fn)
63 var verbose = ""
64
65 if tc.verbose_level > 0 then
66 verbose = "-"
67 for i in [1..tc.verbose_level] do verbose = verbose + "v"
68 end
69
70 f.write("#!/bin/sh\n")
71 f.write("# This shell script is generated by NIT to compile the program {name}.\n")
72 f.write("CLIBDIR=\"{tc.clibdir}\"\n")
73 f.write("{tc.bindir}/gccx {verbose} -d {tc.compdir} -I $CLIBDIR {includes.join(" ")}")
74 if tc.output_file != null then
75 f.write(" -o {tc.output_file}")
76 else if tc.ext_prefix.is_empty then
77 f.write(" -o {name}")
78 else
79 f.write(" -o {name}_{tc.ext_prefix}")
80 end
81 if tc.boost then f.write(" -O")
82 f.write(" \"$@\" \\\n {files.join("\\\n ")}\n")
83 f.close
84
85 if not tc.no_cc then
86 tc.info("Building",1)
87 sys.system("sh {fn}")
88 end
89 end
90
91 # Compile the main file
92 private fun compile_main(tc: ToolContext, ga: GlobalAnalysis)
93 do
94 var v = new GlobalCompilerVisitor(self, tc, ga)
95 v.add_decl("#include <nit_common.h>")
96 compile_tables_to_c(v)
97 compile_main_part(v)
98 var f = new OFStream.open("{tc.compdir}/{name}._tables.c")
99 f.write("/* This C file is generated by NIT to compile program {name}. */\n")
100 for m in mhe.greaters_and_self do
101 f.write("#include \"{m.name}._sep.h\"\n")
102 end
103 f.write(v.to_s)
104 f.close
105 end
106
107 # Compile the sep files (of the current module only)
108 private fun compile_separate_module(tc: ToolContext, ga: GlobalAnalysis)
109 do
110 var v = new GlobalCompilerVisitor(self, tc, ga)
111 v.add_decl("#include <nit_common.h>")
112 var native_name = location.file.strip_extension(".nit")
113 native_name += ("_nit.h")
114 if native_name.file_exists then v.add_decl("#include <{native_name.basename("")}>")
115 declare_class_tables_to_c(v)
116 compile_mod_to_c(v)
117 var f = new OFStream.open("{tc.compdir}/{name}._sep.h")
118 f.write("/* This C header file is generated by NIT to compile modules and programs that requires {name}. */\n")
119 f.write("#ifndef {name}_sep\n")
120 f.write("#define {name}_sep\n")
121 for m in mhe.direct_greaters do f.write("#include \"{m.name}._sep.h\"\n")
122 for s in v.ctx.decls do
123 f.write(s)
124 end
125 f.write("#endif\n")
126 f.close
127 var f = new OFStream.open("{tc.compdir}/{name}._sep.c")
128 f.write("/* This C file is generated by NIT to compile module {name}. */\n")
129 f.write("#include \"{name}._sep.h\"\n")
130 for s in v.ctx.instrs do
131 f.write(s)
132 end
133 f.close
134 end
135 end
136