-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathAddress.h
209 lines (175 loc) · 6.69 KB
/
Address.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
//===--- Address.h - Address Representation ---------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// A structure for holding the address of an object.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_ADDRESS_H
#define SWIFT_IRGEN_ADDRESS_H
#include "IRGen.h"
#include "llvm/ADT/ilist.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
namespace swift {
namespace irgen {
/// The address of an object in memory.
class Address {
llvm::Value *Addr;
llvm::Type *ElementType;
Alignment Align;
public:
Address() : Addr(nullptr) {}
Address(llvm::Value *addr, llvm::Type *elementType, Alignment align)
: Addr(addr), ElementType(elementType), Align(align) {
if (addr == llvm::DenseMapInfo<llvm::Value *>::getEmptyKey() ||
llvm::DenseMapInfo<llvm::Value *>::getTombstoneKey())
return;
assert(addr != nullptr && "building an invalid address");
}
llvm::Value *operator->() const {
assert(isValid());
return getAddress();
}
bool isValid() const { return Addr != nullptr; }
llvm::Value *getAddress() const { return Addr; }
Alignment getAlignment() const {
return Align;
}
llvm::PointerType *getType() const {
return cast<llvm::PointerType>(Addr->getType());
}
llvm::Type *getElementType() const { return ElementType; }
bool operator==(Address RHS) const {
return Addr == RHS.Addr && ElementType == RHS.ElementType &&
Align == RHS.Align;
}
bool operator!=(Address RHS) const { return !(*this == RHS); }
};
/// An address in memory together with the (possibly null) heap
/// allocation which owns it.
class OwnedAddress {
Address Addr;
llvm::Value *Owner;
public:
OwnedAddress() : Owner(nullptr) {}
OwnedAddress(Address address, llvm::Value *owner)
: Addr(address), Owner(owner) {}
llvm::Value *getAddressPointer() const { return Addr.getAddress(); }
Alignment getAlignment() const { return Addr.getAlignment(); }
Address getAddress() const { return Addr; }
llvm::Value *getOwner() const { return Owner; }
Address getUnownedAddress() const {
assert(getOwner() == nullptr);
return getAddress();
}
operator Address() const { return getAddress(); }
bool isValid() const { return Addr.isValid(); }
};
/// An address in memory together with the local allocation which owns it.
class ContainedAddress {
/// The address of an object of type T.
Address Addr;
/// The container of the address.
Address Container;
public:
ContainedAddress() {}
ContainedAddress(Address container, Address address)
: Addr(address), Container(container) {}
llvm::Value *getAddressPointer() const { return Addr.getAddress(); }
Alignment getAlignment() const { return Addr.getAlignment(); }
Address getAddress() const { return Addr; }
Address getContainer() const { return Container; }
bool isValid() const { return Addr.isValid(); }
};
/// An address on the stack together with an optional stack pointer reset
/// location.
class StackAddress {
/// The address of an object of type T.
Address Addr;
/// In a normal function, the result of llvm.stacksave or null.
/// In a coroutine, the result of llvm.coro.alloca.alloc.
/// In an async function, the result of the taskAlloc call.
llvm::Value *ExtraInfo;
public:
StackAddress() : ExtraInfo(nullptr) {}
StackAddress(Address address, llvm::Value *extraInfo = nullptr)
: Addr(address), ExtraInfo(extraInfo) {}
/// Return a StackAddress with the address changed in some superficial way.
StackAddress withAddress(Address addr) const {
return StackAddress(addr, ExtraInfo);
}
llvm::Value *getAddressPointer() const { return Addr.getAddress(); }
Alignment getAlignment() const { return Addr.getAlignment(); }
Address getAddress() const { return Addr; }
llvm::Value *getExtraInfo() const { return ExtraInfo; }
bool isValid() const { return Addr.isValid(); }
bool operator==(StackAddress RHS) const {
return Addr == RHS.Addr && ExtraInfo == RHS.ExtraInfo;
}
bool operator!=(StackAddress RHS) const { return !(*this == RHS); }
};
} // end namespace irgen
} // end namespace swift
namespace llvm {
template <>
struct DenseMapInfo<swift::irgen::Address> {
static swift::irgen::Address getEmptyKey() {
return swift::irgen::Address(DenseMapInfo<llvm::Value *>::getEmptyKey(),
DenseMapInfo<llvm::Type *>::getEmptyKey(),
swift::irgen::Alignment(8));
}
static swift::irgen::Address getTombstoneKey() {
return swift::irgen::Address(DenseMapInfo<llvm::Value *>::getTombstoneKey(),
DenseMapInfo<llvm::Type *>::getTombstoneKey(),
swift::irgen::Alignment(8));
}
static unsigned getHashValue(swift::irgen::Address address) {
return detail::combineHashValue(
DenseMapInfo<llvm::Value *>::getHashValue(address.getAddress()),
detail::combineHashValue(
DenseMapInfo<llvm::Type *>::getHashValue(address.getElementType()),
DenseMapInfo<swift::irgen::Alignment::int_type>::getHashValue(
address.getAlignment().getValue())));
}
static bool isEqual(swift::irgen::Address LHS, swift::irgen::Address RHS) {
return LHS == RHS;
}
};
template <>
struct DenseMapInfo<swift::irgen::StackAddress> {
static swift::irgen::StackAddress getEmptyKey() {
return swift::irgen::StackAddress(
DenseMapInfo<swift::irgen::Address>::getEmptyKey(),
DenseMapInfo<llvm::Value *>::getEmptyKey());
}
static swift::irgen::StackAddress getTombstoneKey() {
return swift::irgen::StackAddress(
DenseMapInfo<swift::irgen::Address>::getTombstoneKey(),
DenseMapInfo<llvm::Value *>::getTombstoneKey());
}
static unsigned getHashValue(swift::irgen::StackAddress address) {
return detail::combineHashValue(
DenseMapInfo<swift::irgen::Address>::getHashValue(address.getAddress()),
DenseMapInfo<swift::irgen::Alignment::int_type>::getHashValue(
address.getAlignment().getValue()));
}
static bool isEqual(swift::irgen::StackAddress LHS,
swift::irgen::StackAddress RHS) {
return LHS == RHS;
}
};
} // end namespace llvm
#endif