annotate python/outstream.py @ 236:8786811a5a59

Fix pcrel
author Windel Bouwman
date Mon, 15 Jul 2013 20:15:31 +0200
parents ff40407c0240
children 81752b0f85a5
rev   line source
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
1 import binascii
234
Windel Bouwman
parents: 225
diff changeset
2 from target import Instruction, Label
Windel Bouwman
parents: 225
diff changeset
3
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
4 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
5 The output stream is a stream of instructions that can be output
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
6 to a file or binary or hexfile.
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
7 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
8
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
9
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
10 class OutputStream:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
11 def __init__(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
12 self.sections = {}
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
13 self.currentSection = None
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 219
diff changeset
14
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
15 def emit(self, item):
234
Windel Bouwman
parents: 225
diff changeset
16 assert isinstance(item, Instruction)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
17 self.sections[self.currentSection].append(item)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
18
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
19 def selectSection(self, s):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
20 self.currentSection = s
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
21 if not s in self.sections:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
22 self.sections[s] = []
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
23
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
24 def getLabelAddress(self, lname):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
25 assert isinstance(lname, str)
234
Windel Bouwman
parents: 225
diff changeset
26 for s in self.sections.values():
Windel Bouwman
parents: 225
diff changeset
27 for i in s:
Windel Bouwman
parents: 225
diff changeset
28 if type(i) is Label:
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
29 if i.name == lname:
234
Windel Bouwman
parents: 225
diff changeset
30 return i.address
Windel Bouwman
parents: 225
diff changeset
31 return 0
Windel Bouwman
parents: 225
diff changeset
32
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
33 def backpatch(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
34 """ Fixup references to other parts in the assembler """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
35 for s in self.sections:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
36 # TODO parameterize this:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
37 if s == 'code':
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
38 address = 0x08000000
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
39 elif s == 'data':
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
40 address = 0x02000000
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
41 else:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
42 address = 0x0
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
43 for i in self.sections[s]:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
44 i.address = address
234
Windel Bouwman
parents: 225
diff changeset
45 i.resolve(self.getLabelAddress)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
46 bts = i.encode()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
47 address += len(bts)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
48
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
49 def dump(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
50 self.backpatch()
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
51 self.backpatch()
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
52 for s in sorted(self.sections.keys()):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
53 self.dumpSection(s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
54
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
55 def dumpSection(self, s):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
56 print('.section '+s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
57 for i in self.sections[s]:
234
Windel Bouwman
parents: 225
diff changeset
58 addr = i.address
Windel Bouwman
parents: 225
diff changeset
59 insword = i.encode()
Windel Bouwman
parents: 225
diff changeset
60 assert type(insword) is bytes
Windel Bouwman
parents: 225
diff changeset
61 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
Windel Bouwman
parents: 225
diff changeset
62 asm = str(i)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
63 if len(insword) == 0:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
64 print(' {}'.format(asm))
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
65 else:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
66 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
67
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
68 class TextOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
69 pass
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
70
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
71 class BinOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
72 def dump(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
73 pass
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
74
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
75 @property
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
76 def Data(self):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
77 d = self.dump()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
78 return bytes(d)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
79
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
80 def dump(self):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
81 self.backpatch()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
82 self.backpatch()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
83 section = list(self.sections.keys())[0]
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
84 return self.dumpSection(section)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
85
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
86 def dumpSection(self, s):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
87 d = bytearray()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
88 for i in self.sections[s]:
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
89 addr = i.address
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
90 insword = i.encode()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
91 assert type(insword) is bytes
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
92 d.extend(insword)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
93 return d
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
94