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