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