Mercurial > projects > ldc
diff gen/structs.cpp @ 705:5a2983f97498
Fixed weird struct problem from downs, see mini/compile_structs1.d
Rewrote DtoIndexStruct/Class , the old implementation were way too complex for what we really need now - since the DotVar changes.
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Tue, 14 Oct 2008 15:35:49 +0200 |
parents | a15ccbf7451d |
children | 3e143b611c1e |
line wrap: on
line diff
--- a/gen/structs.cpp Tue Oct 14 13:21:14 2008 +0200 +++ b/gen/structs.cpp Tue Oct 14 15:35:49 2008 +0200 @@ -52,73 +52,31 @@ ////////////////////////////////////////////////////////////////////////////////////////// -LLValue* DtoIndexStruct(LLValue* ptr, StructDeclaration* sd, Type* t, unsigned os, DStructIndexVector& idxs) +LLValue* DtoIndexStruct(LLValue* src, StructDeclaration* sd, VarDeclaration* vd) { - Logger::println("checking for offset %u type %s:", os, t->toChars()); + Logger::println("indexing struct field %s:", vd->toPrettyChars()); LOG_SCOPE; - if (idxs.empty()) - idxs.push_back(0); + // vd must be a field + IrField* field = vd->ir.irField; + assert(field); + + unsigned idx = field->index; + unsigned off = field->indexOffset; - const LLType* llt = getPtrToType(DtoType(t)); const LLType* st = getPtrToType(DtoType(sd->type)); + src = DtoBitCast(src, st); + + LLValue* val = DtoGEPi(src, 0,idx); + val = DtoBitCast(val, getPtrToType(DtoType(vd->type))); + + if (off) + val = DtoGEPi1(val, off); if (Logger::enabled()) - { - Logger::cout() << "ptr = " << *ptr << '\n'; - Logger::cout() << "st = " << *st << '\n'; - } - - if (ptr->getType() != st) { - assert(sd->ir.irStruct->hasUnions); - ptr = gIR->ir->CreateBitCast(ptr, st, "tmp"); - } + Logger::cout() << "value: " << *val << '\n'; - for (unsigned i=0; i<sd->fields.dim; ++i) { - VarDeclaration* vd = (VarDeclaration*)sd->fields.data[i]; - Type* vdtype = vd->type->toBasetype(); - //Logger::println("found %u type %s", vd->offset, vdtype->toChars()); - assert(vd->ir.irField->index >= 0); - if (os == vd->offset && vdtype == t) { - idxs.push_back(vd->ir.irField->index); - ptr = DtoGEPi(ptr, idxs, "tmp"); - if (ptr->getType() != llt) - ptr = gIR->ir->CreateBitCast(ptr, llt, "tmp"); - if (vd->ir.irField->indexOffset) - ptr = llvm::GetElementPtrInst::Create(ptr, DtoConstUint(vd->ir.irField->indexOffset), "tmp", gIR->scopebb()); - return ptr; - } - else if (vdtype->ty == Tstruct && (vd->offset + vdtype->size()) > os) { - TypeStruct* ts = (TypeStruct*)vdtype; - StructDeclaration* ssd = ts->sym; - idxs.push_back(vd->ir.irField->index); - if (vd->ir.irField->indexOffset) { - Logger::println("has union field offset"); - ptr = DtoGEPi(ptr, idxs, "tmp"); - if (ptr->getType() != llt) - ptr = DtoBitCast(ptr, llt); - ptr = llvm::GetElementPtrInst::Create(ptr, DtoConstUint(vd->ir.irField->indexOffset), "tmp", gIR->scopebb()); - DStructIndexVector tmp; - return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp); - } - else { - const LLType* sty = getPtrToType(DtoType(vd->type)); - if (ptr->getType() != sty) { - ptr = DtoBitCast(ptr, sty); - DStructIndexVector tmp; - return DtoIndexStruct(ptr, ssd, t, os-vd->offset, tmp); - } - else { - return DtoIndexStruct(ptr, ssd, t, os-vd->offset, idxs); - } - } - } - } - - size_t llt_sz = getTypeStoreSize(llt->getContainedType(0)); - assert(os % llt_sz == 0); - ptr = DtoBitCast(ptr, llt); - return llvm::GetElementPtrInst::Create(ptr, DtoConstUint(os / llt_sz), "tmp", gIR->scopebb()); + return val; } //////////////////////////////////////////////////////////////////////////////////////////