[c850687] | 1 | |
---|
[b87a5ed] | 2 | // |
---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 4 | // |
---|
| 5 | // The contents of this file are covered under the licence agreement in the |
---|
| 6 | // file "LICENCE" distributed with Cforall. |
---|
| 7 | // |
---|
[71f4e4f] | 8 | // main.cc -- |
---|
[b87a5ed] | 9 | // |
---|
[843054c2] | 10 | // Author : Richard C. Bilson |
---|
[b87a5ed] | 11 | // Created On : Fri May 15 23:12:02 2015 |
---|
[54d714e] | 12 | // Last Modified By : Andrew Beach |
---|
| 13 | // Last Modified On : Fri Jul 7 11:13:00 2017 |
---|
| 14 | // Update Count : 442 |
---|
[b87a5ed] | 15 | // |
---|
| 16 | |
---|
[bf2438c] | 17 | #include <cassert> // for assertf |
---|
| 18 | #include <cxxabi.h> // for __cxa_demangle |
---|
| 19 | #include <execinfo.h> // for backtrace, backtrace_symbols |
---|
| 20 | #include <getopt.h> // for no_argument, optind, geto... |
---|
| 21 | #include <signal.h> // for signal, SIGABRT, SIGSEGV |
---|
| 22 | #include <cstdio> // for fopen, FILE, fclose, stdin |
---|
| 23 | #include <cstdlib> // for exit, free, abort, EXIT_F... |
---|
| 24 | #include <cstring> // for index |
---|
| 25 | #include <fstream> // for ofstream |
---|
| 26 | #include <iostream> // for operator<<, basic_ostream |
---|
| 27 | #include <iterator> // for back_inserter |
---|
| 28 | #include <list> // for list |
---|
| 29 | #include <string> // for operator<<, allocator |
---|
[e6955b1] | 30 | |
---|
[bf2438c] | 31 | #include "../config.h" // for CFA_LIBDIR |
---|
| 32 | #include "CodeGen/FixMain.h" // for FixMain |
---|
| 33 | #include "CodeGen/FixNames.h" // for fixNames |
---|
| 34 | #include "CodeGen/Generate.h" // for generate |
---|
| 35 | #include "CodeTools/DeclStats.h" // for printDeclStats |
---|
| 36 | #include "CodeTools/TrackLoc.h" // for fillLocations |
---|
| 37 | #include "Common/CompilerError.h" // for CompilerError |
---|
| 38 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 39 | #include "Common/UnimplementedError.h" // for UnimplementedError |
---|
| 40 | #include "Common/utility.h" // for deleteAll, filter, printAll |
---|
| 41 | #include "ControlStruct/ExceptTranslate.h" // for translateEHM |
---|
| 42 | #include "ControlStruct/Mutate.h" // for mutate |
---|
| 43 | #include "GenPoly/Box.h" // for box |
---|
| 44 | #include "GenPoly/CopyParams.h" // for copyParams |
---|
| 45 | #include "GenPoly/InstantiateGeneric.h" // for instantiateGeneric |
---|
| 46 | #include "GenPoly/Lvalue.h" // for convertLvalue |
---|
| 47 | #include "GenPoly/Specialize.h" // for convertSpecializations |
---|
| 48 | #include "InitTweak/FixInit.h" // for fix |
---|
| 49 | #include "InitTweak/GenInit.h" // for genInit |
---|
| 50 | #include "MakeLibCfa.h" // for makeLibCfa |
---|
| 51 | #include "Parser/LinkageSpec.h" // for Spec, Cforall, Intrinsic |
---|
| 52 | #include "Parser/ParseNode.h" // for DeclarationNode, buildList |
---|
| 53 | #include "Parser/TypedefTable.h" // for TypedefTable |
---|
| 54 | #include "ResolvExpr/AlternativePrinter.h" // for AlternativePrinter |
---|
| 55 | #include "ResolvExpr/Resolver.h" // for resolve |
---|
| 56 | #include "SymTab/Validate.h" // for validate |
---|
| 57 | #include "SynTree/Declaration.h" // for Declaration |
---|
| 58 | #include "SynTree/Visitor.h" // for acceptAll |
---|
| 59 | #include "Tuples/Tuples.h" // for expandMemberTuples, expan... |
---|
[51b7345] | 60 | |
---|
| 61 | using namespace std; |
---|
| 62 | |
---|
[e6955b1] | 63 | #define OPTPRINT(x) if ( errorp ) cerr << x << endl; |
---|
[81419b5] | 64 | |
---|
[0da3e2c] | 65 | |
---|
[8b7ee09] | 66 | LinkageSpec::Spec linkage = LinkageSpec::Cforall; |
---|
[0da3e2c] | 67 | TypedefTable typedefTable; |
---|
[cbaee0d] | 68 | DeclarationNode * parseTree = nullptr; // program parse tree |
---|
[81419b5] | 69 | |
---|
[926af74] | 70 | extern int yydebug; // set for -g flag (Grammar) |
---|
[b87a5ed] | 71 | bool |
---|
| 72 | astp = false, |
---|
[de62360d] | 73 | bresolvep = false, |
---|
[fea7ca7] | 74 | bboxp = false, |
---|
[8905f56] | 75 | bcodegenp = false, |
---|
[ca1c11f] | 76 | ctorinitp = false, |
---|
[41a7137] | 77 | declstatsp = false, |
---|
[b87a5ed] | 78 | exprp = false, |
---|
| 79 | expraltp = false, |
---|
| 80 | libcfap = false, |
---|
[de62360d] | 81 | nopreludep = false, |
---|
[1ab4ce2] | 82 | noprotop = false, |
---|
[3fe34ae] | 83 | nomainp = false, |
---|
[de62360d] | 84 | parsep = false, |
---|
[b87a5ed] | 85 | resolvep = false, // used in AlternativeFinder |
---|
| 86 | symtabp = false, |
---|
[d3b7937] | 87 | treep = false, |
---|
[626dbc10] | 88 | tuplep = false, |
---|
[b87a5ed] | 89 | validp = false, |
---|
[de62360d] | 90 | errorp = false, |
---|
[35b1bf4] | 91 | codegenp = false, |
---|
[c850687] | 92 | prettycodegenp = false, |
---|
| 93 | nolinemarks = false; |
---|
[b87a5ed] | 94 | |
---|
[926af74] | 95 | static void parse_cmdline( int argc, char *argv[], const char *& filename ); |
---|
[8b7ee09] | 96 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false ); |
---|
[e6955b1] | 97 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); |
---|
| 98 | |
---|
[0afffee] | 99 | static void backtrace( int start ) { // skip first N stack frames |
---|
[e6955b1] | 100 | enum { Frames = 50 }; |
---|
| 101 | void * array[Frames]; |
---|
[0afffee] | 102 | int size = ::backtrace( array, Frames ); |
---|
| 103 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names |
---|
| 104 | |
---|
| 105 | *index( messages[0], '(' ) = '\0'; // find executable name |
---|
| 106 | cerr << "Stack back trace for: " << messages[0] << endl; |
---|
[e6955b1] | 107 | |
---|
[b542bfb] | 108 | // skip last 2 stack frames after main |
---|
| 109 | for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) { |
---|
[e6955b1] | 110 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; |
---|
| 111 | for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset |
---|
[0afffee] | 112 | if ( *p == '(' ) { |
---|
[46f6134] | 113 | mangled_name = p; |
---|
[0afffee] | 114 | } else if ( *p == '+' ) { |
---|
[e6955b1] | 115 | offset_begin = p; |
---|
[0afffee] | 116 | } else if ( *p == ')' ) { |
---|
[e6955b1] | 117 | offset_end = p; |
---|
| 118 | break; |
---|
| 119 | } // if |
---|
| 120 | } // for |
---|
| 121 | |
---|
| 122 | // if line contains symbol, attempt to demangle |
---|
[b542bfb] | 123 | int frameNo = i - start; |
---|
[e6955b1] | 124 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) { |
---|
[0afffee] | 125 | *mangled_name++ = '\0'; // delimit strings |
---|
[e6955b1] | 126 | *offset_begin++ = '\0'; |
---|
| 127 | *offset_end++ = '\0'; |
---|
| 128 | |
---|
[0afffee] | 129 | int status; |
---|
[e6955b1] | 130 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status ); |
---|
[0afffee] | 131 | // bug in __cxa_demangle for single-character lower-case non-mangled names |
---|
[e6955b1] | 132 | if ( status == 0 ) { // demangling successful ? |
---|
[b542bfb] | 133 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
[e6955b1] | 134 | << real_name << "+" << offset_begin << offset_end << endl; |
---|
| 135 | } else { // otherwise, output mangled name |
---|
[b542bfb] | 136 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
[0afffee] | 137 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl; |
---|
[e6955b1] | 138 | } // if |
---|
[0afffee] | 139 | |
---|
[e6955b1] | 140 | free( real_name ); |
---|
| 141 | } else { // otherwise, print the whole line |
---|
[b542bfb] | 142 | cerr << "(" << frameNo << ") " << messages[i] << endl; |
---|
[e6955b1] | 143 | } // if |
---|
| 144 | } // for |
---|
[b542bfb] | 145 | |
---|
[e6955b1] | 146 | free( messages ); |
---|
[b542bfb] | 147 | } // backtrace |
---|
| 148 | |
---|
| 149 | void sigSegvBusHandler( int sig_num ) { |
---|
| 150 | cerr << "*CFA runtime error* program cfa-cpp terminated with " |
---|
| 151 | << (sig_num == SIGSEGV ? "segment fault" : "bus error") |
---|
[0afffee] | 152 | << "." << endl; |
---|
[b542bfb] | 153 | backtrace( 2 ); // skip first 2 stack frames |
---|
[e6955b1] | 154 | exit( EXIT_FAILURE ); |
---|
| 155 | } // sigSegvBusHandler |
---|
[0da3e2c] | 156 | |
---|
[b3c36f4] | 157 | void sigAbortHandler( __attribute__((unused)) int sig_num ) { |
---|
[b542bfb] | 158 | backtrace( 6 ); // skip first 6 stack frames |
---|
| 159 | signal( SIGABRT, SIG_DFL); // reset default signal handler |
---|
| 160 | raise( SIGABRT ); // reraise SIGABRT |
---|
| 161 | } // sigAbortHandler |
---|
| 162 | |
---|
| 163 | |
---|
[cbaee0d] | 164 | int main( int argc, char * argv[] ) { |
---|
[3b8e52c] | 165 | FILE * input; // use FILE rather than istream because yyin is FILE |
---|
[e6955b1] | 166 | ostream *output = & cout; |
---|
[926af74] | 167 | const char *filename = nullptr; |
---|
[e6955b1] | 168 | list< Declaration * > translationUnit; |
---|
| 169 | |
---|
| 170 | signal( SIGSEGV, sigSegvBusHandler ); |
---|
| 171 | signal( SIGBUS, sigSegvBusHandler ); |
---|
[b542bfb] | 172 | signal( SIGABRT, sigAbortHandler ); |
---|
[b87a5ed] | 173 | |
---|
[0da3e2c] | 174 | parse_cmdline( argc, argv, filename ); // process command-line arguments |
---|
[13de47bc] | 175 | CodeGen::FixMain::setReplaceMain( !nomainp ); |
---|
[b87a5ed] | 176 | |
---|
| 177 | try { |
---|
[81419b5] | 178 | // choose to read the program from a file or stdin |
---|
[3b8e52c] | 179 | if ( optind < argc ) { // any commands after the flags ? => input file name |
---|
[b87a5ed] | 180 | input = fopen( argv[ optind ], "r" ); |
---|
[3b8e52c] | 181 | assertf( input, "cannot open %s\n", argv[ optind ] ); |
---|
[d029162e] | 182 | // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to) |
---|
[926af74] | 183 | if ( filename == nullptr ) filename = argv[ optind ]; |
---|
[37024fd] | 184 | // prelude filename comes in differently |
---|
| 185 | if ( libcfap ) filename = "prelude.cf"; |
---|
[b87a5ed] | 186 | optind += 1; |
---|
[3b8e52c] | 187 | } else { // no input file name |
---|
[b87a5ed] | 188 | input = stdin; |
---|
[2a7e29b] | 189 | // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass |
---|
| 190 | // a fake name along |
---|
[926af74] | 191 | if ( filename == nullptr ) filename = "stdin"; |
---|
[b87a5ed] | 192 | } // if |
---|
| 193 | |
---|
[159c62e] | 194 | // read in the builtins, extras, and the prelude |
---|
[de62360d] | 195 | if ( ! nopreludep ) { // include gcc builtins |
---|
[faf8857] | 196 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here. |
---|
[807ce84] | 197 | |
---|
| 198 | // Read to gcc builtins, if not generating the cfa library |
---|
[6ce3ae9] | 199 | FILE * gcc_builtins = fopen( libcfap | treep ? "../prelude/gcc-builtins.cf" : CFA_LIBDIR "/gcc-builtins.cf", "r" ); |
---|
| 200 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" ); |
---|
| 201 | parse( gcc_builtins, LinkageSpec::Compiler ); |
---|
[81419b5] | 202 | |
---|
[159c62e] | 203 | // read the extra prelude in, if not generating the cfa library |
---|
[375a068] | 204 | FILE * extras = fopen( libcfap | treep ? "../prelude/extras.cf" : CFA_LIBDIR "/extras.cf", "r" ); |
---|
[3b8e52c] | 205 | assertf( extras, "cannot open extras.cf\n" ); |
---|
[159c62e] | 206 | parse( extras, LinkageSpec::C ); |
---|
| 207 | |
---|
[81419b5] | 208 | if ( ! libcfap ) { |
---|
[faf8857] | 209 | // read the prelude in, if not generating the cfa library |
---|
[375a068] | 210 | FILE * prelude = fopen( treep ? "../prelude/prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" ); |
---|
[3b8e52c] | 211 | assertf( prelude, "cannot open prelude.cf\n" ); |
---|
[35304009] | 212 | parse( prelude, LinkageSpec::Intrinsic ); |
---|
[fa4805f] | 213 | |
---|
| 214 | // Read to cfa builtins, if not generating the cfa library |
---|
| 215 | FILE * builtins = fopen( libcfap | treep ? "../prelude/builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" ); |
---|
| 216 | assertf( builtins, "cannot open builtins.cf\n" ); |
---|
[54d714e] | 217 | parse( builtins, LinkageSpec::BuiltinCFA ); |
---|
[b87a5ed] | 218 | } // if |
---|
| 219 | } // if |
---|
[81419b5] | 220 | |
---|
[926af74] | 221 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); |
---|
[71f4e4f] | 222 | |
---|
[b87a5ed] | 223 | if ( parsep ) { |
---|
[e6955b1] | 224 | parseTree->printList( cout ); |
---|
[0da3e2c] | 225 | delete parseTree; |
---|
[b87a5ed] | 226 | return 0; |
---|
| 227 | } // if |
---|
| 228 | |
---|
[0da3e2c] | 229 | buildList( parseTree, translationUnit ); |
---|
| 230 | delete parseTree; |
---|
[cbaee0d] | 231 | parseTree = nullptr; |
---|
[b87a5ed] | 232 | |
---|
| 233 | if ( astp ) { |
---|
[1ab4ce2] | 234 | dump( translationUnit ); |
---|
[b87a5ed] | 235 | return 0; |
---|
| 236 | } // if |
---|
| 237 | |
---|
[bcda04c] | 238 | // OPTPRINT( "Concurrency" ) |
---|
| 239 | // Concurrency::applyKeywords( translationUnit ); |
---|
| 240 | |
---|
[839ccbb] | 241 | // add the assignment statement after the initialization of a type parameter |
---|
[81419b5] | 242 | OPTPRINT( "validate" ) |
---|
| 243 | SymTab::validate( translationUnit, symtabp ); |
---|
| 244 | if ( symtabp ) { |
---|
[46f6134] | 245 | deleteAll( translationUnit ); |
---|
[b87a5ed] | 246 | return 0; |
---|
| 247 | } // if |
---|
| 248 | |
---|
[81419b5] | 249 | if ( expraltp ) { |
---|
[e6955b1] | 250 | ResolvExpr::AlternativePrinter printer( cout ); |
---|
[81419b5] | 251 | acceptAll( translationUnit, printer ); |
---|
[b87a5ed] | 252 | return 0; |
---|
| 253 | } // if |
---|
| 254 | |
---|
| 255 | if ( validp ) { |
---|
[1ab4ce2] | 256 | dump( translationUnit ); |
---|
[b87a5ed] | 257 | return 0; |
---|
| 258 | } // if |
---|
| 259 | |
---|
[81419b5] | 260 | OPTPRINT( "mutate" ) |
---|
| 261 | ControlStruct::mutate( translationUnit ); |
---|
[71f4e4f] | 262 | OPTPRINT( "fixNames" ) |
---|
[81419b5] | 263 | CodeGen::fixNames( translationUnit ); |
---|
[ef42b143] | 264 | OPTPRINT( "genInit" ) |
---|
[a0fdbd5] | 265 | InitTweak::genInit( translationUnit ); |
---|
[bf32bb8] | 266 | OPTPRINT( "expandMemberTuples" ); |
---|
| 267 | Tuples::expandMemberTuples( translationUnit ); |
---|
[81419b5] | 268 | if ( libcfap ) { |
---|
| 269 | // generate the bodies of cfa library functions |
---|
| 270 | LibCfa::makeLibCfa( translationUnit ); |
---|
[b87a5ed] | 271 | } // if |
---|
| 272 | |
---|
[fa2de95] | 273 | if ( declstatsp ) { |
---|
| 274 | CodeTools::printDeclStats( translationUnit ); |
---|
| 275 | deleteAll( translationUnit ); |
---|
| 276 | return 0; |
---|
| 277 | } |
---|
| 278 | |
---|
[de62360d] | 279 | if ( bresolvep ) { |
---|
[1ab4ce2] | 280 | dump( translationUnit ); |
---|
[de62360d] | 281 | return 0; |
---|
| 282 | } // if |
---|
| 283 | |
---|
[81419b5] | 284 | OPTPRINT( "resolve" ) |
---|
[b87a5ed] | 285 | ResolvExpr::resolve( translationUnit ); |
---|
[81419b5] | 286 | if ( exprp ) { |
---|
[1ab4ce2] | 287 | dump( translationUnit ); |
---|
[ca1c11f] | 288 | return 0; |
---|
[926af74] | 289 | } // if |
---|
[81419b5] | 290 | |
---|
[71f4e4f] | 291 | // fix ObjectDecl - replaces ConstructorInit nodes |
---|
[6cf27a07] | 292 | OPTPRINT( "fixInit" ) |
---|
| 293 | InitTweak::fix( translationUnit, filename, libcfap || treep ); |
---|
[ca1c11f] | 294 | if ( ctorinitp ) { |
---|
| 295 | dump ( translationUnit ); |
---|
| 296 | return 0; |
---|
[926af74] | 297 | } // if |
---|
[71f4e4f] | 298 | |
---|
[141b786] | 299 | 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 |
---|
| 300 | Tuples::expandUniqueExpr( translationUnit ); |
---|
[626dbc10] | 301 | |
---|
[307a732] | 302 | OPTPRINT( "translateEHM" ); |
---|
| 303 | ControlStruct::translateEHM( translationUnit ); |
---|
| 304 | |
---|
[626dbc10] | 305 | OPTPRINT( "convertSpecializations" ) // needs to happen before tuple types are expanded |
---|
| 306 | GenPoly::convertSpecializations( translationUnit ); |
---|
| 307 | |
---|
[d9fa60a] | 308 | OPTPRINT( "expandTuples" ); // xxx - is this the right place for this? |
---|
| 309 | Tuples::expandTuples( translationUnit ); |
---|
[626dbc10] | 310 | if ( tuplep ) { |
---|
| 311 | dump( translationUnit ); |
---|
| 312 | return 0; |
---|
| 313 | } |
---|
[141b786] | 314 | |
---|
[ea5daeb] | 315 | OPTPRINT("instantiateGenerics") |
---|
| 316 | GenPoly::instantiateGeneric( translationUnit ); |
---|
[81419b5] | 317 | OPTPRINT( "copyParams" ); |
---|
[b87a5ed] | 318 | GenPoly::copyParams( translationUnit ); |
---|
[81419b5] | 319 | OPTPRINT( "convertLvalue" ) |
---|
[b87a5ed] | 320 | GenPoly::convertLvalue( translationUnit ); |
---|
[fea7ca7] | 321 | |
---|
| 322 | if ( bboxp ) { |
---|
| 323 | dump( translationUnit ); |
---|
| 324 | return 0; |
---|
[926af74] | 325 | } // if |
---|
[81419b5] | 326 | OPTPRINT( "box" ) |
---|
[b87a5ed] | 327 | GenPoly::box( translationUnit ); |
---|
[81419b5] | 328 | |
---|
[8905f56] | 329 | if ( bcodegenp ) { |
---|
| 330 | dump( translationUnit ); |
---|
| 331 | return 0; |
---|
| 332 | } |
---|
| 333 | |
---|
[13de47bc] | 334 | if ( optind < argc ) { // any commands after the flags and input file ? => output file name |
---|
| 335 | output = new ofstream( argv[ optind ] ); |
---|
| 336 | } // if |
---|
[0270824] | 337 | |
---|
[7b15d7a] | 338 | CodeTools::fillLocations( translationUnit ); |
---|
[c850687] | 339 | CodeGen::generate( translationUnit, *output, ! noprotop, prettycodegenp, true, ! nolinemarks ); |
---|
[0270824] | 340 | |
---|
[13de47bc] | 341 | CodeGen::FixMain::fix( *output, treep ? "../prelude/bootloader.c" : CFA_LIBDIR "/bootloader.c" ); |
---|
[0270824] | 342 | |
---|
[e6955b1] | 343 | if ( output != &cout ) { |
---|
[b87a5ed] | 344 | delete output; |
---|
| 345 | } // if |
---|
| 346 | } catch ( SemanticError &e ) { |
---|
| 347 | if ( errorp ) { |
---|
[e6955b1] | 348 | cerr << "---AST at error:---" << endl; |
---|
| 349 | dump( translationUnit, cerr ); |
---|
| 350 | cerr << endl << "---End of AST, begin error message:---\n" << endl; |
---|
[926af74] | 351 | } // if |
---|
[e6955b1] | 352 | e.print( cerr ); |
---|
| 353 | if ( output != &cout ) { |
---|
[b87a5ed] | 354 | delete output; |
---|
| 355 | } // if |
---|
| 356 | return 1; |
---|
| 357 | } catch ( UnimplementedError &e ) { |
---|
[e6955b1] | 358 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl; |
---|
| 359 | if ( output != &cout ) { |
---|
[b87a5ed] | 360 | delete output; |
---|
| 361 | } // if |
---|
| 362 | return 1; |
---|
| 363 | } catch ( CompilerError &e ) { |
---|
[e6955b1] | 364 | cerr << "Compiler Error: " << e.get_what() << endl; |
---|
[c850687] | 365 | cerr << "(please report bugs to [REDACTED])" << endl; |
---|
[e6955b1] | 366 | if ( output != &cout ) { |
---|
[b87a5ed] | 367 | delete output; |
---|
| 368 | } // if |
---|
| 369 | return 1; |
---|
| 370 | } // try |
---|
| 371 | |
---|
[39786813] | 372 | deleteAll( translationUnit ); |
---|
[b87a5ed] | 373 | return 0; |
---|
[d9a0e76] | 374 | } // main |
---|
[51b7345] | 375 | |
---|
[cbaee0d] | 376 | void parse_cmdline( int argc, char * argv[], const char *& filename ) { |
---|
[41a7137] | 377 | enum { Ast, Bbox, Bresolver, CtorInitFix, DeclStats, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, TupleExpansion, Validate, }; |
---|
[0da3e2c] | 378 | |
---|
| 379 | static struct option long_opts[] = { |
---|
| 380 | { "ast", no_argument, 0, Ast }, |
---|
| 381 | { "before-box", no_argument, 0, Bbox }, |
---|
| 382 | { "before-resolver", no_argument, 0, Bresolver }, |
---|
| 383 | { "ctorinitfix", no_argument, 0, CtorInitFix }, |
---|
[41a7137] | 384 | { "decl-stats", no_argument, 0, DeclStats }, |
---|
[0da3e2c] | 385 | { "expr", no_argument, 0, Expr }, |
---|
| 386 | { "expralt", no_argument, 0, ExprAlt }, |
---|
| 387 | { "grammar", no_argument, 0, Grammar }, |
---|
| 388 | { "libcfa", no_argument, 0, LibCFA }, |
---|
| 389 | { "no-preamble", no_argument, 0, Nopreamble }, |
---|
| 390 | { "parse", no_argument, 0, Parse }, |
---|
| 391 | { "no-prototypes", no_argument, 0, Prototypes }, |
---|
| 392 | { "resolver", no_argument, 0, Resolver }, |
---|
| 393 | { "symbol", no_argument, 0, Symbol }, |
---|
| 394 | { "tree", no_argument, 0, Tree }, |
---|
[626dbc10] | 395 | { "tuple-expansion", no_argument, 0, TupleExpansion }, |
---|
[0da3e2c] | 396 | { "validate", no_argument, 0, Validate }, |
---|
| 397 | { 0, 0, 0, 0 } |
---|
| 398 | }; // long_opts |
---|
| 399 | int long_index; |
---|
| 400 | |
---|
| 401 | opterr = 0; // (global) prevent getopt from printing error messages |
---|
| 402 | |
---|
| 403 | int c; |
---|
[8905f56] | 404 | while ( (c = getopt_long( argc, argv, "abBcCdefglLmnpqrstTvyzZD:F:", long_opts, &long_index )) != -1 ) { |
---|
[0da3e2c] | 405 | switch ( c ) { |
---|
| 406 | case Ast: |
---|
| 407 | case 'a': // dump AST |
---|
| 408 | astp = true; |
---|
| 409 | break; |
---|
| 410 | case Bresolver: |
---|
| 411 | case 'b': // print before resolver steps |
---|
| 412 | bresolvep = true; |
---|
| 413 | break; |
---|
[626dbc10] | 414 | case 'B': // print before box steps |
---|
[0da3e2c] | 415 | bboxp = true; |
---|
| 416 | break; |
---|
| 417 | case CtorInitFix: |
---|
[e39241b] | 418 | case 'c': // print after constructors and destructors are replaced |
---|
[0da3e2c] | 419 | ctorinitp = true; |
---|
| 420 | break; |
---|
[8905f56] | 421 | case 'C': // print before code generation |
---|
| 422 | bcodegenp = true; |
---|
| 423 | break; |
---|
[41a7137] | 424 | case DeclStats: |
---|
| 425 | case 'd': |
---|
| 426 | declstatsp = true; |
---|
| 427 | break; |
---|
[0da3e2c] | 428 | case Expr: |
---|
| 429 | case 'e': // dump AST after expression analysis |
---|
| 430 | exprp = true; |
---|
| 431 | break; |
---|
| 432 | case ExprAlt: |
---|
| 433 | case 'f': // print alternatives for expressions |
---|
| 434 | expraltp = true; |
---|
| 435 | break; |
---|
| 436 | case Grammar: |
---|
| 437 | case 'g': // bison debugging info (grammar rules) |
---|
[926af74] | 438 | yydebug = true; |
---|
[0da3e2c] | 439 | break; |
---|
| 440 | case LibCFA: |
---|
| 441 | case 'l': // generate libcfa.c |
---|
| 442 | libcfap = true; |
---|
| 443 | break; |
---|
[c850687] | 444 | case 'L': // surpress lines marks |
---|
| 445 | nolinemarks = true; |
---|
| 446 | break; |
---|
[0da3e2c] | 447 | case Nopreamble: |
---|
| 448 | case 'n': // do not read preamble |
---|
| 449 | nopreludep = true; |
---|
| 450 | break; |
---|
| 451 | case Prototypes: |
---|
| 452 | case 'p': // generate prototypes for preamble functions |
---|
| 453 | noprotop = true; |
---|
| 454 | break; |
---|
[3fe34ae] | 455 | case 'm': // don't replace the main |
---|
| 456 | nomainp = true; |
---|
| 457 | break; |
---|
[0da3e2c] | 458 | case Parse: |
---|
| 459 | case 'q': // dump parse tree |
---|
| 460 | parsep = true; |
---|
| 461 | break; |
---|
| 462 | case Resolver: |
---|
| 463 | case 'r': // print resolver steps |
---|
| 464 | resolvep = true; |
---|
| 465 | break; |
---|
| 466 | case Symbol: |
---|
| 467 | case 's': // print symbol table events |
---|
| 468 | symtabp = true; |
---|
| 469 | break; |
---|
| 470 | case Tree: |
---|
| 471 | case 't': // build in tree |
---|
| 472 | treep = true; |
---|
| 473 | break; |
---|
[626dbc10] | 474 | case TupleExpansion: |
---|
| 475 | case 'T': // print after tuple expansion |
---|
| 476 | tuplep = true; |
---|
| 477 | break; |
---|
[0da3e2c] | 478 | case 'v': // dump AST after decl validation pass |
---|
| 479 | validp = true; |
---|
| 480 | break; |
---|
[e39241b] | 481 | case 'y': // dump AST on error |
---|
[0da3e2c] | 482 | errorp = true; |
---|
| 483 | break; |
---|
[e39241b] | 484 | case 'z': // dump as codegen rather than AST |
---|
[0da3e2c] | 485 | codegenp = true; |
---|
[e39241b] | 486 | break; |
---|
| 487 | case 'Z': // prettyprint during codegen (i.e. print unmangled names, etc.) |
---|
[35b1bf4] | 488 | prettycodegenp = true; |
---|
[0da3e2c] | 489 | break; |
---|
| 490 | case 'D': // ignore -Dxxx |
---|
| 491 | break; |
---|
| 492 | case 'F': // source file-name without suffix |
---|
| 493 | filename = optarg; |
---|
| 494 | break; |
---|
| 495 | case '?': |
---|
[ae47a23] | 496 | if ( optopt ) { // short option ? |
---|
| 497 | assertf( false, "Unknown option: -%c\n", (char)optopt ); |
---|
| 498 | } else { |
---|
| 499 | assertf( false, "Unknown option: %s\n", argv[optind - 1] ); |
---|
| 500 | } // if |
---|
[0da3e2c] | 501 | default: |
---|
| 502 | abort(); |
---|
| 503 | } // switch |
---|
| 504 | } // while |
---|
| 505 | } // parse_cmdline |
---|
| 506 | |
---|
[8b7ee09] | 507 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) { |
---|
[0da3e2c] | 508 | extern int yyparse( void ); |
---|
[cbaee0d] | 509 | extern FILE * yyin; |
---|
[0da3e2c] | 510 | extern int yylineno; |
---|
| 511 | |
---|
[8b7ee09] | 512 | ::linkage = linkage; // set globals |
---|
[0da3e2c] | 513 | yyin = input; |
---|
| 514 | yylineno = 1; |
---|
| 515 | typedefTable.enterScope(); |
---|
| 516 | int parseStatus = yyparse(); |
---|
[81419b5] | 517 | |
---|
| 518 | fclose( input ); |
---|
[0da3e2c] | 519 | if ( shouldExit || parseStatus != 0 ) { |
---|
| 520 | exit( parseStatus ); |
---|
[81419b5] | 521 | } // if |
---|
[0da3e2c] | 522 | } // parse |
---|
[81419b5] | 523 | |
---|
[1ab4ce2] | 524 | static bool notPrelude( Declaration * decl ) { |
---|
| 525 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); |
---|
[0da3e2c] | 526 | } // notPrelude |
---|
[1ab4ce2] | 527 | |
---|
[e6955b1] | 528 | static void dump( list< Declaration * > & translationUnit, ostream & out ) { |
---|
| 529 | list< Declaration * > decls; |
---|
[926af74] | 530 | |
---|
[1ab4ce2] | 531 | if ( noprotop ) { |
---|
[e6955b1] | 532 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude ); |
---|
[1ab4ce2] | 533 | } else { |
---|
| 534 | decls = translationUnit; |
---|
[926af74] | 535 | } // if |
---|
[1ab4ce2] | 536 | |
---|
[e39241b] | 537 | // depending on commandline options, either generate code or dump the AST |
---|
| 538 | if ( codegenp ) { |
---|
| 539 | CodeGen::generate( decls, out, ! noprotop, prettycodegenp ); |
---|
| 540 | } else { |
---|
| 541 | printAll( decls, out ); |
---|
| 542 | } |
---|
[7f5566b] | 543 | deleteAll( translationUnit ); |
---|
[0da3e2c] | 544 | } // dump |
---|
[1ab4ce2] | 545 | |
---|
[51b7345] | 546 | // Local Variables: // |
---|
[b87a5ed] | 547 | // tab-width: 4 // |
---|
| 548 | // mode: c++ // |
---|
| 549 | // compile-command: "make install" // |
---|
[51b7345] | 550 | // End: // |
---|