[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 | |
---|
[bf2438c] | 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 |
---|
[cbbd5b48] | 37 | #include "Common/Heap.h" |
---|
| 38 | #include "Common/PassVisitor.h" |
---|
[bf2438c] | 39 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 40 | #include "Common/UnimplementedError.h" // for UnimplementedError |
---|
| 41 | #include "Common/utility.h" // for deleteAll, filter, printAll |
---|
[9f5ecf5] | 42 | #include "Concurrency/Waitfor.h" // for generateWaitfor |
---|
[bf2438c] | 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... |
---|
[a5f0529] | 61 | #include "Virtual/ExpandCasts.h" // for expandCasts |
---|
[51b7345] | 62 | |
---|
| 63 | using namespace std; |
---|
| 64 | |
---|
[6edd210] | 65 | #define PASS(name, pass) \ |
---|
[ecaeac6e] | 66 | if ( errorp ) { cerr << name << endl; } \ |
---|
| 67 | HeapStats::newPass(name); \ |
---|
[6edd210] | 68 | pass; |
---|
[0da3e2c] | 69 | |
---|
[8b7ee09] | 70 | LinkageSpec::Spec linkage = LinkageSpec::Cforall; |
---|
[0da3e2c] | 71 | TypedefTable typedefTable; |
---|
[cbaee0d] | 72 | DeclarationNode * parseTree = nullptr; // program parse tree |
---|
[81419b5] | 73 | |
---|
[926af74] | 74 | extern int yydebug; // set for -g flag (Grammar) |
---|
[b87a5ed] | 75 | bool |
---|
| 76 | astp = false, |
---|
[de62360d] | 77 | bresolvep = false, |
---|
[fea7ca7] | 78 | bboxp = false, |
---|
[8905f56] | 79 | bcodegenp = false, |
---|
[ca1c11f] | 80 | ctorinitp = false, |
---|
[41a7137] | 81 | declstatsp = false, |
---|
[b87a5ed] | 82 | exprp = false, |
---|
| 83 | expraltp = false, |
---|
[53d3ab4b] | 84 | genericsp = false, |
---|
[b87a5ed] | 85 | libcfap = false, |
---|
[de62360d] | 86 | nopreludep = false, |
---|
[1ab4ce2] | 87 | noprotop = false, |
---|
[3fe34ae] | 88 | nomainp = false, |
---|
[de62360d] | 89 | parsep = false, |
---|
[b87a5ed] | 90 | resolvep = false, // used in AlternativeFinder |
---|
| 91 | symtabp = false, |
---|
[d3b7937] | 92 | treep = false, |
---|
[626dbc10] | 93 | tuplep = false, |
---|
[b87a5ed] | 94 | validp = false, |
---|
[de62360d] | 95 | errorp = false, |
---|
[35b1bf4] | 96 | codegenp = false, |
---|
[c850687] | 97 | prettycodegenp = false, |
---|
[6de43b6] | 98 | linemarks = false; |
---|
[b87a5ed] | 99 | |
---|
[926af74] | 100 | static void parse_cmdline( int argc, char *argv[], const char *& filename ); |
---|
[8b7ee09] | 101 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false ); |
---|
[e6955b1] | 102 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); |
---|
| 103 | |
---|
[0afffee] | 104 | static void backtrace( int start ) { // skip first N stack frames |
---|
[e6955b1] | 105 | enum { Frames = 50 }; |
---|
| 106 | void * array[Frames]; |
---|
[0afffee] | 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; |
---|
[e6955b1] | 112 | |
---|
[b542bfb] | 113 | // skip last 2 stack frames after main |
---|
| 114 | for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) { |
---|
[e6955b1] | 115 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; |
---|
| 116 | for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset |
---|
[0afffee] | 117 | if ( *p == '(' ) { |
---|
[46f6134] | 118 | mangled_name = p; |
---|
[0afffee] | 119 | } else if ( *p == '+' ) { |
---|
[e6955b1] | 120 | offset_begin = p; |
---|
[0afffee] | 121 | } else if ( *p == ')' ) { |
---|
[e6955b1] | 122 | offset_end = p; |
---|
| 123 | break; |
---|
| 124 | } // if |
---|
| 125 | } // for |
---|
| 126 | |
---|
| 127 | // if line contains symbol, attempt to demangle |
---|
[b542bfb] | 128 | int frameNo = i - start; |
---|
[e6955b1] | 129 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) { |
---|
[0afffee] | 130 | *mangled_name++ = '\0'; // delimit strings |
---|
[e6955b1] | 131 | *offset_begin++ = '\0'; |
---|
| 132 | *offset_end++ = '\0'; |
---|
| 133 | |
---|
[0afffee] | 134 | int status; |
---|
[e6955b1] | 135 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status ); |
---|
[0afffee] | 136 | // bug in __cxa_demangle for single-character lower-case non-mangled names |
---|
[e6955b1] | 137 | if ( status == 0 ) { // demangling successful ? |
---|
[b542bfb] | 138 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
[e6955b1] | 139 | << real_name << "+" << offset_begin << offset_end << endl; |
---|
| 140 | } else { // otherwise, output mangled name |
---|
[b542bfb] | 141 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
[0afffee] | 142 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl; |
---|
[e6955b1] | 143 | } // if |
---|
[0afffee] | 144 | |
---|
[e6955b1] | 145 | free( real_name ); |
---|
| 146 | } else { // otherwise, print the whole line |
---|
[b542bfb] | 147 | cerr << "(" << frameNo << ") " << messages[i] << endl; |
---|
[e6955b1] | 148 | } // if |
---|
| 149 | } // for |
---|
[b542bfb] | 150 | |
---|
[e6955b1] | 151 | free( messages ); |
---|
[b542bfb] | 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") |
---|
[0afffee] | 157 | << "." << endl; |
---|
[b542bfb] | 158 | backtrace( 2 ); // skip first 2 stack frames |
---|
[af84a35] | 159 | //_exit( EXIT_FAILURE ); |
---|
| 160 | abort(); |
---|
[e6955b1] | 161 | } // sigSegvBusHandler |
---|
[0da3e2c] | 162 | |
---|
[b3c36f4] | 163 | void sigAbortHandler( __attribute__((unused)) int sig_num ) { |
---|
[b542bfb] | 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 | |
---|
[cbaee0d] | 170 | int main( int argc, char * argv[] ) { |
---|
[3b8e52c] | 171 | FILE * input; // use FILE rather than istream because yyin is FILE |
---|
[e6955b1] | 172 | ostream *output = & cout; |
---|
[926af74] | 173 | const char *filename = nullptr; |
---|
[e6955b1] | 174 | list< Declaration * > translationUnit; |
---|
| 175 | |
---|
| 176 | signal( SIGSEGV, sigSegvBusHandler ); |
---|
| 177 | signal( SIGBUS, sigSegvBusHandler ); |
---|
[b542bfb] | 178 | signal( SIGABRT, sigAbortHandler ); |
---|
[b87a5ed] | 179 | |
---|
[44bca7f] | 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 | |
---|
[0da3e2c] | 185 | parse_cmdline( argc, argv, filename ); // process command-line arguments |
---|
[13de47bc] | 186 | CodeGen::FixMain::setReplaceMain( !nomainp ); |
---|
[b87a5ed] | 187 | |
---|
| 188 | try { |
---|
[81419b5] | 189 | // choose to read the program from a file or stdin |
---|
[3b8e52c] | 190 | if ( optind < argc ) { // any commands after the flags ? => input file name |
---|
[b87a5ed] | 191 | input = fopen( argv[ optind ], "r" ); |
---|
[3b8e52c] | 192 | assertf( input, "cannot open %s\n", argv[ optind ] ); |
---|
[d029162] | 193 | // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to) |
---|
[926af74] | 194 | if ( filename == nullptr ) filename = argv[ optind ]; |
---|
[37024fd] | 195 | // prelude filename comes in differently |
---|
| 196 | if ( libcfap ) filename = "prelude.cf"; |
---|
[b87a5ed] | 197 | optind += 1; |
---|
[3b8e52c] | 198 | } else { // no input file name |
---|
[b87a5ed] | 199 | input = stdin; |
---|
[2a7e29b] | 200 | // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass |
---|
| 201 | // a fake name along |
---|
[926af74] | 202 | if ( filename == nullptr ) filename = "stdin"; |
---|
[b87a5ed] | 203 | } // if |
---|
| 204 | |
---|
[159c62e] | 205 | // read in the builtins, extras, and the prelude |
---|
[de62360d] | 206 | if ( ! nopreludep ) { // include gcc builtins |
---|
[faf8857] | 207 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here. |
---|
[807ce84] | 208 | |
---|
| 209 | // Read to gcc builtins, if not generating the cfa library |
---|
[6ce3ae9] | 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 ); |
---|
[81419b5] | 213 | |
---|
[159c62e] | 214 | // read the extra prelude in, if not generating the cfa library |
---|
[375a068] | 215 | FILE * extras = fopen( libcfap | treep ? "../prelude/extras.cf" : CFA_LIBDIR "/extras.cf", "r" ); |
---|
[3b8e52c] | 216 | assertf( extras, "cannot open extras.cf\n" ); |
---|
[f0994a1] | 217 | parse( extras, LinkageSpec::BuiltinC ); |
---|
[159c62e] | 218 | |
---|
[81419b5] | 219 | if ( ! libcfap ) { |
---|
[faf8857] | 220 | // read the prelude in, if not generating the cfa library |
---|
[375a068] | 221 | FILE * prelude = fopen( treep ? "../prelude/prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" ); |
---|
[3b8e52c] | 222 | assertf( prelude, "cannot open prelude.cf\n" ); |
---|
[35304009] | 223 | parse( prelude, LinkageSpec::Intrinsic ); |
---|
[fa4805f] | 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" ); |
---|
[54d714e] | 228 | parse( builtins, LinkageSpec::BuiltinCFA ); |
---|
[b87a5ed] | 229 | } // if |
---|
| 230 | } // if |
---|
[81419b5] | 231 | |
---|
[926af74] | 232 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); |
---|
[71f4e4f] | 233 | |
---|
[b87a5ed] | 234 | if ( parsep ) { |
---|
[e6955b1] | 235 | parseTree->printList( cout ); |
---|
[0da3e2c] | 236 | delete parseTree; |
---|
[b87a5ed] | 237 | return 0; |
---|
| 238 | } // if |
---|
| 239 | |
---|
[0da3e2c] | 240 | buildList( parseTree, translationUnit ); |
---|
| 241 | delete parseTree; |
---|
[cbaee0d] | 242 | parseTree = nullptr; |
---|
[b87a5ed] | 243 | |
---|
| 244 | if ( astp ) { |
---|
[1ab4ce2] | 245 | dump( translationUnit ); |
---|
[b87a5ed] | 246 | return 0; |
---|
| 247 | } // if |
---|
| 248 | |
---|
[036dd5f] | 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 | |
---|
[839ccbb] | 254 | // add the assignment statement after the initialization of a type parameter |
---|
[6edd210] | 255 | PASS( "validate", SymTab::validate( translationUnit, symtabp ) ); |
---|
[81419b5] | 256 | if ( symtabp ) { |
---|
[46f6134] | 257 | deleteAll( translationUnit ); |
---|
[b87a5ed] | 258 | return 0; |
---|
| 259 | } // if |
---|
| 260 | |
---|
[81419b5] | 261 | if ( expraltp ) { |
---|
[bff09c8] | 262 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout ); |
---|
[81419b5] | 263 | acceptAll( translationUnit, printer ); |
---|
[b87a5ed] | 264 | return 0; |
---|
| 265 | } // if |
---|
| 266 | |
---|
| 267 | if ( validp ) { |
---|
[1ab4ce2] | 268 | dump( translationUnit ); |
---|
[b87a5ed] | 269 | return 0; |
---|
| 270 | } // if |
---|
| 271 | |
---|
[25fcb84] | 272 | PASS( "fixLabels", ControlStruct::fixLabels( translationUnit ) ); |
---|
[6edd210] | 273 | PASS( "fixNames", CodeGen::fixNames( translationUnit ) ); |
---|
| 274 | PASS( "genInit", InitTweak::genInit( translationUnit ) ); |
---|
| 275 | PASS( "expandMemberTuples" , Tuples::expandMemberTuples( translationUnit ) ); |
---|
[81419b5] | 276 | if ( libcfap ) { |
---|
| 277 | // generate the bodies of cfa library functions |
---|
| 278 | LibCfa::makeLibCfa( translationUnit ); |
---|
[b87a5ed] | 279 | } // if |
---|
| 280 | |
---|
[fa2de95] | 281 | if ( declstatsp ) { |
---|
| 282 | CodeTools::printDeclStats( translationUnit ); |
---|
| 283 | deleteAll( translationUnit ); |
---|
| 284 | return 0; |
---|
| 285 | } |
---|
| 286 | |
---|
[de62360d] | 287 | if ( bresolvep ) { |
---|
[1ab4ce2] | 288 | dump( translationUnit ); |
---|
[de62360d] | 289 | return 0; |
---|
| 290 | } // if |
---|
| 291 | |
---|
[76b378d] | 292 | CodeTools::fillLocations( translationUnit ); |
---|
| 293 | |
---|
[6edd210] | 294 | PASS( "resolve", ResolvExpr::resolve( translationUnit ) ); |
---|
[81419b5] | 295 | if ( exprp ) { |
---|
[1ab4ce2] | 296 | dump( translationUnit ); |
---|
[ca1c11f] | 297 | return 0; |
---|
[926af74] | 298 | } // if |
---|
[81419b5] | 299 | |
---|
[71f4e4f] | 300 | // fix ObjectDecl - replaces ConstructorInit nodes |
---|
[6edd210] | 301 | PASS( "fixInit", InitTweak::fix( translationUnit, filename, libcfap || treep ) ); |
---|
[ca1c11f] | 302 | if ( ctorinitp ) { |
---|
| 303 | dump ( translationUnit ); |
---|
| 304 | return 0; |
---|
[926af74] | 305 | } // if |
---|
[71f4e4f] | 306 | |
---|
[6edd210] | 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 |
---|
[626dbc10] | 308 | |
---|
[6edd210] | 309 | PASS( "translateEHM" , ControlStruct::translateEHM( translationUnit ) ); |
---|
| 310 | |
---|
| 311 | PASS( "generateWaitfor" , Concurrency::generateWaitFor( translationUnit ) ); |
---|
[307a732] | 312 | |
---|
[6edd210] | 313 | PASS( "convertSpecializations", GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded |
---|
[9f5ecf5] | 314 | |
---|
[6edd210] | 315 | PASS( "expandTuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this? |
---|
[626dbc10] | 316 | |
---|
| 317 | if ( tuplep ) { |
---|
| 318 | dump( translationUnit ); |
---|
| 319 | return 0; |
---|
| 320 | } |
---|
[141b786] | 321 | |
---|
[6edd210] | 322 | PASS( "virtual expandCasts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM |
---|
[a5f0529] | 323 | |
---|
[6edd210] | 324 | PASS( "instantiateGenerics", GenPoly::instantiateGeneric( translationUnit ) ); |
---|
[53d3ab4b] | 325 | if ( genericsp ) { |
---|
| 326 | dump( translationUnit ); |
---|
| 327 | return 0; |
---|
| 328 | } |
---|
[6edd210] | 329 | PASS( "convertLvalue", GenPoly::convertLvalue( translationUnit ) ); |
---|
[fea7ca7] | 330 | |
---|
[53d3ab4b] | 331 | |
---|
[fea7ca7] | 332 | if ( bboxp ) { |
---|
| 333 | dump( translationUnit ); |
---|
| 334 | return 0; |
---|
[926af74] | 335 | } // if |
---|
[6edd210] | 336 | PASS( "box", GenPoly::box( translationUnit ) ); |
---|
[81419b5] | 337 | |
---|
[8905f56] | 338 | if ( bcodegenp ) { |
---|
| 339 | dump( translationUnit ); |
---|
| 340 | return 0; |
---|
| 341 | } |
---|
| 342 | |
---|
[13de47bc] | 343 | if ( optind < argc ) { // any commands after the flags and input file ? => output file name |
---|
| 344 | output = new ofstream( argv[ optind ] ); |
---|
| 345 | } // if |
---|
[0270824] | 346 | |
---|
[7b15d7a] | 347 | CodeTools::fillLocations( translationUnit ); |
---|
[6edd210] | 348 | PASS( "codegen", CodeGen::generate( translationUnit, *output, ! noprotop, prettycodegenp, true, linemarks ) ); |
---|
[0270824] | 349 | |
---|
[13de47bc] | 350 | CodeGen::FixMain::fix( *output, treep ? "../prelude/bootloader.c" : CFA_LIBDIR "/bootloader.c" ); |
---|
[e6955b1] | 351 | if ( output != &cout ) { |
---|
[b87a5ed] | 352 | delete output; |
---|
| 353 | } // if |
---|
[a16764a6] | 354 | } catch ( SemanticErrorException &e ) { |
---|
[b87a5ed] | 355 | if ( errorp ) { |
---|
[e6955b1] | 356 | cerr << "---AST at error:---" << endl; |
---|
| 357 | dump( translationUnit, cerr ); |
---|
| 358 | cerr << endl << "---End of AST, begin error message:---\n" << endl; |
---|
[926af74] | 359 | } // if |
---|
[d55d7a6] | 360 | e.print(); |
---|
[e6955b1] | 361 | if ( output != &cout ) { |
---|
[b87a5ed] | 362 | delete output; |
---|
| 363 | } // if |
---|
| 364 | return 1; |
---|
| 365 | } catch ( UnimplementedError &e ) { |
---|
[e6955b1] | 366 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl; |
---|
| 367 | if ( output != &cout ) { |
---|
[b87a5ed] | 368 | delete output; |
---|
| 369 | } // if |
---|
| 370 | return 1; |
---|
| 371 | } catch ( CompilerError &e ) { |
---|
[e6955b1] | 372 | cerr << "Compiler Error: " << e.get_what() << endl; |
---|
[c850687] | 373 | cerr << "(please report bugs to [REDACTED])" << endl; |
---|
[e6955b1] | 374 | if ( output != &cout ) { |
---|
[b87a5ed] | 375 | delete output; |
---|
| 376 | } // if |
---|
| 377 | return 1; |
---|
[4990812] | 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 |
---|
[b87a5ed] | 392 | |
---|
[39786813] | 393 | deleteAll( translationUnit ); |
---|
[6d611fb] | 394 | if(!libcfap && !treep) HeapStats::printStats(); |
---|
[b87a5ed] | 395 | return 0; |
---|
[d9a0e76] | 396 | } // main |
---|
[51b7345] | 397 | |
---|
[cbaee0d] | 398 | void parse_cmdline( int argc, char * argv[], const char *& filename ) { |
---|
[6de43b6] | 399 | enum { Ast, Bbox, Bresolver, CtorInitFix, DeclStats, Expr, ExprAlt, Grammar, LibCFA, Linemarks, Nolinemarks, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, TupleExpansion, Validate, }; |
---|
[0da3e2c] | 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 }, |
---|
[41a7137] | 406 | { "decl-stats", no_argument, 0, DeclStats }, |
---|
[0da3e2c] | 407 | { "expr", no_argument, 0, Expr }, |
---|
| 408 | { "expralt", no_argument, 0, ExprAlt }, |
---|
| 409 | { "grammar", no_argument, 0, Grammar }, |
---|
| 410 | { "libcfa", no_argument, 0, LibCFA }, |
---|
[6de43b6] | 411 | { "line-marks", no_argument, 0, Linemarks }, |
---|
| 412 | { "no-line-marks", no_argument, 0, Nolinemarks }, |
---|
[0da3e2c] | 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 }, |
---|
[626dbc10] | 419 | { "tuple-expansion", no_argument, 0, TupleExpansion }, |
---|
[0da3e2c] | 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 | |
---|
[c5e5109] | 427 | bool Wsuppress = false, Werror = false; |
---|
[0da3e2c] | 428 | int c; |
---|
[44bca7f] | 429 | while ( (c = getopt_long( argc, argv, "abBcCdefgGlLmnNpqrstTvwW:yzZD:F:", long_opts, &long_index )) != -1 ) { |
---|
[0da3e2c] | 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; |
---|
[626dbc10] | 439 | case 'B': // print before box steps |
---|
[0da3e2c] | 440 | bboxp = true; |
---|
| 441 | break; |
---|
| 442 | case CtorInitFix: |
---|
[e39241b] | 443 | case 'c': // print after constructors and destructors are replaced |
---|
[0da3e2c] | 444 | ctorinitp = true; |
---|
| 445 | break; |
---|
[8905f56] | 446 | case 'C': // print before code generation |
---|
| 447 | bcodegenp = true; |
---|
| 448 | break; |
---|
[41a7137] | 449 | case DeclStats: |
---|
| 450 | case 'd': |
---|
| 451 | declstatsp = true; |
---|
| 452 | break; |
---|
[0da3e2c] | 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) |
---|
[926af74] | 463 | yydebug = true; |
---|
[0da3e2c] | 464 | break; |
---|
[44bca7f] | 465 | case 'G': // dump AST after instantiate generics |
---|
[53d3ab4b] | 466 | genericsp = true; |
---|
| 467 | break; |
---|
[0da3e2c] | 468 | case LibCFA: |
---|
| 469 | case 'l': // generate libcfa.c |
---|
| 470 | libcfap = true; |
---|
| 471 | break; |
---|
[6de43b6] | 472 | case Linemarks: |
---|
| 473 | case 'L': // print lines marks |
---|
| 474 | linemarks = true; |
---|
[c850687] | 475 | break; |
---|
[0da3e2c] | 476 | case Nopreamble: |
---|
| 477 | case 'n': // do not read preamble |
---|
| 478 | nopreludep = true; |
---|
| 479 | break; |
---|
[6de43b6] | 480 | case Nolinemarks: |
---|
| 481 | case 'N': // suppress line marks |
---|
| 482 | linemarks = false; |
---|
[c59bde6] | 483 | break; |
---|
[0da3e2c] | 484 | case Prototypes: |
---|
| 485 | case 'p': // generate prototypes for preamble functions |
---|
| 486 | noprotop = true; |
---|
| 487 | break; |
---|
[3fe34ae] | 488 | case 'm': // don't replace the main |
---|
| 489 | nomainp = true; |
---|
| 490 | break; |
---|
[0da3e2c] | 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; |
---|
[626dbc10] | 507 | case TupleExpansion: |
---|
| 508 | case 'T': // print after tuple expansion |
---|
| 509 | tuplep = true; |
---|
| 510 | break; |
---|
[0da3e2c] | 511 | case 'v': // dump AST after decl validation pass |
---|
| 512 | validp = true; |
---|
| 513 | break; |
---|
[44bca7f] | 514 | case 'w': |
---|
[c5e5109] | 515 | Wsuppress = true; |
---|
[44bca7f] | 516 | break; |
---|
| 517 | case 'W': |
---|
| 518 | if ( strcmp( optarg, "all" ) == 0 ) { |
---|
[68e9ace] | 519 | SemanticWarning_EnableAll(); |
---|
[44bca7f] | 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 |
---|
[68e9ace] | 531 | SemanticWarning_Set( warning, s ); |
---|
[44bca7f] | 532 | } // if |
---|
| 533 | break; |
---|
[e39241b] | 534 | case 'y': // dump AST on error |
---|
[0da3e2c] | 535 | errorp = true; |
---|
| 536 | break; |
---|
[e39241b] | 537 | case 'z': // dump as codegen rather than AST |
---|
[0da3e2c] | 538 | codegenp = true; |
---|
[e39241b] | 539 | break; |
---|
| 540 | case 'Z': // prettyprint during codegen (i.e. print unmangled names, etc.) |
---|
[35b1bf4] | 541 | prettycodegenp = true; |
---|
[0da3e2c] | 542 | break; |
---|
| 543 | case 'D': // ignore -Dxxx |
---|
| 544 | break; |
---|
| 545 | case 'F': // source file-name without suffix |
---|
| 546 | filename = optarg; |
---|
| 547 | break; |
---|
| 548 | case '?': |
---|
[ae47a23] | 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 |
---|
[b6d7f44] | 554 | #if defined(__GNUC__) && __GNUC__ >= 7 |
---|
[af98d27] | 555 | __attribute__((fallthrough)); |
---|
| 556 | #endif |
---|
[0da3e2c] | 557 | default: |
---|
| 558 | abort(); |
---|
| 559 | } // switch |
---|
| 560 | } // while |
---|
[44bca7f] | 561 | |
---|
| 562 | if ( Werror ) { |
---|
[68e9ace] | 563 | SemanticWarning_WarningAsError(); |
---|
[44bca7f] | 564 | } // if |
---|
[c5e5109] | 565 | if ( Wsuppress ) { |
---|
| 566 | SemanticWarning_SuppressAll(); |
---|
| 567 | } // if |
---|
[44bca7f] | 568 | // for ( const auto w : WarningFormats ) { |
---|
| 569 | // cout << w.name << ' ' << (int)w.severity << endl; |
---|
| 570 | // } // for |
---|
[0da3e2c] | 571 | } // parse_cmdline |
---|
| 572 | |
---|
[8b7ee09] | 573 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) { |
---|
[0da3e2c] | 574 | extern int yyparse( void ); |
---|
[cbaee0d] | 575 | extern FILE * yyin; |
---|
[0da3e2c] | 576 | extern int yylineno; |
---|
| 577 | |
---|
[8b7ee09] | 578 | ::linkage = linkage; // set globals |
---|
[0da3e2c] | 579 | yyin = input; |
---|
| 580 | yylineno = 1; |
---|
| 581 | int parseStatus = yyparse(); |
---|
[81419b5] | 582 | |
---|
| 583 | fclose( input ); |
---|
[0da3e2c] | 584 | if ( shouldExit || parseStatus != 0 ) { |
---|
| 585 | exit( parseStatus ); |
---|
[81419b5] | 586 | } // if |
---|
[0da3e2c] | 587 | } // parse |
---|
[81419b5] | 588 | |
---|
[1ab4ce2] | 589 | static bool notPrelude( Declaration * decl ) { |
---|
| 590 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); |
---|
[0da3e2c] | 591 | } // notPrelude |
---|
[1ab4ce2] | 592 | |
---|
[e6955b1] | 593 | static void dump( list< Declaration * > & translationUnit, ostream & out ) { |
---|
| 594 | list< Declaration * > decls; |
---|
[926af74] | 595 | |
---|
[1ab4ce2] | 596 | if ( noprotop ) { |
---|
[e6955b1] | 597 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude ); |
---|
[1ab4ce2] | 598 | } else { |
---|
| 599 | decls = translationUnit; |
---|
[926af74] | 600 | } // if |
---|
[1ab4ce2] | 601 | |
---|
[e39241b] | 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 | } |
---|
[7f5566b] | 608 | deleteAll( translationUnit ); |
---|
[0da3e2c] | 609 | } // dump |
---|
[1ab4ce2] | 610 | |
---|
[51b7345] | 611 | // Local Variables: // |
---|
[b87a5ed] | 612 | // tab-width: 4 // |
---|
| 613 | // mode: c++ // |
---|
| 614 | // compile-command: "make install" // |
---|
[51b7345] | 615 | // End: // |
---|