13#ifndef LLVM_CLANG_AST_INTERP_INTERPSTACK_H
14#define LLVM_CLANG_AST_INTERP_INTERPSTACK_H
35 template <
typename T,
typename... Tys>
void push(Tys &&...Args) {
36 new (grow(aligned_size<T>()))
T(std::forward<Tys>(Args)...);
38 ItemTypes.push_back(toPrimType<T>());
43 template <
typename T>
T pop() {
45 assert(!ItemTypes.empty());
46 assert(ItemTypes.back() == toPrimType<T>());
49 T *Ptr = &peekInternal<T>();
51 shrink(aligned_size<T>());
58 assert(!ItemTypes.empty());
59 assert(ItemTypes.back() == toPrimType<T>());
62 T *Ptr = &peekInternal<T>();
64 shrink(aligned_size<T>());
68 template <
typename T>
T &
peek()
const {
70 assert(!ItemTypes.empty());
71 assert(ItemTypes.back() == toPrimType<T>());
73 return peekInternal<T>();
76 template <
typename T>
T &
peek(
size_t Offset)
const {
78 return *
reinterpret_cast<T *
>(peekData(Offset));
82 void *
top()
const {
return Chunk ? peekData(0) :
nullptr; }
85 size_t size()
const {
return StackSize; }
91 bool empty()
const {
return StackSize == 0; }
99 template <
typename T>
constexpr size_t aligned_size()
const {
100 constexpr size_t PtrAlign =
alignof(
void *);
101 return ((
sizeof(
T) + PtrAlign - 1) / PtrAlign) * PtrAlign;
105 template <
typename T>
T &peekInternal()
const {
106 return *
reinterpret_cast<T *
>(peekData(aligned_size<T>()));
110 void *grow(
size_t Size);
112 void *peekData(
size_t Size)
const;
114 void shrink(
size_t Size);
117 static constexpr size_t ChunkSize = 1024 * 1024;
130 StackChunk(StackChunk *Prev =
nullptr)
131 : Next(nullptr), Prev(Prev), End(reinterpret_cast<char *>(this + 1)) {}
134 size_t size()
const {
return End - start(); }
137 char *start() {
return reinterpret_cast<char *
>(
this + 1); }
138 const char *start()
const {
139 return reinterpret_cast<const char *
>(
this + 1);
142 static_assert(
sizeof(StackChunk) < ChunkSize,
"Invalid chunk size");
145 StackChunk *Chunk =
nullptr;
147 size_t StackSize = 0;
151 std::vector<PrimType> ItemTypes;
153 template <
typename T>
static constexpr PrimType toPrimType() {
154 if constexpr (std::is_same_v<T, Pointer>)
156 else if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, Boolean>)
158 else if constexpr (std::is_same_v<T, int8_t> ||
159 std::is_same_v<T, Integral<8, true>>)
161 else if constexpr (std::is_same_v<T, uint8_t> ||
162 std::is_same_v<T, Integral<8, false>>)
164 else if constexpr (std::is_same_v<T, int16_t> ||
165 std::is_same_v<T, Integral<16, true>>)
167 else if constexpr (std::is_same_v<T, uint16_t> ||
168 std::is_same_v<T, Integral<16, false>>)
170 else if constexpr (std::is_same_v<T, int32_t> ||
171 std::is_same_v<T, Integral<32, true>>)
173 else if constexpr (std::is_same_v<T, uint32_t> ||
174 std::is_same_v<T, Integral<32, false>>)
176 else if constexpr (std::is_same_v<T, int64_t> ||
177 std::is_same_v<T, Integral<64, true>>)
179 else if constexpr (std::is_same_v<T, uint64_t> ||
180 std::is_same_v<T, Integral<64, false>>)
182 else if constexpr (std::is_same_v<T, Floating>)
184 else if constexpr (std::is_same_v<T, FunctionPointer>)
186 else if constexpr (std::is_same_v<T, IntegralAP<true>>)
188 else if constexpr (std::is_same_v<T, IntegralAP<false>>)
190 else if constexpr (std::is_same_v<T, MemberPointer>)
193 llvm_unreachable(
"unknown type push()'ed into InterpStack");
Stack frame storing temporaries and parameters.
T pop()
Returns the value from the top of the stack and removes it.
void push(Tys &&...Args)
Constructs a value in place on the top of the stack.
T & peek(size_t Offset) const
void dump() const
dump the stack contents to stderr.
void * top() const
Returns a pointer to the top object.
void clear()
Clears the stack without calling any destructors.
size_t size() const
Returns the size of the stack in bytes.
bool empty() const
Returns whether the stack is empty.
void discard()
Discards the top value from the stack.
~InterpStack()
Destroys the stack, freeing up storage.
T & peek() const
Returns a reference to the value on the top of the stack.
constexpr bool aligned(uintptr_t Value)
PrimType
Enumeration of the primitive types of the VM.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T