2007-08-24 10:22:53 +08:00
|
|
|
//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
|
2007-08-11 04:13:28 +08:00
|
|
|
//
|
|
|
|
// 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-08-11 04:13:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Aggregate Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2007-08-11 08:04:45 +08:00
|
|
|
#include "CodeGenModule.h"
|
|
|
|
#include "clang/AST/AST.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
2007-10-27 01:44:44 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2007-08-21 12:25:47 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-04-05 02:42:16 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2007-08-11 04:13:28 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Aggregate Expression Emitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
|
|
|
|
CodeGenFunction &CGF;
|
2008-04-13 15:32:11 +08:00
|
|
|
llvm::IRBuilder &Builder;
|
2007-08-21 12:25:47 +08:00
|
|
|
llvm::Value *DestPtr;
|
|
|
|
bool VolatileDest;
|
|
|
|
public:
|
|
|
|
AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
|
2007-08-27 07:13:56 +08:00
|
|
|
: CGF(cgf), Builder(CGF.Builder),
|
|
|
|
DestPtr(destPtr), VolatileDest(volatileDest) {
|
2007-08-21 12:25:47 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
/// EmitAggLoadOfLValue - Given an expression with aggregate type that
|
|
|
|
/// represents a value lvalue, this method emits the address of the lvalue,
|
|
|
|
/// then loads the result into DestPtr.
|
|
|
|
void EmitAggLoadOfLValue(const Expr *E);
|
|
|
|
|
2007-08-27 07:13:56 +08:00
|
|
|
void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
|
|
|
|
QualType EltTy);
|
2008-02-20 06:04:22 +08:00
|
|
|
|
|
|
|
void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
|
|
|
|
|
|
|
|
void EmitNonConstInit(InitListExpr *E);
|
2007-08-21 12:59:27 +08:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void VisitStmt(Stmt *S) {
|
2007-12-02 09:49:16 +08:00
|
|
|
CGF.WarnUnsupported(S, "aggregate expression");
|
2007-08-21 12:25:47 +08:00
|
|
|
}
|
|
|
|
void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
|
|
|
|
|
|
|
|
// l-values.
|
2007-12-14 10:04:12 +08:00
|
|
|
void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
|
|
|
|
void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
|
|
|
|
void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
|
2007-12-23 11:11:58 +08:00
|
|
|
void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
|
2007-12-14 10:04:12 +08:00
|
|
|
|
|
|
|
void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
}
|
2007-08-21 12:25:47 +08:00
|
|
|
|
|
|
|
// Operators.
|
|
|
|
// case Expr::UnaryOperatorClass:
|
|
|
|
// case Expr::CastExprClass:
|
2008-01-14 14:28:57 +08:00
|
|
|
void VisitImplicitCastExpr(ImplicitCastExpr *E);
|
2007-11-01 06:04:46 +08:00
|
|
|
void VisitCallExpr(const CallExpr *E);
|
2007-09-01 06:54:14 +08:00
|
|
|
void VisitStmtExpr(const StmtExpr *E);
|
2007-08-21 12:25:47 +08:00
|
|
|
void VisitBinaryOperator(const BinaryOperator *BO);
|
2007-08-21 12:43:17 +08:00
|
|
|
void VisitBinAssign(const BinaryOperator *E);
|
2008-01-31 13:38:29 +08:00
|
|
|
void VisitOverloadExpr(const OverloadExpr *E);
|
2007-08-21 12:25:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
void VisitConditionalOperator(const ConditionalOperator *CO);
|
2007-10-27 01:44:44 +08:00
|
|
|
void VisitInitListExpr(InitListExpr *E);
|
2008-04-08 12:40:51 +08:00
|
|
|
void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
Visit(DAE->getExpr());
|
|
|
|
}
|
2008-04-05 02:42:16 +08:00
|
|
|
|
|
|
|
void EmitInitializationToLValue(Expr *E, LValue Address);
|
|
|
|
void EmitNullInitializationToLValue(LValue Address, QualType T);
|
2007-08-21 12:25:47 +08:00
|
|
|
// case Expr::ChooseExprClass:
|
2008-02-19 06:44:02 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-21 12:25:47 +08:00
|
|
|
|
2008-02-20 06:04:22 +08:00
|
|
|
void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
|
2008-04-05 00:54:41 +08:00
|
|
|
assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
|
2008-02-20 06:04:22 +08:00
|
|
|
|
|
|
|
// Aggregate assignment turns into llvm.memset.
|
|
|
|
const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
if (DestPtr->getType() != BP)
|
|
|
|
DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
|
|
|
|
|
|
|
|
// Get size and alignment info for this aggregate.
|
2008-03-06 02:54:05 +08:00
|
|
|
std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
|
2008-02-20 06:04:22 +08:00
|
|
|
|
|
|
|
// FIXME: Handle variable sized types.
|
|
|
|
const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
|
|
|
|
|
|
|
|
llvm::Value *MemSetOps[4] = {
|
|
|
|
DestPtr,
|
|
|
|
llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
|
|
|
|
// TypeInfo.first describes size in bits.
|
|
|
|
llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
|
|
|
|
};
|
|
|
|
|
|
|
|
Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
|
|
|
|
}
|
|
|
|
|
2007-08-27 07:13:56 +08:00
|
|
|
void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
|
|
|
|
llvm::Value *SrcPtr, QualType Ty) {
|
2008-04-05 00:54:41 +08:00
|
|
|
assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
|
2007-08-27 07:13:56 +08:00
|
|
|
|
|
|
|
// Aggregate assignment turns into llvm.memcpy.
|
2007-12-17 09:11:20 +08:00
|
|
|
const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
2007-08-27 07:13:56 +08:00
|
|
|
if (DestPtr->getType() != BP)
|
|
|
|
DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
|
|
|
|
if (SrcPtr->getType() != BP)
|
|
|
|
SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
|
|
|
|
|
|
|
|
// Get size and alignment info for this aggregate.
|
2008-03-06 02:54:05 +08:00
|
|
|
std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
|
2007-08-27 07:13:56 +08:00
|
|
|
|
|
|
|
// FIXME: Handle variable sized types.
|
|
|
|
const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
|
|
|
|
|
|
|
|
llvm::Value *MemCpyOps[4] = {
|
|
|
|
DestPtr, SrcPtr,
|
2007-10-27 01:44:44 +08:00
|
|
|
// TypeInfo.first describes size in bits.
|
|
|
|
llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
|
2008-02-20 06:04:22 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
|
2007-08-27 07:13:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 08:04:45 +08:00
|
|
|
/// EmitAggLoadOfLValue - Given an expression with aggregate type that
|
|
|
|
/// represents a value lvalue, this method emits the address of the lvalue,
|
|
|
|
/// then loads the result into DestPtr.
|
2007-08-21 12:25:47 +08:00
|
|
|
void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
|
|
|
|
LValue LV = CGF.EmitLValue(E);
|
2007-08-11 08:04:45 +08:00
|
|
|
assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
|
|
|
|
llvm::Value *SrcPtr = LV.getAddress();
|
|
|
|
|
|
|
|
// If the result is ignored, don't copy from the value.
|
|
|
|
if (DestPtr == 0)
|
|
|
|
// FIXME: If the source is volatile, we must read from it.
|
|
|
|
return;
|
|
|
|
|
2007-08-27 07:13:56 +08:00
|
|
|
EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
|
2007-08-11 08:04:45 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-14 14:28:57 +08:00
|
|
|
void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
|
|
|
|
{
|
|
|
|
QualType STy = E->getSubExpr()->getType().getCanonicalType();
|
|
|
|
QualType Ty = E->getType().getCanonicalType();
|
2008-02-11 09:09:17 +08:00
|
|
|
|
|
|
|
assert(CGF.getContext().typesAreCompatible(
|
|
|
|
STy.getUnqualifiedType(), Ty.getUnqualifiedType())
|
|
|
|
&& "Implicit cast types must be compatible");
|
2008-01-14 14:28:57 +08:00
|
|
|
|
|
|
|
Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
|
2007-11-01 06:04:46 +08:00
|
|
|
void AggExprEmitter::VisitCallExpr(const CallExpr *E)
|
|
|
|
{
|
|
|
|
RValue RV = CGF.EmitCallExpr(E);
|
|
|
|
assert(RV.isAggregate() && "Return value must be aggregate value!");
|
|
|
|
|
|
|
|
// If the result is ignored, don't copy from the value.
|
|
|
|
if (DestPtr == 0)
|
2008-01-31 13:38:29 +08:00
|
|
|
// FIXME: If the source is volatile, we must read from it.
|
|
|
|
return;
|
|
|
|
|
|
|
|
EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
|
|
|
|
{
|
|
|
|
RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
|
|
|
|
E->getNumArgs(CGF.getContext()));
|
|
|
|
assert(RV.isAggregate() && "Return value must be aggregate value!");
|
|
|
|
|
|
|
|
// If the result is ignored, don't copy from the value.
|
|
|
|
if (DestPtr == 0)
|
2007-11-01 06:04:46 +08:00
|
|
|
// FIXME: If the source is volatile, we must read from it.
|
|
|
|
return;
|
|
|
|
|
|
|
|
EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
|
|
|
|
}
|
|
|
|
|
2007-09-01 06:54:14 +08:00
|
|
|
void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
|
|
|
|
CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
|
|
|
|
}
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
|
2007-12-02 09:49:16 +08:00
|
|
|
CGF.WarnUnsupported(E, "aggregate binary expression");
|
2007-08-21 12:59:27 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:43:17 +08:00
|
|
|
void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
|
2008-02-11 09:09:17 +08:00
|
|
|
// For an assignment to work, the value on the right has
|
|
|
|
// to be compatible with the value on the left.
|
|
|
|
assert(CGF.getContext().typesAreCompatible(
|
|
|
|
E->getLHS()->getType().getUnqualifiedType(),
|
|
|
|
E->getRHS()->getType().getUnqualifiedType())
|
|
|
|
&& "Invalid assignment");
|
2007-08-21 12:25:47 +08:00
|
|
|
LValue LHS = CGF.EmitLValue(E->getLHS());
|
2007-08-11 08:04:45 +08:00
|
|
|
|
|
|
|
// Codegen the RHS so that it stores directly into the LHS.
|
2007-08-21 12:25:47 +08:00
|
|
|
CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2008-02-11 09:09:17 +08:00
|
|
|
if (DestPtr == 0)
|
|
|
|
return;
|
|
|
|
|
2007-08-11 08:04:45 +08:00
|
|
|
// If the result of the assignment is used, copy the RHS there also.
|
2008-02-11 09:09:17 +08:00
|
|
|
EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
|
2007-08-11 08:04:45 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
|
2008-04-07 04:42:52 +08:00
|
|
|
llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
|
|
|
|
llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
|
|
|
|
llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
|
2007-08-27 07:13:56 +08:00
|
|
|
Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
CGF.EmitBlock(LHSBlock);
|
2007-08-11 08:04:45 +08:00
|
|
|
|
|
|
|
// Handle the GNU extension for missing LHS.
|
|
|
|
assert(E->getLHS() && "Must have LHS for aggregate value");
|
|
|
|
|
2007-08-21 13:02:10 +08:00
|
|
|
Visit(E->getLHS());
|
2007-08-27 07:13:56 +08:00
|
|
|
Builder.CreateBr(ContBlock);
|
|
|
|
LHSBlock = Builder.GetInsertBlock();
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
CGF.EmitBlock(RHSBlock);
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2007-08-21 13:02:10 +08:00
|
|
|
Visit(E->getRHS());
|
2007-08-27 07:13:56 +08:00
|
|
|
Builder.CreateBr(ContBlock);
|
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
CGF.EmitBlock(ContBlock);
|
2007-08-11 08:04:45 +08:00
|
|
|
}
|
2007-08-21 12:59:27 +08:00
|
|
|
|
2008-02-20 06:04:22 +08:00
|
|
|
void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
|
2008-02-19 06:44:02 +08:00
|
|
|
|
2008-02-20 06:04:22 +08:00
|
|
|
const llvm::PointerType *APType =
|
|
|
|
cast<llvm::PointerType>(DestPtr->getType());
|
|
|
|
const llvm::Type *DestType = APType->getElementType();
|
2008-02-19 06:44:02 +08:00
|
|
|
|
2008-02-20 06:04:22 +08:00
|
|
|
if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
|
|
|
|
unsigned NumInitElements = E->getNumInits();
|
2008-02-19 06:44:02 +08:00
|
|
|
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != NumInitElements; ++i) {
|
2008-03-19 13:19:41 +08:00
|
|
|
llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
|
2008-02-20 06:04:22 +08:00
|
|
|
Expr *Init = E->getInit(i);
|
|
|
|
if (isa<InitListExpr>(Init))
|
|
|
|
CGF.EmitAggExpr(Init, NextVal, VolatileDest);
|
|
|
|
else
|
|
|
|
Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
|
2008-02-19 06:44:02 +08:00
|
|
|
}
|
2007-10-27 01:44:44 +08:00
|
|
|
|
2008-02-19 06:44:02 +08:00
|
|
|
// Emit remaining default initializers
|
|
|
|
unsigned NumArrayElements = AType->getNumElements();
|
2008-02-20 06:04:22 +08:00
|
|
|
QualType QType = E->getInit(0)->getType();
|
|
|
|
const llvm::Type *EType = AType->getElementType();
|
2008-02-19 06:44:02 +08:00
|
|
|
for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
|
2008-03-19 13:19:41 +08:00
|
|
|
llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
|
2008-02-20 06:04:22 +08:00
|
|
|
if (EType->isFirstClassType())
|
|
|
|
Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
|
|
|
|
else
|
|
|
|
EmitAggregateClear(NextVal, QType);
|
2008-02-19 06:44:02 +08:00
|
|
|
}
|
2008-02-20 06:04:22 +08:00
|
|
|
} else
|
|
|
|
assert(false && "Invalid initializer");
|
2008-02-19 06:44:02 +08:00
|
|
|
}
|
2007-10-27 01:44:44 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
|
|
|
|
// FIXME: Are initializers affected by volatile?
|
|
|
|
if (E->getType()->isComplexType()) {
|
|
|
|
CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
|
2008-05-12 23:06:05 +08:00
|
|
|
} else if (CGF.hasAggregateLLVMType(E->getType())) {
|
|
|
|
CGF.EmitAnyExpr(E, LV.getAddress(), false);
|
|
|
|
} else {
|
|
|
|
CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2008-02-19 06:44:02 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
|
|
|
|
if (!CGF.hasAggregateLLVMType(T)) {
|
|
|
|
// For non-aggregates, we can store zero
|
|
|
|
const llvm::Type *T =
|
|
|
|
cast<llvm::PointerType>(LV.getAddress()->getType())->getElementType();
|
|
|
|
Builder.CreateStore(llvm::Constant::getNullValue(T), LV.getAddress());
|
|
|
|
} else {
|
|
|
|
// Otherwise, just memset the whole thing to zero. This is legal
|
|
|
|
// because in LLVM, all default initializers are guaranteed to have a
|
|
|
|
// bit pattern of all zeros.
|
|
|
|
// There's a potential optimization opportunity in combining
|
|
|
|
// memsets; that would be easy for arrays, but relatively
|
|
|
|
// difficult for structures with the current code.
|
|
|
|
llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
|
|
|
|
uint64_t Size = CGF.getContext().getTypeSize(T);
|
|
|
|
|
|
|
|
const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
|
2008-05-06 08:56:42 +08:00
|
|
|
Builder.CreateCall4(MemSet, DestPtr,
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2008-02-19 06:44:02 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
|
|
|
|
void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
|
|
|
|
if (E->isConstantExpr(CGF.getContext(), 0)) {
|
|
|
|
// FIXME: call into const expr emitter so that we can emit
|
|
|
|
// a memcpy instead of storing the individual members.
|
|
|
|
// This is purely for perf; both codepaths lead to equivalent
|
|
|
|
// (although not necessarily identical) code.
|
|
|
|
// It's worth noting that LLVM keeps on getting smarter, though,
|
|
|
|
// so it might not be worth bothering.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle initialization of an array.
|
|
|
|
if (E->getType()->isArrayType()) {
|
|
|
|
const llvm::PointerType *APType =
|
|
|
|
cast<llvm::PointerType>(DestPtr->getType());
|
|
|
|
const llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(APType->getElementType());
|
|
|
|
|
|
|
|
uint64_t NumInitElements = E->getNumInits();
|
|
|
|
uint64_t NumArrayElements = AType->getNumElements();
|
|
|
|
QualType ElementType = E->getType()->getAsArrayType()->getElementType();
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i != NumArrayElements; ++i) {
|
|
|
|
llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
|
|
|
|
if (i < NumInitElements)
|
|
|
|
EmitInitializationToLValue(E->getInit(i), LValue::MakeAddr(NextVal));
|
|
|
|
else
|
|
|
|
EmitNullInitializationToLValue(LValue::MakeAddr(NextVal),
|
|
|
|
ElementType);
|
|
|
|
}
|
2008-02-19 06:44:02 +08:00
|
|
|
return;
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(E->getType()->isRecordType() && "Only support structs/unions here!");
|
|
|
|
|
|
|
|
// Do struct initialization; this code just sets each individual member
|
|
|
|
// to the approprate value. This makes bitfield support automatic;
|
|
|
|
// the disadvantage is that the generated code is more difficult for
|
|
|
|
// the optimizer, especially with bitfields.
|
|
|
|
unsigned NumInitElements = E->getNumInits();
|
|
|
|
RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
|
|
|
|
unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
|
|
|
|
unsigned CurInitVal = 0;
|
|
|
|
bool isUnion = E->getType()->isUnionType();
|
|
|
|
|
|
|
|
// Here we iterate over the fields; this makes it simpler to both
|
|
|
|
// default-initialize fields and skip over unnamed fields.
|
|
|
|
for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
|
|
|
|
if (CurInitVal >= NumInitElements) {
|
|
|
|
// No more initializers; we're done.
|
|
|
|
break;
|
2008-02-20 03:27:31 +08:00
|
|
|
}
|
2008-04-05 02:42:16 +08:00
|
|
|
|
|
|
|
FieldDecl *CurField = SD->getMember(CurFieldNo);
|
|
|
|
if (CurField->getIdentifier() == 0) {
|
|
|
|
// Initializers can't initialize unnamed fields, e.g. "int : 20;"
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion);
|
|
|
|
if (CurInitVal < NumInitElements) {
|
|
|
|
// Store the initializer into the field
|
|
|
|
// This will probably have to get a bit smarter when we support
|
|
|
|
// designators in initializers
|
|
|
|
EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
|
|
|
|
} else {
|
|
|
|
// We're out of initalizers; default-initialize to null
|
|
|
|
EmitNullInitializationToLValue(FieldLoc, CurField->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unions only initialize one field.
|
|
|
|
// (things can get weird with designators, but they aren't
|
|
|
|
// supported yet.)
|
|
|
|
if (E->getType()->isUnionType())
|
|
|
|
break;
|
2008-02-20 03:27:31 +08:00
|
|
|
}
|
2007-10-27 01:44:44 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Entry Points into this File
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// EmitAggExpr - Emit the computation of the specified expression of
|
|
|
|
/// aggregate type. The result is computed into DestPtr. Note that if
|
|
|
|
/// DestPtr is null, the value of the aggregate expression is not needed.
|
|
|
|
void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
|
|
|
|
bool VolatileDest) {
|
|
|
|
assert(E && hasAggregateLLVMType(E->getType()) &&
|
|
|
|
"Invalid aggregate expression to emit");
|
|
|
|
|
|
|
|
AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
|
|
|
|
}
|