source: translator/main.cc @ 5c7fb6c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 5c7fb6c was b1a6d6b, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

removed duplicate adapters, switch to c99 for initializer declarations

  • Property mode set to 100644
File size: 9.7 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 "InitTweak/RemoveInit.h"
28//#include "Explain/GenProlog.h"
29//#include "Try/Visit.h"
30
31#include "SemanticError.h"
32#include "UnimplementedError.h"
33#include "utility.h"
34
35#include "../config.h"
36
37using namespace std;
38
39extern "C"{
40#include <unistd.h>
41} // extern
42
43FILE *open_prelude();
44FILE *open_builtins();
45bool beVerbose = false;
46bool resolveVerbose = false;
47
48int main( int argc, char *argv[] ) {
49    bool debugp = false, treep = false, astp = false, manglep = false, symtabp = false, validp = false;
50    bool preludep = true, protop = false, libp = false;
51    bool exprp = false, codegenp = false, errorp = false;
52    int c;
53    FILE *input, *prelude, *builtins;
54    std::ostream *output = &std::cout;
55
56    opterr = 0;
57
58    std::list< Declaration* > translationUnit;
59   
60    while ( (c = getopt( argc, argv, "dtsgmvxcenprlDyz:" )) != -1 ) {
61        switch (c) {
62          case 'd':
63            // bison debugging info
64            debugp = true;
65            break;
66          case 't':
67            // dump parse tree
68            treep = true;
69            break;
70          case 's':
71            // dump AST
72            astp = true;
73            break;
74          case 'g':
75            // print alternatives for expressions
76            manglep = true;
77            break;
78          case 'm':
79            // print symbol table events
80            symtabp = true;
81            break;
82          case 'r':
83            // print resolver steps
84            resolveVerbose = true;
85            break;
86          case 'x':
87            // dump AST after decl validation pass
88            validp = true;
89            break;
90          case 'e':
91            // dump AST after expression analysis
92            exprp = true;
93            break;
94          case 'z':
95            codegenp = true;
96            break;
97          case 'y':
98            errorp = true;
99            break;
100          case 'n':
101            // don't read preamble
102            preludep = false;
103            break;
104          case 'p':
105            // generate prototypes for preamble functions
106            protop = true;
107            break;
108          case 'l':
109            // generate libcfa.c
110            libp = true;
111            break;
112          case 'v':
113            // verbose
114            beVerbose = true;
115            break;
116          case 'D':
117            // ignore -Dxxx
118            break;
119          case '?':
120            cout << "Unknown option: '" << (char)optopt << "'" << endl;
121            exit(1);
122          default:
123            abort();
124        } // switch
125    } // while
126
127    try {
128        if ( optind < argc ) {
129            input = fopen( argv[ optind ], "r" );
130            if ( ! input ) {
131                std::cout << "Error: can't open " << argv[optind] << std::endl;
132                exit( 1 );
133            } // if
134            optind++;
135        } else {
136            input = stdin;
137        } // if
138
139        if ( optind < argc ) {
140            output = new ofstream( argv[ optind ] );
141        } // if
142   
143        Parser::get_parser().set_debug( debugp );
144   
145        if ( preludep ) {
146            // include gcc builtins
147            builtins = open_builtins();
148            if ( !builtins ) {
149                std::cout << "Error: can't open builtins" << std::endl;
150                exit( 1 );
151            } // if
152     
153            Parser::get_parser().set_linkage( LinkageSpec::Compiler );
154            Parser::get_parser().parse( builtins );
155   
156            if ( Parser::get_parser().get_parseStatus() != 0 ) {
157                return Parser::get_parser().get_parseStatus();
158            } // if
159            fclose( builtins );
160
161            // include cfa prelude
162            if ( libp ) {
163                prelude = input;
164            } else {
165                prelude = open_prelude();
166            } // if
167            if ( !prelude ) {
168                std::cout << "Error: can't open prelude" << std::endl;
169                exit( 1 );
170            } // if
171     
172            Parser::get_parser().set_linkage( LinkageSpec::Intrinsic );
173            Parser::get_parser().parse( prelude );
174   
175            if ( Parser::get_parser().get_parseStatus() != 0 ) {
176                return Parser::get_parser().get_parseStatus();
177            } // if
178            fclose( prelude );
179        } // if
180   
181        if ( libp ) {
182            std::list< Declaration* > translationUnit;
183            buildList( Parser::get_parser().get_parseTree(), translationUnit );
184            Parser::get_parser().freeTree();
185            SymTab::validate( translationUnit, false );
186            CodeGen::fixNames( translationUnit );
187            LibCfa::makeLibCfa( translationUnit );
188            ResolvExpr::resolve( translationUnit );
189            GenPoly::convertLvalue( translationUnit );
190            GenPoly::box( translationUnit );
191            CodeGen::generate( translationUnit, *output, true );
192            if ( output != &std::cout ) {
193                delete output;
194            } // if
195            return 0;
196        } // if
197   
198        Parser::get_parser().set_linkage( LinkageSpec::Cforall );
199 
200        Parser::get_parser().parse( input );
201        if ( debugp || Parser::get_parser().get_parseStatus() != 0 ) {
202            return Parser::get_parser().get_parseStatus();
203        } // if
204        fclose( input );
205 
206        if ( treep ) {
207            Parser::get_parser().get_parseTree()->printList( std::cout );
208            Parser::get_parser().freeTree();
209            return 0;
210        } // if
211
212        buildList( Parser::get_parser().get_parseTree(), translationUnit );
213
214        Parser::get_parser().freeTree();
215        if ( astp ) {
216            printAll( translationUnit, std::cout );
217            return 0;
218        } // if
219
220        if ( manglep ) {
221            SymTab::validate( translationUnit, false );
222            ResolvExpr::AlternativePrinter printer( std::cout );
223            acceptAll( translationUnit, printer );
224            return 0;
225        } // if
226
227        if ( symtabp ) {
228            SymTab::validate( translationUnit, true );
229            return 0;
230        } // if
231
232        if ( validp ) {
233            SymTab::validate( translationUnit, false );
234            printAll( translationUnit, std::cout );
235            return 0;
236        } // if
237
238        if ( exprp ) {
239            InitTweak::tweak( translationUnit );
240            SymTab::validate( translationUnit, false );
241            ControlStruct::mutate( translationUnit );
242            CodeGen::fixNames( translationUnit );
243            ResolvExpr::resolve( translationUnit );
244            printAll( translationUnit, std::cout );
245            return 0;
246        } // if
247
248        if ( codegenp ) {
249            // print the tree right before code generation
250            cerr << "tweak" << endl;
251            InitTweak::tweak( translationUnit );
252            cerr << "validate" << endl;
253            SymTab::validate( translationUnit, false );
254            cerr << "mutate" << endl;
255            ControlStruct::mutate( translationUnit );
256            cerr << "fixNames" << endl;
257            CodeGen::fixNames( translationUnit );
258            cerr << "resolve" << endl;
259            ResolvExpr::resolve( translationUnit );
260            cerr << "copyParams" << endl;
261            GenPoly::copyParams( translationUnit );
262            cerr << "convertSpecializations" << endl;
263            GenPoly::convertSpecializations( translationUnit );
264            cerr << "convertLvalue" << endl;
265            GenPoly::convertLvalue( translationUnit );
266            cerr << "box" << endl;
267            GenPoly::box( translationUnit );
268            if ( errorp ) {
269                printAll( translationUnit, std::cout );
270            }
271            return 0;
272        } // if
273
274        // add the assignment statement after the
275        // initialization of a type parameter
276        InitTweak::tweak( translationUnit );
277
278        //std::cerr << "before validate" << std::endl;
279        SymTab::validate( translationUnit, false );
280        //Try::visit( translationUnit );
281        //Tuples::mutate( translationUnit );
282        //InitTweak::mutate( translationUnit );
283        //std::cerr << "before mutate" << std::endl;
284        ControlStruct::mutate( translationUnit );
285        //std::cerr << "before fixNames" << std::endl;
286        CodeGen::fixNames( translationUnit );
287        //std::cerr << "before resolve" << std::endl;
288        ResolvExpr::resolve( translationUnit );
289        //Tuples::checkFunctions( translationUnit );
290        //      std::cerr << "Finished tuple checkfunctions" << std::endl;
291        //printAll( translationUnit, std::cerr );
292        //std::cerr << "before copyParams" << std::endl;
293        GenPoly::copyParams( translationUnit );
294        //std::cerr << "before convertSpecializations" << std::endl;
295        GenPoly::convertSpecializations( translationUnit );
296        //std::cerr << "before convertLvalue" << std::endl;
297        GenPoly::convertLvalue( translationUnit );
298        //std::cerr << "before box" << std::endl;
299        GenPoly::box( translationUnit );
300        //Tuples::mutate( translationUnit );
301
302        CodeGen::generate( translationUnit, *output, protop );
303
304        if ( output != &std::cout ) {
305            delete output;
306        } // if
307
308    } catch ( SemanticError &e ) {
309        if ( errorp ) {
310           printAll( translationUnit, std::cout );
311        }
312        e.print( cout );
313        if ( output != &std::cout ) {
314            delete output;
315        } // if
316        return 1;
317    } catch ( UnimplementedError &e ) {
318        std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
319        if ( output != &std::cout ) {
320            delete output;
321        } // if
322        return 1;
323    } catch ( CompilerError &e ) {
324        std::cerr << "Compiler Error: " << e.get_what() << std::endl;
325        std::cerr << "(please report bugs to " << std::endl;
326        if ( output != &std::cout ) {
327            delete output;
328        } // if
329        return 1;
330    } // try
331
332    return 0;
333} // main
334
335FILE *open_prelude() {
336    FILE *ret;
337
338    const string name("prelude.cf"),
339        full_name = string(CFA_LIBDIR) + "/" + name;
340
341    if ( beVerbose ) {
342        cout << "Reading from " << full_name << endl;
343    } // if
344
345    if ( ! (ret = fopen(full_name.c_str(), "r" ) ) )
346        return fopen(name.c_str(), "r" );             // trying current directory
347    else
348        return ret;
349} // open_prelude
350
351FILE *open_builtins() {
352    FILE *ret;
353
354    const char *name = "builtins.cf";
355    const char *full_name = CFA_LIBDIR "/builtins.cf";
356
357    if ( beVerbose ) {
358        cout << "Reading from " << full_name << endl;
359    } // if
360
361    if ( ! (ret = fopen(full_name, "r" ) ) )
362        return fopen(name, "r" );                       // trying current directory
363    else
364        return ret;
365} // open_builtins
366
367// Local Variables: //
368// compile-command: "make" //
369// End:  //
Note: See TracBrowser for help on using the repository browser.