| 1 |
{-| |
| 2 |
Copyright : (C) 2021-2022, QBayLogic |
| 3 |
License : BSD2 (see the file LICENSE) |
| 4 |
Maintainer : QBayLogic B.V. <[email protected]> |
| 5 |
-} |
| 6 |
|
| 7 |
{-# LANGUAGE CPP #-} |
| 8 |
{-# LANGUAGE LambdaCase #-} |
| 9 |
{-# LANGUAGE NamedFieldPuns #-} |
| 10 |
{-# LANGUAGE QuasiQuotes #-} |
| 11 |
|
| 12 |
module Clash.GHCi.Common |
| 13 |
( checkImportDirs |
| 14 |
, checkMonoLocalBinds |
| 15 |
, checkMonoLocalBindsMod |
| 16 |
, getMainTopEntity |
| 17 |
) where |
| 18 |
|
| 19 |
-- Clash |
| 20 |
import Clash.Driver.Types (ClashOpts (..), ClashDesign(..)) |
| 21 |
import Clash.Netlist.Types (TopEntityT(..)) |
| 22 |
|
| 23 |
-- The GHC interface |
| 24 |
#if MIN_VERSION_ghc(9,0,0) |
| 25 |
import qualified GHC.Data.EnumSet as GHC (member) |
| 26 |
import GHC.Utils.Panic (GhcException (..), throwGhcException) |
| 27 |
import qualified GHC |
| 28 |
(DynFlags, ModSummary (..), extensionFlags, moduleName, moduleNameString) |
| 29 |
#else |
| 30 |
import qualified EnumSet as GHC (member) |
| 31 |
import Panic (GhcException (..), throwGhcException) |
| 32 |
import qualified GHC (DynFlags, ModSummary (..), Module (..), |
| 33 |
extensionFlags, moduleNameString) |
| 34 |
#endif |
| 35 |
import Clash.Core.Name (nameOcc) |
| 36 |
import Clash.Core.Var (varName) |
| 37 |
import Clash.Normalize.Util (collectCallGraphUniques, callGraph) |
| 38 |
import qualified Clash.Util.Interpolate as I |
| 39 |
import Clash.Util (ClashException(..), noSrcSpan) |
| 40 |
import Clash.Unique (getUnique) |
| 41 |
import Control.Exception (throw) |
| 42 |
import Data.List (isSuffixOf) |
| 43 |
import qualified Data.Text as Text |
| 44 |
import qualified Data.HashSet as HashSet |
| 45 |
import qualified GHC.LanguageExtensions as LangExt (Extension (..)) |
| 46 |
import GHC.Stack (HasCallStack) |
| 47 |
|
| 48 |
import Control.Monad (forM_, unless, when) |
| 49 |
import System.Directory (doesDirectoryExist) |
| 50 |
import System.IO (hPutStrLn, stderr) |
| 51 |
|
| 52 |
getMainTopEntity |
| 53 |
:: HasCallStack |
| 54 |
=> String |
| 55 |
-- ^ Module name |
| 56 |
-> ClashDesign |
| 57 |
-> String |
| 58 |
-- ^ string passed with -main-is |
| 59 |
-> IO (TopEntityT, [TopEntityT]) |
| 60 |
-- ^ Throws exception if -main-is was set, but no such top entity was found. |
| 61 |
-- Otherwise, returns main top entity and all top entities (transitively) used |
| 62 |
-- in the main top entity. |
| 63 |
getMainTopEntity modName design nm = |
| 64 |
case filter isNm (designEntities design) of |
| 65 |
[] -> throw $ ClashException noSrcSpan [I.i| |
| 66 |
Could not find top entity called #{show nm} in #{show modName} |
| 67 |
|] Nothing |
| 68 |
[t] -> |
| 69 |
let |
| 70 |
bindingMap = designBindings design |
| 71 |
closure0 = collectCallGraphUniques (callGraph bindingMap (topId t)) |
| 72 |
closure1 = HashSet.delete (getUnique (topId t)) closure0 |
| 73 |
in |
| 74 |
pure (t, filter ((`HashSet.member` closure1) . getUnique . topId) (designEntities design)) |
| 75 |
ts -> |
| 76 |
error $ [I.i| |
| 77 |
Internal error: multiple top entities called #{nm} (#{map topId ts}) |
| 78 |
found in #{modName}. |
| 79 |
|] |
| 80 |
where |
| 81 |
isNm (TopEntityT{topId}) = |
| 82 |
let topIdNm = Text.unpack (nameOcc (varName topId)) in |
| 83 |
topIdNm == nm || ('.':nm) `isSuffixOf` topIdNm |
| 84 |
|
| 85 |
-- | Checks whether MonoLocalBinds and MonomorphismRestricton language extensions |
| 86 |
-- are enabled or not in modules. |
| 87 |
checkMonoLocalBindsMod :: GHC.ModSummary -> IO () |
| 88 |
checkMonoLocalBindsMod x = do |
| 89 |
unless (active LangExt.MonoLocalBinds . GHC.ms_hspp_opts $ x) |
| 90 |
(hPutStrLn stderr $ msg LangExt.MonoLocalBinds x) |
| 91 |
unless (active LangExt.MonomorphismRestriction . GHC.ms_hspp_opts $ x) |
| 92 |
(hPutStrLn stderr $ msg LangExt.MonomorphismRestriction x) |
| 93 |
where |
| 94 |
msg ext = messageWith ext . GHC.moduleNameString . GHC.moduleName . GHC.ms_mod |
| 95 |
|
| 96 |
-- | Checks whether MonoLocalBinds and MonomorphismRestriction language extensions |
| 97 |
-- are enabled when generating the HDL directly e.g. in GHCi. modules. |
| 98 |
checkMonoLocalBinds :: GHC.DynFlags -> IO () |
| 99 |
checkMonoLocalBinds dflags = do |
| 100 |
unless (active LangExt.MonoLocalBinds dflags) |
| 101 |
(hPutStrLn stderr $ messageWith LangExt.MonoLocalBinds "") |
| 102 |
unless (active LangExt.MonomorphismRestriction dflags) |
| 103 |
(hPutStrLn stderr $ messageWith LangExt.MonomorphismRestriction "") |
| 104 |
|
| 105 |
messageWith :: LangExt.Extension -> String -> String |
| 106 |
messageWith ext srcModule |
| 107 |
| srcModule == [] = msgStem ++ "." |
| 108 |
| otherwise = msgStem ++ " in module: " ++ srcModule |
| 109 |
where |
| 110 |
msgStem = "Warning: Extension " <> show ext <> |
| 111 |
" is disabled. This might lead to unexpected logic duplication" |
| 112 |
|
| 113 |
active :: LangExt.Extension -> GHC.DynFlags -> Bool |
| 114 |
active ext = GHC.member ext . GHC.extensionFlags |
| 115 |
|
| 116 |
checkImportDirs :: Foldable t => ClashOpts -> t FilePath -> IO () |
| 117 |
checkImportDirs opts idirs = when (opt_checkIDir opts) $ |
| 118 |
forM_ idirs $ \dir -> do |
| 119 |
doesDirectoryExist dir >>= \case |
| 120 |
False -> throwGhcException (CmdLineError $ "Missing directory: " ++ dir) |
| 121 |
_ -> return () |