Commit 470fa01e by Zachary Snow

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
parent 976f5822
...@@ -64,11 +64,8 @@ convertDescription' description = ...@@ -64,11 +64,8 @@ convertDescription' description =
(description', enumPairs) (description', enumPairs)
where where
-- replace and collect the enum types in this description -- replace and collect the enum types in this description
(description', enums) = (description', enums) = runWriter $
runWriter $ traverseModuleItemsM (traverseTypesM traverseType) description
traverseModuleItemsM (traverseTypesM traverseType) $
traverseModuleItems (traverseExprs $ traverseNestedExprs traverseExpr) $
description
-- convert the collected enums into their corresponding localparams -- convert the collected enums into their corresponding localparams
enumPairs = concatMap enumVals $ Set.toList enums enumPairs = concatMap enumVals $ Set.toList enums
...@@ -126,13 +123,6 @@ traverseType other = return other ...@@ -126,13 +123,6 @@ traverseType other = return other
simplifyRange :: Range -> Range simplifyRange :: Range -> Range
simplifyRange (a, b) = (simplify a, simplify b) 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 :: EnumInfo -> [EnumItem]
enumVals (mr, l) = enumVals (mr, l) =
-- check for obviously duplicate values -- check for obviously duplicate values
......
...@@ -66,7 +66,7 @@ convertExpr info (Call (Ident "$clog2") (Args [Just e] [])) = ...@@ -66,7 +66,7 @@ convertExpr info (Call (Ident "$clog2") (Args [Just e] [])) =
clog2' = simplify clog2 clog2' = simplify clog2
convertExpr info (Mux cc aa bb) = convertExpr info (Mux cc aa bb) =
if before == after if before == after
then Mux cc aa bb then simplify $ Mux cc aa bb
else simplify $ Mux after aa bb else simplify $ Mux after aa bb
where where
before = substitute info cc before = substitute info cc
......
...@@ -72,19 +72,25 @@ traverseExprM = ...@@ -72,19 +72,25 @@ traverseExprM =
return orig return orig
convertExprM (Cast (Right s) e) = convertExprM (Cast (Right s) e) =
convertCastM 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 convertExprM other = return other
convertCastM :: Expr -> Expr -> ST Expr convertCastM :: Expr -> Expr -> ST Expr
convertCastM s e = do convertCastM s e = do
typeMap <- get typeMap <- get
case exprSigning typeMap e of case exprSigning typeMap e of
Just sg -> do Just sg -> convertCastWithSigningM s e sg
lift $ tell $ Set.singleton (s, sg)
let f = castFnName s sg
let args = Args [Just e] []
return $ Call (Ident f) args
_ -> return $ Cast (Right s) e _ -> 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 :: Expr -> Signing -> Description
castFn e sg = castFn e sg =
......
...@@ -275,16 +275,20 @@ convertAsgn structs types (lhs, expr) = ...@@ -275,16 +275,20 @@ convertAsgn structs types (lhs, expr) =
where e' = convertExpr (IntegerVector t sg rs) e where e' = convertExpr (IntegerVector t sg rs) e
-- TODO: This is a conversion for concat array literals with elements -- TODO: This is a conversion for concat array literals with elements
-- that are unsized numbers. This probably belongs somewhere else. -- 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) = convertExpr (t @ IntegerVector{}) (Concat exprs) =
if all isUnsizedNumber exprs if all isUnsizedNumber exprs
then Concat exprs' then Concat exprs'
else Concat exprs else Concat exprs
where where
size = DimsFn FnBits (Left $ dropInnerTypeRange t) caster = Cast (Left $ dropInnerTypeRange t)
caster = Cast (Right size)
exprs' = map caster exprs exprs' = map caster exprs
isUnsizedNumber :: Expr -> Bool isUnsizedNumber :: Expr -> Bool
isUnsizedNumber (Number n) = not $ elem '\'' n isUnsizedNumber (Number n) = not $ elem '\'' n
isUnsizedNumber (UniOp UniSub e) = isUnsizedNumber e
isUnsizedNumber _ = False isUnsizedNumber _ = False
convertExpr (Struct packing fields (_:rs)) (Concat exprs) = convertExpr (Struct packing fields (_:rs)) (Concat exprs) =
Concat $ map (convertExpr (Struct packing fields rs)) exprs Concat $ map (convertExpr (Struct packing fields rs)) exprs
......
...@@ -93,6 +93,7 @@ typeof (orig @ (Range e mode r)) = do ...@@ -93,6 +93,7 @@ typeof (orig @ (Range e mode r)) = do
NonIndexed -> snd r NonIndexed -> snd r
IndexedPlus -> BinOp Sub (uncurry (BinOp Add) r) (Number "1") IndexedPlus -> BinOp Sub (uncurry (BinOp Add) r) (Number "1")
IndexedMinus -> BinOp Add (uncurry (BinOp Sub) r) (Number "1") IndexedMinus -> BinOp Add (uncurry (BinOp Sub) r) (Number "1")
typeof (BinOp Add e Number{}) = typeof e
typeof other = return $ TypeOf other typeof other = return $ TypeOf other
-- combines a type with unpacked ranges -- combines a type with unpacked ranges
......
module top; module top;
localparam integer B [4] = { 1, 2, 3, 4 }; localparam integer A [4] = { 1, 2, 3, 4 };
localparam byte C [4] = { 1, 2, 3, 4 }; localparam byte B [4] = { 1, 2, 3, 4 };
localparam bit D [4] = { 1, 2, 3, 4 }; localparam bit C [4] = { 1, 2, 3, 4 };
localparam integer signed D [4] = { -1, -2, -3, -4 };
localparam byte signed E [4] = { -1, -2, -3, -4 };
localparam bit signed F [4] = { -1, -2, -3, -4 };
localparam integer G [4] = '{ 1, 2, 3, 4 };
localparam byte H [4] = '{ 1, 2, 3, 4 };
localparam bit I [4] = '{ 1, 2, 3, 4 };
initial begin initial begin
`define PRINT(X) \ `define PRINT(X) \
$display("%b %2d %2d", {X[0], X[1], X[2], X[3]}, $bits(X), $bits(X[0])); $display("%b %2d %2d", {X[0], X[1], X[2], X[3]}, $bits(X), $bits(X[0]));
`PRINT(A);
`PRINT(B); `PRINT(B);
`PRINT(C); `PRINT(C);
`PRINT(D); `PRINT(D);
`PRINT(E);
`PRINT(F);
`PRINT(G);
`PRINT(H);
`PRINT(I);
end end
endmodule endmodule
module top; module top;
localparam [0:127] B = { 32'h1, 32'h2, 32'h3, 32'h4 }; localparam [0:127] A = { 32'h1, 32'h2, 32'h3, 32'h4 };
localparam [0:31] C = { 8'h1, 8'h2, 8'h3, 8'h4 }; localparam [0:31] B = { 8'h1, 8'h2, 8'h3, 8'h4 };
localparam [0:3] D = { 1'h1, 1'h0, 1'h1, 1'h0 }; localparam [0:3] C = { 1'h1, 1'h0, 1'h1, 1'h0 };
localparam [0:127] D = { 32'hFFFFFFFF, 32'hFFFFFFFE, 32'hFFFFFFFD, 32'hFFFFFFFC };
localparam [0:31] E = { 8'hFF, 8'hFE, 8'hFD, 8'hFC };
localparam [0:3] F = { 1'h1, 1'h0, 1'h1, 1'h0 };
localparam [0:127] G = { 32'h1, 32'h2, 32'h3, 32'h4 };
localparam [0:31] H = { 8'h1, 8'h2, 8'h3, 8'h4 };
localparam [0:3] I = { 1'h1, 1'h0, 1'h1, 1'h0 };
initial begin initial begin
$display("%b %2d %2d", B, $bits(B), 32); $display("%b %2d %2d", A, $bits(A), 32);
$display("%b %2d %2d", C, $bits(C), 8); $display("%b %2d %2d", B, $bits(B), 8);
$display("%b %2d %2d", D, $bits(D), 1); $display("%b %2d %2d", C, $bits(C), 1);
$display("%b %2d %2d", D, $bits(D), 32);
$display("%b %2d %2d", E, $bits(E), 8);
$display("%b %2d %2d", F, $bits(F), 1);
$display("%b %2d %2d", G, $bits(G), 32);
$display("%b %2d %2d", H, $bits(H), 8);
$display("%b %2d %2d", I, $bits(I), 1);
end end
endmodule endmodule
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