comparison gen/toir.cpp @ 205:9d44ec83acd1 trunk

[svn r221] Update: Switched to the 2.3 LLVM svn branch, http://llvm.org/svn/llvm-project/llvm/branches/release_23 . Fixed: Implemented volatile statements. Uses the LLVM memory barrier intrinsic, closes #21 .
author lindquist
date Tue, 13 May 2008 17:58:11 +0200
parents e881c9b1c738
children e0b6040585b4
comparison
equal deleted inserted replaced
204:11fe364b9a3e 205:9d44ec83acd1
629 if (cofs->isZero()) { 629 if (cofs->isZero()) {
630 Logger::println("is zero"); 630 Logger::println("is zero");
631 return new DImValue(type, l->getRVal()); 631 return new DImValue(type, l->getRVal());
632 } 632 }
633 } 633 }
634 llvm::Value* v = new llvm::GetElementPtrInst(l->getRVal(), r->getRVal(), "tmp", p->scopebb()); 634 llvm::Value* v = llvm::GetElementPtrInst::Create(l->getRVal(), r->getRVal(), "tmp", p->scopebb());
635 return new DImValue(type, v); 635 return new DImValue(type, v);
636 } 636 }
637 else if (t->iscomplex()) { 637 else if (t->iscomplex()) {
638 return DtoComplexAdd(type, l, r); 638 return DtoComplexAdd(type, l, r);
639 } 639 }
661 661
662 Type* t = DtoDType(type); 662 Type* t = DtoDType(type);
663 663
664 DValue* res; 664 DValue* res;
665 if (DtoDType(e1->type)->ty == Tpointer) { 665 if (DtoDType(e1->type)->ty == Tpointer) {
666 llvm::Value* gep = new llvm::GetElementPtrInst(l->getRVal(),r->getRVal(),"tmp",p->scopebb()); 666 llvm::Value* gep = llvm::GetElementPtrInst::Create(l->getRVal(),r->getRVal(),"tmp",p->scopebb());
667 res = new DImValue(type, gep); 667 res = new DImValue(type, gep);
668 } 668 }
669 else if (t->iscomplex()) { 669 else if (t->iscomplex()) {
670 res = DtoComplexAdd(e1->type, l, r); 670 res = DtoComplexAdd(e1->type, l, r);
671 } 671 }
711 diff = p->ir->CreateIntToPtr(diff, DtoType(type), "tmp"); 711 diff = p->ir->CreateIntToPtr(diff, DtoType(type), "tmp");
712 return new DImValue(type, diff); 712 return new DImValue(type, diff);
713 } 713 }
714 else if (t1->ty == Tpointer) { 714 else if (t1->ty == Tpointer) {
715 llvm::Value* idx = p->ir->CreateNeg(r->getRVal(), "tmp"); 715 llvm::Value* idx = p->ir->CreateNeg(r->getRVal(), "tmp");
716 llvm::Value* v = new llvm::GetElementPtrInst(l->getRVal(), idx, "tmp", p->scopebb()); 716 llvm::Value* v = llvm::GetElementPtrInst::Create(l->getRVal(), idx, "tmp", p->scopebb());
717 return new DImValue(type, v); 717 return new DImValue(type, v);
718 } 718 }
719 else if (t->iscomplex()) { 719 else if (t->iscomplex()) {
720 return DtoComplexSub(type, l, r); 720 return DtoComplexSub(type, l, r);
721 } 721 }
740 if (DtoDType(e1->type)->ty == Tpointer) { 740 if (DtoDType(e1->type)->ty == Tpointer) {
741 Logger::println("ptr"); 741 Logger::println("ptr");
742 llvm::Value* tmp = r->getRVal(); 742 llvm::Value* tmp = r->getRVal();
743 llvm::Value* zero = llvm::ConstantInt::get(tmp->getType(),0,false); 743 llvm::Value* zero = llvm::ConstantInt::get(tmp->getType(),0,false);
744 tmp = llvm::BinaryOperator::createSub(zero,tmp,"tmp",p->scopebb()); 744 tmp = llvm::BinaryOperator::createSub(zero,tmp,"tmp",p->scopebb());
745 tmp = new llvm::GetElementPtrInst(l->getRVal(),tmp,"tmp",p->scopebb()); 745 tmp = llvm::GetElementPtrInst::Create(l->getRVal(),tmp,"tmp",p->scopebb());
746 res = new DImValue(type, tmp); 746 res = new DImValue(type, tmp);
747 } 747 }
748 else if (t->iscomplex()) { 748 else if (t->iscomplex()) {
749 Logger::println("complex"); 749 Logger::println("complex");
750 res = DtoComplexSub(type, l, r); 750 res = DtoComplexSub(type, l, r);
1202 varname = "tmp"; 1202 varname = "tmp";
1203 1203
1204 //Logger::cout() << "Calling: " << *funcval << '\n'; 1204 //Logger::cout() << "Calling: " << *funcval << '\n';
1205 1205
1206 // call the function 1206 // call the function
1207 llvm::CallInst* call = new llvm::CallInst(funcval, llargs.begin(), llargs.end(), varname, p->scopebb()); 1207 llvm::CallInst* call = llvm::CallInst::Create(funcval, llargs.begin(), llargs.end(), varname, p->scopebb());
1208 llvm::Value* retllval = (retinptr) ? llargs[0] : call; 1208 llvm::Value* retllval = (retinptr) ? llargs[0] : call;
1209 1209
1210 if (retinptr && dfn && dfn->func && dfn->func->runTimeHack) { 1210 if (retinptr && dfn && dfn->func && dfn->func->runTimeHack) {
1211 const llvm::Type* rettype = getPtrToType(DtoType(type)); 1211 const llvm::Type* rettype = getPtrToType(DtoType(type));
1212 if (retllval->getType() != rettype) { 1212 if (retllval->getType() != rettype) {
1546 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); 1546 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
1547 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false); 1547 llvm::Value* one = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1, false);
1548 1548
1549 llvm::Value* arrptr = 0; 1549 llvm::Value* arrptr = 0;
1550 if (e1type->ty == Tpointer) { 1550 if (e1type->ty == Tpointer) {
1551 arrptr = new llvm::GetElementPtrInst(l->getRVal(),r->getRVal(),"tmp",p->scopebb()); 1551 arrptr = llvm::GetElementPtrInst::Create(l->getRVal(),r->getRVal(),"tmp",p->scopebb());
1552 } 1552 }
1553 else if (e1type->ty == Tsarray) { 1553 else if (e1type->ty == Tsarray) {
1554 arrptr = DtoGEP(l->getRVal(), zero, r->getRVal(),"tmp",p->scopebb()); 1554 arrptr = DtoGEP(l->getRVal(), zero, r->getRVal(),"tmp",p->scopebb());
1555 } 1555 }
1556 else if (e1type->ty == Tarray) { 1556 else if (e1type->ty == Tarray) {
1557 arrptr = DtoGEP(l->getRVal(),zero,one,"tmp",p->scopebb()); 1557 arrptr = DtoGEP(l->getRVal(),zero,one,"tmp",p->scopebb());
1558 arrptr = new llvm::LoadInst(arrptr,"tmp",p->scopebb()); 1558 arrptr = new llvm::LoadInst(arrptr,"tmp",p->scopebb());
1559 arrptr = new llvm::GetElementPtrInst(arrptr,r->getRVal(),"tmp",p->scopebb()); 1559 arrptr = llvm::GetElementPtrInst::Create(arrptr,r->getRVal(),"tmp",p->scopebb());
1560 } 1560 }
1561 else if (e1type->ty == Taarray) { 1561 else if (e1type->ty == Taarray) {
1562 return DtoAAIndex(type, l, r); 1562 return DtoAAIndex(type, l, r);
1563 } 1563 }
1564 else { 1564 else {
1613 else 1613 else
1614 assert(emem); 1614 assert(emem);
1615 1615
1616 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(cv->c); 1616 llvm::ConstantInt* c = llvm::cast<llvm::ConstantInt>(cv->c);
1617 if (!(lwr_is_zero = c->isZero())) { 1617 if (!(lwr_is_zero = c->isZero())) {
1618 emem = new llvm::GetElementPtrInst(emem,cv->c,"tmp",p->scopebb()); 1618 emem = llvm::GetElementPtrInst::Create(emem,cv->c,"tmp",p->scopebb());
1619 } 1619 }
1620 } 1620 }
1621 else 1621 else
1622 { 1622 {
1623 if (e1type->ty == Tarray) { 1623 if (e1type->ty == Tarray) {
1624 llvm::Value* tmp = DtoGEP(vmem,zero,one,"tmp",p->scopebb()); 1624 llvm::Value* tmp = DtoGEP(vmem,zero,one,"tmp",p->scopebb());
1625 tmp = new llvm::LoadInst(tmp,"tmp",p->scopebb()); 1625 tmp = new llvm::LoadInst(tmp,"tmp",p->scopebb());
1626 emem = new llvm::GetElementPtrInst(tmp,lo->getRVal(),"tmp",p->scopebb()); 1626 emem = llvm::GetElementPtrInst::Create(tmp,lo->getRVal(),"tmp",p->scopebb());
1627 } 1627 }
1628 else if (e1type->ty == Tsarray) { 1628 else if (e1type->ty == Tsarray) {
1629 emem = DtoGEP(vmem,zero,lo->getRVal(),"tmp",p->scopebb()); 1629 emem = DtoGEP(vmem,zero,lo->getRVal(),"tmp",p->scopebb());
1630 } 1630 }
1631 else if (e1type->ty == Tpointer) { 1631 else if (e1type->ty == Tpointer) {
1632 emem = new llvm::GetElementPtrInst(v->getRVal(),lo->getRVal(),"tmp",p->scopebb()); 1632 emem = llvm::GetElementPtrInst::Create(v->getRVal(),lo->getRVal(),"tmp",p->scopebb());
1633 } 1633 }
1634 else { 1634 else {
1635 Logger::println("type = %s", e1type->toChars()); 1635 Logger::println("type = %s", e1type->toChars());
1636 assert(0); 1636 assert(0);
1637 } 1637 }
1901 { 1901 {
1902 assert(e2type->isintegral()); 1902 assert(e2type->isintegral());
1903 llvm::Constant* minusone = llvm::ConstantInt::get(DtoSize_t(),(uint64_t)-1,true); 1903 llvm::Constant* minusone = llvm::ConstantInt::get(DtoSize_t(),(uint64_t)-1,true);
1904 llvm::Constant* plusone = llvm::ConstantInt::get(DtoSize_t(),(uint64_t)1,false); 1904 llvm::Constant* plusone = llvm::ConstantInt::get(DtoSize_t(),(uint64_t)1,false);
1905 llvm::Constant* whichone = (op == TOKplusplus) ? plusone : minusone; 1905 llvm::Constant* whichone = (op == TOKplusplus) ? plusone : minusone;
1906 post = new llvm::GetElementPtrInst(val, whichone, "tmp", p->scopebb()); 1906 post = llvm::GetElementPtrInst::Create(val, whichone, "tmp", p->scopebb());
1907 } 1907 }
1908 else if (e1type->isfloating()) 1908 else if (e1type->isfloating())
1909 { 1909 {
1910 assert(e2type->isfloating()); 1910 assert(e2type->isfloating());
1911 llvm::Value* one = llvm::ConstantFP::get(val->getType(), llvm::APFloat(1.0f)); 1911 llvm::Value* one = DtoConstFP(e1type, 1.0);
1912 if (op == TOKplusplus) { 1912 if (op == TOKplusplus) {
1913 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb()); 1913 post = llvm::BinaryOperator::createAdd(val,one,"tmp",p->scopebb());
1914 } 1914 }
1915 else if (op == TOKminusminus) { 1915 else if (op == TOKminusminus) {
1916 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb()); 1916 post = llvm::BinaryOperator::createSub(val,one,"tmp",p->scopebb());
2138 // condition 2138 // condition
2139 DValue* cond = e1->toElem(p); 2139 DValue* cond = e1->toElem(p);
2140 2140
2141 // create basic blocks 2141 // create basic blocks
2142 llvm::BasicBlock* oldend = p->scopeend(); 2142 llvm::BasicBlock* oldend = p->scopeend();
2143 llvm::BasicBlock* assertbb = new llvm::BasicBlock("assert", p->topfunc(), oldend); 2143 llvm::BasicBlock* assertbb = llvm::BasicBlock::Create("assert", p->topfunc(), oldend);
2144 llvm::BasicBlock* endbb = new llvm::BasicBlock("endassert", p->topfunc(), oldend); 2144 llvm::BasicBlock* endbb = llvm::BasicBlock::Create("endassert", p->topfunc(), oldend);
2145 2145
2146 // test condition 2146 // test condition
2147 llvm::Value* condval = cond->getRVal(); 2147 llvm::Value* condval = cond->getRVal();
2148 condval = DtoBoolean(condval); 2148 condval = DtoBoolean(condval);
2149 2149
2150 // branch 2150 // branch
2151 new llvm::BranchInst(endbb, assertbb, condval, p->scopebb()); 2151 llvm::BranchInst::Create(endbb, assertbb, condval, p->scopebb());
2152 2152
2153 // call assert runtime functions 2153 // call assert runtime functions
2154 p->scope() = IRScope(assertbb,endbb); 2154 p->scope() = IRScope(assertbb,endbb);
2155 DtoAssert(&loc, msg ? msg->toElem(p) : NULL); 2155 DtoAssert(&loc, msg ? msg->toElem(p) : NULL);
2156 2156
2157 if (!gIR->scopereturned()) 2157 if (!gIR->scopereturned())
2158 new llvm::BranchInst(endbb, p->scopebb()); 2158 llvm::BranchInst::Create(endbb, p->scopebb());
2159 2159
2160 // rewrite the scope 2160 // rewrite the scope
2161 p->scope() = IRScope(endbb,oldend); 2161 p->scope() = IRScope(endbb,oldend);
2162 2162
2163 // no meaningful return value 2163 // no meaningful return value
2194 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"andandtmp",p->topallocapoint()); 2194 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"andandtmp",p->topallocapoint());
2195 2195
2196 DValue* u = e1->toElem(p); 2196 DValue* u = e1->toElem(p);
2197 2197
2198 llvm::BasicBlock* oldend = p->scopeend(); 2198 llvm::BasicBlock* oldend = p->scopeend();
2199 llvm::BasicBlock* andand = new llvm::BasicBlock("andand", gIR->topfunc(), oldend); 2199 llvm::BasicBlock* andand = llvm::BasicBlock::Create("andand", gIR->topfunc(), oldend);
2200 llvm::BasicBlock* andandend = new llvm::BasicBlock("andandend", gIR->topfunc(), oldend); 2200 llvm::BasicBlock* andandend = llvm::BasicBlock::Create("andandend", gIR->topfunc(), oldend);
2201 2201
2202 llvm::Value* ubool = DtoBoolean(u->getRVal()); 2202 llvm::Value* ubool = DtoBoolean(u->getRVal());
2203 new llvm::StoreInst(ubool,resval,p->scopebb()); 2203 new llvm::StoreInst(ubool,resval,p->scopebb());
2204 new llvm::BranchInst(andand,andandend,ubool,p->scopebb()); 2204 llvm::BranchInst::Create(andand,andandend,ubool,p->scopebb());
2205 2205
2206 p->scope() = IRScope(andand, andandend); 2206 p->scope() = IRScope(andand, andandend);
2207 DValue* v = e2->toElem(p); 2207 DValue* v = e2->toElem(p);
2208 2208
2209 llvm::Value* vbool = DtoBoolean(v->getRVal()); 2209 llvm::Value* vbool = DtoBoolean(v->getRVal());
2210 llvm::Value* uandvbool = llvm::BinaryOperator::create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb()); 2210 llvm::Value* uandvbool = llvm::BinaryOperator::create(llvm::BinaryOperator::And, ubool, vbool,"tmp",p->scopebb());
2211 new llvm::StoreInst(uandvbool,resval,p->scopebb()); 2211 new llvm::StoreInst(uandvbool,resval,p->scopebb());
2212 new llvm::BranchInst(andandend,p->scopebb()); 2212 llvm::BranchInst::Create(andandend,p->scopebb());
2213 2213
2214 p->scope() = IRScope(andandend, oldend); 2214 p->scope() = IRScope(andandend, oldend);
2215 2215
2216 resval = new llvm::LoadInst(resval,"tmp",p->scopebb()); 2216 resval = new llvm::LoadInst(resval,"tmp",p->scopebb());
2217 return new DImValue(type, resval); 2217 return new DImValue(type, resval);
2230 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"orortmp",p->topallocapoint()); 2230 resval = new llvm::AllocaInst(llvm::Type::Int1Ty,"orortmp",p->topallocapoint());
2231 2231
2232 DValue* u = e1->toElem(p); 2232 DValue* u = e1->toElem(p);
2233 2233
2234 llvm::BasicBlock* oldend = p->scopeend(); 2234 llvm::BasicBlock* oldend = p->scopeend();
2235 llvm::BasicBlock* oror = new llvm::BasicBlock("oror", gIR->topfunc(), oldend); 2235 llvm::BasicBlock* oror = llvm::BasicBlock::Create("oror", gIR->topfunc(), oldend);
2236 llvm::BasicBlock* ororend = new llvm::BasicBlock("ororend", gIR->topfunc(), oldend); 2236 llvm::BasicBlock* ororend = llvm::BasicBlock::Create("ororend", gIR->topfunc(), oldend);
2237 2237
2238 llvm::Value* ubool = DtoBoolean(u->getRVal()); 2238 llvm::Value* ubool = DtoBoolean(u->getRVal());
2239 new llvm::StoreInst(ubool,resval,p->scopebb()); 2239 new llvm::StoreInst(ubool,resval,p->scopebb());
2240 new llvm::BranchInst(ororend,oror,ubool,p->scopebb()); 2240 llvm::BranchInst::Create(ororend,oror,ubool,p->scopebb());
2241 2241
2242 p->scope() = IRScope(oror, ororend); 2242 p->scope() = IRScope(oror, ororend);
2243 DValue* v = e2->toElem(p); 2243 DValue* v = e2->toElem(p);
2244 2244
2245 llvm::Value* vbool = DtoBoolean(v->getRVal()); 2245 llvm::Value* vbool = DtoBoolean(v->getRVal());
2246 new llvm::StoreInst(vbool,resval,p->scopebb()); 2246 new llvm::StoreInst(vbool,resval,p->scopebb());
2247 new llvm::BranchInst(ororend,p->scopebb()); 2247 llvm::BranchInst::Create(ororend,p->scopebb());
2248 2248
2249 p->scope() = IRScope(ororend, oldend); 2249 p->scope() = IRScope(ororend, oldend);
2250 2250
2251 resval = new llvm::LoadInst(resval,"tmp",p->scopebb()); 2251 resval = new llvm::LoadInst(resval,"tmp",p->scopebb());
2252 return new DImValue(type, resval); 2252 return new DImValue(type, resval);
2461 llvm::BasicBlock* entryblock = &p->topfunc()->front(); 2461 llvm::BasicBlock* entryblock = &p->topfunc()->front();
2462 llvm::Value* resval = new llvm::AllocaInst(resty,"condtmp",p->topallocapoint()); 2462 llvm::Value* resval = new llvm::AllocaInst(resty,"condtmp",p->topallocapoint());
2463 DVarValue* dvv = new DVarValue(type, resval, true); 2463 DVarValue* dvv = new DVarValue(type, resval, true);
2464 2464
2465 llvm::BasicBlock* oldend = p->scopeend(); 2465 llvm::BasicBlock* oldend = p->scopeend();
2466 llvm::BasicBlock* condtrue = new llvm::BasicBlock("condtrue", gIR->topfunc(), oldend); 2466 llvm::BasicBlock* condtrue = llvm::BasicBlock::Create("condtrue", gIR->topfunc(), oldend);
2467 llvm::BasicBlock* condfalse = new llvm::BasicBlock("condfalse", gIR->topfunc(), oldend); 2467 llvm::BasicBlock* condfalse = llvm::BasicBlock::Create("condfalse", gIR->topfunc(), oldend);
2468 llvm::BasicBlock* condend = new llvm::BasicBlock("condend", gIR->topfunc(), oldend); 2468 llvm::BasicBlock* condend = llvm::BasicBlock::Create("condend", gIR->topfunc(), oldend);
2469 2469
2470 DValue* c = econd->toElem(p); 2470 DValue* c = econd->toElem(p);
2471 llvm::Value* cond_val = DtoBoolean(c->getRVal()); 2471 llvm::Value* cond_val = DtoBoolean(c->getRVal());
2472 new llvm::BranchInst(condtrue,condfalse,cond_val,p->scopebb()); 2472 llvm::BranchInst::Create(condtrue,condfalse,cond_val,p->scopebb());
2473 2473
2474 p->scope() = IRScope(condtrue, condfalse); 2474 p->scope() = IRScope(condtrue, condfalse);
2475 DValue* u = e1->toElem(p); 2475 DValue* u = e1->toElem(p);
2476 DtoAssign(dvv, u); 2476 DtoAssign(dvv, u);
2477 new llvm::BranchInst(condend,p->scopebb()); 2477 llvm::BranchInst::Create(condend,p->scopebb());
2478 2478
2479 p->scope() = IRScope(condfalse, condend); 2479 p->scope() = IRScope(condfalse, condend);
2480 DValue* v = e2->toElem(p); 2480 DValue* v = e2->toElem(p);
2481 DtoAssign(dvv, v); 2481 DtoAssign(dvv, v);
2482 new llvm::BranchInst(condend,p->scopebb()); 2482 llvm::BranchInst::Create(condend,p->scopebb());
2483 2483
2484 p->scope() = IRScope(condend, oldend); 2484 p->scope() = IRScope(condend, oldend);
2485 return dvv; 2485 return dvv;
2486 } 2486 }
2487 2487
2519 2519
2520 llvm::Value* zero = 0; 2520 llvm::Value* zero = 0;
2521 if (t->isintegral()) 2521 if (t->isintegral())
2522 zero = llvm::ConstantInt::get(val->getType(), 0, true); 2522 zero = llvm::ConstantInt::get(val->getType(), 0, true);
2523 else if (t->isfloating()) { 2523 else if (t->isfloating()) {
2524 if (t->ty == Tfloat32 || t->ty == Timaginary32) 2524 zero = DtoConstFP(type, 0.0);
2525 zero = llvm::ConstantFP::get(val->getType(), llvm::APFloat(0.0f));
2526 else if (t->ty == Tfloat64 || t->ty == Tfloat80 || t->ty == Timaginary64 || t->ty == Timaginary80)
2527 zero = llvm::ConstantFP::get(val->getType(), llvm::APFloat(0.0));
2528 else
2529 {
2530 Logger::println("unhandled fp negation of type %s", t->toChars());
2531 assert(0);
2532 }
2533 } 2525 }
2534 else 2526 else
2535 assert(0); 2527 assert(0);
2536 2528
2537 val = llvm::BinaryOperator::createSub(zero,val,"tmp",p->scopebb()); 2529 val = llvm::BinaryOperator::createSub(zero,val,"tmp",p->scopebb());