source: src/main.cc@ 29917c6

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 29917c6 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
RevLine 
[b87a5ed]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//
[71f4e4f]7// main.cc --
[b87a5ed]8//
[843054c2]9// Author : Richard C. Bilson
[b87a5ed]10// Created On : Fri May 15 23:12:02 2015
[dd51906]11// Last Modified By : Peter A. Buhr
[0da3e2c]12// Last Modified On : Tue Aug 16 17:47:52 2016
13// Update Count : 252
[b87a5ed]14//
15
[51b73452]16#include <iostream>
17#include <fstream>
[b87a5ed]18#include <getopt.h>
[0da3e2c]19#include "Parser/lex.h"
20#include "Parser/parser.h"
21#include "Parser/TypedefTable.h"
[51b73452]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"
[ea5daeb]28#include "GenPoly/InstantiateGeneric.h"
[51b73452]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"
[a0fdbd5]41#include "InitTweak/GenInit.h"
[71f4e4f]42#include "InitTweak/FixInit.h"
[51b73452]43
[d3b7937]44#include "Common/SemanticError.h"
45#include "Common/UnimplementedError.h"
[51b73452]46
47#include "../config.h"
48
49using namespace std;
50
[0da3e2c]51#define OPTPRINT(x) if ( errorp ) std::cerr << x << std::endl;
[81419b5]52
[0da3e2c]53extern int yydebug;
54
55LinkageSpec::Type linkage = LinkageSpec::Cforall;
56TypedefTable typedefTable;
57DeclarationNode *parseTree = nullptr; // program parse tree
[81419b5]58
[b87a5ed]59bool
60 astp = false,
[de62360d]61 bresolvep = false,
[fea7ca7]62 bboxp = false,
[ca1c11f]63 ctorinitp = false,
[b87a5ed]64 exprp = false,
65 expraltp = false,
66 grammarp = false,
67 libcfap = false,
[de62360d]68 nopreludep = false,
[1ab4ce2]69 noprotop = false,
[de62360d]70 parsep = false,
[b87a5ed]71 resolvep = false, // used in AlternativeFinder
72 symtabp = false,
[d3b7937]73 treep = false,
[b87a5ed]74 validp = false,
[de62360d]75 errorp = false,
76 codegenp = false;
[b87a5ed]77
[0da3e2c]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
[51b73452]82
[8c17ab0]83int main( int argc, char *argv[] ) {
[b87a5ed]84 FILE *input;
85 std::ostream *output = &std::cout;
[7b937575]86 std::list< Declaration * > translationUnit;
[d029162e]87 const char *filename = NULL;
[b87a5ed]88
[0da3e2c]89 parse_cmdline( argc, argv, filename ); // process command-line arguments
[b87a5ed]90
91 try {
[81419b5]92 // choose to read the program from a file or stdin
[b87a5ed]93 if ( optind < argc ) {
94 input = fopen( argv[ optind ], "r" );
95 if ( ! input ) {
[35f9114]96 std::cout << "Error: cannot open " << argv[ optind ] << std::endl;
[dd51906]97 exit( EXIT_FAILURE );
[b87a5ed]98 } // if
[d029162e]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 ];
[37024fd]101 // prelude filename comes in differently
102 if ( libcfap ) filename = "prelude.cf";
[b87a5ed]103 optind += 1;
104 } else {
105 input = stdin;
[2a7e29b]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";
[b87a5ed]109 } // if
110
111 if ( optind < argc ) {
112 output = new ofstream( argv[ optind ] );
113 } // if
[71f4e4f]114
[0da3e2c]115 yydebug = grammarp;
[81419b5]116
[159c62e]117 // read in the builtins, extras, and the prelude
[de62360d]118 if ( ! nopreludep ) { // include gcc builtins
[faf8857]119 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
[d3b7937]120 FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[b87a5ed]121 if ( builtins == NULL ) {
[35f9114]122 std::cerr << "Error: cannot open builtins.cf" << std::endl;
[dd51906]123 exit( EXIT_FAILURE );
[b87a5ed]124 } // if
[81419b5]125 parse( builtins, LinkageSpec::Compiler );
126
[159c62e]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 ) {
[35f9114]130 std::cerr << "Error: cannot open extras.cf" << std::endl;
[159c62e]131 exit( EXIT_FAILURE );
132 } // if
133 parse( extras, LinkageSpec::C );
134
[81419b5]135 if ( ! libcfap ) {
[faf8857]136 // read the prelude in, if not generating the cfa library
[d3b7937]137 FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
[81419b5]138 if ( prelude == NULL ) {
[35f9114]139 std::cerr << "Error: cannot open prelude.cf" << std::endl;
[dd51906]140 exit( EXIT_FAILURE );
[81419b5]141 } // if
[71f4e4f]142
[35304009]143 parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]144 } // if
145 } // if
[81419b5]146
[71f4e4f]147 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
148
[b87a5ed]149 if ( parsep ) {
[0da3e2c]150 parseTree->printList( std::cout );
151 delete parseTree;
[b87a5ed]152 return 0;
153 } // if
154
[0da3e2c]155 buildList( parseTree, translationUnit );
156 delete parseTree;
[b87a5ed]157
158 if ( astp ) {
[1ab4ce2]159 dump( translationUnit );
[b87a5ed]160 return 0;
161 } // if
162
[839ccbb]163 // add the assignment statement after the initialization of a type parameter
[81419b5]164 OPTPRINT( "validate" )
165 SymTab::validate( translationUnit, symtabp );
166 if ( symtabp ) {
[b87a5ed]167 return 0;
168 } // if
169
[81419b5]170 if ( expraltp ) {
171 ResolvExpr::AlternativePrinter printer( std::cout );
172 acceptAll( translationUnit, printer );
[b87a5ed]173 return 0;
174 } // if
175
176 if ( validp ) {
[1ab4ce2]177 dump( translationUnit );
[b87a5ed]178 return 0;
179 } // if
180
[81419b5]181 OPTPRINT( "mutate" )
182 ControlStruct::mutate( translationUnit );
[71f4e4f]183 OPTPRINT( "fixNames" )
[81419b5]184 CodeGen::fixNames( translationUnit );
[f1e012b]185 OPTPRINT( "tweakInit" )
[a0fdbd5]186 InitTweak::genInit( translationUnit );
[b87a5ed]187
[81419b5]188 if ( libcfap ) {
189 // generate the bodies of cfa library functions
190 LibCfa::makeLibCfa( translationUnit );
[b87a5ed]191 } // if
192
[de62360d]193 if ( bresolvep ) {
[1ab4ce2]194 dump( translationUnit );
[de62360d]195 return 0;
196 } // if
197
[81419b5]198 OPTPRINT( "resolve" )
[b87a5ed]199 ResolvExpr::resolve( translationUnit );
[81419b5]200 if ( exprp ) {
[1ab4ce2]201 dump( translationUnit );
[ca1c11f]202 return 0;
[81419b5]203 }
204
[71f4e4f]205 // fix ObjectDecl - replaces ConstructorInit nodes
[6cf27a07]206 OPTPRINT( "fixInit" )
207 InitTweak::fix( translationUnit, filename, libcfap || treep );
[ca1c11f]208 if ( ctorinitp ) {
209 dump ( translationUnit );
210 return 0;
211 }
[71f4e4f]212
[ea5daeb]213 OPTPRINT("instantiateGenerics")
214 GenPoly::instantiateGeneric( translationUnit );
[81419b5]215 OPTPRINT( "copyParams" );
[b87a5ed]216 GenPoly::copyParams( translationUnit );
[81419b5]217 OPTPRINT( "convertSpecializations" )
[698664b3]218 GenPoly::convertSpecializations( translationUnit );
[81419b5]219 OPTPRINT( "convertLvalue" )
[b87a5ed]220 GenPoly::convertLvalue( translationUnit );
[fea7ca7]221
222 if ( bboxp ) {
223 dump( translationUnit );
224 return 0;
225 }
[81419b5]226 OPTPRINT( "box" )
[b87a5ed]227 GenPoly::box( translationUnit );
[81419b5]228
[2871210]229 // print tree right before code generation
[81419b5]230 if ( codegenp ) {
[1ab4ce2]231 dump( translationUnit );
[81419b5]232 return 0;
233 } // if
[b87a5ed]234
[1ab4ce2]235 CodeGen::generate( translationUnit, *output, ! noprotop );
[b87a5ed]236
237 if ( output != &std::cout ) {
238 delete output;
239 } // if
240 } catch ( SemanticError &e ) {
241 if ( errorp ) {
[f77f12e2]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;
[b87a5ed]245 }
[81419b5]246 e.print( std::cerr );
[b87a5ed]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
[39786813]266 deleteAll( translationUnit );
[b87a5ed]267 return 0;
[d9a0e76]268} // main
[51b73452]269
[0da3e2c]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();
[81419b5]386
387 fclose( input );
[0da3e2c]388 if ( shouldExit || parseStatus != 0 ) {
389 exit( parseStatus );
[81419b5]390 } // if
[0da3e2c]391} // parse
[81419b5]392
[1ab4ce2]393static bool notPrelude( Declaration * decl ) {
394 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
[0da3e2c]395} // notPrelude
[1ab4ce2]396
[f77f12e2]397static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
[1ab4ce2]398 std::list< Declaration * > decls;
399 if ( noprotop ) {
[7880579]400 filter( translationUnit.begin(), translationUnit.end(), std::back_inserter( decls ), notPrelude );
[1ab4ce2]401 } else {
402 decls = translationUnit;
403 }
404
[f77f12e2]405 printAll( decls, out );
[7f5566b]406 deleteAll( translationUnit );
[0da3e2c]407} // dump
[1ab4ce2]408
[51b73452]409// Local Variables: //
[b87a5ed]410// tab-width: 4 //
411// mode: c++ //
412// compile-command: "make install" //
[51b73452]413// End: //
Note: See TracBrowser for help on using the repository browser.