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"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-04-17 08:06:03 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2007-08-11 08:04:45 +08:00
|
|
|
#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-11-01 09:53:16 +08:00
|
|
|
CGBuilderTy &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);
|
2008-05-20 01:51:16 +08:00
|
|
|
|
2009-05-24 04:28:01 +08:00
|
|
|
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
|
|
|
|
void EmitFinalDestCopy(const Expr *E, LValue Src);
|
|
|
|
void EmitFinalDestCopy(const Expr *E, RValue Src);
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void VisitStmt(Stmt *S) {
|
2008-08-16 08:56:44 +08:00
|
|
|
CGF.ErrorUnsupported(S, "aggregate expression");
|
2007-08-21 12:25:47 +08:00
|
|
|
}
|
|
|
|
void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
|
2009-01-27 17:03:41 +08:00
|
|
|
void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
|
2007-08-21 12:25:47 +08:00
|
|
|
|
|
|
|
// 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); }
|
2009-04-22 07:00:09 +08:00
|
|
|
void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
}
|
2007-12-14 10:04:12 +08:00
|
|
|
void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
}
|
2009-04-22 07:00:09 +08:00
|
|
|
void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
}
|
|
|
|
void VisitPredefinedExpr(const PredefinedExpr *E) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
}
|
2009-01-29 05:54:33 +08:00
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
// Operators.
|
2009-01-16 04:14:33 +08:00
|
|
|
void VisitCStyleCastExpr(CStyleCastExpr *E);
|
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-05-20 15:56:31 +08:00
|
|
|
void VisitBinComma(const BinaryOperator *E);
|
2007-08-21 12:25:47 +08:00
|
|
|
|
2008-06-25 01:04:18 +08:00
|
|
|
void VisitObjCMessageExpr(ObjCMessageExpr *E);
|
2008-08-23 18:51:21 +08:00
|
|
|
void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
|
|
|
|
EmitAggLoadOfLValue(E);
|
|
|
|
}
|
2008-08-27 14:57:25 +08:00
|
|
|
void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
|
2008-11-23 02:39:36 +08:00
|
|
|
void VisitObjCKVCRefExpr(ObjCKVCRefExpr *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());
|
|
|
|
}
|
2009-05-04 01:47:16 +08:00
|
|
|
void VisitCXXConstructExpr(const CXXConstructExpr *E);
|
2009-05-19 12:48:36 +08:00
|
|
|
void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
|
|
|
|
|
2008-05-27 23:51:49 +08:00
|
|
|
void VisitVAArgExpr(VAArgExpr *E);
|
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
|
|
|
|
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);
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(E, LV);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
|
|
|
|
void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src) {
|
|
|
|
assert(Src.isAggregate() && "value must be aggregate value!");
|
|
|
|
|
2007-08-11 08:04:45 +08:00
|
|
|
// If the result is ignored, don't copy from the value.
|
2009-05-24 06:01:27 +08:00
|
|
|
if (DestPtr == 0) {
|
|
|
|
if (Src.isVolatileQualified())
|
|
|
|
// If the source is volatile, we must read from it; to do that, we need
|
|
|
|
// some place to put it.
|
|
|
|
DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2007-08-11 08:04:45 +08:00
|
|
|
|
2009-05-24 04:28:01 +08:00
|
|
|
// If the result of the assignment is used, copy the LHS there also.
|
|
|
|
// FIXME: Pass VolatileDest as well. I think we also need to merge volatile
|
|
|
|
// from the source as well, as we can't eliminate it if either operand
|
|
|
|
// is volatile, unless copy has volatile for both source and destination..
|
2009-05-24 06:29:41 +08:00
|
|
|
CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
|
|
|
|
VolatileDest|Src.isVolatileQualified());
|
2009-05-24 04:28:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
|
|
|
|
void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src) {
|
|
|
|
assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
|
|
|
|
|
|
|
|
EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
|
|
|
|
Src.isVolatileQualified()));
|
2007-08-11 08:04:45 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:59:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-01-16 04:14:33 +08:00
|
|
|
void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
|
|
|
|
// GCC union extension
|
|
|
|
if (E->getType()->isUnionType()) {
|
|
|
|
RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
|
2009-04-10 05:40:53 +08:00
|
|
|
LValue FieldLoc = CGF.EmitLValueForField(DestPtr,
|
|
|
|
*SD->field_begin(CGF.getContext()),
|
|
|
|
true, 0);
|
2009-01-16 04:14:33 +08:00
|
|
|
EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
|
2008-02-11 09:09:17 +08:00
|
|
|
assert(CGF.getContext().typesAreCompatible(
|
2008-07-27 06:37:01 +08:00
|
|
|
E->getSubExpr()->getType().getUnqualifiedType(),
|
|
|
|
E->getType().getUnqualifiedType()) &&
|
|
|
|
"Implicit cast types must be compatible");
|
2008-01-14 14:28:57 +08:00
|
|
|
Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
|
2007-11-01 06:04:46 +08:00
|
|
|
RValue RV = CGF.EmitCallExpr(E);
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(E, RV);
|
2008-01-31 13:38:29 +08:00
|
|
|
}
|
2008-07-27 06:37:01 +08:00
|
|
|
|
|
|
|
void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
|
2008-08-23 11:46:30 +08:00
|
|
|
RValue RV = CGF.EmitObjCMessageExpr(E);
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(E, RV);
|
2008-06-25 01:04:18 +08:00
|
|
|
}
|
2008-01-31 13:38:29 +08:00
|
|
|
|
2008-08-27 14:57:25 +08:00
|
|
|
void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
|
|
|
|
RValue RV = CGF.EmitObjCPropertyGet(E);
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(E, RV);
|
2008-11-23 02:39:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
|
|
|
|
RValue RV = CGF.EmitObjCPropertyGet(E);
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(E, RV);
|
2007-11-01 06:04:46 +08:00
|
|
|
}
|
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
|
2009-05-24 06:01:27 +08:00
|
|
|
CGF.EmitAnyExpr(E->getLHS());
|
2009-05-24 05:40:07 +08:00
|
|
|
CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest);
|
2008-05-20 15:56:31 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2008-08-16 08:56:44 +08:00
|
|
|
CGF.ErrorUnsupported(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
|
|
|
|
2008-08-30 13:35:15 +08:00
|
|
|
// We have to special case property setters, otherwise we must have
|
|
|
|
// a simple lvalue (no aggregates inside vectors, bitfields).
|
|
|
|
if (LHS.isPropertyRef()) {
|
|
|
|
// FIXME: Volatility?
|
|
|
|
llvm::Value *AggLoc = DestPtr;
|
|
|
|
if (!AggLoc)
|
|
|
|
AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
|
|
|
|
CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
|
|
|
|
CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
|
|
|
|
RValue::getAggregate(AggLoc));
|
2008-11-23 06:30:21 +08:00
|
|
|
}
|
|
|
|
else if (LHS.isKVCRef()) {
|
|
|
|
// FIXME: Volatility?
|
|
|
|
llvm::Value *AggLoc = DestPtr;
|
|
|
|
if (!AggLoc)
|
|
|
|
AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
|
|
|
|
CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
|
|
|
|
CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
|
|
|
|
RValue::getAggregate(AggLoc));
|
2008-08-30 13:35:15 +08:00
|
|
|
} else {
|
|
|
|
// Codegen the RHS so that it stores directly into the LHS.
|
2009-05-23 12:13:59 +08:00
|
|
|
CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(E, LHS);
|
2008-08-30 13:35:15 +08:00
|
|
|
}
|
2007-08-11 08:04:45 +08:00
|
|
|
}
|
|
|
|
|
2007-08-21 12:25:47 +08:00
|
|
|
void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
|
|
|
|
llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
|
|
|
|
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
|
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());
|
2008-11-11 17:41:28 +08:00
|
|
|
CGF.EmitBranch(ContBlock);
|
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());
|
2008-11-11 17:41:28 +08:00
|
|
|
CGF.EmitBranch(ContBlock);
|
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-05-27 23:51:49 +08:00
|
|
|
void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
|
2009-02-12 06:25:55 +08:00
|
|
|
llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
|
2008-11-04 13:30:00 +08:00
|
|
|
llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
|
|
|
|
|
2009-01-10 05:09:38 +08:00
|
|
|
if (!ArgPtr) {
|
2008-11-04 13:30:00 +08:00
|
|
|
CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
|
2009-01-10 05:09:38 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
|
2008-05-27 23:51:49 +08:00
|
|
|
}
|
|
|
|
|
2009-04-17 08:06:03 +08:00
|
|
|
void
|
2009-05-04 01:47:16 +08:00
|
|
|
AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
|
2009-05-19 12:48:36 +08:00
|
|
|
llvm::Value *V = DestPtr;
|
|
|
|
|
|
|
|
if (!V) {
|
|
|
|
assert(isa<CXXTempVarDecl>(E->getVarDecl()) &&
|
|
|
|
"Must have a temp var decl when there's no destination!");
|
|
|
|
|
|
|
|
V = CGF.CreateTempAlloca(CGF.ConvertType(E->getVarDecl()->getType()),
|
|
|
|
"tmpvar");
|
|
|
|
}
|
2009-04-17 08:06:03 +08:00
|
|
|
|
2009-05-19 12:48:36 +08:00
|
|
|
CGF.EmitCXXConstructExpr(V, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
|
|
|
|
// FIXME: Do something with the temporaries!
|
|
|
|
Visit(E->getSubExpr());
|
2009-04-17 08:06:03 +08:00
|
|
|
}
|
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
|
|
|
|
// FIXME: Are initializers affected by volatile?
|
2009-01-30 01:44:32 +08:00
|
|
|
if (isa<ImplicitValueInitExpr>(E)) {
|
2009-01-29 05:54:33 +08:00
|
|
|
EmitNullInitializationToLValue(LV, E->getType());
|
2009-01-30 01:44:32 +08:00
|
|
|
} else 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
|
2008-08-06 13:32:55 +08:00
|
|
|
llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
|
|
|
|
CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
|
2008-04-05 02:42:16 +08:00
|
|
|
} 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.
|
2009-04-14 05:47:26 +08:00
|
|
|
// FIXME: That isn't true for member pointers!
|
2008-04-05 02:42:16 +08:00
|
|
|
// There's a potential optimization opportunity in combining
|
|
|
|
// memsets; that would be easy for arrays, but relatively
|
|
|
|
// difficult for structures with the current code.
|
2009-03-28 11:10:45 +08:00
|
|
|
CGF.EmitMemSetToZero(LV.getAddress(), T);
|
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) {
|
2008-12-02 09:17:45 +08:00
|
|
|
#if 0
|
|
|
|
// FIXME: Disabled while we figure out what to do about
|
|
|
|
// test/CodeGen/bitfield.c
|
|
|
|
//
|
2009-05-16 15:57:57 +08:00
|
|
|
// If we can, prefer a copy from a global; this is a lot less code for long
|
|
|
|
// globals, and it's easier for the current optimizers to analyze.
|
|
|
|
// FIXME: Should we really be doing this? Should we try to avoid cases where
|
|
|
|
// we emit a global with a lot of zeros? Should we try to avoid short
|
|
|
|
// globals?
|
2009-01-25 10:32:41 +08:00
|
|
|
if (E->isConstantInitializer(CGF.getContext(), 0)) {
|
2008-11-30 10:11:09 +08:00
|
|
|
llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
|
|
|
|
llvm::GlobalVariable* GV =
|
|
|
|
new llvm::GlobalVariable(C->getType(), true,
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
|
|
|
C, "", &CGF.CGM.getModule(), 0);
|
2009-05-24 04:28:01 +08:00
|
|
|
EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
|
2008-11-30 10:11:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-12-02 09:17:45 +08:00
|
|
|
#endif
|
2009-01-30 03:42:23 +08:00
|
|
|
if (E->hadArrayRangeDesignator()) {
|
|
|
|
CGF.ErrorUnsupported(E, "GNU array range designator extension");
|
|
|
|
}
|
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
// 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();
|
2008-05-20 01:51:16 +08:00
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
if (E->getNumInits() > 0) {
|
|
|
|
QualType T1 = E->getType();
|
|
|
|
QualType T2 = E->getInit(0)->getType();
|
|
|
|
if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
|
|
|
|
CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
|
|
|
|
EmitAggLoadOfLValue(E->getInit(0));
|
|
|
|
return;
|
|
|
|
}
|
2008-05-20 01:51:16 +08:00
|
|
|
}
|
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
uint64_t NumArrayElements = AType->getNumElements();
|
2008-08-04 15:31:14 +08:00
|
|
|
QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
|
2009-01-29 05:54:33 +08:00
|
|
|
ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
|
2008-04-05 02:42:16 +08:00
|
|
|
|
2008-08-04 15:31:14 +08:00
|
|
|
unsigned CVRqualifier = ElementType.getCVRQualifiers();
|
2008-06-14 07:01:12 +08:00
|
|
|
|
2008-04-05 02:42:16 +08:00
|
|
|
for (uint64_t i = 0; i != NumArrayElements; ++i) {
|
|
|
|
llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
|
|
|
|
if (i < NumInitElements)
|
2008-06-14 07:01:12 +08:00
|
|
|
EmitInitializationToLValue(E->getInit(i),
|
|
|
|
LValue::MakeAddr(NextVal, CVRqualifier));
|
2008-04-05 02:42:16 +08:00
|
|
|
else
|
2008-06-14 07:01:12 +08:00
|
|
|
EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
|
2008-04-05 02:42:16 +08:00
|
|
|
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 CurInitVal = 0;
|
2009-01-30 00:53:55 +08:00
|
|
|
|
|
|
|
if (E->getType()->isUnionType()) {
|
|
|
|
// Only initialize one field of a union. The field itself is
|
|
|
|
// specified by the initializer list.
|
|
|
|
if (!E->getInitializedFieldInUnion()) {
|
|
|
|
// Empty union; we have nothing to do.
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Make sure that it's really an empty and not a failure of
|
|
|
|
// semantic analysis.
|
2009-04-10 05:40:53 +08:00
|
|
|
for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
|
|
|
|
FieldEnd = SD->field_end(CGF.getContext());
|
2009-01-30 00:53:55 +08:00
|
|
|
Field != FieldEnd; ++Field)
|
|
|
|
assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: volatility
|
|
|
|
FieldDecl *Field = E->getInitializedFieldInUnion();
|
|
|
|
LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
|
|
|
|
|
|
|
|
if (NumInitElements) {
|
|
|
|
// Store the initializer into the field
|
|
|
|
EmitInitializationToLValue(E->getInit(0), FieldLoc);
|
|
|
|
} else {
|
|
|
|
// Default-initialize to null
|
|
|
|
EmitNullInitializationToLValue(FieldLoc, Field->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2008-04-05 02:42:16 +08:00
|
|
|
|
|
|
|
// Here we iterate over the fields; this makes it simpler to both
|
|
|
|
// default-initialize fields and skip over unnamed fields.
|
2009-04-10 05:40:53 +08:00
|
|
|
for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
|
|
|
|
FieldEnd = SD->field_end(CGF.getContext());
|
2008-12-12 00:49:14 +08:00
|
|
|
Field != FieldEnd; ++Field) {
|
|
|
|
// We're done once we hit the flexible array member
|
|
|
|
if (Field->getType()->isIncompleteArrayType())
|
|
|
|
break;
|
|
|
|
|
2009-01-29 07:36:17 +08:00
|
|
|
if (Field->isUnnamedBitfield())
|
2008-04-05 02:42:16 +08:00
|
|
|
continue;
|
2009-01-29 07:36:17 +08:00
|
|
|
|
2008-06-14 07:01:12 +08:00
|
|
|
// FIXME: volatility
|
2009-01-30 00:53:55 +08:00
|
|
|
LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
|
2008-04-05 02:42:16 +08:00
|
|
|
if (CurInitVal < NumInitElements) {
|
|
|
|
// Store the initializer into the field
|
|
|
|
EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
|
|
|
|
} else {
|
|
|
|
// We're out of initalizers; default-initialize to null
|
2008-12-12 00:49:14 +08:00
|
|
|
EmitNullInitializationToLValue(FieldLoc, Field->getType());
|
2008-04-05 02:42:16 +08:00
|
|
|
}
|
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));
|
|
|
|
}
|
2008-09-10 04:49:46 +08:00
|
|
|
|
|
|
|
void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
|
|
|
|
assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
|
|
|
|
|
|
|
|
EmitMemSetToZero(DestPtr, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
|
2009-05-24 06:29:41 +08:00
|
|
|
llvm::Value *SrcPtr, QualType Ty,
|
|
|
|
bool isVolatile) {
|
2008-09-10 04:49:46 +08:00
|
|
|
assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
|
|
|
|
|
2009-03-01 02:31:01 +08:00
|
|
|
// Aggregate assignment turns into llvm.memcpy. This is almost valid per
|
2009-03-01 02:18:58 +08:00
|
|
|
// C99 6.5.16.1p3, which states "If the value being stored in an object is
|
|
|
|
// read from another object that overlaps in anyway the storage of the first
|
|
|
|
// object, then the overlap shall be exact and the two objects shall have
|
|
|
|
// qualified or unqualified versions of a compatible type."
|
|
|
|
//
|
2009-03-01 02:31:01 +08:00
|
|
|
// memcpy is not defined if the source and destination pointers are exactly
|
2009-03-01 02:18:58 +08:00
|
|
|
// equal, but other compilers do this optimization, and almost every memcpy
|
|
|
|
// implementation handles this case safely. If there is a libc that does not
|
|
|
|
// safely handle this, we can add a target hook.
|
2008-09-10 04:49:46 +08:00
|
|
|
const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
|
|
|
|
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.
|
|
|
|
std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
|
|
|
|
|
|
|
|
// FIXME: Handle variable sized types.
|
|
|
|
const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
|
|
|
|
|
2009-05-23 12:13:59 +08:00
|
|
|
// FIXME: If we have a volatile struct, the optimizer can remove what might
|
|
|
|
// appear to be `extra' memory ops:
|
|
|
|
//
|
|
|
|
// volatile struct { int i; } a, b;
|
|
|
|
//
|
|
|
|
// int main() {
|
|
|
|
// a = b;
|
|
|
|
// a = b;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// either, we need to use a differnt call here, or the backend needs to be
|
2009-05-24 06:29:41 +08:00
|
|
|
// taught to not do this. We use isVolatile to indicate when either the
|
|
|
|
// source or the destination is volatile.
|
2009-03-01 02:18:58 +08:00
|
|
|
Builder.CreateCall4(CGM.getMemCpyFn(),
|
2008-09-10 04:49:46 +08:00
|
|
|
DestPtr, SrcPtr,
|
|
|
|
// TypeInfo.first describes size in bits.
|
|
|
|
llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int32Ty,
|
|
|
|
TypeInfo.second/8));
|
|
|
|
}
|