2007-06-02 02:02:12 +08:00
|
|
|
//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-02 02:02:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2007-06-20 12:44:43 +08:00
|
|
|
#include "CodeGenModule.h"
|
2008-09-09 05:33:45 +08:00
|
|
|
#include "CGCall.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-05-18 04:03:47 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-06-02 02:02:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-06-03 03:33:17 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Miscellaneous Helper Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-06-23 05:44:33 +08:00
|
|
|
/// CreateTempAlloca - This creates a alloca and inserts it into the entry
|
|
|
|
/// block.
|
|
|
|
llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
|
|
|
|
const char *Name) {
|
2009-03-22 08:24:14 +08:00
|
|
|
if (!Builder.isNamePreserving())
|
|
|
|
Name = "";
|
2009-07-16 08:14:12 +08:00
|
|
|
return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
|
2007-06-23 05:44:33 +08:00
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
|
|
|
|
/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
|
|
|
|
/// expression and compare the result against zero, returning an Int1Ty value.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
|
2007-08-27 00:46:58 +08:00
|
|
|
QualType BoolTy = getContext().BoolTy;
|
2008-04-05 00:54:41 +08:00
|
|
|
if (!E->getType()->isAnyComplexType())
|
2007-08-27 00:46:58 +08:00
|
|
|
return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-08-27 00:46:58 +08:00
|
|
|
return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
|
2007-06-03 03:33:17 +08:00
|
|
|
}
|
|
|
|
|
2007-09-01 06:49:20 +08:00
|
|
|
/// EmitAnyExpr - Emit code to compute the specified expression which can have
|
|
|
|
/// any type. The result is returned as an RValue struct. If this is an
|
|
|
|
/// aggregate expression, the aggloc/agglocvolatile arguments indicate where
|
|
|
|
/// the result should be returned.
|
|
|
|
RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
|
2009-08-16 15:36:22 +08:00
|
|
|
bool IsAggLocVolatile, bool IgnoreResult,
|
|
|
|
bool IsInitializer) {
|
2007-09-01 06:49:20 +08:00
|
|
|
if (!hasAggregateLLVMType(E->getType()))
|
2009-05-29 23:46:01 +08:00
|
|
|
return RValue::get(EmitScalarExpr(E, IgnoreResult));
|
2008-04-05 00:54:41 +08:00
|
|
|
else if (E->getType()->isAnyComplexType())
|
2009-05-29 23:46:01 +08:00
|
|
|
return RValue::getComplex(EmitComplexExpr(E, false, false,
|
|
|
|
IgnoreResult, IgnoreResult));
|
2007-09-01 06:49:20 +08:00
|
|
|
|
2009-08-16 15:36:22 +08:00
|
|
|
EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
|
|
|
|
return RValue::getAggregate(AggLoc, IsAggLocVolatile);
|
2007-09-01 06:49:20 +08:00
|
|
|
}
|
|
|
|
|
2008-09-09 09:06:48 +08:00
|
|
|
/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result
|
|
|
|
/// will always be accessible even if no aggregate location is
|
|
|
|
/// provided.
|
2009-08-16 15:36:22 +08:00
|
|
|
RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
|
|
|
|
bool IsAggLocVolatile,
|
|
|
|
bool IsInitializer) {
|
|
|
|
llvm::Value *AggLoc = 0;
|
|
|
|
|
|
|
|
if (hasAggregateLLVMType(E->getType()) &&
|
2008-09-09 09:06:48 +08:00
|
|
|
!E->getType()->isAnyComplexType())
|
|
|
|
AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
|
2009-08-16 15:36:22 +08:00
|
|
|
return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
|
|
|
|
IsInitializer);
|
2008-09-09 09:06:48 +08:00
|
|
|
}
|
|
|
|
|
2009-05-20 08:24:07 +08:00
|
|
|
RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
|
2009-08-16 15:36:22 +08:00
|
|
|
QualType DestType,
|
|
|
|
bool IsInitializer) {
|
2009-05-20 10:31:19 +08:00
|
|
|
RValue Val;
|
|
|
|
if (E->isLvalue(getContext()) == Expr::LV_Valid) {
|
2009-05-20 08:36:58 +08:00
|
|
|
// Emit the expr as an lvalue.
|
|
|
|
LValue LV = EmitLValue(E);
|
2009-05-20 10:31:19 +08:00
|
|
|
if (LV.isSimple())
|
|
|
|
return RValue::get(LV.getAddress());
|
|
|
|
Val = EmitLoadOfLValue(LV, E->getType());
|
|
|
|
} else {
|
2009-08-17 01:50:25 +08:00
|
|
|
// FIXME: Initializers don't work with casts yet. For example
|
|
|
|
// const A& a = B();
|
|
|
|
// if B inherits from A.
|
2009-08-16 15:36:22 +08:00
|
|
|
Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
|
|
|
|
IsInitializer);
|
2009-08-17 01:50:25 +08:00
|
|
|
|
2009-08-17 01:54:29 +08:00
|
|
|
if (IsInitializer) {
|
|
|
|
// We might have to destroy the temporary variable.
|
|
|
|
if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
|
|
|
|
if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
|
|
|
|
if (!ClassDecl->hasTrivialDestructor()) {
|
|
|
|
const CXXDestructorDecl *Dtor =
|
|
|
|
ClassDecl->getDestructor(getContext());
|
2009-08-17 01:50:25 +08:00
|
|
|
|
2009-08-17 01:54:29 +08:00
|
|
|
CleanupScope scope(*this);
|
|
|
|
EmitCXXDestructorCall(Dtor, Dtor_Complete, Val.getAggregateAddr());
|
|
|
|
}
|
2009-08-17 01:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-20 08:36:58 +08:00
|
|
|
}
|
2009-05-20 10:31:19 +08:00
|
|
|
|
|
|
|
if (Val.isAggregate()) {
|
|
|
|
Val = RValue::get(Val.getAggregateAddr());
|
|
|
|
} else {
|
2009-05-20 09:35:03 +08:00
|
|
|
// Create a temporary variable that we can bind the reference to.
|
|
|
|
llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
|
|
|
|
"reftmp");
|
2009-05-20 10:31:19 +08:00
|
|
|
if (Val.isScalar())
|
|
|
|
EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
|
|
|
|
else
|
|
|
|
StoreComplexToAddr(Val.getComplexVal(), Temp, false);
|
|
|
|
Val = RValue::get(Temp);
|
2009-05-20 09:03:17 +08:00
|
|
|
}
|
2009-05-20 10:31:19 +08:00
|
|
|
|
|
|
|
return Val;
|
2009-05-20 08:24:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-22 08:50:06 +08:00
|
|
|
/// getAccessedFieldNo - Given an encoded value and a result number, return
|
|
|
|
/// the input field number being accessed.
|
|
|
|
unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
|
|
|
|
const llvm::Constant *Elts) {
|
|
|
|
if (isa<llvm::ConstantAggregateZero>(Elts))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
|
|
|
|
}
|
|
|
|
|
2007-09-01 06:49:20 +08:00
|
|
|
|
2007-06-03 03:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-02 13:24:33 +08:00
|
|
|
// LValue Expression Emission
|
2007-06-03 03:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-02 13:24:33 +08:00
|
|
|
|
2009-02-05 15:09:07 +08:00
|
|
|
RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
|
|
|
|
if (Ty->isVoidType()) {
|
|
|
|
return RValue::get(0);
|
|
|
|
} else if (const ComplexType *CTy = Ty->getAsComplexType()) {
|
2009-01-10 04:09:28 +08:00
|
|
|
const llvm::Type *EltTy = ConvertType(CTy->getElementType());
|
2009-07-31 07:11:26 +08:00
|
|
|
llvm::Value *U = llvm::UndefValue::get(EltTy);
|
2009-01-10 04:09:28 +08:00
|
|
|
return RValue::getComplex(std::make_pair(U, U));
|
2009-02-05 15:09:07 +08:00
|
|
|
} else if (hasAggregateLLVMType(Ty)) {
|
2009-07-30 06:16:19 +08:00
|
|
|
const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
|
2009-07-31 07:11:26 +08:00
|
|
|
return RValue::getAggregate(llvm::UndefValue::get(LTy));
|
2009-01-10 04:09:28 +08:00
|
|
|
} else {
|
2009-07-31 07:11:26 +08:00
|
|
|
return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
|
2009-01-10 04:09:28 +08:00
|
|
|
}
|
2009-01-10 00:50:52 +08:00
|
|
|
}
|
|
|
|
|
2009-02-05 15:09:07 +08:00
|
|
|
RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
|
|
|
|
const char *Name) {
|
|
|
|
ErrorUnsupported(E, Name);
|
|
|
|
return GetUndefRValue(E->getType());
|
|
|
|
}
|
|
|
|
|
2008-08-26 04:45:57 +08:00
|
|
|
LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
|
|
|
|
const char *Name) {
|
|
|
|
ErrorUnsupported(E, Name);
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
|
2009-07-31 07:11:26 +08:00
|
|
|
return LValue::MakeAddr(llvm::UndefValue::get(Ty),
|
2009-02-20 07:36:06 +08:00
|
|
|
E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2008-08-26 04:45:57 +08:00
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EmitLValue - Emit code to compute a designator that specifies the location
|
|
|
|
/// of the expression.
|
|
|
|
///
|
|
|
|
/// This can return one of two things: a simple address or a bitfield
|
|
|
|
/// reference. In either case, the LLVM Value* in the LValue structure is
|
|
|
|
/// guaranteed to be an LLVM pointer type.
|
|
|
|
///
|
|
|
|
/// If this returns a bitfield reference, nothing about the pointee type of
|
|
|
|
/// the LLVM value is known: For example, it may not be a pointer to an
|
|
|
|
/// integer.
|
|
|
|
///
|
|
|
|
/// If this returns a normal address, and if the lvalue's C type is fixed
|
|
|
|
/// size, this method guarantees that the returned pointer type will point to
|
|
|
|
/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
|
|
|
|
/// variable length type, this is not possible.
|
|
|
|
///
|
2007-06-02 13:24:33 +08:00
|
|
|
LValue CodeGenFunction::EmitLValue(const Expr *E) {
|
|
|
|
switch (E->getStmtClass()) {
|
2008-08-26 04:45:57 +08:00
|
|
|
default: return EmitUnsupportedLValue(E, "l-value expression");
|
2007-06-02 13:24:33 +08:00
|
|
|
|
2008-09-04 11:20:13 +08:00
|
|
|
case Expr::BinaryOperatorClass:
|
|
|
|
return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
|
2008-11-15 00:09:21 +08:00
|
|
|
case Expr::CallExprClass:
|
|
|
|
case Expr::CXXOperatorCallExprClass:
|
|
|
|
return EmitCallExprLValue(cast<CallExpr>(E));
|
2009-02-12 04:59:32 +08:00
|
|
|
case Expr::VAArgExprClass:
|
|
|
|
return EmitVAArgExprLValue(cast<VAArgExpr>(E));
|
2009-01-06 13:10:23 +08:00
|
|
|
case Expr::DeclRefExprClass:
|
|
|
|
case Expr::QualifiedDeclRefExprClass:
|
|
|
|
return EmitDeclRefLValue(cast<DeclRefExpr>(E));
|
2007-06-05 11:59:43 +08:00
|
|
|
case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
|
2008-08-10 09:53:14 +08:00
|
|
|
case Expr::PredefinedExprClass:
|
|
|
|
return EmitPredefinedLValue(cast<PredefinedExpr>(E));
|
2007-06-06 12:54:52 +08:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return EmitStringLiteralLValue(cast<StringLiteral>(E));
|
2009-02-25 06:18:39 +08:00
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
|
2008-03-31 07:03:07 +08:00
|
|
|
|
2009-02-28 17:07:16 +08:00
|
|
|
case Expr::BlockDeclRefExprClass:
|
|
|
|
return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
|
|
|
|
|
2008-09-10 10:36:38 +08:00
|
|
|
case Expr::CXXConditionDeclExprClass:
|
|
|
|
return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
|
2009-05-31 07:23:33 +08:00
|
|
|
case Expr::CXXTemporaryObjectExprClass:
|
|
|
|
case Expr::CXXConstructExprClass:
|
2009-05-31 07:30:54 +08:00
|
|
|
return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
|
|
|
|
case Expr::CXXBindTemporaryExprClass:
|
|
|
|
return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
|
|
|
|
|
2008-08-23 18:51:21 +08:00
|
|
|
case Expr::ObjCMessageExprClass:
|
|
|
|
return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
|
2008-03-31 07:03:07 +08:00
|
|
|
case Expr::ObjCIvarRefExprClass:
|
|
|
|
return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
|
2008-08-26 04:45:57 +08:00
|
|
|
case Expr::ObjCPropertyRefExprClass:
|
2008-08-29 16:11:39 +08:00
|
|
|
return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
|
2008-11-23 06:30:21 +08:00
|
|
|
case Expr::ObjCKVCRefExprClass:
|
|
|
|
return EmitObjCKVCRefLValue(cast<ObjCKVCRefExpr>(E));
|
2008-11-04 22:56:14 +08:00
|
|
|
case Expr::ObjCSuperExprClass:
|
2009-04-26 03:35:26 +08:00
|
|
|
return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
|
2008-11-04 22:56:14 +08:00
|
|
|
|
2009-04-26 03:35:26 +08:00
|
|
|
case Expr::StmtExprClass:
|
|
|
|
return EmitStmtExprLValue(cast<StmtExpr>(E));
|
2007-06-06 04:53:16 +08:00
|
|
|
case Expr::UnaryOperatorClass:
|
|
|
|
return EmitUnaryOpLValue(cast<UnaryOperator>(E));
|
2007-06-09 07:31:14 +08:00
|
|
|
case Expr::ArraySubscriptExprClass:
|
|
|
|
return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
|
2008-04-19 07:10:10 +08:00
|
|
|
case Expr::ExtVectorElementExprClass:
|
|
|
|
return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
|
2007-10-24 04:28:39 +08:00
|
|
|
case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
|
2008-05-14 07:18:27 +08:00
|
|
|
case Expr::CompoundLiteralExprClass:
|
|
|
|
return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
|
2009-03-24 10:38:23 +08:00
|
|
|
case Expr::ConditionalOperatorClass:
|
|
|
|
return EmitConditionalOperator(cast<ConditionalOperator>(E));
|
2008-12-12 13:35:08 +08:00
|
|
|
case Expr::ChooseExprClass:
|
2009-03-04 13:52:32 +08:00
|
|
|
return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
|
2009-03-18 12:02:57 +08:00
|
|
|
case Expr::ImplicitCastExprClass:
|
|
|
|
case Expr::CStyleCastExprClass:
|
|
|
|
case Expr::CXXFunctionalCastExprClass:
|
|
|
|
case Expr::CXXStaticCastExprClass:
|
|
|
|
case Expr::CXXDynamicCastExprClass:
|
|
|
|
case Expr::CXXReinterpretCastExprClass:
|
|
|
|
case Expr::CXXConstCastExprClass:
|
2009-03-19 02:28:57 +08:00
|
|
|
return EmitCastLValue(cast<CastExpr>(E));
|
2007-06-02 13:24:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-10 08:57:50 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
|
|
|
|
QualType Ty) {
|
|
|
|
llvm::Value *V = Builder.CreateLoad(Addr, Volatile, "tmp");
|
|
|
|
|
2009-05-20 03:36:19 +08:00
|
|
|
// Bool can have different representation in memory than in registers.
|
2009-02-10 08:57:50 +08:00
|
|
|
if (Ty->isBooleanType())
|
2009-08-14 05:57:51 +08:00
|
|
|
if (V->getType() != llvm::Type::getInt1Ty(VMContext))
|
|
|
|
V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
|
2009-02-10 08:57:50 +08:00
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
|
2009-05-20 02:50:41 +08:00
|
|
|
bool Volatile, QualType Ty) {
|
2009-05-20 03:36:19 +08:00
|
|
|
|
|
|
|
if (Ty->isBooleanType()) {
|
|
|
|
// Bool can have different representation in memory than in registers.
|
|
|
|
const llvm::Type *SrcTy = Value->getType();
|
|
|
|
const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
|
|
|
|
if (DstPtr->getElementType() != SrcTy) {
|
|
|
|
const llvm::Type *MemTy =
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
|
2009-05-20 03:36:19 +08:00
|
|
|
Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
|
|
|
|
}
|
2009-02-10 08:57:50 +08:00
|
|
|
}
|
|
|
|
Builder.CreateStore(Value, Addr, Volatile);
|
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
|
|
|
|
/// this method emits the address of the lvalue, then loads the result as an
|
|
|
|
/// rvalue, returning the rvalue.
|
2007-06-30 00:31:29 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
|
2008-11-20 01:34:06 +08:00
|
|
|
if (LV.isObjCWeak()) {
|
2008-11-19 05:45:40 +08:00
|
|
|
// load of a __weak object.
|
|
|
|
llvm::Value *AddrWeakObj = LV.getAddress();
|
2008-11-19 06:37:34 +08:00
|
|
|
llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
|
2008-11-19 05:45:40 +08:00
|
|
|
AddrWeakObj);
|
|
|
|
return RValue::get(read_weak);
|
|
|
|
}
|
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
if (LV.isSimple()) {
|
|
|
|
llvm::Value *Ptr = LV.getAddress();
|
|
|
|
const llvm::Type *EltTy =
|
|
|
|
cast<llvm::PointerType>(Ptr->getType())->getElementType();
|
|
|
|
|
|
|
|
// Simple scalar l-value.
|
2009-02-10 08:57:50 +08:00
|
|
|
if (EltTy->isSingleValueType())
|
|
|
|
return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
|
|
|
|
ExprType));
|
2007-07-11 05:17:59 +08:00
|
|
|
|
2007-08-11 08:04:45 +08:00
|
|
|
assert(ExprType->isFunctionType() && "Unknown scalar value");
|
|
|
|
return RValue::get(Ptr);
|
2007-07-11 05:17:59 +08:00
|
|
|
}
|
2007-06-23 02:48:09 +08:00
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
if (LV.isVectorElt()) {
|
2008-06-14 07:01:12 +08:00
|
|
|
llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
|
|
|
|
LV.isVolatileQualified(), "tmp");
|
2007-07-11 05:17:59 +08:00
|
|
|
return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
|
|
|
|
"vecext"));
|
|
|
|
}
|
implement lvalue to rvalue conversion for ocuvector components. We can now compile stuff
like this:
typedef __attribute__(( ocu_vector_type(4) )) float float4;
float4 test1(float4 V) {
return V.wzyx+V;
}
to:
_test1:
pshufd $27, %xmm0, %xmm1
addps %xmm0, %xmm1
movaps %xmm1, %xmm0
ret
and:
_test1:
mfspr r2, 256
oris r3, r2, 4096
mtspr 256, r3
li r3, lo16(LCPI1_0)
lis r4, ha16(LCPI1_0)
lvx v3, r4, r3
vperm v3, v2, v2, v3
vaddfp v2, v3, v2
mtspr 256, r2
blr
llvm-svn: 40771
2007-08-03 08:16:29 +08:00
|
|
|
|
|
|
|
// If this is a reference to a subset of the elements of a vector, either
|
|
|
|
// shuffle the input or extract/insert them as appropriate.
|
2008-04-19 07:10:10 +08:00
|
|
|
if (LV.isExtVectorElt())
|
|
|
|
return EmitLoadOfExtVectorElementLValue(LV, ExprType);
|
2008-01-23 04:17:04 +08:00
|
|
|
|
|
|
|
if (LV.isBitfield())
|
|
|
|
return EmitLoadOfBitfieldLValue(LV, ExprType);
|
|
|
|
|
2008-08-29 16:11:39 +08:00
|
|
|
if (LV.isPropertyRef())
|
|
|
|
return EmitLoadOfPropertyRefLValue(LV, ExprType);
|
|
|
|
|
2009-02-17 05:11:58 +08:00
|
|
|
assert(LV.isKVCRef() && "Unknown LValue type!");
|
|
|
|
return EmitLoadOfKVCRefLValue(LV, ExprType);
|
2007-08-04 00:18:34 +08:00
|
|
|
}
|
2007-08-03 23:52:31 +08:00
|
|
|
|
2008-01-23 04:17:04 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
|
|
|
|
QualType ExprType) {
|
2008-08-06 13:08:45 +08:00
|
|
|
unsigned StartBit = LV.getBitfieldStartBit();
|
|
|
|
unsigned BitfieldSize = LV.getBitfieldSize();
|
2008-01-23 04:17:04 +08:00
|
|
|
llvm::Value *Ptr = LV.getBitfieldAddr();
|
|
|
|
|
2008-08-06 13:08:45 +08:00
|
|
|
const llvm::Type *EltTy =
|
|
|
|
cast<llvm::PointerType>(Ptr->getType())->getElementType();
|
|
|
|
unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
|
|
|
|
|
|
|
|
// In some cases the bitfield may straddle two memory locations.
|
|
|
|
// Currently we load the entire bitfield, then do the magic to
|
|
|
|
// sign-extend it if necessary. This results in somewhat more code
|
|
|
|
// than necessary for the common case (one load), since two shifts
|
|
|
|
// accomplish both the masking and sign extension.
|
|
|
|
unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
|
|
|
|
llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
|
|
|
|
|
|
|
|
// Shift to proper location.
|
2008-11-13 10:20:34 +08:00
|
|
|
if (StartBit)
|
2009-07-25 07:12:58 +08:00
|
|
|
Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
|
2008-11-13 10:20:34 +08:00
|
|
|
"bf.lo");
|
2008-08-06 13:08:45 +08:00
|
|
|
|
|
|
|
// Mask off unused bits.
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
|
|
|
|
llvm::APInt::getLowBitsSet(EltTySize, LowBits));
|
2008-08-06 13:08:45 +08:00
|
|
|
Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
|
|
|
|
|
|
|
|
// Fetch the high bits if necessary.
|
|
|
|
if (LowBits < BitfieldSize) {
|
|
|
|
unsigned HighBits = BitfieldSize - LowBits;
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
|
2008-08-06 13:08:45 +08:00
|
|
|
llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
|
|
|
|
LV.isVolatileQualified(),
|
|
|
|
"tmp");
|
|
|
|
|
|
|
|
// Mask off unused bits.
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
|
|
|
|
llvm::APInt::getLowBitsSet(EltTySize, HighBits));
|
2008-08-06 13:08:45 +08:00
|
|
|
HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
|
|
|
|
|
|
|
|
// Shift to proper location and or in to bitfield value.
|
|
|
|
HighVal = Builder.CreateShl(HighVal,
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(EltTy, LowBits));
|
2008-08-06 13:08:45 +08:00
|
|
|
Val = Builder.CreateOr(Val, HighVal, "bf.val");
|
|
|
|
}
|
2008-01-23 04:17:04 +08:00
|
|
|
|
2008-08-06 13:08:45 +08:00
|
|
|
// Sign extend if necessary.
|
|
|
|
if (LV.isBitfieldSigned()) {
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
|
2008-08-06 13:08:45 +08:00
|
|
|
EltTySize - BitfieldSize);
|
|
|
|
Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
|
|
|
|
ExtraBits, "bf.val.sext");
|
|
|
|
}
|
2008-05-18 04:03:47 +08:00
|
|
|
|
|
|
|
// The bitfield type and the normal type differ when the storage sizes
|
|
|
|
// differ (currently just _Bool).
|
2008-08-06 13:08:45 +08:00
|
|
|
Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
|
2008-05-18 04:03:47 +08:00
|
|
|
|
2008-08-06 13:08:45 +08:00
|
|
|
return RValue::get(Val);
|
2008-01-23 04:17:04 +08:00
|
|
|
}
|
|
|
|
|
2008-08-29 16:11:39 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
|
|
|
|
QualType ExprType) {
|
|
|
|
return EmitObjCPropertyGet(LV.getPropertyRefExpr());
|
|
|
|
}
|
|
|
|
|
2008-11-23 06:30:21 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
|
|
|
|
QualType ExprType) {
|
|
|
|
return EmitObjCPropertyGet(LV.getKVCRefExpr());
|
|
|
|
}
|
|
|
|
|
2009-01-18 14:42:49 +08:00
|
|
|
// If this is a reference to a subset of the elements of a vector, create an
|
|
|
|
// appropriate shufflevector.
|
2008-04-19 07:10:10 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
|
|
|
|
QualType ExprType) {
|
2008-06-14 07:01:12 +08:00
|
|
|
llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
|
|
|
|
LV.isVolatileQualified(), "tmp");
|
2007-08-04 00:18:34 +08:00
|
|
|
|
2008-05-09 14:41:27 +08:00
|
|
|
const llvm::Constant *Elts = LV.getExtVectorElts();
|
2007-08-04 00:18:34 +08:00
|
|
|
|
|
|
|
// If the result of the expression is a non-vector type, we must be
|
|
|
|
// extracting a single element. Just codegen as an extractelement.
|
2007-08-11 01:10:08 +08:00
|
|
|
const VectorType *ExprVT = ExprType->getAsVectorType();
|
|
|
|
if (!ExprVT) {
|
2008-05-22 08:50:06 +08:00
|
|
|
unsigned InIdx = getAccessedFieldNo(0, Elts);
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Value *Elt = llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), InIdx);
|
2007-08-04 00:18:34 +08:00
|
|
|
return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
|
|
|
|
}
|
2009-01-18 14:42:49 +08:00
|
|
|
|
|
|
|
// Always use shuffle vector to try to retain the original program structure
|
2007-08-11 01:10:08 +08:00
|
|
|
unsigned NumResultElts = ExprVT->getNumElements();
|
2007-08-04 00:18:34 +08:00
|
|
|
|
2009-01-18 14:42:49 +08:00
|
|
|
llvm::SmallVector<llvm::Constant*, 4> Mask;
|
2007-08-04 00:18:34 +08:00
|
|
|
for (unsigned i = 0; i != NumResultElts; ++i) {
|
2008-05-22 08:50:06 +08:00
|
|
|
unsigned InIdx = getAccessedFieldNo(i, Elts);
|
2009-08-14 05:57:51 +08:00
|
|
|
Mask.push_back(llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), InIdx));
|
2007-08-04 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
2009-07-29 05:22:35 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
|
2009-01-18 14:42:49 +08:00
|
|
|
Vec = Builder.CreateShuffleVector(Vec,
|
2009-07-31 07:11:26 +08:00
|
|
|
llvm::UndefValue::get(Vec->getType()),
|
2009-01-18 14:42:49 +08:00
|
|
|
MaskV, "tmp");
|
|
|
|
return RValue::get(Vec);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-08-04 00:18:34 +08:00
|
|
|
|
2007-06-30 00:31:29 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EmitStoreThroughLValue - Store the specified rvalue into the specified
|
|
|
|
/// lvalue, where both are guaranteed to the have the same type, and that type
|
|
|
|
/// is 'Ty'.
|
|
|
|
void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
|
|
|
|
QualType Ty) {
|
2007-08-04 00:28:33 +08:00
|
|
|
if (!Dst.isSimple()) {
|
|
|
|
if (Dst.isVectorElt()) {
|
|
|
|
// Read/modify/write the vector, inserting the new element.
|
2008-06-14 07:01:12 +08:00
|
|
|
llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
|
|
|
|
Dst.isVolatileQualified(), "tmp");
|
2007-09-01 06:49:20 +08:00
|
|
|
Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
|
2007-08-04 00:28:33 +08:00
|
|
|
Dst.getVectorIdx(), "vecins");
|
2008-06-14 07:01:12 +08:00
|
|
|
Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
|
2007-08-04 00:28:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-19 07:10:10 +08:00
|
|
|
// If this is an update of extended vector elements, insert them as
|
|
|
|
// appropriate.
|
|
|
|
if (Dst.isExtVectorElt())
|
|
|
|
return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
|
2008-01-23 06:36:45 +08:00
|
|
|
|
|
|
|
if (Dst.isBitfield())
|
|
|
|
return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
|
|
|
|
|
2008-08-29 16:11:39 +08:00
|
|
|
if (Dst.isPropertyRef())
|
|
|
|
return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
|
|
|
|
|
2008-11-23 06:30:21 +08:00
|
|
|
if (Dst.isKVCRef())
|
|
|
|
return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
|
|
|
|
|
2008-01-23 06:38:35 +08:00
|
|
|
assert(0 && "Unknown LValue type");
|
2007-08-04 00:28:33 +08:00
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2009-02-21 08:30:43 +08:00
|
|
|
if (Dst.isObjCWeak() && !Dst.isNonGC()) {
|
2008-11-20 01:34:06 +08:00
|
|
|
// load of a __weak object.
|
|
|
|
llvm::Value *LvalueDst = Dst.getAddress();
|
|
|
|
llvm::Value *src = Src.getScalarVal();
|
2009-04-14 08:57:29 +08:00
|
|
|
CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
|
2008-11-20 01:34:06 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-02-21 08:30:43 +08:00
|
|
|
if (Dst.isObjCStrong() && !Dst.isNonGC()) {
|
2008-11-20 01:34:06 +08:00
|
|
|
// load of a __strong object.
|
|
|
|
llvm::Value *LvalueDst = Dst.getAddress();
|
|
|
|
llvm::Value *src = Src.getScalarVal();
|
2009-02-20 02:29:24 +08:00
|
|
|
#if 0
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME. We cannot positively determine if we have an 'ivar' assignment,
|
|
|
|
// object assignment or an unknown assignment. For now, generate call to
|
|
|
|
// objc_assign_strongCast assignment which is a safe, but consevative
|
|
|
|
// assumption.
|
2008-11-21 04:53:20 +08:00
|
|
|
if (Dst.isObjCIvar())
|
|
|
|
CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst);
|
|
|
|
else
|
|
|
|
CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
|
2009-02-20 02:29:24 +08:00
|
|
|
#endif
|
2009-05-05 07:27:20 +08:00
|
|
|
if (Dst.isGlobalObjCRef())
|
|
|
|
CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
|
|
|
|
else
|
|
|
|
CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
|
2008-11-20 01:34:06 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-11 08:04:45 +08:00
|
|
|
assert(Src.isScalar() && "Can't emit an agg store with this method");
|
2009-05-20 02:50:41 +08:00
|
|
|
EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
|
|
|
|
Dst.isVolatileQualified(), Ty);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2008-01-23 06:36:45 +08:00
|
|
|
void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
|
2008-11-19 17:36:46 +08:00
|
|
|
QualType Ty,
|
|
|
|
llvm::Value **Result) {
|
2008-08-06 13:08:45 +08:00
|
|
|
unsigned StartBit = Dst.getBitfieldStartBit();
|
|
|
|
unsigned BitfieldSize = Dst.getBitfieldSize();
|
2008-01-23 06:36:45 +08:00
|
|
|
llvm::Value *Ptr = Dst.getBitfieldAddr();
|
|
|
|
|
2008-08-06 13:08:45 +08:00
|
|
|
const llvm::Type *EltTy =
|
|
|
|
cast<llvm::PointerType>(Ptr->getType())->getElementType();
|
|
|
|
unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
|
2008-01-23 06:36:45 +08:00
|
|
|
|
2008-08-06 13:08:45 +08:00
|
|
|
// Get the new value, cast to the appropriate type and masked to
|
|
|
|
// exactly the size of the bit-field.
|
2008-11-19 17:36:46 +08:00
|
|
|
llvm::Value *SrcVal = Src.getScalarVal();
|
|
|
|
llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
|
|
|
|
llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
|
2008-08-06 13:08:45 +08:00
|
|
|
NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
|
|
|
|
|
2008-11-19 17:36:46 +08:00
|
|
|
// Return the new value of the bit-field, if requested.
|
|
|
|
if (Result) {
|
|
|
|
// Cast back to the proper type for result.
|
|
|
|
const llvm::Type *SrcTy = SrcVal->getType();
|
|
|
|
llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
|
|
|
|
"bf.reload.val");
|
|
|
|
|
|
|
|
// Sign extend if necessary.
|
|
|
|
if (Dst.isBitfieldSigned()) {
|
|
|
|
unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
|
2008-11-19 17:36:46 +08:00
|
|
|
SrcTySize - BitfieldSize);
|
|
|
|
SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
|
|
|
|
ExtraBits, "bf.reload.sext");
|
|
|
|
}
|
|
|
|
|
|
|
|
*Result = SrcTrunc;
|
|
|
|
}
|
|
|
|
|
2008-08-06 13:08:45 +08:00
|
|
|
// In some cases the bitfield may straddle two memory locations.
|
|
|
|
// Emit the low part first and check to see if the high needs to be
|
|
|
|
// done.
|
|
|
|
unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
|
|
|
|
llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
|
|
|
|
"bf.prev.low");
|
|
|
|
|
|
|
|
// Compute the mask for zero-ing the low part of this bitfield.
|
|
|
|
llvm::Constant *InvMask =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(VMContext,
|
|
|
|
~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
|
2008-08-06 13:08:45 +08:00
|
|
|
|
|
|
|
// Compute the new low part as
|
|
|
|
// LowVal = (LowVal & InvMask) | (NewVal << StartBit),
|
|
|
|
// with the shift of NewVal implicitly stripping the high bits.
|
|
|
|
llvm::Value *NewLowVal =
|
2009-07-25 07:12:58 +08:00
|
|
|
Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
|
2008-08-06 13:08:45 +08:00
|
|
|
"bf.value.lo");
|
|
|
|
LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
|
|
|
|
LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
|
|
|
|
|
|
|
|
// Write back.
|
|
|
|
Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
|
|
|
|
|
|
|
|
// If the low part doesn't cover the bitfield emit a high part.
|
|
|
|
if (LowBits < BitfieldSize) {
|
|
|
|
unsigned HighBits = BitfieldSize - LowBits;
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
|
2008-08-06 13:08:45 +08:00
|
|
|
llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
|
|
|
|
Dst.isVolatileQualified(),
|
|
|
|
"bf.prev.hi");
|
|
|
|
|
|
|
|
// Compute the mask for zero-ing the high part of this bitfield.
|
|
|
|
llvm::Constant *InvMask =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
|
2009-07-15 07:10:40 +08:00
|
|
|
HighBits));
|
2008-08-06 13:08:45 +08:00
|
|
|
|
|
|
|
// Compute the new high part as
|
|
|
|
// HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
|
|
|
|
// where the high bits of NewVal have already been cleared and the
|
|
|
|
// shift stripping the low bits.
|
|
|
|
llvm::Value *NewHighVal =
|
2009-07-25 07:12:58 +08:00
|
|
|
Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
|
2008-08-06 13:08:45 +08:00
|
|
|
"bf.value.high");
|
|
|
|
HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
|
|
|
|
HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
|
|
|
|
|
|
|
|
// Write back.
|
|
|
|
Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
|
|
|
|
}
|
2008-01-23 06:36:45 +08:00
|
|
|
}
|
|
|
|
|
2008-08-29 16:11:39 +08:00
|
|
|
void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
|
|
|
|
LValue Dst,
|
|
|
|
QualType Ty) {
|
|
|
|
EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
|
|
|
|
}
|
|
|
|
|
2008-11-23 06:30:21 +08:00
|
|
|
void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
|
|
|
|
LValue Dst,
|
|
|
|
QualType Ty) {
|
|
|
|
EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
|
|
|
|
}
|
|
|
|
|
2008-04-19 07:10:10 +08:00
|
|
|
void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
|
|
|
|
LValue Dst,
|
|
|
|
QualType Ty) {
|
2007-08-04 00:28:33 +08:00
|
|
|
// This access turns into a read/modify/write of the vector. Load the input
|
|
|
|
// value now.
|
2008-06-14 07:01:12 +08:00
|
|
|
llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
|
|
|
|
Dst.isVolatileQualified(), "tmp");
|
2008-05-09 14:41:27 +08:00
|
|
|
const llvm::Constant *Elts = Dst.getExtVectorElts();
|
2007-08-04 00:28:33 +08:00
|
|
|
|
2007-09-01 06:49:20 +08:00
|
|
|
llvm::Value *SrcVal = Src.getScalarVal();
|
2007-08-04 00:28:33 +08:00
|
|
|
|
2007-08-04 00:37:04 +08:00
|
|
|
if (const VectorType *VTy = Ty->getAsVectorType()) {
|
|
|
|
unsigned NumSrcElts = VTy->getNumElements();
|
2009-01-18 14:42:49 +08:00
|
|
|
unsigned NumDstElts =
|
|
|
|
cast<llvm::VectorType>(Vec->getType())->getNumElements();
|
|
|
|
if (NumDstElts == NumSrcElts) {
|
|
|
|
// Use shuffle vector is the src and destination are the same number
|
2009-06-27 05:12:50 +08:00
|
|
|
// of elements and restore the vector mask since it is on the side
|
|
|
|
// it will be stored.
|
|
|
|
llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
|
2009-01-18 14:42:49 +08:00
|
|
|
for (unsigned i = 0; i != NumSrcElts; ++i) {
|
|
|
|
unsigned InIdx = getAccessedFieldNo(i, Elts);
|
2009-08-14 05:57:51 +08:00
|
|
|
Mask[InIdx] = llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), i);
|
2009-01-18 14:42:49 +08:00
|
|
|
}
|
|
|
|
|
2009-07-29 05:22:35 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
|
2009-01-18 14:42:49 +08:00
|
|
|
Vec = Builder.CreateShuffleVector(SrcVal,
|
2009-07-31 07:11:26 +08:00
|
|
|
llvm::UndefValue::get(Vec->getType()),
|
2009-01-18 14:42:49 +08:00
|
|
|
MaskV, "tmp");
|
2009-07-31 06:28:39 +08:00
|
|
|
} else if (NumDstElts > NumSrcElts) {
|
2009-01-18 14:42:49 +08:00
|
|
|
// Extended the source vector to the same length and then shuffle it
|
|
|
|
// into the destination.
|
|
|
|
// FIXME: since we're shuffling with undef, can we just use the indices
|
|
|
|
// into that? This could be simpler.
|
|
|
|
llvm::SmallVector<llvm::Constant*, 4> ExtMask;
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != NumSrcElts; ++i)
|
2009-08-14 05:57:51 +08:00
|
|
|
ExtMask.push_back(llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), i));
|
2009-01-18 14:42:49 +08:00
|
|
|
for (; i != NumDstElts; ++i)
|
2009-08-14 05:57:51 +08:00
|
|
|
ExtMask.push_back(llvm::UndefValue::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext)));
|
2009-07-29 05:22:35 +08:00
|
|
|
llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
|
2009-01-18 14:42:49 +08:00
|
|
|
ExtMask.size());
|
2009-02-18 02:31:04 +08:00
|
|
|
llvm::Value *ExtSrcVal =
|
|
|
|
Builder.CreateShuffleVector(SrcVal,
|
2009-07-31 07:11:26 +08:00
|
|
|
llvm::UndefValue::get(SrcVal->getType()),
|
2009-02-18 02:31:04 +08:00
|
|
|
ExtMaskV, "tmp");
|
2009-01-18 14:42:49 +08:00
|
|
|
// build identity
|
|
|
|
llvm::SmallVector<llvm::Constant*, 4> Mask;
|
|
|
|
for (unsigned i = 0; i != NumDstElts; ++i) {
|
2009-08-14 05:57:51 +08:00
|
|
|
Mask.push_back(llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), i));
|
2009-01-18 14:42:49 +08:00
|
|
|
}
|
|
|
|
// modify when what gets shuffled in
|
|
|
|
for (unsigned i = 0; i != NumSrcElts; ++i) {
|
|
|
|
unsigned Idx = getAccessedFieldNo(i, Elts);
|
2009-08-14 05:57:51 +08:00
|
|
|
Mask[Idx] = llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), i+NumDstElts);
|
2009-01-18 14:42:49 +08:00
|
|
|
}
|
2009-07-29 05:22:35 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
|
2009-01-18 14:42:49 +08:00
|
|
|
Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
|
2009-07-31 06:28:39 +08:00
|
|
|
} else {
|
2009-01-18 14:42:49 +08:00
|
|
|
// We should never shorten the vector
|
|
|
|
assert(0 && "unexpected shorten vector length");
|
2007-08-04 00:37:04 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the Src is a scalar (not a vector) it must be updating one element.
|
2008-05-22 08:50:06 +08:00
|
|
|
unsigned InIdx = getAccessedFieldNo(0, Elts);
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Value *Elt = llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), InIdx);
|
2007-08-04 00:28:33 +08:00
|
|
|
Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
|
|
|
|
}
|
|
|
|
|
2008-06-14 07:01:12 +08:00
|
|
|
Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
|
2007-08-04 00:28:33 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 13:24:33 +08:00
|
|
|
LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
|
2008-04-16 06:42:06 +08:00
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
|
|
|
|
|
2008-06-18 02:05:57 +08:00
|
|
|
if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
|
|
|
|
isa<ImplicitParamDecl>(VD))) {
|
2008-11-20 08:15:42 +08:00
|
|
|
LValue LV;
|
2009-06-19 00:11:24 +08:00
|
|
|
bool NonGCable = VD->hasLocalStorage() &&
|
2009-06-30 10:34:44 +08:00
|
|
|
!VD->hasAttr<BlocksAttr>();
|
2009-04-14 10:25:56 +08:00
|
|
|
if (VD->hasExternalStorage()) {
|
2009-05-20 04:40:02 +08:00
|
|
|
llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
|
|
|
|
if (VD->getType()->isReferenceType())
|
|
|
|
V = Builder.CreateLoad(V, "tmp");
|
|
|
|
LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2009-07-31 06:28:39 +08:00
|
|
|
} else {
|
2008-04-16 06:42:06 +08:00
|
|
|
llvm::Value *V = LocalDeclMap[VD];
|
2009-02-28 17:07:16 +08:00
|
|
|
assert(V && "DeclRefExpr not entered in LocalDeclMap?");
|
2009-02-20 09:14:43 +08:00
|
|
|
// local variables do not get their gc attribute set.
|
|
|
|
QualType::GCAttrTypes attr = QualType::GCNone;
|
|
|
|
// local static?
|
2009-05-28 03:48:48 +08:00
|
|
|
if (!NonGCable)
|
2009-02-20 09:14:43 +08:00
|
|
|
attr = getContext().getObjCGCAttrKind(E->getType());
|
2009-06-30 10:34:44 +08:00
|
|
|
if (VD->hasAttr<BlocksAttr>()) {
|
2009-03-04 11:23:46 +08:00
|
|
|
bool needsCopyDispose = BlockRequiresCopying(VD->getType());
|
|
|
|
const llvm::Type *PtrStructTy = V->getType();
|
|
|
|
const llvm::Type *Ty = PtrStructTy;
|
2009-07-30 06:16:19 +08:00
|
|
|
Ty = llvm::PointerType::get(Ty, 0);
|
2009-03-04 11:23:46 +08:00
|
|
|
V = Builder.CreateStructGEP(V, 1, "forwarding");
|
|
|
|
V = Builder.CreateBitCast(V, Ty);
|
|
|
|
V = Builder.CreateLoad(V, false);
|
|
|
|
V = Builder.CreateBitCast(V, PtrStructTy);
|
|
|
|
V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
|
|
|
|
}
|
2009-05-20 04:40:02 +08:00
|
|
|
if (VD->getType()->isReferenceType())
|
|
|
|
V = Builder.CreateLoad(V, "tmp");
|
2009-07-22 11:08:17 +08:00
|
|
|
LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(), attr,
|
|
|
|
E->getType().getAddressSpace());
|
2008-02-17 06:30:38 +08:00
|
|
|
}
|
2009-05-28 03:48:48 +08:00
|
|
|
LValue::SetObjCNonGC(LV, NonGCable);
|
2008-11-20 08:15:42 +08:00
|
|
|
return LV;
|
2008-04-16 06:42:06 +08:00
|
|
|
} else if (VD && VD->isFileVarDecl()) {
|
2009-05-20 04:40:02 +08:00
|
|
|
llvm::Value *V = CGM.GetAddrOfGlobalVar(VD);
|
|
|
|
if (VD->getType()->isReferenceType())
|
|
|
|
V = Builder.CreateLoad(V, "tmp");
|
|
|
|
LValue LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2009-05-05 07:27:20 +08:00
|
|
|
if (LV.isObjCStrong())
|
|
|
|
LV.SetGlobalObjCRef(LV, true);
|
2008-11-19 04:18:11 +08:00
|
|
|
return LV;
|
2008-04-16 06:42:06 +08:00
|
|
|
} else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
|
2009-06-01 18:04:20 +08:00
|
|
|
llvm::Value* V = CGM.GetAddrOfFunction(GlobalDecl(FD));
|
|
|
|
if (!FD->hasPrototype()) {
|
|
|
|
if (const FunctionProtoType *Proto =
|
|
|
|
FD->getType()->getAsFunctionProtoType()) {
|
|
|
|
// Ugly case: for a K&R-style definition, the type of the definition
|
|
|
|
// isn't the same as the type of a use. Correct for this with a
|
|
|
|
// bitcast.
|
|
|
|
QualType NoProtoType =
|
|
|
|
getContext().getFunctionNoProtoType(Proto->getResultType());
|
|
|
|
NoProtoType = getContext().getPointerType(NoProtoType);
|
|
|
|
V = Builder.CreateBitCast(V, ConvertType(NoProtoType), "tmp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2009-07-31 06:28:39 +08:00
|
|
|
} else if (const ImplicitParamDecl *IPD =
|
2008-06-18 02:05:57 +08:00
|
|
|
dyn_cast<ImplicitParamDecl>(E->getDecl())) {
|
|
|
|
llvm::Value *V = LocalDeclMap[IPD];
|
|
|
|
assert(V && "BlockVarDecl not entered in LocalDeclMap?");
|
2009-02-20 07:36:06 +08:00
|
|
|
return LValue::MakeAddr(V, E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
2007-06-02 13:24:33 +08:00
|
|
|
assert(0 && "Unimp declref");
|
2007-09-17 03:23:47 +08:00
|
|
|
//an invalid LValue, but the assert will
|
|
|
|
//ensure that this point is never reached.
|
|
|
|
return LValue();
|
2007-06-02 13:24:33 +08:00
|
|
|
}
|
2007-06-02 02:02:12 +08:00
|
|
|
|
2009-02-28 17:07:16 +08:00
|
|
|
LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
|
2009-05-23 10:49:02 +08:00
|
|
|
return LValue::MakeAddr(GetAddrOfBlockDecl(E),
|
|
|
|
E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2009-02-28 17:07:16 +08:00
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
|
|
|
|
// __extension__ doesn't affect lvalue-ness.
|
|
|
|
if (E->getOpcode() == UnaryOperator::Extension)
|
|
|
|
return EmitLValue(E->getSubExpr());
|
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
|
2007-10-31 06:53:42 +08:00
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default: assert(0 && "Unknown unary operator lvalue!");
|
|
|
|
case UnaryOperator::Deref:
|
2009-02-20 07:36:06 +08:00
|
|
|
{
|
2009-07-11 07:34:53 +08:00
|
|
|
QualType T = E->getSubExpr()->getType()->getPointeeType();
|
|
|
|
assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
|
|
|
|
|
2009-02-24 02:59:50 +08:00
|
|
|
LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
|
2009-07-11 07:34:53 +08:00
|
|
|
T.getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(T),
|
|
|
|
ExprTy.getAddressSpace());
|
2009-02-24 02:59:50 +08:00
|
|
|
// We should not generate __weak write barrier on indirect reference
|
|
|
|
// of a pointer to object; as in void foo (__weak id *param); *param = 0;
|
|
|
|
// But, we continue to generate __strong write barrier on indirect write
|
|
|
|
// into a pointer to object.
|
|
|
|
if (getContext().getLangOptions().ObjC1 &&
|
|
|
|
getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
|
|
|
|
LV.isObjCWeak())
|
2009-06-02 05:29:32 +08:00
|
|
|
LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
|
2009-02-24 02:59:50 +08:00
|
|
|
return LV;
|
2009-02-20 07:36:06 +08:00
|
|
|
}
|
2007-10-31 06:53:42 +08:00
|
|
|
case UnaryOperator::Real:
|
|
|
|
case UnaryOperator::Imag:
|
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
2008-03-19 13:19:41 +08:00
|
|
|
unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
|
|
|
|
return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
|
2008-07-27 06:17:49 +08:00
|
|
|
Idx, "idx"),
|
2009-07-22 11:08:17 +08:00
|
|
|
ExprTy.getCVRQualifiers(),
|
|
|
|
QualType::GCNone,
|
|
|
|
ExprTy.getAddressSpace());
|
2007-10-31 06:53:42 +08:00
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-06-06 12:54:52 +08:00
|
|
|
LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
|
2008-08-14 07:20:05 +08:00
|
|
|
return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0);
|
2007-06-06 12:54:52 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
|
|
|
|
return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-18 05:58:32 +08:00
|
|
|
LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
|
2007-07-21 13:21:51 +08:00
|
|
|
std::string GlobalVarName;
|
2008-10-18 05:58:32 +08:00
|
|
|
|
|
|
|
switch (Type) {
|
2009-02-25 06:18:39 +08:00
|
|
|
default:
|
|
|
|
assert(0 && "Invalid type");
|
|
|
|
case PredefinedExpr::Func:
|
|
|
|
GlobalVarName = "__func__.";
|
|
|
|
break;
|
|
|
|
case PredefinedExpr::Function:
|
|
|
|
GlobalVarName = "__FUNCTION__.";
|
|
|
|
break;
|
|
|
|
case PredefinedExpr::PrettyFunction:
|
|
|
|
// FIXME:: Demangle C++ method names
|
|
|
|
GlobalVarName = "__PRETTY_FUNCTION__.";
|
|
|
|
break;
|
2007-07-21 13:21:51 +08:00
|
|
|
}
|
2008-10-18 05:58:32 +08:00
|
|
|
|
2009-04-23 13:30:27 +08:00
|
|
|
// FIXME: This isn't right at all. The logic for computing this should go
|
|
|
|
// into a method on PredefinedExpr. This would allow sema and codegen to be
|
|
|
|
// consistent for things like sizeof(__func__) etc.
|
2008-10-18 05:58:32 +08:00
|
|
|
std::string FunctionName;
|
2009-04-23 13:30:27 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurCodeDecl)) {
|
2009-02-19 07:53:56 +08:00
|
|
|
FunctionName = CGM.getMangledName(FD);
|
2008-10-18 05:58:32 +08:00
|
|
|
} else {
|
2009-04-07 13:48:37 +08:00
|
|
|
// Just get the mangled name; skipping the asm prefix if it
|
|
|
|
// exists.
|
2008-10-18 05:58:32 +08:00
|
|
|
FunctionName = CurFn->getName();
|
2009-04-07 13:48:37 +08:00
|
|
|
if (FunctionName[0] == '\01')
|
|
|
|
FunctionName = FunctionName.substr(1, std::string::npos);
|
2008-10-18 05:58:32 +08:00
|
|
|
}
|
|
|
|
|
2008-04-04 12:07:35 +08:00
|
|
|
GlobalVarName += FunctionName;
|
2008-10-18 05:58:32 +08:00
|
|
|
llvm::Constant *C =
|
|
|
|
CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
|
|
|
|
return LValue::MakeAddr(C, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
|
|
|
|
switch (E->getIdentType()) {
|
|
|
|
default:
|
|
|
|
return EmitUnsupportedLValue(E, "predefined expression");
|
|
|
|
case PredefinedExpr::Func:
|
|
|
|
case PredefinedExpr::Function:
|
|
|
|
case PredefinedExpr::PrettyFunction:
|
|
|
|
return EmitPredefinedFunctionName(E->getIdentType());
|
|
|
|
}
|
2007-07-21 13:21:51 +08:00
|
|
|
}
|
|
|
|
|
2007-06-09 07:31:14 +08:00
|
|
|
LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
|
2007-08-21 00:18:38 +08:00
|
|
|
// The index must always be an integer, which is not an aggregate. Emit it.
|
2007-08-24 13:35:26 +08:00
|
|
|
llvm::Value *Idx = EmitScalarExpr(E->getIdx());
|
2009-06-07 03:09:26 +08:00
|
|
|
QualType IdxTy = E->getIdx()->getType();
|
|
|
|
bool IdxSigned = IdxTy->isSignedIntegerType();
|
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
// If the base is a vector type, then we are forming a vector element lvalue
|
|
|
|
// with this subscript.
|
2008-06-14 07:01:12 +08:00
|
|
|
if (E->getBase()->getType()->isVectorType()) {
|
2007-07-11 05:17:59 +08:00
|
|
|
// Emit the vector as an lvalue to get its address.
|
2008-06-14 07:01:12 +08:00
|
|
|
LValue LHS = EmitLValue(E->getBase());
|
2007-08-21 00:18:38 +08:00
|
|
|
assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
|
2009-08-14 05:57:51 +08:00
|
|
|
Idx = Builder.CreateIntCast(Idx,
|
|
|
|
llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
|
2008-06-14 07:01:12 +08:00
|
|
|
return LValue::MakeVectorElt(LHS.getAddress(), Idx,
|
|
|
|
E->getBase()->getType().getCVRQualifiers());
|
2007-07-11 05:17:59 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 00:18:38 +08:00
|
|
|
// The base must be a pointer, which is not an aggregate. Emit it.
|
2007-08-24 13:35:26 +08:00
|
|
|
llvm::Value *Base = EmitScalarExpr(E->getBase());
|
2007-07-11 05:17:59 +08:00
|
|
|
|
2007-08-21 00:18:38 +08:00
|
|
|
// Extend or truncate the index type to 32 or 64-bits.
|
2007-06-16 07:05:46 +08:00
|
|
|
unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
|
2009-04-24 10:40:57 +08:00
|
|
|
if (IdxBitwidth != LLVMPointerWidth)
|
2009-08-14 05:57:51 +08:00
|
|
|
Idx = Builder.CreateIntCast(Idx,
|
|
|
|
llvm::IntegerType::get(VMContext, LLVMPointerWidth),
|
2007-06-09 07:31:14 +08:00
|
|
|
IdxSigned, "idxprom");
|
|
|
|
|
2009-04-25 13:08:32 +08:00
|
|
|
// We know that the pointer points to a type of the correct size,
|
|
|
|
// unless the size is a VLA or Objective-C interface.
|
|
|
|
llvm::Value *Address = 0;
|
2008-12-21 08:11:23 +08:00
|
|
|
if (const VariableArrayType *VAT =
|
|
|
|
getContext().getAsVariableArrayType(E->getType())) {
|
2009-08-15 07:43:22 +08:00
|
|
|
llvm::Value *VLASize = GetVLASize(VAT);
|
2008-12-21 08:11:23 +08:00
|
|
|
|
|
|
|
Idx = Builder.CreateMul(Idx, VLASize);
|
|
|
|
|
2008-12-21 11:44:36 +08:00
|
|
|
QualType BaseType = getContext().getBaseElementType(VAT);
|
2008-12-21 08:11:23 +08:00
|
|
|
|
|
|
|
uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
|
|
|
|
Idx = Builder.CreateUDiv(Idx,
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(Idx->getType(),
|
2008-12-21 08:11:23 +08:00
|
|
|
BaseTypeSize));
|
2009-08-12 08:33:55 +08:00
|
|
|
Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
|
2009-04-25 13:08:32 +08:00
|
|
|
} else if (const ObjCInterfaceType *OIT =
|
|
|
|
dyn_cast<ObjCInterfaceType>(E->getType())) {
|
|
|
|
llvm::Value *InterfaceSize =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(Idx->getType(),
|
2009-04-25 13:08:32 +08:00
|
|
|
getContext().getTypeSize(OIT) / 8);
|
|
|
|
|
|
|
|
Idx = Builder.CreateMul(Idx, InterfaceSize);
|
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Type *i8PTy =
|
|
|
|
llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
|
2009-08-12 08:33:55 +08:00
|
|
|
Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
|
2009-04-25 13:08:32 +08:00
|
|
|
Idx, "arrayidx");
|
|
|
|
Address = Builder.CreateBitCast(Address, Base->getType());
|
|
|
|
} else {
|
2009-08-12 08:33:55 +08:00
|
|
|
Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
|
2008-12-21 08:11:23 +08:00
|
|
|
}
|
|
|
|
|
2009-07-11 07:34:53 +08:00
|
|
|
QualType T = E->getBase()->getType()->getPointeeType();
|
|
|
|
assert(!T.isNull() &&
|
|
|
|
"CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
|
|
|
|
|
2009-04-25 13:08:32 +08:00
|
|
|
LValue LV = LValue::MakeAddr(Address,
|
2009-04-18 16:54:40 +08:00
|
|
|
T.getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(T),
|
|
|
|
E->getBase()->getType().getAddressSpace());
|
2009-02-22 07:37:19 +08:00
|
|
|
if (getContext().getLangOptions().ObjC1 &&
|
|
|
|
getContext().getLangOptions().getGCMode() != LangOptions::NonGC)
|
2009-06-02 05:29:32 +08:00
|
|
|
LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
|
2009-02-22 07:37:19 +08:00
|
|
|
return LV;
|
2007-06-09 07:31:14 +08:00
|
|
|
}
|
|
|
|
|
2008-05-14 05:03:02 +08:00
|
|
|
static
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
|
|
|
|
llvm::SmallVector<unsigned, 4> &Elts) {
|
2008-05-14 05:03:02 +08:00
|
|
|
llvm::SmallVector<llvm::Constant *, 4> CElts;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Elts.size(); i != e; ++i)
|
2009-08-14 05:57:51 +08:00
|
|
|
CElts.push_back(llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), Elts[i]));
|
2008-05-14 05:03:02 +08:00
|
|
|
|
2009-07-29 05:22:35 +08:00
|
|
|
return llvm::ConstantVector::get(&CElts[0], CElts.size());
|
2008-05-14 05:03:02 +08:00
|
|
|
}
|
|
|
|
|
2007-08-03 07:37:31 +08:00
|
|
|
LValue CodeGenFunction::
|
2008-04-19 07:10:10 +08:00
|
|
|
EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
|
2007-08-03 07:37:31 +08:00
|
|
|
// Emit the base vector as an l-value.
|
2009-02-17 05:11:58 +08:00
|
|
|
LValue Base;
|
|
|
|
|
|
|
|
// ExtVectorElementExpr's base can either be a vector or pointer to vector.
|
2009-02-17 06:14:05 +08:00
|
|
|
if (!E->isArrow()) {
|
2009-02-17 05:11:58 +08:00
|
|
|
assert(E->getBase()->getType()->isVectorType());
|
|
|
|
Base = EmitLValue(E->getBase());
|
2009-02-17 06:14:05 +08:00
|
|
|
} else {
|
2009-07-30 05:53:49 +08:00
|
|
|
const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
|
2009-02-17 06:14:05 +08:00
|
|
|
llvm::Value *Ptr = EmitScalarExpr(E->getBase());
|
2009-07-22 11:08:17 +08:00
|
|
|
Base = LValue::MakeAddr(Ptr, PT->getPointeeType().getCVRQualifiers(),
|
|
|
|
QualType::GCNone,
|
|
|
|
PT->getPointeeType().getAddressSpace());
|
2009-02-17 05:11:58 +08:00
|
|
|
}
|
2008-05-14 05:03:02 +08:00
|
|
|
|
|
|
|
// Encode the element access list into a vector of unsigned indices.
|
|
|
|
llvm::SmallVector<unsigned, 4> Indices;
|
|
|
|
E->getEncodedElementAccess(Indices);
|
|
|
|
|
|
|
|
if (Base.isSimple()) {
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
|
2008-06-14 07:01:12 +08:00
|
|
|
return LValue::MakeExtVectorElt(Base.getAddress(), CV,
|
2009-02-17 06:25:49 +08:00
|
|
|
Base.getQualifiers());
|
2008-05-09 14:41:27 +08:00
|
|
|
}
|
2008-05-14 05:03:02 +08:00
|
|
|
assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
|
|
|
|
|
|
|
|
llvm::Constant *BaseElts = Base.getExtVectorElts();
|
|
|
|
llvm::SmallVector<llvm::Constant *, 4> CElts;
|
2007-08-03 07:37:31 +08:00
|
|
|
|
2008-05-14 05:03:02 +08:00
|
|
|
for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
|
|
|
|
if (isa<llvm::ConstantAggregateZero>(BaseElts))
|
2009-08-14 05:57:51 +08:00
|
|
|
CElts.push_back(llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), 0));
|
2008-05-14 05:03:02 +08:00
|
|
|
else
|
|
|
|
CElts.push_back(BaseElts->getOperand(Indices[i]));
|
|
|
|
}
|
2009-07-29 05:22:35 +08:00
|
|
|
llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
|
2008-06-14 07:01:12 +08:00
|
|
|
return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
|
2009-02-17 06:25:49 +08:00
|
|
|
Base.getQualifiers());
|
2007-08-03 07:37:31 +08:00
|
|
|
}
|
|
|
|
|
2007-10-24 04:28:39 +08:00
|
|
|
LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
|
2007-12-12 05:33:16 +08:00
|
|
|
bool isUnion = false;
|
2008-11-22 02:14:01 +08:00
|
|
|
bool isIvar = false;
|
2009-02-21 08:30:43 +08:00
|
|
|
bool isNonGC = false;
|
2007-10-25 06:26:28 +08:00
|
|
|
Expr *BaseExpr = E->getBase();
|
|
|
|
llvm::Value *BaseValue = NULL;
|
2008-06-14 07:01:12 +08:00
|
|
|
unsigned CVRQualifiers=0;
|
|
|
|
|
2007-12-03 02:52:07 +08:00
|
|
|
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
|
2007-12-12 05:33:16 +08:00
|
|
|
if (E->isArrow()) {
|
2007-12-03 02:52:07 +08:00
|
|
|
BaseValue = EmitScalarExpr(BaseExpr);
|
2007-12-12 05:33:16 +08:00
|
|
|
const PointerType *PTy =
|
2009-07-30 05:53:49 +08:00
|
|
|
BaseExpr->getType()->getAs<PointerType>();
|
2007-12-12 05:33:16 +08:00
|
|
|
if (PTy->getPointeeType()->isUnionType())
|
|
|
|
isUnion = true;
|
2008-06-14 07:01:12 +08:00
|
|
|
CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
|
2009-02-17 06:25:49 +08:00
|
|
|
} else if (isa<ObjCPropertyRefExpr>(BaseExpr) ||
|
|
|
|
isa<ObjCKVCRefExpr>(BaseExpr)) {
|
2009-01-13 07:27:26 +08:00
|
|
|
RValue RV = EmitObjCPropertyGet(BaseExpr);
|
|
|
|
BaseValue = RV.getAggregateAddr();
|
|
|
|
if (BaseExpr->getType()->isUnionType())
|
|
|
|
isUnion = true;
|
|
|
|
CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
|
2009-02-17 06:25:49 +08:00
|
|
|
} else {
|
2007-10-25 06:26:28 +08:00
|
|
|
LValue BaseLV = EmitLValue(BaseExpr);
|
2008-11-22 02:14:01 +08:00
|
|
|
if (BaseLV.isObjCIvar())
|
|
|
|
isIvar = true;
|
2009-02-21 08:30:43 +08:00
|
|
|
if (BaseLV.isNonGC())
|
|
|
|
isNonGC = true;
|
2007-12-03 02:52:07 +08:00
|
|
|
// FIXME: this isn't right for bitfields.
|
2007-10-25 06:26:28 +08:00
|
|
|
BaseValue = BaseLV.getAddress();
|
2009-07-29 08:44:13 +08:00
|
|
|
QualType BaseTy = BaseExpr->getType();
|
|
|
|
if (BaseTy->isUnionType())
|
2007-12-12 05:33:16 +08:00
|
|
|
isUnion = true;
|
2009-07-29 08:44:13 +08:00
|
|
|
CVRQualifiers = BaseTy.getCVRQualifiers();
|
2007-12-03 02:52:07 +08:00
|
|
|
}
|
2007-10-24 04:28:39 +08:00
|
|
|
|
2008-12-21 07:49:58 +08:00
|
|
|
FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
|
|
|
|
// FIXME: Handle non-field member expressions
|
|
|
|
assert(Field && "No code generation for non-field member references");
|
2009-02-17 06:25:49 +08:00
|
|
|
LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion,
|
|
|
|
CVRQualifiers);
|
2008-11-22 02:14:01 +08:00
|
|
|
LValue::SetObjCIvar(MemExpLV, isIvar);
|
2009-02-21 08:30:43 +08:00
|
|
|
LValue::SetObjCNonGC(MemExpLV, isNonGC);
|
2008-11-22 02:14:01 +08:00
|
|
|
return MemExpLV;
|
2008-02-09 16:50:58 +08:00
|
|
|
}
|
2007-10-24 04:28:39 +08:00
|
|
|
|
2008-12-16 04:35:07 +08:00
|
|
|
LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
|
|
|
|
FieldDecl* Field,
|
2009-02-04 03:03:09 +08:00
|
|
|
unsigned CVRQualifiers) {
|
2009-07-24 01:01:21 +08:00
|
|
|
CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
|
|
|
|
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: CodeGenTypes should expose a method to get the appropriate type for
|
|
|
|
// FieldTy (the appropriate type is ABI-dependent).
|
2009-02-18 02:31:04 +08:00
|
|
|
const llvm::Type *FieldTy =
|
|
|
|
CGM.getTypes().ConvertTypeForMem(Field->getType());
|
2008-12-16 04:35:07 +08:00
|
|
|
const llvm::PointerType *BaseTy =
|
|
|
|
cast<llvm::PointerType>(BaseValue->getType());
|
|
|
|
unsigned AS = BaseTy->getAddressSpace();
|
|
|
|
BaseValue = Builder.CreateBitCast(BaseValue,
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::get(FieldTy, AS),
|
2008-12-16 04:35:07 +08:00
|
|
|
"tmp");
|
|
|
|
|
2009-07-24 01:01:21 +08:00
|
|
|
llvm::Value *Idx =
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
|
2009-07-24 01:01:21 +08:00
|
|
|
llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
|
|
|
|
|
|
|
|
return LValue::MakeBitfield(V, Info.Start, Info.Size,
|
2008-12-16 04:35:07 +08:00
|
|
|
Field->getType()->isSignedIntegerType(),
|
|
|
|
Field->getType().getCVRQualifiers()|CVRQualifiers);
|
|
|
|
}
|
|
|
|
|
2008-02-09 16:50:58 +08:00
|
|
|
LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
|
|
|
|
FieldDecl* Field,
|
2008-06-14 07:01:12 +08:00
|
|
|
bool isUnion,
|
|
|
|
unsigned CVRQualifiers)
|
2008-02-09 16:50:58 +08:00
|
|
|
{
|
2008-12-16 04:35:07 +08:00
|
|
|
if (Field->isBitField())
|
2009-02-04 03:03:09 +08:00
|
|
|
return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
|
2008-11-20 08:15:42 +08:00
|
|
|
|
2009-02-04 03:03:09 +08:00
|
|
|
unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
|
2008-12-16 04:35:07 +08:00
|
|
|
llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
|
2008-05-29 19:33:25 +08:00
|
|
|
|
2007-10-27 03:42:18 +08:00
|
|
|
// Match union field type.
|
2008-02-08 03:29:53 +08:00
|
|
|
if (isUnion) {
|
2008-06-14 07:01:12 +08:00
|
|
|
const llvm::Type *FieldTy =
|
|
|
|
CGM.getTypes().ConvertTypeForMem(Field->getType());
|
2007-10-31 04:59:40 +08:00
|
|
|
const llvm::PointerType * BaseTy =
|
|
|
|
cast<llvm::PointerType>(BaseValue->getType());
|
2008-05-21 21:24:44 +08:00
|
|
|
unsigned AS = BaseTy->getAddressSpace();
|
|
|
|
V = Builder.CreateBitCast(V,
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::get(FieldTy, AS),
|
2008-05-21 21:24:44 +08:00
|
|
|
"tmp");
|
2007-10-27 03:42:18 +08:00
|
|
|
}
|
2009-05-31 05:09:44 +08:00
|
|
|
if (Field->getType()->isReferenceType())
|
|
|
|
V = Builder.CreateLoad(V, "tmp");
|
2008-01-23 04:17:04 +08:00
|
|
|
|
2009-02-20 07:36:06 +08:00
|
|
|
QualType::GCAttrTypes attr = QualType::GCNone;
|
2009-02-19 01:52:36 +08:00
|
|
|
if (CGM.getLangOptions().ObjC1 &&
|
2009-02-19 02:52:41 +08:00
|
|
|
CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
|
|
|
|
QualType Ty = Field->getType();
|
2009-02-20 07:36:06 +08:00
|
|
|
attr = Ty.getObjCGCAttr();
|
2009-02-19 08:48:05 +08:00
|
|
|
if (attr != QualType::GCNone) {
|
2009-02-19 02:52:41 +08:00
|
|
|
// __weak attribute on a field is ignored.
|
2009-02-20 07:36:06 +08:00
|
|
|
if (attr == QualType::Weak)
|
|
|
|
attr = QualType::GCNone;
|
2009-07-31 06:28:39 +08:00
|
|
|
} else if (Ty->isObjCObjectPointerType())
|
2009-02-20 07:36:06 +08:00
|
|
|
attr = QualType::Strong;
|
2009-02-19 02:52:41 +08:00
|
|
|
}
|
2009-02-20 07:36:06 +08:00
|
|
|
LValue LV =
|
|
|
|
LValue::MakeAddr(V,
|
|
|
|
Field->getType().getCVRQualifiers()|CVRQualifiers,
|
2009-07-22 11:08:17 +08:00
|
|
|
attr,
|
|
|
|
Field->getType().getAddressSpace());
|
2008-11-20 08:15:42 +08:00
|
|
|
return LV;
|
2007-10-24 04:28:39 +08:00
|
|
|
}
|
|
|
|
|
2009-03-19 02:28:57 +08:00
|
|
|
LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
|
2008-05-14 07:18:27 +08:00
|
|
|
const llvm::Type *LTy = ConvertType(E->getType());
|
|
|
|
llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
|
|
|
|
|
|
|
|
const Expr* InitExpr = E->getInitializer();
|
2009-07-22 11:08:17 +08:00
|
|
|
LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers(),
|
|
|
|
QualType::GCNone,
|
|
|
|
E->getType().getAddressSpace());
|
2008-05-14 07:18:27 +08:00
|
|
|
|
|
|
|
if (E->getType()->isComplexType()) {
|
|
|
|
EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
|
|
|
|
} else if (hasAggregateLLVMType(E->getType())) {
|
|
|
|
EmitAnyExpr(InitExpr, DeclPtr, false);
|
|
|
|
} else {
|
|
|
|
EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-03-24 10:38:23 +08:00
|
|
|
LValue CodeGenFunction::EmitConditionalOperator(const ConditionalOperator* E) {
|
2009-07-30 09:10:26 +08:00
|
|
|
if (E->isLvalue(getContext()) == Expr::LV_Valid)
|
2009-03-24 10:38:23 +08:00
|
|
|
return EmitUnsupportedLValue(E, "conditional operator");
|
|
|
|
|
|
|
|
// ?: here should be an aggregate.
|
|
|
|
assert((hasAggregateLLVMType(E->getType()) &&
|
|
|
|
!E->getType()->isAnyComplexType()) &&
|
|
|
|
"Unexpected conditional operator!");
|
|
|
|
|
|
|
|
llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
|
|
|
|
EmitAggExpr(E, Temp, false);
|
|
|
|
|
|
|
|
return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2009-03-24 10:38:23 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-19 02:28:57 +08:00
|
|
|
/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code
|
|
|
|
/// generator in an lvalue context, then it must mean that we need the address
|
|
|
|
/// of an aggregate in order to access one of its fields. This can happen for
|
|
|
|
/// all the reasons that casts are permitted with aggregate result, including
|
|
|
|
/// noop aggregate casts, and cast from scalar to union.
|
|
|
|
LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
|
|
|
|
// If this is an aggregate-to-aggregate cast, just use the input's address as
|
|
|
|
// the lvalue.
|
|
|
|
if (getContext().hasSameUnqualifiedType(E->getType(),
|
|
|
|
E->getSubExpr()->getType()))
|
|
|
|
return EmitLValue(E->getSubExpr());
|
|
|
|
|
|
|
|
// Otherwise, we must have a cast from scalar to union.
|
|
|
|
assert(E->getType()->isUnionType() && "Expected scalar-to-union cast");
|
|
|
|
|
|
|
|
// Casts are only lvalues when the source and destination types are the same.
|
|
|
|
llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
|
2009-03-19 02:30:44 +08:00
|
|
|
EmitAnyExpr(E->getSubExpr(), Temp, false);
|
2009-03-19 02:28:57 +08:00
|
|
|
|
|
|
|
return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2009-03-19 02:28:57 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 02:02:12 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Expression Emission
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-08-21 06:37:10 +08:00
|
|
|
|
2007-06-16 05:34:29 +08:00
|
|
|
RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
|
2009-02-21 02:06:48 +08:00
|
|
|
// Builtins never have block type.
|
|
|
|
if (E->getCallee()->getType()->isBlockPointerType())
|
|
|
|
return EmitBlockCallExpr(E);
|
|
|
|
|
2009-04-04 06:50:24 +08:00
|
|
|
if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
|
|
|
|
return EmitCXXMemberCallExpr(CE);
|
|
|
|
|
2009-02-21 02:06:48 +08:00
|
|
|
const Decl *TargetDecl = 0;
|
2009-02-21 03:34:33 +08:00
|
|
|
if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
|
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
|
|
|
|
TargetDecl = DRE->getDecl();
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
|
|
|
|
if (unsigned builtinID = FD->getBuiltinID(getContext()))
|
|
|
|
return EmitBuiltinExpr(FD, builtinID, E);
|
2009-02-21 02:06:48 +08:00
|
|
|
}
|
|
|
|
}
|
2009-01-10 00:50:52 +08:00
|
|
|
|
2009-06-13 08:26:38 +08:00
|
|
|
if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
|
2009-05-27 12:18:27 +08:00
|
|
|
if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
|
|
|
|
return EmitCXXOperatorMemberCallExpr(CE, MD);
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
llvm::Value *Callee = EmitScalarExpr(E->getCallee());
|
2009-05-27 09:22:39 +08:00
|
|
|
return EmitCall(Callee, E->getCallee()->getType(),
|
|
|
|
E->arg_begin(), E->arg_end(), TargetDecl);
|
2007-08-31 12:44:06 +08:00
|
|
|
}
|
|
|
|
|
2008-09-04 11:20:13 +08:00
|
|
|
LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
|
2009-05-13 05:28:12 +08:00
|
|
|
// Comma expressions just emit their LHS then their RHS as an l-value.
|
|
|
|
if (E->getOpcode() == BinaryOperator::Comma) {
|
|
|
|
EmitAnyExpr(E->getLHS());
|
|
|
|
return EmitLValue(E->getRHS());
|
|
|
|
}
|
|
|
|
|
2008-09-04 11:20:13 +08:00
|
|
|
// Can only get l-value for binary operator expressions which are a
|
|
|
|
// simple assignment of aggregate type.
|
|
|
|
if (E->getOpcode() != BinaryOperator::Assign)
|
|
|
|
return EmitUnsupportedLValue(E, "binary l-value expression");
|
|
|
|
|
|
|
|
llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
|
|
|
|
EmitAggExpr(E, Temp, false);
|
|
|
|
// FIXME: Are these qualifiers correct?
|
2009-02-20 07:36:06 +08:00
|
|
|
return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2008-09-04 11:20:13 +08:00
|
|
|
}
|
|
|
|
|
2007-12-29 13:02:41 +08:00
|
|
|
LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
|
|
|
|
RValue RV = EmitCallExpr(E);
|
2009-05-27 09:45:47 +08:00
|
|
|
|
|
|
|
if (RV.isScalar()) {
|
|
|
|
assert(E->getCallReturnType()->isReferenceType() &&
|
|
|
|
"Can't have a scalar return unless the return type is a "
|
|
|
|
"reference type!");
|
|
|
|
|
|
|
|
return LValue::MakeAddr(RV.getScalarVal(), E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2009-05-27 09:45:47 +08:00
|
|
|
}
|
|
|
|
|
2008-06-14 07:01:12 +08:00
|
|
|
return LValue::MakeAddr(RV.getAggregateAddr(),
|
2009-02-20 07:36:06 +08:00
|
|
|
E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2007-12-29 13:02:41 +08:00
|
|
|
}
|
|
|
|
|
2009-02-12 04:59:32 +08:00
|
|
|
LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
|
|
|
|
// FIXME: This shouldn't require another copy.
|
|
|
|
llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
|
|
|
|
EmitAggExpr(E, Temp, false);
|
2009-07-22 11:08:17 +08:00
|
|
|
return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
|
|
|
|
QualType::GCNone, E->getType().getAddressSpace());
|
2009-02-12 04:59:32 +08:00
|
|
|
}
|
|
|
|
|
2008-09-10 10:36:38 +08:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
|
|
|
|
EmitLocalBlockVarDecl(*E->getVarDecl());
|
|
|
|
return EmitDeclRefLValue(E);
|
|
|
|
}
|
|
|
|
|
2009-05-31 07:23:33 +08:00
|
|
|
LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
|
|
|
|
llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
|
|
|
|
EmitCXXConstructExpr(Temp, E);
|
2009-07-22 11:08:17 +08:00
|
|
|
return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(),
|
|
|
|
QualType::GCNone, E->getType().getAddressSpace());
|
2009-05-31 07:23:33 +08:00
|
|
|
}
|
|
|
|
|
2009-05-31 07:30:54 +08:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
|
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
|
2009-05-31 08:34:10 +08:00
|
|
|
PushCXXTemporary(E->getTemporary(), LV.getAddress());
|
2009-05-31 07:30:54 +08:00
|
|
|
|
|
|
|
return LV;
|
|
|
|
}
|
|
|
|
|
2008-08-23 18:51:21 +08:00
|
|
|
LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
|
|
|
|
// Can only get l-value for message expression returning aggregate type
|
|
|
|
RValue RV = EmitObjCMessageExpr(E);
|
|
|
|
// FIXME: can this be volatile?
|
|
|
|
return LValue::MakeAddr(RV.getAggregateAddr(),
|
2009-02-20 07:36:06 +08:00
|
|
|
E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2008-08-23 18:51:21 +08:00
|
|
|
}
|
|
|
|
|
2009-04-22 13:08:15 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
|
2008-09-24 12:00:38 +08:00
|
|
|
const ObjCIvarDecl *Ivar) {
|
2009-02-11 03:02:04 +08:00
|
|
|
return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
|
2008-09-24 12:00:38 +08:00
|
|
|
}
|
2008-08-25 09:53:23 +08:00
|
|
|
|
2009-02-03 08:09:52 +08:00
|
|
|
LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
|
|
|
|
llvm::Value *BaseValue,
|
2008-09-24 12:00:38 +08:00
|
|
|
const ObjCIvarDecl *Ivar,
|
|
|
|
unsigned CVRQualifiers) {
|
2009-04-18 01:44:48 +08:00
|
|
|
return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
|
2009-04-21 09:19:28 +08:00
|
|
|
Ivar, CVRQualifiers);
|
2008-09-24 12:00:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
|
2008-08-25 09:53:23 +08:00
|
|
|
// FIXME: A lot of the code below could be shared with EmitMemberExpr.
|
|
|
|
llvm::Value *BaseValue = 0;
|
|
|
|
const Expr *BaseExpr = E->getBase();
|
|
|
|
unsigned CVRQualifiers = 0;
|
2009-02-03 08:09:52 +08:00
|
|
|
QualType ObjectTy;
|
2008-08-25 09:53:23 +08:00
|
|
|
if (E->isArrow()) {
|
|
|
|
BaseValue = EmitScalarExpr(BaseExpr);
|
2009-07-11 07:34:53 +08:00
|
|
|
ObjectTy = BaseExpr->getType()->getPointeeType();
|
2009-02-03 08:09:52 +08:00
|
|
|
CVRQualifiers = ObjectTy.getCVRQualifiers();
|
2008-08-25 09:53:23 +08:00
|
|
|
} else {
|
|
|
|
LValue BaseLV = EmitLValue(BaseExpr);
|
|
|
|
// FIXME: this isn't right for bitfields.
|
|
|
|
BaseValue = BaseLV.getAddress();
|
2009-02-03 08:09:52 +08:00
|
|
|
ObjectTy = BaseExpr->getType();
|
|
|
|
CVRQualifiers = ObjectTy.getCVRQualifiers();
|
2008-08-25 09:53:23 +08:00
|
|
|
}
|
2008-09-24 12:00:38 +08:00
|
|
|
|
2009-04-21 09:19:28 +08:00
|
|
|
return EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(), CVRQualifiers);
|
2008-03-31 07:03:07 +08:00
|
|
|
}
|
|
|
|
|
2008-08-29 16:11:39 +08:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
|
|
|
|
// This is a special l-value that just issues sends when we load or
|
|
|
|
// store through it.
|
|
|
|
return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
|
|
|
|
}
|
|
|
|
|
2008-11-23 06:30:21 +08:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E) {
|
|
|
|
// This is a special l-value that just issues sends when we load or
|
|
|
|
// store through it.
|
|
|
|
return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
|
|
|
|
}
|
|
|
|
|
2008-11-04 22:56:14 +08:00
|
|
|
LValue
|
2009-04-26 03:35:26 +08:00
|
|
|
CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
|
2008-11-04 22:56:14 +08:00
|
|
|
return EmitUnsupportedLValue(E, "use of super");
|
|
|
|
}
|
|
|
|
|
2009-04-26 03:35:26 +08:00
|
|
|
LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
|
|
|
|
|
|
|
|
// Can only get l-value for message expression returning aggregate type
|
|
|
|
RValue RV = EmitAnyExprToTemp(E);
|
|
|
|
// FIXME: can this be volatile?
|
|
|
|
return LValue::MakeAddr(RV.getAggregateAddr(),
|
|
|
|
E->getType().getCVRQualifiers(),
|
2009-07-22 11:08:17 +08:00
|
|
|
getContext().getObjCGCAttrKind(E->getType()),
|
|
|
|
E->getType().getAddressSpace());
|
2009-04-26 03:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-27 09:22:39 +08:00
|
|
|
RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
|
|
|
|
CallExpr::const_arg_iterator ArgBeg,
|
|
|
|
CallExpr::const_arg_iterator ArgEnd,
|
|
|
|
const Decl *TargetDecl) {
|
2009-01-10 00:50:52 +08:00
|
|
|
// Get the actual function type. The callee type will always be a
|
|
|
|
// pointer to function type or a block pointer type.
|
2009-04-08 02:53:02 +08:00
|
|
|
assert(CalleeType->isFunctionPointerType() &&
|
|
|
|
"Call must have function pointer type!");
|
|
|
|
|
2009-07-30 05:53:49 +08:00
|
|
|
QualType FnType = CalleeType->getAs<PointerType>()->getPointeeType();
|
2009-04-08 02:53:02 +08:00
|
|
|
QualType ResultType = FnType->getAsFunctionType()->getResultType();
|
2008-08-30 11:02:31 +08:00
|
|
|
|
|
|
|
CallArgList Args;
|
2009-04-09 07:13:16 +08:00
|
|
|
EmitCallArgs(Args, FnType->getAsFunctionProtoType(), ArgBeg, ArgEnd);
|
2008-08-30 11:02:31 +08:00
|
|
|
|
2009-02-03 07:23:47 +08:00
|
|
|
return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args),
|
2009-02-21 02:06:48 +08:00
|
|
|
Callee, Args, TargetDecl);
|
2008-08-23 11:46:30 +08:00
|
|
|
}
|