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"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2010-03-31 06:26:10 +08:00
|
|
|
#include "CGRecordLayout.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"
|
2012-10-09 00:25:52 +08:00
|
|
|
#include "llvm/DataLayout.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConstStructBuilder
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:45:26 +08:00
|
|
|
class ConstStructBuilder {
|
2009-07-24 23:20:52 +08:00
|
|
|
CodeGenModule &CGM;
|
|
|
|
CodeGenFunction *CGF;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Packed;
|
2011-03-17 09:33:18 +08:00
|
|
|
CharUnits NextFieldOffsetInChars;
|
2011-03-18 09:26:17 +08:00
|
|
|
CharUnits LLVMStructAlignment;
|
2012-02-07 08:54:58 +08:00
|
|
|
SmallVector<llvm::Constant *, 32> Elements;
|
2010-04-14 01:45:57 +08:00
|
|
|
public:
|
|
|
|
static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
|
|
|
|
InitListExpr *ILE);
|
2012-01-14 12:30:29 +08:00
|
|
|
static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
|
|
|
|
const APValue &Value, QualType ValTy);
|
|
|
|
|
|
|
|
private:
|
2009-07-24 23:20:52 +08:00
|
|
|
ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
|
2011-03-17 09:33:18 +08:00
|
|
|
: CGM(CGM), CGF(CGF), Packed(false),
|
|
|
|
NextFieldOffsetInChars(CharUnits::Zero()),
|
2011-03-18 09:26:17 +08:00
|
|
|
LLVMStructAlignment(CharUnits::One()) { }
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
void AppendVTablePointer(BaseSubobject Base, llvm::Constant *VTable,
|
|
|
|
const CXXRecordDecl *VTableClass);
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
void AppendField(const FieldDecl *Field, uint64_t FieldOffset,
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
llvm::Constant *InitExpr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
void AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst);
|
|
|
|
|
2010-07-06 01:04:23 +08:00
|
|
|
void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
|
|
|
|
llvm::ConstantInt *InitExpr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-12 07:42:54 +08:00
|
|
|
void AppendPadding(CharUnits PadSize);
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2011-03-11 10:17:05 +08:00
|
|
|
void AppendTailPadding(CharUnits RecordSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
void ConvertStructToPacked();
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
bool Build(InitListExpr *ILE);
|
2012-02-23 16:33:23 +08:00
|
|
|
void Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
|
|
|
|
llvm::Constant *VTable, const CXXRecordDecl *VTableClass,
|
|
|
|
CharUnits BaseOffset);
|
2012-01-14 12:30:29 +08:00
|
|
|
llvm::Constant *Finalize(QualType Ty);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-18 09:26:17 +08:00
|
|
|
CharUnits getAlignment(const llvm::Constant *C) const {
|
|
|
|
if (Packed) return CharUnits::One();
|
|
|
|
return CharUnits::fromQuantity(
|
2012-10-09 00:25:52 +08:00
|
|
|
CGM.getDataLayout().getABITypeAlignment(C->getType()));
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-18 09:12:13 +08:00
|
|
|
CharUnits getSizeInChars(const llvm::Constant *C) const {
|
|
|
|
return CharUnits::fromQuantity(
|
2012-10-09 00:25:52 +08:00
|
|
|
CGM.getDataLayout().getTypeAllocSize(C->getType()));
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
|
|
|
};
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
void ConstStructBuilder::AppendVTablePointer(BaseSubobject Base,
|
|
|
|
llvm::Constant *VTable,
|
|
|
|
const CXXRecordDecl *VTableClass) {
|
|
|
|
// Find the appropriate vtable within the vtable group.
|
|
|
|
uint64_t AddressPoint =
|
|
|
|
CGM.getVTableContext().getVTableLayout(VTableClass).getAddressPoint(Base);
|
|
|
|
llvm::Value *Indices[] = {
|
|
|
|
llvm::ConstantInt::get(CGM.Int64Ty, 0),
|
|
|
|
llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint)
|
|
|
|
};
|
|
|
|
llvm::Constant *VTableAddressPoint =
|
|
|
|
llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Indices);
|
|
|
|
|
|
|
|
// Add the vtable at the start of the object.
|
2012-03-30 12:16:06 +08:00
|
|
|
AppendBytes(Base.getBaseOffset(), VTableAddressPoint);
|
2012-02-23 16:33:23 +08:00
|
|
|
}
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
void ConstStructBuilder::
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
AppendField(const FieldDecl *Field, uint64_t FieldOffset,
|
|
|
|
llvm::Constant *InitCst) {
|
2011-03-15 09:09:02 +08:00
|
|
|
const ASTContext &Context = CGM.getContext();
|
|
|
|
|
|
|
|
CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
AppendBytes(FieldOffsetInChars, InitCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstStructBuilder::
|
|
|
|
AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst) {
|
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
assert(NextFieldOffsetInChars <= FieldOffsetInChars
|
2010-04-14 01:45:57 +08:00
|
|
|
&& "Field offset mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-18 09:26:17 +08:00
|
|
|
CharUnits FieldAlignment = getAlignment(InitCst);
|
2009-10-02 12:52:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// Round up the field offset to the alignment of the field type.
|
2011-03-17 09:33:18 +08:00
|
|
|
CharUnits AlignedNextFieldOffsetInChars =
|
2011-03-18 09:26:17 +08:00
|
|
|
NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) {
|
2010-04-14 01:45:57 +08:00
|
|
|
assert(!Packed && "Alignment is wrong even with a packed struct!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// Convert the struct to a packed struct.
|
|
|
|
ConvertStructToPacked();
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) {
|
2010-04-14 01:45:57 +08:00
|
|
|
// We need to append padding.
|
2012-01-14 12:30:29 +08:00
|
|
|
AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
assert(NextFieldOffsetInChars == FieldOffsetInChars &&
|
2010-04-14 01:45:57 +08:00
|
|
|
"Did not add enough padding!");
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// Add the field.
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
Elements.push_back(InitCst);
|
2011-03-17 09:33:18 +08:00
|
|
|
NextFieldOffsetInChars = AlignedNextFieldOffsetInChars +
|
2011-03-18 09:12:13 +08:00
|
|
|
getSizeInChars(InitCst);
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
if (Packed)
|
2012-01-14 12:30:29 +08:00
|
|
|
assert(LLVMStructAlignment == CharUnits::One() &&
|
2011-03-18 09:26:17 +08:00
|
|
|
"Packed struct not byte-aligned!");
|
2010-04-14 01:45:57 +08:00
|
|
|
else
|
|
|
|
LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
|
|
|
|
}
|
|
|
|
|
2010-07-06 01:04:23 +08:00
|
|
|
void ConstStructBuilder::AppendBitField(const FieldDecl *Field,
|
|
|
|
uint64_t FieldOffset,
|
|
|
|
llvm::ConstantInt *CI) {
|
2011-03-12 20:03:11 +08:00
|
|
|
const ASTContext &Context = CGM.getContext();
|
2011-03-19 08:57:28 +08:00
|
|
|
const uint64_t CharWidth = Context.getCharWidth();
|
2011-03-17 09:33:18 +08:00
|
|
|
uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
|
|
|
|
if (FieldOffset > NextFieldOffsetInBits) {
|
2010-04-14 01:45:57 +08:00
|
|
|
// We need to add padding.
|
2011-03-12 20:03:11 +08:00
|
|
|
CharUnits PadSize = Context.toCharUnitsFromBits(
|
2011-03-17 09:33:18 +08:00
|
|
|
llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits,
|
2011-09-02 08:18:52 +08:00
|
|
|
Context.getTargetInfo().getCharAlign()));
|
2010-04-14 01:45:57 +08:00
|
|
|
|
2011-03-12 20:03:11 +08:00
|
|
|
AppendPadding(PadSize);
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-11 02:28:20 +08:00
|
|
|
uint64_t FieldSize = Field->getBitWidthValue(Context);
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
llvm::APInt FieldValue = CI->getValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// 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())
|
2010-12-07 16:25:34 +08:00
|
|
|
FieldValue = FieldValue.zext(FieldSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// Truncate the size of FieldValue to the bit field size.
|
|
|
|
if (FieldSize < FieldValue.getBitWidth())
|
2010-12-07 16:25:34 +08:00
|
|
|
FieldValue = FieldValue.trunc(FieldSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
|
|
|
|
if (FieldOffset < NextFieldOffsetInBits) {
|
2010-04-14 01:45:57 +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!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
bool FitsCompletelyInPreviousByte =
|
|
|
|
BitsInPreviousByte >= FieldValue.getBitWidth();
|
2009-07-27 10:56:37 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
llvm::APInt Tmp = FieldValue;
|
2009-07-27 10:56:37 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
if (!FitsCompletelyInPreviousByte) {
|
|
|
|
unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-10-09 00:25:52 +08:00
|
|
|
if (CGM.getDataLayout().isBigEndian()) {
|
2010-04-14 01:45:57 +08:00
|
|
|
Tmp = Tmp.lshr(NewFieldWidth);
|
2010-12-07 16:25:34 +08:00
|
|
|
Tmp = Tmp.trunc(BitsInPreviousByte);
|
2009-07-27 10:56:37 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// We want the remaining high bits.
|
2010-12-07 16:25:34 +08:00
|
|
|
FieldValue = FieldValue.trunc(NewFieldWidth);
|
2010-04-14 01:45:57 +08:00
|
|
|
} else {
|
2010-12-07 16:25:34 +08:00
|
|
|
Tmp = Tmp.trunc(BitsInPreviousByte);
|
2009-12-09 05:12:32 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// We want the remaining low bits.
|
|
|
|
FieldValue = FieldValue.lshr(BitsInPreviousByte);
|
2010-12-07 16:25:34 +08:00
|
|
|
FieldValue = FieldValue.trunc(NewFieldWidth);
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-19 08:57:28 +08:00
|
|
|
Tmp = Tmp.zext(CharWidth);
|
2012-10-09 00:25:52 +08:00
|
|
|
if (CGM.getDataLayout().isBigEndian()) {
|
2009-07-27 10:56:37 +08:00
|
|
|
if (FitsCompletelyInPreviousByte)
|
2010-04-14 01:45:57 +08:00
|
|
|
Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
|
|
|
|
} else {
|
2011-03-19 08:57:28 +08:00
|
|
|
Tmp = Tmp.shl(CharWidth - BitsInPreviousByte);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-06 02:03:30 +08:00
|
|
|
// 'or' in the bits that go into the previous byte.
|
|
|
|
llvm::Value *LastElt = Elements.back();
|
|
|
|
if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt))
|
2010-04-14 01:45:57 +08:00
|
|
|
Tmp |= Val->getValue();
|
2010-07-06 02:03:30 +08:00
|
|
|
else {
|
|
|
|
assert(isa<llvm::UndefValue>(LastElt));
|
|
|
|
// If there is an undef field that we're adding to, it can either be a
|
|
|
|
// scalar undef (in which case, we just replace it with our field) or it
|
|
|
|
// is an array. If it is an array, we have to pull one byte off the
|
|
|
|
// array so that the other undef bytes stay around.
|
|
|
|
if (!isa<llvm::IntegerType>(LastElt->getType())) {
|
|
|
|
// The undef padding will be a multibyte array, create a new smaller
|
|
|
|
// padding and then an hole for our i8 to get plopped into.
|
|
|
|
assert(isa<llvm::ArrayType>(LastElt->getType()) &&
|
|
|
|
"Expected array padding of undefs");
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType());
|
2011-03-19 08:57:28 +08:00
|
|
|
assert(AT->getElementType()->isIntegerTy(CharWidth) &&
|
2010-07-06 02:03:30 +08:00
|
|
|
AT->getNumElements() != 0 &&
|
|
|
|
"Expected non-empty array padding of undefs");
|
|
|
|
|
|
|
|
// Remove the padding array.
|
2011-03-17 09:33:18 +08:00
|
|
|
NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements());
|
2010-07-06 02:03:30 +08:00
|
|
|
Elements.pop_back();
|
|
|
|
|
|
|
|
// Add the padding back in two chunks.
|
2011-03-12 07:42:54 +08:00
|
|
|
AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1));
|
|
|
|
AppendPadding(CharUnits::One());
|
2010-07-06 02:03:30 +08:00
|
|
|
assert(isa<llvm::UndefValue>(Elements.back()) &&
|
2011-03-19 08:57:28 +08:00
|
|
|
Elements.back()->getType()->isIntegerTy(CharWidth) &&
|
2010-07-06 02:03:30 +08:00
|
|
|
"Padding addition didn't work right");
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
if (FitsCompletelyInPreviousByte)
|
2010-07-06 01:04:23 +08:00
|
|
|
return;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2011-03-19 08:57:28 +08:00
|
|
|
while (FieldValue.getBitWidth() > CharWidth) {
|
2010-04-14 01:45:57 +08:00
|
|
|
llvm::APInt Tmp;
|
2009-07-27 09:23:51 +08:00
|
|
|
|
2012-10-09 00:25:52 +08:00
|
|
|
if (CGM.getDataLayout().isBigEndian()) {
|
2010-04-14 01:45:57 +08:00
|
|
|
// We want the high bits.
|
2011-03-19 08:57:28 +08:00
|
|
|
Tmp =
|
|
|
|
FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth);
|
2010-04-14 01:45:57 +08:00
|
|
|
} else {
|
|
|
|
// We want the low bits.
|
2011-03-19 08:57:28 +08:00
|
|
|
Tmp = FieldValue.trunc(CharWidth);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-19 08:57:28 +08:00
|
|
|
FieldValue = FieldValue.lshr(CharWidth);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
|
2011-03-19 09:28:06 +08:00
|
|
|
++NextFieldOffsetInChars;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-19 08:57:28 +08:00
|
|
|
FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth);
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
assert(FieldValue.getBitWidth() > 0 &&
|
|
|
|
"Should have at least one bit left!");
|
2011-03-19 08:57:28 +08:00
|
|
|
assert(FieldValue.getBitWidth() <= CharWidth &&
|
2010-04-14 01:45:57 +08:00
|
|
|
"Should not have more than a byte left!");
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2011-03-19 08:57:28 +08:00
|
|
|
if (FieldValue.getBitWidth() < CharWidth) {
|
2012-10-09 00:25:52 +08:00
|
|
|
if (CGM.getDataLayout().isBigEndian()) {
|
2010-04-14 01:45:57 +08:00
|
|
|
unsigned BitWidth = FieldValue.getBitWidth();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-19 08:57:28 +08:00
|
|
|
FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth);
|
2010-04-14 01:45:57 +08:00
|
|
|
} else
|
2011-03-19 08:57:28 +08:00
|
|
|
FieldValue = FieldValue.zext(CharWidth);
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// Append the last element.
|
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
|
|
|
|
FieldValue));
|
2011-03-19 09:28:06 +08:00
|
|
|
++NextFieldOffsetInChars;
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2011-03-12 07:42:54 +08:00
|
|
|
void ConstStructBuilder::AppendPadding(CharUnits PadSize) {
|
|
|
|
if (PadSize.isZero())
|
2010-04-14 01:45:57 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Type *Ty = CGM.Int8Ty;
|
2011-03-12 07:42:54 +08:00
|
|
|
if (PadSize > CharUnits::One())
|
|
|
|
Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-17 04:56:35 +08:00
|
|
|
llvm::Constant *C = llvm::UndefValue::get(Ty);
|
2010-04-14 01:45:57 +08:00
|
|
|
Elements.push_back(C);
|
2011-03-18 09:26:17 +08:00
|
|
|
assert(getAlignment(C) == CharUnits::One() &&
|
|
|
|
"Padding must have 1 byte alignment!");
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2011-03-18 09:12:13 +08:00
|
|
|
NextFieldOffsetInChars += getSizeInChars(C);
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2011-03-11 10:17:05 +08:00
|
|
|
void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) {
|
2011-03-17 09:33:18 +08:00
|
|
|
assert(NextFieldOffsetInChars <= RecordSize &&
|
2011-03-11 10:17:05 +08:00
|
|
|
"Size mismatch!");
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
AppendPadding(RecordSize - NextFieldOffsetInChars);
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
void ConstStructBuilder::ConvertStructToPacked() {
|
2012-02-07 08:04:27 +08:00
|
|
|
SmallVector<llvm::Constant *, 16> PackedElements;
|
2011-03-18 08:55:06 +08:00
|
|
|
CharUnits ElementOffsetInChars = CharUnits::Zero();
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
|
|
|
|
llvm::Constant *C = Elements[i];
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2011-03-18 09:26:17 +08:00
|
|
|
CharUnits ElementAlign = CharUnits::fromQuantity(
|
2012-10-09 00:25:52 +08:00
|
|
|
CGM.getDataLayout().getABITypeAlignment(C->getType()));
|
2011-03-18 08:55:06 +08:00
|
|
|
CharUnits AlignedElementOffsetInChars =
|
2011-03-18 09:26:17 +08:00
|
|
|
ElementOffsetInChars.RoundUpToAlignment(ElementAlign);
|
2009-10-02 10:15:20 +08:00
|
|
|
|
2011-03-18 08:55:06 +08:00
|
|
|
if (AlignedElementOffsetInChars > ElementOffsetInChars) {
|
2010-04-14 01:45:57 +08:00
|
|
|
// We need some padding.
|
2011-03-18 08:55:06 +08:00
|
|
|
CharUnits NumChars =
|
|
|
|
AlignedElementOffsetInChars - ElementOffsetInChars;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Type *Ty = CGM.Int8Ty;
|
2011-03-18 08:55:06 +08:00
|
|
|
if (NumChars > CharUnits::One())
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-17 04:56:35 +08:00
|
|
|
llvm::Constant *Padding = llvm::UndefValue::get(Ty);
|
2010-04-14 01:45:57 +08:00
|
|
|
PackedElements.push_back(Padding);
|
2011-03-18 09:12:13 +08:00
|
|
|
ElementOffsetInChars += getSizeInChars(Padding);
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
PackedElements.push_back(C);
|
2011-03-18 09:12:13 +08:00
|
|
|
ElementOffsetInChars += getSizeInChars(C);
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-18 08:55:06 +08:00
|
|
|
assert(ElementOffsetInChars == NextFieldOffsetInChars &&
|
2010-04-14 01:45:57 +08:00
|
|
|
"Packing the struct changed its size!");
|
|
|
|
|
2012-02-07 08:04:27 +08:00
|
|
|
Elements.swap(PackedElements);
|
2011-03-18 09:26:17 +08:00
|
|
|
LLVMStructAlignment = CharUnits::One();
|
2010-04-14 01:45:57 +08:00
|
|
|
Packed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstStructBuilder::Build(InitListExpr *ILE) {
|
2012-02-19 22:53:49 +08:00
|
|
|
if (ILE->initializesStdInitializerList()) {
|
2012-02-26 04:51:20 +08:00
|
|
|
//CGM.ErrorUnsupported(ILE, "global std::initializer_list");
|
2012-02-19 22:53:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
unsigned ElementNo = 0;
|
2011-04-29 06:49:46 +08:00
|
|
|
const FieldDecl *LastFD = 0;
|
2012-10-13 07:29:20 +08:00
|
|
|
bool IsMsStruct = RD->isMsStruct(CGM.getContext());
|
2011-04-29 06:49:46 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
|
2011-04-29 06:49:46 +08:00
|
|
|
if (IsMsStruct) {
|
|
|
|
// Zero-length bitfields following non-bitfield members are
|
|
|
|
// ignored:
|
2012-06-07 04:45:41 +08:00
|
|
|
if (CGM.getContext().ZeroBitfieldFollowsNonBitfield(*Field, LastFD)) {
|
2011-04-29 06:49:46 +08:00
|
|
|
--FieldNo;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-07 04:45:41 +08:00
|
|
|
LastFD = *Field;
|
2011-04-29 06:49:46 +08:00
|
|
|
}
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
|
|
|
|
// If this is a union, skip all the fields that aren't being initialized.
|
2012-06-07 04:45:41 +08:00
|
|
|
if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
|
2010-04-14 01:45:57 +08:00
|
|
|
continue;
|
|
|
|
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
// Don't emit anonymous bitfields, they just affect layout.
|
2011-08-24 04:22:34 +08:00
|
|
|
if (Field->isUnnamedBitfield()) {
|
2012-06-07 04:45:41 +08:00
|
|
|
LastFD = *Field;
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
continue;
|
2011-04-29 06:49:46 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
// Get the initializer. A struct can include fields without initializers,
|
|
|
|
// we just use explicit null values for them.
|
|
|
|
llvm::Constant *EltInit;
|
|
|
|
if (ElementNo < ILE->getNumInits())
|
|
|
|
EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++),
|
|
|
|
Field->getType(), CGF);
|
|
|
|
else
|
|
|
|
EltInit = CGM.EmitNullConstant(Field->getType());
|
2010-07-18 07:55:01 +08:00
|
|
|
|
|
|
|
if (!EltInit)
|
|
|
|
return false;
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
|
|
|
|
if (!Field->isBitField()) {
|
|
|
|
// Handle non-bitfield members.
|
2012-06-07 04:45:41 +08:00
|
|
|
AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit);
|
2012-01-14 12:30:29 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise we have a bitfield.
|
2012-06-07 04:45:41 +08:00
|
|
|
AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
|
2012-01-14 12:30:29 +08:00
|
|
|
cast<llvm::ConstantInt>(EltInit));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
namespace {
|
|
|
|
struct BaseInfo {
|
|
|
|
BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
|
|
|
|
: Decl(Decl), Offset(Offset), Index(Index) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *Decl;
|
|
|
|
CharUnits Offset;
|
|
|
|
unsigned Index;
|
|
|
|
|
|
|
|
bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
|
|
|
|
bool IsPrimaryBase, llvm::Constant *VTable,
|
|
|
|
const CXXRecordDecl *VTableClass,
|
|
|
|
CharUnits Offset) {
|
2012-01-14 12:30:29 +08:00
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
|
|
|
|
// Add a vtable pointer, if we need one and it hasn't already been added.
|
|
|
|
if (CD->isDynamicClass() && !IsPrimaryBase)
|
|
|
|
AppendVTablePointer(BaseSubobject(CD, Offset), VTable, VTableClass);
|
|
|
|
|
|
|
|
// Accumulate and sort bases, in order to visit them in address order, which
|
|
|
|
// may not be the same as declaration order.
|
|
|
|
llvm::SmallVector<BaseInfo, 8> Bases;
|
|
|
|
Bases.reserve(CD->getNumBases());
|
2012-01-14 12:30:29 +08:00
|
|
|
unsigned BaseNo = 0;
|
2012-02-23 16:33:23 +08:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
|
2012-01-14 12:30:29 +08:00
|
|
|
BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
|
2012-02-23 16:33:23 +08:00
|
|
|
assert(!Base->isVirtual() && "should not have virtual bases here");
|
2012-01-14 12:30:29 +08:00
|
|
|
const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
|
|
|
|
CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
|
2012-02-23 16:33:23 +08:00
|
|
|
Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
|
|
|
|
}
|
|
|
|
std::stable_sort(Bases.begin(), Bases.end());
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2012-02-23 16:33:23 +08:00
|
|
|
for (unsigned I = 0, N = Bases.size(); I != N; ++I) {
|
|
|
|
BaseInfo &Base = Bases[I];
|
|
|
|
|
|
|
|
bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
|
|
|
|
Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
|
|
|
|
VTable, VTableClass, Offset + Base.Offset);
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
const FieldDecl *LastFD = 0;
|
2012-10-13 07:29:20 +08:00
|
|
|
bool IsMsStruct = RD->isMsStruct(CGM.getContext());
|
2012-03-30 11:55:31 +08:00
|
|
|
uint64_t OffsetBits = CGM.getContext().toBits(Offset);
|
2012-01-14 12:30:29 +08:00
|
|
|
|
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
|
|
|
|
if (IsMsStruct) {
|
|
|
|
// Zero-length bitfields following non-bitfield members are
|
|
|
|
// ignored:
|
2012-06-07 04:45:41 +08:00
|
|
|
if (CGM.getContext().ZeroBitfieldFollowsNonBitfield(*Field, LastFD)) {
|
2012-01-14 12:30:29 +08:00
|
|
|
--FieldNo;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-07 04:45:41 +08:00
|
|
|
LastFD = *Field;
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a union, skip all the fields that aren't being initialized.
|
2012-06-07 04:45:41 +08:00
|
|
|
if (RD->isUnion() && Val.getUnionField() != *Field)
|
2012-01-14 12:30:29 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Don't emit anonymous bitfields, they just affect layout.
|
|
|
|
if (Field->isUnnamedBitfield()) {
|
2012-06-07 04:45:41 +08:00
|
|
|
LastFD = *Field;
|
2012-01-14 12:30:29 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the value of the initializer.
|
|
|
|
const APValue &FieldValue =
|
|
|
|
RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
|
|
|
|
llvm::Constant *EltInit =
|
2012-03-03 07:27:11 +08:00
|
|
|
CGM.EmitConstantValueForMemory(FieldValue, Field->getType(), CGF);
|
2012-01-14 12:30:29 +08:00
|
|
|
assert(EltInit && "EmitConstantValue can't fail");
|
|
|
|
|
|
|
|
if (!Field->isBitField()) {
|
|
|
|
// Handle non-bitfield members.
|
2012-06-07 04:45:41 +08:00
|
|
|
AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, EltInit);
|
2010-04-14 01:45:57 +08:00
|
|
|
} else {
|
Rework the ConstStructBuilder code to emit missing initializer
elements with explicit zero values instead of with tail padding.
On an example like this:
struct foo { int a; int b; };
struct foo fooarray[] = {
{1, 2},
{4},
};
We now lay this out as:
@fooarray = global [2 x %struct.foo] [%struct.foo { i32 1, i32 2 }, %struct.foo { i32 4, i32 0 }]
instead of as:
@fooarray = global %0 <{ %struct.foo { i32 1, i32 2 }, %1 { i32 4, [4 x i8] zeroinitializer } }>
Preserving both the struct type of the second element, but also the array type of the entire thing.
llvm-svn: 101155
2010-04-14 02:16:19 +08:00
|
|
|
// Otherwise we have a bitfield.
|
2012-06-07 04:45:41 +08:00
|
|
|
AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
|
2010-07-06 01:04:23 +08:00
|
|
|
cast<llvm::ConstantInt>(EltInit));
|
2009-10-02 10:15:20 +08:00
|
|
|
}
|
2010-04-14 01:45:57 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) {
|
|
|
|
RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
CharUnits LayoutSizeInChars = Layout.getSize();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-17 09:33:18 +08:00
|
|
|
if (NextFieldOffsetInChars > LayoutSizeInChars) {
|
2010-04-14 01:45:57 +08:00
|
|
|
// 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!");
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
// No tail padding is necessary.
|
2012-01-14 12:30:29 +08:00
|
|
|
} else {
|
|
|
|
// Append tail padding if necessary.
|
|
|
|
AppendTailPadding(LayoutSizeInChars);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
CharUnits LLVMSizeInChars =
|
|
|
|
NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
// Check if we need to convert the struct to a packed struct.
|
|
|
|
if (NextFieldOffsetInChars <= LayoutSizeInChars &&
|
|
|
|
LLVMSizeInChars > LayoutSizeInChars) {
|
|
|
|
assert(!Packed && "Size mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
ConvertStructToPacked();
|
|
|
|
assert(NextFieldOffsetInChars <= LayoutSizeInChars &&
|
|
|
|
"Converting to packed did not help!");
|
|
|
|
}
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
assert(LayoutSizeInChars == NextFieldOffsetInChars &&
|
|
|
|
"Tail padding mismatch!");
|
|
|
|
}
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2011-06-20 12:01:35 +08:00
|
|
|
// Pick the type to use. If the type is layout identical to the ConvertType
|
|
|
|
// type then use it, otherwise use whatever the builder produced for us.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType *STy =
|
2011-06-20 12:01:35 +08:00
|
|
|
llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(),
|
2012-01-14 12:30:29 +08:00
|
|
|
Elements, Packed);
|
|
|
|
llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty);
|
|
|
|
if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) {
|
|
|
|
if (ValSTy->isLayoutIdentical(STy))
|
|
|
|
STy = ValSTy;
|
2011-06-20 12:01:35 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
|
|
|
|
llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements);
|
|
|
|
|
|
|
|
assert(NextFieldOffsetInChars.RoundUpToAlignment(getAlignment(Result)) ==
|
|
|
|
getSizeInChars(Result) && "Size mismatch!");
|
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
|
|
|
|
CodeGenFunction *CGF,
|
|
|
|
InitListExpr *ILE) {
|
|
|
|
ConstStructBuilder Builder(CGM, CGF);
|
|
|
|
|
|
|
|
if (!Builder.Build(ILE))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Builder.Finalize(ILE->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
|
|
|
|
CodeGenFunction *CGF,
|
|
|
|
const APValue &Val,
|
|
|
|
QualType ValTy) {
|
|
|
|
ConstStructBuilder Builder(CGM, CGF);
|
2012-02-23 16:33:23 +08:00
|
|
|
|
|
|
|
const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl();
|
|
|
|
const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
|
|
|
|
llvm::Constant *VTable = 0;
|
|
|
|
if (CD && CD->isDynamicClass())
|
|
|
|
VTable = CGM.getVTables().GetAddrOfVTable(CD);
|
|
|
|
|
|
|
|
Builder.Build(Val, RD, false, VTable, CD, CharUnits::Zero());
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
return Builder.Finalize(ValTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-14 01:45:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConstExprEmitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-01-18 05:42:19 +08:00
|
|
|
|
|
|
|
/// This class only needs to handle two cases:
|
|
|
|
/// 1) Literals (this is used by APValue emission to emit literals).
|
|
|
|
/// 2) Arrays, structs and unions (outside C++11 mode, we don't currently
|
|
|
|
/// constant fold these types).
|
2009-11-29 03:45:26 +08:00
|
|
|
class ConstExprEmitter :
|
2008-01-26 12:30:23 +08:00
|
|
|
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
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitStmt(Stmt *S) {
|
2009-03-04 00:43:34 +08:00
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
llvm::Constant *VisitParenExpr(ParenExpr *PE) {
|
|
|
|
return Visit(PE->getSubExpr());
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-15 13:09:51 +08:00
|
|
|
llvm::Constant *
|
|
|
|
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
|
|
|
|
return Visit(PE->getReplacement());
|
|
|
|
}
|
|
|
|
|
2011-04-15 08:35:48 +08:00
|
|
|
llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
|
|
|
|
return Visit(GE->getResultExpr());
|
|
|
|
}
|
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
return Visit(E->getInitializer());
|
|
|
|
}
|
2011-02-03 16:15:49 +08:00
|
|
|
|
2008-08-19 07:01:59 +08:00
|
|
|
llvm::Constant *VisitCastExpr(CastExpr* E) {
|
2011-03-16 05:17:48 +08:00
|
|
|
Expr *subExpr = E->getSubExpr();
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF);
|
|
|
|
if (!C) return 0;
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *destType = ConvertType(E->getType());
|
2011-03-16 05:17:48 +08:00
|
|
|
|
2009-08-23 07:54:44 +08:00
|
|
|
switch (E->getCastKind()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ToUnion: {
|
2009-08-23 07:54:44 +08:00
|
|
|
// GCC cast to union extension
|
|
|
|
assert(E->getType()->isUnionType() &&
|
|
|
|
"Destination type is not union type!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 05:38:39 +08:00
|
|
|
// Build a struct with the union sub-element as the first member,
|
|
|
|
// and padded to the appropriate size
|
2012-02-07 08:13:27 +08:00
|
|
|
SmallVector<llvm::Constant*, 2> Elts;
|
|
|
|
SmallVector<llvm::Type*, 2> Types;
|
2009-08-01 05:38:39 +08:00
|
|
|
Elts.push_back(C);
|
|
|
|
Types.push_back(C->getType());
|
2012-10-09 00:25:52 +08:00
|
|
|
unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
|
|
|
|
unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destType);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 05:38:39 +08:00
|
|
|
assert(CurSize <= TotalSize && "Union size mismatch!");
|
|
|
|
if (unsigned NumPadBytes = TotalSize - CurSize) {
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Type *Ty = CGM.Int8Ty;
|
2009-08-01 05:38:39 +08:00
|
|
|
if (NumPadBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumPadBytes);
|
2009-08-23 07:54:44 +08:00
|
|
|
|
2010-04-17 04:56:35 +08:00
|
|
|
Elts.push_back(llvm::UndefValue::get(Ty));
|
2009-08-01 05:38:39 +08:00
|
|
|
Types.push_back(Ty);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-23 07:54:44 +08:00
|
|
|
llvm::StructType* STy =
|
|
|
|
llvm::StructType::get(C->getType()->getContext(), Types, false);
|
2009-08-01 05:38:39 +08:00
|
|
|
return llvm::ConstantStruct::get(STy, Elts);
|
2009-01-17 08:48:48 +08:00
|
|
|
}
|
2009-10-19 04:31:03 +08:00
|
|
|
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_LValueToRValue:
|
2012-01-17 01:27:18 +08:00
|
|
|
case CK_AtomicToNonAtomic:
|
|
|
|
case CK_NonAtomicToAtomic:
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_NoOp:
|
|
|
|
return C;
|
|
|
|
|
|
|
|
case CK_Dependent: llvm_unreachable("saw dependent cast!");
|
|
|
|
|
2012-08-31 08:14:07 +08:00
|
|
|
case CK_BuiltinFnToFnPtr:
|
|
|
|
llvm_unreachable("builtin functions are handled elsewhere");
|
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
case CK_ReinterpretMemberPointer:
|
|
|
|
case CK_DerivedToBaseMemberPointer:
|
|
|
|
case CK_BaseToDerivedMemberPointer:
|
|
|
|
return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
|
|
|
|
|
2011-03-16 05:17:48 +08:00
|
|
|
// These will never be supported.
|
|
|
|
case CK_ObjCObjectLValueCast:
|
2011-09-10 14:18:15 +08:00
|
|
|
case CK_ARCProduceObject:
|
|
|
|
case CK_ARCConsumeObject:
|
|
|
|
case CK_ARCReclaimReturnedObject:
|
|
|
|
case CK_ARCExtendBlockObject:
|
2012-02-22 13:02:47 +08:00
|
|
|
case CK_CopyAndAutoreleaseBlockObject:
|
2011-03-16 05:17:48 +08:00
|
|
|
return 0;
|
|
|
|
|
2012-01-18 05:42:19 +08:00
|
|
|
// These don't need to be handled here because Evaluate knows how to
|
|
|
|
// evaluate them in the cases where they can be folded.
|
2012-02-15 09:22:51 +08:00
|
|
|
case CK_BitCast:
|
2012-01-18 05:42:19 +08:00
|
|
|
case CK_ToVoid:
|
|
|
|
case CK_Dynamic:
|
|
|
|
case CK_LValueBitCast:
|
|
|
|
case CK_NullToMemberPointer:
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_UserDefinedConversion:
|
|
|
|
case CK_ConstructorConversion:
|
2012-01-05 07:13:47 +08:00
|
|
|
case CK_CPointerToObjCPointerCast:
|
|
|
|
case CK_BlockPointerToObjCPointerCast:
|
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
|
|
|
case CK_BaseToDerived:
|
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_UncheckedDerivedToBase:
|
|
|
|
case CK_MemberPointerToBoolean:
|
|
|
|
case CK_VectorSplat:
|
|
|
|
case CK_FloatingRealToComplex:
|
|
|
|
case CK_FloatingComplexToReal:
|
|
|
|
case CK_FloatingComplexToBoolean:
|
|
|
|
case CK_FloatingComplexCast:
|
|
|
|
case CK_FloatingComplexToIntegralComplex:
|
|
|
|
case CK_IntegralRealToComplex:
|
|
|
|
case CK_IntegralComplexToReal:
|
|
|
|
case CK_IntegralComplexToBoolean:
|
|
|
|
case CK_IntegralComplexCast:
|
|
|
|
case CK_IntegralComplexToFloatingComplex:
|
|
|
|
case CK_PointerToIntegral:
|
|
|
|
case CK_PointerToBoolean:
|
|
|
|
case CK_NullToPointer:
|
2012-01-05 07:13:47 +08:00
|
|
|
case CK_IntegralCast:
|
|
|
|
case CK_IntegralToPointer:
|
2011-03-16 05:17:48 +08:00
|
|
|
case CK_IntegralToBoolean:
|
|
|
|
case CK_IntegralToFloating:
|
|
|
|
case CK_FloatingToIntegral:
|
|
|
|
case CK_FloatingToBoolean:
|
|
|
|
case CK_FloatingCast:
|
2012-01-05 07:13:47 +08:00
|
|
|
return 0;
|
2009-02-22 15:29:04 +08:00
|
|
|
}
|
2011-03-17 08:46:34 +08:00
|
|
|
llvm_unreachable("Invalid CastKind");
|
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());
|
|
|
|
}
|
|
|
|
|
2011-06-22 01:03:29 +08:00
|
|
|
llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
|
|
|
|
return Visit(E->GetTemporaryExpr());
|
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
|
2012-04-15 10:50:59 +08:00
|
|
|
if (ILE->isStringLiteralInit())
|
2008-02-22 01:45:41 +08:00
|
|
|
return Visit(ILE->getInit(0));
|
2010-04-19 03:06:43 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::ArrayType *AType =
|
2010-04-19 03:06:43 +08:00
|
|
|
cast<llvm::ArrayType>(ConvertType(ILE->getType()));
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ElemTy = AType->getElementType();
|
2012-04-15 10:50:59 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
2008-02-05 10:39:50 +08:00
|
|
|
unsigned NumElements = AType->getNumElements();
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Initialising an array requires us to automatically
|
2008-02-05 10:39:50 +08:00
|
|
|
// initialise any elements that have not been initialised explicitly
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
|
|
|
|
|
|
|
// Copy initializer elements.
|
2012-02-07 08:13:27 +08:00
|
|
|
std::vector<llvm::Constant*> Elts;
|
|
|
|
Elts.reserve(NumInitableElts + NumElements);
|
2012-02-14 20:06:21 +08:00
|
|
|
|
2008-05-31 03:58:50 +08:00
|
|
|
bool RewriteType = false;
|
2012-02-14 20:06:21 +08:00
|
|
|
for (unsigned i = 0; 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!
|
2011-04-21 08:27:41 +08:00
|
|
|
llvm::Constant *fillC;
|
|
|
|
if (Expr *filler = ILE->getArrayFiller())
|
|
|
|
fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF);
|
|
|
|
else
|
|
|
|
fillC = llvm::Constant::getNullValue(ElemTy);
|
|
|
|
if (!fillC)
|
|
|
|
return 0;
|
|
|
|
RewriteType |= (fillC->getType() != ElemTy);
|
2012-02-14 20:06:21 +08:00
|
|
|
Elts.resize(NumElements, fillC);
|
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
|
2011-07-11 17:56:20 +08:00
|
|
|
std::vector<llvm::Type*> Types;
|
2012-02-07 08:13:27 +08:00
|
|
|
Types.reserve(NumInitableElts + NumElements);
|
2012-02-07 07:46:08 +08:00
|
|
|
for (unsigned i = 0, e = Elts.size(); i < e; ++i)
|
2008-05-31 03:58:50 +08:00
|
|
|
Types.push_back(Elts[i]->getType());
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
|
2009-08-06 07:18:46 +08:00
|
|
|
Types, true);
|
2009-07-28 06:29:56 +08:00
|
|
|
return llvm::ConstantStruct::get(SType, Elts);
|
2008-05-31 03:58:50 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return llvm::ConstantArray::get(AType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 13:51:06 +08:00
|
|
|
llvm::Constant *EmitRecordInitialization(InitListExpr *ILE) {
|
2009-08-01 05:34:04 +08:00
|
|
|
return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
|
2008-05-30 18:24:46 +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
|
|
|
}
|
2009-09-09 23:08:12 +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()->isArrayType())
|
|
|
|
return EmitArrayInitialization(ILE);
|
|
|
|
|
2012-09-05 16:37:43 +08:00
|
|
|
if (ILE->getType()->isRecordType())
|
2012-09-11 13:51:06 +08:00
|
|
|
return EmitRecordInitialization(ILE);
|
2012-09-05 16:37:43 +08:00
|
|
|
|
2012-01-10 12:58:17 +08:00
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-02-22 01:57:49 +08:00
|
|
|
|
2010-02-02 16:02:49 +08:00
|
|
|
llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) {
|
|
|
|
if (!E->getConstructor()->isTrivial())
|
|
|
|
return 0;
|
|
|
|
|
2010-02-06 02:38:45 +08:00
|
|
|
QualType Ty = E->getType();
|
|
|
|
|
|
|
|
// FIXME: We should not have to call getBaseElementType here.
|
|
|
|
const RecordType *RT =
|
|
|
|
CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>();
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
|
|
|
|
// If the class doesn't have a trivial destructor, we can't emit it as a
|
|
|
|
// constant expr.
|
|
|
|
if (!RD->hasTrivialDestructor())
|
|
|
|
return 0;
|
|
|
|
|
2010-02-02 16:02:49 +08:00
|
|
|
// Only copy and default constructors can be trivial.
|
|
|
|
|
|
|
|
|
|
|
|
if (E->getNumArgs()) {
|
|
|
|
assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
|
2011-08-31 03:58:05 +08:00
|
|
|
assert(E->getConstructor()->isCopyOrMoveConstructor() &&
|
|
|
|
"trivial ctor has argument but isn't a copy/move ctor");
|
2010-02-02 16:02:49 +08:00
|
|
|
|
|
|
|
Expr *Arg = E->getArg(0);
|
|
|
|
assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
|
|
|
|
"argument to copy ctor is of wrong type");
|
|
|
|
|
2010-02-02 20:15:55 +08:00
|
|
|
return Visit(Arg);
|
2010-02-02 16:02:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return CGM.EmitNullConstant(Ty);
|
|
|
|
}
|
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitStringLiteral(StringLiteral *E) {
|
2011-11-01 10:23:42 +08:00
|
|
|
return CGM.GetConstantArrayFromStringLiteral(E);
|
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());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
// Resize the string to the right size, adding zeros at the end, or
|
|
|
|
// truncating as needed.
|
|
|
|
Str.resize(CAT->getSize().getZExtValue(), '\0');
|
2012-02-05 10:30:40 +08:00
|
|
|
return llvm::ConstantDataArray::getString(VMContext, Str, false);
|
2009-02-25 06:18:39 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +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
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ConvertType(QualType T) {
|
2008-01-26 09:36:00 +08:00
|
|
|
return CGM.getTypes().ConvertType(T);
|
|
|
|
}
|
2008-01-26 12:30:23 +08:00
|
|
|
|
2008-11-16 14:23:45 +08:00
|
|
|
public:
|
2011-11-13 06:28:03 +08:00
|
|
|
llvm::Constant *EmitLValue(APValue::LValueBase LVBase) {
|
|
|
|
if (const ValueDecl *Decl = LVBase.dyn_cast<const ValueDecl*>()) {
|
2010-03-05 05:26:03 +08:00
|
|
|
if (Decl->hasAttr<WeakRefAttr>())
|
2010-08-22 09:00:03 +08:00
|
|
|
return CGM.GetWeakRefReference(Decl);
|
2008-01-26 10:08:50 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
|
2009-09-11 07:43:36 +08:00
|
|
|
return CGM.GetAddrOfFunction(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.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!VD->hasLocalStorage()) {
|
2009-02-25 02:41:57 +08:00
|
|
|
if (VD->isFileVarDecl() || VD->hasExternalStorage())
|
|
|
|
return CGM.GetAddrOfGlobalVar(VD);
|
2010-10-15 12:57:14 +08:00
|
|
|
else if (VD->isLocalVarDecl()) {
|
2009-02-25 02:41:57 +08:00
|
|
|
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
|
|
|
}
|
2011-11-13 06:28:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr *E = const_cast<Expr*>(LVBase.get<const Expr*>());
|
|
|
|
switch (E->getStmtClass()) {
|
|
|
|
default: break;
|
|
|
|
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.
|
|
|
|
CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
|
2011-12-22 08:04:00 +08:00
|
|
|
llvm::Constant* C = CGM.EmitConstantExpr(CLE->getInitializer(),
|
|
|
|
CLE->getType(), CGF);
|
2011-11-13 06:28:03 +08:00
|
|
|
// FIXME: "Leaked" on failure.
|
|
|
|
if (C)
|
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
|
|
|
|
E->getType().isConstant(CGM.getContext()),
|
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2012-06-23 19:51:46 +08:00
|
|
|
C, ".compoundliteral", 0,
|
|
|
|
llvm::GlobalVariable::NotThreadLocal,
|
2011-11-13 06:28:03 +08:00
|
|
|
CGM.getContext().getTargetAddressSpace(E->getType()));
|
|
|
|
return C;
|
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);
|
2010-01-23 10:40:42 +08:00
|
|
|
llvm::Constant *C =
|
|
|
|
CGM.getObjCRuntime().GenerateConstantString(SL->getString());
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2008-12-12 13:18:02 +08:00
|
|
|
case Expr::PredefinedExprClass: {
|
2009-11-14 16:37:13 +08:00
|
|
|
unsigned Type = cast<PredefinedExpr>(E)->getIdentType();
|
|
|
|
if (CGF) {
|
2010-08-21 11:01:12 +08:00
|
|
|
LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E));
|
2009-11-14 16:37:13 +08:00
|
|
|
return cast<llvm::Constant>(Res.getAddress());
|
|
|
|
} else if (Type == PredefinedExpr::PrettyFunction) {
|
|
|
|
return CGM.GetAddrOfConstantCString("top level", ".tmp");
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-14 16:37:13 +08:00
|
|
|
return CGM.GetAddrOfConstantCString("", ".tmp");
|
2008-12-12 13:18:02 +08:00
|
|
|
}
|
2009-01-25 09:21:06 +08:00
|
|
|
case Expr::AddrLabelExprClass: {
|
|
|
|
assert(CGF && "Invalid address of label expression outside function.");
|
2009-10-29 07:59:40 +08:00
|
|
|
llvm::Constant *Ptr =
|
|
|
|
CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
|
|
|
|
return llvm::ConstantExpr::getBitCast(Ptr, 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);
|
2011-11-10 14:34:14 +08:00
|
|
|
unsigned builtin = CE->isBuiltinCall();
|
2010-01-23 10:40:42 +08:00
|
|
|
if (builtin !=
|
|
|
|
Builtin::BI__builtin___CFStringMakeConstantString &&
|
|
|
|
builtin !=
|
|
|
|
Builtin::BI__builtin___NSStringMakeConstantString)
|
2009-01-25 09:54:01 +08:00
|
|
|
break;
|
|
|
|
const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
|
|
|
|
const StringLiteral *Literal = cast<StringLiteral>(Arg);
|
2010-01-23 10:40:42 +08:00
|
|
|
if (builtin ==
|
|
|
|
Builtin::BI__builtin___NSStringMakeConstantString) {
|
|
|
|
return CGM.getObjCRuntime().GenerateConstantString(Literal);
|
|
|
|
}
|
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
|
|
|
}
|
2011-12-27 20:18:28 +08:00
|
|
|
case Expr::CXXTypeidExprClass: {
|
|
|
|
CXXTypeidExpr *Typeid = cast<CXXTypeidExpr>(E);
|
|
|
|
QualType T;
|
|
|
|
if (Typeid->isTypeOperand())
|
|
|
|
T = Typeid->getTypeOperand();
|
|
|
|
else
|
|
|
|
T = Typeid->getExprOperand()->getType();
|
|
|
|
return CGM.GetAddrOfRTTIDescriptor(T);
|
|
|
|
}
|
2012-10-11 18:13:44 +08:00
|
|
|
case Expr::CXXUuidofExprClass: {
|
|
|
|
return CGM.GetAddrOfUuidDescriptor(cast<CXXUuidofExpr>(E));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D,
|
|
|
|
CodeGenFunction *CGF) {
|
|
|
|
if (const APValue *Value = D.evaluateValue())
|
2012-03-03 07:27:11 +08:00
|
|
|
return EmitConstantValueForMemory(*Value, D.getType(), CGF);
|
2012-01-14 12:30:29 +08:00
|
|
|
|
2012-02-14 06:16:19 +08:00
|
|
|
// FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
|
|
|
|
// reference is a constant expression, and the reference binds to a temporary,
|
|
|
|
// then constant initialization is performed. ConstExprEmitter will
|
|
|
|
// incorrectly emit a prvalue constant in this case, and the calling code
|
|
|
|
// interprets that as the (pointer) value of the reference, rather than the
|
|
|
|
// desired value of the referee.
|
|
|
|
if (D.getType()->isReferenceType())
|
|
|
|
return 0;
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
const Expr *E = D.getInit();
|
|
|
|
assert(E && "No initializer to emit");
|
|
|
|
|
|
|
|
llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
|
|
|
|
if (C && C->getType()->isIntegerTy(1)) {
|
|
|
|
llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
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-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 09:08:03 +08:00
|
|
|
bool Success = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-27 14:04:58 +08:00
|
|
|
if (DestType->isReferenceType())
|
|
|
|
Success = E->EvaluateAsLValue(Result, Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2011-10-29 08:50:52 +08:00
|
|
|
Success = E->EvaluateAsRValue(Result, Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-03-03 07:27:11 +08:00
|
|
|
llvm::Constant *C = 0;
|
2012-01-14 12:30:29 +08:00
|
|
|
if (Success && !Result.HasSideEffects)
|
2012-03-03 07:27:11 +08:00
|
|
|
C = EmitConstantValue(Result.Val, DestType, CGF);
|
|
|
|
else
|
|
|
|
C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
if (C && C->getType()->isIntegerTy(1)) {
|
|
|
|
llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitConstantValue(const APValue &Value,
|
|
|
|
QualType DestType,
|
|
|
|
CodeGenFunction *CGF) {
|
|
|
|
switch (Value.getKind()) {
|
|
|
|
case APValue::Uninitialized:
|
|
|
|
llvm_unreachable("Constant expressions should be initialized.");
|
|
|
|
case APValue::LValue: {
|
|
|
|
llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
|
|
|
|
llvm::Constant *Offset =
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::ConstantInt::get(Int64Ty, Value.getLValueOffset().getQuantity());
|
2012-01-14 12:30:29 +08:00
|
|
|
|
|
|
|
llvm::Constant *C;
|
|
|
|
if (APValue::LValueBase LVBase = Value.getLValueBase()) {
|
|
|
|
// An array can be represented as an lvalue referring to the base.
|
|
|
|
if (isa<llvm::ArrayType>(DestTy)) {
|
|
|
|
assert(Offset->isNullValue() && "offset on array initializer");
|
|
|
|
return ConstExprEmitter(*this, CGF).Visit(
|
|
|
|
const_cast<Expr*>(LVBase.get<const Expr*>()));
|
|
|
|
}
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
C = ConstExprEmitter(*this, CGF).EmitLValue(LVBase);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
// Apply offset if necessary.
|
|
|
|
if (!Offset->isNullValue()) {
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
|
2012-01-14 12:30:29 +08:00
|
|
|
Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset);
|
|
|
|
C = llvm::ConstantExpr::getBitCast(Casted, C->getType());
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
|
|
|
return llvm::ConstantExpr::getBitCast(C, DestTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
return llvm::ConstantExpr::getPtrToInt(C, DestTy);
|
|
|
|
} else {
|
|
|
|
C = Offset;
|
2011-03-27 17:32:40 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
|
|
|
return llvm::ConstantExpr::getIntToPtr(C, DestTy);
|
2011-03-27 17:32:40 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
// If the types don't match this should only be a truncate.
|
|
|
|
if (C->getType() != DestTy)
|
|
|
|
return llvm::ConstantExpr::getTrunc(C, DestTy);
|
2011-03-27 17:32:40 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
return C;
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
}
|
2012-03-03 07:27:11 +08:00
|
|
|
case APValue::Int:
|
|
|
|
return llvm::ConstantInt::get(VMContext, Value.getInt());
|
2012-01-14 12:30:29 +08:00
|
|
|
case APValue::ComplexInt: {
|
|
|
|
llvm::Constant *Complex[2];
|
|
|
|
|
|
|
|
Complex[0] = llvm::ConstantInt::get(VMContext,
|
|
|
|
Value.getComplexIntReal());
|
|
|
|
Complex[1] = llvm::ConstantInt::get(VMContext,
|
|
|
|
Value.getComplexIntImag());
|
|
|
|
|
|
|
|
// FIXME: the target may want to specify that this is packed.
|
|
|
|
llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
|
|
|
|
Complex[1]->getType(),
|
|
|
|
NULL);
|
|
|
|
return llvm::ConstantStruct::get(STy, Complex);
|
|
|
|
}
|
|
|
|
case APValue::Float: {
|
|
|
|
const llvm::APFloat &Init = Value.getFloat();
|
|
|
|
if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf)
|
|
|
|
return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt());
|
|
|
|
else
|
|
|
|
return llvm::ConstantFP::get(VMContext, Init);
|
|
|
|
}
|
|
|
|
case APValue::ComplexFloat: {
|
|
|
|
llvm::Constant *Complex[2];
|
|
|
|
|
|
|
|
Complex[0] = llvm::ConstantFP::get(VMContext,
|
|
|
|
Value.getComplexFloatReal());
|
|
|
|
Complex[1] = llvm::ConstantFP::get(VMContext,
|
|
|
|
Value.getComplexFloatImag());
|
|
|
|
|
|
|
|
// FIXME: the target may want to specify that this is packed.
|
|
|
|
llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
|
|
|
|
Complex[1]->getType(),
|
|
|
|
NULL);
|
|
|
|
return llvm::ConstantStruct::get(STy, Complex);
|
|
|
|
}
|
|
|
|
case APValue::Vector: {
|
|
|
|
SmallVector<llvm::Constant *, 4> Inits;
|
|
|
|
unsigned NumElts = Value.getVectorLength();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
const APValue &Elt = Value.getVectorElt(i);
|
|
|
|
if (Elt.isInt())
|
|
|
|
Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
|
|
|
|
else
|
|
|
|
Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
return llvm::ConstantVector::get(Inits);
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
case APValue::AddrLabelDiff: {
|
|
|
|
const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
|
|
|
|
const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
|
|
|
|
llvm::Constant *LHS = EmitConstantExpr(LHSExpr, LHSExpr->getType(), CGF);
|
|
|
|
llvm::Constant *RHS = EmitConstantExpr(RHSExpr, RHSExpr->getType(), CGF);
|
|
|
|
|
|
|
|
// Compute difference
|
|
|
|
llvm::Type *ResultType = getTypes().ConvertType(DestType);
|
|
|
|
LHS = llvm::ConstantExpr::getPtrToInt(LHS, IntPtrTy);
|
|
|
|
RHS = llvm::ConstantExpr::getPtrToInt(RHS, IntPtrTy);
|
|
|
|
llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
|
|
|
|
|
|
|
|
// LLVM is a bit sensitive about the exact format of the
|
|
|
|
// address-of-label difference; make sure to truncate after
|
|
|
|
// the subtraction.
|
|
|
|
return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
|
|
|
|
}
|
|
|
|
case APValue::Struct:
|
|
|
|
case APValue::Union:
|
|
|
|
return ConstStructBuilder::BuildStruct(*this, CGF, Value, DestType);
|
|
|
|
case APValue::Array: {
|
|
|
|
const ArrayType *CAT = Context.getAsArrayType(DestType);
|
|
|
|
unsigned NumElements = Value.getArraySize();
|
|
|
|
unsigned NumInitElts = Value.getArrayInitializedElts();
|
2008-06-01 23:31:44 +08:00
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
std::vector<llvm::Constant*> Elts;
|
|
|
|
Elts.reserve(NumElements);
|
|
|
|
|
|
|
|
// Emit array filler, if there is one.
|
|
|
|
llvm::Constant *Filler = 0;
|
|
|
|
if (Value.hasArrayFiller())
|
2012-03-03 07:27:11 +08:00
|
|
|
Filler = EmitConstantValueForMemory(Value.getArrayFiller(),
|
|
|
|
CAT->getElementType(), CGF);
|
2012-01-14 12:30:29 +08:00
|
|
|
|
|
|
|
// Emit initializer elements.
|
|
|
|
llvm::Type *CommonElementType = 0;
|
|
|
|
for (unsigned I = 0; I < NumElements; ++I) {
|
|
|
|
llvm::Constant *C = Filler;
|
|
|
|
if (I < NumInitElts)
|
2012-03-03 07:27:11 +08:00
|
|
|
C = EmitConstantValueForMemory(Value.getArrayInitializedElt(I),
|
|
|
|
CAT->getElementType(), CGF);
|
2012-01-14 12:30:29 +08:00
|
|
|
if (I == 0)
|
|
|
|
CommonElementType = C->getType();
|
|
|
|
else if (C->getType() != CommonElementType)
|
|
|
|
CommonElementType = 0;
|
|
|
|
Elts.push_back(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CommonElementType) {
|
|
|
|
// FIXME: Try to avoid packing the array
|
|
|
|
std::vector<llvm::Type*> Types;
|
2012-02-07 08:13:27 +08:00
|
|
|
Types.reserve(NumElements);
|
2012-02-07 07:46:08 +08:00
|
|
|
for (unsigned i = 0, e = Elts.size(); i < e; ++i)
|
2012-01-14 12:30:29 +08:00
|
|
|
Types.push_back(Elts[i]->getType());
|
|
|
|
llvm::StructType *SType = llvm::StructType::get(VMContext, Types, true);
|
|
|
|
return llvm::ConstantStruct::get(SType, Elts);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ArrayType *AType =
|
|
|
|
llvm::ArrayType::get(CommonElementType, NumElements);
|
|
|
|
return llvm::ConstantArray::get(AType, Elts);
|
2008-06-01 23:31:44 +08:00
|
|
|
}
|
2012-01-14 12:30:29 +08:00
|
|
|
case APValue::MemberPointer:
|
|
|
|
return getCXXABI().EmitMemberPointer(Value, DestType);
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown APValue kind");
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
|
2012-03-03 07:27:11 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::EmitConstantValueForMemory(const APValue &Value,
|
|
|
|
QualType DestType,
|
|
|
|
CodeGenFunction *CGF) {
|
|
|
|
llvm::Constant *C = EmitConstantValue(Value, DestType, CGF);
|
|
|
|
if (C->getType()->isIntegerTy(1)) {
|
|
|
|
llvm::Type *BoolTy = getTypes().ConvertTypeForMem(DestType);
|
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
|
|
|
}
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2011-11-23 06:48:32 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
|
|
|
|
assert(E->isFileScope() && "not a file-scope compound literal expr");
|
|
|
|
return ConstExprEmitter(*this, 0).EmitLValue(E);
|
|
|
|
}
|
|
|
|
|
2011-02-03 16:15:49 +08:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
|
|
|
|
// Member pointer constants always have a very particular form.
|
|
|
|
const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
|
|
|
|
const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
|
|
|
|
|
|
|
|
// A member function pointer.
|
|
|
|
if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
|
|
|
|
return getCXXABI().EmitMemberPointer(method);
|
|
|
|
|
|
|
|
// Otherwise, a member data pointer.
|
2012-01-14 12:30:29 +08:00
|
|
|
uint64_t fieldOffset = getContext().getFieldOffset(decl);
|
2011-02-03 16:15:49 +08:00
|
|
|
CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
|
|
|
|
return getCXXABI().EmitMemberDataPointer(type, chars);
|
|
|
|
}
|
|
|
|
|
2010-05-19 00:51:41 +08:00
|
|
|
static void
|
|
|
|
FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T,
|
2012-02-07 08:04:27 +08:00
|
|
|
SmallVectorImpl<llvm::Constant *> &Elements,
|
2010-05-19 00:51:41 +08:00
|
|
|
uint64_t StartOffset) {
|
2011-03-22 08:24:31 +08:00
|
|
|
assert(StartOffset % CGM.getContext().getCharWidth() == 0 &&
|
|
|
|
"StartOffset not byte aligned!");
|
2010-05-19 00:51:41 +08:00
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
if (CGM.getTypes().isZeroInitializable(T))
|
2010-05-19 00:51:41 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (const ConstantArrayType *CAT =
|
|
|
|
CGM.getContext().getAsConstantArrayType(T)) {
|
|
|
|
QualType ElementTy = CAT->getElementType();
|
|
|
|
uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy);
|
|
|
|
|
|
|
|
for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) {
|
|
|
|
FillInNullDataMemberPointers(CGM, ElementTy, Elements,
|
|
|
|
StartOffset + I * ElementSize);
|
|
|
|
}
|
|
|
|
} else if (const RecordType *RT = T->getAs<RecordType>()) {
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Go through all bases and fill in any null pointer to data members.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
2010-05-28 02:51:01 +08:00
|
|
|
if (I->isVirtual()) {
|
2010-11-23 02:42:14 +08:00
|
|
|
// Ignore virtual bases.
|
2010-05-28 02:51:01 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-19 00:51:41 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// Ignore empty bases.
|
|
|
|
if (BaseDecl->isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Ignore bases that don't have any pointer to data members.
|
2010-08-23 05:01:12 +08:00
|
|
|
if (CGM.getTypes().isZeroInitializable(BaseDecl))
|
2010-05-19 00:51:41 +08:00
|
|
|
continue;
|
|
|
|
|
2012-07-05 02:45:14 +08:00
|
|
|
uint64_t BaseOffset =
|
|
|
|
CGM.getContext().toBits(Layout.getBaseClassOffset(BaseDecl));
|
2010-05-19 00:51:41 +08:00
|
|
|
FillInNullDataMemberPointers(CGM, I->getType(),
|
|
|
|
Elements, StartOffset + BaseOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Visit all fields.
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
for (RecordDecl::field_iterator I = RD->field_begin(),
|
|
|
|
E = RD->field_end(); I != E; ++I, ++FieldNo) {
|
|
|
|
QualType FieldType = I->getType();
|
|
|
|
|
2010-08-23 05:01:12 +08:00
|
|
|
if (CGM.getTypes().isZeroInitializable(FieldType))
|
2010-05-19 00:51:41 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo);
|
|
|
|
FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(T->isMemberPointerType() && "Should only see member pointers here!");
|
|
|
|
assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
|
|
|
|
"Should only see pointers to data members here!");
|
|
|
|
|
2011-03-22 08:24:31 +08:00
|
|
|
CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset);
|
|
|
|
CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T);
|
2010-05-19 00:51:41 +08:00
|
|
|
|
2011-02-03 16:15:49 +08:00
|
|
|
// FIXME: hardcodes Itanium member pointer representation!
|
2010-05-19 00:51:41 +08:00
|
|
|
llvm::Constant *NegativeOne =
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::ConstantInt::get(CGM.Int8Ty, -1ULL, /*isSigned*/true);
|
2010-05-19 00:51:41 +08:00
|
|
|
|
|
|
|
// Fill in the null data member pointer.
|
2011-03-22 08:24:31 +08:00
|
|
|
for (CharUnits I = StartIndex; I != EndIndex; ++I)
|
|
|
|
Elements[I.getQuantity()] = NegativeOne;
|
2010-05-19 00:51:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *baseType,
|
2011-02-15 14:40:56 +08:00
|
|
|
const CXXRecordDecl *base);
|
2010-11-23 02:42:14 +08:00
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
|
|
|
|
const CXXRecordDecl *record,
|
|
|
|
bool asCompleteObject) {
|
|
|
|
const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::StructType *structure =
|
2011-02-15 14:40:56 +08:00
|
|
|
(asCompleteObject ? layout.getLLVMType()
|
|
|
|
: layout.getBaseSubobjectLLVMType());
|
|
|
|
|
|
|
|
unsigned numElements = structure->getNumElements();
|
|
|
|
std::vector<llvm::Constant *> elements(numElements);
|
|
|
|
|
|
|
|
// Fill in all the bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator
|
|
|
|
I = record->bases_begin(), E = record->bases_end(); I != E; ++I) {
|
2010-11-23 02:42:14 +08:00
|
|
|
if (I->isVirtual()) {
|
2011-02-15 14:40:56 +08:00
|
|
|
// Ignore virtual bases; if we're laying out for a complete
|
|
|
|
// object, we'll lay these out later.
|
2010-11-23 02:42:14 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
const CXXRecordDecl *base =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
|
2010-11-23 02:42:14 +08:00
|
|
|
|
|
|
|
// Ignore empty bases.
|
2011-02-15 14:40:56 +08:00
|
|
|
if (base->isEmpty())
|
2010-11-23 02:42:14 +08:00
|
|
|
continue;
|
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *baseType = structure->getElementType(fieldIndex);
|
2011-02-15 14:40:56 +08:00
|
|
|
elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
|
2010-11-23 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
// Fill in all the fields.
|
|
|
|
for (RecordDecl::field_iterator I = record->field_begin(),
|
|
|
|
E = record->field_end(); I != E; ++I) {
|
2012-06-07 04:45:41 +08:00
|
|
|
const FieldDecl *field = *I;
|
2011-12-07 09:30:11 +08:00
|
|
|
|
|
|
|
// Fill in non-bitfields. (Bitfields always use a zero pattern, which we
|
|
|
|
// will fill in later.)
|
|
|
|
if (!field->isBitField()) {
|
|
|
|
unsigned fieldIndex = layout.getLLVMFieldNo(field);
|
|
|
|
elements[fieldIndex] = CGM.EmitNullConstant(field->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
// For unions, stop after the first named field.
|
|
|
|
if (record->isUnion() && field->getDeclName())
|
|
|
|
break;
|
2011-02-15 14:40:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the virtual bases, if we're working with the complete object.
|
|
|
|
if (asCompleteObject) {
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator
|
|
|
|
I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *base =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// Ignore empty bases.
|
|
|
|
if (base->isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned fieldIndex = layout.getVirtualBaseIndex(base);
|
|
|
|
|
|
|
|
// We might have already laid this field out.
|
|
|
|
if (elements[fieldIndex]) continue;
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *baseType = structure->getElementType(fieldIndex);
|
2011-02-15 14:40:56 +08:00
|
|
|
elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
|
|
|
|
}
|
2010-11-23 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through all other fields and zero them out.
|
2011-02-15 14:40:56 +08:00
|
|
|
for (unsigned i = 0; i != numElements; ++i) {
|
|
|
|
if (!elements[i])
|
|
|
|
elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
|
2010-11-23 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 14:40:56 +08:00
|
|
|
return llvm::ConstantStruct::get(structure, elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the null constant for a base subobject.
|
|
|
|
static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *baseType,
|
2011-02-15 14:40:56 +08:00
|
|
|
const CXXRecordDecl *base) {
|
|
|
|
const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
|
|
|
|
|
|
|
|
// Just zero out bases that don't have any pointer to data members.
|
|
|
|
if (baseLayout.isZeroInitializableAsBase())
|
|
|
|
return llvm::Constant::getNullValue(baseType);
|
|
|
|
|
|
|
|
// If the base type is a struct, we can just use its null constant.
|
|
|
|
if (isa<llvm::StructType>(baseType)) {
|
|
|
|
return EmitNullConstant(CGM, base, /*complete*/ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, some bases are represented as arrays of i8 if the size
|
|
|
|
// of the base is smaller than its corresponding LLVM type. Figure
|
|
|
|
// out how many elements this base array has.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType);
|
2011-02-15 14:40:56 +08:00
|
|
|
unsigned numBaseElements = baseArrayType->getNumElements();
|
|
|
|
|
|
|
|
// Fill in null data member pointers.
|
2012-02-07 08:04:27 +08:00
|
|
|
SmallVector<llvm::Constant *, 16> baseElements(numBaseElements);
|
2011-02-15 14:40:56 +08:00
|
|
|
FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base),
|
|
|
|
baseElements, 0);
|
|
|
|
|
|
|
|
// Now go through all other elements and zero them out.
|
|
|
|
if (numBaseElements) {
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Constant *i8_zero = llvm::Constant::getNullValue(CGM.Int8Ty);
|
2011-02-15 14:40:56 +08:00
|
|
|
for (unsigned i = 0; i != numBaseElements; ++i) {
|
|
|
|
if (!baseElements[i])
|
|
|
|
baseElements[i] = i8_zero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::ConstantArray::get(baseArrayType, baseElements);
|
2010-11-23 02:42:14 +08:00
|
|
|
}
|
|
|
|
|
2009-04-14 05:47:26 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
|
2010-08-23 05:01:12 +08:00
|
|
|
if (getTypes().isZeroInitializable(T))
|
2009-08-25 01:16:23 +08:00
|
|
|
return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
|
2010-02-02 13:17:25 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
|
2012-02-07 06:00:56 +08:00
|
|
|
llvm::ArrayType *ATy =
|
|
|
|
cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
QualType ElementTy = CAT->getElementType();
|
|
|
|
|
2010-02-02 13:17:25 +08:00
|
|
|
llvm::Constant *Element = EmitNullConstant(ElementTy);
|
|
|
|
unsigned NumElements = CAT->getSize().getZExtValue();
|
2012-02-07 06:00:56 +08:00
|
|
|
|
|
|
|
if (Element->isNullValue())
|
|
|
|
return llvm::ConstantAggregateZero::get(ATy);
|
|
|
|
|
|
|
|
SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
|
2010-02-02 13:17:25 +08:00
|
|
|
return llvm::ConstantArray::get(ATy, Array);
|
2009-08-10 02:26:27 +08:00
|
|
|
}
|
2009-08-23 09:25:01 +08:00
|
|
|
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
2010-02-02 13:17:25 +08:00
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
2011-02-15 14:40:56 +08:00
|
|
|
return ::EmitNullConstant(*this, RD, /*complete object*/ true);
|
2009-08-23 09:25:01 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-19 00:51:41 +08:00
|
|
|
assert(T->isMemberPointerType() && "Should only see member pointers here!");
|
2010-02-02 13:17:25 +08:00
|
|
|
assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
|
|
|
|
"Should only see pointers to data members here!");
|
|
|
|
|
|
|
|
// Itanium C++ ABI 2.3:
|
|
|
|
// A NULL pointer is represented as -1.
|
2011-02-03 16:15:49 +08:00
|
|
|
return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
|
2009-04-14 05:47:26 +08:00
|
|
|
}
|
2011-10-14 10:27:24 +08:00
|
|
|
|
|
|
|
llvm::Constant *
|
|
|
|
CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
|
|
|
|
return ::EmitNullConstant(*this, Record, false);
|
|
|
|
}
|