source: src/main.cc@ 9e0a360

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 9e0a360 was c6a1e8a, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

fix conflicit

  • Property mode set to 100644
File size: 23.0 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[] = {
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};
476enum { printoptsSize = sizeof( printopts ) / sizeof( printopts[0] ) };
477
[3e96559]478static 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
[62ce290]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
[3e96559]498 } // if
[62ce290]499 if ( optstring[j + 1] == ':' ) j += 1;
[3e96559]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
505static void parse_cmdline( int argc, char * argv[], const char *& filename ) {
[0da3e2c]506 opterr = 0; // (global) prevent getopt from printing error messages
507
[c5e5109]508 bool Wsuppress = false, Werror = false;
[0da3e2c]509 int c;
[3e96559]510 while ( (c = getopt_long( argc, argv, optstring, long_opts, nullptr )) != -1 ) {
[0da3e2c]511 switch ( c ) {
[3e96559]512 case 'h': // help message
513 usage( argv ); // no return
[53d3ab4b]514 break;
[3e96559]515 case 'l': // generate libcfa.c
[0da3e2c]516 libcfap = true;
517 break;
[62ce290]518 case 'L': // generate line marks
[6de43b6]519 linemarks = true;
[c850687]520 break;
[3e96559]521 case 'm': // do not replace main
522 nomainp = true;
[0da3e2c]523 break;
[62ce290]524 case 'N': // do not generate line marks
[6de43b6]525 linemarks = false;
[c59bde6]526 break;
[62ce290]527 case 'n': // do not read prelude
[3e96559]528 nopreludep = true;
[0da3e2c]529 break;
[62ce290]530 case 'p': // generate prototypes for prelude functions
531 genproto = true;
[0da3e2c]532 break;
[62ce290]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
[0da3e2c]544 break;
[62ce290]545 case PreludeDir: // prelude directory for debug/nodebug, hidden
546 PreludeDirector = optarg;
[3b3491b6]547 break;
[3e96559]548 case 'S': // enable profiling information, argument comma separated list of names
549 Stats::parse_params( optarg );
[ebcc940]550 break;
[3e96559]551 case 't': // build in tree
[0da3e2c]552 treep = true;
553 break;
[3e96559]554 case 'w': // suppress all warnings, hidden
[c5e5109]555 Wsuppress = true;
[44bca7f]556 break;
[3e96559]557 case 'W': // coordinate gcc -W with CFA, hidden
[44bca7f]558 if ( strcmp( optarg, "all" ) == 0 ) {
[68e9ace]559 SemanticWarning_EnableAll();
[44bca7f]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
[68e9ace]571 SemanticWarning_Set( warning, s );
[44bca7f]572 } // if
573 break;
[3e96559]574 case 'D': // ignore -Dxxx, forwarded by cpp, hidden
[0da3e2c]575 break;
[3e96559]576 case 'F': // source file-name without suffix, hidden
[0da3e2c]577 filename = optarg;
578 break;
[3e96559]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
[ae47a23]587 if ( optopt ) { // short option ?
[3e96559]588 cout << "Missing option for -" << (char)optopt << endl;
[ae47a23]589 } else {
[3e96559]590 cout << "Missing option for " << argv[optind - 1] << endl;
[ae47a23]591 } // if
[3e96559]592 goto Default;
593 Default:
594 default:
595 usage( argv ); // no return
[0da3e2c]596 } // switch
597 } // while
[44bca7f]598
599 if ( Werror ) {
[68e9ace]600 SemanticWarning_WarningAsError();
[44bca7f]601 } // if
[c5e5109]602 if ( Wsuppress ) {
603 SemanticWarning_SuppressAll();
604 } // if
[44bca7f]605 // for ( const auto w : WarningFormats ) {
606 // cout << w.name << ' ' << (int)w.severity << endl;
607 // } // for
[0da3e2c]608} // parse_cmdline
609
[8b7ee09]610static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
[0da3e2c]611 extern int yyparse( void );
[cbaee0d]612 extern FILE * yyin;
[0da3e2c]613 extern int yylineno;
614
[8b7ee09]615 ::linkage = linkage; // set globals
[0da3e2c]616 yyin = input;
617 yylineno = 1;
618 int parseStatus = yyparse();
[81419b5]619
620 fclose( input );
[0da3e2c]621 if ( shouldExit || parseStatus != 0 ) {
622 exit( parseStatus );
[81419b5]623 } // if
[0da3e2c]624} // parse
[81419b5]625
[1ab4ce2]626static bool notPrelude( Declaration * decl ) {
627 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
[0da3e2c]628} // notPrelude
[1ab4ce2]629
[e6955b1]630static void dump( list< Declaration * > & translationUnit, ostream & out ) {
631 list< Declaration * > decls;
[926af74]632
[62ce290]633 if ( genproto ) {
[e6955b1]634 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
[1ab4ce2]635 } else {
636 decls = translationUnit;
[926af74]637 } // if
[1ab4ce2]638
[e39241b]639 // depending on commandline options, either generate code or dump the AST
640 if ( codegenp ) {
[62ce290]641 CodeGen::generate( decls, out, ! genproto, prettycodegenp );
[e39241b]642 } else {
643 printAll( decls, out );
[3e96559]644 } // if
[7f5566b]645 deleteAll( translationUnit );
[0da3e2c]646} // dump
[1ab4ce2]647
[51b73452]648// Local Variables: //
[b87a5ed]649// tab-width: 4 //
650// mode: c++ //
651// compile-command: "make install" //
[51b73452]652// End: //
Note: See TracBrowser for help on using the repository browser.