forked from Terraspace/UASM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoke.c
5791 lines (5443 loc) · 212 KB
/
invoke.c
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
/****************************************************************************
*
* This code is Public Domain.
*
* ========================================================================
*
* Description: Processing of INVOKE directive.
*
****************************************************************************/
#include <ctype.h>
#include <limits.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "reswords.h"
#include "expreval.h"
#include "lqueue.h"
#include "equate.h"
#include "assume.h"
#include "segment.h"
#include "listing.h"
#include "myassert.h"
#include "label.h"
#include "hll.h"
#include "segment.h"
#if DLLIMPORT
#include "mangle.h"
#include "extern.h"
#endif
#include "proc.h"
#if defined(WINDOWSDDK)
#if defined(_WIN32)
typedef _W64 int INT_PTR, *PINT_PTR;
typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
typedef _W64 long LONG_PTR, *PLONG_PTR;
typedef _W64 unsigned long ULONG_PTR, *PULONG_PTR;
#define __int3264 __int32
#else
typedef __int64 INT_PTR, *PINT_PTR;
typedef unsigned __int64 UINT_PTR, *PUINT_PTR;
typedef __int64 LONG_PTR, *PLONG_PTR;
typedef unsigned __int64 ULONG_PTR, *PULONG_PTR;
#define __int3264 __int64
#endif
#else
#include <inttypes.h>
#if defined(_WIN32)
typedef _W64 int INT_PTR, *PINT_PTR;
typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
typedef _W64 long LONG_PTR, *PLONG_PTR;
typedef _W64 unsigned long ULONG_PTR, *PULONG_PTR;
#define __int3264 __int32
#else
typedef int64_t INT_PTR, *PINT_PTR;
typedef uint64_t UINT_PTR, *PUINT_PTR;
typedef int64_t LONG_PTR, *PLONG_PTR;
typedef uint64_t ULONG_PTR, *PULONG_PTR;
#define __int3264 int64_t
#endif
#endif
extern bool write_to_file;
extern int_64 maxintvalues[];
extern int_64 minintvalues[];
extern enum special_token stackreg[];
extern struct dsym *CurrStruct;
extern UINT_PTR UTF8toWideChar(const unsigned char *pSource, UINT_PTR nSourceLen, UINT_PTR *nSourceDone, unsigned short *szTarget, UINT_PTR nTargetMax);
#ifdef __I86__
#define NUMQUAL (long)
#else
#define NUMQUAL
#endif
enum reg_used_flags {
R0_USED = 0x01, /* register contents of AX/EAX/RAX is destroyed */
R0_H_CLEARED = 0x02, /* 16bit: high byte of R0 (=AH) has been set to 0 */
R0_X_CLEARED = 0x04, /* 16bit: register R0 (=AX) has been set to 0 */
R2_USED = 0x08, /* contents of DX is destroyed ( via CWD ); cpu < 80386 only */
#if AMD64_SUPPORT
RCX_USED = 0x08, /* win64: register contents of CL/CX/ECX/RCX is destroyed */
RDX_USED = 0x10, /* win64: register contents of DL/DX/EDX/RDX is destroyed */
R8_USED = 0x20, /* win64: register contents of R8B/R8W/R8D/R8 is destroyed */
R9_USED = 0x40, /* win64: register contents of R9B/R9W/R9D/R9 is destroyed */
#define RPAR_START 3 /* Win64: RCX first param start at bit 3 */
#endif
#if SYSV_SUPPORT
SV_RDI_USED = 0x02, /* sysv: register contents of DI/EDI/RDI is destroyed */
SV_RSI_USED = 0x04, /* sysv: register contents of SI/ESI/RSI is destroyed */
SV_RDX_USED = 0x08, /* sysv: register contents of DL/DX/EDX/RDX is destroyed */
SV_RCX_USED = 0x10, /* sysv: register contents of CL/CX/ECX/RCX is destroyed */
SV_R8_USED = 0x20, /* sysv: register contents of R8B/R8W/R8D/R8 is destroyed */
SV_R9_USED = 0x40, /* sysv: register contents of R9B/R9W/R9D/R9 is destroyed */
#define SYSVR_START 1 /* sysv: RDI first param start at bit 6 */
#endif
#if OWFC_SUPPORT
ROW_AX_USED = 0x08, /* watc: register contents of AL/AX/EAX is destroyed */
ROW_DX_USED = 0x10, /* watc: register contents of DL/DX/EDX is destroyed */
ROW_BX_USED = 0x20, /* watc: register contents of BL/BX/EBX is destroyed */
ROW_CX_USED = 0x40, /* watc: register contents of CL/CX/ECX is destroyed */
#define ROW_START 3 /* watc: first param start at bit 3 */
#endif
};
extern void myatoi128( const char *, uint_64[], int, int );
static int size_vararg; /* size of :VARARG arguments */
static int fcscratch = 0; /* exclusively to be used by FASTCALL helper functions */
static int vcallpass = 0; /* static global to determine which vectorcall pass we're in */
struct fastcall_conv {
int (* invokestart)( struct dsym const *, int, int, struct asm_tok[], int * );
void (* invokeend) ( struct dsym const *, int, int );
int (* handleparam)( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 * );
};
struct thiscall_conv
{
int (*invokestart)(struct dsym const*, int, int, struct asm_tok[], int*);
void (*invokeend) (struct dsym const*, int, int);
int (*handleparam)(struct dsym const*, int, struct dsym*, bool, struct expr*, char*, uint_8*);
};
struct vectorcall_conv {
int (* invokestart)( struct dsym const *, int, int, struct asm_tok[], int * );
void (* invokeend) ( struct dsym const *, int, int );
int (* handleparam)( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 * );
};
struct sysvcall_conv {
int (* invokestart)( struct dsym const *, int, int, struct asm_tok[], int *);
void (* invokeend) ( struct dsym const *, int, int);
int (* handleparam)( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 *);
};
struct delphicall_conv {
int (* invokestart)( struct dsym const *, int, int, struct asm_tok[], int *);
void (* invokeend) ( struct dsym const *, int, int);
int (* handleparam)( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 *);
};
struct regcall_conv {
int(*invokestart)(struct dsym const *, int, int, struct asm_tok[], int *);
void(*invokeend) (struct dsym const *, int, int);
int(*handleparam)(struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 *);
};
static int ms32_fcstart( struct dsym const *, int, int, struct asm_tok[], int * );
static void ms32_fcend ( struct dsym const *, int, int );
static int ms32_param ( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 * );
#if OWFC_SUPPORT
static int watc_fcstart( struct dsym const *, int, int, struct asm_tok[], int * );
static void watc_fcend ( struct dsym const *, int, int );
static int watc_param ( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 * );
#endif
#if AMD64_SUPPORT
static int ms64_fcstart( struct dsym const *, int, int, struct asm_tok[], int * );
static void ms64_fcend ( struct dsym const *, int, int );
static int ms64_param ( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 * );
#define REGPAR_WIN64 0x0306 /* regs 1, 2, 8 and 9 */
#define REGCALLPAR_WIN64 0xDBC7 /* regs 0, 1, 2, 6, 7, 8, 9, 11, 12, 14 and 15 (RAX, RCX, RDX, RDI, RSI, R8, R9, R11, R12, R14 and R15) */
#endif
#if SYSV_SUPPORT
static int sysv_reg ( struct dsym const *, unsigned int );
static int sysv_fcstart ( struct dsym const *, int, int, struct asm_tok[], int * );
static void sysv_fcend ( struct dsym const *, int, int );
static int sysv_param ( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 * );
static int sysv_vararg_param ( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 *);
#define REGPAR_SYSV 0x03C6 /* regs 6, 7, 1, 2, 8 and 9 */
#define REGCALLPAR_UNIX64 0xDFC7 /* regs 0, 1, 2, 6, 7, 8, 9, 10, 11, 12, 14 and 15 (RAX, RCX, RDX, RDI, RSI, R8, R9, R10, R11, R12, R14 and R15) */
#endif
/* added for delphi in v2.28 */
#if DELPHI_SUPPORT
static int delphi32_fcstart( struct dsym const *, int, int, struct asm_tok[], int * );
static void delphi32_fcend ( struct dsym const *, int, int );
static int delphi32_param ( struct dsym const *, int, struct dsym *, bool, struct expr *, char *, uint_8 * );
#define REGPAR_DELPHI 0x03 /* regs 0, 2, 1 */
#endif
static const struct fastcall_conv fastcall_tab[] = {
{ ms32_fcstart, ms32_fcend , ms32_param }, /* FCT_MSC */
#if OWFC_SUPPORT
{ watc_fcstart, watc_fcend , watc_param }, /* FCT_WATCOMC */
#endif
#if AMD64_SUPPORT
{ ms64_fcstart, ms64_fcend , ms64_param } /* FCT_WIN64 */
#endif
};
static const struct thiscall_conv thiscall_tab[] = {
{ ms32_fcstart, ms32_fcend , ms32_param }, /* FCT_MSC */
#if OWFC_SUPPORT
{ watc_fcstart, watc_fcend , watc_param }, /* FCT_WATCOMC */
#endif
#if AMD64_SUPPORT
{ ms64_fcstart, ms64_fcend , ms64_param } /* FCT_WIN64 */
#endif
};
static const struct vectorcall_conv vectorcall_tab[] = {
{ ms32_fcstart, ms32_fcend , ms32_param }, /* FCT_MSC */
#if OWFC_SUPPORT
{ watc_fcstart, watc_fcend , watc_param }, /* FCT_WATCOMC */
#endif
#if AMD64_SUPPORT
{ ms64_fcstart, ms64_fcend , ms64_param } /* FCT_WIN64 */
#endif
};
static const struct sysvcall_conv sysvcall_tab[] = {
{ ms32_fcstart, ms32_fcend , ms32_param }, /* FCT_MSC */
#if OWFC_SUPPORT
{ watc_fcstart, watc_fcend , watc_param }, /* FCT_WATCOMC */
#endif
#if AMD64_SUPPORT
{ sysv_fcstart, sysv_fcend , sysv_param } /* FCT_WIN64 */
#endif
};
/* added for delphi in v2.28 */
static const struct delphicall_conv delphicall_tab[] = {
{ delphi32_fcstart, delphi32_fcend , delphi32_param }, /* FCT_DELPHI */
#if OWFC_SUPPORT
{ watc_fcstart, watc_fcend , watc_param }, /* FCT_WATCOMC */
#endif
#if AMD64_SUPPORT
{ ms64_fcstart, ms64_fcend , ms64_param } /* FCT_WIN64 */
#endif
};
static const struct regcall_conv regcallms_tab[] = {
{ ms32_fcstart, ms32_fcend , ms32_param }, /* FCT_MSC */
#if OWFC_SUPPORT
{ watc_fcstart, watc_fcend , watc_param }, /* FCT_WATCOMC */
#endif
#if AMD64_SUPPORT
{ ms64_fcstart, ms64_fcend , ms64_param } /* FCT_WIN64 */
#endif
};
static const struct regcall_conv regcallunix_tab[] = {
{ ms32_fcstart, ms32_fcend , ms32_param }, /* FCT_MSC */
#if OWFC_SUPPORT
{ watc_fcstart, watc_fcend , watc_param }, /* FCT_WATCOMC */
#endif
#if AMD64_SUPPORT
{ sysv_fcstart, sysv_fcend , sysv_param } /* FCT_WIN64 */
#endif
};
static const enum special_token regax[] = { T_AX, T_EAX,
#if AMD64_SUPPORT
T_RAX
#endif
};
/* 16-bit MS fastcall uses up to 3 registers (AX, DX, BX )
* and additional params are pushed in PASCAL order!
*/
static const enum special_token ms16_regs[] = { T_AX, T_DX, T_BX };
static const enum special_token ms32_regs[] = { T_ECX, T_EDX };
/* added for delphi in v2.28 */
static const enum special_token delphi32_regs[] = { T_EAX, T_EDX, T_ECX };
static const enum special_token thiscall32_regs[] = { T_ECX };
#if AMD64_SUPPORT
static const enum special_token ms64_regs[] = { T_CL, T_DL, T_R8B, T_R9B,
T_CX, T_DX, T_R8W, T_R9W,
T_ECX, T_EDX, T_R8D, T_R9D,
T_RCX, T_RDX, T_R8, T_R9 };
#endif
#if SYSV_SUPPORT
static const enum special_token sysV64_regs[] = { T_DIL, T_SIL, T_DL, T_CL, T_R8B, T_R9B,
T_DI, T_SI, T_DX, T_CX, T_R8W, T_R9W,
T_EDI, T_ESI, T_EDX, T_ECX, T_R8D, T_R9D,
T_RDI, T_RSI, T_RDX, T_RCX, T_R8, T_R9 };
static const enum special_token sysV64_regsXMM[] = { T_XMM0, T_XMM1, T_XMM2, T_XMM3, T_XMM4, T_XMM5, T_XMM6, T_XMM7 };
static const enum special_token sysV64_regsYMM[] = { T_YMM0, T_YMM1, T_YMM2, T_YMM3, T_YMM4, T_YMM5, T_YMM6, T_YMM7 };
static const enum special_token sysV64_regsZMM[] = { T_ZMM0, T_ZMM1, T_ZMM2, T_ZMM3, T_ZMM4, T_ZMM5, T_ZMM6, T_ZMM7 };
#endif
#if REGCALL_SUPPORT
/*
static const enum special_token regcallunix32_regs[] = {
T_AL, T_CL, T_DL, T_DIL, T_SIL,
T_AX, T_CX, T_DX, T_DI, T_SI,
T_EAX, T_ECX, T_EDX, T_EDI, T_ESI,
T_RAX, T_RCX, T_RDX, T_RDI, T_RSI,
};
static const enum special_token regcallms32_regs[] = {
T_CL, T_DL, T_DIL, T_SIL,
T_CX, T_DX, T_DI, T_SI,
T_ECX, T_EDX, T_EDI, T_ESI,
T_RCX, T_RDX, T_RDI, T_RSI,
};*/
static const enum special_token regcallunix32_regs[] = { T_EAX, T_ECX, T_EDX, T_EDI, T_ESI };
static const enum special_token regcallms32_regs[] = { T_ECX, T_EDX, T_EDI, T_ESI };
static const enum special_token regcallunix64_regs[] = { T_AL, T_CL, T_DL, T_DIL, T_SIL, T_R8B, T_R9B, T_R10B, T_R11B, T_R12B, T_R14B, T_R15B,
T_AX, T_CX, T_DX, T_DI, T_SI, T_R8W, T_R9W, T_R10W, T_R11W, T_R12W, T_R14W, T_R15W,
T_EAX, T_ECX, T_EDX, T_EDI, T_ESI, T_R8D, T_R9D, T_R10D, T_R11D, T_R12D, T_R14D, T_R15D,
T_RAX, T_RCX, T_RDX, T_RDI, T_RSI, T_R8, T_R9, T_R10, T_R11, T_R12, T_R14, T_R15 };
static const enum special_token regcallms64_regs[] = { T_AL, T_CL, T_DL, T_DIL, T_SIL, T_R8B, T_R9B, T_R11B, T_R12B, T_R14B, T_R15B,
T_AX, T_CX, T_DX, T_DI, T_SI, T_R8W, T_R9W, T_R11W, T_R12W, T_R14W, T_R15W,
T_EAX, T_ECX, T_EDX, T_EDI, T_ESI, T_R8D, T_R9D, T_R11D, T_R12D, T_R14D, T_R15D,
T_RAX, T_RCX, T_RDX, T_RDI, T_RSI, T_R8, T_R9, T_R11, T_R12, T_R14, T_R15 };
static const enum special_token regcall32_regsXMM[] = { T_XMM0, T_XMM1, T_XMM2, T_XMM3, T_XMM4, T_XMM5, T_XMM6, T_XMM7 };
static const enum special_token regcall32_regsYMM[] = { T_YMM0, T_YMM1, T_YMM2, T_YMM3, T_YMM4, T_YMM5, T_YMM6, T_YMM7 };
static const enum special_token regcall32_regsZMM[] = { T_ZMM0, T_ZMM1, T_ZMM2, T_ZMM3, T_ZMM4, T_ZMM5, T_ZMM6, T_ZMM7 };
static const enum special_token regcall64_regsXMM[] = { T_XMM0, T_XMM1, T_XMM2, T_XMM3, T_XMM4, T_XMM5, T_XMM6, T_XMM7, T_XMM8, T_XMM9, T_XMM10, T_XMM11, T_XMM12, T_XMM13, T_XMM14, T_XMM15 };
static const enum special_token regcall64_regsYMM[] = { T_YMM0, T_YMM1, T_YMM2, T_YMM3, T_YMM4, T_YMM5, T_YMM6, T_YMM7, T_YMM8, T_YMM9, T_YMM10, T_YMM11, T_YMM12, T_YMM13, T_YMM14, T_YMM15 };
static const enum special_token regcall64_regsZMM[] = { T_ZMM0, T_ZMM1, T_ZMM2, T_ZMM3, T_ZMM4, T_ZMM5, T_ZMM6, T_ZMM7, T_ZMM8, T_ZMM9, T_ZMM10, T_ZMM11, T_ZMM12, T_ZMM13, T_ZMM14, T_ZMM15 };
#endif
static const enum special_token vectorcall32_regsXMM[] = { T_XMM0, T_XMM1, T_XMM2, T_XMM3, T_XMM4, T_XMM5 };
static const enum special_token vectorcall32_regsYMM[] = { T_YMM0, T_YMM1, T_YMM2, T_YMM3, T_YMM4, T_YMM5 };
static const enum special_token vectorcall32_regsZMM[] = { T_ZMM0, T_ZMM1, T_ZMM2, T_ZMM3, T_ZMM4, T_ZMM5 };
/* segment register names, order must match ASSUME_ enum */
//static const enum special_token segreg_tab[] = {
// T_ES, T_CS, T_SS, T_DS, T_FS, T_GS };
static int ms32_fcstart( struct dsym const *proc, int numparams, int start, struct asm_tok tokenarray[], int *value )
/*******************************************************************************************************************/
{
struct dsym *param;
DebugMsg1(("ms32_fcstart(proc=%s, ofs=%u)\n", proc->sym.name, GetSymOfssize( &proc->sym ) ));
if ( GetSymOfssize( &proc->sym ) == USE16 )
return( 0 );
/* v2.07: count number of register params */
for ( param = proc->e.procinfo->paralist ; param ; param = param->nextparam )
if ( param->sym.state == SYM_TMACRO )
fcscratch++;
return( 1 );
}
static void ms32_fcend( struct dsym const *proc, int numparams, int value )
/*************************************************************************/
{
/* nothing to do */
return;
}
static int ms32_param( struct dsym const *proc, int index, struct dsym *param, bool addr, struct expr *opnd, char *paramvalue, uint_8 *r0used )
/*********************************************************************************************************************************************/
{
enum special_token const *pst;
DebugMsg1(("ms32_param(proc=%s, ofs=%u, index=%u, param=%s) fcscratch=%u\n", proc->sym.name, proc->sym.Ofssize, index, param->sym.name, fcscratch ));
if ( param->sym.state != SYM_TMACRO || ((param->sym.mem_type == MT_REAL4 || param->sym.mem_type == MT_REAL8 \
|| (param->sym.mem_type == MT_TYPE && _stricmp(param->sym.type->name, "__m128") == 0) || param->sym.mem_type == MT_OWORD \
|| (param->sym.mem_type == MT_TYPE && _stricmp(param->sym.type->name, "__m256") == 0) || param->sym.mem_type == MT_YMMWORD \
|| (param->sym.mem_type == MT_TYPE && _stricmp(param->sym.type->name, "__m512") == 0) || param->sym.mem_type == MT_ZMMWORD) /*&& proc->sym.langtype != LANG_VECTORCALL && proc->sym.langtype != LANG_REGCALL*/))
return( 0 );
if ( GetSymOfssize( &proc->sym ) == USE16 ) {
pst = ms16_regs + fcscratch;
fcscratch++;
}
else if (proc->sym.langtype == LANG_REGCALL && Options.output_format == OFORMAT_ELF)
{
fcscratch--;
pst = regcallunix32_regs + fcscratch;
}
else if (proc->sym.langtype == LANG_REGCALL && Options.output_format == OFORMAT_COFF)
{
fcscratch--;
pst = regcallms32_regs + fcscratch;
}
else if (proc->sym.langtype == LANG_THISCALL && Options.output_format == OFORMAT_COFF)
{
fcscratch--;
pst = thiscall32_regs + fcscratch;
}
else
{
fcscratch--;
pst = ms32_regs + fcscratch;
}
if ( addr )
AddLineQueueX( " lea %r, %s", *pst, paramvalue );
else {
enum special_token reg = *pst;
int size;
/* v2.08: adjust register if size of operand won't require the full register */
if ( ( opnd->kind != EXPR_CONST ) &&
( size = SizeFromMemtype( param->sym.mem_type, USE_EMPTY, param->sym.type ) ) < SizeFromRegister( *pst ) ) {
if (( ModuleInfo.curr_cpu & P_CPU_MASK ) >= P_386 ) {
AddLineQueueX( " %s %r, %s", ( param->sym.mem_type & MT_SIGNED ) ? "movsx" : "movzx", reg, paramvalue );
} else {
/* this is currently always UNSIGNED */
AddLineQueueX( " mov %r, %s", T_AL + GetRegNo( reg ), paramvalue );
AddLineQueueX( " mov %r, 0", T_AH + GetRegNo( reg ) );
}
} else {
/* v2.08: optimization */
if ( opnd->kind == EXPR_REG && opnd->indirect == 0 && opnd->base_reg ) {
if ( opnd->base_reg->tokval == reg )
return( 1 );
}
AddLineQueueX( " mov %r, %s", reg, paramvalue );
}
}
if (*pst == T_AX)
* r0used |= R0_USED;
return( 1 );
}
/* added for delphi in v2.28 */
static int delphi32_fcstart( struct dsym const *proc, int numparams, int start, struct asm_tok tokenarray[], int *value )
/*******************************************************************************************************************/
{
return( 0 );
}
/* added for delphi in v2.28 */
static void delphi32_fcend( struct dsym const *proc, int numparams, int value )
/*************************************************************************/
{
/* nothing to do */
return;
}
/* added for delphi in v2.28 */
static int delphi32_param( struct dsym const *proc, int index, struct dsym *param, bool addr, struct expr *opnd, char *paramvalue, uint_8 *r0used )
/*********************************************************************************************************************************************/
{
enum special_token const *pst;
struct proc_info *info;
enum special_token reg;
info = proc->e.procinfo;
DebugMsg1(("delphi_param(proc=%s, ofs=%u, index=%u, param=%s) fcscratch=%u\n", proc->sym.name, proc->sym.Ofssize, index, param->sym.name, fcscratch ));
if (param->sym.state != SYM_TMACRO && ((param->sym.state == SYM_STACK && !addr) || fcscratch > 2))
{
return(0);
}
pst = delphi32_regs + fcscratch;
reg = *pst;
info->delregsused[fcscratch] = reg;
/* v2.29: optimization */
if ((_stricmp(paramvalue, "EAX") == 0) && fcscratch == 0){
fcscratch++;
return (1);
}
else if ((_stricmp(paramvalue, "EDX") == 0) && fcscratch == 1){
fcscratch++;
return (1);
}
else if ((_stricmp(paramvalue, "ECX") == 0) && fcscratch == 2){
fcscratch++;
return (1);
}
if ( opnd->kind == EXPR_REG && opnd->indirect == 0 && opnd->base_reg ) {
if (opnd->base_reg->tokval == reg){
fcscratch++;
return(1);
}
}
if (Parse_Pass)
{
switch (fcscratch)
{
case 0:
if ((_stricmp(paramvalue, "EDX") == 0) && info->delregsused[1] ||
(_stricmp(paramvalue, "ECX") == 0) && info->delregsused[2])
EmitWarn(2, REGISTER_VALUE_OVERWRITTEN_BY_INVOKE);
break;
case 1:
if ((_stricmp(paramvalue, "EAX") == 0) && info->delregsused[0] ||
(_stricmp(paramvalue, "ECX") == 0) && info->delregsused[2])
EmitWarn(2, REGISTER_VALUE_OVERWRITTEN_BY_INVOKE);
break;
case 2:
if ((_stricmp(paramvalue, "EAX") == 0) && info->delregsused[0] ||
(_stricmp(paramvalue, "EDX") == 0) && info->delregsused[1])
EmitWarn(2, REGISTER_VALUE_OVERWRITTEN_BY_INVOKE);
}
}
if (param->sym.state == SYM_TMACRO && !addr)
AddLineQueueX(" mov %r, %s", reg, paramvalue);
else if (addr)
AddLineQueueX(" lea %r, %s", reg, paramvalue);
fcscratch++;
return( 1 );
}
/*
--------------------------------------------------------------------------------------------------------------------------
WINDOWS 64 FASTCALL HANDLERS
--------------------------------------------------------------------------------------------------------------------------
*/
#if AMD64_SUPPORT
static int ms64_fcstart(struct dsym const *proc, int numparams, int start, struct asm_tok tokenarray[], int *value)
/*******************************************************************************************************************/
{
int j;
if (proc->e.procinfo->has_vararg)
{
for (numparams = 0; tokenarray[start].token != T_FINAL; start++)
if (tokenarray[start].token == T_COMMA) {
numparams++;
}
}
DebugMsg1(("ms64_fcstart(%s, numparams=%u) vararg=%u\n", proc->sym.name, numparams, proc->e.procinfo->has_vararg));
if (proc->sym.langtype == LANG_REGCALL)
{
j = 11;
}
else
{
j = 4;
}
if (numparams < j)
numparams = j;
else if (numparams & 1)
numparams++;
*value = numparams;
if (ModuleInfo.win64_flags & W64F_AUTOSTACKSP)
{
if ((numparams * sizeof(uint_64)) > sym_ReservedStack->value)
sym_ReservedStack->value = numparams * sizeof(uint_64);
}
else
AddLineQueueX(" sub %r, %d", T_RSP, numparams * sizeof(uint_64));
/* since Win64 fastcall doesn't push, it's a better/faster strategy to handle the arguments from left to right. */
return(0);
}
static void ms64_fcend(struct dsym const *proc, int numparams, int value)
/*************************************************************************/
{
/* use <value>, which has been set by ms64_fcstart() */
if (!(ModuleInfo.win64_flags & W64F_AUTOSTACKSP))
AddLineQueueX(" add %r, %d", T_RSP, value * 8);
return;
}
/* macro to convert register number to param number:
* 1 -> 0 (rCX)
* 2 -> 1 (rDX)
* 8 -> 2 (r8)
* 9 -> 3 (r9)
*/
#define GetParmIndex( x ) ( ( (x) >= 8 ) ? (x) - 6 : (x) - 1 )
/* macro to convert regcall unix register number to param number:
* 0 -> 0 (rAX)
* 1 -> 1 (rCX)
* 2 -> 2 (rDX)
* 7 -> 3 (rDI)
* 6 -> 4 (rSI)
* 8 -> 5 (r8)
* 9 -> 6 (r9)
* 10 -> 7 (r10)
* 11 -> 8 (r11)
* 12 -> 9 (r12)
* 14 -> 10 (r14)
* 15 -> 11 (r15)
*/
#define GetregcallunixParmIndex( x ) ( ( (x) >= 14 ) ? (x) - 4 : ( (x) >= 8 ) ? (x) - 3 : ( (x) >= 7 ) ? (x) - 4 : ( (x) >= 6 ) ? (x) - 2 : (x) )
/* macro to convert regcall ms register number to param number:
* 0 -> 0 (rAX)
* 1 -> 1 (rCX)
* 2 -> 2 (rDX)
* 7 -> 3 (rDI)
* 6 -> 4 (rSI)
* 8 -> 5 (r8)
* 9 -> 6 (r9)
* 11 -> 7 (r11)
* 12 -> 8 (r12)
* 14 -> 9 (r14)
* 15 -> 10 (r15)
*/
#define GetregcallmsParmIndex( x ) ( ( (x) >= 14 ) ? (x) - 5 : ( (x) >= 11 ) ? (x) - 4 : ( (x) >= 8 ) ? (x) - 3 : ( (x) >= 7 ) ? (x) - 4 : ( (x) >= 6 ) ? (x) - 2 : (x) )
/*
* parameter for Win64 FASTCALL.
* the first 4 parameters are held in registers: rcx, rdx, r8, r9 for non-float arguments,
* xmm0, xmm1, xmm2, xmm3 for float arguments. If parameter size is > 8, the address of
* the argument is used instead of the value.
* UASM 2.49 For structs that are not 1/2/4/8 bytes in size the address is used too, else they're passed
* in registers.
*/
static int ms64_param(struct dsym const *proc, int index, struct dsym *param, bool addr, struct expr *opnd, char *paramvalue, uint_8 *regs_used)
/************************************************************************************************************************************************/
{
uint_32 size;
uint_32 psize;
int reg;
int reg2;
int i;
int j = 0;
int tCount = 0;
int freevecregs = 0;
int vecidx = -1;
int membersize = 0; /* used for vectorcall array */
int memberCount = 0; /* used for vectorcall array */
int base;
struct proc_info *info = proc->e.procinfo;
struct dsym *t = NULL; /* used for vectorcall array member size */
bool destroyed = FALSE;
struct asym *sym = NULL;
DebugMsg1(("ms64_param(%s, index=%u, param.memtype=%Xh, addr=%u) enter\n", proc->sym.name, index, param->sym.mem_type, addr));
/* v2.11: default size is 32-bit, not 64-bit */
if (param->sym.is_vararg)
{
psize = 0;
/* v2.46 support automatic promotion of 64bit immediates in vararg invoke */
if (opnd->kind == EXPR_CONST && (opnd->llvalue > INT_MAX || opnd->llvalue < INT_MIN))
psize = 8;
else if (addr || opnd->instr == T_OFFSET)
psize = 8;
else if (opnd->kind == EXPR_REG && opnd->indirect == FALSE)
psize = SizeFromRegister(opnd->base_reg->tokval);
else if (opnd->mem_type != MT_EMPTY)
psize = SizeFromMemtype(opnd->mem_type, USE64, opnd->type);
if (psize < 4)
psize = 4;
}
else
psize = SizeFromMemtype(param->sym.mem_type, USE64, param->sym.type);
/* UASM 2.49 Force odd-sized structures to error: pass by reference! */
if (psize == 3 || psize == 5 || psize == 6 || psize == 7)
{
if (param->sym.mem_type == MT_TYPE)
{
EmitErr(INVALID_REG_STRUCT_SIZE);
}
}
if (vcallpass == 1)
goto vcall;
/* check for register overwrites; v2.11: moved out the if( index >= 4 ) block */
if (opnd->base_reg != NULL)
{
reg = opnd->base_reg->tokval;
if (GetValueSp(reg) & OP_R)
{
i = GetRegNo(reg);
if ((proc->sym.langtype == LANG_REGCALL) && (REGCALLPAR_WIN64 & (1 << i)))
{
base = GetregcallmsParmIndex(i);
if (*regs_used & (1 << (base + RPAR_START)))
destroyed = TRUE;
}
else if (REGPAR_WIN64 & (1 << i))
{
base = GetParmIndex(i);
if (*regs_used & (1 << (base + RPAR_START)))
destroyed = TRUE;
}
else if ((*regs_used & R0_USED) && ((GetValueSp(reg) & OP_A) || reg == T_AH))
{
destroyed = TRUE;
}
}
}
if (opnd->idx_reg != NULL)
{
reg2 = opnd->idx_reg->tokval;
if (GetValueSp(reg2) & OP_R)
{
i = GetRegNo(reg2);
if ((proc->sym.langtype == LANG_REGCALL) && (REGCALLPAR_WIN64 & (1 << i)))
{
base = GetregcallmsParmIndex(i);
if (*regs_used & (1 << (base + RPAR_START)))
destroyed = TRUE;
}
else if (REGPAR_WIN64 & (1 << i))
{
base = GetParmIndex(i);
if (*regs_used & (1 << (base + RPAR_START)))
destroyed = TRUE;
}
else if ((*regs_used & R0_USED) && ((GetValueSp(reg2) & OP_A) || reg2 == T_AH))
{
destroyed = TRUE;
}
}
}
if (destroyed)
{
EmitErr(REGISTER_VALUE_OVERWRITTEN_BY_INVOKE);
*regs_used = 0;
}
if (((proc->sym.langtype == LANG_REGCALL) && (index >= 16)) ||
((proc->sym.langtype == LANG_VECTORCALL) && (index >= 6)) ||
((proc->sym.langtype == LANG_FASTCALL) && (index >= 4)))
{
if (addr)
{
if (psize == 4)
i = T_EAX;
else
{
i = T_RAX;
if (psize < 8)
EmitErr(INVOKE_ARGUMENT_TYPE_MISMATCH, index + 1);
}
*regs_used |= R0_USED;
AddLineQueueX(" lea %r, %s", i, paramvalue);
AddLineQueueX(" mov [%r+%u], %r", T_RSP, NUMQUAL index * 8, i);
DebugMsg(("ms64_param(%s, param=%u): ADDR flags=%X\n", proc->sym.name, index, *regs_used));
return(1);
}
if (opnd->kind == EXPR_CONST ||
(opnd->kind == EXPR_ADDR && opnd->indirect == FALSE && opnd->mem_type == MT_EMPTY && opnd->instr != T_OFFSET))
{
/* v2.06: support 64-bit constants for params > 4 */
if (psize == 8 &&
(opnd->value64 > H_LONG_MAX || opnd->value64 < H_LONG_MIN))
{
AddLineQueueX(" mov %r ptr [%r+%u], %r ( %s )", T_DWORD, T_RSP, NUMQUAL index * 8, T_LOW32, paramvalue);
AddLineQueueX(" mov %r ptr [%r+%u], %r ( %s )", T_DWORD, T_RSP, NUMQUAL index * 8 + 4, T_HIGH32, paramvalue);
return(1);
}
else
{
/* v2.11: no expansion if target type is a pointer and argument is an address part */
if (param->sym.mem_type == MT_PTR && opnd->kind == EXPR_ADDR && opnd->sym->state != SYM_UNDEFINED)
{
DebugMsg(("ms64_param(%s, param=%u): MT_PTR, type error, psize=%u\n", proc->sym.name, index, psize));
EmitErr(INVOKE_ARGUMENT_TYPE_MISMATCH, index + 1);
}
switch (psize)
{
case 1: i = T_BYTE; break;
case 2: i = T_WORD; break;
case 4: i = T_DWORD; break;
default: i = T_QWORD; break;
}
AddLineQueueX(" mov %r ptr [%r+%u], %s", i, T_RSP, NUMQUAL index * 8, paramvalue);
return(1);
}
DebugMsg(("ms64_param(%s, param=%u): MT_EMPTY size.p=%u flags=%X\n", proc->sym.name, index, psize, *regs_used));
}
else if (opnd->kind == EXPR_FLOAT)
{
if (param->sym.mem_type == MT_REAL8)
{
AddLineQueueX(" mov %r ptr [%r+%u+0], %r (%s)", T_DWORD, T_RSP, NUMQUAL index * 8, T_LOW32, paramvalue);
AddLineQueueX(" mov %r ptr [%r+%u+4], %r (%s)", T_DWORD, T_RSP, NUMQUAL index * 8, T_HIGH32, paramvalue);
return(1);
}
else
{
AddLineQueueX(" mov %r ptr [%r+%u], %s", T_DWORD, T_RSP, NUMQUAL index * 8, paramvalue);
return(1);
}
}
else
{ /* it's a register or variable */
if (opnd->kind == EXPR_REG && opnd->indirect == FALSE)
{
size = SizeFromRegister(reg);
if (size == 0x10 && param->sym.mem_type == MT_REAL4)
{
AddLineQueueX(" %s %r ptr [%r+%u], %s", MOVE_SINGLE(), T_DWORD, T_RSP, NUMQUAL index * 8, paramvalue);
return(1);
}
if (size == 0x10 && param->sym.mem_type == MT_REAL8)
{
AddLineQueueX(" %s %r ptr [%r+%u], %s", MOVE_DOUBLE(), T_QWORD, T_RSP, NUMQUAL index * 8, paramvalue);
return(1);
}
if (size == psize)
i = reg;
else
{
if (size > psize || (size < psize && param->sym.mem_type == MT_PTR))
{
DebugMsg(("ms64_param(%s, param=%u): type error size.p/a=%u/%u flags=%X\n", proc->sym.name, index, psize, size, *regs_used));
EmitErr(INVOKE_ARGUMENT_TYPE_MISMATCH, index + 1);
psize = size;
}
switch (psize)
{
case 1: i = T_AL; break;
case 2: i = T_AX; break;
case 4: i = T_EAX; break;
default: i = T_RAX; break;
}
*regs_used |= R0_USED;
}
DebugMsg(("ms64_param(%s, param=%u): REG size.p/a=%u/%u flags=%X\n", proc->sym.name, index, psize, size, *regs_used));
}
else
{
if (opnd->mem_type == MT_EMPTY)
size = (opnd->instr == T_OFFSET ? 8 : 4);
else
size = SizeFromMemtype(opnd->mem_type, USE64, opnd->type);
DebugMsg(("ms64_param(%s, param=%u): MEM size.p/a=%u/%u flags=%X\n", proc->sym.name, index, psize, size, *regs_used));
switch (psize)
{
case 1: i = T_AL; break;
case 2: i = T_AX; break;
case 4: i = T_EAX; break;
default: i = T_RAX; break;
}
if (proc->sym.langtype == LANG_VECTORCALL || proc->sym.langtype == LANG_REGCALL) return(1);
*regs_used |= R0_USED;
}
/* v2.11: no expansion if target type is a pointer */
if (size > psize || (size < psize && param->sym.mem_type == MT_PTR))
{
EmitErr(INVOKE_ARGUMENT_TYPE_MISMATCH, index + 1);
}
if (size != psize)
{
if (size == 4)
{
if (IS_SIGNED(opnd->mem_type))
AddLineQueueX(" movsxd %r, %s", i, paramvalue);
else
AddLineQueueX(" mov %r, %s", i, paramvalue);
}
else
AddLineQueueX(" mov%sx %r, %s", IS_SIGNED(opnd->mem_type) ? "s" : "z", i, paramvalue);
}
else if (opnd->kind != EXPR_REG || opnd->indirect == TRUE)
AddLineQueueX(" mov %r, %s", i, paramvalue);
AddLineQueueX(" mov [%r+%u], %r", T_RSP, NUMQUAL index * 8, i);
return(1);
}
}
else if (param->sym.mem_type == MT_REAL4 || param->sym.mem_type == MT_REAL8)
{
/* v2.04: check if argument is the correct XMM register already */
if (opnd->kind == EXPR_REG && opnd->indirect == FALSE)
{
if (GetValueSp(reg) & OP_XMM)
{
if (proc->sym.langtype == LANG_REGCALL)
info->rgcregs[index] = 1;
if (proc->sym.langtype == LANG_VECTORCALL)
info->vregs[index] = 1;
if (reg == T_XMM0 + index)
DebugMsg(("ms64_param(%s, param=%u): argument optimized\n", proc->sym.name, index));
else
{
if (param->sym.mem_type == MT_REAL4)
AddLineQueueX(" %s %r, %s", MOVE_ALIGNED_FLOAT(), T_XMM0 + index, paramvalue);
else
AddLineQueueX(" %s %r, %s", MOVE_ALIGNED_FLOAT(), T_XMM0 + index, paramvalue);
}
return(1);
}
}
else if (opnd->kind == EXPR_REG && opnd->indirect == TRUE)
{
if (reg == T_XMM0 + index)
DebugMsg(("ms64_param(%s, param=%u): argument optimized\n", proc->sym.name, index));
else
{
if (param->sym.mem_type == MT_REAL4)
AddLineQueueX(" %s %r, %s", MOVE_SIMD_DWORD(), T_XMM0 + index, paramvalue);
else
AddLineQueueX(" %s %r, %s", MOVE_SIMD_QWORD(), T_XMM0 + index, paramvalue);
}
return(1);
}
if (opnd->kind == EXPR_FLOAT)
{
if (proc->sym.langtype == LANG_REGCALL)
{
info->rgcregs[index] = 1;
info->regcxyzused[index] = 1; /* JPH */
}
if (proc->sym.langtype == LANG_VECTORCALL)
{
info->vregs[index] = 1;
info->xyzused[index] = 1; /* JPH */
}
*regs_used |= R0_USED;
if (param->sym.mem_type == MT_REAL4)
{
AddLineQueueX("mov %r, %s", T_EAX, paramvalue);
AddLineQueueX("%s %r, %r", MOVE_SIMD_DWORD(), T_XMM0 + index, T_EAX);
return(1);
}
else
{
AddLineQueueX("mov %r, %r ptr %s", T_RAX, T_REAL8, paramvalue);
AddLineQueueX("%s %r, %r", MOVE_SIMD_QWORD(), T_XMM0 + index, T_RAX);
return(1);
}
}
if (opnd->kind == EXPR_ADDR)
{
if (proc->sym.langtype == LANG_VECTORCALL)
{
*regs_used |= R0_USED;
info->vregs[index] = 1;
info->xyzused[index] = 1; /* JPH */
if (opnd->sym->mem_type == MT_REAL8)
{
AddLineQueueX("%s %r,qword ptr %s", MOVE_DOUBLE(), T_XMM0 + index, paramvalue);
}
else
{
AddLineQueueX("%s %r,dword ptr %s", MOVE_SINGLE(), T_XMM0 + index, paramvalue);
}
}
else if (proc->sym.langtype == LANG_REGCALL)
{
*regs_used |= R0_USED;
info->rgcregs[index] = 1;
info->regcxyzused[index] = 1; /* JPH */
if (opnd->sym->mem_type == MT_REAL8)
{
AddLineQueueX("%s %r,qword ptr %s", MOVE_DOUBLE(), T_XMM0 + index, paramvalue);
}
else
{
AddLineQueueX("%s %r,dword ptr %s", MOVE_SINGLE(), T_XMM0 + index, paramvalue);
}
}
else
{
if (param->sym.mem_type == MT_REAL8)
AddLineQueueX("%s %r,qword ptr %s", MOVE_SIMD_QWORD(), T_XMM0 + index, paramvalue);
else if (param->sym.mem_type == MT_REAL4)
AddLineQueueX("%s %r,dword ptr %s", MOVE_SIMD_DWORD(), T_XMM0 + index, paramvalue);
}
return(1);
}
}
if (vcallpass == 1)
goto vcalldone;
vcall:
if ((proc->sym.langtype == LANG_VECTORCALL) && (psize == 8) && (param->sym.ttype != 0) && (param->sym.ttype->e.structinfo->isHFA) && (param->sym.mem_type == MT_TYPE))
{
/* it can be only HFA data type made of 2 REAL4 */
t = param->sym.ttype;
if (t != 0 && t->e.structinfo->isHFA && (vcallpass == 1))
{
freevecregs = 0;
vecidx = -1;
for (i = 0; i < 6; i++)
{
if (info->vregs[i] == 0)
{
if (vecidx == -1) vecidx = i;
freevecregs++;
}
}
memberCount = t->e.structinfo->memberCount;
if (memberCount > freevecregs) goto uselea;
membersize = psize / memberCount; //get the size of a single element which is REAL4 in this case
j = 0;