-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisassembler.py
795 lines (697 loc) · 27.9 KB
/
disassembler.py
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
from typing import Dict
from functools import lru_cache
from dataclasses import dataclass
import bisect
from collections import defaultdict
from binaryninja import (
InstructionTextToken,
InstructionTextTokenType,
BranchType
)
# TODO replace all calls of this
# i am dumb
@lru_cache()
def dec(binary:str):
return int(binary, 2)
@dataclass
class BranchInfo:
_type:BranchType
target:int = None
IMM = 0
ADDR = 1
STR = 2
REG = 3
REG_DEREF = 4
################################################
# Nomenclature b/c idk what else to call these #
################################################
# B A 9 8 7 6 5 4 3 2 1 0 | Label #
#---------------------------|------------------#
#[X X X X] | UW - Upper Word #
# [X X X X] | MW - Middle Word #
# [X X X X] | LW - Lower Word #
# [X X] | ML - Middle Low #
# [X X] | LL - Low Low #
################################################
class Instruction:
r = [
('A', REG),
('B', REG),
('IX', REG_DEREF),
('IY', REG_DEREF)
]
def __init__(self, data:bytes, addr:int):
if len(data) == 0:
raise ValueError("Zero length bytes to decode")
elif len(data) < 2:
data = b'\x00' + data
else:
data = data[-2:]
self.data = data
self.addr = addr
# "Macros" to make parsing easier
self.value = (data[0] << 8) | data[1]
self.upper_word = data[0] & 15
self.middle_word = (data[1] & 240) >> 4
self.lower_word = data[1] & 15
self.middle_low = data[1] & 48 >> 4
self.low_low = data[1] & 3
self.p = data[1] & 31
self.s = (self.middle_word << 4) | self.lower_word
self.l = self.s
self.i = self.lower_word
self.mnemonic = None
self.op1 = None
self.op2 = None
self.branches = []
self.cond = None
self.comment = None
self.parse()
def parse(self):
# BRANCH INSTRUCTIONS
if self.upper_word == dec('1110') and (self.middle_word >> 1) == dec('010'):
self.mnemonic = "PSET"
self.op1 = (self.data[1] & 31, IMM)
return
if self.upper_word == 0:
self.mnemonic = "JP"
self.op1 = (self.s, ADDR)
pset = psets.get(self.addr)
target_addr = 2 * ((pset.op1[0] << 8) | self.s)
self.branches.append(BranchInfo(_type=BranchType.UnconditionalBranch, target=target_addr))
return
if self.upper_word == dec('10'):
self.mnemonic = "JP"
self.op1 = ("C", STR)
self.op2 = (self.s, ADDR)
pset = psets.get(self.addr)
target_addr = 2 * ((pset.op1[0] << 8) | self.s)
self.branches.append(BranchInfo(_type=BranchType.TrueBranch, target=target_addr))
self.branches.append(BranchInfo(_type=BranchType.FalseBranch, target=self.addr+2))
return
if self.upper_word == dec('11'):
self.mnemonic = "JP"
self.op1 = ("NC", STR)
self.op2 = (self.s, ADDR)
pset = psets.get(self.addr)
target_addr = 2 * ((pset.op1[0] << 8) | self.s)
self.branches.append(BranchInfo(_type=BranchType.TrueBranch, target=target_addr))
self.branches.append(BranchInfo(_type=BranchType.FalseBranch, target=self.addr+2))
return
if self.upper_word == dec('110'):
self.mnemonic = "JP"
self.op1 = ("Z", STR)
self.op2 = (self.s, ADDR)
pset = psets.get(self.addr)
target_addr = 2 * ((pset.op1[0] << 8) | self.s)
self.branches.append(BranchInfo(_type=BranchType.TrueBranch, target=target_addr))
self.branches.append(BranchInfo(_type=BranchType.FalseBranch, target=self.addr+2))
return
if self.upper_word == dec('111'):
self.mnemonic = "JP"
self.op1 = ("NZ", STR)
self.op2 = (self.s, ADDR)
pset = psets.get(self.addr)
target_addr = 2 * ((pset.op1[0] << 8) | self.s)
self.branches.append(BranchInfo(_type=BranchType.TrueBranch, target=target_addr))
self.branches.append(BranchInfo(_type=BranchType.FalseBranch, target=self.addr+2))
return
if self.value == dec('111111101000'):
self.mnemonic = "JPBA"
self.branches.append(BranchInfo(_type=BranchType.IndirectBranch))
return
if self.upper_word == dec('0100'):
self.mnemonic = "CALL"
self.op1 = (self.s, ADDR)
pset = psets.get(self.addr)
# NBP not used
# Bank of Current PC | Page set by PSET | op1
target_addr = self.addr & (1 << 13)
target_addr |= (pset.op1[0] & 15) << 8
target_addr |= self.s
target_addr = 2 * target_addr
self.branches.append(BranchInfo(_type=BranchType.CallDestination, target=target_addr))
return
if self.upper_word == dec('0101'):
self.mnemonic = "CALZ"
self.op1 = (self.s, ADDR)
# Bank of Current PC | Page 0 | op1
target_addr = self.addr & (1 << 13)
target_addr |= self.s
target_addr = 2 * target_addr
self.branches.append(BranchInfo(_type=BranchType.CallDestination, target=target_addr))
return
if self.value == dec('111111011111'):
self.mnemonic = "RET"
self.branches.append(BranchInfo(_type=BranchType.FunctionReturn))
return
if self.value == dec('111111011110'):
self.mnemonic = "RETS"
# i.e. the PC it should return to is Return Address + 2
self.comment = "Skips over the next instruction after returning"
self.branches.append(BranchInfo(_type=BranchType.FunctionReturn))
return
if self.upper_word == dec('0001'):
self.mnemonic = "RETD"
self.op1 = ((self.middle_word << 4) | self.lower_word, IMM)
self.branches.append(BranchInfo(_type=BranchType.FunctionReturn))
return
# SYSTEM CONTROL
if self.value == dec('111111111011'):
self.mnemonic = "NOP5"
return
if self.value == dec('111111111111'):
self.mnemonic = "NOP7"
return
if self.value == dec('111111111000'):
self.mnemonic = "HALT"
return
# INDEX OPERATIONS
# INC
if self.value == dec('111011100000'):
self.mnemonic = "INC"
self.op1 = ("X", REG)
return
if self.value == dec('111011110000'):
self.mnemonic = "INC"
self.op1 = ("Y", REG)
return
# LD
if self.upper_word == dec('1011'):
self.mnemonic = "LD"
self.op1 = ("X", REG)
self.op2 = (self.s, IMM)
return
if self.upper_word == dec('1000'):
self.mnemonic = "LD"
self.op1 = ("Y", REG)
self.op2 = (self.s, IMM)
return
if self.upper_word == dec('1010'):
# ADC
if self.middle_word == dec('0000'):
self.mnemonic = "ADC"
self.op1 = ("XH", REG)
self.op2 = (self.lower_word, IMM)
return
elif self.middle_word == dec('0001'):
self.mnemonic = "ADC"
self.op1 = ("XL", REG)
self.op2 = (self.lower_word, IMM)
return
elif self.middle_word == dec('0010'):
self.mnemonic = "ADC"
self.op1 = ("YH", REG)
self.op2 = (self.lower_word, IMM)
return
elif self.middle_word == dec('0011'):
self.mnemonic = "ADC"
self.op1 = ("YL", REG)
self.op2 = (self.lower_word, IMM)
return
# CP
if self.middle_word == dec('0100'):
self.mnemonic = "CP"
self.op1 = ("XH", REG)
self.op2 = (self.lower_word, IMM)
return
if self.middle_word == dec('0101'):
self.mnemonic = "CP"
self.op1 = ("XH", REG)
self.op2 = (self.lower_word, IMM)
return
if self.middle_word == dec('0110'):
self.mnemonic = "CP"
self.op1 = ("XH", REG)
self.op2 = (self.lower_word, IMM)
return
if self.middle_word == dec('0111'):
self.mnemonic = "CP"
self.op1 = ("XH", REG)
self.op2 = (self.lower_word, IMM)
return
if self.middle_word == dec('1000'):
self.mnemonic = "ADD"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('1001'):
self.mnemonic = "ADC"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('1010'):
self.mnemonic = "SUB"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('1011'):
self.mnemonic = "SBC"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('1100'):
self.mnemonic = "AND"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('1101'):
self.mnemonic = "OR"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('1110'):
self.mnemonic = "XOR"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec("1111"):
self.mnemonic = "RLC"
self.op1 = self.r[self.low_low]
return
# LD
if self.upper_word == dec('1110'):
if (self.middle_word >> 2) & 3 == dec('00'):
self.mnemonic = "LD"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if (self.middle_word >> 2) & 3 == dec('11'):
self.mnemonic = "LD"
self.op1 = self.r[(self.lower_word >> 2) & 3]
self.op2 = self.r[self.lower_word & 3]
return
# LDPX
if self.middle_word == dec('0110'):
self.mnemonic = "LDPX"
self.op1 = ("IX", REG_DEREF)
self.op2 = (self.lower_word, IMM)
return
if self.middle_word == dec('1110'):
self.mnemonic = "LDPX"
self.op1 = self.r[(self.lower_word >> 2) & 3]
self.op2 = self.r[self.lower_word & 3]
return
# LDPY
if self.middle_word == dec('0111'):
self.mnemonic = "LDPY"
self.op1 = ("IY", REG_DEREF)
self.op2 = (self.lower_word, IMM)
return
if self.middle_word == dec('1111'):
self.mnemonic = "LDPY"
self.op1 = self.r[(self.lower_word >> 2) & 3]
self.op2 = self.r[self.lower_word & 3]
return
low_high = (self.lower_word >> 2) & 3
if self.middle_word == dec('1000'):
self.mnemonic = "LD"
if low_high == dec('00'):
self.op1 = ("XP", REG)
self.op2 = self.r[low_high]
return
if low_high == dec('01'):
self.op1 = ("XH", REG)
self.op2 = self.r[low_high]
return
if low_high == dec('10'):
self.op1 = ("XL", REG)
self.op2 = self.r[low_high]
return
if self.middle_word == dec('1001'):
self.mnemonic = "LD"
if low_high == dec('00'):
self.op1 = ("YP", REG)
self.op2 = self.r[low_high]
return
if low_high == dec('01'):
self.op1 = ("YH", REG)
self.op2 = self.r[low_high]
return
if low_high == dec('10'):
self.op1 = ("YL", REG)
self.op2 = self.r[low_high]
return
elif self.middle_word == dec('1010'):
self.mnemonic = "LD"
if low_high == dec('00'):
self.op1 = self.r[low_high]
self.op2 = ("XP", REG)
return
elif low_high == dec('01'):
self.op1 = self.r[low_high]
self.op2 = ("XH", REG)
return
elif low_high == dec('10'):
self.op1 = self.r[low_high]
self.op2 = ("XL", REG)
return
elif self.middle_word == dec('1011'):
self.mnemonic = "LD"
if low_high == dec('00'):
self.op1 = self.r[low_high]
self.op2 = ("YP", REG)
return
elif low_high == dec('01'):
self.op1 = self.r[low_high]
self.op2 = ("YH", REG)
return
elif low_high == dec('10'):
self.op1 = self.r[low_high]
self.op2 = ("YL", REG)
return
if self.middle_word == dec('1000'):
if self.lower_word >> 2 == 3:
self.mnemonic = "RRC"
self.op1 = self.r[self.low_low]
return
if self.upper_word == dec('1001'):
self.mnemonic = "LBPX"
self.op1 = ("IX", REG_DEREF)
self.op2 = ((self.middle_word << 4) | self.lower_word, IMM)
return
if self.upper_word == dec('1111'):
if self.middle_word == dec('1010'):
self.mnemonic = "LD"
self.op1 = ("A", REG)
self.op2 = (self.lower_word, ADDR)
return
if self.middle_word == dec('1011'):
self.mnemonic = "LD"
self.op1 = ("B", REG)
self.op2 = (self.lower_word, ADDR)
return
if self.middle_word == dec('1000'):
self.mnemonic = "LD"
self.op1 = (self.lower_word, ADDR)
self.op2 = ("A", REG)
return
if self.middle_word == dec('1001'):
self.mnemonic = "LD"
self.op1 = (self.lower_word, ADDR)
self.op2 = ("B", REG)
return
if self.middle_word == dec('0100'):
if self.lower_word == dec('0001'):
self.mnemonic = "SCF"
return
if self.lower_word == dec('0010'):
self.mnemonic = "SZF"
return
if self.lower_word == dec('0100'):
self.mnemonic = "SDF"
return
if self.lower_word == dec('1000'):
self.mnemonic = "EI"
return
if self.middle_word == dec('0101'):
if self.lower_word == dec("1110"):
self.mnemonic = "RCF"
return
if self.lower_word == dec('1101'):
self.mnemonic = "RZF"
return
if self.lower_word == dec('1011'):
self.mnemonic = "RDF"
return
if self.lower_word == dec('0111'):
self.mnemonic = "DI"
return
if self.middle_word == dec('0100'):
self.mnemonic = "SET"
self.op1 = ("F", REG)
self.op2 = (self.lower_word, IMM)
return
if self.middle_word == dec('0101'):
self.mnemonic = "RST"
self.op1 = ("F", REG)
self.op2 = (self.lower_word, IMM)
return
# Stack Operations
if self.middle_word == dec('1101') and self.lower_word == dec('1011'):
self.mnemonic = "INC"
self.op1 = ("SP", REG)
return
if self.middle_word == dec('1100'):
if self.lower_word == dec('1011'):
self.mnemonic = "DEC"
self.op1 = ("SP", REG)
return
if self.lower_word >> 2 == dec('00'):
self.mnemonic = "PUSH"
self.op1 = self.r[self.low_low & 3]
return
if self.lower_word == dec('0100'):
self.mnemonic = "PUSH"
self.op1 = ("XP", REG)
return
if self.lower_word == dec('0101'):
self.mnemonic = "PUSH"
self.op1 = ("XH", REG)
return
if self.lower_word == dec('0110'):
self.mnemonic = "PUSH"
self.op1 = ("XL", REG)
return
if self.lower_word == dec('0111'):
self.mnemonic = "PUSH"
self.op1 = ("YP", REG)
return
if self.lower_word == dec('1000'):
self.mnemonic = "PUSH"
self.op1 = ("YH", REG)
return
if self.lower_word == dec('1001'):
self.mnemonic = "PUSH"
self.op1 = ("YL", REG)
return
if self.lower_word == dec('1010'):
self.mnemonic = "PUSH"
self.op1 = ("F", REG)
return
if self.middle_word == dec('1101'):
if self.lower_word >> 2 == dec('00'):
self.mnemonic = "POP"
self.op1 = self.r[self.lower_word & 3]
return
if self.lower_word == dec('0100'):
self.mnemonic = "POP"
self.op1 = ("XP", REG)
return
if self.lower_word == dec('0101'):
self.mnemonic = "POP"
self.op1 = ("XH", REG)
return
if self.lower_word == dec('0110'):
self.mnemonic = "POP"
self.op1 = ("XL", REG)
return
if self.lower_word == dec('0111'):
self.mnemonic = "POP"
self.op1 = ("YP", REG)
return
if self.lower_word == dec('1000'):
self.mnemonic = "POP"
self.op1 = ("YH", REG)
return
if self.lower_word == dec('1001'):
self.mnemonic = "POP"
self.op1 = ("YL", REG)
return
if self.lower_word == dec('1010'):
self.mnemonic = "POP"
self.op1 = ("F", REG)
return
if self.middle_word == dec('1110'):
if (self.lower_word >> 2) == 0:
self.mnemonic = "LD"
self.op1 = ("SPH", REG)
self.op2 = self.r[self.low_low]
return
if (self.lower_word >> 2) == 1:
self.mnemonic = "LD"
self.op1 = self.r[self.low_low]
self.op2 = ("SPH", REG)
return
if self.middle_word == dec('1111'):
if (self.lower_word >> 2) == 0:
self.mnemonic = "LD"
self.op1 = ("SPL", REG)
self.op2 = self.r[self.low_low]
return
if (self.lower_word >> 2) == 1:
self.mnemonic = "LD"
self.op1 = self.r[self.low_low]
self.op2 = ("SPL", REG)
return
if self.middle_word == dec('0000'):
self.mnemonic = "CP"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('0001'):
self.mnemonic = "FAN"
self.op1 = self.r[self.lower_word >> 2]
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('0110'):
self.mnemonic = "INC"
self.op1 = (self.lower_word, ADDR)
return
if self.middle_word == dec('0111'):
self.mnemonic = "DEC"
self.op1 = (self.lower_word, ADDR)
return
if self.middle_word == dec('0010'):
if self.lower_word >> 2 == 2:
self.mnemonic = "ACPX"
self.op1 = ("IX", REG_DEREF)
self.op2 = self.r[self.low_low]
return
if self.lower_word >> 2 == 3:
self.mnemonic = "ACPY"
self.op1 = ("IY", REG_DEREF)
self.op2 = self.r[self.low_low]
return
if self.middle_word == dec('0011'):
if self.lower_word >> 2 == 2:
self.mnemonic = "SCPX"
self.op1 = ("IX", REG_DEREF)
self.op2 = self.r[self.low_low]
return
if self.lower_word >> 2 == 3:
self.mnemonic = "SCPY"
self.op1 = ("IY", REG_DEREF)
self.op2 = self.r[self.low_low]
return
# Arithmetic
if self.upper_word == dec('1100'):
if (self.middle_word >> 2) == 0:
self.mnemonic = "ADD"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if (self.middle_word >> 2) == 1:
self.mnemonic = "ADC"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if (self.middle_word >> 2) == 2:
self.mnemonic = "AND"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if (self.middle_word >> 2) == 3:
self.mnemonic = "OR"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if self.upper_word == dec('1101'):
if self.middle_word >> 2 == 1:
self.mnemonic = "SBC"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if self.middle_word >> 2 == 0:
self.mnemonic = "XOR"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if self.middle_word >> 2 == 3:
self.mnemonic = "CP"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if self.middle_word >> 2 == 2:
self.mnemonic = "FAN"
self.op1 = self.r[self.middle_low]
self.op2 = (self.lower_word, IMM)
return
if self.middle_word >> 2 == 0 and self.lower_word == dec('1111'):
self.mnemonic = "NOT"
self.op1 = self.r[self.middle_word & 3]
return
self.mnemonic = "UNKNOWN"
class Disassembler:
def __init__(self):
pass
@classmethod
def parse_operand(cls, op):
value, _type = op
if _type == IMM:
token_type = InstructionTextTokenType.IntegerToken
value = hex(value)
elif _type == ADDR:
token_type = InstructionTextTokenType.AddressDisplayToken
# Because each address in the ROM is 12 bits (round up to 16bits)
# we have to double all addresses displayed since binary ninja
# assumes each address has onle 1 byte
value = hex(value * 2)
elif _type == STR:
token_type = InstructionTextTokenType.TextToken
elif _type == REG:
token_type = InstructionTextTokenType.RegisterToken
elif _type == REG_DEREF:
token_type = InstructionTextTokenType.RegisterToken
return value, token_type
def disasm(self, data, addr):
instr = Instruction(data, addr)
tokens = [InstructionTextToken(InstructionTextTokenType.InstructionToken, instr.mnemonic)]
if instr.op1 is not None:
tokens.append(InstructionTextToken(InstructionTextTokenType.OperandSeparatorToken, " "))
value, token_type = Disassembler.parse_operand(instr.op1)
tokens.append(InstructionTextToken(token_type, value))
if instr.op2 is not None:
tokens.append(InstructionTextToken(InstructionTextTokenType.OperandSeparatorToken, ", "))
value, token_type = Disassembler.parse_operand(instr.op2)
tokens.append(InstructionTextToken(token_type, value))
if instr.comment is not None:
tokens.append(InstructionTextToken(InstructionTextTokenType.CommentToken, f" {instr.comment}"))
return tokens, instr.branches
class PSetFinder:
'''
Data structure for returning the largest address smaller than the given address on retrieval
For branch instructions, we need to query the last PSET that would normally be executed
We also assume that each branch instruction only has a single PSET instruction that could be executed before it executes
i.e Assumes there will never be basic blocks that look like this:
.------. .------.
| PSET | | PSET |
`------' `------'
| |
v v
.--------------.
| BRANCH INSTR |
`--------------'
'''
DEFAULT = Instruction(b'\x0e\x41', addr=None)
def __init__(self, bin_size=256):
self.bin_size = bin_size
self.psets = defaultdict(lambda: [list(), 0])
self.history = set()
self._size = 0
def __len__(self):
return self._size
def add(self, addr:int, instr:Instruction):
if addr in self.history:
return
self.history.add(addr)
self._size += 1
key = addr // self.bin_size
bin = self.psets[key]
bisect.insort(bin[0], instr, key=lambda x: x.addr)
# Update the minimum address found in this bin
if instr.addr < bin[1]:
bin[1] = instr.addr
def get(self, addr) -> Instruction:
key = addr // self.bin_size
bin = self.psets[key]
while addr < bin[1]:
key -= 1
if key < 0:
return PSetFinder.DEFAULT
bin = self.psets[key]
i = bisect.bisect_right(bin[0], addr, key=lambda x: x.addr)
if i:
return bin[0][i-1]
return PSetFinder.DEFAULT
psets = PSetFinder()