Skip to content

Commit cab802d

Browse files
committed
[LLVMABI] Mapper Interface
1 parent 0858b1f commit cab802d

File tree

5 files changed

+129
-78
lines changed

5 files changed

+129
-78
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef LLVM_ABI_QUALTYPE_MAPPER_H
2+
#define LLVM_ABI_QUALTYPE_MAPPER_H
3+
4+
#include "llvm/Support/Allocator.h"
5+
#include <clang/AST/ASTContext.h>
6+
#include <clang/AST/Type.h>
7+
#include <llvm/ABI/Types.h>
8+
#include <llvm/ADT/DenseMap.h>
9+
10+
namespace llvm {
11+
namespace abi {
12+
13+
class QualTypeMapper {
14+
private:
15+
clang::ASTContext &ASTCtx;
16+
TypeBuilder Builder;
17+
18+
// llvm::DenseMap<clang::QualType , const Type*> TypeCache;
19+
20+
const Type *convertBuiltinType(const clang::BuiltinType *BT);
21+
const Type *convertPointerType(const clang::PointerType *PT);
22+
const Type *convertArrayType(const clang::ArrayType *AT);
23+
const Type *convertVectorType(const clang::VectorType *VT);
24+
const Type *convertRecordType(const clang::RecordType *RT);
25+
const Type *convertFunctionType(const clang::FunctionProtoType *FT);
26+
const Type *convertEnumType(const clang::EnumType *ET);
27+
28+
void computeRecordLayout(const clang::RecordDecl *RD,
29+
llvm::SmallVectorImpl<FieldInfo> &Fields,
30+
uint64_t &TotalSize, uint64_t &Alignment,
31+
StructPacking &Packing);
32+
33+
34+
uint64_t getTypeSize(clang::QualType QT) const;
35+
uint64_t getTypeAlign(clang::QualType QT) const;
36+
uint64_t getPointerSize() const;
37+
uint64_t getPointerAlign() const;
38+
39+
40+
public:
41+
42+
explicit QualTypeMapper(clang::ASTContext &Ctx, BumpPtrAllocator &Alloc)
43+
: ASTCtx(Ctx), Builder(Alloc) {}
44+
45+
const Type *convertType(clang::QualType QT);
46+
47+
// void clearCache() {TypeCache.clear();}
48+
49+
TypeBuilder getTypeBuilder() {return Builder;}
50+
51+
};
52+
53+
54+
55+
} // namespace abi
56+
} // namespace llvm
57+
58+
#endif // !LLVM_ABI_QUALTYPE_MAPPER_H

llvm/include/llvm/ABI/Types.h

Lines changed: 35 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#ifndef LLVM_ABI_TYPES_H
22
#define LLVM_ABI_TYPES_H
33

4+
#include "llvm/ADT/APFloat.h"
45
#include "llvm/ADT/ArrayRef.h"
6+
#include <llvm/IR/CallingConv.h>
7+
#include "llvm/Support/Alignment.h"
58
#include "llvm/Support/Allocator.h"
9+
#include "llvm/Support/TypeSize.h"
610
#include <cstdint>
711

812
namespace llvm {
@@ -17,27 +21,26 @@ enum class TypeKind {
1721
Vector,
1822
Struct,
1923
Union,
20-
Function
2124
};
2225

2326
class Type {
2427
protected:
2528
TypeKind Kind;
26-
uint64_t SizeInBits;
27-
uint64_t AlignInBits;
29+
TypeSize SizeInBits;
30+
Align AlignInBits;
2831
bool IsExplicitlyAligned;
2932

30-
Type(TypeKind K, uint64_t Size, uint64_t Align, bool ExplicitAlign = false)
33+
Type(TypeKind K, TypeSize Size, Align Align, bool ExplicitAlign = false)
3134
: Kind(K), SizeInBits(Size), AlignInBits(Align),
3235
IsExplicitlyAligned(ExplicitAlign) {}
3336

3437
public:
3538
TypeKind getKind() const { return Kind; }
36-
uint64_t getSizeInBits() const { return SizeInBits; }
37-
uint64_t getAlignInBits() const { return AlignInBits; }
39+
TypeSize getSizeInBits() const { return SizeInBits; }
40+
Align getAlignInBits() const { return AlignInBits; }
3841
bool hasExplicitAlignment() const { return IsExplicitlyAligned; }
3942

40-
void setExplicitAlignment(uint64_t Align) {
43+
void setExplicitAlignment(Align Align) {
4144
AlignInBits = Align;
4245
IsExplicitlyAligned = true;
4346
}
@@ -50,12 +53,11 @@ class Type {
5053
bool isVector() const { return Kind == TypeKind::Vector; }
5154
bool isStruct() const { return Kind == TypeKind::Struct; }
5255
bool isUnion() const { return Kind == TypeKind::Union; }
53-
bool isFunction() const { return Kind == TypeKind::Function; }
5456
};
5557

5658
class VoidType : public Type {
5759
public:
58-
VoidType() : Type(TypeKind::Void, 0, 0) {}
60+
VoidType() : Type(TypeKind::Void, TypeSize::getFixed(0), Align(1)) {}
5961

6062
static bool classof(const Type *T) { return T->getKind() == TypeKind::Void; }
6163
};
@@ -65,8 +67,8 @@ class IntegerType : public Type {
6567
bool IsSigned;
6668

6769
public:
68-
IntegerType(uint64_t BitWidth, uint64_t Align, bool Signed)
69-
: Type(TypeKind::Integer, BitWidth, Align), IsSigned(Signed) {}
70+
IntegerType(uint64_t BitWidth, Align Align, bool Signed)
71+
: Type(TypeKind::Integer, TypeSize::getFixed(BitWidth), Align), IsSigned(Signed) {}
7072

7173
bool isSigned() const { return IsSigned; }
7274

@@ -76,17 +78,21 @@ class IntegerType : public Type {
7678
};
7779

7880
class FloatType : public Type {
81+
private:
82+
const fltSemantics *Semantics;
83+
7984
public:
80-
FloatType(uint64_t BitWidth, uint64_t Align)
81-
: Type(TypeKind::Float, BitWidth, Align) {}
85+
FloatType(const fltSemantics &FloatSemantics, Align Align)
86+
: Type(TypeKind::Float, TypeSize::getFixed(APFloat::getSizeInBits(FloatSemantics)), Align)
87+
, Semantics(&FloatSemantics){}
8288

8389
static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
8490
};
8591

8692
class PointerType : public Type {
8793
public:
88-
PointerType(uint64_t Size, uint64_t Align)
89-
: Type(TypeKind::Pointer, Size, Align) {}
94+
PointerType(uint64_t Size, Align Align)
95+
: Type(TypeKind::Pointer, TypeSize::getFixed(Size), Align) {}
9096

9197
static bool classof(const Type *T) {
9298
return T->getKind() == TypeKind::Pointer;
@@ -116,7 +122,7 @@ class VectorType : public Type {
116122
uint64_t NumElements;
117123

118124
public:
119-
VectorType(const Type *ElemType, uint64_t NumElems, uint64_t Align)
125+
VectorType(const Type *ElemType, uint64_t NumElems, Align Align)
120126
: Type(TypeKind::Vector, ElemType->getSizeInBits() * NumElems, Align),
121127
ElementType(ElemType), NumElements(NumElems) {}
122128

@@ -149,8 +155,8 @@ class StructType : public Type {
149155
StructPacking Packing;
150156

151157
public:
152-
StructType(const FieldInfo *StructFields, uint32_t FieldCount, uint64_t Size,
153-
uint64_t Align, StructPacking Pack = StructPacking::Default)
158+
StructType(const FieldInfo *StructFields, uint32_t FieldCount, TypeSize Size,
159+
Align Align, StructPacking Pack = StructPacking::Default)
154160
: Type(TypeKind::Struct, Size, Align), Fields(StructFields),
155161
NumFields(FieldCount), Packing(Pack) {}
156162

@@ -170,8 +176,8 @@ class UnionType : public Type {
170176
StructPacking Packing;
171177

172178
public:
173-
UnionType(const FieldInfo *UnionFields, uint32_t FieldCount, uint64_t Size,
174-
uint64_t Align, StructPacking Pack = StructPacking::Default)
179+
UnionType(const FieldInfo *UnionFields, uint32_t FieldCount, TypeSize Size,
180+
Align Align, StructPacking Pack = StructPacking::Default)
175181
: Type(TypeKind::Union, Size, Align), Fields(UnionFields),
176182
NumFields(FieldCount), Packing(Pack) {}
177183

@@ -182,40 +188,7 @@ class UnionType : public Type {
182188
static bool classof(const Type *T) { return T->getKind() == TypeKind::Union; }
183189
};
184190

185-
enum class CallConv {
186-
C,
187-
// TODO: extend for more CallConvs
188-
};
189-
190-
class FunctionType : public Type {
191-
private:
192-
const Type *ReturnType;
193-
const Type *const *ParameterTypes;
194-
uint32_t NumParams;
195-
bool IsVarArg;
196-
CallConv CC;
197-
198-
public:
199-
FunctionType(const Type *RetType, const Type *const *ParamTypes,
200-
uint32_t ParamCount, bool VarArgs, CallConv CallConv)
201-
: Type(TypeKind::Function, 0, 0), ReturnType(RetType),
202-
ParameterTypes(ParamTypes), NumParams(ParamCount), IsVarArg(VarArgs),
203-
CC(CallConv) {}
204-
205-
const Type *getReturnType() const { return ReturnType; }
206-
const Type *const *getParameterTypes() const { return ParameterTypes; }
207-
uint32_t getNumParameters() const { return NumParams; }
208-
const Type *getParameterType(uint32_t Index) const {
209-
assert(Index < NumParams && "Parameter index out of bounds");
210-
return ParameterTypes[Index];
211-
}
212-
bool isVarArg() const { return IsVarArg; }
213-
CallConv getCallingConv() const { return CC; }
214191

215-
static bool classof(const Type *T) {
216-
return T->getKind() == TypeKind::Function;
217-
}
218-
};
219192

220193
// API for creating ABI Types
221194
class TypeBuilder {
@@ -229,17 +202,17 @@ class TypeBuilder {
229202
return new (Allocator.Allocate<VoidType>()) VoidType();
230203
}
231204

232-
const IntegerType *getIntegerType(uint64_t BitWidth, uint64_t Align,
205+
const IntegerType *getIntegerType(uint64_t BitWidth, Align Align,
233206
bool Signed) {
234207
return new (Allocator.Allocate<IntegerType>())
235208
IntegerType(BitWidth, Align, Signed);
236209
}
237210

238-
const FloatType *getFloatType(uint64_t BitWidth, uint64_t Align) {
239-
return new (Allocator.Allocate<FloatType>()) FloatType(BitWidth, Align);
211+
const FloatType *getFloatType(const fltSemantics &Semantics, Align Align) {
212+
return new (Allocator.Allocate<FloatType>()) FloatType(Semantics, Align);
240213
}
241214

242-
const PointerType *getPointerType(uint64_t Size, uint64_t Align) {
215+
const PointerType *getPointerType(uint64_t Size, Align Align) {
243216
return new (Allocator.Allocate<PointerType>()) PointerType(Size, Align);
244217
}
245218

@@ -249,13 +222,13 @@ class TypeBuilder {
249222
}
250223

251224
const VectorType *getVectorType(const Type *ElementType, uint64_t NumElements,
252-
uint64_t Align) {
225+
Align Align) {
253226
return new (Allocator.Allocate<VectorType>())
254227
VectorType(ElementType, NumElements, Align);
255228
}
256229

257-
const StructType *getStructType(ArrayRef<FieldInfo> Fields, uint64_t Size,
258-
uint64_t Align,
230+
const StructType *getStructType(ArrayRef<FieldInfo> Fields, TypeSize Size,
231+
Align Align,
259232
StructPacking Pack = StructPacking::Default) {
260233
FieldInfo *FieldArray = Allocator.Allocate<FieldInfo>(Fields.size());
261234

@@ -267,8 +240,8 @@ class TypeBuilder {
267240
FieldArray, static_cast<uint32_t>(Fields.size()), Size, Align, Pack);
268241
}
269242

270-
const UnionType *getUnionType(ArrayRef<FieldInfo> Fields, uint64_t Size,
271-
uint64_t Align,
243+
const UnionType *getUnionType(ArrayRef<FieldInfo> Fields, TypeSize Size,
244+
Align Align,
272245
StructPacking Pack = StructPacking::Default) {
273246
FieldInfo *FieldArray = Allocator.Allocate<FieldInfo>(Fields.size());
274247

@@ -279,22 +252,6 @@ class TypeBuilder {
279252
return new (Allocator.Allocate<UnionType>()) UnionType(
280253
FieldArray, static_cast<uint32_t>(Fields.size()), Size, Align, Pack);
281254
}
282-
283-
const FunctionType *getFunctionType(const Type *ReturnType,
284-
ArrayRef<const Type *> ParamTypes,
285-
bool IsVarArg,
286-
CallConv CC = CallConv::C) {
287-
const Type **ParamArray =
288-
Allocator.Allocate<const Type *>(ParamTypes.size());
289-
290-
for (size_t I = 0; I < ParamTypes.size(); ++I) {
291-
ParamArray[I] = ParamTypes[I];
292-
}
293-
294-
return new (Allocator.Allocate<FunctionType>())
295-
FunctionType(ReturnType, ParamArray,
296-
static_cast<uint32_t>(ParamTypes.size()), IsVarArg, CC);
297-
}
298255
};
299256

300257
} // namespace abi

llvm/lib/ABI/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
add_llvm_component_library(LLVMABI
2+
QualTypeMapper.cpp
3+
4+
ADDITIONAL_HEADER_DIRS
5+
${LLVM_MAIN_INCLUDE_DIR}/llvm/ABI
6+
7+
DEPENDS
8+
intrinsics_gen
9+
10+
LINK_COMPONENTS
11+
Core
12+
Support
13+
)
14+
15+
target_include_directories(LLVMABI PRIVATE
16+
${LLVM_MAIN_INCLUDE_DIR}
17+
)

llvm/lib/ABI/QualTypeMapper.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- llvm/ABI/QualTypeMapper.cpp - QualType to ABI Mapping ---------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This File contains the logic for converting clang::Qualtype to
10+
// llvm::abi::Type for ABI Lowering
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
15+
#include <llvm/ABI/QualTypeMapper.h>
16+
17+
18+
// TODO: Implementation of Qualtype -> abi::Type Mapping

llvm/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ include(LLVM-Build)
33
# `Demangle', `Support' and `TableGen' libraries are added on the top-level
44
# CMakeLists.txt
55

6+
add_subdirectory(ABI)
67
add_subdirectory(IR)
78
add_subdirectory(FuzzMutate)
89
add_subdirectory(FileCheck)

0 commit comments

Comments
 (0)