annotate python/outstream.py @ 249:e41e4109addd

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