forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirtype.cpp
219 lines (171 loc) · 6.04 KB
/
irtype.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//===-- irtype.cpp --------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "mars.h"
#include "mtype.h"
#include "gen/irstate.h"
#include "gen/logger.h"
#include "gen/llvmhelpers.h"
#include "gen/tollvm.h"
#include "ir/irtype.h"
// These functions use getGlobalContext() as they are invoked before gIR
// is set.
IrType::IrType(Type *dt, LLType *lt) : dtype(dt), type(lt) {
assert(dt && "null D Type");
assert(lt && "null LLVM Type");
assert(!dt->ctype && "already has IrType");
}
IrFuncTy &IrType::getIrFuncTy() {
llvm_unreachable("cannot get IrFuncTy from non lazy/function/delegate");
}
//////////////////////////////////////////////////////////////////////////////
IrTypeBasic::IrTypeBasic(Type *dt) : IrType(dt, basic2llvm(dt)) {}
IrTypeBasic *IrTypeBasic::get(Type *dt) {
auto t = new IrTypeBasic(dt);
dt->ctype = t;
return t;
}
LLType *IrTypeBasic::getComplexType(llvm::LLVMContext &ctx, LLType *type) {
llvm::Type *types[] = {type, type};
return llvm::StructType::get(ctx, types, false);
}
namespace {
llvm::Type *getReal80Type(llvm::LLVMContext &ctx) {
llvm::Triple::ArchType const a = global.params.targetTriple->getArch();
bool const anyX86 = (a == llvm::Triple::x86) || (a == llvm::Triple::x86_64);
bool const anyAarch64 = (a == llvm::Triple::aarch64) || (a == llvm::Triple::aarch64_be)
#if LDC_LLVM_VER == 305
|| (a == llvm::Triple::arm64) || (a == llvm::Triple::arm64_be)
#endif
;
// only x86 has 80bit float - but no support with MS C Runtime!
if (anyX86 && !global.params.targetTriple->isWindowsMSVCEnvironment()) {
return llvm::Type::getX86_FP80Ty(ctx);
}
if (anyAarch64) {
return llvm::Type::getFP128Ty(ctx);
}
return llvm::Type::getDoubleTy(ctx);
}
}
llvm::Type *IrTypeBasic::basic2llvm(Type *t) {
llvm::LLVMContext &ctx = getGlobalContext();
switch (t->ty) {
case Tvoid:
return llvm::Type::getVoidTy(ctx);
case Tint8:
case Tuns8:
case Tchar:
return llvm::Type::getInt8Ty(ctx);
case Tint16:
case Tuns16:
case Twchar:
return llvm::Type::getInt16Ty(ctx);
case Tint32:
case Tuns32:
case Tdchar:
return llvm::Type::getInt32Ty(ctx);
case Tint64:
case Tuns64:
return llvm::Type::getInt64Ty(ctx);
case Tint128:
case Tuns128:
return llvm::IntegerType::get(ctx, 128);
case Tfloat32:
case Timaginary32:
return llvm::Type::getFloatTy(ctx);
case Tfloat64:
case Timaginary64:
return llvm::Type::getDoubleTy(ctx);
case Tfloat80:
case Timaginary80:
return getReal80Type(ctx);
case Tcomplex32:
return getComplexType(ctx, llvm::Type::getFloatTy(ctx));
case Tcomplex64:
return getComplexType(ctx, llvm::Type::getDoubleTy(ctx));
case Tcomplex80:
return getComplexType(ctx, getReal80Type(ctx));
case Tbool:
return llvm::Type::getInt1Ty(ctx);
default:
llvm_unreachable("Unknown basic type.");
}
}
//////////////////////////////////////////////////////////////////////////////
IrTypePointer::IrTypePointer(Type *dt, LLType *lt) : IrType(dt, lt) {}
IrTypePointer *IrTypePointer::get(Type *dt) {
assert(!dt->ctype);
assert((dt->ty == Tpointer || dt->ty == Tnull) && "not pointer/null type");
LLType *elemType;
if (dt->ty == Tnull) {
elemType = llvm::Type::getInt8Ty(getGlobalContext());
} else {
elemType = DtoMemType(dt->nextOf());
// DtoType could have already created the same type, e.g. for
// dt == Node* in struct Node { Node* n; }.
if (dt->ctype) {
return dt->ctype->isPointer();
}
}
auto t = new IrTypePointer(dt, llvm::PointerType::get(elemType, 0));
dt->ctype = t;
return t;
}
//////////////////////////////////////////////////////////////////////////////
IrTypeSArray::IrTypeSArray(Type *dt, LLType *lt) : IrType(dt, lt) {}
IrTypeSArray *IrTypeSArray::get(Type *dt) {
assert(!dt->ctype);
assert(dt->ty == Tsarray && "not static array type");
LLType *elemType = DtoMemType(dt->nextOf());
// We might have already built the type during DtoMemType e.g. as part of a
// forward reference in a struct.
if (!dt->ctype) {
TypeSArray *tsa = static_cast<TypeSArray *>(dt);
uint64_t dim = static_cast<uint64_t>(tsa->dim->toUInteger());
dt->ctype = new IrTypeSArray(dt, llvm::ArrayType::get(elemType, dim));
}
return dt->ctype->isSArray();
}
//////////////////////////////////////////////////////////////////////////////
IrTypeArray::IrTypeArray(Type *dt, LLType *lt) : IrType(dt, lt) {}
IrTypeArray *IrTypeArray::get(Type *dt) {
assert(!dt->ctype);
assert(dt->ty == Tarray && "not dynamic array type");
LLType *elemType = DtoMemType(dt->nextOf());
// Could have already built the type as part of a struct forward reference,
// just as for pointers.
if (!dt->ctype) {
llvm::Type *types[] = {DtoSize_t(), llvm::PointerType::get(elemType, 0)};
LLType *at = llvm::StructType::get(getGlobalContext(), types, false);
dt->ctype = new IrTypeArray(dt, at);
}
return dt->ctype->isArray();
}
//////////////////////////////////////////////////////////////////////////////
IrTypeVector::IrTypeVector(Type *dt, llvm::Type *lt) : IrType(dt, lt) {}
IrTypeVector *IrTypeVector::get(Type *dt) {
LLType *lt = vector2llvm(dt);
// Could have already built the type as part of a struct forward reference,
// just as for pointers and arrays.
if (!dt->ctype) {
dt->ctype = new IrTypeVector(dt, lt);
}
return dt->ctype->isVector();
}
llvm::Type *IrTypeVector::vector2llvm(Type *dt) {
assert(dt->ty == Tvector && "not vector type");
TypeVector *tv = static_cast<TypeVector *>(dt);
assert(tv->basetype->ty == Tsarray);
TypeSArray *tsa = static_cast<TypeSArray *>(tv->basetype);
uint64_t dim = static_cast<uint64_t>(tsa->dim->toUInteger());
LLType *elemType = DtoMemType(tsa->next);
return llvm::VectorType::get(elemType, dim);
}