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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
{-|
Module : VeriFuzz.Verilog.Gen
Description : Various useful generators.
Copyright : (c) 2019, Yann Herklotz
License : GPL-3
Maintainer : ymherklotz [at] gmail [dot] com
Stability : experimental
Portability : POSIX
Various useful generators.
-}
{-# LANGUAGE TemplateHaskell #-}
module VeriFuzz.Verilog.Gen
( -- * Generation methods
procedural
, proceduralIO
, randomMod
)
where
import Control.Lens hiding (Context)
import Control.Monad (replicateM)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Reader hiding (local)
import Control.Monad.Trans.State.Lazy
import Data.Foldable (fold)
import qualified Data.Text as T
import Hedgehog (Gen)
import qualified Hedgehog.Gen as Hog
import qualified Hedgehog.Range as Hog
import VeriFuzz.Config
import VeriFuzz.Internal
import VeriFuzz.Verilog.Arbitrary
import VeriFuzz.Verilog.AST
import VeriFuzz.Verilog.Internal
import VeriFuzz.Verilog.Mutate
data Context = Context { _variables :: [Port]
, _parameters :: [Parameter]
, _modules :: [ModDecl]
, _nameCounter :: {-# UNPACK #-} !Int
, _stmntDepth :: {-# UNPACK #-} !Int
, _modDepth :: {-# UNPACK #-} !Int
}
makeLenses ''Context
type StateGen = StateT Context (ReaderT Config Gen)
toId :: Int -> Identifier
toId = Identifier . ("w" <>) . T.pack . show
toPort :: Identifier -> Gen Port
toPort ident = do
i <- genPositive
return $ wire i ident
sumSize :: [Port] -> Int
sumSize ps = sum $ ps ^.. traverse . portSize
random :: [Identifier] -> (Expr -> ContAssign) -> Gen ModItem
random ctx fun = do
expr <- Hog.sized (exprWithContext (ProbExpr 1 1 1 1 1 1 0 1 1) ctx)
return . ModCA $ fun expr
--randomAssigns :: [Identifier] -> [Gen ModItem]
--randomAssigns ids = random ids . ContAssign <$> ids
randomOrdAssigns :: [Identifier] -> [Identifier] -> [Gen ModItem]
randomOrdAssigns inp ids = snd $ foldr generate (inp, []) ids
where generate cid (i, o) = (cid : i, random i (ContAssign cid) : o)
randomMod :: Int -> Int -> Gen ModDecl
randomMod inps total = do
x <- sequence $ randomOrdAssigns start end
ident <- sequence $ toPort <$> ids
let inputs_ = take inps ident
let other = drop inps ident
let y = ModCA . ContAssign "y" . fold $ Id <$> drop inps ids
let yport = [wire (sumSize other) "y"]
return . declareMod other $ ModDecl "test_module"
yport
inputs_
(x ++ [y])
[]
where
ids = toId <$> [1 .. total]
end = drop inps ids
start = take inps ids
gen :: Gen a -> StateGen a
gen = lift . lift
constExprWithContext :: [Parameter] -> ProbExpr -> Hog.Size -> Gen ConstExpr
constExprWithContext ps prob size
| size == 0 = Hog.frequency
[ (prob ^. probExprNum, ConstNum <$> genPositive <*> arb)
, ( if null ps then 0 else prob ^. probExprId
, ParamId . view paramIdent <$> Hog.element ps
)
]
| size > 0 = Hog.frequency
[ (prob ^. probExprNum, ConstNum <$> genPositive <*> arb)
, ( if null ps then 0 else prob ^. probExprId
, ParamId . view paramIdent <$> Hog.element ps
)
, (prob ^. probExprUnOp, ConstUnOp <$> arb <*> subexpr 2)
, ( prob ^. probExprBinOp
, ConstBinOp <$> subexpr 2 <*> arb <*> subexpr 2
)
, ( prob ^. probExprCond
, ConstCond <$> subexpr 3 <*> subexpr 3 <*> subexpr 3
)
, (prob ^. probExprConcat, ConstConcat <$> listOf1 (subexpr 8))
]
| otherwise = constExprWithContext ps prob 0
where subexpr y = constExprWithContext ps prob $ size `div` y
exprSafeList :: ProbExpr -> [(Int, Gen Expr)]
exprSafeList prob =
[ ( prob ^. probExprNum
, Number <$> genPositive <*> Hog.integral (Hog.linearFrom 0 (-100) 100)
)
]
exprRecList :: ProbExpr -> (Hog.Size -> Gen Expr) -> [(Int, Gen Expr)]
exprRecList prob subexpr =
[ ( prob ^. probExprNum
, Number <$> genPositive <*> Hog.integral (Hog.linearFrom 0 (-100) 100)
)
, (prob ^. probExprConcat , Concat <$> listOf1 (subexpr 8))
, (prob ^. probExprUnOp , UnOp <$> arb <*> subexpr 2)
, (prob ^. probExprStr, Str <$> Hog.text (Hog.linear 0 100) Hog.alphaNum)
, (prob ^. probExprBinOp , BinOp <$> subexpr 2 <*> arb <*> subexpr 2)
, (prob ^. probExprCond , Cond <$> subexpr 3 <*> subexpr 3 <*> subexpr 3)
, (prob ^. probExprSigned , Func <$> pure SignedFunc <*> subexpr 2)
, (prob ^. probExprUnsigned, Func <$> pure UnsignedFunc <*> subexpr 2)
]
exprWithContext :: ProbExpr -> [Identifier] -> Hog.Size -> Gen Expr
exprWithContext prob [] n | n == 0 = Hog.frequency $ exprSafeList prob
| n > 0 = Hog.frequency $ exprRecList prob subexpr
| otherwise = exprWithContext prob [] 0
where subexpr y = exprWithContext prob [] $ n `div` y
exprWithContext prob l n
| n == 0
= Hog.frequency
$ (prob ^. probExprId, Id <$> Hog.element l)
: exprSafeList prob
| n > 0
= Hog.frequency
$ (prob ^. probExprId, Id <$> Hog.element l)
: exprRecList prob subexpr
| otherwise
= exprWithContext prob l 0
where subexpr y = exprWithContext prob l $ n `div` y
some :: StateGen a -> StateGen [a]
some f = do
amount <- gen genPositive
replicateM amount f
many :: StateGen a -> StateGen [a]
many f = do
amount <- gen $ Hog.int (Hog.linear 0 30)
replicateM amount f
makeIdentifier :: T.Text -> StateGen Identifier
makeIdentifier prefix = do
context <- get
let ident = Identifier $ prefix <> showT (context ^. nameCounter)
nameCounter += 1
return ident
newPort :: PortType -> StateGen Port
newPort pt = do
ident <- makeIdentifier . T.toLower $ showT pt
p <- gen $ Port pt <$> arb <*> genPositive <*> pure ident
variables %= (p :)
return p
scopedExpr :: StateGen Expr
scopedExpr = do
context <- get
prob <- askProbability
gen
. Hog.sized
. exprWithContext (prob ^. probExpr)
$ context
^.. variables
. traverse
. portName
contAssign :: StateGen ContAssign
contAssign = do
expr <- scopedExpr
p <- newPort Wire
return $ ContAssign (p ^. portName) expr
lvalFromPort :: Port -> LVal
lvalFromPort (Port _ _ _ i) = RegId i
probability :: Config -> Probability
probability c = c ^. configProbability
askProbability :: StateGen Probability
askProbability = lift $ asks probability
assignment :: StateGen Assign
assignment = do
expr <- scopedExpr
lval <- lvalFromPort <$> newPort Reg
return $ Assign lval Nothing expr
conditional :: StateGen Statement
conditional = do
expr <- scopedExpr
stmntDepth -= 1
tstat <- SeqBlock <$> some statement
fstat <- Hog.maybe $ SeqBlock <$> some statement
stmntDepth += 1
return $ CondStmnt (BinOp expr BinEq 0) (Just tstat) fstat
statement :: StateGen Statement
statement = do
prob <- askProbability
cont <- get
let defProb i = prob ^. probStmnt . i
Hog.frequency
[ (defProb probStmntBlock , BlockAssign <$> assignment)
, (defProb probStmntNonBlock , NonBlockAssign <$> assignment)
, (onDepth cont (defProb probStmntCond), conditional)
]
where onDepth c n = if c ^. stmntDepth > 0 then n else 0
always :: StateGen ModItem
always = do
stat <- SeqBlock <$> some statement
return $ Always (EventCtrl (EPosEdge "clk") (Just stat))
instantiate :: ModDecl -> StateGen ModItem
instantiate (ModDecl i outP inP _ _) = do
context <- get
outs <-
fmap (Id . view portName) <$> (replicateM (length outP) $ newPort Wire)
ins <-
(Id "clk" :)
. fmap (Id . view portName)
. take (length inP - 1)
<$> (Hog.shuffle $ context ^. variables)
ident <- makeIdentifier "modinst"
Hog.choice
[ return . ModInst i ident $ ModConn <$> outs <> ins
, ModInst i ident
<$> Hog.shuffle (zipWith ModConnNamed (view portName <$> outP <> inP) (outs <> ins))
]
-- | Generates a module instance by also generating a new module if there are
-- not enough modules currently in the context. It keeps generating new modules
-- for every instance and for every level until either the deepest level is
-- achieved, or the maximum number of modules are reached.
--
-- If the maximum number of levels are reached, it will always pick an instance
-- from the current context. The problem with this approach is that at the end
-- there may be many more than the max amount of modules, as the modules are
-- always set to empty when entering a new level. This is to fix recursive
-- definitions of modules, which are not defined.
--
-- One way to fix that is to also decrement the max modules for every level,
-- depending on how many modules have already been generated. This would mean
-- there would be moments when the module cannot generate a new instance but
-- also not take a module from the current context. A fix for that may be to
-- have a default definition of a simple module that is used instead.
--
-- Another different way to handle this would be to have a probability of taking
-- a module from a context or generating a new one.
modInst :: StateGen ModItem
modInst = do
prob <- lift ask
context <- get
let maxMods = prob ^. configProperty . propMaxModules
if length (context ^. modules) < maxMods
then do
let currMods = context ^. modules
let params = context ^. parameters
let vars = context ^. variables
modules .= []
variables .= []
parameters .= []
modDepth -= 1
chosenMod <- moduleDef Nothing
ncont <- get
let genMods = ncont ^. modules
modDepth += 1
parameters .= params
variables .= vars
modules .= chosenMod : currMods <> genMods
instantiate chosenMod
else Hog.element (context ^. modules) >>= instantiate
-- | Generate a random module item.
modItem :: StateGen ModItem
modItem = do
prob <- askProbability
context <- get
let defProb i = prob ^. probModItem . i
Hog.frequency
[ (defProb probModItemAssign, ModCA <$> contAssign)
, (defProb probModItemAlways, always)
, ( if context ^. modDepth > 0 then defProb probModItemInst else 0
, modInst
)
]
moduleName :: Maybe Identifier -> StateGen Identifier
moduleName (Just t) = return t
moduleName Nothing = makeIdentifier "module"
constExpr :: StateGen ConstExpr
constExpr = do
prob <- askProbability
context <- get
gen . Hog.sized $ constExprWithContext (context ^. parameters)
(prob ^. probExpr)
parameter :: StateGen Parameter
parameter = do
ident <- makeIdentifier "param"
cexpr <- constExpr
let param = Parameter ident cexpr
parameters %= (param :)
return param
-- | Generates a module definition randomly. It always has one output port which
-- is set to @y@. The size of @y@ is the total combination of all the locally
-- defined wires, so that it correctly reflects the internal state of the
-- module.
moduleDef :: Maybe Identifier -> StateGen ModDecl
moduleDef top = do
name <- moduleName top
portList <- some $ newPort Wire
mi <- some modItem
context <- get
let local = filter (`notElem` portList) $ context ^. variables
let size = sum $ local ^.. traverse . portSize
let clock = Port Wire False 1 "clk"
let yport = Port Wire False size "y"
let comb = combineAssigns_ yport local
declareMod local
. ModDecl name [yport] (clock : portList) (mi <> [comb])
<$> many parameter
-- | Procedural generation method for random Verilog. Uses internal 'Reader' and
-- 'State' to keep track of the current Verilog code structure.
procedural :: Config -> Gen Verilog
procedural config = do
(mainMod, st) <- Hog.resize num
$ runReaderT (runStateT (moduleDef (Just "top")) context) config
return . Verilog $ mainMod : st ^. modules
where
context =
Context [] [] [] 0 (confProp propStmntDepth) $ confProp propModDepth
num = fromIntegral $ confProp propSize
confProp i = config ^. configProperty . i
proceduralIO :: Config -> IO Verilog
proceduralIO = Hog.sample . procedural
|