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