source: src/main.cc@ b726084

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors 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 b726084 was b726084, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

Merge branch 'master' into tuples

Conflicts:

src/ControlStruct/LabelTypeChecker.cc
src/InitTweak/FixInit.cc
src/ResolvExpr/Resolver.cc
src/Tuples/TupleAssignment.cc
src/Tuples/TupleAssignment.h

  • Property mode set to 100644
File size: 13.8 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 : Sun Oct 30 10:11:38 2016
13// Update Count : 435
14//
15
16#include <iostream>
17#include <fstream>
18#include <signal.h> // signal
19#include <getopt.h> // getopt
20#include <execinfo.h> // backtrace, backtrace_symbols
21#include <cxxabi.h> // __cxa_demangle
22#include <cstring> // index
23
24using namespace std;
25
26#include "Parser/lex.h"
27#include "Parser/parser.h"
28#include "Parser/TypedefTable.h"
29#include "GenPoly/Lvalue.h"
30#include "GenPoly/Specialize.h"
31#include "GenPoly/Box.h"
32#include "GenPoly/CopyParams.h"
33#include "GenPoly/InstantiateGeneric.h"
34#include "CodeGen/Generate.h"
35#include "CodeGen/FixNames.h"
36#include "ControlStruct/Mutate.h"
37#include "SymTab/Validate.h"
38#include "ResolvExpr/AlternativePrinter.h"
39#include "ResolvExpr/Resolver.h"
40#include "MakeLibCfa.h"
41#include "InitTweak/GenInit.h"
42#include "InitTweak/FixInit.h"
43#include "Common/UnimplementedError.h"
44#include "../config.h"
45#include "Tuples/Tuples.h"
46
47using namespace std;
48
49#define OPTPRINT(x) if ( errorp ) cerr << x << endl;
50
51
52LinkageSpec::Spec linkage = LinkageSpec::Cforall;
53TypedefTable typedefTable;
54DeclarationNode * parseTree = nullptr; // program parse tree
55
56extern int yydebug; // set for -g flag (Grammar)
57bool
58 astp = false,
59 bresolvep = false,
60 bboxp = false,
61 ctorinitp = false,
62 exprp = false,
63 expraltp = false,
64 libcfap = false,
65 nopreludep = false,
66 noprotop = false,
67 parsep = false,
68 resolvep = false, // used in AlternativeFinder
69 symtabp = false,
70 treep = false,
71 validp = false,
72 errorp = false,
73 codegenp = false;
74
75static void parse_cmdline( int argc, char *argv[], const char *& filename );
76static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
77static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
78
79static void backtrace( int start ) { // skip first N stack frames
80 enum { Frames = 50 };
81 void * array[Frames];
82 int size = ::backtrace( array, Frames );
83 char ** messages = ::backtrace_symbols( array, size ); // does not demangle names
84
85 *index( messages[0], '(' ) = '\0'; // find executable name
86 cerr << "Stack back trace for: " << messages[0] << endl;
87
88 // skip last 2 stack frames after main
89 for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
90 char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
91 for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset
92 if ( *p == '(' ) {
93 mangled_name = p;
94 } else if ( *p == '+' ) {
95 offset_begin = p;
96 } else if ( *p == ')' ) {
97 offset_end = p;
98 break;
99 } // if
100 } // for
101
102 // if line contains symbol, attempt to demangle
103 int frameNo = i - start;
104 if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
105 *mangled_name++ = '\0'; // delimit strings
106 *offset_begin++ = '\0';
107 *offset_end++ = '\0';
108
109 int status;
110 char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
111 // bug in __cxa_demangle for single-character lower-case non-mangled names
112 if ( status == 0 ) { // demangling successful ?
113 cerr << "(" << frameNo << ") " << messages[i] << " : "
114 << real_name << "+" << offset_begin << offset_end << endl;
115 } else { // otherwise, output mangled name
116 cerr << "(" << frameNo << ") " << messages[i] << " : "
117 << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl;
118 } // if
119
120 free( real_name );
121 } else { // otherwise, print the whole line
122 cerr << "(" << frameNo << ") " << messages[i] << endl;
123 } // if
124 } // for
125
126 free( messages );
127} // backtrace
128
129void sigSegvBusHandler( int sig_num ) {
130 cerr << "*CFA runtime error* program cfa-cpp terminated with "
131 << (sig_num == SIGSEGV ? "segment fault" : "bus error")
132 << "." << endl;
133 backtrace( 2 ); // skip first 2 stack frames
134 exit( EXIT_FAILURE );
135} // sigSegvBusHandler
136
137void sigAbortHandler( int sig_num ) {
138 backtrace( 6 ); // skip first 6 stack frames
139 signal( SIGABRT, SIG_DFL); // reset default signal handler
140 raise( SIGABRT ); // reraise SIGABRT
141} // sigAbortHandler
142
143
144int main( int argc, char * argv[] ) {
145 FILE * input; // use FILE rather than istream because yyin is FILE
146 ostream *output = & cout;
147 const char *filename = nullptr;
148 list< Declaration * > translationUnit;
149
150 signal( SIGSEGV, sigSegvBusHandler );
151 signal( SIGBUS, sigSegvBusHandler );
152 signal( SIGABRT, sigAbortHandler );
153
154 parse_cmdline( argc, argv, filename ); // process command-line arguments
155
156 try {
157 // choose to read the program from a file or stdin
158 if ( optind < argc ) { // any commands after the flags ? => input file name
159 input = fopen( argv[ optind ], "r" );
160 assertf( input, "cannot open %s\n", argv[ optind ] );
161 // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
162 if ( filename == nullptr ) filename = argv[ optind ];
163 // prelude filename comes in differently
164 if ( libcfap ) filename = "prelude.cf";
165 optind += 1;
166 } else { // no input file name
167 input = stdin;
168 // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
169 // a fake name along
170 if ( filename == nullptr ) filename = "stdin";
171 } // if
172
173 if ( optind < argc ) { // any commands after the flags and input file ? => output file name
174 output = new ofstream( argv[ optind ] );
175 } // if
176
177 // read in the builtins, extras, and the prelude
178 if ( ! nopreludep ) { // include gcc builtins
179 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
180 FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
181 assertf( builtins, "cannot open builtins.cf\n" );
182 parse( builtins, LinkageSpec::Compiler );
183
184 // read the extra prelude in, if not generating the cfa library
185 FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
186 assertf( extras, "cannot open extras.cf\n" );
187 parse( extras, LinkageSpec::C );
188
189 if ( ! libcfap ) {
190 // read the prelude in, if not generating the cfa library
191 FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
192 assertf( prelude, "cannot open prelude.cf\n" );
193 parse( prelude, LinkageSpec::Intrinsic );
194 } // if
195 } // if
196
197 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
198
199 if ( parsep ) {
200 parseTree->printList( cout );
201 delete parseTree;
202 return 0;
203 } // if
204
205 buildList( parseTree, translationUnit );
206 delete parseTree;
207 parseTree = nullptr;
208
209 if ( astp ) {
210 dump( translationUnit );
211 return 0;
212 } // if
213
214 // add the assignment statement after the initialization of a type parameter
215 OPTPRINT( "validate" )
216 SymTab::validate( translationUnit, symtabp );
217 if ( symtabp ) {
218 deleteAll( translationUnit );
219 return 0;
220 } // if
221
222 if ( expraltp ) {
223 ResolvExpr::AlternativePrinter printer( cout );
224 acceptAll( translationUnit, printer );
225 return 0;
226 } // if
227
228 if ( validp ) {
229 dump( translationUnit );
230 return 0;
231 } // if
232
233 OPTPRINT( "mutate" )
234 ControlStruct::mutate( translationUnit );
235 OPTPRINT( "fixNames" )
236 CodeGen::fixNames( translationUnit );
237 OPTPRINT( "tweakInit" )
238 InitTweak::genInit( translationUnit );
239 OPTPRINT( "expandMemberTuples" );
240 Tuples::expandMemberTuples( translationUnit );
241 if ( libcfap ) {
242 // generate the bodies of cfa library functions
243 LibCfa::makeLibCfa( translationUnit );
244 } // if
245
246 if ( bresolvep ) {
247 dump( translationUnit );
248 return 0;
249 } // if
250
251 OPTPRINT( "resolve" )
252 ResolvExpr::resolve( translationUnit );
253 if ( exprp ) {
254 dump( translationUnit );
255 return 0;
256 } // if
257
258 // fix ObjectDecl - replaces ConstructorInit nodes
259 OPTPRINT( "fixInit" )
260 InitTweak::fix( translationUnit, filename, libcfap || treep );
261 if ( ctorinitp ) {
262 dump ( translationUnit );
263 return 0;
264 } // if
265
266 OPTPRINT( "expandUniqueExpr" ); // xxx - is this the right place for this? want to expand ASAP so that subsequent passes don't need to worry about double-visiting a unique expr - needs to go after InitTweak::fix so that copy constructed return declarations are reused
267 Tuples::expandUniqueExpr( translationUnit );
268
269 OPTPRINT("instantiateGenerics")
270 GenPoly::instantiateGeneric( translationUnit );
271 OPTPRINT( "copyParams" );
272 GenPoly::copyParams( translationUnit );
273 OPTPRINT( "convertSpecializations" )
274 GenPoly::convertSpecializations( translationUnit );
275 OPTPRINT( "convertLvalue" )
276 GenPoly::convertLvalue( translationUnit );
277
278 if ( bboxp ) {
279 dump( translationUnit );
280 return 0;
281 } // if
282 OPTPRINT( "box" )
283 GenPoly::box( translationUnit );
284 OPTPRINT( "expandTuples" ); // xxx - is this the right place for this?
285 Tuples::expandTuples( translationUnit );
286
287 // print tree right before code generation
288 if ( codegenp ) {
289 dump( translationUnit );
290 return 0;
291 } // if
292
293 CodeGen::generate( translationUnit, *output, ! noprotop );
294
295 if ( output != &cout ) {
296 delete output;
297 } // if
298 } catch ( SemanticError &e ) {
299 if ( errorp ) {
300 cerr << "---AST at error:---" << endl;
301 dump( translationUnit, cerr );
302 cerr << endl << "---End of AST, begin error message:---\n" << endl;
303 } // if
304 e.print( cerr );
305 if ( output != &cout ) {
306 delete output;
307 } // if
308 return 1;
309 } catch ( UnimplementedError &e ) {
310 cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
311 if ( output != &cout ) {
312 delete output;
313 } // if
314 return 1;
315 } catch ( CompilerError &e ) {
316 cerr << "Compiler Error: " << e.get_what() << endl;
317 cerr << "(please report bugs to " << endl;
318 if ( output != &cout ) {
319 delete output;
320 } // if
321 return 1;
322 } // try
323
324 deleteAll( translationUnit );
325 return 0;
326} // main
327
328void parse_cmdline( int argc, char * argv[], const char *& filename ) {
329 enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
330
331 static struct option long_opts[] = {
332 { "ast", no_argument, 0, Ast },
333 { "before-box", no_argument, 0, Bbox },
334 { "before-resolver", no_argument, 0, Bresolver },
335 { "ctorinitfix", no_argument, 0, CtorInitFix },
336 { "expr", no_argument, 0, Expr },
337 { "expralt", no_argument, 0, ExprAlt },
338 { "grammar", no_argument, 0, Grammar },
339 { "libcfa", no_argument, 0, LibCFA },
340 { "no-preamble", no_argument, 0, Nopreamble },
341 { "parse", no_argument, 0, Parse },
342 { "no-prototypes", no_argument, 0, Prototypes },
343 { "resolver", no_argument, 0, Resolver },
344 { "symbol", no_argument, 0, Symbol },
345 { "tree", no_argument, 0, Tree },
346 { "validate", no_argument, 0, Validate },
347 { 0, 0, 0, 0 }
348 }; // long_opts
349 int long_index;
350
351 opterr = 0; // (global) prevent getopt from printing error messages
352
353 int c;
354 while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
355 switch ( c ) {
356 case Ast:
357 case 'a': // dump AST
358 astp = true;
359 break;
360 case Bresolver:
361 case 'b': // print before resolver steps
362 bresolvep = true;
363 break;
364 case 'B': // print before resolver steps
365 bboxp = true;
366 break;
367 case CtorInitFix:
368 case 'c':
369 ctorinitp = true;
370 break;
371 case Expr:
372 case 'e': // dump AST after expression analysis
373 exprp = true;
374 break;
375 case ExprAlt:
376 case 'f': // print alternatives for expressions
377 expraltp = true;
378 break;
379 case Grammar:
380 case 'g': // bison debugging info (grammar rules)
381 yydebug = true;
382 break;
383 case LibCFA:
384 case 'l': // generate libcfa.c
385 libcfap = true;
386 break;
387 case Nopreamble:
388 case 'n': // do not read preamble
389 nopreludep = true;
390 break;
391 case Prototypes:
392 case 'p': // generate prototypes for preamble functions
393 noprotop = true;
394 break;
395 case Parse:
396 case 'q': // dump parse tree
397 parsep = true;
398 break;
399 case Resolver:
400 case 'r': // print resolver steps
401 resolvep = true;
402 break;
403 case Symbol:
404 case 's': // print symbol table events
405 symtabp = true;
406 break;
407 case Tree:
408 case 't': // build in tree
409 treep = true;
410 break;
411 case 'v': // dump AST after decl validation pass
412 validp = true;
413 break;
414 case 'y':
415 errorp = true;
416 break;
417 case 'z':
418 codegenp = true;
419 break;
420 case 'D': // ignore -Dxxx
421 break;
422 case 'F': // source file-name without suffix
423 filename = optarg;
424 break;
425 case '?':
426 assertf( false, "Unknown option: '%c'\n", (char)optopt );
427 default:
428 abort();
429 } // switch
430 } // while
431} // parse_cmdline
432
433static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
434 extern int yyparse( void );
435 extern FILE * yyin;
436 extern int yylineno;
437
438 ::linkage = linkage; // set globals
439 yyin = input;
440 yylineno = 1;
441 typedefTable.enterScope();
442 int parseStatus = yyparse();
443
444 fclose( input );
445 if ( shouldExit || parseStatus != 0 ) {
446 exit( parseStatus );
447 } // if
448} // parse
449
450static bool notPrelude( Declaration * decl ) {
451 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
452} // notPrelude
453
454static void dump( list< Declaration * > & translationUnit, ostream & out ) {
455 list< Declaration * > decls;
456
457 if ( noprotop ) {
458 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
459 } else {
460 decls = translationUnit;
461 } // if
462
463 printAll( decls, out );
464 deleteAll( translationUnit );
465} // dump
466
467// Local Variables: //
468// tab-width: 4 //
469// mode: c++ //
470// compile-command: "make install" //
471// End: //
Note: See TracBrowser for help on using the repository browser.