| 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 | //
|
|---|
| 7 | // main.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Richard C. Bilson
|
|---|
| 10 | // Created On : Fri May 15 23:12:02 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Mon Jun 22 17:02:11 2015
|
|---|
| 13 | // Update Count : 73
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <iostream>
|
|---|
| 17 | #include <fstream>
|
|---|
| 18 | #include <cstdlib>
|
|---|
| 19 | #include <cstdio>
|
|---|
| 20 | #include <getopt.h>
|
|---|
| 21 | #include "Parser/Parser.h"
|
|---|
| 22 | #include "Parser/ParseNode.h"
|
|---|
| 23 | #include "Parser/LinkageSpec.h"
|
|---|
| 24 | #include "SynTree/Declaration.h"
|
|---|
| 25 | #include "SynTree/Visitor.h"
|
|---|
| 26 | #include "GenPoly/Lvalue.h"
|
|---|
| 27 | #include "GenPoly/Specialize.h"
|
|---|
| 28 | #include "GenPoly/Box.h"
|
|---|
| 29 | #include "GenPoly/CopyParams.h"
|
|---|
| 30 | #include "CodeGen/Generate.h"
|
|---|
| 31 | #include "CodeGen/FixNames.h"
|
|---|
| 32 | #include "ControlStruct/Mutate.h"
|
|---|
| 33 | #include "Tuples/Mutate.h"
|
|---|
| 34 | #include "Tuples/FunctionChecker.h"
|
|---|
| 35 | #include "SymTab/Mangler.h"
|
|---|
| 36 | #include "SymTab/Indexer.h"
|
|---|
| 37 | #include "SymTab/Validate.h"
|
|---|
| 38 | #include "ResolvExpr/AlternativePrinter.h"
|
|---|
| 39 | #include "ResolvExpr/Resolver.h"
|
|---|
| 40 | #include "MakeLibCfa.h"
|
|---|
| 41 | #include "InitTweak/Mutate.h"
|
|---|
| 42 | #include "InitTweak/RemoveInit.h"
|
|---|
| 43 | //#include "Explain/GenProlog.h"
|
|---|
| 44 | //#include "Try/Visit.h"
|
|---|
| 45 |
|
|---|
| 46 | #include "SemanticError.h"
|
|---|
| 47 | #include "UnimplementedError.h"
|
|---|
| 48 |
|
|---|
| 49 | #include "../config.h"
|
|---|
| 50 |
|
|---|
| 51 | using namespace std;
|
|---|
| 52 |
|
|---|
| 53 | #define OPTPRINT(x) \
|
|---|
| 54 | if ( errorp ) std::cerr << x << std::endl;
|
|---|
| 55 |
|
|---|
| 56 | void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
|
|---|
| 57 |
|
|---|
| 58 | bool
|
|---|
| 59 | astp = false,
|
|---|
| 60 | bresolvep = false,
|
|---|
| 61 | exprp = false,
|
|---|
| 62 | expraltp = false,
|
|---|
| 63 | grammarp = false,
|
|---|
| 64 | libcfap = false,
|
|---|
| 65 | nopreludep = false,
|
|---|
| 66 | protop = false,
|
|---|
| 67 | parsep = false,
|
|---|
| 68 | resolvep = false, // used in AlternativeFinder
|
|---|
| 69 | symtabp = false,
|
|---|
| 70 | validp = false,
|
|---|
| 71 | errorp = false,
|
|---|
| 72 | codegenp = false;
|
|---|
| 73 |
|
|---|
| 74 | enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, };
|
|---|
| 75 |
|
|---|
| 76 | static struct option long_opts[] = {
|
|---|
| 77 | { "ast", no_argument, 0, Ast },
|
|---|
| 78 | { "before-resolver", no_argument, 0, Bresolver },
|
|---|
| 79 | { "expr", no_argument, 0, Expr },
|
|---|
| 80 | { "expralt", no_argument, 0, ExprAlt },
|
|---|
| 81 | { "grammar", no_argument, 0, Grammar },
|
|---|
| 82 | { "libcfa", no_argument, 0, LibCFA },
|
|---|
| 83 | { "nopreamble", no_argument, 0, Nopreamble },
|
|---|
| 84 | { "parse", no_argument, 0, Parse },
|
|---|
| 85 | { "prototypes", no_argument, 0, Prototypes },
|
|---|
| 86 | { "resolver", no_argument, 0, Resolver },
|
|---|
| 87 | { "symbol", no_argument, 0, Symbol },
|
|---|
| 88 | { "validate", no_argument, 0, Validate },
|
|---|
| 89 | { 0, 0, 0, 0 }
|
|---|
| 90 | };
|
|---|
| 91 |
|
|---|
| 92 | int main( int argc, char *argv[] ) {
|
|---|
| 93 | FILE *input;
|
|---|
| 94 | std::ostream *output = &std::cout;
|
|---|
| 95 | int long_index;
|
|---|
| 96 | std::list< Declaration* > translationUnit;
|
|---|
| 97 |
|
|---|
| 98 | opterr = 0; // prevent getopt from printing error messages
|
|---|
| 99 |
|
|---|
| 100 | int c;
|
|---|
| 101 | while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) {
|
|---|
| 102 | switch ( c ) {
|
|---|
| 103 | case Ast:
|
|---|
| 104 | case 'a': // dump AST
|
|---|
| 105 | astp = true;
|
|---|
| 106 | break;
|
|---|
| 107 | case Bresolver:
|
|---|
| 108 | case 'b': // print before resolver steps
|
|---|
| 109 | bresolvep = true;
|
|---|
| 110 | break;
|
|---|
| 111 | case Expr:
|
|---|
| 112 | case 'e': // dump AST after expression analysis
|
|---|
| 113 | exprp = true;
|
|---|
| 114 | break;
|
|---|
| 115 | case ExprAlt:
|
|---|
| 116 | case 'f': // print alternatives for expressions
|
|---|
| 117 | expraltp = true;
|
|---|
| 118 | break;
|
|---|
| 119 | case Grammar:
|
|---|
| 120 | case 'g': // bison debugging info (grammar rules)
|
|---|
| 121 | grammarp = true;
|
|---|
| 122 | break;
|
|---|
| 123 | case LibCFA:
|
|---|
| 124 | case 'l': // generate libcfa.c
|
|---|
| 125 | libcfap = true;
|
|---|
| 126 | break;
|
|---|
| 127 | case Nopreamble:
|
|---|
| 128 | case 'n': // do not read preamble
|
|---|
| 129 | nopreludep = true;
|
|---|
| 130 | break;
|
|---|
| 131 | case Prototypes:
|
|---|
| 132 | case 'p': // generate prototypes for preamble functions
|
|---|
| 133 | protop = true;
|
|---|
| 134 | break;
|
|---|
| 135 | case Parse:
|
|---|
| 136 | case 'q': // dump parse tree
|
|---|
| 137 | parsep = true;
|
|---|
| 138 | break;
|
|---|
| 139 | case Resolver:
|
|---|
| 140 | case 'r': // print resolver steps
|
|---|
| 141 | resolvep = true;
|
|---|
| 142 | break;
|
|---|
| 143 | case Symbol:
|
|---|
| 144 | case 's': // print symbol table events
|
|---|
| 145 | symtabp = true;
|
|---|
| 146 | break;
|
|---|
| 147 | case 'v': // dump AST after decl validation pass
|
|---|
| 148 | validp = true;
|
|---|
| 149 | break;
|
|---|
| 150 | case 'y':
|
|---|
| 151 | errorp = true;
|
|---|
| 152 | break;
|
|---|
| 153 | case 'z':
|
|---|
| 154 | codegenp = true;
|
|---|
| 155 | break;
|
|---|
| 156 | case 'D': // ignore -Dxxx
|
|---|
| 157 | break;
|
|---|
| 158 | case '?':
|
|---|
| 159 | cout << "Unknown option: '" << (char)optopt << "'" << endl;
|
|---|
| 160 | exit(1);
|
|---|
| 161 | default:
|
|---|
| 162 | abort();
|
|---|
| 163 | } // switch
|
|---|
| 164 | } // while
|
|---|
| 165 |
|
|---|
| 166 | try {
|
|---|
| 167 | // choose to read the program from a file or stdin
|
|---|
| 168 | if ( optind < argc ) {
|
|---|
| 169 | input = fopen( argv[ optind ], "r" );
|
|---|
| 170 | if ( ! input ) {
|
|---|
| 171 | std::cout << "Error: can't open " << argv[optind] << std::endl;
|
|---|
| 172 | exit( 1 );
|
|---|
| 173 | } // if
|
|---|
| 174 | optind += 1;
|
|---|
| 175 | } else {
|
|---|
| 176 | input = stdin;
|
|---|
| 177 | } // if
|
|---|
| 178 |
|
|---|
| 179 | if ( optind < argc ) {
|
|---|
| 180 | output = new ofstream( argv[ optind ] );
|
|---|
| 181 | } // if
|
|---|
| 182 |
|
|---|
| 183 | Parser::get_parser().set_debug( grammarp );
|
|---|
| 184 |
|
|---|
| 185 | // read in the builtins and the prelude
|
|---|
| 186 | if ( ! nopreludep ) { // include gcc builtins
|
|---|
| 187 | FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" );
|
|---|
| 188 | if ( builtins == NULL ) {
|
|---|
| 189 | std::cerr << "Error: can't open builtins" << std::endl;
|
|---|
| 190 | exit( 1 );
|
|---|
| 191 | } // if
|
|---|
| 192 |
|
|---|
| 193 | parse( builtins, LinkageSpec::Compiler );
|
|---|
| 194 |
|
|---|
| 195 | if ( ! libcfap ) {
|
|---|
| 196 | // read the prelude in, if we're not generating the cfa library
|
|---|
| 197 | FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
|
|---|
| 198 | if ( prelude == NULL ) {
|
|---|
| 199 | std::cerr << "Error: can't open prelude" << std::endl;
|
|---|
| 200 | exit( 1 );
|
|---|
| 201 | } // if
|
|---|
| 202 |
|
|---|
| 203 | parse( prelude, LinkageSpec::Intrinsic );
|
|---|
| 204 | } // if
|
|---|
| 205 | } // if
|
|---|
| 206 |
|
|---|
| 207 | if ( libcfap ) {
|
|---|
| 208 | parse( input, LinkageSpec::Intrinsic );
|
|---|
| 209 | } else {
|
|---|
| 210 | parse( input, LinkageSpec::Cforall, grammarp );
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | if ( parsep ) {
|
|---|
| 214 | Parser::get_parser().get_parseTree()->printList( std::cout );
|
|---|
| 215 | Parser::get_parser().freeTree();
|
|---|
| 216 | return 0;
|
|---|
| 217 | } // if
|
|---|
| 218 |
|
|---|
| 219 | buildList( Parser::get_parser().get_parseTree(), translationUnit );
|
|---|
| 220 |
|
|---|
| 221 | Parser::get_parser().freeTree();
|
|---|
| 222 | if ( astp ) {
|
|---|
| 223 | printAll( translationUnit, std::cout );
|
|---|
| 224 | return 0;
|
|---|
| 225 | } // if
|
|---|
| 226 |
|
|---|
| 227 | // add the assignment statement after the
|
|---|
| 228 | // initialization of a type parameter
|
|---|
| 229 | OPTPRINT( "tweak" )
|
|---|
| 230 | InitTweak::tweak( translationUnit );
|
|---|
| 231 | OPTPRINT( "validate" )
|
|---|
| 232 | SymTab::validate( translationUnit, symtabp );
|
|---|
| 233 | if ( symtabp ) {
|
|---|
| 234 | return 0;
|
|---|
| 235 | } // if
|
|---|
| 236 |
|
|---|
| 237 | if ( expraltp ) {
|
|---|
| 238 | ResolvExpr::AlternativePrinter printer( std::cout );
|
|---|
| 239 | acceptAll( translationUnit, printer );
|
|---|
| 240 | return 0;
|
|---|
| 241 | } // if
|
|---|
| 242 |
|
|---|
| 243 | if ( validp ) {
|
|---|
| 244 | printAll( translationUnit, std::cout );
|
|---|
| 245 | return 0;
|
|---|
| 246 | } // if
|
|---|
| 247 |
|
|---|
| 248 | OPTPRINT( "mutate" )
|
|---|
| 249 | ControlStruct::mutate( translationUnit );
|
|---|
| 250 | OPTPRINT( "fixNames" )
|
|---|
| 251 | CodeGen::fixNames( translationUnit );
|
|---|
| 252 |
|
|---|
| 253 | if ( libcfap ) {
|
|---|
| 254 | protop = true;
|
|---|
| 255 | // generate the bodies of cfa library functions
|
|---|
| 256 | LibCfa::makeLibCfa( translationUnit );
|
|---|
| 257 | } // if
|
|---|
| 258 |
|
|---|
| 259 | if ( bresolvep ) {
|
|---|
| 260 | printAll( translationUnit, std::cout );
|
|---|
| 261 | return 0;
|
|---|
| 262 | } // if
|
|---|
| 263 |
|
|---|
| 264 | OPTPRINT( "resolve" )
|
|---|
| 265 | ResolvExpr::resolve( translationUnit );
|
|---|
| 266 | if ( exprp ) {
|
|---|
| 267 | printAll( translationUnit, std::cout );
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | OPTPRINT( "copyParams" );
|
|---|
| 271 | GenPoly::copyParams( translationUnit );
|
|---|
| 272 | OPTPRINT( "convertSpecializations" )
|
|---|
| 273 | GenPoly::convertSpecializations( translationUnit );
|
|---|
| 274 | OPTPRINT( "convertLvalue" )
|
|---|
| 275 | GenPoly::convertLvalue( translationUnit );
|
|---|
| 276 | OPTPRINT( "box" )
|
|---|
| 277 | GenPoly::box( translationUnit );
|
|---|
| 278 |
|
|---|
| 279 | // print the tree right before code generation
|
|---|
| 280 | if ( codegenp ) {
|
|---|
| 281 | printAll( translationUnit, std::cout );
|
|---|
| 282 | return 0;
|
|---|
| 283 | } // if
|
|---|
| 284 |
|
|---|
| 285 | CodeGen::generate( translationUnit, *output, protop );
|
|---|
| 286 |
|
|---|
| 287 | if ( output != &std::cout ) {
|
|---|
| 288 | delete output;
|
|---|
| 289 | } // if
|
|---|
| 290 | } catch ( SemanticError &e ) {
|
|---|
| 291 | if ( errorp ) {
|
|---|
| 292 | printAll( translationUnit, std::cerr );
|
|---|
| 293 | }
|
|---|
| 294 | e.print( std::cerr );
|
|---|
| 295 | if ( output != &std::cout ) {
|
|---|
| 296 | delete output;
|
|---|
| 297 | } // if
|
|---|
| 298 | return 1;
|
|---|
| 299 | } catch ( UnimplementedError &e ) {
|
|---|
| 300 | std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
|
|---|
| 301 | if ( output != &std::cout ) {
|
|---|
| 302 | delete output;
|
|---|
| 303 | } // if
|
|---|
| 304 | return 1;
|
|---|
| 305 | } catch ( CompilerError &e ) {
|
|---|
| 306 | std::cerr << "Compiler Error: " << e.get_what() << std::endl;
|
|---|
| 307 | std::cerr << "(please report bugs to " << std::endl;
|
|---|
| 308 | if ( output != &std::cout ) {
|
|---|
| 309 | delete output;
|
|---|
| 310 | } // if
|
|---|
| 311 | return 1;
|
|---|
| 312 | } // try
|
|---|
| 313 |
|
|---|
| 314 | return 0;
|
|---|
| 315 | } // main
|
|---|
| 316 |
|
|---|
| 317 | void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
|
|---|
| 318 | Parser::get_parser().set_linkage( linkage );
|
|---|
| 319 | Parser::get_parser().parse( input );
|
|---|
| 320 |
|
|---|
| 321 | fclose( input );
|
|---|
| 322 | if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
|
|---|
| 323 | exit( Parser::get_parser().get_parseStatus() );
|
|---|
| 324 | } // if
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | // Local Variables: //
|
|---|
| 328 | // tab-width: 4 //
|
|---|
| 329 | // mode: c++ //
|
|---|
| 330 | // compile-command: "make install" //
|
|---|
| 331 | // End: //
|
|---|