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