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