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