-
Notifications
You must be signed in to change notification settings - Fork 5
/
indirection.inc
1791 lines (1675 loc) · 48.6 KB
/
indirection.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
/*
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is the indirection.inc function pointers library.
The Initial Developer of the Original Code is Alex "Y_Less" Cole.
Portions created by the Initial Developer are Copyright (c) 2022
the Initial Developer. All Rights Reserved.
*/
#if defined _INC_indirection
#endinput
#endif
#define _INC_indirection
/**
* <library
* name="indirection"
* summary="Function pointers and code redirection."
* license="Copyright (c) 2022 Alex "Y_Less" Cole. Licensed under MPL 1.1"
* >
* <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>
* <remarks>
* Indirection is a system for calling function pointers in a generic and
* type-safe way. Instead of <c>CallLocalFunction</c>, <c>Call</c>,
* <c>defer</c>, <c>Callback_Call</c>, or any other method, this gives one
* common interface which can be extended by library authors; utilising
* tags for compile-time parameters.
* </remarks>
* </library>
*/
/// <p/>
#if !defined AMX_HEADER_INC
#tryinclude "..\amx\amx_header"
#endif
#if !defined AMX_HEADER_INC
#tryinclude <amx_assembly\amx_header>
#endif
#if !defined AMX_HEADER_INC
#tryinclude "..\amx_assembly\amx_header"
#endif
#if !defined AMX_HEADER_INC
#tryinclude "..\..\amx_assembly\amx_header"
#endif
#if !defined AMX_HEADER_INC
#tryinclude "amx_header"
#endif
#if !defined AMX_HEADER_INC
#tryinclude <amx_header>
#endif
#if !defined AMX_HEADER_INC
#define AMX_INCLUDING_FAILED
#endif
#if defined AMX_INCLUDING_FAILED
#error Could not include "https://github.com/Zeex/amx_assembly" - ensure its files are in "include\amx_assembly\"
//#elseif !defined AddressofResolve
// #error Please update "https://github.com/Zeex/amx_assembly" to the latest version.
#endif
#if defined CUSTOM_TAG_TYPES
#define GLOBAL_TAG_TYPES {_,Bit,Text,Group,File,Float,Text3D,CUSTOM_TAG_TYPES}
#else
#define GLOBAL_TAG_TYPES {_,Bit,Text,Group,File,Float,Text3D}
#endif
#if !defined INDIRECTION_META_DATA_SIZE
#define INDIRECTION_META_DATA_SIZE (8)
#endif
enum E_INDIRECTION
{
E_INDIRECTION_ALWAYS_NULL, // So we can tell this is not a string.
E_INDIRECTION_HANDER, // Called by `@` to handle this pointer.
E_INDIRECTION_CLAIM, // Called by `Indirect_Claim`.
E_INDIRECTION_RELEASE, // Called by `Indirect_Release`.
E_INDIRECTION_METADATA, // Only used by end-users.
E_INDIRECTION_TAG, // Save the encoded parameters tag of this callback.
E_INDIRECTION_OWNER, // Attach the function to players etc.
E_INDIRECTION_NEXT // Next pointer in a generic list of callbacks.
}
stock
INDIRECTION_META[INDIRECTION_META_DATA_SIZE],
INDIRECTION_COUNT = 0,
INDIRECTION_DATA = 0,
INDIRECTION_TAG = 0;
// Constant offsets for assembly.
const
E_INDIRECTION_ALWAYS_NULL__ = _:E_INDIRECTION_ALWAYS_NULL * cellbytes,
E_INDIRECTION_HANDER__ = _:E_INDIRECTION_HANDER * cellbytes,
E_INDIRECTION_CLAIM__ = _:E_INDIRECTION_CLAIM * cellbytes,
E_INDIRECTION_RELEASE__ = _:E_INDIRECTION_RELEASE * cellbytes,
E_INDIRECTION_METADATA__ = _:E_INDIRECTION_METADATA * cellbytes,
E_INDIRECTION_TAG__ = _:E_INDIRECTION_TAG * cellbytes,
E_INDIRECTION_OWNER__ = _:E_INDIRECTION_OWNER * cellbytes,
E_INDIRECTION_NEXT__ = _:E_INDIRECTION_NEXT * cellbytes,
E_INDIRECTION__ = _:E_INDIRECTION * cellbytes;
stock const
INDIRECTION_NAUGHT = 0;
static stock
INDR_gsAddr = 0,
INDR_gsTmp = 0,
INDR_gsTag[64],
INDR_gsCodSize = 0, // The size of `COD`.
INDR_gsKnownOwnedCallbacks = 0;
#if !defined YSI_MAX_STRING
#define YSI_MAX_STRING (144)
#endif
#define string:
#define void:
#define PP_LEFT_BRACKET<> (
#if !defined TAGOF
#if ((__Pawn & 0x0F) >= 0x0A) || ((__Pawn & 0xF0) >= 0xA0)
// Defer `tagof` on things with colons slightly to remove anything after
// it. This lets us do `tagof (Float:i)` with no problem, since the `i`
// is removed.
#define TAGOF(%0);
#define __TAGOF__[%0] %0:
#define tagof(%0:%1) __NO_TAGOF:__IS_TAGOF:tagof PP_LEFT_BRACKET<>%0:$%1)
#define __NO_TAGOF:__IS_TAGOF:tagof%9<>%0)%2:$%1) tagof%9<>%0)%2:%1)
#define __IS_TAGOF:tagof%9<>%0:$%1) tagof%9<>__TAGOF__[%0])
#elseif defined __COMPILER_TAG_DATA
#define TAGOF(%0);
#else
#define TAGOF(%0); stock const %0:__TAGOF__%0;
#define __TAGOF__%0\32; __TAGOF__
#define tagof(%0:%1) __NO_TAGOF:__IS_TAGOF:tagof PP_LEFT_BRACKET<>%0:$%1)
#define __NO_TAGOF:__IS_TAGOF:tagof%9<>%0)%2:$%1) tagof%9<>%0)%2:%1)
#define __IS_TAGOF:tagof%9<>%0:$%1) tagof%9<>__TAGOF__%0)
#endif
#if !defined __COMPILER_TAG_DATA
stock bool:operator==(_:a, __NO_TAGOF:b)
{
return a == _:b;
}
stock bool:operator!=(_:a, __NO_TAGOF:b)
{
return a != _:b;
}
#endif
#define __TAGOF__F@_@%0\32; __TAGOF__F@_@
#if defined __COMPILER_TAG_DATA
#define SPECIFIER(%0) __COMPILER_TAG_DATA(F@_@%0,0)
#else
#define SPECIFIER(%0) TAGOF(F@_@%0)
#endif
#endif
#if defined __PawnBuild
// Because I somewhat scuppered myself!
#if __PawnBuild == 0
#define INDIRECTION_FUNC_INC__
#undef __PawnBuild
#endif
#else
#define INDIRECTION_FUNC_INC__
#endif
#if defined INDIRECTION_FUNC_INC__
forward __Indirect_FuncInc__();
public __Indirect_FuncInc__()
{
memcpy
("", "", 0, 0, 0);
funcidx
("");
}
#undef INDIRECTION_FUNC_INC__
#endif
TAGOF(Float);
TAGOF(File);
// Technically the function tags are not always `F@_@`. The third character can
// be anything, and is reserved for representing the return type. However, it
// is currently not used. DO NOT consume spaces after `F@_@` since this will
// break functions with no parameters since their tag is just `F@_@:` (or
// `F@_@_`). Function tags could be weak, which means if you pass a pointer to
// a function that wants a different type, or pass an untyped pointer to a
// function that wants any type, you will get a mismatch warning. However, you
// can pass a typed pointer to a function that doesn't expect any type for
// backwards-compatability reasons. I decided against this though - make them
// strong and give warnings on all old code!
#define Func:%0<%1> F@_@%1:%0
// Examples of predeclarations of specifier types. These fix a bug in the old
// compiler where you can't (always) do `tagof (Float:)`. This is relevant here
// since the specifiers are passed as `F@_@i:Func`, so we need `tagof (F@_@i:)`
// to get at the specifier. However, since that doesn't work, we instead need
// `tagof (__TAGOF__F@_@i)` with `stock const F@_@i:__TAGOF__F@_@i`. I.e. we need a
// variable using the tag to get the tag from, instead of getting it directly.
SPECIFIER(i);
SPECIFIER(s);
// This is for getting excess parameters on non-strings.
#define _:%0,) _:%0)
// I did consider an alternate method of making `@` do this:
//
// #define @.%0(%1) Indirect_Data(%0),Indirect_Call(%1)
//
// But that would fail when an `@` call contained another `@` call:
//
// @.Func1(@.Func2(42, 43));
//
// It would save the stack manipulation though (not convinced it would be less
// code though with the generation of multiple function calls).
#define @ str_new_static
// V2 design, with meta-data on `@`. Can now do:
//
// @.var(params)
// @.&func<iis>(params)
// @.&func(params)
// @.func[meta](params)
//
#define str_new_static.%0(%1) (Indirect_Call__(_:(INDIRECTION_DATA=_:%0,INDIRECTION_TAG=tagof(%0),Indirect_Meta()),%1))
/// <library>indirection</library>
native Indirect_Memcpy(dest[], source, index = 0, numbytes, maxlength = sizeof (dest)) = memcpy;
/// <library>indirection</library>
stock Indirect_Meta(GLOBAL_TAG_TYPES:...)
{
// Offset to the first parameter.
const cells0 = 3 * cellbytes;
const cells1 = 2 * cellbytes;
new meta;
#emit ADDR.pri cells0
#emit STOR.S.pri meta
#emit LOAD.S.pri cells1
#emit STOR.pri INDIRECTION_COUNT
Indirect_Memcpy(INDIRECTION_META, meta, 0, INDIRECTION_COUNT, INDIRECTION_META_DATA_SIZE);
return (INDIRECTION_COUNT /= cellbytes);
}
// Look for `&` for variable dereferencing.
#define INDIRECTION_DATA=_:%9&%0,INDIRECTION_TAG=tagof(%9), INDIRECTION_DATA=_:INDIR2_:addressof(%0),INDIRECTION_TAG=prototypeof(%0),
// Now we know this is an indirection call, are there any meta-data params?
#define Indirect_Call__(_:(INDIRECTION_DATA=_:%0[%2]%6,INDIRECTION_TAG=tagof(%4),Indirect_Meta()),%3) Indirect_Call__(_:(INDIRECTION_DATA=_:%0%6,INDIRECTION_TAG=tagof(%4),Indirect_Meta(%2)),%3)
// Look for `<>` generic tag types.
#define INDIR2_:addressof(%0<%4>),INDIRECTION_TAG=prototypeof(%9), addressof(%0<%4>),F@_@%4:INDIRECTION_TAG=F@_@%4:tagof(F@_@%4:),
// The `ALS_DO` callback syntax is `CALLBACK<name, specifier>(params)`
#define Indirect_DoCallback<%0,%4>(%9) O@A@("On"#%0),F@_@%4:INDIRECTION_TAG=F@_@%4:tagof(F@_@%4:),
// Simplify the complex ALS syntax.
//#define Indirect_ALS<%0><%1> %1<,%0>()
// Example:
//
// #define ALS_DO_PlayerConnect Indirect_ALSTag<i>
//
// Can now do:
//
// @.var(params)
// @.&func<iis>(params)
// @.func[meta](params)
// @.OnPlayerConnect(params)
//
/// <library>indirection</library>
forward Indirect_Call(func, tag, GLOBAL_TAG_TYPES:...);
// Old API.
#define Indirect_Call(%0,%1) (Indirect_Call__(_:INDIR1_:(INDIRECTION_DATA=(%0),INDIRECTION_TAG=(%1),Indirect_Meta())))
#define INDIR1_:(INDIRECTION_DATA=(%0),INDIRECTION_TAG=(%1,%2),Indirect_Meta())) (INDIRECTION_DATA=(%0),INDIRECTION_TAG=(%1),Indirect_Meta()),%2)
#define Indirect_Meta() INDIRECTION_NAUGHT
/// <library>indirection</library>
stock Indirect_Call__(meta, GLOBAL_TAG_TYPES:...)
{
const cells0 = 4 * cellbytes;
const cells1 = -1 * cellbytes;
const cells3 = 1 * cellbytes;
const cells4 = 2 * cellbytes;
#pragma unused meta
if (!INDIRECTION_DATA)
{
return 0;
}
{}
// The COD and DAT segments are different, so `func` pointing in to DAT
// relative to COD will not be a valid pointer, and THAT you can detect!
//
// Get the previous frame. This undoes the effects of our `PROC` so the
// called function must have a proper `PROC`.
#emit ADDR.pri cells0
#emit STOR.pri INDR_gsAddr
#emit POP.pri
#emit SCTRL 5
// Get the return address.
#emit POP.alt
// Get the parameter count.
#emit POP.pri
// Reduce the parameter count.
#emit ADD.C cells1
// Store the new parameter count.
#emit SWAP.pri
#emit STOR.pri INDIRECTION_COUNT
// Store the return address.
#emit PUSH.alt
// Check the pointer type. If it is in the `COD` area, jump straight to it
// with the tag for parameter types (if it isn't 0). Otherwise, use the
// `func` from `DAT` as a pointer to a handler.
if (INDIRECTION_DATA >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.pri INDIRECTION_DATA
#emit LOAD.alt INDR_gsCodSize
#emit SUB
#emit MOVE.alt
#emit LOAD.I
#emit STOR.pri INDIRECTION_DATA
if (!INDIRECTION_DATA)
{
// Get the function at `func - COD + 4`.
#emit CONST.pri E_INDIRECTION_HANDER__
#emit ADD
#emit LOAD.I
// Call it, passing `func` as a proper pointer, NOT skipping `PROC`.
#emit STOR.alt INDIRECTION_DATA
#emit SCTRL 6
// NEVER RETURNS PAST HERE.
}
{}
// `INDIRECTION_DATA` is now a pointer to a string of a function name.
// Resolve it via index lookup.
#emit PUSH.alt
#emit PUSH.C cells3
#emit SYSREQ.C funcidx
#emit STACK cells4
#emit STOR.pri INDIRECTION_DATA
if (INDIRECTION_DATA == -1)
{
#emit PROC
#emit RETN
}
{}
// Get the address from the index.
#if cellbits == 64
const shifter = 3;
#else
const shifter = 2;
#endif
#emit LCTRL __dat
#emit NEG
#emit ADD.C 32 // Always this, regardless of cell size.
#emit STOR.pri INDR_gsTmp
#emit LREF.alt INDR_gsTmp
#emit LCTRL __dat
#emit NEG
#emit ADD
// This replicates `IDXADDR`, but in no more instructions since the data
// are in the wrong registers.
#emit LOAD.alt INDIRECTION_DATA
#emit SHL.C.alt shifter
#emit ADD
// Add more data, to account for `__defsize` offsets.
#emit LOAD.alt INDIRECTION_DATA
#emit SHL.C.alt 2
#emit ADD
#emit STOR.pri INDR_gsTmp
#emit LREF.pri INDR_gsTmp
#emit STOR.pri INDIRECTION_DATA
}
if (INDIRECTION_TAG)
{
// Skip the `F@_@` prefix.
GetTagNameFromID(INDIRECTION_TAG, INDR_gsTag);
if (INDR_gsTag[0]) for (INDR_gsTmp = 4; ; )
{
switch (INDR_gsTag[INDR_gsTmp++])
{
case 'i', 'd', 't', 'f', 'c':
{
// Resolve non-reference parameters.
#emit LREF.pri INDR_gsAddr
#emit LOAD.I
#emit SREF.pri INDR_gsAddr
}
case '\0', 'x':
break;
}
INDR_gsAddr += cellbytes;
}
}
{}
// No handler, and no tag data. Just jump to it and hope (hope it has a `PROC` too).
#emit LOAD.pri INDIRECTION_DATA
#emit SCTRL 6
return 0;
}
/// <library>indirection</library>
/// <remarks>
/// Not `Indirect_CallString` to make use of the `string:` macro.
/// </remarks>
stock string:Indirect_Callstring(func, tag, GLOBAL_TAG_TYPES:...)
{
const cells0 = 2 * cellbytes;
const cells1 = 3 * cellbytes;
const cells2 = 5 * cellbytes;
const cells3 = -2 * cellbytes;
const cells4 = 1 * cellbytes;
const cells5 = 1 * cellbytes;
const cells6 = 2 * cellbytes;
#pragma unused tag
if (!func)
{
// Get the offset to the secret return parameter.
#emit LOAD.S.pri cells0
#emit ADDR.alt cells1
#emit ADD
#emit LOAD.I
#emit MOVE.alt
#emit ZERO.pri
#emit STOR.I
#emit RETN
new ret[YSI_MAX_STRING];
return ret;
}
{}
#emit ADDR.pri cells2
#emit STOR.pri INDR_gsAddr
#emit POP.pri
#emit SCTRL 5
// Get the return address.
#emit POP.alt
// Get the parameter count.
#emit POP.pri
// Reduce the parameter count.
#emit ADD.C cells3
// Store the return address.
#emit SWAP.alt
#emit STOR.alt INDIRECTION_DATA
#emit POP.alt
// Store the new parameter count.
#emit SWAP.pri
#emit STOR.pri INDIRECTION_TAG
#emit PUSH.alt
// Check the pointer type. If it is in the `COD` area, jump straight to it
// with the tag for parameter types (if it isn't 0). Otherwise, use the
// `func` from `DAT` as a pointer to a handler.
if (INDIRECTION_DATA >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.pri INDIRECTION_DATA
#emit LOAD.alt INDR_gsCodSize
#emit SUB
#emit MOVE.alt
#emit LOAD.I
#emit STOR.pri INDIRECTION_DATA
if (!INDIRECTION_DATA)
{
// Get the function at `func - COD + 4`.
#emit CONST.pri cells4
#emit ADD
#emit LOAD.I
// Call it, passing `func` as a proper pointer, NOT skipping `PROC`.
#emit STOR.alt INDIRECTION_DATA
#emit SCTRL 6
// NEVER RETURNS PAST HERE.
}
{}
// `INDIRECTION_DATA` is now a pointer to a string of a function name.
// Resolve it via index lookup.
#emit PUSH.alt
#emit PUSH.C cells5
#emit SYSREQ.C funcidx
#emit STACK cells6
#emit STOR.pri INDIRECTION_DATA
if (INDIRECTION_DATA == -1)
{
#emit PROC
#emit RETN
}
{}
// Get the address from the index.
#if cellbits == 64
const shifter = 3;
#else
const shifter = 2;
#endif
#emit LCTRL __dat
#emit NEG
#emit ADD.C 32 // Always this, regardless of cell size.
#emit STOR.pri INDR_gsTmp
#emit LREF.alt INDR_gsTmp
#emit LCTRL __dat
#emit NEG
#emit ADD
// This replicates `IDXADDR`, but in no more instructions since the data
// are in the wrong registers.
#emit LOAD.alt INDIRECTION_DATA
#emit SHL.C.alt shifter
#emit ADD
// Add more data, to account for `__defsize` offsets.
#emit LOAD.alt INDIRECTION_DATA
#emit SHL.C.alt 2
#emit ADD
#emit STOR.pri INDR_gsTmp
#emit LREF.pri INDR_gsTmp
#emit STOR.pri INDIRECTION_DATA
}
if (INDIRECTION_TAG)
{
// Skip the `F@_@` prefix.
GetTagNameFromID(INDIRECTION_TAG, INDR_gsTag);
if (INDR_gsTag[0]) for (INDR_gsTmp = 4; ; )
{
switch (INDR_gsTag[INDR_gsTmp++])
{
case 'i', 'd', 't', 'f', 'c':
{
// Resolve non-reference parameters.
#emit LREF.pri INDR_gsAddr
#emit LOAD.I
#emit SREF.pri INDR_gsAddr
}
case '\0', 'x':
break;
}
INDR_gsAddr += cellbytes;
}
}
{}
// No handler, and no tag data. Just jump to it and hope.
#emit LOAD.pri INDIRECTION_DATA
#emit SCTRL 6
// Never called. Don't use "static" because it would allocate real memory
// in advance. Instead, this will want to allocate on the stack but never
// get hit to do so.
new ret[YSI_MAX_STRING];
return ret;
}
/// <library>indirection</library>
/// <remarks>
/// Not `Indirect_Callvoid` to make use of the `void:` macro.
/// </remarks>
stock void:Indirect_Callvoid(func, tag, GLOBAL_TAG_TYPES:...)
{
const cells0 = 5 * cellbytes;
const cells1 = -2 * cellbytes;
const cells2 = 1 * cellbytes;
const cells3 = 1 * cellbytes;
const cells4 = 2 * cellbytes;
#pragma unused tag
if (!func)
{
return;
}
{}
#emit ADDR.pri cells0
#emit STOR.pri INDR_gsAddr
#emit POP.pri
#emit SCTRL 5
// Get the return address.
#emit POP.alt
// Get the parameter count.
#emit POP.pri
// Reduce the parameter count.
#emit ADD.C cells1
// Store the return address.
#emit SWAP.alt
#emit STOR.alt INDIRECTION_DATA
#emit POP.alt
// Store the new parameter count.
#emit SWAP.pri
#emit STOR.pri INDIRECTION_TAG
#emit PUSH.alt
// Check the pointer type. If it is in the `COD` area, jump straight to it
// with the tag for parameter types (if it isn't 0). Otherwise, use the
// `func` from `DAT` as a pointer to a handler.
if (INDIRECTION_DATA >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.pri INDIRECTION_DATA
#emit LOAD.alt INDR_gsCodSize
#emit SUB
#emit MOVE.alt
#emit LOAD.I
#emit STOR.pri INDIRECTION_DATA
if (!INDIRECTION_DATA)
{
// Get the function at `func - COD + 4`.
#emit CONST.pri cells2
#emit ADD
#emit LOAD.I
// Call it, passing `func` as a proper pointer, NOT skipping `PROC`.
#emit STOR.alt INDIRECTION_DATA
#emit SCTRL 6
// NEVER RETURNS PAST HERE.
}
{}
// `INDIRECTION_DATA` is now a pointer to a string of a function name.
// Resolve it via index lookup.
#emit PUSH.alt
#emit PUSH.C cells3
#emit SYSREQ.C funcidx
#emit STACK cells4
#emit STOR.pri INDIRECTION_DATA
if (INDIRECTION_DATA == -1)
{
#emit PROC
#emit RETN
}
{}
// Get the address from the index.
#if cellbits == 64
const shifter = 3;
#else
const shifter = 2;
#endif
#emit LCTRL __dat
#emit NEG
#emit ADD.C 32 // Always this, regardless of cell size.
#emit STOR.pri INDR_gsTmp
#emit LREF.alt INDR_gsTmp
#emit LCTRL __dat
#emit NEG
#emit ADD
// This replicates `IDXADDR`, but in no more instructions since the data
// are in the wrong registers.
#emit LOAD.alt INDIRECTION_DATA
#emit SHL.C.alt shifter
#emit ADD
// Add more data, to account for `__defsize` offsets.
#emit LOAD.alt INDIRECTION_DATA
#emit SHL.C.alt 2
#emit ADD
#emit STOR.pri INDR_gsTmp
#emit LREF.pri INDR_gsTmp
#emit STOR.pri INDIRECTION_DATA
}
if (INDIRECTION_TAG)
{
// Skip the `F@_@` prefix.
GetTagNameFromID(INDIRECTION_TAG, INDR_gsTag);
if (INDR_gsTag[0]) for (INDR_gsTmp = 4; ; )
{
switch (INDR_gsTag[INDR_gsTmp++])
{
case 'i', 'd', 't', 'f', 'c':
{
// Resolve non-reference parameters.
#emit LREF.pri INDR_gsAddr
#emit LOAD.I
#emit SREF.pri INDR_gsAddr
}
case '\0', 'x':
break;
}
INDR_gsAddr += cellbytes;
}
}
{}
// No handler, and no tag data. Just jump to it and hope.
#emit LOAD.pri INDIRECTION_DATA
#emit SCTRL 6
// Don't return anything.
}
/// <library>indirection</library>
stock Indirect_Array(func, tag, const params[], size = sizeof (params))
{
#if cellbits == 32
const cells0 = 2;
#elseif cellbits == 64
const cells0 = 3;
#else
#error Unsupported `cellbits`.
#endif
const cells1 = 5 * cellbytes;
const cells2 = 6 * cellbytes;
const cells4 = 1 * cellbytes;
const cells5 = 2 * cellbytes;
static
ret = 0,
src = 0;
#pragma unused func, tag, params, size
// Get the previous frame. This undoes the effects of our `PROC` so the
// called function must have a proper `PROC`.
#emit POP.pri
#emit SCTRL 5
// Get the return address.
#emit POP.pri
#emit STOR.pri ret
// Remove the parameter count.
#emit POP.pri
// Get the target tag.
#emit POP.pri
#emit STOR.pri INDIRECTION_DATA
// Get the target function.
#emit POP.pri
#emit STOR.pri INDIRECTION_TAG
// Get the source data.
#emit POP.alt
#emit STOR.alt src
// Get the size.
#emit POP.alt
#emit SHL.C.alt cells0
// #emit STOR.alt cnt
// We cannot just adjust the stack size while the other parameters are still
// on it, since the new stack might be smaller than the old one, and dealing
// with that in a simple way is not possible. Well, it is possible - it's
// what we are doing! Copy the parameters.
#emit LCTRL 4
#emit SUB
#emit SCTRL 4
#emit PUSH.alt
#emit PUSH.alt
#emit PUSH.C 0
#emit PUSH src
#emit STOR.pri src
#emit PUSH.pri
#emit PUSH.C cells1
#emit SYSREQ.C memcpy
#emit MOVE.pri
#emit STACK cells2
#emit PUSH.pri
#emit PUSH ret
//
// Check the pointer type. If it is in the `COD` area, jump straight to it
// with the tag for parameter types (if it isn't 0). Otherwise, use the
// `func` from `DAT` as a pointer to a handler.
if (INDIRECTION_DATA >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.pri INDIRECTION_DATA
#emit LOAD.alt INDR_gsCodSize
#emit SUB
#emit MOVE.alt
#emit LOAD.I
#emit STOR.pri INDIRECTION_DATA
if (!INDIRECTION_DATA)
{
#emit STOR.alt INDIRECTION_DATA
// Get the function at `func - COD + 4`.
#emit LOAD.pri INDIRECTION_DATA
#emit ADD.C E_INDIRECTION_HANDER__
#emit LOAD.I
// Call it, passing `func` as a proper pointer, NOT skipping `PROC`.
#emit SCTRL 6
// NEVER RETURNS PAST HERE.
}
{}
// `INDIRECTION_DATA` is now a pointer to a string of a function name.
// Resolve it via index lookup.
#emit PUSH.alt
#emit PUSH.C cells4
#emit SYSREQ.C funcidx
#emit STACK cells5
#emit STOR.pri INDIRECTION_DATA
if (INDIRECTION_DATA == -1)
{
// Failure.
#emit PROC
#emit RETN
}
{}
// Get the address from the index.
#if cellbits == 64
const shifter = 3;
#else
const shifter = 2;
#endif
#emit LCTRL __dat
#emit NEG
#emit ADD.C 32 // Always this, regardless of cell size.
#emit STOR.pri INDR_gsTmp
#emit LREF.alt INDR_gsTmp
#emit LCTRL __dat
#emit NEG
#emit ADD
// This replicates `IDXADDR`, but in no more instructions since the data
// are in the wrong registers.
#emit LOAD.alt INDIRECTION_DATA
#emit SHL.C.alt shifter
#emit ADD
// Add more data, to account for `__defsize` offsets.
#emit LOAD.alt INDIRECTION_DATA
#emit SHL.C.alt 2
#emit ADD
#emit STOR.pri INDR_gsTmp
#emit LREF.pri INDR_gsTmp
#emit STOR.pri INDIRECTION_DATA
}
if (INDIRECTION_TAG)
{
// Skip the `F@_@` prefix.
GetTagNameFromID(INDIRECTION_TAG, INDR_gsTag);
if (INDR_gsTag[0]) for (INDR_gsTmp = 4; ; )
{
switch (INDR_gsTag[INDR_gsTmp++])
{
case 'i', 'd', 't', 'f', 'c':
{
// Resolve non-reference parameters.
#emit LREF.pri src
#emit LOAD.I
#emit SREF.pri src
}
case '\0', 'x':
break;
}
src += cellbytes;
}
}
{}
// No handler, and no tag data. Just jump to it and hope (hope it has a `PROC` too).
#emit LOAD.pri INDIRECTION_DATA
#emit SCTRL 6
return 0;
}
/*-------------------------------------------------------------------------*//**
* <library>indirection</library>
* <param name="func">The function pointer with attached Nextdata.</param>
* <remarks>
* Gets extra data from the pointer.
* </remarks>
*//*------------------------------------------------------------------------**/
stock Indirect_GetNext_(func)
{
if (func >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.S.pri func
#emit LOAD.alt INDR_gsCodSize
#emit SUB
#emit MOVE.alt
#emit LOAD.I
#emit STOR.S.pri func
if (func)
{
// Probably a string.
return 0;
}
{}
// I'm relying on `alt` not changing here...
// Get the function at `func - COD + 16`.
#emit CONST.pri E_INDIRECTION_NEXT__
#emit ADD
#emit LOAD.I
#emit RETN
}
return 0;
}
#define Indirect_GetNext(%0) Indirect_GetNext_(_:%0)
/*-------------------------------------------------------------------------*//**
* <library>indirection</library>
* <param name="func">The function pointer to attach Nextdata to.</param>
* <param name="data">The Nextdata.</param>
*//*------------------------------------------------------------------------**/
stock Indirect_SetNext_(func, data)
{
if (func >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.S.pri func
#emit LOAD.alt INDR_gsCodSize
#emit SUB
#emit MOVE.alt
#emit LOAD.I
#emit STOR.S.pri func
if (func)
{
// Probably a string.
return false;
}
{}
// I'm relying on `alt` not changing here...
// Get the function at `func - COD + 16`.
#emit CONST.pri E_INDIRECTION_NEXT__
#emit ADD
#emit LOAD.S.alt data
#emit XCHG
#emit STOR.I
return true;
}
return false;
}
#define Indirect_SetNext(%0,%1) Indirect_SetNext_(_:(%0),%1)
/*-------------------------------------------------------------------------*//**
* <library>indirection</library>
* <param name="func">The function pointer to attach Nextdata to.</param>
* <returns><c>1</c> - an indirection function. <c>2</c> - a string name.
* <c>3</c> - a code pointer.
*//*------------------------------------------------------------------------**/
stock Indirect_GetType_(func)
{
if (func >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.S.pri func
#emit LOAD.alt INDR_gsCodSize
#emit SUB
#emit MOVE.alt
#emit LOAD.I
#emit STOR.S.pri func
if (func)
{
// String.
return 2;
}
// Indirect.
return 1;
}
// Pointer.
return 3;
}
#define Indirect_GetType(%0) Indirect_GetType_(_:(%0))
/*-------------------------------------------------------------------------*//**
* <library>indirection</library>
* <param name="func">The function pointer with a tag.</param>
* <remarks>
* Gets the tag of the pointer.
* </remarks>
*//*------------------------------------------------------------------------**/
stock Indirect_GetTag_(func)
{
if (func >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.S.pri func
#emit LOAD.alt INDR_gsCodSize
#emit SUB
#emit MOVE.alt
#emit LOAD.I
#emit STOR.S.pri func
if (func)
{
// Probably a string.
return 0;
}
{}
// I'm relying on `alt` not changing here...
// Get the function at `func - COD + 16`.
#emit CONST.pri E_INDIRECTION_TAG__
#emit ADD
#emit LOAD.I
#emit RETN
}
return 0;
}
#define Indirect_GetTag(%0) Indirect_GetTag_(_:%0)
/*-------------------------------------------------------------------------*//**
* <library>indirection</library>
* <param name="func">The function pointer to attach a tag to.</param>
* <param name="tag">The tag.</param>
*//*------------------------------------------------------------------------**/
stock bool:Indirect_SetTag_(func, tag)
{
if (func >= INDR_gsCodSize)
{
// Get the data at `func - COD`.
#emit LOAD.S.pri func