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