parser: factorize the lexer big accept switch
[nit.git] / src / compiling / compiling_base.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 # Common things for NIT compilation and C generation
18 package compiling_base
19
20 import mmloader
21 private import utils
22 import primitive_info
23 import program
24 import compiling_writer
25
26 redef class ToolContext
27 readable writable var _compdir: nullable String = null
28 readable writable var _clibdir: nullable String = null
29 readable writable var _bindir: nullable String = null
30 readable writable var _output_file: nullable String = null
31 readable writable var _boost: Bool = false
32 readable writable var _no_cc: Bool = false
33 readable writable var _cc_link: Bool = false
34 readable writable var _cc_libs: Array[String] = new Array[String]
35 readable writable var _cc_lib_paths: Array[String] = new Array[String]
36 readable writable var _cc_include_paths: Array[String] = new Array[String]
37 readable writable var _ext_prefix: String = ""
38 end
39
40 # A program that is compiled to C
41 class CProgram
42 init(p: Program)
43 do
44 _program = p
45 _compdir = p.tc.compdir.as(not null)
46 _build_file = "{compdir}/{program.module.name}._build.sh"
47 end
48
49 # The Nit program compiled to C
50 readable var _program: Program
51
52 # C files (full path) required to compile
53 readable var _files: Array[String] = new Array[String]
54
55 # Includes paths (gcc -I) required to find the headers (.h) of native modules
56 readable var _include_dirs: ArraySet[String] = new ArraySet[String]
57
58 # The path of the building script
59 readable var _build_file: String
60
61 # The directory where all files are generated
62 readable var _compdir: String
63
64 # Return the basename of the public header file (.h) of a module
65 fun module_header_name(m: MMModule): String
66 do
67 if _module_include.has_key(m) then
68 return _module_include[m]
69 end
70 var filename = "{m.name}.{get_file_ending}.h"
71 _module_include[m] = filename
72 return filename
73 end
74
75 # Cache for module_header_name
76 var _module_include: Map[MMModule, String] = new HashMap[MMModule, String]
77
78 # When we are using global compilation, we generate _glob files instead
79 # of _sep files so that we do not corrupt separate compilation
80 fun get_file_ending: String do return if program.tc.global then "_glob" else "_sep"
81
82 # Generate the shell script that build the program by calling gccx
83 fun generate_build_file
84 do
85 var f = new OFStream.open(_build_file)
86 var verbose = ""
87 var tc = program.tc
88
89 if tc.verbose_level > 0 then
90 verbose = "-"
91 for i in [1..tc.verbose_level] do verbose = verbose + "v"
92 end
93
94 f.write("#!/bin/sh\n")
95 f.write("# This shell script is generated by NIT to compile the program {program.module.name}.\n")
96 f.write("CLIBDIR=\"{tc.clibdir.as(not null)}\"\n")
97 f.write("{tc.bindir.as(not null)}/gccx {verbose} -d {compdir} -I $CLIBDIR {include_dirs.join(" ")}")
98 if tc.output_file != null then
99 f.write(" -o {tc.output_file.as(not null)}")
100 else if tc.ext_prefix.is_empty then
101 f.write(" -o {program.module.name}")
102 else
103 f.write(" -o {program.module.name}_{tc.ext_prefix}")
104 end
105 if tc.boost then f.write(" -O")
106 if not tc.cc_link then f.write(" -x \"-c\"")
107 for l in tc.cc_libs do f.write(" -x \"-l {l}\"")
108 for lp in tc.cc_lib_paths do f.write(" -x \"-L {lp}\"")
109 for ip in tc.cc_include_paths do f.write(" -x \"-I {ip}\"")
110 f.write(" \"$@\" \\\n {files.join("\\\n ")}\n")
111 f.close
112 end
113
114 # Invoke the build_file
115 fun run_c_compiler
116 do
117 program.tc.info("Building",1)
118 sys.system("sh {_build_file}")
119 end
120 end
121
122 # Class used to generate files.
123 # Note that in fact it is not a visitor.
124 # Note also that this class is unefficient and poorly designed thus requires love.
125 class CompilerVisitor
126 # Add a line in the current declaration block
127 fun add_decl(s: String)
128 do
129 add_line_to(_decl_writer, s)
130 end
131
132 # Add a line in the current instr block
133 fun add_instr(s: String)
134 do
135 add_line_to(_writer, s)
136 end
137
138
139 fun add_indent(w: Writer)
140 do
141 if _indent_level >= 8 then
142 w.add("\t\t")
143 else
144 for i in [0.._indent_level[ do
145 w.add(" ")
146 end
147 end
148 end
149
150 fun add_line_to(w: Writer, s: String)
151 do
152 add_indent(w)
153 w.add(s)
154 w.add("\n")
155 end
156
157 # Add a assignment between a variable and an expression
158 fun add_assignment(v: String, s: String)
159 do
160 if v != s then
161 var w = _writer
162 add_indent(w)
163 w.add(v)
164 w.add(" = ")
165 w.add(s)
166 w.add(";\n")
167 end
168 end
169
170 # Return a unique new number for the instance
171 fun new_number: Int
172 do
173 var res = _number_cpt
174 _number_cpt = res + 1
175 return res
176 end
177 # next number for new_number
178 var _number_cpt: Int = 0
179
180 # Add an indent level.
181 # New decl and instr will be indented.
182 fun indent do _indent_level += 1
183
184 # Remove an indent level.
185 fun unindent
186 do
187 _indent_level -= 1
188 if _indent_level < 0 then _indent_level = 0
189 end
190
191 # The processed module
192 readable var _module: MMModule
193
194 # Where header decl are stored (public stuff)
195 readable writable var _header_writer: Writer
196
197 # Where current instr are stored (current function declaration)
198 readable writable var _writer: Writer
199
200 # Where current decl are stored (current function instructions)
201 readable writable var _decl_writer: Writer
202
203 # Where body instr are stored (C functions body)
204 readable writable var _top_writer: Writer
205
206 # Where body decl are stored (private C function proptypes and typedefs)
207 readable writable var _top_decl_writer: Writer
208
209 # The current indent lever
210 readable writable var _indent_level: Int = 0
211
212 # The program we are compiling
213 readable var _program: Program
214
215 # The cprogram associed with program
216 readable var _cprogram: CProgram
217
218 # Create a new CompilerVisitor based on a module
219 init(module: MMModule, cp: CProgram)
220 do
221 _module = module
222 _cprogram = cp
223 _program = cp.program
224
225 var w = new Writer
226 _header_writer = w
227 _decl_writer = w
228 w = new Writer
229 _writer = w
230 _top_writer = w
231 _top_decl_writer = w.sub
232 end
233 end
234
235 redef class MMGlobalProperty
236 # C symbol refering a method inocation
237 fun meth_call: String
238 do
239 return "CALL_{intro.cname}"
240 end
241
242 # C symbol refering an attribure access
243 fun attr_access: String
244 do
245 return "ATTR_{intro.cname}"
246 end
247 end
248
249 redef class MMGlobalClass
250 # C symbol refering the identifier of the class
251 fun id_id: String
252 do
253 return "ID_{intro.name}"
254 end
255
256 # C symbol refering the color of the class (for subtype tests)
257 fun color_id: String
258 do
259 return "COLOR_{intro.name}"
260 end
261
262 # C symbol refering the init table position of the class (for constructor linearization)
263 fun init_table_pos_id: String
264 do
265 return "INIT_TABLE_POS_{intro.name}"
266 end
267 end
268
269 redef class MMLocalProperty
270 # Cacher result of cname
271 var _cname_cache: nullable String
272
273 # The mangled name of the method
274 fun cname: String
275 do
276 var cname = _cname_cache
277 if cname == null then
278 cname = cmangle(module.name, local_class.name, name)
279 _cname_cache = cname
280 end
281 return cname
282 end
283
284 # C macro used to get the function for the call of a super property
285 fun super_meth_call: String
286 do
287 return "CALL_SUPER_{cname}"
288 end
289 end
290