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
|
316
|
36
|
208
|
37 class OutputStream:
|
|
38 def __init__(self):
|
|
39 self.sections = {}
|
|
40 self.currentSection = None
|
225
|
41
|
208
|
42 def emit(self, item):
|
237
|
43 assert self.currentSection
|
250
|
44 self.currentSection.emit(item)
|
219
|
45
|
250
|
46 def selectSection(self, sname):
|
|
47 self.currentSection = self.getSection(sname)
|
208
|
48
|
235
|
49 def getLabelAddress(self, lname):
|
|
50 assert isinstance(lname, str)
|
234
|
51 for s in self.sections.values():
|
237
|
52 for i in s.instructions:
|
234
|
53 if type(i) is Label:
|
235
|
54 if i.name == lname:
|
234
|
55 return i.address
|
|
56 return 0
|
|
57
|
249
|
58 def getSection(self, name):
|
|
59 if not name in self.sections:
|
|
60 self.sections[name] = Section()
|
|
61 return self.sections[name]
|
|
62
|
208
|
63 def backpatch(self):
|
|
64 """ Fixup references to other parts in the assembler """
|
250
|
65 for s in self.sections.values():
|
|
66 address = s.address
|
|
67 for i in s.instructions:
|
208
|
68 i.address = address
|
234
|
69 i.resolve(self.getLabelAddress)
|
208
|
70 bts = i.encode()
|
|
71 address += len(bts)
|
|
72
|
316
|
73 class OutputStreamWriter:
|
|
74 def __init__(self, extra_indent=''):
|
|
75 self.extra_indent = extra_indent
|
208
|
76
|
316
|
77 def dump(self, stream, f):
|
|
78 stream.backpatch()
|
|
79 stream.backpatch()
|
|
80 for s in sorted(stream.sections.keys()):
|
|
81 # print('.section '+ s)
|
|
82 self.dumpSection(stream.sections[s], f)
|
|
83
|
|
84 def dumpSection(self, s, f):
|
|
85 for i in s.instructions:
|
259
|
86 if type(i) is DebugInfo:
|
|
87 continue
|
234
|
88 addr = i.address
|
|
89 insword = i.encode()
|
|
90 assert type(insword) is bytes
|
|
91 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
|
|
92 asm = str(i)
|
235
|
93 if len(insword) == 0:
|
316
|
94 print(' {}'.format(asm), file=f)
|
235
|
95 else:
|
316
|
96 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm), file=f)
|
208
|
97
|
296
|
98
|
208
|
99 class TextOutputStream(OutputStream):
|
|
100 pass
|
|
101
|
296
|
102
|
208
|
103 class BinOutputStream(OutputStream):
|
|
104
|
236
|
105 @property
|
|
106 def Data(self):
|
|
107 d = self.dump()
|
|
108 return bytes(d)
|
|
109
|
|
110 def dump(self):
|
|
111 self.backpatch()
|
|
112 self.backpatch()
|
237
|
113 section = self.sections[list(self.sections.keys())[0]]
|
|
114 return section.to_bytes()
|