From 372bcb00204d225f4b89cfed33f99a519740dc8e Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 2 Apr 2019 14:33:08 +0100 Subject: Add more configuration options and small fix --- examples/config.toml | 17 +++++++++-------- src/VeriFuzz/Config.hs | 37 +++++++++++++++++++++++++++---------- src/VeriFuzz/Gen.hs | 7 ++++--- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/examples/config.toml b/examples/config.toml index 730a195..497afe2 100644 --- a/examples/config.toml +++ b/examples/config.toml @@ -1,10 +1,11 @@ -[property] -seed = 12345 -size = 100 [probability] -assign = 10 -always = 1 -moddecl = 1 -blocking = 1 -nonblocking = 1 + moditem.always = 1 + moditem.assign = 10 + statement.blocking = 5 + statement.conditional = 1 + statement.nonblocking = 1 + +[property] + depth = 3 + size = 50 diff --git a/src/VeriFuzz/Config.hs b/src/VeriFuzz/Config.hs index a5955d7..b135c55 100644 --- a/src/VeriFuzz/Config.hs +++ b/src/VeriFuzz/Config.hs @@ -23,17 +23,22 @@ module VeriFuzz.Config , probCond , propSize , propSeed + , propDepth , configProbability , configProperty , parseConfigFile , parseConfig + , configEncode + , configToFile ) where import Control.Applicative (Alternative) import Control.Lens hiding ((.=)) +import Data.List.NonEmpty (NonEmpty (..)) import Data.Maybe (fromMaybe) import Data.Text (Text) +import qualified Data.Text.IO as T (writeFile) import Toml (TomlCodec, (.=)) import qualified Toml @@ -47,8 +52,9 @@ data Probability = Probability { _probAssign :: {-# UNPACK #-} !Int makeLenses ''Probability -data Property = Property { _propSize :: {-# UNPACK #-} !Int - , _propSeed :: !(Maybe Int) +data Property = Property { _propSize :: {-# UNPACK #-} !Int + , _propSeed :: !(Maybe Int) + , _propDepth :: {-# UNPACK #-} !Int } deriving (Eq, Show) @@ -69,20 +75,23 @@ defaultValue defaultValue x = Toml.dimap Just (fromMaybe x) . Toml.dioptional defaultConfig :: Config -defaultConfig = Config (Probability 10 1 5 1 1) (Property 50 Nothing) +defaultConfig = Config (Probability 10 1 5 1 1) (Property 50 Nothing 3) + +twoKey :: Toml.Piece -> Toml.Piece -> Toml.Key +twoKey a b = Toml.Key (a :| [b]) probCodec :: TomlCodec Probability probCodec = Probability - <$> defaultValue (defProb probAssign) (Toml.int "assign") + <$> defaultValue (defProb probAssign) (Toml.int $ twoKey "moditem" "assign") .= _probAssign - <*> defaultValue (defProb probAlways) (Toml.int "always") - .= _probAlways - <*> defaultValue (defProb probBlock) (Toml.int "blocking") - .= _probAlways - <*> defaultValue (defProb probNonBlock) (Toml.int "nonblocking") + <*> defaultValue (defProb probAlways) (Toml.int $ twoKey "moditem" "always") .= _probAlways - <*> defaultValue (defProb probNonBlock) (Toml.int "conditional") + <*> defaultValue (defProb probBlock) (Toml.int $ twoKey "statement" "blocking") + .= _probBlock + <*> defaultValue (defProb probNonBlock) (Toml.int $ twoKey "statement" "nonblocking") + .= _probNonBlock + <*> defaultValue (defProb probNonBlock) (Toml.int $ twoKey "statement" "conditional") .= _probCond where defProb i = defaultConfig ^. configProbability . i @@ -93,6 +102,8 @@ propCodec = .= _propSize <*> Toml.dioptional (Toml.int "seed") .= _propSeed + <*> defaultValue (defProp propDepth) (Toml.int "depth") + .= _propDepth where defProp i = defaultConfig ^. configProperty . i configCodec :: TomlCodec Config @@ -117,3 +128,9 @@ parseConfig t = case Toml.decode configCodec t of Left (Toml.TypeMismatch k _ _) -> error $ "Type mismatch with key " ++ show k Left (Toml.ParseError _) -> error "Config file parse error" + +configEncode :: Config -> Text +configEncode c = Toml.encode configCodec c + +configToFile :: FilePath -> Config -> IO () +configToFile f = T.writeFile f . configEncode diff --git a/src/VeriFuzz/Gen.hs b/src/VeriFuzz/Gen.hs index 5396701..b9545a8 100644 --- a/src/VeriFuzz/Gen.hs +++ b/src/VeriFuzz/Gen.hs @@ -150,14 +150,15 @@ askProbability = lift $ asks probability assignment :: StateGen Assign assignment = do + expr <- scopedExpr lval <- lvalFromPort <$> newPort Reg - Assign lval Nothing <$> scopedExpr + return $ Assign lval Nothing expr conditional :: StateGen Statement conditional = do expr <- scopedExpr stmntDepth -= 1 - tstat <- fold <$> some statement + tstat <- SeqBlock <$> some statement stmntDepth += 1 return $ CondStmnt expr (Just tstat) Nothing @@ -210,5 +211,5 @@ procedural config = VerilogSrc . (: []) . Description <$> Hog.resize num (runReaderT (evalStateT (moduleDef True) context) config) where - context = Context [] 0 5 + context = Context [] 0 $ config ^. configProperty . propDepth num = fromIntegral $ config ^. configProperty . propSize -- cgit