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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
|
/*-------------------------------------------------------------------------
rtrack.c - tracking content of registers on an mcs51
Copyright 2007 Frieder Ferlemann (Frieder Ferlemann AT web.de)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------
Status:
- passes regression test suite, still bugs are likely
- only active if environment variable SDCC_REGTRACK is set
Missed opportunities:
- does not track offsets to symbols as in "mov dptr,#(_my_int + 2)"
- only used for moves to acc or dptr so chances to use:
"inc r2", "mov r2,a" would not be taken
- a label causes loss of tracking (no handling of information of blocks
known to follow/preceed the current block)
- not used in aopGet or genRet
- SFRX (__xdata volatile unsigned char __at(addr)) not handled as value
- does not track which registers are known to be unchanged within
a function (would not have to be saved when calling the function)
-------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "SDCCglobl.h"
#include "common.h"
#include "ralloc.h"
#include "gen.h"
#include "rtrack.h"
#define D(x) do if (options.verboseAsm) {x;} while(0)
#define DD(x) do if (options.verboseAsm && enableextraverbose) {x;} while(0)
/* move this (or rtrackGetLit() and rtrackMoveALit()
elsewhere? stealing emitcode from gen.c */
void emitcode (const char *inst, const char *fmt,...);
static int enable = +1;
static int enableextraverbose = -1;
static unsigned int rx_num_to_idx (const unsigned int num)
{
const unsigned int regidx[8] =
{ R7_IDX, R6_IDX, R5_IDX, R4_IDX, R3_IDX, R2_IDX, R1_IDX, R0_IDX };
assert( 7 >= num );
return regidx [num & 0x7];
}
static void rtrack_data_unset (const unsigned int idx)
{
assert (idx >= 0);
assert (idx < END_IDX);
if (regs8051[idx].rtrack.symbol || regs8051[idx].rtrack.valueKnown)
{
DD(emitcode (";", "\t%s=?", regs8051[idx].name););
}
if (regs8051[idx].rtrack.symbol)
{
Safe_free (regs8051[idx].rtrack.symbol);
}
memset (®s8051[idx].rtrack, 0, sizeof regs8051[idx].rtrack);
}
static void rtrack_data_set_val (const unsigned int idx, const unsigned char value)
{
assert (idx >= 0);
assert (idx < END_IDX);
regs8051[idx].rtrack.value = value;
regs8051[idx].rtrack.valueKnown = 1;
/* in case it was set by symbol, unset symbol */
if (regs8051[idx].rtrack.symbol)
{
Safe_free (regs8051[idx].rtrack.symbol);
regs8051[idx].rtrack.symbol = NULL;
}
DD(emitcode (";", "\t%s=#0x%02x",
regs8051[idx].name,
regs8051[idx].rtrack.value););
}
static void rtrack_data_set_symbol (const unsigned int idx, const char * const symbol)
{
assert (idx >= 0);
assert (idx < END_IDX);
/* in case it was set by value, unset value */
regs8051[idx].rtrack.value = 0;
regs8051[idx].rtrack.valueKnown = 0;
/* eventually free a previous symbol */
if (regs8051[idx].rtrack.symbol)
{
Safe_free (regs8051[idx].rtrack.symbol);
}
regs8051[idx].rtrack.symbol = Safe_strdup(symbol);
DD(emitcode (";", "\t%s=#%s",
regs8051[idx].name,
regs8051[idx].rtrack.symbol););
}
static int rtrack_data_is_same (const unsigned int idxdst, const unsigned int idxsrc)
{
return ((regs8051[idxdst].rtrack.valueKnown && regs8051[idxsrc].rtrack.valueKnown) &&
(regs8051[idxdst].rtrack.value == regs8051[idxsrc].rtrack.value)) ||
((regs8051[idxdst].rtrack.symbol && regs8051[idxsrc].rtrack.symbol) &&
!strcmp (regs8051[idxdst].rtrack.symbol, regs8051[idxsrc].rtrack.symbol));
}
static void rtrack_data_copy_dst_src (const unsigned int idxdst, const unsigned int idxsrc)
{
assert (idxdst >= 0);
assert (idxdst < END_IDX);
assert (idxsrc >= 0);
assert (idxsrc < END_IDX);
DD
(
if ((NULL != regs8051[idxsrc].rtrack.symbol) || regs8051[idxsrc].rtrack.valueKnown)
{
emitcode (";", "\t%s=%s", regs8051[idxdst].name, regs8051[idxsrc].name);
}
else if (regs8051[idxdst].rtrack.symbol || regs8051[idxdst].rtrack.valueKnown)
{
emitcode (";", "\t%s=*",regs8051[idxdst].name);
}
if (rtrack_data_is_same (idxdst, idxsrc))
{
emitcode (";", "genFromRTrack redundant?");
}
);
/* mov a, acc */
if (idxsrc == idxdst)
return;
regs8051[idxdst].rtrack.valueKnown = regs8051[idxsrc].rtrack.valueKnown;
regs8051[idxdst].rtrack.value = regs8051[idxsrc].rtrack.value;
if (regs8051[idxdst].rtrack.symbol)
{
Safe_free (regs8051[idxdst].rtrack.symbol);
regs8051[idxdst].rtrack.symbol = NULL;
}
memcpy (®s8051[idxdst].rtrack, ®s8051[idxsrc].rtrack, sizeof regs8051[idxdst].rtrack);
if (regs8051[idxsrc].rtrack.symbol)
{
regs8051[idxdst].rtrack.symbol = Safe_strdup(regs8051[idxsrc].rtrack.symbol);
}
}
static void dumpAll()
{
DD
(
unsigned int i;
unsigned int column = 0;
char s[512];
s[0] = 0;
for (i = 0; i < END_IDX; i++)
{
if (regs8051[i].rtrack.valueKnown)
{
column += sprintf(s + column, "%s%s:#0x%02x",
column?" ":"", regs8051[i].name, regs8051[i].rtrack.value);
}
if (NULL != regs8051[i].rtrack.symbol)
{
column += sprintf(s + column, "%s%s:#%s",
column?" ":"", regs8051[i].name, regs8051[i].rtrack.symbol);
}
if (column>160)
{
strcpy (&s[157], "...");
break;
}
}
emitcode (";", "\t%s", s);
);
}
static void invalidateAllRx()
{
unsigned int i;
for (i = 0; i <= 7; i++)
{
rtrack_data_unset (rx_num_to_idx (i));
}
}
static void invalidateAll()
{
invalidateAllRx();
rtrack_data_unset (DPL_IDX);
rtrack_data_unset (DPH_IDX);
rtrack_data_unset (B_IDX);
rtrack_data_unset (A_IDX);
}
static int regidxfromregname (const char* const s)
{
unsigned int i;
for (i = 0; i < END_IDX; i++)
{
if (regs8051[i].name)
if (!strncmp (s, regs8051[i].name, strlen(regs8051[i].name)))
return i;
if (regs8051[i].dname)
if (!strncmp (s, regs8051[i].dname, strlen(regs8051[i].dname)))
return i;
}
return -1;
}
static int valuefromliteral (const char* const s)
{
char* tmp = NULL;
int value;
if (strncmp (s, "0x", 2))
return -1;
value = strtol (s + 2, &tmp, 16);
if (s != tmp)
return value;
return -1;
}
/* tracking values within registers by looking
at the line passed to the assembler.
Tries to keep regs8051[] up to date */
bool _mcs51_rtrackUpdate (const char *line)
{
bool modified = false;
if (enable == -1)
enable = (NULL != getenv("SDCC_REGTRACK"));
if (enableextraverbose == -1)
enableextraverbose = (NULL != getenv("SDCC_REGTRACK_VERBOSE"));
if (!enable ||
*line == ';' || /* comment */
(NULL != strstr( line, "==."))) /* dirty check for _G.debugLine */
return false; /* nothing to do */
dumpAll ();
if (!strncmp (line, "mov", 3))
{
if (!strncmp (line, "movc\ta", 6) ||
!strncmp (line, "movx\ta", 6))
{
rtrack_data_unset (A_IDX);
return false;
}
/* mov to register (r0..r7, dpl, dph, a, b)*/
if (!strncmp (line, "mov\t", 4))
{
int regIdx = regidxfromregname (line + 4);
if (0 <= regIdx)
{
char *argument = strstr (line, ",") + 1;
char *s;
int value;
value = strtol (argument + 1, &s, 16);
/* check literal mov to register */
if ((s != argument + 1) && !strncmp (argument, "#0x", 3))
{
D
(
if (regs8051[regIdx].rtrack.valueKnown && (value == regs8051[regIdx].rtrack.value))
{
emitcode (";", "genFromRTrack removed\t%s", line);
modified = true;
}
if (regs8051[A_IDX].rtrack.valueKnown && (value == regs8051[A_IDX].rtrack.value) &&
(regIdx != A_IDX) && (regIdx != DPL_IDX) && (regIdx != DPH_IDX))
/* ignore DPL/DPH for now as peephole rule for MOV DPTR is much better */
{
emitcode (";", "genFromRTrack replaced\t%s", line);
emitcode ("mov", "%s,a", regs8051[regIdx].dname);
modified = true;
}
else if (regs8051[regIdx].rtrack.valueKnown && (value == regs8051[regIdx].rtrack.value + 1) &&
((regIdx != A_IDX) || (0xff != regs8051[regIdx].rtrack.value)))
{
/* does not occur in regression test mcs51-small */
emitcode (";", "genFromRTrack replaced\t%s", line);
emitcode ("inc", "%s", regs8051[regIdx].name);
modified = true;
}
else if (regs8051[regIdx].rtrack.valueKnown && (value == regs8051[regIdx].rtrack.value - 1) &&
((regIdx != A_IDX) || (0x01 != regs8051[regIdx].rtrack.value)))
{
/* does not occur in regression test mcs51-small */
emitcode (";", "genFromRTrack replaced\t%s", line);
emitcode ("dec", "%s", regs8051[regIdx].name);
modified = true;
}
);
rtrack_data_set_val (regIdx, (unsigned char) value);
}
/* check literal mov of symbol to register */
else if (!strncmp (argument, "#", 1))
{
rtrack_data_set_symbol (regIdx, argument + 1);
}
/* check mov from register to register */
else if (0 <= regidxfromregname (argument))
{
rtrack_data_copy_dst_src (regIdx, regidxfromregname (argument));
}
else
{
/* mov acc.7,c and the likes */
rtrack_data_unset (regIdx);
}
return modified;
}
}
/* mov to psw can change register bank */
if (!strncmp (line, "mov\tpsw,", 8))
{
invalidateAllRx ();
return false;
}
/* tracking dptr */
/* literal number 16 bit */
if (!strncmp (line, "mov\tdptr,#0x", 12))
{
char* s;
int value = strtol (line + 10, &s, 16);
if( s != line + 10 )
{
if (options.verboseAsm)
{
bool foundshortcut = 0;
if ( regs8051[DPH_IDX].rtrack.valueKnown &&
regs8051[DPL_IDX].rtrack.valueKnown &&
(regs8051[DPH_IDX].rtrack.value == (value >> 8)) &&
(regs8051[DPL_IDX].rtrack.value == (value & 0xff)))
{
emitcode (";", "genFromRTrack removed\t%s", line);
foundshortcut = 1;
modified = true;
}
if (!foundshortcut &&
regs8051[DPH_IDX].rtrack.valueKnown &&
regs8051[DPL_IDX].rtrack.valueKnown)
{
/* some instructions are shorter than mov dptr,#0xabcd */
const struct
{
int offset;
const char* inst;
const char* parm;
} reachable[6] =
{
{ 1, "inc", "dptr"},
{ 256, "inc", "dph"},
{-256, "dec", "dph"},
{-255, "inc", "dpl"}, /* if overflow */
{ -1, "dec", "dpl"}, /* if no overflow */
{ 255, "dec", "dpl"} /* if overflow */
};
unsigned int dptr = (regs8051[DPH_IDX].rtrack.value << 8 ) |
regs8051[DPL_IDX].rtrack.value;
unsigned int i;
for (i = 0; i < 6; i++)
{
if (dptr + reachable[i].offset == value)
{
/* check if an overflow would occur */
if ((i == 3) && ((dptr & 0xff) != 0xff)) continue;
if ((i == 4) && ((dptr & 0xff) == 0x00)) continue;
if ((i == 5) && ((dptr & 0xff) != 0x00)) continue;
/* does not occur in regression test mcs51-small */
emitcode (";", "genFromRTrack replaced\t%s", line);
emitcode (reachable[i].inst, "%s", reachable[i].parm);
modified = true;
foundshortcut = 1;
break;
}
};
}
if (!foundshortcut &&
regs8051[DPH_IDX].rtrack.valueKnown &&
(regs8051[DPH_IDX].rtrack.value == (value >> 8)))
{
char s[32];
sprintf (s, "#0x%02x", value & 0xff);
if (s != rtrackGetLit(s))
{
/* does not occur in regression test mcs51-small */
emitcode (";", "genFromRTrack replaced\t%s", line);
emitcode ("mov", "dpl,%s", rtrackGetLit (s));
modified = true;
foundshortcut = 1;
}
}
if (!foundshortcut &&
regs8051[DPL_IDX].rtrack.valueKnown &&
(regs8051[DPL_IDX].rtrack.value == (value & 0xff)))
{
char s[32];
sprintf (s, "#0x%02x", value >> 8);
if (s != rtrackGetLit (s))
{
/* does not occur in regression test mcs51-small */
emitcode (";", "genFromRTrack replaced\t%s", line);
emitcode ("mov", "dph,%s", rtrackGetLit (s));
modified = true;
foundshortcut = 1;
}
}
}
rtrack_data_set_val (DPH_IDX, (unsigned char) (value >> 8));
rtrack_data_set_val (DPL_IDX, (unsigned char) value);
return modified;
}
}
/* literal symbol 16 bit */
else if (!strncmp (line, "mov\tdptr,#", 10))
{
char* s = Safe_alloc (strlen (line) + strlen ("( >> 8)"));
strcat (s, "(");
strcat (s, &line[10]);
strcat (s, " >> 8)");
rtrack_data_set_symbol (DPH_IDX, s);
rtrack_data_set_symbol (DPL_IDX, &line[10]);
Safe_free (s);
return false;
}
else if (!strncmp (line, "mov\tdptr", 8))
{
/* unidentified */
rtrack_data_unset (DPH_IDX);
rtrack_data_unset (DPL_IDX);
return false;
}
/* move direct to symbol */
if (!strncmp (line, "mov\t_", 5) ||
!strncmp (line, "mov\t(", 5))
{
char* argument = strstr (line, ",") + 1;
if (argument && !strncmp (argument, "#0x", 3))
{
char s[8] = {0};
strncpy ((void *)&s, argument, strlen ("#0xab"));
/* could we get it from a, r0..r7? */
if (s != rtrackGetLit (s))
{
int lengthuptoargument = argument - (line + 4);
emitcode (";", "1-genFromRTrack replaced\t%s", line);
emitcode ("mov", "%.*s%s",
lengthuptoargument,
line + 4,
rtrackGetLit (s));
modified = true;
}
}
return modified;
}
/* no tracking of SP, so we do not care */
if (!strncmp (line, "mov\tsp,", 7))
return false;
/* mov to xdata or pdata memory does not change registers */
if (!strncmp (line, "movx\t@", 6))
return false;
/* mov to idata memory might change registers r0..r7
but unless there is a stack problem
compiler generated code does not do idata
writes to 0x00..0x1f? */
if (!strncmp (line, "mov\t@", 5))
{
/* a little too paranoid? */
invalidateAllRx ();
return false;
}
}
/* no tracking of SP */
if (!strncmp (line, "push", 4))
return false;
if (!strncmp (line, "pop\t", 4))
{
int regIdx = regidxfromregname (line + 4);
if (0 <= regIdx)
{
rtrack_data_unset (regIdx);
}
return false;
}
if (!strncmp (line, "inc", 3))
{
if (!strcmp (line, "inc\tdptr"))
{
if (regs8051[DPH_IDX].rtrack.valueKnown &&
regs8051[DPL_IDX].rtrack.valueKnown)
{
int val = (regs8051[DPH_IDX].rtrack.value << 8) | regs8051[DPL_IDX].rtrack.value;
val += 1;
rtrack_data_set_val (DPL_IDX, (unsigned char) val);
rtrack_data_set_val (DPH_IDX, (unsigned char) (val >> 8));
}
else
{
/* not yet handling offset to a symbol. Invalidating. So no inc dptr for:
__xdata char array[4]; array[0] = 0; array[1] = 0; array[2] = 0;
(If an offset to the respective linker segment would be
available then additionally
__xdata int a = 123; __xdata int b = 456; __xdata c= 'a';
could be 4 bytes shorter) */
rtrack_data_unset (DPL_IDX);
rtrack_data_unset (DPH_IDX);
}
return false;
}
if (!strncmp (line, "inc\t", 4))
{
int regIdx = regidxfromregname (line + 4);
if (0 <= regIdx)
{
if (regs8051[regIdx].rtrack.valueKnown)
rtrack_data_set_val (regIdx, (unsigned char) (regs8051[regIdx].rtrack.value + 1));
else
/* explicitely unsetting (could be known by symbol).
not yet handling offset to a symbol. (idata/pdata) */
rtrack_data_unset (regIdx);
return false;
}
}
return false;
}
/* some bit in acc is cleared
MB: I'm too lazy to find out which right now */
if (!strncmp (line, "jbc\tacc", 7))
{
rtrack_data_unset (A_IDX);
return false;
}
/* unfortunately the label typically following these
will cause loss of tracking */
if (!strncmp (line, "jc\t", 3) ||
!strncmp (line, "jnc\t", 4) ||
!strncmp (line, "jb\t", 3) ||
!strncmp (line, "jnb\t", 4) ||
!strncmp (line, "jbc\t", 4))
return false;
/* if branch not taken in "cjne r2,#0x08,somewhere"
r2 is known to be 8 */
if (!strncmp (line, "cjne\t", 5))
{
int regIdx = regidxfromregname (line + 5);
if (0 <= regIdx)
{
char *argument = strstr (line, ",") + 1;
char *s;
int value;
value = strtol (argument + 1, &s, 16);
/* check literal compare to register */
if ((s != argument + 1) && !strncmp (argument, "#0x", 3))
{
rtrack_data_set_val (regIdx, (unsigned char) value);
return false;
}
rtrack_data_unset (regIdx);
}
return false;
}
/* acc eventually known to be zero */
if (!strncmp (line, "jz\t", 3))
return false;
/* acc eventually known to be zero */
if (!strncmp (line, "jnz\t", 4))
{
rtrack_data_set_val (A_IDX, 0x00); // branch not taken
return false;
}
if (!strncmp (line, "djnz\t", 5))
{
int regIdx = regidxfromregname (line + 5);
if (0 <= regIdx)
{
rtrack_data_set_val (regIdx, 0x00); // branch not taken
return false;
}
}
/* only carry bit, so we do not care */
if (!strncmp (line, "setb\tc", 6) ||
!strncmp (line, "clr\tc", 5) ||
!strncmp (line, "cpl\tc", 5))
return false;
/* operations on acc which depend on PSW */
if (!strncmp (line, "addc\ta,", 7)||
!strncmp (line, "subb\ta,", 7)||
!strncmp (line, "da\ta", 4) ||
!strncmp (line, "rlc\ta", 5) ||
!strncmp (line, "rrc\ta", 5))
{
rtrack_data_unset (A_IDX);
return false;
}
/* bitwise operations on acc */
if (!strncmp (line, "setb\ta", 6) ||
!strncmp (line, "clrb\ta", 6))
{
rtrack_data_unset (A_IDX);
return false;
}
/* other operations on acc that can be tracked */
if (!strncmp (line, "add\ta,", 6) ||
!strncmp (line, "anl\ta,", 6) ||
!strncmp (line, "orl\ta,", 6) ||
!strncmp (line, "xrl\ta,", 6) ||
!strcmp (line, "cpl\ta"))
{
if (regs8051[A_IDX].rtrack.valueKnown)
{
if (!strncmp (line, "add\ta,", 6))
{
int regIdx = regidxfromregname (line + 6);
if (0 <= regIdx && regs8051[regIdx].rtrack.valueKnown)
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value + regs8051[regIdx].rtrack.value));
return false;
}
else if (('#' == line[6]) && (0 <= valuefromliteral (line + 7)))
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value + valuefromliteral (line + 7)));
return false;
}
}
if (!strncmp (line, "anl\ta,", 6))
{
int regIdx = regidxfromregname (line + 6);
if (0 <= regIdx && regs8051[regIdx].rtrack.valueKnown)
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value & regs8051[regIdx].rtrack.value));
return false;
}
else if (('#' == line[6]) && (0 <= valuefromliteral (line + 7)))
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value & valuefromliteral (line + 7)));
return false;
}
}
if (!strncmp (line, "orl\ta,", 6))
{
int regIdx = regidxfromregname (line + 6);
if (0 <= regIdx && regs8051[regIdx].rtrack.valueKnown)
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value | regs8051[regIdx].rtrack.value));
return false;
}
else if (('#' == line[6]) && (0 <= valuefromliteral (line + 7)))
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value | valuefromliteral (line + 7)));
return false;
}
}
if (!strncmp (line, "xrl\ta,", 6))
{
int regIdx = regidxfromregname (line + 6);
if (0 <= regIdx && regs8051[regIdx].rtrack.valueKnown)
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value ^ regs8051[regIdx].rtrack.value));
return false;
}
else if (('#' == line[6]) && (0 <= valuefromliteral (line + 7)))
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value ^ valuefromliteral (line + 7)));
return false;
}
}
if (!strcmp (line, "cpl\ta"))
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value ^ 0xff));
return false;
}
rtrack_data_unset (A_IDX);
return false;
}
else
{
rtrack_data_unset (A_IDX);
return false;
}
}
if (!strncmp (line, "dec\t", 4))
{
int regIdx = regidxfromregname (line + 4);
if (0 <= regIdx)
{
if (regs8051[regIdx].rtrack.valueKnown)
rtrack_data_set_val (regIdx, (unsigned char) (regs8051[regIdx].rtrack.value - 1));
/* not handling offset to a symbol. invalidating if needed */
if (NULL != regs8051[regIdx].rtrack.symbol)
rtrack_data_unset (regIdx);
return false;
}
return false;
}
if (!strcmp (line, "clr\ta"))
{
if (regs8051[A_IDX].rtrack.valueKnown && (0 == regs8051[A_IDX].rtrack.value))
{
emitcode (";", "genFromRTrack removed\t%s", line);
modified = true;
}
rtrack_data_set_val (A_IDX, 0);
return modified;
}
if (!strcmp (line, "cpl\ta"))
{
if (regs8051[A_IDX].rtrack.valueKnown)
rtrack_data_set_val (A_IDX, (unsigned char) (~regs8051[A_IDX].rtrack.value));
else
/* in case a holds a symbol */
rtrack_data_unset (A_IDX);
return false;
}
if (!strcmp (line, "rl\ta"))
{
if (regs8051[A_IDX].rtrack.valueKnown)
rtrack_data_set_val (A_IDX, (unsigned char) ((regs8051[A_IDX].rtrack.value<<1) |
(regs8051[A_IDX].rtrack.value>>7)));
else
rtrack_data_unset (A_IDX);
return false;
}
if (!strcmp (line, "rr\ta"))
{
if (regs8051[A_IDX].rtrack.valueKnown)
rtrack_data_set_val (A_IDX, (unsigned char) ((regs8051[A_IDX].rtrack.value>>1) |
(regs8051[A_IDX].rtrack.value<<7)));
else
rtrack_data_unset (A_IDX);
return false;
}
if (!strcmp (line, "swap\ta"))
{
if (regs8051[A_IDX].rtrack.valueKnown)
rtrack_data_set_val (A_IDX, (unsigned char) ((regs8051[A_IDX].rtrack.value>>4) |
(regs8051[A_IDX].rtrack.value<<4)));
else
rtrack_data_unset (A_IDX);
return false;
}
if (!strncmp (line, "mul\t", 4))
{
if (regs8051[A_IDX].rtrack.valueKnown && regs8051[B_IDX].rtrack.valueKnown)
{
unsigned int value = (unsigned int)regs8051[A_IDX].rtrack.value *
(unsigned int)regs8051[B_IDX].rtrack.value;
rtrack_data_set_val (A_IDX, (unsigned char) value);
rtrack_data_set_val (B_IDX, (unsigned char) (value >> 8));
}
else
{
rtrack_data_unset (A_IDX);
rtrack_data_unset (B_IDX);
}
return false;
}
if (!strncmp (line, "div\t", 4))
{
if (regs8051[A_IDX].rtrack.valueKnown && regs8051[B_IDX].rtrack.valueKnown)
{
rtrack_data_set_val (A_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value / regs8051[B_IDX].rtrack.value));
rtrack_data_set_val (B_IDX, (unsigned char) (regs8051[A_IDX].rtrack.value % regs8051[B_IDX].rtrack.value));
}
else
{
rtrack_data_unset (A_IDX);
rtrack_data_unset (B_IDX);
}
return false;
}
/* assuming these library functions have no side-effects */
if (!strncmp (line, "lcall", 5))
{
if (!strcmp (line, "lcall\t__gptrput"))
{
/* invalidate R0..R7 because they might have been changed */
/* MB: too paranoid ? */
//invalidateAllRx();
return false;
}
if (!strcmp (line, "lcall\t__gptrget"))
{
rtrack_data_unset (A_IDX);
return false;
}
if (!strcmp (line, "lcall\t__decdptr"))
{
if (regs8051[DPH_IDX].rtrack.valueKnown &&
regs8051[DPL_IDX].rtrack.valueKnown)
{
int val = (regs8051[DPH_IDX].rtrack.value << 8) | regs8051[DPL_IDX].rtrack.value;
val -= 1;
rtrack_data_set_val (DPL_IDX, (unsigned char) val);
rtrack_data_set_val (DPH_IDX, (unsigned char) (val >> 8));
}
else
{
rtrack_data_unset (DPL_IDX);
rtrack_data_unset (DPH_IDX);
}
return false;
}
/* if callee_saves */
}
if (!strncmp (line, "xch\ta,", 6))
{
/* handle xch from register (r0..r7, dpl, dph, b) */
int regIdx = regidxfromregname (line + 6);
if (0 <= regIdx)
{
void* swap = Safe_malloc (sizeof regs8051[A_IDX].rtrack);
memcpy (swap, ®s8051[A_IDX].rtrack, sizeof regs8051[A_IDX].rtrack);
memcpy (®s8051[A_IDX ].rtrack, ®s8051[regIdx].rtrack, sizeof regs8051[A_IDX].rtrack);
memcpy (®s8051[regIdx].rtrack, swap, sizeof regs8051[A_IDX].rtrack);
Safe_free (swap);
return false;
}
}
/* all others unrecognized, invalidate */
invalidateAll();
return false;
}
/* expects f.e. "#0x01" and returns either "#0x01"
if the value is not known to be within registers
or "a" or "r0".."r7".
(mov a,r7 or add a,r7 need one byte whereas
mov a,#0x01 or add a,#0x01 would take two
*/
char * rtrackGetLit(const char *x)
{
unsigned int i;
char *s;
if (enable != 1)
return (char *)x;
/* was it a numerical literal? */
if (*x == '#')
{
int val = strtol (x+1, &s, 16);
if (x+1 != s)
{
/* try to get from acc */
reg_info *r = ®s8051[A_IDX];
if (r->rtrack.valueKnown &&
r->rtrack.value == val)
{
D(emitcode (";", "genFromRTrack 0x%02x==%s", val, r->name));
return r->name;
}
/* try to get from register R0..R7 */
for (i = 0; i < 8; i++)
{
reg_info *r = ®s8051[rx_num_to_idx(i)];
if (r->rtrack.valueKnown &&
r->rtrack.value == val)
{
D(emitcode (";", "genFromRTrack 0x%02x==%s", val, r->name));
return r->name;
}
}
}
else
{
/* probably a symbolic literal as in "mov r3,#(_i+1)",
not handled... */
}
}
return (char *)x;
}
/* Similar to the above function
As the destination is the accumulator try harder yet and
try to generate the result with arithmetic operations */
int rtrackMoveALit (const char *x)
{
if (enable != 1)
return 0;
/* if it is a literal mov try to get it cheaper */
if ( *x == '#' )
{
reg_info *a = ®s8051[A_IDX];
char *s;
int val = strtol (x+1, &s, 16);
/* was it a numerical literal? */
if (x+1 != s)
{
/* prefer mov a,#0x00 */
if (val == 0 &&
((a->rtrack.valueKnown && a->rtrack.value != 0) ||
!a->rtrack.valueKnown))
{
/* peepholes convert to clr a */
/* (regression test suite is slightly larger if "clr a" is used here) */
emitcode ("mov", "a,#0x00");
return 1;
}
if (a->rtrack.valueKnown)
{
/* already there? */
if (val == a->rtrack.value)
{
D(emitcode (";", "genFromRTrack acc==0x%02x", a->rtrack.value));
return 1;
}
/* can be calculated with an instruction
that does not change flags from acc itself? */
if (val == ((a->rtrack.value+1) & 0xff) )
{
D(emitcode (";", "genFromRTrack 0x%02x==0x%02x+1", val, a->rtrack.value));
emitcode ("inc", "a");
return 1;
}
if (val == ((a->rtrack.value-1) & 0xff) )
{
D(emitcode (";", "genFromRTrack 0x%02x==0x%02x-1", val, a->rtrack.value));
emitcode ("dec", "a");
return 1;
}
if (val == ((~a->rtrack.value) & 0xff) )
{
D(emitcode (";", "genFromRTrack 0x%02x==~0x%02x", val, a->rtrack.value));
emitcode ("cpl", "a");
return 1;
}
if (val == (((a->rtrack.value>>1) |
(a->rtrack.value<<7)) & 0xff))
{
D(emitcode (";", "genFromRTrack 0x%02x==rr(0x%02x)", val, a->rtrack.value));
emitcode ("rr", "a");
return 1;
}
if (val == (((a->rtrack.value<<1) |
(a->rtrack.value>>7)) & 0xff ))
{
D(emitcode (";", "genFromRTrack 0x%02x==rl(0x%02x)", val, a->rtrack.value));
emitcode ("rl", "a");
return 1;
}
if (val == ( ((a->rtrack.value & 0x0f)<<4) |
((a->rtrack.value & 0xf0)>>4) ))
{
D(emitcode (";", "genFromRTrack 0x%02x==swap(0x%02x)", val, a->rtrack.value));
emitcode ("swap", "a");
return 1;
}
/* Decimal Adjust Accumulator (da a) changes flags so not used */
}
{
unsigned int i;
char *ptr= rtrackGetLit(x);
if (x != ptr)
{
/* could get from register, fine */
emitcode ("mov", "a,%s", ptr);
return 1;
}
/* not yet giving up - try to calculate from register R0..R7 */
for (i = 0; i < 8; i++)
{
reg_info *r = ®s8051[rx_num_to_idx(i)];
if (a->rtrack.valueKnown && r->rtrack.valueKnown)
{
/* calculate with a single byte instruction from R0..R7? */
if (val == (a->rtrack.value | r->rtrack.value))
{
D(emitcode (";", "genFromRTrack 0x%02x==0x%02x|0x%02x",
val, a->rtrack.value, r->rtrack.value));
emitcode ("orl", "a,%s",r->name);
return 1;
}
if (val == (a->rtrack.value & r->rtrack.value))
{
D(emitcode (";", "genFromRTrack 0x%02x==0x%02x&0x%02x",
val, a->rtrack.value, r->rtrack.value));
emitcode ("anl", "a,%s", r->name);
return 1;
}
if (val == (a->rtrack.value ^ r->rtrack.value))
{
D(emitcode (";", "genFromRTrack 0x%02x==0x%02x^0x%02x",
val, a->rtrack.value, r->rtrack.value));
emitcode ("xrl", "a,%s", r->name);
return 1;
}
/* changes flags (does that matter?)
if (val == (a->rtrack.value + r->rtrack.value))
{
D(emitcode (";", "genFromRTrack 0x%02x=0x%02x+%0x02x",
val, a->rtrack.value, r->rtrack.value));
emitcode ("add", "a,%s",r->name);
return 1;
}
so not used */
}
}
}
}
}
return 0;
}
/* Loads dptr with symbol (if needed)
*/
void rtrackLoadDptrWithSym (const char *x)
{
if (enable != 1)
{
emitcode ("mov", "dptr,#%s", x);
return;
}
if (regs8051[DPL_IDX].rtrack.symbol &&
regs8051[DPH_IDX].rtrack.symbol)
{
/* rtrack.symbol for dph should look like "(something >> 8)" */
if ((!strcmp (x, regs8051[DPL_IDX].rtrack.symbol) &&
!strncmp (x, regs8051[DPH_IDX].rtrack.symbol + 1, strlen (x) ) &&
!strncmp (" >> 8)", regs8051[DPH_IDX].rtrack.symbol + 1 + strlen (x), 6)))
{
/* dptr already holds the symbol */
D(emitcode (";", "genFromRTrack dptr==#%s",x));
return;
}
}
emitcode ("mov", "dptr,#%s", x);
}
#if 0
/* Loads index registers R0, R1 with symbol (if needed)
*
* R0, R1 index registers are already handled in gen.c (see AOP_INPREG)
*/
void rtrackLoadR0R1WithSym (const char *reg, const char *x)
{
int regNum, regIdx;
if (enable != 1)
{
emitcode ("mov", "%s,#%s", reg, x );
return;
}
regNum = reg[1] - '0';
if (regNum == 0 || regNum == 1)
{
regIdx = rx_num_to_idx(regNum);
if ((NULL != regs8051[regIdx].rtrack.symbol) && !strcmp (x, regs8051[regIdx].rtrack.symbol))
{
/* register already holds the symbol */
D(emitcode (";", "genFromRTrack %s=#%s",reg,x));
return;
}
}
emitcode ("mov", "%s,#%s", reg, x );
}
#endif
|