source: src/main.cc@ 6d160d7

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

First draft of member expressions by passed offset

  • Property mode set to 100644
File size: 8.7 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//
7// main.cc --
8//
[843054c2]9// Author : Richard C. Bilson
[b87a5ed]10// Created On : Fri May 15 23:12:02 2015
[839ccbb]11// Last Modified By : Peter A. Buhr
[faf8857]12// Last Modified On : Thu Dec 17 12:59:06 2015
13// Update Count : 179
[b87a5ed]14//
15
[51b73452]16#include <iostream>
17#include <fstream>
[b87a5ed]18#include <cstdlib>
[51b73452]19#include <cstdio>
[b87a5ed]20#include <getopt.h>
[51b73452]21#include "Parser/Parser.h"
22#include "Parser/ParseNode.h"
23#include "Parser/LinkageSpec.h"
24#include "SynTree/Declaration.h"
25#include "SynTree/Visitor.h"
26#include "GenPoly/Lvalue.h"
27#include "GenPoly/Specialize.h"
28#include "GenPoly/Box.h"
29#include "GenPoly/CopyParams.h"
30#include "CodeGen/Generate.h"
31#include "CodeGen/FixNames.h"
32#include "ControlStruct/Mutate.h"
33#include "Tuples/Mutate.h"
34#include "Tuples/FunctionChecker.h"
35#include "SymTab/Mangler.h"
36#include "SymTab/Indexer.h"
37#include "SymTab/Validate.h"
38#include "ResolvExpr/AlternativePrinter.h"
39#include "ResolvExpr/Resolver.h"
40#include "MakeLibCfa.h"
41#include "InitTweak/Mutate.h"
[42e2ad7]42#include "InitTweak/RemoveInit.h"
[51b73452]43//#include "Explain/GenProlog.h"
44//#include "Try/Visit.h"
45
46#include "SemanticError.h"
47#include "UnimplementedError.h"
48
49#include "../config.h"
50
51using namespace std;
52
[81419b5]53#define OPTPRINT(x) \
54 if ( errorp ) std::cerr << x << std::endl;
55
[1ab4ce2]56static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
57static void dump( std::list< Declaration * > & translationUnit );
[81419b5]58
[b87a5ed]59bool
60 astp = false,
[de62360d]61 bresolvep = false,
[b87a5ed]62 exprp = false,
63 expraltp = false,
64 grammarp = false,
65 libcfap = false,
[de62360d]66 nopreludep = false,
[1ab4ce2]67 noprotop = false,
[de62360d]68 parsep = false,
[b87a5ed]69 resolvep = false, // used in AlternativeFinder
70 symtabp = false,
71 validp = false,
[de62360d]72 errorp = false,
73 codegenp = false;
[b87a5ed]74
[de62360d]75enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, };
[b87a5ed]76
77static struct option long_opts[] = {
78 { "ast", no_argument, 0, Ast },
[de62360d]79 { "before-resolver", no_argument, 0, Bresolver },
[b87a5ed]80 { "expr", no_argument, 0, Expr },
81 { "expralt", no_argument, 0, ExprAlt },
82 { "grammar", no_argument, 0, Grammar },
83 { "libcfa", no_argument, 0, LibCFA },
[1ab4ce2]84 { "no-preamble", no_argument, 0, Nopreamble },
[b1d6dd5]85 { "parse", no_argument, 0, Parse },
[1ab4ce2]86 { "no-prototypes", no_argument, 0, Prototypes },
[b87a5ed]87 { "resolver", no_argument, 0, Resolver },
88 { "symbol", no_argument, 0, Symbol },
[b1d6dd5]89 { "validate", no_argument, 0, Validate },
[b87a5ed]90 { 0, 0, 0, 0 }
91};
[51b73452]92
[8c17ab0]93int main( int argc, char *argv[] ) {
[b87a5ed]94 FILE *input;
95 std::ostream *output = &std::cout;
96 int long_index;
97 std::list< Declaration* > translationUnit;
98
99 opterr = 0; // prevent getopt from printing error messages
100
101 int c;
[de62360d]102 while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) {
[b87a5ed]103 switch ( c ) {
104 case Ast:
105 case 'a': // dump AST
106 astp = true;
107 break;
[de62360d]108 case Bresolver:
109 case 'b': // print before resolver steps
110 bresolvep = true;
111 break;
[b87a5ed]112 case Expr:
113 case 'e': // dump AST after expression analysis
114 exprp = true;
115 break;
116 case ExprAlt:
117 case 'f': // print alternatives for expressions
118 expraltp = true;
119 break;
120 case Grammar:
121 case 'g': // bison debugging info (grammar rules)
122 grammarp = true;
123 break;
124 case LibCFA:
125 case 'l': // generate libcfa.c
126 libcfap = true;
127 break;
128 case Nopreamble:
129 case 'n': // do not read preamble
[de62360d]130 nopreludep = true;
[b87a5ed]131 break;
132 case Prototypes:
133 case 'p': // generate prototypes for preamble functions
[1ab4ce2]134 noprotop = true;
[b87a5ed]135 break;
136 case Parse:
137 case 'q': // dump parse tree
138 parsep = true;
139 break;
140 case Resolver:
141 case 'r': // print resolver steps
142 resolvep = true;
143 break;
144 case Symbol:
145 case 's': // print symbol table events
146 symtabp = true;
147 break;
[b1d6dd5]148 case 'v': // dump AST after decl validation pass
[b87a5ed]149 validp = true;
150 break;
151 case 'y':
152 errorp = true;
153 break;
154 case 'z':
155 codegenp = true;
156 break;
157 case 'D': // ignore -Dxxx
158 break;
159 case '?':
160 cout << "Unknown option: '" << (char)optopt << "'" << endl;
161 exit(1);
162 default:
163 abort();
164 } // switch
165 } // while
166
167 try {
[81419b5]168 // choose to read the program from a file or stdin
[b87a5ed]169 if ( optind < argc ) {
170 input = fopen( argv[ optind ], "r" );
171 if ( ! input ) {
172 std::cout << "Error: can't open " << argv[optind] << std::endl;
173 exit( 1 );
174 } // if
175 optind += 1;
176 } else {
177 input = stdin;
178 } // if
179
180 if ( optind < argc ) {
181 output = new ofstream( argv[ optind ] );
182 } // if
183
184 Parser::get_parser().set_debug( grammarp );
[81419b5]185
186 // read in the builtins and the prelude
[de62360d]187 if ( ! nopreludep ) { // include gcc builtins
[faf8857]188 // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
189 FILE * builtins = fopen( libcfap ? "./builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[b87a5ed]190 if ( builtins == NULL ) {
[81419b5]191 std::cerr << "Error: can't open builtins" << std::endl;
[b87a5ed]192 exit( 1 );
193 } // if
194
[81419b5]195 parse( builtins, LinkageSpec::Compiler );
196
197 if ( ! libcfap ) {
[faf8857]198 // read the prelude in, if not generating the cfa library
[81419b5]199 FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
200 if ( prelude == NULL ) {
201 std::cerr << "Error: can't open prelude" << std::endl;
202 exit( 1 );
203 } // if
204
[35304009]205 parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]206 } // if
207 } // if
[81419b5]208
[faf8857]209 parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
[51b73452]210
[b87a5ed]211 if ( parsep ) {
212 Parser::get_parser().get_parseTree()->printList( std::cout );
213 Parser::get_parser().freeTree();
214 return 0;
215 } // if
216
217 buildList( Parser::get_parser().get_parseTree(), translationUnit );
218
219 Parser::get_parser().freeTree();
220 if ( astp ) {
[1ab4ce2]221 dump( translationUnit );
[b87a5ed]222 return 0;
223 } // if
224
[839ccbb]225 // add the assignment statement after the initialization of a type parameter
[81419b5]226 OPTPRINT( "validate" )
227 SymTab::validate( translationUnit, symtabp );
228 if ( symtabp ) {
[b87a5ed]229 return 0;
230 } // if
231
[81419b5]232 if ( expraltp ) {
233 ResolvExpr::AlternativePrinter printer( std::cout );
234 acceptAll( translationUnit, printer );
[b87a5ed]235 return 0;
236 } // if
237
238 if ( validp ) {
[1ab4ce2]239 dump( translationUnit );
[b87a5ed]240 return 0;
241 } // if
242
[81419b5]243 OPTPRINT( "mutate" )
244 ControlStruct::mutate( translationUnit );
245 OPTPRINT( "fixNames" )
246 CodeGen::fixNames( translationUnit );
[e497c1d]247 OPTPRINT( "tweak" )
248 InitTweak::tweak( translationUnit );
[b87a5ed]249
[81419b5]250 if ( libcfap ) {
251 // generate the bodies of cfa library functions
252 LibCfa::makeLibCfa( translationUnit );
[b87a5ed]253 } // if
254
[de62360d]255 if ( bresolvep ) {
[1ab4ce2]256 dump( translationUnit );
[de62360d]257 return 0;
258 } // if
259
[81419b5]260 OPTPRINT( "resolve" )
[b87a5ed]261 ResolvExpr::resolve( translationUnit );
[81419b5]262 if ( exprp ) {
[1ab4ce2]263 dump( translationUnit );
[81419b5]264 }
265
266 OPTPRINT( "copyParams" );
[b87a5ed]267 GenPoly::copyParams( translationUnit );
[81419b5]268 OPTPRINT( "convertSpecializations" )
[698664b3]269 GenPoly::convertSpecializations( translationUnit );
[81419b5]270 OPTPRINT( "convertLvalue" )
[b87a5ed]271 GenPoly::convertLvalue( translationUnit );
[81419b5]272 OPTPRINT( "box" )
[b87a5ed]273 GenPoly::box( translationUnit );
[05d47278]274
[2871210]275 // print tree right before code generation
[81419b5]276 if ( codegenp ) {
[1ab4ce2]277 dump( translationUnit );
[81419b5]278 return 0;
279 } // if
[b87a5ed]280
[1ab4ce2]281 CodeGen::generate( translationUnit, *output, ! noprotop );
[b87a5ed]282
283 if ( output != &std::cout ) {
284 delete output;
285 } // if
286 } catch ( SemanticError &e ) {
287 if ( errorp ) {
[1ab4ce2]288 dump( translationUnit );
[b87a5ed]289 }
[81419b5]290 e.print( std::cerr );
[b87a5ed]291 if ( output != &std::cout ) {
292 delete output;
293 } // if
294 return 1;
295 } catch ( UnimplementedError &e ) {
296 std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
297 if ( output != &std::cout ) {
298 delete output;
299 } // if
300 return 1;
301 } catch ( CompilerError &e ) {
302 std::cerr << "Compiler Error: " << e.get_what() << std::endl;
303 std::cerr << "(please report bugs to " << std::endl;
304 if ( output != &std::cout ) {
305 delete output;
306 } // if
307 return 1;
308 } // try
309
310 return 0;
[d9a0e76]311} // main
[51b73452]312
[1ab4ce2]313static void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
[81419b5]314 Parser::get_parser().set_linkage( linkage );
315 Parser::get_parser().parse( input );
316
317 fclose( input );
318 if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
319 exit( Parser::get_parser().get_parseStatus() );
320 } // if
321}
322
[1ab4ce2]323static bool notPrelude( Declaration * decl ) {
324 return ! LinkageSpec::isBuiltin( decl->get_linkage() );
325}
326
327static void dump( std::list< Declaration * > & translationUnit ) {
328 std::list< Declaration * > decls;
329 if ( noprotop ) {
330 filter( translationUnit.begin(), translationUnit.end(),
331 std::back_inserter( decls ), notPrelude );
332 } else {
333 decls = translationUnit;
334 }
335
336 printAll( decls, std::cout );
[7f5566b]337 deleteAll( translationUnit );
[1ab4ce2]338}
339
340
[51b73452]341// Local Variables: //
[b87a5ed]342// tab-width: 4 //
343// mode: c++ //
344// compile-command: "make install" //
[51b73452]345// End: //
Note: See TracBrowser for help on using the repository browser.