-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathpreproc.c
1238 lines (1133 loc) · 35.2 KB
/
preproc.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: preprocessor
*
****************************************************************************/
#include <ctype.h>
#include <stdarg.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "condasm.h"
#include "tokenize.h"
#include "equate.h"
#include "macro.h"
#include "input.h"
#include "fastpass.h"
#include "listing.h"
#include "proc.h"
#include "expreval.h"
#include "assume.h"
#define REMOVECOMENT 0 /* 1=remove comments from source */
extern ret_code (* const directive_tab[])( int, struct asm_tok[] );
#ifdef DEBUG_OUT
int_32 cntppl0; /* count preprocessed lines 1 */
int_32 cntppl1; /* count preprocessed lines 2 */
int_32 cntppl2; /* count lines NOT handled by preprocessor */
#endif
/* preprocessor directive or macro procedure is preceded
* by a code label.
*/
ret_code WriteCodeLabel( char *line, struct asm_tok tokenarray[] )
/****************************************************************/
{
int oldcnt;
int oldtoken;
char oldchar;
if ( tokenarray[0].token != T_ID ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[0].string_ptr ) );
}
/* ensure the listing is written with the FULL source line */
if ( CurrFile[LST] ) LstWrite( LSTTYPE_LABEL, 0, NULL );
/* v2.04: call ParseLine() to parse the "label" part of the line */
oldcnt = Token_Count;
oldtoken = tokenarray[2].token;
oldchar = *tokenarray[2].tokpos;
Token_Count = 2;
tokenarray[2].token = T_FINAL;
*tokenarray[2].tokpos = NULLC;
ParseLine( tokenarray );
if ( Options.preprocessor_stdout == TRUE )
WritePreprocessedLine( line );
Token_Count = oldcnt;
tokenarray[2].token = oldtoken;
*tokenarray[2].tokpos = oldchar;
return( NOT_ERROR );
}
/* Verify a matched pair of function call brackets, assuming initial opening bracket position
and return the closing bracket position or -1.
*/
int VerifyBrackets(struct asm_tok tokenarray[], int openIdx, bool inParam)
{
int len;
int i = openIdx;
int opCnt = 0;
if (tokenarray[i].token != T_OP_BRACKET)
{
EmitErr(MISSING_LEFT_PARENTHESIS_IN_EXPRESSION);
return(-1);
}
if (!inParam)
{
for (i = 0;i < openIdx;i++)
{
if (tokenarray[i].token == T_OP_BRACKET || tokenarray[i].token == '(')
opCnt++;
}
}
if (tokenarray[i + 1].token == T_CL_BRACKET)
return(i + 1);
for (i = Token_Count-1;i > openIdx; i--)
{
if (tokenarray[i].token == T_CL_BRACKET && opCnt == 0)
return(i);
else if (tokenarray[i].token == T_CL_BRACKET)
opCnt--;
}
EmitErr(MISSING_RIGHT_PARENTHESIS_IN_EXPRESSION);
return(-1);
}
/* We only allow a single level of nested calls */
static void VerifyNesting(char *line, bool exprBracket)
{
int depth = 0;
int maxdepth = (exprBracket) ? 3 : 2;
char *p = line;
// Reduce allowed nesting for system-v calls as arginvoke doesn't support it yet.
if ((Options.output_format == OFORMAT_ELF || Options.output_format == OFORMAT_MAC) && Options.sub_format == SFORMAT_64BIT)
maxdepth = (exprBracket) ? 2 : 1;
// Same for 32bit code for now..
if (Options.sub_format != SFORMAT_64BIT)
maxdepth = (exprBracket) ? 2 : 1;
while (*p)
{
if (*p == '(')
depth++;
if (*p == ')')
depth--;
if (depth > maxdepth)
{
EmitErr(MAX_C_NESTING);
return;
}
p++;
}
}
static void ExpandObjCalls(char *line, struct asm_tok tokenarray[])
{
int i,j;
struct dsym *sym;
struct dsym *type = NULL;
struct dsym *tsym;
struct dsym *param;
bool foundType = FALSE;
bool foundProc = FALSE;
int derefCount = 0;
int opIdx = 0;
int clIdx = 0;
int clSqIdx = 0;
int opSqIdx = 0;
char newline[MAX_LINE_LEN];
bool gotOpen = FALSE;
bool gotClose = FALSE;
bool gotCloseSqr = FALSE;
bool gotOpenSqr = FALSE;
char methodName[MAX_LINE_LEN];
char indirectAddr[MAX_LINE_LEN];
char *pMethodStr = methodName;
char *pStr = newline;
char *pType = NULL;
bool inExpr = FALSE;
bool hasExprBracket = FALSE;
bool inParam = FALSE;
bool inProc = FALSE;
int firstDeRefIdx = 0;
int paramCount = 0;
char pcs[16];
bool didExpand = TRUE;
char refStr[MAX_LINE_LEN];
char *pRefStr = refStr;
struct sfield *field = NULL;
strcpy(newline, line);
// Scan through tokens, looking for pointer operators.
while (didExpand)
{
memset(&indirectAddr, 0, MAX_LINE_LEN);
memset(&methodName, 0, MAX_LINE_LEN);
memset(&newline, 0, MAX_LINE_LEN);
memset(&refStr, 0, MAX_LINE_LEN);
pStr = newline;
pRefStr = refStr;
didExpand = FALSE;
paramCount = 0;
const int size_refStr = strlen(refStr);
for (i = 0; i < Token_Count; i++)
{
if (tokenarray[i].token == T_POINTER)
{
/* Scan backwards to check if we're in an HLL expression or call parameter */
if (i > 0)
{
for (j = i - 1; j >= 0; j--)
{
tsym = SymCheck(tokenarray[j].string_ptr);
if (tokenarray[j].token == T_DIRECTIVE && (tokenarray[j].dirtype == DRT_HLLSTART || tokenarray[j].dirtype == DRT_HLLEND))
{
inExpr = TRUE;
if (tokenarray[j + 1].token == T_OP_BRACKET || tokenarray[j + 1].tokval == '(')
hasExprBracket = TRUE;
else
hasExprBracket = FALSE;
break;
}
else if ((tokenarray[j].token == T_DIRECTIVE && tokenarray[j].dirtype == DRT_INVOKE) ||
strcmp(tokenarray[j].string_ptr, "arginvoke") == 0 ||
strcmp(tokenarray[j].string_ptr, "_INVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_I") == 0 ||
strcmp(tokenarray[j].string_ptr, "_VINVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_V") == 0 ||
strcmp(tokenarray[j].string_ptr, "_SINVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_DEREF") == 0 ||
strcmp(tokenarray[j].string_ptr, "_DEREFI") == 0 ||
strcmp(tokenarray[j].string_ptr, "uinvoke") == 0)
{
inParam = TRUE;
break;
}
else if (tokenarray[j].token == T_ID && tsym && tsym->sym.isproc)
{
inParam = TRUE;
inProc = TRUE;
break;
}
}
}
if(derefCount == 0)
foundType = FALSE;
foundProc = FALSE;
// The item to the left of the pointer must be an ID with type or register or register indirect.
if (i == 0 && tokenarray[i - 1].token != T_ID && tokenarray[i - 1].token != T_REG && tokenarray[i - 1].token != T_CL_SQ_BRACKET)
{
EmitError(INVALID_POINTER);
}
/* Variable object reference */
else if (tokenarray[i - 1].token == T_ID)
{
if (derefCount == 0)
{
sym = SymCheck(tokenarray[i - 1].string_ptr);
if (sym && sym->sym.target_type && sym->sym.target_type > 0x200000 && sym->sym.target_type->isClass)
{
foundType = TRUE;
pType = tokenarray[i - 1].string_ptr;
type = sym->sym.target_type;
firstDeRefIdx = i - 2; /* pointer->item */
}
else if (sym && sym->sym.type && sym->sym.type->target_type && sym->sym.type->target_type > 0x200000 && sym->sym.type->target_type->isClass)
{
foundType = TRUE;
pType = tokenarray[i - 1].string_ptr;
type = sym->sym.type->target_type;
firstDeRefIdx = i - 2; /* pointer->item */
}
else if (sym && sym->sym.type && sym->sym.type->target_type && sym->sym.type->target_type > 0x200000 && sym->sym.type->target_type->isPtrTable)
{
foundType = TRUE;
pType = tokenarray[i - 1].string_ptr;
type = sym->sym.type->target_type;
firstDeRefIdx = 0; /* pointer->item */
}
/* Indirect register using .TYPE */
else if (sym && (sym->sym.isClass || sym->sym.isPtrTable))
{
gotCloseSqr = TRUE;
gotOpenSqr = FALSE;
clSqIdx = i - 3;
// Scan back to find opening square bracket.
for (j = i - 1; j >= 0; j--)
{
if (tokenarray[j].token == T_OP_SQ_BRACKET)
{
opSqIdx = j;
gotOpenSqr = TRUE;
break;
}
}
if (!gotOpenSqr || !gotCloseSqr)
EmitError(INVALID_POINTER);
// The tokens between opSqIdx and clSqIdx make up the indirect address.
// -> the first register(base) must be assumed to an object pointer.
if (sym && (sym->sym.isClass || sym->sym.isPtrTable))
{
foundType = TRUE;
pType = indirectAddr;
pType = strcpy(pType, "[") + 1;
for (j = opSqIdx + 1; j < clSqIdx; j++)
{
strcpy(pType, tokenarray[j].string_ptr);
pType += strlen(tokenarray[j].string_ptr);
}
pType = strcpy(pType, "]") + 1;
pType = indirectAddr;
type = (struct dsym *)sym;
firstDeRefIdx = opSqIdx - 1; /* pointer->item */
}
else
EmitError(INVALID_POINTER);
}
}
else
{
// Ensure tokenarray[i - 1].string_ptr is a field of the current TYPE, then get it's target type.
bool gotField = FALSE;
field = type->e.structinfo->head;
for (; field; field = field->next)
{
if (_stricmp(field->sym.name, tokenarray[i - 1].string_ptr) == 0)
{
gotField = TRUE;
break;
}
}
if (gotField)
{
type = field->sym.target_type;
if (!type || type < 0x10)
type = field->sym.type->target_type;
if (!type || type < 0x10)
EmitError(INVALID_POINTER);
}
else
EmitError(INVALID_POINTER);
// Append the item to the pType.
strcpy(pType + strlen(pType), ".");
strcpy(pType + strlen(pType), tokenarray[i - 1].string_ptr);
}
if (!foundType)
EmitError(INVALID_POINTER);
}
/* Direct register object reference */
else if (tokenarray[i - 1].token == T_REG)
{
sym = StdAssumeTable[GetRegNo(tokenarray[i - 1].tokval)].symbol;
if (sym && sym->sym.target_type)
{
foundType = TRUE;
pType = tokenarray[i - 1].string_ptr;
type = sym->sym.target_type;
firstDeRefIdx = i - 2; /* pointer->item */
}
else
EmitError(INVALID_POINTER);
}
/* Indirect memory address type object reference */
else if (tokenarray[i - 1].token == T_CL_SQ_BRACKET)
{
gotCloseSqr = TRUE;
gotOpenSqr = FALSE;
clSqIdx = i - 1;
// Scan back to find opening square bracket.
for (j = i - 1; j >= 0; j--)
{
if (tokenarray[j].token == T_OP_SQ_BRACKET)
{
opSqIdx = j;
gotOpenSqr = TRUE;
break;
}
}
if (!gotOpenSqr || !gotCloseSqr)
EmitError(INVALID_POINTER);
// The tokens between opSqIdx and clSqIdx make up the indirect address.
// -> the first register(base) must be assumed to an object pointer.
sym = StdAssumeTable[GetRegNo(tokenarray[opSqIdx + 1].tokval)].symbol;
if (sym && sym->sym.target_type)
{
foundType = TRUE;
pType = &indirectAddr;
pType = strcpy(pType, "[") + 1;
for (j = opSqIdx + 1; j < clSqIdx; j++)
{
strcpy(pType, tokenarray[j].string_ptr);
pType += strlen(tokenarray[j].string_ptr);
}
pType = strcpy(pType, "]") + 1;
pType = &indirectAddr;
type = sym->sym.target_type;
firstDeRefIdx = opSqIdx-1; /* pointer->item */
}
else
EmitError(INVALID_POINTER);
}
// The right of the pointer must be an ID (either a method(proc) or another type).
if (!type || tokenarray[i + 1].token != T_ID)
EmitError(INVALID_POINTER);
else
{
pMethodStr = &methodName;
pMethodStr = strcpy(pMethodStr, "_") + 1;
strcpy(pMethodStr, type->sym.name);
pMethodStr += strlen(type->sym.name);
pMethodStr = strcpy(pMethodStr, "_") + 1;
strcpy(pMethodStr, tokenarray[i + 1].string_ptr);
pMethodStr += strlen(tokenarray[i + 1].string_ptr);
pMethodStr = &methodName;
sym = SymCheck(pMethodStr);
if (sym && sym->sym.isproc)
{
foundProc = TRUE;
pMethodStr = tokenarray[i + 1].string_ptr;
for (param = sym->e.procinfo->paralist; param; param = param->nextparam)
{
paramCount++;
}
}
else if (type && type->sym.isClass)
{
foundProc = FALSE;
if (derefCount == 0)
{
strcpy(pRefStr, pType);
pRefStr += strlen(pType);
}
else
{
pRefStr = strcpy(pRefStr, "rcx") + 3;
}
pRefStr = strcpy(pRefStr, ",") + 1;
strcpy(pRefStr, type->sym.name);
pRefStr += strlen(type->sym.name);
strcpy(pType, type->sym.name); // Reset pType to the type name.
}
else
EmitError(INVALID_POINTER);
}
// Find the proc open/close brackets.
if (foundProc)
{
int openCount = 1;
for (j = i; j < Token_Count; j++)
{
if (tokenarray[j].token == T_OP_BRACKET || tokenarray[j].tokval == '(')
{
opIdx = j;
gotOpen = TRUE;
break;
}
}
for (j = opIdx + 1; j < Token_Count; j++)
{
if (tokenarray[j].token == T_CL_BRACKET || tokenarray[j].tokval == ')')
openCount--;
if (tokenarray[j].token == T_OP_BRACKET || tokenarray[j].tokval == '(')
openCount++;
if (openCount == 0)
{
clIdx = j;
gotClose = TRUE;
break;
}
}
if (!gotOpen)
EmitError(MISSING_LEFT_PARENTHESIS_IN_EXPRESSION);
if (!gotClose)
EmitError(MISSING_RIGHT_PARENTHESIS_IN_EXPRESSION);
}
if (foundProc)
{
if (!type->sym.isPtrTable)
{
for (j = 0; j <= firstDeRefIdx; j++)
{
pStr = strcpy(pStr, " ") + 1;
strcpy(pStr, tokenarray[j].string_ptr);
pStr += strlen(tokenarray[j].string_ptr);
pStr = strcpy(pStr, " ") + 1;
}
}
if (inExpr || inParam)
pStr = strcpy(pStr, "_DEREFI(") + 8;
else if (!type->sym.isPtrTable)
pStr = strcpy(pStr, "_DEREF ") + 7;
else if (type->sym.isPtrTable)
{
if (tokenarray[i - 3].token == T_CL_SQ_BRACKET)
{
pStr = strcpy(pStr, "_DEREFRR ") + 9;
strcpy(pStr, pType);
pStr += strlen(pType);
pStr = strcpy(pStr, ",") + 1;
}
else
{
pStr = strcpy(pStr, "_DEREFR ") + 8;
strcpy(pStr, tokenarray[i - 1].string_ptr);
pStr += strlen(tokenarray[i - 1].string_ptr);
pStr = strcpy(pStr, ",") + 1;
}
}
strcpy(pStr, type->sym.name);
pStr += strlen(type->sym.name);
pStr = strcpy(pStr, ",") + 1;
strcpy(pStr, pMethodStr);
pStr += strlen(pMethodStr);
pStr = strcpy(pStr, ",") + 1;
if(type->sym.isPtrTable)
sprintf(pcs, "%d", paramCount); // no -1, these are raw functions
else
sprintf(pcs, "%d", paramCount - 1); //-1 due to thisPtr implicit
strcpy(pStr, pcs);
pStr += strlen(pcs);
pStr = strcpy(pStr, ",") + 1;
if ((paramCount - 1) == 0 && !type->sym.isPtrTable) // For no argument case, put a dummy 0 in to be filtered out by deref
{
pStr = strcpy(pStr, "0") + 1;
}
else if(!type->sym.isPtrTable)
pStr = strcpy(pStr, "0,") + 2;
for (j = opIdx + 1; j < clIdx; j++)
{
if (*tokenarray[j].string_ptr == '&')
pStr = strcpy(pStr, " ADDR ") + 6;
else
{
strcpy(pStr, tokenarray[j].string_ptr);
pStr += strlen(tokenarray[j].string_ptr);
if (strncmp(tokenarray[j].string_ptr, "ADDR",4) == 0)
pStr = strcpy(pStr, " ") + 1;
}
}
if(!type->sym.isPtrTable)
pStr = strcpy(pStr, ",") + 1;
if (derefCount > 0)
{
strcpy(pStr, refStr);
pStr += size_refStr;
pStr = strcpy(pStr, ",") + 1;
}
if (derefCount == 0 && !type->sym.isPtrTable)
{
strcpy(pStr, pType);
pStr += strlen(pType);
pStr = strcpy(pStr, ",") + 1;
strcpy(pStr, type->sym.name);
pStr += strlen(type->sym.name);
}
else if(!type->sym.isPtrTable)
{
pStr = strcpy(pStr, "rcx,") + 4;
strcpy(pStr, pType);
pStr += strlen(pType);
}
if (inExpr || inParam)
pStr = strcpy(pStr, ")") + 1;
for (j = clIdx + 1; j < Token_Count; j++)
{
pStr = strcpy(pStr, " ") + 1;
strcpy(pStr, tokenarray[j].string_ptr);
pStr += strlen(tokenarray[j].string_ptr);
pStr = strcpy(pStr, " ") + 1;
}
didExpand = TRUE;
break;
}
derefCount++;
}
}
/* Transfer new source line back for token rescan */
if (didExpand)
{
strcpy(line, &newline);
Token_Count = Tokenize(line, 0, tokenarray, TOK_RESCAN);
}
}
}
/* Expand static object type method invocations */
static void ExpandStaticObjCalls(char *line, struct asm_tok tokenarray[])
{
int i, j;
struct asym *sym = NULL;
int opIdx, clIdx;
int method;
int type;
char newline[MAX_LINE_LEN];
char *pStr;
int opCount = 0;
bool hasParams = FALSE;
bool inParam = FALSE;
bool inExpr = FALSE;
bool inProc = FALSE;
bool hasExprBracket = FALSE;
struct dsym *tsym = NULL;
memset(&newline, 0, MAX_LINE_LEN);
pStr = newline;
for (i = 0; i < Token_Count; i++)
{
if (tokenarray[i].token == T_ID)
{
sym = SymSearch(tokenarray[i].string_ptr);
if (sym && sym->isClass)
{
if (tokenarray[i + 1].token == T_DOT && tokenarray[i + 2].token == T_ID && tokenarray[i + 3].token == T_OP_BRACKET)
{
/* Scan backwards to check if we're in an HLL expression or call parameter */
if (i > 0)
{
for (j = i - 1; j >= 0; j--)
{
tsym = SymCheck(tokenarray[j].string_ptr);
if (tokenarray[j].token == T_DIRECTIVE && (tokenarray[j].dirtype == DRT_HLLSTART || tokenarray[j].dirtype == DRT_HLLEND))
{
inExpr = TRUE;
if (tokenarray[j + 1].token == T_OP_BRACKET || tokenarray[j + 1].tokval == '(')
hasExprBracket = TRUE;
else
hasExprBracket = FALSE;
break;
}
else if ((tokenarray[j].token == T_DIRECTIVE && tokenarray[j].dirtype == DRT_INVOKE) ||
strcmp(tokenarray[j].string_ptr, "arginvoke") == 0 ||
strcmp(tokenarray[j].string_ptr, "_INVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_I") == 0 ||
strcmp(tokenarray[j].string_ptr, "_VINVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_V") == 0 ||
strcmp(tokenarray[j].string_ptr, "_SINVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_DEREF") == 0 ||
strcmp(tokenarray[j].string_ptr, "_DEREFI") == 0 ||
strcmp(tokenarray[j].string_ptr, "uinvoke") == 0)
{
inParam = TRUE;
break;
}
else if (tokenarray[j].token == T_ID && tsym && tsym->sym.isproc)
{
inParam = TRUE;
inProc = TRUE;
break;
}
}
}
// Allow expansion in an instruction.
if (tokenarray[i - 1].token == T_COMMA)
inExpr = TRUE;
// Allow expansion in a memory address [].
if (i > 0)
{
for (j = i - 1; j >= 0; j--)
{
if (tokenarray[j].token == T_OP_SQ_BRACKET)
{
inExpr = TRUE;
break;
}
}
}
opIdx = i + 3;
method = i + 2;
type = i;
// Find closing bracket.
opCount = 1;
for (j = opIdx + 1; j < Token_Count; j++)
{
if (tokenarray[j].token == T_OP_BRACKET)
opCount++;
else if (tokenarray[j].token == T_CL_BRACKET)
opCount--;
if (opCount == 0)
{
clIdx = j;
break;
}
}
// Are there parameters?
if (clIdx > opIdx + 1)
hasParams = TRUE;
// **** Build new line. ****
// Copy any pre-tokens
for (j = 0; j < i; j++)
{
pStr = strcpy(pStr, tokenarray[j].string_ptr);
pStr += strlen(tokenarray[j].string_ptr);
pStr = strcpy(pStr, " ") + 1;
}
if(inProc || inParam || inExpr)
pStr = strcpy(pStr, "_STATIC(") + 8;
else
pStr = strcpy(pStr, "_SINVOKE ") + 9;
strcpy(pStr, tokenarray[type].string_ptr);
pStr += strlen(tokenarray[type].string_ptr);
pStr = strcpy(pStr, ",") + 1;
strcpy(pStr, tokenarray[method].string_ptr);
pStr += strlen(tokenarray[method].string_ptr);
if (hasParams)
{
pStr = strcpy(pStr, ",") + 1;
for (j = opIdx + 1; j < clIdx; j++)
{
if (*tokenarray[j].string_ptr == '&')
pStr = strcpy(pStr, " ADDR ") + 6;
else
{
pStr = strcpy(pStr, tokenarray[j].string_ptr);
pStr += strlen(tokenarray[j].string_ptr);
}
//if( j < clIdx-1 )
// pStr = strcpy(pStr, ",") + 1;
}
}
if (inProc || inParam || inExpr)
pStr = strcpy(pStr, ")") + 1;
// If there are more tokens after the closing bracket, add them.
if (clIdx < Token_Count-1)
{
for (i = clIdx+1; i < Token_Count; i++)
{
pStr = strcpy(pStr, tokenarray[i].string_ptr);
pStr += strlen(tokenarray[i].string_ptr);
pStr = strcpy(pStr, " ") + 1;
}
}
strcpy(line, newline);
Token_Count = Tokenize(line, 0, tokenarray, TOK_RESCAN);
}
}
}
}
}
static struct asym * TraverseEquate(struct asym *sym)
{
struct asym *resultSym = sym;
if (sym)
{
while (resultSym->state == SYM_TMACRO && resultSym->string_ptr != NULL)
{
resultSym = SymLookup(resultSym->string_ptr);
}
}
return(resultSym);
}
static void ExpandHllCalls(char *line, struct asm_tok tokenarray[], bool inParam, int argIdx, bool inExpr)
{
int i, j;
struct dsym *sym;
char newline[MAX_LINE_LEN];
int clIdx, opIdx;
int tokenCount;
struct asm_tok *tokenarray2;
char *p = &newline;
char idxStack[] = { 0, 0, 0, 0 };
int stackPt = -1;
char idxline[MAX_LINE_LEN];
char invCnt = 1;
bool hasExprBracket = FALSE;
bool expandedCall = FALSE;
char uCnt = 0;
strcpy(&newline, line);
memset(&idxline, 0, MAX_LINE_LEN);
for (i = 0;i < Token_Count;i++)
{
if (tokenarray[i].token == T_ID)
{
sym = SymCheck(tokenarray[i].string_ptr);
sym = TraverseEquate(sym); /* We may have an equate chain that points to a proc, as we expand here before macro substitution we need to consider this */
if(sym && (sym->sym.isproc || (sym->sym.isfunc && sym->sym.state == SYM_EXTERNAL)) && tokenarray[i+1].tokval != T_PROC && tokenarray[i+1].tokval != T_PROTO &&
tokenarray[i+1].tokval != T_ENDP && tokenarray[i+1].tokval != T_EQU && tokenarray[i+1].token == T_OP_BRACKET)
{
/* Scan backwards to check if we're in an HLL expression or call parameter */
if (i > 0)
{
for (j = i - 1;j >= 0;j--)
{
if (tokenarray[j].token == T_DIRECTIVE && (tokenarray[j].dirtype == DRT_HLLSTART || tokenarray[j].dirtype == DRT_HLLEND))
{
inExpr = TRUE;
if (tokenarray[j + 1].token == T_OP_BRACKET || tokenarray[j + 1].tokval == '(')
hasExprBracket = TRUE;
else
hasExprBracket = FALSE;
break;
}
else if ((tokenarray[j].token == T_DIRECTIVE && tokenarray[j].dirtype == DRT_INVOKE) ||
strcmp(tokenarray[j].string_ptr, "arginvoke") == 0 ||
strcmp(tokenarray[j].string_ptr, "_INVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_I") == 0 ||
strcmp(tokenarray[j].string_ptr, "_VINVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_SINVOKE") == 0 ||
strcmp(tokenarray[j].string_ptr, "_V") == 0 ||
strcmp(tokenarray[j].string_ptr, "_DEREF") == 0 ||
strcmp(tokenarray[j].string_ptr, "_DEREFI") == 0 ||
strcmp(tokenarray[j].string_ptr, "uinvoke") == 0)
{
inParam = TRUE;
break;
}
}
}
/* If we've identifed a Proc Name, there are several more cases where it must not be expanded */
if (i > 0 && (tokenarray[i - 1].token == T_COLON || tokenarray[i - 1].tokval == T_EQU || //(tokenarray[i - 1].token == T_COMMA && !inParam) ||
tokenarray[i - 1].tokval == T_INVOKE || tokenarray[i - 1].token == T_INSTRUCTION || tokenarray[i - 1].tokval == T_ADDR ||
tokenarray[i - 1].tokval == T_OFFSET || tokenarray[i - 1].tokval == T_PTR || tokenarray[i - 1].tokval == T_END ||
(tokenarray[i - 1].token == T_DIRECTIVE && tokenarray[i - 1].dirtype == DRT_DATADIR) || tokenarray[i - 1].token == T_UNARY_OPERATOR || tokenarray[i - 1].tokval == T_PROC ||
strcmp(tokenarray[i - 1].string_ptr,"arginvoke") == 0 || strcmp(tokenarray[i - 1].string_ptr, "@what") == 0)) continue;
// Allow expansion in an instruction.
if (tokenarray[i - 1].token == T_COMMA)
inExpr = TRUE;
// Allow expansion in a memory address [].
if (i > 0)
{
for (j = i - 1;j >= 0;j--)
{
if (tokenarray[j].token == T_OP_SQ_BRACKET)
{
inExpr = TRUE;
break;
}
}
}
/* Verify c-style procedure call has matching brackets */
opIdx = i + 1;
clIdx = VerifyBrackets(tokenarray, opIdx, inParam);
if (clIdx == -1)
return;
expandedCall = TRUE;
/* scan all tokens between opIdx and clIdx and replace & operator with ADDR */
for (j = opIdx; j < clIdx; j++)
{
if (*(tokenarray[j].string_ptr) == '&')
{
// token identifier begins with address of operator.
//strcpy(tokenarray[j].string_ptr, "ADDR ");
tokenarray[j].string_ptr = "ADDR ";
}
}
if (!inParam && !inExpr)
{
/* Shift all the tokens up to remove the close bracket and make space for invoke */
for (j = clIdx; j > i; j--)
tokenarray[j] = tokenarray[j - 1];
if (clIdx > opIdx + 1)
{
tokenarray[clIdx + 1].token = T_FINAL;
tokenarray[opIdx + 1].string_ptr = ",";
tokenarray[opIdx + 1].token = T_COMMA;
}
/* Proc with no params */
else
{
tokenarray[opIdx + 1].string_ptr = "";
tokenarray[opIdx + 1].token = T_FINAL;
}
tokenarray[i].token = T_DIRECTIVE;
tokenarray[i].tokval = T_INVOKE;
tokenarray[i].dirtype = DRT_INVOKE;
tokenarray[i].string_ptr = "invoke";
}
else
{
/* Shift all the tokens up to remove the close bracket and make space for invoke */
for (j = Token_Count; j > i; j--)
tokenarray[j] = tokenarray[j - 1];
/* Shift all the tokens up to add new open bracket */
for (j = Token_Count+1; j > i; j--)
tokenarray[j] = tokenarray[j - 1];
Token_Count+=2;
tokenarray[Token_Count].string_ptr = "";
tokenarray[Token_Count].token = T_FINAL;
if (clIdx > opIdx + 1)
{
tokenarray[opIdx + 2].string_ptr = ",";
tokenarray[opIdx + 2].token = T_COMMA;
}
else
{
tokenarray[opIdx + 2].string_ptr = " ";
}
tokenarray[i].token = T_ID;
tokenarray[i].tokval = 0;
tokenarray[i].dirtype = 0;
if (inExpr && !inParam)
{
tokenarray[i].string_ptr = "uinvoke";
tokenarray[i + 1].string_ptr = "(";
tokenarray[i + 1].token = '(';
uCnt++; // Increment count of uinvokes, as we only allow 1 per expression.
if (uCnt > 1)
{
EmitErr(MAX_C_CALLS);
}
}
else if (inParam)
{
tokenarray[i].string_ptr = "arginvoke(%%,%%,";
tokenarray[i + 1].string_ptr = "";
tokenarray[i + 1].token = 0;
}
}
i += 2;
/* Rebuild string */
for (j = 0;j <= Token_Count; j++)
{
if (tokenarray[j].tokval == T_PTR)
{
// Ensure we put a space between type and PTR UASM 2.50
strcpy(p, " ");
p++;
}
strcpy(p, tokenarray[j].string_ptr);
tokenarray[j].tokpos = p;
p += strlen(tokenarray[j].string_ptr);
if (j == 0 || tokenarray[j].tokval == T_ADDR || tokenarray[j].tokval == T_OFFSET)
{
strcpy(p, " ");
p++;
}
}
/* Reset string pointer*/
p = &newline;
}
}
}
/* Scan string and insert argument numbers */
// MyProc(MyProc3(), 20) MyProc(10, MyProc4())
// invoke MyProc,arginvoke(0,MyProc3),20
// invoke MyProc,10,arginvoke(0,MyProc4)
//find invoke, increment stackPt and set argno 1... for every comma not in () increment argno
//find uinvoke or arginvoke increment stackPt and set argno 1... for every comma after opIdx and before closeIdx and not in () increment argno... at closeIdx decrement StackPt
//at each step write argno to idxline string
//finally replace place-holder with idxline value
if (expandedCall)
{
p = &newline;
p = strstr(p, "invoke"); // even if the line only contains uinvoke, such as in an HLL expression this will still find it.
if (p != NULL)
{
bool inBrackets = FALSE;
j = (int)(p - (char *)&newline);
stackPt++;
idxStack[stackPt] = 0;
while (*p)
{
idxline[j++] = idxStack[stackPt];
if (*p == '(')
inBrackets = TRUE;
if (*p == ')')
inBrackets = FALSE;
if (!inBrackets && *p == ',')
idxStack[stackPt]++;
p++;