| Copyright | (c) Matthew Naylor 2019 |
|---|---|
| License | MIT |
| Maintainer | mattfn@gmail.com |
| Stability | experimental |
| Safe Haskell | Safe-Inferred |
| Language | GHC2021 |
Blarney.BitScan
Description
Dynamically typed pattern matching on bit-strings, similar to scanf.
Here's an instruction decoder for a tiny subset of RISC-V, written
using the match and ==> combinators provided by the module:
-- Semantics of add instruction
add :: Bit 5 -> Bit 5 -> Bit 5 -> Action ()
add rs2 rs1 rd = display "add " rd ", " rs1 ", " rs2
-- Semantics of addi instruction
addi :: Bit 12 -> Bit 5 -> Bit 5 -> Action ()
addi imm rs1 rd = display "addi " rd ", " rs1 ", " imm
-- Semantics of store instruciton
sw :: Bit 12 -> Bit 5 -> Bit 5 -> Action ()
sw imm rs2 rs1 = display "sw " rs2 ", " rs1 "[" imm "]"
top :: Action ()
top = do
let instr :: Bit 32 = 0b1000000_00001_00010_010_00001_0100011
match instr
[
"0000000 rs2[4:0] rs1[4:0] 000 rd[4:0] 0110011" ==> add,
" imm[11:0] rs1[4:0] 000 rd[4:0] 0010011" ==> addi,
"imm[11:5] rs2[4:0] rs1[4:0] 010 imm[4:0] 0100011" ==> sw
]
finish
Synopsis
- (==>) :: RHS rhs => String -> rhs -> Alt
- data Alt
- match :: KnownNat n => Bit n -> [Alt] -> Action ()
- matchDefault :: KnownNat n => Bit n -> [Alt] -> Action () -> Action ()
- matchOpts :: KnownNat n => MatchOpts -> Bit n -> [Alt] -> Action ()
- data MatchOpts = MatchOpts {
- matchOptDefault :: Maybe (Action ())
- matchOptUseSimpleAlgorithm :: Bool
- matchMap :: (KnownNat n, Ord tag) => Bool -> [(String, tag)] -> Bit n -> (TagMap tag, FieldMap)
- type TagMap tag = Map tag (Bit 1)
- type Tag a = (Ord a, Enum a, Bounded a)
- packTagMap :: (KnownNat n, Tag tag) => TagMap tag -> Bit n
- type FieldMap = Map String (Option BitList)
- matchSel :: KnownNat n => [(String, tag)] -> SelMap n
- type SelMap n = Map String (Bit n -> BitList)
- hasBitField :: FieldMap -> String -> Bit 1
- getBitField :: KnownNat n => FieldMap -> String -> Option (Bit n)
- getBitFieldStrict :: KnownNat n => FieldMap -> String -> Option (Bit n)
- getBitFieldSel :: (KnownNat n, KnownNat m) => SelMap n -> String -> Bit n -> Bit m
- makeFieldSelector :: (KnownNat n, KnownNat m) => [(String, tag)] -> String -> Bit n -> Bit m
Documentation
(==>) :: RHS rhs => String -> rhs -> Alt infix 1 Source #
Infix operator to construct a match alternative
match :: KnownNat n => Bit n -> [Alt] -> Action () Source #
Match statement, with a subject and a list of alternatives
matchDefault :: KnownNat n => Bit n -> [Alt] -> Action () -> Action () Source #
Match statement, with a default case
matchOpts :: KnownNat n => MatchOpts -> Bit n -> [Alt] -> Action () Source #
General parameterised match statement
Match options
Constructors
| MatchOpts | |
Fields
| |
matchMap :: (KnownNat n, Ord tag) => Bool -> [(String, tag)] -> Bit n -> (TagMap tag, FieldMap) Source #
Compute tag and field maps given list of pattern/tag pairs. This is a relaxed version which fills field gaps with zero, and sign-extends each field to the length of the longest instance of that field.
packTagMap :: (KnownNat n, Tag tag) => TagMap tag -> Bit n Source #
Pack the tag map into a bit vector
matchSel :: KnownNat n => [(String, tag)] -> SelMap n Source #
Compute a field selector function for each field that occupies precisely the same bits in every match alternative in which it occurs
getBitField :: KnownNat n => FieldMap -> String -> Option (Bit n) Source #
Get field value from a map, and cast to required size using truncation or sign-extension.
getBitFieldStrict :: KnownNat n => FieldMap -> String -> Option (Bit n) Source #
Get field value from a map, and raise an error the size is incorrect