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