2007-06-16 08:12:05 +08:00
|
|
|
//===--- CodeGenTypes.cpp - Type translation for LLVM CodeGen -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-06-16 08:12:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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) :
|
2008-01-22 06:54:57 +08:00
|
|
|
CGT(Types), STy(NULL), llvmFieldNo(0), Cursor(0),
|
|
|
|
llvmSize(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.
|
2008-01-22 06:54:57 +08:00
|
|
|
/// Increment field count.
|
|
|
|
void addLLVMField(const llvm::Type *Ty);
|
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-12-22 02:43:53 +08:00
|
|
|
void addPaddingFields(unsigned WaterMark);
|
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;
|
|
|
|
}
|
|
|
|
|
2007-12-12 03:51:39 +08:00
|
|
|
/// placeBitField - Find a place for FD, which is a bit-field.
|
|
|
|
void placeBitField(const FieldDecl *FD);
|
|
|
|
|
2007-10-25 04:38:06 +08:00
|
|
|
private:
|
2007-11-01 08:07:12 +08:00
|
|
|
CodeGenTypes &CGT;
|
2007-10-25 04:38:06 +08:00
|
|
|
llvm::Type *STy;
|
2008-01-22 06:54:57 +08:00
|
|
|
unsigned llvmFieldNo;
|
|
|
|
uint64_t Cursor;
|
2007-12-22 02:43:53 +08:00
|
|
|
uint64_t llvmSize;
|
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-12-11 08:49:18 +08:00
|
|
|
llvm::SmallVector<uint64_t, 8> Offsets;
|
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-12-22 03:35:28 +08:00
|
|
|
/// isOpaqueTypeDefinition - Return true if LT is a llvm::OpaqueType
|
|
|
|
/// and T is tag definition. This helper routine does not check
|
|
|
|
/// relationship between T and LT.
|
2008-01-31 05:21:08 +08:00
|
|
|
static bool isOpaqueTypeDefinition(QualType T, const llvm::Type *LT) {
|
2008-01-31 23:19:04 +08:00
|
|
|
if (const PointerType* PTy = T->getAsPointerType()) {
|
2008-01-31 05:21:08 +08:00
|
|
|
return
|
2008-01-31 23:19:04 +08:00
|
|
|
isOpaqueTypeDefinition(PTy->getPointeeType(),
|
2008-01-31 05:21:08 +08:00
|
|
|
cast<llvm::PointerType>(LT)->getElementType());
|
|
|
|
}
|
2007-12-22 03:35:28 +08:00
|
|
|
if (!isa<llvm::OpaqueType>(LT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const clang::Type &Ty = *T.getCanonicalType();
|
|
|
|
if (Ty.getTypeClass() == Type::Tagged) {
|
|
|
|
const TagType &TT = cast<TagType>(Ty);
|
|
|
|
const TagDecl *TD = TT.getDecl();
|
|
|
|
if (TD->isDefinition())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-12-22 03:35:28 +08:00
|
|
|
// If type is found in map and this is not a definition for a opaque
|
|
|
|
// place holder type then use it. Otherwise convert type T.
|
|
|
|
if (I != TypeHolderMap.end() && !isOpaqueTypeDefinition(T, I->second.get()))
|
2007-10-31 07:22:14 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-01-10 02:47:25 +08:00
|
|
|
/// ConvertTypeForMem - Convert type T into a llvm::Type. Maintain and use
|
|
|
|
/// type cache through TypeHolderMap. This differs from ConvertType in that
|
|
|
|
/// it is used to convert to the memory representation for a type. For
|
|
|
|
/// example, the scalar representation for _Bool is i1, but the memory
|
|
|
|
/// representation is usually i8 or i32, depending on the target.
|
|
|
|
const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) {
|
|
|
|
const llvm::Type *R = ConvertType(T);
|
|
|
|
|
|
|
|
// If this is a non-bool type, don't map it.
|
|
|
|
if (R != llvm::Type::Int1Ty)
|
|
|
|
return R;
|
|
|
|
|
|
|
|
// Otherwise, return an integer of the target-specified size.
|
|
|
|
unsigned BoolWidth = (unsigned)Context.getTypeSize(T, SourceLocation());
|
|
|
|
return llvm::IntegerType::get(BoolWidth);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-26 02:32:36 +08:00
|
|
|
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:
|
2008-01-10 02:47:25 +08:00
|
|
|
// Note that we always return bool as i1 for use as a scalar type.
|
2007-06-16 08:12:05 +08:00
|
|
|
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-12-17 09:11:20 +08:00
|
|
|
return llvm::PointerType::getUnqual(ConvertType(P.getPointeeType()));
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
case Type::Reference: {
|
|
|
|
const ReferenceType &R = cast<ReferenceType>(Ty);
|
2007-12-17 09:11:20 +08:00
|
|
|
return llvm::PointerType::getUnqual(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-12-17 09:11:20 +08:00
|
|
|
const llvm::Type *RType = llvm::PointerType::getUnqual(ResultType);
|
2007-10-26 02:32:36 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-11-28 02:20:52 +08:00
|
|
|
return llvm::FunctionType::get(ResultType, ArgTys, isVarArg);
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
2007-09-17 03:23:47 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
case Type::ObjCInterface:
|
2007-09-17 03:23:47 +08:00
|
|
|
assert(0 && "FIXME: add missing functionality here");
|
|
|
|
break;
|
2007-10-09 07:06:41 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
case Type::ObjCQualifiedInterface:
|
2007-10-09 07:06:41 +08:00
|
|
|
assert(0 && "FIXME: add missing functionality here");
|
|
|
|
break;
|
2007-09-17 03:23:47 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
case Type::ObjCQualifiedId:
|
2007-12-18 05:03:50 +08:00
|
|
|
assert(0 && "FIXME: add missing functionality here");
|
|
|
|
break;
|
|
|
|
|
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-12-22 03:35:28 +08:00
|
|
|
// If corresponding llvm type is not a opaque struct type
|
|
|
|
// then use it.
|
|
|
|
if (ResultType && !isOpaqueTypeDefinition(T, ResultType))
|
2007-08-21 08:21:21 +08:00
|
|
|
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
|
|
|
|
2007-12-22 03:35:28 +08:00
|
|
|
llvm::OpaqueType *OpaqueTy = NULL;
|
|
|
|
if (ResultType)
|
|
|
|
OpaqueTy = dyn_cast<llvm::OpaqueType>(ResultType);
|
|
|
|
if (!OpaqueTy) {
|
|
|
|
// Create new OpaqueType now for later use.
|
|
|
|
// FIXME: This creates a lot of opaque types, most of them are not
|
|
|
|
// needed. Reevaluate this when performance analyis finds tons of
|
|
|
|
// opaque types.
|
|
|
|
OpaqueTy = llvm::OpaqueType::get();
|
|
|
|
TypeHolderMap.insert(std::make_pair(T.getTypePtr(),
|
|
|
|
llvm::PATypeHolder(OpaqueTy)));
|
|
|
|
}
|
2007-10-24 07:26:46 +08:00
|
|
|
RecordTypesToResolve[RD] = OpaqueTy;
|
|
|
|
|
|
|
|
// 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 += '.';
|
2007-12-01 17:08:41 +08:00
|
|
|
|
|
|
|
// Name the codegen type after the typedef name
|
|
|
|
// if there is no tag type name available
|
2007-12-06 01:21:34 +08:00
|
|
|
if (TD->getIdentifier() == 0) {
|
2007-12-01 17:20:34 +08:00
|
|
|
if (T->getTypeClass() == Type::TypeName) {
|
|
|
|
const TypedefType *TdT = cast<TypedefType>(T);
|
|
|
|
TypeName += TdT->getDecl()->getName();
|
|
|
|
} else
|
|
|
|
TypeName += "anon";
|
2007-12-01 17:08:41 +08:00
|
|
|
} else
|
|
|
|
TypeName += TD->getName();
|
2007-08-18 06:00:32 +08:00
|
|
|
|
|
|
|
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));
|
2007-12-17 09:11:20 +08:00
|
|
|
const llvm::Type *PtrTy = llvm::PointerType::getUnqual(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) {
|
|
|
|
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.
|
2008-01-22 06:54:57 +08:00
|
|
|
void CodeGenTypes::addFieldInfo(const FieldDecl *FD, unsigned No) {
|
|
|
|
FieldInfo[FD] = No;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getBitFieldInfo - Return the BitFieldInfo that corresponds to the field FD.
|
|
|
|
CodeGenTypes::BitFieldInfo CodeGenTypes::getBitFieldInfo(const FieldDecl *FD) {
|
|
|
|
llvm::DenseMap<const FieldDecl *, BitFieldInfo>::iterator
|
|
|
|
I = BitFields.find(FD);
|
|
|
|
assert (I != BitFields.end() && "Unable to find bitfield info");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// addBitFieldInfo - Assign a start bit and a size to field FD.
|
|
|
|
void CodeGenTypes::addBitFieldInfo(const FieldDecl *FD, unsigned Begin,
|
|
|
|
unsigned Size) {
|
|
|
|
BitFields.insert(std::make_pair(FD, BitFieldInfo(Begin, Size)));
|
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
|
2008-01-22 06:54:57 +08:00
|
|
|
llvmSize = 0;
|
|
|
|
llvmFieldNo = 0;
|
2007-11-01 08:07:12 +08:00
|
|
|
Cursor = 0;
|
2007-11-01 07:17:19 +08:00
|
|
|
LLVMFields.clear();
|
2008-01-22 06:54:57 +08:00
|
|
|
Offsets.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-12-12 03:51:39 +08:00
|
|
|
if (FD->isBitField())
|
|
|
|
placeBitField(FD);
|
|
|
|
else {
|
2007-11-07 09:57:13 +08:00
|
|
|
const llvm::Type *Ty = CGT.ConvertType(FD->getType());
|
2008-01-22 06:54:57 +08:00
|
|
|
addLLVMField(Ty);
|
|
|
|
CGT.addFieldInfo(FD, llvmFieldNo - 1);
|
|
|
|
Cursor = llvmSize;
|
2007-11-07 09:57:13 +08:00
|
|
|
}
|
2007-10-23 10:10:49 +08:00
|
|
|
}
|
2007-12-11 08:49:18 +08:00
|
|
|
|
2008-01-22 06:54:57 +08:00
|
|
|
unsigned StructAlign = RL.getAlignment();
|
|
|
|
if (llvmSize % StructAlign) {
|
|
|
|
unsigned StructPadding = StructAlign - (llvmSize % StructAlign);
|
|
|
|
addPaddingFields(llvmSize + StructPadding);
|
|
|
|
}
|
2007-12-11 08:49:18 +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.
|
2007-12-22 02:43:53 +08:00
|
|
|
void RecordOrganizer::addPaddingFields(unsigned WaterMark) {
|
2008-01-22 06:54:57 +08:00
|
|
|
unsigned RequiredBits = WaterMark - llvmSize;
|
|
|
|
unsigned RequiredBytes = (RequiredBits + 7) / 8;
|
2007-11-01 08:07:12 +08:00
|
|
|
for (unsigned i = 0; i != RequiredBytes; ++i)
|
2008-01-22 06:54:57 +08:00
|
|
|
addLLVMField(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.
|
2008-01-22 06:54:57 +08:00
|
|
|
/// Increment field count.
|
|
|
|
void RecordOrganizer::addLLVMField(const llvm::Type *Ty) {
|
2007-11-07 09:57:13 +08:00
|
|
|
|
|
|
|
unsigned AlignmentInBits = CGT.getTargetData().getABITypeAlignment(Ty) * 8;
|
2008-01-22 06:54:57 +08:00
|
|
|
if (llvmSize % AlignmentInBits) {
|
2007-11-07 09:57:13 +08:00
|
|
|
// 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.
|
2008-01-22 06:54:57 +08:00
|
|
|
unsigned Padding = AlignmentInBits - (llvmSize % AlignmentInBits);
|
|
|
|
addPaddingFields(llvmSize + Padding);
|
|
|
|
}
|
2007-11-07 09:57:13 +08:00
|
|
|
|
2008-01-22 06:54:57 +08:00
|
|
|
unsigned TySize = CGT.getTargetData().getABITypeSizeInBits(Ty);
|
|
|
|
Offsets.push_back(llvmSize);
|
|
|
|
llvmSize += TySize;
|
2007-11-01 07:17:19 +08:00
|
|
|
LLVMFields.push_back(Ty);
|
2008-01-22 06:54:57 +08:00
|
|
|
++llvmFieldNo;
|
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());
|
2008-01-22 06:54:57 +08:00
|
|
|
CGT.addFieldInfo(FieldDecls[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.
|
2008-01-22 06:54:57 +08:00
|
|
|
CGT.addFieldInfo(FD, 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);
|
|
|
|
}
|
|
|
|
|
2008-01-22 06:54:57 +08:00
|
|
|
/// placeBitField - Find a place for FD, which is a bit-field.
|
|
|
|
/// This function searches for the last aligned field. If the bit-field fits in
|
|
|
|
/// it, it is reused. Otherwise, the bit-field is placed in a new field.
|
2007-12-12 03:51:39 +08:00
|
|
|
void RecordOrganizer::placeBitField(const FieldDecl *FD) {
|
|
|
|
|
|
|
|
assert (FD->isBitField() && "FD is not a bit-field");
|
|
|
|
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();
|
2008-01-22 06:54:57 +08:00
|
|
|
|
|
|
|
bool FoundPrevField = false;
|
|
|
|
unsigned TotalOffsets = Offsets.size();
|
|
|
|
const llvm::Type *Ty = CGT.ConvertType(FD->getType());
|
|
|
|
uint64_t TySize = CGT.getTargetData().getABITypeSizeInBits(Ty);
|
|
|
|
|
|
|
|
if (!TotalOffsets) {
|
|
|
|
// Special case: the first field.
|
|
|
|
CGT.addFieldInfo(FD, llvmFieldNo);
|
|
|
|
CGT.addBitFieldInfo(FD, 0, BitFieldSize);
|
|
|
|
addPaddingFields(BitFieldSize);
|
|
|
|
Cursor = BitFieldSize;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for the last aligned field.
|
|
|
|
for (unsigned i = TotalOffsets; i != 0; --i) {
|
|
|
|
uint64_t O = Offsets[i - 1];
|
|
|
|
if (O % TySize == 0) {
|
|
|
|
FoundPrevField = true;
|
|
|
|
if (TySize - (Cursor - O) >= BitFieldSize) {
|
|
|
|
// The bitfield fits in the last aligned field.
|
|
|
|
// This is : struct { char a; int CurrentField:10;};
|
|
|
|
// where 'CurrentField' shares first field with 'a'.
|
|
|
|
addPaddingFields(Cursor + BitFieldSize);
|
2008-01-23 03:15:30 +08:00
|
|
|
CGT.addFieldInfo(FD, i - 1);
|
|
|
|
CGT.addBitFieldInfo(FD, Cursor - O, BitFieldSize);
|
2008-01-22 06:54:57 +08:00
|
|
|
Cursor += BitFieldSize;
|
|
|
|
} else {
|
|
|
|
// Place the bitfield in a new LLVM field.
|
|
|
|
// This is : struct { char a; short CurrentField:10;};
|
|
|
|
// where 'CurrentField' needs a new llvm field.
|
|
|
|
addPaddingFields(O + TySize);
|
|
|
|
CGT.addFieldInfo(FD, llvmFieldNo);
|
|
|
|
CGT.addBitFieldInfo(FD, 0, BitFieldSize);
|
|
|
|
addPaddingFields(O + TySize + BitFieldSize);
|
|
|
|
Cursor = O + TySize + BitFieldSize;
|
2007-12-12 03:51:39 +08:00
|
|
|
}
|
2008-01-22 06:54:57 +08:00
|
|
|
break;
|
2007-12-12 03:51:39 +08:00
|
|
|
}
|
|
|
|
}
|
2008-01-22 06:54:57 +08:00
|
|
|
|
|
|
|
assert(FoundPrevField &&
|
|
|
|
"Unable to find a place for bitfield in struct layout");
|
2007-12-12 03:51:39 +08:00
|
|
|
}
|