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
13#ifndef LLVM_CLANG_AST_INTERP_EVALEMITTER_H
14#define LLVM_CLANG_AST_INTERP_EVALEMITTER_H
15
16#include "EvaluationResult.h"
17#include "InterpState.h"
18#include "PrimType.h"
19#include "Record.h"
20#include "Source.h"
21
22namespace clang {
23namespace interp {
24class Context;
25class Function;
26class InterpStack;
27class Program;
28enum Opcode : uint32_t;
29
30/// An emitter which evaluates opcodes as they are emitted.
31class EvalEmitter : public SourceMapper {
32public:
33 using LabelTy = uint32_t;
34 using AddrTy = uintptr_t;
35 using Local = Scope::Local;
36 using PtrCallback =
37 llvm::function_ref<bool(InterpState &S, CodePtr OpPC, const Pointer &)>;
38
39 EvaluationResult interpretExpr(const Expr *E,
40 bool ConvertResultToRValue = false,
41 bool DestroyToplevelScope = false);
42 EvaluationResult interpretDecl(const VarDecl *VD, const Expr *Init,
43 bool CheckFullyInitialized);
44 EvaluationResult interpretDestructor(const VarDecl *VD, const APValue &Value);
45 /// Interpret the given Expr to a Pointer.
46 EvaluationResult interpretAsPointer(const Expr *E, PtrCallback PtrCB);
47 EvaluationResult interpretAsLValuePointer(const Expr *E, PtrCallback PtrCB);
48 /// Interpret the given expression as if it was in the body of the given
49 /// function, i.e. the parameters of the function are available for use.
50 bool interpretCall(const FunctionDecl *FD, const Expr *E);
51
52 /// Clean up all resources.
53 void cleanup();
54
55protected:
56 EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk);
57
58 virtual ~EvalEmitter();
59
60 /// Define a label.
61 void emitLabel(LabelTy Label);
62 /// Create a label.
63 LabelTy getLabel();
64
65 /// Methods implemented by the compiler.
66 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
67 virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) = 0;
68 virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
69 bool ConstantContext) = 0;
70 virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value) = 0;
71 virtual bool visitFunc(const FunctionDecl *F) = 0;
72 virtual bool visit(const Expr *E) = 0;
73 virtual bool emitBool(bool V, const Expr *E) = 0;
74
75 /// Emits jumps.
76 bool jumpTrue(const LabelTy &Label, SourceInfo SI);
77 bool jumpFalse(const LabelTy &Label, SourceInfo SI);
78 bool jump(const LabelTy &Label, SourceInfo SI);
79 bool fallthrough(const LabelTy &Label);
80 /// Speculative execution.
81 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
82
83 /// Since expressions can only jump forward, predicated execution is
84 /// used to deal with if-else statements.
85 bool isActive() const { return CurrentLabel == ActiveLabel; }
86 bool checkingForUndefinedBehavior() const {
87 return S.checkingForUndefinedBehavior();
88 }
89
90 /// Callback for registering a local.
91 Local createLocal(Descriptor *D);
92
93 /// Returns the source location of the current opcode.
94 SourceInfo getSource(const Function *F, CodePtr PC) const override {
95 return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
96 }
97
98 /// Parameter indices.
99 llvm::DenseMap<const ParmVarDecl *, FuncParam> Params;
100 /// Local descriptors.
101 llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
102 std::optional<SourceInfo> LocOverride = std::nullopt;
103
104private:
105 /// Current compilation context.
106 Context &Ctx;
107 /// Current program.
108 Program &P;
109 /// Callee evaluation state.
110 InterpState S;
111 /// Location to write the result to.
112 EvaluationResult EvalResult;
113 /// Whether the result should be converted to an RValue.
114 bool ConvertResultToRValue = false;
115 /// Whether we should check if the result has been fully
116 /// initialized.
117 bool CheckFullyInitialized = false;
118 /// Callback to call when using interpretAsPointer.
119 std::optional<PtrCallback> PtrCB;
120
121 /// Temporaries which require storage.
122 llvm::SmallVector<std::unique_ptr<char[]>> Locals;
123
124 Block *getLocal(unsigned Index) const {
125 assert(Index < Locals.size());
126 return reinterpret_cast<Block *>(Locals[Index].get());
127 }
128
129 void updateGlobalTemporaries();
130
131 // The emitter always tracks the current instruction and sets OpPC to a token
132 // value which is mapped to the location of the opcode being evaluated.
133 CodePtr OpPC;
134 /// Location of the current instruction.
135 SourceInfo CurrentSource;
136
137 /// Next label ID to generate - first label is 1.
138 LabelTy NextLabel = 1;
139 /// Label being executed - 0 is the entry label.
140 LabelTy CurrentLabel = 0;
141 /// Active block which should be executed.
142 LabelTy ActiveLabel = 0;
143
144protected:
145#define GET_EVAL_PROTO
146#include "Opcodes.inc"
147#undef GET_EVAL_PROTO
148};
149
150} // namespace interp
151} // namespace clang
152
153#endif
154