2008-01-26 04:36:00 +03: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 11:33:07 +04:00
|
|
|
#include "CGCXXABI.h"
|
2008-08-13 04:59:25 +04:00
|
|
|
#include "CGObjCRuntime.h"
|
2010-03-31 02:26:10 +04:00
|
|
|
#include "CGRecordLayout.h"
|
2008-10-06 09:59:01 +04:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-08-11 09:00:27 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-07-24 19:20:52 +04:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-08-11 09:00:27 +04:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-06-14 05:54:56 +04:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2008-01-26 04:36:00 +03:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
2008-05-30 14:24:46 +04:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-01-26 04:36:00 +03:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConstStructBuilder
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 22:45:26 +03:00
|
|
|
class ConstStructBuilder {
|
2009-07-24 19:20:52 +04:00
|
|
|
CodeGenModule &CGM;
|
|
|
|
CodeGenFunction *CGF;
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
bool Packed;
|
2009-07-24 19:20:52 +04:00
|
|
|
unsigned NextFieldOffsetInBytes;
|
2009-10-02 06:15:20 +04:00
|
|
|
unsigned LLVMStructAlignment;
|
2009-07-24 19:20:52 +04:00
|
|
|
std::vector<llvm::Constant *> Elements;
|
2010-04-13 21:45:57 +04:00
|
|
|
public:
|
|
|
|
static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
|
|
|
|
InitListExpr *ILE);
|
|
|
|
|
|
|
|
private:
|
2009-07-24 19:20:52 +04:00
|
|
|
ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
|
2009-10-02 06:15:20 +04:00
|
|
|
: CGM(CGM), CGF(CGF), Packed(false), NextFieldOffsetInBytes(0),
|
|
|
|
LLVMStructAlignment(1) { }
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
bool 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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04:00
|
|
|
llvm::Constant *InitExpr);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-07-05 21:04:23 +04:00
|
|
|
void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
|
|
|
|
llvm::ConstantInt *InitExpr);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
void AppendPadding(uint64_t NumBytes);
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
void AppendTailPadding(uint64_t RecordSize);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
void ConvertStructToPacked();
|
|
|
|
|
|
|
|
bool Build(InitListExpr *ILE);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
unsigned getAlignment(const llvm::Constant *C) const {
|
|
|
|
if (Packed) return 1;
|
|
|
|
return CGM.getTargetData().getABITypeAlignment(C->getType());
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
uint64_t getSizeInBytes(const llvm::Constant *C) const {
|
|
|
|
return CGM.getTargetData().getTypeAllocSize(C->getType());
|
|
|
|
}
|
|
|
|
};
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
bool 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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04:00
|
|
|
AppendField(const FieldDecl *Field, uint64_t FieldOffset,
|
|
|
|
llvm::Constant *InitCst) {
|
2010-04-13 21:45:57 +04:00
|
|
|
uint64_t FieldOffsetInBytes = FieldOffset / 8;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
assert(NextFieldOffsetInBytes <= FieldOffsetInBytes
|
|
|
|
&& "Field offset mismatch!");
|
2009-09-09 19:08:12 +04: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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04:00
|
|
|
unsigned FieldAlignment = getAlignment(InitCst);
|
2009-10-02 08:52:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
// Round up the field offset to the alignment of the field type.
|
|
|
|
uint64_t AlignedNextFieldOffsetInBytes =
|
|
|
|
llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
if (AlignedNextFieldOffsetInBytes > FieldOffsetInBytes) {
|
|
|
|
assert(!Packed && "Alignment is wrong even with a packed struct!");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
// Convert the struct to a packed struct.
|
|
|
|
ConvertStructToPacked();
|
|
|
|
|
|
|
|
AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
|
|
|
|
// We need to append padding.
|
|
|
|
AppendPadding(FieldOffsetInBytes - NextFieldOffsetInBytes);
|
2009-07-27 05:23:51 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
assert(NextFieldOffsetInBytes == FieldOffsetInBytes &&
|
|
|
|
"Did not add enough padding!");
|
2009-07-27 05:23:51 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04: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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04:00
|
|
|
Elements.push_back(InitCst);
|
|
|
|
NextFieldOffsetInBytes = AlignedNextFieldOffsetInBytes +
|
|
|
|
getSizeInBytes(InitCst);
|
2010-04-13 21:45:57 +04:00
|
|
|
|
|
|
|
if (Packed)
|
|
|
|
assert(LLVMStructAlignment == 1 && "Packed struct not byte-aligned!");
|
|
|
|
else
|
|
|
|
LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-05 21:04:23 +04:00
|
|
|
void ConstStructBuilder::AppendBitField(const FieldDecl *Field,
|
|
|
|
uint64_t FieldOffset,
|
|
|
|
llvm::ConstantInt *CI) {
|
2010-04-13 21:45:57 +04:00
|
|
|
if (FieldOffset > NextFieldOffsetInBytes * 8) {
|
|
|
|
// We need to add padding.
|
|
|
|
uint64_t NumBytes =
|
|
|
|
llvm::RoundUpToAlignment(FieldOffset -
|
|
|
|
NextFieldOffsetInBytes * 8, 8) / 8;
|
|
|
|
|
|
|
|
AppendPadding(NumBytes);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
uint64_t FieldSize =
|
|
|
|
Field->getBitWidth()->EvaluateAsInt(CGM.getContext()).getZExtValue();
|
2009-07-27 05:23:51 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
llvm::APInt FieldValue = CI->getValue();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04: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())
|
|
|
|
FieldValue.zext(FieldSize);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
// Truncate the size of FieldValue to the bit field size.
|
|
|
|
if (FieldSize < FieldValue.getBitWidth())
|
|
|
|
FieldValue.trunc(FieldSize);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
if (FieldOffset < NextFieldOffsetInBytes * 8) {
|
|
|
|
// 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 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
unsigned BitsInPreviousByte =
|
|
|
|
NextFieldOffsetInBytes * 8 - FieldOffset;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
bool FitsCompletelyInPreviousByte =
|
|
|
|
BitsInPreviousByte >= FieldValue.getBitWidth();
|
2009-07-27 06:56:37 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
llvm::APInt Tmp = FieldValue;
|
2009-07-27 06:56:37 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
if (!FitsCompletelyInPreviousByte) {
|
|
|
|
unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-27 06:56:37 +04:00
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
2010-04-13 21:45:57 +04:00
|
|
|
Tmp = Tmp.lshr(NewFieldWidth);
|
|
|
|
Tmp.trunc(BitsInPreviousByte);
|
2009-07-27 06:56:37 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
// We want the remaining high bits.
|
|
|
|
FieldValue.trunc(NewFieldWidth);
|
|
|
|
} else {
|
|
|
|
Tmp.trunc(BitsInPreviousByte);
|
2009-12-09 00:12:32 +03:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
// We want the remaining low bits.
|
|
|
|
FieldValue = FieldValue.lshr(BitsInPreviousByte);
|
|
|
|
FieldValue.trunc(NewFieldWidth);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
Tmp.zext(8);
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
2009-07-27 06:56:37 +04:00
|
|
|
if (FitsCompletelyInPreviousByte)
|
2010-04-13 21:45:57 +04:00
|
|
|
Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
|
|
|
|
} else {
|
|
|
|
Tmp = Tmp.shl(8 - BitsInPreviousByte);
|
2009-07-27 05:23:51 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-07-05 22:03:30 +04: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-13 21:45:57 +04:00
|
|
|
Tmp |= Val->getValue();
|
2010-07-05 22:03:30 +04: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");
|
|
|
|
const llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType());
|
|
|
|
assert(AT->getElementType()->isIntegerTy(8) &&
|
|
|
|
AT->getNumElements() != 0 &&
|
|
|
|
"Expected non-empty array padding of undefs");
|
|
|
|
|
|
|
|
// Remove the padding array.
|
|
|
|
NextFieldOffsetInBytes -= AT->getNumElements();
|
|
|
|
Elements.pop_back();
|
|
|
|
|
|
|
|
// Add the padding back in two chunks.
|
|
|
|
AppendPadding(AT->getNumElements()-1);
|
|
|
|
AppendPadding(1);
|
|
|
|
assert(isa<llvm::UndefValue>(Elements.back()) &&
|
|
|
|
Elements.back()->getType()->isIntegerTy(8) &&
|
|
|
|
"Padding addition didn't work right");
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
if (FitsCompletelyInPreviousByte)
|
2010-07-05 21:04:23 +04:00
|
|
|
return;
|
2010-04-13 21:45:57 +04:00
|
|
|
}
|
2009-07-27 05:23:51 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
while (FieldValue.getBitWidth() > 8) {
|
|
|
|
llvm::APInt Tmp;
|
2009-07-27 05:23:51 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
// We want the high bits.
|
|
|
|
Tmp = FieldValue;
|
|
|
|
Tmp = Tmp.lshr(Tmp.getBitWidth() - 8);
|
|
|
|
Tmp.trunc(8);
|
|
|
|
} else {
|
|
|
|
// We want the low bits.
|
|
|
|
Tmp = FieldValue;
|
|
|
|
Tmp.trunc(8);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
FieldValue = FieldValue.lshr(8);
|
2009-07-27 05:23:51 +04:00
|
|
|
}
|
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
|
2009-07-27 05:23:51 +04:00
|
|
|
NextFieldOffsetInBytes++;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
FieldValue.trunc(FieldValue.getBitWidth() - 8);
|
|
|
|
}
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
assert(FieldValue.getBitWidth() > 0 &&
|
|
|
|
"Should have at least one bit left!");
|
|
|
|
assert(FieldValue.getBitWidth() <= 8 &&
|
|
|
|
"Should not have more than a byte left!");
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
if (FieldValue.getBitWidth() < 8) {
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
unsigned BitWidth = FieldValue.getBitWidth();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
FieldValue.zext(8);
|
|
|
|
FieldValue = FieldValue << (8 - BitWidth);
|
|
|
|
} else
|
|
|
|
FieldValue.zext(8);
|
2009-07-24 19:20:52 +04:00
|
|
|
}
|
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
// Append the last element.
|
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
|
|
|
|
FieldValue));
|
|
|
|
NextFieldOffsetInBytes++;
|
|
|
|
}
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
void ConstStructBuilder::AppendPadding(uint64_t NumBytes) {
|
|
|
|
if (!NumBytes)
|
|
|
|
return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext());
|
|
|
|
if (NumBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumBytes);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-17 00:56:35 +04:00
|
|
|
llvm::Constant *C = llvm::UndefValue::get(Ty);
|
2010-04-13 21:45:57 +04:00
|
|
|
Elements.push_back(C);
|
|
|
|
assert(getAlignment(C) == 1 && "Padding must have 1 byte alignment!");
|
2009-10-02 06:15:20 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
NextFieldOffsetInBytes += getSizeInBytes(C);
|
|
|
|
}
|
2009-10-02 06:15:20 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
void ConstStructBuilder::AppendTailPadding(uint64_t RecordSize) {
|
|
|
|
assert(RecordSize % 8 == 0 && "Invalid record size!");
|
2009-10-02 06:15:20 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
uint64_t RecordSizeInBytes = RecordSize / 8;
|
|
|
|
assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
|
2009-10-02 06:15:20 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
|
|
|
|
AppendPadding(NumPadBytes);
|
|
|
|
}
|
2009-10-02 06:15:20 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
void ConstStructBuilder::ConvertStructToPacked() {
|
|
|
|
std::vector<llvm::Constant *> PackedElements;
|
|
|
|
uint64_t ElementOffsetInBytes = 0;
|
2009-10-02 06:15:20 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
|
|
|
|
llvm::Constant *C = Elements[i];
|
2009-10-02 06:15:20 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
unsigned ElementAlign =
|
|
|
|
CGM.getTargetData().getABITypeAlignment(C->getType());
|
|
|
|
uint64_t AlignedElementOffsetInBytes =
|
|
|
|
llvm::RoundUpToAlignment(ElementOffsetInBytes, ElementAlign);
|
2009-10-02 06:15:20 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
if (AlignedElementOffsetInBytes > ElementOffsetInBytes) {
|
|
|
|
// We need some padding.
|
|
|
|
uint64_t NumBytes =
|
|
|
|
AlignedElementOffsetInBytes - ElementOffsetInBytes;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext());
|
|
|
|
if (NumBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumBytes);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-17 00:56:35 +04:00
|
|
|
llvm::Constant *Padding = llvm::UndefValue::get(Ty);
|
2010-04-13 21:45:57 +04:00
|
|
|
PackedElements.push_back(Padding);
|
|
|
|
ElementOffsetInBytes += getSizeInBytes(Padding);
|
2009-07-24 19:20:52 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
PackedElements.push_back(C);
|
|
|
|
ElementOffsetInBytes += getSizeInBytes(C);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
assert(ElementOffsetInBytes == NextFieldOffsetInBytes &&
|
|
|
|
"Packing the struct changed its size!");
|
|
|
|
|
|
|
|
Elements = PackedElements;
|
|
|
|
LLVMStructAlignment = 1;
|
|
|
|
Packed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstStructBuilder::Build(InitListExpr *ILE) {
|
|
|
|
RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
unsigned FieldNo = 0;
|
|
|
|
unsigned ElementNo = 0;
|
|
|
|
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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04:00
|
|
|
FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
|
|
|
|
|
|
|
|
// If this is a union, skip all the fields that aren't being initialized.
|
2010-04-13 21:45:57 +04:00
|
|
|
if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
|
|
|
|
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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04:00
|
|
|
// Don't emit anonymous bitfields, they just affect layout.
|
|
|
|
if (Field->isBitField() && !Field->getIdentifier())
|
|
|
|
continue;
|
2009-09-09 19:08:12 +04: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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04: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 03:55:01 +04: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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04:00
|
|
|
|
|
|
|
if (!Field->isBitField()) {
|
|
|
|
// Handle non-bitfield members.
|
|
|
|
if (!AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit))
|
2010-04-13 21:45:57 +04:00
|
|
|
return false;
|
|
|
|
} 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.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101155 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-13 22:16:19 +04:00
|
|
|
// Otherwise we have a bitfield.
|
2010-07-05 21:04:23 +04:00
|
|
|
AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
|
|
|
|
cast<llvm::ConstantInt>(EltInit));
|
2009-10-02 06:15:20 +04:00
|
|
|
}
|
2010-04-13 21:45:57 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
uint64_t LayoutSizeInBytes = Layout.getSize() / 8;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
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.
|
2009-07-24 19:20:52 +04:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
uint64_t LLVMSizeInBytes = llvm::RoundUpToAlignment(NextFieldOffsetInBytes,
|
|
|
|
LLVMStructAlignment);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
// Check if we need to convert the struct to a packed struct.
|
|
|
|
if (NextFieldOffsetInBytes <= LayoutSizeInBytes &&
|
|
|
|
LLVMSizeInBytes > LayoutSizeInBytes) {
|
|
|
|
assert(!Packed && "Size mismatch!");
|
|
|
|
|
|
|
|
ConvertStructToPacked();
|
2010-04-17 01:02:32 +04:00
|
|
|
assert(NextFieldOffsetInBytes <= LayoutSizeInBytes &&
|
2010-04-13 21:45:57 +04:00
|
|
|
"Converting to packed did not help!");
|
2009-07-24 19:20:52 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
// Append tail padding if necessary.
|
|
|
|
AppendTailPadding(Layout.getSize());
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
assert(Layout.getSize() / 8 == NextFieldOffsetInBytes &&
|
|
|
|
"Tail padding mismatch!");
|
2009-07-24 19:20:52 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *ConstStructBuilder::
|
|
|
|
BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, InitListExpr *ILE) {
|
|
|
|
ConstStructBuilder Builder(CGM, CGF);
|
|
|
|
|
|
|
|
if (!Builder.Build(ILE))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
llvm::Constant *Result =
|
|
|
|
llvm::ConstantStruct::get(CGM.getLLVMContext(),
|
|
|
|
Builder.Elements, Builder.Packed);
|
|
|
|
|
|
|
|
assert(llvm::RoundUpToAlignment(Builder.NextFieldOffsetInBytes,
|
|
|
|
Builder.getAlignment(Result)) ==
|
|
|
|
Builder.getSizeInBytes(Result) && "Size mismatch!");
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-13 21:45:57 +04:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConstExprEmitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-28 22:45:26 +03:00
|
|
|
class ConstExprEmitter :
|
2008-01-26 07:30:23 +03:00
|
|
|
public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
|
2008-01-26 04:36:00 +03:00
|
|
|
CodeGenModule &CGM;
|
2008-02-27 00:41:45 +03:00
|
|
|
CodeGenFunction *CGF;
|
2009-07-15 03:10:40 +04:00
|
|
|
llvm::LLVMContext &VMContext;
|
2008-01-26 04:36:00 +03:00
|
|
|
public:
|
2008-02-27 00:41:45 +03:00
|
|
|
ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
|
2009-07-15 03:10:40 +04:00
|
|
|
: CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
|
2008-01-26 04:36:00 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-01-26 04:36:00 +03:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-01-26 04:36:00 +03:00
|
|
|
llvm::Constant *VisitStmt(Stmt *S) {
|
2009-03-03 19:43:34 +03:00
|
|
|
return 0;
|
2008-01-26 04:36:00 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
llvm::Constant *VisitParenExpr(ParenExpr *PE) {
|
|
|
|
return Visit(PE->getSubExpr());
|
2008-01-26 04:36:00 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-01-26 04:36:00 +03:00
|
|
|
llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
return Visit(E->getInitializer());
|
|
|
|
}
|
2009-10-13 11:14:16 +04:00
|
|
|
|
2009-10-03 19:02:02 +04:00
|
|
|
llvm::Constant *VisitUnaryAddrOf(UnaryOperator *E) {
|
|
|
|
if (const MemberPointerType *MPT =
|
2010-08-22 14:59:02 +04:00
|
|
|
E->getType()->getAs<MemberPointerType>()) {
|
2010-02-02 06:37:46 +03:00
|
|
|
DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr());
|
|
|
|
NamedDecl *ND = DRE->getDecl();
|
2010-08-23 05:21:21 +04:00
|
|
|
if (MPT->isMemberFunctionPointer())
|
|
|
|
return CGM.getCXXABI().EmitMemberPointer(cast<CXXMethodDecl>(ND));
|
|
|
|
else
|
|
|
|
return CGM.getCXXABI().EmitMemberPointer(cast<FieldDecl>(ND));
|
2009-10-03 19:02:02 +04:00
|
|
|
}
|
2010-02-02 06:37:46 +03:00
|
|
|
|
2009-10-03 19:02:02 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-13 11:14:16 +04:00
|
|
|
llvm::Constant *VisitBinSub(BinaryOperator *E) {
|
|
|
|
// This must be a pointer/pointer subtraction. This only happens for
|
|
|
|
// address of label.
|
|
|
|
if (!isa<AddrLabelExpr>(E->getLHS()->IgnoreParenNoopCasts(CGM.getContext())) ||
|
|
|
|
!isa<AddrLabelExpr>(E->getRHS()->IgnoreParenNoopCasts(CGM.getContext())))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
llvm::Constant *LHS = CGM.EmitConstantExpr(E->getLHS(),
|
|
|
|
E->getLHS()->getType(), CGF);
|
|
|
|
llvm::Constant *RHS = CGM.EmitConstantExpr(E->getRHS(),
|
|
|
|
E->getRHS()->getType(), CGF);
|
|
|
|
|
|
|
|
const llvm::Type *ResultType = ConvertType(E->getType());
|
|
|
|
LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType);
|
|
|
|
RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType);
|
|
|
|
|
|
|
|
// No need to divide by element size, since addr of label is always void*,
|
|
|
|
// which has size 1 in GNUish.
|
|
|
|
return llvm::ConstantExpr::getSub(LHS, RHS);
|
|
|
|
}
|
|
|
|
|
2008-08-19 03:01:59 +04:00
|
|
|
llvm::Constant *VisitCastExpr(CastExpr* E) {
|
2009-08-23 03:54:44 +04:00
|
|
|
switch (E->getCastKind()) {
|
2010-08-25 15:45:40 +04:00
|
|
|
case CK_ToUnion: {
|
2009-08-23 03:54:44 +04:00
|
|
|
// GCC cast to union extension
|
|
|
|
assert(E->getType()->isUnionType() &&
|
|
|
|
"Destination type is not union type!");
|
2009-01-17 03:48:48 +03:00
|
|
|
const llvm::Type *Ty = ConvertType(E->getType());
|
2009-04-08 08:48:15 +04:00
|
|
|
Expr *SubExpr = E->getSubExpr();
|
2009-08-23 03:54:44 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *C =
|
2009-08-01 01:38:39 +04:00
|
|
|
CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF);
|
|
|
|
if (!C)
|
|
|
|
return 0;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-01 01:38:39 +04: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());
|
|
|
|
unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType());
|
|
|
|
unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(Ty);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-01 01:38:39 +04:00
|
|
|
assert(CurSize <= TotalSize && "Union size mismatch!");
|
|
|
|
if (unsigned NumPadBytes = TotalSize - CurSize) {
|
2009-08-14 01:57:51 +04:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext);
|
2009-08-01 01:38:39 +04:00
|
|
|
if (NumPadBytes > 1)
|
|
|
|
Ty = llvm::ArrayType::get(Ty, NumPadBytes);
|
2009-08-23 03:54:44 +04:00
|
|
|
|
2010-04-17 00:56:35 +04:00
|
|
|
Elts.push_back(llvm::UndefValue::get(Ty));
|
2009-08-01 01:38:39 +04:00
|
|
|
Types.push_back(Ty);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-23 03:54:44 +04:00
|
|
|
llvm::StructType* STy =
|
|
|
|
llvm::StructType::get(C->getType()->getContext(), Types, false);
|
2009-08-01 01:38:39 +04:00
|
|
|
return llvm::ConstantStruct::get(STy, Elts);
|
2009-01-17 03:48:48 +03:00
|
|
|
}
|
2010-08-25 15:45:40 +04:00
|
|
|
case CK_NullToMemberPointer: {
|
2010-08-22 14:59:02 +04:00
|
|
|
const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
|
2010-08-23 05:21:21 +04:00
|
|
|
return CGM.getCXXABI().EmitNullMemberPointer(MPT);
|
2010-08-22 14:59:02 +04:00
|
|
|
}
|
2009-10-03 19:13:22 +04:00
|
|
|
|
2010-08-25 15:45:40 +04:00
|
|
|
case CK_BaseToDerivedMemberPointer: {
|
2010-08-22 14:59:02 +04:00
|
|
|
Expr *SubExpr = E->getSubExpr();
|
2010-08-22 08:16:24 +04:00
|
|
|
llvm::Constant *C =
|
|
|
|
CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF);
|
|
|
|
if (!C) return 0;
|
|
|
|
|
2010-08-23 05:21:21 +04:00
|
|
|
return CGM.getCXXABI().EmitMemberPointerConversion(C, E);
|
2009-10-03 19:13:22 +04:00
|
|
|
}
|
2009-10-19 00:31:03 +04:00
|
|
|
|
2010-08-25 15:45:40 +04:00
|
|
|
case CK_BitCast:
|
2009-10-19 00:31:03 +04:00
|
|
|
// This must be a member function pointer cast.
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
|
2009-08-23 03:54:44 +04:00
|
|
|
default: {
|
|
|
|
// FIXME: This should be handled by the CK_NoOp cast kind.
|
|
|
|
// Explicit and implicit no-op casts
|
|
|
|
QualType Ty = E->getType(), SubTy = E->getSubExpr()->getType();
|
|
|
|
if (CGM.getContext().hasSameUnqualifiedType(Ty, SubTy))
|
2009-10-14 02:12:09 +04:00
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
|
|
|
|
// Handle integer->integer casts for address-of-label differences.
|
|
|
|
if (Ty->isIntegerType() && SubTy->isIntegerType() &&
|
|
|
|
CGF) {
|
|
|
|
llvm::Value *Src = Visit(E->getSubExpr());
|
|
|
|
if (Src == 0) return 0;
|
|
|
|
|
|
|
|
// Use EmitScalarConversion to perform the conversion.
|
|
|
|
return cast<llvm::Constant>(CGF->EmitScalarConversion(Src, SubTy, Ty));
|
|
|
|
}
|
|
|
|
|
2009-08-23 03:54:44 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2009-02-22 10:29:04 +03:00
|
|
|
}
|
2008-01-26 04:36:00 +03:00
|
|
|
}
|
2008-01-29 04:15:48 +03:00
|
|
|
|
2008-04-08 08:40:51 +04:00
|
|
|
llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
return Visit(DAE->getExpr());
|
|
|
|
}
|
|
|
|
|
2008-05-30 14:24:46 +04:00
|
|
|
llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
|
2008-02-21 20:45:41 +03:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
2010-04-18 23:06:43 +04:00
|
|
|
if (NumInitElements == 1 &&
|
2009-02-25 01:18:39 +03:00
|
|
|
(isa<StringLiteral>(ILE->getInit(0)) ||
|
2010-04-18 23:06:43 +04:00
|
|
|
isa<ObjCEncodeExpr>(ILE->getInit(0))))
|
2008-02-21 20:45:41 +03:00
|
|
|
return Visit(ILE->getInit(0));
|
2010-04-18 23:06:43 +04:00
|
|
|
|
|
|
|
std::vector<llvm::Constant*> Elts;
|
|
|
|
const llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(ConvertType(ILE->getType()));
|
2008-02-05 05:39:50 +03:00
|
|
|
const llvm::Type *ElemTy = AType->getElementType();
|
|
|
|
unsigned NumElements = AType->getNumElements();
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
// Initialising an array requires us to automatically
|
2008-02-05 05:39:50 +03:00
|
|
|
// initialise any elements that have not been initialised explicitly
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
|
|
|
|
|
|
|
// Copy initializer elements.
|
|
|
|
unsigned i = 0;
|
2008-05-30 23:58:50 +04:00
|
|
|
bool RewriteType = false;
|
2008-02-05 05:39:50 +03:00
|
|
|
for (; i < NumInitableElts; ++i) {
|
2009-04-08 08:48:15 +04:00
|
|
|
Expr *Init = ILE->getInit(i);
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
2009-02-17 21:43:32 +03:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
2008-05-30 23:58:50 +04:00
|
|
|
RewriteType |= (C->getType() != ElemTy);
|
2008-02-05 05:39:50 +03:00
|
|
|
Elts.push_back(C);
|
2008-01-26 04:36:00 +03:00
|
|
|
}
|
2008-05-30 23:58:50 +04:00
|
|
|
|
2008-02-05 05:39:50 +03:00
|
|
|
// Initialize remaining array elements.
|
2009-04-14 01:47:26 +04:00
|
|
|
// FIXME: This doesn't handle member pointers correctly!
|
2008-02-05 05:39:50 +03:00
|
|
|
for (; i < NumElements; ++i)
|
2009-08-01 00:28:54 +04:00
|
|
|
Elts.push_back(llvm::Constant::getNullValue(ElemTy));
|
2008-02-05 05:39:50 +03:00
|
|
|
|
2008-05-30 23:58:50 +04: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-09-09 19:08:12 +04:00
|
|
|
const llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
|
2009-08-06 03:18:46 +04:00
|
|
|
Types, true);
|
2009-07-28 02:29:56 +04:00
|
|
|
return llvm::ConstantStruct::get(SType, Elts);
|
2008-05-30 23:58:50 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
return llvm::ConstantArray::get(AType, Elts);
|
2008-02-05 05:39:50 +03:00
|
|
|
}
|
|
|
|
|
2008-05-30 14:24:46 +04:00
|
|
|
llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
|
2009-07-27 09:54:15 +04:00
|
|
|
return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
|
2008-02-05 05:39:50 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-05-30 14:24:46 +04:00
|
|
|
llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
|
2009-08-01 01:34:04 +04:00
|
|
|
return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
|
2008-05-30 14:24:46 +04:00
|
|
|
}
|
|
|
|
|
2009-01-30 09:13:25 +03:00
|
|
|
llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
|
2009-04-14 01:47:26 +04:00
|
|
|
return CGM.EmitNullConstant(E->getType());
|
2009-01-30 09:13:25 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-02-05 05:39:50 +03:00
|
|
|
llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
|
2008-05-30 14:24:46 +04:00
|
|
|
if (ILE->getType()->isScalarType()) {
|
|
|
|
// We have a scalar in braces. Just use the first element.
|
2009-04-08 08:48:15 +04:00
|
|
|
if (ILE->getNumInits() > 0) {
|
|
|
|
Expr *Init = ILE->getInit(0);
|
|
|
|
return CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
|
|
|
}
|
2009-04-14 01:47:26 +04:00
|
|
|
return CGM.EmitNullConstant(ILE->getType());
|
2008-01-30 02:23:18 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-05-30 14:24:46 +04:00
|
|
|
if (ILE->getType()->isArrayType())
|
|
|
|
return EmitArrayInitialization(ILE);
|
|
|
|
|
2009-11-22 02:56:04 +03:00
|
|
|
if (ILE->getType()->isRecordType())
|
2008-05-30 14:24:46 +04:00
|
|
|
return EmitStructInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isUnionType())
|
|
|
|
return EmitUnionInitialization(ILE);
|
|
|
|
|
2010-01-03 02:43:59 +03:00
|
|
|
// If ILE was a constant vector, we would have handled it already.
|
2008-05-30 14:24:46 +04:00
|
|
|
if (ILE->getType()->isVectorType())
|
2010-01-03 02:43:59 +03:00
|
|
|
return 0;
|
2008-01-30 02:23:18 +03:00
|
|
|
|
2008-02-05 05:39:50 +03:00
|
|
|
assert(0 && "Unable to handle InitListExpr");
|
2008-03-01 11:45:05 +03:00
|
|
|
// Get rid of control reaches end of void function warning.
|
|
|
|
// Not reached.
|
|
|
|
return 0;
|
2008-01-26 04:36:00 +03:00
|
|
|
}
|
2008-02-21 20:57:49 +03:00
|
|
|
|
2010-02-02 11:02:49 +03:00
|
|
|
llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) {
|
|
|
|
if (!E->getConstructor()->isTrivial())
|
|
|
|
return 0;
|
|
|
|
|
2010-02-05 21:38:45 +03: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 11:02:49 +03:00
|
|
|
// Only copy and default constructors can be trivial.
|
|
|
|
|
|
|
|
|
|
|
|
if (E->getNumArgs()) {
|
|
|
|
assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
|
|
|
|
assert(E->getConstructor()->isCopyConstructor() &&
|
|
|
|
"trivial ctor has argument but isn't a copy ctor");
|
|
|
|
|
|
|
|
Expr *Arg = E->getArg(0);
|
|
|
|
assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
|
|
|
|
"argument to copy ctor is of wrong type");
|
|
|
|
|
2010-02-02 15:15:55 +03:00
|
|
|
return Visit(Arg);
|
2010-02-02 11:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return CGM.EmitNullConstant(Ty);
|
|
|
|
}
|
|
|
|
|
2008-01-26 04:36:00 +03:00
|
|
|
llvm::Constant *VisitStringLiteral(StringLiteral *E) {
|
2008-02-11 03:02:17 +03:00
|
|
|
assert(!E->getType()->isPointerType() && "Strings are always arrays");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-25 01:18:39 +03: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-08-14 01:57:51 +04:00
|
|
|
return llvm::ConstantArray::get(VMContext,
|
|
|
|
CGM.GetStringForStringLiteral(E), false);
|
2008-01-26 04:36:00 +03:00
|
|
|
}
|
|
|
|
|
2009-02-25 01:18:39 +03: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 19:08:12 +04:00
|
|
|
|
2009-02-25 01:18:39 +03:00
|
|
|
// Resize the string to the right size, adding zeros at the end, or
|
|
|
|
// truncating as needed.
|
|
|
|
Str.resize(CAT->getSize().getZExtValue(), '\0');
|
2009-08-14 01:57:51 +04:00
|
|
|
return llvm::ConstantArray::get(VMContext, Str, false);
|
2009-02-25 01:18:39 +03:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-05-29 15:22:45 +04:00
|
|
|
llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
2009-02-20 01:01:56 +03:00
|
|
|
|
2008-01-26 04:36:00 +03:00
|
|
|
// Utility methods
|
|
|
|
const llvm::Type *ConvertType(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertType(T);
|
|
|
|
}
|
2008-01-26 07:30:23 +03:00
|
|
|
|
2008-11-16 09:23:45 +03:00
|
|
|
public:
|
2008-01-26 07:30:23 +03:00
|
|
|
llvm::Constant *EmitLValue(Expr *E) {
|
2008-01-26 05:08:50 +03:00
|
|
|
switch (E->getStmtClass()) {
|
2008-02-11 03:23:10 +03:00
|
|
|
default: break;
|
2008-01-26 05:08:50 +03: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 07:30:23 +03:00
|
|
|
CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
|
|
|
|
llvm::Constant* C = Visit(CLE->getInitializer());
|
2009-02-17 21:43:32 +03:00
|
|
|
// FIXME: "Leaked" on failure.
|
|
|
|
if (C)
|
2009-07-08 23:05:04 +04:00
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
|
2009-08-27 00:01:39 +04:00
|
|
|
E->getType().isConstant(CGM.getContext()),
|
2009-02-17 21:43:32 +03:00
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2009-08-27 00:01:39 +04:00
|
|
|
C, ".compoundliteral", 0, false,
|
|
|
|
E->getType().getAddressSpace());
|
2008-01-26 05:08:50 +03:00
|
|
|
return C;
|
2008-01-26 07:30:23 +03:00
|
|
|
}
|
2009-10-23 22:54:35 +04:00
|
|
|
case Expr::DeclRefExprClass: {
|
2010-03-05 00:26:03 +03:00
|
|
|
ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
|
|
|
|
if (Decl->hasAttr<WeakRefAttr>())
|
2010-08-22 05:00:03 +04:00
|
|
|
return CGM.GetWeakRefReference(Decl);
|
2008-01-26 05:08:50 +03:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
|
2009-09-11 03:43:36 +04:00
|
|
|
return CGM.GetAddrOfFunction(FD);
|
2008-04-16 02:42:06 +04:00
|
|
|
if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
|
2009-02-24 21:41:57 +03:00
|
|
|
// We can never refer to a variable with local storage.
|
2009-09-09 19:08:12 +04:00
|
|
|
if (!VD->hasLocalStorage()) {
|
2009-02-24 21:41:57 +03:00
|
|
|
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 02:42:06 +04:00
|
|
|
}
|
2008-02-27 00:41:45 +03:00
|
|
|
}
|
2008-02-11 03:23:10 +03:00
|
|
|
break;
|
2008-01-26 05:08:50 +03:00
|
|
|
}
|
2008-08-14 03:20:05 +04:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
|
2009-02-25 01:18:39 +03:00
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
|
2008-11-16 09:23:45 +03:00
|
|
|
case Expr::ObjCStringLiteralClass: {
|
|
|
|
ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
|
2010-01-23 05:40:42 +03:00
|
|
|
llvm::Constant *C =
|
|
|
|
CGM.getObjCRuntime().GenerateConstantString(SL->getString());
|
2009-07-29 22:54:39 +04:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
|
2008-11-16 09:23:45 +03:00
|
|
|
}
|
2008-12-12 08:18:02 +03:00
|
|
|
case Expr::PredefinedExprClass: {
|
2009-11-14 11:37:13 +03:00
|
|
|
unsigned Type = cast<PredefinedExpr>(E)->getIdentType();
|
|
|
|
if (CGF) {
|
2010-08-21 07:01:12 +04:00
|
|
|
LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E));
|
2009-11-14 11:37:13 +03:00
|
|
|
return cast<llvm::Constant>(Res.getAddress());
|
|
|
|
} else if (Type == PredefinedExpr::PrettyFunction) {
|
|
|
|
return CGM.GetAddrOfConstantCString("top level", ".tmp");
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-14 11:37:13 +03:00
|
|
|
return CGM.GetAddrOfConstantCString("", ".tmp");
|
2008-12-12 08:18:02 +03:00
|
|
|
}
|
2009-01-25 04:21:06 +03:00
|
|
|
case Expr::AddrLabelExprClass: {
|
|
|
|
assert(CGF && "Invalid address of label expression outside function.");
|
2009-10-29 02:59:40 +03:00
|
|
|
llvm::Constant *Ptr =
|
|
|
|
CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
|
|
|
|
return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType()));
|
2009-01-25 04:21:06 +03:00
|
|
|
}
|
2009-01-25 04:54:01 +03:00
|
|
|
case Expr::CallExprClass: {
|
|
|
|
CallExpr* CE = cast<CallExpr>(E);
|
2010-01-23 05:40:42 +03:00
|
|
|
unsigned builtin = CE->isBuiltinCall(CGM.getContext());
|
|
|
|
if (builtin !=
|
|
|
|
Builtin::BI__builtin___CFStringMakeConstantString &&
|
|
|
|
builtin !=
|
|
|
|
Builtin::BI__builtin___NSStringMakeConstantString)
|
2009-01-25 04:54:01 +03:00
|
|
|
break;
|
|
|
|
const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
|
|
|
|
const StringLiteral *Literal = cast<StringLiteral>(Arg);
|
2010-01-23 05:40:42 +03:00
|
|
|
if (builtin ==
|
|
|
|
Builtin::BI__builtin___NSStringMakeConstantString) {
|
|
|
|
return CGM.getObjCRuntime().GenerateConstantString(Literal);
|
|
|
|
}
|
2009-03-31 20:53:37 +04:00
|
|
|
// FIXME: need to deal with UCN conversion issues.
|
2009-04-01 17:55:36 +04:00
|
|
|
return CGM.GetAddrOfConstantCFString(Literal);
|
2009-01-25 04:54:01 +03:00
|
|
|
}
|
2009-02-19 04:01:04 +03:00
|
|
|
case Expr::BlockExprClass: {
|
2009-03-01 04:09:12 +03: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 04:01:04 +03:00
|
|
|
}
|
2008-02-11 03:23:10 +03:00
|
|
|
}
|
2009-02-17 21:43:32 +03:00
|
|
|
|
|
|
|
return 0;
|
2008-01-26 05:08:50 +03:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-01-26 04:36:00 +03:00
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2008-02-27 00:41:45 +03:00
|
|
|
llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
|
2009-04-08 08:48:15 +04:00
|
|
|
QualType DestType,
|
2008-07-27 02:37:01 +04:00
|
|
|
CodeGenFunction *CGF) {
|
2008-12-01 05:42:14 +03:00
|
|
|
Expr::EvalResult Result;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-11 05:08:03 +04:00
|
|
|
bool Success = false;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-27 10:04:58 +04:00
|
|
|
if (DestType->isReferenceType())
|
|
|
|
Success = E->EvaluateAsLValue(Result, Context);
|
2009-09-09 19:08:12 +04:00
|
|
|
else
|
2009-04-11 05:08:03 +04:00
|
|
|
Success = E->Evaluate(Result, Context);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-14 11:51:33 +03:00
|
|
|
if (Success && !Result.HasSideEffects) {
|
2008-12-01 05:42:14 +03:00
|
|
|
switch (Result.Val.getKind()) {
|
2009-01-29 01:24:07 +03:00
|
|
|
case APValue::Uninitialized:
|
2009-02-17 21:43:32 +03:00
|
|
|
assert(0 && "Constant expressions should be initialized.");
|
|
|
|
return 0;
|
2008-11-15 23:45:50 +03:00
|
|
|
case APValue::LValue: {
|
2009-04-11 05:08:03 +04:00
|
|
|
const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *Offset =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
|
2010-01-15 15:37:54 +03:00
|
|
|
Result.Val.getLValueOffset().getQuantity());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-20 00:44:24 +03:00
|
|
|
llvm::Constant *C;
|
2008-12-01 05:42:14 +03:00
|
|
|
if (const Expr *LVBase = Result.Val.getLValueBase()) {
|
2009-02-20 00:44:24 +03:00
|
|
|
C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase));
|
|
|
|
|
|
|
|
// Apply offset if necessary.
|
|
|
|
if (!Offset->isNullValue()) {
|
2009-10-13 14:07:13 +04:00
|
|
|
const llvm::Type *Type = llvm::Type::getInt8PtrTy(VMContext);
|
2009-07-29 22:54:39 +04:00
|
|
|
llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type);
|
|
|
|
Casted = llvm::ConstantExpr::getGetElementPtr(Casted, &Offset, 1);
|
|
|
|
C = llvm::ConstantExpr::getBitCast(Casted, C->getType());
|
2009-02-20 00:44:24 +03:00
|
|
|
}
|
2008-11-16 09:23:45 +03:00
|
|
|
|
2009-02-20 00:44:24 +03:00
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 08:59:59 +04:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-29 22:54:39 +04:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, DestTy);
|
2009-02-20 00:44:24 +03:00
|
|
|
|
2009-07-29 22:54:39 +04:00
|
|
|
return llvm::ConstantExpr::getPtrToInt(C, DestTy);
|
2009-02-20 00:44:24 +03:00
|
|
|
} else {
|
|
|
|
C = Offset;
|
|
|
|
|
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 08:59:59 +04:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-29 22:54:39 +04:00
|
|
|
return llvm::ConstantExpr::getIntToPtr(C, DestTy);
|
2009-02-20 00:44:24 +03:00
|
|
|
|
|
|
|
// If the types don't match this should only be a truncate.
|
2009-04-10 08:59:59 +04:00
|
|
|
if (C->getType() != DestTy)
|
2009-07-29 22:54:39 +04:00
|
|
|
return llvm::ConstantExpr::getTrunc(C, DestTy);
|
2009-02-20 00:44:24 +03:00
|
|
|
|
|
|
|
return C;
|
2008-11-16 09:23:45 +03:00
|
|
|
}
|
2008-11-15 23:45:50 +03:00
|
|
|
}
|
2008-11-17 06:57:28 +03:00
|
|
|
case APValue::Int: {
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *C = llvm::ConstantInt::get(VMContext,
|
2009-07-25 03:12:58 +04:00
|
|
|
Result.Val.getInt());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-06-27 01:52:32 +04:00
|
|
|
if (C->getType()->isIntegerTy(1)) {
|
2008-11-15 23:45:50 +03:00
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-29 22:54:39 +04:00
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
2008-11-15 23:45:50 +03:00
|
|
|
}
|
|
|
|
return C;
|
2008-11-17 06:57:28 +03:00
|
|
|
}
|
2009-01-29 01:24:07 +03:00
|
|
|
case APValue::ComplexInt: {
|
|
|
|
llvm::Constant *Complex[2];
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-07-25 03:12:58 +04:00
|
|
|
Complex[0] = llvm::ConstantInt::get(VMContext,
|
|
|
|
Result.Val.getComplexIntReal());
|
2009-09-09 19:08:12 +04:00
|
|
|
Complex[1] = llvm::ConstantInt::get(VMContext,
|
2009-07-25 03:12:58 +04:00
|
|
|
Result.Val.getComplexIntImag());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-20 00:00:52 +04:00
|
|
|
// FIXME: the target may want to specify that this is packed.
|
|
|
|
return llvm::ConstantStruct::get(VMContext, Complex, 2, false);
|
2009-01-29 01:24:07 +03:00
|
|
|
}
|
2008-11-15 23:45:50 +03:00
|
|
|
case APValue::Float:
|
2009-07-28 01:00:51 +04:00
|
|
|
return llvm::ConstantFP::get(VMContext, Result.Val.getFloat());
|
2008-11-17 04:58:55 +03:00
|
|
|
case APValue::ComplexFloat: {
|
|
|
|
llvm::Constant *Complex[2];
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
Complex[0] = llvm::ConstantFP::get(VMContext,
|
2009-07-28 01:00:51 +04:00
|
|
|
Result.Val.getComplexFloatReal());
|
|
|
|
Complex[1] = llvm::ConstantFP::get(VMContext,
|
|
|
|
Result.Val.getComplexFloatImag());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-20 00:00:52 +04:00
|
|
|
// FIXME: the target may want to specify that this is packed.
|
|
|
|
return llvm::ConstantStruct::get(VMContext, Complex, 2, false);
|
2008-11-17 04:58:55 +03:00
|
|
|
}
|
2009-01-18 04:01:34 +03:00
|
|
|
case APValue::Vector: {
|
|
|
|
llvm::SmallVector<llvm::Constant *, 4> Inits;
|
|
|
|
unsigned NumElts = Result.Val.getVectorLength();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-18 04:01:34 +03:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
APValue &Elt = Result.Val.getVectorElt(i);
|
|
|
|
if (Elt.isInt())
|
2009-07-25 03:12:58 +04:00
|
|
|
Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
|
2009-01-18 04:01:34 +03:00
|
|
|
else
|
2009-07-28 01:00:51 +04:00
|
|
|
Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
|
2009-01-18 04:01:34 +03:00
|
|
|
}
|
2009-07-29 01:22:35 +04:00
|
|
|
return llvm::ConstantVector::get(&Inits[0], Inits.size());
|
2009-01-18 04:01:34 +03:00
|
|
|
}
|
2008-11-15 23:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
2008-06-01 19:31:44 +04:00
|
|
|
|
|
|
|
llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
|
2010-06-27 01:52:32 +04:00
|
|
|
if (C && C->getType()->isIntegerTy(1)) {
|
2008-06-01 19:31:44 +04:00
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-29 22:54:39 +04:00
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
2008-06-01 19:31:44 +04:00
|
|
|
}
|
|
|
|
return C;
|
2008-01-26 04:36:00 +03:00
|
|
|
}
|
2009-04-14 01:47:26 +04:00
|
|
|
|
2010-05-18 20:51:41 +04:00
|
|
|
static void
|
|
|
|
FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T,
|
|
|
|
std::vector<llvm::Constant *> &Elements,
|
|
|
|
uint64_t StartOffset) {
|
|
|
|
assert(StartOffset % 8 == 0 && "StartOffset not byte aligned!");
|
|
|
|
|
2010-08-23 01:01:12 +04:00
|
|
|
if (CGM.getTypes().isZeroInitializable(T))
|
2010-05-18 20:51:41 +04: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-27 22:51:01 +04:00
|
|
|
if (I->isVirtual()) {
|
|
|
|
// FIXME: We should initialize null pointer to data members in virtual
|
|
|
|
// bases here.
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-18 20:51:41 +04: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 01:01:12 +04:00
|
|
|
if (CGM.getTypes().isZeroInitializable(BaseDecl))
|
2010-05-18 20:51:41 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
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 01:01:12 +04:00
|
|
|
if (CGM.getTypes().isZeroInitializable(FieldType))
|
2010-05-18 20:51:41 +04: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!");
|
|
|
|
|
|
|
|
uint64_t StartIndex = StartOffset / 8;
|
|
|
|
uint64_t EndIndex = StartIndex + CGM.getContext().getTypeSize(T) / 8;
|
|
|
|
|
|
|
|
llvm::Constant *NegativeOne =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt8Ty(CGM.getLLVMContext()),
|
|
|
|
-1ULL, /*isSigned=*/true);
|
|
|
|
|
|
|
|
// Fill in the null data member pointer.
|
|
|
|
for (uint64_t I = StartIndex; I != EndIndex; ++I)
|
|
|
|
Elements[I] = NegativeOne;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-14 01:47:26 +04:00
|
|
|
llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
|
2010-08-23 01:01:12 +04:00
|
|
|
if (getTypes().isZeroInitializable(T))
|
2009-08-24 21:16:23 +04:00
|
|
|
return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
|
2010-02-02 08:17:25 +03:00
|
|
|
|
2009-08-09 22:26:27 +04:00
|
|
|
if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-08-09 22:26:27 +04:00
|
|
|
QualType ElementTy = CAT->getElementType();
|
|
|
|
|
2010-02-02 08:17:25 +03:00
|
|
|
llvm::Constant *Element = EmitNullConstant(ElementTy);
|
|
|
|
unsigned NumElements = CAT->getSize().getZExtValue();
|
|
|
|
std::vector<llvm::Constant *> Array(NumElements);
|
|
|
|
for (unsigned i = 0; i != NumElements; ++i)
|
|
|
|
Array[i] = Element;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-02-02 08:17:25 +03:00
|
|
|
const llvm::ArrayType *ATy =
|
|
|
|
cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
|
|
|
|
return llvm::ConstantArray::get(ATy, Array);
|
2009-08-09 22:26:27 +04:00
|
|
|
}
|
2009-08-23 05:25:01 +04:00
|
|
|
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
2010-02-02 08:17:25 +03:00
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
|
|
|
|
const llvm::StructType *STy =
|
|
|
|
cast<llvm::StructType>(getTypes().ConvertTypeForMem(T));
|
|
|
|
unsigned NumElements = STy->getNumElements();
|
|
|
|
std::vector<llvm::Constant *> Elements(NumElements);
|
|
|
|
|
2010-05-18 20:51:41 +04:00
|
|
|
const CGRecordLayout &Layout = getTypes().getCGRecordLayout(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-27 22:51:01 +04:00
|
|
|
if (I->isVirtual()) {
|
|
|
|
// FIXME: We should initialize null pointer to data members in virtual
|
|
|
|
// bases here.
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-18 20:51:41 +04: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 01:01:12 +04:00
|
|
|
if (getTypes().isZeroInitializable(BaseDecl))
|
2010-05-18 20:51:41 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Currently, all bases are arrays of i8. Figure out how many elements
|
|
|
|
// this base array has.
|
|
|
|
unsigned BaseFieldNo = Layout.getNonVirtualBaseLLVMFieldNo(BaseDecl);
|
|
|
|
const llvm::ArrayType *BaseArrayTy =
|
|
|
|
cast<llvm::ArrayType>(STy->getElementType(BaseFieldNo));
|
|
|
|
|
|
|
|
unsigned NumBaseElements = BaseArrayTy->getNumElements();
|
|
|
|
std::vector<llvm::Constant *> BaseElements(NumBaseElements);
|
|
|
|
|
|
|
|
// Now fill in null data member pointers.
|
|
|
|
FillInNullDataMemberPointers(*this, I->getType(), BaseElements, 0);
|
|
|
|
|
|
|
|
// Now go through all other elements and zero them out.
|
|
|
|
if (NumBaseElements) {
|
|
|
|
llvm::Constant *Zero =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt8Ty(getLLVMContext()), 0);
|
|
|
|
|
|
|
|
for (unsigned I = 0; I != NumBaseElements; ++I) {
|
|
|
|
if (!BaseElements[I])
|
|
|
|
BaseElements[I] = Zero;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Elements[BaseFieldNo] = llvm::ConstantArray::get(BaseArrayTy,
|
|
|
|
BaseElements);
|
|
|
|
}
|
|
|
|
|
2010-02-02 08:17:25 +03:00
|
|
|
for (RecordDecl::field_iterator I = RD->field_begin(),
|
|
|
|
E = RD->field_end(); I != E; ++I) {
|
|
|
|
const FieldDecl *FD = *I;
|
2010-06-03 19:36:07 +04:00
|
|
|
|
|
|
|
// Ignore bit fields.
|
|
|
|
if (FD->isBitField())
|
|
|
|
continue;
|
|
|
|
|
2010-05-18 20:51:41 +04:00
|
|
|
unsigned FieldNo = Layout.getLLVMFieldNo(FD);
|
2010-02-02 08:17:25 +03:00
|
|
|
Elements[FieldNo] = EmitNullConstant(FD->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through all other fields and zero them out.
|
|
|
|
for (unsigned i = 0; i != NumElements; ++i) {
|
|
|
|
if (!Elements[i])
|
|
|
|
Elements[i] = llvm::Constant::getNullValue(STy->getElementType(i));
|
2009-08-23 05:25:01 +04:00
|
|
|
}
|
2010-02-02 08:17:25 +03:00
|
|
|
|
|
|
|
return llvm::ConstantStruct::get(STy, Elements);
|
2009-08-23 05:25:01 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-05-18 20:51:41 +04:00
|
|
|
assert(T->isMemberPointerType() && "Should only see member pointers here!");
|
2010-02-02 08:17:25 +03: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.
|
2010-02-11 00:41:43 +03:00
|
|
|
return llvm::ConstantInt::get(getTypes().ConvertTypeForMem(T), -1ULL,
|
2010-02-02 08:17:25 +03:00
|
|
|
/*isSigned=*/true);
|
2009-04-14 01:47:26 +04:00
|
|
|
}
|