InterpState.cpp 2.0 KB

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