source: src/main.cc@ 0da3e2c

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 0da3e2c was 0da3e2c, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

more refactoring of parser code

  • Property mode set to 100644
File size: 11.0 KB
Line 
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 : Tue Aug 16 17:47:52 2016
13// Update Count : 252
14//
15
16#include <iostream>
17#include <fstream>
18#include <getopt.h>
19#include "Parser/lex.h"
20#include "Parser/parser.h"
21#include "Parser/TypedefTable.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Visitor.h"
24#include "GenPoly/Lvalue.h"
25#include "GenPoly/Specialize.h"
26#include "GenPoly/Box.h"
27#include "GenPoly/CopyParams.h"
28#include "GenPoly/InstantiateGeneric.h"
29#include "CodeGen/Generate.h"
30#include "CodeGen/FixNames.h"
31#include "ControlStruct/Mutate.h"
32#include "Tuples/Mutate.h"
33#include "Tuples/FunctionChecker.h"
34#include "SymTab/Mangler.h"
35#include "SymTab/Indexer.h"
36#include "SymTab/Validate.h"
37#include "ResolvExpr/AlternativePrinter.h"
38#include "ResolvExpr/Resolver.h"
39#include "MakeLibCfa.h"
40#include "InitTweak/Mutate.h"
41#include "InitTweak/GenInit.h"
42#include "InitTweak/FixInit.h"
43
44#include "Common/SemanticError.h"
45#include "Common/UnimplementedError.h"
46
47#include "../config.h"
48
49using namespace std;
50
51#define OPTPRINT(x) if ( errorp ) std::cerr << x << std::endl;
52
53extern int yydebug;
54
55LinkageSpec::Type linkage = LinkageSpec::Cforall;
56TypedefTable typedefTable;
57DeclarationNode *parseTree = nullptr; // program parse tree
58
59bool
60 astp = false,
61 bresolvep = false,
62 bboxp = 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
78static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
79static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
80static void parse_cmdline( int argc, char *argv[], const char *&filename );
81
82
83int main( int argc, char *argv[] ) {
84 FILE *input;
85 std::ostream *output = &std::cout;
86 std::list< Declaration * > translationUnit;
87 const char *filename = NULL;
88
89 parse_cmdline( argc, argv, filename ); // process command-line arguments
90
91 try {
92 // choose to read the program from a file or stdin
93 if ( optind < argc ) {
94 input = fopen( argv[ optind ], "r" );
95 if ( ! input ) {
96 std::cout << "Error: cannot open " << argv[ optind ] << std::endl;
97 exit( EXIT_FAILURE );
98 } // if
99 // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
100 if ( filename == NULL ) filename = argv[ optind ];
101 // prelude filename comes in differently
102 if ( libcfap ) filename = "prelude.cf";
103 optind += 1;
104 } else {
105 input = stdin;
106 // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
107 // a fake name along
108 if ( filename == NULL ) filename = "stdin";
109 } // if
110
111 if ( optind < argc ) {
112 output = new ofstream( argv[ optind ] );
113 } // if
114
115 yydebug = grammarp;
116
117 // read in the builtins, extras, and the prelude
118 if ( ! nopreludep ) { // include gcc builtins
119 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
120 FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
121 if ( builtins == NULL ) {
122 std::cerr << "Error: cannot open builtins.cf" << std::endl;
123 exit( EXIT_FAILURE );
124 } // if
125 parse( builtins, LinkageSpec::Compiler );
126
127 // read the extra prelude in, if not generating the cfa library
128 FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
129 if ( extras == NULL ) {
130 std::cerr << "Error: cannot open extras.cf" << std::endl;
131 exit( EXIT_FAILURE );
132 } // if
133 parse( extras, LinkageSpec::C );
134
135 if ( ! libcfap ) {
136 // read the prelude in, if not generating the cfa library
137 FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
138 if ( prelude == NULL ) {
139 std::cerr << "Error: cannot open prelude.cf" << std::endl;
140 exit( EXIT_FAILURE );
141 } // if
142
143 parse( prelude, LinkageSpec::Intrinsic );
144 } // if
145 } // if
146
147 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
148
149 if ( parsep ) {
150 parseTree->printList( std::cout );
151 delete parseTree;
152 return 0;
153 } // if
154
155 buildList( parseTree, translationUnit );
156 delete parseTree;
157
158 if ( astp ) {
159 dump( translationUnit );
160 return 0;
161 } // if
162
163 // add the assignment statement after the initialization of a type parameter
164 OPTPRINT( "validate" )
165 SymTab::validate( translationUnit, symtabp );
166 if ( symtabp ) {
167 return 0;
168 } // if
169
170 if ( expraltp ) {
171 ResolvExpr::AlternativePrinter printer( std::cout );
172 acceptAll( translationUnit, printer );
173 return 0;
174 } // if
175
176 if ( validp ) {
177 dump( translationUnit );
178 return 0;
179 } // if
180
181 OPTPRINT( "mutate" )
182 ControlStruct::mutate( translationUnit );
183 OPTPRINT( "fixNames" )
184 CodeGen::fixNames( translationUnit );
185 OPTPRINT( "tweakInit" )
186 InitTweak::genInit( translationUnit );
187
188 if ( libcfap ) {
189 // generate the bodies of cfa library functions
190 LibCfa::makeLibCfa( translationUnit );
191 } // if
192
193 if ( bresolvep ) {
194 dump( translationUnit );
195 return 0;
196 } // if
197
198 OPTPRINT( "resolve" )
199 ResolvExpr::resolve( translationUnit );
200 if ( exprp ) {
201 dump( translationUnit );
202 return 0;
203 }
204
205 // fix ObjectDecl - replaces ConstructorInit nodes
206 OPTPRINT( "fixInit" )
207 InitTweak::fix( translationUnit, filename, libcfap || treep );
208 if ( ctorinitp ) {
209 dump ( translationUnit );
210 return 0;
211 }
212
213 OPTPRINT("instantiateGenerics")
214 GenPoly::instantiateGeneric( translationUnit );
215 OPTPRINT( "copyParams" );
216 GenPoly::copyParams( translationUnit );
217 OPTPRINT( "convertSpecializations" )
218 GenPoly::convertSpecializations( translationUnit );
219 OPTPRINT( "convertLvalue" )
220 GenPoly::convertLvalue( translationUnit );
221
222 if ( bboxp ) {
223 dump( translationUnit );
224 return 0;
225 }
226 OPTPRINT( "box" )
227 GenPoly::box( translationUnit );
228
229 // print tree right before code generation
230 if ( codegenp ) {
231 dump( translationUnit );
232 return 0;
233 } // if
234
235 CodeGen::generate( translationUnit, *output, ! noprotop );
236
237 if ( output != &std::cout ) {
238 delete output;
239 } // if
240 } catch ( SemanticError &e ) {
241 if ( errorp ) {
242 std::cerr << "---AST at error:---" << std::endl;
243 dump( translationUnit, std::cerr );
244 std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
245 }
246 e.print( std::cerr );
247 if ( output != &std::cout ) {
248 delete output;
249 } // if
250 return 1;
251 } catch ( UnimplementedError &e ) {
252 std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
253 if ( output != &std::cout ) {
254 delete output;
255 } // if
256 return 1;
257 } catch ( CompilerError &e ) {
258 std::cerr << "Compiler Error: " << e.get_what() << std::endl;
259 std::cerr << "(please report bugs to " << std::endl;
260 if ( output != &std::cout ) {
261 delete output;
262 } // if
263 return 1;
264 } // try
265
266 deleteAll( translationUnit );
267 return 0;
268} // main
269
270void parse_cmdline( int argc, char *argv[], const char *&filename ) {
271 enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
272
273 static struct option long_opts[] = {
274 { "ast", no_argument, 0, Ast },
275 { "before-box", no_argument, 0, Bbox },
276 { "before-resolver", no_argument, 0, Bresolver },
277 { "ctorinitfix", no_argument, 0, CtorInitFix },
278 { "expr", no_argument, 0, Expr },
279 { "expralt", no_argument, 0, ExprAlt },
280 { "grammar", no_argument, 0, Grammar },
281 { "libcfa", no_argument, 0, LibCFA },
282 { "no-preamble", no_argument, 0, Nopreamble },
283 { "parse", no_argument, 0, Parse },
284 { "no-prototypes", no_argument, 0, Prototypes },
285 { "resolver", no_argument, 0, Resolver },
286 { "symbol", no_argument, 0, Symbol },
287 { "tree", no_argument, 0, Tree },
288 { "validate", no_argument, 0, Validate },
289 { 0, 0, 0, 0 }
290 }; // long_opts
291 int long_index;
292
293 opterr = 0; // (global) prevent getopt from printing error messages
294
295 int c;
296 while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
297 switch ( c ) {
298 case Ast:
299 case 'a': // dump AST
300 astp = true;
301 break;
302 case Bresolver:
303 case 'b': // print before resolver steps
304 bresolvep = true;
305 break;
306 case 'B': // print before resolver steps
307 bboxp = true;
308 break;
309 case CtorInitFix:
310 case 'c':
311 ctorinitp = true;
312 break;
313 case Expr:
314 case 'e': // dump AST after expression analysis
315 exprp = true;
316 break;
317 case ExprAlt:
318 case 'f': // print alternatives for expressions
319 expraltp = true;
320 break;
321 case Grammar:
322 case 'g': // bison debugging info (grammar rules)
323 grammarp = true;
324 break;
325 case LibCFA:
326 case 'l': // generate libcfa.c
327 libcfap = true;
328 break;
329 case Nopreamble:
330 case 'n': // do not read preamble
331 nopreludep = true;
332 break;
333 case Prototypes:
334 case 'p': // generate prototypes for preamble functions
335 noprotop = true;
336 break;
337 case Parse:
338 case 'q': // dump parse tree
339 parsep = true;
340 break;
341 case Resolver:
342 case 'r': // print resolver steps
343 resolvep = true;
344 break;
345 case Symbol:
346 case 's': // print symbol table events
347 symtabp = true;
348 break;
349 case Tree:
350 case 't': // build in tree
351 treep = true;
352 break;
353 case 'v': // dump AST after decl validation pass
354 validp = true;
355 break;
356 case 'y':
357 errorp = true;
358 break;
359 case 'z':
360 codegenp = true;
361 break;
362 case 'D': // ignore -Dxxx
363 break;
364 case 'F': // source file-name without suffix
365 filename = optarg;
366 break;
367 case '?':
368 cout << "Unknown option: '" << (char)optopt << "'" << endl;
369 exit( EXIT_FAILURE );
370 default:
371 abort();
372 } // switch
373 } // while
374} // parse_cmdline
375
376static void parse( FILE * input, LinkageSpec::Type link, bool shouldExit ) {
377 extern int yyparse( void );
378 extern FILE *yyin;
379 extern int yylineno;
380
381 linkage = link; // set globals
382 yyin = input;
383 yylineno = 1;
384 typedefTable.enterScope();
385 int parseStatus = yyparse();
386
387 fclose( input );
388 if ( shouldExit || parseStatus != 0 ) {
389 exit( parseStatus );
390 } // if
391} // parse
392
393static bool notPrelude( Declaration * decl ) {
394 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
395} // notPrelude
396
397static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
398 std::list< Declaration * > decls;
399 if ( noprotop ) {
400 filter( translationUnit.begin(), translationUnit.end(), std::back_inserter( decls ), notPrelude );
401 } else {
402 decls = translationUnit;
403 }
404
405 printAll( decls, out );
406 deleteAll( translationUnit );
407} // dump
408
409// Local Variables: //
410// tab-width: 4 //
411// mode: c++ //
412// compile-command: "make install" //
413// End: //
Note: See TracBrowser for help on using the repository browser.