EvalEmitter.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===--- EvalEmitter.h - Instruction emitter for the 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. // Defines the instruction emitters.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_AST_INTERP_EVALEMITTER_H
  13. #define LLVM_CLANG_AST_INTERP_EVALEMITTER_H
  14. #include "ByteCodeGenError.h"
  15. #include "Context.h"
  16. #include "InterpStack.h"
  17. #include "InterpState.h"
  18. #include "PrimType.h"
  19. #include "Program.h"
  20. #include "Source.h"
  21. #include "llvm/Support/Error.h"
  22. namespace clang {
  23. namespace interp {
  24. class Context;
  25. class Function;
  26. class InterpState;
  27. class Program;
  28. class SourceInfo;
  29. enum Opcode : uint32_t;
  30. /// An emitter which evaluates opcodes as they are emitted.
  31. class EvalEmitter : public SourceMapper {
  32. public:
  33. using LabelTy = uint32_t;
  34. using AddrTy = uintptr_t;
  35. using Local = Scope::Local;
  36. llvm::Expected<bool> interpretExpr(const Expr *E);
  37. llvm::Expected<bool> interpretDecl(const VarDecl *VD);
  38. protected:
  39. EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk,
  40. APValue &Result);
  41. virtual ~EvalEmitter() {}
  42. /// Define a label.
  43. void emitLabel(LabelTy Label);
  44. /// Create a label.
  45. LabelTy getLabel();
  46. /// Methods implemented by the compiler.
  47. virtual bool visitExpr(const Expr *E) = 0;
  48. virtual bool visitDecl(const VarDecl *VD) = 0;
  49. bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
  50. bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
  51. bool bail(const SourceLocation &Loc);
  52. /// Emits jumps.
  53. bool jumpTrue(const LabelTy &Label);
  54. bool jumpFalse(const LabelTy &Label);
  55. bool jump(const LabelTy &Label);
  56. bool fallthrough(const LabelTy &Label);
  57. /// Callback for registering a local.
  58. Local createLocal(Descriptor *D);
  59. /// Returns the source location of the current opcode.
  60. SourceInfo getSource(const Function *F, CodePtr PC) const override {
  61. return F ? F->getSource(PC) : CurrentSource;
  62. }
  63. /// Parameter indices.
  64. llvm::DenseMap<const ParmVarDecl *, unsigned> Params;
  65. /// Local descriptors.
  66. llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
  67. private:
  68. /// Current compilation context.
  69. Context &Ctx;
  70. /// Current program.
  71. Program &P;
  72. /// Callee evaluation state.
  73. InterpState S;
  74. /// Location to write the result to.
  75. APValue &Result;
  76. /// Temporaries which require storage.
  77. llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals;
  78. // The emitter always tracks the current instruction and sets OpPC to a token
  79. // value which is mapped to the location of the opcode being evaluated.
  80. CodePtr OpPC;
  81. /// Location of a failure.
  82. std::optional<SourceLocation> BailLocation;
  83. /// Location of the current instruction.
  84. SourceInfo CurrentSource;
  85. /// Next label ID to generate - first label is 1.
  86. LabelTy NextLabel = 1;
  87. /// Label being executed - 0 is the entry label.
  88. LabelTy CurrentLabel = 0;
  89. /// Active block which should be executed.
  90. LabelTy ActiveLabel = 0;
  91. /// Since expressions can only jump forward, predicated execution is
  92. /// used to deal with if-else statements.
  93. bool isActive() const { return CurrentLabel == ActiveLabel; }
  94. protected:
  95. #define GET_EVAL_PROTO
  96. #include "Opcodes.inc"
  97. #undef GET_EVAL_PROTO
  98. };
  99. } // namespace interp
  100. } // namespace clang
  101. #endif