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