Skip to content

Commit

Permalink
Remove 'renderVoids'
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnbastiaan committed Nov 17, 2019
1 parent 7945fcc commit 3e0ce96
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 51 deletions.
91 changes: 40 additions & 51 deletions clash-lib/src/Clash/Netlist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -535,17 +535,17 @@ mkFunApp dstId fun args tickDecls = do
, length fArgTys == length args
-> do
argHWTys <- mapM (unsafeCoreTypeToHWTypeM' $(curLoc)) fArgTys
voidDecls <- renderVoids (zip (map Just argHWTys) args)

-- Filter out the arguments of hwtype `Void` and only translate them
-- to the intermediate HDL afterwards
let argsBundled = zip argHWTys (zip args fArgTys)
argsFiltered = filter (not . isVoid . fst) argsBundled
argsFiltered' = map snd argsFiltered
hWTysFiltered = filter (not . isVoid) argHWTys
(argExprs,argDecls) <- second concat . unzip <$>
mapM (\(e,t) -> mkExpr False (Left dstId) t e)
argsFiltered'
argExprDecls <-
mapM (\(e,t) -> mkExpr False (Left dstId) t e) (zip args fArgTys)

-- Filter void arguments, but make sure to render their declarations:
let
argTypeExprDecls = zip argHWTys argExprDecls
filteredTypeExprDecls =
filter (not . isVoid . fst) argTypeExprDecls
(hWTysFiltered, unzip -> (argExprs, concat -> argDecls)) =
unzip filteredTypeExprDecls

dstHWty <- unsafeCoreTypeToHWTypeM' $(curLoc) fResTy
env <- Lens.use hdlDir
mkId <- Lens.use mkIdentifierFn
Expand All @@ -562,7 +562,7 @@ mkFunApp dstId fun args tickDecls = do
instDecls <- mkTopUnWrapper fun annM man (dstId,dstHWty)
(zip argExprs hWTysFiltered)
tickDecls
return (argDecls ++ instDecls ++ voidDecls)
return (argDecls ++ instDecls)

| otherwise -> error $ $(curLoc) ++ "under-applied TopEntity"
_ -> do
Expand All @@ -572,18 +572,21 @@ mkFunApp dstId fun args tickDecls = do
(_,_,_,Component compName compInps co _) <- preserveVarEnv $ genComponent fun
let argTys = map (termType tcm) args
argHWTys <- mapM coreTypeToHWTypeM' argTys
voidDecls <- renderVoids (zip argHWTys args)
-- Filter out the arguments of hwtype `Void` and only translate
-- them to the intermediate HDL afterwards
let argsBundled = zip argHWTys (zip args argTys)
argsFiltered = filter (maybe True (not . isVoid) . fst) argsBundled
argsFiltered' = map snd argsFiltered
tysFiltered = map snd argsFiltered'
compOutp = snd <$> listToMaybe co
if length tysFiltered == length compInps

(argExprs, concat -> argDecls) <- unzip <$>
mapM (\(e,t) -> mkExpr False (Left dstId) t e) (zip args argTys)

-- Filter void arguments, but make sure to render their declarations:
let
argTypeExprDecls = zip3 argHWTys argTys argExprs
filteredTypeExprDecls =
filter (\(t, _, _) -> not (isVoidMaybe True t)) argTypeExprDecls
(unzip3 -> (_, argTysFiltered, argsFiltered)) = filteredTypeExprDecls

let compOutp = snd <$> listToMaybe co
if length argTysFiltered == length compInps
then do
(argExprs,argDecls) <- fmap (second concat . unzip) $! mapM (\(e,t) -> mkExpr False (Left dstId) t e) argsFiltered'
(argExprs',argDecls') <- (second concat . unzip) <$> mapM (toSimpleVar dstId) (zip argExprs tysFiltered)
(argExprs',argDecls') <- (second concat . unzip) <$> mapM (toSimpleVar dstId) (zip argsFiltered argTysFiltered)
let inpAssigns = zipWith (\(i,t) e -> (Identifier i Nothing,In,t,e)) compInps argExprs'
outpAssign = case compOutp of
Nothing -> []
Expand All @@ -593,7 +596,7 @@ mkFunApp dstId fun args tickDecls = do
instLabel2 <- affixName instLabel1
instLabel3 <- mkUniqueIdentifier Basic instLabel2
let instDecl = InstDecl Entity Nothing compName instLabel3 [] (outpAssign ++ inpAssigns)
return (voidDecls ++ argDecls ++ argDecls' ++ tickDecls ++ [instDecl])
return (argDecls ++ argDecls' ++ tickDecls ++ [instDecl])
else error $ $(curLoc) ++ "under-applied normalized function"
Nothing -> case args of
-- TODO: Figure out what to do with zero-width constructs
Expand Down Expand Up @@ -784,18 +787,6 @@ mkProjection mkDec bndr scrut altTy alt@(pat,v) = do
nestModifier m Nothing = m
nestModifier (Just m1) (Just m2) = Just (Nested m1 m2)

-- | Given a list of (potentially) zero-width terms, render them if there is
-- a blackbox insisting they be rendered.
renderVoids :: [(Maybe HWType, Term)] -> NetlistMonad [Declaration]
renderVoids terms = do
tcm <- Lens.use tcCache
concatMapM
(renderVoid0 tcm)
(map snd (filter (maybe False isVoid . fst) terms))
where
renderVoid0 :: TyConMap -> Term -> NetlistMonad [Declaration]
renderVoid0 tcm t = snd <$> mkExpr False (Left "__ignored__") (termType tcm t) t

-- | Generate an expression for a DataCon application occurring on the RHS of a let-binder
mkDcApplication
:: HasCallStack
Expand All @@ -815,21 +806,19 @@ mkDcApplication dstHType bndr dc args = do
let argTys = map (termType tcm) args
argNm <- either return (\b -> extendIdentifier Extended (nameOcc (varName b)) "_dc_arg") bndr
argHWTys <- mapM coreTypeToHWTypeM' argTys
voidDecls <- renderVoids (zip argHWTys args)
-- Filter out the arguments of hwtype `Void` and only translate
-- them to the intermediate HDL afterwards
let argsBundled = zip argHWTys (zip args argTys)
(hWTysFiltered, argsFiltered) = unzip
(filter (maybe True (not . isVoid) . fst) argsBundled)

(argExprs, argDecls) <-
fmap
(second concat . unzip) $!
(mapM
(\(e,t) -> mkExpr False (Left argNm) t e)
argsFiltered)

fmap (,argDecls++voidDecls) $! case (hWTysFiltered,argExprs) of

argExprDecls <-
mapM (\(e,t) -> mkExpr False (Left argNm) t e) (zip args argTys)

-- Filter void arguments, but make sure to render their declarations:
let
argTypeExprDecls = zip argHWTys argExprDecls
filteredTypeExprDecls =
filter (not . isVoidMaybe True . fst) argTypeExprDecls
(hWTysFiltered, unzip -> (argExprs, concat -> argDecls)) =
unzip filteredTypeExprDecls

fmap (,argDecls) $! case (hWTysFiltered,argExprs) of
-- Is the DC just a newtype wrapper?
([Just argHwTy],[argExpr]) | argHwTy == dstHType ->
return (HW.DataCon dstHType (DC (Void Nothing,-1)) [argExpr])
Expand Down
4 changes: 4 additions & 0 deletions clash-lib/src/Clash/Netlist/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ stripFiltered (FilteredHWType hwty _filtered) = hwty
flattenFiltered :: FilteredHWType -> [[Bool]]
flattenFiltered (FilteredHWType _hwty filtered) = map (map fst) filtered

isVoidMaybe :: Bool -> Maybe HWType -> Bool
isVoidMaybe dflt Nothing = dflt
isVoidMaybe _dflt (Just t) = isVoid t

-- | Determines if type is a zero-width construct ("void")
isVoid :: HWType -> Bool
isVoid Void {} = True
Expand Down

0 comments on commit 3e0ce96

Please sign in to comment.