Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/ByteCodeEmitter.h
35292 views
//===--- ByteCodeEmitter.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_LINKEMITTER_H13#define LLVM_CLANG_AST_INTERP_LINKEMITTER_H1415#include "Context.h"16#include "PrimType.h"17#include "Program.h"18#include "Source.h"1920namespace clang {21namespace interp {22enum Opcode : uint32_t;2324/// An emitter which links the program to bytecode for later use.25class ByteCodeEmitter {26protected:27using LabelTy = uint32_t;28using AddrTy = uintptr_t;29using Local = Scope::Local;3031public:32/// Compiles the function into the module.33Function *compileFunc(const FunctionDecl *FuncDecl);3435protected:36ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}3738virtual ~ByteCodeEmitter() {}3940/// Define a label.41void emitLabel(LabelTy Label);42/// Create a label.43LabelTy getLabel() { return ++NextLabel; }4445/// Methods implemented by the compiler.46virtual bool visitFunc(const FunctionDecl *E) = 0;47virtual bool visitExpr(const Expr *E) = 0;48virtual bool visitDeclAndReturn(const VarDecl *E, bool ConstantContext) = 0;4950/// Emits jumps.51bool jumpTrue(const LabelTy &Label);52bool jumpFalse(const LabelTy &Label);53bool jump(const LabelTy &Label);54bool fallthrough(const LabelTy &Label);5556/// We're always emitting bytecode.57bool isActive() const { return true; }5859/// Callback for local registration.60Local createLocal(Descriptor *D);6162/// Parameter indices.63llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params;64/// Lambda captures.65llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures;66/// Offset of the This parameter in a lambda record.67ParamOffset LambdaThisCapture{0, false};68/// Local descriptors.69llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;7071private:72/// Current compilation context.73Context &Ctx;74/// Program to link to.75Program &P;76/// Index of the next available label.77LabelTy NextLabel = 0;78/// Offset of the next local variable.79unsigned NextLocalOffset = 0;80/// Label information for linker.81llvm::DenseMap<LabelTy, unsigned> LabelOffsets;82/// Location of label relocations.83llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;84/// Program code.85std::vector<std::byte> Code;86/// Opcode to expression mapping.87SourceMap SrcMap;8889/// Returns the offset for a jump or records a relocation.90int32_t getOffset(LabelTy Label);9192/// Emits an opcode.93template <typename... Tys>94bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);9596protected:97#define GET_LINK_PROTO98#include "Opcodes.inc"99#undef GET_LINK_PROTO100};101102} // namespace interp103} // namespace clang104105#endif106107108