| 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 | //
|
|---|
| 7 | // main.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Peter Buhr and Rob Schluntz
|
|---|
| 10 | // Created On : Fri May 15 23:12:02 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Thu Oct 8 18:17:46 2020
|
|---|
| 13 | // Update Count : 637
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 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 <cassert> // for assertf
|
|---|
| 20 | #include <cstdio> // for fopen, FILE, fclose, stdin
|
|---|
| 21 | #include <cstdlib> // for exit, free, abort, EXIT_F...
|
|---|
| 22 | #include <csignal> // for signal, SIGABRT, SIGSEGV
|
|---|
| 23 | #include <cstring> // for index
|
|---|
| 24 | #include <fstream> // for ofstream
|
|---|
| 25 | #include <iostream> // for operator<<, basic_ostream
|
|---|
| 26 | #include <iomanip>
|
|---|
| 27 | #include <iterator> // for back_inserter
|
|---|
| 28 | #include <list> // for list
|
|---|
| 29 | #include <string> // for char_traits, operator<<
|
|---|
| 30 |
|
|---|
| 31 | using namespace std;
|
|---|
| 32 |
|
|---|
| 33 | #include "AST/Convert.hpp"
|
|---|
| 34 | #include "CompilationState.h"
|
|---|
| 35 | #include "../config.h" // for CFA_LIBDIR
|
|---|
| 36 | #include "CodeGen/FixMain.h" // for FixMain
|
|---|
| 37 | #include "CodeGen/FixNames.h" // for fixNames
|
|---|
| 38 | #include "CodeGen/Generate.h" // for generate
|
|---|
| 39 | #include "CodeTools/DeclStats.h" // for printDeclStats
|
|---|
| 40 | #include "CodeTools/ResolvProtoDump.h" // for dumpAsResolvProto
|
|---|
| 41 | #include "CodeTools/TrackLoc.h" // for fillLocations
|
|---|
| 42 | #include "Common/CompilerError.h" // for CompilerError
|
|---|
| 43 | #include "Common/Stats.h"
|
|---|
| 44 | #include "Common/PassVisitor.h"
|
|---|
| 45 | #include "Common/SemanticError.h" // for SemanticError
|
|---|
| 46 | #include "Common/UnimplementedError.h" // for UnimplementedError
|
|---|
| 47 | #include "Common/utility.h" // for deleteAll, filter, printAll
|
|---|
| 48 | #include "Concurrency/Waitfor.h" // for generateWaitfor
|
|---|
| 49 | #include "ControlStruct/ExceptTranslate.h" // for translateEHM
|
|---|
| 50 | #include "ControlStruct/Mutate.h" // for mutate
|
|---|
| 51 | #include "GenPoly/Box.h" // for box
|
|---|
| 52 | #include "GenPoly/InstantiateGeneric.h" // for instantiateGeneric
|
|---|
| 53 | #include "GenPoly/Lvalue.h" // for convertLvalue
|
|---|
| 54 | #include "GenPoly/Specialize.h" // for convertSpecializations
|
|---|
| 55 | #include "InitTweak/FixInit.h" // for fix
|
|---|
| 56 | #include "InitTweak/GenInit.h" // for genInit
|
|---|
| 57 | #include "MakeLibCfa.h" // for makeLibCfa
|
|---|
| 58 | #include "Parser/ParseNode.h" // for DeclarationNode, buildList
|
|---|
| 59 | #include "Parser/TypedefTable.h" // for TypedefTable
|
|---|
| 60 | #include "ResolvExpr/AlternativePrinter.h" // for AlternativePrinter
|
|---|
| 61 | #include "ResolvExpr/Resolver.h" // for resolve
|
|---|
| 62 | #include "SymTab/Validate.h" // for validate
|
|---|
| 63 | #include "SynTree/LinkageSpec.h" // for Spec, Cforall, Intrinsic
|
|---|
| 64 | #include "SynTree/Declaration.h" // for Declaration
|
|---|
| 65 | #include "SynTree/Visitor.h" // for acceptAll
|
|---|
| 66 | #include "Tuples/Tuples.h" // for expandMemberTuples, expan...
|
|---|
| 67 | #include "Virtual/ExpandCasts.h" // for expandCasts
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | static void NewPass( const char * const name ) {
|
|---|
| 71 | Stats::Heap::newPass( name );
|
|---|
| 72 | using namespace Stats::Counters;
|
|---|
| 73 | {
|
|---|
| 74 | static auto group = build<CounterGroup>( "Pass Visitor" );
|
|---|
| 75 | auto pass = build<CounterGroup>( name, group );
|
|---|
| 76 | pass_visitor_stats.depth = 0;
|
|---|
| 77 | pass_visitor_stats.avg = build<AverageCounter<double>>( "Average Depth", pass );
|
|---|
| 78 | pass_visitor_stats.max = build<MaxCounter<double>>( "Max Depth", pass );
|
|---|
| 79 | }
|
|---|
| 80 | {
|
|---|
| 81 | static auto group = build<CounterGroup>( "Syntax Node" );
|
|---|
| 82 | auto pass = build<CounterGroup>( name, group );
|
|---|
| 83 | BaseSyntaxNode::new_nodes = build<SimpleCounter>( "Allocs", pass );
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | #define PASS( name, pass ) \
|
|---|
| 88 | if ( errorp ) { cerr << name << endl; } \
|
|---|
| 89 | NewPass(name); \
|
|---|
| 90 | Stats::Time::StartBlock(name); \
|
|---|
| 91 | pass; \
|
|---|
| 92 | Stats::Time::StopBlock();
|
|---|
| 93 |
|
|---|
| 94 | LinkageSpec::Spec linkage = LinkageSpec::Cforall;
|
|---|
| 95 | TypedefTable typedefTable;
|
|---|
| 96 | DeclarationNode * parseTree = nullptr; // program parse tree
|
|---|
| 97 |
|
|---|
| 98 | static bool waiting_for_gdb = false; // flag to set cfa-cpp to wait for gdb on start
|
|---|
| 99 |
|
|---|
| 100 | static string PreludeDirector = "";
|
|---|
| 101 |
|
|---|
| 102 | static void parse_cmdline( int argc, char *argv[] );
|
|---|
| 103 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
|
|---|
| 104 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
|
|---|
| 105 |
|
|---|
| 106 | static void backtrace( int start ) { // skip first N stack frames
|
|---|
| 107 | enum { Frames = 50, }; // maximum number of stack frames
|
|---|
| 108 | void * array[Frames];
|
|---|
| 109 | size_t size = ::backtrace( array, Frames );
|
|---|
| 110 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names
|
|---|
| 111 |
|
|---|
| 112 | *index( messages[0], '(' ) = '\0'; // find executable name
|
|---|
| 113 | cerr << "Stack back trace for: " << messages[0] << endl;
|
|---|
| 114 |
|
|---|
| 115 | // skip last 2 stack frames after main
|
|---|
| 116 | for ( unsigned int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
|
|---|
| 117 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
|
|---|
| 118 |
|
|---|
| 119 | for ( char * p = messages[i]; *p; p += 1 ) { // find parantheses and +offset
|
|---|
| 120 | if ( *p == '(' ) {
|
|---|
| 121 | mangled_name = p;
|
|---|
| 122 | } else if ( *p == '+' ) {
|
|---|
| 123 | offset_begin = p;
|
|---|
| 124 | } else if ( *p == ')' ) {
|
|---|
| 125 | offset_end = p;
|
|---|
| 126 | break;
|
|---|
| 127 | } // if
|
|---|
| 128 | } // for
|
|---|
| 129 |
|
|---|
| 130 | // if line contains symbol, attempt to demangle
|
|---|
| 131 | int frameNo = i - start;
|
|---|
| 132 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
|
|---|
| 133 | *mangled_name++ = '\0'; // delimit strings
|
|---|
| 134 | *offset_begin++ = '\0';
|
|---|
| 135 | *offset_end++ = '\0';
|
|---|
| 136 |
|
|---|
| 137 | int status;
|
|---|
| 138 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
|
|---|
| 139 | // bug in __cxa_demangle for single-character lower-case non-mangled names
|
|---|
| 140 | if ( status == 0 ) { // demangling successful ?
|
|---|
| 141 | cerr << "(" << frameNo << ") " << messages[i] << " : "
|
|---|
| 142 | << real_name << "+" << offset_begin << offset_end << endl;
|
|---|
| 143 | } else { // otherwise, output mangled name
|
|---|
| 144 | cerr << "(" << frameNo << ") " << messages[i] << " : "
|
|---|
| 145 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl;
|
|---|
| 146 | } // if
|
|---|
| 147 |
|
|---|
| 148 | free( real_name );
|
|---|
| 149 | } else { // otherwise, print the whole line
|
|---|
| 150 | cerr << "(" << frameNo << ") " << messages[i] << endl;
|
|---|
| 151 | } // if
|
|---|
| 152 | } // for
|
|---|
| 153 |
|
|---|
| 154 | free( messages );
|
|---|
| 155 | } // backtrace
|
|---|
| 156 |
|
|---|
| 157 | #define SIGPARMS int sig __attribute__(( unused )), siginfo_t * sfp __attribute__(( unused )), ucontext_t * cxt __attribute__(( unused ))
|
|---|
| 158 |
|
|---|
| 159 | static void Signal( int sig, void (*handler)(SIGPARMS), int flags ) {
|
|---|
| 160 | struct sigaction act;
|
|---|
| 161 |
|
|---|
| 162 | act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
|
|---|
| 163 | act.sa_flags = flags;
|
|---|
| 164 |
|
|---|
| 165 | if ( sigaction( sig, &act, nullptr ) == -1 ) {
|
|---|
| 166 | cerr << "*CFA runtime error* problem installing signal handler, error(" << errno << ") " << strerror( errno ) << endl;
|
|---|
| 167 | _exit( EXIT_FAILURE );
|
|---|
| 168 | } // if
|
|---|
| 169 | } // Signal
|
|---|
| 170 |
|
|---|
| 171 | static void sigSegvBusHandler( SIGPARMS ) {
|
|---|
| 172 | if ( sfp->si_addr == nullptr ) {
|
|---|
| 173 | cerr << "Null pointer (nullptr) dereference." << endl;
|
|---|
| 174 | } else {
|
|---|
| 175 | cerr << (sig == SIGSEGV ? "Segment fault" : "Bus error") << " at memory location " << sfp->si_addr << "." << endl
|
|---|
| 176 | << "Possible cause is reading outside the address space or writing to a protected area within the address space with an invalid pointer or subscript." << endl;
|
|---|
| 177 | } // if
|
|---|
| 178 | backtrace( 2 ); // skip first 2 stack frames
|
|---|
| 179 | abort(); // cause core dump for debugging
|
|---|
| 180 | } // sigSegvBusHandler
|
|---|
| 181 |
|
|---|
| 182 | static void sigFpeHandler( SIGPARMS ) {
|
|---|
| 183 | const char * msg;
|
|---|
| 184 |
|
|---|
| 185 | switch ( sfp->si_code ) {
|
|---|
| 186 | case FPE_INTDIV: case FPE_FLTDIV: msg = "divide by zero"; break;
|
|---|
| 187 | case FPE_FLTOVF: msg = "overflow"; break;
|
|---|
| 188 | case FPE_FLTUND: msg = "underflow"; break;
|
|---|
| 189 | case FPE_FLTRES: msg = "inexact result"; break;
|
|---|
| 190 | case FPE_FLTINV: msg = "invalid operation"; break;
|
|---|
| 191 | default: msg = "unknown";
|
|---|
| 192 | } // choose
|
|---|
| 193 | cerr << "Computation error " << msg << " at location " << sfp->si_addr << endl
|
|---|
| 194 | << "Possible cause is constant-expression evaluation invalid." << endl;
|
|---|
| 195 | backtrace( 2 ); // skip first 2 stack frames
|
|---|
| 196 | abort(); // cause core dump for debugging
|
|---|
| 197 | } // sigFpeHandler
|
|---|
| 198 |
|
|---|
| 199 | static void sigAbortHandler( SIGPARMS ) {
|
|---|
| 200 | backtrace( 6 ); // skip first 6 stack frames
|
|---|
| 201 | Signal( SIGABRT, (void (*)(SIGPARMS))SIG_DFL, SA_SIGINFO ); // reset default signal handler
|
|---|
| 202 | raise( SIGABRT ); // reraise SIGABRT
|
|---|
| 203 | } // sigAbortHandler
|
|---|
| 204 |
|
|---|
| 205 | int main( int argc, char * argv[] ) {
|
|---|
| 206 | FILE * input; // use FILE rather than istream because yyin is FILE
|
|---|
| 207 | ostream * output = & cout;
|
|---|
| 208 | list< Declaration * > translationUnit;
|
|---|
| 209 |
|
|---|
| 210 | Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO );
|
|---|
| 211 | Signal( SIGBUS, sigSegvBusHandler, SA_SIGINFO );
|
|---|
| 212 | Signal( SIGFPE, sigFpeHandler, SA_SIGINFO );
|
|---|
| 213 | Signal( SIGABRT, sigAbortHandler, SA_SIGINFO );
|
|---|
| 214 |
|
|---|
| 215 | // cout << "main" << endl;
|
|---|
| 216 | // for ( int i = 0; i < argc; i += 1 ) {
|
|---|
| 217 | // cout << '\t' << argv[i] << endl;
|
|---|
| 218 | // } // for
|
|---|
| 219 |
|
|---|
| 220 | parse_cmdline( argc, argv ); // process command-line arguments
|
|---|
| 221 | CodeGen::FixMain::setReplaceMain( !nomainp );
|
|---|
| 222 |
|
|---|
| 223 | if ( waiting_for_gdb ) {
|
|---|
| 224 | cerr << "Waiting for gdb" << endl;
|
|---|
| 225 | cerr << "run :" << endl;
|
|---|
| 226 | cerr << " gdb attach " << getpid() << endl;
|
|---|
| 227 | raise(SIGSTOP);
|
|---|
| 228 | } // if
|
|---|
| 229 |
|
|---|
| 230 | try {
|
|---|
| 231 | // choose to read the program from a file or stdin
|
|---|
| 232 | if ( optind < argc ) { // any commands after the flags ? => input file name
|
|---|
| 233 | input = fopen( argv[ optind ], "r" );
|
|---|
| 234 | assertf( input, "cannot open %s because %s\n", argv[ optind ], strerror( errno ) );
|
|---|
| 235 | optind += 1;
|
|---|
| 236 | } else { // no input file name
|
|---|
| 237 | input = stdin;
|
|---|
| 238 | } // if
|
|---|
| 239 |
|
|---|
| 240 | Stats::Time::StartGlobal();
|
|---|
| 241 | NewPass("Parse");
|
|---|
| 242 | Stats::Time::StartBlock("Parse");
|
|---|
| 243 |
|
|---|
| 244 | // read in the builtins, extras, and the prelude
|
|---|
| 245 | if ( ! nopreludep ) { // include gcc builtins
|
|---|
| 246 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
|
|---|
| 247 |
|
|---|
| 248 | assertf( !PreludeDirector.empty(), "Can't find prelude without option --prelude-dir must be used." );
|
|---|
| 249 |
|
|---|
| 250 | // Read to gcc builtins, if not generating the cfa library
|
|---|
| 251 | FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" );
|
|---|
| 252 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" );
|
|---|
| 253 | parse( gcc_builtins, LinkageSpec::Compiler );
|
|---|
| 254 |
|
|---|
| 255 | // read the extra prelude in, if not generating the cfa library
|
|---|
| 256 | FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" );
|
|---|
| 257 | assertf( extras, "cannot open extras.cf\n" );
|
|---|
| 258 | parse( extras, LinkageSpec::BuiltinC );
|
|---|
| 259 |
|
|---|
| 260 | if ( ! libcfap ) {
|
|---|
| 261 | // read the prelude in, if not generating the cfa library
|
|---|
| 262 | FILE * prelude = fopen( (PreludeDirector + "/prelude.cfa").c_str(), "r" );
|
|---|
| 263 | assertf( prelude, "cannot open prelude.cfa\n" );
|
|---|
| 264 | parse( prelude, LinkageSpec::Intrinsic );
|
|---|
| 265 |
|
|---|
| 266 | // Read to cfa builtins, if not generating the cfa library
|
|---|
| 267 | FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" );
|
|---|
| 268 | assertf( builtins, "cannot open builtins.cf\n" );
|
|---|
| 269 | parse( builtins, LinkageSpec::BuiltinCFA );
|
|---|
| 270 | } // if
|
|---|
| 271 | } // if
|
|---|
| 272 |
|
|---|
| 273 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
|
|---|
| 274 |
|
|---|
| 275 | if ( parsep ) {
|
|---|
| 276 | parseTree->printList( cout );
|
|---|
| 277 | delete parseTree;
|
|---|
| 278 | return EXIT_SUCCESS;
|
|---|
| 279 | } // if
|
|---|
| 280 |
|
|---|
| 281 | buildList( parseTree, translationUnit );
|
|---|
| 282 | delete parseTree;
|
|---|
| 283 | parseTree = nullptr;
|
|---|
| 284 |
|
|---|
| 285 | if ( astp ) {
|
|---|
| 286 | dump( translationUnit );
|
|---|
| 287 | return EXIT_SUCCESS;
|
|---|
| 288 | } // if
|
|---|
| 289 |
|
|---|
| 290 | // Temporary: fill locations after parsing so that every node has a location, for early error messages.
|
|---|
| 291 | // Eventually we should pass the locations from the parser to every node, but this quick and dirty solution
|
|---|
| 292 | // works okay for now.
|
|---|
| 293 | CodeTools::fillLocations( translationUnit );
|
|---|
| 294 | Stats::Time::StopBlock();
|
|---|
| 295 |
|
|---|
| 296 | // add the assignment statement after the initialization of a type parameter
|
|---|
| 297 | PASS( "Validate", SymTab::validate( translationUnit, symtabp ) );
|
|---|
| 298 | if ( symtabp ) {
|
|---|
| 299 | deleteAll( translationUnit );
|
|---|
| 300 | return EXIT_SUCCESS;
|
|---|
| 301 | } // if
|
|---|
| 302 |
|
|---|
| 303 | if ( expraltp ) {
|
|---|
| 304 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout );
|
|---|
| 305 | acceptAll( translationUnit, printer );
|
|---|
| 306 | return EXIT_SUCCESS;
|
|---|
| 307 | } // if
|
|---|
| 308 |
|
|---|
| 309 | if ( validp ) {
|
|---|
| 310 | dump( translationUnit );
|
|---|
| 311 | return EXIT_SUCCESS;
|
|---|
| 312 | } // if
|
|---|
| 313 |
|
|---|
| 314 | PASS( "Translate Throws", ControlStruct::translateThrows( translationUnit ) );
|
|---|
| 315 | PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) );
|
|---|
| 316 | PASS( "Fix Names", CodeGen::fixNames( translationUnit ) );
|
|---|
| 317 | PASS( "Gen Init", InitTweak::genInit( translationUnit ) );
|
|---|
| 318 | PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( translationUnit ) );
|
|---|
| 319 | if ( libcfap ) {
|
|---|
| 320 | // generate the bodies of cfa library functions
|
|---|
| 321 | LibCfa::makeLibCfa( translationUnit );
|
|---|
| 322 | } // if
|
|---|
| 323 |
|
|---|
| 324 | if ( declstatsp ) {
|
|---|
| 325 | CodeTools::printDeclStats( translationUnit );
|
|---|
| 326 | deleteAll( translationUnit );
|
|---|
| 327 | return EXIT_SUCCESS;
|
|---|
| 328 | } // if
|
|---|
| 329 |
|
|---|
| 330 | if ( bresolvep ) {
|
|---|
| 331 | dump( translationUnit );
|
|---|
| 332 | return EXIT_SUCCESS;
|
|---|
| 333 | } // if
|
|---|
| 334 |
|
|---|
| 335 | CodeTools::fillLocations( translationUnit );
|
|---|
| 336 |
|
|---|
| 337 | if ( resolvprotop ) {
|
|---|
| 338 | CodeTools::dumpAsResolvProto( translationUnit );
|
|---|
| 339 | return EXIT_SUCCESS;
|
|---|
| 340 | } // if
|
|---|
| 341 |
|
|---|
| 342 | if( useNewAST) {
|
|---|
| 343 | auto transUnit = convert( move( translationUnit ) );
|
|---|
| 344 | PASS( "Resolve", ResolvExpr::resolve( transUnit ) );
|
|---|
| 345 | if ( exprp ) {
|
|---|
| 346 | translationUnit = convert( move( transUnit ) );
|
|---|
| 347 | dump( translationUnit );
|
|---|
| 348 | return EXIT_SUCCESS;
|
|---|
| 349 | } // if
|
|---|
| 350 |
|
|---|
| 351 | PASS( "Fix Init", InitTweak::fix(transUnit, buildingLibrary()));
|
|---|
| 352 | translationUnit = convert( move( transUnit ) );
|
|---|
| 353 | } else {
|
|---|
| 354 | PASS( "Resolve", ResolvExpr::resolve( translationUnit ) );
|
|---|
| 355 | if ( exprp ) {
|
|---|
| 356 | dump( translationUnit );
|
|---|
| 357 | return EXIT_SUCCESS;
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) );
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | // fix ObjectDecl - replaces ConstructorInit nodes
|
|---|
| 364 | if ( ctorinitp ) {
|
|---|
| 365 | dump ( translationUnit );
|
|---|
| 366 | return EXIT_SUCCESS;
|
|---|
| 367 | } // if
|
|---|
| 368 |
|
|---|
| 369 | 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
|
|---|
| 370 |
|
|---|
| 371 | PASS( "Translate Tries" , ControlStruct::translateTries( translationUnit ) );
|
|---|
| 372 |
|
|---|
| 373 | PASS( "Gen Waitfor" , Concurrency::generateWaitFor( translationUnit ) );
|
|---|
| 374 |
|
|---|
| 375 | PASS( "Convert Specializations", GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded
|
|---|
| 376 |
|
|---|
| 377 | PASS( "Expand Tuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this?
|
|---|
| 378 |
|
|---|
| 379 | if ( tuplep ) {
|
|---|
| 380 | dump( translationUnit );
|
|---|
| 381 | return EXIT_SUCCESS;
|
|---|
| 382 | } // if
|
|---|
| 383 |
|
|---|
| 384 | PASS( "Virtual Expand Casts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM
|
|---|
| 385 |
|
|---|
| 386 | PASS( "Instantiate Generics", GenPoly::instantiateGeneric( translationUnit ) );
|
|---|
| 387 | if ( genericsp ) {
|
|---|
| 388 | dump( translationUnit );
|
|---|
| 389 | return EXIT_SUCCESS;
|
|---|
| 390 | } // if
|
|---|
| 391 |
|
|---|
| 392 | PASS( "Convert L-Value", GenPoly::convertLvalue( translationUnit ) );
|
|---|
| 393 |
|
|---|
| 394 | if ( bboxp ) {
|
|---|
| 395 | dump( translationUnit );
|
|---|
| 396 | return EXIT_SUCCESS;
|
|---|
| 397 | } // if
|
|---|
| 398 | PASS( "Box", GenPoly::box( translationUnit ) );
|
|---|
| 399 |
|
|---|
| 400 | if ( bcodegenp ) {
|
|---|
| 401 | dump( translationUnit );
|
|---|
| 402 | return EXIT_SUCCESS;
|
|---|
| 403 | } // if
|
|---|
| 404 |
|
|---|
| 405 | if ( optind < argc ) { // any commands after the flags and input file ? => output file name
|
|---|
| 406 | output = new ofstream( argv[ optind ] );
|
|---|
| 407 | } // if
|
|---|
| 408 |
|
|---|
| 409 | CodeTools::fillLocations( translationUnit );
|
|---|
| 410 | PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) );
|
|---|
| 411 |
|
|---|
| 412 | CodeGen::FixMain::fix( *output, (PreludeDirector + "/bootloader.c").c_str() );
|
|---|
| 413 | if ( output != &cout ) {
|
|---|
| 414 | delete output;
|
|---|
| 415 | } // if
|
|---|
| 416 | } catch ( SemanticErrorException &e ) {
|
|---|
| 417 | if ( errorp ) {
|
|---|
| 418 | cerr << "---AST at error:---" << endl;
|
|---|
| 419 | dump( translationUnit, cerr );
|
|---|
| 420 | cerr << endl << "---End of AST, begin error message:---\n" << endl;
|
|---|
| 421 | } // if
|
|---|
| 422 | e.print();
|
|---|
| 423 | if ( output != &cout ) {
|
|---|
| 424 | delete output;
|
|---|
| 425 | } // if
|
|---|
| 426 | return EXIT_FAILURE;
|
|---|
| 427 | } catch ( UnimplementedError &e ) {
|
|---|
| 428 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
|
|---|
| 429 | if ( output != &cout ) {
|
|---|
| 430 | delete output;
|
|---|
| 431 | } // if
|
|---|
| 432 | return EXIT_FAILURE;
|
|---|
| 433 | } catch ( CompilerError &e ) {
|
|---|
| 434 | cerr << "Compiler Error: " << e.get_what() << endl;
|
|---|
| 435 | cerr << "(please report bugs to [REDACTED])" << endl;
|
|---|
| 436 | if ( output != &cout ) {
|
|---|
| 437 | delete output;
|
|---|
| 438 | } // if
|
|---|
| 439 | return EXIT_FAILURE;
|
|---|
| 440 | } catch ( ... ) {
|
|---|
| 441 | exception_ptr eptr = current_exception();
|
|---|
| 442 | try {
|
|---|
| 443 | if (eptr) {
|
|---|
| 444 | rethrow_exception(eptr);
|
|---|
| 445 | } else {
|
|---|
| 446 | cerr << "Exception Uncaught and Unknown" << endl;
|
|---|
| 447 | } // if
|
|---|
| 448 | } catch(const exception& e) {
|
|---|
| 449 | cerr << "Uncaught Exception \"" << e.what() << "\"\n";
|
|---|
| 450 | } // try
|
|---|
| 451 | return EXIT_FAILURE;
|
|---|
| 452 | } // try
|
|---|
| 453 |
|
|---|
| 454 | deleteAll( translationUnit );
|
|---|
| 455 | Stats::print();
|
|---|
| 456 | return EXIT_SUCCESS;
|
|---|
| 457 | } // main
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 | static const char optstring[] = ":c:ghlLmNnpdOAP:S:twW:D:";
|
|---|
| 461 |
|
|---|
| 462 | enum { PreludeDir = 128 };
|
|---|
| 463 | static struct option long_opts[] = {
|
|---|
| 464 | { "colors", required_argument, nullptr, 'c' },
|
|---|
| 465 | { "gdb", no_argument, nullptr, 'g' },
|
|---|
| 466 | { "help", no_argument, nullptr, 'h' },
|
|---|
| 467 | { "libcfa", no_argument, nullptr, 'l' },
|
|---|
| 468 | { "linemarks", no_argument, nullptr, 'L' },
|
|---|
| 469 | { "no-main", no_argument, 0, 'm' },
|
|---|
| 470 | { "no-linemarks", no_argument, nullptr, 'N' },
|
|---|
| 471 | { "no-prelude", no_argument, nullptr, 'n' },
|
|---|
| 472 | { "prototypes", no_argument, nullptr, 'p' },
|
|---|
| 473 | { "deterministic-out", no_argument, nullptr, 'd' },
|
|---|
| 474 | { "old-ast", no_argument, nullptr, 'O'},
|
|---|
| 475 | { "new-ast", no_argument, nullptr, 'A'},
|
|---|
| 476 | { "print", required_argument, nullptr, 'P' },
|
|---|
| 477 | { "prelude-dir", required_argument, nullptr, PreludeDir },
|
|---|
| 478 | { "statistics", required_argument, nullptr, 'S' },
|
|---|
| 479 | { "tree", no_argument, nullptr, 't' },
|
|---|
| 480 | { "", no_argument, nullptr, 0 }, // -w
|
|---|
| 481 | { "", no_argument, nullptr, 0 }, // -W
|
|---|
| 482 | { "", no_argument, nullptr, 0 }, // -D
|
|---|
| 483 | { nullptr, 0, nullptr, 0 }
|
|---|
| 484 | }; // long_opts
|
|---|
| 485 |
|
|---|
| 486 | static const char * description[] = {
|
|---|
| 487 | "diagnostic color: never, always, or auto.", // -c
|
|---|
| 488 | "wait for gdb to attach", // -g
|
|---|
| 489 | "print help message", // -h
|
|---|
| 490 | "generate libcfa.c", // -l
|
|---|
| 491 | "generate line marks", // -L
|
|---|
| 492 | "do not replace main", // -m
|
|---|
| 493 | "do not generate line marks", // -N
|
|---|
| 494 | "do not read prelude", // -n
|
|---|
| 495 | "generate prototypes for prelude functions", // -p
|
|---|
| 496 | "only print deterministic output", // -d
|
|---|
| 497 | "Use the old-ast", // -O
|
|---|
| 498 | "Use the new-ast", // -A
|
|---|
| 499 | "print", // -P
|
|---|
| 500 | "<directory> prelude directory for debug/nodebug", // no flag
|
|---|
| 501 | "<option-list> enable profiling information:\n counters,heap,time,all,none", // -S
|
|---|
| 502 | "building cfa standard lib", // -t
|
|---|
| 503 | "", // -w
|
|---|
| 504 | "", // -W
|
|---|
| 505 | "", // -D
|
|---|
| 506 | }; // description
|
|---|
| 507 |
|
|---|
| 508 | static_assert( sizeof( long_opts ) / sizeof( long_opts[0] ) - 1 == sizeof( description ) / sizeof( description[0] ), "Long opts and description must match" );
|
|---|
| 509 |
|
|---|
| 510 | static struct Printopts {
|
|---|
| 511 | const char * name;
|
|---|
| 512 | int & flag;
|
|---|
| 513 | int val;
|
|---|
| 514 | const char * descript;
|
|---|
| 515 | } printopts[] = {
|
|---|
| 516 | { "ascodegen", codegenp, true, "print AST as codegen rather than AST" },
|
|---|
| 517 | { "asterr", errorp, true, "print AST on error" },
|
|---|
| 518 | { "declstats", declstatsp, true, "code property statistics" },
|
|---|
| 519 | { "parse", yydebug, true, "yacc (parsing) debug information" },
|
|---|
| 520 | { "pretty", prettycodegenp, true, "prettyprint for ascodegen flag" },
|
|---|
| 521 | { "rproto", resolvprotop, true, "resolver-proto instance" },
|
|---|
| 522 | { "rsteps", resolvep, true, "print resolver steps" },
|
|---|
| 523 | { "tree", parsep, true, "print parse tree" },
|
|---|
| 524 | // code dumps
|
|---|
| 525 | { "ast", astp, true, "print AST after parsing" },
|
|---|
| 526 | { "symevt", symtabp, true, "print AST after symbol table events" },
|
|---|
| 527 | { "altexpr", expraltp, true, "print alternatives for expressions" },
|
|---|
| 528 | { "astdecl", validp, true, "print AST after declaration validation pass" },
|
|---|
| 529 | { "resolver", bresolvep, true, "print AST before resolver step" },
|
|---|
| 530 | { "astexpr", exprp, true, "print AST after expression analysis" },
|
|---|
| 531 | { "ctordtor", ctorinitp, true, "print AST after ctor/dtor are replaced" },
|
|---|
| 532 | { "tuple", tuplep, true, "print AST after tuple expansion" },
|
|---|
| 533 | { "astgen", genericsp, true, "print AST after instantiate generics" },
|
|---|
| 534 | { "box", bboxp, true, "print AST before box step" },
|
|---|
| 535 | { "codegen", bcodegenp, true, "print AST before code generation" },
|
|---|
| 536 | };
|
|---|
| 537 | enum { printoptsSize = sizeof( printopts ) / sizeof( printopts[0] ) };
|
|---|
| 538 |
|
|---|
| 539 | static void usage( char *argv[] ) {
|
|---|
| 540 | cout << "Usage: " << argv[0] << " [options] [input-file (default stdin)] [output-file (default stdout)], where options are:" << endl;
|
|---|
| 541 | int i = 0, j = 1; // j skips starting colon
|
|---|
| 542 | for ( ; long_opts[i].name != 0 && optstring[j] != '\0'; i += 1, j += 1 ) {
|
|---|
| 543 | if ( long_opts[i].name[0] != '\0' ) { // hidden option, internal usage only
|
|---|
| 544 | if ( strcmp( long_opts[i].name, "prelude-dir" ) != 0 ) { // flag
|
|---|
| 545 | cout << " -" << optstring[j] << ",";
|
|---|
| 546 | } else { // no flag
|
|---|
| 547 | j -= 1; // compensate
|
|---|
| 548 | cout << " ";
|
|---|
| 549 | } // if
|
|---|
| 550 | cout << " --" << left << setw(12) << long_opts[i].name << " ";
|
|---|
| 551 | if ( strcmp( long_opts[i].name, "print" ) == 0 ) {
|
|---|
| 552 | cout << "one of: " << endl;
|
|---|
| 553 | for ( int i = 0; i < printoptsSize; i += 1 ) {
|
|---|
| 554 | cout << setw(10) << " " << left << setw(10) << printopts[i].name << " " << printopts[i].descript << endl;
|
|---|
| 555 | } // for
|
|---|
| 556 | } else {
|
|---|
| 557 | cout << description[i] << endl;
|
|---|
| 558 | } // if
|
|---|
| 559 | } // if
|
|---|
| 560 | if ( optstring[j + 1] == ':' ) j += 1;
|
|---|
| 561 | } // for
|
|---|
| 562 | if ( long_opts[i].name != 0 || optstring[j] != '\0' ) assertf( false, "internal error, mismatch of option flags and names\n" );
|
|---|
| 563 | exit( EXIT_FAILURE );
|
|---|
| 564 | } // usage
|
|---|
| 565 |
|
|---|
| 566 | static void parse_cmdline( int argc, char * argv[] ) {
|
|---|
| 567 | opterr = 0; // (global) prevent getopt from printing error messages
|
|---|
| 568 |
|
|---|
| 569 | bool Wsuppress = false, Werror = false;
|
|---|
| 570 | int c;
|
|---|
| 571 | while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) {
|
|---|
| 572 | switch ( c ) {
|
|---|
| 573 | case 'c': // diagnostic colors
|
|---|
| 574 | if ( strcmp( optarg, "always" ) == 0 ) {
|
|---|
| 575 | ErrorHelpers::colors = ErrorHelpers::Colors::Always;
|
|---|
| 576 | } else if ( strcmp( optarg, "never" ) == 0 ) {
|
|---|
| 577 | ErrorHelpers::colors = ErrorHelpers::Colors::Never;
|
|---|
| 578 | } else if ( strcmp( optarg, "auto" ) == 0 ) {
|
|---|
| 579 | ErrorHelpers::colors = ErrorHelpers::Colors::Auto;
|
|---|
| 580 | } // if
|
|---|
| 581 | break;
|
|---|
| 582 | case 'h': // help message
|
|---|
| 583 | usage( argv ); // no return
|
|---|
| 584 | break;
|
|---|
| 585 | case 'l': // generate libcfa.c
|
|---|
| 586 | libcfap = true;
|
|---|
| 587 | break;
|
|---|
| 588 | case 'L': // generate line marks
|
|---|
| 589 | linemarks = true;
|
|---|
| 590 | break;
|
|---|
| 591 | case 'm': // do not replace main
|
|---|
| 592 | nomainp = true;
|
|---|
| 593 | break;
|
|---|
| 594 | case 'N': // do not generate line marks
|
|---|
| 595 | linemarks = false;
|
|---|
| 596 | break;
|
|---|
| 597 | case 'n': // do not read prelude
|
|---|
| 598 | nopreludep = true;
|
|---|
| 599 | break;
|
|---|
| 600 | case 'p': // generate prototypes for prelude functions
|
|---|
| 601 | genproto = true;
|
|---|
| 602 | break;
|
|---|
| 603 | case 'd': // don't print non-deterministic output
|
|---|
| 604 | deterministic_output = true;
|
|---|
| 605 | break;
|
|---|
| 606 | case 'O': // don't print non-deterministic output
|
|---|
| 607 | useNewAST = false;
|
|---|
| 608 | break;
|
|---|
| 609 | case 'A': // don't print non-deterministic output
|
|---|
| 610 | useNewAST = true;
|
|---|
| 611 | break;
|
|---|
| 612 | case 'P': // print options
|
|---|
| 613 | for ( int i = 0;; i += 1 ) {
|
|---|
| 614 | if ( i == printoptsSize ) {
|
|---|
| 615 | cout << "Unknown --print option " << optarg << endl;
|
|---|
| 616 | goto Default;
|
|---|
| 617 | } // if
|
|---|
| 618 | if ( strcmp( optarg, printopts[i].name ) == 0 ) {
|
|---|
| 619 | printopts[i].flag = printopts[i].val;
|
|---|
| 620 | break;
|
|---|
| 621 | } // if
|
|---|
| 622 | } // for
|
|---|
| 623 | break;
|
|---|
| 624 | case PreludeDir: // prelude directory for debug/nodebug, hidden
|
|---|
| 625 | PreludeDirector = optarg;
|
|---|
| 626 | break;
|
|---|
| 627 | case 'S': // enable profiling information, argument comma separated list of names
|
|---|
| 628 | Stats::parse_params( optarg );
|
|---|
| 629 | break;
|
|---|
| 630 | case 't': // building cfa stdlib
|
|---|
| 631 | treep = true;
|
|---|
| 632 | break;
|
|---|
| 633 | case 'g': // wait for gdb
|
|---|
| 634 | waiting_for_gdb = true;
|
|---|
| 635 | break;
|
|---|
| 636 | case 'w': // suppress all warnings, hidden
|
|---|
| 637 | Wsuppress = true;
|
|---|
| 638 | break;
|
|---|
| 639 | case 'W': // coordinate gcc -W with CFA, hidden
|
|---|
| 640 | if ( strcmp( optarg, "all" ) == 0 ) {
|
|---|
| 641 | SemanticWarning_EnableAll();
|
|---|
| 642 | } else if ( strcmp( optarg, "error" ) == 0 ) {
|
|---|
| 643 | Werror = true;
|
|---|
| 644 | } else {
|
|---|
| 645 | char * warning = optarg;
|
|---|
| 646 | Severity s;
|
|---|
| 647 | if ( strncmp( optarg, "no-", 3 ) == 0 ) {
|
|---|
| 648 | warning += 3;
|
|---|
| 649 | s = Severity::Suppress;
|
|---|
| 650 | } else {
|
|---|
| 651 | s = Severity::Warn;
|
|---|
| 652 | } // if
|
|---|
| 653 | SemanticWarning_Set( warning, s );
|
|---|
| 654 | } // if
|
|---|
| 655 | break;
|
|---|
| 656 | case 'D': // ignore -Dxxx, forwarded by cpp, hidden
|
|---|
| 657 | break;
|
|---|
| 658 | case '?': // unknown option
|
|---|
| 659 | if ( optopt ) { // short option ?
|
|---|
| 660 | cout << "Unknown option -" << (char)optopt << endl;
|
|---|
| 661 | } else {
|
|---|
| 662 | cout << "Unknown option " << argv[optind - 1] << endl;
|
|---|
| 663 | } // if
|
|---|
| 664 | goto Default;
|
|---|
| 665 | case ':': // missing option
|
|---|
| 666 | if ( optopt ) { // short option ?
|
|---|
| 667 | cout << "Missing option for -" << (char)optopt << endl;
|
|---|
| 668 | } else {
|
|---|
| 669 | cout << "Missing option for " << argv[optind - 1] << endl;
|
|---|
| 670 | } // if
|
|---|
| 671 | goto Default;
|
|---|
| 672 | Default:
|
|---|
| 673 | default:
|
|---|
| 674 | usage( argv ); // no return
|
|---|
| 675 | } // switch
|
|---|
| 676 | } // while
|
|---|
| 677 |
|
|---|
| 678 | if ( Werror ) {
|
|---|
| 679 | SemanticWarning_WarningAsError();
|
|---|
| 680 | } // if
|
|---|
| 681 | if ( Wsuppress ) {
|
|---|
| 682 | SemanticWarning_SuppressAll();
|
|---|
| 683 | } // if
|
|---|
| 684 | // for ( const auto w : WarningFormats ) {
|
|---|
| 685 | // cout << w.name << ' ' << (int)w.severity << endl;
|
|---|
| 686 | // } // for
|
|---|
| 687 | } // parse_cmdline
|
|---|
| 688 |
|
|---|
| 689 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
|
|---|
| 690 | extern int yyparse( void );
|
|---|
| 691 | extern FILE * yyin;
|
|---|
| 692 | extern int yylineno;
|
|---|
| 693 |
|
|---|
| 694 | ::linkage = linkage; // set globals
|
|---|
| 695 | yyin = input;
|
|---|
| 696 | yylineno = 1;
|
|---|
| 697 | int parseStatus = yyparse();
|
|---|
| 698 |
|
|---|
| 699 | fclose( input );
|
|---|
| 700 | if ( shouldExit || parseStatus != 0 ) {
|
|---|
| 701 | exit( parseStatus );
|
|---|
| 702 | } // if
|
|---|
| 703 | } // parse
|
|---|
| 704 |
|
|---|
| 705 | static bool notPrelude( Declaration * decl ) {
|
|---|
| 706 | return ! LinkageSpec::isBuiltin( decl->get_linkage() );
|
|---|
| 707 | } // notPrelude
|
|---|
| 708 |
|
|---|
| 709 | static void dump( list< Declaration * > & translationUnit, ostream & out ) {
|
|---|
| 710 | list< Declaration * > decls;
|
|---|
| 711 |
|
|---|
| 712 | if ( genproto ) {
|
|---|
| 713 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
|
|---|
| 714 | } else {
|
|---|
| 715 | decls = translationUnit;
|
|---|
| 716 | } // if
|
|---|
| 717 |
|
|---|
| 718 | // depending on commandline options, either generate code or dump the AST
|
|---|
| 719 | if ( codegenp ) {
|
|---|
| 720 | CodeGen::generate( decls, out, ! genproto, prettycodegenp );
|
|---|
| 721 | } else {
|
|---|
| 722 | printAll( decls, out );
|
|---|
| 723 | } // if
|
|---|
| 724 | deleteAll( translationUnit );
|
|---|
| 725 | } // dump
|
|---|
| 726 |
|
|---|
| 727 | // Local Variables: //
|
|---|
| 728 | // tab-width: 4 //
|
|---|
| 729 | // mode: c++ //
|
|---|
| 730 | // compile-command: "make install" //
|
|---|
| 731 | // End: //
|
|---|