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.
|
2008-02-05 11:17:42 +08:00
|
|
|
/// FIXME : Handle field aligments. Handle packed structs.
|
2007-10-25 04:38:06 +08:00
|
|
|
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.
|
2008-02-05 09:40:48 +08:00
|
|
|
void addLLVMField(const llvm::Type *Ty, bool isPaddingField = false);
|
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);
|
|
|
|
|
2008-02-05 10:39:50 +08:00
|
|
|
llvm::SmallSet<unsigned, 8> &getPaddingFields() {
|
2008-02-05 09:40:48 +08:00
|
|
|
return PaddingFields;
|
|
|
|
}
|
|
|
|
|
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;
|
2008-02-05 10:39:50 +08:00
|
|
|
llvm::SmallSet<unsigned, 8> PaddingFields;
|
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() {
|
2008-02-05 14:55:31 +08:00
|
|
|
for(llvm::DenseMap<const TagDecl *, CGRecordLayout *>::iterator
|
2007-11-02 03:11:01 +08:00
|
|
|
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
|
2008-02-06 14:03:51 +08:00
|
|
|
I = TypeCache.find(T.getCanonicalType().getTypePtr());
|
2007-12-22 03:35:28 +08:00
|
|
|
// If type is found in map and this is not a definition for a opaque
|
2008-02-06 13:29:46 +08:00
|
|
|
// place holder type then use it. Otherwise, convert type T.
|
2008-02-06 13:21:55 +08:00
|
|
|
if (I != TypeCache.end())
|
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);
|
2008-02-06 14:03:51 +08:00
|
|
|
TypeCache.insert(std::make_pair(T.getCanonicalType().getTypePtr(),
|
2008-02-06 13:21:55 +08:00
|
|
|
llvm::PATypeHolder(ResultType)));
|
2007-10-26 02:32:36 +08:00
|
|
|
return ResultType;
|
|
|
|
}
|
|
|
|
|
2008-02-06 13:21:55 +08:00
|
|
|
/// ConvertTypeForMem - Convert type T into a llvm::Type. 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.
|
2008-01-10 02:47:25 +08:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-02-06 13:08:19 +08:00
|
|
|
/// UpdateCompletedType - When we find the full definition for a TagDecl,
|
|
|
|
/// replace the 'opaque' type we previously made for it if applicable.
|
|
|
|
void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) {
|
2008-02-05 16:06:13 +08:00
|
|
|
llvm::DenseMap<const TagDecl*, llvm::PATypeHolder>::iterator TDTI =
|
2008-02-06 13:12:09 +08:00
|
|
|
TagDeclTypes.find(TD);
|
|
|
|
if (TDTI == TagDeclTypes.end()) return;
|
|
|
|
|
|
|
|
// Remember the opaque LLVM type for this tagdecl.
|
2008-02-05 16:06:13 +08:00
|
|
|
llvm::PATypeHolder OpaqueHolder = TDTI->second;
|
|
|
|
assert(isa<llvm::OpaqueType>(OpaqueHolder.get()) &&
|
2008-02-06 13:12:09 +08:00
|
|
|
"Updating compilation of an already non-opaque type?");
|
2008-02-05 16:06:13 +08:00
|
|
|
|
|
|
|
// Remove it from TagDeclTypes so that it will be regenerated.
|
|
|
|
TagDeclTypes.erase(TDTI);
|
|
|
|
|
2008-02-06 14:06:49 +08:00
|
|
|
// Generate the new type.
|
|
|
|
const llvm::Type *NT = ConvertTagDeclType(TD);
|
2008-02-05 16:06:13 +08:00
|
|
|
|
2008-02-06 14:06:49 +08:00
|
|
|
// Refine the old opaque type to its new definition.
|
|
|
|
cast<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NT);
|
2008-02-05 16:06:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-10 02:47:25 +08:00
|
|
|
|
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);
|
2008-02-04 10:31:56 +08:00
|
|
|
QualType ETy = P.getPointeeType();
|
|
|
|
return llvm::PointerType::get(ConvertType(ETy), ETy.getAddressSpace());
|
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);
|
2008-02-16 02:16:39 +08:00
|
|
|
assert(A.getIndexTypeQualifier() == 0 &&
|
2007-06-16 08:12:05 +08:00
|
|
|
"FIXME: We only handle trivial array types so far!");
|
2008-02-16 02:16:39 +08:00
|
|
|
// VLAs resolve to the innermost element type; this matches
|
|
|
|
// the return of alloca, and there isn't any obviously better choice.
|
|
|
|
return ConvertType(A.getElementType());
|
|
|
|
}
|
|
|
|
case Type::IncompleteArray: {
|
|
|
|
const IncompleteArrayType &A = cast<IncompleteArrayType>(Ty);
|
|
|
|
assert(A.getIndexTypeQualifier() == 0 &&
|
|
|
|
"FIXME: We only handle trivial array types so far!");
|
|
|
|
// int X[] -> [0 x int]
|
|
|
|
return llvm::ArrayType::get(ConvertType(A.getElementType()), 0);
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
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) {
|
2008-02-06 13:29:46 +08:00
|
|
|
ArgTys.push_back(llvm::PointerType::get(ResultType,
|
|
|
|
FP.getResultType().getAddressSpace()));
|
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
|
|
|
}
|
2008-02-04 10:31:56 +08:00
|
|
|
|
|
|
|
case Type::ASQual:
|
2008-02-21 04:55:12 +08:00
|
|
|
return ConvertType(QualType(cast<ASQualType>(Ty).getBaseType(), 0));
|
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;
|
|
|
|
|
2008-02-06 13:48:29 +08:00
|
|
|
case Type::Tagged: {
|
|
|
|
const TagDecl *TD = cast<TagType>(Ty).getDecl();
|
2008-02-06 14:06:49 +08:00
|
|
|
const llvm::Type *Res = ConvertTagDeclType(TD);
|
2008-02-06 13:48:29 +08:00
|
|
|
|
|
|
|
std::string TypeName(TD->getKindName());
|
|
|
|
TypeName += '.';
|
|
|
|
|
|
|
|
// Name the codegen type after the typedef name
|
|
|
|
// if there is no tag type name available
|
|
|
|
if (TD->getIdentifier())
|
|
|
|
TypeName += TD->getName();
|
|
|
|
else if (const TypedefType *TdT = dyn_cast<TypedefType>(T))
|
|
|
|
TypeName += TdT->getDecl()->getName();
|
|
|
|
else
|
|
|
|
TypeName += "anon";
|
|
|
|
|
|
|
|
TheModule.addTypeName(TypeName, Res);
|
|
|
|
return Res;
|
|
|
|
}
|
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);
|
2008-02-06 13:29:46 +08:00
|
|
|
else
|
|
|
|
// byval arguments are always on the stack, which is addr space #0.
|
|
|
|
ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
|
2007-06-16 08:12:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-06 13:18:32 +08:00
|
|
|
/// ConvertTagDeclType - Lay out a tagged decl type like struct or union or
|
|
|
|
/// enum.
|
2008-02-06 14:06:49 +08:00
|
|
|
const llvm::Type *CodeGenTypes::ConvertTagDeclType(const TagDecl *TD) {
|
2008-02-06 13:18:32 +08:00
|
|
|
llvm::DenseMap<const TagDecl*, llvm::PATypeHolder>::iterator TDTI =
|
|
|
|
TagDeclTypes.find(TD);
|
|
|
|
|
2008-02-06 14:03:51 +08:00
|
|
|
// If we've already compiled this tag type, use the previous definition.
|
|
|
|
if (TDTI != TagDeclTypes.end())
|
2008-02-06 13:18:32 +08:00
|
|
|
return TDTI->second;
|
|
|
|
|
2008-02-06 14:03:51 +08:00
|
|
|
// If this is still a forward definition, just define an opaque type to use
|
|
|
|
// for this tagged decl.
|
2008-02-06 13:18:32 +08:00
|
|
|
if (!TD->isDefinition()) {
|
2008-02-06 14:03:51 +08:00
|
|
|
llvm::Type *ResultType = llvm::OpaqueType::get();
|
2008-02-06 13:18:32 +08:00
|
|
|
TagDeclTypes.insert(std::make_pair(TD, ResultType));
|
2008-02-06 14:03:51 +08:00
|
|
|
return ResultType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, this is a definition of a type. Compile the implementation now.
|
|
|
|
|
|
|
|
if (TD->getKind() == Decl::Enum) {
|
2008-02-06 13:18:32 +08:00
|
|
|
// Don't bother storing enums in TagDeclTypes.
|
|
|
|
return ConvertType(cast<EnumDecl>(TD)->getIntegerType());
|
2008-02-06 14:03:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This decl could well be recursive. In this case, insert an opaque
|
|
|
|
// definition of this type, which the recursive uses will get. We will then
|
|
|
|
// refine this opaque version later.
|
|
|
|
|
|
|
|
// Create new OpaqueType now for later use in case this is a recursive
|
|
|
|
// type. This will later be refined to the actual type.
|
|
|
|
llvm::PATypeHolder ResultHolder = llvm::OpaqueType::get();
|
|
|
|
TagDeclTypes.insert(std::make_pair(TD, ResultHolder));
|
|
|
|
|
|
|
|
const llvm::Type *ResultType;
|
|
|
|
const RecordDecl *RD = cast<const RecordDecl>(TD);
|
|
|
|
if (TD->getKind() == Decl::Struct || TD->getKind() == Decl::Class) {
|
2008-02-06 13:18:32 +08:00
|
|
|
// Layout fields.
|
|
|
|
RecordOrganizer RO(*this);
|
|
|
|
for (unsigned i = 0, e = RD->getNumMembers(); i != e; ++i)
|
|
|
|
RO.addField(RD->getMember(i));
|
|
|
|
|
2008-02-06 14:03:51 +08:00
|
|
|
RO.layoutStructFields(Context.getASTRecordLayout(RD, SourceLocation()));
|
2008-02-06 13:18:32 +08:00
|
|
|
|
2008-02-06 14:03:51 +08:00
|
|
|
// Get llvm::StructType.
|
|
|
|
CGRecordLayouts[TD] = new CGRecordLayout(RO.getLLVMType(),
|
|
|
|
RO.getPaddingFields());
|
|
|
|
ResultType = RO.getLLVMType();
|
2008-02-06 13:18:32 +08:00
|
|
|
|
|
|
|
} else if (TD->getKind() == Decl::Union) {
|
|
|
|
// Just use the largest element of the union, breaking ties with the
|
|
|
|
// highest aligned member.
|
|
|
|
if (RD->getNumMembers() != 0) {
|
|
|
|
RecordOrganizer RO(*this);
|
|
|
|
for (unsigned i = 0, e = RD->getNumMembers(); i != e; ++i)
|
|
|
|
RO.addField(RD->getMember(i));
|
2008-02-06 14:03:51 +08:00
|
|
|
|
|
|
|
RO.layoutUnionFields();
|
|
|
|
|
|
|
|
// Get llvm::StructType.
|
|
|
|
CGRecordLayouts[TD] = new CGRecordLayout(RO.getLLVMType(),
|
|
|
|
RO.getPaddingFields());
|
|
|
|
ResultType = RO.getLLVMType();
|
2008-02-06 13:18:32 +08:00
|
|
|
} else {
|
2008-02-06 14:03:51 +08:00
|
|
|
ResultType = llvm::StructType::get(std::vector<const llvm::Type*>());
|
2008-02-06 13:18:32 +08:00
|
|
|
}
|
|
|
|
} else {
|
2008-02-06 13:29:46 +08:00
|
|
|
assert(0 && "FIXME: Unknown tag decl kind!");
|
2008-02-06 13:18:32 +08:00
|
|
|
}
|
|
|
|
|
2008-02-06 14:03:51 +08:00
|
|
|
// Refine our Opaque type to ResultType. This can invalidate ResultType, so
|
|
|
|
// make sure to read the result out of the holder.
|
|
|
|
cast<llvm::OpaqueType>(ResultHolder.get())
|
|
|
|
->refineAbstractTypeTo(ResultType);
|
|
|
|
|
|
|
|
return ResultHolder.get();
|
2008-02-06 13:18:32 +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 *
|
2008-02-05 14:55:31 +08:00
|
|
|
CodeGenTypes::getCGRecordLayout(const TagDecl *TD) const {
|
|
|
|
llvm::DenseMap<const TagDecl*, CGRecordLayout *>::iterator I
|
|
|
|
= CGRecordLayouts.find(TD);
|
2007-11-02 03:11:01 +08:00
|
|
|
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
|
|
|
|
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-02-08 02:18:58 +08:00
|
|
|
assert(WaterMark >= llvmSize && "Invalid padding Field");
|
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-02-05 09:40:48 +08:00
|
|
|
addLLVMField(llvm::Type::Int8Ty, true);
|
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.
|
2008-02-05 09:40:48 +08:00
|
|
|
void RecordOrganizer::addLLVMField(const llvm::Type *Ty, bool isPaddingField) {
|
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);
|
|
|
|
llvmSize += TySize;
|
2008-02-05 09:40:48 +08:00
|
|
|
if (isPaddingField)
|
2008-02-05 10:39:50 +08:00
|
|
|
PaddingFields.insert(llvmFieldNo);
|
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
|
|
|
|
|
|
|
const llvm::Type *Ty = CGT.ConvertType(FD->getType());
|
|
|
|
uint64_t TySize = CGT.getTargetData().getABITypeSizeInBits(Ty);
|
|
|
|
|
2008-02-08 03:29:53 +08:00
|
|
|
unsigned Idx = Cursor / TySize;
|
|
|
|
unsigned BitsLeft = TySize - (Cursor % TySize);
|
2008-01-22 06:54:57 +08:00
|
|
|
|
2008-02-08 03:29:53 +08:00
|
|
|
if (BitsLeft >= BitFieldSize) {
|
|
|
|
// The bitfield fits in the last aligned field.
|
|
|
|
// This is : struct { char a; int CurrentField:10;};
|
|
|
|
// where 'CurrentField' shares first field with 'a'.
|
|
|
|
CGT.addFieldInfo(FD, Idx);
|
|
|
|
CGT.addBitFieldInfo(FD, TySize - BitsLeft, BitFieldSize);
|
|
|
|
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.
|
|
|
|
CGT.addFieldInfo(FD, Idx + 1);
|
|
|
|
CGT.addBitFieldInfo(FD, 0, BitFieldSize);
|
|
|
|
Cursor = (Idx + 1) * TySize + BitFieldSize;
|
|
|
|
}
|
2008-02-09 03:46:46 +08:00
|
|
|
if (Cursor > llvmSize)
|
|
|
|
addPaddingFields(Cursor);
|
2007-12-12 03:51:39 +08:00
|
|
|
}
|