/src/llvm-project/clang/lib/AST/Interp/InterpStack.cpp
Line | Count | Source (jump to first uncovered line) |
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 "Floating.h" |
12 | | #include "Integral.h" |
13 | | #include "Pointer.h" |
14 | | #include <cassert> |
15 | | #include <cstdlib> |
16 | | |
17 | | using namespace clang; |
18 | | using namespace clang::interp; |
19 | | |
20 | 0 | InterpStack::~InterpStack() { |
21 | 0 | clear(); |
22 | 0 | } |
23 | | |
24 | 0 | void InterpStack::clear() { |
25 | 0 | if (Chunk && Chunk->Next) |
26 | 0 | std::free(Chunk->Next); |
27 | 0 | if (Chunk) |
28 | 0 | std::free(Chunk); |
29 | 0 | Chunk = nullptr; |
30 | 0 | StackSize = 0; |
31 | 0 | #ifndef NDEBUG |
32 | 0 | ItemTypes.clear(); |
33 | 0 | #endif |
34 | 0 | } |
35 | | |
36 | 0 | void *InterpStack::grow(size_t Size) { |
37 | 0 | assert(Size < ChunkSize - sizeof(StackChunk) && "Object too large"); |
38 | | |
39 | 0 | if (!Chunk || sizeof(StackChunk) + Chunk->size() + Size > ChunkSize) { |
40 | 0 | if (Chunk && Chunk->Next) { |
41 | 0 | Chunk = Chunk->Next; |
42 | 0 | } else { |
43 | 0 | StackChunk *Next = new (std::malloc(ChunkSize)) StackChunk(Chunk); |
44 | 0 | if (Chunk) |
45 | 0 | Chunk->Next = Next; |
46 | 0 | Chunk = Next; |
47 | 0 | } |
48 | 0 | } |
49 | |
|
50 | 0 | auto *Object = reinterpret_cast<void *>(Chunk->End); |
51 | 0 | Chunk->End += Size; |
52 | 0 | StackSize += Size; |
53 | 0 | return Object; |
54 | 0 | } |
55 | | |
56 | 0 | void *InterpStack::peekData(size_t Size) const { |
57 | 0 | assert(Chunk && "Stack is empty!"); |
58 | | |
59 | 0 | StackChunk *Ptr = Chunk; |
60 | 0 | while (Size > Ptr->size()) { |
61 | 0 | Size -= Ptr->size(); |
62 | 0 | Ptr = Ptr->Prev; |
63 | 0 | assert(Ptr && "Offset too large"); |
64 | 0 | } |
65 | |
|
66 | 0 | return reinterpret_cast<void *>(Ptr->End - Size); |
67 | 0 | } |
68 | | |
69 | 0 | void InterpStack::shrink(size_t Size) { |
70 | 0 | assert(Chunk && "Chunk is empty!"); |
71 | | |
72 | 0 | while (Size > Chunk->size()) { |
73 | 0 | Size -= Chunk->size(); |
74 | 0 | if (Chunk->Next) { |
75 | 0 | std::free(Chunk->Next); |
76 | 0 | Chunk->Next = nullptr; |
77 | 0 | } |
78 | 0 | Chunk->End = Chunk->start(); |
79 | 0 | Chunk = Chunk->Prev; |
80 | 0 | assert(Chunk && "Offset too large"); |
81 | 0 | } |
82 | |
|
83 | 0 | Chunk->End -= Size; |
84 | 0 | StackSize -= Size; |
85 | 0 | } |
86 | | |
87 | 0 | void InterpStack::dump() const { |
88 | 0 | #ifndef NDEBUG |
89 | 0 | llvm::errs() << "Items: " << ItemTypes.size() << ". Size: " << size() << '\n'; |
90 | 0 | if (ItemTypes.empty()) |
91 | 0 | return; |
92 | | |
93 | 0 | size_t Index = 0; |
94 | 0 | size_t Offset = 0; |
95 | | |
96 | | // The type of the item on the top of the stack is inserted to the back |
97 | | // of the vector, so the iteration has to happen backwards. |
98 | 0 | for (auto TyIt = ItemTypes.rbegin(); TyIt != ItemTypes.rend(); ++TyIt) { |
99 | 0 | Offset += align(primSize(*TyIt)); |
100 | |
|
101 | 0 | llvm::errs() << Index << '/' << Offset << ": "; |
102 | 0 | TYPE_SWITCH(*TyIt, { |
103 | 0 | const T &V = peek<T>(Offset); |
104 | 0 | llvm::errs() << V; |
105 | 0 | }); |
106 | 0 | llvm::errs() << '\n'; |
107 | |
|
108 | 0 | ++Index; |
109 | 0 | } |
110 | 0 | #endif |
111 | 0 | } |