Skip to content

Commit

Permalink
Verify blackbox context when rendering includes
Browse files Browse the repository at this point in the history
Fixes #1945
  • Loading branch information
christiaanb committed Oct 8, 2021
1 parent 10af0ef commit e361a8b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2021-10-07T16_55_54+02_00_fix1945
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FIXED: valid field of TemplateFunction isn't checked for includes [#1945](https://github.com/clash-lang/clash-compiler/issues/1945)
8 changes: 8 additions & 0 deletions clash-lib/src/Clash/Netlist/BlackBox/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ renderBlackBox libs imps includes bb bbCtx = do
layout = LayoutOptions (AvailablePerLine 120 0.4)
nms <-
forM includes $ \((nm,_),inc) -> do
case verifyBlackBoxContext bbCtx inc of
Nothing -> return ()
Just err0 -> do
sp <- getSrcSpan
let err1 = concat [ "Couldn't instantiate blackbox for "
, Data.Text.unpack (bbName bbCtx), ". Verification "
, "procedure reported:\n\n" ++ err0 ]
throw (ClashException sp ($(curLoc) ++ err1) Nothing)
let bbCtx' = bbCtx {bbQsysIncName = map Text.toStrict nms'}
incForHash <- onBlackBox (renderTemplate bbCtx')
(\_name _hash (N.TemplateFunction _ _ f) -> do
Expand Down
4 changes: 4 additions & 0 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ runClashTest = defaultMain $ clashTestRoot
primitive for WrongReference.myMultiplyX. These names should be
the same. |])
}
, runTest "T1945" def{
hdlTargets=[VHDL]
, expectClashFail=Just (def, "Template function for returned False")
}
]
, clashTestGroup "InvalidPrimitive"
[ runTest "InvalidPrimitive" def{
Expand Down
39 changes: 39 additions & 0 deletions tests/shouldfail/BlackBox/T1945.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{-# LANGUAGE OverloadedStrings #-}
module T1945 where

import Clash.Annotations.Primitive (Primitive(..), HDL(..), hasBlackBox)
import Clash.Netlist.Types (TemplateFunction (..))
import Clash.Prelude

import Data.String.Interpolate (i)
import Data.String.Interpolate.Util (unindent)

bb :: a -> a
bb x = x
{-# NOINLINE bb #-}
{-# ANN bb hasBlackBox #-}
{-# ANN bb (InlinePrimitive [VHDL,Verilog,SystemVerilog] $ unindent [i|
[ { "BlackBox" :
{ "name" : "T1945.bb",
"kind" : "Expression",
"template": "~ARG[0]",
"includes" :
[ { "extension" : "bin"
, "name" : "mem"
, "format" : "Haskell"
, "templateFunction" : "T1945.tf"}

]
}
}
]
|]) #-}

tf :: TemplateFunction
tf = TemplateFunction used valid (const (pure "QQ"))
where
used = [0]
valid = const False

topEntity :: Bool -> Bool
topEntity = bb

0 comments on commit e361a8b

Please sign in to comment.