aboutsummaryrefslogtreecommitdiffstats
path: root/src/Verismith/Shuffle.hs
blob: d53b18c6b140df4ef390199f64609b6c1de30421 (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
{-# LANGUAGE QuasiQuotes #-}

-- |
-- Module      : Verismith.Shuffle
-- Description : Shuffle Verilog around.
-- Copyright   : (c) 2021, Yann Herklotz
-- License     : GPL-3
-- Maintainer  : yann [at] yannherklotz [dot] com
-- Stability   : experimental
-- Portability : POSIX
--
-- Shuffles the Verilog file around a bit.

module Verismith.Shuffle where

import Control.Lens hiding (Context)
import Control.Monad (replicateM)
import Control.Monad.Reader
import Control.Monad.State.Strict
import Data.Text (Text)
import Data.List (intercalate, partition)
import Data.Containers.ListUtils (nubOrd)
import qualified Data.Text as T
import Hedgehog (Gen, GenT, MonadGen)
import qualified Hedgehog as Hog
import qualified Hedgehog.Gen as Hog
import qualified Hedgehog.Range as HogR
import Data.Maybe (fromMaybe)
import Verismith.Config
import Verismith.Internal
import Verismith.Verilog.AST
import Verismith.Verilog.BitVec
import Verismith.Verilog.Eval
import Verismith.Verilog.Internal
import Verismith.Verilog.Mutate
import Verismith.Generate
import qualified Data.Map.Strict as Map

import Verismith.Verilog.CodeGen
import Verismith.Verilog.Quote
import qualified Data.Text.IO as T

import Hedgehog (Gen, GenT, MonadGen)

-- | Shuffles assign statements and always blocks in a Verilog file.
shuffleLinesModule :: (MonadGen m) => ModDecl a -> m (ModDecl a)
shuffleLinesModule m = do
  shuf' <- Hog.shuffle shuf
  return (m&modItems.~(stat <> shuf'))
  where
    (shuf, stat) = partition (\x -> case x of
                                   Always _ -> True
                                   ModCA _ -> True
                                   _ -> False
                               ) (m^.modItems)

renameIdent :: Map.Map Text Text -> Identifier -> Identifier
renameIdent map (Identifier e) = Identifier $ Map.findWithDefault e e map

renameExpr :: Map.Map Text Text -> Expr -> Expr
renameExpr map (Id e) = Id (renameIdent map e)
renameExpr _ e = e

renameVariablesModule :: (MonadGen m) => ModDecl a -> m (ModDecl a)
renameVariablesModule m = do
  shuf' <- Hog.shuffle ids
  let map = Map.fromList $ zip ids shuf'
  return (m &
          (modItems.traverse %~ (mutExpr (transform $ renameExpr map)))
        . (modOutPorts.traverse.portName %~ renameIdent map)
        . (modInPorts.traverse.portName %~ renameIdent map)
        . (modItems.traverse.declPort.portName %~ renameIdent map)
        . (transformOn (modItems.traverse._Always) (stmntBA.assignReg.regId %~ renameIdent map))
        . (transformOn (modItems.traverse._Always) (stmntNBA.assignReg.regId %~ renameIdent map))
        . (transformOn (modItems.traverse._Initial) (stmntBA.assignReg.regId %~ renameIdent map))
        . (transformOn (modItems.traverse._Initial) (stmntNBA.assignReg.regId %~ renameIdent map))
        . (modItems.traverse.modContAssign.contAssignNetLVal %~ renameIdent map))
  where
    ids = nubOrd $ (concatMap universe $ allExprCA <> allExprStmnt)^..traverse._Id._Wrapped
    allExprCA = m^..modItems.traverse.modContAssign.contAssignExpr
    allExprStmnt =
      (allStat^..traverse.stmntCondExpr)
      <> (allStat^..traverse.stmntCaseExpr)
      <> (allStat^..traverse.forExpr)
      <> (allStat^..traverse.stmntBA.assignExpr)
      <> (allStat^..traverse.stmntNBA.assignExpr)
    allStat = concatMap universe stat
    stat = (m^..modItems.traverse._Initial)
           <> (m^..modItems.traverse._Always)

identModule :: (MonadGen m) => ModDecl a -> m (ModDecl a)
identModule = return

applyModules :: (MonadGen m) => (ModDecl a -> m (ModDecl a)) -> SourceInfo a -> m (SourceInfo a)
applyModules f s = do
  ms' <- sequence (f <$> ms)
  return (s & infoSrc._Wrapped .~ ms')
  where
    ms = s^.infoSrc._Wrapped

shuffleLines, renameVariables, identityMod :: (SourceInfo a) -> Gen (SourceInfo a)
shuffleLines    = applyModules shuffleLinesModule
renameVariables = applyModules renameVariablesModule
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|
module fir_kernel_4tap_arch_1 #(
    parameter   BW = 32,
    parameter   SW = 5
) (X1, X2, X3, X5, X4, S, result);
    input  [BW-1:0] X1;
    input  [BW-1:0] X2;
    input  [BW-1:0] X3;
    input  [BW-1:0] X5;
    input  [BW-1:0] X4;
    input  [SW-1:0] S;
    output [BW-1:0] result;
    reg [SW:0] two_shift;
    wire [BW-1:0] first_sum;
    wire [BW-1:0] second_sum;

    always @* two_shift = S<<1;
    assign first_sum = X1 + X2 + (X3<<S);
    assign second_sum = X4 + (X5 << S);
    assign result = ((first_sum >> two_shift) + second_sum) >> S;
endmodule
|]

renameExample, shuffleExample :: IO (GenVerilog (SourceInfo ()))
renameExample = GenVerilog <$> Hog.sample (renameVariables m')
shuffleExample = GenVerilog <$> Hog.sample (shuffleLines m')