2008-01-26 09:36:00 +08:00
|
|
|
//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Constant Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2008-10-06 13:59:01 +08:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-07-24 23:20:52 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-05-30 18:24:46 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
namespace {
|
2009-07-24 23:20:52 +08:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN ConstStructBuilder {
|
|
|
|
CodeGenModule &CGM;
|
|
|
|
CodeGenFunction *CGF;
|
|
|
|
|
|
|
|
bool Packed;
|
2009-07-25 08:24:56 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned NextFieldOffsetInBytes;
|
|
|
|
|
|
|
|
std::vector<llvm::Constant *> Elements;
|
|
|
|
|
|
|
|
ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
|
|
|
|
: CGM(CGM), CGF(CGF), Packed(false), NextFieldOffsetInBytes(0) { }
|
|
|
|
|
|
|
|
bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
|
|
|
|
const Expr *InitExpr) {
|
|
|
|
uint64_t FieldOffsetInBytes = FieldOffset / 8;
|
|
|
|
|
|
|
|
assert(NextFieldOffsetInBytes <= FieldOffsetInBytes
|
|
|
|
&& "Field offset mismatch!");
|
|
|
|
|
|
|
|
// Emit the field.
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(InitExpr, Field->getType(), CGF);
|
|
|
|
if (!C)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned FieldAlignment = getAlignment(C);
|
|
|
|
|
|
|
|
// Round up the field offset to the alignment of the field type.
|
|
|
|
uint64_t AlignedNextFieldOffsetInBytes =
|
|
|
|
llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
|
|
|
|
|
|
|
|
if (AlignedNextFieldOffsetInBytes > FieldOffsetInBytes) {
|
2009-07-25 09:40:29 +08:00
|
|
|
std::vector<llvm::Constant *> PackedElements;
|
|
|
|
|
|
|
|
assert(!Packed && "Alignment is wrong even with a packed struct!");
|
|
|
|
|
|
|
|
// Convert the struct to a packed struct.
|
|
|
|
uint64_t ElementOffsetInBytes = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
|
|
|
|
llvm::Constant *C = Elements[i];
|
|
|
|
|
|
|
|
unsigned ElementAlign =
|
|
|
|
CGM.getTargetData().getABITypeAlignment(C->getType());
|
|
|
|
uint64_t AlignedElementOffsetInBytes =
|
|
|
|
llvm::RoundUpToAlignment(ElementOffsetInBytes, ElementAlign);
|
|
|
|
|
|
|
|
if (AlignedElementOffsetInBytes > ElementOffsetInBytes) {
|
|
|
|
// We need some padding.
|
|
|
|
uint64_t NumBytes =
|
|
|
|
AlignedElementOffsetInBytes - ElementOffsetInBytes;
|
|
|
|
|
|
|
|
const llvm::Type *Ty = llvm::Type::Int8Ty;
|
|
|
|
if (NumBytes > 1)
|
|
|
|
Ty = CGM.getLLVMContext().getArrayType(Ty, NumBytes);
|
|
|
|
|
|
|
|
llvm::Constant *Padding = CGM.getLLVMContext().getNullValue(Ty);
|
|
|
|
PackedElements.push_back(Padding);
|
|
|
|
ElementOffsetInBytes += getSizeInBytes(Padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
PackedElements.push_back(C);
|
|
|
|
ElementOffsetInBytes += getSizeInBytes(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ElementOffsetInBytes == NextFieldOffsetInBytes &&
|
|
|
|
"Packing the struct changed its size!");
|
|
|
|
|
|
|
|
Elements = PackedElements;
|
|
|
|
Packed = true;
|
|
|
|
AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes;
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
|
|
|
|
// We need to append padding.
|
2009-07-25 09:40:29 +08:00
|
|
|
AppendPadding(FieldOffsetInBytes - NextFieldOffsetInBytes);
|
2009-07-24 23:20:52 +08:00
|
|
|
|
|
|
|
assert(NextFieldOffsetInBytes == FieldOffsetInBytes &&
|
|
|
|
"Did not add enough padding!");
|
|
|
|
|
|
|
|
AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the field.
|
|
|
|
Elements.push_back(C);
|
|
|
|
NextFieldOffsetInBytes = AlignedNextFieldOffsetInBytes + getSizeInBytes(C);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
|
|
|
|
const Expr *InitExpr) {
|
|
|
|
llvm::ConstantInt *CI =
|
|
|
|
cast_or_null<llvm::ConstantInt>(CGM.EmitConstantExpr(InitExpr,
|
|
|
|
Field->getType(),
|
|
|
|
CGF));
|
|
|
|
// FIXME: Can this ever happen?
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (FieldOffset > NextFieldOffsetInBytes * 8) {
|
2009-07-27 10:56:37 +08:00
|
|
|
// We need to add padding.
|
|
|
|
uint64_t NumBytes =
|
|
|
|
llvm::RoundUpToAlignment(FieldOffset -
|
|
|
|
NextFieldOffsetInBytes * 8, 8) / 8;
|
|
|
|
|
|
|
|
AppendPadding(NumBytes);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t FieldSize =
|
|
|
|
Field->getBitWidth()->EvaluateAsInt(CGM.getContext()).getZExtValue();
|
|
|
|
|
|
|
|
llvm::APInt FieldValue = CI->getValue();
|
|
|
|
|
|
|
|
// Promote the size of FieldValue if necessary
|
|
|
|
// FIXME: This should never occur, but currently it can because initializer
|
|
|
|
// constants are cast to bool, and because clang is not enforcing bitfield
|
|
|
|
// width limits.
|
|
|
|
if (FieldSize > FieldValue.getBitWidth())
|
|
|
|
FieldValue.zext(FieldSize);
|
|
|
|
|
|
|
|
// Truncate the size of FieldValue to the bit field size.
|
|
|
|
if (FieldSize < FieldValue.getBitWidth())
|
|
|
|
FieldValue.trunc(FieldSize);
|
|
|
|
|
|
|
|
if (FieldOffset < NextFieldOffsetInBytes * 8) {
|
2009-07-27 10:56:37 +08:00
|
|
|
// Either part of the field or the entire field can go into the previous
|
|
|
|
// byte.
|
|
|
|
assert(!Elements.empty() && "Elements can't be empty!");
|
|
|
|
|
|
|
|
unsigned BitsInPreviousByte =
|
|
|
|
NextFieldOffsetInBytes * 8 - FieldOffset;
|
|
|
|
|
|
|
|
bool FitsCompletelyInPreviousByte =
|
|
|
|
BitsInPreviousByte >= FieldValue.getBitWidth();
|
|
|
|
|
|
|
|
llvm::APInt Tmp = FieldValue;
|
|
|
|
|
|
|
|
if (!FitsCompletelyInPreviousByte) {
|
|
|
|
unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
|
|
|
|
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
Tmp = Tmp.lshr(NewFieldWidth);
|
|
|
|
Tmp.trunc(BitsInPreviousByte);
|
|
|
|
|
|
|
|
// We want the remaining high bits.
|
|
|
|
FieldValue.trunc(NewFieldWidth);
|
|
|
|
} else {
|
|
|
|
Tmp.trunc(BitsInPreviousByte);
|
|
|
|
|
|
|
|
// We want the remaining low bits.
|
|
|
|
FieldValue = FieldValue.lshr(BitsInPreviousByte);
|
|
|
|
FieldValue.trunc(NewFieldWidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Tmp.zext(8);
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
if (FitsCompletelyInPreviousByte)
|
|
|
|
Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
|
|
|
|
} else {
|
|
|
|
Tmp = Tmp.shl(8 - BitsInPreviousByte);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Or in the bits that go into the previous byte.
|
|
|
|
Tmp |= cast<llvm::ConstantInt>(Elements.back())->getValue();
|
|
|
|
Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
|
|
|
|
|
|
|
|
if (FitsCompletelyInPreviousByte)
|
|
|
|
return true;
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (FieldValue.getBitWidth() > 8) {
|
|
|
|
llvm::APInt Tmp;
|
|
|
|
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
// We want the high bits.
|
2009-07-27 10:56:37 +08:00
|
|
|
Tmp = FieldValue;
|
|
|
|
Tmp = Tmp.lshr(Tmp.getBitWidth() - 8);
|
|
|
|
Tmp.trunc(8);
|
|
|
|
} else {
|
|
|
|
// We want the low bits.
|
|
|
|
Tmp = FieldValue;
|
|
|
|
Tmp.trunc(8);
|
|
|
|
|
|
|
|
FieldValue = FieldValue.lshr(8);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
|
|
|
|
NextFieldOffsetInBytes++;
|
|
|
|
|
|
|
|
FieldValue.trunc(FieldValue.getBitWidth() - 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(FieldValue.getBitWidth() > 0 &&
|
|
|
|
"Should have at least one bit left!");
|
|
|
|
assert(FieldValue.getBitWidth() <= 8 &&
|
|
|
|
"Should not have more than a byte left!");
|
|
|
|
|
|
|
|
if (FieldValue.getBitWidth() < 8) {
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
unsigned BitWidth = FieldValue.getBitWidth();
|
|
|
|
|
|
|
|
FieldValue.zext(8);
|
|
|
|
FieldValue = FieldValue << (8 - BitWidth);
|
|
|
|
} else
|
|
|
|
FieldValue.zext(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the last element.
|
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
|
|
|
|
FieldValue));
|
|
|
|
NextFieldOffsetInBytes++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
void AppendPadding(uint64_t NumBytes) {
|
|
|
|
if (!NumBytes)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const llvm::Type *Ty = llvm::Type::Int8Ty;
|
|
|
|
if (NumBytes > 1)
|
|
|
|
Ty = CGM.getLLVMContext().getArrayType(Ty, NumBytes);
|
|
|
|
|
|
|
|
llvm::Constant *C = CGM.getLLVMContext().getNullValue(Ty);
|
|
|
|
Elements.push_back(C);
|
|
|
|
assert(getAlignment(C) == 1 && "Padding must have 1 byte alignment!");
|
|
|
|
|
|
|
|
NextFieldOffsetInBytes += getSizeInBytes(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendTailPadding(uint64_t RecordSize) {
|
|
|
|
assert(RecordSize % 8 == 0 && "Invalid record size!");
|
|
|
|
|
|
|
|
uint64_t RecordSizeInBytes = RecordSize / 8;
|
|
|
|
assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
|
|
|
|
|
|
|
|
unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
|
|
|
|
AppendPadding(NumPadBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Build(const InitListExpr *ILE) {
|
|
|
|
RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
unsigned ElementNo = 0;
|
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
|
|
|
ElementNo < ILE->getNumInits() && Field != FieldEnd;
|
|
|
|
++Field, ++FieldNo) {
|
|
|
|
if (Field->isBitField()) {
|
|
|
|
if (!Field->getIdentifier())
|
|
|
|
continue;
|
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
|
|
|
|
ILE->getInit(ElementNo)))
|
|
|
|
return false;
|
2009-07-24 23:20:52 +08:00
|
|
|
} else {
|
|
|
|
if (!AppendField(*Field, Layout.getFieldOffset(FieldNo),
|
|
|
|
ILE->getInit(ElementNo)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ElementNo++;
|
|
|
|
}
|
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
uint64_t LayoutSizeInBytes = Layout.getSize() / 8;
|
|
|
|
|
|
|
|
if (NextFieldOffsetInBytes > LayoutSizeInBytes) {
|
|
|
|
// If the struct is bigger than the size of the record type,
|
|
|
|
// we must have a flexible array member at the end.
|
|
|
|
assert(RD->hasFlexibleArrayMember() &&
|
|
|
|
"Must have flexible array member if struct is bigger than type!");
|
|
|
|
|
|
|
|
// No tail padding is necessary.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Append tail padding if necessary.
|
|
|
|
AppendTailPadding(Layout.getSize());
|
2009-07-25 08:24:56 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
assert(Layout.getSize() / 8 == NextFieldOffsetInBytes &&
|
|
|
|
"Tail padding mismatch!");
|
2009-07-25 08:24:56 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getAlignment(const llvm::Constant *C) const {
|
|
|
|
if (Packed)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return CGM.getTargetData().getABITypeAlignment(C->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t getSizeInBytes(const llvm::Constant *C) const {
|
|
|
|
return CGM.getTargetData().getTypeAllocSize(C->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
|
|
|
|
const InitListExpr *ILE) {
|
|
|
|
ConstStructBuilder Builder(CGM, CGF);
|
|
|
|
|
|
|
|
if (!Builder.Build(ILE))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
llvm::Constant *Result =
|
|
|
|
CGM.getLLVMContext().getConstantStruct(Builder.Elements, Builder.Packed);
|
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
assert(llvm::RoundUpToAlignment(Builder.NextFieldOffsetInBytes,
|
|
|
|
Builder.getAlignment(Result)) ==
|
|
|
|
Builder.getSizeInBytes(Result) && "Size mismatch!");
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2009-07-25 02:20:38 +08:00
|
|
|
return Result;
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-01-26 12:30:23 +08:00
|
|
|
class VISIBILITY_HIDDEN ConstExprEmitter :
|
|
|
|
public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
|
2008-01-26 09:36:00 +08:00
|
|
|
CodeGenModule &CGM;
|
2008-02-27 05:41:45 +08:00
|
|
|
CodeGenFunction *CGF;
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::LLVMContext &VMContext;
|
2008-01-26 09:36:00 +08:00
|
|
|
public:
|
2008-02-27 05:41:45 +08:00
|
|
|
ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
|
2009-07-15 07:10:40 +08:00
|
|
|
: CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
llvm::Constant *VisitStmt(Stmt *S) {
|
2009-03-04 00:43:34 +08:00
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *VisitParenExpr(ParenExpr *PE) {
|
|
|
|
return Visit(PE->getSubExpr());
|
|
|
|
}
|
2008-11-22 10:34:39 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
return Visit(E->getInitializer());
|
|
|
|
}
|
|
|
|
|
2008-08-19 07:01:59 +08:00
|
|
|
llvm::Constant *VisitCastExpr(CastExpr* E) {
|
2009-01-17 08:48:48 +08:00
|
|
|
// GCC cast to union extension
|
|
|
|
if (E->getType()->isUnionType()) {
|
|
|
|
const llvm::Type *Ty = ConvertType(E->getType());
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *SubExpr = E->getSubExpr();
|
|
|
|
return EmitUnion(CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF),
|
|
|
|
Ty);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
2009-04-30 15:03:22 +08:00
|
|
|
// Explicit and implicit no-op casts
|
|
|
|
QualType Ty = E->getType(), SubTy = E->getSubExpr()->getType();
|
|
|
|
if (CGM.getContext().hasSameUnqualifiedType(Ty, SubTy)) {
|
2009-02-22 15:29:04 +08:00
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-01-29 09:15:48 +08:00
|
|
|
|
2008-04-08 12:40:51 +08:00
|
|
|
llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
return Visit(DAE->getExpr());
|
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
|
2008-02-22 01:45:41 +08:00
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-05-30 18:24:46 +08:00
|
|
|
const llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(ConvertType(ILE->getType()));
|
2008-02-22 01:45:41 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
|
|
|
// FIXME: Check for wide strings
|
2009-02-25 06:18:39 +08:00
|
|
|
// FIXME: Check for NumInitElements exactly equal to 1??
|
|
|
|
if (NumInitElements > 0 &&
|
|
|
|
(isa<StringLiteral>(ILE->getInit(0)) ||
|
|
|
|
isa<ObjCEncodeExpr>(ILE->getInit(0))) &&
|
2008-08-04 15:31:14 +08:00
|
|
|
ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType())
|
2008-02-22 01:45:41 +08:00
|
|
|
return Visit(ILE->getInit(0));
|
2008-02-05 10:39:50 +08:00
|
|
|
const llvm::Type *ElemTy = AType->getElementType();
|
|
|
|
unsigned NumElements = AType->getNumElements();
|
|
|
|
|
|
|
|
// Initialising an array requires us to automatically
|
|
|
|
// initialise any elements that have not been initialised explicitly
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
|
|
|
|
|
|
|
// Copy initializer elements.
|
|
|
|
unsigned i = 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
bool RewriteType = false;
|
2008-02-05 10:39:50 +08:00
|
|
|
for (; i < NumInitableElts; ++i) {
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *Init = ILE->getInit(i);
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
RewriteType |= (C->getType() != ElemTy);
|
2008-02-05 10:39:50 +08:00
|
|
|
Elts.push_back(C);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-05-31 03:58:50 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
// Initialize remaining array elements.
|
2009-04-14 05:47:26 +08:00
|
|
|
// FIXME: This doesn't handle member pointers correctly!
|
2008-02-05 10:39:50 +08:00
|
|
|
for (; i < NumElements; ++i)
|
2009-07-15 07:10:40 +08:00
|
|
|
Elts.push_back(VMContext.getNullValue(ElemTy));
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-31 03:58:50 +08:00
|
|
|
if (RewriteType) {
|
|
|
|
// FIXME: Try to avoid packing the array
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
for (unsigned i = 0; i < Elts.size(); ++i)
|
|
|
|
Types.push_back(Elts[i]->getType());
|
2009-07-15 07:10:40 +08:00
|
|
|
const llvm::StructType *SType = VMContext.getStructType(Types, true);
|
|
|
|
return VMContext.getConstantStruct(SType, Elts);
|
2008-05-31 03:58:50 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantArray(AType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts,
|
|
|
|
FieldDecl* Field, Expr* E) {
|
|
|
|
// Calculate the value to insert
|
2009-04-08 12:48:15 +08:00
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(E, Field->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return;
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C);
|
|
|
|
if (!CI) {
|
2008-08-16 08:56:44 +08:00
|
|
|
CGM.ErrorUnsupported(E, "bitfield initialization");
|
2008-05-30 18:24:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm::APInt V = CI->getValue();
|
|
|
|
|
|
|
|
// Calculate information about the relevant field
|
|
|
|
const llvm::Type* Ty = CI->getType();
|
2008-08-10 15:00:24 +08:00
|
|
|
const llvm::TargetData &TD = CGM.getTypes().getTargetData();
|
2009-05-09 15:08:47 +08:00
|
|
|
unsigned size = TD.getTypeAllocSizeInBits(Ty);
|
2009-07-24 01:01:21 +08:00
|
|
|
CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
|
|
|
|
unsigned FieldOffset = Info.FieldNo * size;
|
|
|
|
|
|
|
|
FieldOffset += Info.Start;
|
2008-05-30 18:24:46 +08:00
|
|
|
|
|
|
|
// Find where to start the insertion
|
|
|
|
// FIXME: This is O(n^2) in the number of bit-fields!
|
|
|
|
// FIXME: This won't work if the struct isn't completely packed!
|
|
|
|
unsigned offset = 0, i = 0;
|
2009-07-24 01:01:21 +08:00
|
|
|
while (offset < (FieldOffset & -8))
|
2009-05-09 15:08:47 +08:00
|
|
|
offset += TD.getTypeAllocSizeInBits(Elts[i++]->getType());
|
2008-08-10 15:00:24 +08:00
|
|
|
|
|
|
|
// Advance over 0 sized elements (must terminate in bounds since
|
|
|
|
// the bitfield must have a size).
|
2009-05-09 15:08:47 +08:00
|
|
|
while (TD.getTypeAllocSizeInBits(Elts[i]->getType()) == 0)
|
2008-08-10 15:00:24 +08:00
|
|
|
++i;
|
2008-05-30 18:24:46 +08:00
|
|
|
|
2008-07-18 05:07:48 +08:00
|
|
|
// Promote the size of V if necessary
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: This should never occur, but currently it can because initializer
|
|
|
|
// constants are cast to bool, and because clang is not enforcing bitfield
|
|
|
|
// width limits.
|
2009-07-24 01:01:21 +08:00
|
|
|
if (Info.Size > V.getBitWidth())
|
|
|
|
V.zext(Info.Size);
|
2008-07-18 05:07:48 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
// Insert the bits into the struct
|
|
|
|
// FIXME: This algorthm is only correct on X86!
|
|
|
|
// FIXME: THis algorthm assumes bit-fields only have byte-size elements!
|
2009-07-24 01:01:21 +08:00
|
|
|
unsigned bitsToInsert = Info.Size;
|
|
|
|
unsigned curBits = std::min(8 - (FieldOffset & 7), bitsToInsert);
|
|
|
|
unsigned byte = V.getLoBits(curBits).getZExtValue() << (FieldOffset & 7);
|
2008-05-30 18:24:46 +08:00
|
|
|
do {
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::Constant* byteC =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::Int8Ty, byte);
|
2009-07-15 07:10:40 +08:00
|
|
|
Elts[i] = VMContext.getConstantExprOr(Elts[i], byteC);
|
2008-05-30 18:24:46 +08:00
|
|
|
++i;
|
|
|
|
V = V.lshr(curBits);
|
|
|
|
bitsToInsert -= curBits;
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
if (!bitsToInsert)
|
|
|
|
break;
|
|
|
|
|
|
|
|
curBits = bitsToInsert > 8 ? 8 : bitsToInsert;
|
|
|
|
byte = V.getLoBits(curBits).getZExtValue();
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
|
2009-07-27 13:54:15 +08:00
|
|
|
return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
|
|
|
|
|
|
|
|
// FIXME: Remove the old struct builder once we're sure that the new one
|
|
|
|
// works well enough!
|
2008-05-30 18:24:46 +08:00
|
|
|
const llvm::StructType *SType =
|
|
|
|
cast<llvm::StructType>(ConvertType(ILE->getType()));
|
2009-07-18 01:50:17 +08:00
|
|
|
RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
|
2008-02-05 10:39:50 +08:00
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-05-20 08:11:07 +08:00
|
|
|
|
|
|
|
// Initialize the whole structure to zero.
|
2009-04-14 05:47:26 +08:00
|
|
|
// FIXME: This doesn't handle member pointers correctly!
|
2008-05-20 08:11:07 +08:00
|
|
|
for (unsigned i = 0; i < SType->getNumElements(); ++i) {
|
|
|
|
const llvm::Type *FieldTy = SType->getElementType(i);
|
2009-07-15 07:10:40 +08:00
|
|
|
Elts.push_back(VMContext.getNullValue(FieldTy));
|
2008-05-20 08:11:07 +08:00
|
|
|
}
|
2008-02-05 10:39:50 +08:00
|
|
|
|
|
|
|
// Copy initializer elements. Skip padding fields.
|
|
|
|
unsigned EltNo = 0; // Element no in ILE
|
2008-05-31 03:58:50 +08:00
|
|
|
bool RewriteType = false;
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2008-12-12 00:49:14 +08:00
|
|
|
EltNo < ILE->getNumInits() && Field != FieldEnd; ++Field) {
|
|
|
|
if (Field->isBitField()) {
|
2009-06-15 05:41:37 +08:00
|
|
|
if (!Field->getIdentifier())
|
|
|
|
continue;
|
2008-12-12 00:49:14 +08:00
|
|
|
InsertBitfieldIntoStruct(Elts, *Field, ILE->getInit(EltNo));
|
2008-05-20 08:11:07 +08:00
|
|
|
} else {
|
2008-12-12 00:49:14 +08:00
|
|
|
unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(*Field);
|
2009-04-08 12:48:15 +08:00
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(EltNo),
|
|
|
|
Field->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C) return 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
RewriteType |= (C->getType() != Elts[FieldNo]->getType());
|
|
|
|
Elts[FieldNo] = C;
|
2008-05-20 08:11:07 +08:00
|
|
|
}
|
2008-02-05 10:39:50 +08:00
|
|
|
EltNo++;
|
|
|
|
}
|
2008-05-20 08:11:07 +08:00
|
|
|
|
2008-05-31 03:58:50 +08:00
|
|
|
if (RewriteType) {
|
|
|
|
// FIXME: Make this work for non-packed structs
|
|
|
|
assert(SType->isPacked() && "Cannot recreate unpacked structs");
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
for (unsigned i = 0; i < Elts.size(); ++i)
|
|
|
|
Types.push_back(Elts[i]->getType());
|
2009-07-15 07:10:40 +08:00
|
|
|
SType = VMContext.getStructType(Types, true);
|
2008-05-31 03:58:50 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantStruct(SType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2009-01-17 08:48:48 +08:00
|
|
|
llvm::Constant *EmitUnion(llvm::Constant *C, const llvm::Type *Ty) {
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
|
|
|
|
2009-01-17 08:48:48 +08:00
|
|
|
// Build a struct with the union sub-element as the first member,
|
|
|
|
// and padded to the appropriate size
|
|
|
|
std::vector<llvm::Constant*> Elts;
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
Elts.push_back(C);
|
|
|
|
Types.push_back(C->getType());
|
2009-05-09 15:08:47 +08:00
|
|
|
unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType());
|
|
|
|
unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(Ty);
|
2009-07-23 12:50:01 +08:00
|
|
|
|
|
|
|
assert(CurSize <= TotalSize && "Union size mismatch!");
|
|
|
|
if (unsigned NumPadBytes = TotalSize - CurSize) {
|
|
|
|
const llvm::Type *Ty = llvm::Type::Int8Ty;
|
|
|
|
if (NumPadBytes > 1)
|
|
|
|
Ty = VMContext.getArrayType(Ty, NumPadBytes);
|
|
|
|
|
|
|
|
Elts.push_back(VMContext.getNullValue(Ty));
|
|
|
|
Types.push_back(Ty);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
|
|
|
|
2009-07-23 12:50:01 +08:00
|
|
|
llvm::StructType* STy = VMContext.getStructType(Types, false);
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantStruct(STy, Elts);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
|
|
|
|
const llvm::Type *Ty = ConvertType(ILE->getType());
|
|
|
|
|
2009-01-30 00:53:55 +08:00
|
|
|
FieldDecl* curField = ILE->getInitializedFieldInUnion();
|
|
|
|
if (!curField) {
|
|
|
|
// There's no field to initialize, so value-initialize the union.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Make sure that it's really an empty and not a failure of
|
|
|
|
// semantic analysis.
|
2009-07-18 01:50:17 +08:00
|
|
|
RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2009-01-30 00:53:55 +08:00
|
|
|
Field != FieldEnd; ++Field)
|
|
|
|
assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
|
|
|
|
#endif
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getNullValue(Ty);
|
2009-01-30 00:53:55 +08:00
|
|
|
}
|
2008-05-30 18:24:46 +08:00
|
|
|
|
|
|
|
if (curField->isBitField()) {
|
|
|
|
// Create a dummy struct for bit-field insertion
|
2009-05-09 15:08:47 +08:00
|
|
|
unsigned NumElts = CGM.getTargetData().getTypeAllocSize(Ty);
|
2009-07-13 12:10:07 +08:00
|
|
|
llvm::Constant* NV =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getNullValue(llvm::Type::Int8Ty);
|
2008-05-30 18:24:46 +08:00
|
|
|
std::vector<llvm::Constant*> Elts(NumElts, NV);
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0));
|
|
|
|
const llvm::ArrayType *RetTy =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getArrayType(NV->getType(), NumElts);
|
|
|
|
return VMContext.getConstantArray(RetTy, Elts);
|
2008-05-30 18:24:46 +08:00
|
|
|
}
|
|
|
|
|
2009-04-14 05:47:26 +08:00
|
|
|
llvm::Constant *InitElem;
|
|
|
|
if (ILE->getNumInits() > 0) {
|
|
|
|
Expr *Init = ILE->getInit(0);
|
|
|
|
InitElem = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
|
|
|
} else {
|
|
|
|
InitElem = CGM.EmitNullConstant(curField->getType());
|
|
|
|
}
|
|
|
|
return EmitUnion(InitElem, Ty);
|
2008-05-30 18:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) {
|
|
|
|
const llvm::VectorType *VType =
|
|
|
|
cast<llvm::VectorType>(ConvertType(ILE->getType()));
|
2008-05-30 20:02:14 +08:00
|
|
|
const llvm::Type *ElemTy = VType->getElementType();
|
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-02-05 10:39:50 +08:00
|
|
|
unsigned NumElements = VType->getNumElements();
|
2008-05-30 20:02:14 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
|
|
|
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
// Copy initializer elements.
|
|
|
|
unsigned i = 0;
|
2008-05-30 20:02:14 +08:00
|
|
|
for (; i < NumInitableElts; ++i) {
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *Init = ILE->getInit(i);
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
Elts.push_back(C);
|
|
|
|
}
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-30 20:02:14 +08:00
|
|
|
for (; i < NumElements; ++i)
|
2009-07-15 07:10:40 +08:00
|
|
|
Elts.push_back(VMContext.getNullValue(ElemTy));
|
2008-05-30 20:02:14 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantVector(VType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
2009-01-30 14:13:25 +08:00
|
|
|
|
|
|
|
llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
|
2009-04-14 05:47:26 +08:00
|
|
|
return CGM.EmitNullConstant(E->getType());
|
2009-01-30 14:13:25 +08:00
|
|
|
}
|
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isScalarType()) {
|
|
|
|
// We have a scalar in braces. Just use the first element.
|
2009-04-08 12:48:15 +08:00
|
|
|
if (ILE->getNumInits() > 0) {
|
|
|
|
Expr *Init = ILE->getInit(0);
|
|
|
|
return CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
return CGM.EmitNullConstant(ILE->getType());
|
2008-01-30 07:23:18 +08:00
|
|
|
}
|
2008-10-27 07:53:12 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isArrayType())
|
|
|
|
return EmitArrayInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isStructureType())
|
|
|
|
return EmitStructInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isUnionType())
|
|
|
|
return EmitUnionInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isVectorType())
|
|
|
|
return EmitVectorInitialization(ILE);
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
assert(0 && "Unable to handle InitListExpr");
|
2008-03-01 16:45:05 +08:00
|
|
|
// Get rid of control reaches end of void function warning.
|
|
|
|
// Not reached.
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-02-22 01:57:49 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitStringLiteral(StringLiteral *E) {
|
2008-02-11 08:02:17 +08:00
|
|
|
assert(!E->getType()->isPointerType() && "Strings are always arrays");
|
2008-01-26 09:36:00 +08:00
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
// This must be a string initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantArray(CGM.GetStringForStringLiteral(E), false);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
|
|
|
|
// This must be an @encode initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
|
|
|
std::string Str;
|
|
|
|
CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
|
|
|
|
const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
|
|
|
|
|
|
|
|
// Resize the string to the right size, adding zeros at the end, or
|
|
|
|
// truncating as needed.
|
|
|
|
Str.resize(CAT->getSize().getZExtValue(), '\0');
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantArray(Str, false);
|
2009-02-25 06:18:39 +08:00
|
|
|
}
|
|
|
|
|
2008-05-29 19:22:45 +08:00
|
|
|
llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
2009-02-20 06:01:56 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
// Utility methods
|
|
|
|
const llvm::Type *ConvertType(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertType(T);
|
|
|
|
}
|
2008-01-26 12:30:23 +08:00
|
|
|
|
2008-11-16 14:23:45 +08:00
|
|
|
public:
|
2008-01-26 12:30:23 +08:00
|
|
|
llvm::Constant *EmitLValue(Expr *E) {
|
2008-01-26 10:08:50 +08:00
|
|
|
switch (E->getStmtClass()) {
|
2008-02-11 08:23:10 +08:00
|
|
|
default: break;
|
2008-01-26 10:08:50 +08:00
|
|
|
case Expr::CompoundLiteralExprClass: {
|
|
|
|
// Note that due to the nature of compound literals, this is guaranteed
|
|
|
|
// to be the only use of the variable, so we just generate it here.
|
2008-01-26 12:30:23 +08:00
|
|
|
CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
|
|
|
|
llvm::Constant* C = Visit(CLE->getInitializer());
|
2009-02-18 02:43:32 +08:00
|
|
|
// FIXME: "Leaked" on failure.
|
|
|
|
if (C)
|
2009-07-09 03:05:04 +08:00
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
|
2009-02-18 02:43:32 +08:00
|
|
|
E->getType().isConstQualified(),
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2009-07-09 03:05:04 +08:00
|
|
|
C, ".compoundliteral");
|
2008-01-26 10:08:50 +08:00
|
|
|
return C;
|
2008-01-26 12:30:23 +08:00
|
|
|
}
|
2009-01-06 13:10:23 +08:00
|
|
|
case Expr::DeclRefExprClass:
|
|
|
|
case Expr::QualifiedDeclRefExprClass: {
|
2008-10-22 00:13:35 +08:00
|
|
|
NamedDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
|
2008-01-26 10:08:50 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
|
2009-05-13 05:21:08 +08:00
|
|
|
return CGM.GetAddrOfFunction(GlobalDecl(FD));
|
2008-04-16 06:42:06 +08:00
|
|
|
if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
|
2009-02-25 02:41:57 +08:00
|
|
|
// We can never refer to a variable with local storage.
|
|
|
|
if (!VD->hasLocalStorage()) {
|
|
|
|
if (VD->isFileVarDecl() || VD->hasExternalStorage())
|
|
|
|
return CGM.GetAddrOfGlobalVar(VD);
|
|
|
|
else if (VD->isBlockVarDecl()) {
|
|
|
|
assert(CGF && "Can't access static local vars without CGF");
|
|
|
|
return CGF->GetAddrOfStaticLocalVar(VD);
|
|
|
|
}
|
2008-04-16 06:42:06 +08:00
|
|
|
}
|
2008-02-27 05:41:45 +08:00
|
|
|
}
|
2008-02-11 08:23:10 +08:00
|
|
|
break;
|
2008-01-26 10:08:50 +08:00
|
|
|
}
|
2008-08-14 07:20:05 +08:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
|
2009-02-25 06:18:39 +08:00
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
|
2008-11-16 14:23:45 +08:00
|
|
|
case Expr::ObjCStringLiteralClass: {
|
|
|
|
ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
|
2009-04-01 00:53:37 +08:00
|
|
|
llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(SL);
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprBitCast(C, ConvertType(E->getType()));
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2008-12-12 13:18:02 +08:00
|
|
|
case Expr::PredefinedExprClass: {
|
|
|
|
// __func__/__FUNCTION__ -> "". __PRETTY_FUNCTION__ -> "top level".
|
|
|
|
std::string Str;
|
|
|
|
if (cast<PredefinedExpr>(E)->getIdentType() ==
|
|
|
|
PredefinedExpr::PrettyFunction)
|
|
|
|
Str = "top level";
|
|
|
|
|
|
|
|
return CGM.GetAddrOfConstantCString(Str, ".tmp");
|
|
|
|
}
|
2009-01-25 09:21:06 +08:00
|
|
|
case Expr::AddrLabelExprClass: {
|
|
|
|
assert(CGF && "Invalid address of label expression outside function.");
|
|
|
|
unsigned id = CGF->GetIDForAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Constant *C = llvm::ConstantInt::get(llvm::Type::Int32Ty, id);
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprIntToPtr(C, ConvertType(E->getType()));
|
2009-01-25 09:21:06 +08:00
|
|
|
}
|
2009-01-25 09:54:01 +08:00
|
|
|
case Expr::CallExprClass: {
|
|
|
|
CallExpr* CE = cast<CallExpr>(E);
|
2009-02-15 02:57:46 +08:00
|
|
|
if (CE->isBuiltinCall(CGM.getContext()) !=
|
|
|
|
Builtin::BI__builtin___CFStringMakeConstantString)
|
2009-01-25 09:54:01 +08:00
|
|
|
break;
|
|
|
|
const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
|
|
|
|
const StringLiteral *Literal = cast<StringLiteral>(Arg);
|
2009-04-01 00:53:37 +08:00
|
|
|
// FIXME: need to deal with UCN conversion issues.
|
2009-04-01 21:55:36 +08:00
|
|
|
return CGM.GetAddrOfConstantCFString(Literal);
|
2009-01-25 09:54:01 +08:00
|
|
|
}
|
2009-02-19 09:01:04 +08:00
|
|
|
case Expr::BlockExprClass: {
|
2009-03-01 09:09:12 +08:00
|
|
|
std::string FunctionName;
|
|
|
|
if (CGF)
|
|
|
|
FunctionName = CGF->CurFn->getName();
|
|
|
|
else
|
|
|
|
FunctionName = "global";
|
|
|
|
|
|
|
|
return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
|
2009-02-19 09:01:04 +08:00
|
|
|
}
|
2008-02-11 08:23:10 +08:00
|
|
|
}
|
2009-02-18 02:43:32 +08:00
|
|
|
|
|
|
|
return 0;
|
2008-01-26 10:08:50 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2008-02-27 05:41:45 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
|
2009-04-08 12:48:15 +08:00
|
|
|
QualType DestType,
|
2008-07-27 06:37:01 +08:00
|
|
|
CodeGenFunction *CGF) {
|
2008-12-01 10:42:14 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
|
2009-04-11 09:08:03 +08:00
|
|
|
bool Success = false;
|
|
|
|
|
2009-05-27 14:04:58 +08:00
|
|
|
if (DestType->isReferenceType())
|
|
|
|
Success = E->EvaluateAsLValue(Result, Context);
|
|
|
|
else
|
2009-04-11 09:08:03 +08:00
|
|
|
Success = E->Evaluate(Result, Context);
|
|
|
|
|
|
|
|
if (Success) {
|
2008-12-01 10:42:14 +08:00
|
|
|
assert(!Result.HasSideEffects &&
|
|
|
|
"Constant expr should not have any side effects!");
|
|
|
|
switch (Result.Val.getKind()) {
|
2009-01-29 06:24:07 +08:00
|
|
|
case APValue::Uninitialized:
|
2009-02-18 02:43:32 +08:00
|
|
|
assert(0 && "Constant expressions should be initialized.");
|
|
|
|
return 0;
|
2008-11-16 04:45:50 +08:00
|
|
|
case APValue::LValue: {
|
2009-04-11 09:08:03 +08:00
|
|
|
const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
|
2008-12-01 10:42:14 +08:00
|
|
|
llvm::Constant *Offset =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::Int64Ty,
|
2008-12-01 10:42:14 +08:00
|
|
|
Result.Val.getLValueOffset());
|
2008-11-16 04:45:50 +08:00
|
|
|
|
2009-02-20 05:44:24 +08:00
|
|
|
llvm::Constant *C;
|
2008-12-01 10:42:14 +08:00
|
|
|
if (const Expr *LVBase = Result.Val.getLValueBase()) {
|
2009-02-20 05:44:24 +08:00
|
|
|
C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase));
|
|
|
|
|
|
|
|
// Apply offset if necessary.
|
|
|
|
if (!Offset->isNullValue()) {
|
|
|
|
const llvm::Type *Type =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
|
|
|
|
llvm::Constant *Casted = VMContext.getConstantExprBitCast(C, Type);
|
|
|
|
Casted = VMContext.getConstantExprGetElementPtr(Casted, &Offset, 1);
|
|
|
|
C = VMContext.getConstantExprBitCast(Casted, C->getType());
|
2009-02-20 05:44:24 +08:00
|
|
|
}
|
2008-11-16 14:23:45 +08:00
|
|
|
|
2009-02-20 05:44:24 +08:00
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprBitCast(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprPtrToInt(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
} else {
|
|
|
|
C = Offset;
|
|
|
|
|
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprIntToPtr(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
|
|
|
// If the types don't match this should only be a truncate.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (C->getType() != DestTy)
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprTrunc(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
|
|
|
return C;
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
2008-11-17 11:57:28 +08:00
|
|
|
case APValue::Int: {
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Constant *C = llvm::ConstantInt::get(VMContext,
|
|
|
|
Result.Val.getInt());
|
2008-11-16 04:45:50 +08:00
|
|
|
|
|
|
|
if (C->getType() == llvm::Type::Int1Ty) {
|
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-15 07:10:40 +08:00
|
|
|
C = VMContext.getConstantExprZExt(C, BoolTy);
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
|
|
|
return C;
|
2008-11-17 11:57:28 +08:00
|
|
|
}
|
2009-01-29 06:24:07 +08:00
|
|
|
case APValue::ComplexInt: {
|
|
|
|
llvm::Constant *Complex[2];
|
|
|
|
|
2009-07-25 07:12:58 +08:00
|
|
|
Complex[0] = llvm::ConstantInt::get(VMContext,
|
|
|
|
Result.Val.getComplexIntReal());
|
|
|
|
Complex[1] = llvm::ConstantInt::get(VMContext,
|
|
|
|
Result.Val.getComplexIntImag());
|
2009-01-29 06:24:07 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantStruct(Complex, 2);
|
2009-01-29 06:24:07 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
case APValue::Float:
|
2009-07-28 05:00:51 +08:00
|
|
|
return llvm::ConstantFP::get(VMContext, Result.Val.getFloat());
|
2008-11-17 09:58:55 +08:00
|
|
|
case APValue::ComplexFloat: {
|
|
|
|
llvm::Constant *Complex[2];
|
|
|
|
|
2009-07-28 05:00:51 +08:00
|
|
|
Complex[0] = llvm::ConstantFP::get(VMContext,
|
|
|
|
Result.Val.getComplexFloatReal());
|
|
|
|
Complex[1] = llvm::ConstantFP::get(VMContext,
|
|
|
|
Result.Val.getComplexFloatImag());
|
2008-11-17 09:58:55 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantStruct(Complex, 2);
|
2008-11-17 09:58:55 +08:00
|
|
|
}
|
2009-01-18 09:01:34 +08:00
|
|
|
case APValue::Vector: {
|
|
|
|
llvm::SmallVector<llvm::Constant *, 4> Inits;
|
|
|
|
unsigned NumElts = Result.Val.getVectorLength();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
APValue &Elt = Result.Val.getVectorElt(i);
|
|
|
|
if (Elt.isInt())
|
2009-07-25 07:12:58 +08:00
|
|
|
Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
|
2009-01-18 09:01:34 +08:00
|
|
|
else
|
2009-07-28 05:00:51 +08:00
|
|
|
Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantVector(&Inits[0], Inits.size());
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
|
|
|
}
|
2008-06-01 23:31:44 +08:00
|
|
|
|
|
|
|
llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
|
2009-02-18 02:43:32 +08:00
|
|
|
if (C && C->getType() == llvm::Type::Int1Ty) {
|
2008-06-01 23:31:44 +08:00
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-15 07:10:40 +08:00
|
|
|
C = VMContext.getConstantExprZExt(C, BoolTy);
|
2008-06-01 23:31:44 +08:00
|
|
|
}
|
|
|
|
return C;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
|
|
|
|
// Always return an LLVM null constant for now; this will change when we
|
|
|
|
// get support for IRGen of member pointers.
|
2009-07-13 12:10:07 +08:00
|
|
|
return getLLVMContext().getNullValue(getTypes().ConvertType(T));
|
2009-04-14 05:47:26 +08:00
|
|
|
}
|