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