aboutsummaryrefslogtreecommitdiffstats
path: root/src/Verismith/Verilog/Mutate.hs
blob: 0855000329c6ef9c9b199730373d8c429b9f6e2d (plain)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
{-# LANGUAGE FlexibleInstances #-}

-- |
-- Module      : Verismith.Verilog.Mutate
-- Description : Functions to mutate the Verilog AST.
-- Copyright   : (c) 2018-2019, Yann Herklotz
-- License     : GPL-3
-- Maintainer  : yann [at] yannherklotz [dot] com
-- Stability   : experimental
-- Portability : POSIX
--
-- Functions to mutate the Verilog AST from "Verismith.Verilog.AST" to generate more
-- random patterns, such as nesting wires instead of creating new ones.
module Verismith.Verilog.Mutate
  ( Mutate (..),
    inPort,
    findAssign,
    idTrans,
    replace,
    nestId,
    nestSource,
    nestUpTo,
    allVars,
    instantiateMod,
    instantiateMod_,
    instantiateModSpec_,
    filterChar,
    initMod,
    makeIdFrom,
    makeTop,
    makeTopAssert,
    simplify,
    removeId,
    combineAssigns,
    combineAssigns_,
    declareMod,
    fromPort,
  )
where

import Control.Lens
import Data.Foldable (fold)
import Data.Maybe (catMaybes, fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Verismith.Circuit.Internal
import Verismith.Internal
import Verismith.Verilog.AST
import Verismith.Verilog.BitVec
import Verismith.Verilog.CodeGen
import Verismith.Verilog.Internal

class Mutate a where
  mutExpr :: (Expr -> Expr) -> a -> a

instance Mutate Identifier where
  mutExpr _ = id

instance Mutate Delay where
  mutExpr _ = id

instance Mutate Event where
  mutExpr f (EExpr e) = EExpr $ f e
  mutExpr _ a = a

instance Mutate BinaryOperator where
  mutExpr _ = id

instance Mutate UnaryOperator where
  mutExpr _ = id

instance Mutate Expr where
  mutExpr f = f

instance Mutate ConstExpr where
  mutExpr _ = id

instance Mutate Task where
  mutExpr f (Task i e) = Task i $ fmap f e

instance Mutate LVal where
  mutExpr f (RegExpr a e) = RegExpr a $ f e
  mutExpr _ a = a

instance Mutate PortDir where
  mutExpr _ = id

instance Mutate PortType where
  mutExpr _ = id

instance Mutate Range where
  mutExpr _ = id

instance Mutate Port where
  mutExpr _ = id

instance Mutate ModConn where
  mutExpr f (ModConn e) = ModConn $ f e
  mutExpr f (ModConnNamed a e) = ModConnNamed a $ f e

instance Mutate Assign where
  mutExpr f (Assign a b c) = Assign a b $ f c

instance Mutate ContAssign where
  mutExpr f (ContAssign a e) = ContAssign a $ f e

instance Mutate (CasePair ann) where
  mutExpr f (CasePair e s) = CasePair (f e) $ mutExpr f s

instance Mutate (Statement ann) where
  mutExpr f (TimeCtrl d s) = TimeCtrl d $ mutExpr f <$> s
  mutExpr f (EventCtrl e s) = EventCtrl e $ mutExpr f <$> s
  mutExpr f (SeqBlock s) = SeqBlock $ mutExpr f <$> s
  mutExpr f (BlockAssign a) = BlockAssign $ mutExpr f a
  mutExpr f (NonBlockAssign a) = NonBlockAssign $ mutExpr f a
  mutExpr f (TaskEnable a) = TaskEnable $ mutExpr f a
  mutExpr f (SysTaskEnable a) = SysTaskEnable $ mutExpr f a
  mutExpr f (CondStmnt a b c) = CondStmnt (f a) (mutExpr f <$> b) $ mutExpr f <$> c
  mutExpr f (ForLoop a1 e a2 s) = ForLoop a1 e a2 $ mutExpr f s
  mutExpr f (StmntAnn a s) = StmntAnn a $ mutExpr f s
  mutExpr f (StmntCase t e cp cd) = StmntCase t (f e) (mutExpr f cp) $ mutExpr f cd

instance Mutate Parameter where
  mutExpr _ = id

instance Mutate LocalParam where
  mutExpr _ = id

instance Mutate (ModItem ann) where
  mutExpr f (ModCA (ContAssign a e)) = ModCA . ContAssign a $ f e
  mutExpr f (ModInst a b conns) = ModInst a b $ mutExpr f conns
  mutExpr f (Initial s) = Initial $ mutExpr f s
  mutExpr f (Always s) = Always $ mutExpr f s
  mutExpr f (ModItemAnn a s) = ModItemAnn a $ mutExpr f s
  mutExpr _ d@Decl {} = d
  mutExpr _ p@ParamDecl {} = p
  mutExpr _ l@LocalParamDecl {} = l

instance Mutate (ModDecl ann) where
  mutExpr f (ModDecl a b c d e) =
    ModDecl (mutExpr f a) (mutExpr f b) (mutExpr f c) (mutExpr f d) (mutExpr f e)
  mutExpr f (ModDeclAnn a m) = ModDeclAnn a $ mutExpr f m

instance Mutate (Verilog ann) where
  mutExpr f (Verilog a) = Verilog $ mutExpr f a

instance Mutate (SourceInfo ann) where
  mutExpr f (SourceInfo a b) = SourceInfo a $ mutExpr f b

instance Mutate a => Mutate [a] where
  mutExpr f a = mutExpr f <$> a

instance Mutate a => Mutate (Maybe a) where
  mutExpr f a = mutExpr f <$> a

instance Mutate a => Mutate (GenVerilog a) where
  mutExpr f (GenVerilog a) = GenVerilog $ mutExpr f a

-- | Return if the 'Identifier' is in a '(ModDecl ann)'.
inPort :: Identifier -> (ModDecl ann) -> Bool
inPort i m = inInput
  where
    inInput =
      any (\a -> a ^. portName == i) $ m ^. modInPorts ++ m ^. modOutPorts

-- | Find the last assignment of a specific wire/reg to an expression, and
-- returns that expression.
findAssign :: Identifier -> [ModItem ann] -> Maybe Expr
findAssign i items = safe last . catMaybes $ isAssign <$> items
  where
    isAssign (ModCA (ContAssign val expr))
      | val == i = Just expr
      | otherwise = Nothing
    isAssign _ = Nothing

-- | Transforms an expression by replacing an Identifier with an
-- expression. This is used inside 'transformOf' and 'traverseExpr' to replace
-- the 'Identifier' recursively.
idTrans :: Identifier -> Expr -> Expr -> Expr
idTrans i expr (Id id')
  | id' == i = expr
  | otherwise = Id id'
idTrans _ _ e = e

-- | Replaces the identifier recursively in an expression.
replace :: Identifier -> Expr -> Expr -> Expr
replace = (transform .) . idTrans

-- | Nest expressions for a specific 'Identifier'. If the 'Identifier' is not
-- found, the AST is not changed.
--
-- This could be improved by instead of only using the last assignment to the
-- wire that one finds, to use the assignment to the wire before the current
-- expression. This would require a different approach though.
nestId :: Identifier -> (ModDecl ann) -> (ModDecl ann)
nestId i m
  | not $ inPort i m =
    let expr = fromMaybe def . findAssign i $ m ^. modItems
     in m & get %~ replace i expr
  | otherwise =
    m
  where
    get = modItems . traverse . modContAssign . contAssignExpr
    def = Id i

-- | Replaces an identifier by a expression in all the module declaration.
nestSource :: Identifier -> (Verilog ann) -> (Verilog ann)
nestSource i src = src & getModule %~ nestId i

-- | Nest variables in the format @w[0-9]*@ up to a certain number.
nestUpTo :: Int -> (Verilog ann) -> (Verilog ann)
nestUpTo i src =
  foldl (flip nestSource) src $ Identifier . fromNode <$> [1 .. i]

allVars :: (ModDecl ann) -> [Identifier]
allVars m =
  (m ^.. modOutPorts . traverse . portName)
    <> (m ^.. modInPorts . traverse . portName)

-- $setup
-- >>> import Verismith.Verilog.CodeGen
-- >>> let m = (ModDecl (Identifier "m") [Port Wire False 5 (Identifier "y")] [Port Wire False 5 "x"] [] [])
-- >>> let main = (ModDecl "main" [] [] [] [])

-- | Add a Module Instantiation using 'ModInst' from the first module passed to
-- it to the body of the second module. It first has to make all the inputs into
-- @reg@.
--
-- >>> render $ instantiateMod m main
-- module main;
--   wire [(3'h4):(1'h0)] y;
--   reg [(3'h4):(1'h0)] x;
--   m m1(y, x);
-- endmodule
-- <BLANKLINE>
-- <BLANKLINE>
instantiateMod :: (ModDecl ann) -> (ModDecl ann) -> (ModDecl ann)
instantiateMod m main = main & modItems %~ ((out ++ regIn ++ [inst]) ++)
  where
    out = Decl Nothing <$> m ^. modOutPorts <*> pure Nothing
    regIn =
      Decl Nothing
        <$> (m ^. modInPorts & traverse . portType .~ Reg)
        <*> pure Nothing
    inst =
      ModInst
        (m ^. modId)
        (m ^. modId <> (Identifier . showT $ count + 1))
        conns
    count =
      length
        . filter (== m ^. modId)
        $ main
          ^.. modItems
          . traverse
          . modInstId
    conns = uncurry ModConnNamed . fmap Id <$> zip (allVars m) (allVars m)

-- | Instantiate without adding wire declarations. It also does not count the
-- current instantiations of the same module.
--
-- >>> GenVerilog $ instantiateMod_ m
-- m m(y, x);
-- <BLANKLINE>
instantiateMod_ :: (ModDecl ann) -> (ModItem ann)
instantiateMod_ m = ModInst (m ^. modId) (m ^. modId) conns
  where
    conns =
      ModConn
        . Id
        <$> (m ^.. modOutPorts . traverse . portName)
        ++ (m ^.. modInPorts . traverse . portName)

-- | Instantiate without adding wire declarations. It also does not count the
-- current instantiations of the same module.
--
-- >>> GenVerilog $ instantiateModSpec_ "_" m
-- m m(.y(y), .x(x));
-- <BLANKLINE>
instantiateModSpec_ :: Text -> (ModDecl ann) -> (ModItem ann)
instantiateModSpec_ outChar m = ModInst (m ^. modId) (m ^. modId) conns
  where
    conns = zipWith ModConnNamed ids (Id <$> instIds)
    ids = filterChar outChar (name modOutPorts) <> name modInPorts
    instIds = name modOutPorts <> name modInPorts
    name v = m ^.. v . traverse . portName

filterChar :: Text -> [Identifier] -> [Identifier]
filterChar t ids =
  ids & traverse . _Wrapped %~ (\x -> fromMaybe x . safe head $ T.splitOn t x)

-- | Initialise all the inputs and outputs to a module.
--
-- >>> GenVerilog $ initMod m
-- module m(y, x);
--   output wire [(3'h4):(1'h0)] y;
--   input wire [(3'h4):(1'h0)] x;
-- endmodule
-- <BLANKLINE>
-- <BLANKLINE>
initMod :: (ModDecl ann) -> (ModDecl ann)
initMod m = m & modItems %~ ((out ++ inp) ++)
  where
    out = Decl (Just PortOut) <$> (m ^. modOutPorts) <*> pure Nothing
    inp = Decl (Just PortIn) <$> (m ^. modInPorts) <*> pure Nothing

-- | Make an 'Identifier' from and existing Identifier and an object with a
-- 'Show' instance to make it unique.
makeIdFrom :: (Show a) => a -> Identifier -> Identifier
makeIdFrom a i = (i <>) . Identifier . ("_" <>) $ showT a

-- | Make top level module for equivalence verification. Also takes in how many
-- modules to instantiate.
makeTop :: Int -> (ModDecl ann) -> (ModDecl ann)
makeTop i m = ModDecl (m ^. modId) ys (m ^. modInPorts) modIt []
  where
    ys = yPort . flip makeIdFrom "y" <$> [1 .. i]
    modIt = instantiateModSpec_ "_" . modN <$> [1 .. i]
    modN n =
      m & modId %~ makeIdFrom n & modOutPorts .~ [yPort (makeIdFrom n "y")]

-- | Make a top module with an assert that requires @y_1@ to always be equal to
-- @y_2@, which can then be proven using a formal verification tool.
makeTopAssert :: (ModDecl ann) -> (ModDecl ann)
makeTopAssert = (modItems %~ (++ [assert])) . makeTop 2
  where
    assert =
      Always . EventCtrl e . Just $
        SeqBlock
          [TaskEnable $ Task "assert" [BinOp (Id "y_1") BinEq (Id "y_2")]]
    e = EPosEdge "clk"

-- | Provide declarations for all the ports that are passed to it. If they are
-- registers, it should assign them to 0.
declareMod :: [Port] -> (ModDecl ann) -> (ModDecl ann)
declareMod ports = initMod . (modItems %~ (fmap decl ports ++))
  where
    decl p@(Port Reg _ _ _) = Decl Nothing p (Just 0)
    decl p = Decl Nothing p Nothing

-- | Simplify an 'Expr' by using constants to remove 'BinaryOperator' and
-- simplify expressions. To make this work effectively, it should be run until
-- no more changes were made to the expression.
--
-- >>> GenVerilog . simplify $ (Id "x") + 0
-- x
--
-- >>> GenVerilog . simplify $ (Id "y") + (Id "x")
-- (y + x)
simplify :: Expr -> Expr
simplify (BinOp (Number (BitVec _ 1)) BinAnd e) = e
simplify (BinOp e BinAnd (Number (BitVec _ 1))) = e
simplify (BinOp (Number (BitVec _ 0)) BinAnd _) = Number 0
simplify (BinOp _ BinAnd (Number (BitVec _ 0))) = Number 0
simplify (BinOp e BinPlus (Number (BitVec _ 0))) = e
simplify (BinOp (Number (BitVec _ 0)) BinPlus e) = e
simplify (BinOp e BinMinus (Number (BitVec _ 0))) = e
simplify (BinOp (Number (BitVec _ 0)) BinMinus e) = e
simplify (BinOp e BinTimes (Number (BitVec _ 1))) = e
simplify (BinOp (Number (BitVec _ 1)) BinTimes e) = e
simplify (BinOp _ BinTimes (Number (BitVec _ 0))) = Number 0
simplify (BinOp (Number (BitVec _ 0)) BinTimes _) = Number 0
simplify (BinOp e BinOr (Number (BitVec _ 0))) = e
simplify (BinOp (Number (BitVec _ 0)) BinOr e) = e
simplify (BinOp e BinLSL (Number (BitVec _ 0))) = e
simplify (BinOp (Number (BitVec _ 0)) BinLSL e) = e
simplify (BinOp e BinLSR (Number (BitVec _ 0))) = e
simplify (BinOp (Number (BitVec _ 0)) BinLSR e) = e
simplify (BinOp e BinASL (Number (BitVec _ 0))) = e
simplify (BinOp (Number (BitVec _ 0)) BinASL e) = e
simplify (BinOp e BinASR (Number (BitVec _ 0))) = e
simplify (BinOp (Number (BitVec _ 0)) BinASR e) = e
simplify (UnOp UnPlus e) = e
simplify e = e

-- | Remove all 'Identifier' that do not appeare in the input list from an
-- 'Expr'. The identifier will be replaced by @1'b0@, which can then later be
-- simplified further.
--
-- >>> GenVerilog . removeId ["x"] $ Id "x" + Id "y"
-- (x + (1'h0))
removeId :: [Identifier] -> Expr -> Expr
removeId i = transform trans
  where
    trans (Id ident)
      | ident `notElem` i = Number 0
      | otherwise = Id ident
    trans e = e

combineAssigns :: Port -> [ModItem ann] -> [ModItem ann]
combineAssigns p a =
  a
    <> [ ModCA
           . ContAssign (p ^. portName)
           . UnOp UnXor
           . fold
           $ Id
             <$> assigns
       ]
  where
    assigns = a ^.. traverse . modContAssign . contAssignNetLVal

combineAssigns_ :: Bool -> Port -> [Port] -> (ModItem ann)
combineAssigns_ comb p ps =
  ModCA
    . ContAssign (p ^. portName)
    . (if comb then UnOp UnXor else id)
    . fold
    $ Id
      <$> ps
      ^.. traverse
      . portName

fromPort :: Port -> Identifier
fromPort (Port _ _ _ i) = i