Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/EvalEmitter.h
35291 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"19#include "llvm/Support/Error.h"2021namespace clang {22namespace interp {23class Context;24class Function;25class InterpStack;26class Program;27enum Opcode : uint32_t;2829/// An emitter which evaluates opcodes as they are emitted.30class EvalEmitter : public SourceMapper {31public:32using LabelTy = uint32_t;33using AddrTy = uintptr_t;34using Local = Scope::Local;3536EvaluationResult interpretExpr(const Expr *E,37bool ConvertResultToRValue = false);38EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized);3940/// Clean up all resources.41void cleanup();4243InterpState &getState() { return S; }4445protected:46EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk);4748virtual ~EvalEmitter();4950/// Define a label.51void emitLabel(LabelTy Label);52/// Create a label.53LabelTy getLabel();5455/// Methods implemented by the compiler.56virtual bool visitExpr(const Expr *E) = 0;57virtual bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext) = 0;58virtual bool visitFunc(const FunctionDecl *F) = 0;5960/// Emits jumps.61bool jumpTrue(const LabelTy &Label);62bool jumpFalse(const LabelTy &Label);63bool jump(const LabelTy &Label);64bool fallthrough(const LabelTy &Label);6566/// Since expressions can only jump forward, predicated execution is67/// used to deal with if-else statements.68bool isActive() const { return CurrentLabel == ActiveLabel; }6970/// Callback for registering a local.71Local createLocal(Descriptor *D);7273/// Returns the source location of the current opcode.74SourceInfo getSource(const Function *F, CodePtr PC) const override {75return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;76}7778/// Parameter indices.79llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;80/// Lambda captures.81llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;82/// Offset of the This parameter in a lambda record.83ParamOffset LambdaThisCapture{0, false};84/// Local descriptors.85llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;8687private:88/// Current compilation context.89Context &Ctx;90/// Current program.91Program &P;92/// Callee evaluation state.93InterpState S;94/// Location to write the result to.95EvaluationResult EvalResult;96/// Whether the result should be converted to an RValue.97bool ConvertResultToRValue = false;98/// Whether we should check if the result has been fully99/// initialized.100bool CheckFullyInitialized = false;101102/// Temporaries which require storage.103llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals;104105Block *getLocal(unsigned Index) const {106auto It = Locals.find(Index);107assert(It != Locals.end() && "Missing local variable");108return reinterpret_cast<Block *>(It->second.get());109}110111void updateGlobalTemporaries();112113// The emitter always tracks the current instruction and sets OpPC to a token114// value which is mapped to the location of the opcode being evaluated.115CodePtr OpPC;116/// Location of the current instruction.117SourceInfo CurrentSource;118119/// Next label ID to generate - first label is 1.120LabelTy NextLabel = 1;121/// Label being executed - 0 is the entry label.122LabelTy CurrentLabel = 0;123/// Active block which should be executed.124LabelTy ActiveLabel = 0;125126protected:127#define GET_EVAL_PROTO128#include "Opcodes.inc"129#undef GET_EVAL_PROTO130};131132} // namespace interp133} // namespace clang134135#endif136137138