Skip to content

Commit

Permalink
Removed recursive attributes as it's not standard compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
qcorradi committed Apr 29, 2024
1 parent eb1f012 commit c7cb2d0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 160 deletions.
4 changes: 0 additions & 4 deletions src/Verismith/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ module Verismith.Config
gaoCurrent,
goExpr,
goStatement,
goAttributeAttenuation,
geoAttenuation,
gstoAttenuation,
parseConfigFile,
Expand Down Expand Up @@ -353,7 +352,6 @@ uniformCP = CPBiasedUniform [] 1

data GarbageOpts = GarbageOpts
{ _goSeed :: !(Maybe (VU.Vector Word32)),
_goAttributeAttenuation :: !GarbageAttenuationOpts,
_goConfig :: !GarbageConfigOpts,
_goPrimitive :: !GarbagePrimitiveOpts,
_goModule :: !GarbageModuleOpts,
Expand Down Expand Up @@ -573,7 +571,6 @@ defGarbageOpts :: GarbageOpts
defGarbageOpts =
GarbageOpts
{ _goSeed = Nothing,
_goAttributeAttenuation = defAttenuationOpts,
_goConfig = GarbageConfigOpts
{ _gcoBlocks = NPPoisson 0 1,
_gcoDesigns = NPPoisson 0 1,
Expand Down Expand Up @@ -1345,7 +1342,6 @@ garbageCodec :: TomlCodec GarbageOpts
garbageCodec =
GarbageOpts
<$> Toml.dioptional (Toml.read "seed") .= _goSeed
<*> garbageAttenuationCodec "attribute.attenuation" .= _goAttributeAttenuation
<*> tfield _goConfig "config" garbageConfigCodec
<*> tfield _goPrimitive "primitive" garbagePrimitiveCodec
<*> tfield _goModule "module" garbageModuleCodec
Expand Down
63 changes: 33 additions & 30 deletions src/Verismith/Verilog2005/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
{-# LANGUAGE TemplateHaskell #-}

-- TODO:
-- ModGenSingleItem is useless? as they are implicitely in a block except `else if`
-- ModGenSingleItem is useless? as they are implicitely in a block except nested conditionals
-- so one solution is GenerateBlock are (Identifier, [ModGenBlockedItem])
-- and MGIIf have a list of `if` branches
-- dangling stuff is not well handled as inserting a block changes the hierarchy of `else if`s
-- the correct solution is to add `else ;`
-- This should simplify prettyprinting and complexify parsing...
-- Also track the number of generate blocks when parsing and give the block a name when it's missing
-- and conditional use a ConditionalConstruct which is a GenerateBlock, an MGIIf or an MGICase
-- Dangling stuff is not well handled as inserting a block changes the hierarchy of `else if`s
-- the correct solution is to add `else ;`, hopefully not that complicated with previous change
-- Two previous changes should simplify prettyprinting and complexify parsing...
-- Also number the generate blocks when parsing a generate block or module and give the block a name when it's missing
-- Also do more grouping in pretty-printing and add flags to not group!
-- Maybe eliminate generate blocks that have default name but that requires counting
-- so fold or mapM with StateT
Expand Down Expand Up @@ -297,7 +297,7 @@ data Number
deriving (Show, Eq, Data, Generic)

-- | Parametric primary expression
data GenPrim i r
data GenPrim i r a
= PrimNumber
{ _pnSize :: !(Maybe Natural),
_pnSigned :: !Bool,
Expand All @@ -308,21 +308,21 @@ data GenPrim i r
{ _piIdent :: !i,
_piSub :: !r
}
| PrimConcat !(NonEmpty (GenExpr i r))
| PrimConcat !(NonEmpty (GenExpr i r a))
| PrimMultConcat
{ _pmcMul :: !CExpr,
_pmcExpr :: !(NonEmpty (GenExpr i r))
{ _pmcMul :: !(GenExpr Identifier (Maybe CRangeExpr) a),
_pmcExpr :: !(NonEmpty (GenExpr i r a))
}
| PrimFun
{ _pfIdent :: !i,
_pfAttr :: ![Attribute],
_pfArg :: ![GenExpr i r]
_pfAttr :: !a,
_pfArg :: ![GenExpr i r a]
}
| PrimSysFun
{ _psfIdent :: !ByteString,
_psfArg :: ![GenExpr i r]
_psfArg :: ![GenExpr i r a]
}
| PrimMinTypMax !(GenMinTypMax (GenExpr i r))
| PrimMinTypMax !(GenMinTypMax (GenExpr i r a))
| PrimString !ByteString
deriving (Show, Eq, Data, Generic)

Expand All @@ -339,38 +339,41 @@ type DimRange = GenDimRange Expr
type CDimRange = GenDimRange CExpr

-- | Parametric expression
data GenExpr i r
= ExprPrim !(GenPrim i r)
data GenExpr i r a
= ExprPrim !(GenPrim i r a)
| ExprUnOp
{ _euOp :: !UnaryOperator,
_euAttr :: ![Attribute],
_euPrim :: !(GenPrim i r)
_euAttr :: !a,
_euPrim :: !(GenPrim i r a)
}
| ExprBinOp
{ _ebLhs :: !(GenExpr i r),
{ _ebLhs :: !(GenExpr i r a),
_ebOp :: !BinaryOperator,
_ebAttr :: ![Attribute],
_ebRhs :: !(GenExpr i r)
_ebAttr :: !a,
_ebRhs :: !(GenExpr i r a)
}
| ExprCond
{ _ecCond :: !(GenExpr i r),
_ecAttr :: ![Attribute],
_ecTrue :: !(GenExpr i r),
_ecFalse :: !(GenExpr i r)
{ _ecCond :: !(GenExpr i r a),
_ecAttr :: !a,
_ecTrue :: !(GenExpr i r a),
_ecFalse :: !(GenExpr i r a)
}
deriving (Show, Eq, Data, Generic)

instance (Data i, Data r) => Plated (GenExpr i r) where
instance (Data i, Data r, Data a) => Plated (GenExpr i r a) where
plate = uniplate

newtype CExpr = CExpr (GenExpr Identifier (Maybe CRangeExpr))
newtype CExpr = CExpr (GenExpr Identifier (Maybe CRangeExpr) [Attribute])
deriving (Show, Eq, Data, Generic)

newtype Expr = Expr (GenExpr HierIdent (Maybe DimRange))
newtype Expr = Expr (GenExpr HierIdent (Maybe DimRange) [Attribute])
deriving (Show, Eq, Data, Generic)

-- | Attributes which can be set to various nodes in the AST.
data Attribute = Attribute {_attrIdent :: !ByteString, _attrValue :: !(Maybe CExpr)}
data Attribute = Attribute
{ _attrIdent :: !ByteString,
_attrValue :: !(Maybe (GenExpr Identifier (Maybe CRangeExpr) ()))
}
deriving (Show, Eq, Data, Generic)

data Attributed t = Attributed {_attrAttr :: ![Attribute], _attrData :: !t}
Expand Down Expand Up @@ -834,7 +837,7 @@ data STCAddArgs = STCAddArgs

-- | Module path condition
data ModulePathCondition
= MPCCond !(GenExpr Identifier ())
= MPCCond !(GenExpr Identifier () [Attribute])
| MPCNone
| MPCAlways
deriving (Show, Eq, Data, Generic)
Expand Down
65 changes: 36 additions & 29 deletions src/Verismith/Verilog2005/Generator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ applyAttenuation x = x & gaoCurrent *~ _gaoDecrease x
tameExprRecursion :: GenM' a -> GenM' a
tameExprRecursion = local $ _1 . goExpr . geoAttenuation %~ applyAttenuation

tameAttrRecursion :: GenM' a -> GenM' a
tameAttrRecursion = local $ _1 . goAttributeAttenuation %~ applyAttenuation

tameStmtRecursion :: GenM' a -> GenM' a
tameStmtRecursion = local $ _1 . goStatement . gstoAttenuation %~ applyAttenuation

Expand Down Expand Up @@ -191,8 +188,8 @@ garbageNumIdent =
NIIdent <$> garbageIdent
]

garbagePrim :: GenM' i -> GenM' r -> GenM' (GenPrim i r)
garbagePrim ident grng =
garbagePrim :: GenM' i -> GenM' r -> GenM' a -> GenM' (GenPrim i r a)
garbagePrim ident grng gattr =
sampleAttenuatedBranch
(e _geoAttenuation)
(e _geoPrimary)
Expand All @@ -215,9 +212,14 @@ garbagePrim ident grng =
),
(0, PrimIdent <$> ident <*> tameExprRecursion grng),
recurse $ PrimConcat <$> sampleNE (e _geoConcatenations) gexpr,
recurse $ PrimMultConcat <$> garbageCExpr <*> sampleNE (e _geoConcatenations) gexpr,
recurse $ PrimMultConcat
<$> garbageGenExpr
garbageIdent
(sampleMaybe (_geoDimRange . _goExpr) garbageCRangeExpr)
gattr
<*> sampleNE (e _geoConcatenations) gexpr,
recurse $ PrimFun <$> ident
<*> garbageAttributes
<*> gattr
<*> (toList <$> sampleNE (_ggoTaskFunPorts . _goGenerate) gexpr),
recurse $ PrimSysFun <$> garbageSysIdent <*> sampleN (e _geoSysFunArgs) gexpr,
recurse $ PrimMinTypMax <$> garbageGenMinTypMax gexpr
Expand All @@ -232,28 +234,25 @@ garbagePrim ident grng =
<$> sampleBernoulli (e _geoLiteralSigned)
<*> x
)
gexpr = garbageGenExpr ident grng
gexpr = garbageGenExpr ident grng gattr
recurse x = (1, tameExprRecursion x)

garbageGenExpr :: GenM' i -> GenM' r -> GenM' (GenExpr i r)
garbageGenExpr ident grng =
garbageGenExpr :: GenM' i -> GenM' r -> GenM' a -> GenM' (GenExpr i r a)
garbageGenExpr ident grng gattr =
sampleAttenuatedBranch
(e _geoAttenuation)
(e _geoItem)
[ (0, ExprPrim <$> garbagePrim ident grng),
[ (0, ExprPrim <$> garbagePrim ident grng gattr),
( 0.5,
tameExprRecursion $
ExprUnOp <$> sampleEnum (e _geoUnary) <*> garbageAttributes <*> garbagePrim ident grng
),
( 1,
tameExprRecursion $
ExprBinOp <$> gexpr <*> sampleEnum (e _geoBinary) <*> garbageAttributes <*> gexpr
ExprUnOp <$> sampleEnum (e _geoUnary) <*> gattr <*> garbagePrim ident grng gattr
),
(2, tameExprRecursion $ ExprCond <$> gexpr <*> garbageAttributes <*> gexpr <*> gexpr)
(1, tameExprRecursion $ ExprBinOp <$> gexpr <*> sampleEnum (e _geoBinary) <*> gattr <*> gexpr),
(2, tameExprRecursion $ ExprCond <$> gexpr <*> gattr <*> gexpr <*> gexpr)
]
where
e x = x . _goExpr
gexpr = garbageGenExpr ident grng
gexpr = garbageGenExpr ident grng gattr

garbageGenMinTypMax :: GenM' e -> GenM' (GenMinTypMax e)
garbageGenMinTypMax gexpr =
Expand Down Expand Up @@ -282,12 +281,16 @@ garbageGenDimRange ge =
garbageExpr :: GenM' Expr
garbageExpr =
Expr <$> garbageGenExpr
(tameExprRecursion garbageHierIdent)
(sampleMaybe (_geoDimRange . _goExpr) garbageDimRange)
(tameExprRecursion garbageHierIdent)
(sampleMaybe (_geoDimRange . _goExpr) garbageDimRange)
garbageAttributes

garbageCExpr :: GenM' CExpr
garbageCExpr =
CExpr <$> garbageGenExpr garbageIdent (sampleMaybe (_geoDimRange . _goExpr) garbageCRangeExpr)
CExpr <$> garbageGenExpr
garbageIdent
(sampleMaybe (_geoDimRange . _goExpr) garbageCRangeExpr)
garbageAttributes

garbageRangeExpr :: GenM' RangeExpr
garbageRangeExpr = garbageGenRangeExpr garbageExpr
Expand All @@ -308,13 +311,14 @@ garbageCMinTypMax :: GenM' CMinTypMax
garbageCMinTypMax = garbageGenMinTypMax garbageCExpr

garbageAttributes :: GenM' [Attribute]
garbageAttributes = do
gen <- asks snd
attrn <- asks $ _goAttributes . fst
att <- asks $ _gaoCurrent . _goAttributeAttenuation . fst
n <- sampleNumberProbability gen $ attenuateNum att attrn
sequence $ replicate n $ Attribute <$> garbageBS
<*> sampleMaybe _goAttributeOptionalValue (tameAttrRecursion garbageCExpr)
garbageAttributes =
sampleN _goAttributes $ Attribute <$> garbageBS <*> sampleMaybe _goAttributeOptionalValue gattr
where
gattr =
garbageGenExpr
garbageIdent
(sampleMaybe (_geoDimRange . _goExpr) garbageCRangeExpr)
(pure ())

garbageAttributed :: GenM' x -> GenM' (Attributed x)
garbageAttributed = liftA2 Attributed garbageAttributes
Expand Down Expand Up @@ -726,7 +730,10 @@ garbageSpecifyItem =
do
cond <- sampleBranch
(p _gspoCondition)
[pure MPCNone, pure MPCAlways, MPCCond <$> garbageGenExpr garbageIdent (pure ())]
[ pure MPCNone,
pure MPCAlways,
MPCCond <$> garbageGenExpr garbageIdent (pure ()) garbageAttributes
]
conn <- choice
(p _gspoFull_Parallel)
( SPFull <$> sampleNE (p _gspoFullSources) garbageSpecTerm
Expand Down
Loading

0 comments on commit c7cb2d0

Please sign in to comment.