11{-# LANGUAGE UndecidableInstances #-}
22{-# LANGUAGE FlexibleInstances #-}
33{-# LANGUAGE RankNTypes #-}
4+ {-# LANGUAGE DeriveGeneric #-}
45module Data.BitCode where
56
67import Data.Word (Word32 , Word64 )
78import Data.Maybe (catMaybes )
89import Data.Bits (FiniteBits , finiteBitSize , countLeadingZeros )
910
11+ import GHC.Generics (Generic )
12+ import Data.Binary (Binary )
1013
1114--- Bit Codes ------------------------------------------------------------------
1215-- see BitCodes.h (e.g. http://llvm.org/docs/doxygen/html/BitCodes_8h_source.html)
@@ -29,12 +32,16 @@ data EncVal = Fixed !Val -- code 1 fixed value
2932 -- when reading an array, the first is a vbr6 field indicating the length.
3033 | Char6 -- code 4 6-bit char
3134 | Blob -- code 5 note: the value for this is: [vbr6:val,pad32bit,8bit array,pad32bit]
32- deriving Show
35+ deriving (Show , Generic )
36+
37+ instance Binary EncVal
3338
3439-- | Operators for abbreviated records, are encoded as either literal (1) or encoded value (0).
3540data Op = Lit ! Val -- [1,vbr8:val]
3641 | Enc ! EncVal -- [0,f3:enc(,vbr5:val)?], vbr5 value only if given.
37- deriving Show
42+ deriving (Show , Generic )
43+
44+ instance Binary Op
3845
3946-- | The Fields contained in an abbreviated record can be one of the following.
4047data Field = Vbr ! Int ! Val
@@ -47,7 +54,9 @@ data Field = Vbr !Int !Val
4754 -- abbreviated record matches the def abbrev. This could be considered
4855 -- a TODO, as it would be an improvement to enforce the that AbbrevRecord
4956 -- matches the actuall DefAbbrev.
50- deriving Show
57+ deriving (Show , Generic )
58+
59+ instance Binary Field
5160
5261-- | Bit Code Data consists of a series of blocks. Their interpretation is dependent
5362-- on the container they are in. The top level blocks are emitted with an abbreviation
@@ -73,7 +82,9 @@ data BitCode
7382 , aRecordFields :: ! [Field ]
7483 }
7584 | Located { srcLoc :: (Loc , Loc ), unLoc :: ! BitCode }
76- deriving Show
85+ deriving (Show , Generic )
86+
87+ instance Binary BitCode
7788
7889-- | BitCode contains some additional control information,
7990-- like abbreviation records, or the BLOCKINFO block, which
@@ -85,7 +96,9 @@ data BitCode
8596data NBitCode
8697 = NBlock ! BlockId ! [NBitCode ]
8798 | NRec ! Code ! [Val ]
88- deriving Show
99+ deriving (Show , Generic )
100+
101+ instance Binary NBitCode
89102
90103idOrCode :: NBitCode -> Int
91104idOrCode (NBlock i _) = i
0 commit comments