| [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 | 
|---|
| [8686f31] | 12 | // Last Modified On : Mon Jul 06 15:01:26 2015 | 
|---|
|  | 13 | // Update Count     : 79 | 
|---|
| [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 |  | 
|---|
|  | 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 |  | 
|---|
| [68cd1ce] | 56 | void parse( FILE * input, LinkageSpec::Type t, bool shouldExit = false ); | 
|---|
| [81419b5] | 57 |  | 
|---|
| [b87a5ed] | 58 | bool | 
|---|
|  | 59 | astp = false, | 
|---|
| [de62360d] | 60 | bresolvep = false, | 
|---|
| [b87a5ed] | 61 | exprp = false, | 
|---|
|  | 62 | expraltp = false, | 
|---|
|  | 63 | grammarp = false, | 
|---|
|  | 64 | libcfap = false, | 
|---|
| [de62360d] | 65 | nopreludep = false, | 
|---|
|  | 66 | protop = false, | 
|---|
|  | 67 | parsep = false, | 
|---|
| [b87a5ed] | 68 | resolvep = false,                                                                       // used in AlternativeFinder | 
|---|
|  | 69 | symtabp = false, | 
|---|
|  | 70 | validp = false, | 
|---|
| [de62360d] | 71 | errorp = false, | 
|---|
|  | 72 | codegenp = false; | 
|---|
| [b87a5ed] | 73 |  | 
|---|
| [de62360d] | 74 | enum { Ast, Bresolver, Expr, ExprAlt, Grammar, LibCFA, Nopreamble, Parse, Prototypes, Resolver, Symbol, Validate, }; | 
|---|
| [b87a5ed] | 75 |  | 
|---|
|  | 76 | static struct option long_opts[] = { | 
|---|
|  | 77 | { "ast", no_argument, 0, Ast }, | 
|---|
| [de62360d] | 78 | { "before-resolver", no_argument, 0, Bresolver }, | 
|---|
| [b87a5ed] | 79 | { "expr", no_argument, 0, Expr }, | 
|---|
|  | 80 | { "expralt", no_argument, 0, ExprAlt }, | 
|---|
|  | 81 | { "grammar", no_argument, 0, Grammar }, | 
|---|
|  | 82 | { "libcfa", no_argument, 0, LibCFA }, | 
|---|
|  | 83 | { "nopreamble", no_argument, 0, Nopreamble }, | 
|---|
| [b1d6dd5] | 84 | { "parse", no_argument, 0, Parse }, | 
|---|
| [b87a5ed] | 85 | { "prototypes", no_argument, 0, Prototypes }, | 
|---|
|  | 86 | { "resolver", no_argument, 0, Resolver }, | 
|---|
|  | 87 | { "symbol", no_argument, 0, Symbol }, | 
|---|
| [b1d6dd5] | 88 | { "validate", no_argument, 0, Validate }, | 
|---|
| [b87a5ed] | 89 | { 0, 0, 0, 0 } | 
|---|
|  | 90 | }; | 
|---|
| [51b73452] | 91 |  | 
|---|
| [8c17ab0] | 92 | int main( int argc, char *argv[] ) { | 
|---|
| [b87a5ed] | 93 | FILE *input; | 
|---|
|  | 94 | std::ostream *output = &std::cout; | 
|---|
|  | 95 | int long_index; | 
|---|
|  | 96 | std::list< Declaration* > translationUnit; | 
|---|
|  | 97 |  | 
|---|
|  | 98 | opterr = 0;                                                                                     // prevent getopt from printing error messages | 
|---|
|  | 99 |  | 
|---|
|  | 100 | int c; | 
|---|
| [de62360d] | 101 | while ( (c = getopt_long( argc, argv, "abefglnpqrsvyzD:", long_opts, &long_index )) != -1 ) { | 
|---|
| [b87a5ed] | 102 | switch ( c ) { | 
|---|
|  | 103 | case Ast: | 
|---|
|  | 104 | case 'a':                                                                             // dump AST | 
|---|
|  | 105 | astp = true; | 
|---|
|  | 106 | break; | 
|---|
| [de62360d] | 107 | case Bresolver: | 
|---|
|  | 108 | case 'b':                                                                             // print before resolver steps | 
|---|
|  | 109 | bresolvep = true; | 
|---|
|  | 110 | break; | 
|---|
| [b87a5ed] | 111 | case Expr: | 
|---|
|  | 112 | case 'e':                                                                             // dump AST after expression analysis | 
|---|
|  | 113 | exprp = true; | 
|---|
|  | 114 | break; | 
|---|
|  | 115 | case ExprAlt: | 
|---|
|  | 116 | case 'f':                                                                             // print alternatives for expressions | 
|---|
|  | 117 | expraltp = true; | 
|---|
|  | 118 | break; | 
|---|
|  | 119 | case Grammar: | 
|---|
|  | 120 | case 'g':                                                                             // bison debugging info (grammar rules) | 
|---|
|  | 121 | grammarp = true; | 
|---|
|  | 122 | break; | 
|---|
|  | 123 | case LibCFA: | 
|---|
|  | 124 | case 'l':                                                                             // generate libcfa.c | 
|---|
|  | 125 | libcfap = true; | 
|---|
|  | 126 | break; | 
|---|
|  | 127 | case Nopreamble: | 
|---|
|  | 128 | case 'n':                                                                             // do not read preamble | 
|---|
| [de62360d] | 129 | nopreludep = true; | 
|---|
| [b87a5ed] | 130 | break; | 
|---|
|  | 131 | case Prototypes: | 
|---|
|  | 132 | case 'p':                                                                             // generate prototypes for preamble functions | 
|---|
|  | 133 | protop = true; | 
|---|
|  | 134 | break; | 
|---|
|  | 135 | case Parse: | 
|---|
|  | 136 | case 'q':                                                                             // dump parse tree | 
|---|
|  | 137 | parsep = true; | 
|---|
|  | 138 | break; | 
|---|
|  | 139 | case Resolver: | 
|---|
|  | 140 | case 'r':                                                                             // print resolver steps | 
|---|
|  | 141 | resolvep = true; | 
|---|
|  | 142 | break; | 
|---|
|  | 143 | case Symbol: | 
|---|
|  | 144 | case 's':                                                                             // print symbol table events | 
|---|
|  | 145 | symtabp = true; | 
|---|
|  | 146 | break; | 
|---|
| [b1d6dd5] | 147 | case 'v':                                                                             // dump AST after decl validation pass | 
|---|
| [b87a5ed] | 148 | validp = true; | 
|---|
|  | 149 | break; | 
|---|
|  | 150 | case 'y': | 
|---|
|  | 151 | errorp = true; | 
|---|
|  | 152 | break; | 
|---|
|  | 153 | case 'z': | 
|---|
|  | 154 | codegenp = true; | 
|---|
|  | 155 | break; | 
|---|
|  | 156 | case 'D':                                                                             // ignore -Dxxx | 
|---|
|  | 157 | break; | 
|---|
|  | 158 | case '?': | 
|---|
|  | 159 | cout << "Unknown option: '" << (char)optopt << "'" << endl; | 
|---|
|  | 160 | exit(1); | 
|---|
|  | 161 | default: | 
|---|
|  | 162 | abort(); | 
|---|
|  | 163 | } // switch | 
|---|
|  | 164 | } // while | 
|---|
|  | 165 |  | 
|---|
|  | 166 | try { | 
|---|
| [81419b5] | 167 | // choose to read the program from a file or stdin | 
|---|
| [b87a5ed] | 168 | if ( optind < argc ) { | 
|---|
|  | 169 | input = fopen( argv[ optind ], "r" ); | 
|---|
|  | 170 | if ( ! input ) { | 
|---|
|  | 171 | std::cout << "Error: can't open " << argv[optind] << std::endl; | 
|---|
|  | 172 | exit( 1 ); | 
|---|
|  | 173 | } // if | 
|---|
|  | 174 | optind += 1; | 
|---|
|  | 175 | } else { | 
|---|
|  | 176 | input = stdin; | 
|---|
|  | 177 | } // if | 
|---|
|  | 178 |  | 
|---|
|  | 179 | if ( optind < argc ) { | 
|---|
|  | 180 | output = new ofstream( argv[ optind ] ); | 
|---|
|  | 181 | } // if | 
|---|
|  | 182 |  | 
|---|
|  | 183 | Parser::get_parser().set_debug( grammarp ); | 
|---|
| [81419b5] | 184 |  | 
|---|
|  | 185 | // read in the builtins and the prelude | 
|---|
| [de62360d] | 186 | if ( ! nopreludep ) {                                                   // include gcc builtins | 
|---|
| [81419b5] | 187 | FILE * builtins = fopen( CFA_LIBDIR "/builtins.cf", "r" ); | 
|---|
| [b87a5ed] | 188 | if ( builtins == NULL ) { | 
|---|
| [81419b5] | 189 | std::cerr << "Error: can't open builtins" << std::endl; | 
|---|
| [b87a5ed] | 190 | exit( 1 ); | 
|---|
|  | 191 | } // if | 
|---|
|  | 192 |  | 
|---|
| [81419b5] | 193 | parse( builtins, LinkageSpec::Compiler ); | 
|---|
|  | 194 |  | 
|---|
|  | 195 | if ( ! libcfap ) { | 
|---|
|  | 196 | // read the prelude in, if we're not generating the cfa library | 
|---|
|  | 197 | FILE * prelude = fopen( CFA_LIBDIR "/prelude.cf", "r" ); | 
|---|
|  | 198 | if ( prelude == NULL ) { | 
|---|
|  | 199 | std::cerr << "Error: can't open prelude" << std::endl; | 
|---|
|  | 200 | exit( 1 ); | 
|---|
|  | 201 | } // if | 
|---|
|  | 202 |  | 
|---|
|  | 203 | parse( prelude, LinkageSpec::Intrinsic ); | 
|---|
| [b87a5ed] | 204 | } // if | 
|---|
|  | 205 | } // if | 
|---|
| [81419b5] | 206 |  | 
|---|
| [b87a5ed] | 207 | if ( libcfap ) { | 
|---|
| [81419b5] | 208 | parse( input, LinkageSpec::Intrinsic ); | 
|---|
|  | 209 | } else { | 
|---|
|  | 210 | parse( input, LinkageSpec::Cforall, grammarp ); | 
|---|
|  | 211 | } | 
|---|
| [51b73452] | 212 |  | 
|---|
| [b87a5ed] | 213 | if ( parsep ) { | 
|---|
|  | 214 | Parser::get_parser().get_parseTree()->printList( std::cout ); | 
|---|
|  | 215 | Parser::get_parser().freeTree(); | 
|---|
|  | 216 | return 0; | 
|---|
|  | 217 | } // if | 
|---|
|  | 218 |  | 
|---|
|  | 219 | buildList( Parser::get_parser().get_parseTree(), translationUnit ); | 
|---|
|  | 220 |  | 
|---|
|  | 221 | Parser::get_parser().freeTree(); | 
|---|
|  | 222 | if ( astp ) { | 
|---|
|  | 223 | printAll( translationUnit, std::cout ); | 
|---|
|  | 224 | return 0; | 
|---|
|  | 225 | } // if | 
|---|
|  | 226 |  | 
|---|
| [81419b5] | 227 | // add the assignment statement after the | 
|---|
|  | 228 | // initialization of a type parameter | 
|---|
|  | 229 | OPTPRINT( "tweak" ) | 
|---|
|  | 230 | InitTweak::tweak( translationUnit ); | 
|---|
|  | 231 | OPTPRINT( "validate" ) | 
|---|
|  | 232 | SymTab::validate( translationUnit, symtabp ); | 
|---|
|  | 233 | if ( symtabp ) { | 
|---|
| [b87a5ed] | 234 | return 0; | 
|---|
|  | 235 | } // if | 
|---|
|  | 236 |  | 
|---|
| [81419b5] | 237 | if ( expraltp ) { | 
|---|
|  | 238 | ResolvExpr::AlternativePrinter printer( std::cout ); | 
|---|
|  | 239 | acceptAll( translationUnit, printer ); | 
|---|
| [b87a5ed] | 240 | return 0; | 
|---|
|  | 241 | } // if | 
|---|
|  | 242 |  | 
|---|
|  | 243 | if ( validp ) { | 
|---|
|  | 244 | printAll( translationUnit, std::cout ); | 
|---|
|  | 245 | return 0; | 
|---|
|  | 246 | } // if | 
|---|
|  | 247 |  | 
|---|
| [81419b5] | 248 | OPTPRINT( "mutate" ) | 
|---|
|  | 249 | ControlStruct::mutate( translationUnit ); | 
|---|
|  | 250 | OPTPRINT( "fixNames" ) | 
|---|
|  | 251 | CodeGen::fixNames( translationUnit ); | 
|---|
| [b87a5ed] | 252 |  | 
|---|
| [81419b5] | 253 | if ( libcfap ) { | 
|---|
|  | 254 | protop = true; | 
|---|
|  | 255 | // generate the bodies of cfa library functions | 
|---|
|  | 256 | LibCfa::makeLibCfa( translationUnit ); | 
|---|
| [b87a5ed] | 257 | } // if | 
|---|
|  | 258 |  | 
|---|
| [de62360d] | 259 | if ( bresolvep ) { | 
|---|
|  | 260 | printAll( translationUnit, std::cout ); | 
|---|
|  | 261 | return 0; | 
|---|
|  | 262 | } // if | 
|---|
|  | 263 |  | 
|---|
| [81419b5] | 264 | OPTPRINT( "resolve" ) | 
|---|
| [b87a5ed] | 265 | ResolvExpr::resolve( translationUnit ); | 
|---|
| [81419b5] | 266 | if ( exprp ) { | 
|---|
|  | 267 | printAll( translationUnit, std::cout ); | 
|---|
|  | 268 | } | 
|---|
|  | 269 |  | 
|---|
|  | 270 | OPTPRINT( "copyParams" ); | 
|---|
| [b87a5ed] | 271 | GenPoly::copyParams( translationUnit ); | 
|---|
| [81419b5] | 272 | OPTPRINT( "convertSpecializations" ) | 
|---|
|  | 273 | GenPoly::convertSpecializations( translationUnit ); | 
|---|
|  | 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 ) { | 
|---|
|  | 281 | printAll( translationUnit, std::cout ); | 
|---|
|  | 282 | return 0; | 
|---|
|  | 283 | } // if | 
|---|
| [b87a5ed] | 284 |  | 
|---|
| [f6d7e0f] | 285 | CodeGen::generate( translationUnit, *output, true ); //protop ); | 
|---|
| [b87a5ed] | 286 |  | 
|---|
|  | 287 | if ( output != &std::cout ) { | 
|---|
|  | 288 | delete output; | 
|---|
|  | 289 | } // if | 
|---|
|  | 290 | } catch ( SemanticError &e ) { | 
|---|
|  | 291 | if ( errorp ) { | 
|---|
| [81419b5] | 292 | printAll( translationUnit, std::cerr ); | 
|---|
| [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 | 
|---|
| [51b73452] | 316 |  | 
|---|
| [68cd1ce] | 317 | 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 |  | 
|---|
| [51b73452] | 327 | // Local Variables: // | 
|---|
| [b87a5ed] | 328 | // tab-width: 4 // | 
|---|
|  | 329 | // mode: c++ // | 
|---|
|  | 330 | // compile-command: "make install" // | 
|---|
| [51b73452] | 331 | // End:  // | 
|---|