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