source: src/main.cc@ 9a92216

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 9a92216 was 9a92216, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

fix filename for library code to make clashes with user code less likely

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