annotate python/outstream.py @ 250:f5fba5b554d7

Removal of obsolete editor
author Windel Bouwman
date Sun, 28 Jul 2013 19:07:51 +0200
parents e41e4109addd
children 04c19282a5aa
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
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
28 def debugInfos(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
29 di = [i for i in self.instructions if isinstance(i, DebugInfo)]
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
30 return di
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
31
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
32 class OutputStream:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
33 def __init__(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
34 self.sections = {}
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
35 self.currentSection = None
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 219
diff changeset
36
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
37 def emit(self, item):
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
38 assert self.currentSection
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
39 self.currentSection.emit(item)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
40
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
41 def selectSection(self, sname):
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
42 self.currentSection = self.getSection(sname)
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 """
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
60 for s in self.sections.values():
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
61 address = s.address
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
62 for i in s.instructions:
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
63 i.address = address
234
Windel Bouwman
parents: 225
diff changeset
64 i.resolve(self.getLabelAddress)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
65 bts = i.encode()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
66 address += len(bts)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
67
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
68 def dump(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
69 self.backpatch()
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
70 self.backpatch()
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
71 for s in sorted(self.sections.keys()):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
72 self.dumpSection(s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
73
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
74 def dumpSection(self, s):
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
75 print('.section '+ s)
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
76 for i in self.sections[s].instructions:
234
Windel Bouwman
parents: 225
diff changeset
77 addr = i.address
Windel Bouwman
parents: 225
diff changeset
78 insword = i.encode()
Windel Bouwman
parents: 225
diff changeset
79 assert type(insword) is bytes
Windel Bouwman
parents: 225
diff changeset
80 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
Windel Bouwman
parents: 225
diff changeset
81 asm = str(i)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
82 if len(insword) == 0:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
83 print(' {}'.format(asm))
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
84 else:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
85 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
86
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
87 class TextOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
88 pass
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
89
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
90 class BinOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
91
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
92 @property
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
93 def Data(self):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
94 d = self.dump()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
95 return bytes(d)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
96
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
97 def dump(self):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
98 self.backpatch()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
99 self.backpatch()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
100 section = self.sections[list(self.sections.keys())[0]]
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
101 return section.to_bytes()
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
102
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
103