| 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 : Thu Dec 17 12:59:06 2015
|
|---|
| 13 | // Update Count : 179
|
|---|
| 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 | static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
|
|---|
| 57 | static void dump( std::list< Declaration * > & translationUnit );
|
|---|
| 58 |
|
|---|
| 59 | bool
|
|---|
| 60 | astp = false,
|
|---|
| 61 | bresolvep = false,
|
|---|
| 62 | exprp = false,
|
|---|
| 63 | expraltp = false,
|
|---|
| 64 | grammarp = false,
|
|---|
| 65 | libcfap = false,
|
|---|
| 66 | nopreludep = false,
|
|---|
| 67 | noprotop = false,
|
|---|
| 68 | parsep = false,
|
|---|
| 69 | resolvep = false, // used in AlternativeFinder
|
|---|
| 70 | symtabp = false,
|
|---|
| 71 | validp = false,
|
|---|
| 72 | errorp = false,
|
|---|
| 73 | codegenp = false;
|
|---|
| 74 |
|
|---|
| 75 | enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, };
|
|---|
| 76 |
|
|---|
| 77 | static struct option long_opts[] = {
|
|---|
| 78 | { "ast", no_argument, 0, Ast },
|
|---|
| 79 | { "before-resolver", no_argument, 0, Bresolver },
|
|---|
| 80 | { "expr", no_argument, 0, Expr },
|
|---|
| 81 | { "expralt", no_argument, 0, ExprAlt },
|
|---|
| 82 | { "grammar", no_argument, 0, Grammar },
|
|---|
| 83 | { "libcfa", no_argument, 0, LibCFA },
|
|---|
| 84 | { "no-preamble", no_argument, 0, Nopreamble },
|
|---|
| 85 | { "parse", no_argument, 0, Parse },
|
|---|
| 86 | { "no-prototypes", no_argument, 0, Prototypes },
|
|---|
| 87 | { "resolver", no_argument, 0, Resolver },
|
|---|
| 88 | { "symbol", no_argument, 0, Symbol },
|
|---|
| 89 | { "validate", no_argument, 0, Validate },
|
|---|
| 90 | { 0, 0, 0, 0 }
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | int main( int argc, char *argv[] ) {
|
|---|
| 94 | FILE *input;
|
|---|
| 95 | std::ostream *output = &std::cout;
|
|---|
| 96 | int long_index;
|
|---|
| 97 | std::list< Declaration* > translationUnit;
|
|---|
| 98 |
|
|---|
| 99 | opterr = 0; // prevent getopt from printing error messages
|
|---|
| 100 |
|
|---|
| 101 | int c;
|
|---|
| 102 | while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) {
|
|---|
| 103 | switch ( c ) {
|
|---|
| 104 | case Ast:
|
|---|
| 105 | case 'a': // dump AST
|
|---|
| 106 | astp = true;
|
|---|
| 107 | break;
|
|---|
| 108 | case Bresolver:
|
|---|
| 109 | case 'b': // print before resolver steps
|
|---|
| 110 | bresolvep = true;
|
|---|
| 111 | break;
|
|---|
| 112 | case Expr:
|
|---|
| 113 | case 'e': // dump AST after expression analysis
|
|---|
| 114 | exprp = true;
|
|---|
| 115 | break;
|
|---|
| 116 | case ExprAlt:
|
|---|
| 117 | case 'f': // print alternatives for expressions
|
|---|
| 118 | expraltp = true;
|
|---|
| 119 | break;
|
|---|
| 120 | case Grammar:
|
|---|
| 121 | case 'g': // bison debugging info (grammar rules)
|
|---|
| 122 | grammarp = true;
|
|---|
| 123 | break;
|
|---|
| 124 | case LibCFA:
|
|---|
| 125 | case 'l': // generate libcfa.c
|
|---|
| 126 | libcfap = true;
|
|---|
| 127 | break;
|
|---|
| 128 | case Nopreamble:
|
|---|
| 129 | case 'n': // do not read preamble
|
|---|
| 130 | nopreludep = true;
|
|---|
| 131 | break;
|
|---|
| 132 | case Prototypes:
|
|---|
| 133 | case 'p': // generate prototypes for preamble functions
|
|---|
| 134 | noprotop = true;
|
|---|
| 135 | break;
|
|---|
| 136 | case Parse:
|
|---|
| 137 | case 'q': // dump parse tree
|
|---|
| 138 | parsep = true;
|
|---|
| 139 | break;
|
|---|
| 140 | case Resolver:
|
|---|
| 141 | case 'r': // print resolver steps
|
|---|
| 142 | resolvep = true;
|
|---|
| 143 | break;
|
|---|
| 144 | case Symbol:
|
|---|
| 145 | case 's': // print symbol table events
|
|---|
| 146 | symtabp = true;
|
|---|
| 147 | break;
|
|---|
| 148 | case 'v': // dump AST after decl validation pass
|
|---|
| 149 | validp = true;
|
|---|
| 150 | break;
|
|---|
| 151 | case 'y':
|
|---|
| 152 | errorp = true;
|
|---|
| 153 | break;
|
|---|
| 154 | case 'z':
|
|---|
| 155 | codegenp = true;
|
|---|
| 156 | break;
|
|---|
| 157 | case 'D': // ignore -Dxxx
|
|---|
| 158 | break;
|
|---|
| 159 | case '?':
|
|---|
| 160 | cout << "Unknown option: '" << (char)optopt << "'" << endl;
|
|---|
| 161 | exit(1);
|
|---|
| 162 | default:
|
|---|
| 163 | abort();
|
|---|
| 164 | } // switch
|
|---|
| 165 | } // while
|
|---|
| 166 |
|
|---|
| 167 | try {
|
|---|
| 168 | // choose to read the program from a file or stdin
|
|---|
| 169 | if ( optind < argc ) {
|
|---|
| 170 | input = fopen( argv[ optind ], "r" );
|
|---|
| 171 | if ( ! input ) {
|
|---|
| 172 | std::cout << "Error: can't open " << argv[optind] << std::endl;
|
|---|
| 173 | exit( 1 );
|
|---|
| 174 | } // if
|
|---|
| 175 | optind += 1;
|
|---|
| 176 | } else {
|
|---|
| 177 | input = stdin;
|
|---|
| 178 | } // if
|
|---|
| 179 |
|
|---|
| 180 | if ( optind < argc ) {
|
|---|
| 181 | output = new ofstream( argv[ optind ] );
|
|---|
| 182 | } // if
|
|---|
| 183 |
|
|---|
| 184 | Parser::get_parser().set_debug( grammarp );
|
|---|
| 185 |
|
|---|
| 186 | // read in the builtins and the prelude
|
|---|
| 187 | if ( ! nopreludep ) { // include gcc builtins
|
|---|
| 188 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
|
|---|
| 189 | FILE * builtins = fopen( libcfap ? "./builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
|
|---|
| 190 | if ( builtins == NULL ) {
|
|---|
| 191 | std::cerr << "Error: can't open builtins" << std::endl;
|
|---|
| 192 | exit( 1 );
|
|---|
| 193 | } // if
|
|---|
| 194 |
|
|---|
| 195 | parse( builtins, LinkageSpec::Compiler );
|
|---|
| 196 |
|
|---|
| 197 | if ( ! libcfap ) {
|
|---|
| 198 | // read the prelude in, if not generating the cfa library
|
|---|
| 199 | FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" );
|
|---|
| 200 | if ( prelude == NULL ) {
|
|---|
| 201 | std::cerr << "Error: can't open prelude" << std::endl;
|
|---|
| 202 | exit( 1 );
|
|---|
| 203 | } // if
|
|---|
| 204 |
|
|---|
| 205 | parse( prelude, LinkageSpec::Intrinsic );
|
|---|
| 206 | } // if
|
|---|
| 207 | } // if
|
|---|
| 208 |
|
|---|
| 209 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
|
|---|
| 210 |
|
|---|
| 211 | if ( parsep ) {
|
|---|
| 212 | Parser::get_parser().get_parseTree()->printList( std::cout );
|
|---|
| 213 | Parser::get_parser().freeTree();
|
|---|
| 214 | return 0;
|
|---|
| 215 | } // if
|
|---|
| 216 |
|
|---|
| 217 | buildList( Parser::get_parser().get_parseTree(), translationUnit );
|
|---|
| 218 |
|
|---|
| 219 | Parser::get_parser().freeTree();
|
|---|
| 220 | if ( astp ) {
|
|---|
| 221 | dump( translationUnit );
|
|---|
| 222 | return 0;
|
|---|
| 223 | } // if
|
|---|
| 224 |
|
|---|
| 225 | // add the assignment statement after the initialization of a type parameter
|
|---|
| 226 | OPTPRINT( "validate" )
|
|---|
| 227 | SymTab::validate( translationUnit, symtabp );
|
|---|
| 228 | if ( symtabp ) {
|
|---|
| 229 | return 0;
|
|---|
| 230 | } // if
|
|---|
| 231 |
|
|---|
| 232 | if ( expraltp ) {
|
|---|
| 233 | ResolvExpr::AlternativePrinter printer( std::cout );
|
|---|
| 234 | acceptAll( translationUnit, printer );
|
|---|
| 235 | return 0;
|
|---|
| 236 | } // if
|
|---|
| 237 |
|
|---|
| 238 | if ( validp ) {
|
|---|
| 239 | dump( translationUnit );
|
|---|
| 240 | return 0;
|
|---|
| 241 | } // if
|
|---|
| 242 |
|
|---|
| 243 | OPTPRINT( "mutate" )
|
|---|
| 244 | ControlStruct::mutate( translationUnit );
|
|---|
| 245 | OPTPRINT( "fixNames" )
|
|---|
| 246 | CodeGen::fixNames( translationUnit );
|
|---|
| 247 | OPTPRINT( "tweak" )
|
|---|
| 248 | InitTweak::tweak( translationUnit );
|
|---|
| 249 |
|
|---|
| 250 | if ( libcfap ) {
|
|---|
| 251 | // generate the bodies of cfa library functions
|
|---|
| 252 | LibCfa::makeLibCfa( translationUnit );
|
|---|
| 253 | } // if
|
|---|
| 254 |
|
|---|
| 255 | if ( bresolvep ) {
|
|---|
| 256 | dump( translationUnit );
|
|---|
| 257 | return 0;
|
|---|
| 258 | } // if
|
|---|
| 259 |
|
|---|
| 260 | OPTPRINT( "resolve" )
|
|---|
| 261 | ResolvExpr::resolve( translationUnit );
|
|---|
| 262 | if ( exprp ) {
|
|---|
| 263 | dump( translationUnit );
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | OPTPRINT( "copyParams" );
|
|---|
| 267 | GenPoly::copyParams( translationUnit );
|
|---|
| 268 | OPTPRINT( "convertSpecializations" )
|
|---|
| 269 | GenPoly::convertSpecializations( translationUnit );
|
|---|
| 270 | OPTPRINT( "convertLvalue" )
|
|---|
| 271 | GenPoly::convertLvalue( translationUnit );
|
|---|
| 272 | OPTPRINT( "box" )
|
|---|
| 273 | GenPoly::box( translationUnit );
|
|---|
| 274 |
|
|---|
| 275 | // print tree right before code generation
|
|---|
| 276 | if ( codegenp ) {
|
|---|
| 277 | dump( translationUnit );
|
|---|
| 278 | return 0;
|
|---|
| 279 | } // if
|
|---|
| 280 |
|
|---|
| 281 | CodeGen::generate( translationUnit, *output, ! noprotop );
|
|---|
| 282 |
|
|---|
| 283 | if ( output != &std::cout ) {
|
|---|
| 284 | delete output;
|
|---|
| 285 | } // if
|
|---|
| 286 | } catch ( SemanticError &e ) {
|
|---|
| 287 | if ( errorp ) {
|
|---|
| 288 | dump( translationUnit );
|
|---|
| 289 | }
|
|---|
| 290 | e.print( std::cerr );
|
|---|
| 291 | if ( output != &std::cout ) {
|
|---|
| 292 | delete output;
|
|---|
| 293 | } // if
|
|---|
| 294 | return 1;
|
|---|
| 295 | } catch ( UnimplementedError &e ) {
|
|---|
| 296 | std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl;
|
|---|
| 297 | if ( output != &std::cout ) {
|
|---|
| 298 | delete output;
|
|---|
| 299 | } // if
|
|---|
| 300 | return 1;
|
|---|
| 301 | } catch ( CompilerError &e ) {
|
|---|
| 302 | std::cerr << "Compiler Error: " << e.get_what() << std::endl;
|
|---|
| 303 | std::cerr << "(please report bugs to " << std::endl;
|
|---|
| 304 | if ( output != &std::cout ) {
|
|---|
| 305 | delete output;
|
|---|
| 306 | } // if
|
|---|
| 307 | return 1;
|
|---|
| 308 | } // try
|
|---|
| 309 |
|
|---|
| 310 | return 0;
|
|---|
| 311 | } // main
|
|---|
| 312 |
|
|---|
| 313 | static void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) {
|
|---|
| 314 | Parser::get_parser().set_linkage( linkage );
|
|---|
| 315 | Parser::get_parser().parse( input );
|
|---|
| 316 |
|
|---|
| 317 | fclose( input );
|
|---|
| 318 | if ( shouldExit || Parser::get_parser().get_parseStatus() != 0 ) {
|
|---|
| 319 | exit( Parser::get_parser().get_parseStatus() );
|
|---|
| 320 | } // if
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | static bool notPrelude( Declaration * decl ) {
|
|---|
| 324 | return ! LinkageSpec::isBuiltin( decl->get_linkage() );
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | static void dump( std::list< Declaration * > & translationUnit ) {
|
|---|
| 328 | std::list< Declaration * > decls;
|
|---|
| 329 | if ( noprotop ) {
|
|---|
| 330 | filter( translationUnit.begin(), translationUnit.end(),
|
|---|
| 331 | std::back_inserter( decls ), notPrelude );
|
|---|
| 332 | } else {
|
|---|
| 333 | decls = translationUnit;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | printAll( decls, std::cout );
|
|---|
| 337 | deleteAll( translationUnit );
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 | // Local Variables: //
|
|---|
| 342 | // tab-width: 4 //
|
|---|
| 343 | // mode: c++ //
|
|---|
| 344 | // compile-command: "make install" //
|
|---|
| 345 | // End: //
|
|---|