Mercurial > projects > ldc
comparison gen/statements.cpp @ 758:f04dde6e882c
Added initial D2 support, D2 frontend and changes to codegen to make things compile.
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Tue, 11 Nov 2008 01:38:48 +0100 |
parents | 46d0755451a4 |
children | 9688da40cd4d |
comparison
equal
deleted
inserted
replaced
757:2c730d530c98 | 758:f04dde6e882c |
---|---|
351 | 351 |
352 // replace current scope | 352 // replace current scope |
353 gIR->scope() = IRScope(forbb,forbodybb); | 353 gIR->scope() = IRScope(forbb,forbodybb); |
354 | 354 |
355 // create the condition | 355 // create the condition |
356 DValue* cond_e = condition->toElem(p); | 356 LLValue* cond_val; |
357 LLValue* cond_val = DtoBoolean(loc, cond_e); | 357 if (condition) |
358 delete cond_e; | 358 { |
359 DValue* cond_e = condition->toElem(p); | |
360 cond_val = DtoBoolean(loc, cond_e); | |
361 delete cond_e; | |
362 } | |
363 else | |
364 { | |
365 cond_val = DtoConstBool(true); | |
366 } | |
359 | 367 |
360 // conditional branch | 368 // conditional branch |
361 assert(!gIR->scopereturned()); | 369 assert(!gIR->scopereturned()); |
362 llvm::BranchInst::Create(forbodybb, endbb, cond_val, gIR->scopebb()); | 370 llvm::BranchInst::Create(forbodybb, endbb, cond_val, gIR->scopebb()); |
363 | 371 |
673 }; | 681 }; |
674 | 682 |
675 static LLValue* call_string_switch_runtime(llvm::Value* table, Expression* e) | 683 static LLValue* call_string_switch_runtime(llvm::Value* table, Expression* e) |
676 { | 684 { |
677 Type* dt = e->type->toBasetype(); | 685 Type* dt = e->type->toBasetype(); |
678 Type* dtnext = dt->next->toBasetype(); | 686 Type* dtnext = dt->nextOf()->toBasetype(); |
679 TY ty = dtnext->ty; | 687 TY ty = dtnext->ty; |
680 const char* fname; | 688 const char* fname; |
681 if (ty == Tchar) { | 689 if (ty == Tchar) { |
682 fname = "_d_switch_string"; | 690 fname = "_d_switch_string"; |
683 } | 691 } |
1024 llvm::BranchInst::Create(condbb, p->scopebb()); | 1032 llvm::BranchInst::Create(condbb, p->scopebb()); |
1025 | 1033 |
1026 // end | 1034 // end |
1027 p->scope() = IRScope(endbb,oldend); | 1035 p->scope() = IRScope(endbb,oldend); |
1028 } | 1036 } |
1037 | |
1038 ////////////////////////////////////////////////////////////////////////////// | |
1039 | |
1040 #if DMDV2 | |
1041 | |
1042 void ForeachRangeStatement::toIR(IRState* p) | |
1043 { | |
1044 Logger::println("ForeachRangeStatement::toIR(): %s", loc.toChars()); | |
1045 LOG_SCOPE; | |
1046 | |
1047 if (global.params.symdebug) | |
1048 DtoDwarfStopPoint(loc.linnum); | |
1049 | |
1050 // evaluate lwr/upr | |
1051 assert(lwr->type->isintegral()); | |
1052 LLValue* lower = lwr->toElem(p)->getRVal(); | |
1053 assert(upr->type->isintegral()); | |
1054 LLValue* upper = upr->toElem(p)->getRVal(); | |
1055 | |
1056 // handle key | |
1057 assert(key->type->isintegral()); | |
1058 LLValue* keyval = DtoRawVarDeclaration(key); | |
1059 | |
1060 // store initial value in key | |
1061 if (op == TOKforeach) | |
1062 DtoStore(lower, keyval); | |
1063 else | |
1064 DtoStore(upper, keyval); | |
1065 | |
1066 // set up the block we'll need | |
1067 llvm::BasicBlock* oldend = gIR->scopeend(); | |
1068 llvm::BasicBlock* condbb = llvm::BasicBlock::Create("foreachrange_cond", p->topfunc(), oldend); | |
1069 llvm::BasicBlock* bodybb = llvm::BasicBlock::Create("foreachrange_body", p->topfunc(), oldend); | |
1070 llvm::BasicBlock* nextbb = llvm::BasicBlock::Create("foreachrange_next", p->topfunc(), oldend); | |
1071 llvm::BasicBlock* endbb = llvm::BasicBlock::Create("foreachrange_end", p->topfunc(), oldend); | |
1072 | |
1073 // jump to condition | |
1074 llvm::BranchInst::Create(condbb, p->scopebb()); | |
1075 | |
1076 // CONDITION | |
1077 p->scope() = IRScope(condbb,bodybb); | |
1078 | |
1079 // first we test that lwr < upr | |
1080 lower = DtoLoad(keyval); | |
1081 assert(lower->getType() == upper->getType()); | |
1082 llvm::ICmpInst::Predicate cmpop; | |
1083 if (key->type->isunsigned()) | |
1084 { | |
1085 cmpop = (op == TOKforeach) | |
1086 ? llvm::ICmpInst::ICMP_ULT | |
1087 : llvm::ICmpInst::ICMP_UGT; | |
1088 } | |
1089 else | |
1090 { | |
1091 cmpop = (op == TOKforeach) | |
1092 ? llvm::ICmpInst::ICMP_SLT | |
1093 : llvm::ICmpInst::ICMP_SGT; | |
1094 } | |
1095 LLValue* cond = p->ir->CreateICmp(cmpop, lower, upper); | |
1096 | |
1097 // jump to the body if range is ok, to the end if not | |
1098 llvm::BranchInst::Create(bodybb, endbb, cond, p->scopebb()); | |
1099 | |
1100 // BODY | |
1101 p->scope() = IRScope(bodybb,nextbb); | |
1102 | |
1103 // reverse foreach decrements here | |
1104 if (op == TOKforeach_reverse) | |
1105 { | |
1106 LLValue* v = DtoLoad(keyval); | |
1107 LLValue* one = LLConstantInt::get(v->getType(), 1, false); | |
1108 v = p->ir->CreateSub(v, one); | |
1109 DtoStore(v, keyval); | |
1110 } | |
1111 | |
1112 // emit body | |
1113 p->loopbbs.push_back(IRLoopScope(this,enclosinghandler,nextbb,endbb)); | |
1114 if (body) | |
1115 body->toIR(p); | |
1116 p->loopbbs.pop_back(); | |
1117 | |
1118 // jump to next iteration | |
1119 if (!p->scopereturned()) | |
1120 llvm::BranchInst::Create(nextbb, p->scopebb()); | |
1121 | |
1122 // NEXT | |
1123 p->scope() = IRScope(nextbb,endbb); | |
1124 | |
1125 // forward foreach increments here | |
1126 if (op == TOKforeach) | |
1127 { | |
1128 LLValue* v = DtoLoad(keyval); | |
1129 LLValue* one = LLConstantInt::get(v->getType(), 1, false); | |
1130 v = p->ir->CreateAdd(v, one); | |
1131 DtoStore(v, keyval); | |
1132 } | |
1133 | |
1134 // jump to condition | |
1135 llvm::BranchInst::Create(condbb, p->scopebb()); | |
1136 | |
1137 // END | |
1138 p->scope() = IRScope(endbb,oldend); | |
1139 } | |
1140 | |
1141 #endif // D2 | |
1029 | 1142 |
1030 ////////////////////////////////////////////////////////////////////////////// | 1143 ////////////////////////////////////////////////////////////////////////////// |
1031 | 1144 |
1032 void LabelStatement::toIR(IRState* p) | 1145 void LabelStatement::toIR(IRState* p) |
1033 { | 1146 { |
1289 //STUBST(GotoCaseStatement); | 1402 //STUBST(GotoCaseStatement); |
1290 //STUBST(GotoDefaultStatement); | 1403 //STUBST(GotoDefaultStatement); |
1291 //STUBST(GotoStatement); | 1404 //STUBST(GotoStatement); |
1292 //STUBST(UnrolledLoopStatement); | 1405 //STUBST(UnrolledLoopStatement); |
1293 //STUBST(OnScopeStatement); | 1406 //STUBST(OnScopeStatement); |
1407 | |
1408 #if DMDV2 | |
1409 STUBST(PragmaStatement); | |
1410 #endif |