| [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 | 
|---|
| [68fe946e] | 11 | // Last Modified By : Andrew Beach | 
|---|
| [a488783] | 12 | // Last Modified On : Wed Jan 26 14:09:00 2022 | 
|---|
|  | 13 | // Update Count     : 670 | 
|---|
| [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 |  | 
|---|
| [9ea38de] | 33 | #include "AST/Convert.hpp" | 
|---|
| [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 | 
|---|
| [aff7e86] | 39 | #include "CodeGen/LinkOnce.h"               // for translateLinkOnce | 
|---|
| [bf2438c] | 40 | #include "CodeTools/DeclStats.h"            // for printDeclStats | 
|---|
| [3b3491b6] | 41 | #include "CodeTools/ResolvProtoDump.h"      // for dumpAsResolvProto | 
|---|
| [bf2438c] | 42 | #include "CodeTools/TrackLoc.h"             // for fillLocations | 
|---|
| [f57faf6f] | 43 | #include "Common/CodeLocationTools.hpp"     // for forceFillCodeLocations | 
|---|
| [bf2438c] | 44 | #include "Common/CompilerError.h"           // for CompilerError | 
|---|
| [55cbff8] | 45 | #include "Common/DeclStats.hpp"             // for printDeclStats | 
|---|
|  | 46 | #include "Common/ResolvProtoDump.hpp"       // for dumpAsResolverProto | 
|---|
| [7abee38] | 47 | #include "Common/Stats.h" | 
|---|
| [cbbd5b48] | 48 | #include "Common/PassVisitor.h" | 
|---|
| [bf2438c] | 49 | #include "Common/SemanticError.h"           // for SemanticError | 
|---|
|  | 50 | #include "Common/UnimplementedError.h"      // for UnimplementedError | 
|---|
|  | 51 | #include "Common/utility.h"                 // for deleteAll, filter, printAll | 
|---|
| [2cf3b87] | 52 | #include "Concurrency/Keywords.h"           // for implementMutex, implement... | 
|---|
| [9f5ecf5] | 53 | #include "Concurrency/Waitfor.h"            // for generateWaitfor | 
|---|
| [0c730d9] | 54 | #include "ControlStruct/ExceptDecl.h"       // for translateExcept | 
|---|
| [bf2438c] | 55 | #include "ControlStruct/ExceptTranslate.h"  // for translateEHM | 
|---|
| [b8ab91a] | 56 | #include "ControlStruct/FixLabels.hpp"      // for fixLabels | 
|---|
| [a488783] | 57 | #include "ControlStruct/HoistControlDecls.hpp" //  hoistControlDecls | 
|---|
| [bf2438c] | 58 | #include "ControlStruct/Mutate.h"           // for mutate | 
|---|
|  | 59 | #include "GenPoly/Box.h"                    // for box | 
|---|
|  | 60 | #include "GenPoly/InstantiateGeneric.h"     // for instantiateGeneric | 
|---|
|  | 61 | #include "GenPoly/Lvalue.h"                 // for convertLvalue | 
|---|
|  | 62 | #include "GenPoly/Specialize.h"             // for convertSpecializations | 
|---|
|  | 63 | #include "InitTweak/FixInit.h"              // for fix | 
|---|
|  | 64 | #include "InitTweak/GenInit.h"              // for genInit | 
|---|
|  | 65 | #include "MakeLibCfa.h"                     // for makeLibCfa | 
|---|
|  | 66 | #include "Parser/ParseNode.h"               // for DeclarationNode, buildList | 
|---|
|  | 67 | #include "Parser/TypedefTable.h"            // for TypedefTable | 
|---|
|  | 68 | #include "ResolvExpr/AlternativePrinter.h"  // for AlternativePrinter | 
|---|
| [1622af5] | 69 | #include "ResolvExpr/CandidatePrinter.hpp"  // for printCandidates | 
|---|
| [bf2438c] | 70 | #include "ResolvExpr/Resolver.h"            // for resolve | 
|---|
|  | 71 | #include "SymTab/Validate.h"                // for validate | 
|---|
| [bffcd66] | 72 | #include "SynTree/LinkageSpec.h"            // for Spec, Cforall, Intrinsic | 
|---|
| [bf2438c] | 73 | #include "SynTree/Declaration.h"            // for Declaration | 
|---|
|  | 74 | #include "SynTree/Visitor.h"                // for acceptAll | 
|---|
|  | 75 | #include "Tuples/Tuples.h"                  // for expandMemberTuples, expan... | 
|---|
| [a488783] | 76 | #include "Validate/Autogen.hpp"             // for autogenerateRoutines | 
|---|
| [ce36b55] | 77 | #include "Validate/FindSpecialDecls.h"      // for findGlobalDecls | 
|---|
| [2cf3b87] | 78 | #include "Validate/CompoundLiteral.hpp"     // for handleCompoundLiterals | 
|---|
| [ce36b55] | 79 | #include "Validate/InitializerLength.hpp"   // for setLengthFromInitializer | 
|---|
|  | 80 | #include "Validate/LabelAddressFixer.hpp"   // for fixLabelAddresses | 
|---|
| [a5f0529] | 81 | #include "Virtual/ExpandCasts.h"            // for expandCasts | 
|---|
| [51b73452] | 82 |  | 
|---|
| [3e96559] | 83 | static void NewPass( const char * const name ) { | 
|---|
|  | 84 | Stats::Heap::newPass( name ); | 
|---|
| [1cb7fab2] | 85 | using namespace Stats::Counters; | 
|---|
| [b8665e3] | 86 | { | 
|---|
| [3e96559] | 87 | static auto group = build<CounterGroup>( "Pass Visitor" ); | 
|---|
|  | 88 | auto pass = build<CounterGroup>( name, group ); | 
|---|
| [b8665e3] | 89 | pass_visitor_stats.depth = 0; | 
|---|
| [3e96559] | 90 | pass_visitor_stats.avg = build<AverageCounter<double>>( "Average Depth", pass ); | 
|---|
|  | 91 | pass_visitor_stats.max = build<MaxCounter<double>>( "Max Depth", pass ); | 
|---|
| [b8665e3] | 92 | } | 
|---|
|  | 93 | { | 
|---|
| [3e96559] | 94 | static auto group = build<CounterGroup>( "Syntax Node" ); | 
|---|
|  | 95 | auto pass = build<CounterGroup>( name, group ); | 
|---|
|  | 96 | BaseSyntaxNode::new_nodes = build<SimpleCounter>( "Allocs", pass ); | 
|---|
| [b8665e3] | 97 | } | 
|---|
| [675716e] | 98 | } | 
|---|
|  | 99 |  | 
|---|
| [3e96559] | 100 | #define PASS( name, pass )                  \ | 
|---|
| [ecaeac6e] | 101 | if ( errorp ) { cerr << name << endl; } \ | 
|---|
| [675716e] | 102 | NewPass(name);                          \ | 
|---|
| [4f97937] | 103 | Stats::Time::StartBlock(name);          \ | 
|---|
|  | 104 | pass;                                   \ | 
|---|
|  | 105 | Stats::Time::StopBlock(); | 
|---|
| [0da3e2c] | 106 |  | 
|---|
| [8b7ee09] | 107 | LinkageSpec::Spec linkage = LinkageSpec::Cforall; | 
|---|
| [0da3e2c] | 108 | TypedefTable typedefTable; | 
|---|
| [cbaee0d] | 109 | DeclarationNode * parseTree = nullptr;                                  // program parse tree | 
|---|
| [81419b5] | 110 |  | 
|---|
| [ef22ad6] | 111 | static bool waiting_for_gdb = false;                                    // flag to set cfa-cpp to wait for gdb on start | 
|---|
| [dee1f89] | 112 |  | 
|---|
| [bffcd66] | 113 | static string PreludeDirector = ""; | 
|---|
| [4dcaed2] | 114 |  | 
|---|
| [77d601f] | 115 | static void parse_cmdline( int argc, char * argv[] ); | 
|---|
| [8b7ee09] | 116 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false ); | 
|---|
| [e6955b1] | 117 | static void dump( list< Declaration * > & translationUnit, ostream & out = cout ); | 
|---|
| [e499381] | 118 | static void dump( ast::TranslationUnit && transUnit, ostream & out = cout ); | 
|---|
| [e6955b1] | 119 |  | 
|---|
| [0afffee] | 120 | static void backtrace( int start ) {                                    // skip first N stack frames | 
|---|
| [74330e7] | 121 | enum { Frames = 50, };                                                          // maximum number of stack frames | 
|---|
| [e6955b1] | 122 | void * array[Frames]; | 
|---|
| [74330e7] | 123 | size_t size = ::backtrace( array, Frames ); | 
|---|
| [0afffee] | 124 | char ** messages = ::backtrace_symbols( array, size ); // does not demangle names | 
|---|
|  | 125 |  | 
|---|
|  | 126 | *index( messages[0], '(' ) = '\0';                                      // find executable name | 
|---|
|  | 127 | cerr << "Stack back trace for: " << messages[0] << endl; | 
|---|
| [e6955b1] | 128 |  | 
|---|
| [b542bfb] | 129 | // skip last 2 stack frames after main | 
|---|
| [74330e7] | 130 | for ( unsigned int i = start; i < size - 2 && messages != nullptr; i += 1 ) { | 
|---|
| [e6955b1] | 131 | char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr; | 
|---|
| [7006ba5] | 132 |  | 
|---|
|  | 133 | for ( char * p = messages[i]; *p; p += 1 ) {    // find parantheses and +offset | 
|---|
| [0afffee] | 134 | if ( *p == '(' ) { | 
|---|
| [46f6134] | 135 | mangled_name = p; | 
|---|
| [0afffee] | 136 | } else if ( *p == '+' ) { | 
|---|
| [e6955b1] | 137 | offset_begin = p; | 
|---|
| [0afffee] | 138 | } else if ( *p == ')' ) { | 
|---|
| [e6955b1] | 139 | offset_end = p; | 
|---|
|  | 140 | break; | 
|---|
|  | 141 | } // if | 
|---|
|  | 142 | } // for | 
|---|
|  | 143 |  | 
|---|
|  | 144 | // if line contains symbol, attempt to demangle | 
|---|
| [b542bfb] | 145 | int frameNo = i - start; | 
|---|
| [e6955b1] | 146 | if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) { | 
|---|
| [0afffee] | 147 | *mangled_name++ = '\0';                                         // delimit strings | 
|---|
| [e6955b1] | 148 | *offset_begin++ = '\0'; | 
|---|
|  | 149 | *offset_end++ = '\0'; | 
|---|
|  | 150 |  | 
|---|
| [0afffee] | 151 | int status; | 
|---|
| [e6955b1] | 152 | char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status ); | 
|---|
| [0afffee] | 153 | // bug in __cxa_demangle for single-character lower-case non-mangled names | 
|---|
| [e6955b1] | 154 | if ( status == 0 ) {                                            // demangling successful ? | 
|---|
| [b542bfb] | 155 | cerr << "(" << frameNo << ") " << messages[i] << " : " | 
|---|
| [e6955b1] | 156 | << real_name << "+" << offset_begin << offset_end << endl; | 
|---|
|  | 157 | } else {                                                                        // otherwise, output mangled name | 
|---|
| [b542bfb] | 158 | cerr << "(" << frameNo << ") " << messages[i] << " : " | 
|---|
| [0afffee] | 159 | << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl; | 
|---|
| [e6955b1] | 160 | } // if | 
|---|
| [0afffee] | 161 |  | 
|---|
| [e6955b1] | 162 | free( real_name ); | 
|---|
|  | 163 | } else {                                                                                // otherwise, print the whole line | 
|---|
| [b542bfb] | 164 | cerr << "(" << frameNo << ") " << messages[i] << endl; | 
|---|
| [e6955b1] | 165 | } // if | 
|---|
|  | 166 | } // for | 
|---|
| [b542bfb] | 167 |  | 
|---|
| [e6955b1] | 168 | free( messages ); | 
|---|
| [b542bfb] | 169 | } // backtrace | 
|---|
|  | 170 |  | 
|---|
| [bffcd66] | 171 | #define SIGPARMS int sig __attribute__(( unused )), siginfo_t * sfp __attribute__(( unused )), ucontext_t * cxt __attribute__(( unused )) | 
|---|
|  | 172 |  | 
|---|
| [1f68d5d] | 173 | static void _Signal(struct sigaction & act, int sig, int flags ) { | 
|---|
| [bffcd66] | 174 | act.sa_flags = flags; | 
|---|
|  | 175 |  | 
|---|
|  | 176 | if ( sigaction( sig, &act, nullptr ) == -1 ) { | 
|---|
| [77d601f] | 177 | cerr << "*cfa-cpp compilation error* problem installing signal handler, error(" << errno << ") " << strerror( errno ) << endl; | 
|---|
| [bffcd66] | 178 | _exit( EXIT_FAILURE ); | 
|---|
|  | 179 | } // if | 
|---|
| [1f68d5d] | 180 | } | 
|---|
|  | 181 |  | 
|---|
|  | 182 | static void Signal( int sig, void (* handler)(SIGPARMS), int flags ) { | 
|---|
|  | 183 | struct sigaction act; | 
|---|
|  | 184 | act.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler; | 
|---|
|  | 185 | _Signal(act, sig, flags); | 
|---|
|  | 186 | } // Signal | 
|---|
|  | 187 |  | 
|---|
|  | 188 | static void Signal( int sig, void (* handler)(int), int flags ) { | 
|---|
|  | 189 | struct sigaction act; | 
|---|
|  | 190 | act.sa_handler = handler; | 
|---|
|  | 191 | _Signal(act, sig, flags); | 
|---|
| [bffcd66] | 192 | } // Signal | 
|---|
|  | 193 |  | 
|---|
|  | 194 | static void sigSegvBusHandler( SIGPARMS ) { | 
|---|
|  | 195 | if ( sfp->si_addr == nullptr ) { | 
|---|
|  | 196 | cerr << "Null pointer (nullptr) dereference." << endl; | 
|---|
|  | 197 | } else { | 
|---|
|  | 198 | cerr << (sig == SIGSEGV ? "Segment fault" : "Bus error") << " at memory location " << sfp->si_addr << "." << endl | 
|---|
|  | 199 | << "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; | 
|---|
|  | 200 | } // if | 
|---|
| [b542bfb] | 201 | backtrace( 2 );                                                                         // skip first 2 stack frames | 
|---|
| [3e96559] | 202 | abort();                                                                                        // cause core dump for debugging | 
|---|
| [e6955b1] | 203 | } // sigSegvBusHandler | 
|---|
| [0da3e2c] | 204 |  | 
|---|
| [74330e7] | 205 | static void sigFpeHandler( SIGPARMS ) { | 
|---|
|  | 206 | const char * msg; | 
|---|
|  | 207 |  | 
|---|
|  | 208 | switch ( sfp->si_code ) { | 
|---|
|  | 209 | case FPE_INTDIV: case FPE_FLTDIV: msg = "divide by zero"; break; | 
|---|
|  | 210 | case FPE_FLTOVF: msg = "overflow"; break; | 
|---|
|  | 211 | case FPE_FLTUND: msg = "underflow"; break; | 
|---|
|  | 212 | case FPE_FLTRES: msg = "inexact result"; break; | 
|---|
|  | 213 | case FPE_FLTINV: msg = "invalid operation"; break; | 
|---|
|  | 214 | default: msg = "unknown"; | 
|---|
|  | 215 | } // choose | 
|---|
|  | 216 | cerr << "Computation error " << msg << " at location " << sfp->si_addr << endl | 
|---|
|  | 217 | << "Possible cause is constant-expression evaluation invalid." << endl; | 
|---|
|  | 218 | backtrace( 2 );                                                                         // skip first 2 stack frames | 
|---|
|  | 219 | abort();                                                                                        // cause core dump for debugging | 
|---|
|  | 220 | } // sigFpeHandler | 
|---|
|  | 221 |  | 
|---|
| [bffcd66] | 222 | static void sigAbortHandler( SIGPARMS ) { | 
|---|
| [b542bfb] | 223 | backtrace( 6 );                                                                         // skip first 6 stack frames | 
|---|
| [1f68d5d] | 224 | Signal( SIGABRT, SIG_DFL, SA_SIGINFO ); // reset default signal handler | 
|---|
| [9be45a2] | 225 | raise( SIGABRT );                                                                       // reraise SIGABRT | 
|---|
| [b542bfb] | 226 | } // sigAbortHandler | 
|---|
|  | 227 |  | 
|---|
| [cbaee0d] | 228 | int main( int argc, char * argv[] ) { | 
|---|
| [3b8e52c] | 229 | FILE * input;                                                                           // use FILE rather than istream because yyin is FILE | 
|---|
| [d08beee] | 230 | ostream * output = & cout; | 
|---|
| [e6955b1] | 231 | list< Declaration * > translationUnit; | 
|---|
|  | 232 |  | 
|---|
| [bffcd66] | 233 | Signal( SIGSEGV, sigSegvBusHandler, SA_SIGINFO ); | 
|---|
|  | 234 | Signal( SIGBUS, sigSegvBusHandler, SA_SIGINFO ); | 
|---|
| [74330e7] | 235 | Signal( SIGFPE, sigFpeHandler, SA_SIGINFO ); | 
|---|
| [bffcd66] | 236 | Signal( SIGABRT, sigAbortHandler, SA_SIGINFO ); | 
|---|
| [b87a5ed] | 237 |  | 
|---|
| [bffcd66] | 238 | // cout << "main" << endl; | 
|---|
| [44bca7f] | 239 | // for ( int i = 0; i < argc; i += 1 ) { | 
|---|
| [bffcd66] | 240 | //      cout << '\t' << argv[i] << endl; | 
|---|
| [44bca7f] | 241 | // } // for | 
|---|
|  | 242 |  | 
|---|
| [e0bd0f9] | 243 | parse_cmdline( argc, argv );                                            // process command-line arguments | 
|---|
| [13de47bc] | 244 | CodeGen::FixMain::setReplaceMain( !nomainp ); | 
|---|
| [b87a5ed] | 245 |  | 
|---|
| [ef22ad6] | 246 | if ( waiting_for_gdb ) { | 
|---|
| [bffcd66] | 247 | cerr << "Waiting for gdb" << endl; | 
|---|
|  | 248 | cerr << "run :" << endl; | 
|---|
|  | 249 | cerr << "  gdb attach " << getpid() << endl; | 
|---|
| [dee1f89] | 250 | raise(SIGSTOP); | 
|---|
| [ef22ad6] | 251 | } // if | 
|---|
| [dee1f89] | 252 |  | 
|---|
| [b87a5ed] | 253 | try { | 
|---|
| [81419b5] | 254 | // choose to read the program from a file or stdin | 
|---|
| [3b8e52c] | 255 | if ( optind < argc ) {                                                  // any commands after the flags ? => input file name | 
|---|
| [b87a5ed] | 256 | input = fopen( argv[ optind ], "r" ); | 
|---|
| [e0bd0f9] | 257 | assertf( input, "cannot open %s because %s\n", argv[ optind ], strerror( errno ) ); | 
|---|
| [b87a5ed] | 258 | optind += 1; | 
|---|
| [3b8e52c] | 259 | } else {                                                                                // no input file name | 
|---|
| [b87a5ed] | 260 | input = stdin; | 
|---|
|  | 261 | } // if | 
|---|
|  | 262 |  | 
|---|
| [79eaeb7] | 263 | Stats::Time::StartGlobal(); | 
|---|
| [3c0d4cd] | 264 | NewPass("Parse"); | 
|---|
|  | 265 | Stats::Time::StartBlock("Parse"); | 
|---|
| [675716e] | 266 |  | 
|---|
| [159c62e] | 267 | // read in the builtins, extras, and the prelude | 
|---|
| [de62360d] | 268 | if ( ! nopreludep ) {                                                   // include gcc builtins | 
|---|
| [faf8857] | 269 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here. | 
|---|
| [807ce84] | 270 |  | 
|---|
| [37fe352] | 271 | assertf( !PreludeDirector.empty(), "Can't find prelude without option --prelude-dir must be used." ); | 
|---|
| [4dcaed2] | 272 |  | 
|---|
| [807ce84] | 273 | // Read to gcc builtins, if not generating the cfa library | 
|---|
| [37fe352] | 274 | FILE * gcc_builtins = fopen( (PreludeDirector + "/gcc-builtins.cf").c_str(), "r" ); | 
|---|
| [6ce3ae9] | 275 | assertf( gcc_builtins, "cannot open gcc-builtins.cf\n" ); | 
|---|
|  | 276 | parse( gcc_builtins, LinkageSpec::Compiler ); | 
|---|
| [81419b5] | 277 |  | 
|---|
| [159c62e] | 278 | // read the extra prelude in, if not generating the cfa library | 
|---|
| [37fe352] | 279 | FILE * extras = fopen( (PreludeDirector + "/extras.cf").c_str(), "r" ); | 
|---|
| [3b8e52c] | 280 | assertf( extras, "cannot open extras.cf\n" ); | 
|---|
| [f0994a1] | 281 | parse( extras, LinkageSpec::BuiltinC ); | 
|---|
| [159c62e] | 282 |  | 
|---|
| [81419b5] | 283 | if ( ! libcfap ) { | 
|---|
| [faf8857] | 284 | // read the prelude in, if not generating the cfa library | 
|---|
| [e523b07] | 285 | FILE * prelude = fopen( (PreludeDirector + "/prelude.cfa").c_str(), "r" ); | 
|---|
|  | 286 | assertf( prelude, "cannot open prelude.cfa\n" ); | 
|---|
| [35304009] | 287 | parse( prelude, LinkageSpec::Intrinsic ); | 
|---|
| [fa4805f] | 288 |  | 
|---|
|  | 289 | // Read to cfa builtins, if not generating the cfa library | 
|---|
| [37fe352] | 290 | FILE * builtins = fopen( (PreludeDirector + "/builtins.cf").c_str(), "r" ); | 
|---|
| [fa4805f] | 291 | assertf( builtins, "cannot open builtins.cf\n" ); | 
|---|
| [54d714e] | 292 | parse( builtins, LinkageSpec::BuiltinCFA ); | 
|---|
| [b87a5ed] | 293 | } // if | 
|---|
|  | 294 | } // if | 
|---|
| [81419b5] | 295 |  | 
|---|
| [926af74] | 296 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug ); | 
|---|
| [71f4e4f] | 297 |  | 
|---|
| [b87a5ed] | 298 | if ( parsep ) { | 
|---|
| [e6955b1] | 299 | parseTree->printList( cout ); | 
|---|
| [0da3e2c] | 300 | delete parseTree; | 
|---|
| [3e96559] | 301 | return EXIT_SUCCESS; | 
|---|
| [b87a5ed] | 302 | } // if | 
|---|
|  | 303 |  | 
|---|
| [0da3e2c] | 304 | buildList( parseTree, translationUnit ); | 
|---|
|  | 305 | delete parseTree; | 
|---|
| [cbaee0d] | 306 | parseTree = nullptr; | 
|---|
| [b87a5ed] | 307 |  | 
|---|
|  | 308 | if ( astp ) { | 
|---|
| [1ab4ce2] | 309 | dump( translationUnit ); | 
|---|
| [3e96559] | 310 | return EXIT_SUCCESS; | 
|---|
| [b87a5ed] | 311 | } // if | 
|---|
|  | 312 |  | 
|---|
| [036dd5f] | 313 | // Temporary: fill locations after parsing so that every node has a location, for early error messages. | 
|---|
|  | 314 | // Eventually we should pass the locations from the parser to every node, but this quick and dirty solution | 
|---|
|  | 315 | // works okay for now. | 
|---|
|  | 316 | CodeTools::fillLocations( translationUnit ); | 
|---|
| [3c0d4cd] | 317 | Stats::Time::StopBlock(); | 
|---|
| [036dd5f] | 318 |  | 
|---|
| [0c730d9] | 319 | PASS( "Translate Exception Declarations", ControlStruct::translateExcept( translationUnit ) ); | 
|---|
| [00da199] | 320 | if ( exdeclp ) { | 
|---|
|  | 321 | dump( translationUnit ); | 
|---|
|  | 322 | return EXIT_SUCCESS; | 
|---|
|  | 323 | } // if | 
|---|
| [0c730d9] | 324 |  | 
|---|
| [839ccbb] | 325 | // add the assignment statement after the initialization of a type parameter | 
|---|
| [5dcb881] | 326 | PASS( "Validate-A", SymTab::validate_A( translationUnit ) ); | 
|---|
|  | 327 | PASS( "Validate-B", SymTab::validate_B( translationUnit ) ); | 
|---|
|  | 328 | PASS( "Validate-C", SymTab::validate_C( translationUnit ) ); | 
|---|
| [b87a5ed] | 329 |  | 
|---|
| [76b378d] | 330 | CodeTools::fillLocations( translationUnit ); | 
|---|
|  | 331 |  | 
|---|
| [4a8f150] | 332 | if( useNewAST ) { | 
|---|
| [a488783] | 333 | PASS( "Apply Concurrent Keywords", Concurrency::applyKeywords( translationUnit ) ); | 
|---|
|  | 334 | PASS( "Forall Pointer Decay", SymTab::decayForallPointers( translationUnit ) ); | 
|---|
|  | 335 | CodeTools::fillLocations( translationUnit ); | 
|---|
|  | 336 |  | 
|---|
| [3746f777] | 337 | if (Stats::Counters::enabled) { | 
|---|
|  | 338 | ast::pass_visitor_stats.avg = Stats::Counters::build<Stats::Counters::AverageCounter<double>>("Average Depth - New"); | 
|---|
|  | 339 | ast::pass_visitor_stats.max = Stats::Counters::build<Stats::Counters::MaxCounter<double>>("Max depth - New"); | 
|---|
|  | 340 | } | 
|---|
| [9ea38de] | 341 | auto transUnit = convert( move( translationUnit ) ); | 
|---|
| [9f5a19fa] | 342 |  | 
|---|
| [68fe946e] | 343 | forceFillCodeLocations( transUnit ); | 
|---|
|  | 344 |  | 
|---|
| [a488783] | 345 | // Must happen before autogen routines are added. | 
|---|
|  | 346 | PASS( "Hoist Control Declarations", ControlStruct::hoistControlDecls( transUnit ) ); | 
|---|
|  | 347 |  | 
|---|
|  | 348 | // Must be after enum and pointer decay. | 
|---|
|  | 349 | // Must be before compound literals. | 
|---|
|  | 350 | PASS( "Generate Autogen Routines", Validate::autogenerateRoutines( transUnit ) ); | 
|---|
|  | 351 |  | 
|---|
| [2cf3b87] | 352 | PASS( "Implement Mutex", Concurrency::implementMutex( transUnit ) ); | 
|---|
|  | 353 | PASS( "Implement Thread Start", Concurrency::implementThreadStarter( transUnit ) ); | 
|---|
|  | 354 | PASS( "Compound Literal", Validate::handleCompoundLiterals( transUnit ) ); | 
|---|
| [ce36b55] | 355 | PASS( "Set Length From Initializer", Validate::setLengthFromInitializer( transUnit ) ); | 
|---|
|  | 356 | PASS( "Find Global Decls", Validate::findGlobalDecls( transUnit ) ); | 
|---|
|  | 357 | PASS( "Fix Label Address", Validate::fixLabelAddresses( transUnit ) ); | 
|---|
|  | 358 |  | 
|---|
| [1622af5] | 359 | if ( symtabp ) { | 
|---|
|  | 360 | return EXIT_SUCCESS; | 
|---|
|  | 361 | } // if | 
|---|
|  | 362 |  | 
|---|
|  | 363 | if ( expraltp ) { | 
|---|
|  | 364 | ResolvExpr::printCandidates( transUnit ); | 
|---|
|  | 365 | return EXIT_SUCCESS; | 
|---|
|  | 366 | } // if | 
|---|
|  | 367 |  | 
|---|
|  | 368 | if ( validp ) { | 
|---|
|  | 369 | dump( move( transUnit ) ); | 
|---|
|  | 370 | return EXIT_SUCCESS; | 
|---|
|  | 371 | } // if | 
|---|
|  | 372 |  | 
|---|
| [5ee153d] | 373 | PASS( "Translate Throws", ControlStruct::translateThrows( transUnit ) ); | 
|---|
| [b8ab91a] | 374 | PASS( "Fix Labels", ControlStruct::fixLabels( transUnit ) ); | 
|---|
| [0c577f7] | 375 | PASS( "Fix Names", CodeGen::fixNames( transUnit ) ); | 
|---|
| [a36eb2d] | 376 | PASS( "Gen Init", InitTweak::genInit( transUnit ) ); | 
|---|
| [9f5a19fa] | 377 | PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( transUnit ) ); | 
|---|
| [68fe946e] | 378 |  | 
|---|
| [da6396f] | 379 | if ( libcfap ) { | 
|---|
|  | 380 | // Generate the bodies of cfa library functions. | 
|---|
|  | 381 | LibCfa::makeLibCfa( transUnit ); | 
|---|
|  | 382 | } // if | 
|---|
| [68fe946e] | 383 |  | 
|---|
|  | 384 | if ( declstatsp ) { | 
|---|
|  | 385 | printDeclStats( transUnit ); | 
|---|
|  | 386 | return EXIT_SUCCESS; | 
|---|
|  | 387 | } // if | 
|---|
|  | 388 |  | 
|---|
|  | 389 | if ( bresolvep ) { | 
|---|
|  | 390 | dump( move( transUnit ) ); | 
|---|
|  | 391 | return EXIT_SUCCESS; | 
|---|
|  | 392 | } // if | 
|---|
|  | 393 |  | 
|---|
|  | 394 | if ( resolvprotop ) { | 
|---|
| [55cbff8] | 395 | dumpAsResolverProto( transUnit ); | 
|---|
| [68fe946e] | 396 | return EXIT_SUCCESS; | 
|---|
|  | 397 | } // if | 
|---|
|  | 398 |  | 
|---|
| [9ea38de] | 399 | PASS( "Resolve", ResolvExpr::resolve( transUnit ) ); | 
|---|
| [490fb92e] | 400 | if ( exprp ) { | 
|---|
| [e499381] | 401 | dump( move( transUnit ) ); | 
|---|
| [490fb92e] | 402 | return EXIT_SUCCESS; | 
|---|
|  | 403 | } // if | 
|---|
|  | 404 |  | 
|---|
| [f57faf6f] | 405 | forceFillCodeLocations( transUnit ); | 
|---|
| [4a8f150] | 406 |  | 
|---|
| [490fb92e] | 407 | PASS( "Fix Init", InitTweak::fix(transUnit, buildingLibrary())); | 
|---|
| [da6396f] | 408 |  | 
|---|
| [01d433e] | 409 | // fix ObjectDecl - replaces ConstructorInit nodes | 
|---|
|  | 410 | if ( ctorinitp ) { | 
|---|
|  | 411 | dump( move( transUnit ) ); | 
|---|
|  | 412 | return EXIT_SUCCESS; | 
|---|
|  | 413 | } // if | 
|---|
|  | 414 |  | 
|---|
|  | 415 | // Currently not working due to unresolved issues with UniqueExpr | 
|---|
|  | 416 | PASS( "Expand Unique Expr", Tuples::expandUniqueExpr( transUnit ) ); // 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 | 
|---|
| [5f3ba11] | 417 |  | 
|---|
|  | 418 | PASS( "Translate Tries" , ControlStruct::translateTries( transUnit ) ); | 
|---|
|  | 419 |  | 
|---|
| [9ea38de] | 420 | translationUnit = convert( move( transUnit ) ); | 
|---|
| [a77257be] | 421 | } else { | 
|---|
| [a488783] | 422 | PASS( "Validate-D", SymTab::validate_D( translationUnit ) ); | 
|---|
| [2cf3b87] | 423 | PASS( "Validate-E", SymTab::validate_E( translationUnit ) ); | 
|---|
| [ce36b55] | 424 | PASS( "Validate-F", SymTab::validate_F( translationUnit ) ); | 
|---|
|  | 425 |  | 
|---|
| [1622af5] | 426 | if ( symtabp ) { | 
|---|
|  | 427 | deleteAll( translationUnit ); | 
|---|
|  | 428 | return EXIT_SUCCESS; | 
|---|
|  | 429 | } // if | 
|---|
|  | 430 |  | 
|---|
|  | 431 | if ( expraltp ) { | 
|---|
|  | 432 | PassVisitor<ResolvExpr::AlternativePrinter> printer( cout ); | 
|---|
|  | 433 | acceptAll( translationUnit, printer ); | 
|---|
|  | 434 | return EXIT_SUCCESS; | 
|---|
|  | 435 | } // if | 
|---|
|  | 436 |  | 
|---|
|  | 437 | if ( validp ) { | 
|---|
|  | 438 | dump( translationUnit ); | 
|---|
|  | 439 | return EXIT_SUCCESS; | 
|---|
|  | 440 | } // if | 
|---|
|  | 441 |  | 
|---|
| [5ee153d] | 442 | PASS( "Translate Throws", ControlStruct::translateThrows( translationUnit ) ); | 
|---|
| [b8ab91a] | 443 | PASS( "Fix Labels", ControlStruct::fixLabels( translationUnit ) ); | 
|---|
| [0c577f7] | 444 | PASS( "Fix Names", CodeGen::fixNames( translationUnit ) ); | 
|---|
| [a36eb2d] | 445 | PASS( "Gen Init", InitTweak::genInit( translationUnit ) ); | 
|---|
| [9f5a19fa] | 446 | PASS( "Expand Member Tuples" , Tuples::expandMemberTuples( translationUnit ) ); | 
|---|
|  | 447 |  | 
|---|
| [da6396f] | 448 | if ( libcfap ) { | 
|---|
|  | 449 | // Generate the bodies of cfa library functions. | 
|---|
|  | 450 | LibCfa::makeLibCfa( translationUnit ); | 
|---|
|  | 451 | } // if | 
|---|
| [68fe946e] | 452 |  | 
|---|
|  | 453 | if ( declstatsp ) { | 
|---|
|  | 454 | CodeTools::printDeclStats( translationUnit ); | 
|---|
|  | 455 | deleteAll( translationUnit ); | 
|---|
|  | 456 | return EXIT_SUCCESS; | 
|---|
|  | 457 | } // if | 
|---|
|  | 458 |  | 
|---|
|  | 459 | if ( bresolvep ) { | 
|---|
|  | 460 | dump( translationUnit ); | 
|---|
|  | 461 | return EXIT_SUCCESS; | 
|---|
|  | 462 | } // if | 
|---|
|  | 463 |  | 
|---|
|  | 464 | CodeTools::fillLocations( translationUnit ); | 
|---|
|  | 465 |  | 
|---|
|  | 466 | if ( resolvprotop ) { | 
|---|
|  | 467 | CodeTools::dumpAsResolvProto( translationUnit ); | 
|---|
|  | 468 | return EXIT_SUCCESS; | 
|---|
|  | 469 | } // if | 
|---|
|  | 470 |  | 
|---|
| [a77257be] | 471 | PASS( "Resolve", ResolvExpr::resolve( translationUnit ) ); | 
|---|
| [490fb92e] | 472 | if ( exprp ) { | 
|---|
|  | 473 | dump( translationUnit ); | 
|---|
|  | 474 | return EXIT_SUCCESS; | 
|---|
|  | 475 | } | 
|---|
| [4615ac8] | 476 |  | 
|---|
| [490fb92e] | 477 | PASS( "Fix Init", InitTweak::fix( translationUnit, buildingLibrary() ) ); | 
|---|
| [81419b5] | 478 |  | 
|---|
| [01d433e] | 479 | // fix ObjectDecl - replaces ConstructorInit nodes | 
|---|
|  | 480 | if ( ctorinitp ) { | 
|---|
|  | 481 | dump ( translationUnit ); | 
|---|
|  | 482 | return EXIT_SUCCESS; | 
|---|
|  | 483 | } // if | 
|---|
| [71f4e4f] | 484 |  | 
|---|
| [01d433e] | 485 | 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 | 
|---|
| [5f3ba11] | 486 |  | 
|---|
|  | 487 | PASS( "Translate Tries" , ControlStruct::translateTries( translationUnit ) ); | 
|---|
| [01d433e] | 488 | } | 
|---|
| [626dbc10] | 489 |  | 
|---|
| [5f3ba11] | 490 |  | 
|---|
| [6edd210] | 491 |  | 
|---|
| [675716e] | 492 | PASS( "Gen Waitfor" , Concurrency::generateWaitFor( translationUnit ) ); | 
|---|
| [307a732] | 493 |  | 
|---|
| [675716e] | 494 | PASS( "Convert Specializations",  GenPoly::convertSpecializations( translationUnit ) ); // needs to happen before tuple types are expanded | 
|---|
| [9f5ecf5] | 495 |  | 
|---|
| [675716e] | 496 | PASS( "Expand Tuples", Tuples::expandTuples( translationUnit ) ); // xxx - is this the right place for this? | 
|---|
| [626dbc10] | 497 |  | 
|---|
|  | 498 | if ( tuplep ) { | 
|---|
|  | 499 | dump( translationUnit ); | 
|---|
| [3e96559] | 500 | return EXIT_SUCCESS; | 
|---|
|  | 501 | } // if | 
|---|
| [141b786] | 502 |  | 
|---|
| [675716e] | 503 | PASS( "Virtual Expand Casts", Virtual::expandCasts( translationUnit ) ); // Must come after translateEHM | 
|---|
| [a5f0529] | 504 |  | 
|---|
| [675716e] | 505 | PASS( "Instantiate Generics", GenPoly::instantiateGeneric( translationUnit ) ); | 
|---|
| [53d3ab4b] | 506 | if ( genericsp ) { | 
|---|
|  | 507 | dump( translationUnit ); | 
|---|
| [3e96559] | 508 | return EXIT_SUCCESS; | 
|---|
|  | 509 | } // if | 
|---|
| [b4f8808] | 510 |  | 
|---|
| [675716e] | 511 | PASS( "Convert L-Value", GenPoly::convertLvalue( translationUnit ) ); | 
|---|
| [53d3ab4b] | 512 |  | 
|---|
| [fea7ca7] | 513 | if ( bboxp ) { | 
|---|
|  | 514 | dump( translationUnit ); | 
|---|
| [3e96559] | 515 | return EXIT_SUCCESS; | 
|---|
| [926af74] | 516 | } // if | 
|---|
| [675716e] | 517 | PASS( "Box", GenPoly::box( translationUnit ) ); | 
|---|
| [81419b5] | 518 |  | 
|---|
| [aff7e86] | 519 | PASS( "Link-Once", CodeGen::translateLinkOnce( translationUnit ) ); | 
|---|
|  | 520 |  | 
|---|
|  | 521 | // Code has been lowered to C, now we can start generation. | 
|---|
|  | 522 |  | 
|---|
| [8905f56] | 523 | if ( bcodegenp ) { | 
|---|
|  | 524 | dump( translationUnit ); | 
|---|
| [3e96559] | 525 | return EXIT_SUCCESS; | 
|---|
|  | 526 | } // if | 
|---|
| [8905f56] | 527 |  | 
|---|
| [13de47bc] | 528 | if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name | 
|---|
|  | 529 | output = new ofstream( argv[ optind ] ); | 
|---|
|  | 530 | } // if | 
|---|
| [0270824] | 531 |  | 
|---|
| [7b15d7a] | 532 | CodeTools::fillLocations( translationUnit ); | 
|---|
| [62ce290] | 533 | PASS( "Code Gen", CodeGen::generate( translationUnit, *output, ! genproto, prettycodegenp, true, linemarks ) ); | 
|---|
| [0270824] | 534 |  | 
|---|
| [8e48fca4] | 535 | CodeGen::FixMain::fix( translationUnit, *output, | 
|---|
|  | 536 | (PreludeDirector + "/bootloader.c").c_str() ); | 
|---|
| [e6955b1] | 537 | if ( output != &cout ) { | 
|---|
| [b87a5ed] | 538 | delete output; | 
|---|
|  | 539 | } // if | 
|---|
| [77d601f] | 540 | } catch ( SemanticErrorException & e ) { | 
|---|
| [b87a5ed] | 541 | if ( errorp ) { | 
|---|
| [e6955b1] | 542 | cerr << "---AST at error:---" << endl; | 
|---|
|  | 543 | dump( translationUnit, cerr ); | 
|---|
|  | 544 | cerr << endl << "---End of AST, begin error message:---\n" << endl; | 
|---|
| [926af74] | 545 | } // if | 
|---|
| [d55d7a6] | 546 | e.print(); | 
|---|
| [e6955b1] | 547 | if ( output != &cout ) { | 
|---|
| [b87a5ed] | 548 | delete output; | 
|---|
|  | 549 | } // if | 
|---|
| [3e96559] | 550 | return EXIT_FAILURE; | 
|---|
| [77d601f] | 551 | } catch ( UnimplementedError & e ) { | 
|---|
| [e6955b1] | 552 | cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl; | 
|---|
|  | 553 | if ( output != &cout ) { | 
|---|
| [b87a5ed] | 554 | delete output; | 
|---|
|  | 555 | } // if | 
|---|
| [3e96559] | 556 | return EXIT_FAILURE; | 
|---|
| [77d601f] | 557 | } catch ( CompilerError & e ) { | 
|---|
| [e6955b1] | 558 | cerr << "Compiler Error: " << e.get_what() << endl; | 
|---|
| [c850687] | 559 | cerr << "(please report bugs to [REDACTED])" << endl; | 
|---|
| [e6955b1] | 560 | if ( output != &cout ) { | 
|---|
| [b87a5ed] | 561 | delete output; | 
|---|
|  | 562 | } // if | 
|---|
| [3e96559] | 563 | return EXIT_FAILURE; | 
|---|
| [77d601f] | 564 | } catch ( std::bad_alloc & ) { | 
|---|
|  | 565 | cerr << "*cfa-cpp compilation error* std::bad_alloc" << endl; | 
|---|
|  | 566 | backtrace( 1 ); | 
|---|
|  | 567 | abort(); | 
|---|
| [3e96559] | 568 | } catch ( ... ) { | 
|---|
| [bffcd66] | 569 | exception_ptr eptr = current_exception(); | 
|---|
| [4990812] | 570 | try { | 
|---|
|  | 571 | if (eptr) { | 
|---|
| [bffcd66] | 572 | rethrow_exception(eptr); | 
|---|
| [3e96559] | 573 | } else { | 
|---|
| [77d601f] | 574 | cerr << "*cfa-cpp compilation error* exception uncaught and unknown" << endl; | 
|---|
| [3e96559] | 575 | } // if | 
|---|
| [77d601f] | 576 | } catch( const exception & e ) { | 
|---|
|  | 577 | cerr << "*cfa-cpp compilation error* uncaught exception \"" << e.what() << "\"\n"; | 
|---|
| [3e96559] | 578 | } // try | 
|---|
|  | 579 | return EXIT_FAILURE; | 
|---|
|  | 580 | } // try | 
|---|
| [b87a5ed] | 581 |  | 
|---|
| [39786813] | 582 | deleteAll( translationUnit ); | 
|---|
| [1cb7fab2] | 583 | Stats::print(); | 
|---|
| [3e96559] | 584 | return EXIT_SUCCESS; | 
|---|
| [d9a0e76] | 585 | } // main | 
|---|
| [51b73452] | 586 |  | 
|---|
| [0da3e2c] | 587 |  | 
|---|
| [3e9de01] | 588 | static const char optstring[] = ":c:ghlLmNnpdOAP:S:twW:D:"; | 
|---|
| [3e96559] | 589 |  | 
|---|
| [62ce290] | 590 | enum { PreludeDir = 128 }; | 
|---|
| [3e96559] | 591 | static struct option long_opts[] = { | 
|---|
| [1a69a90] | 592 | { "colors", required_argument, nullptr, 'c' }, | 
|---|
|  | 593 | { "gdb", no_argument, nullptr, 'g' }, | 
|---|
| [3e96559] | 594 | { "help", no_argument, nullptr, 'h' }, | 
|---|
|  | 595 | { "libcfa", no_argument, nullptr, 'l' }, | 
|---|
| [62ce290] | 596 | { "linemarks", no_argument, nullptr, 'L' }, | 
|---|
| [3e96559] | 597 | { "no-main", no_argument, 0, 'm' }, | 
|---|
| [62ce290] | 598 | { "no-linemarks", no_argument, nullptr, 'N' }, | 
|---|
|  | 599 | { "no-prelude", no_argument, nullptr, 'n' }, | 
|---|
| [3e96559] | 600 | { "prototypes", no_argument, nullptr, 'p' }, | 
|---|
| [7215000] | 601 | { "deterministic-out", no_argument, nullptr, 'd' }, | 
|---|
| [a77257be] | 602 | { "old-ast", no_argument, nullptr, 'O'}, | 
|---|
|  | 603 | { "new-ast", no_argument, nullptr, 'A'}, | 
|---|
| [62ce290] | 604 | { "print", required_argument, nullptr, 'P' }, | 
|---|
|  | 605 | { "prelude-dir", required_argument, nullptr, PreludeDir }, | 
|---|
|  | 606 | { "statistics", required_argument, nullptr, 'S' }, | 
|---|
| [3e96559] | 607 | { "tree", no_argument, nullptr, 't' }, | 
|---|
|  | 608 | { "", no_argument, nullptr, 0 },                                        // -w | 
|---|
|  | 609 | { "", no_argument, nullptr, 0 },                                        // -W | 
|---|
|  | 610 | { "", no_argument, nullptr, 0 },                                        // -D | 
|---|
|  | 611 | { nullptr, 0, nullptr, 0 } | 
|---|
|  | 612 | }; // long_opts | 
|---|
|  | 613 |  | 
|---|
|  | 614 | static const char * description[] = { | 
|---|
| [aa88cb9a] | 615 | "diagnostic color: never, always, auto",                        // -c | 
|---|
| [3e9de01] | 616 | "wait for gdb to attach",                                                       // -g | 
|---|
| [aa88cb9a] | 617 | "print translator help message",                                        // -h | 
|---|
| [3e9de01] | 618 | "generate libcfa.c",                                                            // -l | 
|---|
|  | 619 | "generate line marks",                                                          // -L | 
|---|
|  | 620 | "do not replace main",                                                          // -m | 
|---|
|  | 621 | "do not generate line marks",                                           // -N | 
|---|
|  | 622 | "do not read prelude",                                                          // -n | 
|---|
| [aa88cb9a] | 623 | "do not generate prelude prototypes => prelude not printed", // -p | 
|---|
| [3e9de01] | 624 | "only print deterministic output",                  // -d | 
|---|
|  | 625 | "Use the old-ast",                                                                      // -O | 
|---|
|  | 626 | "Use the new-ast",                                                                      // -A | 
|---|
|  | 627 | "print",                                                                                        // -P | 
|---|
| [62ce290] | 628 | "<directory> prelude directory for debug/nodebug",      // no flag | 
|---|
| [aa88cb9a] | 629 | "<option-list> enable profiling information: counters, heap, time, all, none", // -S | 
|---|
| [3e9de01] | 630 | "building cfa standard lib",                                            // -t | 
|---|
|  | 631 | "",                                                                                                     // -w | 
|---|
|  | 632 | "",                                                                                                     // -W | 
|---|
|  | 633 | "",                                                                                                     // -D | 
|---|
| [3e96559] | 634 | }; // description | 
|---|
|  | 635 |  | 
|---|
| [0c0f548] | 636 | static_assert( sizeof( long_opts ) / sizeof( long_opts[0] ) - 1 == sizeof( description ) / sizeof( description[0] ), "Long opts and description must match" ); | 
|---|
| [62ce290] | 637 |  | 
|---|
|  | 638 | static struct Printopts { | 
|---|
|  | 639 | const char * name; | 
|---|
|  | 640 | int & flag; | 
|---|
|  | 641 | int val; | 
|---|
|  | 642 | const char * descript; | 
|---|
|  | 643 | } printopts[] = { | 
|---|
| [0e464f6] | 644 | { "ascodegen", codegenp, true, "print AST as codegen rather than AST" }, | 
|---|
|  | 645 | { "asterr", errorp, true, "print AST on error" }, | 
|---|
| [62ce290] | 646 | { "declstats", declstatsp, true, "code property statistics" }, | 
|---|
|  | 647 | { "parse", yydebug, true, "yacc (parsing) debug information" }, | 
|---|
|  | 648 | { "pretty", prettycodegenp, true, "prettyprint for ascodegen flag" }, | 
|---|
|  | 649 | { "rproto", resolvprotop, true, "resolver-proto instance" }, | 
|---|
| [0e464f6] | 650 | { "rsteps", resolvep, true, "print resolver steps" }, | 
|---|
|  | 651 | { "tree", parsep, true, "print parse tree" }, | 
|---|
|  | 652 | // code dumps | 
|---|
|  | 653 | { "ast", astp, true, "print AST after parsing" }, | 
|---|
| [00da199] | 654 | { "exdecl", exdeclp, true, "print AST after translating exception decls" }, | 
|---|
| [0e464f6] | 655 | { "symevt", symtabp, true, "print AST after symbol table events" }, | 
|---|
|  | 656 | { "altexpr", expraltp, true, "print alternatives for expressions" }, | 
|---|
|  | 657 | { "astdecl", validp, true, "print AST after declaration validation pass" }, | 
|---|
|  | 658 | { "resolver", bresolvep, true, "print AST before resolver step" }, | 
|---|
|  | 659 | { "astexpr", exprp, true, "print AST after expression analysis" }, | 
|---|
|  | 660 | { "ctordtor", ctorinitp, true, "print AST after ctor/dtor are replaced" }, | 
|---|
|  | 661 | { "tuple", tuplep, true, "print AST after tuple expansion" }, | 
|---|
|  | 662 | { "astgen", genericsp, true, "print AST after instantiate generics" }, | 
|---|
|  | 663 | { "box", bboxp, true, "print AST before box step" }, | 
|---|
|  | 664 | { "codegen", bcodegenp, true, "print AST before code generation" }, | 
|---|
| [62ce290] | 665 | }; | 
|---|
|  | 666 | enum { printoptsSize = sizeof( printopts ) / sizeof( printopts[0] ) }; | 
|---|
|  | 667 |  | 
|---|
| [77d601f] | 668 | static void usage( char * argv[] ) { | 
|---|
| [e0bd0f9] | 669 | cout << "Usage: " << argv[0] << " [options] [input-file (default stdin)] [output-file (default stdout)], where options are:" << endl; | 
|---|
| [3e96559] | 670 | int i = 0, j = 1;                                                                       // j skips starting colon | 
|---|
|  | 671 | for ( ; long_opts[i].name != 0 && optstring[j] != '\0'; i += 1, j += 1 ) { | 
|---|
|  | 672 | if ( long_opts[i].name[0] != '\0' ) {                   // hidden option, internal usage only | 
|---|
| [62ce290] | 673 | if ( strcmp( long_opts[i].name, "prelude-dir" ) != 0 ) { // flag | 
|---|
|  | 674 | cout << "  -" << optstring[j] << ","; | 
|---|
|  | 675 | } else {                                                                        // no flag | 
|---|
|  | 676 | j -= 1;                                                                 // compensate | 
|---|
|  | 677 | cout << "     "; | 
|---|
|  | 678 | } // if | 
|---|
|  | 679 | cout << " --" << left << setw(12) << long_opts[i].name << "  "; | 
|---|
|  | 680 | if ( strcmp( long_opts[i].name, "print" ) == 0 ) { | 
|---|
|  | 681 | cout << "one of: " << endl; | 
|---|
|  | 682 | for ( int i = 0; i < printoptsSize; i += 1 ) { | 
|---|
|  | 683 | cout << setw(10) << " " << left << setw(10) << printopts[i].name << "  " << printopts[i].descript << endl; | 
|---|
|  | 684 | } // for | 
|---|
|  | 685 | } else { | 
|---|
|  | 686 | cout << description[i] << endl; | 
|---|
|  | 687 | } // if | 
|---|
| [3e96559] | 688 | } // if | 
|---|
| [62ce290] | 689 | if ( optstring[j + 1] == ':' ) j += 1; | 
|---|
| [3e96559] | 690 | } // for | 
|---|
|  | 691 | if ( long_opts[i].name != 0 || optstring[j] != '\0' ) assertf( false, "internal error, mismatch of option flags and names\n" ); | 
|---|
|  | 692 | exit( EXIT_FAILURE ); | 
|---|
|  | 693 | } // usage | 
|---|
|  | 694 |  | 
|---|
| [e0bd0f9] | 695 | static void parse_cmdline( int argc, char * argv[] ) { | 
|---|
| [0da3e2c] | 696 | opterr = 0;                                                                                     // (global) prevent getopt from printing error messages | 
|---|
|  | 697 |  | 
|---|
| [c5e5109] | 698 | bool Wsuppress = false, Werror = false; | 
|---|
| [0da3e2c] | 699 | int c; | 
|---|
| [3e96559] | 700 | while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) { | 
|---|
| [0da3e2c] | 701 | switch ( c ) { | 
|---|
| [1a69a90] | 702 | case 'c':                                                                             // diagnostic colors | 
|---|
|  | 703 | if ( strcmp( optarg, "always" ) == 0 ) { | 
|---|
|  | 704 | ErrorHelpers::colors = ErrorHelpers::Colors::Always; | 
|---|
|  | 705 | } else if ( strcmp( optarg, "never" ) == 0 ) { | 
|---|
|  | 706 | ErrorHelpers::colors = ErrorHelpers::Colors::Never; | 
|---|
|  | 707 | } else if ( strcmp( optarg, "auto" ) == 0 ) { | 
|---|
|  | 708 | ErrorHelpers::colors = ErrorHelpers::Colors::Auto; | 
|---|
|  | 709 | } // if | 
|---|
|  | 710 | break; | 
|---|
| [3e96559] | 711 | case 'h':                                                                             // help message | 
|---|
|  | 712 | usage( argv );                                                          // no return | 
|---|
| [53d3ab4b] | 713 | break; | 
|---|
| [3e96559] | 714 | case 'l':                                                                             // generate libcfa.c | 
|---|
| [0da3e2c] | 715 | libcfap = true; | 
|---|
|  | 716 | break; | 
|---|
| [62ce290] | 717 | case 'L':                                                                             // generate line marks | 
|---|
| [6de43b6] | 718 | linemarks = true; | 
|---|
| [c850687] | 719 | break; | 
|---|
| [3e96559] | 720 | case 'm':                                                                             // do not replace main | 
|---|
|  | 721 | nomainp = true; | 
|---|
| [0da3e2c] | 722 | break; | 
|---|
| [62ce290] | 723 | case 'N':                                                                             // do not generate line marks | 
|---|
| [6de43b6] | 724 | linemarks = false; | 
|---|
| [c59bde6] | 725 | break; | 
|---|
| [62ce290] | 726 | case 'n':                                                                             // do not read prelude | 
|---|
| [3e96559] | 727 | nopreludep = true; | 
|---|
| [0da3e2c] | 728 | break; | 
|---|
| [62ce290] | 729 | case 'p':                                                                             // generate prototypes for prelude functions | 
|---|
|  | 730 | genproto = true; | 
|---|
| [0da3e2c] | 731 | break; | 
|---|
| [7215000] | 732 | case 'd':                                     // don't print non-deterministic output | 
|---|
| [a77257be] | 733 | deterministic_output = true; | 
|---|
|  | 734 | break; | 
|---|
|  | 735 | case 'O':                                     // don't print non-deterministic output | 
|---|
|  | 736 | useNewAST = false; | 
|---|
|  | 737 | break; | 
|---|
|  | 738 | case 'A':                                     // don't print non-deterministic output | 
|---|
|  | 739 | useNewAST = true; | 
|---|
| [7215000] | 740 | break; | 
|---|
| [62ce290] | 741 | case 'P':                                                                             // print options | 
|---|
|  | 742 | for ( int i = 0;; i += 1 ) { | 
|---|
|  | 743 | if ( i == printoptsSize ) { | 
|---|
|  | 744 | cout << "Unknown --print option " << optarg << endl; | 
|---|
|  | 745 | goto Default; | 
|---|
|  | 746 | } // if | 
|---|
|  | 747 | if ( strcmp( optarg, printopts[i].name ) == 0 ) { | 
|---|
|  | 748 | printopts[i].flag = printopts[i].val; | 
|---|
|  | 749 | break; | 
|---|
|  | 750 | } // if | 
|---|
|  | 751 | } // for | 
|---|
| [0da3e2c] | 752 | break; | 
|---|
| [62ce290] | 753 | case PreludeDir:                                                              // prelude directory for debug/nodebug, hidden | 
|---|
|  | 754 | PreludeDirector = optarg; | 
|---|
| [3b3491b6] | 755 | break; | 
|---|
| [3e96559] | 756 | case 'S':                                                                             // enable profiling information, argument comma separated list of names | 
|---|
|  | 757 | Stats::parse_params( optarg ); | 
|---|
| [ebcc940] | 758 | break; | 
|---|
| [dee1f89] | 759 | case 't':                                                                             // building cfa stdlib | 
|---|
| [0da3e2c] | 760 | treep = true; | 
|---|
|  | 761 | break; | 
|---|
| [dee1f89] | 762 | case 'g':                                                                             // wait for gdb | 
|---|
|  | 763 | waiting_for_gdb = true; | 
|---|
|  | 764 | break; | 
|---|
| [3e96559] | 765 | case 'w':                                                                             // suppress all warnings, hidden | 
|---|
| [c5e5109] | 766 | Wsuppress = true; | 
|---|
| [44bca7f] | 767 | break; | 
|---|
| [3e96559] | 768 | case 'W':                                                                             // coordinate gcc -W with CFA, hidden | 
|---|
| [44bca7f] | 769 | if ( strcmp( optarg, "all" ) == 0 ) { | 
|---|
| [68e9ace] | 770 | SemanticWarning_EnableAll(); | 
|---|
| [44bca7f] | 771 | } else if ( strcmp( optarg, "error" ) == 0 ) { | 
|---|
|  | 772 | Werror = true; | 
|---|
|  | 773 | } else { | 
|---|
|  | 774 | char * warning = optarg; | 
|---|
|  | 775 | Severity s; | 
|---|
|  | 776 | if ( strncmp( optarg, "no-", 3 ) == 0 ) { | 
|---|
|  | 777 | warning += 3; | 
|---|
|  | 778 | s = Severity::Suppress; | 
|---|
|  | 779 | } else { | 
|---|
|  | 780 | s = Severity::Warn; | 
|---|
|  | 781 | } // if | 
|---|
| [68e9ace] | 782 | SemanticWarning_Set( warning, s ); | 
|---|
| [44bca7f] | 783 | } // if | 
|---|
|  | 784 | break; | 
|---|
| [3e96559] | 785 | case 'D':                                                                             // ignore -Dxxx, forwarded by cpp, hidden | 
|---|
| [0da3e2c] | 786 | break; | 
|---|
| [3e96559] | 787 | case '?':                                                                             // unknown option | 
|---|
|  | 788 | if ( optopt ) {                                                         // short option ? | 
|---|
|  | 789 | cout << "Unknown option -" << (char)optopt << endl; | 
|---|
|  | 790 | } else { | 
|---|
|  | 791 | cout << "Unknown option " << argv[optind - 1] << endl; | 
|---|
|  | 792 | } // if | 
|---|
|  | 793 | goto Default; | 
|---|
|  | 794 | case ':':                                                                             // missing option | 
|---|
| [ae47a23] | 795 | if ( optopt ) {                                                         // short option ? | 
|---|
| [3e96559] | 796 | cout << "Missing option for -" << (char)optopt << endl; | 
|---|
| [ae47a23] | 797 | } else { | 
|---|
| [3e96559] | 798 | cout << "Missing option for " << argv[optind - 1] << endl; | 
|---|
| [ae47a23] | 799 | } // if | 
|---|
| [3e96559] | 800 | goto Default; | 
|---|
|  | 801 | Default: | 
|---|
|  | 802 | default: | 
|---|
|  | 803 | usage( argv );                                                          // no return | 
|---|
| [0da3e2c] | 804 | } // switch | 
|---|
|  | 805 | } // while | 
|---|
| [44bca7f] | 806 |  | 
|---|
|  | 807 | if ( Werror ) { | 
|---|
| [68e9ace] | 808 | SemanticWarning_WarningAsError(); | 
|---|
| [44bca7f] | 809 | } // if | 
|---|
| [c5e5109] | 810 | if ( Wsuppress ) { | 
|---|
|  | 811 | SemanticWarning_SuppressAll(); | 
|---|
|  | 812 | } // if | 
|---|
| [44bca7f] | 813 | // for ( const auto w : WarningFormats ) { | 
|---|
|  | 814 | //      cout << w.name << ' ' << (int)w.severity << endl; | 
|---|
|  | 815 | // } // for | 
|---|
| [0da3e2c] | 816 | } // parse_cmdline | 
|---|
|  | 817 |  | 
|---|
| [8b7ee09] | 818 | static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) { | 
|---|
| [0da3e2c] | 819 | extern int yyparse( void ); | 
|---|
| [cbaee0d] | 820 | extern FILE * yyin; | 
|---|
| [0da3e2c] | 821 | extern int yylineno; | 
|---|
|  | 822 |  | 
|---|
| [8b7ee09] | 823 | ::linkage = linkage;                                                            // set globals | 
|---|
| [0da3e2c] | 824 | yyin = input; | 
|---|
|  | 825 | yylineno = 1; | 
|---|
|  | 826 | int parseStatus = yyparse(); | 
|---|
| [81419b5] | 827 |  | 
|---|
|  | 828 | fclose( input ); | 
|---|
| [0da3e2c] | 829 | if ( shouldExit || parseStatus != 0 ) { | 
|---|
|  | 830 | exit( parseStatus ); | 
|---|
| [81419b5] | 831 | } // if | 
|---|
| [0da3e2c] | 832 | } // parse | 
|---|
| [81419b5] | 833 |  | 
|---|
| [1ab4ce2] | 834 | static bool notPrelude( Declaration * decl ) { | 
|---|
|  | 835 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); | 
|---|
| [0da3e2c] | 836 | } // notPrelude | 
|---|
| [1ab4ce2] | 837 |  | 
|---|
| [e6955b1] | 838 | static void dump( list< Declaration * > & translationUnit, ostream & out ) { | 
|---|
|  | 839 | list< Declaration * > decls; | 
|---|
| [926af74] | 840 |  | 
|---|
| [62ce290] | 841 | if ( genproto ) { | 
|---|
| [e6955b1] | 842 | filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude ); | 
|---|
| [1ab4ce2] | 843 | } else { | 
|---|
|  | 844 | decls = translationUnit; | 
|---|
| [926af74] | 845 | } // if | 
|---|
| [1ab4ce2] | 846 |  | 
|---|
| [e39241b] | 847 | // depending on commandline options, either generate code or dump the AST | 
|---|
|  | 848 | if ( codegenp ) { | 
|---|
| [62ce290] | 849 | CodeGen::generate( decls, out, ! genproto, prettycodegenp ); | 
|---|
| [e39241b] | 850 | } else { | 
|---|
|  | 851 | printAll( decls, out ); | 
|---|
| [3e96559] | 852 | } // if | 
|---|
| [7f5566b] | 853 | deleteAll( translationUnit ); | 
|---|
| [0da3e2c] | 854 | } // dump | 
|---|
| [1ab4ce2] | 855 |  | 
|---|
| [e499381] | 856 | static void dump( ast::TranslationUnit && transUnit, ostream & out ) { | 
|---|
|  | 857 | std::list< Declaration * > translationUnit = convert( move( transUnit ) ); | 
|---|
|  | 858 | dump( translationUnit, out ); | 
|---|
|  | 859 | } | 
|---|
|  | 860 |  | 
|---|
| [51b73452] | 861 | // Local Variables: // | 
|---|
| [b87a5ed] | 862 | // tab-width: 4 // | 
|---|
|  | 863 | // mode: c++ // | 
|---|
|  | 864 | // compile-command: "make install" // | 
|---|
| [51b73452] | 865 | // End:  // | 
|---|