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