compile: cnames for classes and modules
[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.main_module.cname}._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.cname}.{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 == 1 then
90 verbose = "-v"
91 else if tc.verbose_level >= 2 then
92 # We catch tc.verbose_level >= 2, since 3+ is not valid with gccx
93 verbose = "-vv"
94 end
95
96 f.write("#!/bin/sh\n")
97 f.write("# This shell script is generated by NIT to compile the program {program.main_module.full_name}.\n")
98 f.write("CLIBDIR=\"{tc.clibdir.as(not null)}\"\n")
99 f.write("{tc.bindir.as(not null)}/gccx {verbose} -d {compdir} -I $CLIBDIR {include_dirs.join(" ")}")
100 if tc.output_file != null then
101 f.write(" -o {tc.output_file.as(not null)}")
102 else if tc.ext_prefix.is_empty then
103 f.write(" -o {program.main_module.name}")
104 else
105 f.write(" -o {program.main_module.name}_{tc.ext_prefix}")
106 end
107 if tc.boost then f.write(" -O")
108 if not tc.cc_link then f.write(" -x \"-c\"")
109 for l in tc.cc_libs do f.write(" -x \"-l {l}\"")
110 for lp in tc.cc_lib_paths do f.write(" -x \"-L {lp}\"")
111 for ip in tc.cc_include_paths do f.write(" -x \"-I {ip}\"")
112 f.write(" \"$@\" \\\n {files.join("\\\n ")}\n")
113 f.close
114 end
115
116 # Invoke the build_file
117 fun run_c_compiler
118 do
119 program.tc.info("Building",1)
120 sys.system("sh {_build_file}")
121 end
122 end
123
124 # Class used to generate files.
125 # Note that in fact it is not a visitor.
126 # Note also that this class is unefficient and poorly designed thus requires love.
127 class CompilerVisitor
128 # Add a line in the current declaration block
129 fun add_decl(s: String)
130 do
131 add_line_to(_decl_writer, s)
132 end
133
134 # Add a line in the current instr block
135 fun add_instr(s: String)
136 do
137 add_line_to(_writer, s)
138 end
139
140
141 fun add_indent(w: Writer)
142 do
143 if _indent_level >= 8 then
144 w.add("\t\t")
145 else
146 for i in [0.._indent_level[ do
147 w.add(" ")
148 end
149 end
150 end
151
152 fun add_line_to(w: Writer, s: String)
153 do
154 add_indent(w)
155 w.add(s)
156 w.add("\n")
157 end
158
159 # Add a assignment between a variable and an expression
160 fun add_assignment(v: String, s: String)
161 do
162 if v != s then
163 var w = _writer
164 add_indent(w)
165 w.add(v)
166 w.add(" = ")
167 w.add(s)
168 w.add(";\n")
169 end
170 end
171
172 # Return a unique new number for the instance
173 fun new_number: Int
174 do
175 var res = _number_cpt
176 _number_cpt = res + 1
177 return res
178 end
179 # next number for new_number
180 var _number_cpt: Int = 0
181
182 # Add an indent level.
183 # New decl and instr will be indented.
184 fun indent do _indent_level += 1
185
186 # Remove an indent level.
187 fun unindent
188 do
189 _indent_level -= 1
190 if _indent_level < 0 then _indent_level = 0
191 end
192
193 # The processed mmmodule
194 readable var _mmmodule: MMModule
195
196 # Where header decl are stored (public stuff)
197 readable writable var _header_writer: Writer
198
199 # Where current instr are stored (current function declaration)
200 readable writable var _writer: Writer
201
202 # Where current decl are stored (current function instructions)
203 readable writable var _decl_writer: Writer
204
205 # Where body instr are stored (C functions body)
206 readable writable var _top_writer: Writer
207
208 # Where body decl are stored (private C function proptypes and typedefs)
209 readable writable var _top_decl_writer: Writer
210
211 # The current indent lever
212 readable writable var _indent_level: Int = 0
213
214 # The program we are compiling
215 readable var _program: Program
216
217 # The cprogram associed with program
218 readable var _cprogram: CProgram
219
220 # Create a new CompilerVisitor based on a module
221 init(mod: MMModule, cp: CProgram)
222 do
223 _mmmodule = mod
224 _cprogram = cp
225 _program = cp.program
226
227 var w = new Writer
228 _header_writer = w
229 _decl_writer = w
230 w = new Writer
231 _writer = w
232 _top_writer = w
233 _top_decl_writer = w.sub
234 end
235 end
236
237 redef class MMGlobalProperty
238 # C symbol refering a method inocation
239 fun meth_call: String
240 do
241 return "CALL_{intro.cname}"
242 end
243
244 # C symbol refering an attribure access
245 fun attr_access: String
246 do
247 return "ATTR_{intro.cname}"
248 end
249 end
250
251 redef class MMGlobalClass
252 # Cacher result of cname
253 var _cname_cache: nullable String
254
255 # The mangled name of the global class
256 fun cname: String
257 do
258 var cname = _cname_cache
259 if cname == null then
260 cname = intro.mmmodule.cname + "___" + cmangle(intro.name)
261 _cname_cache = cname
262 end
263 return cname
264 end
265
266 # C symbol refering the identifier of the class
267 fun id_id: String
268 do
269 return "ID_{cname}"
270 end
271
272 # C symbol refering the color of the class (for subtype tests)
273 fun color_id: String
274 do
275 return "COLOR_{cname}"
276 end
277
278 # C symbol refering the init table position of the class (for constructor linearization)
279 fun init_table_pos_id: String
280 do
281 return "INIT_TABLE_POS_{cname}"
282 end
283 end
284
285 redef class MMModule
286 # Cacher result of cname
287 var _cname_cache: nullable String
288
289 # The mangled name of the module
290 fun cname: String
291 do
292 var cname = _cname_cache
293 if cname == null then
294 var l = new List[String]
295 var m: nullable MMModule = self
296 while m != null do
297 l.unshift(cmangle(m.name))
298 var d: nullable MMDirectory = m.directory
299 while d != null and d.owner == m do d = d.parent
300 if d == null then m = null else m = d.owner
301 end
302 cname = l.to_a.join("___")
303 _cname_cache = cname
304 end
305 return cname
306 end
307 end
308
309 redef class MMLocalClass
310 # The mangled name of the global class
311 fun cname: String do return global.cname
312 end
313
314 redef class MMLocalProperty
315 # Cacher result of cname
316 var _cname_cache: nullable String
317
318 # The mangled name of the method
319 fun cname: String
320 do
321 var cname = _cname_cache
322 if cname == null then
323 cname = mmmodule.cname + "___" + cmangle(local_class.name, name)
324 _cname_cache = cname
325 end
326 return cname
327 end
328
329 # C macro used to get the function for the call of a super property
330 fun super_meth_call: String
331 do
332 return "CALL_SUPER_{cname}"
333 end
334 end
335