2007-06-16 08:12:05 +08:00
|
|
|
//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is the code that handles AST -> LLVM type lowering.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenTypes.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/AST/AST.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2007-08-18 06:00:32 +08:00
|
|
|
#include "llvm/Module.h"
|
2007-11-01 04:08:22 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-06-16 08:12:05 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-10-25 04:38:06 +08:00
|
|
|
namespace {
|
2007-11-02 03:11:01 +08:00
|
|
|
/// RecordOrganizer - This helper class, used by CGRecordLayout, layouts
|
2007-10-25 04:38:06 +08:00
|
|
|
/// structs and unions. It manages transient information used during layout.
|
|
|
|
/// FIXME : At the moment assume
|
|
|
|
/// - one to one mapping between AST FieldDecls and
|
|
|
|
/// llvm::StructType elements.
|
|
|
|
/// - Ignore bit fields
|
|
|
|
/// - Ignore field aligments
|
|
|
|
/// - Ignore packed structs
|
|
|
|
class RecordOrganizer {
|
|
|
|
public:
|
2007-11-01 08:07:12 +08:00
|
|
|
explicit RecordOrganizer(CodeGenTypes &Types) :
|
2007-11-08 08:17:59 +08:00
|
|
|
CGT(Types), STy(NULL), FieldNo(0), Cursor(0), ExtraBits(0),
|
|
|
|
CurrentFieldStart(0) {}
|
2007-10-25 04:38:06 +08:00
|
|
|
|
|
|
|
/// addField - Add new field.
|
|
|
|
void addField(const FieldDecl *FD);
|
|
|
|
|
2007-11-07 09:57:13 +08:00
|
|
|
/// addLLVMField - Add llvm struct field that corresponds to llvm type Ty.
|
|
|
|
/// Update cursor and increment field count.
|
|
|
|
void addLLVMField(const llvm::Type *Ty, uint64_t Size,
|
|
|
|
const FieldDecl *FD = NULL, unsigned Begin = 0,
|
|
|
|
unsigned End = 0);
|
2007-11-01 07:17:19 +08:00
|
|
|
|
2007-11-08 05:04:59 +08:00
|
|
|
/// addPaddingFields - Current cursor is not suitable place to add next
|
|
|
|
/// field. Add required padding fields.
|
2007-11-01 08:07:12 +08:00
|
|
|
void addPaddingFields(unsigned RequiredBits);
|
2007-11-01 07:17:19 +08:00
|
|
|
|
2007-10-30 04:50:19 +08:00
|
|
|
/// layoutStructFields - Do the actual work and lay out all fields. Create
|
2007-10-25 04:38:06 +08:00
|
|
|
/// corresponding llvm struct type. This should be invoked only after
|
|
|
|
/// all fields are added.
|
2007-11-02 03:11:01 +08:00
|
|
|
void layoutStructFields(const ASTRecordLayout &RL);
|
2007-10-30 04:50:19 +08:00
|
|
|
|
|
|
|
/// layoutUnionFields - Do the actual work and lay out all fields. Create
|
|
|
|
/// corresponding llvm struct type. This should be invoked only after
|
|
|
|
/// all fields are added.
|
2007-11-01 08:07:12 +08:00
|
|
|
void layoutUnionFields();
|
2007-10-25 04:38:06 +08:00
|
|
|
|
|
|
|
/// getLLVMType - Return associated llvm struct type. This may be NULL
|
|
|
|
/// if fields are not laid out.
|
|
|
|
llvm::Type *getLLVMType() const {
|
|
|
|
return STy;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2007-11-01 08:07:12 +08:00
|
|
|
CodeGenTypes &CGT;
|
2007-10-25 04:38:06 +08:00
|
|
|
llvm::Type *STy;
|
2007-11-01 07:17:19 +08:00
|
|
|
unsigned FieldNo;
|
|
|
|
uint64_t Cursor;
|
2007-11-07 09:57:13 +08:00
|
|
|
/* If last field is a bitfield then it may not have occupied all allocated
|
|
|
|
bits. Use remaining bits for next field if it also a bitfield. */
|
|
|
|
uint64_t ExtraBits;
|
2007-11-08 08:17:59 +08:00
|
|
|
/* CurrentFieldStart - Indicates starting offset for current llvm field.
|
|
|
|
When current llvm field is shared by multiple bitfields, this is
|
|
|
|
used find starting bit offset for the bitfield from the beginning of
|
|
|
|
llvm field. */
|
|
|
|
uint64_t CurrentFieldStart;
|
2007-10-25 04:38:06 +08:00
|
|
|
llvm::SmallVector<const FieldDecl *, 8> FieldDecls;
|
2007-11-01 07:17:19 +08:00
|
|
|
std::vector<const llvm::Type*> LLVMFields;
|
2007-10-25 04:38:06 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-11-01 04:01:01 +08:00
|
|
|
CodeGenTypes::CodeGenTypes(ASTContext &Ctx, llvm::Module& M,
|
|
|
|
const llvm::TargetData &TD)
|
|
|
|
: Context(Ctx), Target(Ctx.Target), TheModule(M), TheTargetData(TD) {
|
2007-07-14 09:29:45 +08:00
|
|
|
}
|
2007-06-16 08:12:05 +08:00
|
|
|
|
2007-10-23 10:10:49 +08:00
|
|
|
CodeGenTypes::~CodeGenTypes() {
|
2007-11-02 03:11:01 +08:00
|
|
|
for(llvm::DenseMap<const llvm::Type *, CGRecordLayout *>::iterator
|
|
|
|
I = CGRecordLayouts.begin(), E = CGRecordLayouts.end();
|
2007-10-23 10:10:49 +08:00
|
|
|
I != E; ++I)
|
|
|
|
delete I->second;
|
2007-11-02 03:11:01 +08:00
|
|
|
CGRecordLayouts.clear();
|
2007-10-23 10:10:49 +08:00
|
|
|
}
|
|
|
|
|
2007-06-16 08:12:05 +08:00
|
|
|
/// ConvertType - Convert the specified type to its LLVM form.
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *CodeGenTypes::ConvertType(QualType T) {
|
2007-10-26 02:32:36 +08:00
|
|
|
// See if type is already cached.
|
2007-10-31 04:46:47 +08:00
|
|
|
llvm::DenseMap<Type *, llvm::PATypeHolder>::iterator
|
2007-10-26 02:32:36 +08:00
|
|
|
I = TypeHolderMap.find(T.getTypePtr());
|
2007-10-31 07:22:14 +08:00
|
|
|
if (I != TypeHolderMap.end())
|
|
|
|
return I->second.get();
|
2007-10-26 02:32:36 +08:00
|
|
|
|
|
|
|
const llvm::Type *ResultType = ConvertNewType(T);
|
2007-10-31 04:46:47 +08:00
|
|
|
TypeHolderMap.insert(std::make_pair(T.getTypePtr(),
|
|
|
|
llvm::PATypeHolder(ResultType)));
|
2007-10-26 02:32:36 +08:00
|
|
|
return ResultType;
|
|
|
|
}
|
|
|
|
|
|
|
|
const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
|
2007-06-16 08:12:05 +08:00
|
|
|
const clang::Type &Ty = *T.getCanonicalType();
|
|
|
|
|
|
|
|
switch (Ty.getTypeClass()) {
|
2007-08-03 05:50:34 +08:00
|
|
|
case Type::TypeName: // typedef isn't canonical.
|
|
|
|
case Type::TypeOfExp: // typeof isn't canonical.
|
|
|
|
case Type::TypeOfTyp: // typeof isn't canonical.
|
|
|
|
assert(0 && "Non-canonical type, shouldn't happen");
|
2007-06-16 08:12:05 +08:00
|
|
|
case Type::Builtin: {
|
|
|
|
switch (cast<BuiltinType>(Ty).getKind()) {
|
|
|
|
case BuiltinType::Void:
|
|
|
|
// LLVM void type can only be used as the result of a function call. Just
|
|
|
|
// map to the same as char.
|
2007-07-14 09:29:45 +08:00
|
|
|
return llvm::IntegerType::get(8);
|
2007-06-16 08:12:05 +08:00
|
|
|
|
|
|
|
case BuiltinType::Bool:
|
|
|
|
// FIXME: This is very strange. We want scalars to be i1, but in memory
|
|
|
|
// they can be i1 or i32. Should the codegen handle this issue?
|
|
|
|
return llvm::Type::Int1Ty;
|
|
|
|
|
2007-07-14 09:29:45 +08:00
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
case BuiltinType::UChar:
|
2007-06-16 08:12:05 +08:00
|
|
|
case BuiltinType::Short:
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
case BuiltinType::Int:
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
case BuiltinType::Long:
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
case BuiltinType::ULongLong:
|
2007-09-04 10:34:27 +08:00
|
|
|
return llvm::IntegerType::get(
|
|
|
|
static_cast<unsigned>(Context.getTypeSize(T, SourceLocation())));
|
2007-06-16 08:12:05 +08:00
|
|
|
|
|
|
|
case BuiltinType::Float: return llvm::Type::FloatTy;
|
|
|
|
case BuiltinType::Double: return llvm::Type::DoubleTy;
|
|
|
|
case BuiltinType::LongDouble:
|
2007-06-23 02:15:26 +08:00
|
|
|
// FIXME: mapping long double onto double.
|
|
|
|
return llvm::Type::DoubleTy;
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-06-23 04:56:16 +08:00
|
|
|
case Type::Complex: {
|
|
|
|
std::vector<const llvm::Type*> Elts;
|
|
|
|
Elts.push_back(ConvertType(cast<ComplexType>(Ty).getElementType()));
|
|
|
|
Elts.push_back(Elts[0]);
|
|
|
|
return llvm::StructType::get(Elts);
|
|
|
|
}
|
2007-06-16 08:12:05 +08:00
|
|
|
case Type::Pointer: {
|
|
|
|
const PointerType &P = cast<PointerType>(Ty);
|
2007-06-23 03:05:19 +08:00
|
|
|
return llvm::PointerType::get(ConvertType(P.getPointeeType()));
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
case Type::Reference: {
|
|
|
|
const ReferenceType &R = cast<ReferenceType>(Ty);
|
2007-06-23 03:05:19 +08:00
|
|
|
return llvm::PointerType::get(ConvertType(R.getReferenceeType()));
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
|
2007-08-30 09:06:46 +08:00
|
|
|
case Type::VariableArray: {
|
|
|
|
const VariableArrayType &A = cast<VariableArrayType>(Ty);
|
2007-06-16 08:12:05 +08:00
|
|
|
assert(A.getSizeModifier() == ArrayType::Normal &&
|
|
|
|
A.getIndexTypeQualifier() == 0 &&
|
|
|
|
"FIXME: We only handle trivial array types so far!");
|
2007-08-26 13:02:07 +08:00
|
|
|
if (A.getSizeExpr() == 0) {
|
|
|
|
// int X[] -> [0 x int]
|
|
|
|
return llvm::ArrayType::get(ConvertType(A.getElementType()), 0);
|
2007-06-16 08:12:05 +08:00
|
|
|
} else {
|
|
|
|
assert(0 && "FIXME: VLAs not implemented yet!");
|
|
|
|
}
|
|
|
|
}
|
2007-08-30 09:06:46 +08:00
|
|
|
case Type::ConstantArray: {
|
|
|
|
const ConstantArrayType &A = cast<ConstantArrayType>(Ty);
|
|
|
|
const llvm::Type *EltTy = ConvertType(A.getElementType());
|
|
|
|
return llvm::ArrayType::get(EltTy, A.getSize().getZExtValue());
|
|
|
|
}
|
2007-07-19 13:13:51 +08:00
|
|
|
case Type::OCUVector:
|
2007-07-10 08:23:39 +08:00
|
|
|
case Type::Vector: {
|
|
|
|
const VectorType &VT = cast<VectorType>(Ty);
|
|
|
|
return llvm::VectorType::get(ConvertType(VT.getElementType()),
|
|
|
|
VT.getNumElements());
|
|
|
|
}
|
2007-06-16 08:12:05 +08:00
|
|
|
case Type::FunctionNoProto:
|
|
|
|
case Type::FunctionProto: {
|
|
|
|
const FunctionType &FP = cast<FunctionType>(Ty);
|
|
|
|
const llvm::Type *ResultType;
|
|
|
|
|
|
|
|
if (FP.getResultType()->isVoidType())
|
|
|
|
ResultType = llvm::Type::VoidTy; // Result of function uses llvm void.
|
|
|
|
else
|
2007-06-23 03:05:19 +08:00
|
|
|
ResultType = ConvertType(FP.getResultType());
|
2007-06-16 08:12:05 +08:00
|
|
|
|
|
|
|
// FIXME: Convert argument types.
|
|
|
|
bool isVarArg;
|
|
|
|
std::vector<const llvm::Type*> ArgTys;
|
2007-06-23 06:02:34 +08:00
|
|
|
|
|
|
|
// Struct return passes the struct byref.
|
2007-06-28 02:08:49 +08:00
|
|
|
if (!ResultType->isFirstClassType() && ResultType != llvm::Type::VoidTy) {
|
2007-10-26 02:32:36 +08:00
|
|
|
const llvm::Type *RType = llvm::PointerType::get(ResultType);
|
|
|
|
QualType RTy = Context.getPointerType(FP.getResultType());
|
2007-10-31 04:46:47 +08:00
|
|
|
TypeHolderMap.insert(std::make_pair(RTy.getTypePtr(),
|
|
|
|
llvm::PATypeHolder(RType)));
|
|
|
|
|
2007-10-26 02:32:36 +08:00
|
|
|
ArgTys.push_back(RType);
|
2007-06-23 06:02:34 +08:00
|
|
|
ResultType = llvm::Type::VoidTy;
|
|
|
|
}
|
|
|
|
|
2007-06-16 08:12:05 +08:00
|
|
|
if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(&FP)) {
|
2007-06-23 03:05:19 +08:00
|
|
|
DecodeArgumentTypes(*FTP, ArgTys);
|
2007-06-16 08:12:05 +08:00
|
|
|
isVarArg = FTP->isVariadic();
|
|
|
|
} else {
|
|
|
|
isVarArg = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return llvm::FunctionType::get(ResultType, ArgTys, isVarArg, 0);
|
|
|
|
}
|
2007-09-17 03:23:47 +08:00
|
|
|
|
|
|
|
case Type::ObjcInterface:
|
|
|
|
assert(0 && "FIXME: add missing functionality here");
|
|
|
|
break;
|
2007-10-09 07:06:41 +08:00
|
|
|
|
|
|
|
case Type::ObjcQualifiedInterface:
|
|
|
|
assert(0 && "FIXME: add missing functionality here");
|
|
|
|
break;
|
2007-09-17 03:23:47 +08:00
|
|
|
|
2007-06-16 08:12:05 +08:00
|
|
|
case Type::Tagged:
|
2007-08-18 06:00:32 +08:00
|
|
|
const TagType &TT = cast<TagType>(Ty);
|
|
|
|
const TagDecl *TD = TT.getDecl();
|
2007-08-21 08:21:21 +08:00
|
|
|
llvm::Type *&ResultType = TagDeclTypes[TD];
|
2007-08-18 06:00:32 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
if (ResultType)
|
|
|
|
return ResultType;
|
|
|
|
|
2007-08-18 06:00:32 +08:00
|
|
|
if (!TD->isDefinition()) {
|
2007-08-28 01:44:34 +08:00
|
|
|
ResultType = llvm::OpaqueType::get();
|
|
|
|
} else if (TD->getKind() == Decl::Enum) {
|
2007-08-29 02:24:31 +08:00
|
|
|
return ConvertType(cast<EnumDecl>(TD)->getIntegerType());
|
2007-08-26 12:50:19 +08:00
|
|
|
} else if (TD->getKind() == Decl::Struct) {
|
|
|
|
const RecordDecl *RD = cast<const RecordDecl>(TD);
|
2007-10-24 07:26:46 +08:00
|
|
|
|
|
|
|
// If this is nested record and this RecordDecl is already under
|
|
|
|
// process then return associated OpaqueType for now.
|
|
|
|
llvm::DenseMap<const RecordDecl *, llvm::Type *>::iterator
|
2007-10-24 08:26:24 +08:00
|
|
|
OpaqueI = RecordTypesToResolve.find(RD);
|
2007-10-24 07:26:46 +08:00
|
|
|
if (OpaqueI != RecordTypesToResolve.end())
|
2007-10-24 08:26:24 +08:00
|
|
|
return OpaqueI->second;
|
2007-10-24 07:26:46 +08:00
|
|
|
|
|
|
|
// Create new OpaqueType now for later use.
|
2007-10-31 04:59:40 +08:00
|
|
|
// FIXME: This creates a lot of opaque types, most of them are not
|
|
|
|
// needed. Reevaluate this when performance analyis finds tons of
|
|
|
|
// opaque types.
|
2007-10-24 07:26:46 +08:00
|
|
|
llvm::OpaqueType *OpaqueTy = llvm::OpaqueType::get();
|
|
|
|
RecordTypesToResolve[RD] = OpaqueTy;
|
2007-10-31 04:46:47 +08:00
|
|
|
TypeHolderMap.insert(std::make_pair(T.getTypePtr(),
|
|
|
|
llvm::PATypeHolder(OpaqueTy)));
|
2007-10-24 07:26:46 +08:00
|
|
|
|
|
|
|
// Layout fields.
|
2007-11-01 08:07:12 +08:00
|
|
|
RecordOrganizer RO(*this);
|
2007-08-26 12:50:19 +08:00
|
|
|
for (unsigned i = 0, e = RD->getNumMembers(); i != e; ++i)
|
2007-10-24 08:56:23 +08:00
|
|
|
RO.addField(RD->getMember(i));
|
2007-11-08 05:04:59 +08:00
|
|
|
const ASTRecordLayout &RL = Context.getASTRecordLayout(RD,
|
|
|
|
SourceLocation());
|
2007-11-01 08:07:12 +08:00
|
|
|
RO.layoutStructFields(RL);
|
2007-10-24 07:26:46 +08:00
|
|
|
|
|
|
|
// Get llvm::StructType.
|
2007-11-02 03:11:01 +08:00
|
|
|
CGRecordLayout *RLI = new CGRecordLayout(RO.getLLVMType());
|
2007-10-23 10:10:49 +08:00
|
|
|
ResultType = RLI->getLLVMType();
|
2007-11-02 03:11:01 +08:00
|
|
|
CGRecordLayouts[ResultType] = RLI;
|
2007-10-24 07:26:46 +08:00
|
|
|
|
|
|
|
// Refine any OpaqueType associated with this RecordDecl.
|
|
|
|
OpaqueTy->refineAbstractTypeTo(ResultType);
|
|
|
|
OpaqueI = RecordTypesToResolve.find(RD);
|
|
|
|
assert (OpaqueI != RecordTypesToResolve.end()
|
2007-10-24 08:26:24 +08:00
|
|
|
&& "Expected RecordDecl in RecordTypesToResolve");
|
2007-10-24 07:26:46 +08:00
|
|
|
RecordTypesToResolve.erase(OpaqueI);
|
|
|
|
|
2007-08-26 12:50:19 +08:00
|
|
|
} else if (TD->getKind() == Decl::Union) {
|
|
|
|
const RecordDecl *RD = cast<const RecordDecl>(TD);
|
|
|
|
// Just use the largest element of the union, breaking ties with the
|
|
|
|
// highest aligned member.
|
2007-10-27 03:42:18 +08:00
|
|
|
|
2007-08-26 12:50:19 +08:00
|
|
|
if (RD->getNumMembers() != 0) {
|
2007-11-01 08:07:12 +08:00
|
|
|
RecordOrganizer RO(*this);
|
2007-10-30 04:50:19 +08:00
|
|
|
for (unsigned i = 0, e = RD->getNumMembers(); i != e; ++i)
|
|
|
|
RO.addField(RD->getMember(i));
|
2007-11-01 08:07:12 +08:00
|
|
|
RO.layoutUnionFields();
|
2007-10-27 03:42:18 +08:00
|
|
|
|
|
|
|
// Get llvm::StructType.
|
2007-11-02 03:11:01 +08:00
|
|
|
CGRecordLayout *RLI = new CGRecordLayout(RO.getLLVMType());
|
2007-10-27 03:42:18 +08:00
|
|
|
ResultType = RLI->getLLVMType();
|
2007-11-02 03:11:01 +08:00
|
|
|
CGRecordLayouts[ResultType] = RLI;
|
2007-10-27 03:42:18 +08:00
|
|
|
} else {
|
|
|
|
std::vector<const llvm::Type*> Fields;
|
|
|
|
ResultType = llvm::StructType::get(Fields);
|
|
|
|
}
|
2007-08-18 06:00:32 +08:00
|
|
|
} else {
|
2007-08-26 12:50:19 +08:00
|
|
|
assert(0 && "FIXME: Implement tag decl kind!");
|
2007-08-18 06:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TypeName(TD->getKindName());
|
|
|
|
TypeName += '.';
|
|
|
|
TypeName += TD->getName();
|
|
|
|
|
|
|
|
TheModule.addTypeName(TypeName, ResultType);
|
|
|
|
return ResultType;
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: implement.
|
|
|
|
return llvm::OpaqueType::get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenTypes::DecodeArgumentTypes(const FunctionTypeProto &FTP,
|
2007-06-23 03:05:19 +08:00
|
|
|
std::vector<const llvm::Type*> &ArgTys) {
|
2007-06-16 08:12:05 +08:00
|
|
|
for (unsigned i = 0, e = FTP.getNumArgs(); i != e; ++i) {
|
2007-06-23 03:05:19 +08:00
|
|
|
const llvm::Type *Ty = ConvertType(FTP.getArgType(i));
|
2007-06-16 08:12:05 +08:00
|
|
|
if (Ty->isFirstClassType())
|
|
|
|
ArgTys.push_back(Ty);
|
2007-10-26 02:32:36 +08:00
|
|
|
else {
|
|
|
|
QualType PTy = Context.getPointerType(FTP.getArgType(i));
|
|
|
|
const llvm::Type *PtrTy = llvm::PointerType::get(Ty);
|
2007-10-31 04:46:47 +08:00
|
|
|
TypeHolderMap.insert(std::make_pair(PTy.getTypePtr(),
|
|
|
|
llvm::PATypeHolder(PtrTy)));
|
|
|
|
|
2007-10-26 02:32:36 +08:00
|
|
|
ArgTys.push_back(PtrTy);
|
|
|
|
}
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-23 10:10:49 +08:00
|
|
|
/// getLLVMFieldNo - Return llvm::StructType element number
|
|
|
|
/// that corresponds to the field FD.
|
|
|
|
unsigned CodeGenTypes::getLLVMFieldNo(const FieldDecl *FD) {
|
2007-11-07 09:57:13 +08:00
|
|
|
// FIXME : Check bit fields also
|
2007-10-23 10:10:49 +08:00
|
|
|
llvm::DenseMap<const FieldDecl *, unsigned>::iterator
|
|
|
|
I = FieldInfo.find(FD);
|
2007-10-24 08:07:36 +08:00
|
|
|
assert (I != FieldInfo.end() && "Unable to find field info");
|
2007-10-23 10:10:49 +08:00
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2007-10-24 08:56:23 +08:00
|
|
|
/// addFieldInfo - Assign field number to field FD.
|
2007-11-07 09:57:13 +08:00
|
|
|
void CodeGenTypes::addFieldInfo(const FieldDecl *FD, unsigned No,
|
|
|
|
unsigned Begin, unsigned End) {
|
|
|
|
if (Begin == 0 && End == 0)
|
|
|
|
FieldInfo[FD] = No;
|
|
|
|
else
|
|
|
|
// FD is a bit field
|
|
|
|
BitFields.insert(std::make_pair(FD, BitFieldInfo(No, Begin, End)));
|
2007-10-23 10:10:49 +08:00
|
|
|
}
|
|
|
|
|
2007-11-02 03:11:01 +08:00
|
|
|
/// getCGRecordLayout - Return record layout info for the given llvm::Type.
|
|
|
|
const CGRecordLayout *
|
|
|
|
CodeGenTypes::getCGRecordLayout(const llvm::Type* Ty) const {
|
|
|
|
llvm::DenseMap<const llvm::Type*, CGRecordLayout *>::iterator I
|
|
|
|
= CGRecordLayouts.find(Ty);
|
|
|
|
assert (I != CGRecordLayouts.end()
|
2007-10-23 10:10:49 +08:00
|
|
|
&& "Unable to find record layout information for type");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addField - Add new field.
|
|
|
|
void RecordOrganizer::addField(const FieldDecl *FD) {
|
|
|
|
assert (!STy && "Record fields are already laid out");
|
|
|
|
FieldDecls.push_back(FD);
|
|
|
|
}
|
|
|
|
|
2007-10-30 04:50:19 +08:00
|
|
|
/// layoutStructFields - Do the actual work and lay out all fields. Create
|
2007-10-23 10:10:49 +08:00
|
|
|
/// corresponding llvm struct type. This should be invoked only after
|
|
|
|
/// all fields are added.
|
|
|
|
/// FIXME : At the moment assume
|
|
|
|
/// - one to one mapping between AST FieldDecls and
|
|
|
|
/// llvm::StructType elements.
|
|
|
|
/// - Ignore bit fields
|
|
|
|
/// - Ignore field aligments
|
|
|
|
/// - Ignore packed structs
|
2007-11-02 03:11:01 +08:00
|
|
|
void RecordOrganizer::layoutStructFields(const ASTRecordLayout &RL) {
|
2007-10-23 10:10:49 +08:00
|
|
|
// FIXME : Use SmallVector
|
2007-11-01 08:07:12 +08:00
|
|
|
Cursor = 0;
|
2007-11-01 07:17:19 +08:00
|
|
|
FieldNo = 0;
|
|
|
|
LLVMFields.clear();
|
2007-10-23 10:10:49 +08:00
|
|
|
for (llvm::SmallVector<const FieldDecl *, 8>::iterator I = FieldDecls.begin(),
|
2007-10-24 08:26:24 +08:00
|
|
|
E = FieldDecls.end(); I != E; ++I) {
|
2007-10-23 10:10:49 +08:00
|
|
|
const FieldDecl *FD = *I;
|
|
|
|
|
2007-11-07 09:57:13 +08:00
|
|
|
if (FD->isBitField()) {
|
|
|
|
Expr *BitWidth = FD->getBitWidth();
|
|
|
|
llvm::APSInt FieldSize(32);
|
|
|
|
bool isBitField =
|
|
|
|
BitWidth->isIntegerConstantExpr(FieldSize, CGT.getContext());
|
|
|
|
assert (isBitField && "Invalid BitField size expression");
|
|
|
|
uint64_t BitFieldSize = FieldSize.getZExtValue();
|
|
|
|
if (ExtraBits == 0) {
|
2007-11-08 08:32:12 +08:00
|
|
|
const llvm::Type *PrevTy = LLVMFields.back();
|
2007-11-07 09:57:13 +08:00
|
|
|
const llvm::Type *Ty = CGT.ConvertType(FD->getType());
|
2007-11-08 08:32:12 +08:00
|
|
|
assert (CGT.getTargetData().getTypeSizeInBits(PrevTy) >=
|
|
|
|
CGT.getTargetData().getTypeSizeInBits(Ty)
|
|
|
|
&& "FIXME Unable to handle bit field. Reuse last field");
|
|
|
|
|
2007-11-07 09:57:13 +08:00
|
|
|
// Calculate extra bits available in this bitfield.
|
|
|
|
ExtraBits = CGT.getTargetData().getTypeSizeInBits(Ty) - BitFieldSize;
|
|
|
|
addLLVMField(Ty, BitFieldSize, FD, 0, ExtraBits);
|
2007-11-08 08:17:59 +08:00
|
|
|
} else if (ExtraBits >= BitFieldSize) {
|
2007-11-07 09:57:13 +08:00
|
|
|
// Reuse existing llvm field
|
|
|
|
ExtraBits = ExtraBits - BitFieldSize;
|
2007-11-08 08:17:59 +08:00
|
|
|
CGT.addFieldInfo(FD, FieldNo, Cursor - CurrentFieldStart,
|
2007-11-07 09:57:13 +08:00
|
|
|
ExtraBits);
|
2007-11-08 08:17:59 +08:00
|
|
|
Cursor = Cursor + BitFieldSize;
|
2007-11-07 09:57:13 +08:00
|
|
|
++FieldNo;
|
|
|
|
} else
|
|
|
|
assert (!FD->isBitField() && "Bit fields are not yet supported");
|
|
|
|
} else {
|
|
|
|
ExtraBits = 0;
|
|
|
|
const llvm::Type *Ty = CGT.ConvertType(FD->getType());
|
|
|
|
addLLVMField(Ty, CGT.getTargetData().getTypeSizeInBits(Ty), FD, 0, 0);
|
|
|
|
}
|
2007-10-23 10:10:49 +08:00
|
|
|
}
|
2007-11-01 07:17:19 +08:00
|
|
|
STy = llvm::StructType::get(LLVMFields);
|
|
|
|
}
|
|
|
|
|
2007-11-01 08:07:12 +08:00
|
|
|
/// addPaddingFields - Current cursor is not suitable place to add next field.
|
|
|
|
/// Add required padding fields.
|
|
|
|
void RecordOrganizer::addPaddingFields(unsigned RequiredBits) {
|
|
|
|
assert ((RequiredBits % 8) == 0 && "FIXME Invalid struct layout");
|
|
|
|
unsigned RequiredBytes = RequiredBits / 8;
|
|
|
|
for (unsigned i = 0; i != RequiredBytes; ++i)
|
2007-11-07 09:57:13 +08:00
|
|
|
addLLVMField(llvm::Type::Int8Ty,
|
|
|
|
CGT.getTargetData().getTypeSizeInBits(llvm::Type::Int8Ty));
|
2007-11-01 08:07:12 +08:00
|
|
|
}
|
|
|
|
|
2007-11-08 05:04:59 +08:00
|
|
|
/// addLLVMField - Add llvm struct field that corresponds to llvm type Ty.
|
|
|
|
/// Update cursor and increment field count. If field decl FD is available than
|
2007-11-01 07:17:19 +08:00
|
|
|
/// update field info at CodeGenTypes level.
|
2007-11-07 09:57:13 +08:00
|
|
|
void RecordOrganizer::addLLVMField(const llvm::Type *Ty, uint64_t Size,
|
|
|
|
const FieldDecl *FD, unsigned Begin,
|
|
|
|
unsigned End) {
|
|
|
|
|
|
|
|
unsigned AlignmentInBits = CGT.getTargetData().getABITypeAlignment(Ty) * 8;
|
|
|
|
if (Cursor % AlignmentInBits != 0)
|
|
|
|
// At the moment, insert padding fields even if target specific llvm
|
|
|
|
// type alignment enforces implict padding fields for FD. Later on,
|
|
|
|
// optimize llvm fields by removing implicit padding fields and
|
|
|
|
// combining consequetive padding fields.
|
|
|
|
addPaddingFields(Cursor % AlignmentInBits);
|
|
|
|
|
2007-11-08 08:17:59 +08:00
|
|
|
CurrentFieldStart = Cursor;
|
2007-11-07 09:57:13 +08:00
|
|
|
Cursor += Size;
|
2007-11-01 07:17:19 +08:00
|
|
|
LLVMFields.push_back(Ty);
|
|
|
|
if (FD)
|
2007-11-07 09:57:13 +08:00
|
|
|
CGT.addFieldInfo(FD, FieldNo, Begin, End);
|
2007-11-01 07:17:19 +08:00
|
|
|
++FieldNo;
|
2007-10-23 10:10:49 +08:00
|
|
|
}
|
2007-10-24 08:56:23 +08:00
|
|
|
|
2007-10-30 04:50:19 +08:00
|
|
|
/// layoutUnionFields - Do the actual work and lay out all fields. Create
|
|
|
|
/// corresponding llvm struct type. This should be invoked only after
|
|
|
|
/// all fields are added.
|
2007-11-01 08:07:12 +08:00
|
|
|
void RecordOrganizer::layoutUnionFields() {
|
2007-10-30 04:50:19 +08:00
|
|
|
|
|
|
|
unsigned PrimaryEltNo = 0;
|
|
|
|
std::pair<uint64_t, unsigned> PrimaryElt =
|
|
|
|
CGT.getContext().getTypeInfo(FieldDecls[0]->getType(), SourceLocation());
|
2007-11-07 09:57:13 +08:00
|
|
|
CGT.addFieldInfo(FieldDecls[0], 0, 0, 0);
|
2007-10-30 04:50:19 +08:00
|
|
|
|
|
|
|
unsigned Size = FieldDecls.size();
|
|
|
|
for(unsigned i = 1; i != Size; ++i) {
|
|
|
|
const FieldDecl *FD = FieldDecls[i];
|
2007-11-07 09:57:13 +08:00
|
|
|
assert (!FD->isBitField() && "Bit fields are not yet supported");
|
2007-10-30 04:50:19 +08:00
|
|
|
std::pair<uint64_t, unsigned> EltInfo =
|
|
|
|
CGT.getContext().getTypeInfo(FD->getType(), SourceLocation());
|
|
|
|
|
|
|
|
// Use largest element, breaking ties with the hightest aligned member.
|
|
|
|
if (EltInfo.first > PrimaryElt.first ||
|
|
|
|
(EltInfo.first == PrimaryElt.first &&
|
|
|
|
EltInfo.second > PrimaryElt.second)) {
|
|
|
|
PrimaryElt = EltInfo;
|
|
|
|
PrimaryEltNo = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In union, each field gets first slot.
|
2007-11-07 09:57:13 +08:00
|
|
|
CGT.addFieldInfo(FD, 0, 0, 0);
|
2007-10-30 04:50:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const llvm::Type*> Fields;
|
|
|
|
const llvm::Type *Ty = CGT.ConvertType(FieldDecls[PrimaryEltNo]->getType());
|
|
|
|
Fields.push_back(Ty);
|
|
|
|
STy = llvm::StructType::get(Fields);
|
|
|
|
}
|
|
|
|
|