| 1 |
{-| |
| 2 |
Copyright : (C) 2015-2016, University of Twente, |
| 3 |
2016-2017, Myrtle Software Ltd, |
| 4 |
2021, QBayLogic B.V., |
| 5 |
2022, Google Inc., |
| 6 |
License : BSD2 (see the file LICENSE) |
| 7 |
Maintainer : QBayLogic B.V. <[email protected]> |
| 8 |
-} |
| 9 |
|
| 10 |
{-# LANGUAGE CPP #-} |
| 11 |
|
| 12 |
module Clash.GHC.ClashFlags |
| 13 |
( parseClashFlags |
| 14 |
, flagsClash |
| 15 |
) |
| 16 |
where |
| 17 |
|
| 18 |
#if MIN_VERSION_ghc(9,0,0) |
| 19 |
import GHC.Driver.CmdLine |
| 20 |
import GHC.Utils.Panic |
| 21 |
import GHC.Types.SrcLoc |
| 22 |
#else |
| 23 |
import CmdLineParser |
| 24 |
import Panic |
| 25 |
import SrcLoc |
| 26 |
#endif |
| 27 |
|
| 28 |
import Control.Monad |
| 29 |
import Data.Char (isSpace) |
| 30 |
import Data.IORef |
| 31 |
import Data.List (dropWhileEnd) |
| 32 |
import Data.List.Split (splitOn) |
| 33 |
import qualified Data.Set as Set |
| 34 |
import qualified Data.Text as Text |
| 35 |
import Text.Read (readMaybe) |
| 36 |
|
| 37 |
import Clash.Backend.Verilog.Time (parsePeriod) |
| 38 |
import Clash.Driver.Types |
| 39 |
import Clash.Netlist.BlackBox.Types (HdlSyn (..)) |
| 40 |
import Clash.Netlist.Types (PreserveCase (ToLower)) |
| 41 |
|
| 42 |
parseClashFlags :: IORef ClashOpts -> [Located String] |
| 43 |
-> IO ([Located String],[Warn]) |
| 44 |
parseClashFlags r = parseClashFlagsFull (flagsClash r) |
| 45 |
|
| 46 |
parseClashFlagsFull :: [Flag IO] -> [Located String] |
| 47 |
-> IO ([Located String],[Warn]) |
| 48 |
parseClashFlagsFull flagsAvialable args = do |
| 49 |
(leftovers,errs,warns) <- processArgs flagsAvialable args |
| 50 |
#if MIN_VERSION_ghc(9,4,0) |
| 51 |
parseResponseFile |
| 52 |
#endif |
| 53 |
|
| 54 |
unless (null errs) $ throwGhcExceptionIO $ |
| 55 |
errorsToGhcException . map (("on the commandline", ) . unLoc . errMsg) |
| 56 |
$ errs |
| 57 |
|
| 58 |
return (leftovers, warns) |
| 59 |
|
| 60 |
flagsClash :: IORef ClashOpts -> [Flag IO] |
| 61 |
flagsClash r = [ |
| 62 |
defFlag "fclash-debug" $ SepArg (setDebugLevel r) |
| 63 |
, defFlag "fclash-debug-info" $ SepArg (setDebugInfo r) |
| 64 |
, defFlag "fclash-debug-invariants" $ NoArg (liftEwM (setDebugInvariants r)) |
| 65 |
, defFlag "fclash-debug-count-transformations" $ NoArg (liftEwM (setDebugCountTransformations r)) |
| 66 |
, defFlag "fclash-debug-transformations" $ SepArg (setDebugTransformations r) |
| 67 |
, defFlag "fclash-debug-transformations-from" $ IntSuffix (setDebugTransformationsFrom r) |
| 68 |
, defFlag "fclash-debug-transformations-limit" $ IntSuffix (setDebugTransformationsLimit r) |
| 69 |
, defFlag "fclash-debug-history" $ AnySuffix (liftEwM . (setRewriteHistoryFile r)) |
| 70 |
, defFlag "fclash-hdldir" $ SepArg (setHdlDir r) |
| 71 |
, defFlag "fclash-hdlsyn" $ SepArg (setHdlSyn r) |
| 72 |
, defFlag "fclash-nocache" $ NoArg (deprecated "nocache" "no-cache" setNoCache r) |
| 73 |
, defFlag "fclash-no-cache" $ NoArg (liftEwM (setNoCache r)) |
| 74 |
, defFlag "fclash-no-check-inaccessible-idirs" $ NoArg (liftEwM (setNoIDirCheck r)) |
| 75 |
, defFlag "fclash-no-clean" $ NoArg (setNoClean r) |
| 76 |
, defFlag "fclash-clear" $ NoArg (liftEwM (setClear r)) |
| 77 |
, defFlag "fclash-no-prim-warn" $ NoArg (liftEwM (setNoPrimWarn r)) |
| 78 |
, defFlag "fclash-spec-limit" $ IntSuffix (liftEwM . setSpecLimit r) |
| 79 |
, defFlag "fclash-inline-limit" $ IntSuffix (liftEwM . setInlineLimit r) |
| 80 |
, defFlag "fclash-inline-function-limit" $ IntSuffix (liftEwM . setInlineFunctionLimit r) |
| 81 |
, defFlag "fclash-inline-constant-limit" $ IntSuffix (liftEwM . setInlineConstantLimit r) |
| 82 |
, defFlag "fclash-evaluator-fuel-limit" $ IntSuffix (liftEwM . setEvaluatorFuelLimit r) |
| 83 |
, defFlag "fclash-intwidth" $ IntSuffix (setIntWidth r) |
| 84 |
, defFlag "fclash-error-extra" $ NoArg (liftEwM (setErrorExtra r)) |
| 85 |
, defFlag "fclash-float-support" $ NoArg (setFloatSupport r) |
| 86 |
, defFlag "fclash-component-prefix" $ SepArg (liftEwM . setComponentPrefix r) |
| 87 |
, defFlag "fclash-old-inline-strategy" $ NoArg (liftEwM (setOldInlineStrategy r)) |
| 88 |
, defFlag "fclash-no-escaped-identifiers" $ NoArg (liftEwM (setNoEscapedIds r)) |
| 89 |
, defFlag "fclash-lower-case-basic-identifiers"$ NoArg (liftEwM (setLowerCaseBasicIds r)) |
| 90 |
, defFlag "fclash-compile-ultra" $ NoArg (liftEwM (setUltra r)) |
| 91 |
, defFlag "fclash-force-undefined" $ OptIntSuffix (setUndefined r) |
| 92 |
, defFlag "fclash-aggressive-x-optimization" $ NoArg (liftEwM (setAggressiveXOpt r)) |
| 93 |
, defFlag "fclash-aggressive-x-optimization-blackboxes" $ NoArg (liftEwM (setAggressiveXOptBB r)) |
| 94 |
, defFlag "fclash-inline-workfree-limit" $ IntSuffix (liftEwM . setInlineWFLimit r) |
| 95 |
, defFlag "fclash-edalize" $ NoArg (liftEwM (setEdalize r)) |
| 96 |
, defFlag "fclash-no-render-enums" $ NoArg (liftEwM (setNoRenderEnums r)) |
| 97 |
, defFlag "fclash-timescale-precision" $ SepArg (setTimescalePrecision r) |
| 98 |
] |
| 99 |
|
| 100 |
-- | Print deprecated flag warning |
| 101 |
deprecated |
| 102 |
:: String |
| 103 |
-- ^ Deprecated flag |
| 104 |
-> String |
| 105 |
-- ^ Use X instead |
| 106 |
-> (a -> IO ()) |
| 107 |
-> a |
| 108 |
-> EwM IO () |
| 109 |
deprecated wrong right f a = do |
| 110 |
addWarn ("Using '-fclash-" ++ wrong |
| 111 |
++ "' is deprecated. Use '-fclash-" |
| 112 |
++ right |
| 113 |
++ "' instead.") |
| 114 |
liftEwM (f a) |
| 115 |
|
| 116 |
setInlineLimit :: IORef ClashOpts |
| 117 |
-> Int |
| 118 |
-> IO () |
| 119 |
setInlineLimit r n = modifyIORef r (\c -> c {opt_inlineLimit = n}) |
| 120 |
|
| 121 |
setInlineFunctionLimit |
| 122 |
:: IORef ClashOpts |
| 123 |
-> Int |
| 124 |
-> IO () |
| 125 |
setInlineFunctionLimit r n = modifyIORef r (\c -> c {opt_inlineFunctionLimit = toEnum n}) |
| 126 |
|
| 127 |
setInlineConstantLimit |
| 128 |
:: IORef ClashOpts |
| 129 |
-> Int |
| 130 |
-> IO () |
| 131 |
setInlineConstantLimit r n = modifyIORef r (\c -> c {opt_inlineConstantLimit = toEnum n}) |
| 132 |
|
| 133 |
setEvaluatorFuelLimit |
| 134 |
:: IORef ClashOpts |
| 135 |
-> Int |
| 136 |
-> IO () |
| 137 |
setEvaluatorFuelLimit r n = modifyIORef r (\c -> c {opt_evaluatorFuelLimit = toEnum n}) |
| 138 |
|
| 139 |
setInlineWFLimit |
| 140 |
:: IORef ClashOpts |
| 141 |
-> Int |
| 142 |
-> IO () |
| 143 |
setInlineWFLimit r n = modifyIORef r (\c -> c {opt_inlineWFCacheLimit = toEnum n}) |
| 144 |
|
| 145 |
setSpecLimit :: IORef ClashOpts |
| 146 |
-> Int |
| 147 |
-> IO () |
| 148 |
setSpecLimit r n = modifyIORef r (\c -> c {opt_specLimit = n}) |
| 149 |
|
| 150 |
setDebugInvariants :: IORef ClashOpts -> IO () |
| 151 |
setDebugInvariants r = |
| 152 |
modifyIORef r $ \c -> |
| 153 |
c { opt_debug = (opt_debug c) { dbg_invariants = True } } |
| 154 |
|
| 155 |
setDebugCountTransformations :: IORef ClashOpts -> IO () |
| 156 |
setDebugCountTransformations r = |
| 157 |
modifyIORef r $ \c -> |
| 158 |
c { opt_debug = (opt_debug c) { dbg_countTransformations = True } } |
| 159 |
|
| 160 |
setDebugTransformations :: IORef ClashOpts -> String -> EwM IO () |
| 161 |
setDebugTransformations r s = |
| 162 |
liftEwM (modifyIORef r (setTransformations transformations)) |
| 163 |
where |
| 164 |
transformations = Set.fromList (filter (not . null) (map trim (splitOn "," s))) |
| 165 |
trim = dropWhileEnd isSpace . dropWhile isSpace |
| 166 |
|
| 167 |
setTransformations xs opts = |
| 168 |
opts { opt_debug = (opt_debug opts) { dbg_transformations = xs } } |
| 169 |
|
| 170 |
setDebugTransformationsFrom :: IORef ClashOpts -> Int -> EwM IO () |
| 171 |
setDebugTransformationsFrom r n = |
| 172 |
liftEwM (modifyIORef r (setFrom (fromIntegral n))) |
| 173 |
where |
| 174 |
setFrom from opts = |
| 175 |
opts { opt_debug = (opt_debug opts) { dbg_transformationsFrom = Just from } } |
| 176 |
|
| 177 |
setDebugTransformationsLimit :: IORef ClashOpts -> Int -> EwM IO () |
| 178 |
setDebugTransformationsLimit r n = |
| 179 |
liftEwM (modifyIORef r (setLimit (fromIntegral n))) |
| 180 |
where |
| 181 |
setLimit limit opts = |
| 182 |
opts { opt_debug = (opt_debug opts) { dbg_transformationsLimit = Just limit } } |
| 183 |
|
| 184 |
setDebugLevel :: IORef ClashOpts -> String -> EwM IO () |
| 185 |
setDebugLevel r s = |
| 186 |
case s of |
| 187 |
"DebugNone" -> |
| 188 |
liftEwM $ modifyIORef r (setLevel debugNone) |
| 189 |
"DebugSilent" -> |
| 190 |
liftEwM $ do |
| 191 |
modifyIORef r (setLevel debugSilent) |
| 192 |
setNoCache r |
| 193 |
"DebugFinal" -> |
| 194 |
liftEwM $ do |
| 195 |
modifyIORef r (setLevel debugFinal) |
| 196 |
setNoCache r |
| 197 |
"DebugCount" -> |
| 198 |
liftEwM $ do |
| 199 |
modifyIORef r (setLevel debugCount) |
| 200 |
setNoCache r |
| 201 |
"DebugName" -> |
| 202 |
liftEwM $ do |
| 203 |
modifyIORef r (setLevel debugName) |
| 204 |
setNoCache r |
| 205 |
"DebugTry" -> |
| 206 |
liftEwM $ do |
| 207 |
modifyIORef r (setLevel debugTry) |
| 208 |
setNoCache r |
| 209 |
"DebugApplied" -> |
| 210 |
liftEwM $ do |
| 211 |
modifyIORef r (setLevel debugApplied) |
| 212 |
setNoCache r |
| 213 |
"DebugAll" -> |
| 214 |
liftEwM $ do |
| 215 |
modifyIORef r (setLevel debugAll) |
| 216 |
setNoCache r |
| 217 |
_ -> |
| 218 |
addWarn (s ++ " is an invalid debug level") |
| 219 |
where |
| 220 |
setLevel lvl opts = |
| 221 |
opts { opt_debug = lvl } |
| 222 |
|
| 223 |
setDebugInfo :: IORef ClashOpts -> String -> EwM IO () |
| 224 |
setDebugInfo r s = |
| 225 |
case readMaybe s of |
| 226 |
Just info -> |
| 227 |
liftEwM $ do |
| 228 |
modifyIORef r (setInfo info) |
| 229 |
when (info /= None) (setNoCache r) |
| 230 |
|
| 231 |
Nothing -> |
| 232 |
addWarn (s ++ " is an invalid debug info") |
| 233 |
where |
| 234 |
setInfo info opts = |
| 235 |
opts { opt_debug = (opt_debug opts) { dbg_transformationInfo = info } } |
| 236 |
|
| 237 |
setNoCache :: IORef ClashOpts -> IO () |
| 238 |
setNoCache r = modifyIORef r (\c -> c {opt_cachehdl = False}) |
| 239 |
|
| 240 |
setNoIDirCheck :: IORef ClashOpts -> IO () |
| 241 |
setNoIDirCheck r = modifyIORef r (\c -> c {opt_checkIDir = False}) |
| 242 |
|
| 243 |
setNoClean :: a -> EwM IO () |
| 244 |
setNoClean _ = addWarn "-fclash-no-clean has been removed" |
| 245 |
|
| 246 |
setClear :: IORef ClashOpts -> IO () |
| 247 |
setClear r = modifyIORef r (\c -> c {opt_clear = True}) |
| 248 |
|
| 249 |
setNoPrimWarn :: IORef ClashOpts -> IO () |
| 250 |
setNoPrimWarn r = modifyIORef r (\c -> c {opt_primWarn = False}) |
| 251 |
|
| 252 |
setIntWidth :: IORef ClashOpts |
| 253 |
-> Int |
| 254 |
-> EwM IO () |
| 255 |
setIntWidth r n = |
| 256 |
if n == 32 || n == 64 |
| 257 |
then liftEwM $ modifyIORef r (\c -> c {opt_intWidth = n}) |
| 258 |
else addWarn (show n ++ " is an invalid Int/Word/Integer bit-width. Allowed widths: 32, 64.") |
| 259 |
|
| 260 |
setHdlDir :: IORef ClashOpts |
| 261 |
-> String |
| 262 |
-> EwM IO () |
| 263 |
setHdlDir r s = liftEwM $ modifyIORef r (\c -> c {opt_hdlDir = Just s}) |
| 264 |
|
| 265 |
setHdlSyn :: IORef ClashOpts |
| 266 |
-> String |
| 267 |
-> EwM IO () |
| 268 |
setHdlSyn r s = case readMaybe s of |
| 269 |
Just hdlSyn -> liftEwM $ modifyIORef r (\c -> c {opt_hdlSyn = hdlSyn}) |
| 270 |
Nothing -> case s of |
| 271 |
"Xilinx" -> liftEwM $ modifyIORef r (\c -> c {opt_hdlSyn = Vivado}) |
| 272 |
"ISE" -> liftEwM $ modifyIORef r (\c -> c {opt_hdlSyn = Vivado}) |
| 273 |
"Altera" -> liftEwM $ modifyIORef r (\c -> c {opt_hdlSyn = Quartus}) |
| 274 |
"Intel" -> liftEwM $ modifyIORef r (\c -> c {opt_hdlSyn = Quartus}) |
| 275 |
_ -> addWarn (s ++ " is an unknown hdl synthesis tool") |
| 276 |
|
| 277 |
setErrorExtra :: IORef ClashOpts -> IO () |
| 278 |
setErrorExtra r = modifyIORef r (\c -> c {opt_errorExtra = True}) |
| 279 |
|
| 280 |
setFloatSupport :: IORef ClashOpts -> EwM IO () |
| 281 |
setFloatSupport _ = |
| 282 |
addWarn "Deprecated flag: -fclash-float-support is always enabled from Clash 1.6 and onwards" |
| 283 |
|
| 284 |
setComponentPrefix |
| 285 |
:: IORef ClashOpts |
| 286 |
-> String |
| 287 |
-> IO () |
| 288 |
setComponentPrefix r s = |
| 289 |
modifyIORef r (\c -> c {opt_componentPrefix = Just (Text.pack s)}) |
| 290 |
|
| 291 |
setTimescalePrecision |
| 292 |
:: IORef ClashOpts |
| 293 |
-> String |
| 294 |
-> EwM IO () |
| 295 |
setTimescalePrecision r s = |
| 296 |
case parsePeriod s of |
| 297 |
Just period -> |
| 298 |
liftEwM $ modifyIORef r $ \c -> |
| 299 |
c{opt_timescalePrecision = period} |
| 300 |
Nothing -> |
| 301 |
addWarn $ "Could not parse " <> s <> " as a period." |
| 302 |
|
| 303 |
setOldInlineStrategy :: IORef ClashOpts -> IO () |
| 304 |
setOldInlineStrategy r = modifyIORef r (\c -> c {opt_newInlineStrat = False}) |
| 305 |
|
| 306 |
setNoEscapedIds :: IORef ClashOpts -> IO () |
| 307 |
setNoEscapedIds r = modifyIORef r (\c -> c {opt_escapedIds = False}) |
| 308 |
|
| 309 |
setLowerCaseBasicIds :: IORef ClashOpts -> IO () |
| 310 |
setLowerCaseBasicIds r = modifyIORef r (\c -> c {opt_lowerCaseBasicIds = ToLower}) |
| 311 |
|
| 312 |
setUltra :: IORef ClashOpts -> IO () |
| 313 |
setUltra r = modifyIORef r (\c -> c {opt_ultra = True}) |
| 314 |
|
| 315 |
setUndefined :: IORef ClashOpts -> Maybe Int -> EwM IO () |
| 316 |
setUndefined _ (Just x) | x < 0 || x > 1 = |
| 317 |
addWarn ("-fclash-force-undefined=" ++ show x ++ " ignored, " ++ show x ++ |
| 318 |
" not in range [0,1]") |
| 319 |
setUndefined r iM = |
| 320 |
liftEwM (modifyIORef r (\c -> c {opt_forceUndefined = Just iM})) |
| 321 |
|
| 322 |
setAggressiveXOpt :: IORef ClashOpts -> IO () |
| 323 |
setAggressiveXOpt r = do |
| 324 |
modifyIORef r (\c -> c { opt_aggressiveXOpt = True }) |
| 325 |
setAggressiveXOptBB r |
| 326 |
|
| 327 |
|
| 328 |
setAggressiveXOptBB :: IORef ClashOpts -> IO () |
| 329 |
setAggressiveXOptBB r = modifyIORef r (\c -> c { opt_aggressiveXOptBB = True }) |
| 330 |
|
| 331 |
setEdalize :: IORef ClashOpts -> IO () |
| 332 |
setEdalize r = modifyIORef r (\c -> c { opt_edalize = True }) |
| 333 |
|
| 334 |
setRewriteHistoryFile :: IORef ClashOpts -> String -> IO () |
| 335 |
setRewriteHistoryFile r arg = do |
| 336 |
let fileNm = case drop (length "-fclash-debug-history=") arg of |
| 337 |
[] -> "history.dat" |
| 338 |
str -> str |
| 339 |
modifyIORef r (setFile fileNm) |
| 340 |
where |
| 341 |
setFile file opts = |
| 342 |
opts { opt_debug = (opt_debug opts) { dbg_historyFile = Just file } } |
| 343 |
|
| 344 |
setNoRenderEnums :: IORef ClashOpts -> IO () |
| 345 |
setNoRenderEnums r = modifyIORef r (\c -> c { opt_renderEnums = False }) |