source: translator/main.cc @ 2c2242c

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 2c2242c was d9a0e76, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

remove Parser.old, add -XCFA to driver, copy ptrdiff_t from stddef.h in preclude, remove casts from initialization constants, adjust formatting

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