[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 | // |
---|
[3e96559] | 9 | // Author : Peter Buhr and Rob Schluntz |
---|
[b87a5ed] | 10 | // Created On : Fri May 15 23:12:02 2015 |
---|
[c59bde6] | 11 | // Last Modified By : Peter A. Buhr |
---|
[3e96559] | 12 | // Last Modified On : Thu May 2 11:09:37 2019 |
---|
| 13 | // Update Count : 523 |
---|
[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 |
---|
[7abee38] | 39 | #include "Common/Stats.h" |
---|
[cbbd5b48] | 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 | |
---|
[3e96559] | 67 | static void NewPass( const char * const name ) { |
---|
| 68 | Stats::Heap::newPass( name ); |
---|
[1cb7fab2] | 69 | using namespace Stats::Counters; |
---|
[b8665e3] | 70 | { |
---|
[3e96559] | 71 | static auto group = build<CounterGroup>( "Pass Visitor" ); |
---|
| 72 | auto pass = build<CounterGroup>( name, group ); |
---|
[b8665e3] | 73 | pass_visitor_stats.depth = 0; |
---|
[3e96559] | 74 | pass_visitor_stats.avg = build<AverageCounter<double>>( "Average Depth", pass ); |
---|
| 75 | pass_visitor_stats.max = build<MaxCounter<double>>( "Max Depth", pass ); |
---|
[b8665e3] | 76 | } |
---|
| 77 | { |
---|
[3e96559] | 78 | static auto group = build<CounterGroup>( "Syntax Node" ); |
---|
| 79 | auto pass = build<CounterGroup>( name, group ); |
---|
| 80 | BaseSyntaxNode::new_nodes = build<SimpleCounter>( "Allocs", pass ); |
---|
[b8665e3] | 81 | } |
---|
[675716e] | 82 | } |
---|
| 83 | |
---|
[3e96559] | 84 | #define PASS( name, pass ) \ |
---|
[ecaeac6e] | 85 | if ( errorp ) { cerr << name << endl; } \ |
---|
[675716e] | 86 | NewPass(name); \ |
---|
[4f97937] | 87 | Stats::Time::StartBlock(name); \ |
---|
| 88 | pass; \ |
---|
| 89 | Stats::Time::StopBlock(); |
---|
[0da3e2c] | 90 | |
---|
[8b7ee09] | 91 | LinkageSpec::Spec linkage = LinkageSpec::Cforall; |
---|
[0da3e2c] | 92 | TypedefTable typedefTable; |
---|
[cbaee0d] | 93 | DeclarationNode * parseTree = nullptr; // program parse tree |
---|
[81419b5] | 94 | |
---|
[3e96559] | 95 | static std::string PreludeDirector = ""; |
---|
[4dcaed2] | 96 | |
---|
[926af74] | 97 | static void parse_cmdline( int argc, char *argv[], const char *& filename ); |
---|
[8b7ee09] | 98 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false ); |
---|
[e6955b1] | 99 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); |
---|
| 100 | |
---|
[0afffee] | 101 | static void backtrace( int start ) { // skip first N stack frames |
---|
[e6955b1] | 102 | enum { Frames = 50 }; |
---|
| 103 | void * array[Frames]; |
---|
[0afffee] | 104 | int size = ::backtrace( array, Frames ); |
---|
| 105 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names |
---|
| 106 | |
---|
| 107 | *index( messages[0], '(' ) = '\0'; // find executable name |
---|
| 108 | cerr << "Stack back trace for: " << messages[0] << endl; |
---|
[e6955b1] | 109 | |
---|
[b542bfb] | 110 | // skip last 2 stack frames after main |
---|
| 111 | for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) { |
---|
[e6955b1] | 112 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; |
---|
| 113 | for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset |
---|
[0afffee] | 114 | if ( *p == '(' ) { |
---|
[46f6134] | 115 | mangled_name = p; |
---|
[0afffee] | 116 | } else if ( *p == '+' ) { |
---|
[e6955b1] | 117 | offset_begin = p; |
---|
[0afffee] | 118 | } else if ( *p == ')' ) { |
---|
[e6955b1] | 119 | offset_end = p; |
---|
| 120 | break; |
---|
| 121 | } // if |
---|
| 122 | } // for |
---|
| 123 | |
---|
| 124 | // if line contains symbol, attempt to demangle |
---|
[b542bfb] | 125 | int frameNo = i - start; |
---|
[e6955b1] | 126 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) { |
---|
[0afffee] | 127 | *mangled_name++ = '\0'; // delimit strings |
---|
[e6955b1] | 128 | *offset_begin++ = '\0'; |
---|
| 129 | *offset_end++ = '\0'; |
---|
| 130 | |
---|
[0afffee] | 131 | int status; |
---|
[e6955b1] | 132 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status ); |
---|
[0afffee] | 133 | // bug in __cxa_demangle for single-character lower-case non-mangled names |
---|
[e6955b1] | 134 | if ( status == 0 ) { // demangling successful ? |
---|
[b542bfb] | 135 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
[e6955b1] | 136 | << real_name << "+" << offset_begin << offset_end << endl; |
---|
| 137 | } else { // otherwise, output mangled name |
---|
[b542bfb] | 138 | cerr << "(" << frameNo << ") " << messages[i] << " : " |
---|
[0afffee] | 139 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl; |
---|
[e6955b1] | 140 | } // if |
---|
[0afffee] | 141 | |
---|
[e6955b1] | 142 | free( real_name ); |
---|
| 143 | } else { // otherwise, print the whole line |
---|
[b542bfb] | 144 | cerr << "(" << frameNo << ") " << messages[i] << endl; |
---|
[e6955b1] | 145 | } // if |
---|
| 146 | } // for |
---|
[b542bfb] | 147 | |
---|
[e6955b1] | 148 | free( messages ); |
---|
[b542bfb] | 149 | } // backtrace |
---|
| 150 | |
---|
[3e96559] | 151 | static void sigSegvBusHandler( int sig_num ) { |
---|
[b542bfb] | 152 | cerr << "*CFA runtime error* program cfa-cpp terminated with " |
---|
| 153 | << (sig_num == SIGSEGV ? "segment fault" : "bus error") |
---|
[0afffee] | 154 | << "." << endl; |
---|
[b542bfb] | 155 | backtrace( 2 ); // skip first 2 stack frames |
---|
[af84a35] | 156 | //_exit( EXIT_FAILURE ); |
---|
[3e96559] | 157 | abort(); // cause core dump for debugging |
---|
[e6955b1] | 158 | } // sigSegvBusHandler |
---|
[0da3e2c] | 159 | |
---|
[3e96559] | 160 | static void sigAbortHandler( __attribute__((unused)) int sig_num ) { |
---|
[b542bfb] | 161 | backtrace( 6 ); // skip first 6 stack frames |
---|
| 162 | signal( SIGABRT, SIG_DFL); // reset default signal handler |
---|
[675716e] | 163 | raise( SIGABRT ); // reraise SIGABRT |
---|
[b542bfb] | 164 | } // sigAbortHandler |
---|
| 165 | |
---|
| 166 | |
---|
[cbaee0d] | 167 | int main( int argc, char * argv[] ) { |
---|
[3b8e52c] | 168 | FILE * input; // use FILE rather than istream because yyin is FILE |
---|
[d08beee] | 169 | ostream * output = & cout; |
---|
| 170 | const char * filename = nullptr; |
---|
[e6955b1] | 171 | list< Declaration * > translationUnit; |
---|
| 172 | |
---|
| 173 | signal( SIGSEGV, sigSegvBusHandler ); |
---|
| 174 | signal( SIGBUS, sigSegvBusHandler ); |
---|
[b542bfb] | 175 | signal( SIGABRT, sigAbortHandler ); |
---|
[b87a5ed] | 176 | |
---|
[44bca7f] | 177 | // std::cout << "main" << std::endl; |
---|
| 178 | // for ( int i = 0; i < argc; i += 1 ) { |
---|
| 179 | // std::cout << '\t' << argv[i] << std::endl; |
---|
| 180 | // } // for |
---|
| 181 | |
---|
[0da3e2c] | 182 | parse_cmdline( argc, argv, filename ); // process command-line arguments |
---|
[13de47bc] | 183 | CodeGen::FixMain::setReplaceMain( !nomainp ); |
---|
[b87a5ed] | 184 | |
---|
| 185 | try { |
---|
[81419b5] | 186 | // choose to read the program from a file or stdin |
---|
[3b8e52c] | 187 | if ( optind < argc ) { // any commands after the flags ? => input file name |
---|
[b87a5ed] | 188 | input = fopen( argv[ optind ], "r" ); |
---|
[3b8e52c] | 189 | assertf( input, "cannot open %s\n", argv[ optind ] ); |
---|
[d029162] | 190 | // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to) |
---|
[926af74] | 191 | if ( filename == nullptr ) filename = argv[ optind ]; |
---|
[37024fd] | 192 | // prelude filename comes in differently |
---|
[e523b07] | 193 | if ( libcfap ) filename = "prelude.cfa"; |
---|
[b87a5ed] | 194 | optind += 1; |
---|
[3b8e52c] | 195 | } else { // no input file name |
---|
[b87a5ed] | 196 | input = stdin; |
---|
[2a7e29b] | 197 | // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass |
---|
| 198 | // a fake name along |
---|
[926af74] | 199 | if ( filename == nullptr ) filename = "stdin"; |
---|
[b87a5ed] | 200 | } // if |
---|
| 201 | |
---|
[79eaeb7] | 202 | Stats::Time::StartGlobal(); |
---|
[3c0d4cd] | 203 | NewPass("Parse"); |
---|
| 204 | Stats::Time::StartBlock("Parse"); |
---|
[675716e] | 205 | |
---|
[159c62e] | 206 | // read in the builtins, extras, and the prelude |
---|
[de62360d] | 207 | if ( ! nopreludep ) { // include gcc builtins |
---|
[faf8857] | 208 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here. |
---|
[807ce84] | 209 | |
---|
[37fe352] | 210 | assertf( !PreludeDirector.empty(), "Can't find prelude without option --prelude-dir must be used." ); |
---|
[4dcaed2] | 211 | |
---|
[807ce84] | 212 | // Read to gcc builtins, if not generating the cfa library |
---|
[37fe352] | 213 | FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" ); |
---|
[6ce3ae9] | 214 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" ); |
---|
| 215 | parse( gcc_builtins, LinkageSpec::Compiler ); |
---|
[81419b5] | 216 | |
---|
[159c62e] | 217 | // read the extra prelude in, if not generating the cfa library |
---|
[37fe352] | 218 | FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" ); |
---|
[3b8e52c] | 219 | assertf( extras, "cannot open extras.cf\n" ); |
---|
[f0994a1] | 220 | parse( extras, LinkageSpec::BuiltinC ); |
---|
[159c62e] | 221 | |
---|
[81419b5] | 222 | if ( ! libcfap ) { |
---|
[faf8857] | 223 | // read the prelude in, if not generating the cfa library |
---|
[e523b07] | 224 | FILE * prelude = fopen( (PreludeDirector + "/prelude.cfa").c_str(), "r" ); |
---|
| 225 | assertf( prelude, "cannot open prelude.cfa\n" ); |
---|
[35304009] | 226 | parse( prelude, LinkageSpec::Intrinsic ); |
---|
[fa4805f] | 227 | |
---|
| 228 | // Read to cfa builtins, if not generating the cfa library |
---|
[37fe352] | 229 | FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" ); |
---|
[fa4805f] | 230 | assertf( builtins, "cannot open builtins.cf\n" ); |
---|
[54d714e] | 231 | parse( builtins, LinkageSpec::BuiltinCFA ); |
---|
[b87a5ed] | 232 | } // if |
---|
| 233 | } // if |
---|
[81419b5] | 234 | |
---|
[926af74] | 235 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); |
---|
[71f4e4f] | 236 | |
---|
[b87a5ed] | 237 | if ( parsep ) { |
---|
[e6955b1] | 238 | parseTree->printList( cout ); |
---|
[0da3e2c] | 239 | delete parseTree; |
---|
[3e96559] | 240 | return EXIT_SUCCESS; |
---|
[b87a5ed] | 241 | } // if |
---|
| 242 | |
---|
[0da3e2c] | 243 | buildList( parseTree, translationUnit ); |
---|
| 244 | delete parseTree; |
---|
[cbaee0d] | 245 | parseTree = nullptr; |
---|
[b87a5ed] | 246 | |
---|
| 247 | if ( astp ) { |
---|
[1ab4ce2] | 248 | dump( translationUnit ); |
---|
[3e96559] | 249 | return EXIT_SUCCESS; |
---|
[b87a5ed] | 250 | } // if |
---|
| 251 | |
---|
[036dd5f] | 252 | // Temporary: fill locations after parsing so that every node has a location, for early error messages. |
---|
| 253 | // Eventually we should pass the locations from the parser to every node, but this quick and dirty solution |
---|
| 254 | // works okay for now. |
---|
| 255 | CodeTools::fillLocations( translationUnit ); |
---|
[3c0d4cd] | 256 | Stats::Time::StopBlock(); |
---|
[036dd5f] | 257 | |
---|
[839ccbb] | 258 | // add the assignment statement after the initialization of a type parameter |
---|
[675716e] | 259 | PASS( "Validate", SymTab::validate( translationUnit, symtabp ) ); |
---|
[81419b5] | 260 | if ( symtabp ) { |
---|
[46f6134] | 261 | deleteAll( translationUnit ); |
---|
[3e96559] | 262 | return EXIT_SUCCESS; |
---|
[b87a5ed] | 263 | } // if |
---|
| 264 | |
---|
[81419b5] | 265 | if ( expraltp ) { |
---|
[bff09c8] | 266 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout ); |
---|
[81419b5] | 267 | acceptAll( translationUnit, printer ); |
---|
[3e96559] | 268 | return EXIT_SUCCESS; |
---|
[b87a5ed] | 269 | } // if |
---|
| 270 | |
---|
| 271 | if ( validp ) { |
---|
[1ab4ce2] | 272 | dump( translationUnit ); |
---|
[3e96559] | 273 | return EXIT_SUCCESS; |
---|
[b87a5ed] | 274 | } // if |
---|
| 275 | |
---|
[675716e] | 276 | PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) ); |
---|
| 277 | PASS( "Fix Names", CodeGen::fixNames( translationUnit ) ); |
---|
| 278 | PASS( "Gen Init", InitTweak::genInit( translationUnit ) ); |
---|
| 279 | PASS( "Expand Member Tuples" , 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 ); |
---|
[3e96559] | 288 | return EXIT_SUCCESS; |
---|
| 289 | } // if |
---|
[fa2de95] | 290 | |
---|
[de62360d] | 291 | if ( bresolvep ) { |
---|
[1ab4ce2] | 292 | dump( translationUnit ); |
---|
[3e96559] | 293 | return EXIT_SUCCESS; |
---|
[de62360d] | 294 | } // if |
---|
| 295 | |
---|
[76b378d] | 296 | CodeTools::fillLocations( translationUnit ); |
---|
| 297 | |
---|
[3b3491b] | 298 | if ( resolvprotop ) { |
---|
| 299 | CodeTools::dumpAsResolvProto( translationUnit ); |
---|
[3e96559] | 300 | return EXIT_SUCCESS; |
---|
| 301 | } // if |
---|
[3b3491b] | 302 | |
---|
[675716e] | 303 | PASS( "Resolve", ResolvExpr::resolve( translationUnit ) ); |
---|
[81419b5] | 304 | if ( exprp ) { |
---|
[1ab4ce2] | 305 | dump( translationUnit ); |
---|
[3e96559] | 306 | return EXIT_SUCCESS; |
---|
[926af74] | 307 | } // if |
---|
[81419b5] | 308 | |
---|
[71f4e4f] | 309 | // fix ObjectDecl - replaces ConstructorInit nodes |
---|
[675716e] | 310 | PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) ); |
---|
[ca1c11f] | 311 | if ( ctorinitp ) { |
---|
| 312 | dump ( translationUnit ); |
---|
[3e96559] | 313 | return EXIT_SUCCESS; |
---|
[926af74] | 314 | } // if |
---|
[71f4e4f] | 315 | |
---|
[675716e] | 316 | PASS( "Expand Unique Expr", 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] | 317 | |
---|
[675716e] | 318 | PASS( "Translate EHM" , ControlStruct::translateEHM( translationUnit ) ); |
---|
[6edd210] | 319 | |
---|
[675716e] | 320 | PASS( "Gen Waitfor" , Concurrency::generateWaitFor( translationUnit ) ); |
---|
[307a732] | 321 | |
---|
[675716e] | 322 | PASS( "Convert Specializations", GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded |
---|
[9f5ecf5] | 323 | |
---|
[675716e] | 324 | PASS( "Expand Tuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this? |
---|
[626dbc10] | 325 | |
---|
| 326 | if ( tuplep ) { |
---|
| 327 | dump( translationUnit ); |
---|
[3e96559] | 328 | return EXIT_SUCCESS; |
---|
| 329 | } // if |
---|
[141b786] | 330 | |
---|
[675716e] | 331 | PASS( "Virtual Expand Casts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM |
---|
[a5f0529] | 332 | |
---|
[675716e] | 333 | PASS( "Instantiate Generics", GenPoly::instantiateGeneric( translationUnit ) ); |
---|
[53d3ab4b] | 334 | if ( genericsp ) { |
---|
| 335 | dump( translationUnit ); |
---|
[3e96559] | 336 | return EXIT_SUCCESS; |
---|
| 337 | } // if |
---|
[675716e] | 338 | PASS( "Convert L-Value", GenPoly::convertLvalue( translationUnit ) ); |
---|
[fea7ca7] | 339 | |
---|
[53d3ab4b] | 340 | |
---|
[fea7ca7] | 341 | if ( bboxp ) { |
---|
| 342 | dump( translationUnit ); |
---|
[3e96559] | 343 | return EXIT_SUCCESS; |
---|
[926af74] | 344 | } // if |
---|
[675716e] | 345 | PASS( "Box", GenPoly::box( translationUnit ) ); |
---|
[81419b5] | 346 | |
---|
[8905f56] | 347 | if ( bcodegenp ) { |
---|
| 348 | dump( translationUnit ); |
---|
[3e96559] | 349 | return EXIT_SUCCESS; |
---|
| 350 | } // if |
---|
[8905f56] | 351 | |
---|
[13de47bc] | 352 | if ( optind < argc ) { // any commands after the flags and input file ? => output file name |
---|
| 353 | output = new ofstream( argv[ optind ] ); |
---|
| 354 | } // if |
---|
[0270824] | 355 | |
---|
[7b15d7a] | 356 | CodeTools::fillLocations( translationUnit ); |
---|
[675716e] | 357 | PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! noprotop, prettycodegenp, true, linemarks ) ); |
---|
[0270824] | 358 | |
---|
[37fe352] | 359 | CodeGen::FixMain::fix( *output, (PreludeDirector + "/bootloader.c").c_str() ); |
---|
[e6955b1] | 360 | if ( output != &cout ) { |
---|
[b87a5ed] | 361 | delete output; |
---|
| 362 | } // if |
---|
[a16764a6] | 363 | } catch ( SemanticErrorException &e ) { |
---|
[b87a5ed] | 364 | if ( errorp ) { |
---|
[e6955b1] | 365 | cerr << "---AST at error:---" << endl; |
---|
| 366 | dump( translationUnit, cerr ); |
---|
| 367 | cerr << endl << "---End of AST, begin error message:---\n" << endl; |
---|
[926af74] | 368 | } // if |
---|
[d55d7a6] | 369 | e.print(); |
---|
[e6955b1] | 370 | if ( output != &cout ) { |
---|
[b87a5ed] | 371 | delete output; |
---|
| 372 | } // if |
---|
[3e96559] | 373 | return EXIT_FAILURE; |
---|
[b87a5ed] | 374 | } catch ( UnimplementedError &e ) { |
---|
[e6955b1] | 375 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl; |
---|
| 376 | if ( output != &cout ) { |
---|
[b87a5ed] | 377 | delete output; |
---|
| 378 | } // if |
---|
[3e96559] | 379 | return EXIT_FAILURE; |
---|
[b87a5ed] | 380 | } catch ( CompilerError &e ) { |
---|
[e6955b1] | 381 | cerr << "Compiler Error: " << e.get_what() << endl; |
---|
[c850687] | 382 | cerr << "(please report bugs to [REDACTED])" << endl; |
---|
[e6955b1] | 383 | if ( output != &cout ) { |
---|
[b87a5ed] | 384 | delete output; |
---|
| 385 | } // if |
---|
[3e96559] | 386 | return EXIT_FAILURE; |
---|
| 387 | } catch ( ... ) { |
---|
[4990812] | 388 | std::exception_ptr eptr = std::current_exception(); |
---|
| 389 | try { |
---|
| 390 | if (eptr) { |
---|
| 391 | std::rethrow_exception(eptr); |
---|
[3e96559] | 392 | } else { |
---|
| 393 | std::cerr << "Exception Uncaught and Unknown" << std::endl; |
---|
| 394 | } // if |
---|
[4990812] | 395 | } catch(const std::exception& e) { |
---|
[0689cd9] | 396 | std::cerr << "Uncaught Exception \"" << e.what() << "\"\n"; |
---|
[3e96559] | 397 | } // try |
---|
| 398 | return EXIT_FAILURE; |
---|
| 399 | } // try |
---|
[b87a5ed] | 400 | |
---|
[39786813] | 401 | deleteAll( translationUnit ); |
---|
[1cb7fab2] | 402 | Stats::print(); |
---|
[3e96559] | 403 | return EXIT_SUCCESS; |
---|
[d9a0e76] | 404 | } // main |
---|
[51b7345] | 405 | |
---|
[0da3e2c] | 406 | |
---|
[3e96559] | 407 | static const char optstring[] = ":abBcCdeEGghlLmNnpP:qrRS:stTvwW:yzZD:F:"; |
---|
| 408 | |
---|
| 409 | static struct option long_opts[] = { |
---|
| 410 | { "ast", no_argument, nullptr, 'a' }, |
---|
| 411 | { "before-resolver", no_argument, nullptr, 'b' }, |
---|
| 412 | { "box", no_argument, nullptr, 'B' }, |
---|
| 413 | { "ctorinitfix", no_argument, nullptr, 'c' }, |
---|
| 414 | { "code", no_argument, nullptr, 'C' }, |
---|
| 415 | { "decl-stmts", no_argument, nullptr, 'd' }, |
---|
| 416 | { "expr", no_argument, nullptr, 'e' }, |
---|
| 417 | { "expralt", no_argument, nullptr, 'E' }, |
---|
| 418 | { "generics", no_argument, nullptr, 'G' }, |
---|
| 419 | { "grammar", no_argument, nullptr, 'g' }, |
---|
| 420 | { "help", no_argument, nullptr, 'h' }, |
---|
| 421 | { "libcfa", no_argument, nullptr, 'l' }, |
---|
| 422 | { "line-marks", no_argument, nullptr, 'L' }, |
---|
| 423 | { "no-main", no_argument, 0, 'm' }, |
---|
| 424 | { "no-line-marks", no_argument, nullptr, 'N' }, |
---|
| 425 | { "no-preamble", no_argument, nullptr, 'n' }, |
---|
| 426 | { "prototypes", no_argument, nullptr, 'p' }, |
---|
| 427 | { "prelude-dir <directory>", required_argument, nullptr, 'P' }, |
---|
| 428 | { "parse-tree", no_argument, nullptr, 'q' }, |
---|
| 429 | { "resolver", no_argument, nullptr, 'r' }, |
---|
| 430 | { "resolver-proto", no_argument, nullptr, 'R' }, |
---|
| 431 | { "statistics <option-list>", required_argument, nullptr, 'S' }, |
---|
| 432 | { "symbol", no_argument, nullptr, 's' }, |
---|
| 433 | { "tree", no_argument, nullptr, 't' }, |
---|
| 434 | { "tuple", no_argument, nullptr, 'T' }, |
---|
| 435 | { "validate", no_argument, nullptr, 'v' }, |
---|
| 436 | { "", no_argument, nullptr, 0 }, // -w |
---|
| 437 | { "", no_argument, nullptr, 0 }, // -W |
---|
| 438 | { "dump-ast-on-error", no_argument, nullptr, 'y' }, |
---|
| 439 | { "dump-ast-code", no_argument, nullptr, 'z' }, |
---|
| 440 | { "prettyprint-codegen", no_argument, nullptr, 'Z' }, |
---|
| 441 | { "", no_argument, nullptr, 0 }, // -D |
---|
| 442 | { "", no_argument, nullptr, 0 }, // -F |
---|
| 443 | { nullptr, 0, nullptr, 0 } |
---|
| 444 | }; // long_opts |
---|
| 445 | |
---|
| 446 | static const char * description[] = { |
---|
| 447 | "dump AST", // -a |
---|
| 448 | "print before resolver step", // -b |
---|
| 449 | "print before box step", // -B |
---|
| 450 | "print after ctor/dtor are replaced", // -c |
---|
| 451 | "print before code generation", // -C |
---|
| 452 | "print declaration statements", // -d |
---|
| 453 | "dump AST after expression analysis", // -e |
---|
| 454 | "print alternatives for expressions", // -E |
---|
| 455 | "dump AST after instantiate generics", // -G |
---|
| 456 | "print bison (parsing) debugging information", // -g |
---|
| 457 | "help message", // -h |
---|
| 458 | "generate libcfa.c", // -l |
---|
| 459 | "print line marks", // -L |
---|
| 460 | "do not replace main", // -m |
---|
| 461 | "suppress line marks", // -N |
---|
| 462 | "do not read preamble", // -n |
---|
| 463 | "generate prototypes for preamble functions", // -p |
---|
| 464 | "prelude directory for debug/nodebug", // -P |
---|
| 465 | "dump parse tree", // -q |
---|
| 466 | "print resolver steps", // -r |
---|
| 467 | "dump resolver-proto instance", // -R |
---|
| 468 | "enable profiling information: counters,heap,time", // -S |
---|
| 469 | "print symbol table events", // -s |
---|
| 470 | "build in tree", // -t |
---|
| 471 | "print after tuple expansion", // -T |
---|
| 472 | "dump AST after declaration validation pass", // -v |
---|
| 473 | "", // -w |
---|
| 474 | "", // -W |
---|
| 475 | "dump AST on error", // -y |
---|
| 476 | "dump as codegen rather than AST", // -z |
---|
| 477 | "prettyprint during codegen", // -Z |
---|
| 478 | "", // -D |
---|
| 479 | "", // -F |
---|
| 480 | }; // description |
---|
| 481 | |
---|
| 482 | static void usage( char *argv[] ) { |
---|
| 483 | cout << "Usage: " << argv[0] << " options are:" << endl; |
---|
| 484 | int i = 0, j = 1; // j skips starting colon |
---|
| 485 | for ( ; long_opts[i].name != 0 && optstring[j] != '\0'; i += 1, j += 1 ) { |
---|
| 486 | if ( long_opts[i].name[0] != '\0' ) { // hidden option, internal usage only |
---|
| 487 | cout << " -" << optstring[j] << ", --" << long_opts[i].name << " " << description[i] << endl; |
---|
| 488 | } // if |
---|
| 489 | if ( optstring[j+1] == ':' ) j += 1; |
---|
| 490 | } // for |
---|
| 491 | if ( long_opts[i].name != 0 || optstring[j] != '\0' ) assertf( false, "internal error, mismatch of option flags and names\n" ); |
---|
| 492 | exit( EXIT_FAILURE ); |
---|
| 493 | } // usage |
---|
| 494 | |
---|
| 495 | static void parse_cmdline( int argc, char * argv[], const char *& filename ) { |
---|
[0da3e2c] | 496 | opterr = 0; // (global) prevent getopt from printing error messages |
---|
| 497 | |
---|
[c5e5109] | 498 | bool Wsuppress = false, Werror = false; |
---|
[0da3e2c] | 499 | int c; |
---|
[3e96559] | 500 | while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) { |
---|
[0da3e2c] | 501 | switch ( c ) { |
---|
[3e96559] | 502 | case 'a': // dump AST |
---|
[0da3e2c] | 503 | astp = true; |
---|
| 504 | break; |
---|
[3e96559] | 505 | case 'b': // print before resolver steps |
---|
[0da3e2c] | 506 | bresolvep = true; |
---|
| 507 | break; |
---|
[3e96559] | 508 | case 'B': // print before box steps |
---|
[0da3e2c] | 509 | bboxp = true; |
---|
| 510 | break; |
---|
[3e96559] | 511 | case 'c': // print after ctor/dtor are replaced |
---|
[0da3e2c] | 512 | ctorinitp = true; |
---|
| 513 | break; |
---|
[3e96559] | 514 | case 'C': // print before code generation |
---|
[8905f56] | 515 | bcodegenp = true; |
---|
| 516 | break; |
---|
[3e96559] | 517 | case 'd': // print declaration statements |
---|
| 518 | declstatsp = true; |
---|
[41a7137] | 519 | break; |
---|
[3e96559] | 520 | case 'e': // dump AST after expression analysis |
---|
[0da3e2c] | 521 | exprp = true; |
---|
| 522 | break; |
---|
[3e96559] | 523 | case 'E': // print alternatives for expressions |
---|
[0da3e2c] | 524 | expraltp = true; |
---|
| 525 | break; |
---|
[3e96559] | 526 | case 'G': // dump AST after instantiate generics |
---|
| 527 | genericsp = true; |
---|
| 528 | break; |
---|
| 529 | case 'g': // print bison (parsing) debugging information |
---|
[926af74] | 530 | yydebug = true; |
---|
[0da3e2c] | 531 | break; |
---|
[3e96559] | 532 | case 'h': // help message |
---|
| 533 | usage( argv ); // no return |
---|
[53d3ab4b] | 534 | break; |
---|
[3e96559] | 535 | case 'l': // generate libcfa.c |
---|
[0da3e2c] | 536 | libcfap = true; |
---|
| 537 | break; |
---|
[3e96559] | 538 | case 'L': // print line marks |
---|
[6de43b6] | 539 | linemarks = true; |
---|
[c850687] | 540 | break; |
---|
[3e96559] | 541 | case 'm': // do not replace main |
---|
| 542 | nomainp = true; |
---|
[0da3e2c] | 543 | break; |
---|
[3e96559] | 544 | case 'N': // suppress line marks |
---|
[6de43b6] | 545 | linemarks = false; |
---|
[c59bde6] | 546 | break; |
---|
[3e96559] | 547 | case 'n': // do not read preamble |
---|
| 548 | nopreludep = true; |
---|
[0da3e2c] | 549 | break; |
---|
[3e96559] | 550 | case 'p': // generate prototypes for preamble functions |
---|
| 551 | noprotop = true; |
---|
[4dcaed2] | 552 | break; |
---|
[3e96559] | 553 | case 'P': // prelude directory for debug/nodebug, hidden |
---|
| 554 | PreludeDirector = optarg; |
---|
[3fe34ae] | 555 | break; |
---|
[3e96559] | 556 | case 'q': // dump parse tree |
---|
[0da3e2c] | 557 | parsep = true; |
---|
| 558 | break; |
---|
[3e96559] | 559 | case 'r': // print resolver steps |
---|
[0da3e2c] | 560 | resolvep = true; |
---|
| 561 | break; |
---|
[3e96559] | 562 | case 'R': // dump resolver-proto instance |
---|
[3b3491b] | 563 | resolvprotop = true; |
---|
| 564 | break; |
---|
[3e96559] | 565 | case 'S': // enable profiling information, argument comma separated list of names |
---|
| 566 | Stats::parse_params( optarg ); |
---|
[ebcc940] | 567 | break; |
---|
[3e96559] | 568 | case 's': // print symbol table events |
---|
[0da3e2c] | 569 | symtabp = true; |
---|
| 570 | break; |
---|
[3e96559] | 571 | case 't': // build in tree |
---|
[0da3e2c] | 572 | treep = true; |
---|
| 573 | break; |
---|
[3e96559] | 574 | case 'T': // print after tuple expansion |
---|
[626dbc10] | 575 | tuplep = true; |
---|
| 576 | break; |
---|
[3e96559] | 577 | case 'v': // dump AST after declaration validation pass |
---|
[0da3e2c] | 578 | validp = true; |
---|
| 579 | break; |
---|
[3e96559] | 580 | case 'w': // suppress all warnings, hidden |
---|
[c5e5109] | 581 | Wsuppress = true; |
---|
[44bca7f] | 582 | break; |
---|
[3e96559] | 583 | case 'W': // coordinate gcc -W with CFA, hidden |
---|
[44bca7f] | 584 | if ( strcmp( optarg, "all" ) == 0 ) { |
---|
[68e9ace] | 585 | SemanticWarning_EnableAll(); |
---|
[44bca7f] | 586 | } else if ( strcmp( optarg, "error" ) == 0 ) { |
---|
| 587 | Werror = true; |
---|
| 588 | } else { |
---|
| 589 | char * warning = optarg; |
---|
| 590 | Severity s; |
---|
| 591 | if ( strncmp( optarg, "no-", 3 ) == 0 ) { |
---|
| 592 | warning += 3; |
---|
| 593 | s = Severity::Suppress; |
---|
| 594 | } else { |
---|
| 595 | s = Severity::Warn; |
---|
| 596 | } // if |
---|
[68e9ace] | 597 | SemanticWarning_Set( warning, s ); |
---|
[44bca7f] | 598 | } // if |
---|
| 599 | break; |
---|
[3e96559] | 600 | case 'y': // dump AST on error |
---|
[0da3e2c] | 601 | errorp = true; |
---|
| 602 | break; |
---|
[3e96559] | 603 | case 'z': // dump as codegen rather than AST |
---|
[0da3e2c] | 604 | codegenp = true; |
---|
[e39241b] | 605 | break; |
---|
[3e96559] | 606 | case 'Z': // prettyprint during codegen (i.e. print unmangled names, etc.) |
---|
[35b1bf4] | 607 | prettycodegenp = true; |
---|
[0da3e2c] | 608 | break; |
---|
[3e96559] | 609 | case 'D': // ignore -Dxxx, forwarded by cpp, hidden |
---|
[0da3e2c] | 610 | break; |
---|
[3e96559] | 611 | case 'F': // source file-name without suffix, hidden |
---|
[0da3e2c] | 612 | filename = optarg; |
---|
| 613 | break; |
---|
[3e96559] | 614 | case '?': // unknown option |
---|
| 615 | if ( optopt ) { // short option ? |
---|
| 616 | cout << "Unknown option -" << (char)optopt << endl; |
---|
| 617 | } else { |
---|
| 618 | cout << "Unknown option " << argv[optind - 1] << endl; |
---|
| 619 | } // if |
---|
| 620 | goto Default; |
---|
| 621 | case ':': // missing option |
---|
[ae47a23] | 622 | if ( optopt ) { // short option ? |
---|
[3e96559] | 623 | cout << "Missing option for -" << (char)optopt << endl; |
---|
[ae47a23] | 624 | } else { |
---|
[3e96559] | 625 | cout << "Missing option for " << argv[optind - 1] << endl; |
---|
[ae47a23] | 626 | } // if |
---|
[3e96559] | 627 | goto Default; |
---|
| 628 | Default: |
---|
| 629 | default: |
---|
| 630 | usage( argv ); // no return |
---|
[0da3e2c] | 631 | } // switch |
---|
| 632 | } // while |
---|
[44bca7f] | 633 | |
---|
| 634 | if ( Werror ) { |
---|
[68e9ace] | 635 | SemanticWarning_WarningAsError(); |
---|
[44bca7f] | 636 | } // if |
---|
[c5e5109] | 637 | if ( Wsuppress ) { |
---|
| 638 | SemanticWarning_SuppressAll(); |
---|
| 639 | } // if |
---|
[44bca7f] | 640 | // for ( const auto w : WarningFormats ) { |
---|
| 641 | // cout << w.name << ' ' << (int)w.severity << endl; |
---|
| 642 | // } // for |
---|
[0da3e2c] | 643 | } // parse_cmdline |
---|
| 644 | |
---|
[8b7ee09] | 645 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) { |
---|
[0da3e2c] | 646 | extern int yyparse( void ); |
---|
[cbaee0d] | 647 | extern FILE * yyin; |
---|
[0da3e2c] | 648 | extern int yylineno; |
---|
| 649 | |
---|
[8b7ee09] | 650 | ::linkage = linkage; // set globals |
---|
[0da3e2c] | 651 | yyin = input; |
---|
| 652 | yylineno = 1; |
---|
| 653 | int parseStatus = yyparse(); |
---|
[81419b5] | 654 | |
---|
| 655 | fclose( input ); |
---|
[0da3e2c] | 656 | if ( shouldExit || parseStatus != 0 ) { |
---|
| 657 | exit( parseStatus ); |
---|
[81419b5] | 658 | } // if |
---|
[0da3e2c] | 659 | } // parse |
---|
[81419b5] | 660 | |
---|
[1ab4ce2] | 661 | static bool notPrelude( Declaration * decl ) { |
---|
| 662 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); |
---|
[0da3e2c] | 663 | } // notPrelude |
---|
[1ab4ce2] | 664 | |
---|
[e6955b1] | 665 | static void dump( list< Declaration * > & translationUnit, ostream & out ) { |
---|
| 666 | list< Declaration * > decls; |
---|
[926af74] | 667 | |
---|
[1ab4ce2] | 668 | if ( noprotop ) { |
---|
[e6955b1] | 669 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude ); |
---|
[1ab4ce2] | 670 | } else { |
---|
| 671 | decls = translationUnit; |
---|
[926af74] | 672 | } // if |
---|
[1ab4ce2] | 673 | |
---|
[e39241b] | 674 | // depending on commandline options, either generate code or dump the AST |
---|
| 675 | if ( codegenp ) { |
---|
| 676 | CodeGen::generate( decls, out, ! noprotop, prettycodegenp ); |
---|
| 677 | } else { |
---|
| 678 | printAll( decls, out ); |
---|
[3e96559] | 679 | } // if |
---|
[7f5566b] | 680 | deleteAll( translationUnit ); |
---|
[0da3e2c] | 681 | } // dump |
---|
[1ab4ce2] | 682 | |
---|
[51b7345] | 683 | // Local Variables: // |
---|
[b87a5ed] | 684 | // tab-width: 4 // |
---|
| 685 | // mode: c++ // |
---|
| 686 | // compile-command: "make install" // |
---|
[51b7345] | 687 | // End: // |
---|