1 //===--- InterpStack.cpp - Stack implementation for the VM ------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "InterpStack.h"
13 using namespace clang;
14 using namespace clang::interp;
16 InterpStack::~InterpStack() {
20 void InterpStack::clear() {
21 if (Chunk && Chunk->Next)
29 void *InterpStack::grow(size_t Size) {
30 assert(Size < ChunkSize - sizeof(StackChunk) && "Object too large");
32 if (!Chunk || sizeof(StackChunk) + Chunk->size() + Size > ChunkSize) {
33 if (Chunk && Chunk->Next) {
36 StackChunk *Next = new (malloc(ChunkSize)) StackChunk(Chunk);
43 auto *Object = reinterpret_cast<void *>(Chunk->End);
49 void *InterpStack::peek(size_t Size) {
50 assert(Chunk && "Stack is empty!");
52 StackChunk *Ptr = Chunk;
53 while (Size > Ptr->size()) {
56 assert(Ptr && "Offset too large");
59 return reinterpret_cast<void *>(Ptr->End - Size);
62 void InterpStack::shrink(size_t Size) {
63 assert(Chunk && "Chunk is empty!");
65 while (Size > Chunk->size()) {
66 Size -= Chunk->size();
69 Chunk->Next = nullptr;
71 Chunk->End = Chunk->start();
73 assert(Chunk && "Offset too large");