source: src/main.cc@ d2ac7d5

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 d2ac7d5 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
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
[0afffee]12// Last Modified On : Sun Oct 30 10:11:38 2016
13// Update Count : 435
[b87a5ed]14//
15
[51b73452]16#include <iostream>
17#include <fstream>
[e6955b1]18#include <signal.h> // signal
19#include <getopt.h> // getopt
[0afffee]20#include <execinfo.h> // backtrace, backtrace_symbols
[e6955b1]21#include <cxxabi.h> // __cxa_demangle
[0afffee]22#include <cstring> // index
[e6955b1]23
24using namespace std;
25
[0da3e2c]26#include "Parser/lex.h"
27#include "Parser/parser.h"
28#include "Parser/TypedefTable.h"
[51b73452]29#include "GenPoly/Lvalue.h"
30#include "GenPoly/Specialize.h"
31#include "GenPoly/Box.h"
32#include "GenPoly/CopyParams.h"
[ea5daeb]33#include "GenPoly/InstantiateGeneric.h"
[51b73452]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"
[a0fdbd5]41#include "InitTweak/GenInit.h"
[71f4e4f]42#include "InitTweak/FixInit.h"
[d3b7937]43#include "Common/UnimplementedError.h"
[51b73452]44#include "../config.h"
[6eb8948]45#include "Tuples/Tuples.h"
[51b73452]46
47using namespace std;
48
[e6955b1]49#define OPTPRINT(x) if ( errorp ) cerr << x << endl;
[81419b5]50
[0da3e2c]51
[8b7ee09]52LinkageSpec::Spec linkage = LinkageSpec::Cforall;
[0da3e2c]53TypedefTable typedefTable;
[cbaee0d]54DeclarationNode * parseTree = nullptr; // program parse tree
[81419b5]55
[926af74]56extern int yydebug; // set for -g flag (Grammar)
[b87a5ed]57bool
58 astp = false,
[de62360d]59 bresolvep = false,
[fea7ca7]60 bboxp = false,
[ca1c11f]61 ctorinitp = false,
[b87a5ed]62 exprp = false,
63 expraltp = false,
64 libcfap = false,
[de62360d]65 nopreludep = false,
[1ab4ce2]66 noprotop = false,
[de62360d]67 parsep = false,
[b87a5ed]68 resolvep = false, // used in AlternativeFinder
69 symtabp = false,
[d3b7937]70 treep = false,
[b87a5ed]71 validp = false,
[de62360d]72 errorp = false,
73 codegenp = false;
[b87a5ed]74
[926af74]75static void parse_cmdline( int argc, char *argv[], const char *& filename );
[8b7ee09]76static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
[e6955b1]77static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
78
[0afffee]79static void backtrace( int start ) { // skip first N stack frames
[e6955b1]80 enum { Frames = 50 };
81 void * array[Frames];
[0afffee]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;
[e6955b1]87
[b542bfb]88 // skip last 2 stack frames after main
89 for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
[e6955b1]90 char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
91 for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset
[0afffee]92 if ( *p == '(' ) {
[46f6134]93 mangled_name = p;
[0afffee]94 } else if ( *p == '+' ) {
[e6955b1]95 offset_begin = p;
[0afffee]96 } else if ( *p == ')' ) {
[e6955b1]97 offset_end = p;
98 break;
99 } // if
100 } // for
101
102 // if line contains symbol, attempt to demangle
[b542bfb]103 int frameNo = i - start;
[e6955b1]104 if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
[0afffee]105 *mangled_name++ = '\0'; // delimit strings
[e6955b1]106 *offset_begin++ = '\0';
107 *offset_end++ = '\0';
108
[0afffee]109 int status;
[e6955b1]110 char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
[0afffee]111 // bug in __cxa_demangle for single-character lower-case non-mangled names
[e6955b1]112 if ( status == 0 ) { // demangling successful ?
[b542bfb]113 cerr << "(" << frameNo << ") " << messages[i] << " : "
[e6955b1]114 << real_name << "+" << offset_begin << offset_end << endl;
115 } else { // otherwise, output mangled name
[b542bfb]116 cerr << "(" << frameNo << ") " << messages[i] << " : "
[0afffee]117 << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl;
[e6955b1]118 } // if
[0afffee]119
[e6955b1]120 free( real_name );
121 } else { // otherwise, print the whole line
[b542bfb]122 cerr << "(" << frameNo << ") " << messages[i] << endl;
[e6955b1]123 } // if
124 } // for
[b542bfb]125
[e6955b1]126 free( messages );
[b542bfb]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")
[0afffee]132 << "." << endl;
[b542bfb]133 backtrace( 2 ); // skip first 2 stack frames
[e6955b1]134 exit( EXIT_FAILURE );
135} // sigSegvBusHandler
[0da3e2c]136
[b542bfb]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
[cbaee0d]144int main( int argc, char * argv[] ) {
[3b8e52c]145 FILE * input; // use FILE rather than istream because yyin is FILE
[e6955b1]146 ostream *output = & cout;
[926af74]147 const char *filename = nullptr;
[e6955b1]148 list< Declaration * > translationUnit;
149
150 signal( SIGSEGV, sigSegvBusHandler );
151 signal( SIGBUS, sigSegvBusHandler );
[b542bfb]152 signal( SIGABRT, sigAbortHandler );
[b87a5ed]153
[0da3e2c]154 parse_cmdline( argc, argv, filename ); // process command-line arguments
[b87a5ed]155
156 try {
[81419b5]157 // choose to read the program from a file or stdin
[3b8e52c]158 if ( optind < argc ) { // any commands after the flags ? => input file name
[b87a5ed]159 input = fopen( argv[ optind ], "r" );
[3b8e52c]160 assertf( input, "cannot open %s\n", argv[ optind ] );
[d029162e]161 // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
[926af74]162 if ( filename == nullptr ) filename = argv[ optind ];
[37024fd]163 // prelude filename comes in differently
164 if ( libcfap ) filename = "prelude.cf";
[b87a5ed]165 optind += 1;
[3b8e52c]166 } else { // no input file name
[b87a5ed]167 input = stdin;
[2a7e29b]168 // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
169 // a fake name along
[926af74]170 if ( filename == nullptr ) filename = "stdin";
[b87a5ed]171 } // if
172
[3b8e52c]173 if ( optind < argc ) { // any commands after the flags and input file ? => output file name
[b87a5ed]174 output = new ofstream( argv[ optind ] );
175 } // if
[71f4e4f]176
[159c62e]177 // read in the builtins, extras, and the prelude
[de62360d]178 if ( ! nopreludep ) { // include gcc builtins
[faf8857]179 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
[d3b7937]180 FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[3b8e52c]181 assertf( builtins, "cannot open builtins.cf\n" );
[81419b5]182 parse( builtins, LinkageSpec::Compiler );
183
[159c62e]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" );
[3b8e52c]186 assertf( extras, "cannot open extras.cf\n" );
[159c62e]187 parse( extras, LinkageSpec::C );
188
[81419b5]189 if ( ! libcfap ) {
[faf8857]190 // read the prelude in, if not generating the cfa library
[d3b7937]191 FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
[3b8e52c]192 assertf( prelude, "cannot open prelude.cf\n" );
[35304009]193 parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]194 } // if
195 } // if
[81419b5]196
[926af74]197 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
[71f4e4f]198
[b87a5ed]199 if ( parsep ) {
[e6955b1]200 parseTree->printList( cout );
[0da3e2c]201 delete parseTree;
[b87a5ed]202 return 0;
203 } // if
204
[0da3e2c]205 buildList( parseTree, translationUnit );
206 delete parseTree;
[cbaee0d]207 parseTree = nullptr;
[b87a5ed]208
209 if ( astp ) {
[1ab4ce2]210 dump( translationUnit );
[b87a5ed]211 return 0;
212 } // if
213
[839ccbb]214 // add the assignment statement after the initialization of a type parameter
[81419b5]215 OPTPRINT( "validate" )
216 SymTab::validate( translationUnit, symtabp );
217 if ( symtabp ) {
[46f6134]218 deleteAll( translationUnit );
[b87a5ed]219 return 0;
220 } // if
221
[81419b5]222 if ( expraltp ) {
[e6955b1]223 ResolvExpr::AlternativePrinter printer( cout );
[81419b5]224 acceptAll( translationUnit, printer );
[b87a5ed]225 return 0;
226 } // if
227
228 if ( validp ) {
[1ab4ce2]229 dump( translationUnit );
[b87a5ed]230 return 0;
231 } // if
232
[81419b5]233 OPTPRINT( "mutate" )
234 ControlStruct::mutate( translationUnit );
[71f4e4f]235 OPTPRINT( "fixNames" )
[81419b5]236 CodeGen::fixNames( translationUnit );
[f1e012b]237 OPTPRINT( "tweakInit" )
[a0fdbd5]238 InitTweak::genInit( translationUnit );
[bf32bb8]239 OPTPRINT( "expandMemberTuples" );
240 Tuples::expandMemberTuples( translationUnit );
[81419b5]241 if ( libcfap ) {
242 // generate the bodies of cfa library functions
243 LibCfa::makeLibCfa( translationUnit );
[b87a5ed]244 } // if
245
[de62360d]246 if ( bresolvep ) {
[1ab4ce2]247 dump( translationUnit );
[de62360d]248 return 0;
249 } // if
250
[81419b5]251 OPTPRINT( "resolve" )
[b87a5ed]252 ResolvExpr::resolve( translationUnit );
[81419b5]253 if ( exprp ) {
[1ab4ce2]254 dump( translationUnit );
[ca1c11f]255 return 0;
[926af74]256 } // if
[81419b5]257
[71f4e4f]258 // fix ObjectDecl - replaces ConstructorInit nodes
[6cf27a07]259 OPTPRINT( "fixInit" )
260 InitTweak::fix( translationUnit, filename, libcfap || treep );
[ca1c11f]261 if ( ctorinitp ) {
262 dump ( translationUnit );
263 return 0;
[926af74]264 } // if
[71f4e4f]265
[141b786]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
[ea5daeb]269 OPTPRINT("instantiateGenerics")
270 GenPoly::instantiateGeneric( translationUnit );
[81419b5]271 OPTPRINT( "copyParams" );
[b87a5ed]272 GenPoly::copyParams( translationUnit );
[81419b5]273 OPTPRINT( "convertSpecializations" )
[698664b3]274 GenPoly::convertSpecializations( translationUnit );
[81419b5]275 OPTPRINT( "convertLvalue" )
[b87a5ed]276 GenPoly::convertLvalue( translationUnit );
[fea7ca7]277
278 if ( bboxp ) {
279 dump( translationUnit );
280 return 0;
[926af74]281 } // if
[81419b5]282 OPTPRINT( "box" )
[b87a5ed]283 GenPoly::box( translationUnit );
[1132b62]284 OPTPRINT( "expandTuples" ); // xxx - is this the right place for this?
285 Tuples::expandTuples( translationUnit );
[81419b5]286
[2871210]287 // print tree right before code generation
[81419b5]288 if ( codegenp ) {
[1ab4ce2]289 dump( translationUnit );
[81419b5]290 return 0;
291 } // if
[b87a5ed]292
[1ab4ce2]293 CodeGen::generate( translationUnit, *output, ! noprotop );
[b87a5ed]294
[e6955b1]295 if ( output != &cout ) {
[b87a5ed]296 delete output;
297 } // if
298 } catch ( SemanticError &e ) {
299 if ( errorp ) {
[e6955b1]300 cerr << "---AST at error:---" << endl;
301 dump( translationUnit, cerr );
302 cerr << endl << "---End of AST, begin error message:---\n" << endl;
[926af74]303 } // if
[e6955b1]304 e.print( cerr );
305 if ( output != &cout ) {
[b87a5ed]306 delete output;
307 } // if
308 return 1;
309 } catch ( UnimplementedError &e ) {
[e6955b1]310 cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
311 if ( output != &cout ) {
[b87a5ed]312 delete output;
313 } // if
314 return 1;
315 } catch ( CompilerError &e ) {
[e6955b1]316 cerr << "Compiler Error: " << e.get_what() << endl;
317 cerr << "(please report bugs to " << endl;
318 if ( output != &cout ) {
[b87a5ed]319 delete output;
320 } // if
321 return 1;
322 } // try
323
[39786813]324 deleteAll( translationUnit );
[b87a5ed]325 return 0;
[d9a0e76]326} // main
[51b73452]327
[cbaee0d]328void parse_cmdline( int argc, char * argv[], const char *& filename ) {
[0da3e2c]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)
[926af74]381 yydebug = true;
[0da3e2c]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 '?':
[3b8e52c]426 assertf( false, "Unknown option: '%c'\n", (char)optopt );
[0da3e2c]427 default:
428 abort();
429 } // switch
430 } // while
431} // parse_cmdline
432
[8b7ee09]433static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
[0da3e2c]434 extern int yyparse( void );
[cbaee0d]435 extern FILE * yyin;
[0da3e2c]436 extern int yylineno;
437
[8b7ee09]438 ::linkage = linkage; // set globals
[0da3e2c]439 yyin = input;
440 yylineno = 1;
441 typedefTable.enterScope();
442 int parseStatus = yyparse();
[81419b5]443
444 fclose( input );
[0da3e2c]445 if ( shouldExit || parseStatus != 0 ) {
446 exit( parseStatus );
[81419b5]447 } // if
[0da3e2c]448} // parse
[81419b5]449
[1ab4ce2]450static bool notPrelude( Declaration * decl ) {
451 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
[0da3e2c]452} // notPrelude
[1ab4ce2]453
[e6955b1]454static void dump( list< Declaration * > & translationUnit, ostream & out ) {
455 list< Declaration * > decls;
[926af74]456
[1ab4ce2]457 if ( noprotop ) {
[e6955b1]458 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
[1ab4ce2]459 } else {
460 decls = translationUnit;
[926af74]461 } // if
[1ab4ce2]462
[f77f12e2]463 printAll( decls, out );
[7f5566b]464 deleteAll( translationUnit );
[0da3e2c]465} // dump
[1ab4ce2]466
[51b73452]467// Local Variables: //
[b87a5ed]468// tab-width: 4 //
469// mode: c++ //
470// compile-command: "make install" //
[51b73452]471// End: //
Note: See TracBrowser for help on using the repository browser.