annotate clang/lib/AST/Interp/ByteCodeEmitter.h @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 1d019706d866
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- ByteCodeEmitter.h - Instruction emitter for the VM ---------*- C++ -*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // Defines the instruction emitters.
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #ifndef LLVM_CLANG_AST_INTERP_LINKEMITTER_H
anatofuz
parents:
diff changeset
14 #define LLVM_CLANG_AST_INTERP_LINKEMITTER_H
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 #include "ByteCodeGenError.h"
anatofuz
parents:
diff changeset
17 #include "Context.h"
anatofuz
parents:
diff changeset
18 #include "InterpStack.h"
anatofuz
parents:
diff changeset
19 #include "InterpState.h"
anatofuz
parents:
diff changeset
20 #include "PrimType.h"
anatofuz
parents:
diff changeset
21 #include "Program.h"
anatofuz
parents:
diff changeset
22 #include "Source.h"
anatofuz
parents:
diff changeset
23 #include "llvm/Support/Error.h"
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 namespace clang {
anatofuz
parents:
diff changeset
26 namespace interp {
anatofuz
parents:
diff changeset
27 class Context;
anatofuz
parents:
diff changeset
28 class SourceInfo;
anatofuz
parents:
diff changeset
29 enum Opcode : uint32_t;
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 /// An emitter which links the program to bytecode for later use.
anatofuz
parents:
diff changeset
32 class ByteCodeEmitter {
anatofuz
parents:
diff changeset
33 protected:
anatofuz
parents:
diff changeset
34 using LabelTy = uint32_t;
anatofuz
parents:
diff changeset
35 using AddrTy = uintptr_t;
anatofuz
parents:
diff changeset
36 using Local = Scope::Local;
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 public:
anatofuz
parents:
diff changeset
39 /// Compiles the function into the module.
anatofuz
parents:
diff changeset
40 llvm::Expected<Function *> compileFunc(const FunctionDecl *F);
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 protected:
anatofuz
parents:
diff changeset
43 ByteCodeEmitter(Context &Ctx, Program &P) : Ctx(Ctx), P(P) {}
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 virtual ~ByteCodeEmitter() {}
anatofuz
parents:
diff changeset
46
anatofuz
parents:
diff changeset
47 /// Define a label.
anatofuz
parents:
diff changeset
48 void emitLabel(LabelTy Label);
anatofuz
parents:
diff changeset
49 /// Create a label.
anatofuz
parents:
diff changeset
50 LabelTy getLabel() { return ++NextLabel; }
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 /// Methods implemented by the compiler.
anatofuz
parents:
diff changeset
53 virtual bool visitFunc(const FunctionDecl *E) = 0;
anatofuz
parents:
diff changeset
54 virtual bool visitExpr(const Expr *E) = 0;
anatofuz
parents:
diff changeset
55 virtual bool visitDecl(const VarDecl *E) = 0;
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 /// Bails out if a given node cannot be compiled.
anatofuz
parents:
diff changeset
58 bool bail(const Stmt *S) { return bail(S->getBeginLoc()); }
anatofuz
parents:
diff changeset
59 bool bail(const Decl *D) { return bail(D->getBeginLoc()); }
anatofuz
parents:
diff changeset
60 bool bail(const SourceLocation &Loc);
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 /// Emits jumps.
anatofuz
parents:
diff changeset
63 bool jumpTrue(const LabelTy &Label);
anatofuz
parents:
diff changeset
64 bool jumpFalse(const LabelTy &Label);
anatofuz
parents:
diff changeset
65 bool jump(const LabelTy &Label);
anatofuz
parents:
diff changeset
66 bool fallthrough(const LabelTy &Label);
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 /// Callback for local registration.
anatofuz
parents:
diff changeset
69 Local createLocal(Descriptor *D);
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 /// Parameter indices.
anatofuz
parents:
diff changeset
72 llvm::DenseMap<const ParmVarDecl *, unsigned> Params;
anatofuz
parents:
diff changeset
73 /// Local descriptors.
anatofuz
parents:
diff changeset
74 llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
anatofuz
parents:
diff changeset
75
anatofuz
parents:
diff changeset
76 private:
anatofuz
parents:
diff changeset
77 /// Current compilation context.
anatofuz
parents:
diff changeset
78 Context &Ctx;
anatofuz
parents:
diff changeset
79 /// Program to link to.
anatofuz
parents:
diff changeset
80 Program &P;
anatofuz
parents:
diff changeset
81 /// Index of the next available label.
anatofuz
parents:
diff changeset
82 LabelTy NextLabel = 0;
anatofuz
parents:
diff changeset
83 /// Offset of the next local variable.
anatofuz
parents:
diff changeset
84 unsigned NextLocalOffset = 0;
anatofuz
parents:
diff changeset
85 /// Location of a failure.
anatofuz
parents:
diff changeset
86 llvm::Optional<SourceLocation> BailLocation;
anatofuz
parents:
diff changeset
87 /// Label information for linker.
anatofuz
parents:
diff changeset
88 llvm::DenseMap<LabelTy, unsigned> LabelOffsets;
anatofuz
parents:
diff changeset
89 /// Location of label relocations.
anatofuz
parents:
diff changeset
90 llvm::DenseMap<LabelTy, llvm::SmallVector<unsigned, 5>> LabelRelocs;
anatofuz
parents:
diff changeset
91 /// Program code.
anatofuz
parents:
diff changeset
92 std::vector<char> Code;
anatofuz
parents:
diff changeset
93 /// Opcode to expression mapping.
anatofuz
parents:
diff changeset
94 SourceMap SrcMap;
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 /// Returns the offset for a jump or records a relocation.
anatofuz
parents:
diff changeset
97 int32_t getOffset(LabelTy Label);
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 /// Emits an opcode.
anatofuz
parents:
diff changeset
100 template <typename... Tys>
anatofuz
parents:
diff changeset
101 bool emitOp(Opcode Op, const Tys &... Args, const SourceInfo &L);
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 protected:
anatofuz
parents:
diff changeset
104 #define GET_LINK_PROTO
anatofuz
parents:
diff changeset
105 #include "Opcodes.inc"
anatofuz
parents:
diff changeset
106 #undef GET_LINK_PROTO
anatofuz
parents:
diff changeset
107 };
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 } // namespace interp
anatofuz
parents:
diff changeset
110 } // namespace clang
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 #endif