source: src/main.cc@ 907eccb

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 907eccb was 626dbc10, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

major refactoring of specialization code, added code to generate thunks for ttype functions, move specialize pass to before tuple expansion

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