source: src/main.cc@ 1fcc2f3

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 1fcc2f3 was 3f3bfe5a, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Merge from master to new-ast. Removing old lvalue support.

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