source: src/main.cc@ 893256d

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 with_gc
Last change on this file since 893256d was 1ab4ce2, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

modified -p option to remove the prelude from output whenever this option is on

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