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