clang 23.0.0git
InterpStack.cpp
Go to the documentation of this file.
1//===--- InterpStack.cpp - Stack implementation for the VM ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "InterpStack.h"
10#include "Boolean.h"
11#include "Char.h"
12#include "FixedPoint.h"
13#include "Floating.h"
14#include "Integral.h"
15#include "MemberPointer.h"
16#include "Pointer.h"
17#include <cassert>
18#include <cstdlib>
19
20using namespace clang;
21using namespace clang::interp;
22
24 if (Chunk && Chunk->Next)
25 std::free(Chunk->Next);
26 if (Chunk)
27 std::free(Chunk);
28}
29
30// We keep the last chunk around to reuse.
32 for (PrimType Item : llvm::reverse(ItemTypes)) {
33 TYPE_SWITCH(Item, { this->discard<T>(); });
34 }
35 assert(ItemTypes.empty());
36 assert(empty());
37}
38
39void InterpStack::clearTo(size_t NewSize) {
40 if (NewSize == 0)
41 return clear();
42 if (NewSize == size())
43 return;
44
45 assert(NewSize <= size());
46 for (PrimType Item : llvm::reverse(ItemTypes)) {
47 TYPE_SWITCH(Item, { this->discard<T>(); });
48
49 if (size() == NewSize)
50 break;
51 }
52
53 // Note: discard() above already removed the types from ItemTypes.
54 assert(size() == NewSize);
55}
56
57void *InterpStack::peekData(size_t Size) const {
58 assert(Chunk && "Stack is empty!");
59
60 if (LLVM_LIKELY(Size <= Chunk->size()))
61 return reinterpret_cast<void *>(Chunk->start() + Chunk->Size - Size);
62
63 StackChunk *Ptr = Chunk;
64 while (Size > Ptr->size()) {
65 Size -= Ptr->size();
66 Ptr = Ptr->Prev;
67 assert(Ptr && "Offset too large");
68 }
69
70 return reinterpret_cast<void *>(Ptr->start() + Ptr->Size - Size);
71}
72
73void InterpStack::shrink(size_t Size) {
74 assert(Chunk && "Chunk is empty!");
75
76 // Likely case is that we simply remove something from the current chunk.
77 if (LLVM_LIKELY(Size <= Chunk->size())) {
78 Chunk->Size -= Size;
79 StackSize -= Size;
80 return;
81 }
82
83 while (Size > Chunk->size()) {
84 Size -= Chunk->size();
85 if (Chunk->Next) {
86 std::free(Chunk->Next);
87 Chunk->Next = nullptr;
88 }
89 Chunk->Size = 0;
90 Chunk = Chunk->Prev;
91 assert(Chunk && "Offset too large");
92 }
93
94 Chunk->Size -= Size;
95 StackSize -= Size;
96}
97
98void InterpStack::dump() const {
99 llvm::errs() << "Items: " << ItemTypes.size() << ". Size: " << size() << '\n';
100 if (ItemTypes.empty())
101 return;
102
103 size_t Index = 0;
104 size_t Offset = 0;
105
106 // The type of the item on the top of the stack is inserted to the back
107 // of the vector, so the iteration has to happen backwards.
108 for (PrimType Item : llvm::reverse(ItemTypes)) {
109 Offset += align(primSize(Item));
110
111 llvm::errs() << Index << '/' << Offset << ": ";
112 TYPE_SWITCH(Item, {
113 const T &V = peek<T>(Offset);
114 llvm::errs() << V;
115 });
116 llvm::errs() << '\n';
117
118 ++Index;
119 }
120}
#define V(N, I)
#define TYPE_SWITCH(Expr, B)
Definition PrimType.h:223
void clearTo(size_t NewSize)
void dump() const
dump the stack contents to stderr.
void clear()
Clears the stack.
size_t size() const
Returns the size of the stack in bytes.
Definition InterpStack.h:77
bool empty() const
Returns whether the stack is empty.
Definition InterpStack.h:84
void discard()
Discards the top value from the stack.
Definition InterpStack.h:50
~InterpStack()
Destroys the stack, freeing up storage.
T & peek() const
Returns a reference to the value on the top of the stack.
Definition InterpStack.h:62
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:201
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition PrimType.cpp:24
The JSON file list parser is used to communicate input to InstallAPI.