1 //===--- InterpState.cpp - Interpreter for the constexpr 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 "InterpState.h" 10 #include "InterpFrame.h" 11 #include "InterpStack.h" 12 #include "Program.h" 13 #include "State.h" 14 15 using namespace clang; 16 using namespace clang::interp; 17 18 InterpState::InterpState(State &Parent, Program &P, InterpStack &Stk, 19 Context &Ctx, SourceMapper *M) 20 : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), Current(nullptr) {} 21 22 InterpState::~InterpState() { 23 while (Current) { 24 InterpFrame *Next = Current->Caller; 25 delete Current; 26 Current = Next; 27 } 28 29 while (DeadBlocks) { 30 DeadBlock *Next = DeadBlocks->Next; 31 std::free(DeadBlocks); 32 DeadBlocks = Next; 33 } 34 } 35 36 Frame *InterpState::getCurrentFrame() { 37 if (Current && Current->Caller) 38 return Current; 39 return Parent.getCurrentFrame(); 40 } 41 42 bool InterpState::reportOverflow(const Expr *E, const llvm::APSInt &Value) { 43 QualType Type = E->getType(); 44 CCEDiag(E, diag::note_constexpr_overflow) << Value << Type; 45 return noteUndefinedBehavior(); 46 } 47 48 void InterpState::deallocate(Block *B) { 49 assert(B); 50 const Descriptor *Desc = B->getDescriptor(); 51 assert(Desc); 52 53 if (B->hasPointers()) { 54 size_t Size = B->getSize(); 55 56 // Allocate a new block, transferring over pointers. 57 char *Memory = 58 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size)); 59 auto *D = new (Memory) DeadBlock(DeadBlocks, B); 60 61 // Move data from one block to another. 62 if (Desc->MoveFn) 63 Desc->MoveFn(B, B->data(), D->data(), Desc); 64 } else { 65 // Free storage, if necessary. 66 if (Desc->DtorFn) 67 Desc->DtorFn(B, B->data(), Desc); 68 } 69 } 70