source: translator/main.cc @ 3848e0e

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

underscore changes, ptrdiff_t changes, formating, _Bool prelude

  • Property mode set to 100644
File size: 7.9 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        std::cerr << "before validate" << std::endl;
235        SymTab::validate( translationUnit, false );
236        //Try::visit( translationUnit );
237        //Tuples::mutate( translationUnit );
238        //InitTweak::mutate( translationUnit );
239        std::cerr << "before mutate" << std::endl;
240        ControlStruct::mutate( translationUnit );
241        std::cerr << "before fixNames" << std::endl;
242        CodeGen::fixNames( translationUnit );
243        std::cerr << "before resolve" << std::endl;
244        ResolvExpr::resolve( translationUnit );
245        //Tuples::checkFunctions( translationUnit );
246        //      std::cerr << "Finished tuple checkfunctions" << std::endl;
247        //printAll( translationUnit, std::cerr );
248        std::cerr << "before copyParams" << std::endl;
249        GenPoly::copyParams( translationUnit );
250        std::cerr << "before convertSpecializations" << std::endl;
251        GenPoly::convertSpecializations( translationUnit );
252        std::cerr << "before convertLvalue" << std::endl;
253        GenPoly::convertLvalue( translationUnit );
254        std::cerr << "before box" << std::endl;
255        GenPoly::box( translationUnit );
256        //Tuples::mutate( translationUnit );
257
258        CodeGen::generate( translationUnit, *output, protop );
259
260        if ( output != &std::cout ) {
261            delete output;
262        }
263
264    } catch ( SemanticError &e ) {
265        e.print( cout );
266        if ( output != &std::cout ) {
267            delete output;
268        }
269        return 1;
270    } catch ( UnimplementedError &e ) {
271        std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
272        if ( output != &std::cout ) {
273            delete output;
274        }
275        return 1;
276    } catch ( CompilerError &e ) {
277        std::cerr << "Compiler Error: " << e.get_what() << std::endl;
278        std::cerr << "(please report bugs to " << std::endl;
279        if ( output != &std::cout ) {
280            delete output;
281        }
282        return 1;
283    }
284
285    return 0;
286}
287
288FILE *open_prelude() {
289    FILE *ret;
290
291    const string name("prelude.cf"),
292        full_name = string(CFA_LIBDIR) + "/" + name;
293
294    if ( beVerbose ) {
295        cout << "Reading from " << full_name << endl;
296    }
297
298    if (! (ret = fopen(full_name.c_str(), "r" ) ) )
299        return fopen(name.c_str(), "r" );             // trying current directory
300    else
301        return ret;
302}
303
304FILE *open_builtins() {
305    FILE *ret;
306
307    const char *name = "builtins.cf";
308    const char *full_name = CFA_LIBDIR "/builtins.cf";
309
310    if ( beVerbose ) {
311        cout << "Reading from " << full_name << endl;
312    }
313
314    if (! (ret = fopen(full_name, "r" ) ) )
315        return fopen(name, "r" );                       // trying current directory
316    else
317        return ret;
318}
319
320// Local Variables: //
321// compile-command: "make" //
322// End:  //
Note: See TracBrowser for help on using the repository browser.