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)...);
35 ItemTypes.push_back(toPrimType<T>());
39 template <
typename T>
T pop() {
40 assert(!ItemTypes.empty());
41 assert(ItemTypes.back() == toPrimType<T>());
43 T *Ptr = &peekInternal<T>();
45 shrink(aligned_size<T>());
51 assert(!ItemTypes.empty());
52 assert(ItemTypes.back() == toPrimType<T>());
54 T *Ptr = &peekInternal<T>();
55 if constexpr (!std::is_trivially_destructible_v<T>) {
58 shrink(aligned_size<T>());
62 template <
typename T>
T &
peek()
const {
63 assert(!ItemTypes.empty());
64 assert(ItemTypes.back() == toPrimType<T>());
65 return peekInternal<T>();
68 template <
typename T>
T &
peek(
size_t Offset)
const {
70 return *
reinterpret_cast<T *
>(peekData(Offset));
74 void *
top()
const {
return Chunk ? peekData(0) :
nullptr; }
77 size_t size()
const {
return StackSize; }
84 bool empty()
const {
return StackSize == 0; }
92 template <
typename T>
constexpr size_t aligned_size()
const {
93 constexpr size_t PtrAlign =
alignof(
void *);
94 return ((
sizeof(
T) + PtrAlign - 1) / PtrAlign) * PtrAlign;
98 template <
typename T>
T &peekInternal()
const {
99 return *
reinterpret_cast<T *
>(peekData(aligned_size<T>()));
103 void *grow(
size_t Size);
105 void *peekData(
size_t Size)
const;
107 void shrink(
size_t Size);
110 static constexpr size_t ChunkSize = 1024 * 1024;
123 StackChunk(StackChunk *Prev =
nullptr)
124 : Next(nullptr), Prev(Prev), End(reinterpret_cast<char *>(this + 1)) {}
127 size_t size()
const {
return End - start(); }
130 char *start() {
return reinterpret_cast<char *
>(
this + 1); }
131 const char *start()
const {
132 return reinterpret_cast<const char *
>(
this + 1);
135 static_assert(
sizeof(StackChunk) < ChunkSize,
"Invalid chunk size");
138 StackChunk *Chunk =
nullptr;
140 size_t StackSize = 0;
148 template <
typename T>
static constexpr PrimType toPrimType() {
149 if constexpr (std::is_same_v<T, Pointer>)
151 else if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, Boolean>)
153 else if constexpr (std::is_same_v<T, int8_t> ||
154 std::is_same_v<T, Integral<8, true>>)
156 else if constexpr (std::is_same_v<T, uint8_t> ||
157 std::is_same_v<T, Integral<8, false>>)
159 else if constexpr (std::is_same_v<T, int16_t> ||
160 std::is_same_v<T, Integral<16, true>>)
162 else if constexpr (std::is_same_v<T, uint16_t> ||
163 std::is_same_v<T, Integral<16, false>>)
165 else if constexpr (std::is_same_v<T, int32_t> ||
166 std::is_same_v<T, Integral<32, true>>)
168 else if constexpr (std::is_same_v<T, uint32_t> ||
169 std::is_same_v<T, Integral<32, false>>)
171 else if constexpr (std::is_same_v<T, int64_t> ||
172 std::is_same_v<T, Integral<64, true>>)
174 else if constexpr (std::is_same_v<T, uint64_t> ||
175 std::is_same_v<T, Integral<64, false>>)
177 else if constexpr (std::is_same_v<T, Floating>)
179 else if constexpr (std::is_same_v<T, IntegralAP<true>>)
181 else if constexpr (std::is_same_v<T, IntegralAP<false>>)
183 else if constexpr (std::is_same_v<T, MemberPointer>)
185 else if constexpr (std::is_same_v<T, FixedPoint>)
188 llvm_unreachable(
"unknown type push()'ed into InterpStack");
Stack frame storing temporaries and parameters.
void clearTo(size_t NewSize)
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.
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