compile: extract table computation from compiling_global to table_computation
[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 table_computation
21 import compiling_base
22 private import compiling_global
23 private import compiling_icode
24
25 redef class Program
26 # Compile the program
27 # Generate all sep files (_sep.[ch]), the main file (_table.c) and the build file (_build.sh)
28 # Then execute the build.sh
29 fun compile_prog_to_c(tc: ToolContext)
30 do
31 tc.compdir.mkdir
32
33 var files = new Array[String]
34 var includes = new ArraySet[String]
35 files.add("$CLIBDIR/nit_main.c")
36 files.add("$CLIBDIR/gc.c")
37 files.add("$CLIBDIR/gc_static_objects_list.c")
38 tc.info("Generating C code",1)
39 for m in module.mhe.greaters_and_self do
40 files.add("{tc.compdir}/{m.name}._sep.c")
41 tc.info("Generating C code for module: {m.name}",2)
42 m.compile_separate_module(tc, self)
43 var native_name = m.location.file.strip_extension(".nit")
44 if (native_name + "_nit.h").file_exists then
45 includes.add("-I {native_name.dirname}")
46 end
47 native_name += "_nit.c"
48 if native_name.file_exists then files.add(native_name)
49 end
50
51 tc.info("Generating main, tables and makefile ...",1)
52 files.add("{tc.compdir}/{module.name}._tables.c")
53 compile_main(tc)
54
55 var fn = "{tc.compdir}/{module.name}._build.sh"
56 var f = new OFStream.open(fn)
57 var verbose = ""
58
59 if tc.verbose_level > 0 then
60 verbose = "-"
61 for i in [1..tc.verbose_level] do verbose = verbose + "v"
62 end
63
64 f.write("#!/bin/sh\n")
65 f.write("# This shell script is generated by NIT to compile the program {module.name}.\n")
66 f.write("CLIBDIR=\"{tc.clibdir}\"\n")
67 f.write("{tc.bindir}/gccx {verbose} -d {tc.compdir} -I $CLIBDIR {includes.join(" ")}")
68 if tc.output_file != null then
69 f.write(" -o {tc.output_file}")
70 else if tc.ext_prefix.is_empty then
71 f.write(" -o {module.name}")
72 else
73 f.write(" -o {module.name}_{tc.ext_prefix}")
74 end
75 if tc.boost then f.write(" -O")
76 f.write(" \"$@\" \\\n {files.join("\\\n ")}\n")
77 f.close
78
79 if not tc.no_cc then
80 tc.info("Building",1)
81 sys.system("sh {fn}")
82 end
83 end
84
85 # Compile the main file
86 private fun compile_main(tc: ToolContext)
87 do
88 var v = new GlobalCompilerVisitor(module, tc, self)
89 v.add_decl("#include <nit_common.h>")
90 compile_tables_to_c(v)
91 compile_main_part(v)
92 var f = new OFStream.open("{tc.compdir}/{module.name}._tables.c")
93 f.write("/* This C file is generated by NIT to compile program {module.name}. */\n")
94 for m in module.mhe.greaters_and_self do
95 f.write("#include \"{m.name}._sep.h\"\n")
96 end
97 f.write(v.to_s)
98 f.close
99 end
100 end
101
102 redef class MMModule
103 # Compile the sep files (of the current module only)
104 private fun compile_separate_module(tc: ToolContext, program: Program)
105 do
106 var v = new GlobalCompilerVisitor(self, tc, program)
107 v.add_decl("#include <nit_common.h>")
108 var native_name = location.file.strip_extension(".nit")
109 native_name += ("_nit.h")
110 if native_name.file_exists then v.add_decl("#include <{native_name.basename("")}>")
111 declare_class_tables_to_c(v)
112 compile_mod_to_c(v)
113 var f = new OFStream.open("{tc.compdir}/{name}._sep.h")
114 f.write("/* This C header file is generated by NIT to compile modules and programs that requires {name}. */\n")
115 f.write("#ifndef {name}_sep\n")
116 f.write("#define {name}_sep\n")
117 for m in mhe.direct_greaters do f.write("#include \"{m.name}._sep.h\"\n")
118 for s in v.ctx.decls do
119 f.write(s)
120 end
121 f.write("#endif\n")
122 f.close
123
124 f = new OFStream.open("{tc.compdir}/{name}._sep.c")
125 f.write("/* This C file is generated by NIT to compile module {name}. */\n")
126 f.write("#include \"{name}._sep.h\"\n")
127 for s in v.ctx.instrs do
128 f.write(s)
129 end
130 f.close
131 end
132 end
133