mirror of
https://github.com/zachjs/sv2v.git
synced 2026-09-08 04:43:20 +02:00
updated casting conventions
- explicit enum casts in source are converted to size casts - conversion for basic pattern array literals of unsized numbers - unsized number array literals preserve signing - more aggressive ternary simplification
This commit is contained in:
+2
-12
@@ -64,11 +64,8 @@ convertDescription' description =
|
||||
(description', enumPairs)
|
||||
where
|
||||
-- replace and collect the enum types in this description
|
||||
(description', enums) =
|
||||
runWriter $
|
||||
traverseModuleItemsM (traverseTypesM traverseType) $
|
||||
traverseModuleItems (traverseExprs $ traverseNestedExprs traverseExpr) $
|
||||
description
|
||||
(description', enums) = runWriter $
|
||||
traverseModuleItemsM (traverseTypesM traverseType) description
|
||||
-- convert the collected enums into their corresponding localparams
|
||||
enumPairs = concatMap enumVals $ Set.toList enums
|
||||
|
||||
@@ -126,13 +123,6 @@ traverseType other = return other
|
||||
simplifyRange :: Range -> Range
|
||||
simplifyRange (a, b) = (simplify a, simplify b)
|
||||
|
||||
-- drop any enum type casts in favor of implicit conversion from the
|
||||
-- converted type
|
||||
traverseExpr :: Expr -> Expr
|
||||
traverseExpr (Cast (Left (IntegerVector _ _ _)) e) = e
|
||||
traverseExpr (Cast (Left (Enum _ _ _)) e) = e
|
||||
traverseExpr other = other
|
||||
|
||||
enumVals :: EnumInfo -> [EnumItem]
|
||||
enumVals (mr, l) =
|
||||
-- check for obviously duplicate values
|
||||
|
||||
@@ -66,7 +66,7 @@ convertExpr info (Call (Ident "$clog2") (Args [Just e] [])) =
|
||||
clog2' = simplify clog2
|
||||
convertExpr info (Mux cc aa bb) =
|
||||
if before == after
|
||||
then Mux cc aa bb
|
||||
then simplify $ Mux cc aa bb
|
||||
else simplify $ Mux after aa bb
|
||||
where
|
||||
before = substitute info cc
|
||||
|
||||
+11
-5
@@ -72,19 +72,25 @@ traverseExprM =
|
||||
return orig
|
||||
convertExprM (Cast (Right s) e) =
|
||||
convertCastM s e
|
||||
convertExprM (Cast (Left (IntegerVector _ Signed rs)) e) =
|
||||
convertCastWithSigningM (dimensionsSize rs) e Signed
|
||||
convertExprM (Cast (Left (IntegerVector _ _ rs)) e) =
|
||||
convertExprM $ Cast (Right $ dimensionsSize rs) e
|
||||
convertExprM other = return other
|
||||
|
||||
convertCastM :: Expr -> Expr -> ST Expr
|
||||
convertCastM s e = do
|
||||
typeMap <- get
|
||||
case exprSigning typeMap e of
|
||||
Just sg -> do
|
||||
lift $ tell $ Set.singleton (s, sg)
|
||||
let f = castFnName s sg
|
||||
let args = Args [Just e] []
|
||||
return $ Call (Ident f) args
|
||||
Just sg -> convertCastWithSigningM s e sg
|
||||
_ -> return $ Cast (Right s) e
|
||||
|
||||
convertCastWithSigningM :: Expr -> Expr -> Signing -> ST Expr
|
||||
convertCastWithSigningM s e sg = do
|
||||
lift $ tell $ Set.singleton (s, sg)
|
||||
let f = castFnName s sg
|
||||
let args = Args [Just e] []
|
||||
return $ Call (Ident f) args
|
||||
|
||||
castFn :: Expr -> Signing -> Description
|
||||
castFn e sg =
|
||||
|
||||
@@ -275,16 +275,20 @@ convertAsgn structs types (lhs, expr) =
|
||||
where e' = convertExpr (IntegerVector t sg rs) e
|
||||
-- TODO: This is a conversion for concat array literals with elements
|
||||
-- that are unsized numbers. This probably belongs somewhere else.
|
||||
convertExpr (t @ IntegerVector{}) (Pattern items) =
|
||||
if all (null . fst) items
|
||||
then convertExpr t $ Concat $ map snd items
|
||||
else Pattern items
|
||||
convertExpr (t @ IntegerVector{}) (Concat exprs) =
|
||||
if all isUnsizedNumber exprs
|
||||
then Concat exprs'
|
||||
else Concat exprs
|
||||
where
|
||||
size = DimsFn FnBits (Left $ dropInnerTypeRange t)
|
||||
caster = Cast (Right size)
|
||||
caster = Cast (Left $ dropInnerTypeRange t)
|
||||
exprs' = map caster exprs
|
||||
isUnsizedNumber :: Expr -> Bool
|
||||
isUnsizedNumber (Number n) = not $ elem '\'' n
|
||||
isUnsizedNumber (UniOp UniSub e) = isUnsizedNumber e
|
||||
isUnsizedNumber _ = False
|
||||
convertExpr (Struct packing fields (_:rs)) (Concat exprs) =
|
||||
Concat $ map (convertExpr (Struct packing fields rs)) exprs
|
||||
|
||||
@@ -93,6 +93,7 @@ typeof (orig @ (Range e mode r)) = do
|
||||
NonIndexed -> snd r
|
||||
IndexedPlus -> BinOp Sub (uncurry (BinOp Add) r) (Number "1")
|
||||
IndexedMinus -> BinOp Add (uncurry (BinOp Sub) r) (Number "1")
|
||||
typeof (BinOp Add e Number{}) = typeof e
|
||||
typeof other = return $ TypeOf other
|
||||
|
||||
-- combines a type with unpacked ranges
|
||||
|
||||
Reference in New Issue
Block a user