@@ -23,11 +23,14 @@ import Data.ByteString.Base16 (encode)
2323
2424import Data.BitCode
2525
26+ import Data.Sequence (Seq (.. ), fromList )
27+ import Data.Foldable (toList )
28+
2629-- * BitCode
27- data BitC = BitC { _words :: Int , _bits :: Int , _body :: Bits } deriving (Show )
30+ data BitC = BitC { _words :: Int , _bits :: Int , _body :: Seq Bool } deriving (Show )
2831
2932instance Monoid BitC where
30- mempty = BitC 0 0 []
33+ mempty = BitC 0 0 mempty
3134 (BitC w l bs) `mappend` (BitC w' l' bs') = BitC (w+ w' + ((l+ l') `div` 32 )) ((l+ l') `mod` 32 ) (bs `mappend` bs')
3235
3336-- * BitCode Writer Monad (with access to the current state)
@@ -40,7 +43,7 @@ evalBitCodeWriter :: BitCodeWriter a -> a
4043evalBitCodeWriter = fst . runBitCodeWriter
4144
4245execBitCodeWriter :: BitCodeWriter a -> Bits
43- execBitCodeWriter = _body . snd . runBitCodeWriter
46+ execBitCodeWriter = toList . _body . snd . runBitCodeWriter
4447
4548class ToBits a where
4649 emit :: a -> BitCodeWriter ()
@@ -74,7 +77,7 @@ instance Monoid (BitCodeWriter ()) where
7477tell :: Bits -> BitCodeWriter ()
7578tell bs = BitCode $ \ b -> (() , b `mappend` b')
7679 where n = length bs
77- b' = BitC (n `div` 32 ) (n `mod` 32 ) bs
80+ b' = BitC (n `div` 32 ) (n `mod` 32 ) (fromList bs)
7881
7982-- | Get the number of words and bits in the stream.
8083ask :: BitCodeWriter (Int , Int )
@@ -86,6 +89,7 @@ partition _ [] = []
8689partition n xs | length xs < n = [xs]
8790partition n xs | otherwise = h: partition n t
8891 where (h,t) = (take n xs, drop n xs)
92+
8993toBytes :: Bits -> [Word8 ]
9094toBytes = map toFiniteBits . partition 8
9195 where toFiniteBits :: (FiniteBits a ) => Bits -> a
@@ -100,7 +104,7 @@ ppBitCodeWriter :: BitCodeWriter () -> PP.Doc
100104ppBitCodeWriter w = PP. vcat [ PP. text " * Bitcode"
101105 , PP. text " words =" PP. <+> PP. int (_words b)
102106 , PP. text " bits =" PP. <+> PP. int (_bits b)
103- , PP. text " body =" PP. <+> ppBitsAndBytes (_body b)
107+ , PP. text " body =" PP. <+> ppBitsAndBytes (toList $ _body b)
104108 ]
105109 where b = snd (runBitCodeWriter w)
106110 toBitString :: Bits -> String
0 commit comments