-
Notifications
You must be signed in to change notification settings - Fork 4
/
Label.h
540 lines (492 loc) · 17.7 KB
/
Label.h
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
/***************************************************************************
* 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. *
***************************************************************************/
/*****************************************************************************/
/* Label.h : definitions for the Label classes */
/*****************************************************************************/
#ifndef __Label_h_defined__
#define __Label_h_defined__
#include "dasmfw.h"
/*****************************************************************************/
/* MemoryType : the various types of memory that may occur in a module */
/*****************************************************************************/
enum MemoryType
{
Untyped, /* unknown type */
Code, /* Code */
Data, /* initialized data */
Const, /* initialized constant data */
Bss, /* uninitialized data */
IOPort, /* I/O Port address if separate */
};
/*****************************************************************************/
/* AddrType : a basic address / type combination */
/*****************************************************************************/
class AddrType
{
public:
AddrType(adr_t addr = 0, MemoryType memType = Code)
: addr(addr), memType(memType)
{ }
virtual ~AddrType() { }
bool operator<(const AddrType &other)
{
if (addr != other.addr)
return addr < other.addr;
// allow "Untyped" wildcards
MemoryType imt = (memType == Untyped) ? other.memType : memType;
MemoryType omt = (other.memType == Untyped) ? imt : other.memType;
return imt < omt;
}
bool operator<=(const AddrType &other)
{
if (addr != other.addr)
return addr < other.addr;
// allow "Untyped" wildcards
MemoryType imt = (memType == Untyped) ? other.memType : memType;
MemoryType omt = (other.memType == Untyped) ? imt : other.memType;
return imt <= omt;
}
bool operator==(const AddrType &other)
{
if (addr != other.addr)
return false;
// allow "Untyped" wildcards
MemoryType imt = (memType == Untyped) ? other.memType : memType;
MemoryType omt = (other.memType == Untyped) ? imt : other.memType;
return imt == omt;
}
bool operator!=(const AddrType &other) { return !operator==(other); }
bool operator>=(const AddrType &other) { return !operator<(other); }
bool operator>(const AddrType &other) { return !operator<=(other); }
// Attributes
public:
adr_t GetAddress() const { return addr; }
void SetAddress(adr_t newAddr = 0) { addr = newAddr; }
// Label Type
MemoryType GetType() const { return memType; }
void SetType(MemoryType newType = Untyped) { memType = newType; }
bool IsCode() { return memType == Code; }
bool IsData() { return memType == Data || memType == Const; }
bool IsConst() { return memType == Const; }
bool IsBss() { return memType == Bss; }
bool IsIOPort() { return memType == IOPort; }
void SetCode() { memType = Code; }
void SetData() { memType = Data; }
void SetConst() { memType = Const; }
void SetBss() { memType = Bss; }
void SetIOPort() { memType = IOPort; }
void CopyUnset(const AddrType &other)
{
if (addr == NO_ADDRESS) addr = other.addr;
if (memType == Untyped) memType = other.memType;
}
protected:
adr_t addr;
MemoryType memType;
};
/*****************************************************************************/
/* AddrTypeArray : array of address/type-sorted structures */
/*****************************************************************************/
class AddrTypeArray : public vector<AddrType*>
{
public:
AddrTypeArray(bool bMultipleDefs = false)
: bMultipleDefs(bMultipleDefs) { }
virtual ~AddrTypeArray(void)
{ clear(); }
void SetMultipleDefs(bool bOn = true) { bMultipleDefs = bOn; }
bool HasMultipleDefs() { return bMultipleDefs; }
public:
void clear()
{
for (const_iterator cit = begin();
cit != end();
cit++)
{
AddrType *p = *cit;
delete p;
}
vector<AddrType*>::clear();
}
// binary search in sorted array
iterator find(const AddrType &l, bool bTypeMatch = false)
{
int lo, hi = size() - 1, mid;
// little optimization if definitely outside range
if (empty() || *at(0) > l || *at(hi) < l)
return end();
lo = 0;
while (lo <= hi)
{
mid = (hi + lo) / 2;
if (*at(mid) < l)
lo = mid + 1;
else if (*at(mid) > l)
hi = mid - 1;
else
{
// override "Untyped" if specially requested
if (bTypeMatch && (int)at(mid)->GetType() != (int)l.GetType())
{
if (at(mid)->GetType() < l.GetType())
lo = mid + 1;
else
hi = mid - 1;
}
else
{
// return 1st match for multiple definitions
while (mid > 0 && *at(mid - 1) == l)
mid--;
return begin() + mid;
}
}
}
return end();
}
// binary search in sorted array, returning predecessor if no direct match
iterator findimprec(const AddrType &l)
{
int lo, hi = size() - 1, mid;
// little optimization if definitely outside range
if (empty() || *at(0) > l)
return end();
if (*at(hi) < l)
lo = hi;
else
lo = 0;
while (lo <= hi)
{
mid = (hi + lo) / 2;
if (*at(mid) < l)
lo = mid + 1;
else if (*at(mid) > l)
hi = mid - 1;
else
break;
}
if (hi < lo)
hi = mid = lo;
while ((*at(mid) > l) && mid)
mid--;
return (*at(mid) > l) ? end() : begin() + mid;
}
// convenience - find code and/or data labels
iterator find(adr_t addr, MemoryType memType = Untyped, bool bTypeMatch = false)
{ return find(AddrType(addr, memType), bTypeMatch); }
// insert into address/type-sorted array
iterator insert(AddrType *pNewEl, bool bAfter = true, bool bTypeMatch = false)
{
iterator it;
#if 0
// Copying pre-existing element's data is done by template below
#endif
int lo, max = size() - 1, hi = max, mid = -1;
if (empty() || *pNewEl < *at(0))
it = begin();
else if (*pNewEl > *at(hi))
it = end();
else
{
// binary search for insert pos
lo = 0;
it = end();
while (lo <= hi)
{
mid = (hi + lo) / 2;
if (*at(mid) < *pNewEl)
lo = mid + 1;
else if (*at(mid) > *pNewEl)
hi = mid - 1;
else
{
// override "Untyped" if specially requested
if (bTypeMatch && at(mid)->GetType() != pNewEl->GetType())
{
if (at(mid)->GetType() < pNewEl->GetType())
lo = mid + 1;
else
hi = mid - 1;
}
else
{
if (bMultipleDefs)
{
// we found an existing label for this address,
// so we /might/ check the label name to assure
// alphanumerically sorted labelling.
// For now, simply insert as last one (order by sequence).
do
{
if (bAfter)
mid++;
else
mid--;
if (bTypeMatch && at(mid)->GetType() != pNewEl->GetType())
break;
} while (mid >= 0 && mid <= max && *at(mid) == *pNewEl);
}
break;
}
}
}
if (mid < 0)
mid++;
else if (mid <= max)
{
if (bTypeMatch &&
*at(mid) == *pNewEl &&
at(mid)->GetType() < pNewEl->GetType())
mid++;
else if (*at(mid) < *pNewEl)
mid++;
}
it = (mid > max) ? end() : (begin() + mid);
}
return vector<AddrType*>::insert(it, pNewEl);
}
// erase an element
iterator erase(iterator _Where)
{
AddrType *p = *_Where;
delete p;
return vector<AddrType*>::erase(_Where);
}
iterator erase(iterator _First, iterator _Last)
{
for (const_iterator cit = _First;
cit != _Last;
cit++)
{
AddrType *p = *cit;
delete p;
}
return vector<AddrType*>::erase(_First, _Last);
}
protected:
bool bMultipleDefs; // flag whether multiple definitions possible
};
/*****************************************************************************/
/* TAddrTypeArray : template for an array of address-sorted structures */
/*****************************************************************************/
template<class T> class TAddrTypeArray : public AddrTypeArray
{
public:
TAddrTypeArray(bool bMultipleDefs = false)
: AddrTypeArray(bMultipleDefs) { }
// vector<> specializations
T const *at(size_type _Pos) const { return (T const *)AddrTypeArray::at(_Pos); }
T * at(size_type _Pos) { return (T *)AddrTypeArray::at(_Pos); }
T const * operator[](size_type _Pos) const { return at(_Pos); }
T *operator[](size_type _Pos) { return at(_Pos); }
// insert into address/type-sorted array, allowing copying
iterator insert(T *pNewEl, bool bAfter = true, bool bTypeMatch = false)
{
// if multiple definitions not allowed, replace old one
if (!bMultipleDefs)
{
iterator it = AddrTypeArray::find(*pNewEl, bTypeMatch);
if (it != end())
{
// copy unset data from pre-existing element
pNewEl->CopyUnset((T const &)*(*it));
delete *it;
*it = pNewEl;
return it;
}
}
return AddrTypeArray::insert(pNewEl, bAfter, bTypeMatch);
}
};
/*****************************************************************************/
/* AddrText : a basic Text with address and type */
/*****************************************************************************/
class AddrText : public AddrType
{
public:
AddrText(adr_t addr = 0, MemoryType memType = Code, string stext = "")
: AddrType(addr, memType), stext(stext)
{ }
virtual ~AddrText() { }
// Attributes
public:
// Label Text
string GetText() const { return stext; }
bool SetText(string sNewText = "") { stext = sNewText; return true; }
bool HasText() const { return !!stext.size(); }
void CopyUnset(const AddrText &other)
{
AddrType::CopyUnset((const AddrType &)other);
if (stext.empty()) stext = other.stext;
}
protected:
string stext;
};
/*****************************************************************************/
/* AddrTextArray : an array of texts with address and type */
/*****************************************************************************/
class AddrTextArray : public TAddrTypeArray<AddrText>
{
};
/*****************************************************************************/
/* Label : definition of a label */
/*****************************************************************************/
class Label : public AddrText
{
public:
Label(adr_t addr = 0, MemoryType memType = Code, string sLabel = "", bool bUsed = false)
: AddrText(addr, memType, sLabel), bUsed(bUsed)
{ }
virtual ~Label() { }
// Attributes
public:
// Flag whether it is used
bool IsUsed() { return bUsed; }
bool SetUsed(bool bSet = true, adr_t ref = NO_ADDRESS, int bus = BusCode)
{
bUsed = bSet;
if (ref != NO_ADDRESS)
AddRef(ref, bus);
return bSet;
}
bool AddRef(adr_t addr, int bus = BusCode)
{
try
{
refs.push_back(AddrBus(addr, bus));
}
catch(...)
{
return false;
}
return true;
}
size_t RefCount() { return refs.size(); }
AddrBus &GetRef(size_t index) { return refs[index]; }
adr_t GetRefAddr(size_t index) { return refs[index].addr; }
int GetRefBus(size_t index) { return refs[index].bus; }
void SortRefs()
{
AddrBusComp sortfunc;
if (refs.size() > 1)
sort(refs.begin(), refs.end(), sortfunc);
}
void CopyUnset(const Label &other)
{
AddrText::CopyUnset((const AddrText &)other);
if (!bUsed) bUsed = other.bUsed;
}
protected:
bool bUsed;
vector<AddrBus> refs;
};
/*****************************************************************************/
/* LabelArray : an array of labels */
/*****************************************************************************/
class LabelArray : public TAddrTypeArray<Label>
{
public:
LabelArray(bool bMultipleDefs = false) : TAddrTypeArray<Label>(bMultipleDefs) { }
Label *GetFirst(adr_t addr, LabelArray::iterator &it, MemoryType memType = Untyped)
{
it = find(addr, memType);
return (it != end()) ? (Label *)(*it) : NULL;
}
Label *GetPrevNamed(adr_t addr, LabelArray::iterator &it, MemoryType memType = Untyped)
{
// finds the named label of the given type at this address or before
if ((it = findimprec(addr)) == end())
return NULL;
while (it != begin())
{
Label *pLabel = (Label *)(*it);
MemoryType lblMemType = pLabel->GetType();
if (pLabel->HasText() &&
((memType == Untyped && lblMemType != Const) || (memType == lblMemType)))
break;
it--;
}
return (it != end()) ? (Label *)(*it) : NULL;
}
// only makes sense if the label array allows multiples
Label *GetNext(adr_t addr, LabelArray::iterator &it, MemoryType memType = Untyped)
{
it++;
bool bStill = (it != end() && (*it)->GetAddress() == addr);
if (bStill)
bStill &= (memType == Untyped || (*it)->GetType() == memType);
return bStill ? (Label *)(*it) : NULL;
}
// little helper for single label arrays
Label *Find(adr_t addr, MemoryType memType = Untyped)
{
LabelArray::iterator p;
return GetFirst(addr, p, memType);
}
};
/*****************************************************************************/
/* DefLabel : definition of an definition label (i.e., label plus definition)*/
/*****************************************************************************/
class DefLabel : public Label
{
public:
DefLabel(adr_t addr = 0, MemoryType memType = Const, string sLabel = "", string sDefinition = "")
: Label(addr, memType, sLabel, true), sDefinition(sDefinition)
{ }
// Attributes
public:
void SetDefinition(string sTxt = "") { sDefinition = sTxt; }
string GetDefinition() { return sDefinition; }
protected:
string sDefinition;
};
/*****************************************************************************/
/* DefLabelArray : an array of definition labels */
/*****************************************************************************/
class DefLabelArray : public TAddrTypeArray<DefLabel>
{
public:
DefLabelArray(bool caseSensitive = true)
: TAddrTypeArray<DefLabel>(false), caseSensitive(caseSensitive) { }
void SetCaseSensitive(bool bOn = true) { caseSensitive = bOn; }
bool IsCaseSensitive() { return caseSensitive; }
// this one is definitely one label per address, so no GetFirst()/GetNext()
DefLabel *Find(adr_t addr, MemoryType memType = Untyped)
{
DefLabelArray::iterator p = find(addr, memType);
return (p != end()) ? (DefLabel *)(*p) : NULL;
}
DefLabel *Find(string sLabel)
{
// this is not really performant, but I don't think adding
// a separate index for the names is necessary
if (!caseSensitive) sLabel = lowercase(sLabel);
for (LabelArray::iterator p = begin(); p != end(); p++)
{
string sChk(((DefLabel*)*p)->GetText());
if (!caseSensitive)
sChk = lowercase(sChk);
if (sChk == sLabel)
return (DefLabel *)(*p);
}
return NULL;
}
protected:
bool caseSensitive;
};
#endif // __Label_h_defined__