1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
module Verismith.OptParser
( OptTool (..)
, Opts (..)
, opts
)
where
import Control.Applicative ((<|>))
import Data.Text (Text)
import qualified Data.Text as T
import Options.Applicative (Mod (..), OptionFields (..), Parser (..),
ParserInfo (..), ReadM (..), (<**>))
import qualified Options.Applicative as Opt
import Prelude hiding (FilePath (..))
import Shelly (FilePath (..), fromText)
import Verismith.Config (SynthDescription (..), versionInfo)
data OptTool = TYosys
| TXST
| TIcarus
instance Show OptTool where
show TYosys = "yosys"
show TXST = "xst"
show TIcarus = "icarus"
data Opts = Fuzz { fuzzOutput :: {-# UNPACK #-} !Text
, fuzzConfigFile :: !(Maybe FilePath)
, fuzzForced :: !Bool
, fuzzKeepAll :: !Bool
, fuzzNum :: {-# UNPACK #-} !Int
, fuzzNoSim :: !Bool
, fuzzNoEquiv :: !Bool
, fuzzNoReduction :: !Bool
}
| Generate { generateFilename :: !(Maybe FilePath)
, generateConfigFile :: !(Maybe FilePath)
}
| Parse { parseFilename :: {-# UNPACK #-} !FilePath
}
| Reduce { reduceFilename :: {-# UNPACK #-} !FilePath
, reduceTop :: {-# UNPACK #-} !Text
, reduceScript :: !(Maybe FilePath)
, reduceSynthesiserDesc :: ![SynthDescription]
, reduceRerun :: !Bool
}
| ConfigOpt { configOptWriteConfig :: !(Maybe FilePath)
, configOptConfigFile :: !(Maybe FilePath)
, configOptDoRandomise :: !Bool
}
textOption :: Mod OptionFields String -> Parser Text
textOption = fmap T.pack . Opt.strOption
optReader :: (String -> Maybe a) -> ReadM a
optReader f = Opt.eitherReader $ \arg -> case f arg of
Just a -> Right a
Nothing -> Left $ "Cannot parse option: " <> arg
parseSynth :: String -> Maybe OptTool
parseSynth val | val == "yosys" = Just TYosys
| val == "xst" = Just TXST
| otherwise = Nothing
parseSynthDesc :: String -> Maybe SynthDescription
parseSynthDesc val
| val == "yosys" = Just $ SynthDescription "yosys" Nothing Nothing Nothing
| val == "vivado" = Just $ SynthDescription "vivado" Nothing Nothing Nothing
| val == "xst" = Just $ SynthDescription "xst" Nothing Nothing Nothing
| val == "quartus" = Just
$ SynthDescription "quartus" Nothing Nothing Nothing
| val == "identity" = Just
$ SynthDescription "identity" Nothing Nothing Nothing
| otherwise = Nothing
parseSim :: String -> Maybe OptTool
parseSim val | val == "icarus" = Just TIcarus
| otherwise = Nothing
fuzzOpts :: Parser Opts
fuzzOpts =
Fuzz
<$> textOption
( Opt.long "output"
<> Opt.short 'o'
<> Opt.metavar "DIR"
<> Opt.help "Output directory that the fuzz run takes place in."
<> Opt.showDefault
<> Opt.value "output")
<*> ( Opt.optional
. Opt.strOption
$ Opt.long "config"
<> Opt.short 'c'
<> Opt.metavar "FILE"
<> Opt.help "Config file for the current fuzz run.")
<*> (Opt.switch $ Opt.long "force" <> Opt.short 'f' <> Opt.help
"Overwrite the specified directory.")
<*> (Opt.switch $ Opt.long "keep" <> Opt.short 'k' <> Opt.help
"Keep all the directories.")
<*> ( Opt.option Opt.auto
$ Opt.long "num"
<> Opt.short 'n'
<> Opt.help "The number of fuzz runs that should be performed."
<> Opt.showDefault
<> Opt.value 1
<> Opt.metavar "INT")
<*> (Opt.switch $ Opt.long "no-sim" <> Opt.help
"Do not run simulation on the output netlist.")
<*> (Opt.switch $ Opt.long "no-equiv" <> Opt.help
"Do not run an equivalence check on the output netlist.")
<*> (Opt.switch $ Opt.long "no-reduction" <> Opt.help
"Do not run reduction on a failed testcase.")
genOpts :: Parser Opts
genOpts =
Generate
<$> ( Opt.optional
. Opt.strOption
$ Opt.long "output"
<> Opt.short 'o'
<> Opt.metavar "FILE"
<> Opt.help "Output to a verilog file instead."
)
<*> ( Opt.optional
. Opt.strOption
$ Opt.long "config"
<> Opt.short 'c'
<> Opt.metavar "FILE"
<> Opt.help "Config file for the generation run."
)
parseOpts :: Parser Opts
parseOpts = Parse . fromText . T.pack <$> Opt.strArgument
(Opt.metavar "FILE" <> Opt.help "Verilog input file.")
reduceOpts :: Parser Opts
reduceOpts =
Reduce
. 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 "script"
<> Opt.metavar "SCRIPT"
<> Opt.help
"Script that determines if the current file is interesting, which is determined by the script returning 0."
)
<*> ( Opt.many
. Opt.option (optReader parseSynthDesc)
$ Opt.short 's'
<> Opt.long "synth"
<> Opt.metavar "SYNTH"
<> Opt.help "Specify synthesiser to use."
)
<*> ( Opt.switch
$ Opt.short 'r'
<> Opt.long "rerun"
<> Opt.help
"Only rerun the current synthesis file with all the synthesisers."
)
configOpts :: Parser Opts
configOpts =
ConfigOpt
<$> ( Opt.optional
. Opt.strOption
$ Opt.long "output"
<> Opt.short 'o'
<> Opt.metavar "FILE"
<> Opt.help "Output to a TOML Config file."
)
<*> ( Opt.optional
. Opt.strOption
$ Opt.long "config"
<> Opt.short 'c'
<> Opt.metavar "FILE"
<> Opt.help "Config file for the current fuzz run."
)
<*> ( Opt.switch
$ Opt.long "randomise"
<> Opt.short 'r'
<> Opt.help
"Randomise the given default config, or the default config by randomly switchin on and off options."
)
argparse :: Parser Opts
argparse =
Opt.hsubparser
( Opt.command
"fuzz"
(Opt.info
fuzzOpts
(Opt.progDesc
"Run fuzzing on the specified simulators and synthesisers."
)
)
<> Opt.metavar "fuzz"
)
<|> Opt.hsubparser
( Opt.command
"generate"
(Opt.info
genOpts
(Opt.progDesc "Generate a random Verilog program.")
)
<> Opt.metavar "generate"
)
<|> Opt.hsubparser
( Opt.command
"parse"
(Opt.info
parseOpts
(Opt.progDesc
"Parse a verilog file and output a pretty printed version."
)
)
<> Opt.metavar "parse"
)
<|> Opt.hsubparser
( Opt.command
"reduce"
(Opt.info
reduceOpts
(Opt.progDesc
"Reduce a Verilog file by rerunning the fuzzer on the file."
)
)
<> Opt.metavar "reduce"
)
<|> Opt.hsubparser
( Opt.command
"config"
(Opt.info
configOpts
(Opt.progDesc
"Print the current configuration of the fuzzer."
)
)
<> Opt.metavar "config"
)
version :: Parser (a -> a)
version = Opt.infoOption versionInfo $ mconcat
[Opt.long "version", Opt.short 'v', Opt.help "Show version information.", Opt.hidden]
opts :: ParserInfo Opts
opts = Opt.info
(argparse <**> Opt.helper <**> version)
( Opt.fullDesc
<> Opt.progDesc "Fuzz different simulators and synthesisers."
<> Opt.header
"Verismith - A hardware simulator and synthesiser Verilog fuzzer."
)
|