-
Notifications
You must be signed in to change notification settings - Fork 14
/
amx_base.inc
157 lines (135 loc) · 4.15 KB
/
amx_base.inc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2011-2012 Zeex
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#if defined AMX_BASE_INC
#endinput
#endif
#define AMX_BASE_INC
/**
* <library name="amx_assembly amx_base" summary="AMX Assembly Library: Script load address determination.">
* <summary pawndoc="true">
* This library uses the enhanced <em>pawndoc.xsl</em> from
* <a href="https://github.com/pawn-lang/pawndoc">pawn-lang/pawndoc</a>.
* This XSL has features such as library and markdown support, and will not
* render this message when used.
* </summary>
* </library>
*/
/// <p/>
#if defined __PawnBuild
#if __PawnBuild == 0
#undef __PawnBuild
#define __pawn_build 0
#else
#define __pawn_build __PawnBuild
#endif
#else
#define __pawn_build 0
#endif
#if !defined cellbytes
const cellbytes = cellbits / charbits;
#endif
#if !defined __register_names
const __cod = 0;
const __dat = 1;
const __hea = 2;
const __stp = 3;
const __stk = 4;
const __frm = 5;
const __cip = 6;
const __jit = 7;
const __jmp = 8;
const __flg = 9;
#define __register_names
#endif
#if __pawn_build >= 10
#define HasSysreqD() (bool:(__emit(zero.pri, lctrl __flg, eq.c.pri 0)))
#define HasReloc() (bool:(__emit(zero.pri, lctrl __flg, const.alt 512, and, eq.c.pri 0)))
#else
forward bool:HasSysreqD();
forward bool:HasReloc();
#endif
#if !defined cellbytes
const cellbytes = cellbits / charbits;
#endif
/// <library>amx_assembly amx_base</library>
static stock GetAmxBaseAddress_helper() {
return 0;
}
/// <library>amx_assembly amx_base</library>
/// <summary>
/// Returns the AMX base address i.e. <c>amx->base</c>.
/// </summary>
stock GetAmxBaseAddressNow() {
// Required for this trick to work.
assert(HasReloc());
new cod = 0, dat = 0;
#emit lctrl __cod
#emit stor.s.pri cod
#emit lctrl __dat
#emit stor.s.pri dat
// Get code section start address relative to data.
new code_start = cod - dat;
// Get address of GetAmxBaseAddress_helper().
new fn_addr = 0;
#emit const.pri GetAmxBaseAddress_helper
#emit stor.s.pri fn_addr
// Get absolute address from the CALL instruction.
new fn_addr_reloc = 0, call_addr = 0;
GetAmxBaseAddress_helper();
#emit lctrl __cip
#emit stor.s.pri call_addr
call_addr = call_addr - 3 * cellbytes + code_start;
#emit lref.s.pri call_addr
#emit stor.s.pri fn_addr_reloc
return fn_addr_reloc - fn_addr - cod;
}
/// <library>amx_assembly amx_base</library>
stock GetAmxBaseAddress() {
static amx_base = 0;
if (amx_base == 0) {
amx_base = GetAmxBaseAddressNow();
}
return amx_base;
}
#if __pawn_build >= 10
#endinput
#endif
/// <library>amx_assembly amx_base</library>
stock bool:HasSysreqD() {
// SA:MP doesn't have the AMX flags in register 9, open.mp does. Thus if
// `pri` is 0 it wasn't set by the control register.
#emit zero.pri
#emit lctrl __flg // flags
#emit eq.c.pri 0
#emit retn
return false;
}
/// <library>amx_assembly amx_base</library>
stock bool:HasReloc() {
// SA:MP doesn't have the AMX flags in register 9, open.mp does. Thus if
// `pri` is 0 it wasn't set by the control register.
#emit zero.pri
#emit lctrl __flg // flags
#emit const.alt 512
#emit and
#emit eq.c.pri 0
#emit retn
return false;
}