1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // main.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Fri May 15 23:12:02 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu May 21 21:15:54 2015 |
---|
13 | // Update Count : 9 |
---|
14 | // |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | #include <fstream> |
---|
18 | #include <cstdlib> |
---|
19 | #include <cstdio> |
---|
20 | #include <getopt.h> |
---|
21 | #include "Parser/Parser.h" |
---|
22 | #include "Parser/ParseNode.h" |
---|
23 | #include "Parser/LinkageSpec.h" |
---|
24 | #include "SynTree/Declaration.h" |
---|
25 | #include "SynTree/Visitor.h" |
---|
26 | #include "GenPoly/Lvalue.h" |
---|
27 | #include "GenPoly/Specialize.h" |
---|
28 | #include "GenPoly/Box.h" |
---|
29 | #include "GenPoly/CopyParams.h" |
---|
30 | #include "CodeGen/Generate.h" |
---|
31 | #include "CodeGen/FixNames.h" |
---|
32 | #include "ControlStruct/Mutate.h" |
---|
33 | #include "Tuples/Mutate.h" |
---|
34 | #include "Tuples/FunctionChecker.h" |
---|
35 | #include "SymTab/Mangler.h" |
---|
36 | #include "SymTab/Indexer.h" |
---|
37 | #include "SymTab/Validate.h" |
---|
38 | #include "ResolvExpr/AlternativePrinter.h" |
---|
39 | #include "ResolvExpr/Resolver.h" |
---|
40 | #include "MakeLibCfa.h" |
---|
41 | #include "InitTweak/Mutate.h" |
---|
42 | #include "InitTweak/RemoveInit.h" |
---|
43 | //#include "Explain/GenProlog.h" |
---|
44 | //#include "Try/Visit.h" |
---|
45 | |
---|
46 | #include "SemanticError.h" |
---|
47 | #include "UnimplementedError.h" |
---|
48 | |
---|
49 | #include "../config.h" |
---|
50 | |
---|
51 | using namespace std; |
---|
52 | |
---|
53 | bool |
---|
54 | astp = false, |
---|
55 | exprp = false, |
---|
56 | expraltp = false, |
---|
57 | grammarp = false, |
---|
58 | libcfap = false, |
---|
59 | resolvep = false, // used in AlternativeFinder |
---|
60 | symtabp = false, |
---|
61 | parsep = false, |
---|
62 | validp = false, |
---|
63 | preludep = true, |
---|
64 | protop = false, |
---|
65 | codegenp = false, |
---|
66 | errorp = false; |
---|
67 | |
---|
68 | enum { Ast, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Prototypes, Resolver, Symbol, Parse, }; |
---|
69 | |
---|
70 | static struct option long_opts[] = { |
---|
71 | { "ast", no_argument, 0, Ast }, |
---|
72 | { "expr", no_argument, 0, Expr }, |
---|
73 | { "expralt", no_argument, 0, ExprAlt }, |
---|
74 | { "grammar", no_argument, 0, Grammar }, |
---|
75 | { "libcfa", no_argument, 0, LibCFA }, |
---|
76 | { "nopreamble", no_argument, 0, Nopreamble }, |
---|
77 | { "prototypes", no_argument, 0, Prototypes }, |
---|
78 | { "resolver", no_argument, 0, Resolver }, |
---|
79 | { "symbol", no_argument, 0, Symbol }, |
---|
80 | { "parse", no_argument, 0, Parse }, |
---|
81 | { 0, 0, 0, 0 } |
---|
82 | }; |
---|
83 | |
---|
84 | int main( int argc, char *argv[] ) { |
---|
85 | FILE *input; |
---|
86 | std::ostream *output = &std::cout; |
---|
87 | int long_index; |
---|
88 | std::list< Declaration* > translationUnit; |
---|
89 | |
---|
90 | opterr = 0; // prevent getopt from printing error messages |
---|
91 | |
---|
92 | int c; |
---|
93 | while ( (c = getopt_long( argc, argv, "aefglnpqrsxyzD:", long_opts, &long_index )) != -1 ) { |
---|
94 | switch ( c ) { |
---|
95 | case Ast: |
---|
96 | case 'a': // dump AST |
---|
97 | astp = true; |
---|
98 | break; |
---|
99 | case Expr: |
---|
100 | case 'e': // dump AST after expression analysis |
---|
101 | exprp = true; |
---|
102 | break; |
---|
103 | case ExprAlt: |
---|
104 | case 'f': // print alternatives for expressions |
---|
105 | expraltp = true; |
---|
106 | break; |
---|
107 | case Grammar: |
---|
108 | case 'g': // bison debugging info (grammar rules) |
---|
109 | grammarp = true; |
---|
110 | break; |
---|
111 | case LibCFA: |
---|
112 | case 'l': // generate libcfa.c |
---|
113 | libcfap = true; |
---|
114 | break; |
---|
115 | case Nopreamble: |
---|
116 | case 'n': // do not read preamble |
---|
117 | preludep = false; |
---|
118 | break; |
---|
119 | case Prototypes: |
---|
120 | case 'p': // generate prototypes for preamble functions |
---|
121 | protop = true; |
---|
122 | break; |
---|
123 | case Parse: |
---|
124 | case 'q': // dump parse tree |
---|
125 | parsep = true; |
---|
126 | break; |
---|
127 | case Resolver: |
---|
128 | case 'r': // print resolver steps |
---|
129 | resolvep = true; |
---|
130 | break; |
---|
131 | case Symbol: |
---|
132 | case 's': // print symbol table events |
---|
133 | symtabp = true; |
---|
134 | break; |
---|
135 | case 'x': // dump AST after decl validation pass |
---|
136 | validp = true; |
---|
137 | break; |
---|
138 | case 'y': |
---|
139 | errorp = true; |
---|
140 | break; |
---|
141 | case 'z': |
---|
142 | codegenp = true; |
---|
143 | break; |
---|
144 | case 'D': // ignore -Dxxx |
---|
145 | break; |
---|
146 | case '?': |
---|
147 | cout << "Unknown option: '" << (char)optopt << "'" << endl; |
---|
148 | exit(1); |
---|
149 | default: |
---|
150 | abort(); |
---|
151 | } // switch |
---|
152 | } // while |
---|
153 | |
---|
154 | try { |
---|
155 | if ( optind < argc ) { |
---|
156 | input = fopen( argv[ optind ], "r" ); |
---|
157 | if ( ! input ) { |
---|
158 | std::cout << "Error: can't open " << argv[optind] << std::endl; |
---|
159 | exit( 1 ); |
---|
160 | } // if |
---|
161 | optind += 1; |
---|
162 | } else { |
---|
163 | input = stdin; |
---|
164 | } // if |
---|
165 | |
---|
166 | if ( optind < argc ) { |
---|
167 | output = new ofstream( argv[ optind ] ); |
---|
168 | } // if |
---|
169 | |
---|
170 | Parser::get_parser().set_debug( grammarp ); |
---|
171 | |
---|
172 | if ( preludep ) { // include gcc builtins |
---|
173 | FILE *builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" ); |
---|
174 | if ( builtins == NULL ) { |
---|
175 | std::cout << "Error: can't open builtins" << std::endl; |
---|
176 | exit( 1 ); |
---|
177 | } // if |
---|
178 | |
---|
179 | Parser::get_parser().set_linkage( LinkageSpec::Compiler ); |
---|
180 | Parser::get_parser().parse( builtins ); |
---|
181 | |
---|
182 | if ( Parser::get_parser().get_parseStatus() != 0 ) { |
---|
183 | return Parser::get_parser().get_parseStatus(); |
---|
184 | } // if |
---|
185 | fclose( builtins ); |
---|
186 | |
---|
187 | FILE *prelude; |
---|
188 | if ( libcfap ) { // include cfa prelude |
---|
189 | prelude = input; |
---|
190 | } else { |
---|
191 | prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" ); |
---|
192 | } // if |
---|
193 | if ( prelude == NULL ) { |
---|
194 | std::cout << "Error: can't open prelude" << std::endl; |
---|
195 | exit( 1 ); |
---|
196 | } // if |
---|
197 | |
---|
198 | Parser::get_parser().set_linkage( LinkageSpec::Intrinsic ); |
---|
199 | Parser::get_parser().parse( prelude ); |
---|
200 | |
---|
201 | if ( Parser::get_parser().get_parseStatus() != 0 ) { |
---|
202 | return Parser::get_parser().get_parseStatus(); |
---|
203 | } // if |
---|
204 | fclose( prelude ); |
---|
205 | } // if |
---|
206 | |
---|
207 | if ( libcfap ) { |
---|
208 | std::list< Declaration* > translationUnit; |
---|
209 | buildList( Parser::get_parser().get_parseTree(), translationUnit ); |
---|
210 | Parser::get_parser().freeTree(); |
---|
211 | SymTab::validate( translationUnit, false ); |
---|
212 | CodeGen::fixNames( translationUnit ); |
---|
213 | LibCfa::makeLibCfa( translationUnit ); |
---|
214 | ResolvExpr::resolve( translationUnit ); |
---|
215 | GenPoly::convertLvalue( translationUnit ); |
---|
216 | GenPoly::box( translationUnit ); |
---|
217 | CodeGen::generate( translationUnit, *output, true ); |
---|
218 | if ( output != &std::cout ) { |
---|
219 | delete output; |
---|
220 | } // if |
---|
221 | return 0; |
---|
222 | } // if |
---|
223 | |
---|
224 | Parser::get_parser().set_linkage( LinkageSpec::Cforall ); |
---|
225 | |
---|
226 | Parser::get_parser().parse( input ); |
---|
227 | if ( grammarp || Parser::get_parser().get_parseStatus() != 0 ) { |
---|
228 | return Parser::get_parser().get_parseStatus(); |
---|
229 | } // if |
---|
230 | fclose( input ); |
---|
231 | |
---|
232 | if ( parsep ) { |
---|
233 | Parser::get_parser().get_parseTree()->printList( std::cout ); |
---|
234 | Parser::get_parser().freeTree(); |
---|
235 | return 0; |
---|
236 | } // if |
---|
237 | |
---|
238 | buildList( Parser::get_parser().get_parseTree(), translationUnit ); |
---|
239 | |
---|
240 | Parser::get_parser().freeTree(); |
---|
241 | if ( astp ) { |
---|
242 | printAll( translationUnit, std::cout ); |
---|
243 | return 0; |
---|
244 | } // if |
---|
245 | |
---|
246 | if ( expraltp ) { |
---|
247 | SymTab::validate( translationUnit, false ); |
---|
248 | ResolvExpr::AlternativePrinter printer( std::cout ); |
---|
249 | acceptAll( translationUnit, printer ); |
---|
250 | return 0; |
---|
251 | } // if |
---|
252 | |
---|
253 | if ( symtabp ) { |
---|
254 | SymTab::validate( translationUnit, true ); |
---|
255 | return 0; |
---|
256 | } // if |
---|
257 | |
---|
258 | if ( validp ) { |
---|
259 | SymTab::validate( translationUnit, false ); |
---|
260 | printAll( translationUnit, std::cout ); |
---|
261 | return 0; |
---|
262 | } // if |
---|
263 | |
---|
264 | if ( exprp ) { |
---|
265 | InitTweak::tweak( translationUnit ); |
---|
266 | SymTab::validate( translationUnit, false ); |
---|
267 | ControlStruct::mutate( translationUnit ); |
---|
268 | CodeGen::fixNames( translationUnit ); |
---|
269 | ResolvExpr::resolve( translationUnit ); |
---|
270 | printAll( translationUnit, std::cout ); |
---|
271 | return 0; |
---|
272 | } // if |
---|
273 | |
---|
274 | if ( codegenp ) { |
---|
275 | // print the tree right before code generation |
---|
276 | cerr << "tweak" << endl; |
---|
277 | InitTweak::tweak( translationUnit ); |
---|
278 | cerr << "validate" << endl; |
---|
279 | SymTab::validate( translationUnit, false ); |
---|
280 | cerr << "mutate" << endl; |
---|
281 | ControlStruct::mutate( translationUnit ); |
---|
282 | cerr << "fixNames" << endl; |
---|
283 | CodeGen::fixNames( translationUnit ); |
---|
284 | cerr << "resolve" << endl; |
---|
285 | ResolvExpr::resolve( translationUnit ); |
---|
286 | cerr << "copyParams" << endl; |
---|
287 | GenPoly::copyParams( translationUnit ); |
---|
288 | cerr << "convertSpecializations" << endl; |
---|
289 | GenPoly::convertSpecializations( translationUnit ); |
---|
290 | cerr << "convertLvalue" << endl; |
---|
291 | GenPoly::convertLvalue( translationUnit ); |
---|
292 | cerr << "box" << endl; |
---|
293 | GenPoly::box( translationUnit ); |
---|
294 | if ( errorp ) { |
---|
295 | printAll( translationUnit, std::cout ); |
---|
296 | } |
---|
297 | return 0; |
---|
298 | } // if |
---|
299 | |
---|
300 | // add the assignment statement after the |
---|
301 | // initialization of a type parameter |
---|
302 | InitTweak::tweak( translationUnit ); |
---|
303 | |
---|
304 | //std::cerr << "before validate" << std::endl; |
---|
305 | SymTab::validate( translationUnit, false ); |
---|
306 | //Try::visit( translationUnit ); |
---|
307 | //Tuples::mutate( translationUnit ); |
---|
308 | //InitTweak::mutate( translationUnit ); |
---|
309 | //std::cerr << "before mutate" << std::endl; |
---|
310 | ControlStruct::mutate( translationUnit ); |
---|
311 | //std::cerr << "before fixNames" << std::endl; |
---|
312 | CodeGen::fixNames( translationUnit ); |
---|
313 | //std::cerr << "before resolve" << std::endl; |
---|
314 | ResolvExpr::resolve( translationUnit ); |
---|
315 | //Tuples::checkFunctions( translationUnit ); |
---|
316 | // std::cerr << "Finished tuple checkfunctions" << std::endl; |
---|
317 | //printAll( translationUnit, std::cerr ); |
---|
318 | //std::cerr << "before copyParams" << std::endl; |
---|
319 | GenPoly::copyParams( translationUnit ); |
---|
320 | //std::cerr << "before convertSpecializations" << std::endl; |
---|
321 | GenPoly::convertSpecializations( translationUnit ); |
---|
322 | //std::cerr << "before convertLvalue" << std::endl; |
---|
323 | GenPoly::convertLvalue( translationUnit ); |
---|
324 | //std::cerr << "before box" << std::endl; |
---|
325 | GenPoly::box( translationUnit ); |
---|
326 | //Tuples::mutate( translationUnit ); |
---|
327 | |
---|
328 | CodeGen::generate( translationUnit, *output, protop ); |
---|
329 | |
---|
330 | if ( output != &std::cout ) { |
---|
331 | delete output; |
---|
332 | } // if |
---|
333 | |
---|
334 | } catch ( SemanticError &e ) { |
---|
335 | if ( errorp ) { |
---|
336 | printAll( translationUnit, std::cout ); |
---|
337 | } |
---|
338 | e.print( cout ); |
---|
339 | if ( output != &std::cout ) { |
---|
340 | delete output; |
---|
341 | } // if |
---|
342 | return 1; |
---|
343 | } catch ( UnimplementedError &e ) { |
---|
344 | std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl; |
---|
345 | if ( output != &std::cout ) { |
---|
346 | delete output; |
---|
347 | } // if |
---|
348 | return 1; |
---|
349 | } catch ( CompilerError &e ) { |
---|
350 | std::cerr << "Compiler Error: " << e.get_what() << std::endl; |
---|
351 | std::cerr << "(please report bugs to " << std::endl; |
---|
352 | if ( output != &std::cout ) { |
---|
353 | delete output; |
---|
354 | } // if |
---|
355 | return 1; |
---|
356 | } // try |
---|
357 | |
---|
358 | return 0; |
---|
359 | } // main |
---|
360 | |
---|
361 | // Local Variables: // |
---|
362 | // tab-width: 4 // |
---|
363 | // mode: c++ // |
---|
364 | // compile-command: "make install" // |
---|
365 | // End: // |
---|