13#ifndef LLVM_CLANG_AST_INTERP_INTERPSTACK_H
14#define LLVM_CLANG_AST_INTERP_INTERPSTACK_H
36 template <
typename T,
typename... Tys>
void push(Tys &&...Args) {
37 new (grow(aligned_size<T>()))
T(std::forward<Tys>(Args)...);
39 ItemTypes.push_back(toPrimType<T>());
44 template <
typename T>
T pop() {
46 assert(!ItemTypes.empty());
47 assert(ItemTypes.back() == toPrimType<T>());
50 T *Ptr = &peekInternal<T>();
52 shrink(aligned_size<T>());
59 assert(!ItemTypes.empty());
60 assert(ItemTypes.back() == toPrimType<T>());
63 T *Ptr = &peekInternal<T>();
65 shrink(aligned_size<T>());
69 template <
typename T>
T &
peek()
const {
71 assert(!ItemTypes.empty());
72 assert(ItemTypes.back() == toPrimType<T>());
74 return peekInternal<T>();
77 template <
typename T>
T &
peek(
size_t Offset)
const {
79 return *
reinterpret_cast<T *
>(peekData(Offset));
83 void *
top()
const {
return Chunk ? peekData(0) :
nullptr; }
86 size_t size()
const {
return StackSize; }
93 bool empty()
const {
return StackSize == 0; }
101 template <
typename T>
constexpr size_t aligned_size()
const {
102 constexpr size_t PtrAlign =
alignof(
void *);
103 return ((
sizeof(
T) + PtrAlign - 1) / PtrAlign) * PtrAlign;
107 template <
typename T>
T &peekInternal()
const {
108 return *
reinterpret_cast<T *
>(peekData(aligned_size<T>()));
112 void *grow(
size_t Size);
114 void *peekData(
size_t Size)
const;
116 void shrink(
size_t Size);
119 static constexpr size_t ChunkSize = 1024 * 1024;
132 StackChunk(StackChunk *Prev =
nullptr)
133 : Next(nullptr), Prev(Prev), End(reinterpret_cast<char *>(this + 1)) {}
136 size_t size()
const {
return End - start(); }
139 char *start() {
return reinterpret_cast<char *
>(
this + 1); }
140 const char *start()
const {
141 return reinterpret_cast<const char *
>(
this + 1);
144 static_assert(
sizeof(StackChunk) < ChunkSize,
"Invalid chunk size");
147 StackChunk *Chunk =
nullptr;
149 size_t StackSize = 0;
153 std::vector<PrimType> ItemTypes;
155 template <
typename T>
static constexpr PrimType toPrimType() {
156 if constexpr (std::is_same_v<T, Pointer>)
158 else if constexpr (std::is_same_v<T, bool> || std::is_same_v<T, Boolean>)
160 else if constexpr (std::is_same_v<T, int8_t> ||
161 std::is_same_v<T, Integral<8, true>>)
163 else if constexpr (std::is_same_v<T, uint8_t> ||
164 std::is_same_v<T, Integral<8, false>>)
166 else if constexpr (std::is_same_v<T, int16_t> ||
167 std::is_same_v<T, Integral<16, true>>)
169 else if constexpr (std::is_same_v<T, uint16_t> ||
170 std::is_same_v<T, Integral<16, false>>)
172 else if constexpr (std::is_same_v<T, int32_t> ||
173 std::is_same_v<T, Integral<32, true>>)
175 else if constexpr (std::is_same_v<T, uint32_t> ||
176 std::is_same_v<T, Integral<32, false>>)
178 else if constexpr (std::is_same_v<T, int64_t> ||
179 std::is_same_v<T, Integral<64, true>>)
181 else if constexpr (std::is_same_v<T, uint64_t> ||
182 std::is_same_v<T, Integral<64, false>>)
184 else if constexpr (std::is_same_v<T, Floating>)
186 else if constexpr (std::is_same_v<T, FunctionPointer>)
188 else if constexpr (std::is_same_v<T, IntegralAP<true>>)
190 else if constexpr (std::is_same_v<T, IntegralAP<false>>)
192 else if constexpr (std::is_same_v<T, MemberPointer>)
194 else if constexpr (std::is_same_v<T, FixedPoint>)
197 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 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