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