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