forked from alliedmodders/sourcepawn
/
jit.h
135 lines (109 loc) · 3.45 KB
/
jit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// vim: set ts=8 sts=2 sw=2 tw=99 et:
//
// This file is part of SourcePawn.
//
// SourcePawn is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SourcePawn is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SourcePawn. If not, see <http://www.gnu.org/licenses/>.
#ifndef _include_sourcepawn_jit_h_
#define _include_sourcepawn_jit_h_
#include <sp_vm_types.h>
#include <sp_vm_api.h>
#include <am-vector.h>
#include "macro-assembler.h"
#include "opcodes.h"
#include "pool-allocator.h"
#include "outofline-asm.h"
#include "pcode-visitor.h"
#include "compiled-function.h"
namespace sp {
using namespace SourcePawn;
class PluginRuntime;
class PluginContext;
class LegacyImage;
struct BackwardJump {
// The pc at the jump instruction (i.e. after it).
uint32_t pc;
// The cip of the jump.
const cell_t* cip;
// The offset of the timeout thunk. This is filled in at the end.
uint32_t timeout_offset;
BackwardJump()
{}
BackwardJump(uint32_t pc, const cell_t* cip)
: pc(pc),
cip(cip)
{}
};
class CompilerBase : public PcodeVisitor
{
friend class ErrorPath;
public:
CompilerBase(PluginRuntime* rt, cell_t pcode_offs);
virtual ~CompilerBase();
static CompiledFunction* Compile(PluginContext* cx, RefPtr<MethodInfo> method, int* err);
int error() const {
return error_;
}
protected:
CompiledFunction* emit();
virtual void emitPrologue() = 0;
virtual void emitThrowPath(int err) = 0;
virtual void emitErrorHandlers() = 0;
virtual void emitOutOfBoundsErrorPath(OutOfBoundsErrorPath* path) = 0;
virtual void emitDebugBreakHandler() = 0;
// Helpers.
static int CompileFromThunk(PluginContext* cx, cell_t pcode_offs, void** addrp, uint8_t* pc);
static void* find_entry_fp();
static void InvokeReportError(int err);
static void InvokeReportTimeout();
static void PatchCallThunk(uint8_t* pc, void* target);
protected:
cell_t readCell();
// Map a return address (i.e. an exit point from a function) to its source
// cip. This lets us avoid tracking the cip during runtime. These are
// sorted by definition since we assemble and emit in forward order.
void emitCipMapping(const cell_t* cip) {
CipMapEntry entry;
entry.cipoffs = uintptr_t(cip) - uintptr_t(code_start_);
entry.pcoffs = masm.pc();
cip_map_.append(entry);
}
protected:
void emitErrorPath(ErrorPath* path);
void emitThrowPathIfNeeded(int err);
void reportError(int err);
protected:
Environment* env_;
PluginRuntime* rt_;
PluginContext* context_;
LegacyImage* image_;
PoolScope scope_;
int error_;
uint32_t pcode_start_;
const cell_t* code_start_;
const cell_t* op_cip_;
const cell_t* code_end_;
MacroAssembler masm;
Label* jump_map_;
ke::Vector<OutOfLinePath*> ool_paths_;
Label throw_timeout_;
Label throw_error_code_[SP_MAX_ERROR_CODES];
Label report_error_;
Label return_reported_error_;
// Debugging.
Label debug_break_;
ke::Vector<BackwardJump> backward_jumps_;
ke::Vector<CipMapEntry> cip_map_;
};
} // namespace sp
#endif // _include_sourcepawn_jit_h_