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