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