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