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