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 "Source.h" |
20 | #include "llvm/Support/Error.h" |
21 | |
22 | namespace clang { |
23 | namespace interp { |
24 | class Context; |
25 | class Function; |
26 | class InterpStack; |
27 | class Program; |
28 | enum Opcode : uint32_t; |
29 | |
30 | /// An emitter which evaluates opcodes as they are emitted. |
31 | class EvalEmitter : public SourceMapper { |
32 | public: |
33 | using LabelTy = uint32_t; |
34 | using AddrTy = uintptr_t; |
35 | using Local = Scope::Local; |
36 | |
37 | EvaluationResult interpretExpr(const Expr *E, |
38 | bool ConvertResultToRValue = false); |
39 | EvaluationResult interpretDecl(const VarDecl *VD, bool CheckFullyInitialized); |
40 | |
41 | InterpState &getState() { return S; } |
42 | |
43 | protected: |
44 | EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk); |
45 | |
46 | virtual ~EvalEmitter(); |
47 | |
48 | /// Define a label. |
49 | void emitLabel(LabelTy Label); |
50 | /// Create a label. |
51 | LabelTy getLabel(); |
52 | |
53 | /// Methods implemented by the compiler. |
54 | virtual bool visitExpr(const Expr *E) = 0; |
55 | virtual bool visitDecl(const VarDecl *VD) = 0; |
56 | |
57 | /// Emits jumps. |
58 | bool jumpTrue(const LabelTy &Label); |
59 | bool jumpFalse(const LabelTy &Label); |
60 | bool jump(const LabelTy &Label); |
61 | bool fallthrough(const LabelTy &Label); |
62 | |
63 | /// Callback for registering a local. |
64 | Local createLocal(Descriptor *D); |
65 | |
66 | /// Returns the source location of the current opcode. |
67 | SourceInfo getSource(const Function *F, CodePtr PC) const override { |
68 | return (F && F->hasBody()) ? F->getSource(PC) : CurrentSource; |
69 | } |
70 | |
71 | /// Parameter indices. |
72 | llvm::DenseMap<const ParmVarDecl *, ParamOffset> Params; |
73 | /// Lambda captures. |
74 | llvm::DenseMap<const ValueDecl *, ParamOffset> LambdaCaptures; |
75 | /// Offset of the This parameter in a lambda record. |
76 | ParamOffset LambdaThisCapture{.Offset: 0, .IsPtr: false}; |
77 | /// Local descriptors. |
78 | llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors; |
79 | |
80 | private: |
81 | /// Current compilation context. |
82 | Context &Ctx; |
83 | /// Current program. |
84 | Program &P; |
85 | /// Callee evaluation state. |
86 | InterpState S; |
87 | /// Location to write the result to. |
88 | EvaluationResult EvalResult; |
89 | /// Whether the result should be converted to an RValue. |
90 | bool ConvertResultToRValue = false; |
91 | /// Whether we should check if the result has been fully |
92 | /// initialized. |
93 | bool CheckFullyInitialized = false; |
94 | |
95 | /// Temporaries which require storage. |
96 | llvm::DenseMap<unsigned, std::unique_ptr<char[]>> Locals; |
97 | |
98 | Block *getLocal(unsigned Index) const { |
99 | auto It = Locals.find(Index); |
100 | assert(It != Locals.end() && "Missing local variable" ); |
101 | return reinterpret_cast<Block *>(It->second.get()); |
102 | } |
103 | |
104 | // The emitter always tracks the current instruction and sets OpPC to a token |
105 | // value which is mapped to the location of the opcode being evaluated. |
106 | CodePtr OpPC; |
107 | /// Location of the current instruction. |
108 | SourceInfo CurrentSource; |
109 | |
110 | /// Next label ID to generate - first label is 1. |
111 | LabelTy NextLabel = 1; |
112 | /// Label being executed - 0 is the entry label. |
113 | LabelTy CurrentLabel = 0; |
114 | /// Active block which should be executed. |
115 | LabelTy ActiveLabel = 0; |
116 | |
117 | /// Since expressions can only jump forward, predicated execution is |
118 | /// used to deal with if-else statements. |
119 | bool isActive() const { return CurrentLabel == ActiveLabel; } |
120 | |
121 | protected: |
122 | #define GET_EVAL_PROTO |
123 | #include "Opcodes.inc" |
124 | #undef GET_EVAL_PROTO |
125 | }; |
126 | |
127 | } // namespace interp |
128 | } // namespace clang |
129 | |
130 | #endif |
131 | |