view python/ppci/core/bitreader.py @ 95:4a37d6992bd3

movage
author windel
date Mon, 24 Dec 2012 13:24:59 +0100
parents python/libs/compiler/core/bitreader.py@1be00bcfaabb
children ed230e947dc6
line wrap: on
line source

from .errors import CompilerException
import struct

def bits(f):
   while True:
      Byte = f.read(1)
      for i in range(8):
         yield Byte & 0x1
         Byte >>= 1

class BitReader:
   def __init__(self, f):
      self.stream = bits(f)
      self.curword = None
   def parseModule(self):
      if self.read(8) != ord('B') or self.read(8) != ord('C'):
         raise CompilerException('Invalid bitcode signature')
      for bitsig in [0x0, 0xC, 0xE, 0xD]:
         if self.read(4) != bitsig:
            raise CompilerException('Invalid bitcode signature')

   def read(self, numbits):
      if numbits == 8:
         b = self.stream.read(1)
         print(b)
         return int(b[0])
      return 2