2007-08-24 06:22:53 +04:00
|
|
|
//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
|
2007-08-11 00:13:28 +04:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Aggregate Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2007-08-11 04:04:45 +04:00
|
|
|
#include "CodeGenModule.h"
|
|
|
|
#include "clang/AST/AST.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
2007-08-21 08:25:47 +04:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-08-11 00:13:28 +04:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
2007-08-11 04:04:45 +04:00
|
|
|
|
2007-08-21 08:25:47 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Aggregate Expression Emitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
|
|
|
|
CodeGenFunction &CGF;
|
2007-10-09 23:49:58 +04:00
|
|
|
llvm::LLVMFoldingBuilder &Builder;
|
2007-08-21 08:25:47 +04:00
|
|
|
llvm::Value *DestPtr;
|
|
|
|
bool VolatileDest;
|
|
|
|
public:
|
|
|
|
AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
|
2007-08-27 03:13:56 +04:00
|
|
|
: CGF(cgf), Builder(CGF.Builder),
|
|
|
|
DestPtr(destPtr), VolatileDest(volatileDest) {
|
2007-08-21 08:25:47 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 08:59:27 +04:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-08-21 08:25:47 +04: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 03:13:56 +04:00
|
|
|
void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
|
|
|
|
QualType EltTy);
|
2007-08-21 08:59:27 +04:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-08-21 08:25:47 +04:00
|
|
|
void VisitStmt(Stmt *S) {
|
|
|
|
fprintf(stderr, "Unimplemented agg expr!\n");
|
2007-09-13 05:17:29 +04:00
|
|
|
S->dump(CGF.getContext().SourceMgr);
|
2007-08-21 08:25:47 +04:00
|
|
|
}
|
|
|
|
void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
|
|
|
|
|
|
|
|
// l-values.
|
|
|
|
void VisitDeclRefExpr(DeclRefExpr *DRE) { return EmitAggLoadOfLValue(DRE); }
|
|
|
|
// case Expr::ArraySubscriptExprClass:
|
|
|
|
|
|
|
|
// Operators.
|
|
|
|
// case Expr::UnaryOperatorClass:
|
|
|
|
// case Expr::ImplicitCastExprClass:
|
|
|
|
// case Expr::CastExprClass:
|
|
|
|
// case Expr::CallExprClass:
|
2007-09-01 02:54:14 +04:00
|
|
|
void VisitStmtExpr(const StmtExpr *E);
|
2007-08-21 08:25:47 +04:00
|
|
|
void VisitBinaryOperator(const BinaryOperator *BO);
|
2007-08-21 08:43:17 +04:00
|
|
|
void VisitBinAssign(const BinaryOperator *E);
|
2007-08-21 08:25:47 +04:00
|
|
|
|
|
|
|
|
|
|
|
void VisitConditionalOperator(const ConditionalOperator *CO);
|
|
|
|
// case Expr::ChooseExprClass:
|
|
|
|
};
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2007-08-21 08:59:27 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-08-21 08:25:47 +04:00
|
|
|
|
2007-08-27 03:13:56 +04:00
|
|
|
void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
|
|
|
|
llvm::Value *SrcPtr, QualType Ty) {
|
|
|
|
assert(!Ty->isComplexType() && "Shouldn't happen for complex");
|
|
|
|
|
|
|
|
// Aggregate assignment turns into llvm.memcpy.
|
|
|
|
const llvm::Type *BP = llvm::PointerType::get(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 =
|
|
|
|
CGF.getContext().getTypeInfo(Ty, SourceLocation());
|
|
|
|
|
|
|
|
// FIXME: Handle variable sized types.
|
|
|
|
const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
|
|
|
|
|
|
|
|
llvm::Value *MemCpyOps[4] = {
|
|
|
|
DestPtr, SrcPtr,
|
|
|
|
llvm::ConstantInt::get(IntPtr, TypeInfo.first),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second)
|
|
|
|
};
|
|
|
|
|
|
|
|
Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 04:04:45 +04: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 08:25:47 +04:00
|
|
|
void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
|
|
|
|
LValue LV = CGF.EmitLValue(E);
|
2007-08-11 04:04:45 +04: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 03:13:56 +04:00
|
|
|
EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
|
2007-08-11 04:04:45 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 08:59:27 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-09-01 02:54:14 +04:00
|
|
|
void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
|
|
|
|
CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
|
|
|
|
}
|
|
|
|
|
2007-08-21 08:25:47 +04:00
|
|
|
void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
|
2007-08-21 08:43:17 +04:00
|
|
|
fprintf(stderr, "Unimplemented aggregate binary expr!\n");
|
2007-09-13 05:17:29 +04:00
|
|
|
E->dump(CGF.getContext().SourceMgr);
|
2007-08-21 08:59:27 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 08:43:17 +04:00
|
|
|
void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
|
2007-08-11 04:04:45 +04:00
|
|
|
assert(E->getLHS()->getType().getCanonicalType() ==
|
|
|
|
E->getRHS()->getType().getCanonicalType() && "Invalid assignment");
|
2007-08-21 08:25:47 +04:00
|
|
|
LValue LHS = CGF.EmitLValue(E->getLHS());
|
2007-08-11 04:04:45 +04:00
|
|
|
|
|
|
|
// Codegen the RHS so that it stores directly into the LHS.
|
2007-08-21 08:25:47 +04:00
|
|
|
CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
|
2007-08-11 04:04:45 +04:00
|
|
|
|
|
|
|
// If the result of the assignment is used, copy the RHS there also.
|
|
|
|
if (DestPtr) {
|
|
|
|
assert(0 && "FIXME: Chained agg assignment not implemented yet");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-21 08:25:47 +04:00
|
|
|
void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
|
2007-08-11 04:04:45 +04:00
|
|
|
llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
|
|
|
|
llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
|
|
|
|
llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
|
|
|
|
|
2007-08-21 08:25:47 +04:00
|
|
|
llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
|
2007-08-27 03:13:56 +04:00
|
|
|
Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
|
2007-08-11 04:04:45 +04:00
|
|
|
|
2007-08-21 08:25:47 +04:00
|
|
|
CGF.EmitBlock(LHSBlock);
|
2007-08-11 04:04:45 +04:00
|
|
|
|
|
|
|
// Handle the GNU extension for missing LHS.
|
|
|
|
assert(E->getLHS() && "Must have LHS for aggregate value");
|
|
|
|
|
2007-08-21 09:02:10 +04:00
|
|
|
Visit(E->getLHS());
|
2007-08-27 03:13:56 +04:00
|
|
|
Builder.CreateBr(ContBlock);
|
|
|
|
LHSBlock = Builder.GetInsertBlock();
|
2007-08-11 04:04:45 +04:00
|
|
|
|
2007-08-21 08:25:47 +04:00
|
|
|
CGF.EmitBlock(RHSBlock);
|
2007-08-11 04:04:45 +04:00
|
|
|
|
2007-08-21 09:02:10 +04:00
|
|
|
Visit(E->getRHS());
|
2007-08-27 03:13:56 +04:00
|
|
|
Builder.CreateBr(ContBlock);
|
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
2007-08-11 04:04:45 +04:00
|
|
|
|
2007-08-21 08:25:47 +04:00
|
|
|
CGF.EmitBlock(ContBlock);
|
2007-08-11 04:04:45 +04:00
|
|
|
}
|
2007-08-21 08:59:27 +04: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));
|
|
|
|
}
|