From fed11369d95c1fbce9c3c22cadde36bd67ea621a Mon Sep 17 00:00:00 2001 From: Nadia Yvette Chambers Date: Sat, 29 Jul 2023 20:24:21 -0400 Subject: [PATCH] Add and fix up testcases for hLit/oLit don't cares https://github.com/clash-lang/clash-compiler/pull/2505#discussion_r1231776998 Also adjusts comments/documentation so the precise strings are shown. --- .../src/Clash/Sized/Internal/BitVector.hs | 14 ++++++++------ clash-prelude/tests/Clash/Tests/BitVector.hs | 4 ++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/clash-prelude/src/Clash/Sized/Internal/BitVector.hs b/clash-prelude/src/Clash/Sized/Internal/BitVector.hs index 803d6bc007..8331c03fb1 100644 --- a/clash-prelude/src/Clash/Sized/Internal/BitVector.hs +++ b/clash-prelude/src/Clash/Sized/Internal/BitVector.hs @@ -558,8 +558,9 @@ read# cs0 = (fromIntegral (length cs1), BV m v) ++ show c ++ " in input: " ++ cs0 -- | Create a hexadecimal literal. --- >>> $(hLit "dead") --- 0b1101111010101101 +-- ghci> $(hLit "dead") +-- 0b1101_1110_1010_1101 +-- it :: BitVector 16 -- -- Don't care digits set 4 bits in the undefined mask. hLit :: String -> ExpQ @@ -580,16 +581,17 @@ read16# cs0 = (fromIntegral $ 4 * length cs1, BV m v) v = combineHexDigits vs m = combineHexDigits ms -- The dot is a don't care, which applies to a whole digit. + readHexDigit '.' = (0, 0xf) readHexDigit c = case readHex [c] of [(n, "")] -> (n, 0) - [(_, ".")] -> (0, 0xf) _ -> error $ "Clash.Sized.Internal.hLit: unknown character: " ++ show c ++ " in input: " ++ cs0 -- | Create an octal literal. --- >>> $(oLit "5234") --- 0b101011010100 +-- ghci> $(oLit "5234") +-- 0b1010_1001_1100 +-- it :: BitVector 12 -- -- Don't care digits set 3 bits in the undefined mask. oLit :: String -> ExpQ @@ -610,9 +612,9 @@ read8# cs0 = (fromIntegral $ 3 * length cs1, BV m v) v = combineOctDigits vs m = combineOctDigits ms -- The dot is a don't care, which applies to a whole digit. + readOctDigit '.' = (0, 0o7) readOctDigit c = case readOct [c] of [(n, "")] -> (n, 0) - [(_, ".")] -> (0, 0o7) _ -> error $ "Clash.Sized.Internal.oLit: unknown character: " ++ show c ++ " in input: " ++ cs0 diff --git a/clash-prelude/tests/Clash/Tests/BitVector.hs b/clash-prelude/tests/Clash/Tests/BitVector.hs index 51a7feb7ba..8be4200d54 100644 --- a/clash-prelude/tests/Clash/Tests/BitVector.hs +++ b/clash-prelude/tests/Clash/Tests/BitVector.hs @@ -126,9 +126,13 @@ tests = localOption (Q.QuickCheckMaxRatio 2) $ testGroup "All" , testCase "show13" $ show @(BitVector 9) $(bLit "10001010.") @?= "0b1_0001_010." , testCase "show14" $ show @(BitVector 16) $(hLit "dead") @?= "0b1101_1110_1010_1101" + , testCase "show14" $ show @(BitVector 16) $(hLit "de.d") @?= "0b1101_1110_...._1101" , testCase "show15" $ show @(BitVector 16) $(hLit "beef") @?= "0b1011_1110_1110_1111" + , testCase "show15" $ show @(BitVector 16) $(hLit ".eef") @?= "0b...._1110_1110_1111" , testCase "show16" $ show @(BitVector 12) $(oLit "7734") @?= "0b1111_1101_1100" + , testCase "show16" $ show @(BitVector 12) $(oLit "77.4") @?= "0b1111_11.._.100" , testCase "show17" $ show @(BitVector 12) $(oLit "5324") @?= "0b1010_1101_0100" + , testCase "show17" $ show @(BitVector 12) $(oLit ".324") @?= "0b...0_1101_0100" ] ]