13#ifndef LLVM_CLANG_AST_INTERP_INTERPSTACK_H
14#define LLVM_CLANG_AST_INTERP_INTERPSTACK_H
33 template <
typename T,
typename... Tys>
void push(Tys &&... Args) {
34 new (grow(aligned_size<T>())) T(std::forward<Tys>(Args)...);
36 ItemTypes.push_back(toPrimType<T>());
41 template <
typename T> T
pop() {
43 assert(!ItemTypes.empty());
44 assert(ItemTypes.back() == toPrimType<T>());
47 T *Ptr = &peekInternal<T>();
48 T
Value = std::move(*Ptr);
50 shrink(aligned_size<T>());
57 assert(!ItemTypes.empty());
58 assert(ItemTypes.back() == toPrimType<T>());
61 T *Ptr = &peekInternal<T>();
63 shrink(aligned_size<T>());
67 template <
typename T> T &
peek()
const {
69 assert(!ItemTypes.empty());
70 assert(ItemTypes.back() == toPrimType<T>());
72 return peekInternal<T>();
75 template <
typename T> T &
peek(
size_t Offset)
const {
77 return *
reinterpret_cast<T *
>(peekData(Offset));
81 void *
top()
const {
return Chunk ? peekData(0) :
nullptr; }
84 size_t size()
const {
return StackSize; }
90 bool empty()
const {
return StackSize == 0; }
98 template <
typename T>
constexpr size_t aligned_size()
const {
99 constexpr size_t PtrAlign =
alignof(
void *);
100 return ((
sizeof(T) + PtrAlign - 1) / PtrAlign) * PtrAlign;
104 template <
typename T> T &peekInternal()
const {
105 return *
reinterpret_cast<T *
>(peekData(aligned_size<T>()));
109 void *grow(
size_t Size);
111 void *peekData(
size_t Size)
const;
113 void shrink(
size_t Size);
116 static constexpr size_t ChunkSize = 1024 * 1024;
129 StackChunk(StackChunk *Prev =
nullptr)
130 : Next(nullptr), Prev(Prev), End(reinterpret_cast<char *>(this + 1)) {}
133 size_t size()
const {
return End - start(); }
136 char *start() {
return reinterpret_cast<char *
>(
this + 1); }
137 const char *start()
const {
138 return reinterpret_cast<const char *
>(
this + 1);
141 static_assert(
sizeof(StackChunk) < ChunkSize,
"Invalid chunk size");
144 StackChunk *Chunk =
nullptr;
146 size_t StackSize = 0;
150 std::vector<PrimType> ItemTypes;
152 template <
typename T>
static constexpr PrimType toPrimType() {
153 if constexpr (std::is_same_v<T, Pointer>)
155 else if constexpr (std::is_same_v<T, bool> ||
156 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>)
187 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.
T & peek(size_t Offset) const
void dump() const
dump the stack contents to stderr.
void push(Tys &&... Args)
Constructs a value in place on the top of the stack.
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.