| //===--- EvalEmitter.h - Instruction emitter for the VM ---------*- C++ -*-===// | |
| // | |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |
| // See https://llvm.org/LICENSE.txt for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| // | |
| //===----------------------------------------------------------------------===// | |
| // | |
| // Defines the instruction emitters. | |
| // | |
| //===----------------------------------------------------------------------===// | |
| #ifndef LLVM_CLANG_AST_INTERP_EVALEMITTER_H | |
| #define LLVM_CLANG_AST_INTERP_EVALEMITTER_H | |
| #include "EvaluationResult.h" | |
| #include "InterpState.h" | |
| #include "PrimType.h" | |
| #include "Record.h" | |
| #include "Source.h" | |
| namespace clang { | |
| namespace interp { | |
| class Context; | |
| class Function; | |
| class InterpStack; | |
| class FrameAllocator; | |
| class Program; | |
| enum Opcode : uint32_t; | |
| /// An emitter which evaluates opcodes as they are emitted. | |
| class EvalEmitter : public SourceMapper { | |
| public: | |
| using LabelTy = uint32_t; | |
| using AddrTy = uintptr_t; | |
| using Local = Scope::Local; | |
| using PtrCallback = | |
| llvm::function_ref<bool(InterpState &S, CodePtr OpPC, const Pointer &)>; | |
| EvaluationResult interpretExpr(const Expr *E) { | |
| return interpretExpr(E, /*ConvertResultToRValue=*/E->isGLValue(), | |
| /*DestroyToplevelScope=*/false); | |
| } | |
| EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue, | |
| bool DestroyToplevelScope = false); | |
| EvaluationResult interpretDecl(const VarDecl *VD, const Expr *Init, | |
| bool CheckFullyInitialized); | |
| EvaluationResult interpretDestructor(const VarDecl *VD, const APValue &Value); | |
| /// Interpret the given Expr to a Pointer. | |
| EvaluationResult interpretAsPointer(const Expr *E, PtrCallback PtrCB); | |
| EvaluationResult interpretAsLValuePointer(const Expr *E, PtrCallback PtrCB); | |
| /// Interpret the given expression as if it was in the body of the given | |
| /// function, i.e. the parameters of the function are available for use. | |
| bool interpretCall(const FunctionDecl *FD, const Expr *E); | |
| std::optional<bool> interpretWithSubstitutions(const FunctionDecl *Callee, | |
| ArrayRef<const Expr *> Args, | |
| const Expr *This, | |
| const Expr *Condition); | |
| /// Clean up all resources. | |
| void cleanup(); | |
| /// Returns the source location of the current opcode. | |
| SourceInfo getSource(CodePtr PC) const override { return CurrentSource; } | |
| bool constantFolding() const { | |
| return S.EvalMode == EvaluationMode::ConstantFold; | |
| } | |
| protected: | |
| EvalEmitter(Context &Ctx, Program &P, const EvalSettings &Settings, | |
| InterpStack &Stk, FrameAllocator &FrameAlloc); | |
| EvalEmitter(Context &Ctx, Program &P, Expr::EvalStatus &Status, | |
| InterpStack &Stk, FrameAllocator &FrameAlloc); | |
| /// Define a label. | |
| void emitLabel(LabelTy Label); | |
| /// Create a label. | |
| LabelTy getLabel(); | |
| /// Methods implemented by the compiler. | |
| virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0; | |
| virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) = 0; | |
| virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, | |
| bool ConstantContext) = 0; | |
| virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value) = 0; | |
| virtual bool visitWithSubstitutions(const FunctionDecl *Callee, | |
| ArrayRef<const Expr *> Args, | |
| const Expr *This, | |
| const Expr *Condition) = 0; | |
| virtual bool visitFunc(const FunctionDecl *F) = 0; | |
| virtual bool visit(const Expr *E) = 0; | |
| virtual bool emitBool(bool V, const Expr *E) = 0; | |
| /// Emits jumps. | |
| bool jumpTrue(const LabelTy &Label, SourceInfo SI); | |
| bool jumpFalse(const LabelTy &Label, SourceInfo SI); | |
| bool jump(const LabelTy &Label, SourceInfo SI); | |
| bool fallthrough(const LabelTy &Label); | |
| /// Speculative execution. | |
| bool speculate(const CallExpr *E, const LabelTy &EndLabel); | |
| /// Since expressions can only jump forward, predicated execution is | |
| /// used to deal with if-else statements. | |
| bool isActive() const { return CurrentLabel == ActiveLabel; } | |
| bool checkingForUndefinedBehavior() const { | |
| return S.checkingForUndefinedBehavior(); | |
| } | |
| /// Callback for registering a local. | |
| Local createLocal(const Descriptor *D); | |
| /// Parameter indices. | |
| llvm::DenseMap<const ParmVarDecl *, FuncParam> Params; | |
| /// Local descriptors. | |
| llvm::SmallVector<SmallVector<Local, 2>, 1> Descriptors; | |
| std::optional<SourceInfo> LocOverride = std::nullopt; | |
| private: | |
| /// Current compilation context. | |
| Context &Ctx; | |
| /// Current program. | |
| Program &P; | |
| /// Callee evaluation state. | |
| InterpState S; | |
| /// Location to write the result to. | |
| EvaluationResult EvalResult; | |
| ConstantExprKind ConstexprKind = ConstantExprKind::Normal; | |
| /// Whether the result should be converted to an RValue. | |
| bool ConvertResultToRValue = false; | |
| /// Whether we should check if the result has been fully | |
| /// initialized. | |
| bool CheckFullyInitialized = false; | |
| /// Callback to call when using interpretAsPointer. | |
| std::optional<PtrCallback> PtrCB; | |
| /// Temporaries which require storage. | |
| llvm::SmallVector<char *> Locals; | |
| Block *getLocal(unsigned Index) const { | |
| assert(Index < Locals.size()); | |
| return reinterpret_cast<Block *>(Locals[Index]); | |
| } | |
| void updateGlobalTemporaries(); | |
| /// Location of the current instruction. | |
| SourceInfo CurrentSource; | |
| /// Next label ID to generate - first label is 1. | |
| LabelTy NextLabel = 1; | |
| /// Label being executed - 0 is the entry label. | |
| LabelTy CurrentLabel = 0; | |
| /// Active block which should be executed. | |
| LabelTy ActiveLabel = 0; | |
| protected: | |
| #define GET_EVAL_PROTO | |
| #include "Opcodes.inc" | |
| #undef GET_EVAL_PROTO | |
| }; | |
| } // namespace interp | |
| } // namespace clang | |
| #endif |