EvalEmitter.h 3.7 KB

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