| 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 Jun 6 15:51:47 2018
|
|---|
| 13 | // Update Count : 498
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <cxxabi.h> // for __cxa_demangle
|
|---|
| 17 | #include <execinfo.h> // for backtrace, backtrace_symbols
|
|---|
| 18 | #include <getopt.h> // for no_argument, optind, geto...
|
|---|
| 19 | #include <signal.h> // for signal, SIGABRT, SIGSEGV
|
|---|
| 20 | #include <cassert> // for assertf
|
|---|
| 21 | #include <cstdio> // for fopen, FILE, fclose, stdin
|
|---|
| 22 | #include <cstdlib> // for exit, free, abort, EXIT_F...
|
|---|
| 23 | #include <cstring> // for index
|
|---|
| 24 | #include <fstream> // for ofstream
|
|---|
| 25 | #include <iostream> // for operator<<, basic_ostream
|
|---|
| 26 | #include <iterator> // for back_inserter
|
|---|
| 27 | #include <list> // for list
|
|---|
| 28 | #include <string> // for char_traits, operator<<
|
|---|
| 29 |
|
|---|
| 30 | #include "../config.h" // for CFA_LIBDIR
|
|---|
| 31 | #include "CodeGen/FixMain.h" // for FixMain
|
|---|
| 32 | #include "CodeGen/FixNames.h" // for fixNames
|
|---|
| 33 | #include "CodeGen/Generate.h" // for generate
|
|---|
| 34 | #include "CodeTools/DeclStats.h" // for printDeclStats
|
|---|
| 35 | #include "CodeTools/TrackLoc.h" // for fillLocations
|
|---|
| 36 | #include "Common/CompilerError.h" // for CompilerError
|
|---|
| 37 | #include "Common/Heap.h"
|
|---|
| 38 | #include "Common/PassVisitor.h"
|
|---|
| 39 | #include "Common/SemanticError.h" // for SemanticError
|
|---|
| 40 | #include "Common/UnimplementedError.h" // for UnimplementedError
|
|---|
| 41 | #include "Common/utility.h" // for deleteAll, filter, printAll
|
|---|
| 42 | #include "Concurrency/Waitfor.h" // for generateWaitfor
|
|---|
| 43 | #include "ControlStruct/ExceptTranslate.h" // for translateEHM
|
|---|
| 44 | #include "ControlStruct/Mutate.h" // for mutate
|
|---|
| 45 | #include "GenPoly/Box.h" // for box
|
|---|
| 46 | #include "GenPoly/InstantiateGeneric.h" // for instantiateGeneric
|
|---|
| 47 | #include "GenPoly/Lvalue.h" // for convertLvalue
|
|---|
| 48 | #include "GenPoly/Specialize.h" // for convertSpecializations
|
|---|
| 49 | #include "InitTweak/FixInit.h" // for fix
|
|---|
| 50 | #include "InitTweak/GenInit.h" // for genInit
|
|---|
| 51 | #include "MakeLibCfa.h" // for makeLibCfa
|
|---|
| 52 | #include "Parser/LinkageSpec.h" // for Spec, Cforall, Intrinsic
|
|---|
| 53 | #include "Parser/ParseNode.h" // for DeclarationNode, buildList
|
|---|
| 54 | #include "Parser/TypedefTable.h" // for TypedefTable
|
|---|
| 55 | #include "ResolvExpr/AlternativePrinter.h" // for AlternativePrinter
|
|---|
| 56 | #include "ResolvExpr/Resolver.h" // for resolve
|
|---|
| 57 | #include "SymTab/Validate.h" // for validate
|
|---|
| 58 | #include "SynTree/Declaration.h" // for Declaration
|
|---|
| 59 | #include "SynTree/Visitor.h" // for acceptAll
|
|---|
| 60 | #include "Tuples/Tuples.h" // for expandMemberTuples, expan...
|
|---|
| 61 | #include "Virtual/ExpandCasts.h" // for expandCasts
|
|---|
| 62 |
|
|---|
| 63 | using namespace std;
|
|---|
| 64 |
|
|---|
| 65 | #define PASS(name, pass) \
|
|---|
| 66 | if ( errorp ) { cerr << name << endl; } \
|
|---|
| 67 | HeapStats::newPass(name); \
|
|---|
| 68 | pass;
|
|---|
| 69 |
|
|---|
| 70 | LinkageSpec::Spec linkage = LinkageSpec::Cforall;
|
|---|
| 71 | TypedefTable typedefTable;
|
|---|
| 72 | DeclarationNode * parseTree = nullptr; // program parse tree
|
|---|
| 73 |
|
|---|
| 74 | extern int yydebug; // set for -g flag (Grammar)
|
|---|
| 75 | bool
|
|---|
| 76 | astp = false,
|
|---|
| 77 | bresolvep = false,
|
|---|
| 78 | bboxp = false,
|
|---|
| 79 | bcodegenp = false,
|
|---|
| 80 | ctorinitp = false,
|
|---|
| 81 | declstatsp = false,
|
|---|
| 82 | exprp = false,
|
|---|
| 83 | expraltp = false,
|
|---|
| 84 | genericsp = false,
|
|---|
| 85 | libcfap = false,
|
|---|
| 86 | nopreludep = false,
|
|---|
| 87 | noprotop = false,
|
|---|
| 88 | nomainp = false,
|
|---|
| 89 | parsep = false,
|
|---|
| 90 | resolvep = false, // used in AlternativeFinder
|
|---|
| 91 | symtabp = false,
|
|---|
| 92 | treep = false,
|
|---|
| 93 | tuplep = false,
|
|---|
| 94 | validp = false,
|
|---|
| 95 | errorp = false,
|
|---|
| 96 | codegenp = false,
|
|---|
| 97 | prettycodegenp = false,
|
|---|
| 98 | linemarks = false;
|
|---|
| 99 |
|
|---|
| 100 | static void parse_cmdline( int argc, char *argv[], const char *& filename );
|
|---|
| 101 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
|
|---|
| 102 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
|
|---|
| 103 |
|
|---|
| 104 | static void backtrace( int start ) { // skip first N stack frames
|
|---|
| 105 | enum { Frames = 50 };
|
|---|
| 106 | void * array[Frames];
|
|---|
| 107 | int size = ::backtrace( array, Frames );
|
|---|
| 108 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names
|
|---|
| 109 |
|
|---|
| 110 | *index( messages[0], '(' ) = '\0'; // find executable name
|
|---|
| 111 | cerr << "Stack back trace for: " << messages[0] << endl;
|
|---|
| 112 |
|
|---|
| 113 | // skip last 2 stack frames after main
|
|---|
| 114 | for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
|
|---|
| 115 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
|
|---|
| 116 | for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset
|
|---|
| 117 | if ( *p == '(' ) {
|
|---|
| 118 | mangled_name = p;
|
|---|
| 119 | } else if ( *p == '+' ) {
|
|---|
| 120 | offset_begin = p;
|
|---|
| 121 | } else if ( *p == ')' ) {
|
|---|
| 122 | offset_end = p;
|
|---|
| 123 | break;
|
|---|
| 124 | } // if
|
|---|
| 125 | } // for
|
|---|
| 126 |
|
|---|
| 127 | // if line contains symbol, attempt to demangle
|
|---|
| 128 | int frameNo = i - start;
|
|---|
| 129 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
|
|---|
| 130 | *mangled_name++ = '\0'; // delimit strings
|
|---|
| 131 | *offset_begin++ = '\0';
|
|---|
| 132 | *offset_end++ = '\0';
|
|---|
| 133 |
|
|---|
| 134 | int status;
|
|---|
| 135 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
|
|---|
| 136 | // bug in __cxa_demangle for single-character lower-case non-mangled names
|
|---|
| 137 | if ( status == 0 ) { // demangling successful ?
|
|---|
| 138 | cerr << "(" << frameNo << ") " << messages[i] << " : "
|
|---|
| 139 | << real_name << "+" << offset_begin << offset_end << endl;
|
|---|
| 140 | } else { // otherwise, output mangled name
|
|---|
| 141 | cerr << "(" << frameNo << ") " << messages[i] << " : "
|
|---|
| 142 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl;
|
|---|
| 143 | } // if
|
|---|
| 144 |
|
|---|
| 145 | free( real_name );
|
|---|
| 146 | } else { // otherwise, print the whole line
|
|---|
| 147 | cerr << "(" << frameNo << ") " << messages[i] << endl;
|
|---|
| 148 | } // if
|
|---|
| 149 | } // for
|
|---|
| 150 |
|
|---|
| 151 | free( messages );
|
|---|
| 152 | } // backtrace
|
|---|
| 153 |
|
|---|
| 154 | void sigSegvBusHandler( int sig_num ) {
|
|---|
| 155 | cerr << "*CFA runtime error* program cfa-cpp terminated with "
|
|---|
| 156 | << (sig_num == SIGSEGV ? "segment fault" : "bus error")
|
|---|
| 157 | << "." << endl;
|
|---|
| 158 | backtrace( 2 ); // skip first 2 stack frames
|
|---|
| 159 | //_exit( EXIT_FAILURE );
|
|---|
| 160 | abort();
|
|---|
| 161 | } // sigSegvBusHandler
|
|---|
| 162 |
|
|---|
| 163 | void sigAbortHandler( __attribute__((unused)) int sig_num ) {
|
|---|
| 164 | backtrace( 6 ); // skip first 6 stack frames
|
|---|
| 165 | signal( SIGABRT, SIG_DFL); // reset default signal handler
|
|---|
| 166 | raise( SIGABRT ); // reraise SIGABRT
|
|---|
| 167 | } // sigAbortHandler
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 | int main( int argc, char * argv[] ) {
|
|---|
| 171 | FILE * input; // use FILE rather than istream because yyin is FILE
|
|---|
| 172 | ostream *output = & cout;
|
|---|
| 173 | const char *filename = nullptr;
|
|---|
| 174 | list< Declaration * > translationUnit;
|
|---|
| 175 |
|
|---|
| 176 | signal( SIGSEGV, sigSegvBusHandler );
|
|---|
| 177 | signal( SIGBUS, sigSegvBusHandler );
|
|---|
| 178 | signal( SIGABRT, sigAbortHandler );
|
|---|
| 179 |
|
|---|
| 180 | // std::cout << "main" << std::endl;
|
|---|
| 181 | // for ( int i = 0; i < argc; i += 1 ) {
|
|---|
| 182 | // std::cout << '\t' << argv[i] << std::endl;
|
|---|
| 183 | // } // for
|
|---|
| 184 |
|
|---|
| 185 | parse_cmdline( argc, argv, filename ); // process command-line arguments
|
|---|
| 186 | CodeGen::FixMain::setReplaceMain( !nomainp );
|
|---|
| 187 |
|
|---|
| 188 | try {
|
|---|
| 189 | // choose to read the program from a file or stdin
|
|---|
| 190 | if ( optind < argc ) { // any commands after the flags ? => input file name
|
|---|
| 191 | input = fopen( argv[ optind ], "r" );
|
|---|
| 192 | assertf( input, "cannot open %s\n", argv[ optind ] );
|
|---|
| 193 | // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
|
|---|
| 194 | if ( filename == nullptr ) filename = argv[ optind ];
|
|---|
| 195 | // prelude filename comes in differently
|
|---|
| 196 | if ( libcfap ) filename = "prelude.cf";
|
|---|
| 197 | optind += 1;
|
|---|
| 198 | } else { // no input file name
|
|---|
| 199 | input = stdin;
|
|---|
| 200 | // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
|
|---|
| 201 | // a fake name along
|
|---|
| 202 | if ( filename == nullptr ) filename = "stdin";
|
|---|
| 203 | } // if
|
|---|
| 204 |
|
|---|
| 205 | // read in the builtins, extras, and the prelude
|
|---|
| 206 | if ( ! nopreludep ) { // include gcc builtins
|
|---|
| 207 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
|
|---|
| 208 |
|
|---|
| 209 | // Read to gcc builtins, if not generating the cfa library
|
|---|
| 210 | FILE * gcc_builtins = fopen( libcfap | treep ? "../prelude/gcc-builtins.cf" : CFA_LIBDIR "/gcc-builtins.cf", "r" );
|
|---|
| 211 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" );
|
|---|
| 212 | parse( gcc_builtins, LinkageSpec::Compiler );
|
|---|
| 213 |
|
|---|
| 214 | // read the extra prelude in, if not generating the cfa library
|
|---|
| 215 | FILE * extras = fopen( libcfap | treep ? "../prelude/extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
|
|---|
| 216 | assertf( extras, "cannot open extras.cf\n" );
|
|---|
| 217 | parse( extras, LinkageSpec::BuiltinC );
|
|---|
| 218 |
|
|---|
| 219 | if ( ! libcfap ) {
|
|---|
| 220 | // read the prelude in, if not generating the cfa library
|
|---|
| 221 | FILE * prelude = fopen( treep ? "../prelude/prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
|
|---|
| 222 | assertf( prelude, "cannot open prelude.cf\n" );
|
|---|
| 223 | parse( prelude, LinkageSpec::Intrinsic );
|
|---|
| 224 |
|
|---|
| 225 | // Read to cfa builtins, if not generating the cfa library
|
|---|
| 226 | FILE * builtins = fopen( libcfap | treep ? "../prelude/builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
|
|---|
| 227 | assertf( builtins, "cannot open builtins.cf\n" );
|
|---|
| 228 | parse( builtins, LinkageSpec::BuiltinCFA );
|
|---|
| 229 | } // if
|
|---|
| 230 | } // if
|
|---|
| 231 |
|
|---|
| 232 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
|
|---|
| 233 |
|
|---|
| 234 | if ( parsep ) {
|
|---|
| 235 | parseTree->printList( cout );
|
|---|
| 236 | delete parseTree;
|
|---|
| 237 | return 0;
|
|---|
| 238 | } // if
|
|---|
| 239 |
|
|---|
| 240 | buildList( parseTree, translationUnit );
|
|---|
| 241 | delete parseTree;
|
|---|
| 242 | parseTree = nullptr;
|
|---|
| 243 |
|
|---|
| 244 | if ( astp ) {
|
|---|
| 245 | dump( translationUnit );
|
|---|
| 246 | return 0;
|
|---|
| 247 | } // if
|
|---|
| 248 |
|
|---|
| 249 | // Temporary: fill locations after parsing so that every node has a location, for early error messages.
|
|---|
| 250 | // Eventually we should pass the locations from the parser to every node, but this quick and dirty solution
|
|---|
| 251 | // works okay for now.
|
|---|
| 252 | CodeTools::fillLocations( translationUnit );
|
|---|
| 253 |
|
|---|
| 254 | // add the assignment statement after the initialization of a type parameter
|
|---|
| 255 | PASS( "validate", SymTab::validate( translationUnit, symtabp ) );
|
|---|
| 256 | if ( symtabp ) {
|
|---|
| 257 | deleteAll( translationUnit );
|
|---|
| 258 | return 0;
|
|---|
| 259 | } // if
|
|---|
| 260 |
|
|---|
| 261 | if ( expraltp ) {
|
|---|
| 262 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout );
|
|---|
| 263 | acceptAll( translationUnit, printer );
|
|---|
| 264 | return 0;
|
|---|
| 265 | } // if
|
|---|
| 266 |
|
|---|
| 267 | if ( validp ) {
|
|---|
| 268 | dump( translationUnit );
|
|---|
| 269 | return 0;
|
|---|
| 270 | } // if
|
|---|
| 271 |
|
|---|
| 272 | PASS( "fixLabels", ControlStruct::fixLabels( translationUnit ) );
|
|---|
| 273 | PASS( "fixNames", CodeGen::fixNames( translationUnit ) );
|
|---|
| 274 | PASS( "genInit", InitTweak::genInit( translationUnit ) );
|
|---|
| 275 | PASS( "expandMemberTuples" , Tuples::expandMemberTuples( translationUnit ) );
|
|---|
| 276 | if ( libcfap ) {
|
|---|
| 277 | // generate the bodies of cfa library functions
|
|---|
| 278 | LibCfa::makeLibCfa( translationUnit );
|
|---|
| 279 | } // if
|
|---|
| 280 |
|
|---|
| 281 | if ( declstatsp ) {
|
|---|
| 282 | CodeTools::printDeclStats( translationUnit );
|
|---|
| 283 | deleteAll( translationUnit );
|
|---|
| 284 | return 0;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | if ( bresolvep ) {
|
|---|
| 288 | dump( translationUnit );
|
|---|
| 289 | return 0;
|
|---|
| 290 | } // if
|
|---|
| 291 |
|
|---|
| 292 | CodeTools::fillLocations( translationUnit );
|
|---|
| 293 |
|
|---|
| 294 | PASS( "resolve", ResolvExpr::resolve( translationUnit ) );
|
|---|
| 295 | if ( exprp ) {
|
|---|
| 296 | dump( translationUnit );
|
|---|
| 297 | return 0;
|
|---|
| 298 | } // if
|
|---|
| 299 |
|
|---|
| 300 | // fix ObjectDecl - replaces ConstructorInit nodes
|
|---|
| 301 | PASS( "fixInit", InitTweak::fix( translationUnit, filename, libcfap || treep ) );
|
|---|
| 302 | if ( ctorinitp ) {
|
|---|
| 303 | dump ( translationUnit );
|
|---|
| 304 | return 0;
|
|---|
| 305 | } // if
|
|---|
| 306 |
|
|---|
| 307 | PASS( "expandUniqueExpr", Tuples::expandUniqueExpr( translationUnit ) ); // xxx - is this the right place for this? want to expand ASAP so tha, sequent 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
|
|---|
| 308 |
|
|---|
| 309 | PASS( "translateEHM" , ControlStruct::translateEHM( translationUnit ) );
|
|---|
| 310 |
|
|---|
| 311 | PASS( "generateWaitfor" , Concurrency::generateWaitFor( translationUnit ) );
|
|---|
| 312 |
|
|---|
| 313 | PASS( "convertSpecializations", GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded
|
|---|
| 314 |
|
|---|
| 315 | PASS( "expandTuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this?
|
|---|
| 316 |
|
|---|
| 317 | if ( tuplep ) {
|
|---|
| 318 | dump( translationUnit );
|
|---|
| 319 | return 0;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | PASS( "virtual expandCasts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM
|
|---|
| 323 |
|
|---|
| 324 | PASS( "instantiateGenerics", GenPoly::instantiateGeneric( translationUnit ) );
|
|---|
| 325 | if ( genericsp ) {
|
|---|
| 326 | dump( translationUnit );
|
|---|
| 327 | return 0;
|
|---|
| 328 | }
|
|---|
| 329 | PASS( "convertLvalue", GenPoly::convertLvalue( translationUnit ) );
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 | if ( bboxp ) {
|
|---|
| 333 | dump( translationUnit );
|
|---|
| 334 | return 0;
|
|---|
| 335 | } // if
|
|---|
| 336 | PASS( "box", GenPoly::box( translationUnit ) );
|
|---|
| 337 |
|
|---|
| 338 | if ( bcodegenp ) {
|
|---|
| 339 | dump( translationUnit );
|
|---|
| 340 | return 0;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | if ( optind < argc ) { // any commands after the flags and input file ? => output file name
|
|---|
| 344 | output = new ofstream( argv[ optind ] );
|
|---|
| 345 | } // if
|
|---|
| 346 |
|
|---|
| 347 | CodeTools::fillLocations( translationUnit );
|
|---|
| 348 | PASS( "codegen", CodeGen::generate( translationUnit, *output, ! noprotop, prettycodegenp, true, linemarks ) );
|
|---|
| 349 |
|
|---|
| 350 | CodeGen::FixMain::fix( *output, treep ? "../prelude/bootloader.c" : CFA_LIBDIR "/bootloader.c" );
|
|---|
| 351 | if ( output != &cout ) {
|
|---|
| 352 | delete output;
|
|---|
| 353 | } // if
|
|---|
| 354 | } catch ( SemanticErrorException &e ) {
|
|---|
| 355 | if ( errorp ) {
|
|---|
| 356 | cerr << "---AST at error:---" << endl;
|
|---|
| 357 | dump( translationUnit, cerr );
|
|---|
| 358 | cerr << endl << "---End of AST, begin error message:---\n" << endl;
|
|---|
| 359 | } // if
|
|---|
| 360 | e.print();
|
|---|
| 361 | if ( output != &cout ) {
|
|---|
| 362 | delete output;
|
|---|
| 363 | } // if
|
|---|
| 364 | return 1;
|
|---|
| 365 | } catch ( UnimplementedError &e ) {
|
|---|
| 366 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
|
|---|
| 367 | if ( output != &cout ) {
|
|---|
| 368 | delete output;
|
|---|
| 369 | } // if
|
|---|
| 370 | return 1;
|
|---|
| 371 | } catch ( CompilerError &e ) {
|
|---|
| 372 | cerr << "Compiler Error: " << e.get_what() << endl;
|
|---|
| 373 | cerr << "(please report bugs to [REDACTED])" << endl;
|
|---|
| 374 | if ( output != &cout ) {
|
|---|
| 375 | delete output;
|
|---|
| 376 | } // if
|
|---|
| 377 | return 1;
|
|---|
| 378 | } catch(...) {
|
|---|
| 379 | std::exception_ptr eptr = std::current_exception();
|
|---|
| 380 | try {
|
|---|
| 381 | if (eptr) {
|
|---|
| 382 | std::rethrow_exception(eptr);
|
|---|
| 383 | }
|
|---|
| 384 | else {
|
|---|
| 385 | std::cerr << "Exception Uncaught and Unkown" << std::endl;
|
|---|
| 386 | }
|
|---|
| 387 | } catch(const std::exception& e) {
|
|---|
| 388 | std::cerr << "Unaught Exception \"" << e.what() << "\"\n";
|
|---|
| 389 | }
|
|---|
| 390 | return 1;
|
|---|
| 391 | }// try
|
|---|
| 392 |
|
|---|
| 393 | deleteAll( translationUnit );
|
|---|
| 394 | if(!libcfap && !treep) HeapStats::printStats();
|
|---|
| 395 | return 0;
|
|---|
| 396 | } // main
|
|---|
| 397 |
|
|---|
| 398 | void parse_cmdline( int argc, char * argv[], const char *& filename ) {
|
|---|
| 399 | enum { Ast, Bbox, Bresolver, CtorInitFix, DeclStats, Expr, ExprAlt, Grammar, LibCFA, Linemarks, Nolinemarks, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, TupleExpansion, Validate, };
|
|---|
| 400 |
|
|---|
| 401 | static struct option long_opts[] = {
|
|---|
| 402 | { "ast", no_argument, 0, Ast },
|
|---|
| 403 | { "before-box", no_argument, 0, Bbox },
|
|---|
| 404 | { "before-resolver", no_argument, 0, Bresolver },
|
|---|
| 405 | { "ctorinitfix", no_argument, 0, CtorInitFix },
|
|---|
| 406 | { "decl-stats", no_argument, 0, DeclStats },
|
|---|
| 407 | { "expr", no_argument, 0, Expr },
|
|---|
| 408 | { "expralt", no_argument, 0, ExprAlt },
|
|---|
| 409 | { "grammar", no_argument, 0, Grammar },
|
|---|
| 410 | { "libcfa", no_argument, 0, LibCFA },
|
|---|
| 411 | { "line-marks", no_argument, 0, Linemarks },
|
|---|
| 412 | { "no-line-marks", no_argument, 0, Nolinemarks },
|
|---|
| 413 | { "no-preamble", no_argument, 0, Nopreamble },
|
|---|
| 414 | { "parse", no_argument, 0, Parse },
|
|---|
| 415 | { "no-prototypes", no_argument, 0, Prototypes },
|
|---|
| 416 | { "resolver", no_argument, 0, Resolver },
|
|---|
| 417 | { "symbol", no_argument, 0, Symbol },
|
|---|
| 418 | { "tree", no_argument, 0, Tree },
|
|---|
| 419 | { "tuple-expansion", no_argument, 0, TupleExpansion },
|
|---|
| 420 | { "validate", no_argument, 0, Validate },
|
|---|
| 421 | { 0, 0, 0, 0 }
|
|---|
| 422 | }; // long_opts
|
|---|
| 423 | int long_index;
|
|---|
| 424 |
|
|---|
| 425 | opterr = 0; // (global) prevent getopt from printing error messages
|
|---|
| 426 |
|
|---|
| 427 | bool Wsuppress = false, Werror = false;
|
|---|
| 428 | int c;
|
|---|
| 429 | while ( (c = getopt_long( argc, argv, "abBcCdefgGlLmnNpqrstTvwW:yzZD:F:", long_opts, &long_index )) != -1 ) {
|
|---|
| 430 | switch ( c ) {
|
|---|
| 431 | case Ast:
|
|---|
| 432 | case 'a': // dump AST
|
|---|
| 433 | astp = true;
|
|---|
| 434 | break;
|
|---|
| 435 | case Bresolver:
|
|---|
| 436 | case 'b': // print before resolver steps
|
|---|
| 437 | bresolvep = true;
|
|---|
| 438 | break;
|
|---|
| 439 | case 'B': // print before box steps
|
|---|
| 440 | bboxp = true;
|
|---|
| 441 | break;
|
|---|
| 442 | case CtorInitFix:
|
|---|
| 443 | case 'c': // print after constructors and destructors are replaced
|
|---|
| 444 | ctorinitp = true;
|
|---|
| 445 | break;
|
|---|
| 446 | case 'C': // print before code generation
|
|---|
| 447 | bcodegenp = true;
|
|---|
| 448 | break;
|
|---|
| 449 | case DeclStats:
|
|---|
| 450 | case 'd':
|
|---|
| 451 | declstatsp = true;
|
|---|
| 452 | break;
|
|---|
| 453 | case Expr:
|
|---|
| 454 | case 'e': // dump AST after expression analysis
|
|---|
| 455 | exprp = true;
|
|---|
| 456 | break;
|
|---|
| 457 | case ExprAlt:
|
|---|
| 458 | case 'f': // print alternatives for expressions
|
|---|
| 459 | expraltp = true;
|
|---|
| 460 | break;
|
|---|
| 461 | case Grammar:
|
|---|
| 462 | case 'g': // bison debugging info (grammar rules)
|
|---|
| 463 | yydebug = true;
|
|---|
| 464 | break;
|
|---|
| 465 | case 'G': // dump AST after instantiate generics
|
|---|
| 466 | genericsp = true;
|
|---|
| 467 | break;
|
|---|
| 468 | case LibCFA:
|
|---|
| 469 | case 'l': // generate libcfa.c
|
|---|
| 470 | libcfap = true;
|
|---|
| 471 | break;
|
|---|
| 472 | case Linemarks:
|
|---|
| 473 | case 'L': // print lines marks
|
|---|
| 474 | linemarks = true;
|
|---|
| 475 | break;
|
|---|
| 476 | case Nopreamble:
|
|---|
| 477 | case 'n': // do not read preamble
|
|---|
| 478 | nopreludep = true;
|
|---|
| 479 | break;
|
|---|
| 480 | case Nolinemarks:
|
|---|
| 481 | case 'N': // suppress line marks
|
|---|
| 482 | linemarks = false;
|
|---|
| 483 | break;
|
|---|
| 484 | case Prototypes:
|
|---|
| 485 | case 'p': // generate prototypes for preamble functions
|
|---|
| 486 | noprotop = true;
|
|---|
| 487 | break;
|
|---|
| 488 | case 'm': // don't replace the main
|
|---|
| 489 | nomainp = true;
|
|---|
| 490 | break;
|
|---|
| 491 | case Parse:
|
|---|
| 492 | case 'q': // dump parse tree
|
|---|
| 493 | parsep = true;
|
|---|
| 494 | break;
|
|---|
| 495 | case Resolver:
|
|---|
| 496 | case 'r': // print resolver steps
|
|---|
| 497 | resolvep = true;
|
|---|
| 498 | break;
|
|---|
| 499 | case Symbol:
|
|---|
| 500 | case 's': // print symbol table events
|
|---|
| 501 | symtabp = true;
|
|---|
| 502 | break;
|
|---|
| 503 | case Tree:
|
|---|
| 504 | case 't': // build in tree
|
|---|
| 505 | treep = true;
|
|---|
| 506 | break;
|
|---|
| 507 | case TupleExpansion:
|
|---|
| 508 | case 'T': // print after tuple expansion
|
|---|
| 509 | tuplep = true;
|
|---|
| 510 | break;
|
|---|
| 511 | case 'v': // dump AST after decl validation pass
|
|---|
| 512 | validp = true;
|
|---|
| 513 | break;
|
|---|
| 514 | case 'w':
|
|---|
| 515 | Wsuppress = true;
|
|---|
| 516 | break;
|
|---|
| 517 | case 'W':
|
|---|
| 518 | if ( strcmp( optarg, "all" ) == 0 ) {
|
|---|
| 519 | SemanticWarning_EnableAll();
|
|---|
| 520 | } else if ( strcmp( optarg, "error" ) == 0 ) {
|
|---|
| 521 | Werror = true;
|
|---|
| 522 | } else {
|
|---|
| 523 | char * warning = optarg;
|
|---|
| 524 | Severity s;
|
|---|
| 525 | if ( strncmp( optarg, "no-", 3 ) == 0 ) {
|
|---|
| 526 | warning += 3;
|
|---|
| 527 | s = Severity::Suppress;
|
|---|
| 528 | } else {
|
|---|
| 529 | s = Severity::Warn;
|
|---|
| 530 | } // if
|
|---|
| 531 | SemanticWarning_Set( warning, s );
|
|---|
| 532 | } // if
|
|---|
| 533 | break;
|
|---|
| 534 | case 'y': // dump AST on error
|
|---|
| 535 | errorp = true;
|
|---|
| 536 | break;
|
|---|
| 537 | case 'z': // dump as codegen rather than AST
|
|---|
| 538 | codegenp = true;
|
|---|
| 539 | break;
|
|---|
| 540 | case 'Z': // prettyprint during codegen (i.e. print unmangled names, etc.)
|
|---|
| 541 | prettycodegenp = true;
|
|---|
| 542 | break;
|
|---|
| 543 | case 'D': // ignore -Dxxx
|
|---|
| 544 | break;
|
|---|
| 545 | case 'F': // source file-name without suffix
|
|---|
| 546 | filename = optarg;
|
|---|
| 547 | break;
|
|---|
| 548 | case '?':
|
|---|
| 549 | if ( optopt ) { // short option ?
|
|---|
| 550 | assertf( false, "Unknown option: -%c\n", (char)optopt );
|
|---|
| 551 | } else {
|
|---|
| 552 | assertf( false, "Unknown option: %s\n", argv[optind - 1] );
|
|---|
| 553 | } // if
|
|---|
| 554 | #if defined(__GNUC__) && __GNUC__ >= 7
|
|---|
| 555 | __attribute__((fallthrough));
|
|---|
| 556 | #endif
|
|---|
| 557 | default:
|
|---|
| 558 | abort();
|
|---|
| 559 | } // switch
|
|---|
| 560 | } // while
|
|---|
| 561 |
|
|---|
| 562 | if ( Werror ) {
|
|---|
| 563 | SemanticWarning_WarningAsError();
|
|---|
| 564 | } // if
|
|---|
| 565 | if ( Wsuppress ) {
|
|---|
| 566 | SemanticWarning_SuppressAll();
|
|---|
| 567 | } // if
|
|---|
| 568 | // for ( const auto w : WarningFormats ) {
|
|---|
| 569 | // cout << w.name << ' ' << (int)w.severity << endl;
|
|---|
| 570 | // } // for
|
|---|
| 571 | } // parse_cmdline
|
|---|
| 572 |
|
|---|
| 573 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
|
|---|
| 574 | extern int yyparse( void );
|
|---|
| 575 | extern FILE * yyin;
|
|---|
| 576 | extern int yylineno;
|
|---|
| 577 |
|
|---|
| 578 | ::linkage = linkage; // set globals
|
|---|
| 579 | yyin = input;
|
|---|
| 580 | yylineno = 1;
|
|---|
| 581 | int parseStatus = yyparse();
|
|---|
| 582 |
|
|---|
| 583 | fclose( input );
|
|---|
| 584 | if ( shouldExit || parseStatus != 0 ) {
|
|---|
| 585 | exit( parseStatus );
|
|---|
| 586 | } // if
|
|---|
| 587 | } // parse
|
|---|
| 588 |
|
|---|
| 589 | static bool notPrelude( Declaration * decl ) {
|
|---|
| 590 | return ! LinkageSpec::isBuiltin( decl->get_linkage() );
|
|---|
| 591 | } // notPrelude
|
|---|
| 592 |
|
|---|
| 593 | static void dump( list< Declaration * > & translationUnit, ostream & out ) {
|
|---|
| 594 | list< Declaration * > decls;
|
|---|
| 595 |
|
|---|
| 596 | if ( noprotop ) {
|
|---|
| 597 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
|
|---|
| 598 | } else {
|
|---|
| 599 | decls = translationUnit;
|
|---|
| 600 | } // if
|
|---|
| 601 |
|
|---|
| 602 | // depending on commandline options, either generate code or dump the AST
|
|---|
| 603 | if ( codegenp ) {
|
|---|
| 604 | CodeGen::generate( decls, out, ! noprotop, prettycodegenp );
|
|---|
| 605 | } else {
|
|---|
| 606 | printAll( decls, out );
|
|---|
| 607 | }
|
|---|
| 608 | deleteAll( translationUnit );
|
|---|
| 609 | } // dump
|
|---|
| 610 |
|
|---|
| 611 | // Local Variables: //
|
|---|
| 612 | // tab-width: 4 //
|
|---|
| 613 | // mode: c++ //
|
|---|
| 614 | // compile-command: "make install" //
|
|---|
| 615 | // End: //
|
|---|