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