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