-
Notifications
You must be signed in to change notification settings - Fork 3
/
optreg.cpp
1243 lines (1184 loc) · 31.7 KB
/
optreg.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 _REGOPT_
#include "misc.h"
#include "tok.h"
unsigned char optinitreg = TRUE;
int OptNameIDX(char *name, char *nam, int size);
int getnumber(char *buf, unsigned long *num, int *typenum = NULL);
int convertrazr(int *reg, int razr);
enum {
t_undef,
t_const,
t_string,
t_id,
};
enum { razr_8l, razr_8h, razr_16, razr_32 };
REGISTERSTAT *regstat;
#ifdef OPTVARCONST
void CompareLVIC(BLVIC *bak);
void FreeLVIC(BLVIC *bak);
BLVIC *BakLVIC();
void CopyLVIC(BLVIC *bak);
#endif
void KillRegLec(int reg) {
KillVar(regs[0][reg]);
KillVar(regs[1][reg]);
if (reg < 4) {
KillVar(begs[reg]);
KillVar(begs[reg + 4]);
}
}
void ClearReg(int reg) {
if (optinitreg == FALSE)
return;
(regstat + reg)->type = t_undef;
REGEQVAR *curv, *nextv;
curv = (regstat + reg)->next;
while (curv) {
nextv = curv->next;
free(curv);
curv = nextv;
}
(regstat + reg)->next = NULL;
// printf("clear reg %d\n",reg);
KillRegLec(reg);
}
void clearregstat(int regs) {
if (optinitreg == FALSE)
return;
for (int i = 0; i < 8; i++) {
if ((regs & (1 << i)) == 0) {
(regstat + i)->type = t_undef;
REGEQVAR *curv, *nextv;
curv = (regstat + i)->next;
while (curv) {
nextv = curv->next;
free(curv);
curv = nextv;
}
(regstat + i)->next = NULL;
}
}
}
void initregstat() {
regstat = (REGISTERSTAT *)MALLOC(sizeof(REGISTERSTAT) * 8);
for (int i = 0; i < 8; i++)
(regstat + i)->next = NULL;
clearregstat();
}
void deinitregstat() {
if (regstat) {
clearregstat();
free(regstat);
}
regstat = NULL;
}
REGISTERSTAT *BakRegStat() {
REGISTERSTAT *bakregstat;
#ifndef OPTVARCONST
if (optinitreg == FALSE)
return NULL;
bakregstat = (REGISTERSTAT *)MALLOC(sizeof(REGISTERSTAT) * 8);
#else
bakregstat =
(REGISTERSTAT *)MALLOC(sizeof(REGISTERSTAT) * 8 + sizeof(BLVIC *));
(bakregstat + 8)->bakvic = BakLVIC();
#endif
memcpy(bakregstat, regstat, sizeof(REGISTERSTAT) * 8);
REGEQVAR *cur, *news;
for (int i = 0; i < 8; i++) {
if ((bakregstat + i)->next) {
cur = (REGEQVAR *)MALLOC(sizeof(REGEQVAR));
memcpy(cur, (bakregstat + i)->next, sizeof(REGEQVAR));
(bakregstat + i)->next = cur;
while (cur->next) {
news = (REGEQVAR *)MALLOC(sizeof(REGEQVAR));
memcpy(news, cur->next, sizeof(REGEQVAR));
cur->next = news->next;
}
}
}
return bakregstat;
}
void CopyRegStat(REGISTERSTAT *bak) {
#ifndef OPTVARCONST
if (optinitreg == FALSE || bak == NULL)
return;
#else
if (bak == NULL)
return;
CopyLVIC((bak + 8)->bakvic);
#endif
memcpy(regstat, bak, sizeof(REGISTERSTAT) * 8);
REGEQVAR *cur, *news;
for (int i = 0; i < 8; i++) {
if ((regstat + i)->next) {
cur = (REGEQVAR *)MALLOC(sizeof(REGEQVAR));
memcpy(cur, (regstat + i)->next, sizeof(REGEQVAR));
(regstat + i)->next = cur;
while (cur->next) {
news = (REGEQVAR *)MALLOC(sizeof(REGEQVAR));
memcpy(news, cur->next, sizeof(REGEQVAR));
cur->next = news->next;
}
}
}
}
void ClearRegBak(int reg, REGISTERSTAT *regst) {
REGEQVAR *curv, *nextv;
(regst + reg)->type = t_undef;
curv = (regst + reg)->next;
while (curv) {
nextv = curv->next;
free(curv);
curv = nextv;
}
(regst + reg)->next = NULL;
}
void AddRegVar(int reg, int razr, ITOK *itok4) {
if (optinitreg == FALSE || (itok4->flag & f_useidx) != 0)
return;
REGEQVAR *cur, *news;
cur = (REGEQVAR *)MALLOC(sizeof(REGEQVAR));
cur->razr = convertrazr(®, razr);
cur->next = NULL;
strcpy(cur->name, itok4->name);
if ((regstat + reg)->next == NULL)
(regstat + reg)->next = cur;
else {
news = (regstat + reg)->next;
while (news->next)
news = news->next;
news->next = cur;
}
}
int GetRegVar(ITOK *itok4) {
if (optinitreg == FALSE || (itok4->flag & f_useidx) != 0)
return 0;
int retval = 0;
for (int i = 0; i < 8; i++) {
REGEQVAR *cur;
cur = (regstat + i)->next;
while (cur) {
if (strcmp(itok4->name, cur->name) == 0) {
retval |= (1 << i);
break;
}
cur = cur->next;
}
}
return retval;
}
void FreeStat(REGISTERSTAT *bak) {
#ifndef OPTVARCONST
if (optinitreg == FALSE || bak == NULL)
return;
#else
if (bak == NULL)
return;
FreeLVIC((bak + 8)->bakvic);
#endif
for (int i = 0; i < 8; i++)
ClearRegBak(i, bak);
free(bak);
}
void CompareRegStat(REGISTERSTAT *bak) {
#ifndef OPTVARCONST
if (optinitreg == FALSE || bak == NULL)
return;
#else
if (bak == NULL)
return;
CompareLVIC((bak + 8)->bakvic);
#endif
for (int i = 0; i < 8; i++) {
if ((regstat + i)->type == t_undef ||
(regstat + i)->type != (bak + i)->type ||
((regstat + i)->type == t_id &&
strcmp((regstat + i)->id, (bak + i)->id) != 0))
ClearRegBak(i, bak);
}
}
int convertrazr(int *reg, int razr) {
int nr;
if (razr == r8) {
if (*reg < 4)
nr = razr_8l;
else {
nr = razr_8h;
*reg -= 4;
}
} else if (razr == r16)
nr = razr_16;
else
nr = razr_32;
return nr;
}
void ConstToReg(unsigned long num, int reg, int razr) {
char buf[64];
if (optinitreg) {
sprintf(buf, "%ld", num);
IDZToReg(buf, reg, razr);
}
}
void GenRegToReg(int regd, int regs, int razr) {
switch (razr) {
case r8:
op(0x88);
op(128 + 64 + regs * 8 + regd);
break;
case r16:
case r32:
op66(razr);
op(0x89);
op(128 + 64 + regs * 8 + regd);
break;
}
}
int CheckIDZReg(char *name, int reg, int razr)
/*
name - адрес строки
*/
{
int nr;
int retreg = NOINREG;
int i;
if (optinitreg == FALSE || razr > r32)
return NOINREG;
nr = convertrazr(®, razr);
for (i = 0; i < 8; i++) {
// printf("%d type=%d razr=%d
// %s\n",i,(regstat+i)->type,(regstat+i)->razr,(regstat+i)->id);
if ((regstat + i)->type == t_id) {
if ((regstat + i)->razr == nr ||
(razr == r8 && ((regstat + i)->razr == razr_8l ||
(regstat + i)->razr == razr_8h))) {
if (strcmp(name, (regstat + i)->id) == 0) {
if (i == reg && nr == (regstat + i)->razr) {
if (/*razr==r8&&*/ nr == razr_8h)
reg += 4;
waralreadinit(razr == r8 ? begs[reg] : regs[razr / 4][reg]);
// printstatreg();
return SKIPREG;
}
retreg = i;
}
}
}
REGEQVAR *cur;
cur = (regstat + i)->next;
while (cur) {
if (cur->razr == nr ||
(razr == r8 && (cur->razr == razr_8l || cur->razr == razr_8h))) {
if (strcmp(name, cur->name) == 0) {
if (i == reg && nr == cur->razr) {
if (nr == razr_8h)
reg += 4;
waralreadinit(razr == r8 ? begs[reg] : regs[razr / 4][reg]);
return SKIPREG;
}
retreg = i;
break;
}
}
cur = cur->next;
}
}
if (retreg != NOINREG) {
if (razr == r8) {
if ((regstat + retreg)->razr == razr_8h)
retreg += 4;
if (nr == razr_8h)
reg += 4;
waralreadinitreg(begs[reg], begs[retreg]);
} else
waralreadinitreg(regs[razr / 4][reg], regs[razr / 4][retreg]);
}
return retreg;
}
void IDZToReg(char *name, int reg, int razr)
/*
name - адрес строки
*/
{
int nr;
if (optinitreg == FALSE || name == NULL || razr > r32)
return;
nr = convertrazr(®, razr);
ClearReg(reg);
(regstat + reg)->type = t_id;
(regstat + reg)->razr = nr;
strcpy((regstat + reg)->id, name);
KillRegLec(reg);
}
int GetRegNumber(int reg, unsigned long *num, int razr) {
int nr;
nr = convertrazr(®, razr);
if (optinitreg != FALSE && razr <= r32 && (regstat + reg)->type == t_id &&
(regstat + reg)->razr == nr) {
if (isdigit((regstat + reg)->id[0])) {
if (getnumber((regstat + reg)->id, num))
return reg;
}
}
return NOINREG;
}
int GetNumberR(int sreg, unsigned long *num, int razr, unsigned long number) {
int nr;
int reg, rreg = NOINREG;
unsigned long dnum = 0xffffffff;
unsigned long nnum;
if (optinitreg != FALSE && razr <= r32) {
for (reg = 0; reg < 8; reg++) {
if (reg == sreg)
continue;
nr = convertrazr(®, razr);
if ((regstat + reg)->type == t_id && (regstat + reg)->razr == nr) {
// printf("reg=%d
//%s\n",reg,(regstat+reg)->id);
if (isdigit((regstat + reg)->id[0])) {
if (getnumber((regstat + reg)->id, num)) {
if (*num == number)
return reg;
if (*num > number && (*num - number) < dnum) {
dnum = *num - number;
nnum = *num;
rreg = reg;
} else if ((number - *num) < dnum) {
dnum = number - *num;
nnum = *num;
rreg = reg;
}
}
}
}
}
*num = nnum;
}
return rreg;
}
int getnumber(char *buf, unsigned long *num, int *typenum) {
int temp2;
unsigned char *oinput;
unsigned int oinptr, oendinptr;
int retcode = FALSE;
oinptr = inptr;
oinput = input;
oendinptr = endinptr;
input = (unsigned char *)buf;
inptr = 0;
endinptr = 256;
*num = scannumber(typenum == NULL ? &temp2 : typenum);
if (cha == 0)
retcode = TRUE;
inptr = oinptr;
input = oinput;
endinptr = oendinptr;
return retcode;
}
char *GetLecsem(int stop1, int stop2, int type) {
int oinptr, oinptr2;
char ocha;
int oline;
char nam[SIZEIDREG];
int i, j;
ITOK oitok;
int otok;
char c;
int pinptr;
switch (tok2) {
case tk_string:
case tk_proc:
case tk_apiproc:
case tk_undefproc:
case tk_declare:
return NULL;
}
oinptr = inptr;
oinptr2 = inptr = inptr2;
ocha = cha;
cha = cha2;
oline = linenumber;
otok = tok;
oitok = itok;
i = 0;
do {
pinptr = inptr;
FastTok(0);
while (tok == tk_closebracket && i) {
i--;
pinptr = inptr;
FastTok(0);
}
if (tok == tk_openbracket)
i++;
} while (tok != stop1 && tok != stop2 && tok != tk_eof && itok.type != type);
if (pinptr)
pinptr--;
i = 0;
if (cha2 > ' ') {
nam[0] = cha2;
i++;
}
for (j = 0; i < SIZEIDREG; i++) {
do {
c = input[j + oinptr2];
j++;
} while (c <= 0x20);
if ((oinptr2 + j) > pinptr)
break;
nam[i] = c;
}
inptr2 = oinptr2;
inptr = oinptr;
cha = ocha;
linenumber = oline;
tok = otok;
itok = oitok;
if (i == SIZEIDREG)
return NULL;
c = nam[0];
if (c == ';' || c == ',' || c == ')' || c == '}')
return NULL;
nam[i] = 0;
return BackString(nam);
}
void GetEndLex(int stop1, int stop2, int type) {
int i = 0;
int oscanlexmode;
if (bufrm) {
free(bufrm);
bufrm = NULL;
}
if (strinf.bufstr) {
free(strinf.bufstr);
strinf.bufstr = NULL;
}
oscanlexmode = scanlexmode;
scanlexmode = DEFLEX;
while (tok2 != stop1 && tok2 != stop2 && tok2 != tk_eof &&
itok2.type != type) {
nexttok();
while (tok == tk_closebracket && i) {
i--;
nexttok();
}
if (tok == tk_openbracket)
i++;
if (bufrm) {
free(bufrm);
bufrm = NULL;
}
if (strinf.bufstr) {
free(strinf.bufstr);
strinf.bufstr = NULL;
}
}
// retoldscanmode(oscanlexmode);
scanlexmode = oscanlexmode;
// printf("tok2=%d type2=%d\n",tok2,itok2.type);
}
void IDXToReg(char *name, int size, int reg)
/*
name - адрес начала
size - размерность
*/
{
int nr;
char nam[SIZEIDREG];
if (optinitreg == FALSE)
return;
nr = (am32 == 0 ? razr_16 : razr_32);
ClearReg(reg);
if (OptNameIDX(name, nam, size) == TRUE) {
(regstat + reg)->type = t_id;
(regstat + reg)->razr = nr;
strcpy((regstat + reg)->id, nam);
}
KillRegLec(reg);
}
int OptNameIDX(char *name, char *nam, int size) {
int i, j;
int maxlen = SIZEIDREG;
j = 0;
if (name[j] == '&') {
size = 1;
j++;
}
if (size > 1)
maxlen -= 10;
for (i = 0;; i++) {
if (i == maxlen)
return FALSE;
char c;
do {
c = name[j];
j++;
} while (c <= 0x20 && c != 0);
if (c == 0 || c == ';')
break;
nam[i] = c;
}
nam[i] = 0;
if (size > 1)
sprintf(nam + i, "*%d", size);
return TRUE;
}
int CheckIDXReg(char *name, int size, int reg)
/*
name - адрес начала
size - размерность
*/
{
int nr;
char nam[SIZEIDREG];
int retreg = NOINREG;
int i;
if (optinitreg == FALSE)
return NOINREG;
nr = (am32 == 0 ? razr_16 : razr_32);
if (OptNameIDX(name, nam, size) == TRUE) {
for (i = 0; i < 8; i++) {
if ((regstat + i)->type == t_id && (regstat + i)->razr == nr) {
if (strcmp(nam, (regstat + i)->id) == 0) {
if (i == reg) {
// printf("%s
//%s\n",nam,(regstat+i)->id);
waralreadinit(regs[am32][reg]);
return SKIPREG;
}
retreg = i;
}
}
REGEQVAR *cur;
cur = (regstat + i)->next;
while (cur) {
if (cur->razr == nr && strcmp(nam, cur->name) == 0) {
if (i == reg) {
waralreadinit(regs[am32][reg]);
return SKIPREG;
}
retreg = i;
break;
}
cur = cur->next;
}
}
}
return retreg;
}
int RegToReg(int regd, int regs, int razr) {
int nr, nrs;
if (optinitreg == FALSE)
return NOINREG;
nrs = convertrazr(®s, razr);
nr = convertrazr(®d, razr);
if (razr == r8 && nrs != nr && regs == regd)
goto noreg;
if (nrs == nr && (regstat + regd)->type == (regstat + regs)->type &&
(regstat + regs)->type != t_undef &&
(regstat + regd)->razr == (regstat + regs)->razr) {
if (strcmp((regstat + regd)->id, (regstat + regs)->id) == 0)
return SKIPREG;
}
noreg:
ClearReg(regd);
memcpy((regstat + regd), (regstat + regs), sizeof(REGISTERSTAT));
REGEQVAR *cur, *news;
if ((regstat + regd)->next) {
cur = (REGEQVAR *)MALLOC(sizeof(REGEQVAR));
memcpy(cur, (regstat + regd)->next, sizeof(REGEQVAR));
(regstat + regd)->next = cur;
while (cur->next) {
news = (REGEQVAR *)MALLOC(sizeof(REGEQVAR));
memcpy(news, cur->next, sizeof(REGEQVAR));
cur->next = news->next;
}
}
(regstat + regd)->razr = nr;
return NOINREG;
;
}
int RegSwapReg(int reg1, int reg2, int razr) {
int nr, nrs;
REGISTERSTAT temp;
if (optinitreg == FALSE)
return NOINREG;
nrs = convertrazr(®2, razr);
nr = convertrazr(®1, razr);
if ((regstat + reg1)->type && (regstat + reg1)->razr > nr)
ClearReg(reg1);
if ((regstat + reg2)->type && (regstat + reg2)->razr > nrs)
ClearReg(reg2);
if ((regstat + reg1)->razr < razr_16 && nrs >= razr_16)
ClearReg(reg1);
if ((regstat + reg2)->razr < razr_16 && nr >= razr_16)
ClearReg(reg2);
if (razr == r8 && nrs != nr && reg1 == reg2)
goto noreg;
if ((regstat + reg1)->type == (regstat + reg2)->type &&
(regstat + reg2)->type != r_undef &&
(regstat + reg1)->razr == (regstat + reg2)->razr) {
if (strcmp((regstat + reg1)->id, (regstat + reg2)->id) == 0)
return SKIPREG;
}
noreg:
KillRegLec(reg1);
KillRegLec(reg2);
memcpy(&temp, (regstat + reg1), sizeof(REGISTERSTAT));
memcpy((regstat + reg1), (regstat + reg2), sizeof(REGISTERSTAT));
memcpy((regstat + reg2), &temp, sizeof(REGISTERSTAT));
return NOINREG;
;
}
void KillVar(char *name)
/*-----------------22.04.03 15:51-------------------
поиск переменной в регистрах и если найдена, то
убрать инициализацию регистра
--------------------------------------------------*/
{
char *pstr, *nam;
int len;
if (optinitreg == FALSE)
return;
len = strlen(name);
for (int i = 0; i < 8; i++) {
if ((regstat + i)->type == t_id) {
pstr = (regstat + i)->id;
for (;;) {
if ((nam = strstr(pstr, name)) != NULL) {
char c;
c = nam[len];
if (c != '_' && (!isalpha(c))) {
if (nam != pstr) {
c = nam[-1];
if (c == '_' || isalpha(c))
goto novar;
}
ClearReg(i);
break;
}
} else
break;
novar:
pstr = nam + 1;
}
}
restart:
if ((regstat + i)->next) {
REGEQVAR *cur, *prev;
cur = (regstat + i)->next;
if (strcmp(cur->name, name) == 0) {
(regstat + i)->next = cur->next;
// printf("kill '%s'\n",name);
free(cur);
goto restart;
}
prev = cur;
cur = cur->next;
while (cur) {
if (strcmp(cur->name, name) == 0) {
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
}
}
/*-----------------16.06.05 23:45-------------------
Замена переменных константами
--------------------------------------------------*/
#ifdef OPTVARCONST
BLVIC *mainvic;
unsigned char replasevar = TRUE;
// void updnum(int tok4,long long *num);
#define MAXSIZEVIC 64
int cursizevic = 0;
LVIC *listvic = NULL;
void CreateMainLVIC() {
mainvic = (BLVIC *)MALLOC(sizeof(BLVIC));
listvic = mainvic->listvic = (LVIC *)MALLOC(sizeof(LVIC) * MAXSIZEVIC);
cursizevic = mainvic->sizevic = MAXSIZEVIC;
}
void KillMainLVIC() {
free(listvic);
free(mainvic);
listvic = NULL;
}
BLVIC *BakLVIC() {
BLVIC *bakvic;
if (replasevar == 0)
return NULL;
bakvic = (BLVIC *)MALLOC(sizeof(BLVIC));
bakvic->listvic = (LVIC *)MALLOC(sizeof(LVIC) * cursizevic);
bakvic->sizevic = cursizevic;
memcpy(bakvic->listvic, listvic, sizeof(LVIC) * cursizevic);
return bakvic;
}
void CopyLVIC(BLVIC *bak) {
if (bak == NULL)
return;
if (cursizevic < bak->sizevic) {
listvic = (LVIC *)REALLOC(listvic, sizeof(LVIC) * bak->sizevic);
}
memcpy(listvic, bak->listvic, sizeof(LVIC) * bak->sizevic);
if (cursizevic > bak->sizevic) {
for (int i = bak->sizevic; i < cursizevic; i++)
(listvic + i)->rec = NULL;
} else
cursizevic = bak->sizevic;
}
void FreeLVIC(BLVIC *bak) {
if (bak == NULL)
return;
free(bak->listvic);
free(bak);
}
void CompareLVIC(BLVIC *bak) {
LVIC *bvic;
if (bak == NULL)
return;
for (int i = 0; i < bak->sizevic; i++) {
if ((bak->listvic + i)->rec != NULL) {
int j;
bvic = bak->listvic + i;
for (j = 0; j < cursizevic; j++) {
if ((listvic + j)->rec == bvic->rec) {
if ((listvic + j)->lnumber != bvic->lnumber)
bvic->rec = NULL;
break;
}
}
if (j == cursizevic)
bvic->rec = NULL;
}
}
}
void ClearLVIC() {
memset(listvic, 0, cursizevic * sizeof(LVIC));
/* printf("%s(%d)> Init table LVIC\n",
startfileinfo==NULL?"":(startfileinfo+currentfileinfo)->filename,
linenumber);*/
}
int Const2Var(ITOK *itok4, long long num, int typenum) {
int i;
int freevic = -1;
if (replasevar && itok4->rec && (itok4->flag & f_useidx) == 0) {
if ((typenum == tk_word || typenum == tk_dword) && num < 256)
typenum = tk_byte;
for (i = 0; i < cursizevic; i++) {
if ((listvic + i)->rec == itok4->rec)
break;
if (freevic == -1 && (listvic + i)->rec == NULL)
freevic = i;
}
if (i == cursizevic) {
if (freevic == -1) {
cursizevic += MAXSIZEVIC;
listvic = (LVIC *)REALLOC(listvic, sizeof(LVIC) * cursizevic);
for (freevic = cursizevic - 1; freevic > i; freevic--)
(listvic + freevic)->rec = NULL;
} else
i = freevic;
} else if ((listvic + i)->contype == typenum &&
(listvic + i)->lnumber == num)
return FALSE;
(listvic + i)->rec = itok4->rec;
(listvic + i)->contype = typenum;
(listvic + i)->lnumber = num;
switch (itok4->type) {
case tp_localvar:
case tp_paramvar:
(listvic + i)->typevar = tp_localvar;
break;
case tp_postvar:
case tp_gvar:
(listvic + i)->typevar = tp_gvar;
break;
default:
(listvic + i)->typevar = itok4->type;
break;
}
/* printf("%s(%d)> %s var '%s' by num=%u type=%d\n",
startfileinfo==NULL?"":(startfileinfo+currentfileinfo)->filename,
linenumber,
freevic==-1?"Update":"Add",
itok4->name,
(listvic+i)->number,
typenum);*/
}
return TRUE;
}
void Const2VarRec(LVIC *varconst) {
int i;
int freevic = -1;
if (replasevar == 0)
return;
for (i = 0; i < cursizevic; i++) {
if ((listvic + i)->rec == varconst->rec)
break;
if (freevic == -1 && (listvic + i)->rec == NULL)
freevic = i;
}
if (i == cursizevic) {
if (freevic == -1) {
cursizevic += MAXSIZEVIC;
listvic = (LVIC *)REALLOC(listvic, sizeof(LVIC) * cursizevic);
for (freevic = cursizevic - 1; freevic > i; freevic--)
(listvic + freevic)->rec = NULL;
} else
i = freevic;
}
(listvic + i)->rec = varconst->rec;
(listvic + i)->contype = varconst->contype;
(listvic + i)->lnumber = varconst->lnumber;
switch (varconst->rec->type) {
case tp_localvar:
case tp_paramvar:
(listvic + i)->typevar = tp_localvar;
break;
case tp_postvar:
case tp_gvar:
(listvic + i)->typevar = tp_gvar;
break;
default:
(listvic + i)->typevar = varconst->rec->type;
break;
}
}
void ClearVarByNum(ITOK *itok4) {
if (replasevar && itok4->rec && (itok4->flag & f_useidx) == 0) {
for (int i = 0; i < cursizevic; i++) {
if ((listvic + i)->rec == itok4->rec) {
(listvic + i)->rec = NULL;
/* printf("%s(%d)> Clear by constant var
'%s'\n",
startfileinfo==NULL?"":(startfileinfo+currentfileinfo)->filename,
linenumber,
itok4->name);*/
break;
}
}
}
}
int UpdVarConst(ITOK *itok4, long long num, int typenum, int operand) {
int retcode = FALSE;
int i;
if (replasevar == 0 || itok4->rec == NULL || (itok4->flag & f_useidx) != 0)
return TRUE;
for (i = 0; i < cursizevic; i++) {
if ((listvic + i)->rec == itok4->rec) {
// printf("%s(%d)> Modif var '%s' by num=%u
//",startfileinfo==NULL?"":(startfileinfo+currentfileinfo)->filename,linenumber,itok4->name,(listvic+i)->number);
switch (operand) {
case tk_plusequals:
case tk_plusplus:
operand = tk_plus;
break;
case tk_minusequals:
case tk_minusminus:
operand = tk_minus;
break;
case tk_xorequals:
operand = tk_xor;
break;
case tk_andequals:
operand = tk_and;
break;
case tk_orequals:
operand = tk_or;
break;
case tk_multequals:
operand = tk_mult;
break;
case tk_divequals:
operand = tk_div;
break;
case tk_rrequals:
operand = tk_rr;
break;
case tk_llequals:
operand = tk_ll;
break;
case tk_numsign:
if ((listvic + i)->contype == tk_float)
(listvic + i)->number |= 0x80000000;
else if ((listvic + i)->contype == tk_double)
(listvic + i)->lnumber |= 0x8000000000000000LL;
else
(listvic + i)->lnumber = -(listvic + i)->lnumber;
return TRUE;
;
case tk_not:
(listvic + i)->lnumber = ~(listvic + i)->lnumber;
return TRUE;
;
}
switch ((listvic + i)->contype) {
case tk_char:
case tk_int:
case tk_long:
retcode = calclongnumber(&(listvic + i)->number, num, operand);
break;
case tk_byte:
case tk_word:
case tk_dword:
retcode = calcdwordnumber((unsigned long *)&(listvic + i)->number, num,
operand);
break;
case tk_qword:
retcode = calcqwordnumber((unsigned long long *)&(listvic + i)->lnumber,
num, operand);
break;
case tk_float:
retcode =
calcfloatnumber(&(listvic + i)->fnumber, *(float *)&num, operand);
break;
case tk_double:
retcode =
calcdoublenumber(&(listvic + i)->dnumber, *(double *)&num, operand);
break;
}
/* if(retcode)printf("new
num=%u\n",(listvic+i)->number); else{ puts("error");
(listvic+i)->rec=NULL;
} */
break;
}
}
return TRUE;