annotate python/outstream.py @ 225:1c7364bd74c7

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