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