annotate python/outstream.py @ 271:cf7d5fb7d9c8

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