annotate python/outstream.py @ 219:1fa3e0050b49

Expanded ad hoc code generator
author Windel Bouwman
date Sat, 06 Jul 2013 12:38:09 +0200
parents 4cb47d80fd1f
children 1c7364bd74c7
rev   line source
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
1 import binascii
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
2 from asmnodes import ALabel
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
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
16 def emit(self, item):
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
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
19 def align(self, alignment):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
20 self.emit(Alignment(alignment))
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
21
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
22 def selectSection(self, s):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
23 self.currentSection = s
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
24 if not s in self.sections:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
25 self.sections[s] = []
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
26
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
27 def backpatch(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
28 """ Fixup references to other parts in the assembler """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
29 for s in self.sections:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
30 # TODO parameterize this:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
31 if s == 'code':
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
32 address = 0x08000000
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
33 elif s == 'data':
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
34 address = 0x02000000
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
35 else:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
36 address = 0x0
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
37 for i in self.sections[s]:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
38 i.address = address
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
39 if type(i) is ALabel:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
40 continue
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
41 if type(i) is Alignment:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
42 while (address % i.align) != 0:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
43 address += 1
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
44 continue
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
45 bts = i.encode()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
46 address += len(bts)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
47
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
48 def dump(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
49 self.backpatch()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
50 for s in sorted(self.sections.keys()):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
51 self.dumpSection(s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
52
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
53 def dumpSection(self, s):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
54 print('.section '+s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
55 for i in self.sections[s]:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
56 if type(i) is ALabel:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
57 print(i)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
58 elif type(i) is Alignment:
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
59 pass
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
60 else:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
61 addr = i.address
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
62 insword = i.encode()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
63 assert type(insword) is bytes
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
64 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
65 asm = str(i)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
66 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
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