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