From 2a866251a80a8d88f6908f7dc68ce06d1023ec1d Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Sat, 25 Apr 2009 05:08:32 +0000 Subject: Fix pointer addressing and array subscripting of Objective-C interface types. - I broke this in the switch to representing interfaces with opaque types. - clang crashes on subscript of interface in 32-bit mode git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70009 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExprScalar.cpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'lib/CodeGen/CGExprScalar.cpp') diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index 308a45e433..5e0159e597 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -968,14 +968,27 @@ Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); } + const QualType ElementType = PT->getPointeeType(); + // Handle interface types, which are not represented with a concrete + // type. + if (const ObjCInterfaceType *OIT = dyn_cast(ElementType)) { + llvm::Value *InterfaceSize = + llvm::ConstantInt::get(Idx->getType(), + CGF.getContext().getTypeSize(OIT) / 8); + Idx = Builder.CreateMul(Idx, InterfaceSize); + const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); + Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); + Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); + return Builder.CreateBitCast(Res, Ptr->getType()); + } + // Explicitly handle GNU void* and function pointer arithmetic // extensions. The GNU void* casts amount to no-ops since our void* // type is i8*, but this is future proof. - const QualType ElementType = PT->getPointeeType(); if (ElementType->isVoidType() || ElementType->isFunctionType()) { const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); - Value *Res = Builder.CreateGEP(Casted, Idx, "sub.ptr"); + Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); return Builder.CreateBitCast(Res, Ptr->getType()); } @@ -1015,6 +1028,20 @@ Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { } Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); + // Handle interface types, which are not represented with a concrete + // type. + if (const ObjCInterfaceType *OIT = + dyn_cast(LHSElementType)) { + llvm::Value *InterfaceSize = + llvm::ConstantInt::get(Idx->getType(), + CGF.getContext().getTypeSize(OIT) / 8); + Idx = Builder.CreateMul(Idx, InterfaceSize); + const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); + Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); + Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr"); + return Builder.CreateBitCast(Res, Ops.LHS->getType()); + } + // Explicitly handle GNU void* and function pointer arithmetic // extensions. The GNU void* casts amount to no-ops since our // void* type is i8*, but this is future proof. -- cgit v1.2.3