208
|
1 import binascii
|
225
|
2 from asmnodes import ALabel, AComment
|
208
|
3 """
|
|
4 The output stream is a stream of instructions that can be output
|
|
5 to a file or binary or hexfile.
|
|
6 """
|
|
7
|
219
|
8 class Alignment:
|
|
9 def __init__(self, a):
|
|
10 self.align = a
|
|
11
|
208
|
12 class OutputStream:
|
|
13 def __init__(self):
|
|
14 self.sections = {}
|
|
15 self.currentSection = None
|
225
|
16
|
208
|
17 def emit(self, item):
|
|
18 self.sections[self.currentSection].append(item)
|
219
|
19
|
|
20 def align(self, alignment):
|
|
21 self.emit(Alignment(alignment))
|
|
22
|
208
|
23 def selectSection(self, s):
|
|
24 self.currentSection = s
|
|
25 if not s in self.sections:
|
|
26 self.sections[s] = []
|
|
27
|
|
28 def backpatch(self):
|
|
29 """ Fixup references to other parts in the assembler """
|
|
30 for s in self.sections:
|
|
31 # TODO parameterize this:
|
|
32 if s == 'code':
|
|
33 address = 0x08000000
|
|
34 elif s == 'data':
|
|
35 address = 0x02000000
|
|
36 else:
|
|
37 address = 0x0
|
|
38 for i in self.sections[s]:
|
|
39 i.address = address
|
225
|
40 if type(i) in [ALabel, AComment]:
|
208
|
41 continue
|
219
|
42 if type(i) is Alignment:
|
|
43 while (address % i.align) != 0:
|
|
44 address += 1
|
|
45 continue
|
208
|
46 bts = i.encode()
|
|
47 address += len(bts)
|
|
48
|
|
49 def dump(self):
|
|
50 self.backpatch()
|
|
51 for s in sorted(self.sections.keys()):
|
|
52 self.dumpSection(s)
|
|
53
|
|
54 def dumpSection(self, s):
|
|
55 print('.section '+s)
|
|
56 for i in self.sections[s]:
|
225
|
57 if type(i) in [ALabel, AComment]:
|
208
|
58 print(i)
|
219
|
59 elif type(i) is Alignment:
|
|
60 pass
|
208
|
61 else:
|
|
62 addr = i.address
|
|
63 insword = i.encode()
|
|
64 assert type(insword) is bytes
|
|
65 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
|
|
66 asm = str(i)
|
|
67 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
|
|
68
|
|
69 class TextOutputStream(OutputStream):
|
|
70 pass
|
|
71
|
|
72 class BinOutputStream(OutputStream):
|
|
73 def dump(self):
|
|
74 pass
|
|
75
|