Thanks to visit codestin.com
Credit goes to clang.llvm.org

clang 22.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 "FixedPoint.h"
12#include "Floating.h"
13#include "Integral.h"
14#include "MemberPointer.h"
15#include "Pointer.h"
16#include <cassert>
17#include <cstdlib>
18
19using namespace clang;
20using namespace clang::interp;
21
23 if (Chunk && Chunk->Next)
24 std::free(Chunk->Next);
25 if (Chunk)
26 std::free(Chunk);
27}
28
29// We keep the last chunk around to reuse.
31 for (PrimType Item : llvm::reverse(ItemTypes)) {
32 TYPE_SWITCH(Item, { this->discard<T>(); });
33 }
34 assert(ItemTypes.empty());
35 assert(empty());
36}
37
38void InterpStack::clearTo(size_t NewSize) {
39 if (NewSize == 0)
40 return clear();
41 if (NewSize == size())
42 return;
43
44 assert(NewSize <= size());
45 for (PrimType Item : llvm::reverse(ItemTypes)) {
46 TYPE_SWITCH(Item, { this->discard<T>(); });
47
48 if (size() == NewSize)
49 break;
50 }
51
52 // Note: discard() above already removed the types from ItemTypes.
53 assert(size() == NewSize);
54}
55
56void *InterpStack::peekData(size_t Size) const {
57 assert(Chunk && "Stack is empty!");
58
59 if (LLVM_LIKELY(Size <= Chunk->size()))
60 return reinterpret_cast<void *>(Chunk->start() + Chunk->Size - Size);
61
62 StackChunk *Ptr = Chunk;
63 while (Size > Ptr->size()) {
64 Size -= Ptr->size();
65 Ptr = Ptr->Prev;
66 assert(Ptr && "Offset too large");
67 }
68
69 return reinterpret_cast<void *>(Ptr->start() + Ptr->Size - Size);
70}
71
72void InterpStack::shrink(size_t Size) {
73 assert(Chunk && "Chunk is empty!");
74
75 // Likely case is that we simply remove something from the current chunk.
76 if (LLVM_LIKELY(Size <= Chunk->size())) {
77 Chunk->Size -= Size;
78 StackSize -= Size;
79 return;
80 }
81
82 while (Size > Chunk->size()) {
83 Size -= Chunk->size();
84 if (Chunk->Next) {
85 std::free(Chunk->Next);
86 Chunk->Next = nullptr;
87 }
88 Chunk->Size = 0;
89 Chunk = Chunk->Prev;
90 assert(Chunk && "Offset too large");
91 }
92
93 Chunk->Size -= Size;
94 StackSize -= Size;
95}
96
97void InterpStack::dump() const {
98 llvm::errs() << "Items: " << ItemTypes.size() << ". Size: " << size() << '\n';
99 if (ItemTypes.empty())
100 return;
101
102 size_t Index = 0;
103 size_t Offset = 0;
104
105 // The type of the item on the top of the stack is inserted to the back
106 // of the vector, so the iteration has to happen backwards.
107 for (PrimType Item : llvm::reverse(ItemTypes)) {
108 Offset += align(primSize(Item));
109
110 llvm::errs() << Index << '/' << Offset << ": ";
111 TYPE_SWITCH(Item, {
112 const T &V = peek<T>(Offset);
113 llvm::errs() << V;
114 });
115 llvm::errs() << '\n';
116
117 ++Index;
118 }
119}
#define V(N, I)
#define TYPE_SWITCH(Expr, B)
Definition PrimType.h:207
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:185
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:23
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T