Skip to content

Commit 1bc05cd

Browse files
committed
Fix (System)Verilog literal rendering of Index values
There was confusion between the size in bits and the type level argument to Index. Fixes #2813
1 parent 3b755b9 commit 1bc05cd

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

clash-lib/src/Clash/Backend/SystemVerilog.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-|
22
Copyright : (C) 2015-2016, University of Twente,
33
2017-2018, Google Inc.,
4-
2021-2023, QBayLogic B.V.,
4+
2021-2024, QBayLogic B.V.,
55
2022 , Google Inc.
66
License : BSD2 (see the file LICENSE)
77
Maintainer : QBayLogic B.V. <[email protected]>
@@ -65,7 +65,7 @@ import Clash.Netlist.Types hiding (intWidth, usages,
6565
import Clash.Netlist.Util
6666
import Clash.Signal.Internal (ActiveEdge (..))
6767
import Clash.Util
68-
(SrcSpan, noSrcSpan, curLoc, makeCached, indexNote)
68+
(SrcSpan, clogBase, noSrcSpan, curLoc, makeCached, indexNote)
6969
import Clash.Util.Graph (reverseTopSort)
7070

7171
-- | State for the 'Clash.Backend.SystemVerilog.SystemVerilogM' monad:
@@ -1228,7 +1228,9 @@ expr_ _ (BlackBoxE pNm _ _ _ _ bbCtx _)
12281228
expr_ _ (BlackBoxE pNm _ _ _ _ bbCtx _)
12291229
| pNm == "Clash.Sized.Internal.Index.fromInteger#"
12301230
, [Literal _ (NumLit n), Literal _ i] <- extractLiterals bbCtx
1231-
= exprLitSV (Just (Index (fromInteger n),fromInteger n)) i
1231+
, Just k <- clogBase 2 n
1232+
, let k' = max 1 k
1233+
= exprLitSV (Just (Index (fromInteger n),k')) i
12321234

12331235
expr_ b (BlackBoxE _ libs imps inc bs bbCtx b') =
12341236
parenIf (b || b') (Ap (renderBlackBox libs imps inc bs bbCtx <*> pure 0))

clash-lib/src/Clash/Backend/Verilog.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import Clash.Netlist.Types as N hiding (intWidth, usages,
8282
import Clash.Netlist.Util
8383
import Clash.Signal.Internal (ActiveEdge (..))
8484
import Clash.Util
85-
(SrcSpan, noSrcSpan, curLoc, indexNote, makeCached)
85+
(SrcSpan, clogBase, noSrcSpan, curLoc, indexNote, makeCached)
8686

8787
-- | State for the 'Clash.Backend.Verilog.VerilogM' monad:
8888
data VerilogState =
@@ -1151,7 +1151,9 @@ expr_ _ (BlackBoxE pNm _ _ _ _ bbCtx _)
11511151
expr_ _ (BlackBoxE pNm _ _ _ _ bbCtx _)
11521152
| pNm == "Clash.Sized.Internal.Index.fromInteger#"
11531153
, [Literal _ (NumLit n), Literal _ i] <- extractLiterals bbCtx
1154-
= exprLit undefValue (Just (Index (fromInteger n),fromInteger n)) i
1154+
, Just k <- clogBase 2 n
1155+
, let k' = max 1 k
1156+
= exprLit undefValue (Just (Index (fromInteger n),k')) i
11551157

11561158
expr_ b (BlackBoxE _ libs imps inc bs bbCtx b') = do
11571159
parenIf (b || b') (Ap (renderBlackBox libs imps inc bs bbCtx <*> pure 0))

0 commit comments

Comments
 (0)