source: src/main.cc@ 2e60a1a

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 2e60a1a was ae8b942, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

Merge branch 'master' of plg2:software/cfa/cfa-cc

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