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