source: src/main.cc@ 90ce35aa

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 90ce35aa was 4a60488, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Merged from master taking the lvalue changes to expression and everything before that.

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