[51b7345] | 1 | #include <iostream> |
---|
| 2 | #include <fstream> |
---|
| 3 | #include <iterator> |
---|
| 4 | #include <algorithm> |
---|
| 5 | #include <cstdio> |
---|
| 6 | #include "Parser/Parser.h" |
---|
| 7 | #include "Parser/ParseNode.h" |
---|
| 8 | #include "Parser/LinkageSpec.h" |
---|
| 9 | #include "SynTree/Declaration.h" |
---|
| 10 | #include "SynTree/Visitor.h" |
---|
| 11 | #include "GenPoly/Lvalue.h" |
---|
| 12 | #include "GenPoly/Specialize.h" |
---|
| 13 | #include "GenPoly/Box.h" |
---|
| 14 | #include "GenPoly/CopyParams.h" |
---|
| 15 | #include "CodeGen/Generate.h" |
---|
| 16 | #include "CodeGen/FixNames.h" |
---|
| 17 | #include "ControlStruct/Mutate.h" |
---|
| 18 | #include "Tuples/Mutate.h" |
---|
| 19 | #include "Tuples/FunctionChecker.h" |
---|
| 20 | #include "SymTab/Mangler.h" |
---|
| 21 | #include "SymTab/Indexer.h" |
---|
| 22 | #include "SymTab/Validate.h" |
---|
| 23 | #include "ResolvExpr/AlternativePrinter.h" |
---|
| 24 | #include "ResolvExpr/Resolver.h" |
---|
| 25 | #include "MakeLibCfa.h" |
---|
| 26 | #include "InitTweak/Mutate.h" |
---|
| 27 | //#include "Explain/GenProlog.h" |
---|
| 28 | //#include "Try/Visit.h" |
---|
| 29 | |
---|
| 30 | #include "SemanticError.h" |
---|
| 31 | #include "UnimplementedError.h" |
---|
| 32 | #include "utility.h" |
---|
| 33 | |
---|
| 34 | #include "../config.h" |
---|
| 35 | |
---|
| 36 | using namespace std; |
---|
| 37 | |
---|
| 38 | extern "C"{ |
---|
| 39 | #include <unistd.h> |
---|
[d9a0e76] | 40 | } // extern |
---|
[51b7345] | 41 | |
---|
| 42 | FILE *open_prelude(); |
---|
| 43 | FILE *open_builtins(); |
---|
| 44 | bool beVerbose = false; |
---|
[d9a0e76] | 45 | bool resolveVerbose = false; |
---|
[51b7345] | 46 | |
---|
[8c17ab0] | 47 | int main( int argc, char *argv[] ) { |
---|
| 48 | bool debugp = false, treep = false, astp = false, manglep = false, symtabp = false, validp = false; |
---|
| 49 | bool preludep = true, protop = false, libp = false; |
---|
| 50 | bool exprp = false; |
---|
| 51 | int c; |
---|
| 52 | FILE *input, *prelude, *builtins; |
---|
| 53 | std::ostream *output = &std::cout; |
---|
[51b7345] | 54 | |
---|
[8c17ab0] | 55 | opterr = 0; |
---|
[51b7345] | 56 | |
---|
[d9a0e76] | 57 | while ( (c = getopt( argc, argv, "dtsgmvxcenprlD:" )) != -1 ) { |
---|
[8c17ab0] | 58 | switch (c) { |
---|
| 59 | case 'd': |
---|
| 60 | /* bison debugging info */ |
---|
| 61 | debugp = true; |
---|
| 62 | break; |
---|
| 63 | case 't': |
---|
| 64 | /* dump parse tree */ |
---|
| 65 | treep = true; |
---|
| 66 | break; |
---|
| 67 | case 's': |
---|
| 68 | /* dump AST */ |
---|
| 69 | astp = true; |
---|
| 70 | break; |
---|
| 71 | case 'g': |
---|
| 72 | /* print alternatives for expressions */ |
---|
| 73 | manglep = true; |
---|
| 74 | break; |
---|
| 75 | case 'm': |
---|
| 76 | /* print symbol table events */ |
---|
| 77 | symtabp = true; |
---|
| 78 | break; |
---|
[d9a0e76] | 79 | case 'r': |
---|
| 80 | /* print resolver steps */ |
---|
| 81 | resolveVerbose = true; |
---|
| 82 | break; |
---|
[8c17ab0] | 83 | case 'x': |
---|
| 84 | /* dump AST after decl validation pass */ |
---|
| 85 | validp = true; |
---|
| 86 | break; |
---|
| 87 | case 'e': |
---|
| 88 | /* dump AST after expression analysis */ |
---|
| 89 | exprp = true; |
---|
| 90 | break; |
---|
| 91 | case 'n': |
---|
| 92 | /* don't read preamble */ |
---|
| 93 | preludep = false; |
---|
| 94 | break; |
---|
| 95 | case 'p': |
---|
| 96 | /* generate prototypes for preamble functions */ |
---|
| 97 | protop = true; |
---|
| 98 | break; |
---|
| 99 | case 'l': |
---|
| 100 | /* generate libcfa.c */ |
---|
| 101 | libp = true; |
---|
| 102 | break; |
---|
| 103 | case 'v': |
---|
| 104 | /* verbose */ |
---|
| 105 | beVerbose = true; |
---|
| 106 | break; |
---|
| 107 | case 'D': |
---|
| 108 | /* ignore -Dxxx */ |
---|
| 109 | break; |
---|
| 110 | case '?': |
---|
| 111 | cout << "Unknown option: '" << (char)optopt << "'" << endl; |
---|
| 112 | exit(1); |
---|
| 113 | default: |
---|
| 114 | abort(); |
---|
[d9a0e76] | 115 | } // switch |
---|
| 116 | } // while |
---|
[51b7345] | 117 | |
---|
[8c17ab0] | 118 | try { |
---|
| 119 | if ( optind < argc ) { |
---|
| 120 | input = fopen( argv[ optind ], "r" ); |
---|
[d9a0e76] | 121 | if ( ! input ) { |
---|
[8c17ab0] | 122 | std::cout << "Error: can't open " << argv[optind] << std::endl; |
---|
| 123 | exit( 1 ); |
---|
[d9a0e76] | 124 | } // if |
---|
[8c17ab0] | 125 | optind++; |
---|
| 126 | } else { |
---|
| 127 | input = stdin; |
---|
[d9a0e76] | 128 | } // if |
---|
[51b7345] | 129 | |
---|
[8c17ab0] | 130 | if ( optind < argc ) { |
---|
| 131 | output = new ofstream( argv[ optind ] ); |
---|
[d9a0e76] | 132 | } // if |
---|
[51b7345] | 133 | |
---|
[8c17ab0] | 134 | Parser::get_parser().set_debug( debugp ); |
---|
[51b7345] | 135 | |
---|
[8c17ab0] | 136 | if ( preludep ) { |
---|
| 137 | // include gcc builtins |
---|
| 138 | builtins = open_builtins(); |
---|
| 139 | if ( !builtins ) { |
---|
| 140 | std::cout << "Error: can't open builtins" << std::endl; |
---|
| 141 | exit( 1 ); |
---|
[d9a0e76] | 142 | } // if |
---|
[51b7345] | 143 | |
---|
[8c17ab0] | 144 | Parser::get_parser().set_linkage( LinkageSpec::Compiler ); |
---|
| 145 | Parser::get_parser().parse( builtins ); |
---|
[51b7345] | 146 | |
---|
[8c17ab0] | 147 | if ( Parser::get_parser().get_parseStatus() != 0 ) { |
---|
| 148 | return Parser::get_parser().get_parseStatus(); |
---|
[d9a0e76] | 149 | } // if |
---|
[8c17ab0] | 150 | fclose( builtins ); |
---|
[51b7345] | 151 | |
---|
[8c17ab0] | 152 | // include cfa prelude |
---|
| 153 | if ( libp ) { |
---|
| 154 | prelude = input; |
---|
| 155 | } else { |
---|
| 156 | prelude = open_prelude(); |
---|
[d9a0e76] | 157 | } // if |
---|
[8c17ab0] | 158 | if ( !prelude ) { |
---|
| 159 | std::cout << "Error: can't open prelude" << std::endl; |
---|
| 160 | exit( 1 ); |
---|
[d9a0e76] | 161 | } // if |
---|
[51b7345] | 162 | |
---|
[8c17ab0] | 163 | Parser::get_parser().set_linkage( LinkageSpec::Intrinsic ); |
---|
| 164 | Parser::get_parser().parse( prelude ); |
---|
[51b7345] | 165 | |
---|
[8c17ab0] | 166 | if ( Parser::get_parser().get_parseStatus() != 0 ) { |
---|
| 167 | return Parser::get_parser().get_parseStatus(); |
---|
[d9a0e76] | 168 | } // if |
---|
[8c17ab0] | 169 | fclose( prelude ); |
---|
[d9a0e76] | 170 | } // if |
---|
[51b7345] | 171 | |
---|
[8c17ab0] | 172 | if ( libp ) { |
---|
| 173 | std::list< Declaration* > translationUnit; |
---|
| 174 | buildList( Parser::get_parser().get_parseTree(), translationUnit ); |
---|
| 175 | Parser::get_parser().freeTree(); |
---|
| 176 | SymTab::validate( translationUnit, false ); |
---|
| 177 | CodeGen::fixNames( translationUnit ); |
---|
| 178 | LibCfa::makeLibCfa( translationUnit ); |
---|
| 179 | ResolvExpr::resolve( translationUnit ); |
---|
| 180 | GenPoly::convertLvalue( translationUnit ); |
---|
| 181 | GenPoly::box( translationUnit ); |
---|
| 182 | CodeGen::generate( translationUnit, *output, true ); |
---|
| 183 | if ( output != &std::cout ) { |
---|
| 184 | delete output; |
---|
[d9a0e76] | 185 | } // if |
---|
[8c17ab0] | 186 | return 0; |
---|
[d9a0e76] | 187 | } // if |
---|
[51b7345] | 188 | |
---|
[8c17ab0] | 189 | Parser::get_parser().set_linkage( LinkageSpec::Cforall ); |
---|
[51b7345] | 190 | |
---|
[8c17ab0] | 191 | Parser::get_parser().parse( input ); |
---|
| 192 | if ( debugp || Parser::get_parser().get_parseStatus() != 0 ) { |
---|
| 193 | return Parser::get_parser().get_parseStatus(); |
---|
[d9a0e76] | 194 | } // if |
---|
[8c17ab0] | 195 | fclose( input ); |
---|
[51b7345] | 196 | |
---|
[8c17ab0] | 197 | if ( treep ) { |
---|
| 198 | Parser::get_parser().get_parseTree()->printList( std::cout ); |
---|
| 199 | Parser::get_parser().freeTree(); |
---|
| 200 | return 0; |
---|
[d9a0e76] | 201 | } // if |
---|
[51b7345] | 202 | |
---|
[8c17ab0] | 203 | std::list< Declaration* > translationUnit; |
---|
| 204 | buildList( Parser::get_parser().get_parseTree(), translationUnit ); |
---|
[51b7345] | 205 | |
---|
[8c17ab0] | 206 | Parser::get_parser().freeTree(); |
---|
| 207 | if ( astp ) { |
---|
| 208 | printAll( translationUnit, std::cout ); |
---|
| 209 | return 0; |
---|
[d9a0e76] | 210 | } // if |
---|
[51b7345] | 211 | |
---|
[8c17ab0] | 212 | if ( manglep ) { |
---|
| 213 | SymTab::validate( translationUnit, false ); |
---|
| 214 | ResolvExpr::AlternativePrinter printer( std::cout ); |
---|
| 215 | acceptAll( translationUnit, printer ); |
---|
| 216 | return 0; |
---|
[d9a0e76] | 217 | } // if |
---|
[51b7345] | 218 | |
---|
[8c17ab0] | 219 | if ( symtabp ) { |
---|
| 220 | SymTab::validate( translationUnit, true ); |
---|
| 221 | return 0; |
---|
[d9a0e76] | 222 | } // if |
---|
[51b7345] | 223 | |
---|
[8c17ab0] | 224 | if ( validp ) { |
---|
| 225 | SymTab::validate( translationUnit, false ); |
---|
| 226 | printAll( translationUnit, std::cout ); |
---|
| 227 | return 0; |
---|
[d9a0e76] | 228 | } // if |
---|
| 229 | |
---|
[8c17ab0] | 230 | if ( exprp ) { |
---|
| 231 | SymTab::validate( translationUnit, false ); |
---|
| 232 | ResolvExpr::resolve( translationUnit ); |
---|
| 233 | printAll( translationUnit, std::cout ); |
---|
| 234 | return 0; |
---|
[d9a0e76] | 235 | } // if |
---|
[51b7345] | 236 | |
---|
[d9a0e76] | 237 | //std::cerr << "before validate" << std::endl; |
---|
[8c17ab0] | 238 | SymTab::validate( translationUnit, false ); |
---|
| 239 | //Try::visit( translationUnit ); |
---|
| 240 | //Tuples::mutate( translationUnit ); |
---|
| 241 | //InitTweak::mutate( translationUnit ); |
---|
[d9a0e76] | 242 | //std::cerr << "before mutate" << std::endl; |
---|
[8c17ab0] | 243 | ControlStruct::mutate( translationUnit ); |
---|
[d9a0e76] | 244 | //std::cerr << "before fixNames" << std::endl; |
---|
[8c17ab0] | 245 | CodeGen::fixNames( translationUnit ); |
---|
[d9a0e76] | 246 | //std::cerr << "before resolve" << std::endl; |
---|
[8c17ab0] | 247 | ResolvExpr::resolve( translationUnit ); |
---|
| 248 | //Tuples::checkFunctions( translationUnit ); |
---|
| 249 | // std::cerr << "Finished tuple checkfunctions" << std::endl; |
---|
| 250 | //printAll( translationUnit, std::cerr ); |
---|
[d9a0e76] | 251 | //std::cerr << "before copyParams" << std::endl; |
---|
[8c17ab0] | 252 | GenPoly::copyParams( translationUnit ); |
---|
[d9a0e76] | 253 | //std::cerr << "before convertSpecializations" << std::endl; |
---|
[8c17ab0] | 254 | GenPoly::convertSpecializations( translationUnit ); |
---|
[d9a0e76] | 255 | //std::cerr << "before convertLvalue" << std::endl; |
---|
[8c17ab0] | 256 | GenPoly::convertLvalue( translationUnit ); |
---|
[d9a0e76] | 257 | //std::cerr << "before box" << std::endl; |
---|
[8c17ab0] | 258 | GenPoly::box( translationUnit ); |
---|
| 259 | //Tuples::mutate( translationUnit ); |
---|
[51b7345] | 260 | |
---|
[8c17ab0] | 261 | CodeGen::generate( translationUnit, *output, protop ); |
---|
[51b7345] | 262 | |
---|
[8c17ab0] | 263 | if ( output != &std::cout ) { |
---|
| 264 | delete output; |
---|
[d9a0e76] | 265 | } // if |
---|
[51b7345] | 266 | |
---|
[8c17ab0] | 267 | } catch ( SemanticError &e ) { |
---|
| 268 | e.print( cout ); |
---|
| 269 | if ( output != &std::cout ) { |
---|
| 270 | delete output; |
---|
[d9a0e76] | 271 | } // if |
---|
[8c17ab0] | 272 | return 1; |
---|
| 273 | } catch ( UnimplementedError &e ) { |
---|
| 274 | std::cout << "Sorry, " << e.get_what() << " is not currently implemented" << std::endl; |
---|
| 275 | if ( output != &std::cout ) { |
---|
| 276 | delete output; |
---|
[d9a0e76] | 277 | } // if |
---|
[8c17ab0] | 278 | return 1; |
---|
| 279 | } catch ( CompilerError &e ) { |
---|
| 280 | std::cerr << "Compiler Error: " << e.get_what() << std::endl; |
---|
| 281 | std::cerr << "(please report bugs to " << std::endl; |
---|
| 282 | if ( output != &std::cout ) { |
---|
| 283 | delete output; |
---|
[d9a0e76] | 284 | } // if |
---|
[8c17ab0] | 285 | return 1; |
---|
[d9a0e76] | 286 | } // try |
---|
[51b7345] | 287 | |
---|
[8c17ab0] | 288 | return 0; |
---|
[d9a0e76] | 289 | } // main |
---|
[51b7345] | 290 | |
---|
[8c17ab0] | 291 | FILE *open_prelude() { |
---|
| 292 | FILE *ret; |
---|
[51b7345] | 293 | |
---|
[8c17ab0] | 294 | const string name("prelude.cf"), |
---|
| 295 | full_name = string(CFA_LIBDIR) + "/" + name; |
---|
[51b7345] | 296 | |
---|
[8c17ab0] | 297 | if ( beVerbose ) { |
---|
| 298 | cout << "Reading from " << full_name << endl; |
---|
[d9a0e76] | 299 | } // if |
---|
[51b7345] | 300 | |
---|
[d9a0e76] | 301 | if ( ! (ret = fopen(full_name.c_str(), "r" ) ) ) |
---|
[8c17ab0] | 302 | return fopen(name.c_str(), "r" ); // trying current directory |
---|
| 303 | else |
---|
| 304 | return ret; |
---|
[d9a0e76] | 305 | } // open_prelude |
---|
[51b7345] | 306 | |
---|
[8c17ab0] | 307 | FILE *open_builtins() { |
---|
| 308 | FILE *ret; |
---|
[51b7345] | 309 | |
---|
[8c17ab0] | 310 | const char *name = "builtins.cf"; |
---|
| 311 | const char *full_name = CFA_LIBDIR "/builtins.cf"; |
---|
[51b7345] | 312 | |
---|
[8c17ab0] | 313 | if ( beVerbose ) { |
---|
| 314 | cout << "Reading from " << full_name << endl; |
---|
[d9a0e76] | 315 | } // if |
---|
[51b7345] | 316 | |
---|
[d9a0e76] | 317 | if ( ! (ret = fopen(full_name, "r" ) ) ) |
---|
[8c17ab0] | 318 | return fopen(name, "r" ); // trying current directory |
---|
| 319 | else |
---|
| 320 | return ret; |
---|
[d9a0e76] | 321 | } // open_builtins |
---|
[51b7345] | 322 | |
---|
| 323 | // Local Variables: // |
---|
| 324 | // compile-command: "make" // |
---|
| 325 | // End: // |
---|