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