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 : Rob Schluntz
|
---|
12 | // Last Modified On : Mon Apr 11 17:49:38 2016
|
---|
13 | // Update Count : 200
|
---|
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 "InitTweak/FixInit.h"
|
---|
44 | //#include "Explain/GenProlog.h"
|
---|
45 | //#include "Try/Visit.h"
|
---|
46 |
|
---|
47 | #include "Common/SemanticError.h"
|
---|
48 | #include "Common/UnimplementedError.h"
|
---|
49 |
|
---|
50 | #include "../config.h"
|
---|
51 |
|
---|
52 | using namespace std;
|
---|
53 |
|
---|
54 | #define OPTPRINT(x) \
|
---|
55 | if ( errorp ) std::cerr << x << std::endl;
|
---|
56 |
|
---|
57 | static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
|
---|
58 | static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
|
---|
59 |
|
---|
60 | bool
|
---|
61 | astp = false,
|
---|
62 | bresolvep = false,
|
---|
63 | ctorinitp = false,
|
---|
64 | exprp = false,
|
---|
65 | expraltp = false,
|
---|
66 | grammarp = false,
|
---|
67 | libcfap = false,
|
---|
68 | nopreludep = false,
|
---|
69 | noprotop = false,
|
---|
70 | parsep = false,
|
---|
71 | resolvep = false, // used in AlternativeFinder
|
---|
72 | symtabp = false,
|
---|
73 | treep = false,
|
---|
74 | validp = false,
|
---|
75 | errorp = false,
|
---|
76 | codegenp = false;
|
---|
77 |
|
---|
78 | enum { Ast, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
|
---|
79 |
|
---|
80 | static struct option long_opts[] = {
|
---|
81 | { "ast", no_argument, 0, Ast },
|
---|
82 | { "before-resolver", no_argument, 0, Bresolver },
|
---|
83 | { "ctorinitfix", no_argument, 0, CtorInitFix },
|
---|
84 | { "expr", no_argument, 0, Expr },
|
---|
85 | { "expralt", no_argument, 0, ExprAlt },
|
---|
86 | { "grammar", no_argument, 0, Grammar },
|
---|
87 | { "libcfa", no_argument, 0, LibCFA },
|
---|
88 | { "no-preamble", no_argument, 0, Nopreamble },
|
---|
89 | { "parse", no_argument, 0, Parse },
|
---|
90 | { "no-prototypes", no_argument, 0, Prototypes },
|
---|
91 | { "resolver", no_argument, 0, Resolver },
|
---|
92 | { "symbol", no_argument, 0, Symbol },
|
---|
93 | { "tree", no_argument, 0, Tree },
|
---|
94 | { "validate", no_argument, 0, Validate },
|
---|
95 | { 0, 0, 0, 0 }
|
---|
96 | };
|
---|
97 |
|
---|
98 | int main( int argc, char *argv[] ) {
|
---|
99 | FILE *input;
|
---|
100 | std::ostream *output = &std::cout;
|
---|
101 | int long_index;
|
---|
102 | std::list< Declaration* > translationUnit;
|
---|
103 |
|
---|
104 | opterr = 0; // prevent getopt from printing error messages
|
---|
105 |
|
---|
106 | int c;
|
---|
107 | while ( (c = getopt_long( argc, argv, "abcefFglnpqrstvyzD:", long_opts, &long_index )) != -1 ) {
|
---|
108 | switch ( c ) {
|
---|
109 | case Ast:
|
---|
110 | case 'a': // dump AST
|
---|
111 | astp = true;
|
---|
112 | break;
|
---|
113 | case Bresolver:
|
---|
114 | case 'b': // print before resolver steps
|
---|
115 | bresolvep = true;
|
---|
116 | break;
|
---|
117 | case CtorInitFix:
|
---|
118 | case 'c':
|
---|
119 | ctorinitp = true;
|
---|
120 | break;
|
---|
121 | case Expr:
|
---|
122 | case 'e': // dump AST after expression analysis
|
---|
123 | exprp = true;
|
---|
124 | break;
|
---|
125 | case ExprAlt:
|
---|
126 | case 'f': // print alternatives for expressions
|
---|
127 | expraltp = true;
|
---|
128 | break;
|
---|
129 | case Grammar:
|
---|
130 | case 'g': // bison debugging info (grammar rules)
|
---|
131 | grammarp = true;
|
---|
132 | break;
|
---|
133 | case LibCFA:
|
---|
134 | case 'l': // generate libcfa.c
|
---|
135 | libcfap = true;
|
---|
136 | break;
|
---|
137 | case Nopreamble:
|
---|
138 | case 'n': // do not read preamble
|
---|
139 | nopreludep = true;
|
---|
140 | break;
|
---|
141 | case Prototypes:
|
---|
142 | case 'p': // generate prototypes for preamble functions
|
---|
143 | noprotop = true;
|
---|
144 | break;
|
---|
145 | case Parse:
|
---|
146 | case 'q': // dump parse tree
|
---|
147 | parsep = true;
|
---|
148 | break;
|
---|
149 | case Resolver:
|
---|
150 | case 'r': // print resolver steps
|
---|
151 | resolvep = true;
|
---|
152 | break;
|
---|
153 | case Symbol:
|
---|
154 | case 's': // print symbol table events
|
---|
155 | symtabp = true;
|
---|
156 | break;
|
---|
157 | case Tree:
|
---|
158 | case 't': // build in tree
|
---|
159 | treep = true;
|
---|
160 | break;
|
---|
161 | case 'v': // dump AST after decl validation pass
|
---|
162 | validp = true;
|
---|
163 | break;
|
---|
164 | case 'y':
|
---|
165 | errorp = true;
|
---|
166 | break;
|
---|
167 | case 'z':
|
---|
168 | codegenp = true;
|
---|
169 | break;
|
---|
170 | case 'D': // ignore -Dxxx
|
---|
171 | break;
|
---|
172 | case '?':
|
---|
173 | cout << "Unknown option: '" << (char)optopt << "'" << endl;
|
---|
174 | exit(1);
|
---|
175 | default:
|
---|
176 | abort();
|
---|
177 | } // switch
|
---|
178 | } // while
|
---|
179 |
|
---|
180 | try {
|
---|
181 | // choose to read the program from a file or stdin
|
---|
182 | if ( optind < argc ) {
|
---|
183 | input = fopen( argv[ optind ], "r" );
|
---|
184 | if ( ! input ) {
|
---|
185 | std::cout << "Error: can't open " << argv[optind] << std::endl;
|
---|
186 | exit( 1 );
|
---|
187 | } // if
|
---|
188 | optind += 1;
|
---|
189 | } else {
|
---|
190 | input = stdin;
|
---|
191 | } // if
|
---|
192 |
|
---|
193 | if ( optind < argc ) {
|
---|
194 | output = new ofstream( argv[ optind ] );
|
---|
195 | } // if
|
---|
196 |
|
---|
197 | Parser::get_parser().set_debug( grammarp );
|
---|
198 |
|
---|
199 | // read in the builtins and the prelude
|
---|
200 | if ( ! nopreludep ) { // include gcc builtins
|
---|
201 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
|
---|
202 | FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
|
---|
203 | if ( builtins == NULL ) {
|
---|
204 | std::cerr << "Error: can't open builtins.cf" << std::endl;
|
---|
205 | exit( 1 );
|
---|
206 | } // if
|
---|
207 |
|
---|
208 | parse( builtins, LinkageSpec::Compiler );
|
---|
209 |
|
---|
210 | if ( ! libcfap ) {
|
---|
211 | // read the prelude in, if not generating the cfa library
|
---|
212 | FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
|
---|
213 | if ( prelude == NULL ) {
|
---|
214 | std::cerr << "Error: can't open prelude.cf" << std::endl;
|
---|
215 | exit( 1 );
|
---|
216 | } // if
|
---|
217 |
|
---|
218 | parse( prelude, LinkageSpec::Intrinsic );
|
---|
219 | } // if
|
---|
220 | } // if
|
---|
221 |
|
---|
222 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
|
---|
223 |
|
---|
224 | if ( parsep ) {
|
---|
225 | Parser::get_parser().get_parseTree()->printList( std::cout );
|
---|
226 | Parser::get_parser().freeTree();
|
---|
227 | return 0;
|
---|
228 | } // if
|
---|
229 |
|
---|
230 | buildList( Parser::get_parser().get_parseTree(), translationUnit );
|
---|
231 |
|
---|
232 | Parser::get_parser().freeTree();
|
---|
233 | if ( astp ) {
|
---|
234 | dump( translationUnit );
|
---|
235 | return 0;
|
---|
236 | } // if
|
---|
237 |
|
---|
238 | // add the assignment statement after the initialization of a type parameter
|
---|
239 | OPTPRINT( "validate" )
|
---|
240 | SymTab::validate( translationUnit, symtabp );
|
---|
241 | if ( symtabp ) {
|
---|
242 | return 0;
|
---|
243 | } // if
|
---|
244 |
|
---|
245 | if ( expraltp ) {
|
---|
246 | ResolvExpr::AlternativePrinter printer( std::cout );
|
---|
247 | acceptAll( translationUnit, printer );
|
---|
248 | return 0;
|
---|
249 | } // if
|
---|
250 |
|
---|
251 | if ( validp ) {
|
---|
252 | dump( translationUnit );
|
---|
253 | return 0;
|
---|
254 | } // if
|
---|
255 |
|
---|
256 | OPTPRINT( "mutate" )
|
---|
257 | ControlStruct::mutate( translationUnit );
|
---|
258 | OPTPRINT( "fixNames" )
|
---|
259 | CodeGen::fixNames( translationUnit );
|
---|
260 | OPTPRINT( "tweakInit" )
|
---|
261 | InitTweak::tweak( translationUnit );
|
---|
262 |
|
---|
263 | if ( libcfap ) {
|
---|
264 | // generate the bodies of cfa library functions
|
---|
265 | LibCfa::makeLibCfa( translationUnit );
|
---|
266 | } // if
|
---|
267 |
|
---|
268 | if ( bresolvep ) {
|
---|
269 | dump( translationUnit );
|
---|
270 | return 0;
|
---|
271 | } // if
|
---|
272 |
|
---|
273 | OPTPRINT( "resolve" )
|
---|
274 | ResolvExpr::resolve( translationUnit );
|
---|
275 | if ( exprp ) {
|
---|
276 | dump( translationUnit );
|
---|
277 | return 0;
|
---|
278 | }
|
---|
279 |
|
---|
280 | OPTPRINT( "fixInit" )
|
---|
281 | // fix ObjectDecl - replaces ConstructorInit nodes
|
---|
282 | InitTweak::fix( translationUnit );
|
---|
283 | if ( ctorinitp ) {
|
---|
284 | dump ( translationUnit );
|
---|
285 | return 0;
|
---|
286 | }
|
---|
287 |
|
---|
288 | OPTPRINT( "copyParams" );
|
---|
289 | GenPoly::copyParams( translationUnit );
|
---|
290 | OPTPRINT( "convertSpecializations" )
|
---|
291 | GenPoly::convertSpecializations( translationUnit );
|
---|
292 | OPTPRINT( "convertLvalue" )
|
---|
293 | GenPoly::convertLvalue( translationUnit );
|
---|
294 | OPTPRINT( "box" )
|
---|
295 | GenPoly::box( translationUnit );
|
---|
296 |
|
---|
297 | // print tree right before code generation
|
---|
298 | if ( codegenp ) {
|
---|
299 | dump( translationUnit );
|
---|
300 | return 0;
|
---|
301 | } // if
|
---|
302 |
|
---|
303 | CodeGen::generate( translationUnit, *output, ! noprotop );
|
---|
304 |
|
---|
305 | if ( output != &std::cout ) {
|
---|
306 | delete output;
|
---|
307 | } // if
|
---|
308 | } catch ( SemanticError &e ) {
|
---|
309 | if ( errorp ) {
|
---|
310 | std::cerr << "---AST at error:---" << std::endl;
|
---|
311 | dump( translationUnit, std::cerr );
|
---|
312 | std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
|
---|
313 | }
|
---|
314 | e.print( std::cerr );
|
---|
315 | if ( output != &std::cout ) {
|
---|
316 | delete output;
|
---|
317 | } // if
|
---|
318 | return 1;
|
---|
319 | } catch ( UnimplementedError &e ) {
|
---|
320 | std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
|
---|
321 | if ( output != &std::cout ) {
|
---|
322 | delete output;
|
---|
323 | } // if
|
---|
324 | return 1;
|
---|
325 | } catch ( CompilerError &e ) {
|
---|
326 | std::cerr << "Compiler Error: " << e.get_what() << std::endl;
|
---|
327 | std::cerr << "(please report bugs to " << std::endl;
|
---|
328 | if ( output != &std::cout ) {
|
---|
329 | delete output;
|
---|
330 | } // if
|
---|
331 | return 1;
|
---|
332 | } // try
|
---|
333 |
|
---|
334 | deleteAll( translationUnit );
|
---|
335 | return 0;
|
---|
336 | } // main
|
---|
337 |
|
---|
338 | static void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
|
---|
339 | Parser::get_parser().set_linkage( linkage );
|
---|
340 | Parser::get_parser().parse( input );
|
---|
341 |
|
---|
342 | fclose( input );
|
---|
343 | if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
|
---|
344 | exit( Parser::get_parser().get_parseStatus() );
|
---|
345 | } // if
|
---|
346 | }
|
---|
347 |
|
---|
348 | static bool notPrelude( Declaration * decl ) {
|
---|
349 | return ! LinkageSpec::isBuiltin( decl->get_linkage() );
|
---|
350 | }
|
---|
351 |
|
---|
352 | static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
|
---|
353 | std::list< Declaration * > decls;
|
---|
354 | if ( noprotop ) {
|
---|
355 | filter( translationUnit.begin(), translationUnit.end(),
|
---|
356 | std::back_inserter( decls ), notPrelude );
|
---|
357 | } else {
|
---|
358 | decls = translationUnit;
|
---|
359 | }
|
---|
360 |
|
---|
361 | printAll( decls, out );
|
---|
362 | deleteAll( translationUnit );
|
---|
363 | }
|
---|
364 |
|
---|
365 |
|
---|
366 | // Local Variables: //
|
---|
367 | // tab-width: 4 //
|
---|
368 | // mode: c++ //
|
---|
369 | // compile-command: "make install" //
|
---|
370 | // End: //
|
---|