source: src/main.cc@ 242d458

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 242d458 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
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 Jul 5 15:23:11 2016
13// Update Count : 209
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 "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
52using namespace std;
53
54#define OPTPRINT(x) \
55 if ( errorp ) std::cerr << x << std::endl;
56
57static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
58static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
59
60bool
61 astp = false,
62 bresolvep = false,
63 bboxp = false,
64 ctorinitp = false,
65 exprp = false,
66 expraltp = false,
67 grammarp = false,
68 libcfap = false,
69 nopreludep = false,
70 noprotop = false,
71 parsep = false,
72 resolvep = false, // used in AlternativeFinder
73 symtabp = false,
74 treep = false,
75 validp = false,
76 errorp = false,
77 codegenp = false;
78
79enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
80
81static struct option long_opts[] = {
82 { "ast", no_argument, 0, Ast },
83 { "before-box", no_argument, 0, Bbox },
84 { "before-resolver", no_argument, 0, Bresolver },
85 { "ctorinitfix", no_argument, 0, CtorInitFix },
86 { "expr", no_argument, 0, Expr },
87 { "expralt", no_argument, 0, ExprAlt },
88 { "grammar", no_argument, 0, Grammar },
89 { "libcfa", no_argument, 0, LibCFA },
90 { "no-preamble", no_argument, 0, Nopreamble },
91 { "parse", no_argument, 0, Parse },
92 { "no-prototypes", no_argument, 0, Prototypes },
93 { "resolver", no_argument, 0, Resolver },
94 { "symbol", no_argument, 0, Symbol },
95 { "tree", no_argument, 0, Tree },
96 { "validate", no_argument, 0, Validate },
97 { 0, 0, 0, 0 }
98};
99
100int main( int argc, char *argv[] ) {
101 FILE *input;
102 std::ostream *output = &std::cout;
103 int long_index;
104 std::list< Declaration * > translationUnit;
105 const char *filename = NULL;
106
107 opterr = 0; // prevent getopt from printing error messages
108
109 int c;
110 while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
111 switch ( c ) {
112 case Ast:
113 case 'a': // dump AST
114 astp = true;
115 break;
116 case Bresolver:
117 case 'b': // print before resolver steps
118 bresolvep = true;
119 break;
120 case 'B': // print before resolver steps
121 bboxp = true;
122 break;
123 case CtorInitFix:
124 case 'c':
125 ctorinitp = true;
126 break;
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
145 nopreludep = true;
146 break;
147 case Prototypes:
148 case 'p': // generate prototypes for preamble functions
149 noprotop = true;
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;
163 case Tree:
164 case 't': // build in tree
165 treep = true;
166 break;
167 case 'v': // dump AST after decl validation pass
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;
178 case 'F': // source file-name without suffix
179 filename = optarg;
180 break;
181 case '?':
182 cout << "Unknown option: '" << (char)optopt << "'" << endl;
183 exit( EXIT_FAILURE );
184 default:
185 abort();
186 } // switch
187 } // while
188
189 try {
190 // choose to read the program from a file or stdin
191 if ( optind < argc ) {
192 input = fopen( argv[ optind ], "r" );
193 if ( ! input ) {
194 std::cout << "Error: can't open " << argv[ optind ] << std::endl;
195 exit( EXIT_FAILURE );
196 } // if
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 ];
199 // prelude filename comes in differently
200 if ( libcfap ) filename = "prelude.cf";
201 optind += 1;
202 } else {
203 input = stdin;
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";
207 } // if
208
209 if ( optind < argc ) {
210 output = new ofstream( argv[ optind ] );
211 } // if
212
213 Parser::get_parser().set_debug( grammarp );
214
215 // read in the builtins, extras, and the prelude
216 if ( ! nopreludep ) { // include gcc builtins
217 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
218 FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
219 if ( builtins == NULL ) {
220 std::cerr << "Error: can't open builtins.cf" << std::endl;
221 exit( EXIT_FAILURE );
222 } // if
223 parse( builtins, LinkageSpec::Compiler );
224
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
233 if ( ! libcfap ) {
234 // read the prelude in, if not generating the cfa library
235 FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
236 if ( prelude == NULL ) {
237 std::cerr << "Error: can't open prelude.cf" << std::endl;
238 exit( EXIT_FAILURE );
239 } // if
240
241 parse( prelude, LinkageSpec::Intrinsic );
242 } // if
243 } // if
244
245 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
246
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 ) {
257 dump( translationUnit );
258 return 0;
259 } // if
260
261 // add the assignment statement after the initialization of a type parameter
262 OPTPRINT( "validate" )
263 SymTab::validate( translationUnit, symtabp );
264 if ( symtabp ) {
265 return 0;
266 } // if
267
268 if ( expraltp ) {
269 ResolvExpr::AlternativePrinter printer( std::cout );
270 acceptAll( translationUnit, printer );
271 return 0;
272 } // if
273
274 if ( validp ) {
275 dump( translationUnit );
276 return 0;
277 } // if
278
279 OPTPRINT( "mutate" )
280 ControlStruct::mutate( translationUnit );
281 OPTPRINT( "fixNames" )
282 CodeGen::fixNames( translationUnit );
283 OPTPRINT( "tweakInit" )
284 InitTweak::genInit( translationUnit );
285
286 if ( libcfap ) {
287 // generate the bodies of cfa library functions
288 LibCfa::makeLibCfa( translationUnit );
289 } // if
290
291 if ( bresolvep ) {
292 dump( translationUnit );
293 return 0;
294 } // if
295
296 OPTPRINT( "resolve" )
297 ResolvExpr::resolve( translationUnit );
298 if ( exprp ) {
299 dump( translationUnit );
300 return 0;
301 }
302
303 // fix ObjectDecl - replaces ConstructorInit nodes
304 OPTPRINT( "fixInit" )
305 InitTweak::fix( translationUnit, filename, libcfap || treep );
306 if ( ctorinitp ) {
307 dump ( translationUnit );
308 return 0;
309 }
310
311 OPTPRINT( "copyParams" );
312 GenPoly::copyParams( translationUnit );
313 OPTPRINT( "convertSpecializations" )
314 GenPoly::convertSpecializations( translationUnit );
315 OPTPRINT( "convertLvalue" )
316 GenPoly::convertLvalue( translationUnit );
317
318 if ( bboxp ) {
319 dump( translationUnit );
320 return 0;
321 }
322 OPTPRINT( "box" )
323 GenPoly::box( translationUnit );
324
325 // print tree right before code generation
326 if ( codegenp ) {
327 dump( translationUnit );
328 return 0;
329 } // if
330
331 CodeGen::generate( translationUnit, *output, ! noprotop );
332
333 if ( output != &std::cout ) {
334 delete output;
335 } // if
336 } catch ( SemanticError &e ) {
337 if ( errorp ) {
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;
341 }
342 e.print( std::cerr );
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
362 deleteAll( translationUnit );
363 return 0;
364} // main
365
366static void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
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
376static bool notPrelude( Declaration * decl ) {
377 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
378}
379
380static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
381 std::list< Declaration * > decls;
382 if ( noprotop ) {
383 filter( translationUnit.begin(), translationUnit.end(),
384 std::back_inserter( decls ), notPrelude );
385 } else {
386 decls = translationUnit;
387 }
388
389 printAll( decls, out );
390 deleteAll( translationUnit );
391}
392
393
394// Local Variables: //
395// tab-width: 4 //
396// mode: c++ //
397// compile-command: "make install" //
398// End: //
Note: See TracBrowser for help on using the repository browser.