2008-01-26 09:36:00 +08:00
|
|
|
//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Constant Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2008-10-06 13:59:01 +08:00
|
|
|
#include "clang/AST/APValue.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-07-24 23:20:52 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-05-30 18:24:46 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-01-26 09:36:00 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
namespace {
|
2009-07-24 23:20:52 +08:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN ConstStructBuilder {
|
|
|
|
CodeGenModule &CGM;
|
|
|
|
CodeGenFunction *CGF;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Packed;
|
2009-07-25 08:24:56 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned NextFieldOffsetInBytes;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
std::vector<llvm::Constant *> Elements;
|
|
|
|
|
|
|
|
ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
|
|
|
|
: CGM(CGM), CGF(CGF), Packed(false), NextFieldOffsetInBytes(0) { }
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
|
2009-07-24 23:20:52 +08:00
|
|
|
const Expr *InitExpr) {
|
|
|
|
uint64_t FieldOffsetInBytes = FieldOffset / 8;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
assert(NextFieldOffsetInBytes <= FieldOffsetInBytes
|
2009-07-24 23:20:52 +08:00
|
|
|
&& "Field offset mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Emit the field.
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(InitExpr, Field->getType(), CGF);
|
|
|
|
if (!C)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned FieldAlignment = getAlignment(C);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Round up the field offset to the alignment of the field type.
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t AlignedNextFieldOffsetInBytes =
|
2009-07-24 23:20:52 +08:00
|
|
|
llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
if (AlignedNextFieldOffsetInBytes > FieldOffsetInBytes) {
|
2009-07-25 09:40:29 +08:00
|
|
|
std::vector<llvm::Constant *> PackedElements;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 09:40:29 +08:00
|
|
|
assert(!Packed && "Alignment is wrong even with a packed struct!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 09:40:29 +08:00
|
|
|
// Convert the struct to a packed struct.
|
|
|
|
uint64_t ElementOffsetInBytes = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 09:40:29 +08:00
|
|
|
for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
|
|
|
|
llvm::Constant *C = Elements[i];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
unsigned ElementAlign =
|
2009-07-25 09:40:29 +08:00
|
|
|
CGM.getTargetData().getABITypeAlignment(C->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t AlignedElementOffsetInBytes =
|
2009-07-25 09:40:29 +08:00
|
|
|
llvm::RoundUpToAlignment(ElementOffsetInBytes, ElementAlign);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 09:40:29 +08:00
|
|
|
if (AlignedElementOffsetInBytes > ElementOffsetInBytes) {
|
|
|
|
// We need some padding.
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t NumBytes =
|
2009-07-25 09:40:29 +08:00
|
|
|
AlignedElementOffsetInBytes - ElementOffsetInBytes;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(CGF->getLLVMContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
if (NumBytes > 1)
|
2009-07-30 06:16:19 +08:00
|
|
|
Ty = llvm::ArrayType::get(Ty, NumBytes);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-01 04:28:54 +08:00
|
|
|
llvm::Constant *Padding = llvm::Constant::getNullValue(Ty);
|
2009-07-25 09:40:29 +08:00
|
|
|
PackedElements.push_back(Padding);
|
|
|
|
ElementOffsetInBytes += getSizeInBytes(Padding);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 09:40:29 +08:00
|
|
|
PackedElements.push_back(C);
|
|
|
|
ElementOffsetInBytes += getSizeInBytes(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ElementOffsetInBytes == NextFieldOffsetInBytes &&
|
|
|
|
"Packing the struct changed its size!");
|
|
|
|
|
|
|
|
Elements = PackedElements;
|
|
|
|
Packed = true;
|
|
|
|
AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes;
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
|
|
|
|
// We need to append padding.
|
2009-07-25 09:40:29 +08:00
|
|
|
AppendPadding(FieldOffsetInBytes - NextFieldOffsetInBytes);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
assert(NextFieldOffsetInBytes == FieldOffsetInBytes &&
|
|
|
|
"Did not add enough padding!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
AlignedNextFieldOffsetInBytes = NextFieldOffsetInBytes;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Add the field.
|
|
|
|
Elements.push_back(C);
|
|
|
|
NextFieldOffsetInBytes = AlignedNextFieldOffsetInBytes + getSizeInBytes(C);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
|
2009-07-27 09:23:51 +08:00
|
|
|
const Expr *InitExpr) {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::ConstantInt *CI =
|
|
|
|
cast_or_null<llvm::ConstantInt>(CGM.EmitConstantExpr(InitExpr,
|
|
|
|
Field->getType(),
|
2009-07-27 09:23:51 +08:00
|
|
|
CGF));
|
|
|
|
// FIXME: Can this ever happen?
|
|
|
|
if (!CI)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
if (FieldOffset > NextFieldOffsetInBytes * 8) {
|
2009-07-27 10:56:37 +08:00
|
|
|
// We need to add padding.
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t NumBytes =
|
|
|
|
llvm::RoundUpToAlignment(FieldOffset -
|
2009-07-27 10:56:37 +08:00
|
|
|
NextFieldOffsetInBytes * 8, 8) / 8;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
AppendPadding(NumBytes);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
uint64_t FieldSize =
|
2009-07-27 09:23:51 +08:00
|
|
|
Field->getBitWidth()->EvaluateAsInt(CGM.getContext()).getZExtValue();
|
|
|
|
|
|
|
|
llvm::APInt FieldValue = CI->getValue();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +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())
|
|
|
|
FieldValue.zext(FieldSize);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
// Truncate the size of FieldValue to the bit field size.
|
|
|
|
if (FieldSize < FieldValue.getBitWidth())
|
|
|
|
FieldValue.trunc(FieldSize);
|
|
|
|
|
|
|
|
if (FieldOffset < NextFieldOffsetInBytes * 8) {
|
2009-07-27 10:56:37 +08:00
|
|
|
// Either part of the field or the entire field can go into the previous
|
|
|
|
// byte.
|
|
|
|
assert(!Elements.empty() && "Elements can't be empty!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
unsigned BitsInPreviousByte =
|
2009-07-27 10:56:37 +08:00
|
|
|
NextFieldOffsetInBytes * 8 - FieldOffset;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
bool FitsCompletelyInPreviousByte =
|
2009-07-27 10:56:37 +08:00
|
|
|
BitsInPreviousByte >= FieldValue.getBitWidth();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
llvm::APInt Tmp = FieldValue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
if (!FitsCompletelyInPreviousByte) {
|
|
|
|
unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
Tmp = Tmp.lshr(NewFieldWidth);
|
|
|
|
Tmp.trunc(BitsInPreviousByte);
|
|
|
|
|
|
|
|
// We want the remaining high bits.
|
|
|
|
FieldValue.trunc(NewFieldWidth);
|
|
|
|
} else {
|
|
|
|
Tmp.trunc(BitsInPreviousByte);
|
|
|
|
|
|
|
|
// We want the remaining low bits.
|
|
|
|
FieldValue = FieldValue.lshr(BitsInPreviousByte);
|
|
|
|
FieldValue.trunc(NewFieldWidth);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
Tmp.zext(8);
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
if (FitsCompletelyInPreviousByte)
|
|
|
|
Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
|
|
|
|
} else {
|
|
|
|
Tmp = Tmp.shl(8 - BitsInPreviousByte);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Or in the bits that go into the previous byte.
|
|
|
|
Tmp |= cast<llvm::ConstantInt>(Elements.back())->getValue();
|
|
|
|
Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
if (FitsCompletelyInPreviousByte)
|
|
|
|
return true;
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
while (FieldValue.getBitWidth() > 8) {
|
|
|
|
llvm::APInt Tmp;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
// We want the high bits.
|
2009-07-27 10:56:37 +08:00
|
|
|
Tmp = FieldValue;
|
|
|
|
Tmp = Tmp.lshr(Tmp.getBitWidth() - 8);
|
|
|
|
Tmp.trunc(8);
|
|
|
|
} else {
|
|
|
|
// We want the low bits.
|
|
|
|
Tmp = FieldValue;
|
|
|
|
Tmp.trunc(8);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 10:56:37 +08:00
|
|
|
FieldValue = FieldValue.lshr(8);
|
2009-07-27 09:23:51 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
|
|
|
|
NextFieldOffsetInBytes++;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
FieldValue.trunc(FieldValue.getBitWidth() - 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(FieldValue.getBitWidth() > 0 &&
|
|
|
|
"Should have at least one bit left!");
|
|
|
|
assert(FieldValue.getBitWidth() <= 8 &&
|
|
|
|
"Should not have more than a byte left!");
|
|
|
|
|
|
|
|
if (FieldValue.getBitWidth() < 8) {
|
|
|
|
if (CGM.getTargetData().isBigEndian()) {
|
|
|
|
unsigned BitWidth = FieldValue.getBitWidth();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
FieldValue.zext(8);
|
|
|
|
FieldValue = FieldValue << (8 - BitWidth);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else
|
2009-07-27 09:23:51 +08:00
|
|
|
FieldValue.zext(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the last element.
|
|
|
|
Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
|
|
|
|
FieldValue));
|
|
|
|
NextFieldOffsetInBytes++;
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
void AppendPadding(uint64_t NumBytes) {
|
|
|
|
if (!NumBytes)
|
|
|
|
return;
|
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(CGM.getLLVMContext());
|
2009-09-09 23:08:12 +08:00
|
|
|
if (NumBytes > 1)
|
2009-07-30 06:16:19 +08:00
|
|
|
Ty = llvm::ArrayType::get(Ty, NumBytes);
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2009-08-01 04:28:54 +08:00
|
|
|
llvm::Constant *C = llvm::Constant::getNullValue(Ty);
|
2009-07-24 23:20:52 +08:00
|
|
|
Elements.push_back(C);
|
|
|
|
assert(getAlignment(C) == 1 && "Padding must have 1 byte alignment!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
NextFieldOffsetInBytes += getSizeInBytes(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppendTailPadding(uint64_t RecordSize) {
|
|
|
|
assert(RecordSize % 8 == 0 && "Invalid record size!");
|
|
|
|
|
|
|
|
uint64_t RecordSizeInBytes = RecordSize / 8;
|
|
|
|
assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
|
|
|
|
AppendPadding(NumPadBytes);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-02 07:11:24 +08:00
|
|
|
bool Build(InitListExpr *ILE) {
|
2009-07-30 05:53:49 +08:00
|
|
|
RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
|
2009-07-24 23:20:52 +08:00
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned FieldNo = 0;
|
|
|
|
unsigned ElementNo = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2009-07-24 23:20:52 +08:00
|
|
|
ElementNo < ILE->getNumInits() && Field != FieldEnd;
|
|
|
|
++Field, ++FieldNo) {
|
2009-08-02 07:11:24 +08:00
|
|
|
if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
|
|
|
|
continue;
|
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
if (Field->isBitField()) {
|
|
|
|
if (!Field->getIdentifier())
|
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-27 09:23:51 +08:00
|
|
|
if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
|
|
|
|
ILE->getInit(ElementNo)))
|
|
|
|
return false;
|
2009-07-24 23:20:52 +08:00
|
|
|
} else {
|
|
|
|
if (!AppendField(*Field, Layout.getFieldOffset(FieldNo),
|
|
|
|
ILE->getInit(ElementNo)))
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
ElementNo++;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
uint64_t LayoutSizeInBytes = Layout.getSize() / 8;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 08:24:56 +08: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!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
// No tail padding is necessary.
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
// Append tail padding if necessary.
|
|
|
|
AppendTailPadding(Layout.getSize());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
assert(Layout.getSize() / 8 == NextFieldOffsetInBytes &&
|
2009-07-24 23:20:52 +08:00
|
|
|
"Tail padding mismatch!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
unsigned getAlignment(const llvm::Constant *C) const {
|
|
|
|
if (Packed)
|
|
|
|
return 1;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
return CGM.getTargetData().getABITypeAlignment(C->getType());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
uint64_t getSizeInBytes(const llvm::Constant *C) const {
|
|
|
|
return CGM.getTargetData().getTypeAllocSize(C->getType());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
public:
|
|
|
|
static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
|
2009-08-02 07:11:24 +08:00
|
|
|
InitListExpr *ILE) {
|
2009-07-24 23:20:52 +08:00
|
|
|
ConstStructBuilder Builder(CGM, CGF);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-24 23:20:52 +08:00
|
|
|
if (!Builder.Build(ILE))
|
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
llvm::Constant *Result =
|
2009-08-06 09:24:27 +08:00
|
|
|
llvm::ConstantStruct::get(CGM.getLLVMContext(),
|
2009-08-06 07:18:46 +08:00
|
|
|
Builder.Elements, Builder.Packed);
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2009-07-25 08:24:56 +08:00
|
|
|
assert(llvm::RoundUpToAlignment(Builder.NextFieldOffsetInBytes,
|
2009-09-09 23:08:12 +08:00
|
|
|
Builder.getAlignment(Result)) ==
|
2009-07-25 08:24:56 +08:00
|
|
|
Builder.getSizeInBytes(Result) && "Size mismatch!");
|
2009-07-24 23:20:52 +08:00
|
|
|
|
2009-07-25 02:20:38 +08:00
|
|
|
return Result;
|
2009-07-24 23:20:52 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN 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
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
return Visit(E->getInitializer());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-19 07:01:59 +08:00
|
|
|
llvm::Constant *VisitCastExpr(CastExpr* E) {
|
2009-08-23 07:54:44 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
case CastExpr::CK_ToUnion: {
|
|
|
|
// GCC cast to union extension
|
|
|
|
assert(E->getType()->isUnionType() &&
|
|
|
|
"Destination type is not union type!");
|
2009-01-17 08:48:48 +08:00
|
|
|
const llvm::Type *Ty = ConvertType(E->getType());
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *SubExpr = E->getSubExpr();
|
2009-08-23 07:54:44 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *C =
|
2009-08-01 05:38:39 +08:00
|
|
|
CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF);
|
|
|
|
if (!C)
|
|
|
|
return 0;
|
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
|
|
|
|
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 23:08:12 +08:00
|
|
|
|
2009-08-01 05:38:39 +08:00
|
|
|
assert(CurSize <= TotalSize && "Union size mismatch!");
|
|
|
|
if (unsigned NumPadBytes = TotalSize - CurSize) {
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *Ty = llvm::Type::getInt8Ty(VMContext);
|
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
|
|
|
|
2009-08-01 05:38:39 +08:00
|
|
|
Elts.push_back(llvm::Constant::getNullValue(Ty));
|
|
|
|
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-08-23 08:02:11 +08:00
|
|
|
case CastExpr::CK_NullToMemberPointer:
|
|
|
|
return CGM.EmitNullConstant(E->getType());
|
2009-08-23 07:54:44 +08: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))
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-22 15:29:04 +08:00
|
|
|
}
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-01-29 09:15:48 +08:00
|
|
|
|
2008-04-08 12:40:51 +08:00
|
|
|
llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
return Visit(DAE->getExpr());
|
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
|
2008-02-22 01:45:41 +08:00
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-05-30 18:24:46 +08:00
|
|
|
const llvm::ArrayType *AType =
|
|
|
|
cast<llvm::ArrayType>(ConvertType(ILE->getType()));
|
2008-02-22 01:45:41 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
|
|
|
// FIXME: Check for wide strings
|
2009-02-25 06:18:39 +08:00
|
|
|
// FIXME: Check for NumInitElements exactly equal to 1??
|
2009-09-09 23:08:12 +08:00
|
|
|
if (NumInitElements > 0 &&
|
2009-02-25 06:18:39 +08:00
|
|
|
(isa<StringLiteral>(ILE->getInit(0)) ||
|
|
|
|
isa<ObjCEncodeExpr>(ILE->getInit(0))) &&
|
2008-08-04 15:31:14 +08:00
|
|
|
ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType())
|
2008-02-22 01:45:41 +08:00
|
|
|
return Visit(ILE->getInit(0));
|
2008-02-05 10:39:50 +08:00
|
|
|
const llvm::Type *ElemTy = AType->getElementType();
|
|
|
|
unsigned NumElements = AType->getNumElements();
|
|
|
|
|
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.
|
|
|
|
unsigned i = 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
bool RewriteType = false;
|
2008-02-05 10:39:50 +08:00
|
|
|
for (; i < NumInitableElts; ++i) {
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *Init = ILE->getInit(i);
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
2008-05-31 03:58:50 +08:00
|
|
|
RewriteType |= (C->getType() != ElemTy);
|
2008-02-05 10:39:50 +08:00
|
|
|
Elts.push_back(C);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-05-31 03:58:50 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
// Initialize remaining array elements.
|
2009-04-14 05:47:26 +08:00
|
|
|
// FIXME: This doesn't handle member pointers correctly!
|
2008-02-05 10:39:50 +08:00
|
|
|
for (; i < NumElements; ++i)
|
2009-08-01 04:28:54 +08:00
|
|
|
Elts.push_back(llvm::Constant::getNullValue(ElemTy));
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-31 03:58:50 +08:00
|
|
|
if (RewriteType) {
|
|
|
|
// FIXME: Try to avoid packing the array
|
|
|
|
std::vector<const llvm::Type*> Types;
|
|
|
|
for (unsigned i = 0; i < Elts.size(); ++i)
|
|
|
|
Types.push_back(Elts[i]->getType());
|
2009-09-09 23:08:12 +08:00
|
|
|
const 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
|
|
|
}
|
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
|
2009-07-27 13:54:15 +08:00
|
|
|
return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
|
2009-08-01 05:34:04 +08:00
|
|
|
return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
|
2008-05-30 18:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) {
|
|
|
|
const llvm::VectorType *VType =
|
|
|
|
cast<llvm::VectorType>(ConvertType(ILE->getType()));
|
2008-05-30 20:02:14 +08:00
|
|
|
const llvm::Type *ElemTy = VType->getElementType();
|
|
|
|
std::vector<llvm::Constant*> Elts;
|
2008-02-05 10:39:50 +08:00
|
|
|
unsigned NumElements = VType->getNumElements();
|
2008-05-30 20:02:14 +08:00
|
|
|
unsigned NumInitElements = ILE->getNumInits();
|
|
|
|
|
|
|
|
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
// Copy initializer elements.
|
|
|
|
unsigned i = 0;
|
2008-05-30 20:02:14 +08:00
|
|
|
for (; i < NumInitableElts; ++i) {
|
2009-04-08 12:48:15 +08:00
|
|
|
Expr *Init = ILE->getInit(i);
|
|
|
|
llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
2009-02-18 02:43:32 +08:00
|
|
|
if (!C)
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
Elts.push_back(C);
|
|
|
|
}
|
2008-02-05 10:39:50 +08:00
|
|
|
|
2008-05-30 20:02:14 +08:00
|
|
|
for (; i < NumElements; ++i)
|
2009-08-01 04:28:54 +08:00
|
|
|
Elts.push_back(llvm::Constant::getNullValue(ElemTy));
|
2008-05-30 20:02:14 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
return llvm::ConstantVector::get(VType, Elts);
|
2008-02-05 10:39:50 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +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()->isScalarType()) {
|
|
|
|
// We have a scalar in braces. Just use the first element.
|
2009-04-08 12:48:15 +08:00
|
|
|
if (ILE->getNumInits() > 0) {
|
|
|
|
Expr *Init = ILE->getInit(0);
|
|
|
|
return CGM.EmitConstantExpr(Init, Init->getType(), CGF);
|
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
return CGM.EmitNullConstant(ILE->getType());
|
2008-01-30 07:23:18 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-05-30 18:24:46 +08:00
|
|
|
if (ILE->getType()->isArrayType())
|
|
|
|
return EmitArrayInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isStructureType())
|
|
|
|
return EmitStructInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isUnionType())
|
|
|
|
return EmitUnionInitialization(ILE);
|
|
|
|
|
|
|
|
if (ILE->getType()->isVectorType())
|
|
|
|
return EmitVectorInitialization(ILE);
|
2008-01-30 07:23:18 +08:00
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
assert(0 && "Unable to handle InitListExpr");
|
2008-03-01 16:45:05 +08:00
|
|
|
// Get rid of control reaches end of void function warning.
|
|
|
|
// Not reached.
|
|
|
|
return 0;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2008-02-22 01:57:49 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
llvm::Constant *VisitStringLiteral(StringLiteral *E) {
|
2008-02-11 08:02:17 +08:00
|
|
|
assert(!E->getType()->isPointerType() && "Strings are always arrays");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
// This must be a string initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
2009-08-14 05:57:51 +08:00
|
|
|
return llvm::ConstantArray::get(VMContext,
|
|
|
|
CGM.GetStringForStringLiteral(E), false);
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
|
|
|
|
// This must be an @encode initializing an array in a static initializer.
|
|
|
|
// Don't emit it as the address of the string, emit the string data itself
|
|
|
|
// as an inline array.
|
|
|
|
std::string Str;
|
|
|
|
CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
|
|
|
|
const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
|
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');
|
2009-08-14 05:57:51 +08:00
|
|
|
return llvm::ConstantArray::get(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
|
|
|
|
const llvm::Type *ConvertType(QualType T) {
|
|
|
|
return CGM.getTypes().ConvertType(T);
|
|
|
|
}
|
2008-01-26 12:30:23 +08:00
|
|
|
|
2008-11-16 14:23:45 +08:00
|
|
|
public:
|
2008-01-26 12:30:23 +08:00
|
|
|
llvm::Constant *EmitLValue(Expr *E) {
|
2008-01-26 10:08:50 +08:00
|
|
|
switch (E->getStmtClass()) {
|
2008-02-11 08:23:10 +08:00
|
|
|
default: break;
|
2008-01-26 10:08:50 +08:00
|
|
|
case Expr::CompoundLiteralExprClass: {
|
|
|
|
// Note that due to the nature of compound literals, this is guaranteed
|
|
|
|
// to be the only use of the variable, so we just generate it here.
|
2008-01-26 12:30:23 +08:00
|
|
|
CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
|
|
|
|
llvm::Constant* C = Visit(CLE->getInitializer());
|
2009-02-18 02:43:32 +08:00
|
|
|
// FIXME: "Leaked" on failure.
|
|
|
|
if (C)
|
2009-07-09 03:05:04 +08:00
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
|
2009-08-27 04:01:39 +08:00
|
|
|
E->getType().isConstant(CGM.getContext()),
|
2009-02-18 02:43:32 +08:00
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2009-08-27 04:01:39 +08:00
|
|
|
C, ".compoundliteral", 0, false,
|
|
|
|
E->getType().getAddressSpace());
|
2008-01-26 10:08:50 +08:00
|
|
|
return C;
|
2008-01-26 12:30:23 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
case Expr::DeclRefExprClass:
|
2009-01-06 13:10:23 +08:00
|
|
|
case Expr::QualifiedDeclRefExprClass: {
|
2008-10-22 00:13:35 +08:00
|
|
|
NamedDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
|
2008-01-26 10:08:50 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
|
2009-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);
|
|
|
|
else if (VD->isBlockVarDecl()) {
|
|
|
|
assert(CGF && "Can't access static local vars without CGF");
|
|
|
|
return CGF->GetAddrOfStaticLocalVar(VD);
|
|
|
|
}
|
2008-04-16 06:42:06 +08:00
|
|
|
}
|
2008-02-27 05:41:45 +08:00
|
|
|
}
|
2008-02-11 08:23:10 +08:00
|
|
|
break;
|
2008-01-26 10:08:50 +08:00
|
|
|
}
|
2008-08-14 07:20:05 +08:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
|
2009-02-25 06:18:39 +08:00
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
|
2008-11-16 14:23:45 +08:00
|
|
|
case Expr::ObjCStringLiteralClass: {
|
|
|
|
ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
|
2009-04-01 00:53:37 +08:00
|
|
|
llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(SL);
|
2009-07-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: {
|
|
|
|
// __func__/__FUNCTION__ -> "". __PRETTY_FUNCTION__ -> "top level".
|
|
|
|
std::string Str;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (cast<PredefinedExpr>(E)->getIdentType() ==
|
2008-12-12 13:18:02 +08:00
|
|
|
PredefinedExpr::PrettyFunction)
|
|
|
|
Str = "top level";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 13:18:02 +08:00
|
|
|
return CGM.GetAddrOfConstantCString(Str, ".tmp");
|
|
|
|
}
|
2009-01-25 09:21:06 +08:00
|
|
|
case Expr::AddrLabelExprClass: {
|
|
|
|
assert(CGF && "Invalid address of label expression outside function.");
|
2009-08-14 05:57:51 +08:00
|
|
|
unsigned id =
|
|
|
|
CGF->GetIDForAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
|
|
|
|
llvm::Constant *C =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), id);
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getIntToPtr(C, ConvertType(E->getType()));
|
2009-01-25 09:21:06 +08:00
|
|
|
}
|
2009-01-25 09:54:01 +08:00
|
|
|
case Expr::CallExprClass: {
|
|
|
|
CallExpr* CE = cast<CallExpr>(E);
|
2009-09-09 23:08:12 +08:00
|
|
|
if (CE->isBuiltinCall(CGM.getContext()) !=
|
2009-02-15 02:57:46 +08:00
|
|
|
Builtin::BI__builtin___CFStringMakeConstantString)
|
2009-01-25 09:54:01 +08:00
|
|
|
break;
|
|
|
|
const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
|
|
|
|
const StringLiteral *Literal = cast<StringLiteral>(Arg);
|
2009-04-01 00:53:37 +08:00
|
|
|
// FIXME: need to deal with UCN conversion issues.
|
2009-04-01 21:55:36 +08:00
|
|
|
return CGM.GetAddrOfConstantCFString(Literal);
|
2009-01-25 09:54:01 +08:00
|
|
|
}
|
2009-02-19 09:01:04 +08:00
|
|
|
case Expr::BlockExprClass: {
|
2009-03-01 09:09:12 +08:00
|
|
|
std::string FunctionName;
|
|
|
|
if (CGF)
|
|
|
|
FunctionName = CGF->CurFn->getName();
|
|
|
|
else
|
|
|
|
FunctionName = "global";
|
|
|
|
|
|
|
|
return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
|
2009-02-19 09:01:04 +08:00
|
|
|
}
|
2008-02-11 08:23:10 +08:00
|
|
|
}
|
2009-02-18 02:43:32 +08:00
|
|
|
|
|
|
|
return 0;
|
2008-01-26 10:08:50 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-01-26 09:36:00 +08:00
|
|
|
} // end anonymous namespace.
|
|
|
|
|
2008-02-27 05:41:45 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
|
2009-04-08 12:48:15 +08:00
|
|
|
QualType DestType,
|
2008-07-27 06:37:01 +08:00
|
|
|
CodeGenFunction *CGF) {
|
2008-12-01 10:42:14 +08:00
|
|
|
Expr::EvalResult Result;
|
2009-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
|
2009-04-11 09:08:03 +08:00
|
|
|
Success = E->Evaluate(Result, Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-11 09:08:03 +08:00
|
|
|
if (Success) {
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(!Result.HasSideEffects &&
|
2008-12-01 10:42:14 +08:00
|
|
|
"Constant expr should not have any side effects!");
|
|
|
|
switch (Result.Val.getKind()) {
|
2009-01-29 06:24:07 +08:00
|
|
|
case APValue::Uninitialized:
|
2009-02-18 02:43:32 +08:00
|
|
|
assert(0 && "Constant expressions should be initialized.");
|
|
|
|
return 0;
|
2008-11-16 04:45:50 +08:00
|
|
|
case APValue::LValue: {
|
2009-04-11 09:08:03 +08:00
|
|
|
const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *Offset =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
|
2008-12-01 10:42:14 +08:00
|
|
|
Result.Val.getLValueOffset());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-20 05:44:24 +08:00
|
|
|
llvm::Constant *C;
|
2008-12-01 10:42:14 +08:00
|
|
|
if (const Expr *LVBase = Result.Val.getLValueBase()) {
|
2009-02-20 05:44:24 +08:00
|
|
|
C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase));
|
|
|
|
|
|
|
|
// Apply offset if necessary.
|
|
|
|
if (!Offset->isNullValue()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
const llvm::Type *Type =
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
|
2009-07-30 02:54:39 +08: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 05:44:24 +08:00
|
|
|
}
|
2008-11-16 14:23:45 +08:00
|
|
|
|
2009-02-20 05:44:24 +08:00
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getPtrToInt(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
} else {
|
|
|
|
C = Offset;
|
|
|
|
|
|
|
|
// Convert to the appropriate type; this could be an lvalue for
|
|
|
|
// an integer.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (isa<llvm::PointerType>(DestTy))
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getIntToPtr(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
|
|
|
// If the types don't match this should only be a truncate.
|
2009-04-10 12:59:59 +08:00
|
|
|
if (C->getType() != DestTy)
|
2009-07-30 02:54:39 +08:00
|
|
|
return llvm::ConstantExpr::getTrunc(C, DestTy);
|
2009-02-20 05:44:24 +08:00
|
|
|
|
|
|
|
return C;
|
2008-11-16 14:23:45 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
2008-11-17 11:57:28 +08:00
|
|
|
case APValue::Int: {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::Constant *C = llvm::ConstantInt::get(VMContext,
|
2009-07-25 07:12:58 +08:00
|
|
|
Result.Val.getInt());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
if (C->getType() == llvm::Type::getInt1Ty(VMContext)) {
|
2008-11-16 04:45:50 +08:00
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-30 02:54:39 +08:00
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
|
|
|
return C;
|
2008-11-17 11:57:28 +08:00
|
|
|
}
|
2009-01-29 06:24:07 +08:00
|
|
|
case APValue::ComplexInt: {
|
|
|
|
llvm::Constant *Complex[2];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-25 07:12:58 +08:00
|
|
|
Complex[0] = llvm::ConstantInt::get(VMContext,
|
|
|
|
Result.Val.getComplexIntReal());
|
2009-09-09 23:08:12 +08:00
|
|
|
Complex[1] = llvm::ConstantInt::get(VMContext,
|
2009-07-25 07:12:58 +08:00
|
|
|
Result.Val.getComplexIntImag());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-20 04:00:52 +08:00
|
|
|
// FIXME: the target may want to specify that this is packed.
|
|
|
|
return llvm::ConstantStruct::get(VMContext, Complex, 2, false);
|
2009-01-29 06:24:07 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
case APValue::Float:
|
2009-07-28 05:00:51 +08:00
|
|
|
return llvm::ConstantFP::get(VMContext, Result.Val.getFloat());
|
2008-11-17 09:58:55 +08:00
|
|
|
case APValue::ComplexFloat: {
|
|
|
|
llvm::Constant *Complex[2];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Complex[0] = llvm::ConstantFP::get(VMContext,
|
2009-07-28 05:00:51 +08:00
|
|
|
Result.Val.getComplexFloatReal());
|
|
|
|
Complex[1] = llvm::ConstantFP::get(VMContext,
|
|
|
|
Result.Val.getComplexFloatImag());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-20 04:00:52 +08:00
|
|
|
// FIXME: the target may want to specify that this is packed.
|
|
|
|
return llvm::ConstantStruct::get(VMContext, Complex, 2, false);
|
2008-11-17 09:58:55 +08:00
|
|
|
}
|
2009-01-18 09:01:34 +08:00
|
|
|
case APValue::Vector: {
|
|
|
|
llvm::SmallVector<llvm::Constant *, 4> Inits;
|
|
|
|
unsigned NumElts = Result.Val.getVectorLength();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-18 09:01:34 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
APValue &Elt = Result.Val.getVectorElt(i);
|
|
|
|
if (Elt.isInt())
|
2009-07-25 07:12:58 +08:00
|
|
|
Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
|
2009-01-18 09:01:34 +08:00
|
|
|
else
|
2009-07-28 05:00:51 +08:00
|
|
|
Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2009-07-29 05:22:35 +08:00
|
|
|
return llvm::ConstantVector::get(&Inits[0], Inits.size());
|
2009-01-18 09:01:34 +08:00
|
|
|
}
|
2008-11-16 04:45:50 +08:00
|
|
|
}
|
|
|
|
}
|
2008-06-01 23:31:44 +08:00
|
|
|
|
|
|
|
llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
|
2009-08-14 05:57:51 +08:00
|
|
|
if (C && C->getType() == llvm::Type::getInt1Ty(VMContext)) {
|
2008-06-01 23:31:44 +08:00
|
|
|
const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
|
2009-07-30 02:54:39 +08:00
|
|
|
C = llvm::ConstantExpr::getZExt(C, BoolTy);
|
2008-06-01 23:31:44 +08:00
|
|
|
}
|
|
|
|
return C;
|
2008-01-26 09:36:00 +08:00
|
|
|
}
|
2009-04-14 05:47:26 +08:00
|
|
|
|
2009-09-29 10:09:01 +08:00
|
|
|
static inline bool isDataMemberPointerType(QualType T) {
|
|
|
|
if (const MemberPointerType *MPT = T->getAs<MemberPointerType>())
|
|
|
|
return !MPT->getPointeeType()->isFunctionType();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-14 05:47:26 +08:00
|
|
|
llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
|
2009-08-25 01:16:23 +08:00
|
|
|
// No need to check for member pointers when not compiling C++.
|
|
|
|
if (!getContext().getLangOptions().CPlusPlus)
|
|
|
|
return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
QualType ElementTy = CAT->getElementType();
|
|
|
|
|
|
|
|
// FIXME: Handle arrays of structs that contain member pointers.
|
2009-09-29 10:09:01 +08:00
|
|
|
if (isDataMemberPointerType(Context.getBaseElementType(ElementTy))) {
|
2009-08-10 02:26:27 +08:00
|
|
|
llvm::Constant *Element = EmitNullConstant(ElementTy);
|
|
|
|
uint64_t NumElements = CAT->getSize().getZExtValue();
|
|
|
|
std::vector<llvm::Constant *> Array(NumElements);
|
|
|
|
for (uint64_t i = 0; i != NumElements; ++i)
|
|
|
|
Array[i] = Element;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
const llvm::ArrayType *ATy =
|
2009-08-10 02:26:27 +08:00
|
|
|
cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
|
|
|
|
return llvm::ConstantArray::get(ATy, Array);
|
|
|
|
}
|
|
|
|
}
|
2009-08-23 09:25:01 +08:00
|
|
|
|
|
|
|
if (const RecordType *RT = T->getAs<RecordType>()) {
|
2009-08-25 01:16:23 +08:00
|
|
|
const RecordDecl *RD = RT->getDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: It would be better if there was a way to explicitly compute the
|
2009-08-25 01:16:23 +08:00
|
|
|
// record layout instead of converting to a type.
|
|
|
|
Types.ConvertTagDeclType(RD);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-25 01:16:23 +08:00
|
|
|
const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
|
|
|
|
if (Layout.containsMemberPointer()) {
|
2009-08-23 09:25:01 +08:00
|
|
|
assert(0 && "FIXME: No support for structs with member pointers yet!");
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
// FIXME: Handle structs that contain member pointers.
|
2009-09-29 10:09:01 +08:00
|
|
|
if (isDataMemberPointerType(T))
|
2009-08-10 02:26:27 +08:00
|
|
|
return llvm::Constant::getAllOnesValue(getTypes().ConvertTypeForMem(T));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-03 05:18:22 +08:00
|
|
|
return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
|
2009-04-14 05:47:26 +08:00
|
|
|
}
|