source: src/main.cc@ 4e2a1137

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 4e2a1137 was 6cf27a07, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

reorganize global init so that it is simpler and generates less unnecessary code

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