1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
| %{ #include <iostream> #include <ostream> #include <string> #include <cstdlib> #include "default-defs.h" #include "decafexpr.cc"
int yylex(void); int yyerror(char *);
bool printAST = false;
using namespace std;
llvm::Module *TheModule;
typedef map<string, descriptor* > symbol_table; typedef list<symbol_table > symbol_table_list; symbol_table symtbl; symbol_table_list symtblt;
llvm::LLVMContext TheContext; llvm::LLVMContext &Context = TheContext; llvm::IRBuilder<> Builder(TheContext);
static llvm::Function *TheFunction = 0; static bool get_return;
descriptor *get_symbolt(string name){ for(auto s:symtblt){ map<string, descriptor* >::iterator it; for (it = s.begin(); it != s.end(); it++) { string s = it->first; if(name == s) return it->second; } } return NULL; }
descriptor *get_symbol(string name){ map<string, descriptor* >::iterator it; for (it = symtbl.begin(); it != symtbl.end(); it++) { string s = it->first; if(name == s) return it->second; } return NULL; }
int put_symbolt(string name, descriptor * des){ if(symtblt.front()[name] != NULL){ return 0; } else{ symtblt.front()[name] = des; return 1; } }
int put_symbol(string name, descriptor * des){ if(symtbl[name] != NULL){ return 0; } else{ symtbl[name] = des; return 1; } }
bool check_symbol(string name, llvmValue k){ map<string, descriptor* >::iterator it; for (it = symtbl.begin(); it != symtbl.end(); it++) { string s = it->first; if(name == s && it->second->kind == k){ return true; } } return false; }
llvm::Value * decafBlock::Codegen() { symbol_table s; symtblt.push_front(s); llvm::Value *val = NULL; if (NULL != FieldDeclList) { val = FieldDeclList->Codegen(); } if (NULL != StateDeclList) { val = StateDeclList->Codegen(); } symtblt.pop_front(); return val; }
llvm::Value * decafStmt::Codegen() { if(this->kind == dass){ decafAssign* s = (decafAssign*)this; return s->Codegen(); } else if(this->kind == dcall){ decafFunCall* s = (decafFunCall*)this; return s->Codegen(); } else if(this->kind == dblo){ decafBlock* s = (decafBlock*)this; return s->Codegen(); } else if(this->kind == dret){ decafReturn* s = (decafReturn*)this; return s->Codegen(); } else{ return NULL; } }
llvm::Value * decafFunCall::Codegen() { descriptor * des = get_symbol(this->get_name()); llvm::Function * llvm_func = des->func; std::vector<llvm::Value *> putsargs; std::vector<llvm::Type *> putstypes;
for (auto& arg : llvm_func->args()) { llvm::Type* func_type = arg.getType(); putstypes.push_back(func_type); } int i = 0; for(auto p:this->get_para()){ decafBinexp * exp = (decafBinexp *)p; llvm::Value* vt = exp->Codegen(); llvm::Type* func_type = putstypes[i++]; vt = Builder.CreateZExt(vt, func_type, "zexttmp");
putsargs.push_back(vt); }
llvm::Value *ret_value = Builder.CreateCall(llvm_func, putsargs); return ret_value; }
llvm::Value * decafAssign::Codegen() { decafBinexp* exp = this->get_exp(); exp->Codegen(); descriptor * des = get_symbolt(this->get_var()); Builder.CreateStore(exp->des.value,des->alloc); return des->value; }
llvm::Value * decafReturn::Codegen() { decafBinexp* exp = this->get_exp(); if(exp){ exp->Codegen(); Builder.CreateRet(exp->des.value); get_return = 0; return exp->des.value; } else{ Builder.CreateRetVoid(); get_return = 0; return NULL; } }
llvm::Value *decafBinexp::Codegen() { if(this->des.kind == valueK){ decafAllexp * exp = (decafAllexp *)this; return exp->Codegen(); } else if(this->des.kind == expuK){ llvm::Value *L = Exp1->Codegen(); if (L == 0) { return 0; } switch (this->get_op(Option)) { case nottmp: this->des.value = Builder.CreateNot(L, "nottmp"); break; case negtmp: this->des.value = Builder.CreateNeg(L , "negtmp"); break; } return this->des.value; } else if(this->des.kind == expbK){ llvm::Value *L = Exp1->Codegen(); llvm::Value *R = Exp2->Codegen(); if (L == 0 || R == 0) { return 0; }
switch (this->get_op(Option)) { case addtmp: this->des.value = Builder.CreateAdd(L, R, "addtmp"); break; case subtmp: this->des.value = Builder.CreateSub(L, R, "subtmp"); break; case multmp: this->des.value = Builder.CreateMul(L, R, "multmp"); break; case remtmp: this->des.value = Builder.CreateSRem(L, R, "remtmp"); break; case divtmp: this->des.value = Builder.CreateSDiv(L, R, "divtmp"); break; case ortmp: this->des.value = Builder.CreateOr(L, R, "ortmp"); break; case andtmp: this->des.value = Builder.CreateAnd(L, R, "andtmp"); break; case eqtmp: this->des.value = Builder.CreateICmpEQ(L, R, "eqtmp"); break; case netmp: this->des.value = Builder.CreateICmpNE(L, R, "netmp"); break; case slttmp: this->des.value = Builder.CreateICmpSLT(L, R, "slttmp"); break; case sgttmp: this->des.value = Builder.CreateICmpSGT(L, R, "sgttmp"); break; case sletmp: this->des.value = Builder.CreateICmpSLE(L, R, "sletmp"); break; case sgetmp: this->des.value = Builder.CreateICmpSGE(L, R, "sgetmp"); break; case shltmp: this->des.value = Builder.CreateShl(L, R, "shltmp"); break; case shrtmp: this->des.value = Builder.CreateLShr(L, R, "shrtmp"); break; default: this->des.value = NULL; break; } return this->des.value; } else if(this->des.kind == funcK){ decafFunCall * call = (decafFunCall *)this; this->des.value = call->Codegen(); return this->des.value; } }
llvm::Value *decafAllexp::Codegen() { if(this->get_kind() == "VariableExpr"){ descriptor * des = get_symbolt(this->get_name()); llvm::Value* load = Builder.CreateLoad(des->alloc,this->get_name()); this->des.value = load; } else if(this->get_kind() == "NumberExpr"){ string s = this->get_name().substr(0, 2); llvm::Value* arg; if(s == "0x"){ arg = llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), std::stoi(this->get_name(),NULL,16)); } else{ arg = llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), std::stoi(this->get_name(),NULL,10)); } this->des.value = arg; } else if(this->get_kind() == "BoolExpr"){ if(this->get_name() == "True"){ llvm::Type* boolType = llvm::Type::getInt1Ty(Context); llvm::Constant* trueValue = llvm::ConstantInt::get(boolType, 1); this->des.value = trueValue; }else if(this->get_name() == "False"){ llvm::Type* boolType = llvm::Type::getInt1Ty(Context); llvm::Constant* falseValue = llvm::ConstantInt::get(boolType, 0); this->des.value = falseValue; } } else if(this->get_kind() == "StringConstant"){ llvm::GlobalVariable *GS = Builder.CreateGlobalString(this->get_name() ,"globalstring"); llvm::Value *stringConst = Builder.CreateConstGEP2_32(GS->getValueType(), GS, 0, 0, "cast"); this->des.value = stringConst; } return this->des.value; }
llvm::Value *decafVar::Codegen() { descriptor * des = new descriptor(); if(put_symbolt(this->get_name(),des)){ this->Alloca = Builder.CreateAlloca(this->lType, 0, this->get_name()); des->alloc = this->Alloca; des->kind = valueK; } return this->Alloca; }
llvm::Value *decafEXFuncDef::Codegen() { descriptor* des = get_symbol(this->get_name()); if(check_symbol( this->get_name(), funcK)){ return this->lfunc; } else{ return 0; } }
llvm::Value *decafFuncDef::Codegen() { descriptor* des = get_symbol(this->get_name()); if(check_symbol( this->get_name(), funcK)){ symbol_table s; symtblt.push_front(s); get_return = true; llvm::BasicBlock *BB = llvm::BasicBlock::Create(TheContext, "entry", this->lfunc); Builder.SetInsertPoint(BB);
int i = 0; for(auto p:this->get_para()->get_para()){ this->lfunc->getArg(i)->setName(p->get_name());
descriptor * des2 = new descriptor(); if(put_symbolt(p->get_name(),des2)){ des2->alloc = Builder.CreateAlloca(p->lType, 0, p->get_name()); des2->kind = valueK; } Builder.CreateStore(this->lfunc->getArg(i),des2->alloc); i++; } decafBlock * B = this->get_block(); B->Codegen();
if(get_return){ if(this->lType->isVoidTy()) Builder.CreateRetVoid(); else Builder.CreateRet(llvm::ConstantInt::get(TheContext, llvm::APInt(32, 0))); } symtblt.pop_front(); symtblt.clear(); } return this->lfunc; }
llvm::Function *gen_main_def() { llvm::FunctionType *FT = llvm::FunctionType::get(llvm::IntegerType::get(TheContext, 32), false); llvm::Function *TheFunction = llvm::Function::Create(FT, llvm::Function::ExternalLinkage, "main", TheModule); if (TheFunction == 0) { throw runtime_error("empty function block"); } llvm::BasicBlock *BB = llvm::BasicBlock::Create(TheContext, "entry", TheFunction); Builder.SetInsertPoint(BB);
descriptor* des = new descriptor(); des->func = TheFunction; des->kind = funcK; put_symbol("main",des);
return TheFunction; }
%}
%define parse.error verbose
%union{ class decafAST *ast; std::string *sval; }
%token T_PACKAGE T_EXTERN T_FUNC T_SEMICOLON T_COMMA T_CONTINUE T_FALSE T_TRUE T_VAR T_FOR T_NULL T_RETURN T_WHITESPACE %token T_AND T_ASSIGN T_DIV T_DOT T_EQ T_RIGHTSHIFT T_GEQ T_GT T_LEFTSHIFT T_LEQ T_LT T_MINUS T_MOD T_MULT T_NEQ T_NOT T_OR T_PLUS %token T_VOID T_INTTYPE T_BOOLTYPE T_STRINGTYPE %token T_LCB T_RCB T_LPAREN T_RPAREN T_LSB T_RSB %token T_COMMENT %token T_BREAK T_ELSE T_IF T_WHILE %token <sval> T_ID T_INTCONSTANT T_CHARCONSTANT T_STRINGCONSTANT
%type <ast> state_if state_while lvalues state_for state_break state_continue state_return exp assign assigns assignss method_call lvalue statements statement extern_list para_list_use para_usen para_use para_list_def block blockt var_decls var_decl method_decls method_decl decafpackage var_declp var_declps extern_def extern_defn extern_typen extern_type func_typen func_type method_type type
%right T_ASSIGN %left T_OR %left T_AND %left T_EQ T_NEQ T_LT T_GT T_GEQ T_LEQ %left T_PLUS T_MINUS %left T_MULT T_DIV T_MOD T_RIGHTSHIFT T_LEFTSHIFT %right T_NOT %right T_UMINUS %right T_LPAREN %left T_RPAREN %nonassoc T_IF %nonassoc T_ELSE
%%
start: program
program: extern_list decafpackage{ ProgramAST *prog = new ProgramAST((decafEXFuncDefList *)$1, (PackageAST *)$2); prog->Codegen(); if (printAST) { cout << getString(prog) << endl; } delete prog; } ;
extern_list: extern_defn { $$ = $1; } | { decafEXFuncDefList *slist = new decafEXFuncDefList(); $$ = slist; } ;
extern_defn: extern_def extern_defn { decafEXFuncDefList *slist = (decafEXFuncDefList *)$2; slist->push_front((decafEXFuncDef *)$1); $$ = slist; } | { decafEXFuncDefList *slist = new decafEXFuncDefList(); $$ = slist; } ;
extern_def: T_EXTERN T_FUNC T_ID T_LPAREN para_list_def T_RPAREN method_type T_SEMICOLON { decafEXFuncDef *func = new decafEXFuncDef(); decafPara* para = (decafPara*)$5; decafType * type = (decafType *)$7; func->put_name($3); func->put_type(type); func->put_para((decafPara*)para); $$ = func; delete $3; descriptor* des = new descriptor(); if(put_symbol( func->get_name(), des)){ llvm::Type *returnTy = func->lType; string Name = func->get_name(); llvm::SmallVector<llvm::Type *,0> functionArgs;
for(auto p:func->get_para()->get_para()){ functionArgs.push_back(p->lType); } func->lfunc = llvm::Function::Create( llvm::FunctionType::get(returnTy, functionArgs, false), llvm::Function::ExternalLinkage, Name, TheModule ); des->func = func->lfunc; des->kind = funcK; } } ;
para_list_use: para_usen { $$ = $1; } | { decafStmtList *slist = new decafStmtList(); $$ = slist; } ;
para_usen: para_use T_COMMA para_usen { decafStmtList * para = (decafStmtList *)$3; para->push_front($1); $$ = para; } | para_use { decafStmtList * para = new decafStmtList(); para->push_front($1); $$ = para; } ;
para_use: exp { $$ = $1;} ;
para_list_def: extern_typen { $$ = $1; } | func_typen { $$ = $1; } | { decafStmtList *slist = new decafStmtList(); $$ = slist; } ;
func_typen: func_type T_COMMA func_typen { decafPara * para = (decafPara *)$3; para->push_front((decafType *)$1); $$ = para; } | func_type { decafPara * para = new decafPara(); para->push_front((decafType*)$1); $$ = para; } ;
func_type: T_ID extern_type { decafType* type = (decafType*)$2; type->put_name(*$1); $$ = type; delete $1; } ;
extern_typen: extern_type T_COMMA extern_typen { decafPara * para = (decafPara *)$3; para->push_front((decafType *)$1); $$ = para; } | extern_type { decafPara * para = new decafPara(); para->push_front((decafType*)$1); $$ = para; } ;
extern_type: T_STRINGTYPE { decafType* type = new decafType("StringType"); type->Ty = stringTy; $$ = type;
type->lType = getLLVMType(type->Ty,Context); } | type { decafType* type = (decafType* )$1; $$ = type; } ;
method_type: T_VOID { decafType* type = new decafType("VoidType"); type->Ty = voidTy; $$ = type;
type->lType = getLLVMType(type->Ty,Context); } | type { decafType* type = (decafType* )$1; $$ = type; } ;
type: T_INTTYPE { decafType* type = new decafType("IntType"); type->Ty = intTy; $$ = type;
type->lType = getLLVMType(type->Ty,Context); } | T_BOOLTYPE { decafType* type = new decafType("BoolType"); type->Ty = boolTy; $$ = type;
type->lType = getLLVMType(type->Ty,Context); } ;
decafpackage: T_PACKAGE T_ID T_LCB var_declps method_decls T_RCB { decafVarList *field = (decafVarList *)$4; decafFuncDefList *method = (decafFuncDefList *)$5; $$ = new PackageAST(*$2, field, method); delete $2; } | T_PACKAGE T_ID T_LCB T_RCB { $$ = new PackageAST(*$2, new decafVarList(), new decafFuncDefList()); delete $2; } ;
var_declps: var_declp var_declps { decafVarList *slist = (decafVarList *)$2; slist->cat_front((decafVarList *)$1); $$ = slist; } | { decafVarList *slist = new decafVarList(); $$ = slist; } ;
var_declp: T_VAR lvalues type T_SEMICOLON { decafType * type = (decafType *)$3; decafVarList * list = (decafVarList *)$2; list->put_types(type); list->put_kinds("Scalar"); $$ = list; } | T_VAR lvalue type T_ASSIGN exp T_SEMICOLON { decafVarList * list = new decafVarList(); decafType * type = (decafType *)$3; decafVar * var = (decafVar *)$2; decafAllexp * exp = (decafAllexp *)$5; var->put_kind("Scalar"); var->put_type(type); var->put_exp(exp); list->push_front(var); $$ = list; } | T_VAR lvalue type T_SEMICOLON { decafVarList * list = new decafVarList(); decafType * type = (decafType *)$3; decafVar * var = (decafVar *)$2; var->put_kind("Scalar"); var->put_type(type); list->push_front(var); $$ = list; } ;
var_decls: var_decl var_decls { decafVarList *slist = (decafVarList *)$2; slist->cat_front((decafVarList *)$1); $$ = slist; } | { decafVarList *slist = new decafVarList(); $$ = slist; } ;
var_decl: T_VAR lvalues type T_SEMICOLON { decafType * type = (decafType *)$3; decafVarList * list = (decafVarList *)$2; list->put_types(type); $$ = list; } | T_VAR lvalue type T_ASSIGN exp T_SEMICOLON { decafVarList * list = new decafVarList(); decafType * type = (decafType *)$3; decafVar * var = (decafVar *)$2; decafAllexp * exp = (decafAllexp *)$5; var->put_type(type); var->put_exp(exp); list->push_front(var); $$ = list; } | T_VAR lvalue type T_SEMICOLON { decafVarList * list = new decafVarList(); decafType * type = (decafType *)$3; decafVar * var = (decafVar *)$2; var->put_type(type); list->push_front(var); $$ = list; } ;
lvalues: lvalue T_COMMA lvalues { decafVar* var = (decafVar*)$1; decafVarList * list = (decafVarList*)$3; list->push_back(var); $$ = list; } | lvalue { decafVar* var = (decafVar*)$1; decafVarList * list = new decafVarList(); list->push_back(var); $$ = list; } ;
lvalue: T_ID { decafVar* var = new decafVar(*$1) ; $$ = var; delete $1; } | T_ID T_LSB exp T_RSB { decafVar* var = new decafVar(*$1) ; decafAllexp* arr = (decafAllexp *)$3; var->put_arr(arr); var->put_kind("Array("+arr->get_name()+")"); $$ = var; delete $1; } ;
CONSTANT : T_INTCONSTANT | T_CHARCONSTANT | T_STRINGCONSTANT { };
method_decls: method_decl method_decls{ decafFuncDefList *slist = (decafFuncDefList *)$2; slist->push_front((decafFuncDef *)$1); $$ = slist; } | { decafFuncDefList *slist = new decafFuncDefList(); $$ = slist; } ;
method_decl: T_FUNC T_ID T_LPAREN para_list_def T_RPAREN method_type block { decafFuncDef *func = new decafFuncDef(); decafPara* para = (decafPara*)$4; decafBlock * block = (decafBlock*)$7; decafType * type = (decafType *)$6; func->put_name($2); func->put_type(type); func->put_para(para); func->put_block(block); $$ = func; delete $2;
descriptor* des = new descriptor(); if(put_symbol( func->get_name(), des)){ llvm::Type *returnTy = func->lType; std::vector<llvm::Type *> functionArgs; for(auto p:func->get_para()->get_para()){ functionArgs.push_back(p->lType); }
func->lfunc = llvm::Function::Create( llvm::FunctionType::get(returnTy, functionArgs ,false), llvm::Function::ExternalLinkage, func->get_name(), TheModule); if (func->lfunc == 0) { throw runtime_error("empty function block"); }
des->func = func->lfunc; des->kind = funcK; } } ;
blockt: T_LCB var_decls statements T_RCB { decafVarList *field = (decafVarList *)$2; decafStmts *state = (decafStmts *)$3; decafBlock *block = new decafBlock("Block",field,state); $$ = block; } | T_LCB T_RCB { decafVarList *field = new decafVarList(); decafStmts *state = new decafStmts(); decafBlock *block = new decafBlock("Block",field,state); $$ = block; } ;
block: T_LCB var_decls statements T_RCB { decafVarList *field = (decafVarList *)$2; decafStmts *state = (decafStmts *)$3; decafBlock *block = new decafBlock("MethodBlock",field,state); $$ = block; } | T_LCB T_RCB { decafVarList *field = new decafVarList(); decafStmts *state = new decafStmts(); decafBlock *block = new decafBlock("MethodBlock",field,state); $$ = block; } ;
statements: statement statements { decafStmts *slist = (decafStmts *)$2; slist->push_front((decafStmt *)$1); $$ = slist; } | { decafStmts *slist = new decafStmts(); $$ = slist; } ;
statement: blockt { $$ = $1; } | assign T_SEMICOLON { $$ = $1; } | method_call T_SEMICOLON { $$ = $1; } | state_return T_SEMICOLON { $$ = $1; } | state_if { $$ = $1; } | state_while { $$ = $1; } | state_for { $$ = $1; } | state_break T_SEMICOLON { $$ = $1; } | state_continue T_SEMICOLON { $$ = $1; } ;
state_if: T_IF T_LPAREN exp T_RPAREN blockt T_ELSE blockt { decafAllexp *exp = (decafAllexp *)$3; decafBlock *if_block = (decafBlock *)$5; decafBlock *else_block = (decafBlock *)$7; decafIF *ifs = new decafIF(exp,if_block,else_block); $$ = ifs; } | T_IF T_LPAREN exp T_RPAREN blockt { decafAllexp *exp = (decafAllexp *)$3; decafBlock *if_block = (decafBlock *)$5; decafIF *ifs = new decafIF(exp,if_block,NULL); $$ = ifs; } ;
state_while: T_WHILE T_LPAREN exp T_RPAREN blockt { decafAllexp *exp = (decafAllexp *)$3; decafBlock *block = (decafBlock *)$5; decafWhile *whiles = new decafWhile(exp,block); $$ = whiles; } ;
state_for: T_FOR T_LPAREN assignss T_SEMICOLON exp T_SEMICOLON assignss T_RPAREN blockt{ decafAllexp *exp = (decafAllexp *)$5; decafBlock *block = (decafBlock *)$9; decafAssignList *aslist = (decafAssignList *)$3; decafAssignList *aslist2 = (decafAssignList *)$7; decafFor * fors = new decafFor(exp,block,aslist,aslist2); $$ = fors; } ;
state_break: T_BREAK { decafOutput * data = new decafOutput("BreakStmt"); $$ = data; } ;
state_continue: T_CONTINUE { decafOutput * data = new decafOutput("ContinueStmt"); $$ = data; } ;
state_return: T_RETURN T_LPAREN exp T_RPAREN { decafBinexp *exp = (decafBinexp *)$3; decafReturn *ret = new decafReturn(exp); $$ = ret; } | T_RETURN T_LPAREN T_RPAREN { decafReturn *ret = new decafReturn(NULL); $$ = ret; } | T_RETURN { decafReturn *ret = new decafReturn(NULL); $$ = ret; } ;
assignss : assigns { $$ = $1; } | { decafAssignList *aslist = new decafAssignList(); $$ = aslist; } ;
assigns: assign T_COMMA assigns { decafAssignList *aslist = (decafAssignList *)$3; decafAssign *ass = (decafAssign *)$1; aslist->push_front(ass); $$ = aslist; } | assign { decafAssignList *aslist = new decafAssignList(); decafAssign *ass = (decafAssign *)$1; aslist->push_front(ass); $$ = aslist; } ;
assign: lvalue T_ASSIGN exp { decafVar* var = (decafVar *)$1; decafBinexp* exp = (decafBinexp *)$3; decafAssign* ass = new decafAssign(var->get_name(),exp); ass->kind = dass; ass->put_arr(var->get_arr()); $$ = ass; } ;
exp : T_NOT exp { decafUnaryexp * exp = new decafUnaryexp("Not", (decafBinexp*)$2); $$ = exp; } | T_MINUS exp %prec T_UMINUS { decafUnaryexp * exp = new decafUnaryexp("UnaryMinus", (decafBinexp*)$2); $$ = exp; } | exp T_PLUS exp { decafBinexp * exp = new decafBinexp("Plus", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_MINUS exp { decafBinexp * exp = new decafBinexp("Minus", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_MULT exp { decafBinexp * exp = new decafBinexp("Mult", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_DIV exp { decafBinexp * exp = new decafBinexp("Div", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_MOD exp { decafBinexp * exp = new decafBinexp("Mod", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_LEFTSHIFT exp { decafBinexp * exp = new decafBinexp("Leftshift", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_RIGHTSHIFT exp { decafBinexp * exp = new decafBinexp("Rightshift", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_LEQ exp { decafBinexp * exp = new decafBinexp("Leq", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_GEQ exp { decafBinexp * exp = new decafBinexp("Geq", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_LT exp { decafBinexp * exp = new decafBinexp("Lt", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_GT exp { decafBinexp * exp = new decafBinexp("Gt", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_EQ exp { decafBinexp * exp = new decafBinexp("Eq", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_NEQ exp { decafBinexp * exp = new decafBinexp("Neq", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_AND exp { decafBinexp * exp = new decafBinexp("And", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | exp T_OR exp { decafBinexp * exp = new decafBinexp("Or", (decafBinexp*)$1, (decafBinexp*)$3); $$ = exp; } | T_LPAREN exp T_RPAREN { $$ = $2; } | T_ID { decafAllexp * exp = new decafAllexp(*$1,"VariableExpr"); $$ = exp; delete $1; } | T_ID T_LSB exp T_RSB { decafAllexp * exp = (decafAllexp *)$3; decafArrexp * arr = new decafArrexp(*$1,exp); $$ = arr; delete $1; } | T_INTCONSTANT { decafAllexp * exp = new decafAllexp(*$1,"NumberExpr"); $$ = exp; delete $1; } | T_CHARCONSTANT { decafAllexp * exp = new decafAllexp(*$1,"NumberExpr"); $$ = exp; } | T_STRINGCONSTANT { decafAllexp * exp = new decafAllexp(*$1,"StringConstant"); $$ = exp; } | T_TRUE { decafAllexp * exp = new decafAllexp("True","BoolExpr"); $$ = exp; } | T_FALSE { decafAllexp * exp = new decafAllexp("False","BoolExpr"); $$ = exp; } | method_call { $$ = $1; } ;
method_call: T_ID T_LPAREN para_list_use T_RPAREN { decafFunCall *call = new decafFunCall(); decafStmtList* para = (decafStmtList*)$3; call->put_name($1); call->put_para(para->get_para()); $$ = call; delete $1; } ;
%%
int main() { TheModule = new llvm::Module("Test", Context); llvm::StringRef newFilename = "DecafExpr"; TheModule->setSourceFileName(newFilename); int retval = yyparse(); TheModule->print(llvm::errs(), nullptr); return(retval >= 1 ? EXIT_FAILURE : EXIT_SUCCESS); }
|