forked from pawn-lang/amx_assembly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode.inc
1259 lines (1210 loc) · 36.5 KB
/
opcode.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 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 OPCODE_INC
#endinput
#endif
#define OPCODE_INC
/**
* <library name="amx_assembly opcode" summary="AMX Assembly Library: Information on all PAWN opcodes.">
* <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/>
#include "amx_header"
#if __Pawn >= 0x30A
// Disable the recursion warning in the Russian compiler.
#pragma warning push
#pragma warning disable 207
#pragma disablerecursion
#pragma warning pop
// Disable the recursion warning in the fast compiler.
#pragma warning disable 238
#endif
#define OPCODE_HAS_O2 0
#if defined __optimization
#if __optimization == 2
#undef OPCODE_HAS_O2
#define OPCODE_HAS_O2 1
#endif
#endif
#define OPCODE_MAX_INSN_NAME 15
/// <p/>
/// <library>amx_assembly opcode</library>
enum Opcode {
OP_NONE, OP_LOAD_PRI, OP_LOAD_ALT,
OP_LOAD_S_PRI, OP_LOAD_S_ALT, OP_LREF_PRI,
OP_LREF_ALT, OP_LREF_S_PRI, OP_LREF_S_ALT,
OP_LOAD_I, OP_LODB_I, OP_CONST_PRI,
OP_CONST_ALT, OP_ADDR_PRI, OP_ADDR_ALT,
OP_STOR_PRI, OP_STOR_ALT, OP_STOR_S_PRI,
OP_STOR_S_ALT, OP_SREF_PRI, OP_SREF_ALT,
OP_SREF_S_PRI, OP_SREF_S_ALT, OP_STOR_I,
OP_STRB_I, OP_LIDX, OP_LIDX_B,
OP_IDXADDR, OP_IDXADDR_B, OP_ALIGN_PRI,
OP_ALIGN_ALT, OP_LCTRL, OP_SCTRL,
OP_MOVE_PRI, OP_MOVE_ALT, OP_XCHG,
OP_PUSH_PRI, OP_PUSH_ALT, OP_PUSH_R,
OP_PUSH_C, OP_PUSH, OP_PUSH_S,
OP_POP_PRI, OP_POP_ALT, OP_STACK,
OP_HEAP, OP_PROC, OP_RET,
OP_RETN, OP_CALL, OP_CALL_PRI,
OP_JUMP, OP_JREL, OP_JZER,
OP_JNZ, OP_JEQ, OP_JNEQ,
OP_JLESS, OP_JLEQ, OP_JGRTR,
OP_JGEQ, OP_JSLESS, OP_JSLEQ,
OP_JSGRTR, OP_JSGEQ, OP_SHL,
OP_SHR, OP_SSHR, OP_SHL_C_PRI,
OP_SHL_C_ALT, OP_SHR_C_PRI, OP_SHR_C_ALT,
OP_SMUL, OP_SDIV, OP_SDIV_ALT,
OP_UMUL, OP_UDIV, OP_UDIV_ALT,
OP_ADD, OP_SUB, OP_SUB_ALT,
OP_AND, OP_OR, OP_XOR,
OP_NOT, OP_NEG, OP_INVERT,
OP_ADD_C, OP_SMUL_C, OP_ZERO_PRI,
OP_ZERO_ALT, OP_ZERO, OP_ZERO_S,
OP_SIGN_PRI, OP_SIGN_ALT, OP_EQ,
OP_NEQ, OP_LESS, OP_LEQ,
OP_GRTR, OP_GEQ, OP_SLESS,
OP_SLEQ, OP_SGRTR, OP_SGEQ,
OP_EQ_C_PRI, OP_EQ_C_ALT, OP_INC_PRI,
OP_INC_ALT, OP_INC, OP_INC_S,
OP_INC_I, OP_DEC_PRI, OP_DEC_ALT,
OP_DEC, OP_DEC_S, OP_DEC_I,
OP_MOVS, OP_CMPS, OP_FILL,
OP_HALT, OP_BOUNDS, OP_SYSREQ_PRI,
OP_SYSREQ_C, OP_FILE, OP_LINE,
OP_SYMBOL, OP_SRANGE, OP_JUMP_PRI,
OP_SWITCH, OP_CASETBL, OP_SWAP_PRI,
OP_SWAP_ALT, OP_PUSH_ADR, OP_NOP,
OP_SYSREQ_D, OP_SYMTAG, OP_BREAK,
#if OPCODE_HAS_O2
OP_PUSH2_C, OP_PUSH2, OP_PUSH2_S,
OP_PUSH2_ADR, OP_PUSH3_C, OP_PUSH3,
OP_PUSH3_S, OP_PUSH3_ADR, OP_PUSH4_C,
OP_PUSH4, OP_PUSH4_S, OP_PUSH4_ADR,
OP_PUSH5_C, OP_PUSH5, OP_PUSH5_S,
OP_PUSH5_ADR, OP_LOAD_BOTH, OP_LOAD_S_BOTH,
OP_CONST, OP_CONST_S, OP_SYSREQ_N,
#endif
OP_LAST_
};
/// <library>amx_assembly opcode</library>
const Opcode:Opcode__ = Opcode;
#if OPCODE_HAS_O2
static Opcode:OP_SYSREQ_DN = OP_SYSREQ_D;
#endif
// Doesn't work inline.
#if OPCODE_HAS_O2
#define OPCODE_O2_PARAMETER_COUNTS , 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 2, 2, 2, 2, 2
#else
#define OPCODE_O2_PARAMETER_COUNTS
#endif
/// <library>amx_assembly opcode</library>
stock const gAMXOpcodeParameterCounts[] = {
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
0, 1, 1, 0, 0, 1, 0, 1, 1, 0
OPCODE_O2_PARAMETER_COUNTS
};
// Doesn't work inline.
#if OPCODE_HAS_O2
#define OPCODE_O2_NEEDS_RELOC , false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
#else
#define OPCODE_O2_NEEDS_RELOC
#endif
/// <library>amx_assembly opcode</library>
stock const gAMXOpcodeNeedsReloc[] = {
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, true , false, true ,
false, true , true , true , true , true , true , true , true , true , true , true , true ,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false, false, true ,
true , false, false, false, false, false, false, false
OPCODE_O2_NEEDS_RELOC
};
// Doesn't work inline.
#if OPCODE_HAS_O2
#define OPCODE_O2_NAMES , "push2.c", "push2", "push2.s", "push2.adr", "push3.c", "push3", "push3.s", "push3.adr", "push4.c", "push4", "push4.s", "push4.adr", "push5.c", "push5", "push5.s", "push5.adr", "load.both", "load.s.both", "const", "const.s", "sysreq.n"
#else
#define OPCODE_O2_NAMES
#endif
/// <library>amx_assembly opcode</library>
stock const gAMXOpcodeNames[][OPCODE_MAX_INSN_NAME] = {
"none", "load.pri", "load.alt", "load.s.pri", "load.s.alt", "lref.pri", "lref.alt",
"lref.s.pri", "lref.s.alt", "load.i", "lodb.i", "const.pri", "const.alt", "addr.pri",
"addr.alt", "stor.pri", "stor.alt", "stor.s.pri", "stor.s.alt", "sref.pri", "sref.alt",
"sref.s.pri", "sref.s.alt", "stor.i", "strb.i", "lidx", "lidx.b", "idxaddr", "idxaddr.b",
"align.pri", "align.alt", "lctrl", "sctrl", "move.pri", "move.alt", "xchg", "push.pri",
"push.alt", "push.r", "push.c", "push", "push.s", "pop.pri", "pop.alt", "stack", "heap", "proc",
"ret", "retn", "call", "call.pri", "jump", "jrel", "jzer", "jnz", "jeq", "jneq", "jless",
"jleq", "jgrtr", "jgeq", "jsless", "jsleq", "jsgrtr", "jsgeq", "shl", "shr", "sshr",
"shl.c.pri", "shl.c.alt", "shr.c.pri", "shr.c.alt", "smul", "sdiv", "sdiv.alt", "umul", "udiv",
"udiv.alt", "add", "sub", "sub.alt", "and", "or", "xort", "not", "neg", "invert", "add.c",
"smul.c", "zero.pri", "zero.alt", "zero", "zero.s", "sign.pri", "sign.alt", "eq", "neq", "less",
"leq", "grtr", "geq", "sless", "sleq", "sgrtr", "sgeq", "eq.c.pri", "eq.c.alt", "inc.pri",
"inc.alt", "inc", "inc.s", "inc.i", "dec.pri", "dec.alt", "dec", "dec.s", "dec.i", "movs",
"cmps", "fill", "halt", "bounds", "sysreq.pri", "sysreq.c", "file", "line", "symbol", "srange",
"jump.pri", "switch", "casetbl", "swap.pri", "swap.alt", "push.adr", "nop", "sysreq.d",
"symtag", "break"
OPCODE_O2_NAMES
};
// Doesn't work inline.
#if OPCODE_HAS_O2
#define OPCODE_O2_BASE_SIZES , 3 * cellbytes, 3 * cellbytes, 3 * cellbytes, 3 * cellbytes, 4 * cellbytes, 4 * cellbytes, 4 * cellbytes, 4 * cellbytes, 5 * cellbytes, 5 * cellbytes, 5 * cellbytes, 5 * cellbytes, 6 * cellbytes, 6 * cellbytes, 6 * cellbytes, 6 * cellbytes, 3 * cellbytes, 3 * cellbytes, 3 * cellbytes, 3 * cellbytes, 3 * cellbytes
#else
#define OPCODE_O2_BASE_SIZES
#endif
/// <library>amx_assembly opcode</library>
stock const gAMXOpcodeBaseSizes[] = {
1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 2 * cellbytes, 1 * cellbytes, 2 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes,
2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes,
2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes,
1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes, 2 * cellbytes,
1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes, 1 * cellbytes, 2 * cellbytes, 1 * cellbytes, 2 * cellbytes, 2 * cellbytes, 1 * cellbytes
OPCODE_O2_BASE_SIZES
};
/// <library>amx_assembly opcode</library>
const NUM_OPCODES = _:OP_LAST_;
/// <library>amx_assembly opcode</library>
forward Opcode:UnsafeUnrelocateOpcode(Opcode:opcode);
/// <library>amx_assembly opcode</library>
static stock Opcode:ReadOpcodeNearThis(offset = 0) {
const cells0 = 1 * cellbytes;
const cells1 = 1 * cellbytes;
new ret_addr = 0;
// Get return address + COD - DAT + offset.
#emit load.s.alt cells0
#emit lctrl __cod
#emit add
#emit move.alt
#emit lctrl __dat
#emit xchg
#emit sub
#emit load.s.alt offset
#emit add
#emit stor.s.pri ret_addr
#emit lref.s.pri ret_addr
#emit stack cells1
#emit retn
return OP_NONE; // make compiler happy
}
/// <library>amx_assembly opcode</library>
static stock bool:HaveToRelocateOpcodes() {
return ReadOpcodeNearThis(-2 * cellbytes) != OP_CALL;
}
/// <library>amx_assembly opcode</library>
/// <remarks>
/// Separate the macro opcodes for `-O2`.
/// </remarks>
#if OPCODE_HAS_O2
forward Opcode:RelocateMacroOpcodeNow(Opcode:opcode);
#endif
/// <library>amx_assembly opcode</library>
/// <remarks>
/// Based on this idea: http://forum.sa-mp.com/showthread.php?t=358084
/// </remarks>
stock Opcode:RelocateOpcodeNow(Opcode:opcode) {
if (!HaveToRelocateOpcodes()) {
#if OPCODE_HAS_O2
if (opcode == OP_SYSREQ_N) {
return OP_SYSREQ_D;
} else if (opcode == OP_SYSREQ_D) {
return OP_LAST_;
}
#endif
return opcode;
}
switch (opcode) {
case OP_LOAD_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit load.pri 0
}
case OP_LOAD_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit load.alt 0
}
case OP_LOAD_S_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit load.s.pri 0
}
case OP_LOAD_S_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit load.s.alt 0
}
case OP_LREF_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit lref.pri 0
}
case OP_LREF_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit lref.alt 0
}
case OP_LREF_S_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit lref.s.pri 0
}
case OP_LREF_S_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit lref.s.alt 0
}
case OP_LOAD_I: {
return ReadOpcodeNearThis(cellbytes);
#emit load.i
}
case OP_LODB_I: {
return ReadOpcodeNearThis(cellbytes);
#emit lodb.i 1
}
case OP_CONST_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit const.pri 0
}
case OP_CONST_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit const.alt 0
}
case OP_ADDR_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit addr.pri 0
}
case OP_ADDR_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit addr.alt 0
}
case OP_STOR_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit stor.pri 0
}
case OP_STOR_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit stor.alt 0
}
case OP_STOR_S_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit stor.s.pri 0
}
case OP_STOR_S_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit stor.s.alt 0
}
case OP_SREF_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit sref.pri 0
}
case OP_SREF_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit sref.alt 0
}
case OP_SREF_S_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit sref.s.pri 0
}
case OP_SREF_S_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit sref.s.alt 0
}
case OP_STOR_I: {
return ReadOpcodeNearThis(cellbytes);
#emit stor.i
}
case OP_STRB_I: {
return ReadOpcodeNearThis(cellbytes);
#emit strb.i 1
}
case OP_LIDX: {
return ReadOpcodeNearThis(cellbytes);
#emit lidx
}
case OP_LIDX_B: {
return ReadOpcodeNearThis(cellbytes);
#emit lidx.b 0
}
case OP_IDXADDR: {
return ReadOpcodeNearThis(cellbytes);
#emit idxaddr
}
case OP_IDXADDR_B: {
return ReadOpcodeNearThis(cellbytes);
#emit idxaddr.b 0
}
case OP_ALIGN_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit align.pri 0
}
case OP_ALIGN_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit align.alt 0
}
case OP_LCTRL: {
return ReadOpcodeNearThis(cellbytes);
#emit lctrl 0 // Doesn't use `__cod` because the value isn't important.
}
case OP_SCTRL: {
return ReadOpcodeNearThis(cellbytes);
#emit sctrl 0 // Doesn't use `__cod` because the value isn't important.
}
case OP_MOVE_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit move.pri
}
case OP_MOVE_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit move.alt
}
case OP_XCHG: {
return ReadOpcodeNearThis(cellbytes);
#emit xchg
}
case OP_PUSH_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit push.pri
}
case OP_PUSH_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit push.alt
}
case OP_PUSH_C: {
return ReadOpcodeNearThis(cellbytes);
#emit push.c 0
}
case OP_PUSH: {
return ReadOpcodeNearThis(cellbytes);
#emit push 0
}
case OP_PUSH_S: {
return ReadOpcodeNearThis(cellbytes);
#emit push.s 0
}
case OP_POP_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit pop.pri
}
case OP_POP_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit pop.alt
}
case OP_STACK: {
return ReadOpcodeNearThis(cellbytes);
#emit stack 0
}
case OP_HEAP: {
return ReadOpcodeNearThis(cellbytes);
#emit heap 0
}
case OP_PROC: {
return ReadOpcodeNearThis(cellbytes);
#emit proc
}
case OP_RET: {
return ReadOpcodeNearThis(cellbytes);
#emit ret
}
case OP_RETN: {
return ReadOpcodeNearThis(cellbytes);
#emit retn
}
case OP_CALL: {
// We can't do just "#emit call 0" - this will crash compiler for some reason (bug?).
return ReadOpcodeNearThis(-2 * cellbytes);
}
case OP_JUMP: {
return ReadOpcodeNearThis(cellbytes);
#emit jump 0
}
case OP_JZER: {
return ReadOpcodeNearThis(cellbytes);
#emit jzer 0
}
case OP_JNZ: {
return ReadOpcodeNearThis(cellbytes);
#emit jnz 0
}
case OP_JEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit jeq 0
}
case OP_JNEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit jneq 0
}
case OP_JLESS: {
return ReadOpcodeNearThis(cellbytes);
#emit jless 0
}
case OP_JLEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit jleq 0
}
case OP_JGRTR: {
return ReadOpcodeNearThis(cellbytes);
#emit jgrtr 0
}
case OP_JGEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit jgeq 0
}
case OP_JSLESS: {
return ReadOpcodeNearThis(cellbytes);
#emit jsless 0
}
case OP_JSLEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit jsleq 0
}
case OP_JSGRTR: {
return ReadOpcodeNearThis(cellbytes);
#emit jsgrtr 0
}
case OP_JSGEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit jsgeq 0
}
case OP_SHL: {
return ReadOpcodeNearThis(cellbytes);
#emit shl
}
case OP_SHR: {
return ReadOpcodeNearThis(cellbytes);
#emit shr
}
case OP_SSHR: {
return ReadOpcodeNearThis(cellbytes);
#emit sshr
}
case OP_SHL_C_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit shl.c.pri 0
}
case OP_SHL_C_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit shl.c.alt 0
}
case OP_SHR_C_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit shr.c.pri 0
}
case OP_SHR_C_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit shr.c.alt 0
}
case OP_SMUL: {
return ReadOpcodeNearThis(cellbytes);
#emit smul
}
case OP_SDIV: {
return ReadOpcodeNearThis(cellbytes);
#emit sdiv
}
case OP_SDIV_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit sdiv.alt
}
case OP_UMUL: {
return ReadOpcodeNearThis(cellbytes);
#emit umul
}
case OP_UDIV: {
return ReadOpcodeNearThis(cellbytes);
#emit udiv
}
case OP_UDIV_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit udiv.alt
}
case OP_ADD: {
return ReadOpcodeNearThis(cellbytes);
#emit add
}
case OP_SUB: {
return ReadOpcodeNearThis(cellbytes);
#emit sub
}
case OP_SUB_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit sub.alt
}
case OP_AND: {
return ReadOpcodeNearThis(cellbytes);
#emit and
}
case OP_OR: {
return ReadOpcodeNearThis(cellbytes);
#emit or
}
case OP_XOR: {
return ReadOpcodeNearThis(cellbytes);
#emit xor
}
case OP_NOT: {
return ReadOpcodeNearThis(cellbytes);
#emit not
}
case OP_NEG: {
return ReadOpcodeNearThis(cellbytes);
#emit neg
}
case OP_INVERT: {
return ReadOpcodeNearThis(cellbytes);
#emit invert
}
case OP_ADD_C: {
return ReadOpcodeNearThis(cellbytes);
#emit add.c 0
}
case OP_SMUL_C: {
return ReadOpcodeNearThis(cellbytes);
#emit smul.c 0
}
case OP_ZERO_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit zero.pri
}
case OP_ZERO_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit zero.alt
}
case OP_ZERO: {
return ReadOpcodeNearThis(cellbytes);
#emit zero 0
}
case OP_ZERO_S: {
return ReadOpcodeNearThis(cellbytes);
#emit zero.s 0
}
case OP_SIGN_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit sign.pri
}
case OP_SIGN_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit sign.alt
}
case OP_EQ: {
return ReadOpcodeNearThis(cellbytes);
#emit eq
}
case OP_NEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit neq
}
case OP_LESS: {
return ReadOpcodeNearThis(cellbytes);
#emit less
}
case OP_LEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit leq
}
case OP_GRTR: {
return ReadOpcodeNearThis(cellbytes);
#emit grtr
}
case OP_GEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit geq
}
case OP_SLESS: {
return ReadOpcodeNearThis(cellbytes);
#emit sless
}
case OP_SLEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit sleq
}
case OP_SGRTR: {
return ReadOpcodeNearThis(cellbytes);
#emit sgrtr
}
case OP_SGEQ: {
return ReadOpcodeNearThis(cellbytes);
#emit sgeq
}
case OP_EQ_C_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit eq.c.pri 0
}
case OP_EQ_C_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit eq.c.alt 0
}
case OP_INC_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit inc.pri
}
case OP_INC_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit inc.alt
}
case OP_INC: {
return ReadOpcodeNearThis(cellbytes);
#emit inc 0
}
case OP_INC_S: {
return ReadOpcodeNearThis(cellbytes);
#emit inc.s 0
}
case OP_INC_I: {
return ReadOpcodeNearThis(cellbytes);
#emit inc.i
}
case OP_DEC_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit dec.pri
}
case OP_DEC_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit dec.alt
}
case OP_DEC: {
return ReadOpcodeNearThis(cellbytes);
#emit dec 0
}
case OP_DEC_S: {
return ReadOpcodeNearThis(cellbytes);
#emit dec.s 0
}
case OP_DEC_I: {
return ReadOpcodeNearThis(cellbytes);
#emit dec.i
}
case OP_MOVS: {
return ReadOpcodeNearThis(cellbytes);
#emit movs 0
}
case OP_CMPS: {
return ReadOpcodeNearThis(cellbytes);
#emit cmps 0
}
case OP_FILL: {
return ReadOpcodeNearThis(cellbytes);
#emit fill 0
}
case OP_HALT: {
return ReadOpcodeNearThis(cellbytes);
#emit halt 0
}
case OP_BOUNDS: {
return ReadOpcodeNearThis(cellbytes);
#emit bounds 0
}
case OP_SYSREQ_C: {
return ReadOpcodeNearThis(cellbytes);
#emit sysreq.c 0
}
case OP_SWITCH: {
static T = 1;
#if debug > 0
if (T) return ReadOpcodeNearThis(3 * cellbytes);
#else
if (T) return ReadOpcodeNearThis(2 * cellbytes);
#endif
#if ((__Pawn & 0x0F) >= 0x0A) || ((__Pawn & 0xF0) >= 0xA0)
// Disable `switch control expression is constant`.
#pragma warning push
#pragma warning disable 243
#endif
switch (0) {
case 0: {}
}
#if ((__Pawn & 0x0F) >= 0x0A) || ((__Pawn & 0xF0) >= 0xA0)
#pragma warning pop
#endif
}
case OP_CASETBL: {
new x = 0;
switch (x) { case 0: return ReadOpcodeNearThis(5 * cellbytes); }
}
case OP_SWAP_PRI: {
return ReadOpcodeNearThis(cellbytes);
#emit swap.pri
}
case OP_SWAP_ALT: {
return ReadOpcodeNearThis(cellbytes);
#emit swap.alt
}
case OP_PUSH_ADR: {
return ReadOpcodeNearThis(cellbytes);
#emit push.adr 0
}
case OP_NOP: {
return ReadOpcodeNearThis(cellbytes);
#emit nop
}
case OP_SYSREQ_D: {
if (HasSysreqD()) {
#if OPCODE_HAS_O2
OP_SYSREQ_DN = OP_SYSREQ_D;
#endif
// Since there's no way to #emit sysreq.d we have to compute its
// address using a relative offset to another opcode, e.g. nop.
new bool:is_crashdetect = false;
#emit zero.pri
#emit lctrl 0xFF
#emit stor.s.pri is_crashdetect
if (is_crashdetect) {
// The offset is different when running under crashdetect as
// it uses its own (modified) version of the VM. This value
// value works only with crashdetect 4.13.
return RelocateOpcodeNow(OP_NOP) - Opcode:0x1EA;
} else {
return RelocateOpcodeNow(OP_NOP) - Opcode:0x4F;
}
} else {
return RelocateOpcodeNow(OP_NOP);
}
}
case OP_BREAK: {
return ReadOpcodeNearThis(cellbytes);
#emit break
}
#if OPCODE_HAS_O2
default: {
return RelocateMacroOpcodeNow(opcode);
}
#endif
}
return opcode;
}
/// <library>amx_assembly opcode</library>
static stock Opcode:opcode_lookup[NUM_OPCODES];
/// <library>amx_assembly opcode</library>
static stock bool:opcode_table_is_ready = false;
/// <library>amx_assembly opcode</library>
stock bool:IsOpcodeValid(Opcode:opcode) {
return (OP_NONE <= opcode < Opcode:NUM_OPCODES);
}
/// <library>amx_assembly opcode</library>
static stock OpcodeTableSwap(addr0, addr1) {
new v0, v1;
return
// Swap the cases.
v0 = ReadAmxMemory(addr0),
v1 = ReadAmxMemory(addr1),
WriteAmxMemory(addr1, v0),
WriteAmxMemory(addr0, v1),
// Move on.
addr0 += cellbytes,
addr1 += cellbytes,
// Swap the pointers.
v0 = ReadAmxMemory(addr0),
v1 = ReadAmxMemory(addr1),
WriteAmxMemory(addr1, v0),
WriteAmxMemory(addr0, v1);
}
/// <library>amx_assembly opcode</library>
static stock OpcodeTablePartition(low, high) {
new pivot = ReadAmxMemory(high);
new j = low;
for (low -= 2 * cellbytes; j != high; j += 2 * cellbytes) {
if (ReadAmxMemory(j) < pivot) {
// Swap.
low += 2 * cellbytes,
OpcodeTableSwap(low, j);
}
}
return
low += 2 * cellbytes,
OpcodeTableSwap(low, high),
low;
}
/// <library>amx_assembly opcode</library>
static stock OpcodeTableQuickSort(low, high) {
if (low < high) {
new partition = OpcodeTablePartition(low, high);
OpcodeTableQuickSort(low, partition - (2 * cellbytes));
OpcodeTableQuickSort(partition + (2 * cellbytes), high);
}
}
/// <library>amx_assembly opcode</library>
static stock OpcodeTableWrite(addr, i) {
// Loop through all the opcodes in the table and replace them.
new Opcode:op;
while (i--) {
op = Opcode:ReadAmxMemory(addr),
op = opcode_lookup[_:op],
WriteAmxMemory(addr, _:op),
addr += 2 * cellbytes;
}
}
/// <library>amx_assembly opcode</library>
static stock InitOpcodeTable() {
new Opcode:op;
for (new i = 0; i < NUM_OPCODES; i++) {
op = Opcode:i;
opcode_lookup[i] = RelocateOpcodeNow(op);
}
opcode_table_is_ready = true;
// Now rewrite the giant switch in `UnsafeUnrelocateOpcode` for speed. This
// is extra fun because we can't use any of the features provided by this
// file or `disasm.inc` to do the code reading! So instead we need a very
// very very simple system to decompile code here. Fortuantely it only
// needs to be run on like three instructions, so can be slow.
new hdr[AMX_HDR];
GetAmxHeader(hdr);
for (new addr = GetPublicAddressFromName("UnsafeUnrelocateOpcode") + hdr[AMX_HDR_COD] - hdr[AMX_HDR_DAT]; ; ) {
op = Opcode:ReadAmxMemory(addr);
// Slowly determine the opcode.
for (new Opcode:i = OP_NONE; i < OP_LAST_; i++) {
if (op == opcode_lookup[_:i]) {
if (i == OP_SWITCH) {
// Jump straight to the casetable, to save time.
addr = ReadAmxMemory(addr + cellbytes) - GetAmxBaseAddress() - hdr[AMX_HDR_DAT];
} else if (i == OP_CASETBL) {
// Found the instruction we want. Do the complete rewrite.
addr += cellbytes,
i = Opcode:ReadAmxMemory(addr),
OpcodeTableWrite(addr + 2 * cellbytes, _:i),
OpcodeTableQuickSort(addr + 2 * cellbytes, addr + _:i * (2 * cellbytes));
return;
} else {
addr += gAMXOpcodeBaseSizes[_:i];
}
break;
}
}
}
}
/// <library>amx_assembly opcode</library>
stock Opcode:RelocateOpcode(Opcode:opcode) {
if (!opcode_table_is_ready) {
InitOpcodeTable();
}
return opcode_lookup[_:opcode];
}
/// <library>amx_assembly opcode</library>
stock bool:IsOpcodeRelocationRequired() {
return RelocateOpcode(OP_LOAD_PRI) != OP_LOAD_PRI;
}
// Why is this function public? So that we can get its address without
// `addressof.inc`, only `amx_header.inc`.
/// <library>amx_assembly opcode</library>
public Opcode:UnsafeUnrelocateOpcode(Opcode:opcode) {
// This code seems pointless. It isn't. It gets rewritten at runtime based
// on the true opcode internal values, so the whole function is a switch on
// different values than the ones shown here.
switch (opcode) {
case OP_LOAD_PRI: return OP_LOAD_PRI;
case OP_LOAD_ALT: return OP_LOAD_ALT;
case OP_LOAD_S_PRI: return OP_LOAD_S_PRI;
case OP_LOAD_S_ALT: return OP_LOAD_S_ALT;
case OP_LREF_PRI: return OP_LREF_PRI;
case OP_LREF_ALT: return OP_LREF_ALT;
case OP_LREF_S_PRI: return OP_LREF_S_PRI;
case OP_LREF_S_ALT: return OP_LREF_S_ALT;
case OP_LOAD_I: return OP_LOAD_I;
case OP_LODB_I: return OP_LODB_I;
case OP_CONST_PRI: return OP_CONST_PRI;
case OP_CONST_ALT: return OP_CONST_ALT;
case OP_ADDR_PRI: return OP_ADDR_PRI;
case OP_ADDR_ALT: return OP_ADDR_ALT;
case OP_STOR_PRI: return OP_STOR_PRI;
case OP_STOR_ALT: return OP_STOR_ALT;
case OP_STOR_S_PRI: return OP_STOR_S_PRI;
case OP_STOR_S_ALT: return OP_STOR_S_ALT;
case OP_SREF_PRI: return OP_SREF_PRI;
case OP_SREF_ALT: return OP_SREF_ALT;
case OP_SREF_S_PRI: return OP_SREF_S_PRI;
case OP_SREF_S_ALT: return OP_SREF_S_ALT;
case OP_STOR_I: return OP_STOR_I;
case OP_STRB_I: return OP_STRB_I;
case OP_LIDX: return OP_LIDX;
case OP_LIDX_B: return OP_LIDX_B;
case OP_IDXADDR: return OP_IDXADDR;
case OP_IDXADDR_B: return OP_IDXADDR_B;
case OP_ALIGN_PRI: return OP_ALIGN_PRI;
case OP_ALIGN_ALT: return OP_ALIGN_ALT;
case OP_LCTRL: return OP_LCTRL;
case OP_SCTRL: return OP_SCTRL;
case OP_MOVE_PRI: return OP_MOVE_PRI;
case OP_MOVE_ALT: return OP_MOVE_ALT;
case OP_XCHG: return OP_XCHG;
case OP_PUSH_PRI: return OP_PUSH_PRI;
case OP_PUSH_ALT: return OP_PUSH_ALT;
case OP_PUSH_R: return OP_PUSH_R;
case OP_PUSH_C: return OP_PUSH_C;
case OP_PUSH: return OP_PUSH;
case OP_PUSH_S: return OP_PUSH_S;
case OP_POP_PRI: return OP_POP_PRI;
case OP_POP_ALT: return OP_POP_ALT;
case OP_STACK: return OP_STACK;
case OP_HEAP: return OP_HEAP;