source: src/main.cc@ b5c5684

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since b5c5684 was 81419b5, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

refactored main

  • Property mode set to 100644
File size: 7.9 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 : Rob Schluntz
12// Last Modified On : Tue Jun 09 15:10:05 2015
13// Update Count : 68
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/RemoveInit.h"
43//#include "Explain/GenProlog.h"
44//#include "Try/Visit.h"
45
46#include "SemanticError.h"
47#include "UnimplementedError.h"
48
49#include "../config.h"
50
51using namespace std;
52
53#define OPTPRINT(x) \
54 if ( errorp ) std::cerr << x << std::endl;
55
56void parse(FILE * input, LinkageSpec::Type t, bool shouldExit = false );
57
58bool
59 astp = false,
60 exprp = false,
61 expraltp = false,
62 grammarp = false,
63 libcfap = false,
64 resolvep = false, // used in AlternativeFinder
65 symtabp = false,
66 parsep = false,
67 validp = false,
68 preludep = true,
69 protop = false,
70 codegenp = false,
71 errorp = false;
72
73enum { Ast, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Prototypes, Resolver, Symbol, Parse, };
74
75static struct option long_opts[] = {
76 { "ast", no_argument, 0, Ast },
77 { "expr", no_argument, 0, Expr },
78 { "expralt", no_argument, 0, ExprAlt },
79 { "grammar", no_argument, 0, Grammar },
80 { "libcfa", no_argument, 0, LibCFA },
81 { "nopreamble", no_argument, 0, Nopreamble },
82 { "prototypes", no_argument, 0, Prototypes },
83 { "resolver", no_argument, 0, Resolver },
84 { "symbol", no_argument, 0, Symbol },
85 { "parse", no_argument, 0, Parse },
86 { 0, 0, 0, 0 }
87};
88
89int main( int argc, char *argv[] ) {
90 FILE *input;
91 std::ostream *output = &std::cout;
92 int long_index;
93 std::list< Declaration* > translationUnit;
94
95 opterr = 0; // prevent getopt from printing error messages
96
97 int c;
98 while ( (c = getopt_long( argc, argv, "aefglnpqrsxyzD:", long_opts, &long_index )) != -1 ) {
99 switch ( c ) {
100 case Ast:
101 case 'a': // dump AST
102 astp = true;
103 break;
104 case Expr:
105 case 'e': // dump AST after expression analysis
106 exprp = true;
107 break;
108 case ExprAlt:
109 case 'f': // print alternatives for expressions
110 expraltp = true;
111 break;
112 case Grammar:
113 case 'g': // bison debugging info (grammar rules)
114 grammarp = true;
115 break;
116 case LibCFA:
117 case 'l': // generate libcfa.c
118 libcfap = true;
119 break;
120 case Nopreamble:
121 case 'n': // do not read preamble
122 preludep = false;
123 break;
124 case Prototypes:
125 case 'p': // generate prototypes for preamble functions
126 protop = true;
127 break;
128 case Parse:
129 case 'q': // dump parse tree
130 parsep = true;
131 break;
132 case Resolver:
133 case 'r': // print resolver steps
134 resolvep = true;
135 break;
136 case Symbol:
137 case 's': // print symbol table events
138 symtabp = true;
139 break;
140 case 'x': // dump AST after decl validation pass
141 validp = true;
142 break;
143 case 'y':
144 errorp = true;
145 break;
146 case 'z':
147 codegenp = true;
148 break;
149 case 'D': // ignore -Dxxx
150 break;
151 case '?':
152 cout << "Unknown option: '" << (char)optopt << "'" << endl;
153 exit(1);
154 default:
155 abort();
156 } // switch
157 } // while
158
159 try {
160 // choose to read the program from a file or stdin
161 if ( optind < argc ) {
162 input = fopen( argv[ optind ], "r" );
163 if ( ! input ) {
164 std::cout << "Error: can't open " << argv[optind] << std::endl;
165 exit( 1 );
166 } // if
167 optind += 1;
168 } else {
169 input = stdin;
170 } // if
171
172 if ( optind < argc ) {
173 output = new ofstream( argv[ optind ] );
174 } // if
175
176 Parser::get_parser().set_debug( grammarp );
177
178 // read in the builtins and the prelude
179 if ( preludep ) { // include gcc builtins
180 FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
181 if ( builtins == NULL ) {
182 std::cerr << "Error: can't open builtins" << std::endl;
183 exit( 1 );
184 } // if
185
186 parse( builtins, LinkageSpec::Compiler );
187
188 if ( ! libcfap ) {
189 // read the prelude in, if we're not generating the cfa library
190 FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
191 if ( prelude == NULL ) {
192 std::cerr << "Error: can't open prelude" << std::endl;
193 exit( 1 );
194 } // if
195
196 parse( prelude, LinkageSpec::Intrinsic );
197 } // if
198 } // if
199
200 if ( libcfap ) {
201 parse( input, LinkageSpec::Intrinsic );
202 } else {
203 parse( input, LinkageSpec::Cforall, grammarp );
204 }
205
206 if ( parsep ) {
207 Parser::get_parser().get_parseTree()->printList( std::cout );
208 Parser::get_parser().freeTree();
209 return 0;
210 } // if
211
212 buildList( Parser::get_parser().get_parseTree(), translationUnit );
213
214 Parser::get_parser().freeTree();
215 if ( astp ) {
216 printAll( translationUnit, std::cout );
217 return 0;
218 } // if
219
220 // add the assignment statement after the
221 // initialization of a type parameter
222 OPTPRINT( "tweak" )
223 InitTweak::tweak( translationUnit );
224 OPTPRINT( "validate" )
225 SymTab::validate( translationUnit, symtabp );
226 if ( symtabp ) {
227 return 0;
228 } // if
229
230 if ( expraltp ) {
231 ResolvExpr::AlternativePrinter printer( std::cout );
232 acceptAll( translationUnit, printer );
233 return 0;
234 } // if
235
236 if ( validp ) {
237 printAll( translationUnit, std::cout );
238 return 0;
239 } // if
240
241 OPTPRINT( "mutate" )
242 ControlStruct::mutate( translationUnit );
243 OPTPRINT( "fixNames" )
244 CodeGen::fixNames( translationUnit );
245
246 if ( libcfap ) {
247 protop = true;
248 // generate the bodies of cfa library functions
249 LibCfa::makeLibCfa( translationUnit );
250 } // if
251
252 OPTPRINT( "resolve" )
253 ResolvExpr::resolve( translationUnit );
254 if ( exprp ) {
255 printAll( translationUnit, std::cout );
256 }
257
258 OPTPRINT( "copyParams" );
259 GenPoly::copyParams( translationUnit );
260 OPTPRINT( "convertSpecializations" )
261 GenPoly::convertSpecializations( translationUnit );
262 OPTPRINT( "convertLvalue" )
263 GenPoly::convertLvalue( translationUnit );
264 OPTPRINT( "box" )
265 GenPoly::box( translationUnit );
266
267 // print the tree right before code generation
268 if ( codegenp ) {
269 printAll( translationUnit, std::cout );
270 return 0;
271 } // if
272
273 CodeGen::generate( translationUnit, *output, protop );
274
275 if ( output != &std::cout ) {
276 delete output;
277 } // if
278
279 } catch ( SemanticError &e ) {
280 if ( errorp ) {
281 printAll( translationUnit, std::cerr );
282 }
283 e.print( std::cerr );
284 if ( output != &std::cout ) {
285 delete output;
286 } // if
287 return 1;
288 } catch ( UnimplementedError &e ) {
289 std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
290 if ( output != &std::cout ) {
291 delete output;
292 } // if
293 return 1;
294 } catch ( CompilerError &e ) {
295 std::cerr << "Compiler Error: " << e.get_what() << std::endl;
296 std::cerr << "(please report bugs to " << std::endl;
297 if ( output != &std::cout ) {
298 delete output;
299 } // if
300 return 1;
301 } // try
302
303 return 0;
304} // main
305
306void parse(FILE * input, LinkageSpec::Type linkage, bool shouldExit) {
307 Parser::get_parser().set_linkage( linkage );
308 Parser::get_parser().parse( input );
309
310 fclose( input );
311 if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
312 exit( Parser::get_parser().get_parseStatus() );
313 } // if
314}
315
316// Local Variables: //
317// tab-width: 4 //
318// mode: c++ //
319// compile-command: "make install" //
320// End: //
Note: See TracBrowser for help on using the repository browser.