Commit 41bba1b2 by Zachary Snow

generate localparams for all package enums (closes #26)

parent 92feef87
...@@ -50,7 +50,6 @@ phases excludes = ...@@ -50,7 +50,6 @@ phases excludes =
, Convert.ForDecl.convert , Convert.ForDecl.convert
, Convert.FuncRet.convert , Convert.FuncRet.convert
, Convert.EmptyArgs.convert , Convert.EmptyArgs.convert
, Convert.Enum.convert
, Convert.IntTypes.convert , Convert.IntTypes.convert
, Convert.KWArgs.convert , Convert.KWArgs.convert
, Convert.Mux.convert , Convert.Mux.convert
...@@ -63,6 +62,7 @@ phases excludes = ...@@ -63,6 +62,7 @@ phases excludes =
, Convert.UnbasedUnsized.convert , Convert.UnbasedUnsized.convert
, Convert.Unique.convert , Convert.Unique.convert
, Convert.Package.convert , Convert.Package.convert
, Convert.Enum.convert
, Convert.NestPI.convert , Convert.NestPI.convert
, Convert.Return.convert , Convert.Return.convert
, selectExclude (Job.Interface, Convert.Interface.convert) , selectExclude (Job.Interface, Convert.Interface.convert)
......
...@@ -44,15 +44,33 @@ convertDescription (description @ (Part _ _ _ _ _ _)) = ...@@ -44,15 +44,33 @@ convertDescription (description @ (Part _ _ _ _ _ _)) =
Part extern kw lifetime name ports (enumItems ++ items) Part extern kw lifetime name ports (enumItems ++ items)
where where
-- replace and collect the enum types in this description -- replace and collect the enum types in this description
(Part extern kw lifetime name ports items, enums) = (Part extern kw lifetime name ports items, enumPairs) =
convertDescription' description
-- convert the collected enums into their corresponding localparams
enumItems = map MIPackageItem $ map toItem $ sortOn snd $ convergeUsage items enumPairs
convertDescription (description @ (Package _ _ _)) =
Package ml name (items ++ enumItems)
where
-- replace and collect the enum types in this description
(Package ml name items, enumPairs) =
convertDescription' description
-- convert the collected enums into their corresponding localparams
enumItems = map toItem $ sortOn snd $ enumPairs
convertDescription other = other
-- replace and collect the enum types in a description
convertDescription' :: Description -> (Description, [EnumItem])
convertDescription' description =
(description', enumPairs)
where
-- replace and collect the enum types in this description
(description', enums) =
runWriter $ runWriter $
traverseModuleItemsM (traverseTypesM traverseType) $ traverseModuleItemsM (traverseTypesM traverseType) $
traverseModuleItems (traverseExprs $ traverseNestedExprs traverseExpr) $ traverseModuleItems (traverseExprs $ traverseNestedExprs traverseExpr) $
description 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
enumItems = map toItem $ sortOn snd $ convergeUsage items enumPairs
convertDescription other = other
-- add only the enums actually used in the given items -- add only the enums actually used in the given items
convergeUsage :: [ModuleItem] -> [EnumItem] -> [EnumItem] convergeUsage :: [ModuleItem] -> [EnumItem] -> [EnumItem]
...@@ -63,7 +81,7 @@ convergeUsage items enums = ...@@ -63,7 +81,7 @@ convergeUsage items enums =
where where
-- determine which of the enum items are actually used here -- determine which of the enum items are actually used here
(usedEnums, unusedEnums) = partition isUsed enums (usedEnums, unusedEnums) = partition isUsed enums
enumItems = map toItem usedEnums enumItems = map MIPackageItem $ map toItem usedEnums
isUsed ((_, x), _) = Set.member x usedIdents isUsed ((_, x), _) = Set.member x usedIdents
usedIdents = execWriter $ usedIdents = execWriter $
mapM (collectExprsM $ collectNestedExprsM collectIdent) $ items mapM (collectExprsM $ collectNestedExprsM collectIdent) $ items
...@@ -71,9 +89,9 @@ convergeUsage items enums = ...@@ -71,9 +89,9 @@ convergeUsage items enums =
collectIdent (Ident x) = tell $ Set.singleton x collectIdent (Ident x) = tell $ Set.singleton x
collectIdent _ = return () collectIdent _ = return ()
toItem :: EnumItem -> ModuleItem toItem :: EnumItem -> PackageItem
toItem ((r, x), v) = toItem ((r, x), v) =
MIPackageItem $ Decl $ Localparam itemType x v' Decl $ Localparam itemType x v'
where where
v' = sizedExpr x r (simplify v) v' = sizedExpr x r (simplify v)
itemType = Implicit Unspecified [r] itemType = Implicit Unspecified [r]
...@@ -103,6 +121,7 @@ simplifyRange (a, b) = (simplify a, simplify b) ...@@ -103,6 +121,7 @@ simplifyRange (a, b) = (simplify a, simplify b)
-- drop any enum type casts in favor of implicit conversion from the -- drop any enum type casts in favor of implicit conversion from the
-- converted type -- converted type
traverseExpr :: Expr -> Expr traverseExpr :: Expr -> Expr
traverseExpr (Cast (Left (IntegerVector _ _ _)) e) = e
traverseExpr (Cast (Left (Enum _ _ _)) e) = e traverseExpr (Cast (Left (Enum _ _ _)) e) = e
traverseExpr other = other traverseExpr other = other
......
package foo_pkg;
typedef enum logic [2:0] {
AccessAck = 3'd0,
AccessAckData = 3'd1
} inp_t;
endpackage
module top;
import foo_pkg::*;
inp_t test;
assign test = foo_pkg::AccessAck;
initial $display(test);
endmodule
module top;
localparam [2:0] foo_pkg_AccessAck = 3'd0;
wire [2:0] test;
assign test = foo_pkg_AccessAck;
initial $display(test);
endmodule
package foo_pkg;
typedef enum logic [2:0] {
AccessAck = 3'd0,
AccessAckData = 3'd1
} inp_t;
endpackage
module top;
import foo_pkg::*;
wire [2:0] test;
always_comb begin
case (test)
AccessAck: $display("Ack");
default : $display("default");
endcase
end
endmodule
module top;
localparam [2:0] AccessAck = 3'd0;
wire [2:0] test;
always @(*) begin
case (test)
AccessAck: $display("Ack");
default : $display("default");
endcase
end
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