forked from Arakula/dasmfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Disassembler.cpp
1507 lines (1377 loc) · 48.3 KB
/
Disassembler.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
/***************************************************************************
* dasmfw -- Disassembler Framework *
* *
* 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., 675 Mass Ave, Cambridge, MA 02139, USA. *
***************************************************************************/
/*****************************************************************************/
/* Disassembler.cpp : disassembler base class functionality */
/*****************************************************************************/
#include "Disassembler.h"
/*****************************************************************************/
/* GetHex : retrieves a hex value in given length from file */
/*****************************************************************************/
adr_t GetHex(FILE *f, int nChars, uint8_t *pchks)
{
adr_t out = 0;
for (int i = 0; i < nChars; i++)
{
int c = fgetc(f);
if (c == EOF)
return NO_ADDRESS;
if ((c >= 'a') && (c <= 'f'))
c -= ('a' - 'A');
if ((!((c >= '0') && (c <= '9'))) &&
(!((c >= 'A') && (c <= 'F'))))
{
ungetc(c, f);
return NO_ADDRESS;
}
// ASCII-based. Needs rework for other encodings.
c -= '0';
if (c > 9)
c -= 7;
if (pchks)
*pchks += (i & 1) ? c : (c << 4);
out = out * 16 + c;
}
return out;
}
/*****************************************************************************/
/* GetBasicDisassemblyFlags : returns basic disassembly flag set */
/*****************************************************************************/
uint32_t GetBasicDisassemblyFlags(MemAttribute *pAttr, uint8_t mem, Label *plbl)
{
// should be sufficient for most, if not all, disassemblers
uint32_t wf = 0;
MemoryType mt = pAttr ? pAttr->GetMemType() : Untyped;
if (!pAttr ||
mt == Code ||
!pAttr->IsUsed())
return 0;
MemAttribute::Display display = pAttr->GetDisplay();
wf = pAttr->cellSize | SHMF_DATA; /* assemble flags for data byte */
if (pAttr->GetSize() != 1 ||
#if RB_VARIANT
(display != MemAttribute::Char ||
#else
(display == MemAttribute::Binary ||
#endif
mt == Const))
wf |= SHMF_NOTXT;
if (mt == Bss)
wf |= SHMF_RMB;
else if (!(wf & SHMF_NOTXT))
{
// NB: this is totally ASCII-centric, but as long as we don't
// have to deal with other encodings, it should be OK.
if ((mem >= 0x20) && (mem <= 0x7e) &&
(mem != '&') && (mem != '\"'))
wf |= SHMF_TXT;
}
if (pAttr->GetBreakBefore())
wf |= SHMF_BREAK;
if (plbl && plbl->IsUsed())
{
// labels containing '+' or '-' are no real labels; they are
// references to another label (e.g., "OLBL+4")
string const &s = plbl->GetText();
if (s.find('-') == s.npos && s.find('+') == s.npos)
wf |= SHMF_BREAK;
}
return wf;
}
/*===========================================================================*/
/* Disassembler class members */
/*===========================================================================*/
// determine the endianness of the machine the disassembler runs on
static const uint16_t chkEnd = (((uint16_t)Disassembler::BigEndian) << 8) |
(uint16_t)Disassembler::LittleEndian;
const Disassembler::Endian Disassembler::prgEndian = (Disassembler::Endian)*((uint8_t *)&chkEnd);
/*****************************************************************************/
/* Disassembler : constructor */
/*****************************************************************************/
Disassembler::Disassembler(Application *pApp)
: pApp(pApp)
{
begin = end = load = NO_ADDRESS;
offset = 0;
commentStart = ";";
#if RB_VARIANT
labelDelim = ":";
#else
labelDelim = "";
#endif
defaultDisplay = MemAttribute::Hex;
pbase = 16;
disassemblyFlagMask = (uint32_t)-1;
bLoadLabel = true;
bSetSysVec = true;
bMultiLabel = false;
bAutoLabel = false;
bPIC = false;
nMnemoUpper = -1; // don't care
// set up options table
// base class uses one generic option setter/getter pair (not mandatory)
AddOption("pbase", "{number}\tdefault base for parsing numbers",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("defdisp", "{bin|char|oct|dec|hex}\tdefault number output format",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("begin", "{addr}\tstart disassembly address",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("end", "{addr}\tend disassembly address",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("offset", "{addr}\taddress to load program",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("cchar", "{char}\tcomment start character",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("ldchar", "{char}\tlabel delimiter character",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("loadlabel", "{off|on}\tuse entry point label",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("multilabel", "{off|on}\tallow multiple labels for an address",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("autolabel", "{off|on}\tset labels based on previous text label",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("sysvec", "{off|on}\tset system vector labels",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("pic", "{off|on}\tdiasassemble as position-indpendent code",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
AddOption("upmnemo", "{off|on}\tdiasassemble mnemonics in UPPERCASE format",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
}
/*****************************************************************************/
/* ~Disassembler : destructor */
/*****************************************************************************/
Disassembler::~Disassembler()
{
for (int i = 0; i < GetOptionCount(); i++)
delete options[i];
}
/*****************************************************************************/
/* Setup : Setup disassembler after construction */
/*****************************************************************************/
bool Disassembler::Setup()
{
bool bOK = true;
busnames.resize(GetBusCount());
memory.resize(GetBusCount());
memattr.resize(GetBusCount());
Labels.resize(GetBusCount());
DefLabels.resize(GetBusCount());
Relatives.resize(GetBusCount());
Phases.resize(GetBusCount());
busorder.resize(GetBusCount());
busbits.resize(GetBusCount());
busnames[BusCode] = "Code";
// default to basic memory attribute handlers
for (int bus = 0; bus < GetBusCount(); bus++)
{
memattr[bus] = CreateAttributeHandler();
busorder[bus] = bus;
bOK &= !!memattr[bus];
RecalcBusBits(bus);
}
return bOK;
}
/*****************************************************************************/
/* GetBusID : return a bus ID based on its name */
/*****************************************************************************/
int Disassembler::GetBusID(string busname)
{
bool numeric = !!busname.size();
for (string::size_type s = 0; s < busname.size(); s++)
if (busname[s] < '0' || busname[s] > '9')
{
numeric = false;
break;
}
if (numeric)
{
int i = atoi(busname.c_str());
if (i >= 0 && i < GetBusCount())
return i;
}
else
{
busname = lowercase(busname);
for (int i = GetBusCount() - 1; i >= 0; i--)
if (busname == lowercase(GetBusName(i)))
return i;
}
return -1;
}
/*****************************************************************************/
/* AddOption : add an option and its handler to the options array */
/*****************************************************************************/
bool Disassembler::AddOption
(
string name,
string help,
PSetter setter,
PGetter getter
)
{
// allow overriding of base class option handlers
string lname(lowercase(name));
for (int i = 0; i < GetOptionCount(); i++)
if (GetOptionName(i) == lname)
{
options[i]->help = help;
options[i]->setter = setter;
options[i]->getter = getter;
return true;
}
// append new option
try
{
OptionHandler *p = new OptionHandler;
if (p)
{
p->name = lname;
p->help = help;
p->setter = setter;
p->getter = getter;
options.push_back(p);
}
else return false; // for very old compilers
}
catch(...)
{
return false;
}
return true;
}
/*****************************************************************************/
/* FindOption : returns index for an option or -1 */
/*****************************************************************************/
int Disassembler::FindOption(string name)
{
string lname(lowercase(name));
for (int i = 0; i < GetOptionCount(); i++)
if (name == options[i]->name)
return i;
return -1;
}
/*****************************************************************************/
/* RemoveOption : removes an option if it's there */
/*****************************************************************************/
bool Disassembler::RemoveOption(string name)
{
int i = FindOption(name);
if (i < 0)
return false;
options.erase(options.begin() + i);
return true;
}
/*****************************************************************************/
/* SetOption : sets a disassembler option */
/*****************************************************************************/
int Disassembler::SetOption(string name, string value)
{
string lname(lowercase(name));
for (int i = 0; i < GetOptionCount(); i++)
if (name == options[i]->name)
return ((*this).*(options[i]->setter))(name, value);
return 0;
}
/*****************************************************************************/
/* GetOption : retrieves a disassembler option */
/*****************************************************************************/
string Disassembler::GetOption(string name)
{
string lname(lowercase(name));
for (int i = 0; i < GetOptionCount(); i++)
if (name == options[i]->name)
return ((*this).*(options[i]->getter))(name);
return "";
}
/*****************************************************************************/
/* DisassemblerSetOption : disassembler base class option handler */
/*****************************************************************************/
int Disassembler::DisassemblerSetOption(string lname, string value)
{
// this could be expanded ... "0x" or "$" header come to mind...
string lvalue(lowercase(value));
adr_t ivalue = (adr_t)strtoul(value.c_str(), NULL, 10);
adr_t avalue;
String2Number(value, avalue);
bool bnValue = true; /* default to "on" */
bool bIsBool = ParseBool(value, bnValue);
if (lname == "pbase" &&
ivalue >= 2 && ivalue <= 16)
pbase = ivalue;
else if (lname == "defdisp")
{
MemAttribute::Display disp = MemAttribute::DefaultDisplay;
if (lvalue == "bin")
disp = MemAttribute::Binary;
else if (lvalue == "char")
disp = MemAttribute::Char;
else if (lvalue == "oct")
disp = MemAttribute::Octal;
else if (lvalue == "dec")
disp = MemAttribute::Decimal;
else if (lvalue == "hex")
disp = MemAttribute::Hex;
if (disp != MemAttribute::DefaultDisplay)
SetDisplay(disp);
}
else if (lname == "begin" &&
avalue >= GetLowestCodeAddr() &&
avalue <= GetHighestCodeAddr())
begin = avalue;
else if (lname == "end" &&
avalue >= GetLowestCodeAddr() &&
avalue <= GetHighestCodeAddr())
end = avalue;
else if (lname == "offset" &&
avalue >= GetLowestCodeAddr() &&
avalue <= GetHighestCodeAddr())
offset = avalue;
else if (lname == "cchar")
commentStart = value;
else if (lname == "ldchar")
labelDelim = value;
else if (lname == "loadlabel")
{
bLoadLabel = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "multilabel")
{
bMultiLabel = bnValue;
for (int i = 0; i < GetBusCount(); i++)
Labels[i].SetMultipleDefs(bMultiLabel);
return bIsBool ? 1 : 0;
}
else if (lname == "autolabel")
{
bAutoLabel = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "sysvec")
{
bSetSysVec = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "pic")
{
bPIC = bnValue;
return bIsBool ? 1 : 0;
}
else if (lname == "upmnemo")
{
if (lvalue == "default")
nMnemoUpper = -1;
else
nMnemoUpper = bnValue;
return bIsBool ? 1 : 0;
}
else
return 0; /* only option consumed */
return 1; /* option + value consumed */
}
/*****************************************************************************/
/* DisassemblerGetOption : disassembler base class option retrieval */
/*****************************************************************************/
string Disassembler::DisassemblerGetOption(string lname)
{
string oval;
if (lname == "pbase") oval = sformat("%ld", pbase);
else if (lname == "defdisp")
{
#if 0
AddOption("defdisp", "{bin|char|dec|oct|hex|}\tdefault number output format",
&Disassembler::DisassemblerSetOption,
&Disassembler::DisassemblerGetOption);
#endif
#ifdef _DEBUG
MemAttribute::Display disp = GetDisplay();
#endif
switch (GetDisplay())
{
case MemAttribute::Binary : oval = "bin"; break;
case MemAttribute::Char : oval = "char"; break;
case MemAttribute::Octal : oval = "oct"; break;
case MemAttribute::Decimal : oval = "dec"; break;
case MemAttribute::Hex : oval = "hex"; break;
default : break; // keep gcc happy
}
}
else if (lname == "begin") oval = (begin == NO_ADDRESS) ? "-1" : sformat("0x%lx", begin);
else if (lname == "end") oval = (end == NO_ADDRESS) ? "-1" : sformat("0x%lx", end);
else if (lname == "offset") oval = (offset == NO_ADDRESS) ? "-1" : sformat("0x%lx", offset);
else if (lname == "cchar") oval = commentStart;
else if (lname == "ldchar") oval = labelDelim;
else if (lname == "loadlabel") oval = bLoadLabel ? "on" : "off";
else if (lname == "multilabel") oval = bMultiLabel ? "on" : "off";
else if (lname == "autolabel") oval = bAutoLabel ? "on" : "off";
else if (lname == "sysvec") oval = bSetSysVec ? "on" : "off";
else if (lname == "pic") oval = bPIC ? "on" : "off";
else if (lname == "upmnemo") oval =
(nMnemoUpper < 0) ? "default" :
(nMnemoUpper > 0) ? "on" :
"off";
return oval;
}
/*****************************************************************************/
/* IsForwardRef : returns whether label at given address is a forward ref */
/*****************************************************************************/
bool Disassembler::IsForwardRef
(
adr_t value,
bool bUseLabel,
adr_t addr,
int bus
)
{
// if not using labels, it can be a deflabel at best, and these are always
// output before the code
if (!bUseLabel)
return false;
bool bForwardRef = false;
adr_t relative = GetRelative(addr, bus);
Label *pLbl;
MemoryType lblType;
if (relative)
{
pLbl = FindLabel(relative, Untyped, bus);
lblType = pLbl ? pLbl->GetType() : Untyped;
bForwardRef |=
((lblType == Code || lblType == Data || lblType == Bss) &&
relative >= addr);
}
adr_t Wrel = (value + relative);
adr_t hiaddr = GetHighestBusAddr(bus) + 1;
adr_t WrelMod = (hiaddr) ? Wrel % hiaddr : Wrel;
pLbl = FindLabel(Wrel, Untyped, bus);
lblType = pLbl ? pLbl->GetType() : Untyped;
bForwardRef |=
((lblType == Code || lblType == Data || lblType == Bss) &&
Wrel >= addr);
if (!pLbl && Wrel != WrelMod)
{
pLbl = FindLabel(WrelMod, Untyped, bus);
lblType = pLbl ? pLbl->GetType() : Untyped;
bForwardRef |=
((lblType == Code || lblType == Data || lblType == Bss) &&
WrelMod >= addr);
}
return bForwardRef;
}
/*****************************************************************************/
/* Label2String : converts a value to a (label) string */
/*****************************************************************************/
string Disassembler::Label2String
(
adr_t value,
int nDigits,
bool bUseLabel,
adr_t addr,
int bus
)
{
string sOut;
adr_t relative = GetRelative(addr, bus);
bool relConst = GetRelConst(addr, bus);
adr_t Wrel = (value + relative);
adr_t hiaddr = GetHighestBusAddr(bus) + 1;
adr_t WrelMod = (hiaddr) ? Wrel % hiaddr : Wrel;
// % might not be the best choice under all circumstances, but works for me...
// NB: this always uses the LAST found label for this address.
// There's no way to find out which should be used for multiples.
Label *pLbl = NULL;
if (bUseLabel)
{
pLbl = FindLabel(Wrel, Untyped, bus);
if (!pLbl && Wrel != WrelMod)
pLbl = FindLabel(WrelMod, Untyped, bus);
}
// DefLabel is independent of bUseLabel and is used if no normal label is there
#if 1
if (!pLbl)
pLbl = FindLabel(WrelMod, Const, bus);
#endif
string sLabel;
if (pLbl) /* get label name */
sLabel = pLbl->GetText();
MemoryType memType = pLbl ? pLbl->GetType() : Untyped;
if (pLbl && memType == Untyped)
memType = GetMemType(WrelMod, bus);
if (memType == Const || memType == Bss)
memType = Data;
/* if there and absolute */
if (Wrel == value && sLabel.size())
return sLabel; /* return it */
if (sLabel.size())
sOut = sLabel;
else if (bUseLabel && memType == Code)
sOut = UnnamedLabel(WrelMod, true, bus);
else if (bUseLabel && memType == Data)
sOut = UnnamedLabel(WrelMod, false, bus);
else // no label - use ORIGINAL value
sOut = Number2String(relative ? Wrel : value, nDigits, addr, bus);
if (relative) /* if it's relative addressing */
{
string sAdd("-");
bool bInvert = true;
int32_t nDiff = Wrel - value; /* get difference */
/* get base name */
bool bRelUseLabel = (relConst) ? false : bUseLabel;
pLbl = (bRelUseLabel) ? FindLabel(relative, Untyped, bus) : NULL;
// DefLabel overrides normal labels and is independent of bUseLabel
if (!pLbl)
pLbl = FindLabel(relative, Const, bus);
sLabel = (pLbl) ? pLbl->GetText() : "";
if (sLabel.size())
sAdd += sLabel;
else if (bRelUseLabel && IsCLabel(relative, bus))
sAdd += UnnamedLabel(relative, true, bus);
else if (bRelUseLabel && IsDLabel(relative, bus))
sAdd += UnnamedLabel(relative, false, bus);
else
{
if (nDiff < 0) /* if negative displacement */
{
sAdd = "+"; /* negative*negative is positive... */
bInvert = false; /* so invert the sign */
/* and make the number positive */
relative = (adr_t) (-((int32_t)relative));
}
sAdd += Number2String(relative, nDigits, addr, bus);
}
if (bInvert) /* if inverting necessary, */
{
string::iterator i = sAdd.begin();
i++;
while (i != sAdd.end()) /* invert signs! */
{
if (*i == '+')
*i = '-';
else if (*i == '-')
*i = '+';
i++;
}
}
sOut += sAdd;
}
return sOut;
}
/*****************************************************************************/
/* AddLabel : adds (or replaces) a label */
/*****************************************************************************/
bool Disassembler::AddLabel
(
adr_t addr,
MemoryType memType,
string sLabel,
bool bUsed,
int bus
)
{
#if 1
LabelArray::iterator it;
Label *pLbl = GetFirstLabel(addr, it, Untyped, bus);
if (pLbl)
{
bool bHasTxt = !!sLabel.size();
#if 0
bool bMulti = Labels[bus].HasMultipleDefs();
#endif
bool bInsert = true /*!bMulti*/;
do
{
MemoryType oType = pLbl->GetType();
#if 0
if (bMulti)
{
#endif
bool oHasTxt = pLbl->HasText();
if ((bHasTxt && sLabel == pLbl->GetText()) ||
((!bHasTxt || !oHasTxt) && oType != Const))
{
if (!oHasTxt && bHasTxt)
pLbl->SetText(sLabel);
// This should deal with "used xxxx / label xxxx txt"
if (!bHasTxt || !pLbl->IsUsed())
pLbl->SetUsed(bUsed); /* just set new used state */
if (oType == Untyped && /* and new type if none set yet */
memType != Const &&
oType != memType)
pLbl->SetType(memType);
bInsert = false;
}
#if 0
}
else
{
// In theory, this would also work in Label::CopyUnset(), like this:
// if (other.memType != Untyped) memType = other.memType;
// but since it's more LabelArray- than Label-specific, I'll leave it here
// override incoming type if already set
if (oType != Untyped /* && oType != memType */)
memType = oType;
}
#endif
pLbl = GetNextLabel(addr, it, Untyped, bus);
} while (pLbl);
if (!bInsert)
return true;
}
#endif
Labels[bus].insert(new Label(addr, memType, sLabel, bUsed),
true,
(memType == Const));
return true;
}
/*****************************************************************************/
/* AddRelativeLabel : add label for addr OR the relative addresses */
/*****************************************************************************/
// TODO: is that enough? Or does it need 2 busses (for addr and for at)?
bool Disassembler::AddRelativeLabel
(
adr_t addr,
adr_t at,
MemoryType memType,
bool bUsed,
int bus,
adr_t craddr, /* cref address */
int crbus /* cref bus */
)
{
(void)bUsed;
adr_t relative = GetRelative(at, bus);
adr_t addrRel = addr + relative;
adr_t hiaddr = GetHighestBusAddr(bus) + 1;
adr_t addrRelMod = (hiaddr) ? addrRel % hiaddr : addrRel;
bool rc;
if (addrRelMod != addr)
{
#if 0
// add relative if it is not defined.
if (AddLabel(relative, memType, "", true, bus) &&
#else
// don't add relative if it is not defined. If not, it's a constant.
if (FindLabel(relative, Untyped, bus) &&
#endif
craddr != NO_ADDRESS)
SetLabelUsed(relative, memType, bus, craddr, crbus);
#if 0
// add label in any case
rc = AddLabel(addrRelMod, memType, "", true, bus);
#else
// don't add relative if it is not defined. If not, it's a constant.
// If rel is set but neither label is there, outputs a funny addition...
rc = !!FindLabel(addrRelMod, Untyped, bus);
#endif
if (rc && craddr != NO_ADDRESS)
SetLabelUsed(addrRelMod, memType, bus, craddr, crbus);
}
else
{
rc = AddLabel(addr, memType, "", true, bus);
if (rc && craddr != NO_ADDRESS)
SetLabelUsed(addr, memType, bus, craddr, crbus);
}
return rc;
}
/*****************************************************************************/
/* FindLabel : find label of a given address and type */
/*****************************************************************************/
Label *Disassembler::FindLabel
(
adr_t addr,
MemoryType memType,
int bus
)
{
LabelArray::iterator it;
// "Untyped" really means "anything but Const" -
// Const is reserved for DefLabels, which have to be searched with that type
// search rules:
// .) if searching for Untyped, return last non-Const
// .) if searching for Const, return last Const
// .) if searching for Code/Data, return last matching or Untyped
Label *found = NULL;
Label *lbl = Labels[bus].GetFirst(addr, it, memType);
while (lbl)
{
switch (memType)
{
case Const :
if (lbl->IsConst())
#if 1
// return LAST matching label (single-def-compatibe)
found = lbl;
#else
// return FIRST matching label
return lbl;
#endif
break;
default :
if (!lbl->IsConst())
#if 1
// return LAST matching label (single-def-compatibe)
found = lbl;
#else
// return FIRST matching label
return lbl;
#endif
break;
}
lbl = Labels[bus].GetNext(addr, it, memType);
}
return found;
}
/*****************************************************************************/
/* FindPrevNamedLabel : find previous named label for address and type */
/*****************************************************************************/
Label *Disassembler::FindPrevNamedLabel
(
adr_t addr,
MemoryType memType,
int bus
)
{
LabelArray::iterator it;
// "Untyped" really means "anything but Const" -
// Const is reserved for DefLabels, which have to be searched with that type
// search rules:
// .) if searching for Untyped, return last non-Const
// .) if searching for Const, return last Const
// .) if searching for Code/Data, return last matching or Untyped
Label *found = NULL;
Label *lbl = Labels[bus].GetPrevNamed(addr, it, memType);
while (lbl)
{
switch (memType)
{
case Const :
if (lbl->IsConst())
#if 1
// return LAST matching label (single-def-compatibe)
found = lbl;
#else
// return FIRST matching label
return lbl;
#endif
break;
// case Untyped :
default :
if (!lbl->IsConst())
#if 1
// return LAST matching label (single-def-compatibe)
found = lbl;
#else
// return FIRST matching label
return lbl;
#endif
break;
}
lbl = Labels[bus].GetNext(addr, it, memType);
if (found && lbl && (lbl->GetAddress() != found->GetAddress()))
break;
}
return found;
}
/*****************************************************************************/
/* ResolveLabels : resolve all XXXXXXXX+/-nnnn and definition labels */
/*****************************************************************************/
bool Disassembler::ResolveLabels(int bus)
{
for (int i = GetLabelCount(bus) - 1; i >= 0; i--)
{
Label *pLbl = LabelAt(i, bus);
string s = pLbl->GetText();
string::size_type p = s.find_first_of("+-");
if (!pLbl->IsUsed())
continue;
// first, check for +/-nnnn offset
adr_t offs;
if (p != s.npos &&
// String2Number(s.substr(p + 1), offs))
sscanf(s.substr(p + 1).c_str(), "%d", &offs) == 1)
{
if (s[p] == '+') offs = (adr_t)(-(int32_t)offs);
pLbl->SetUsed(false);
adr_t addaddr = pLbl->GetAddress() + offs;
AddLabel(addaddr, pLbl->GetType(),
s.substr(0, p), true, bus);
// copy all references to the base label
Label *pBaseLbl = FindLabel(addaddr, pLbl->GetType(), bus);
if (pBaseLbl)
{
for (size_t refi = 0; refi < pLbl->RefCount(); refi++)
{
AddrBus &ref = pLbl->GetRef(refi);
pBaseLbl->SetUsed(true, ref.addr, ref.bus);
}
}
// this might have caused an insertion, so restart here
i++;
continue;
}
// then, resolve DefLabels without value
if (pLbl->IsConst())
{
adr_t addr = pLbl->GetAddress();
MemoryType memType = GetMemType(addr, bus);
int sz = GetCellSize(addr, bus);
if (memType != Untyped && memType != Bss && !IsFloat(addr, bus))
{
if (s.size() > 0)
{
adr_t value = GetTypedInt(addr, bus);
// insert with last found value
AddDefLabel(addr,
s,
Number2String(value, sz * 2, addr, bus),
Const, bus);
}
}
}
}
return true;
}
/*****************************************************************************/
/* DefLabel2String : DefLabel or numeric constant to string */
/*****************************************************************************/
string Disassembler::DefLabel2String
(
adr_t value,
int nDigits,
adr_t addr,
int bus
)
{
string svalue = Number2String(value, nDigits, addr, bus);
// NB: this always uses the FIRST found label for this address.
// There's no way to find out which should be used for multiples.
Label *pLabel = FindLabel(addr, Const, bus);
if (pLabel)
{
string dname = pLabel->GetText();
if (DefLabels[bus].Find(dname))
svalue = dname;
}
return svalue;
}
/*****************************************************************************/
/* AutoLabel2String : automatic label generation based on previous label */
/*****************************************************************************/
string Disassembler::AutoLabel2String(adr_t addr, bool bCode, int bus)
{
(void)bCode; // unused ATM
Label *pLabel = FindPrevNamedLabel(addr, Untyped /* Const */, bus);
if (pLabel)
{
adr_t off = addr - pLabel->GetAddress();
return sformat("O%x_%s", off, pLabel->GetText().c_str());
}
return "";
}
/*****************************************************************************/
/* GetTypedInt : get memory from given address defined by cell type/size */
/*****************************************************************************/
adr_t Disassembler::GetTypedInt(adr_t addr, int bus)
{
MemoryType memType = GetMemType(addr, bus);
int sz = GetCellSize(addr, bus);
if (memType == Untyped || IsFloat(addr, bus)) return 0;
bool bSigned = IsSigned(addr, bus);
switch (sz)
{