From e8cc0d0d2902ad06133f196ea23c23962ec55b0f Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 23 Jul 2021 12:44:04 +0200 Subject: Add command line variables --- src/Verismith.hs | 14 +++++++++++++ src/Verismith/OptParser.hs | 49 ++++++++++++++++++++++++++++++++++++++++++++++ src/Verismith/Shuffle.hs | 10 ++++++++-- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/Verismith.hs b/src/Verismith.hs index 726d6f9..53b2de6 100644 --- a/src/Verismith.hs +++ b/src/Verismith.hs @@ -210,6 +210,20 @@ handleOpts (Parse f t o rc) = do where file = T.unpack . toTextIgnore $ f mapply i f = if i then f else id +handleOpts (ShuffleOpt f t o nshuffle nrename) = do + verilogSrc <- T.readFile file + case parseVerilog (T.pack file) verilogSrc of + Left l -> print l + Right v -> do + let sv = SourceInfo t v + sv' <- if nshuffle then return sv else shuffleLines sv + sv'' <- if nrename then return sv' else renameVariables sv' + case ( o, GenVerilog sv'') of + (Nothing, a) -> print a + (Just o', a) -> writeFile (T.unpack $ toTextIgnore o') $ show a + where + file = T.unpack . toTextIgnore $ f + mapply i f = if i then f else id handleOpts (Reduce f t _ ls' False) = do src <- parseSourceInfoFile t (toTextIgnore f) datadir <- getDataDir diff --git a/src/Verismith/OptParser.hs b/src/Verismith/OptParser.hs index a6b5fb9..64a4545 100644 --- a/src/Verismith/OptParser.hs +++ b/src/Verismith/OptParser.hs @@ -84,6 +84,13 @@ data Opts { distanceOptVerilogA :: !FilePath, distanceOptVerilogB :: !FilePath } + | ShuffleOpt + { shuffleOptFilename :: !FilePath, + shuffleOptTop :: !Text, + shuffleOptOutput :: !(Maybe FilePath), + shuffleOptShuffleLines :: !Bool, + shuffleOptRenameVars :: !Bool + } textOption :: Mod OptionFields String -> Parser Text textOption = fmap T.pack . Opt.strOption @@ -301,6 +308,37 @@ parseOpts = "Remove constants in concatenation to simplify the Verilog." ) +shuffleOpts :: Parser Opts +shuffleOpts = + ShuffleOpt + <$> ( fromText . T.pack + <$> Opt.strArgument + (Opt.metavar "FILE" <> Opt.help "Verilog input file.") + ) + <*> textOption + ( Opt.short 't' + <> Opt.long "top" + <> Opt.metavar "TOP" + <> Opt.help "Name of top level module." + <> Opt.showDefault + <> Opt.value "top" + ) + <*> ( Opt.optional + . Opt.strOption + $ Opt.long "output" + <> Opt.short 'o' + <> Opt.metavar "FILE" + <> Opt.help "Output file to write the parsed file to." + ) + <*> ( Opt.switch $ + Opt.long "no-shuffle-lines" + <> Opt.help + "Shuffle the lines in a Verilog file." ) + <*> ( Opt.switch $ + Opt.long "no-rename-vars" + <> Opt.help + "Rename the variables in a Verilog file." ) + reduceOpts :: Parser Opts reduceOpts = Reduce @@ -427,6 +465,17 @@ argparse = ) <> Opt.metavar "reduce" ) + <|> Opt.hsubparser + ( Opt.command + "shuffle" + ( Opt.info + shuffleOpts + ( Opt.progDesc + "Shuffle a Verilog file." + ) + ) + <> Opt.metavar "shuffle" + ) <|> Opt.hsubparser ( Opt.command "config" diff --git a/src/Verismith/Shuffle.hs b/src/Verismith/Shuffle.hs index 4c72900..d53b18c 100644 --- a/src/Verismith/Shuffle.hs +++ b/src/Verismith/Shuffle.hs @@ -99,9 +99,15 @@ applyModules f s = do ms = s^.infoSrc._Wrapped shuffleLines, renameVariables, identityMod :: (SourceInfo a) -> Gen (SourceInfo a) -shuffleLines = applyModules shuffleLinesModule +shuffleLines = applyModules shuffleLinesModule renameVariables = applyModules renameVariablesModule -identityMod = applyModules identModule +identityMod = applyModules identModule + +shuffleLinesIO :: (SourceInfo a) -> Gen (SourceInfo a) +shuffleLinesIO = Hog.sample . shuffleLines + +renameVariablesIO :: (SourceInfo a) -> Gen (SourceInfo a) +renameVariablesIO = Hog.sample . renameVariables m' :: SourceInfo () m' = SourceInfo "m" [verilog| -- cgit