annotate python/ppci/core/bitreader.py @ 104:ed230e947dc6

Added hexviewer
author windel
date Sun, 30 Dec 2012 22:31:55 +0100
parents 4a37d6992bd3
children 6a303f835c6d
rev   line source
94
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
1 from .errors import CompilerException
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
2 import struct
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
3
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
4 def bits(f):
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
5 while True:
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
6 Byte = f.read(1)
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
7 for i in range(8):
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
8 yield Byte & 0x1
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
9 Byte >>= 1
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
10
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
11 class BitReader:
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
12 def __init__(self, f):
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
13 self.stream = bits(f)
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
14 self.curword = None
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
15 def parseModule(self):
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
16 if self.read(8) != ord('B') or self.read(8) != ord('C'):
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
17 raise CompilerException('Invalid bitcode signature')
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
18 for bitsig in [0x0, 0xC, 0xE, 0xD]:
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
19 if self.read(4) != bitsig:
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
20 raise CompilerException('Invalid bitcode signature')
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
21
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
22 def read(self, numbits):
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
23 if numbits == 8:
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
24 b = self.stream.read(1)
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
25 print(b)
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
26 return int(b[0])
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
27 return 2
1be00bcfaabb Last parts before movage
windel
parents:
diff changeset
28
104
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
29 class BitstreamWriter:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
30 def __init__(self, f):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
31 self.f = f
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
32 self.u32 = 0
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
33 self.curpos = 0
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
34 def Emit1(self, val):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
35 self.Emit(val, 1)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
36 def Emit(self, val, numbits):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
37 """ Emits value using numbits bits """
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
38 if numbits == 1:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
39 if val != 0:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
40 self.u32 |= (0x1 << self.curpos)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
41 self.curpos += 1
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
42 if self.curpos == 32:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
43 self.writeWord()
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
44 elif numbits > 1:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
45 for i in range(numbits):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
46 if val & (1 << i) != 0:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
47 self.Emit1(1)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
48 else:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
49 self.Emit1(0)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
50 def writeWord(self):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
51 bts = struct.pack('<I', self.u32)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
52 self.f.write(bts)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
53 self.u32 = 0
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
54 self.curpos = 0
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
55 def flush(self):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
56 if self.curpos != 0:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
57 self.writeWord()
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
58
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
59 class BitcodeWriter:
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
60 def __init__(self):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
61 pass
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
62 def WriteModule(self, module):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
63 pass
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
64 def WriteModuleToFile(self, module, f):
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
65 s = BitstreamWriter(f)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
66 s.Emit(ord('B'), 8)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
67 s.Emit(ord('C'), 8)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
68 s.Emit(0x0, 4)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
69 s.Emit(0xC, 4)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
70 s.Emit(0xE, 4)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
71 s.Emit(0xD, 4)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
72 self.WriteModule(module)
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
73 s.flush()
ed230e947dc6 Added hexviewer
windel
parents: 95
diff changeset
74