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