source: src/main.cc@ 3c13c03

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 3c13c03 was 6eb8948, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

make TupleAssignment generate temporaries, add StmtExpr for GCC statement expressions, expand tuple assignment expressions, collapse SolvedTupleExpr, MassAssignExpr, and MultipleAssignExpr into TupleAssignExpr

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