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