forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstantBuilder.h
262 lines (226 loc) · 8.86 KB
/
ConstantBuilder.h
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//===--- ConstantBuilder.h - IR generation for constant structs -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements IR generation of constant packed LLVM structs.
//===----------------------------------------------------------------------===//
#include "swift/AST/Mangle.h"
#include "swift/ABI/MetadataValues.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Instructions.h"
#include "Address.h"
#include "IRGenModule.h"
#include "IRGenFunction.h"
namespace swift {
namespace irgen {
class ConstantBuilderBase {
protected:
IRGenModule &IGM;
ConstantBuilderBase(IRGenModule &IGM) : IGM(IGM) {}
};
template <class Base = ConstantBuilderBase>
class ConstantBuilder : public Base {
protected:
template <class... T>
ConstantBuilder(T &&...args) : Base(std::forward<T>(args)...) {}
IRGenModule &IGM = Base::IGM;
private:
llvm::GlobalVariable *relativeAddressBase = nullptr;
llvm::SmallVector<llvm::Constant*, 16> Fields;
Size NextOffset = Size(0);
protected:
Size getNextOffset() const { return NextOffset; }
void addStruct(llvm::Constant *st) {
assert(st->getType()->isStructTy());
Fields.push_back(st);
NextOffset += Size(IGM.DataLayout.getTypeStoreSize(st->getType()));
}
/// Add a constant word-sized value.
void addConstantWord(int64_t value) {
addWord(llvm::ConstantInt::get(IGM.SizeTy, value));
}
/// Add a word-sized value.
void addWord(llvm::Constant *value) {
assert(value->getType() == IGM.IntPtrTy ||
value->getType()->isPointerTy());
Fields.push_back(value);
NextOffset += IGM.getPointerSize();
}
void setRelativeAddressBase(llvm::GlobalVariable *base) {
relativeAddressBase = base;
}
llvm::Constant *getRelativeAddressFromNextField(ConstantReference referent,
llvm::IntegerType *addressTy) {
assert(relativeAddressBase && "no relative address base set");
// Determine the address of the next field in the initializer.
llvm::Constant *fieldAddr =
llvm::ConstantExpr::getPtrToInt(relativeAddressBase, IGM.IntPtrTy);
fieldAddr = llvm::ConstantExpr::getAdd(fieldAddr,
llvm::ConstantInt::get(IGM.SizeTy,
getNextOffset().getValue()));
llvm::Constant *referentValue =
llvm::ConstantExpr::getPtrToInt(referent.getValue(), IGM.IntPtrTy);
llvm::Constant *relative
= llvm::ConstantExpr::getSub(referentValue, fieldAddr);
if (relative->getType() != addressTy)
relative = llvm::ConstantExpr::getTrunc(relative, addressTy);
if (referent.isIndirect()) {
relative = llvm::ConstantExpr::getAdd(relative,
llvm::ConstantInt::get(addressTy, 1));
}
return relative;
}
/// Add a 32-bit relative address from the current location in the local
/// being built to another global variable.
void addRelativeAddress(llvm::Constant *referent) {
addRelativeAddress({referent, ConstantReference::Direct});
}
void addRelativeAddress(ConstantReference referent) {
addInt32(getRelativeAddressFromNextField(referent, IGM.RelativeAddressTy));
}
/// Add a pointer-sized relative address from the current location in the
/// local being built to another global variable.
void addFarRelativeAddress(llvm::Constant *referent) {
addFarRelativeAddress({referent, ConstantReference::Direct});
}
void addFarRelativeAddress(ConstantReference referent) {
addWord(getRelativeAddressFromNextField(referent,
IGM.FarRelativeAddressTy));
}
/// Add a 32-bit relative address from the current location in the local
/// being built to another global variable, or null if a null referent
/// is passed.
void addRelativeAddressOrNull(llvm::Constant *referent) {
addRelativeAddressOrNull({referent, ConstantReference::Direct});
}
void addRelativeAddressOrNull(ConstantReference referent) {
if (referent)
addRelativeAddress(referent);
else
addConstantInt32(0);
}
/// Add a pointer-sized relative address from the current location in the
/// local being built to another global variable, or null if a null referent
/// is passed.
void addFarRelativeAddressOrNull(llvm::Constant *referent) {
addFarRelativeAddressOrNull({referent, ConstantReference::Direct});
}
void addFarRelativeAddressOrNull(ConstantReference referent) {
if (referent)
addFarRelativeAddress(referent);
else
addConstantWord(0);
}
/// Add a 32-bit relative address from the current location in the local
/// being built to another global variable. Pack a constant integer into
/// the alignment bits of the pointer.
void addRelativeAddressWithTag(llvm::Constant *referent,
unsigned tag) {
assert(tag < 4 && "tag too big to pack in relative address");
llvm::Constant *relativeAddr =
getRelativeAddressFromNextField({referent, ConstantReference::Direct},
IGM.RelativeAddressTy);
relativeAddr = llvm::ConstantExpr::getAdd(relativeAddr,
llvm::ConstantInt::get(IGM.RelativeAddressTy, tag));
addInt32(relativeAddr);
}
/// Add a pointer-size relative address from the current location in the
/// local being built to another global variable. Pack a constant integer
/// into the alignment bits of the pointer.
void addFarRelativeAddressWithTag(llvm::Constant *referent,
unsigned tag) {
// FIXME: could be 8 when targeting 64-bit platforms
assert(tag < 4 && "tag too big to pack in relative address");
llvm::Constant *relativeAddr =
getRelativeAddressFromNextField(referent, IGM.FarRelativeAddressTy);
relativeAddr = llvm::ConstantExpr::getAdd(relativeAddr,
llvm::ConstantInt::get(IGM.FarRelativeAddressTy, tag));
addWord(relativeAddr);
}
/// Add a uint32_t value that represents the given offset
/// scaled to a number of words.
void addConstantInt32InWords(Size value) {
addConstantInt32(IGM.getOffsetInWords(value));
}
/// Add a constant 32-bit value.
void addConstantInt32(int32_t value) {
addInt32(llvm::ConstantInt::get(IGM.Int32Ty, value));
}
/// Add a 32-bit value.
void addInt32(llvm::Constant *value) {
assert(value->getType() == IGM.Int32Ty);
Fields.push_back(value);
NextOffset += Size(4);
}
/// Add a constant 16-bit value.
void addConstantInt16(int16_t value) {
addInt16(llvm::ConstantInt::get(IGM.Int16Ty, value));
}
/// Add a 16-bit value.
void addInt16(llvm::Constant *value) {
assert(value->getType() == IGM.Int16Ty);
Fields.push_back(value);
NextOffset += Size(2);
}
/// Add a constant 8-bit value.
void addConstantInt8(int8_t value) {
addInt8(llvm::ConstantInt::get(IGM.Int8Ty, value));
}
/// Add an 8-bit value.
void addInt8(llvm::Constant *value) {
assert(value->getType() == IGM.Int8Ty);
Fields.push_back(value);
NextOffset += Size(1);
}
/// Add a constant of the given size.
void addStruct(llvm::Constant *value, Size size) {
assert(size.getValue()
== IGM.DataLayout.getTypeStoreSize(value->getType()));
Fields.push_back(value);
NextOffset += size;
}
class ReservationToken {
size_t Index;
ReservationToken(size_t index) : Index(index) {}
friend ConstantBuilder<Base>;
};
ReservationToken reserveFields(unsigned numFields, Size size) {
unsigned index = Fields.size();
Fields.append(numFields, nullptr);
NextOffset += size;
return ReservationToken(index);
}
MutableArrayRef<llvm::Constant*> claimReservation(ReservationToken token,
unsigned numFields) {
return MutableArrayRef<llvm::Constant*>(&Fields[0] + token.Index,
numFields);
}
public:
llvm::Constant *getInit() const {
if (Fields.empty())
return nullptr;
return llvm::ConstantStruct::getAnon(Fields, /*packed*/ true);
}
/// An optimization of getInit for when we have a known type we
/// can use when there aren't any extra fields.
llvm::Constant *getInitWithSuggestedType(unsigned numFields,
llvm::StructType *type) {
if (Fields.size() == numFields) {
return llvm::ConstantStruct::get(type, Fields);
} else {
return getInit();
}
}
};
} // end namespace irgen
} // end namespace swift