source: translator/main.cc@ 3c70d38

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 stuck-waitfor-destruct with_gc
Last change on this file since 3c70d38 was 8c17ab0, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

add quoted identifiers, add compilation include directory, reformatted some files

  • Property mode set to 100644
File size: 7.5 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <iterator>
4#include <algorithm>
5#include <cstdio>
6#include "Parser/Parser.h"
7#include "Parser/ParseNode.h"
8#include "Parser/LinkageSpec.h"
9#include "SynTree/Declaration.h"
10#include "SynTree/Visitor.h"
11#include "GenPoly/Lvalue.h"
12#include "GenPoly/Specialize.h"
13#include "GenPoly/Box.h"
14#include "GenPoly/CopyParams.h"
15#include "CodeGen/Generate.h"
16#include "CodeGen/FixNames.h"
17#include "ControlStruct/Mutate.h"
18#include "Tuples/Mutate.h"
19#include "Tuples/FunctionChecker.h"
20#include "SymTab/Mangler.h"
21#include "SymTab/Indexer.h"
22#include "SymTab/Validate.h"
23#include "ResolvExpr/AlternativePrinter.h"
24#include "ResolvExpr/Resolver.h"
25#include "MakeLibCfa.h"
26#include "InitTweak/Mutate.h"
27//#include "Explain/GenProlog.h"
28//#include "Try/Visit.h"
29
30#include "SemanticError.h"
31#include "UnimplementedError.h"
32#include "utility.h"
33
34#include "../config.h"
35
36using namespace std;
37
38extern "C"{
39#include <unistd.h>
40extern int getopt( int, char *const *, const char * );
41extern char *optarg;
42extern int opterr, optind, optopt;
43}
44
45FILE *open_prelude();
46FILE *open_builtins();
47bool beVerbose = false;
48
49int main( int argc, char *argv[] ) {
50 bool debugp = false, treep = false, astp = false, manglep = false, symtabp = false, validp = false;
51 bool preludep = true, protop = false, libp = false;
52 bool exprp = false;
53 int c;
54 FILE *input, *prelude, *builtins;
55 std::ostream *output = &std::cout;
56
57 opterr = 0;
58
59 while ( (c = getopt( argc, argv, "dtsgmvxcenplD:" )) != -1 ) {
60 switch (c) {
61 case 'd':
62 /* bison debugging info */
63 debugp = true;
64 break;
65 case 't':
66 /* dump parse tree */
67 treep = true;
68 break;
69 case 's':
70 /* dump AST */
71 astp = true;
72 break;
73 case 'g':
74 /* print alternatives for expressions */
75 manglep = true;
76 break;
77 case 'm':
78 /* print symbol table events */
79 symtabp = true;
80 break;
81 case 'x':
82 /* dump AST after decl validation pass */
83 validp = true;
84 break;
85 case 'e':
86 /* dump AST after expression analysis */
87 exprp = true;
88 break;
89 case 'n':
90 /* don't read preamble */
91 preludep = false;
92 break;
93 case 'p':
94 /* generate prototypes for preamble functions */
95 protop = true;
96 break;
97 case 'l':
98 /* generate libcfa.c */
99 libp = true;
100 break;
101 case 'v':
102 /* verbose */
103 beVerbose = true;
104 break;
105 case 'D':
106 /* ignore -Dxxx */
107 break;
108 case '?':
109 cout << "Unknown option: '" << (char)optopt << "'" << endl;
110 exit(1);
111 default:
112 abort();
113 }
114 }
115
116 try {
117 if ( optind < argc ) {
118 input = fopen( argv[ optind ], "r" );
119 if ( !input ) {
120 std::cout << "Error: can't open " << argv[optind] << std::endl;
121 exit( 1 );
122 }
123 optind++;
124 } else {
125 input = stdin;
126 }
127
128 if ( optind < argc ) {
129 output = new ofstream( argv[ optind ] );
130 }
131
132 Parser::get_parser().set_debug( debugp );
133
134 if ( preludep ) {
135 // include gcc builtins
136 builtins = open_builtins();
137 if ( !builtins ) {
138 std::cout << "Error: can't open builtins" << std::endl;
139 exit( 1 );
140 }
141
142 Parser::get_parser().set_linkage( LinkageSpec::Compiler );
143 Parser::get_parser().parse( builtins );
144
145 if ( Parser::get_parser().get_parseStatus() != 0 ) {
146 return Parser::get_parser().get_parseStatus();
147 }
148 fclose( builtins );
149
150 // include cfa prelude
151 if ( libp ) {
152 prelude = input;
153 } else {
154 prelude = open_prelude();
155 }
156 if ( !prelude ) {
157 std::cout << "Error: can't open prelude" << std::endl;
158 exit( 1 );
159 }
160
161 Parser::get_parser().set_linkage( LinkageSpec::Intrinsic );
162 Parser::get_parser().parse( prelude );
163
164 if ( Parser::get_parser().get_parseStatus() != 0 ) {
165 return Parser::get_parser().get_parseStatus();
166 }
167 fclose( prelude );
168 }
169
170 if ( libp ) {
171 std::list< Declaration* > translationUnit;
172 buildList( Parser::get_parser().get_parseTree(), translationUnit );
173 Parser::get_parser().freeTree();
174 SymTab::validate( translationUnit, false );
175 CodeGen::fixNames( translationUnit );
176 LibCfa::makeLibCfa( translationUnit );
177 ResolvExpr::resolve( translationUnit );
178 GenPoly::convertLvalue( translationUnit );
179 GenPoly::box( translationUnit );
180 CodeGen::generate( translationUnit, *output, true );
181 if ( output != &std::cout ) {
182 delete output;
183 }
184 return 0;
185 }
186
187 Parser::get_parser().set_linkage( LinkageSpec::Cforall );
188
189 Parser::get_parser().parse( input );
190 if ( debugp || Parser::get_parser().get_parseStatus() != 0 ) {
191 return Parser::get_parser().get_parseStatus();
192 }
193 fclose( input );
194
195 if ( treep ) {
196 Parser::get_parser().get_parseTree()->printList( std::cout );
197 Parser::get_parser().freeTree();
198 return 0;
199 }
200
201 std::list< Declaration* > translationUnit;
202 buildList( Parser::get_parser().get_parseTree(), translationUnit );
203
204 Parser::get_parser().freeTree();
205 if ( astp ) {
206 printAll( translationUnit, std::cout );
207 return 0;
208 }
209
210 if ( manglep ) {
211 SymTab::validate( translationUnit, false );
212 ResolvExpr::AlternativePrinter printer( std::cout );
213 acceptAll( translationUnit, printer );
214 return 0;
215 }
216
217 if ( symtabp ) {
218 SymTab::validate( translationUnit, true );
219 return 0;
220 }
221
222 if ( validp ) {
223 SymTab::validate( translationUnit, false );
224 printAll( translationUnit, std::cout );
225 return 0;
226 }
227 if ( exprp ) {
228 SymTab::validate( translationUnit, false );
229 ResolvExpr::resolve( translationUnit );
230 printAll( translationUnit, std::cout );
231 return 0;
232 }
233
234 SymTab::validate( translationUnit, false );
235 //Try::visit( translationUnit );
236 //Tuples::mutate( translationUnit );
237 //InitTweak::mutate( translationUnit );
238 ControlStruct::mutate( translationUnit );
239 CodeGen::fixNames( translationUnit );
240 ResolvExpr::resolve( translationUnit );
241 //Tuples::checkFunctions( translationUnit );
242 // std::cerr << "Finished tuple checkfunctions" << std::endl;
243 //printAll( translationUnit, std::cerr );
244 GenPoly::copyParams( translationUnit );
245 GenPoly::convertSpecializations( translationUnit );
246 GenPoly::convertLvalue( translationUnit );
247 GenPoly::box( translationUnit );
248 //Tuples::mutate( translationUnit );
249
250 CodeGen::generate( translationUnit, *output, protop );
251
252 if ( output != &std::cout ) {
253 delete output;
254 }
255
256 } catch ( SemanticError &e ) {
257 e.print( cout );
258 if ( output != &std::cout ) {
259 delete output;
260 }
261 return 1;
262 } catch ( UnimplementedError &e ) {
263 std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
264 if ( output != &std::cout ) {
265 delete output;
266 }
267 return 1;
268 } catch ( CompilerError &e ) {
269 std::cerr << "Compiler Error: " << e.get_what() << std::endl;
270 std::cerr << "(please report bugs to " << std::endl;
271 if ( output != &std::cout ) {
272 delete output;
273 }
274 return 1;
275 }
276
277 return 0;
278}
279
280FILE *open_prelude() {
281 FILE *ret;
282
283 const string name("prelude.cf"),
284 full_name = string(CFA_LIBDIR) + "/" + name;
285
286 if ( beVerbose ) {
287 cout << "Reading from " << full_name << endl;
288 }
289
290 if (! (ret = fopen(full_name.c_str(), "r" ) ) )
291 return fopen(name.c_str(), "r" ); // trying current directory
292 else
293 return ret;
294}
295
296FILE *open_builtins() {
297 FILE *ret;
298
299 const char *name = "builtins.cf";
300 const char *full_name = CFA_LIBDIR "/builtins.cf";
301
302 if ( beVerbose ) {
303 cout << "Reading from " << full_name << endl;
304 }
305
306 if (! (ret = fopen(full_name, "r" ) ) )
307 return fopen(name, "r" ); // trying current directory
308 else
309 return ret;
310}
311
312// Local Variables: //
313// compile-command: "make" //
314// End: //
Note: See TracBrowser for help on using the repository browser.