Path: blob/main/contrib/llvm-project/clang/lib/AST/ByteCode/EvalEmitter.h
213799 views
//===--- EvalEmitter.h - Instruction emitter for the VM ---------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Defines the instruction emitters.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_CLANG_AST_INTERP_EVALEMITTER_H13#define LLVM_CLANG_AST_INTERP_EVALEMITTER_H1415#include "EvaluationResult.h"16#include "InterpState.h"17#include "PrimType.h"18#include "Source.h"1920namespace clang {21namespace interp {22class Context;23class Function;24class InterpStack;25class Program;26enum Opcode : uint32_t;2728/// An emitter which evaluates opcodes as they are emitted.29class EvalEmitter : public SourceMapper {30public:31using LabelTy = uint32_t;32using AddrTy = uintptr_t;33using Local = Scope::Local;34using PtrCallback = llvm::function_ref<bool(const Pointer &)>;3536EvaluationResult interpretExpr(const Expr *E,37bool ConvertResultToRValue = false,38bool DestroyToplevelScope = false);39EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized);40/// Interpret the given Expr to a Pointer.41EvaluationResult interpretAsPointer(const Expr *E, PtrCallback PtrCB);4243/// Clean up all resources.44void cleanup();4546protected:47EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk);4849virtual ~EvalEmitter();5051/// Define a label.52void emitLabel(LabelTy Label);53/// Create a label.54LabelTy getLabel();5556/// Methods implemented by the compiler.57virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;58virtual bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext) = 0;59virtual bool visitFunc(const FunctionDecl *F) = 0;60virtual bool visit(const Expr *E) = 0;61virtual bool emitBool(bool V, const Expr *E) = 0;6263/// Emits jumps.64bool jumpTrue(const LabelTy &Label);65bool jumpFalse(const LabelTy &Label);66bool jump(const LabelTy &Label);67bool fallthrough(const LabelTy &Label);68/// Speculative execution.69bool speculate(const CallExpr *E, const LabelTy &EndLabel);7071/// Since expressions can only jump forward, predicated execution is72/// used to deal with if-else statements.73bool isActive() const { return CurrentLabel == ActiveLabel; }74bool checkingForUndefinedBehavior() const {75return S.checkingForUndefinedBehavior();76}7778/// Callback for registering a local.79Local createLocal(Descriptor *D);8081/// Returns the source location of the current opcode.82SourceInfo getSource(const Function *F, CodePtr PC) const override {83return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;84}8586/// Parameter indices.87llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;88/// Lambda captures.89llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;90/// Offset of the This parameter in a lambda record.91ParamOffset LambdaThisCapture{0, false};92/// Local descriptors.93llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;9495private:96/// Current compilation context.97Context &Ctx;98/// Current program.99Program &P;100/// Callee evaluation state.101InterpState S;102/// Location to write the result to.103EvaluationResult EvalResult;104/// Whether the result should be converted to an RValue.105bool ConvertResultToRValue = false;106/// Whether we should check if the result has been fully107/// initialized.108bool CheckFullyInitialized = false;109/// Callback to call when using interpretAsPointer.110std::optional<PtrCallback> PtrCB;111112/// Temporaries which require storage.113llvm::SmallVector<std::unique_ptr<char[]>> Locals;114115Block *getLocal(unsigned Index) const {116assert(Index < Locals.size());117return reinterpret_cast<Block *>(Locals[Index].get());118}119120void updateGlobalTemporaries();121122// The emitter always tracks the current instruction and sets OpPC to a token123// value which is mapped to the location of the opcode being evaluated.124CodePtr OpPC;125/// Location of the current instruction.126SourceInfo CurrentSource;127128/// Next label ID to generate - first label is 1.129LabelTy NextLabel = 1;130/// Label being executed - 0 is the entry label.131LabelTy CurrentLabel = 0;132/// Active block which should be executed.133LabelTy ActiveLabel = 0;134135protected:136#define GET_EVAL_PROTO137#include "Opcodes.inc"138#undef GET_EVAL_PROTO139};140141} // namespace interp142} // namespace clang143144#endif145146147