source: src/main.cc @ 4a7d895

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4a7d895 was 8b7ee09, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

rename type LinkageSpec::Type to LinkageSpec::Spec, which affects many files

  • Property mode set to 100644
File size: 10.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//
[71f4e4f]7// main.cc --
[b87a5ed]8//
[843054c2]9// Author           : Richard C. Bilson
[b87a5ed]10// Created On       : Fri May 15 23:12:02 2015
[dd51906]11// Last Modified By : Peter A. Buhr
[8b7ee09]12// Last Modified On : Fri Aug 19 08:31:22 2016
13// Update Count     : 350
[b87a5ed]14//
15
[51b7345]16#include <iostream>
17#include <fstream>
[b87a5ed]18#include <getopt.h>
[0da3e2c]19#include "Parser/lex.h"
20#include "Parser/parser.h"
21#include "Parser/TypedefTable.h"
[51b7345]22#include "GenPoly/Lvalue.h"
23#include "GenPoly/Specialize.h"
24#include "GenPoly/Box.h"
25#include "GenPoly/CopyParams.h"
[ea5daeb]26#include "GenPoly/InstantiateGeneric.h"
[51b7345]27#include "CodeGen/Generate.h"
28#include "CodeGen/FixNames.h"
29#include "ControlStruct/Mutate.h"
30#include "SymTab/Validate.h"
31#include "ResolvExpr/AlternativePrinter.h"
32#include "ResolvExpr/Resolver.h"
33#include "MakeLibCfa.h"
[a0fdbd5]34#include "InitTweak/GenInit.h"
[71f4e4f]35#include "InitTweak/FixInit.h"
[d3b7937]36#include "Common/UnimplementedError.h"
[51b7345]37
38#include "../config.h"
39
40using namespace std;
41
[0da3e2c]42#define OPTPRINT(x) if ( errorp ) std::cerr << x << std::endl;
[81419b5]43
[0da3e2c]44
[8b7ee09]45LinkageSpec::Spec linkage = LinkageSpec::Cforall;
[0da3e2c]46TypedefTable typedefTable;
[cbaee0d]47DeclarationNode * parseTree = nullptr;                                  // program parse tree
[81419b5]48
[926af74]49extern int yydebug;                                                                             // set for -g flag (Grammar)
[b87a5ed]50bool
51        astp = false,
[de62360d]52        bresolvep = false,
[fea7ca7]53        bboxp = false,
[ca1c11f]54        ctorinitp = false,
[b87a5ed]55        exprp = false,
56        expraltp = false,
57        libcfap = false,
[de62360d]58        nopreludep = false,
[1ab4ce2]59        noprotop = false,
[de62360d]60        parsep = false,
[b87a5ed]61        resolvep = false,                                                                       // used in AlternativeFinder
62        symtabp = false,
[d3b7937]63        treep = false,
[b87a5ed]64        validp = false,
[de62360d]65        errorp = false,
66        codegenp = false;
[b87a5ed]67
[926af74]68static void parse_cmdline( int argc, char *argv[], const char *& filename );
[8b7ee09]69static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
[0da3e2c]70static void dump( std::list< Declaration * > & translationUnit, std::ostream & out = std::cout );
71
[cbaee0d]72int main( int argc, char * argv[] ) {
[3b8e52c]73        FILE * input;                                                                           // use FILE rather than istream because yyin is FILE
[cbaee0d]74        std::ostream *output = & std::cout;
[7b937575]75        std::list< Declaration * > translationUnit;
[926af74]76        const char *filename = nullptr;
[b87a5ed]77
[0da3e2c]78        parse_cmdline( argc, argv, filename );                          // process command-line arguments
[b87a5ed]79
80        try {
[81419b5]81                // choose to read the program from a file or stdin
[3b8e52c]82                if ( optind < argc ) {                                                  // any commands after the flags ? => input file name
[b87a5ed]83                        input = fopen( argv[ optind ], "r" );
[3b8e52c]84                        assertf( input, "cannot open %s\n", argv[ optind ] );
[d029162]85                        // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
[926af74]86                        if ( filename == nullptr ) filename = argv[ optind ];
[37024fd]87                        // prelude filename comes in differently
88                        if ( libcfap ) filename = "prelude.cf";
[b87a5ed]89                        optind += 1;
[3b8e52c]90                } else {                                                                                // no input file name
[b87a5ed]91                        input = stdin;
[2a7e29b]92                        // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
93                        // a fake name along
[926af74]94                        if ( filename == nullptr ) filename = "stdin";
[b87a5ed]95                } // if
96
[3b8e52c]97                if ( optind < argc ) {                                                  // any commands after the flags and input file ? => output file name
[b87a5ed]98                        output = new ofstream( argv[ optind ] );
99                } // if
[71f4e4f]100
[159c62e]101                // read in the builtins, extras, and the prelude
[de62360d]102                if ( ! nopreludep ) {                                                   // include gcc builtins
[faf8857]103                        // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
[d3b7937]104                        FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
[3b8e52c]105                        assertf( builtins, "cannot open builtins.cf\n" );
[81419b5]106                        parse( builtins, LinkageSpec::Compiler );
107
[159c62e]108                        // read the extra prelude in, if not generating the cfa library
109                        FILE * extras = fopen( libcfap | treep ? "extras.cf" : CFA_LIBDIR "/extras.cf", "r" );
[3b8e52c]110                        assertf( extras, "cannot open extras.cf\n" );
[159c62e]111                        parse( extras, LinkageSpec::C );
112
[81419b5]113                        if ( ! libcfap ) {
[faf8857]114                                // read the prelude in, if not generating the cfa library
[d3b7937]115                                FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
[3b8e52c]116                                assertf( prelude, "cannot open prelude.cf\n" );
[35304009]117                                parse( prelude, LinkageSpec::Intrinsic );
[b87a5ed]118                        } // if
119                } // if
[81419b5]120
[926af74]121                parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, yydebug );
[71f4e4f]122
[b87a5ed]123                if ( parsep ) {
[0da3e2c]124                        parseTree->printList( std::cout );
125                        delete parseTree;
[b87a5ed]126                        return 0;
127                } // if
128
[0da3e2c]129                buildList( parseTree, translationUnit );
130                delete parseTree;
[cbaee0d]131                parseTree = nullptr;
[b87a5ed]132
133                if ( astp ) {
[1ab4ce2]134                        dump( translationUnit );
[b87a5ed]135                        return 0;
136                } // if
137
[839ccbb]138                // add the assignment statement after the initialization of a type parameter
[81419b5]139                OPTPRINT( "validate" )
140                SymTab::validate( translationUnit, symtabp );
141                if ( symtabp ) {
[b87a5ed]142                        return 0;
143                } // if
144
[81419b5]145                if ( expraltp ) {
146                        ResolvExpr::AlternativePrinter printer( std::cout );
147                        acceptAll( translationUnit, printer );
[b87a5ed]148                        return 0;
149                } // if
150
151                if ( validp ) {
[1ab4ce2]152                        dump( translationUnit );
[b87a5ed]153                        return 0;
154                } // if
155
[81419b5]156                OPTPRINT( "mutate" )
157                ControlStruct::mutate( translationUnit );
[71f4e4f]158                OPTPRINT( "fixNames" )
[81419b5]159                CodeGen::fixNames( translationUnit );
[f1e012b]160                OPTPRINT( "tweakInit" )
[a0fdbd5]161                InitTweak::genInit( translationUnit );
[b87a5ed]162
[81419b5]163                if ( libcfap ) {
164                        // generate the bodies of cfa library functions
165                        LibCfa::makeLibCfa( translationUnit );
[b87a5ed]166                } // if
167
[de62360d]168                if ( bresolvep ) {
[1ab4ce2]169                        dump( translationUnit );
[de62360d]170                        return 0;
171                } // if
172
[81419b5]173                OPTPRINT( "resolve" )
[b87a5ed]174                ResolvExpr::resolve( translationUnit );
[81419b5]175                if ( exprp ) {
[1ab4ce2]176                        dump( translationUnit );
[ca1c11f]177                        return 0;
[926af74]178                } // if
[81419b5]179
[71f4e4f]180                // fix ObjectDecl - replaces ConstructorInit nodes
[6cf27a07]181                OPTPRINT( "fixInit" )
182                InitTweak::fix( translationUnit, filename, libcfap || treep );
[ca1c11f]183                if ( ctorinitp ) {
184                        dump ( translationUnit );
185                        return 0;
[926af74]186                } // if
[71f4e4f]187
[ea5daeb]188                OPTPRINT("instantiateGenerics")
189                GenPoly::instantiateGeneric( translationUnit );
[81419b5]190                OPTPRINT( "copyParams" );
[b87a5ed]191                GenPoly::copyParams( translationUnit );
[81419b5]192                OPTPRINT( "convertSpecializations" )
[698664b3]193                GenPoly::convertSpecializations( translationUnit );
[81419b5]194                OPTPRINT( "convertLvalue" )
[b87a5ed]195                GenPoly::convertLvalue( translationUnit );
[fea7ca7]196
197                if ( bboxp ) {
198                        dump( translationUnit );
199                        return 0;
[926af74]200                } // if
[81419b5]201                OPTPRINT( "box" )
[b87a5ed]202                GenPoly::box( translationUnit );
[81419b5]203
[2871210]204                // print tree right before code generation
[81419b5]205                if ( codegenp ) {
[1ab4ce2]206                        dump( translationUnit );
[81419b5]207                        return 0;
208                } // if
[b87a5ed]209
[1ab4ce2]210                CodeGen::generate( translationUnit, *output, ! noprotop );
[b87a5ed]211
212                if ( output != &std::cout ) {
213                        delete output;
214                } // if
215        } catch ( SemanticError &e ) {
216                if ( errorp ) {
[f77f12e2]217                        std::cerr << "---AST at error:---" << std::endl;
218                        dump( translationUnit, std::cerr );
219                        std::cerr << std::endl << "---End of AST, begin error message:---\n" << std::endl;
[926af74]220                } // if
[81419b5]221                e.print( std::cerr );
[b87a5ed]222                if ( output != &std::cout ) {
223                        delete output;
224                } // if
225                return 1;
226        } catch ( UnimplementedError &e ) {
227                std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
228                if ( output != &std::cout ) {
229                        delete output;
230                } // if
231                return 1;
232        } catch ( CompilerError &e ) {
233                std::cerr << "Compiler Error: " << e.get_what() << std::endl;
234                std::cerr << "(please report bugs to " << std::endl;
235                if ( output != &std::cout ) {
236                        delete output;
237                } // if
238                return 1;
239        } // try
240
[39786813]241        deleteAll( translationUnit );
[b87a5ed]242        return 0;
[d9a0e76]243} // main
[51b7345]244
[cbaee0d]245void parse_cmdline( int argc, char * argv[], const char *& filename ) {
[0da3e2c]246        enum { Ast, Bbox, Bresolver, CtorInitFix, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, Validate, };
247
248        static struct option long_opts[] = {
249                { "ast", no_argument, 0, Ast },
250                { "before-box", no_argument, 0, Bbox },
251                { "before-resolver", no_argument, 0, Bresolver },
252                { "ctorinitfix", no_argument, 0, CtorInitFix },
253                { "expr", no_argument, 0, Expr },
254                { "expralt", no_argument, 0, ExprAlt },
255                { "grammar", no_argument, 0, Grammar },
256                { "libcfa", no_argument, 0, LibCFA },
257                { "no-preamble", no_argument, 0, Nopreamble },
258                { "parse", no_argument, 0, Parse },
259                { "no-prototypes", no_argument, 0, Prototypes },
260                { "resolver", no_argument, 0, Resolver },
261                { "symbol", no_argument, 0, Symbol },
262                { "tree", no_argument, 0, Tree },
263                { "validate", no_argument, 0, Validate },
264                { 0, 0, 0, 0 }
265        }; // long_opts
266        int long_index;
267
268        opterr = 0;                                                                                     // (global) prevent getopt from printing error messages
269
270        int c;
271        while ( (c = getopt_long( argc, argv, "abBcefglnpqrstvyzD:F:", long_opts, &long_index )) != -1 ) {
272                switch ( c ) {
273                  case Ast:
274                  case 'a':                                                                             // dump AST
275                        astp = true;
276                        break;
277                  case Bresolver:
278                  case 'b':                                                                             // print before resolver steps
279                        bresolvep = true;
280                        break;
281                  case 'B':                                                                             // print before resolver steps
282                        bboxp = true;
283                        break;
284                  case CtorInitFix:
285                  case 'c':
286                        ctorinitp = true;
287                        break;
288                  case Expr:
289                  case 'e':                                                                             // dump AST after expression analysis
290                        exprp = true;
291                        break;
292                  case ExprAlt:
293                  case 'f':                                                                             // print alternatives for expressions
294                        expraltp = true;
295                        break;
296                  case Grammar:
297                  case 'g':                                                                             // bison debugging info (grammar rules)
[926af74]298                        yydebug = true;
[0da3e2c]299                        break;
300                  case LibCFA:
301                  case 'l':                                                                             // generate libcfa.c
302                        libcfap = true;
303                        break;
304                  case Nopreamble:
305                  case 'n':                                                                             // do not read preamble
306                        nopreludep = true;
307                        break;
308                  case Prototypes:
309                  case 'p':                                                                             // generate prototypes for preamble functions
310                        noprotop = true;
311                        break;
312                  case Parse:
313                  case 'q':                                                                             // dump parse tree
314                        parsep = true;
315                        break;
316                  case Resolver:
317                  case 'r':                                                                             // print resolver steps
318                        resolvep = true;
319                        break;
320                  case Symbol:
321                  case 's':                                                                             // print symbol table events
322                        symtabp = true;
323                        break;
324                  case Tree:
325                  case 't':                                                                             // build in tree
326                        treep = true;
327                        break;
328                  case 'v':                                                                             // dump AST after decl validation pass
329                        validp = true;
330                        break;
331                  case 'y':
332                        errorp = true;
333                        break;
334                  case 'z':
335                        codegenp = true;
336                        break;
337                  case 'D':                                                                             // ignore -Dxxx
338                        break;
339                  case 'F':                                                                             // source file-name without suffix
340                        filename = optarg;
341                        break;
342                  case '?':
[3b8e52c]343                        assertf( false, "Unknown option: '%c'\n", (char)optopt );
[0da3e2c]344                  default:
345                        abort();
346                } // switch
347        } // while
348} // parse_cmdline
349
[8b7ee09]350static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit ) {
[0da3e2c]351        extern int yyparse( void );
[cbaee0d]352        extern FILE * yyin;
[0da3e2c]353        extern int yylineno;
354
[8b7ee09]355        ::linkage = linkage;                                                            // set globals
[0da3e2c]356        yyin = input;
357        yylineno = 1;
358        typedefTable.enterScope();
359        int parseStatus = yyparse();
[81419b5]360
361        fclose( input );
[0da3e2c]362        if ( shouldExit || parseStatus != 0 ) {
363                exit( parseStatus );
[81419b5]364        } // if
[0da3e2c]365} // parse
[81419b5]366
[1ab4ce2]367static bool notPrelude( Declaration * decl ) {
368        return ! LinkageSpec::isBuiltin( decl->get_linkage() );
[0da3e2c]369} // notPrelude
[1ab4ce2]370
[f77f12e2]371static void dump( std::list< Declaration * > & translationUnit, std::ostream & out ) {
[1ab4ce2]372        std::list< Declaration * > decls;
[926af74]373
[1ab4ce2]374        if ( noprotop ) {
[7880579]375                filter( translationUnit.begin(), translationUnit.end(), std::back_inserter( decls ), notPrelude );
[1ab4ce2]376        } else {
377                decls = translationUnit;
[926af74]378        } // if
[1ab4ce2]379
[f77f12e2]380        printAll( decls, out );
[7f5566b]381        deleteAll( translationUnit );
[0da3e2c]382} // dump
[1ab4ce2]383
[51b7345]384// Local Variables: //
[b87a5ed]385// tab-width: 4 //
386// mode: c++ //
387// compile-command: "make install" //
[51b7345]388// End:  //
Note: See TracBrowser for help on using the repository browser.