-
Notifications
You must be signed in to change notification settings - Fork 14
/
dynamic_call.inc
935 lines (770 loc) · 19.2 KB
/
dynamic_call.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
// 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 DYNAMIC_CALL_INC
#endinput
#endif
#define DYNAMIC_CALL_INC
/**
* <library name="amx_assembly dynamic_call" summary="AMX Assembly; Call arbitrary functions by name.">
* <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_jit"
#include "amx_header"
#include "amx_memory"
#include "opcode"
#include "addressof"
#if !defined DYNAMIC_CALL_MAX_ARGS
#define DYNAMIC_CALL_MAX_ARGS 256
#endif
forward bool:Push(arg);
forward bool:PushString(const string[]);
forward bool:Pop(&arg = 0);
forward Call(address, bool:auto_pop = true);
forward SysreqC(index, bool:auto_pop = true);
forward SysreqD(address, bool:auto_pop = true);
forward CallN(address, args_to_push, bool:auto_pop = true);
forward SysreqCN(index, args_to_push, bool:auto_pop = true);
forward SysreqDN(address, args_to_push, bool:auto_pop = true);
forward CallFunction(address, {Float,_}:...);
forward CallNative(index, {Float,_}:...);
forward CallNativeByAddress(address, {Float,_}:...);
/// <library>amx_assembly dynamic_call</library>
/// <remarks>
/// When you declare an alternative version of a function like this you end up
/// with two copies in the header. As long as we never actually use this
/// function we can keep changing the pointer to whatever we want and call the
/// native via `SYSREQ.C`. Note that if we do this we need to check if
/// `SYSREQ.D` is still in the server because the `SYSREQ.C` will be replaced.
/// </remarks>
native AmxSysreqDStub() = heapspace;
/// <library>amx_assembly dynamic_call</library>
static stock Opcode:UpdateSysreqDAndGetIndex(&address, wide) {
// This function handily ensures that the function is in the header on old
// compilers which don't insert it when used via `SYSREQ.C`.
static index = cellmin;
static Opcode:sysreq_c = OP_NONE;
static Opcode:sysreq_d = OP_NONE;
static off = 0;
if (index == cellmin) {
sysreq_d = RelocateOpcode(OP_SYSREQ_D);
// `nativeidxof` is a bypass for the fact that the header may be
// clobbered, which this new `SYSREQ.D` bypass is also for.
index = nativeidxof(AmxSysreqDStub<>);
sysreq_c = RelocateOpcode(OP_SYSREQ_C);
new amxhdr[AMX_HDR];
GetAmxHeader(amxhdr);
off = amxhdr[AMX_HDR_NATIVES] - amxhdr[AMX_HDR_DAT] + index * amxhdr[AMX_HDR_DEFSIZE];
}
if (wide == 0 && HasSysreqD()) {
return sysreq_d;
}
// Change where the native points.
WriteAmxMemory(off, address);
if (!HasReloc()) {
WriteAmxMemory(off + cellbytes, wide);
}
address = index;
return sysreq_c;
}
/// <library>amx_assembly dynamic_call</library>
static stock g_nargs = 0;
/// <library>amx_assembly dynamic_call</library>
static stock g_args[DYNAMIC_CALL_MAX_ARGS];
/// <library>amx_assembly dynamic_call</library>
stock bool:Push(arg) {
if (g_nargs < sizeof(g_args)) {
g_args[g_nargs++] = arg;
return true;
}
return false;
}
/// <library>amx_assembly dynamic_call</library>
stock bool:PushString(const string[]) {
new address = 0;
#emit load.s.pri string
#emit stor.s.pri address
return Push(address);
}
/// <library>amx_assembly dynamic_call</library>
stock bool:Pop(&arg = 0) {
if (g_nargs > 0) {
arg = g_args[--g_nargs];
return true;
}
return false;
}
/// <library>amx_assembly dynamic_call</library>
stock Call(address, bool:auto_pop = true) {
const cells0 = 9 * cellbytes;
new arg = 0;
new index = g_nargs;
new bytes = g_nargs * cellbytes;
new retval = 0;
while (--index >= 0) {
arg = g_args[index];
#emit push.s arg
}
#emit load.s.pri bytes
#emit push.pri
#emit lctrl __cip
#emit add.c cells0
#emit lctrl __jmp
#emit push.pri
#emit load.s.pri address
#emit sctrl __cip
#emit stor.s.pri retval
if (auto_pop) {
g_nargs = 0;
}
return retval;
}
/// <library>amx_assembly dynamic_call</library>
stock CallN(address, args_to_push, bool:auto_pop = true) {
const cells0 = 9 * cellbytes;
// Like "Call", but doesn't pass all parameters.
new arg = 0;
new index = g_nargs;
new bytes = args_to_push * cellbytes;
new end = g_nargs - args_to_push;
new retval = 0;
if (end < 0) {
return cellmin;
}
while (--index >= end) {
arg = g_args[index];
#emit push.s arg
}
#emit load.s.pri bytes
#emit push.pri
#emit lctrl __cip
#emit add.c cells0
#emit lctrl __jmp
#emit push.pri
#emit load.s.pri address
#emit sctrl __cip
#emit stor.s.pri retval
if (auto_pop) {
g_nargs = end;
}
return retval;
}
/// <library>amx_assembly dynamic_call</library>
stock CallFunction(address, {Float,_}:...) {
const cells_ = 2 * cellbytes;
const cells0 = 1 * cellbytes;
const cells1 = 3 * cellbytes;
const cells2 = 4 * cellbytes;
const cells3 = 9 * cellbytes;
const cells4 = 4 * cellbytes;
new arg_bytes = 0, arg_begin = 0, arg_end = 0;
// Get number of bytes passed.
#emit load.s.pri cells_
#emit const.alt cells0
#emit sub
#emit stor.s.pri arg_bytes
#emit move.alt
// Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
#emit lctrl __frm
#emit add.c cells1
#emit add
#emit stor.s.pri arg_end
// Frist argument is at FRM + 0x10.
#emit lctrl __frm
#emit add.c cells2
#emit stor.s.pri arg_begin
new arg = arg_end;
while (arg >= arg_begin) {
#emit lref.s.pri arg
#emit load.i
#emit push.pri
arg -= cellbytes;
}
// Call the function
#emit push.s arg_bytes
#emit lctrl __cip
#emit add.c cells3
#emit lctrl __jmp
#emit push.pri
#emit load.s.pri address
#emit sctrl __cip
// Arguments are popped by callee.
// Pop locals and return.
#emit stack cells4
#emit retn
return 0; // make compiler happy
}
/// <library>amx_assembly dynamic_call</library>
stock SysreqC(index, bool:auto_pop = true) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = 23 * cellbytes;
const cells1 = 1 * cellbytes;
const cells2 = 1 * cellbytes;
new arg = 0;
new i = g_nargs;
new bytes = g_nargs * cellbytes;
new tmp = 0;
new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
new retval = 0;
while (--i >= 0) {
arg = g_args[i];
#emit push.s arg
}
#emit load.s.pri bytes
#emit push.pri
// tmp = cod + cip - dat + <distance to SYSREQ.C's operand>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells0
#emit stor.s.pri tmp
// nop #1 = sysreq.c
#emit load.s.pri sysreq_c
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells1
#emit stor.s.pri tmp
// nop #2 = index
#emit load.s.pri index
#emit sref.s.pri tmp
#emit nop
#emit nop
// #emit sysreq.c 0
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt bytes
#emit add
#emit add.c cells2
#emit sctrl __stk
if (auto_pop) {
g_nargs = 0;
}
return retval;
}
/// <library>amx_assembly dynamic_call</library>
stock SysreqD(address, bool:auto_pop = true) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = 23 * cellbytes;
const cells1 = 1 * cellbytes;
const cells2 = 1 * cellbytes;
new arg = 0;
new i = g_nargs;
new bytes = g_nargs * cellbytes;
new tmp = 0;
new Opcode:sysreq = UpdateSysreqDAndGetIndex(address, 0);
new retval = 0;
while (--i >= 0) {
arg = g_args[i];
#emit push.s arg
}
#emit load.s.pri bytes
#emit push.pri
// tmp = cod + cip - dat + <distance to nop #1>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells0
#emit stor.s.pri tmp
// nop #1 = sysreq.d
#emit load.s.pri sysreq
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells1
#emit stor.s.pri tmp
// nop #2 = address
#emit load.s.pri address
#emit sref.s.pri tmp
#emit nop
#emit nop
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt bytes
#emit add
#emit add.c cells2
#emit sctrl __stk
if (auto_pop) {
g_nargs = 0;
}
return retval;
}
/// <library>amx_assembly dynamic_call</library>
stock SysreqDWide(address, wide, bool:auto_pop = true) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = 23 * cellbytes;
const cells1 = 1 * cellbytes;
const cells2 = 1 * cellbytes;
new arg = 0;
new i = g_nargs;
new bytes = g_nargs * cellbytes;
new tmp = 0;
new Opcode:sysreq = UpdateSysreqDAndGetIndex(address, wide);
new retval = 0;
while (--i >= 0) {
arg = g_args[i];
#emit push.s arg
}
#emit load.s.pri bytes
#emit push.pri
// tmp = cod + cip - dat + <distance to nop #1>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells0
#emit stor.s.pri tmp
// nop #1 = sysreq.d
#emit load.s.pri sysreq
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells1
#emit stor.s.pri tmp
// nop #2 = address
#emit load.s.pri address
#emit sref.s.pri tmp
#emit nop
#emit nop
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt bytes
#emit add
#emit add.c cells2
#emit sctrl __stk
if (auto_pop) {
g_nargs = 0;
}
return retval;
}
/// <library>amx_assembly dynamic_call</library>
stock SysreqCN(index, args_to_push, bool:auto_pop = true) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = 23 * cellbytes;
const cells1 = 1 * cellbytes;
const cells2 = 1 * cellbytes;
new arg = 0;
new i = g_nargs;
new bytes = args_to_push * cellbytes;
new tmp = 0;
new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
new end = g_nargs - args_to_push;
new retval = 0;
if (end < 0) {
return cellmin;
}
while (--i >= end) {
arg = g_args[i];
#emit push.s arg
}
#emit load.s.pri bytes
#emit push.pri
// tmp = cod + cip - dat + <distance to SYSREQ.C's operand>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells0
#emit stor.s.pri tmp
// nop #1 = sysreq.c
#emit load.s.pri sysreq_c
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells1
#emit stor.s.pri tmp
// nop #2 = index
#emit load.s.pri index
#emit sref.s.pri tmp
#emit nop
#emit nop
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt bytes
#emit add
#emit add.c cells2
#emit sctrl __stk
if (auto_pop) {
g_nargs = end;
}
return retval;
}
/// <library>amx_assembly dynamic_call</library>
stock SysreqDN(address, args_to_push, bool:auto_pop = true) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = 23 * cellbytes;
const cells1 = 1 * cellbytes;
const cells2 = 1 * cellbytes;
new arg = 0;
new i = g_nargs;
new bytes = args_to_push * cellbytes;
new tmp = 0;
new Opcode:sysreq = UpdateSysreqDAndGetIndex(address, 0);
new end = g_nargs - args_to_push;
new retval = 0;
if (end < 0) {
return cellmin;
}
while (--i >= end) {
arg = g_args[i];
#emit push.s arg
}
#emit load.s.pri bytes
#emit push.pri
// tmp = cod + cip - dat + <distance to nop #1>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells0
#emit stor.s.pri tmp
// nop #1 = sysreq.d
#emit load.s.pri sysreq
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells1
#emit stor.s.pri tmp
// nop #2 = address
#emit load.s.pri address
#emit sref.s.pri tmp
#emit nop
#emit nop
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt bytes
#emit add
#emit add.c cells2
#emit sctrl __stk
if (auto_pop) {
g_nargs = end;
}
return retval;
}
/// <library>amx_assembly dynamic_call</library>
stock SysreqDNWide(address, wide, args_to_push, bool:auto_pop = true) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = 23 * cellbytes;
const cells1 = 1 * cellbytes;
const cells2 = 1 * cellbytes;
new arg = 0;
new i = g_nargs;
new bytes = args_to_push * cellbytes;
new tmp = 0;
new Opcode:sysreq = UpdateSysreqDAndGetIndex(address, wide);
new end = g_nargs - args_to_push;
new retval = 0;
if (end < 0) {
return cellmin;
}
while (--i >= end) {
arg = g_args[i];
#emit push.s arg
}
#emit load.s.pri bytes
#emit push.pri
// tmp = cod + cip - dat + <distance to nop #1>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells0
#emit stor.s.pri tmp
// nop #1 = sysreq.d
#emit load.s.pri sysreq
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells1
#emit stor.s.pri tmp
// nop #2 = address
#emit load.s.pri address
#emit sref.s.pri tmp
#emit nop
#emit nop
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt bytes
#emit add
#emit add.c cells2
#emit sctrl __stk
if (auto_pop) {
g_nargs = end;
}
return retval;
}
/// <library>amx_assembly dynamic_call</library>
stock CallNative(index, {Float,_}:...) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = -1 * cellbytes;
const cells1 = 3 * cellbytes;
const cells2 = 4 * cellbytes;
const cells3 = 23 * cellbytes;
const cells4 = 1 * cellbytes;
const cells5 = 1 * cellbytes;
new arg_bytes = 0, arg_begin = 0, arg_end = 0;
new Opcode:sysreq_c = RelocateOpcode(OP_SYSREQ_C);
// Get number of bytes passed.
#emit load.s.pri 0x8
#emit add.c cells0
#emit stor.s.pri arg_bytes
#emit move.alt
// Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
#emit lctrl __frm
#emit add.c cells1
#emit add
#emit stor.s.pri arg_end
// Frist argument is at FRM + 0x10.
#emit lctrl __frm
#emit add.c cells2
#emit stor.s.pri arg_begin
new arg = arg_end;
new tmp = 0;
new retval = 0;
while (arg >= arg_begin) {
#emit lref.s.pri arg
#emit load.i
#emit push.pri
arg -= cellbytes;
}
// Push number of arguments * 4 (which is params[0]).
#emit push.s arg_bytes
// tmp = cod + cip - dat + <distance to nop #1>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells3
#emit stor.s.pri tmp
// nop #1 = sysreq.c
#emit load.s.pri sysreq_c
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells4
#emit stor.s.pri tmp
// nop #2 = index
#emit load.s.pri index
#emit sref.s.pri tmp
#emit nop
#emit nop
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt arg_bytes
#emit add
#emit add.c cells5
#emit sctrl __stk
return retval;
}
/// <library>amx_assembly dynamic_call</library>
/// <remarks>
/// Unlike CallNative(), this function calls natives directly via SYSREQ.D.
/// </remarks>
stock CallNativeByAddress(address, {Float,_}:...) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = -1 * cellbytes;
const cells1 = 3 * cellbytes;
const cells2 = 4 * cellbytes;
const cells3 = 23 * cellbytes;
const cells4 = 1 * cellbytes;
const cells5 = 1 * cellbytes;
new arg_bytes = 0, arg_begin = 0, arg_end = 0;
new Opcode:sysreq = UpdateSysreqDAndGetIndex(address, 0);
// Get number of bytes passed.
#emit load.s.pri 0x8
#emit add.c cells0
#emit stor.s.pri arg_bytes
#emit move.alt
// Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
#emit lctrl __frm
#emit add.c cells1
#emit add
#emit stor.s.pri arg_end
// Frist argument is at FRM + 0x10.
#emit lctrl __frm
#emit add.c cells2
#emit stor.s.pri arg_begin
new arg = arg_end;
new tmp = 0;
new retval = 0;
while (arg >= arg_begin) {
#emit lref.s.pri arg
#emit load.i
#emit push.pri
arg -= cellbytes;
}
// Push number of arguments * 4 (which is params[0]).
#emit push.s arg_bytes
// tmp = cod + cip - dat + <distance to nop #1>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells3
#emit stor.s.pri tmp
// nop #1 = sysreq.d
#emit load.s.pri sysreq
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells4
#emit stor.s.pri tmp
// nop #2 = address
#emit load.s.pri address
#emit sref.s.pri tmp
#emit nop
#emit nop
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt arg_bytes
#emit add
#emit add.c cells5
#emit sctrl __stk
return retval;
}
/// <library>amx_assembly dynamic_call</library>
/// <remarks>
/// Unlike CallNative(), this function calls natives directly via SYSREQ.D.
/// Takes two pointers to replicate 64-bit pointers.
/// </remarks>
stock CallNativeByAddressWide(address, wide, {Float,_}:...) {
if (GetJITGeneratorVersion()) {
return cellmin;
}
const cells0 = -1 * cellbytes;
const cells1 = 3 * cellbytes;
const cells2 = 4 * cellbytes;
const cells3 = 23 * cellbytes;
const cells4 = 1 * cellbytes;
const cells5 = 1 * cellbytes;
new arg_bytes = 0, arg_begin = 0, arg_end = 0;
new Opcode:sysreq = UpdateSysreqDAndGetIndex(address, wide);
// Get number of bytes passed.
#emit load.s.pri 0x8
#emit add.c cells0
#emit stor.s.pri arg_bytes
#emit move.alt
// Last argument is at FRM + 0x0C + arg_bytes (which is in ALT).
#emit lctrl __frm
#emit add.c cells1
#emit add
#emit stor.s.pri arg_end
// Frist argument is at FRM + 0x10.
#emit lctrl __frm
#emit add.c cells2
#emit stor.s.pri arg_begin
new arg = arg_end;
new tmp = 0;
new retval = 0;
while (arg >= arg_begin) {
#emit lref.s.pri arg
#emit load.i
#emit push.pri
arg -= cellbytes;
}
// Push number of arguments * 4 (which is params[0]).
#emit push.s arg_bytes
// tmp = cod + cip - dat + <distance to nop #1>
#emit lctrl __cod // COD
#emit move.alt
#emit lctrl __cip // CIP
#emit add
#emit move.alt
#emit lctrl __dat // DAT
#emit sub.alt
#emit add.c cells3
#emit stor.s.pri tmp
// nop #1 = sysreq.d
#emit load.s.pri sysreq
#emit sref.s.pri tmp
// tmp += 4
#emit load.s.pri tmp
#emit add.c cells4
#emit stor.s.pri tmp
// nop #2 = address
#emit load.s.pri address
#emit sref.s.pri tmp
#emit nop
#emit nop
#emit stor.s.pri retval
// Pop native arguments.
#emit lctrl __stk
#emit load.s.alt arg_bytes
#emit add
#emit add.c cells5
#emit sctrl __stk
return retval;
}