[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
|
---|
[d3b7937] | 12 | // Last Modified On : Wed Jan 27 22:20:20 2016
|
---|
| 13 | // Update Count : 199
|
---|
[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"
|
---|
| 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"
|
---|
[42e2ad7] | 42 | #include "InitTweak/RemoveInit.h"
|
---|
[51b73452] | 43 | //#include "Explain/GenProlog.h"
|
---|
| 44 | //#include "Try/Visit.h"
|
---|
| 45 |
|
---|
[d3b7937] | 46 | #include "Common/SemanticError.h"
|
---|
| 47 | #include "Common/UnimplementedError.h"
|
---|
[51b73452] | 48 |
|
---|
| 49 | #include "../config.h"
|
---|
| 50 |
|
---|
| 51 | using namespace std;
|
---|
| 52 |
|
---|
[81419b5] | 53 | #define OPTPRINT(x) \
|
---|
| 54 | if ( errorp ) std::cerr << x << std::endl;
|
---|
| 55 |
|
---|
[1ab4ce2] | 56 | static void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false );
|
---|
| 57 | static void dump( std::list< Declaration * > & translationUnit );
|
---|
[81419b5] | 58 |
|
---|
[b87a5ed] | 59 | bool
|
---|
| 60 | astp = false,
|
---|
[de62360d] | 61 | bresolvep = false,
|
---|
[b87a5ed] | 62 | exprp = false,
|
---|
| 63 | expraltp = false,
|
---|
| 64 | grammarp = false,
|
---|
| 65 | libcfap = false,
|
---|
[de62360d] | 66 | nopreludep = false,
|
---|
[1ab4ce2] | 67 | noprotop = false,
|
---|
[de62360d] | 68 | parsep = false,
|
---|
[b87a5ed] | 69 | resolvep = false, // used in AlternativeFinder
|
---|
| 70 | symtabp = false,
|
---|
[d3b7937] | 71 | treep = false,
|
---|
[b87a5ed] | 72 | validp = false,
|
---|
[de62360d] | 73 | errorp = false,
|
---|
| 74 | codegenp = false;
|
---|
[b87a5ed] | 75 |
|
---|
[d3b7937] | 76 | enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Tree, 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 },
|
---|
[d3b7937] | 90 | { "tree", no_argument, 0, Tree },
|
---|
[b1d6dd5] | 91 | { "validate", no_argument, 0, Validate },
|
---|
[b87a5ed] | 92 | { 0, 0, 0, 0 }
|
---|
| 93 | };
|
---|
[51b73452] | 94 |
|
---|
[8c17ab0] | 95 | int main( int argc, char *argv[] ) {
|
---|
[b87a5ed] | 96 | FILE *input;
|
---|
| 97 | std::ostream *output = &std::cout;
|
---|
| 98 | int long_index;
|
---|
| 99 | std::list< Declaration* > translationUnit;
|
---|
| 100 |
|
---|
| 101 | opterr = 0; // prevent getopt from printing error messages
|
---|
| 102 |
|
---|
| 103 | int c;
|
---|
[d3b7937] | 104 | while ( (c = getopt_long( argc, argv, "abefglnpqrstvyzD:", long_opts, &long_index )) != -1 ) {
|
---|
[b87a5ed] | 105 | switch ( c ) {
|
---|
| 106 | case Ast:
|
---|
| 107 | case 'a': // dump AST
|
---|
| 108 | astp = true;
|
---|
| 109 | break;
|
---|
[de62360d] | 110 | case Bresolver:
|
---|
| 111 | case 'b': // print before resolver steps
|
---|
| 112 | bresolvep = true;
|
---|
| 113 | break;
|
---|
[b87a5ed] | 114 | case Expr:
|
---|
| 115 | case 'e': // dump AST after expression analysis
|
---|
| 116 | exprp = true;
|
---|
| 117 | break;
|
---|
| 118 | case ExprAlt:
|
---|
| 119 | case 'f': // print alternatives for expressions
|
---|
| 120 | expraltp = true;
|
---|
| 121 | break;
|
---|
| 122 | case Grammar:
|
---|
| 123 | case 'g': // bison debugging info (grammar rules)
|
---|
| 124 | grammarp = true;
|
---|
| 125 | break;
|
---|
| 126 | case LibCFA:
|
---|
| 127 | case 'l': // generate libcfa.c
|
---|
| 128 | libcfap = true;
|
---|
| 129 | break;
|
---|
| 130 | case Nopreamble:
|
---|
| 131 | case 'n': // do not read preamble
|
---|
[de62360d] | 132 | nopreludep = true;
|
---|
[b87a5ed] | 133 | break;
|
---|
| 134 | case Prototypes:
|
---|
| 135 | case 'p': // generate prototypes for preamble functions
|
---|
[1ab4ce2] | 136 | noprotop = true;
|
---|
[b87a5ed] | 137 | break;
|
---|
| 138 | case Parse:
|
---|
| 139 | case 'q': // dump parse tree
|
---|
| 140 | parsep = true;
|
---|
| 141 | break;
|
---|
| 142 | case Resolver:
|
---|
| 143 | case 'r': // print resolver steps
|
---|
| 144 | resolvep = true;
|
---|
| 145 | break;
|
---|
| 146 | case Symbol:
|
---|
| 147 | case 's': // print symbol table events
|
---|
| 148 | symtabp = true;
|
---|
| 149 | break;
|
---|
[d3b7937] | 150 | case Tree:
|
---|
| 151 | case 't': // build in tree
|
---|
| 152 | treep = true;
|
---|
| 153 | break;
|
---|
[b1d6dd5] | 154 | case 'v': // dump AST after decl validation pass
|
---|
[b87a5ed] | 155 | validp = true;
|
---|
| 156 | break;
|
---|
| 157 | case 'y':
|
---|
| 158 | errorp = true;
|
---|
| 159 | break;
|
---|
| 160 | case 'z':
|
---|
| 161 | codegenp = true;
|
---|
| 162 | break;
|
---|
| 163 | case 'D': // ignore -Dxxx
|
---|
| 164 | break;
|
---|
| 165 | case '?':
|
---|
| 166 | cout << "Unknown option: '" << (char)optopt << "'" << endl;
|
---|
| 167 | exit(1);
|
---|
| 168 | default:
|
---|
| 169 | abort();
|
---|
| 170 | } // switch
|
---|
| 171 | } // while
|
---|
| 172 |
|
---|
| 173 | try {
|
---|
[81419b5] | 174 | // choose to read the program from a file or stdin
|
---|
[b87a5ed] | 175 | if ( optind < argc ) {
|
---|
| 176 | input = fopen( argv[ optind ], "r" );
|
---|
| 177 | if ( ! input ) {
|
---|
| 178 | std::cout << "Error: can't open " << argv[optind] << std::endl;
|
---|
| 179 | exit( 1 );
|
---|
| 180 | } // if
|
---|
| 181 | optind += 1;
|
---|
| 182 | } else {
|
---|
| 183 | input = stdin;
|
---|
| 184 | } // if
|
---|
| 185 |
|
---|
| 186 | if ( optind < argc ) {
|
---|
| 187 | output = new ofstream( argv[ optind ] );
|
---|
| 188 | } // if
|
---|
| 189 |
|
---|
| 190 | Parser::get_parser().set_debug( grammarp );
|
---|
[81419b5] | 191 |
|
---|
| 192 | // read in the builtins and the prelude
|
---|
[de62360d] | 193 | if ( ! nopreludep ) { // include gcc builtins
|
---|
[faf8857] | 194 | // -l is for initial build ONLY and builtins.cf is not in the lib directory so access it here.
|
---|
[d3b7937] | 195 | FILE * builtins = fopen( libcfap | treep ? "builtins.cf" : CFA_LIBDIR "/builtins.cf", "r" );
|
---|
[b87a5ed] | 196 | if ( builtins == NULL ) {
|
---|
[d3b7937] | 197 | std::cerr << "Error: can't open builtins.cf" << std::endl;
|
---|
[b87a5ed] | 198 | exit( 1 );
|
---|
| 199 | } // if
|
---|
| 200 |
|
---|
[81419b5] | 201 | parse( builtins, LinkageSpec::Compiler );
|
---|
| 202 |
|
---|
| 203 | if ( ! libcfap ) {
|
---|
[faf8857] | 204 | // read the prelude in, if not generating the cfa library
|
---|
[d3b7937] | 205 | FILE * prelude = fopen( treep ? "prelude.cf" : CFA_LIBDIR "/prelude.cf", "r" );
|
---|
[81419b5] | 206 | if ( prelude == NULL ) {
|
---|
[d3b7937] | 207 | std::cerr << "Error: can't open prelude.cf" << std::endl;
|
---|
[81419b5] | 208 | exit( 1 );
|
---|
| 209 | } // if
|
---|
| 210 |
|
---|
[35304009] | 211 | parse( prelude, LinkageSpec::Intrinsic );
|
---|
[b87a5ed] | 212 | } // if
|
---|
| 213 | } // if
|
---|
[81419b5] | 214 |
|
---|
[faf8857] | 215 | parse( input, libcfap ? LinkageSpec::Intrinsic : LinkageSpec::Cforall, grammarp );
|
---|
[51b73452] | 216 |
|
---|
[b87a5ed] | 217 | if ( parsep ) {
|
---|
| 218 | Parser::get_parser().get_parseTree()->printList( std::cout );
|
---|
| 219 | Parser::get_parser().freeTree();
|
---|
| 220 | return 0;
|
---|
| 221 | } // if
|
---|
| 222 |
|
---|
| 223 | buildList( Parser::get_parser().get_parseTree(), translationUnit );
|
---|
| 224 |
|
---|
| 225 | Parser::get_parser().freeTree();
|
---|
| 226 | if ( astp ) {
|
---|
[1ab4ce2] | 227 | dump( translationUnit );
|
---|
[b87a5ed] | 228 | return 0;
|
---|
| 229 | } // if
|
---|
| 230 |
|
---|
[839ccbb] | 231 | // add the assignment statement after the initialization of a type parameter
|
---|
[81419b5] | 232 | OPTPRINT( "validate" )
|
---|
| 233 | SymTab::validate( translationUnit, symtabp );
|
---|
| 234 | if ( symtabp ) {
|
---|
[b87a5ed] | 235 | return 0;
|
---|
| 236 | } // if
|
---|
| 237 |
|
---|
[81419b5] | 238 | if ( expraltp ) {
|
---|
| 239 | ResolvExpr::AlternativePrinter printer( std::cout );
|
---|
| 240 | acceptAll( translationUnit, printer );
|
---|
[b87a5ed] | 241 | return 0;
|
---|
| 242 | } // if
|
---|
| 243 |
|
---|
| 244 | if ( validp ) {
|
---|
[1ab4ce2] | 245 | dump( translationUnit );
|
---|
[b87a5ed] | 246 | return 0;
|
---|
| 247 | } // if
|
---|
| 248 |
|
---|
[81419b5] | 249 | OPTPRINT( "mutate" )
|
---|
| 250 | ControlStruct::mutate( translationUnit );
|
---|
| 251 | OPTPRINT( "fixNames" )
|
---|
| 252 | CodeGen::fixNames( translationUnit );
|
---|
[e497c1d] | 253 | OPTPRINT( "tweak" )
|
---|
| 254 | InitTweak::tweak( translationUnit );
|
---|
[b87a5ed] | 255 |
|
---|
[81419b5] | 256 | if ( libcfap ) {
|
---|
| 257 | // generate the bodies of cfa library functions
|
---|
| 258 | LibCfa::makeLibCfa( translationUnit );
|
---|
[b87a5ed] | 259 | } // if
|
---|
| 260 |
|
---|
[de62360d] | 261 | if ( bresolvep ) {
|
---|
[1ab4ce2] | 262 | dump( translationUnit );
|
---|
[de62360d] | 263 | return 0;
|
---|
| 264 | } // if
|
---|
| 265 |
|
---|
[81419b5] | 266 | OPTPRINT( "resolve" )
|
---|
[b87a5ed] | 267 | ResolvExpr::resolve( translationUnit );
|
---|
[81419b5] | 268 | if ( exprp ) {
|
---|
[1ab4ce2] | 269 | dump( translationUnit );
|
---|
[81419b5] | 270 | }
|
---|
| 271 |
|
---|
| 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 );
|
---|
[05d47278] | 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: //
|
---|