208
|
1 import binascii
|
234
|
2 from target import Instruction, Label
|
|
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
|
208
|
10 class OutputStream:
|
|
11 def __init__(self):
|
|
12 self.sections = {}
|
|
13 self.currentSection = None
|
225
|
14
|
208
|
15 def emit(self, item):
|
234
|
16 assert isinstance(item, Instruction)
|
208
|
17 self.sections[self.currentSection].append(item)
|
219
|
18
|
208
|
19 def selectSection(self, s):
|
|
20 self.currentSection = s
|
|
21 if not s in self.sections:
|
|
22 self.sections[s] = []
|
|
23
|
235
|
24 def getLabelAddress(self, lname):
|
|
25 assert isinstance(lname, str)
|
234
|
26 for s in self.sections.values():
|
|
27 for i in s:
|
|
28 if type(i) is Label:
|
235
|
29 if i.name == lname:
|
234
|
30 return i.address
|
|
31 return 0
|
|
32
|
208
|
33 def backpatch(self):
|
|
34 """ Fixup references to other parts in the assembler """
|
|
35 for s in self.sections:
|
|
36 # TODO parameterize this:
|
|
37 if s == 'code':
|
|
38 address = 0x08000000
|
|
39 elif s == 'data':
|
|
40 address = 0x02000000
|
|
41 else:
|
|
42 address = 0x0
|
|
43 for i in self.sections[s]:
|
|
44 i.address = address
|
234
|
45 i.resolve(self.getLabelAddress)
|
208
|
46 bts = i.encode()
|
|
47 address += len(bts)
|
|
48
|
|
49 def dump(self):
|
|
50 self.backpatch()
|
236
|
51 self.backpatch()
|
208
|
52 for s in sorted(self.sections.keys()):
|
|
53 self.dumpSection(s)
|
|
54
|
|
55 def dumpSection(self, s):
|
|
56 print('.section '+s)
|
|
57 for i in self.sections[s]:
|
234
|
58 addr = i.address
|
|
59 insword = i.encode()
|
|
60 assert type(insword) is bytes
|
|
61 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
|
|
62 asm = str(i)
|
235
|
63 if len(insword) == 0:
|
|
64 print(' {}'.format(asm))
|
|
65 else:
|
|
66 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
|
208
|
67
|
|
68 class TextOutputStream(OutputStream):
|
|
69 pass
|
|
70
|
|
71 class BinOutputStream(OutputStream):
|
|
72 def dump(self):
|
|
73 pass
|
|
74
|
236
|
75 @property
|
|
76 def Data(self):
|
|
77 d = self.dump()
|
|
78 return bytes(d)
|
|
79
|
|
80 def dump(self):
|
|
81 self.backpatch()
|
|
82 self.backpatch()
|
|
83 section = list(self.sections.keys())[0]
|
|
84 return self.dumpSection(section)
|
|
85
|
|
86 def dumpSection(self, s):
|
|
87 d = bytearray()
|
|
88 for i in self.sections[s]:
|
|
89 addr = i.address
|
|
90 insword = i.encode()
|
|
91 assert type(insword) is bytes
|
|
92 d.extend(insword)
|
|
93 return d
|
|
94
|