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