Unverified Commit 619bde4b by CORRADI Quentin Committed by GitHub

support for attributes in unary, binary, and ternary expressions

Co-authored-by: qcorradi <q.corradi22@imperial.ac.uk>
Co-authored-by: Zachary Snow <zach@zachjs.com>
parent 211bce6b
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
* Removed deprecated CLI flags `-d`/`-e`/`-i`, which have been aliased to * Removed deprecated CLI flags `-d`/`-e`/`-i`, which have been aliased to
`-D`/`-E`/`-I` with a warning since late 2019 `-D`/`-E`/`-I` with a warning since late 2019
### New Features
* Added support for attributes in unary, binary, and ternary expressions
### Bug Fixes ### Bug Fixes
* Fixed an issue that prevented parsing tasks and functions with `inout` ports * Fixed an issue that prevented parsing tasks and functions with `inout` ports
......
...@@ -29,15 +29,15 @@ simplify = simplifyStep . traverseSinglyNestedExprs simplify . simplifyStep ...@@ -29,15 +29,15 @@ simplify = simplifyStep . traverseSinglyNestedExprs simplify . simplifyStep
simplifyStep :: Expr -> Expr simplifyStep :: Expr -> Expr
simplifyStep (UniOp LogNot (Number n)) = simplifyStep (UniOpA LogNot a (Number n)) =
case numberToInteger n of case numberToInteger n of
Just 0 -> bool True Just 0 -> bool True
Just _ -> bool False Just _ -> bool False
Nothing -> UniOp LogNot $ Number n Nothing -> UniOpA LogNot a $ Number n
simplifyStep (UniOp LogNot (BinOp Eq a b)) = BinOp Ne a b simplifyStep (UniOp LogNot (BinOp Eq a b)) = BinOp Ne a b
simplifyStep (UniOp LogNot (BinOp Ne a b)) = BinOp Eq a b simplifyStep (UniOp LogNot (BinOp Ne a b)) = BinOp Eq a b
simplifyStep (UniOp UniSub (UniOp UniSub e)) = e simplifyStep (UniOpA UniSub _ (UniOpA UniSub _ e)) = e
simplifyStep (UniOp UniSub (BinOp Sub e1 e2)) = BinOp Sub e2 e1 simplifyStep (UniOp UniSub (BinOp Sub e1 e2)) = BinOp Sub e2 e1
simplifyStep (Concat [Number (Decimal size _ value)]) = simplifyStep (Concat [Number (Decimal size _ value)]) =
...@@ -49,11 +49,11 @@ simplifyStep (Concat [e@Repeat{}]) = e ...@@ -49,11 +49,11 @@ simplifyStep (Concat [e@Repeat{}]) = e
simplifyStep (Concat es) = Concat $ flattenConcat es simplifyStep (Concat es) = Concat $ flattenConcat es
simplifyStep (Repeat (Dec 0) _) = Concat [] simplifyStep (Repeat (Dec 0) _) = Concat []
simplifyStep (Repeat (Dec 1) es) = Concat es simplifyStep (Repeat (Dec 1) es) = Concat es
simplifyStep (Mux (Number n) e1 e2) = simplifyStep (MuxA a (Number n) e1 e2) =
case numberToInteger n of case numberToInteger n of
Just 0 -> e2 Just 0 -> e2
Just _ -> e1 Just _ -> e1
Nothing -> Mux (Number n) e1 e2 Nothing -> MuxA a (Number n) e1 e2
simplifyStep (Call (Ident "$clog2") (Args [SizDec k] [])) = simplifyStep (Call (Ident "$clog2") (Args [SizDec k] [])) =
simplifyStep $ Call (Ident "$clog2") (Args [RawNum k] []) simplifyStep $ Call (Ident "$clog2") (Args [RawNum k] [])
......
...@@ -114,27 +114,27 @@ convertExpr info (Call (Ident "$clog2") (Args [e] [])) = ...@@ -114,27 +114,27 @@ convertExpr info (Call (Ident "$clog2") (Args [e] [])) =
e' = convertExpr info $ substitute info e e' = convertExpr info $ substitute info e
val = Call (Ident "$clog2") (Args [e'] []) val = Call (Ident "$clog2") (Args [e'] [])
val' = simplifyStep val val' = simplifyStep val
convertExpr info (Mux cc aa bb) = convertExpr info (MuxA a cc aa bb) =
if before == after if before == after
then simplifyStep $ Mux cc' aa' bb' then simplifyStep $ MuxA a cc' aa' bb'
else simplifyStep $ Mux after aa' bb' else simplifyStep $ MuxA a after aa' bb'
where where
before = substitute info cc' before = substitute info cc'
after = convertExpr info before after = convertExpr info before
aa' = convertExpr info aa aa' = convertExpr info aa
bb' = convertExpr info bb bb' = convertExpr info bb
cc' = convertExpr info cc cc' = convertExpr info cc
convertExpr info (BinOp op e1 e2) = convertExpr info (BinOpA op a e1 e2) =
case simplifyStep $ BinOp op e1'Sub e2'Sub of case simplifyStep $ BinOpA op a e1'Sub e2'Sub of
Number n -> Number n Number n -> Number n
_ -> simplifyStep $ BinOp op e1' e2' _ -> simplifyStep $ BinOpA op a e1' e2'
where where
e1' = convertExpr info e1 e1' = convertExpr info e1
e2' = convertExpr info e2 e2' = convertExpr info e2
e1'Sub = substituteIdent info e1' e1'Sub = substituteIdent info e1'
e2'Sub = substituteIdent info e2' e2'Sub = substituteIdent info e2'
convertExpr info (UniOp op expr) = convertExpr info (UniOpA op a expr) =
simplifyStep $ UniOp op $ convertExpr info expr simplifyStep $ UniOpA op a $ convertExpr info expr
convertExpr info (Repeat expr exprs) = convertExpr info (Repeat expr exprs) =
simplifyStep $ Repeat simplifyStep $ Repeat
(convertExpr info expr) (convertExpr info expr)
......
...@@ -198,8 +198,8 @@ structIsntReady = (Nothing ==) . convertStruct ...@@ -198,8 +198,8 @@ structIsntReady = (Nothing ==) . convertStruct
-- try expression conversion by looking at the *outermost* type first -- try expression conversion by looking at the *outermost* type first
convertExpr :: Scopes a -> Type -> Expr -> Expr convertExpr :: Scopes a -> Type -> Expr -> Expr
convertExpr _ _ Nil = Nil convertExpr _ _ Nil = Nil
convertExpr scopes t (Mux c e1 e2) = convertExpr scopes t (MuxA a c e1 e2) =
Mux c e1' e2' MuxA a c e1' e2'
where where
e1' = convertExpr scopes t e1 e1' = convertExpr scopes t e1
e2' = convertExpr scopes t e2 e2' = convertExpr scopes t e2
...@@ -319,8 +319,8 @@ convertExpr scopes t@IntegerVector{} (Concat exprs) = ...@@ -319,8 +319,8 @@ convertExpr scopes t@IntegerVector{} (Concat exprs) =
t' = dropInnerTypeRange t t' = dropInnerTypeRange t
isUnsizedNumber :: Expr -> Bool isUnsizedNumber :: Expr -> Bool
isUnsizedNumber (Number n) = not $ numberIsSized n isUnsizedNumber (Number n) = not $ numberIsSized n
isUnsizedNumber (UniOp _ e) = isUnsizedNumber e isUnsizedNumber (UniOpA _ _ e) = isUnsizedNumber e
isUnsizedNumber (BinOp _ e1 e2) = isUnsizedNumber (BinOpA _ _ e1 e2) =
isUnsizedNumber e1 || isUnsizedNumber e2 isUnsizedNumber e1 || isUnsizedNumber e2
isUnsizedNumber _ = False isUnsizedNumber _ = False
...@@ -507,8 +507,8 @@ convertSubExpr scopes (Pattern items) = ...@@ -507,8 +507,8 @@ convertSubExpr scopes (Pattern items) =
items' = map mapItem items items' = map mapItem items
mapItem (x, e) = (x, e') mapItem (x, e) = (x, e')
where (_, e') = convertSubExpr scopes e where (_, e') = convertSubExpr scopes e
convertSubExpr scopes (Mux a b c) = convertSubExpr scopes (MuxA r a b c) =
(t, Mux a' b' c') (t, MuxA r a' b' c')
where where
(_, a') = convertSubExpr scopes a (_, a') = convertSubExpr scopes a
(t, b') = convertSubExpr scopes b (t, b') = convertSubExpr scopes b
......
...@@ -448,17 +448,17 @@ traverseSinglyNestedExprsM exprMapper = em ...@@ -448,17 +448,17 @@ traverseSinglyNestedExprsM exprMapper = em
pes <- mapM exprMapper $ map snd p pes <- mapM exprMapper $ map snd p
let p' = zip (map fst p) pes let p' = zip (map fst p) pes
return $ Call e' (Args l' p') return $ Call e' (Args l' p')
em (UniOp o e) = em (UniOpA o a e) =
exprMapper e >>= return . UniOp o exprMapper e >>= return . UniOpA o a
em (BinOp o e1 e2) = do em (BinOpA o a e1 e2) = do
e1' <- exprMapper e1 e1' <- exprMapper e1
e2' <- exprMapper e2 e2' <- exprMapper e2
return $ BinOp o e1' e2' return $ BinOpA o a e1' e2'
em (Mux e1 e2 e3) = do em (MuxA a e1 e2 e3) = do
e1' <- exprMapper e1 e1' <- exprMapper e1
e2' <- exprMapper e2 e2' <- exprMapper e2
e3' <- exprMapper e3 e3' <- exprMapper e3
return $ Mux e1' e2' e3' return $ MuxA a e1' e2' e3'
em (Cast tore e) = do em (Cast tore e) = do
tore' <- typeOrExprMapper tore tore' <- typeOrExprMapper tore
e' <- exprMapper e e' <- exprMapper e
......
...@@ -255,9 +255,9 @@ typeof orig@(Dot e x) = do ...@@ -255,9 +255,9 @@ typeof orig@(Dot e x) = do
Just typ -> typ Just typ -> typ
Nothing -> TypeOf orig Nothing -> TypeOf orig
typeof (Cast (Left t) _) = traverseTypeM t typeof (Cast (Left t) _) = traverseTypeM t
typeof (UniOp op expr) = typeofUniOp op expr typeof (UniOpA op _ expr) = typeofUniOp op expr
typeof (BinOp op a b) = typeofBinOp op a b typeof (BinOpA op _ a b) = typeofBinOp op a b
typeof (Mux _ a b) = largerSizeType a b typeof (MuxA _ _ a b) = largerSizeType a b
typeof (Concat exprs) = return $ typeOfSize Unsigned $ concatSize exprs typeof (Concat exprs) = return $ typeOfSize Unsigned $ concatSize exprs
typeof (Stream _ _ exprs) = return $ typeOfSize Unsigned $ concatSize exprs typeof (Stream _ _ exprs) = return $ typeOfSize Unsigned $ concatSize exprs
typeof (Repeat reps exprs) = return $ typeOfSize Unsigned size typeof (Repeat reps exprs) = return $ typeOfSize Unsigned size
...@@ -378,7 +378,7 @@ concatSize exprs = ...@@ -378,7 +378,7 @@ concatSize exprs =
-- returns the size of an expression, with the short-circuiting -- returns the size of an expression, with the short-circuiting
sizeof :: Expr -> Expr sizeof :: Expr -> Expr
sizeof (Number n) = RawNum $ numberBitLength n sizeof (Number n) = RawNum $ numberBitLength n
sizeof (Mux _ a b) = largerSizeOf a b sizeof (MuxA _ _ a b) = largerSizeOf a b
sizeof expr = DimsFn FnBits $ Left $ TypeOf expr sizeof expr = DimsFn FnBits $ Left $ TypeOf expr
-- returns the maximum size of the two given expressions -- returns the maximum size of the two given expressions
...@@ -446,8 +446,8 @@ typeCastUnneeded t1 t2 = ...@@ -446,8 +446,8 @@ typeCastUnneeded t1 t2 =
makeExplicit :: Expr -> Expr makeExplicit :: Expr -> Expr
makeExplicit (Number n) = makeExplicit (Number n) =
Number $ numberCast (numberIsSigned n) (fromIntegral $ numberBitLength n) n Number $ numberCast (numberIsSigned n) (fromIntegral $ numberBitLength n) n
makeExplicit (BinOp op e1 e2) = makeExplicit (BinOpA op a e1 e2) =
BinOp op (makeExplicit e1) (makeExplicit e2) BinOpA op a (makeExplicit e1) (makeExplicit e2)
makeExplicit (UniOp op e) = makeExplicit (UniOpA op a e) =
UniOp op $ makeExplicit e UniOpA op a $ makeExplicit e
makeExplicit other = other makeExplicit other = other
...@@ -218,48 +218,48 @@ convertExpr _ (Call expr (Args pnArgs [])) = ...@@ -218,48 +218,48 @@ convertExpr _ (Call expr (Args pnArgs [])) =
where pnArgs' = map (convertExpr SelfDetermined) pnArgs where pnArgs' = map (convertExpr SelfDetermined) pnArgs
convertExpr _ (Repeat count exprs) = convertExpr _ (Repeat count exprs) =
Repeat count $ map (convertExpr SelfDetermined) exprs Repeat count $ map (convertExpr SelfDetermined) exprs
convertExpr SelfDetermined (Mux cond e1@UU{} e2@UU{}) = convertExpr SelfDetermined (MuxA a cond e1@UU{} e2@UU{}) =
Mux MuxA a
(convertExpr SelfDetermined cond) (convertExpr SelfDetermined cond)
(convertExpr SelfDetermined e1) (convertExpr SelfDetermined e1)
(convertExpr SelfDetermined e2) (convertExpr SelfDetermined e2)
convertExpr SelfDetermined (Mux cond e1 e2) = convertExpr SelfDetermined (MuxA a cond e1 e2) =
Mux MuxA a
(convertExpr SelfDetermined cond) (convertExpr SelfDetermined cond)
(convertExpr (ContextDetermined e2) e1) (convertExpr (ContextDetermined e2) e1)
(convertExpr (ContextDetermined e1) e2) (convertExpr (ContextDetermined e1) e2)
convertExpr (ContextDetermined expr) (Mux cond e1 e2) = convertExpr (ContextDetermined expr) (MuxA a cond e1 e2) =
Mux MuxA a
(convertExpr SelfDetermined cond) (convertExpr SelfDetermined cond)
(convertExpr context e1) (convertExpr context e1)
(convertExpr context e2) (convertExpr context e2)
where context = ContextDetermined expr where context = ContextDetermined expr
convertExpr SelfDetermined (BinOp op e1 e2) = convertExpr SelfDetermined (BinOpA op a e1 e2) =
if isPeerSizedBinOp op || isParentSizedBinOp op if isPeerSizedBinOp op || isParentSizedBinOp op
then BinOp op then BinOpA op a
(convertExpr (ContextDetermined e2) e1) (convertExpr (ContextDetermined e2) e1)
(convertExpr (ContextDetermined e1) e2) (convertExpr (ContextDetermined e1) e2)
else BinOp op else BinOpA op a
(convertExpr SelfDetermined e1) (convertExpr SelfDetermined e1)
(convertExpr SelfDetermined e2) (convertExpr SelfDetermined e2)
convertExpr (ContextDetermined expr) (BinOp op e1 e2) = convertExpr (ContextDetermined expr) (BinOpA op a e1 e2) =
if isPeerSizedBinOp op then if isPeerSizedBinOp op then
BinOp op BinOpA op a
(convertExpr (ContextDetermined e2) e1) (convertExpr (ContextDetermined e2) e1)
(convertExpr (ContextDetermined e1) e2) (convertExpr (ContextDetermined e1) e2)
else if isParentSizedBinOp op then else if isParentSizedBinOp op then
BinOp op BinOpA op a
(convertExpr context e1) (convertExpr context e1)
(convertExpr context e2) (convertExpr context e2)
else else
BinOp op BinOpA op a
(convertExpr SelfDetermined e1) (convertExpr SelfDetermined e1)
(convertExpr SelfDetermined e2) (convertExpr SelfDetermined e2)
where context = ContextDetermined expr where context = ContextDetermined expr
convertExpr context (UniOp op expr) = convertExpr context (UniOpA op a expr) =
if isSizedUniOp op if isSizedUniOp op
then UniOp op (convertExpr context expr) then UniOpA op a (convertExpr context expr)
else UniOp op (convertExpr SelfDetermined expr) else UniOpA op a (convertExpr SelfDetermined expr)
convertExpr SelfDetermined (UU bit) = convertExpr SelfDetermined (UU bit) =
literalFor bit literalFor bit
convertExpr (ContextDetermined expr) (UU bit) = convertExpr (ContextDetermined expr) (UU bit) =
......
...@@ -120,7 +120,7 @@ instance Key Expr where ...@@ -120,7 +120,7 @@ instance Key Expr where
unbit (Range e _ _) = (e', n) unbit (Range e _ _) = (e', n)
where (e', n) = unbit e where (e', n) = unbit e
unbit e = (e, 0) unbit e = (e, 0)
split (Mux _ a b) = Just (a, b) split (MuxA _ _ a b) = Just (a, b)
split _ = Nothing split _ = Nothing
instance Key LHS where instance Key LHS where
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
module Language.SystemVerilog.AST.Attr module Language.SystemVerilog.AST.Attr
( Attr (..) ( Attr (..)
, AttrSpec , AttrSpec
, showsAttrs
) where ) where
import Text.Printf (printf) import Text.Printf (printf)
...@@ -25,5 +26,8 @@ type AttrSpec = (Identifier, Expr) ...@@ -25,5 +26,8 @@ type AttrSpec = (Identifier, Expr)
instance Show Attr where instance Show Attr where
show (Attr specs) = printf "(* %s *)" $ commas $ map showSpec specs show (Attr specs) = printf "(* %s *)" $ commas $ map showSpec specs
showsAttrs :: [Attr] -> ShowS
showsAttrs = foldr (\a f -> shows a . showChar ' ' . f) id
showSpec :: AttrSpec -> String showSpec :: AttrSpec -> String
showSpec (x, e) = x ++ showAssignment e showSpec (x, e) = x ++ showAssignment e
module Language.SystemVerilog.AST.Attr
( Attr
, showsAttrs
) where
data Attr
instance Eq Attr
showsAttrs :: [Attr] -> ShowS
...@@ -20,6 +20,9 @@ module Language.SystemVerilog.AST.Expr ...@@ -20,6 +20,9 @@ module Language.SystemVerilog.AST.Expr
, ParamBinding , ParamBinding
, showParams , showParams
, pattern RawNum , pattern RawNum
, pattern UniOp
, pattern BinOp
, pattern Mux
) where ) where
import Data.List (intercalate) import Data.List (intercalate)
...@@ -28,6 +31,7 @@ import Text.Printf (printf) ...@@ -28,6 +31,7 @@ import Text.Printf (printf)
import Language.SystemVerilog.AST.Number (Number(..)) import Language.SystemVerilog.AST.Number (Number(..))
import Language.SystemVerilog.AST.Op import Language.SystemVerilog.AST.Op
import Language.SystemVerilog.AST.ShowHelp import Language.SystemVerilog.AST.ShowHelp
import {-# SOURCE #-} Language.SystemVerilog.AST.Attr
import {-# SOURCE #-} Language.SystemVerilog.AST.Type import {-# SOURCE #-} Language.SystemVerilog.AST.Type
type Range = (Expr, Expr) type Range = (Expr, Expr)
...@@ -37,6 +41,13 @@ type TypeOrExpr = Either Type Expr ...@@ -37,6 +41,13 @@ type TypeOrExpr = Either Type Expr
pattern RawNum :: Integer -> Expr pattern RawNum :: Integer -> Expr
pattern RawNum n = Number (Decimal (-32) True n) pattern RawNum n = Number (Decimal (-32) True n)
pattern UniOp :: UniOp -> Expr -> Expr
pattern UniOp op e = UniOpA op [] e
pattern BinOp :: BinOp -> Expr -> Expr -> Expr
pattern BinOp op l r = BinOpA op [] l r
pattern Mux :: Expr -> Expr -> Expr -> Expr
pattern Mux c t f = MuxA [] c t f
data Expr data Expr
= String String = String String
| Real String | Real String
...@@ -51,9 +62,9 @@ data Expr ...@@ -51,9 +62,9 @@ data Expr
| Concat [Expr] | Concat [Expr]
| Stream StreamOp Expr [Expr] | Stream StreamOp Expr [Expr]
| Call Expr Args | Call Expr Args
| UniOp UniOp Expr | UniOpA UniOp [Attr] Expr
| BinOp BinOp Expr Expr | BinOpA BinOp [Attr] Expr Expr
| Mux Expr Expr Expr | MuxA [Attr] Expr Expr Expr
| Cast TypeOrExpr Expr | Cast TypeOrExpr Expr
| DimsFn DimsFn TypeOrExpr | DimsFn DimsFn TypeOrExpr
| DimFn DimFn TypeOrExpr Expr | DimFn DimFn TypeOrExpr Expr
...@@ -94,34 +105,38 @@ instance Show Expr where ...@@ -94,34 +105,38 @@ instance Show Expr where
where tStr = if null (show t) then "default" else show t where tStr = if null (show t) then "default" else show t
show (MinTypMax a b c) = printf "(%s : %s : %s)" (show a) (show b) (show c) show (MinTypMax a b c) = printf "(%s : %s : %s)" (show a) (show b) (show c)
show (ExprAsgn l r) = printf "(%s = %s)" (show l) (show r) show (ExprAsgn l r) = printf "(%s = %s)" (show l) (show r)
show e@UniOp{} = showsPrec 0 e "" show e@UniOpA{} = showsPrec 0 e ""
show e@BinOp{} = showsPrec 0 e "" show e@BinOpA{} = showsPrec 0 e ""
show e@Dot {} = showsPrec 0 e "" show e@Dot {} = showsPrec 0 e ""
show e@Mux {} = showsPrec 0 e "" show e@MuxA {} = showsPrec 0 e ""
show e@Call {} = showsPrec 0 e "" show e@Call {} = showsPrec 0 e ""
showsPrec _ (UniOp o e ) = showsPrec _ (UniOpA o a e ) =
shows o . shows o .
(if null a then id else showChar ' ') .
showsAttrs a .
showUniOpPrec e showUniOpPrec e
showsPrec _ (BinOp o a b) = showsPrec _ (BinOpA o a l r) =
showBinOpPrec a . showBinOpPrec l .
showChar ' ' . showChar ' ' .
shows o . shows o .
showChar ' ' . showChar ' ' .
showBinOpPrec b showsAttrs a .
showsPrec _ (Dot e n ) = showBinOpPrec r
showsPrec _ (Dot e n ) =
shows e . shows e .
showChar '.' . showChar '.' .
showString n showString n
showsPrec _ (Mux c a b) = showsPrec _ (MuxA a c t f) =
showChar '(' . showChar '(' .
shows c . shows c .
showString " ? " . showString " ? " .
shows a . showsAttrs a .
shows t .
showString " : " . showString " : " .
shows b . shows f .
showChar ')' showChar ')'
showsPrec _ (Call e l ) = showsPrec _ (Call e l ) =
shows e . shows e .
shows l shows l
showsPrec _ e = \s -> show e ++ s showsPrec _ e = \s -> show e ++ s
......
...@@ -1344,7 +1344,7 @@ Expr :: { Expr } ...@@ -1344,7 +1344,7 @@ Expr :: { Expr }
| Expr "[" Expr "]" { Bit $1 $3 } | Expr "[" Expr "]" { Bit $1 $3 }
| "{" Expr Concat "}" { Repeat $2 $3 } | "{" Expr Concat "}" { Repeat $2 $3 }
| Concat { Concat $1 } | Concat { Concat $1 }
| Expr "?" Expr ":" Expr { Mux $1 $3 $5 } | Expr "?" AttributeInstances Expr ":" Expr { MuxA $3 $1 $4 $6 }
| Expr "." Identifier { Dot $1 $3 } | Expr "." Identifier { Dot $1 $3 }
| "'" "{" PatternItems "}" { Pattern $3 } | "'" "{" PatternItems "}" { Pattern $3 }
| Expr "'" "{" PatternItems "}"{ Cast (Right $1) (Pattern $4) } | Expr "'" "{" PatternItems "}"{ Cast (Right $1) (Pattern $4) }
...@@ -1358,47 +1358,47 @@ Expr :: { Expr } ...@@ -1358,47 +1358,47 @@ Expr :: { Expr }
| Identifier "::" Identifier { PSIdent $1 $3 } | Identifier "::" Identifier { PSIdent $1 $3 }
| Identifier ParamBindings "::" Identifier { CSIdent $1 $2 $4 } | Identifier ParamBindings "::" Identifier { CSIdent $1 $2 $4 }
-- binary expressions -- binary expressions
| Expr "||" Expr { BinOp LogOr $1 $3 } | Expr "||" AttributeInstances Expr { BinOpA LogOr $3 $1 $4 }
| Expr "&&" Expr { BinOp LogAnd $1 $3 } | Expr "&&" AttributeInstances Expr { BinOpA LogAnd $3 $1 $4 }
| Expr "->" Expr { BinOp LogImp $1 $3 } | Expr "->" AttributeInstances Expr { BinOpA LogImp $3 $1 $4 }
| Expr "<->" Expr { BinOp LogEq $1 $3 } | Expr "<->" AttributeInstances Expr { BinOpA LogEq $3 $1 $4 }
| Expr "|" Expr { BinOp BitOr $1 $3 } | Expr "|" AttributeInstances Expr { BinOpA BitOr $3 $1 $4 }
| Expr "^" Expr { BinOp BitXor $1 $3 } | Expr "^" AttributeInstances Expr { BinOpA BitXor $3 $1 $4 }
| Expr "&" Expr { BinOp BitAnd $1 $3 } | Expr "&" AttributeInstances Expr { BinOpA BitAnd $3 $1 $4 }
| Expr "~^" Expr { BinOp BitXnor $1 $3 } | Expr "~^" AttributeInstances Expr { BinOpA BitXnor $3 $1 $4 }
| Expr "^~" Expr { BinOp BitXnor $1 $3 } | Expr "^~" AttributeInstances Expr { BinOpA BitXnor $3 $1 $4 }
| Expr "+" Expr { BinOp Add $1 $3 } | Expr "+" AttributeInstances Expr { BinOpA Add $3 $1 $4 }
| Expr "-" Expr { BinOp Sub $1 $3 } | Expr "-" AttributeInstances Expr { BinOpA Sub $3 $1 $4 }
| Expr "*" Expr { BinOp Mul $1 $3 } | Expr "*" AttributeInstances Expr { BinOpA Mul $3 $1 $4 }
| Expr "/" Expr { BinOp Div $1 $3 } | Expr "/" AttributeInstances Expr { BinOpA Div $3 $1 $4 }
| Expr "%" Expr { BinOp Mod $1 $3 } | Expr "%" AttributeInstances Expr { BinOpA Mod $3 $1 $4 }
| Expr "**" Expr { BinOp Pow $1 $3 } | Expr "**" AttributeInstances Expr { BinOpA Pow $3 $1 $4 }
| Expr "==" Expr { BinOp Eq $1 $3 } | Expr "==" AttributeInstances Expr { BinOpA Eq $3 $1 $4 }
| Expr "!=" Expr { BinOp Ne $1 $3 } | Expr "!=" AttributeInstances Expr { BinOpA Ne $3 $1 $4 }
| Expr "<" Expr { BinOp Lt $1 $3 } | Expr "<" AttributeInstances Expr { BinOpA Lt $3 $1 $4 }
| Expr "<=" Expr { BinOp Le $1 $3 } | Expr "<=" AttributeInstances Expr { BinOpA Le $3 $1 $4 }
| Expr ">" Expr { BinOp Gt $1 $3 } | Expr ">" AttributeInstances Expr { BinOpA Gt $3 $1 $4 }
| Expr ">=" Expr { BinOp Ge $1 $3 } | Expr ">=" AttributeInstances Expr { BinOpA Ge $3 $1 $4 }
| Expr "===" Expr { BinOp TEq $1 $3 } | Expr "===" AttributeInstances Expr { BinOpA TEq $3 $1 $4 }
| Expr "!==" Expr { BinOp TNe $1 $3 } | Expr "!==" AttributeInstances Expr { BinOpA TNe $3 $1 $4 }
| Expr "==?" Expr { BinOp WEq $1 $3 } | Expr "==?" AttributeInstances Expr { BinOpA WEq $3 $1 $4 }
| Expr "!=?" Expr { BinOp WNe $1 $3 } | Expr "!=?" AttributeInstances Expr { BinOpA WNe $3 $1 $4 }
| Expr "<<" Expr { BinOp ShiftL $1 $3 } | Expr "<<" AttributeInstances Expr { BinOpA ShiftL $3 $1 $4 }
| Expr ">>" Expr { BinOp ShiftR $1 $3 } | Expr ">>" AttributeInstances Expr { BinOpA ShiftR $3 $1 $4 }
| Expr "<<<" Expr { BinOp ShiftAL $1 $3 } | Expr "<<<" AttributeInstances Expr { BinOpA ShiftAL $3 $1 $4 }
| Expr ">>>" Expr { BinOp ShiftAR $1 $3 } | Expr ">>>" AttributeInstances Expr { BinOpA ShiftAR $3 $1 $4 }
-- unary expressions -- unary expressions
| "!" Expr { UniOp LogNot $2 } | "!" AttributeInstances Expr { UniOpA LogNot $2 $3 }
| "~" Expr { UniOp BitNot $2 } | "~" AttributeInstances Expr { UniOpA BitNot $2 $3 }
| "+" Expr %prec REDUCE_OP { UniOp UniAdd $2 } | "+" AttributeInstances Expr %prec REDUCE_OP { UniOpA UniAdd $2 $3 }
| "-" Expr %prec REDUCE_OP { UniOp UniSub $2 } | "-" AttributeInstances Expr %prec REDUCE_OP { UniOpA UniSub $2 $3 }
| "&" Expr %prec REDUCE_OP { UniOp RedAnd $2 } | "&" AttributeInstances Expr %prec REDUCE_OP { UniOpA RedAnd $2 $3 }
| "~&" Expr %prec REDUCE_OP { UniOp RedNand $2 } | "~&" AttributeInstances Expr %prec REDUCE_OP { UniOpA RedNand $2 $3 }
| "|" Expr %prec REDUCE_OP { UniOp RedOr $2 } | "|" AttributeInstances Expr %prec REDUCE_OP { UniOpA RedOr $2 $3 }
| "~|" Expr %prec REDUCE_OP { UniOp RedNor $2 } | "~|" AttributeInstances Expr %prec REDUCE_OP { UniOpA RedNor $2 $3 }
| "^" Expr %prec REDUCE_OP { UniOp RedXor $2 } | "^" AttributeInstances Expr %prec REDUCE_OP { UniOpA RedXor $2 $3 }
| "~^" Expr %prec REDUCE_OP { UniOp RedXnor $2 } | "~^" AttributeInstances Expr %prec REDUCE_OP { UniOpA RedXnor $2 $3 }
| "^~" Expr %prec REDUCE_OP { UniOp RedXnor $2 } | "^~" AttributeInstances Expr %prec REDUCE_OP { UniOpA RedXnor $2 $3 }
-- assignments within expressions -- assignments within expressions
| "(" Expr "=" Expr ")" {% makeExprAsgn (tokenPosition $3, AsgnOpEq) $2 $4 } | "(" Expr "=" Expr ")" {% makeExprAsgn (tokenPosition $3, AsgnOpEq) $2 $4 }
| "(" Expr AsgnBinOpP Expr ")" {% makeExprAsgn $3 $2 $4 } | "(" Expr AsgnBinOpP Expr ")" {% makeExprAsgn $3 $2 $4 }
......
module top;
wire a, b, c, d, e;
assign a = b ? (* ternary *) ~^ (* unary *) c : d & (* binary *) e;
endmodule
affirm (* unary *)
affirm (* binary *)
affirm (* ternary *)
...@@ -89,4 +89,15 @@ module top; ...@@ -89,4 +89,15 @@ module top;
$display("%b %d %d %d", tY, tY, $left(tY), $right(tY)); $display("%b %d %d %d", tY, tY, $left(tY), $right(tY));
$display("%b %d %d %d", tZ, tZ, $left(tZ), $right(tZ)); $display("%b %d %d %d", tZ, tZ, $left(tZ), $right(tZ));
end end
initial begin
type(~(*hi*)X) w = '1;
type(X+(*hi*)Y) x = '1;
type(X?(*hi*)Y:Z) y = '1;
type(!y?(*hi*)Y:Z) z = '1;
$display("%b %d %d %d", w, w, $left(w), $right(w));
$display("%b %d %d %d", x, x, $left(x), $right(x));
$display("%b %d %d %d", y, y, $left(y), $right(y));
$display("%b %d %d %d", z, z, $left(z), $right(z));
end
endmodule endmodule
...@@ -109,4 +109,19 @@ module top; ...@@ -109,4 +109,19 @@ module top;
$display("%b %d %d %d", tY, tY, 5, 0); $display("%b %d %d %d", tY, tY, 5, 0);
$display("%b %d %d %d", tZ, tZ, 7, 0); $display("%b %d %d %d", tZ, tZ, 7, 0);
end end
initial begin : block6
reg [4:0] w;
reg [5:0] x;
reg [7:0] y;
reg [7:0] z;
w = 1'sb1;
x = 1'sb1;
y = 1'sb1;
z = 1'sb1;
$display("%b %d %d %d", w, w, 4, 0);
$display("%b %d %d %d", x, x, 5, 0);
$display("%b %d %d %d", y, y, 7, 0);
$display("%b %d %d %d", z, z, 7, 0);
end
endmodule endmodule
...@@ -121,13 +121,14 @@ module top; ...@@ -121,13 +121,14 @@ module top;
end end
reg pick; reg pick;
logic [8:0] w0, w1, w2, w3; logic [8:0] w0, w1, w2, w3, w4;
assign w0 = pick ? '1 : $unsigned(4'd0); assign w0 = pick ? '1 : $unsigned(4'd0);
assign w1 = pick ? '1 : unsigned'(5'd0); assign w1 = pick ? '1 : unsigned'(5'd0);
assign w2 = pick ? '1 : $signed(6'd0); assign w2 = pick ? '1 : $signed(6'd0);
assign w3 = pick ? '1 : signed'(7'd0); assign w3 = pick ? '1 : signed'(7'd0);
assign w4 = pick ? (* foo *) (* bar *) 'z : 3'(~w3);
initial begin initial begin
$monitor("%0d %b %b %b %b %b", $time, pick, w0, w1, w2, w3); $monitor("%0d %b %b %b %b %b %b", $time, pick, w0, w1, w2, w3, w4);
#1 pick = 0; #1 pick = 0;
#1 pick = 1; #1 pick = 1;
#1 pick = 0; #1 pick = 0;
......
affirm ? (* foo *) (* bar *)
...@@ -116,13 +116,14 @@ module top; ...@@ -116,13 +116,14 @@ module top;
end end
reg pick; reg pick;
wire [8:0] w0, w1, w2, w3; wire [8:0] w0, w1, w2, w3, w4;
assign w0 = pick ? 9'h1FF : 9'h000; assign w0 = pick ? 9'h1FF : 9'h000;
assign w1 = pick ? 9'h1FF : 9'h000; assign w1 = pick ? 9'h1FF : 9'h000;
assign w2 = pick ? 9'h1FF : 9'h000; assign w2 = pick ? 9'h1FF : 9'h000;
assign w3 = pick ? 9'h1FF : 9'h000; assign w3 = pick ? 9'h1FF : 9'h000;
assign w4 = pick ? 9'hZZZ : 9'h007;
initial begin initial begin
$monitor("%0d %b %b %b %b %b", $time, pick, w0, w1, w2, w3); $monitor("%0d %b %b %b %b %b %b", $time, pick, w0, w1, w2, w3, w4);
#1 pick = 0; #1 pick = 0;
#1 pick = 1; #1 pick = 1;
#1 pick = 0; #1 pick = 0;
......
...@@ -53,7 +53,7 @@ assertConverts() { ...@@ -53,7 +53,7 @@ assertConverts() {
while read line; do while read line; do
rule=${line:0:6} rule=${line:0:6}
pattern=${line:7} pattern=${line:7}
grep -G "$pattern" < $ac_tmpa > /dev/null grep -F "$pattern" < $ac_tmpa > /dev/null
matches=$? matches=$?
if [ $rule == "affirm" ]; then if [ $rule == "affirm" ]; then
assertTrue "conversion of $ac_file does not contain $pattern" $matches assertTrue "conversion of $ac_file does not contain $pattern" $matches
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment