[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 |
---|
[f6d7e0f] | 11 | // Last Modified By : Rob Schluntz |
---|
[f678663e] | 12 | // Last Modified On : Thu Jul 30 16:08:18 2015 |
---|
| 13 | // Update Count : 167 |
---|
[b87a5ed] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <iostream> |
---|
| 17 | #include <fstream> |
---|
[b87a5ed] | 18 | #include <cstdlib> |
---|
[51b7345] | 19 | #include <cstdio> |
---|
[b87a5ed] | 20 | #include <getopt.h> |
---|
[51b7345] | 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" |
---|
[51b7345] | 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 | |
---|
[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, |
---|
| 71 | validp = false, |
---|
[de62360d] | 72 | errorp = false, |
---|
| 73 | codegenp = false; |
---|
[b87a5ed] | 74 | |
---|
[de62360d] | 75 | enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, }; |
---|
[b87a5ed] | 76 | |
---|
| 77 | static struct option long_opts[] = { |
---|
| 78 | { "ast", no_argument, 0, Ast }, |
---|
[de62360d] | 79 | { "before-resolver", no_argument, 0, Bresolver }, |
---|
[b87a5ed] | 80 | { "expr", no_argument, 0, Expr }, |
---|
| 81 | { "expralt", no_argument, 0, ExprAlt }, |
---|
| 82 | { "grammar", no_argument, 0, Grammar }, |
---|
| 83 | { "libcfa", no_argument, 0, LibCFA }, |
---|
[1ab4ce2] | 84 | { "no-preamble", no_argument, 0, Nopreamble }, |
---|
[b1d6dd5] | 85 | { "parse", no_argument, 0, Parse }, |
---|
[1ab4ce2] | 86 | { "no-prototypes", no_argument, 0, Prototypes }, |
---|
[b87a5ed] | 87 | { "resolver", no_argument, 0, Resolver }, |
---|
| 88 | { "symbol", no_argument, 0, Symbol }, |
---|
[b1d6dd5] | 89 | { "validate", no_argument, 0, Validate }, |
---|
[b87a5ed] | 90 | { 0, 0, 0, 0 } |
---|
| 91 | }; |
---|
[51b7345] | 92 | |
---|
[8c17ab0] | 93 | int main( int argc, char *argv[] ) { |
---|
[b87a5ed] | 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; |
---|
[de62360d] | 102 | while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) { |
---|
[b87a5ed] | 103 | switch ( c ) { |
---|
| 104 | case Ast: |
---|
| 105 | case 'a': // dump AST |
---|
| 106 | astp = true; |
---|
| 107 | break; |
---|
[de62360d] | 108 | case Bresolver: |
---|
| 109 | case 'b': // print before resolver steps |
---|
| 110 | bresolvep = true; |
---|
| 111 | break; |
---|
[b87a5ed] | 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 |
---|
[de62360d] | 130 | nopreludep = true; |
---|
[b87a5ed] | 131 | break; |
---|
| 132 | case Prototypes: |
---|
| 133 | case 'p': // generate prototypes for preamble functions |
---|
[1ab4ce2] | 134 | noprotop = true; |
---|
[b87a5ed] | 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; |
---|
[b1d6dd5] | 148 | case 'v': // dump AST after decl validation pass |
---|
[b87a5ed] | 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 { |
---|
[81419b5] | 168 | // choose to read the program from a file or stdin |
---|
[b87a5ed] | 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 ); |
---|
[81419b5] | 185 | |
---|
| 186 | // read in the builtins and the prelude |
---|
[de62360d] | 187 | if ( ! nopreludep ) { // include gcc builtins |
---|
[81419b5] | 188 | FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" ); |
---|
[b87a5ed] | 189 | if ( builtins == NULL ) { |
---|
[81419b5] | 190 | std::cerr << "Error: can't open builtins" << std::endl; |
---|
[b87a5ed] | 191 | exit( 1 ); |
---|
| 192 | } // if |
---|
| 193 | |
---|
[81419b5] | 194 | parse( builtins, LinkageSpec::Compiler ); |
---|
| 195 | |
---|
| 196 | if ( ! libcfap ) { |
---|
| 197 | // read the prelude in, if we're not generating the cfa library |
---|
| 198 | FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" ); |
---|
| 199 | if ( prelude == NULL ) { |
---|
| 200 | std::cerr << "Error: can't open prelude" << std::endl; |
---|
| 201 | exit( 1 ); |
---|
| 202 | } // if |
---|
| 203 | |
---|
| 204 | parse( prelude, LinkageSpec::Intrinsic ); |
---|
[b87a5ed] | 205 | } // if |
---|
| 206 | } // if |
---|
[81419b5] | 207 | |
---|
[b87a5ed] | 208 | if ( libcfap ) { |
---|
[81419b5] | 209 | parse( input, LinkageSpec::Intrinsic ); |
---|
| 210 | } else { |
---|
| 211 | parse( input, LinkageSpec::Cforall, grammarp ); |
---|
| 212 | } |
---|
[51b7345] | 213 | |
---|
[b87a5ed] | 214 | if ( parsep ) { |
---|
| 215 | Parser::get_parser().get_parseTree()->printList( std::cout ); |
---|
| 216 | Parser::get_parser().freeTree(); |
---|
| 217 | return 0; |
---|
| 218 | } // if |
---|
| 219 | |
---|
| 220 | buildList( Parser::get_parser().get_parseTree(), translationUnit ); |
---|
| 221 | |
---|
| 222 | Parser::get_parser().freeTree(); |
---|
| 223 | if ( astp ) { |
---|
[1ab4ce2] | 224 | dump( translationUnit ); |
---|
[b87a5ed] | 225 | return 0; |
---|
| 226 | } // if |
---|
| 227 | |
---|
[81419b5] | 228 | // add the assignment statement after the |
---|
| 229 | // initialization of a type parameter |
---|
| 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 | |
---|
| 270 | OPTPRINT( "copyParams" ); |
---|
[b87a5ed] | 271 | GenPoly::copyParams( translationUnit ); |
---|
[81419b5] | 272 | OPTPRINT( "convertSpecializations" ) |
---|
[698664b3] | 273 | GenPoly::convertSpecializations( translationUnit ); |
---|
[81419b5] | 274 | OPTPRINT( "convertLvalue" ) |
---|
[b87a5ed] | 275 | GenPoly::convertLvalue( translationUnit ); |
---|
[81419b5] | 276 | OPTPRINT( "box" ) |
---|
[b87a5ed] | 277 | GenPoly::box( translationUnit ); |
---|
[81419b5] | 278 | |
---|
[2871210] | 279 | // print tree right before code generation |
---|
[81419b5] | 280 | if ( codegenp ) { |
---|
[1ab4ce2] | 281 | dump( translationUnit ); |
---|
[81419b5] | 282 | return 0; |
---|
| 283 | } // if |
---|
[b87a5ed] | 284 | |
---|
[1ab4ce2] | 285 | CodeGen::generate( translationUnit, *output, ! noprotop ); |
---|
[b87a5ed] | 286 | |
---|
| 287 | if ( output != &std::cout ) { |
---|
| 288 | delete output; |
---|
| 289 | } // if |
---|
| 290 | } catch ( SemanticError &e ) { |
---|
| 291 | if ( errorp ) { |
---|
[1ab4ce2] | 292 | dump( translationUnit ); |
---|
[b87a5ed] | 293 | } |
---|
[81419b5] | 294 | e.print( std::cerr ); |
---|
[b87a5ed] | 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; |
---|
[d9a0e76] | 315 | } // main |
---|
[51b7345] | 316 | |
---|
[1ab4ce2] | 317 | static void parse( FILE * input, LinkageSpec::Type linkage, bool shouldExit ) { |
---|
[81419b5] | 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 | |
---|
[1ab4ce2] | 327 | static bool notPrelude( Declaration * decl ) { |
---|
| 328 | return ! LinkageSpec::isBuiltin( decl->get_linkage() ); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | static void dump( std::list< Declaration * > & translationUnit ) { |
---|
| 332 | std::list< Declaration * > decls; |
---|
| 333 | if ( noprotop ) { |
---|
| 334 | filter( translationUnit.begin(), translationUnit.end(), |
---|
| 335 | std::back_inserter( decls ), notPrelude ); |
---|
| 336 | } else { |
---|
| 337 | decls = translationUnit; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | printAll( decls, std::cout ); |
---|
[7f5566b] | 341 | deleteAll( translationUnit ); |
---|
[1ab4ce2] | 342 | } |
---|
| 343 | |
---|
| 344 | |
---|
[51b7345] | 345 | // Local Variables: // |
---|
[b87a5ed] | 346 | // tab-width: 4 // |
---|
| 347 | // mode: c++ // |
---|
| 348 | // compile-command: "make install" // |
---|
[51b7345] | 349 | // End: // |
---|