annotate python/ppci/core/bitreader.py @ 101:af0d7913677a

Fixes and splitting into 3 stage
author windel
date Mon, 24 Dec 2012 17:55:08 +0100
parents 4a37d6992bd3
children ed230e947dc6
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