source: src/main.cc@ 6215a5c

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 6215a5c was fa2de95, checked in by Aaron Moss <a3moss@…>, 9 years ago

Initial functional version of DeclStats

  • Property mode set to 100644
File size: 14.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// main.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Fri May 15 23:12:02 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Dec 14 14:35:54 2016
13// Update Count : 436
14//
15
16#include <iostream>
17#include <fstream>
18#include <signal.h> // signal
19#include <getopt.h> // getopt
20#include <execinfo.h> // backtrace, backtrace_symbols
21#include <cxxabi.h> // __cxa_demangle
22#include <cstring> // index
23
24using namespace std;
25
26#include "Parser/lex.h"
27#include "Parser/parser.h"
28#include "Parser/TypedefTable.h"
29#include "GenPoly/Lvalue.h"
30#include "GenPoly/Specialize.h"
31#include "GenPoly/Box.h"
32#include "GenPoly/CopyParams.h"
33#include "GenPoly/InstantiateGeneric.h"
34#include "CodeGen/Generate.h"
35#include "CodeGen/FixNames.h"
36#include "CodeGen/FixMain.h"
37#include "CodeTools/DeclStats.h"
38#include "ControlStruct/Mutate.h"
39#include "SymTab/Validate.h"
40#include "ResolvExpr/AlternativePrinter.h"
41#include "ResolvExpr/Resolver.h"
42#include "MakeLibCfa.h"
43#include "InitTweak/GenInit.h"
44#include "InitTweak/FixInit.h"
45#include "Common/UnimplementedError.h"
46#include "../config.h"
47#include "Tuples/Tuples.h"
48
49using namespace std;
50
51#define OPTPRINT(x) if ( errorp ) cerr << x << endl;
52
53
54LinkageSpec::Spec linkage = LinkageSpec::Cforall;
55TypedefTable typedefTable;
56DeclarationNode * parseTree = nullptr; // program parse tree
57
58extern int yydebug; // set for -g flag (Grammar)
59bool
60 astp = false,
61 bresolvep = false,
62 bboxp = false,
63 ctorinitp = false,
64 declstatsp = false,
65 exprp = false,
66 expraltp = false,
67 libcfap = false,
68 nopreludep = false,
69 noprotop = false,
70 nomainp = false,
71 parsep = false,
72 resolvep = false, // used in AlternativeFinder
73 symtabp = false,
74 treep = false,
75 tuplep = false,
76 validp = false,
77 errorp = false,
78 codegenp = false;
79
80static void parse_cmdline( int argc, char *argv[], const char *& filename );
81static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
82static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
83
84static void backtrace( int start ) { // skip first N stack frames
85 enum { Frames = 50 };
86 void * array[Frames];
87 int size = ::backtrace( array, Frames );
88 char ** messages = ::backtrace_symbols( array, size ); // does not demangle names
89
90 *index( messages[0], '(' ) = '\0'; // find executable name
91 cerr << "Stack back trace for: " << messages[0] << endl;
92
93 // skip last 2 stack frames after main
94 for ( int i = start; i < size - 2 && messages != nullptr; i += 1 ) {
95 char * mangled_name = nullptr, * offset_begin = nullptr, * offset_end = nullptr;
96 for ( char *p = messages[i]; *p; ++p ) { // find parantheses and +offset
97 if ( *p == '(' ) {
98 mangled_name = p;
99 } else if ( *p == '+' ) {
100 offset_begin = p;
101 } else if ( *p == ')' ) {
102 offset_end = p;
103 break;
104 } // if
105 } // for
106
107 // if line contains symbol, attempt to demangle
108 int frameNo = i - start;
109 if ( mangled_name && offset_begin && offset_end && mangled_name < offset_begin ) {
110 *mangled_name++ = '\0'; // delimit strings
111 *offset_begin++ = '\0';
112 *offset_end++ = '\0';
113
114 int status;
115 char * real_name = __cxxabiv1::__cxa_demangle( mangled_name, 0, 0, &status );
116 // bug in __cxa_demangle for single-character lower-case non-mangled names
117 if ( status == 0 ) { // demangling successful ?
118 cerr << "(" << frameNo << ") " << messages[i] << " : "
119 << real_name << "+" << offset_begin << offset_end << endl;
120 } else { // otherwise, output mangled name
121 cerr << "(" << frameNo << ") " << messages[i] << " : "
122 << mangled_name << "(/*unknown*/)+" << offset_begin << offset_end << endl;
123 } // if
124
125 free( real_name );
126 } else { // otherwise, print the whole line
127 cerr << "(" << frameNo << ") " << messages[i] << endl;
128 } // if
129 } // for
130
131 free( messages );
132} // backtrace
133
134void sigSegvBusHandler( int sig_num ) {
135 cerr << "*CFA runtime error* program cfa-cpp terminated with "
136 << (sig_num == SIGSEGV ? "segment fault" : "bus error")
137 << "." << endl;
138 backtrace( 2 ); // skip first 2 stack frames
139 exit( EXIT_FAILURE );
140} // sigSegvBusHandler
141
142void sigAbortHandler( int sig_num ) {
143 backtrace( 6 ); // skip first 6 stack frames
144 signal( SIGABRT, SIG_DFL); // reset default signal handler
145 raise( SIGABRT ); // reraise SIGABRT
146} // sigAbortHandler
147
148
149int main( int argc, char * argv[] ) {
150 FILE * input; // use FILE rather than istream because yyin is FILE
151 ostream *output = & cout;
152 const char *filename = nullptr;
153 list< Declaration * > translationUnit;
154
155 signal( SIGSEGV, sigSegvBusHandler );
156 signal( SIGBUS, sigSegvBusHandler );
157 signal( SIGABRT, sigAbortHandler );
158
159 parse_cmdline( argc, argv, filename ); // process command-line arguments
160 CodeGen::FixMain::setReplaceMain( !nomainp );
161
162 try {
163 // choose to read the program from a file or stdin
164 if ( optind < argc ) { // any commands after the flags ? => input file name
165 input = fopen( argv[ optind ], "r" );
166 assertf( input, "cannot open %s\n", argv[ optind ] );
167 // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
168 if ( filename == nullptr ) filename = argv[ optind ];
169 // prelude filename comes in differently
170 if ( libcfap ) filename = "prelude.cf";
171 optind += 1;
172 } else { // no input file name
173 input = stdin;
174 // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
175 // a fake name along
176 if ( filename == nullptr ) filename = "stdin";
177 } // if
178
179 // read in the builtins, extras, and the prelude
180 if ( ! nopreludep ) { // include gcc builtins
181 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
182 FILE * builtins = fopen( libcfap | treep ? "../prelude/builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
183 assertf( builtins, "cannot open builtins.cf\n" );
184 parse( builtins, LinkageSpec::Compiler );
185
186 // read the extra prelude in, if not generating the cfa library
187 FILE * extras = fopen( libcfap | treep ? "../prelude/extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
188 assertf( extras, "cannot open extras.cf\n" );
189 parse( extras, LinkageSpec::C );
190
191 if ( ! libcfap ) {
192 // read the prelude in, if not generating the cfa library
193 FILE * prelude = fopen( treep ? "../prelude/prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
194 assertf( prelude, "cannot open prelude.cf\n" );
195 parse( prelude, LinkageSpec::Intrinsic );
196 } // if
197 } // if
198
199 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
200
201 if ( parsep ) {
202 parseTree->printList( cout );
203 delete parseTree;
204 return 0;
205 } // if
206
207 buildList( parseTree, translationUnit );
208 delete parseTree;
209 parseTree = nullptr;
210
211 if ( astp ) {
212 dump( translationUnit );
213 return 0;
214 } // if
215
216 // add the assignment statement after the initialization of a type parameter
217 OPTPRINT( "validate" )
218 SymTab::validate( translationUnit, symtabp );
219 if ( symtabp ) {
220 deleteAll( translationUnit );
221 return 0;
222 } // if
223
224 if ( expraltp ) {
225 ResolvExpr::AlternativePrinter printer( cout );
226 acceptAll( translationUnit, printer );
227 return 0;
228 } // if
229
230 if ( validp ) {
231 dump( translationUnit );
232 return 0;
233 } // if
234
235 OPTPRINT( "mutate" )
236 ControlStruct::mutate( translationUnit );
237 OPTPRINT( "fixNames" )
238 CodeGen::fixNames( translationUnit );
239 OPTPRINT( "tweakInit" )
240 InitTweak::genInit( translationUnit );
241 OPTPRINT( "expandMemberTuples" );
242 Tuples::expandMemberTuples( translationUnit );
243 if ( libcfap ) {
244 // generate the bodies of cfa library functions
245 LibCfa::makeLibCfa( translationUnit );
246 } // if
247
248 if ( declstatsp ) {
249 CodeTools::printDeclStats( translationUnit );
250 deleteAll( translationUnit );
251 return 0;
252 }
253
254 if ( bresolvep ) {
255 dump( translationUnit );
256 return 0;
257 } // if
258
259 OPTPRINT( "resolve" )
260 ResolvExpr::resolve( translationUnit );
261 if ( exprp ) {
262 dump( translationUnit );
263 return 0;
264 } // if
265
266 // fix ObjectDecl - replaces ConstructorInit nodes
267 OPTPRINT( "fixInit" )
268 InitTweak::fix( translationUnit, filename, libcfap || treep );
269 if ( ctorinitp ) {
270 dump ( translationUnit );
271 return 0;
272 } // if
273
274 OPTPRINT( "expandUniqueExpr" ); // xxx - is this the right place for this? want to expand ASAP so that subsequent 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
275 Tuples::expandUniqueExpr( translationUnit );
276
277 OPTPRINT( "convertSpecializations" ) // needs to happen before tuple types are expanded
278 GenPoly::convertSpecializations( translationUnit );
279
280 OPTPRINT( "expandTuples" ); // xxx - is this the right place for this?
281 Tuples::expandTuples( translationUnit );
282 if ( tuplep ) {
283 dump( translationUnit );
284 return 0;
285 }
286
287 OPTPRINT("instantiateGenerics")
288 GenPoly::instantiateGeneric( translationUnit );
289 OPTPRINT( "copyParams" );
290 GenPoly::copyParams( translationUnit );
291 OPTPRINT( "convertLvalue" )
292 GenPoly::convertLvalue( translationUnit );
293
294 if ( bboxp ) {
295 dump( translationUnit );
296 return 0;
297 } // if
298 OPTPRINT( "box" )
299 GenPoly::box( translationUnit );
300
301 // print tree right before code generation
302 if ( codegenp ) {
303 dump( translationUnit );
304 return 0;
305 } // if
306
307 if ( optind < argc ) { // any commands after the flags and input file ? => output file name
308 output = new ofstream( argv[ optind ] );
309 } // if
310
311 CodeGen::generate( translationUnit, *output, ! noprotop );
312
313 CodeGen::FixMain::fix( *output, treep ? "../prelude/bootloader.c" : CFA_LIBDIR "/bootloader.c" );
314
315 if ( output != &cout ) {
316 delete output;
317 } // if
318 } catch ( SemanticError &e ) {
319 if ( errorp ) {
320 cerr << "---AST at error:---" << endl;
321 dump( translationUnit, cerr );
322 cerr << endl << "---End of AST, begin error message:---\n" << endl;
323 } // if
324 e.print( cerr );
325 if ( output != &cout ) {
326 delete output;
327 } // if
328 return 1;
329 } catch ( UnimplementedError &e ) {
330 cout << "Sorry, " << e.get_what() << " is not currently implemented" << endl;
331 if ( output != &cout ) {
332 delete output;
333 } // if
334 return 1;
335 } catch ( CompilerError &e ) {
336 cerr << "Compiler Error: " << e.get_what() << endl;
337 cerr << "(please report bugs to " << endl;
338 if ( output != &cout ) {
339 delete output;
340 } // if
341 return 1;
342 } // try
343
344 deleteAll( translationUnit );
345 return 0;
346} // main
347
348void parse_cmdline( int argc, char * argv[], const char *& filename ) {
349 enum { Ast, Bbox, Bresolver, CtorInitFix, DeclStats, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, TupleExpansion, Validate, };
350
351 static struct option long_opts[] = {
352 { "ast", no_argument, 0, Ast },
353 { "before-box", no_argument, 0, Bbox },
354 { "before-resolver", no_argument, 0, Bresolver },
355 { "ctorinitfix", no_argument, 0, CtorInitFix },
356 { "decl-stats", no_argument, 0, DeclStats },
357 { "expr", no_argument, 0, Expr },
358 { "expralt", no_argument, 0, ExprAlt },
359 { "grammar", no_argument, 0, Grammar },
360 { "libcfa", no_argument, 0, LibCFA },
361 { "no-preamble", no_argument, 0, Nopreamble },
362 { "parse", no_argument, 0, Parse },
363 { "no-prototypes", no_argument, 0, Prototypes },
364 { "resolver", no_argument, 0, Resolver },
365 { "symbol", no_argument, 0, Symbol },
366 { "tree", no_argument, 0, Tree },
367 { "tuple-expansion", no_argument, 0, TupleExpansion },
368 { "validate", no_argument, 0, Validate },
369 { 0, 0, 0, 0 }
370 }; // long_opts
371 int long_index;
372
373 opterr = 0; // (global) prevent getopt from printing error messages
374
375 int c;
376 while ( (c = getopt_long( argc, argv, "abBcdefglmnpqrstTvyzD:F:", long_opts, &long_index )) != -1 ) {
377 switch ( c ) {
378 case Ast:
379 case 'a': // dump AST
380 astp = true;
381 break;
382 case Bresolver:
383 case 'b': // print before resolver steps
384 bresolvep = true;
385 break;
386 case 'B': // print before box steps
387 bboxp = true;
388 break;
389 case CtorInitFix:
390 case 'c':
391 ctorinitp = true;
392 break;
393 case DeclStats:
394 case 'd':
395 declstatsp = true;
396 break;
397 case Expr:
398 case 'e': // dump AST after expression analysis
399 exprp = true;
400 break;
401 case ExprAlt:
402 case 'f': // print alternatives for expressions
403 expraltp = true;
404 break;
405 case Grammar:
406 case 'g': // bison debugging info (grammar rules)
407 yydebug = true;
408 break;
409 case LibCFA:
410 case 'l': // generate libcfa.c
411 libcfap = true;
412 break;
413 case Nopreamble:
414 case 'n': // do not read preamble
415 nopreludep = true;
416 break;
417 case Prototypes:
418 case 'p': // generate prototypes for preamble functions
419 noprotop = true;
420 break;
421 case 'm': // don't replace the main
422 nomainp = true;
423 break;
424 case Parse:
425 case 'q': // dump parse tree
426 parsep = true;
427 break;
428 case Resolver:
429 case 'r': // print resolver steps
430 resolvep = true;
431 break;
432 case Symbol:
433 case 's': // print symbol table events
434 symtabp = true;
435 break;
436 case Tree:
437 case 't': // build in tree
438 treep = true;
439 break;
440 case TupleExpansion:
441 case 'T': // print after tuple expansion
442 tuplep = true;
443 break;
444 case 'v': // dump AST after decl validation pass
445 validp = true;
446 break;
447 case 'y':
448 errorp = true;
449 break;
450 case 'z':
451 codegenp = true;
452 break;
453 case 'D': // ignore -Dxxx
454 break;
455 case 'F': // source file-name without suffix
456 filename = optarg;
457 break;
458 case '?':
459 assertf( false, "Unknown option: '%c'\n", (char)optopt );
460 default:
461 abort();
462 } // switch
463 } // while
464} // parse_cmdline
465
466static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
467 extern int yyparse( void );
468 extern FILE * yyin;
469 extern int yylineno;
470
471 ::linkage = linkage; // set globals
472 yyin = input;
473 yylineno = 1;
474 typedefTable.enterScope();
475 int parseStatus = yyparse();
476
477 fclose( input );
478 if ( shouldExit || parseStatus != 0 ) {
479 exit( parseStatus );
480 } // if
481} // parse
482
483static bool notPrelude( Declaration * decl ) {
484 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
485} // notPrelude
486
487static void dump( list< Declaration * > & translationUnit, ostream & out ) {
488 list< Declaration * > decls;
489
490 if ( noprotop ) {
491 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), notPrelude );
492 } else {
493 decls = translationUnit;
494 } // if
495
496 printAll( decls, out );
497 deleteAll( translationUnit );
498} // dump
499
500// Local Variables: //
501// tab-width: 4 //
502// mode: c++ //
503// compile-command: "make install" //
504// End: //
Note: See TracBrowser for help on using the repository browser.