source: src/main.cc@ aefcc3b

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 aefcc3b was aefcc3b, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

major change to instantiateFunction to match arguments against the formal parameter's structure rather than the reverse

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