source: src/main.cc@ f253e4a

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since f253e4a was 0e464f6, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

cfa-cpp help message now puts all dump commands together and in order.

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