annotate python/outstream.py @ 320:84d67cce67b7

Working burg
author Windel Bouwman
date Sun, 19 Jan 2014 16:09:44 +0100
parents 56e6ff84f646
children 582a1aaa3983
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
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
36
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
37 class OutputStream:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
38 def __init__(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
39 self.sections = {}
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
40 self.currentSection = None
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 219
diff changeset
41
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
42 def emit(self, item):
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
43 assert self.currentSection
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
44 self.currentSection.emit(item)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
45
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
46 def selectSection(self, sname):
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
47 self.currentSection = self.getSection(sname)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
48
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
49 def getLabelAddress(self, lname):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
50 assert isinstance(lname, str)
234
Windel Bouwman
parents: 225
diff changeset
51 for s in self.sections.values():
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
52 for i in s.instructions:
234
Windel Bouwman
parents: 225
diff changeset
53 if type(i) is Label:
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
54 if i.name == lname:
234
Windel Bouwman
parents: 225
diff changeset
55 return i.address
Windel Bouwman
parents: 225
diff changeset
56 return 0
Windel Bouwman
parents: 225
diff changeset
57
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
58 def getSection(self, name):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
59 if not name in self.sections:
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
60 self.sections[name] = Section()
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
61 return self.sections[name]
e41e4109addd Added current position arrow
Windel Bouwman
parents: 237
diff changeset
62
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
63 def backpatch(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
64 """ Fixup references to other parts in the assembler """
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
65 for s in self.sections.values():
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
66 address = s.address
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
67 for i in s.instructions:
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
68 i.address = address
234
Windel Bouwman
parents: 225
diff changeset
69 i.resolve(self.getLabelAddress)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
70 bts = i.encode()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
71 address += len(bts)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
72
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
73 class OutputStreamWriter:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
74 def __init__(self, extra_indent=''):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
75 self.extra_indent = extra_indent
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
76
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
77 def dump(self, stream, f):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
78 stream.backpatch()
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
79 stream.backpatch()
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
80 for s in sorted(stream.sections.keys()):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
81 # print('.section '+ s)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
82 self.dumpSection(stream.sections[s], f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
83
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
84 def dumpSection(self, s, f):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
85 for i in s.instructions:
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
86 if type(i) is DebugInfo:
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
87 continue
234
Windel Bouwman
parents: 225
diff changeset
88 addr = i.address
Windel Bouwman
parents: 225
diff changeset
89 insword = i.encode()
Windel Bouwman
parents: 225
diff changeset
90 assert type(insword) is bytes
Windel Bouwman
parents: 225
diff changeset
91 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
Windel Bouwman
parents: 225
diff changeset
92 asm = str(i)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
93 if len(insword) == 0:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
94 print(' {}'.format(asm), file=f)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
95 else:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
96 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm), file=f)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
97
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 259
diff changeset
98
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
99 class TextOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
100 pass
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
101
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 259
diff changeset
102
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
103 class BinOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
104
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
105 @property
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
106 def Data(self):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
107 d = self.dump()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
108 return bytes(d)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
109
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
110 def dump(self):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
111 self.backpatch()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
112 self.backpatch()
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
113 section = self.sections[list(self.sections.keys())[0]]
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
114 return section.to_bytes()