-
Notifications
You must be signed in to change notification settings - Fork 3
/
toka.cpp
7260 lines (7131 loc) · 201 KB
/
toka.cpp
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
#define _TOKA_
#include <string.h>
#include <sys/stat.h>
#ifndef _MSC_VER
#include <unistd.h>
#else
#include <io.h>
#endif
#ifdef __CONSOLE__
#include <windows.h>
#else
#include <wchar.h>
#endif
#include "dirlist.h"
#include "id.h"
#include "misc.h"
#include "resname.h"
#include "tok.h"
idrec *treestart = NULL;
idrec *definestart = NULL;
UNDEFOFF *undefoffstart = NULL;
DLLLIST *listdll = NULL;
structteg *tegtree = NULL; //глобальный срисок тегов
structteg *ltegtree = NULL; //локальный срисок тегов
// idrec *lstructlist=NULL; //список локальных структур
SINFO strinf = {NULL};
static int notdef = TRUE;
static char precha;
int scanalltoks = TRUE;
static volatile idrec **DynamicList = NULL;
static int sizeDL; //размер списка
static volatile int countDP; //число динамических процедур в списке
static int findofset = FALSE;
#define STEPDL 128; //шаг увеличения размера списка
ITOK structadr;
char id2[ID2S][9] = {
"ESCHAR", "ESBYTE", "ESINT", "ESWORD", "ESLONG", "ESDWORD",
"ESFLOAT", "ESQWORD", "ESDOUBLE", "CSCHAR", "CSBYTE", "CSINT",
"CSWORD", "CSLONG", "CSDWORD", "CSFLOAT", "CSQWORD", "CSDOUBLE",
"SSCHAR", "SSBYTE", "SSINT", "SSWORD", "SSLONG", "SSDWORD",
"SSFLOAT", "SSQWORD", "SSDOUBLE", "DSCHAR", "DSBYTE", "DSINT",
"DSWORD", "DSLONG", "DSDWORD", "DSFLOAT", "DSQWORD", "DSDOUBLE",
"FSCHAR", "FSBYTE", "FSINT", "FSWORD", "FSLONG", "FSDWORD",
"FSFLOAT", "FSQWORD", "FSDOUBLE", "GSCHAR", "GSBYTE", "GSINT",
"GSWORD", "GSLONG", "GSDWORD", "GSFLOAT", "GSQWORD", "GSDOUBLE",
};
char regs[2][8][4] = {{"AX", "CX", "DX", "BX", "SP", "BP", "SI", "DI"},
{"EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI"}};
char begs[8][3] = {"AL", "CL", "DL", "BL", "AH", "CH", "DH", "BH"};
char segs[6][3] = {"ES", "CS", "SS", "DS", "FS", "GS"};
char mon[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
unsigned char cha2;
char skipfind =
FALSE; /* пропуск поиска в глобальном дереве и среди
локальных переменных */
static unsigned char savestring3 = FALSE; //разрешить запись в буфер string3
static int posstr3; //указатель позиции в string3
unsigned int inptr2;
unsigned int linenum2 = 0; //если не нуль, то идет обраьотка
char displaytokerrors; /* flag to display errors, 0 for tok2 scan */
char *bufrm = NULL;
char *startline = NULL;
char *endinput = NULL;
unsigned char skiplocals = FALSE;
int scanlexmode = STDLEX;
unsigned char bytesize = TRUE;
COM_MOD *cur_mod = NULL;
void docals(idrec *ptr);
void dostructvar2(
int *tok4, ITOK *itok4, structteg *tteg,
unsigned char *string4); //разбор структур на переменные и структуры
void dosizeof(ITOK *itok4); //опр значение sizeof
void ofsstr(int *tok4, ITOK *itok4);
int searchlocals(ITOK *itok4, int *tok4, unsigned char *string4);
unsigned char convert_char();
void tokscan(int *tok4, ITOK *itok4, unsigned char *string4);
int calcnum(int *ctok, ITOK *cstok, char *cstring, long *retval);
int GetDirective(char *str);
int CheckAsmName(char *name);
void tag_massiv(int *tok4, ITOK *itok4, unsigned char *string4);
int CheckResName(char *name);
void GetTypeVar(int *tok4);
int RegToRM(int number, int tok4);
elementteg *FindClassEl(structteg *searcht, unsigned char *string4, int *addofs,
structteg *subteg);
int AskUseDestr(structteg *searcht);
int SaveStruct(int size, idrec *newrec);
int CheckDefPar(char *name);
int searchtree2(idrec *fptr, ITOK *itok4, int *tok4, unsigned char *string4);
extern void blockerror();
extern void block16_32error();
extern void notstructname();
extern void badtoken();
extern void opb(unsigned long num, unsigned int ofs, unsigned int size);
extern void CorrectOfsBit(int bitofs);
extern int skipstring(int pos, unsigned char term);
extern int skipcomment(int pos);
void retoldscanmode(int mode) {
if (mode == STDLEX && (cha2 == 13 || cha2 == 10) && tok != tk_endline) {
cha2 = cha;
}
scanlexmode = mode;
}
#ifdef __CONSOLE__
void CheckConvertString(char *string4) {
if (cha == 'o') {
OemToChar(string4, string4);
nextchar();
} else if (cha == 'w') {
CharToOem(string4, string4);
nextchar();
}
}
#endif
void DateToStr(char *buf) {
// GetDateFormat(LOCALE_SYSTEM_DEFAULT,DATE_SHORTDATE|LOCALE_NOUSEROVERRIDE,NULL,NULL,buf,80);
sprintf(buf, "%2d %s %d", timeptr.tm_mday, mon[timeptr.tm_mon],
timeptr.tm_year + 1900);
}
void compressoffset(ITOK *thetok) {
if (thetok->sib == CODE16) {
if (thetok->rm != rm_d16) {
thetok->rm &= 7;
if (thetok->number == 0) {
if (thetok->rm == rm_BP)
thetok->rm |= rm_mod01;
} else if ((int)thetok->number < 128 && (int)thetok->number >= -128)
thetok->rm |= rm_mod01;
else
thetok->rm |= rm_mod10;
}
} else {
if (thetok->rm != rm_d32 && thetok->rm != rm_sib) {
thetok->rm &= 7;
if (thetok->number == 0) {
if (thetok->rm == 5)
thetok->rm |= rm_mod01;
if (thetok->rm == 4 && (thetok->sib & 7) == 5)
thetok->rm |= rm_mod01;
} else if (thetok->number < 128 && thetok->number >= -128)
thetok->rm |= rm_mod01;
else
thetok->rm |= rm_mod10;
}
}
}
int CalcRm16(int base, int idx) {
int rm;
rm = 0;
switch (base + idx) {
case 2:
rm++;
case 4:
rm++;
case 6:
rm++;
case 5:
rm++;
case 12:
rm++;
case 11:
rm++;
case 10:
rm++;
}
return rm;
}
void SRBackBuf(int mode) // save/restore BackTextBlock
{
static int size = 0;
static char *buf;
if (mode == 0) {
if (SizeBackBuf) {
size = SizeBackBuf;
SizeBackBuf = 0;
buf = BackTextBlock;
}
} else {
SizeBackBuf = size;
size = 0;
BackTextBlock = buf;
}
}
void ExpandRm(int rm, int sib, int *zoom, int *base, int *idx) {
int rm0;
int reg1 = -1, reg2 = -1;
int z = 0;
rm0 = rm & 0x7;
if (sib == CODE16 || sib == (CODE16 + 1)) {
switch (rm0) {
case 0:
reg1 = BX;
reg2 = SI;
break;
case 1:
reg1 = BX;
reg2 = DI;
break;
case 2:
reg1 = BP;
reg2 = SI;
break;
case 3:
reg1 = BP;
reg2 = DI;
break;
case 4:
reg2 = SI;
break;
case 5:
reg2 = DI;
break;
case 6:
if ((rm & 0xC0) != 0)
reg1 = BP;
break;
case 7:
reg1 = BX;
break;
}
} else {
if (rm0 == 4) {
reg1 = sib & 7;
reg2 = (sib >> 3) & 7;
if (reg1 == 5 && (rm & 0xc0) == 0)
reg1 = -1;
if (reg2 == 4)
reg2 = -1;
else
z = sib >> 6;
} else {
reg1 = rm0;
if (reg1 == 5 && (rm & 0xc0) == 0)
reg1 = -1;
}
}
*base = reg1;
*idx = reg2;
*zoom = z;
}
int CheckZoom(int size) {
int zoom = 0;
switch (size) {
case 8:
zoom++;
case 4:
zoom++;
case 2:
zoom++;
}
return zoom;
}
void calcrm(ITOK *itok4, int ttok) //обработка выражения в []
{
int idx, base, razr = 0, zoom, rm = 0;
long numrm = 0, cnum, ocnum;
int nextscan;
ITOK cstok, dstok;
int ctok, sopenb;
unsigned char *pstring;
unsigned int flag;
unsigned int sizevar = 1;
unsigned int prevtok = tk_number, operand = tk_plus;
int dsword, dsword2;
nextchar();
whitespace(); //пропуск незначащих символов
if (!displaytokerrors) {
for (int i = 1; i != 0;) {
FastTok(0, &ctok, &cstok);
switch (ctok) {
case tk_closeblock:
i--;
break;
case tk_openblock:
i++;
break;
case tk_eof:
i = 0;
break;
}
}
return;
}
dsword2 = dsword = itok4->post & POINTER;
itok4->post &= NOTPOINTER;
memcpy(&dstok, itok4, sizeof(ITOK));
ExpandRm(dstok.rm, dstok.sib, &zoom, &base, &idx);
if ((dstok.sib == CODE16 || dstok.sib == (CODE16 + 1)) && dstok.rm != rm_d16)
razr = r16;
if (am32 && dstok.rm != rm_d32) {
rm = dstok.rm & 0xC0;
razr = r32;
}
// printf("in idx=%d base=%d zoom=%d\n",idx,base,zoom);
pstring = (unsigned char *)MALLOC(STRLEN);
sopenb = inptr;
char bcha = cha;
SRBackBuf(0);
flag = f_useidx; // 0;//cstok.flag;
if (bytesize == FALSE) {
sizevar = GetVarSize(ttok);
if (dsword)
sizevar = 1;
if (am32)
zoom = CheckZoom(sizevar);
}
if (cha == '&') {
nextchar();
whitespace(); //пропуск незначащих символов
sizevar = 1;
zoom = 0;
}
for (;;) {
nextscan = TRUE;
if (cha == '#' && dsword)
dsword = 1;
tokscan(&ctok, &cstok, pstring);
whitespace(); //пропуск незначащих символов
if (dsword == 1)
strcpy(dstok.name, cstok.name);
// printf("tok=%d num=%d %s\n",ctok,cstok.number,cstok.name);
loopsw:
switch (ctok) {
case tk_reg:
if (razr == 0)
razr = r16;
else if (razr == r32 || sizevar != 1 || operand != tk_plus)
goto runblock; // 16.12.04 22:39
if (cstok.number == BP || cstok.number == BX) {
if (base == -1)
base = cstok.number;
else
goto runblock;
} else if (cstok.number == SI || cstok.number == DI) {
if (idx == -1)
idx = cstok.number;
else
goto runblock;
} else
goto runblock;
prevtok = tk_reg;
break;
case tk_minus:
if (calcnum(&ctok, &cstok, (char *)pstring, &cnum) == 0)
goto runblock;
prevtok = tk_number;
ocnum = cstok.number;
goto enumb;
case tk_number:
if (operand == tk_mult && prevtok == tk_reg32) {
if (zoom == 0) {
prevtok = tk_number;
if (razr == 0)
razr = r32;
else if (razr == r16)
goto runblock;
zoom = CheckZoom(cstok.number);
if (zoom)
break;
} else
goto runblock;
}
prevtok = tk_number;
ocnum = cstok.number;
if (zoom == 0 && razr != r16 && (idx == -1 || base == -1)) {
if ((zoom = CheckZoom(cstok.number)) != 0) {
if (cha == '*') {
nextchar();
tokscan(&ctok, &cstok, pstring);
if (ctok == tk_reg32) {
if (idx != -1)
base = idx;
idx = cstok.number;
prevtok = tk_reg32;
if (razr == 0)
razr = r32;
break;
}
if (ctok == tk_number) {
calclongnumber(&ocnum, cstok.number, tk_mult);
numrm += ocnum;
zoom = 0;
break;
}
goto runblock;
}
zoom = 0;
}
}
if (calcnum(&ctok, &cstok, (char *)pstring, &cnum) == 0)
goto runblock;
enumb:
flag ^= cstok.flag;
whitespace(); //пропуск незначащих символов
ocnum = cnum;
numrm += cnum;
if (cstok.type == tp_opperand || cstok.type == tp_stopper)
nextscan = FALSE;
else {
operand = tk_plus;
goto loopsw;
}
break;
case tk_reg32:
// printf("prevtok=%d operand=%d ocnum=%d idx=%d base=%d
// zoom=%d\n",prevtok,operand,ocnum,idx,base,zoom);
if (razr == 0)
razr = r32;
if (razr == r16 || (operand != tk_plus && operand != tk_mult) ||
(idx != -1 && base != -1))
goto runblock;
if (sizevar != 1) {
if (idx != -1)
goto runblock;
idx = cstok.number;
} else if (operand == tk_mult) {
if (prevtok != tk_number)
goto runblock;
zoom = CheckZoom(ocnum);
numrm -= ocnum;
if (idx != -1)
base = idx;
idx = cstok.number;
} else if (cha == '*') {
if (idx != -1)
base = idx;
idx = cstok.number;
} else if (base == -1)
base = cstok.number;
else if (idx == -1)
idx = cstok.number;
// else goto runblock;
prevtok = tk_reg32;
break;
case tk_postnumber:
if (dsword == 0 || sizevar != 1)
goto runblock;
if ((cstok.post & USED_DIN_VAR) ==
USED_DIN_VAR) { //динамическая переменная
if (dstok.rec != NULL)
goto runblock;
dstok.rec = cstok.rec;
}
if (operand == tk_minus)
numrm -= cstok.number;
else
numrm += cstok.number;
dstok.post |= cstok.post;
prevtok = tk_number;
break;
case tk_undefofs:
if (dsword == 0 || sizevar != 1)
goto runblock;
if (operand == tk_minus)
numrm -= cstok.number;
else
numrm += cstok.number;
dstok.post |= UNDEF_OFSET;
strcpy(dstok.name, cstok.name);
flag ^= cstok.flag;
prevtok = tk_number;
break;
case tk_rmnumber:
/* if(strinf.bufstr!=NULL){
free(strinf.bufstr);
strinf.bufstr=NULL;
goto runblock;
}*/
if (dsword == 0 || sizevar != 1)
goto runblock;
if (operand == tk_minus)
numrm -= cstok.number;
else
numrm += cstok.number;
dstok.post |= cstok.post;
if (dstok.sib == CODE16) {
if (cstok.rm != rm_d16) {
switch (cstok.rm & 7) {
case 0:
if (base != -1 || idx != -1)
goto runblock;
base = BX;
idx = SI;
break;
case 1:
if (base != -1 || idx != -1)
goto runblock;
base = BX;
idx = DI;
break;
case 2:
if (base != -1 || idx != -1)
goto runblock;
base = BP;
idx = SI;
break;
case 3:
if (base != -1 || idx != -1)
goto runblock;
base = BP;
idx = DI;
break;
case 4:
if (idx != -1)
goto runblock;
idx = SI;
break;
case 5:
if (idx != -1)
goto runblock;
idx = DI;
break;
case 6:
if (idx != -1)
goto runblock;
base = BP;
break;
case 7:
if (idx != -1)
goto runblock;
base = BX;
break;
}
}
razr = r16;
} else {
if (cstok.rm != rm_d32) {
rm = cstok.rm & 0xC0;
cstok.rm &= 7;
if (base == -1)
base = cstok.rm;
else if (idx == -1) {
idx = base;
base = cstok.rm;
} else
goto runblock;
if (base == 4) {
base = cstok.sib & 7;
idx = (cstok.sib >> 3) & 7;
if (base == 5 && rm == 0)
base = -1;
if (idx == 4)
idx = -1;
}
} else
dstok.flag |= cstok.flag & f_reloc;
razr = r32;
}
break;
//-----------------28.07.98 13:59-------------------
// var[i]
//--------------------------------------------------
default:
runblock:
if (dsword2) {
CharToBackBuf('&');
if (strinf.bufstr) {
free(strinf.bufstr);
strinf.bufstr = NULL;
}
}
AddBackBuf(sopenb, bcha);
if (bufrm) {
if (strcmp(bufrm, "&this;") == 0) {
free(bufrm);
CharToBackBuf(0);
sprintf((char *)string3, "&%s*%d+this;", BackTextBlock,
GetVarSize(ttok));
bufrm = BackString((char *)string3);
// puts((char
//*)string3);
goto con1;
} else
free(bufrm);
}
CharToBackBuf(';');
CharToBackBuf(0);
bufrm = (char *)REALLOC(BackTextBlock, SizeBackBuf + 1);
con1:
if (cha != ']')
blockerror();
tokscan(&ctok, &cstok, pstring);
if (itok4->sib >= CODE16)
itok4->sib++;
SRBackBuf(1);
// if(itok4->post&POINTER)itok4->post=0;//&=NOTPOINTER;
free(pstring);
itok4->flag |= flag;
return;
//-----------------28.07.98 13:59-------------------
// end
//--------------------------------------------------
}
if (dsword == 1)
dsword = 0;
if (nextscan) {
tokscan(&ctok, &cstok, pstring);
whitespace(); //пропуск незначащих символов
}
while (ctok == tk_minus) {
if (calcnum(&ctok, &cstok, (char *)pstring, &cnum) == 0)
goto runblock;
whitespace(); //пропуск незначащих символов
numrm += cnum;
flag ^= cstok.flag;
if (cstok.type != tp_opperand && cstok.type != tp_stopper) {
operand = tk_plus;
goto loopsw;
}
}
operand = ctok;
// printf("operand tok=%d flag=%08X
//%s\n",ctok,cstok.flag,cstok.name);
if (ctok == tk_closeblock || ctok == tokens || ctok == tk_eof)
break;
if (ctok != tk_plus && ctok != tk_mult)
goto runblock;
// flag|=cstok.flag;
}
// printf("out idx=%d base=%d zoom=%d\n",idx,base,zoom);
SRBackBuf(1);
if (ctok != tk_closeblock)
expected(']');
numrm *= sizevar;
dstok.number += numrm;
if (razr == r16) {
if (idx == -1 && base == -1)
cstok.rm = 6;
else {
cstok.rm = CalcRm16(base, idx) | rm_mod10;
dstok.sib = CODE16;
}
} else if (razr == 0) {
if (dstok.number < 65536 && dstok.number > -65535)
cstok.rm = 6;
else {
cstok.rm = 5;
dstok.sib = 0;
}
if (am32)
cstok.rm = 5;
} else {
// printf("razr=%d sib=%08X %s\n",razr,dstok.sib,dstok.name);
if (dstok.sib > CODE32)
dstok.sib -= CODE32;
else
dstok.sib = 0;
if (idx != -1) {
cstok.rm = 4;
if (idx == 4)
preerror("ESP cannot be the index register");
if (base != -1)
dstok.sib = (zoom << 6) + (idx << 3) + base;
else {
dstok.sib = (zoom << 6) + (idx << 3) + 5;
rm = (base == 5 ? rm_mod10 : 0);
}
} else {
if (base == 4) {
dstok.sib = (4 << 3) + 4;
cstok.rm = 4;
} else if (base == -1) {
cstok.rm = rm_d32;
} else
cstok.rm = base;
}
if (base != -1 || rm != 0)
cstok.rm |= rm_mod10; //корекция MOD будет позднее, а сейчас максимум
}
dstok.rm = cstok.rm;
dstok.flag |= flag;
memcpy(itok4, &dstok, sizeof(ITOK));
free(pstring);
}
int calcnum(int *ctok, ITOK *cstok, char *cstring, long *retval) {
long value;
unsigned int flag;
if (*ctok == tk_minus) {
tokscan(ctok, cstok, (unsigned char *)cstring);
if (*ctok != tk_number)
return (0);
cstok->number = -cstok->number;
} else if (*ctok != tk_number)
return (0);
value = cstok->number;
flag = cstok->flag;
for (;;) {
int otok;
tokscan(ctok, cstok, (unsigned char *)cstring);
otok = *ctok;
if (otok == tk_closeblock)
break;
tokscan(ctok, cstok, (unsigned char *)cstring);
if (*ctok != tk_number) {
if (*ctok == tk_reg32 && (otok == tk_mult || otok == tk_minus)) {
*ctok = (unsigned int)cstok->number;
*retval = value;
cstok->flag = flag;
return 0;
}
break;
}
if (calclongnumber(&value, cstok->number, otok) == FALSE) {
if (displaytokerrors)
blockerror();
break;
}
/* switch(otok){
case tk_minus: value-=cstok->number; break;
case tk_plus: value+=cstok->number; break;
case tk_xor: value^=cstok->number; break;
case tk_and: value&=cstok->number; break;
case tk_or: value|=cstok->number; break;
case tk_mod: value%=cstok->number; break;
case tk_div: value/=cstok->number; break;
case tk_mult: value*=cstok->number; break;
case tk_rr: value>>=cstok->number; break;
case tk_ll: value<<=cstok->number; break;
case tk_xorminus: value^=-cstok->number; break;
case tk_andminus: value&=-cstok->number; break;
case tk_orminus: value|=-cstok->number; break;
case tk_modminus: value%=-cstok->number; break;
case tk_divminus: value/=-cstok->number; break;
case tk_multminus: value*=-cstok->number; break;
case tk_rrminus: value>>=-cstok->number; break;
case tk_llminus: value<<=-cstok->number; break;
default: if(displaytokerrors)blockerror(); goto end;
}*/
flag ^= cstok->flag;
}
// end:
cstok->flag = flag;
*retval = value;
return -1;
}
void CheckReg(int idx, int base, int *reg1, int *reg2, int razr) {
if (razr == r32) {
unsigned char lreg[8];
int i;
for (i = 0; i < 8; i++) {
if (i == ESP || i == EBP)
lreg[i] = 1;
else {
if ((idx != -1 && idx == i) || (base != -1 && base == i))
lreg[i] = 1;
else
lreg[i] = 0;
}
// printf("%c",lreg[i]+'0');
}
if (lreg[*reg1] == 0)
lreg[*reg1] = 1;
else {
for (i = 8; i != 0; i--) {
if (lreg[i] == 0) {
lreg[i] = 1;
*reg1 = i;
break;
}
}
}
// printf("\nreg1=%d",*reg1);
if (lreg[*reg2] != 0) {
for (i = 8; i != 0; i--) {
if (lreg[i] == 0) {
*reg2 = i;
break;
}
}
}
// printf(" reg2=%d\n",*reg2);
} else {
if (base != -1) {
if (base == *reg1)
*reg1 = SI;
else if (base == *reg2)
*reg2 = DI;
if (*reg1 == *reg2) {
if (*reg1 == SI)
*reg2 = DI;
else
*reg2 = SI;
}
}
if (idx != -1) {
if (idx == SI)
*reg1 = DI;
else
*reg1 = SI;
*reg2 = CX;
}
}
}
void CheckAllMassiv(char *&buf, int sizeel, SINFO *strc, ITOK *ctok, int reg1,
int reg2) {
int zoom0, idx0, base0, newreg;
int zoom1 = 0, zoom2 = 0;
char pv = FALSE;
int razr = r32;
// printf("sib=%X\n",ctok->sib);
switch (ctok->sib) {
case CODE16 + 2:
case CODE32 + 2:
ctok->sib--;
case CODE16 + 1:
case CODE32 + 1:
ctok->sib--;
}
if (ctok->sib == CODE16) {
razr = r16;
if (am32)
block16_32error();
}
if (buf == NULL && strc->bufstr == NULL)
return;
ExpandRm(ctok->rm, ctok->sib, &zoom0, &base0, &idx0);
if (base0 == -1 && idx0 != -1 && zoom0 == 0) {
base0 = idx0;
idx0 = -1;
}
CheckReg(idx0, base0, ®1, ®2, razr);
// printf("reg1=%d reg2=%d idx0=%d base0=%d
// zoom0=%d\n",reg1,reg2,idx0,base0,zoom0);
if (buf != NULL) {
if (*buf == '&') {
sizeel = 1;
pv = TRUE;
ctok->flag &= ~f_reloc;
}
if ((newreg = CheckIDXReg(buf, sizeel, reg1)) != NOINREG) {
if (newreg != SKIPREG) {
if (razr == r16) {
if (newreg != BX && newreg != DI && newreg != SI && newreg != BP)
goto noopt;
if (base0 != -1 && (newreg == BX || newreg == BP))
goto noopt;
if (idx0 != -1 && (newreg == DI || newreg == SI))
goto noopt;
}
waralreadinitreg(regs[am32][reg1], regs[am32][newreg]);
reg1 = newreg;
}
free(buf);
buf = NULL;
} else {
if (razr == r32) {
if ((zoom1 = CheckZoom(sizeel))) {
sizeel = 1;
if ((newreg = CheckIDXReg(buf, sizeel, reg1)) != NOINREG) {
if (newreg != SKIPREG) {
waralreadinitreg(regs[1][reg1], regs[1][newreg]);
reg2 = reg1;
reg1 = newreg;
}
free(buf);
buf = NULL;
goto cont1;
}
}
}
noopt:
if (razr == r32 && idx0 == -1 && base0 == -1 && zoom1 == 0) {
ITOK wtok;
wtok = *ctok;
newreg = CheckMassiv(buf, sizeel, reg1, &idx0, &base0, &wtok.number);
*ctok = wtok;
// newreg=CheckMassiv(buf,sizeel,reg1);
} else
newreg = CheckMassiv(buf, sizeel, reg1);
if (newreg != -1) {
reg2 = reg1;
reg1 = newreg;
}
}
} else {
reg2 = reg1;
reg1 = -1;
}
cont1:
// printf("reg1=%d reg2=%d idx0=%d base0=%d
// zoom0=%d\n",reg1,reg2,idx0,base0,zoom0);
if (strc->bufstr != NULL) {
sizeel = strc->size;
if ((newreg = CheckIDXReg(strc->bufstr, sizeel, reg2)) != NOINREG) {
if (newreg != SKIPREG) {
if (razr == r16) {
if (newreg != BX && newreg != DI && newreg != SI && newreg != BP)
goto noopt2;
if (base0 != -1 && (newreg == BX || newreg == BP))
goto noopt2;
if (idx0 != -1 && (newreg == DI || newreg == SI))
goto noopt2;
}
waralreadinitreg(regs[am32][reg2], regs[am32][newreg]);
reg2 = newreg;
}
free(strc->bufstr);
strc->bufstr = NULL;
} else {
if (razr == r32 && zoom1 == 0) {
if ((zoom2 = CheckZoom(sizeel))) {
sizeel = 1;
if ((newreg = CheckIDXReg(strc->bufstr, sizeel, reg2)) != NOINREG) {
if (newreg != SKIPREG) {
waralreadinitreg(regs[2][reg1], regs[2][newreg]);
reg2 = newreg;
}
free(strc->bufstr);
strc->bufstr = NULL;
goto cont2;
}
}
}
noopt2:
if ((newreg = CheckMassiv(strc->bufstr, sizeel, reg2)) != -1)
reg2 = newreg;
}
cont2:
if (reg1 == -1) {
reg1 = reg2;
zoom1 = zoom2;
reg2 = -1;
zoom2 = 0;
}
} else
reg2 = -1;
// printf("reg1=%d reg2=%d idx0=%d base0=%d
// zoom0=%d\n",reg1,reg2,idx0,base0,zoom0);
if (base0 == -1) {
if (reg1 != -1 && zoom1 == 0) {
base0 = reg1;
reg1 = reg2;
zoom1 = zoom2;
reg2 = -1;
zoom2 = 0;
} else if (reg2 != -1 && zoom2 == 0) {
base0 = reg2;
reg2 = -1;
}
}
if (idx0 == -1) {
if (zoom1) {
zoom0 = zoom1;
zoom1 = 0; // zoom2;
idx0 = reg1;
reg1 = reg2;
reg2 = -1;
} else {
if (reg1 != -1) {
idx0 = reg1;
reg1 = reg2;
zoom1 = zoom2;
reg2 = -1;
zoom2 = 0;
}
}
}
// printf("reg1=%d reg2=%d idx0=%d base0=%d
// zoom0=%d\n",reg1,reg2,idx0,base0,zoom0);
if (reg2 != -1) {
if (zoom1 == 0 && zoom2 == 0) {
op(3);
op(0xC0 + reg1 * 8 + reg2); // add reg1,reg2
} else {
if (zoom1 == 0) {
op(0x8d); // lea reg1,[reg1+reg2*zoom2]
op(reg1 * 8 + 4);
op((zoom2 << 6) + (reg2 << 3) + reg1);
} else {
op(0x8d); // lea reg1,[reg2+reg1*zoom1]
op(reg1 * 8 + 4);
op((zoom1 << 6) + (reg1 << 3) + reg2);
zoom1 = 0;