-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc.c
2670 lines (2407 loc) · 91.4 KB
/
proc.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 PROC/ENDP/LOCAL directives.
*
****************************************************************************/
#include <ctype.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "segment.h"
#include "extern.h"
#include "equate.h"
#include "fixup.h"
#include "labels.h"
#include "input.h"
#include "tokenize.h"
#include "expreval.h"
#include "types.h"
#include "condasm.h"
#include "macro.h"
#include "proc.h"
#include "fastpass.h"
#include "listing.h"
#include "posndir.h"
#include "myassert.h"
#include "reswords.h"
#if AMD64_SUPPORT
#include "win64seh.h"
#endif
#ifdef __I86__
#define NUMQUAL (long)
#else
#define NUMQUAL
#endif
extern const char szDgroup[];
/*
* Masm allows nested procedures
* but they must NOT have params or locals
*/
/*
* calling convention FASTCALL supports:
* - Watcom C: registers e/ax,e/dx,e/bx,e/cx
* - MS fastcall 16-bit: registers ax,dx,bx (default for 16bit)
* - MS fastcall 32-bit: registers ecx,edx (default for 32bit)
* - Win64: registers rcx, rdx, r8, r9 (default for 64bit)
*/
struct dsym *CurrProc; /* current procedure */
int procidx; /* procedure index */
static struct proc_info *ProcStack;
bool DefineProc; /* TRUE if definition of procedure
* hasn't ended yet */
#if AMD64_SUPPORT
static bool endprolog_found;
static uint_8 unw_segs_defined;
static UNWIND_INFO unw_info = {UNW_VERSION, 0, 0, 0, 0, 0 };
static UNWIND_CODE unw_code[128];
#endif
#if AMD64_SUPPORT
/* fields: next, name, segment, offset/value */
struct asym ReservedStack = { NULL,"@ReservedStack", 0 }; /* max stack space required by INVOKE */
#endif
/* tables for FASTCALL support */
/* v2.07: 16-bit MS FASTCALL registers are AX, DX, BX.
* And params on stack are in PASCAL order.
*/
//static const enum special_token ms32_regs16[] = { T_CX, T_DX };
static const enum special_token ms32_regs16[] = { T_AX, T_DX, T_BX };
static const enum special_token ms32_regs32[] = { T_ECX,T_EDX };
/* v2.07: added */
static const int ms32_maxreg[] = {
sizeof( ms32_regs16) / sizeof(ms32_regs16[0] ),
sizeof( ms32_regs32) / sizeof(ms32_regs32[0] ),
};
#if OWFC_SUPPORT
static const enum special_token watc_regs8[] = {T_AL, T_DL, T_BL, T_CL };
static const enum special_token watc_regs16[] = {T_AX, T_DX, T_BX, T_CX };
static const enum special_token watc_regs32[] = {T_EAX, T_EDX, T_EBX, T_ECX };
static const enum special_token watc_regs_qw[] = {T_AX, T_BX, T_CX, T_DX };
#endif
#if AMD64_SUPPORT
static const enum special_token ms64_regs[] = {T_RCX, T_RDX, T_R8, T_R9 };
/* win64 non-volatile GPRs:
* T_RBX, T_RBP, T_RSI, T_RDI, T_R12, T_R13, T_R14, T_R15
*/
static const uint_16 win64_nvgpr = 0xF0E8;
/* win64 non-volatile XMM regs: XMM6-XMM15 */
static const uint_16 win64_nvxmm = 0xFFC0;
#endif
struct fastcall_conv {
int (* paramcheck)( struct dsym *, struct dsym *, int * );
void (* handlereturn)( struct dsym *, char *buffer );
};
static int ms32_pcheck( struct dsym *, struct dsym *, int * );
static void ms32_return( struct dsym *, char * );
#if OWFC_SUPPORT
static int watc_pcheck( struct dsym *, struct dsym *, int * );
static void watc_return( struct dsym *, char * );
#endif
#if AMD64_SUPPORT
static int ms64_pcheck( struct dsym *, struct dsym *, int * );
static void ms64_return( struct dsym *, char * );
#endif
/* table of fastcall types.
* must match order of enum fastcall_type!
* also see table in mangle.c!
*/
static const struct fastcall_conv fastcall_tab[] = {
{ ms32_pcheck, ms32_return }, /* FCT_MSC */
#if OWFC_SUPPORT
{ watc_pcheck, watc_return }, /* FCT_WATCOMC */
#endif
#if AMD64_SUPPORT
{ ms64_pcheck, ms64_return } /* FCT_WIN64 */
#endif
};
static const enum special_token basereg[] = { T_BP, T_EBP,
#if AMD64_SUPPORT
T_RBP
#endif
};
static const enum special_token stackreg[] = { T_SP, T_ESP,
#if AMD64_SUPPORT
T_RSP
#endif
};
#define ROUND_UP( i, r ) (((i)+((r)-1)) & ~((r)-1))
#if OWFC_SUPPORT
/* register usage for OW fastcall (register calling convention).
* registers are used for parameter size 1,2,4,8.
* if a parameter doesn't fit in a register, a register pair is used.
* however, valid register pairs are e/dx:e/ax and e/cx:e/bx only!
* if a parameter doesn't fit in a register pair, registers
* are used ax:bx:cx:dx!!!
* stack cleanup for OW fastcall: if the proc is VARARG, the caller
* will do the cleanup, else the called proc does it.
* in VARARG procs, all parameters are pushed onto the stack!
*/
static int watc_pcheck( struct dsym *proc, struct dsym *paranode, int *used )
/***************************************************************************/
{
static char regname[64];
static char regist[32];
int newflg;
int shift;
int firstreg;
uint_8 Ofssize = GetSymOfssize( &proc->sym );
int size = SizeFromMemtype( paranode->sym.mem_type, paranode->sym.Ofssize, paranode->sym.type );
/* v2.05: VARARG procs don't have register params */
if ( proc->e.procinfo->is_vararg )
return( 0 );
if ( size != 1 && size != 2 && size != 4 && size != 8 )
return( 0 );
/* v2.05: rewritten. The old code didn't allow to "fill holes" */
if ( size == 8 ) {
newflg = Ofssize ? 3 : 15;
shift = Ofssize ? 2 : 4;
} else if ( size == 4 && Ofssize == USE16 ) {
newflg = 3;
shift = 2;
} else {
newflg = 1;
shift = 1;
}
/* scan if there's a free register (pair/quadrupel) */
for ( firstreg = 0; firstreg < 4 && (newflg & *used ); newflg <<= shift, firstreg += shift );
if ( firstreg >= 4 ) /* exit if nothing is free */
return( 0 );
paranode->sym.state = SYM_TMACRO;
switch ( size ) {
case 1:
GetResWName( watc_regs8[firstreg], regname );
break;
case 2:
GetResWName( watc_regs16[firstreg], regname );
break;
case 4:
if ( Ofssize ) {
GetResWName( watc_regs32[firstreg], regname );
} else {
sprintf( regname, "%s::%s",
GetResWName( watc_regs16[firstreg+1], regist ),
GetResWName( watc_regs16[firstreg], NULL ) );
}
break;
case 8:
if ( Ofssize ) {
sprintf( regname, "%s::%s",
GetResWName( watc_regs32[firstreg+1], regist ),
GetResWName( watc_regs32[firstreg], NULL ) );
} else {
/* the AX:BX:CX:DX sequence is for 16-bit only */
for( firstreg = 0, regname[0] = NULLC; firstreg < 4; firstreg++ ) {
GetResWName( watc_regs_qw[firstreg], regname + strlen( regname ) );
if ( firstreg != 3 )
strcat( regname, "::");
}
}
}
*used |= newflg;
paranode->sym.string_ptr = LclAlloc( strlen( regname ) + 1 );
strcpy( paranode->sym.string_ptr, regname );
DebugMsg(("watc_pcheck(%s.%s): size=%u ptr=%u far=%u reg=%s\n", proc->sym.name, paranode->sym.name, size, paranode->sym.is_ptr, paranode->sym.isfar, regname ));
return( 1 );
}
static void watc_return( struct dsym *proc, char *buffer )
/********************************************************/
{
int value;
value = 4 * CurrWordSize;
if( proc->e.procinfo->is_vararg == FALSE && proc->e.procinfo->parasize > value )
sprintf( buffer + strlen( buffer ), "%d%c", proc->e.procinfo->parasize - value, ModuleInfo.radix != 10 ? 't' : NULLC );
return;
}
#endif
/* the MS Win32 fastcall ABI is simple: register ecx and edx are used,
* if the parameter's value fits into the register.
* there is no space reserved on the stack for a register backup.
* The 16-bit ABI uses registers AX, DX and BX - additional registers
* are pushed in PASCAL order (i.o.w.: left to right).
*/
static int ms32_pcheck( struct dsym *proc, struct dsym *paranode, int *used )
/***************************************************************************/
{
char regname[32];
int size = SizeFromMemtype( paranode->sym.mem_type, paranode->sym.Ofssize, paranode->sym.type );
/* v2.07: 16-bit has 3 register params (AX,DX,BX) */
//if ( size > CurrWordSize || *used >= 2 )
if ( size > CurrWordSize || *used >= ms32_maxreg[ModuleInfo.Ofssize] )
return( 0 );
paranode->sym.state = SYM_TMACRO;
GetResWName( ModuleInfo.Ofssize ? ms32_regs32[*used] : ms32_regs16[*used], regname );
paranode->sym.string_ptr = LclAlloc( strlen( regname ) + 1 );
strcpy( paranode->sym.string_ptr, regname );
(*used)++;
return( 1 );
}
static void ms32_return( struct dsym *proc, char *buffer )
/********************************************************/
{
/* v2.07: changed */
//if( proc->e.procinfo->parasize > ( 2 * CurrWordSize ) )
// sprintf( buffer + strlen( buffer ), "%d%c", proc->e.procinfo->parasize - (2 * CurrWordSize), ModuleInfo.radix != 10 ? 't' : NULLC );
if( proc->e.procinfo->parasize > ( ms32_maxreg[ModuleInfo.Ofssize] * CurrWordSize ) )
sprintf( buffer + strlen( buffer ), "%d%c", proc->e.procinfo->parasize - ( ms32_maxreg[ModuleInfo.Ofssize] * CurrWordSize), ModuleInfo.radix != 10 ? 't' : NULLC );
return;
}
#if AMD64_SUPPORT
/* the MS Win64 fastcall ABI is strict: the first four parameters are
* passed in registers. If a parameter's value doesn't fit in a register,
* it's address is used instead. parameter 1 is stored in rcx/xmm0,
* then comes rdx/xmm1, r8/xmm2, r9/xmm3. The xmm regs are used if the
* param is a float/double (but not long double!).
* Additionally, there's space for the registers reserved by the caller on,
* the stack. On a function's entry it's located at [esp+8] for param 1,
* [esp+16] for param 2,... The parameter names refer to those stack
* locations, not to the register names.
*/
static int ms64_pcheck( struct dsym *proc, struct dsym *paranode, int *used )
/***************************************************************************/
{
/* since the parameter names refer the stack-backup locations,
* there's nothing to do here!
* That is, if a parameter's size is > 8, it has to be changed
* to a pointer. This is to be done yet.
*/
return( 0 );
}
static void ms64_return( struct dsym *proc, char *buffer )
/********************************************************/
{
/* nothing to do, the caller cleans the stack */
return;
}
#endif
static void pushitem( void *stk, void *elmt )
/*******************************************/
{
void **stack = stk;
struct qnode *node;
node = LclAlloc( sizeof( struct qnode ));
node->next = *stack;
node->elmt = elmt;
*stack = node;
}
static void *popitem( void *stk )
/*******************************/
{
void **stack = stk;
struct qnode *node;
void *elmt;
node = (struct qnode *)(*stack);
*stack = node->next;
elmt = (void *)node->elmt;
LclFree( node );
return( elmt );
}
#if 0
void *peekitem( void *stk, int level )
/************************************/
{
struct qnode *node = (struct qnode *)stk;
for ( ; node && level; level-- ) {
node = node->next;
}
if ( node )
return( node->elt );
else
return( NULL );
}
#endif
static void push_proc( struct dsym *proc )
/****************************************/
{
if ( Parse_Pass == PASS_1 ) /* get the locals stored so far */
SymGetLocal( (struct asym *)proc );
pushitem( &ProcStack, proc );
return;
}
static struct dsym *pop_proc( void )
/**********************************/
{
if( ProcStack == NULL )
return( NULL );
return( (struct dsym *)popitem( &ProcStack ) );
}
/* LOCAL directive. Called on Pass 1 only */
ret_code LocalDir( int i, struct asm_tok tokenarray[] )
/*****************************************************/
{
char *name;
struct dsym *local;
struct dsym *curr;
struct proc_info *info;
//int size;
//int idx;
#if AMD64_SUPPORT
int displ;
int cnt;
int sizestd;
int sizexmm;
#endif
struct qualified_type ti;
int align = CurrWordSize;
/*
LOCAL symbol[,symbol]...
symbol:name [[count]] [:[type]]
count: number of array elements, default is 1
type: Simple Type, structured type, ptr to simple/structured type
*/
if ( Parse_Pass != PASS_1 )
return( NOT_ERROR );
DebugMsg1(("LocalDir(%u) entry\n", i));
if( DefineProc == FALSE || CurrProc == NULL ) {
EmitError( PROC_MACRO_MUST_PRECEDE_LOCAL );
return( ERROR );
}
info = CurrProc->e.procinfo;
i++; /* go past LOCAL */
#if AMD64_SUPPORT
if ( info->isframe ) {
uint_16 *regs = info->regslist;
sizexmm = 0;
sizestd = 0;
/* adjust start displacement for Win64 FRAME procs.
* v2.06: the list may contain xmm registers, which have size 16!
*/
if ( regs )
for( cnt = *regs++; cnt; cnt--, regs++ )
if ( GetValueSp( *regs ) & OP_XMM )
sizexmm += 16;
else
sizestd += 8;
displ = sizexmm + sizestd;
/* v2.07: ( fix by habran )
* see below why this is to be done only when sizexmm is != 0
*/
if ( sizexmm && (sizestd & 0xf) )
displ += 8;
}
#endif
do {
if( tokenarray[i].token != T_ID ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
name = tokenarray[i].string_ptr;
DebugMsg1(("LocalDir(%s)\n", name ));
ti.symtype = NULL;
ti.is_ptr = 0;
ti.ptr_memtype = MT_EMPTY;
if ( SIZE_DATAPTR & ( 1 << ModuleInfo.model ) )
ti.is_far = TRUE;
else
ti.is_far = FALSE;
ti.Ofssize = ModuleInfo.Ofssize;
#if 0
/* since v1.95 a local hash table is used. No need to search the
* symbol before SymLCreate() is called. SymLCreate() will display
* an error if the symbol is already defined.
*/
if ((local = (struct dsym *)SymSearch( name )) && local->sym.state != SYM_UNDEFINED ) {
EmitErr( SYMBOL_ALREADY_DEFINED, name );
return( ERROR );
}
#endif
local = (struct dsym *)SymLCreate( name );
if( !local ) { /* if it failed, an error msg has been written already */
DebugMsg(("LocalDir: SymLCreate( %s ) failed\n", name ));
return( ERROR );
}
local->sym.state = SYM_STACK;
local->sym.isdefined = TRUE;
local->sym.total_length = 1; /* v2.04: added */
switch ( ti.Ofssize ) {
case USE16:
local->sym.mem_type = MT_WORD;
break;
#if AMD64_SUPPORT
/* v2.08: default type for locals in 64-bit is still DWORD (at least in Win64) */
//case USE64: local->sym.mem_type = MT_QWORD; break;
#endif
default:
local->sym.mem_type = MT_DWORD; break;
}
/* v2.08: default size for 64-bit is 4! */
//ti.size = align;
ti.size = ( ( ti.Ofssize == USE16 ) ? sizeof(uint_16) : sizeof(uint_32) );
i++; /* go past name */
/* get an optional index factor: local name[xx]:... */
if( tokenarray[i].token == T_OP_SQ_BRACKET ) {
int j;
struct expr opndx;
i++; /* go past '[' */
/* scan for comma or colon. this isn't really necessary,
* but will prevent the expression evaluator from emitting
* confusing error messages.
*/
for ( j = i; j < Token_Count; j++ )
if ( tokenarray[j].token == T_COMMA ||
tokenarray[j].token == T_COLON)
break;
if ( ERROR == EvalOperand( &i, tokenarray, j, &opndx, 0 ) )
return( ERROR );
if ( opndx.kind != EXPR_CONST ) {
EmitError( CONSTANT_EXPECTED );
opndx.value = 1;
}
// local->factor = tokenarray[i++].value;
/* zero is allowed as value! */
local->sym.total_length = opndx.value;
local->sym.isarray = TRUE;
if( tokenarray[i].token == T_CL_SQ_BRACKET ) {
i++; /* go past ']' */
} else {
EmitError( EXPECTED_CL_SQ_BRACKET );
}
}
/* get the optional type: local name[xx]:type */
if( tokenarray[i].token == T_COLON ) {
DebugMsg1(("LocalDir(%s): i=%u, token=%X\n", name, i, tokenarray[i].token ));
i++;
if ( GetQualifiedType( &i, tokenarray, &ti ) == ERROR )
return( ERROR );
local->sym.mem_type = ti.mem_type;
if ( ti.mem_type == MT_TYPE ) {
local->sym.type = ti.symtype;
} else {
local->sym.target_type = ti.symtype;
}
DebugMsg1(("LocalDir: memtype=%X, type=%s, size=%u (curr localsize=%X)\n",
local->sym.mem_type,
ti.symtype ? ti.symtype->name : "NULL",
ti.size, info->localsize ));
}
local->sym.is_ptr = ti.is_ptr;
local->sym.isfar = ti.is_far;
local->sym.Ofssize = ti.Ofssize;
local->sym.ptr_memtype = ti.ptr_memtype;
local->sym.total_size = ti.size * local->sym.total_length;
#if AMD64_SUPPORT
/* v2.07: add the alignment here! */
if ( info->isframe && ( info->localsize == 0 ) ) {
if ( sizexmm == 0 && ( displ & 0xf ) )
info->localsize = 8 - ( local->sym.total_size & 0x7 );
}
#endif
info->localsize += local->sym.total_size;
if ( ti.size > align )
info->localsize = ROUND_UP( info->localsize, align );
else if ( ti.size ) /* v2.04: skip if size == 0 */
info->localsize = ROUND_UP( info->localsize, ti.size );
DebugMsg1(("LocalDir(%s): aligned local total=%X\n", name, info->localsize));
#if AMD64_SUPPORT
if ( info->isframe )
local->sym.offset = - ( info->localsize + displ );
else
#endif
local->sym.offset = - info->localsize;
DebugMsg1(("LocalDir(%s): symbol offset=%d\n", name, local->sym.offset));
if( info->locallist == NULL ) {
info->locallist = local;
} else {
for( curr = info->locallist; curr->nextlocal ; curr = curr->nextlocal );
curr->nextlocal = local;
}
if ( tokenarray[i].token != T_FINAL )
if ( tokenarray[i].token == T_COMMA ) {
if ( (i + 1) < Token_Count )
i++;
} else {
EmitError( EXPECTING_COMMA );
return( ERROR );
}
} while ( i < Token_Count );
return( NOT_ERROR );
}
/* parse parameters of a PROC/PROTO
* i=token buffer index
*/
static ret_code ParseParams( int i, struct asm_tok tokenarray[], struct dsym *proc, bool IsPROC )
/***********************************************************************************************/
{
char *name;
struct asym *sym;
int cntParam;
int offset;
//int size;
int fcint = 0;
struct qualified_type ti;
bool is_vararg;
struct dsym *paranode;
struct dsym *paracurr;
/* parse PROC parms */
/* it's important to remember that params are stored in "push" order! */
if (proc->sym.langtype == LANG_C ||
proc->sym.langtype == LANG_SYSCALL ||
#if AMD64_SUPPORT
( proc->sym.langtype == LANG_FASTCALL && ModuleInfo.Ofssize != USE64 ) ||
#else
proc->sym.langtype == LANG_FASTCALL ||
#endif
proc->sym.langtype == LANG_STDCALL)
for (paracurr = proc->e.procinfo->paralist; paracurr && paracurr->nextparam; paracurr = paracurr->nextparam );
else
paracurr = proc->e.procinfo->paralist;
for( cntParam = 0 ; tokenarray[i].token != T_FINAL ; cntParam++ ) {
if ( tokenarray[i].token == T_ID ) {
name = tokenarray[i++].string_ptr;
} else if ( IsPROC == FALSE && tokenarray[i].token == T_COLON ) {
if ( paracurr )
name = paracurr->sym.name;
else
name = "";
} else {
/* PROC needs a parameter name, PROTO accepts <void> also */
DebugMsg(("ParseParams: name missing/invalid for parameter %u, i=%u\n", cntParam+1, i));
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
ti.symtype = NULL;
ti.is_ptr = 0;
ti.ptr_memtype = MT_EMPTY;
/* v2.02: init is_far depending on memory model */
//ti.is_far = FALSE;
if ( SIZE_DATAPTR & ( 1 << ModuleInfo.model ) )
ti.is_far = TRUE;
else
ti.is_far = FALSE;
ti.Ofssize = ModuleInfo.Ofssize;
ti.size = CurrWordSize;
is_vararg = FALSE;
/* read colon. It's optional for PROC.
* Masm also allows a missing colon for PROTO - if there's
* just one parameter. Probably a Masm bug.
* JWasm always require a colon for PROTO.
*/
if( tokenarray[i].token != T_COLON ) {
if ( IsPROC == FALSE ) {
EmitError( COLON_EXPECTED );
return( ERROR );
}
switch ( ti.Ofssize ) {
case USE16:
ti.mem_type = MT_WORD; break;
#if AMD64_SUPPORT
/* v2.08: default size for arguments is DWORD in 64-bit ( Win64 ) */
//case USE64: ti.mem_type = MT_QWORD; break;
#endif
default:
ti.mem_type = MT_DWORD; break;
}
} else {
i++;
if (( tokenarray[i].token == T_RES_ID ) && ( tokenarray[i].tokval == T_VARARG )) {
switch( proc->sym.langtype ) {
case LANG_NONE:
case LANG_BASIC:
case LANG_FORTRAN:
case LANG_PASCAL:
case LANG_STDCALL:
EmitError( VARARG_REQUIRES_C_CALLING_CONVENTION );
return( ERROR );
}
/* v2.05: added check */
if ( tokenarray[i+1].token != T_FINAL )
EmitError( VARARG_PARAMETER_MUST_BE_LAST );
else
is_vararg = TRUE;
ti.mem_type = MT_EMPTY;
ti.size = 0;
i++;
} else {
if ( GetQualifiedType( &i, tokenarray, &ti ) == ERROR )
return( ERROR );
}
}
/* check if parameter name is defined already */
if (( IsPROC ) && ( sym = SymSearch( name ) ) && sym->state != SYM_UNDEFINED ) {
DebugMsg(("ParseParams: %s defined already, state=%u, local=%u\n", sym->name, sym->state, sym->scoped ));
EmitErr( SYMBOL_REDEFINITION, name );
return( ERROR );
}
/* redefinition? */
if ( paracurr ) {
#if 0 /* was active till v2.04 */
int newsize = ti.size;
int oldsize;
/* check size only (so UINT <-> DWORD wont cause an error) */
if ( paracurr->sym.type )
oldsize = paracurr->sym.total_size;
else if ( paracurr->sym.mem_type == MT_EMPTY )
oldsize = 0;
else if ( paracurr->sym.mem_type == MT_PTR )
oldsize = SizeFromMemtype( paracurr->sym.isfar ? MT_FAR : MT_NEAR, paracurr->sym.Ofssize, NULL );
else
oldsize = SizeFromMemtype( paracurr->sym.mem_type, paracurr->sym.Ofssize, paracurr->sym.type );
if ( oldsize != newsize ) {
DebugMsg(("ParseParams: old memtype=%u, new memtype=%u\n", paracurr->sym.mem_type, ti.mem_type));
EmitErr( CONFLICTING_PARAMETER_DEFINITION, name );
//return( ERROR );
}
/* the parameter type used in PROC has highest priority! */
if ( IsPROC ) {
if ( ti.symtype ) {
paracurr->sym.type = ti.symtype;
paracurr->sym.mem_type = MT_TYPE;
} else
paracurr->sym.mem_type = ti.mem_type;
}
#else
struct asym *to;
struct asym *tn;
char oo;
char on;
for( tn = ti.symtype; tn && tn->type; tn = tn->type );
to = ( paracurr->sym.mem_type == MT_TYPE ) ? paracurr->sym.type : paracurr->sym.target_type;
for( ; to && to->type; to = to->type );
oo = ( paracurr->sym.Ofssize != USE_EMPTY ) ? paracurr->sym.Ofssize : ModuleInfo.Ofssize;
on = ( ti.Ofssize != USE_EMPTY ) ? ti.Ofssize : ModuleInfo.Ofssize;
if ( ti.mem_type != paracurr->sym.mem_type ||
( ti.mem_type == MT_TYPE && tn != to ) ||
( ti.mem_type == MT_PTR &&
( ti.is_far != paracurr->sym.isfar ||
on != oo ||
ti.ptr_memtype != paracurr->sym.ptr_memtype ||
tn != to ))) {
DebugMsg(("ParseParams: old-new memtype=%X-%X type=%X(%s)-%X(%s) far=%u-%u ind=%u-%u ofss=%d-%d pmt=%X-%X\n",
paracurr->sym.mem_type, ti.mem_type,
(paracurr->sym.mem_type == MT_TYPE) ? paracurr->sym.type : paracurr->sym.target_type,
(paracurr->sym.mem_type == MT_TYPE) ? paracurr->sym.type->name : paracurr->sym.target_type ? paracurr->sym.target_type->name : "",
ti.symtype, ti.symtype ? ti.symtype->name : "",
paracurr->sym.isfar, ti.is_far,
paracurr->sym.is_ptr, ti.is_ptr,
paracurr->sym.Ofssize, ti.Ofssize,
paracurr->sym.ptr_memtype, ti.ptr_memtype ));
EmitErr( CONFLICTING_PARAMETER_DEFINITION, name );
//return( ERROR );
}
#endif
if ( IsPROC ) {
DebugMsg(("ParseParams: calling SymAddLocal(%s, %s)\n", paracurr->sym.name, name ));
/* it has been checked already that the name isn't found - SymAddLocal() shouldn't fail */
SymAddLocal( ¶curr->sym, name );
}
/* set paracurr to next parameter */
if ( proc->sym.langtype == LANG_C ||
proc->sym.langtype == LANG_SYSCALL ||
#if AMD64_SUPPORT
( proc->sym.langtype == LANG_FASTCALL && ti.Ofssize != USE64 ) ||
#else
proc->sym.langtype == LANG_FASTCALL ||
#endif
proc->sym.langtype == LANG_STDCALL) {
struct dsym *l;
for (l = proc->e.procinfo->paralist;
l && ( l->nextparam != paracurr );
l = l->nextparam );
paracurr = l;
} else
paracurr = paracurr->nextparam;
} else if ( proc->e.procinfo->init == TRUE ) {
/* second definition has more parameters than first */
DebugMsg(("ParseParams: different param count\n"));
EmitErr( CONFLICTING_PARAMETER_DEFINITION, "" );
return( ERROR );
} else {
if ( IsPROC ) {
paranode = (struct dsym *)SymLCreate( name );
} else
paranode = (struct dsym *)SymAlloc( "" );/* for PROTO, no param name needed */
if( paranode == NULL ) { /* error msg has been displayed already */
DebugMsg(("ParseParams: SymLCreate(%s) failed\n", name ));
return( ERROR );
}
paranode->sym.isdefined = TRUE;
paranode->sym.mem_type = ti.mem_type;
if ( ti.mem_type == MT_TYPE ) {
paranode->sym.type = ti.symtype;
} else {
paranode->sym.target_type = ti.symtype;
}
/* v2.05: moved BEFORE fastcall_tab() */
paranode->sym.isfar = ti.is_far;
paranode->sym.Ofssize = ti.Ofssize;
paranode->sym.is_ptr = ti.is_ptr;
paranode->sym.ptr_memtype = ti.ptr_memtype;
paranode->sym.is_vararg = is_vararg;
if ( proc->sym.langtype == LANG_FASTCALL &&
fastcall_tab[ModuleInfo.fctype].paramcheck( proc, paranode, &fcint ) ) {
} else {
paranode->sym.state = SYM_STACK;
}
paranode->sym.total_length = 1; /* v2.04: added */
paranode->sym.total_size = ti.size;
if( paranode->sym.is_vararg == FALSE )
proc->e.procinfo->parasize += ROUND_UP( ti.size, CurrWordSize );
/* v2.05: the PROC's vararg flag has been set already */
//proc->e.procinfo->is_vararg |= paranode->sym.is_vararg;
/* Parameters usually are stored in "push" order.
* However, for Win64, it's better to store them
* the "natural" way from left to right, since the
* arguments aren't "pushed".
*/
switch( proc->sym.langtype ) {
case LANG_BASIC:
case LANG_FORTRAN:
case LANG_PASCAL:
left_to_right:
paranode->nextparam = NULL;
if( proc->e.procinfo->paralist == NULL ) {
proc->e.procinfo->paralist = paranode;
} else {
for( paracurr = proc->e.procinfo->paralist;; paracurr = paracurr->nextparam ) {
if( paracurr->nextparam == NULL ) {
break;
}
}
paracurr->nextparam = paranode;
paracurr = NULL;
}
break;
#if AMD64_SUPPORT
case LANG_FASTCALL:
if ( ti.Ofssize == USE64 )
goto left_to_right;
#endif
/* v2.07: MS fastcall 16-bit is PASCAL! */
if ( ti.Ofssize == USE16 && ModuleInfo.fctype == FCT_MSC )
goto left_to_right;
default:
paranode->nextparam = proc->e.procinfo->paralist;
proc->e.procinfo->paralist = paranode;
break;
}
}
if ( tokenarray[i].token != T_FINAL ) {
if( tokenarray[i].token != T_COMMA ) {
DebugMsg(("ParseParams: error, cntParam=%u, found %s\n", cntParam, tokenarray[i].tokpos ));
EmitError( EXPECTING_COMMA );
return( ERROR );
}
i++; /* go past comma */
}
} /* end for */
if ( proc->e.procinfo->init == TRUE ) {
if ( paracurr ) {
/* first definition has more parameters than second */
DebugMsg(("ParseParams: a param is left over, cntParam=%u\n", cntParam));
EmitErr( CONFLICTING_PARAMETER_DEFINITION, "" );
return( ERROR );
}
} else {
int curr;
/* calc starting offset for parameters,
* offset from [E]BP : return addr + old [E]BP
* NEAR: 2 * wordsize, FAR: 3 * wordsize
* +4 ; USE16 + NEAR
* +8 : USE32 + NEAR
* +16 : USE64 + NEAR
* +6 : USE16 + FAR
* +12 : USE32 + FAR
* +24 : USE64 + FAR
*/
//if( proc->e.procinfo->mem_type == MT_NEAR ) {
if( proc->sym.mem_type == MT_NEAR ) {
offset = 4 << ModuleInfo.Ofssize;
} else {
offset = 6 << ModuleInfo.Ofssize;
}
/* now calculate the (E)BP offsets */
#if AMD64_SUPPORT
if ( ModuleInfo.Ofssize == USE64 && proc->sym.langtype == LANG_FASTCALL ) {
for ( paranode = proc->e.procinfo->paralist; paranode ;paranode = paranode->nextparam )
if ( paranode->sym.state == SYM_TMACRO ) /* register param */
;
else {
paranode->sym.offset = offset;
proc->e.procinfo->stackparam = TRUE;
offset += ROUND_UP( paranode->sym.total_size, CurrWordSize );
}
} else
#endif
for ( ; cntParam; cntParam-- ) {
for ( curr = 1, paranode = proc->e.procinfo->paralist; curr < cntParam;paranode = paranode->nextparam, curr++ );
DebugMsg1(("ParseParams: parm=%s, ofs=%u, size=%d\n", paranode->sym.name, offset, paranode->sym.total_size));
if ( paranode->sym.state == SYM_TMACRO ) /* register param? */
;
else {
paranode->sym.offset = offset;
proc->e.procinfo->stackparam = TRUE;
offset += ROUND_UP( paranode->sym.total_size, CurrWordSize );
}
}
}
return ( NOT_ERROR );
}
/*
* create a PROC type
* i = start position of attributes
* strategy to set default value for "offset size" (16/32):
* 1. if current model is FLAT, use 32, else
* 2. use the current segment's attribute
* 3. if no segment is set, use cpu setting
*/
ret_code ExamineProc( int i, struct asm_tok tokenarray[], struct dsym *proc, bool IsPROC )
/****************************************************************************************/
{
char *token;
uint_16 *regist;
//int type;
enum lang_type langtype;
enum memtype newmemtype;
uint_8 newofssize;
#if FASTPASS
bool oldpublic = proc->sym.public;
#endif
/* set some default values */
proc->sym.isdefined = TRUE;
if ( IsPROC ) {
proc->e.procinfo->export = ModuleInfo.procs_export;
/* don't overwrite a PUBLIC directive for this symbol! */
if ( ModuleInfo.procs_private == FALSE )
proc->sym.public = TRUE;
/* write epilog code */
if ( Options.masm_compat_gencode ) {
/* v2.07: Masm uses LEAVE if
* - current code is 32-bit/64-bit or
* - cpu is .286 or .586+ */
//proc->e.procinfo->pe_type = ( ( ModuleInfo.curr_cpu & P_CPU_MASK ) >= P_286 );
proc->e.procinfo->pe_type = ( ModuleInfo.Ofssize > USE16 ||
( ModuleInfo.curr_cpu & P_CPU_MASK ) == P_286 ||
( ModuleInfo.curr_cpu & P_CPU_MASK ) >= P_586 ) ? 1 : 0;
} else {
/* use LEAVE for 286, 386 (and x64) */
proc->e.procinfo->pe_type = ( ( ModuleInfo.curr_cpu & P_CPU_MASK ) == P_286 ||
#if AMD64_SUPPORT
( ModuleInfo.curr_cpu & P_CPU_MASK ) == P_64 ||
#endif
( ModuleInfo.curr_cpu & P_CPU_MASK ) == P_386 ) ? 1 : 0;
}
}
#if MANGLERSUPP
/* OW name mangling */
if( tokenarray[i].token == T_STRING && IsPROC ) {
/* SetMangler() will ignore LANG_NONE */
SetMangler( &proc->sym, LANG_NONE, tokenarray[i].string_ptr );
i++;
}
#endif
/* 1. attribute is <distance> */
if ( tokenarray[i].token == T_STYPE &&
tokenarray[i].tokval >= T_NEAR && tokenarray[i].tokval <= T_FAR32 ) {
uint_8 Ofssize = GetSflagsSp( tokenarray[i].tokval );
/* v2.06: SimpleType is obsolete */
/* v2.05: FindStdType() is obsolete */