comparison gen/toir.c @ 8:5e69b77a5c51 trunk

[svn r12] fixed accessing aggregate fields of aggregates removed some useless branches for successive scopes ala {}{}{}
author lindquist
date Thu, 27 Sep 2007 06:03:06 +0200
parents 35d93ce68cf4
children dafae18f9c08
comparison
equal deleted inserted replaced
7:7a155ba88c53 8:5e69b77a5c51
432 elem* l = e1->toElem(p); 432 elem* l = e1->toElem(p);
433 elem* r = e2->toElem(p); 433 elem* r = e2->toElem(p);
434 434
435 if (e1->type != e2->type) { 435 if (e1->type != e2->type) {
436 if (e1->type->ty == Tpointer && e1->type->next->ty == Tstruct) { 436 if (e1->type->ty == Tpointer && e1->type->next->ty == Tstruct) {
437 assert(l->field); 437 //assert(l->field);
438 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); 438 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
439 assert(r->type == elem::CONST); 439 assert(r->type == elem::CONST);
440 llvm::ConstantInt* cofs = llvm::cast<llvm::ConstantInt>(r->val); 440 llvm::ConstantInt* cofs = llvm::cast<llvm::ConstantInt>(r->val);
441 441
442 TypeStruct* ts = (TypeStruct*)e1->type->next; 442 TypeStruct* ts = (TypeStruct*)e1->type->next;
443 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, ts->sym->offsetToIndex(cofs->getZExtValue()), false); 443 std::vector<unsigned> offsets(1,0);
444 444 ts->sym->offsetToIndex(cofs->getZExtValue(), offsets);
445 e->mem = LLVM_DtoGEP(l->getValue(), zero, offset, "tmp", p->scopebb()); 445 e->mem = LLVM_DtoGEP(l->getValue(), offsets, "tmp", p->scopebb());
446 e->type = elem::VAR; 446 e->type = elem::VAR;
447 e->field = true; 447 e->field = true;
448 } 448 }
449 else if (e1->type->ty == Tpointer) { 449 else if (e1->type->ty == Tpointer) {
450 e->val = new llvm::GetElementPtrInst(l->getValue(), r->getValue(), "tmp", p->scopebb()); 450 e->val = new llvm::GetElementPtrInst(l->getValue(), r->getValue(), "tmp", p->scopebb());
1124 { 1124 {
1125 Logger::println("VarDeclaration"); 1125 Logger::println("VarDeclaration");
1126 if (vd->type->ty == Tstruct && !(type->ty == Tpointer && type->next == vd->type)) { 1126 if (vd->type->ty == Tstruct && !(type->ty == Tpointer && type->next == vd->type)) {
1127 TypeStruct* vdt = (TypeStruct*)vd->type; 1127 TypeStruct* vdt = (TypeStruct*)vd->type;
1128 e = new elem; 1128 e = new elem;
1129 llvm::Value* idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); 1129 std::vector<unsigned> dst(1,0);
1130 llvm::Value* idx1 = llvm::ConstantInt::get(llvm::Type::Int32Ty, (uint64_t)vdt->sym->offsetToIndex(offset), false); 1130 vdt->sym->offsetToIndex(offset, dst);
1131 //const llvm::Type* _typ = llvm::GetElementPtrInst::getIndexedType(LLVM_DtoType(type), idx1);
1132 llvm::Value* ptr = vd->llvmValue; 1131 llvm::Value* ptr = vd->llvmValue;
1133 assert(ptr); 1132 assert(ptr);
1134 e->mem = LLVM_DtoGEP(ptr,idx0,idx1,"tmp",p->scopebb()); 1133 e->mem = LLVM_DtoGEP(ptr,dst,"tmp",p->scopebb());
1135 e->type = elem::VAL; 1134 e->type = elem::VAL;
1136 e->field = true; 1135 e->field = true;
1137 } 1136 }
1138 else if (vd->type->ty == Tsarray) { 1137 else if (vd->type->ty == Tsarray) {
1139 /*e = new elem; 1138 /*e = new elem;
1208 elem* l = e1->toElem(p); 1207 elem* l = e1->toElem(p);
1209 1208
1210 Logger::print("e1->type=%s\n", e1->type->toChars()); 1209 Logger::print("e1->type=%s\n", e1->type->toChars());
1211 1210
1212 if (VarDeclaration* vd = var->isVarDeclaration()) { 1211 if (VarDeclaration* vd = var->isVarDeclaration()) {
1213 size_t vdoffset = (size_t)-1; 1212 std::vector<unsigned> vdoffsets(1,0);
1214 llvm::Value* src = 0; 1213 llvm::Value* src = 0;
1215 if (e1->type->ty == Tpointer) { 1214 if (e1->type->ty == Tpointer) {
1216 assert(e1->type->next->ty == Tstruct); 1215 assert(e1->type->next->ty == Tstruct);
1217 TypeStruct* ts = (TypeStruct*)e1->type->next; 1216 TypeStruct* ts = (TypeStruct*)e1->type->next;
1218 vdoffset = ts->sym->offsetToIndex(vd->offset); 1217 ts->sym->offsetToIndex(vd->offset, vdoffsets);
1219 Logger::println("Struct member offset:%d index:%d", vd->offset, vdoffset); 1218 Logger::println("Struct member offset:%d", vd->offset);
1220 src = l->val; 1219 src = l->val;
1221 } 1220 }
1222 else if (e1->type->ty == Tclass) { 1221 else if (e1->type->ty == Tclass) {
1223 TypeClass* tc = (TypeClass*)e1->type; 1222 TypeClass* tc = (TypeClass*)e1->type;
1224 Logger::println("Class member offset: %d", vd->offset); 1223 Logger::println("Class member offset: %d", vd->offset);
1225 vdoffset = tc->sym->offsetToIndex(vd->offset); 1224 tc->sym->offsetToIndex(vd->offset, vdoffsets);
1226 src = l->getValue(); 1225 src = l->getValue();
1227 } 1226 }
1228 assert(vdoffset != (size_t)-1); 1227 assert(vdoffsets.size() != 1);
1229 assert(src != 0); 1228 assert(src != 0);
1230 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); 1229 llvm::Value* arrptr = LLVM_DtoGEP(src,vdoffsets,"tmp",p->scopebb());
1231 llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, vdoffset, false);
1232 llvm::Value* arrptr = LLVM_DtoGEP(src,zero,offset,"tmp",p->scopebb());
1233 e->mem = arrptr; 1230 e->mem = arrptr;
1234 Logger::cout() << "mem: " << *e->mem << '\n'; 1231 Logger::cout() << "mem: " << *e->mem << '\n';
1235 e->type = elem::VAR; 1232 e->type = elem::VAR;
1236 } 1233 }
1237 else if (FuncDeclaration* fdecl = var->isFuncDeclaration()) 1234 else if (FuncDeclaration* fdecl = var->isFuncDeclaration())
2586 LOG_SCOPE; 2583 LOG_SCOPE;
2587 2584
2588 llvm::BasicBlock* oldend = gIR->scopeend(); 2585 llvm::BasicBlock* oldend = gIR->scopeend();
2589 2586
2590 IRScope irs; 2587 IRScope irs;
2591 irs.begin = new llvm::BasicBlock("scope", gIR->topfunc(), oldend); 2588 // remove useless branches by clearing and reusing the current basicblock
2589 llvm::BasicBlock* bb = gIR->scopebegin();
2590 if (bb->empty()) {
2591 irs.begin = bb;
2592 }
2593 else {
2594 irs.begin = new llvm::BasicBlock("scope", gIR->topfunc(), oldend);
2595 new llvm::BranchInst(irs.begin, gIR->scopebegin());
2596 }
2592 irs.end = new llvm::BasicBlock("endscope", gIR->topfunc(), oldend); 2597 irs.end = new llvm::BasicBlock("endscope", gIR->topfunc(), oldend);
2593
2594 // pass the previous BB into this
2595 new llvm::BranchInst(irs.begin, gIR->scopebegin());
2596 2598
2597 gIR->scope() = irs; 2599 gIR->scope() = irs;
2598 2600
2599 statement->toIR(p); 2601 statement->toIR(p);
2600 if (!gIR->scopereturned()) { 2602 if (!gIR->scopereturned()) {