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