-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.c
1473 lines (1385 loc) · 61.9 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 "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "reswords.h"
#include "expreval.h"
#include "input.h"
#include "equate.h"
#include "assume.h"
#include "segment.h"
#include "listing.h"
#include "myassert.h"
#if DLLIMPORT
#include "mangle.h"
#include "extern.h"
#endif
#include "proc.h"
extern int_64 maxintvalues[];
extern int_64 minintvalues[];
#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 */
#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 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: irst param start at bit 3 */
#endif
};
static int size_vararg;
static int fcscratch; /* exclusively to be used by FASTCALL helper functions */
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 * );
};
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 */
#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 enum special_token stackreg[] = { T_SP, T_ESP,
#if AMD64_SUPPORT
T_RSP
#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
};
#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
/* 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 *opndx, 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 )
return( 0 );
if ( GetSymOfssize( &proc->sym ) == USE16 ) {
pst = ms16_regs + fcscratch;
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 ( ( opndx->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 ( opndx->kind == EXPR_REG && opndx->indirect == 0 && opndx->base_reg ) {
if ( opndx->base_reg->tokval == reg )
return( 1 );
}
AddLineQueueX( " mov %r, %s", reg, paramvalue );
}
}
if ( *pst == T_AX )
*r0used |= R0_USED;
return( 1 );
}
#if AMD64_SUPPORT
static int ms64_fcstart( struct dsym const *proc, int numparams, int start, struct asm_tok tokenarray[], int *value )
/*******************************************************************************************************************/
{
/* v2.04: VARARG didn't work */
if ( proc->e.procinfo->is_vararg ) {
//numparams = ( tokenarray[start].token != T_FINAL ? 1 : 0 );
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->is_vararg ));
if ( numparams < 4 )
numparams = 4;
else if ( numparams & 1 )
numparams++;
*value = numparams;
if ( ModuleInfo.win64_flags & W64F_AUTOSTACKSP ) {
if ( ( numparams * sizeof( uint_64 ) ) > ReservedStack.value )
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 )
/*
* parameter for Win64 FASTCALL.
* the first 4 parameters are hold 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.
*/
static int ms64_param( struct dsym const *proc, int index, struct dsym *param, bool addr, struct expr *opndx, char *paramvalue, uint_8 *regs_used )
/*************************************************************************************************************************************************/
{
uint_32 size;
uint_32 psize;
int reg;
int reg2;
int i;
int base;
bool destroyed = FALSE;
DebugMsg1(("ms64_param(%s, index=%u, param.memtype=%Xh, addr=%u) enter\n", proc->sym.name, index, param->sym.mem_type, addr ));
psize = SizeFromMemtype( param->sym.mem_type, USE64, param->sym.type );
if ( index >= 4 ) {
/* check for register overwrites */
if ( opndx->base_reg != NULL ) {
reg = opndx->base_reg->tokval;
if ( GetValueSp( reg ) & OP_R ) {
i = GetRegNo( reg );
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 ( opndx->idx_reg != NULL ) {
reg2 = opndx->idx_reg->tokval;
if ( GetValueSp( reg2 ) & OP_R ) {
i = GetRegNo( reg2 );
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 ( opndx->indirect == FALSE && opndx->base_reg != NULL ) {
i = reg;
size = SizeFromRegister( reg );
if ( size != psize ) {
if ( param->sym.is_vararg == FALSE ) {
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;
}
}
if ( opndx->mem_type == MT_EMPTY && addr == FALSE ) {
/* v2.06: support 64-bit constants for params > 4 */
if ( psize == 8 && opndx->kind == EXPR_CONST &&
( opndx->value64 > LONG_MAX || opndx->value64 < 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 );
} else if ( param->sym.mem_type == MT_REAL8 && opndx->kind == EXPR_FLOAT ) {
*regs_used |= R0_USED;
AddLineQueueX( " mov %r, %r ptr %s", T_RAX, T_REAL8, paramvalue );
AddLineQueueX( " mov [%r+%u], %r", T_RSP, NUMQUAL index*8, T_RAX );
} else {
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 );
}
DebugMsg(("ms64_param(%s, param=%u): MT_EMPTY size.p/a=%u/%u flags=%X\n", proc->sym.name, index, psize, size, *regs_used ));
} else {
if ( addr == FALSE ) {
if ( opndx->indirect == FALSE && opndx->base_reg != NULL ) {
DebugMsg(("ms64_param(%s, param=%u): REG size.p/a=%u/%u flags=%X\n", proc->sym.name, index, psize, size, *regs_used ));
} else {
DebugMsg(("ms64_param(%s, param=%u): MEM size.p/a=%u/%u flags=%X\n", proc->sym.name, index, psize, size, *regs_used ));
size = SizeFromMemtype( opndx->mem_type, USE64, opndx->type );
if ( size != psize && param->sym.is_vararg == FALSE )
EmitErr( INVOKE_ARGUMENT_TYPE_MISMATCH, index+1 );
switch ( size ) {
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;
if ( size <= 8 )
AddLineQueueX( " mov %r, %s", i, paramvalue );
else {
AddLineQueueX( " lea %r, %s", i, paramvalue );
}
}
} else {
if ( param->sym.is_vararg == TRUE )
psize = 8;
switch ( psize ) {
case 4: i = T_EAX; break;
case 8: i = T_RAX; break;
default:
EmitErr( INVOKE_ARGUMENT_TYPE_MISMATCH, index+1 );
i = T_RAX;
}
*regs_used |= R0_USED;
AddLineQueueX( " lea %r, %s", i, paramvalue );
DebugMsg(("ms64_param(%s, param=%u): ADDR flags=%X\n", proc->sym.name, index, *regs_used ));
}
AddLineQueueX( " mov [%r+%u], %r", T_RSP, NUMQUAL index*8, i );
}
} 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 ( opndx->indirect == FALSE && opndx->base_reg != NULL ) {
reg = opndx->base_reg->tokval;
if ( GetValueSp( reg ) & OP_XMM ) {
if ( reg == T_XMM0 + index )
DebugMsg(("ms64_param(%s, param=%u): argument optimized\n", proc->sym.name, index ));
else
AddLineQueueX( " movq %r, %s", T_XMM0 + index, paramvalue );
return( 1 );
}
}
if ( opndx->kind == EXPR_FLOAT ) {
*regs_used |= R0_USED;
if ( param->sym.mem_type == MT_REAL4 ) {
AddLineQueueX( " mov %r, %s", T_EAX, paramvalue );
AddLineQueueX( " movd %r, %r", T_XMM0 + index, T_EAX );
} else {
AddLineQueueX( " mov %r, %r ptr %s", T_RAX, T_REAL8, paramvalue );
AddLineQueueX( " movd %r, %r", T_XMM0 + index, T_RAX );
}
} else {
if ( param->sym.mem_type == MT_REAL4 )
AddLineQueueX( " movd %r, %s", T_XMM0 + index, paramvalue );
else
AddLineQueueX( " movq %r, %s", T_XMM0 + index, paramvalue );
}
} else {
if ( addr || psize > 8 ) { /* psize > 8 shouldn't happen! */
if ( psize == 4 )
AddLineQueueX( " lea %r, %s", ms64_regs[index+2*4], paramvalue );
//else if ( psize > 4 ) /* v2.07: psize==0 happens for VARARG */
else if ( psize > 4 || param->sym.is_vararg )
AddLineQueueX( " lea %r, %s", ms64_regs[index+3*4], paramvalue );
else
EmitErr( INVOKE_ARGUMENT_TYPE_MISMATCH, index+1 );
*regs_used |= ( 1 << ( index + RPAR_START ) );
return( 1 );
}
/* register argument? */
if ( opndx->indirect == FALSE && opndx->base_reg != NULL ) {
reg = opndx->base_reg->tokval;
size = SizeFromRegister( reg );
} else {
if ( opndx->mem_type != MT_EMPTY )
size = SizeFromMemtype( opndx->mem_type, USE64, opndx->type );
else {
size = psize;
}
}
if ( size != psize && param->sym.is_vararg == FALSE ) {
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 );
}
switch ( size ) {
case 1: base = 0*4; break;
case 2: base = 1*4; break;
case 4: base = 2*4; break;
default:base = 3*4; break;
}
/* optimization if the register holds the value already */
if ( opndx->indirect == FALSE && opndx->base_reg != NULL ) {
if ( GetValueSp( reg ) & OP_R ) {
if ( ms64_regs[index+base] == reg ) {
DebugMsg(("ms64_param(%s, param=%u): argument optimized\n", proc->sym.name, index ));
return( 1 );
}
i = GetRegNo( reg );
if ( REGPAR_WIN64 & ( 1 << i ) ) {
i = GetParmIndex( i );
if ( *regs_used & ( 1 << ( i + RPAR_START ) ) )
EmitErr( REGISTER_VALUE_OVERWRITTEN_BY_INVOKE );
}
}
}
AddLineQueueX( " mov %r, %s", ms64_regs[index+base], paramvalue );
*regs_used |= ( 1 << ( index + RPAR_START ) );
DebugMsg(("ms64_param(%s, param=%u): size=%u flags=%X\n", proc->sym.name, index, size, *regs_used ));
}
return( 1 );
}
#endif
/* get segment part of an argument
* v2.05: extracted from PushInvokeParam(),
* so it could be used by watc_param() as well.
*/
static void GetSegmentPart( struct expr *opndx, char *buffer, const char *fullparam )
/***********************************************************************************/
{
if ( opndx->override != NULL ) {
strcpy( buffer, opndx->override->string_ptr );
} else if ( opndx->sym != NULL && opndx->sym->segment != NULL ) {
struct dsym *dir = GetSegm( opndx->sym );
enum assume_segreg as;
if ( dir->e.seginfo->segtype == SEGTYPE_DATA ||
dir->e.seginfo->segtype == SEGTYPE_BSS )
as = search_assume( (struct asym *)dir, ASSUME_DS, TRUE );
else
as = search_assume( (struct asym *)dir, ASSUME_CS, TRUE );
if ( as != ASSUME_NOTHING ) {
//GetResWName( segreg_tab[as], buffer );
GetResWName( T_ES + as, buffer ); /* v2.08: T_ES is first seg reg in special.h */
} else {
struct asym *seg;
seg = GetGroup( opndx->sym );
if (seg == NULL)
seg = &dir->sym;
if ( seg )
strcpy( buffer, seg->name );
else {
strcpy( buffer, "seg " );
strcat( buffer, fullparam );
}
}
} else if ( opndx->sym && opndx->sym->state == SYM_STACK ) {
GetResWName( T_SS, buffer );
} else {
strcpy( buffer,"seg " );
strcat( buffer, fullparam );
}
return;
}
#if OWFC_SUPPORT
/* the watcomm fastcall variant is somewhat peculiar:
* 16-bit:
* - for BYTE/WORD arguments, there are 4 registers: AX,DX,BX,CX
* - for DWORD arguments, there are 2 register pairs: DX::AX and CX::BX
* - there is a "usage" flag for each register. Thus the prototype:
* sample proto :WORD, :DWORD, :WORD
* will assign AX to the first param, CX::BX to the second, and DX to
* the third!
*/
static int watc_fcstart( struct dsym const *proc, int numparams, int start, struct asm_tok tokenarray[], int *value )
/*******************************************************************************************************************/
{
DebugMsg1(("watc_fcstart(%s, %u, %u)\n", proc->sym.name, numparams, start ));
return( 1 );
}
static void watc_fcend( struct dsym const *proc, int numparams, int value )
/*************************************************************************/
{
DebugMsg1(("watc_fcend(%s, %u, %u)\n", proc->sym.name, numparams, value ));
if ( proc->e.procinfo->is_vararg ) {
AddLineQueueX( " add %r, %u", stackreg[ModuleInfo.Ofssize], NUMQUAL proc->e.procinfo->parasize + size_vararg );
} else if ( fcscratch < proc->e.procinfo->parasize ) {
AddLineQueueX( " add %r, %u", stackreg[ModuleInfo.Ofssize], NUMQUAL ( proc->e.procinfo->parasize - fcscratch ) );
}
return;
}
/* get the register for parms 0 to 3,
* using the watcom register parm passing conventions ( A D B C )
*/
static int watc_param( struct dsym const *proc, int index, struct dsym *param, bool addr, struct expr *opndx, char *paramvalue, uint_8 *r0used )
/**********************************************************************************************************************************************/
{
int opc;
int qual;
int i;
char regs[64];
char *reg[4];
char *p;
int psize = SizeFromMemtype( param->sym.mem_type, USE_EMPTY, param->sym.type );
DebugMsg1(("watc_param(%s, param=%u [name=%s, state=%u]),addr=%u: psize=%u\n", proc->sym.name, index, param->sym.name, param->sym.state, addr, psize ));
if ( param->sym.state != SYM_TMACRO )
return( 0 );
DebugMsg1(("watc_param(%s): register param=%s\n", proc->sym.name, param->sym.string_ptr ));
fcscratch += CurrWordSize;
/* the "name" might be a register pair */
reg[0] = param->sym.string_ptr;
reg[1] = NULL;
reg[2] = NULL;
reg[3] = NULL;
if ( strchr( reg[0], ':' ) ) {
strcpy( regs, reg[0] );
fcscratch += CurrWordSize;
for ( p = regs, i = 0; i < 4; i++ ) {
reg[i] = p;
p = strchr( p, ':' );
if ( p == NULL )
break;
*p++ = NULLC;
p++;
}
}
if ( addr ) {
if ( opndx->kind == T_REG || opndx->sym->state == SYM_STACK ) {
opc = T_LEA;
qual = T_NULL;
} else {
opc = T_MOV;
qual = T_OFFSET;
}
/* v2.05: filling of segment part added */
i = 0;
if ( reg[1] != NULL ) {
char buffer[128];
GetSegmentPart( opndx, buffer, paramvalue );
AddLineQueueX( "%r %s, %s", T_MOV, reg[0], buffer );
i++;
}
AddLineQueueX( "%r %s, %r %s", opc, reg[i], qual, paramvalue );
return( 1 );
}
for ( i = 3; i >= 0; i-- ) {
if ( reg[i] ) {
if ( opndx->kind == EXPR_CONST ) {
if ( i > 0 )
qual = T_LOWWORD;
else if ( i == 0 && reg[1] != NULL )
qual = T_HIGHWORD;
else
qual = T_NULL;
if ( qual != T_NULL )
AddLineQueueX( "mov %s, %r (%s)", reg[i], qual, paramvalue );
else
AddLineQueueX( "mov %s, %s", reg[i], paramvalue );
} else if ( opndx->kind == EXPR_REG ) {
AddLineQueueX( "mov %s, %s", reg[i], paramvalue );
} else {
if ( i == 0 && reg[1] == NULL )
AddLineQueueX( "mov %s, %s", reg[i], paramvalue );
else {
if ( ModuleInfo.Ofssize )
qual = T_DWORD;
else
qual = T_WORD;
AddLineQueueX( "mov %s, %r %r %s[%u]", reg[i], qual, T_PTR, paramvalue, psize - ( (i+1) * ( 2 << ModuleInfo.Ofssize ) ) );
}
}
}
}
return( 1 );
}
#endif
static void SkipTypecast( char *fullparam, int i, struct asm_tok tokenarray[] )
/*****************************************************************************/
{
int j;
fullparam[0] = NULLC;
for ( j = i; ; j++ ) {
if (( tokenarray[j].token == T_COMMA ) || ( tokenarray[j].token == T_FINAL ) )
break;
if (( tokenarray[j+1].token == T_BINARY_OPERATOR ) && ( tokenarray[j+1].tokval == T_PTR ) )
j = j + 1;
else {
if ( fullparam[0] != NULLC )
strcat( fullparam," " );
strcat( fullparam, tokenarray[j].string_ptr );
}
}
}
/*
* push one parameter of a procedure called with INVOKE onto the stack
* - i : index of the start of the parameter list
* - tokenarray : token array
* - proc : the PROC to call
* - curr : the current parameter
* - reqParam: the index of the parameter which is to be pushed
* - r0flags : flags for register usage across params
*
* psize,asize: size of parameter/argument in bytes.
*/
static int PushInvokeParam( int i, struct asm_tok tokenarray[], struct dsym *proc, struct dsym *curr, int reqParam, uint_8 *r0flags)
/**********************************************************************************************************************************/
{
int currParm;
int psize;
int asize;
int pushsize;
int j;
int fptrsize;
bool addr = FALSE; /* ADDR operator found */
struct expr opndx;
char fullparam[MAX_LINE_LEN];
char buffer[MAX_LINE_LEN];
DebugMsg1(("PushInvokeParam(%s, param=%s:%u, i=%u ) enter\n", proc->sym.name, curr ? curr->sym.name : "NULL", reqParam, i ));
for ( currParm = 0; currParm <= reqParam; ) {
if ( tokenarray[i].token == T_FINAL ) { /* this is no real error! */
DebugMsg1(("PushInvokeParam(%s): T_FINAL token, i=%u\n", proc->sym.name, i));
return( ERROR );
}
if ( tokenarray[i].token == T_COMMA ) {
currParm++;
}
i++;
}
/* if curr is NULL this call is just a parameter check */
if ( !curr ) return( NOT_ERROR );
#if 1 /* v2.05 */
psize = curr->sym.total_size;
DebugMsg1(("PushInvokeParam(%s,%u): memtype=%X, psize=%u\n", proc->sym.name, reqParam, curr->sym.mem_type, psize ));
#else
/* set psize (size of parameter) */
if ( curr->is_ptr ) {
psize = 2 << curr->sym.Ofssize;
if ( curr->sym.isfar )
psize += 2;
} else
psize = SizeFromMemtype( curr->sym.mem_type, curr->sym.Ofssize, curr->sym.type );
DebugMsg1(("PushInvokeParam(%s,%u): is_ptr=%u, memtype=%X, psize=%u\n", proc->sym.name, reqParam, curr->is_ptr, curr->sym.mem_type, psize ));
#endif
/* ADDR: the argument's address is to be pushed? */
if ( tokenarray[i].token == T_RES_ID && tokenarray[i].tokval == T_ADDR ) {
addr = TRUE;
i++;
}
/* copy the parameter tokens to fullparam */
for ( j = i; tokenarray[j].token != T_COMMA && tokenarray[j].token != T_FINAL; j++ );
memcpy( fullparam, tokenarray[i].tokpos, tokenarray[j].tokpos - tokenarray[i].tokpos );
fullparam[tokenarray[j].tokpos - tokenarray[i].tokpos] = NULLC;
j = i;
fptrsize = 2 + ( 2 << GetSymOfssize( &proc->sym ) );
if ( addr ) {
/* v2.06: don't handle forward refs if -Zne is set */
//if ( EvalOperand( &j, Token_Count, &opndx, 0 ) == ERROR )
if ( EvalOperand( &j, tokenarray, Token_Count, &opndx, ModuleInfo.invoke_exprparm ) == ERROR )
return( ERROR );
/* DWORD (16bit) and FWORD(32bit) are treated like FAR ptrs */
if ( psize > fptrsize ) {
/* QWORD is NOT accepted as a FAR ptr */
DebugMsg1(("PushInvokeParm(%u): error, psize=%u, fptrsize=%u\n",
reqParam, psize, fptrsize));
EmitErr( INVOKE_ARGUMENT_TYPE_MISMATCH, reqParam+1 );
return( NOT_ERROR );
}
if ( proc->sym.langtype == LANG_FASTCALL )
if ( fastcall_tab[ModuleInfo.fctype].handleparam( proc, reqParam, curr, addr, &opndx, fullparam, r0flags ) )
return( NOT_ERROR );
if ( opndx.kind == EXPR_REG || opndx.indirect ) {
if ( curr->sym.isfar || psize == fptrsize ) {
DebugMsg1(("PushInvokeParam: far ptr, %s isfar=%u, psize=%u, fptrsize=%u\n", curr->sym.name, curr->sym.isfar, psize, fptrsize ));
if ( opndx.sym && opndx.sym->state == SYM_STACK )
GetResWName( T_SS, buffer );
else if ( opndx.override != NULL )
strcpy( buffer, opndx.override->string_ptr );
else
GetResWName( T_DS, buffer );
AddLineQueueX( " push %s", buffer );
}
AddLineQueueX( " lea %r, %s", regax[ModuleInfo.Ofssize], fullparam );
*r0flags |= R0_USED;
AddLineQueueX( " push %r", regax[ModuleInfo.Ofssize] );
} else {
push_address:
/* push segment part of address? */
if ( curr->sym.isfar || psize == fptrsize ) {
GetSegmentPart( &opndx, buffer, fullparam );
AddLineQueueX( " push %s", buffer );
}
/* push offset part of address */
if ( (ModuleInfo.curr_cpu & P_CPU_MASK ) < P_186 ) {
AddLineQueueX( " mov %r, offset %s", T_AX, fullparam );
AddLineQueueX( " push %r", T_AX );
*r0flags |= R0_USED;
} else {
if ( curr->sym.is_vararg &&
opndx.Ofssize == USE_EMPTY &&
opndx.sym )
opndx.Ofssize = GetSymOfssize( opndx.sym );
/* v2.04: expand 16-bit offset to 32 */
if ( opndx.Ofssize == USE16 && CurrWordSize > 2 ) {
AddLineQueueX( " pushd offset %s", fullparam );
} else {
AddLineQueueX( " push offset %s", fullparam );
/* v2.04: a 32bit offset pushed in 16-bit code */
if ( curr->sym.is_vararg &&
CurrWordSize == 2 &&
opndx.Ofssize > USE16 )
size_vararg += CurrWordSize;
}
}
}
if ( curr->sym.is_vararg ) {
size_vararg += CurrWordSize;
DebugMsg1(("PushInvokeParm(%u): %u added to size_vararg, now=%u\n", reqParam, CurrWordSize, size_vararg ));
if ( curr->sym.isfar ) {
size_vararg += CurrWordSize;
DebugMsg1(("PushInvokeParm(%u): %u added to size_vararg, now=%u\n", reqParam, CurrWordSize, size_vararg ));
}
}
} else { /* ! ADDR branch */
/* handle the <reg>::<reg> case here, the evaluator wont handle it */
if ( tokenarray[j].token == T_REG &&
tokenarray[j+1].token == T_DBL_COLON &&
tokenarray[j+2].token == T_REG ) {
int asize2;
/* for pointers, segreg size is assumed to be always 2 */
if ( GetValueSp( tokenarray[j].tokval ) & OP_SR )
asize2 = 2;
else
asize2 = SizeFromRegister( tokenarray[j].tokval );
asize = SizeFromRegister( tokenarray[j+2].tokval );
AddLineQueueX( " push %r", tokenarray[j].tokval );
/* v2.04: changed */
if (( curr->sym.is_vararg ) && (asize + asize2) != CurrWordSize )
size_vararg += asize2;
else
asize += asize2;
strcpy( fullparam, tokenarray[j+2].string_ptr );
opndx.kind = EXPR_REG;
opndx.indirect = FALSE;
opndx.sym = NULL;
opndx.base_reg = &tokenarray[j+2]; /* for error msg 'eax overwritten...' */
} else {
/* v2.06: don't handle forward refs if -Zne is set */
//if ( EvalOperand( &j, Token_Count, &opndx, 0 ) == ERROR ) {
if ( EvalOperand( &j, tokenarray, Token_Count, &opndx, ModuleInfo.invoke_exprparm ) == ERROR ) {
return( ERROR );
}
/* for a simple register, get its size */
if ( opndx.kind == EXPR_REG && opndx.indirect == FALSE ) {
asize = SizeFromRegister( opndx.base_reg->tokval );
} else if ( opndx.mem_type == MT_EMPTY ) {
asize = psize;
/* v2.04: added, to catch 0-size params ( STRUCT without members ) */
if ( psize == 0 ) {
if ( curr->sym.is_vararg == FALSE ) {
DebugMsg1(("PushInvokeParm(%u): error, psize=0\n" ));
EmitErr( INVOKE_ARGUMENT_TYPE_MISMATCH, reqParam+1 );
}
/* v2.07: for VARARG, get the member's size if it is a structured var */
if ( opndx.mbr && opndx.mbr->mem_type == MT_TYPE )
asize = SizeFromMemtype( opndx.mbr->mem_type, opndx.Ofssize, opndx.mbr->type );
}
DebugMsg1(("PushInvokeParm(%u): memtype EMPTY, asize=%u psize=%u\n", reqParam, asize, psize ));
} else if ( opndx.mem_type != MT_TYPE ) {
if ( opndx.kind == EXPR_ADDR &&
opndx.indirect == FALSE &&
opndx.sym &&
opndx.instr == EMPTY &&
(opndx.mem_type == MT_PROC ||
opndx.mem_type == MT_FAR ||
opndx.mem_type == MT_NEAR))
goto push_address;
if ( opndx.Ofssize == USE_EMPTY )
opndx.Ofssize = ModuleInfo.Ofssize;
asize = SizeFromMemtype( opndx.mem_type, opndx.Ofssize, opndx.type );
} else {
if ( opndx.sym != NULL )
asize = opndx.sym->type->total_size;
else
asize = opndx.mbr->type->total_size;
}
}
if ( curr->sym.is_vararg == TRUE )
psize = asize;
#ifdef DEBUG_OUT
if ( opndx.sym )
DebugMsg1(("PushInvokeParam(%s, %u): arg name=%s, asize=%u, psize=%u\n", proc->sym.name, reqParam, opndx.sym->name, asize, psize));
else
DebugMsg1(("PushInvokeParam(%s, %u): arg no name, asize=%u, psize=%u\n", proc->sym.name, reqParam, asize, psize));
#endif
pushsize = CurrWordSize;
if ( proc->sym.langtype == LANG_FASTCALL )
if ( fastcall_tab[ModuleInfo.fctype].handleparam( proc, reqParam, curr, addr, &opndx, fullparam, r0flags ) )
return( NOT_ERROR );
/* v2.04: this check has been moved behind the fastcall_tab() call */
if ( asize > psize ) { /* argument's size too big? */
DebugMsg(("PushInvokeParm(%u): argsize error, arg size=%d, parm size=%d\n", reqParam, asize, psize));
EmitErr( INVOKE_ARGUMENT_TYPE_MISMATCH, reqParam+1 );
return( NOT_ERROR );
}
if ( ( opndx.kind == EXPR_ADDR && opndx.instr != T_OFFSET ) ||
( opndx.kind == EXPR_REG && opndx.indirect == TRUE ) ) {
/* catch the case when EAX has been used for ADDR,
* and is later used as addressing register!
*
*/
if ( *r0flags &&
(( opndx.base_reg != NULL &&
( opndx.base_reg->tokval == T_EAX
#if AMD64_SUPPORT
|| opndx.base_reg->tokval == T_RAX
#endif
)) ||
( opndx.idx_reg != NULL &&
( opndx.idx_reg->tokval == T_EAX
#if AMD64_SUPPORT
|| opndx.idx_reg->tokval == T_RAX
#endif
)))) {
EmitErr( REGISTER_VALUE_OVERWRITTEN_BY_INVOKE );
*r0flags = 0;
}
if ( asize > pushsize ) {
short dw = T_WORD;
if (( ModuleInfo.curr_cpu & P_CPU_MASK ) >= P_386 ) {
pushsize = 4;
dw = T_DWORD;
}
if ( curr->sym.is_vararg ) {
size_vararg += asize;
DebugMsg1(("PushInvokeParm(%u): asize=%u added to size_vararg, now=%u\n", reqParam, asize, size_vararg ));
}
/* in params like "qword ptr [eax]" the typecast
* has to be removed */
if ( opndx.explicit ) {
SkipTypecast( fullparam, i, tokenarray );
opndx.explicit = FALSE;
}
while ( asize > 0 ) {
#if 0
if ( opndx.explicit ) {
sprintf( buffer, " push %s", fullparam );
asize = 0;
} else if ( asize & 2 ) {
#else
if ( asize & 2 ) {
#endif
/* ensure the stack remains dword-aligned in 32bit */
if ( ModuleInfo.Ofssize > USE16 ) {
/* v2.05: better push a 0 word? */
//AddLineQueueX( " pushw 0" );
#if AMD64_SUPPORT
AddLineQueueX( " sub %r, 2", stackreg[ModuleInfo.Ofssize] );
#else
AddLineQueueX( " sub %r, 2", T_ESP );
#endif
}
AddLineQueueX( " push word ptr %s+%u", fullparam, NUMQUAL asize-2 );
asize -= 2;
} else {
AddLineQueueX( " push %r ptr %s+%u", dw, fullparam, NUMQUAL asize-pushsize );
asize -= pushsize;
}
}
return( NOT_ERROR );
} else if ( asize < pushsize ) {
if ( curr->sym.is_vararg ) {
size_vararg += pushsize;
DebugMsg1(("PushInvokeParm(%u): %u added to size_vararg, now=%u\n", reqParam, pushsize, size_vararg ));
}
if ( psize > 4 ) {
DebugMsg1(("PushInvokeParm(%u): error, ADDR, psize=%u, is > 4\n",
reqParam, psize ));
EmitErr( INVOKE_ARGUMENT_TYPE_MISMATCH, reqParam+1 );
}
//switch (sym->mem_type) {
switch ( opndx.mem_type ) {
case MT_BYTE:
case MT_SBYTE:
if ( psize == 1 && curr->sym.is_vararg == FALSE ) {
AddLineQueueX( " mov %r, %s", T_AL, fullparam );
AddLineQueueX( " push %r", pushsize == 4 ? T_EAX : T_AX );
} else if ( pushsize == 2 ) {
if ( psize == 4 )
AddLineQueue( " push 0" );
AddLineQueueX( " mov %r, %s", T_AL, fullparam );
if ( opndx.mem_type == MT_BYTE ) {
if ( !( *r0flags & R0_H_CLEARED ))
AddLineQueueX( " mov %r, 0", T_AH );
*r0flags |= R0_H_CLEARED;
} else {
*r0flags = 0; /* reset AH_CLEARED */
AddLineQueue( " cbw" );
}
AddLineQueueX( " push %r", T_AX );
} else {
AddLineQueueX( " %r %r, %s", opndx.mem_type == MT_BYTE ? T_MOVZX : T_MOVSX, T_EAX, fullparam );
AddLineQueueX( " push %r", T_EAX );
}
*r0flags |= R0_USED;
break;
case MT_WORD:
case MT_SWORD:
/* v2.04: use the Masm-compatible, non-destructive
* PUSH if psize is 2.
*/
//if ( Options.masm_compat_gencode ) {
if ( Options.masm_compat_gencode || psize == 2 ) {
/* v2.05: push a 0 word if argument is VARARG */
if ( curr->sym.is_vararg )
AddLineQueueX( " pushw 0" );
else {
#if AMD64_SUPPORT
AddLineQueueX( " sub %r, 2", stackreg[ModuleInfo.Ofssize] );
#else
AddLineQueueX( " sub %r, 2", T_ESP );
#endif
}
AddLineQueueX( " push %s", fullparam );
} else {
AddLineQueueX( " %r %r, %s", opndx.mem_type == MT_WORD ? T_MOVZX : T_MOVSX, T_EAX, fullparam );
AddLineQueueX( " push %r", T_EAX );
*r0flags = R0_USED; /* reset R0_H_CLEARED */
}
break;
default:
AddLineQueueX( " push %s", fullparam );
}
} else { /* asize == pushsize */
if (( pushsize == 2 ) || (( ModuleInfo.curr_cpu & P_CPU_MASK ) >= P_386 ))
AddLineQueueX( " push %s", fullparam );
else {
AddLineQueueX( " push word ptr %s+2", tokenarray[i].string_ptr );
AddLineQueueX( " push word ptr %s", tokenarray[i].string_ptr );
}
if ( curr->sym.is_vararg ) {
size_vararg += pushsize;
DebugMsg1(("PushInvokeParm(%u): asize=%u added to size_vararg, now=%u\n", reqParam, pushsize, size_vararg ));
}
}
} else { /* the parameter is a register or constant value! */
char is_r0 = FALSE;