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