Commit 9249c9fa by Zachary Snow

typdef conversion obeys generate scoping

parent ae392d45
......@@ -67,6 +67,7 @@ module Convert.Traverse
, traverseGenItems
, collectGenItemsM
, traverseNestedGenItemsM
, traverseNestedGenItems
, traverseAsgnsM
, traverseAsgns
, collectAsgnsM
......@@ -974,6 +975,9 @@ traverseNestedGenItemsM mapper = fullMapper
fullMapper stmt =
mapper stmt >>= traverseSinglyNestedGenItemsM fullMapper
traverseNestedGenItems :: Mapper GenItem -> Mapper GenItem
traverseNestedGenItems = unmonad traverseNestedGenItemsM
flattenGenBlocks :: GenItem -> [GenItem]
flattenGenBlocks (GenBlock "" items) = items
flattenGenBlocks (GenFor _ _ _ GenNull) = []
......
......@@ -35,17 +35,20 @@ convert =
convertDescription :: Types -> Description -> Description
convertDescription globalTypes description =
traverseModuleItems removeTypedef $
traverseModuleItems convertModuleItem $
traverseModuleItems (traverseExprs $ traverseNestedExprs $ convertExpr) $
traverseModuleItems (traverseTypes $ resolveType types) $
description
traverseModuleItems (convertTypedef types) description'
where
description' =
traverseModuleItems (traverseGenItems convertGenItem) description
types = Map.union globalTypes $
execWriter $ collectModuleItemsM getTypedef description
getTypedef :: ModuleItem -> Writer Types ()
getTypedef (MIPackageItem (Typedef a b)) = tell $ Map.singleton b a
getTypedef _ = return ()
execWriter $ collectModuleItemsM collectTypedefM description'
convertTypedef :: Types -> ModuleItem -> ModuleItem
convertTypedef types =
removeTypedef .
convertModuleItem .
(traverseExprs $ traverseNestedExprs $ convertExpr) .
(traverseTypes $ resolveType types)
where
removeTypedef :: ModuleItem -> ModuleItem
removeTypedef (MIPackageItem (Typedef _ x)) =
MIPackageItem $ Decl $ CommentDecl $ "removed typedef: " ++ x
......@@ -71,6 +74,27 @@ convertDescription globalTypes description =
where mapParam (i, v) = (i, convertTypeOrExpr v)
convertModuleItem other = other
convertGenItem :: GenItem -> GenItem
convertGenItem (GenIf c a b) =
GenIf c a' b'
where
a' = convertGenItem' a
b' = convertGenItem' b
convertGenItem other = other
convertGenItem' :: GenItem -> GenItem
convertGenItem' item = do
GenBlock "" items
where
-- convert inner generate blocks first
item' = Generate [traverseNestedGenItems convertGenItem item]
types = execWriter $ collectNestedModuleItemsM collectTypedefM item'
Generate items = traverseNestedModuleItems (convertTypedef types) item'
collectTypedefM :: ModuleItem -> Writer Types ()
collectTypedefM (MIPackageItem (Typedef a b)) = tell $ Map.singleton b a
collectTypedefM _ = return ()
resolveItem :: Types -> (Type, Identifier) -> (Type, Identifier)
resolveItem types (t, x) = (resolveType types t, x)
......
module Example;
parameter FLAG_1 = 0;
parameter FLAG_2 = 0;
typedef logic [2:0] T;
if (FLAG_1) begin
typedef logic [1:0] T;
T t = 0;
initial $display("2 %b", t);
if (FLAG_2) begin
typedef logic [3:0] T;
T t = 0;
initial $display("4 %b", t);
end
end
else begin
typedef logic T;
T t = 0;
initial $display("1 %b", t);
end
T t = 0;
initial $display("3 %b", t);
endmodule
module top;
Example #(0, 0) a();
Example #(1, 0) b();
Example #(0, 1) c();
Example #(1, 1) d();
endmodule
module Example;
parameter FLAG_1 = 0;
parameter FLAG_2 = 0;
if (FLAG_1) begin
wire [1:0] t = 0;
initial $display("2 %b", t);
if (FLAG_2) begin
wire [3:0] t = 0;
initial $display("4 %b", t);
end
end
else begin
wire t = 0;
initial $display("1 %b", t);
end
wire [2:0] t = 0;
initial $display("3 %b", t);
endmodule
module top;
Example #(0, 0) a();
Example #(1, 0) b();
Example #(0, 1) c();
Example #(1, 1) d();
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