208
|
1 import binascii
|
249
|
2 from target import Instruction, Label, DebugInfo
|
234
|
3
|
208
|
4 """
|
|
5 The output stream is a stream of instructions that can be output
|
|
6 to a file or binary or hexfile.
|
|
7 """
|
|
8
|
219
|
9
|
237
|
10 class Section:
|
|
11 def __init__(self):
|
250
|
12 self.address = 0
|
237
|
13 self.instructions = []
|
|
14
|
|
15 def emit(self, item):
|
|
16 assert isinstance(item, Instruction)
|
|
17 self.instructions.append(item)
|
|
18
|
|
19 def to_bytes(self):
|
|
20 d = bytearray()
|
|
21 for i in self.instructions:
|
|
22 addr = i.address
|
|
23 insword = i.encode()
|
|
24 assert type(insword) is bytes
|
|
25 d.extend(insword)
|
|
26 return bytes(d)
|
|
27
|
258
|
28 @property
|
|
29 def Size(self):
|
|
30 return len(self.to_bytes())
|
|
31
|
249
|
32 def debugInfos(self):
|
|
33 di = [i for i in self.instructions if isinstance(i, DebugInfo)]
|
|
34 return di
|
|
35
|
208
|
36 class OutputStream:
|
|
37 def __init__(self):
|
|
38 self.sections = {}
|
|
39 self.currentSection = None
|
225
|
40
|
208
|
41 def emit(self, item):
|
237
|
42 assert self.currentSection
|
250
|
43 self.currentSection.emit(item)
|
219
|
44
|
250
|
45 def selectSection(self, sname):
|
|
46 self.currentSection = self.getSection(sname)
|
208
|
47
|
235
|
48 def getLabelAddress(self, lname):
|
|
49 assert isinstance(lname, str)
|
234
|
50 for s in self.sections.values():
|
237
|
51 for i in s.instructions:
|
234
|
52 if type(i) is Label:
|
235
|
53 if i.name == lname:
|
234
|
54 return i.address
|
|
55 return 0
|
|
56
|
249
|
57 def getSection(self, name):
|
|
58 if not name in self.sections:
|
|
59 self.sections[name] = Section()
|
|
60 return self.sections[name]
|
|
61
|
208
|
62 def backpatch(self):
|
|
63 """ Fixup references to other parts in the assembler """
|
250
|
64 for s in self.sections.values():
|
|
65 address = s.address
|
|
66 for i in s.instructions:
|
208
|
67 i.address = address
|
234
|
68 i.resolve(self.getLabelAddress)
|
208
|
69 bts = i.encode()
|
|
70 address += len(bts)
|
|
71
|
|
72 def dump(self):
|
|
73 self.backpatch()
|
236
|
74 self.backpatch()
|
208
|
75 for s in sorted(self.sections.keys()):
|
|
76 self.dumpSection(s)
|
|
77
|
|
78 def dumpSection(self, s):
|
237
|
79 print('.section '+ s)
|
|
80 for i in self.sections[s].instructions:
|
259
|
81 if type(i) is DebugInfo:
|
|
82 continue
|
234
|
83 addr = i.address
|
|
84 insword = i.encode()
|
|
85 assert type(insword) is bytes
|
|
86 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
|
|
87 asm = str(i)
|
235
|
88 if len(insword) == 0:
|
259
|
89 print(' {}'.format(asm))
|
235
|
90 else:
|
|
91 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
|
208
|
92
|
296
|
93
|
208
|
94 class TextOutputStream(OutputStream):
|
|
95 pass
|
|
96
|
296
|
97
|
208
|
98 class BinOutputStream(OutputStream):
|
|
99
|
236
|
100 @property
|
|
101 def Data(self):
|
|
102 d = self.dump()
|
|
103 return bytes(d)
|
|
104
|
|
105 def dump(self):
|
|
106 self.backpatch()
|
|
107 self.backpatch()
|
237
|
108 section = self.sections[list(self.sections.keys())[0]]
|
|
109 return section.to_bytes()
|