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