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 std::optional<bool> interpretWithSubstitutions(const FunctionDecl *Callee,
53 ArrayRef<const Expr *> Args,
54 const Expr *This,
55 const Expr *Condition);
56
57 /// Clean up all resources.
58 void cleanup();
59
60protected:
61 EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk);
62
63 virtual ~EvalEmitter();
64
65 /// Define a label.
66 void emitLabel(LabelTy Label);
67 /// Create a label.
68 LabelTy getLabel();
69
70 /// Methods implemented by the compiler.
71 virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope) = 0;
72 virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) = 0;
73 virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
74 bool ConstantContext) = 0;
75 virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value) = 0;
76 virtual bool visitWithSubstitutions(const FunctionDecl *Callee,
77 ArrayRef<const Expr *> Args,
78 const Expr *This,
79 const Expr *Condition) = 0;
80 virtual bool visitFunc(const FunctionDecl *F) = 0;
81 virtual bool visit(const Expr *E) = 0;
82 virtual bool emitBool(bool V, const Expr *E) = 0;
83
84 /// Emits jumps.
85 bool jumpTrue(const LabelTy &Label, SourceInfo SI);
86 bool jumpFalse(const LabelTy &Label, SourceInfo SI);
87 bool jump(const LabelTy &Label, SourceInfo SI);
88 bool fallthrough(const LabelTy &Label);
89 /// Speculative execution.
90 bool speculate(const CallExpr *E, const LabelTy &EndLabel);
91
92 /// Since expressions can only jump forward, predicated execution is
93 /// used to deal with if-else statements.
94 bool isActive() const { return CurrentLabel == ActiveLabel; }
95 bool checkingForUndefinedBehavior() const {
96 return S.checkingForUndefinedBehavior();
97 }
98
99 /// Callback for registering a local.
100 Local createLocal(Descriptor *D);
101
102 /// Returns the source location of the current opcode.
103 SourceInfo getSource(const Function *F, CodePtr PC) const override {
104 return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource;
105 }
106
107 /// Parameter indices.
108 llvm::DenseMap<const ParmVarDecl *, FuncParam> Params;
109 /// Local descriptors.
110 llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
111 std::optional<SourceInfo> LocOverride = std::nullopt;
112
113private:
114 /// Current compilation context.
115 Context &Ctx;
116 /// Current program.
117 Program &P;
118 /// Callee evaluation state.
119 InterpState S;
120 /// Location to write the result to.
121 EvaluationResult EvalResult;
122 /// Whether the result should be converted to an RValue.
123 bool ConvertResultToRValue = false;
124 /// Whether we should check if the result has been fully
125 /// initialized.
126 bool CheckFullyInitialized = false;
127 /// Callback to call when using interpretAsPointer.
128 std::optional<PtrCallback> PtrCB;
129
130 /// Temporaries which require storage.
131 llvm::SmallVector<std::unique_ptr<char[]>> Locals;
132
133 Block *getLocal(unsigned Index) const {
134 assert(Index < Locals.size());
135 return reinterpret_cast<Block *>(Locals[Index].get());
136 }
137
138 void updateGlobalTemporaries();
139
140 /// Location of the current instruction.
141 SourceInfo CurrentSource;
142
143 /// Next label ID to generate - first label is 1.
144 LabelTy NextLabel = 1;
145 /// Label being executed - 0 is the entry label.
146 LabelTy CurrentLabel = 0;
147 /// Active block which should be executed.
148 LabelTy ActiveLabel = 0;
149
150protected:
151#define GET_EVAL_PROTO
152#include "Opcodes.inc"
153#undef GET_EVAL_PROTO
154};
155
156} // namespace interp
157} // namespace clang
158
159#endif
160