Commit 9249c9fa by Zachary Snow

typdef conversion obeys generate scoping

parent ae392d45
...@@ -67,6 +67,7 @@ module Convert.Traverse ...@@ -67,6 +67,7 @@ module Convert.Traverse
, traverseGenItems , traverseGenItems
, collectGenItemsM , collectGenItemsM
, traverseNestedGenItemsM , traverseNestedGenItemsM
, traverseNestedGenItems
, traverseAsgnsM , traverseAsgnsM
, traverseAsgns , traverseAsgns
, collectAsgnsM , collectAsgnsM
...@@ -974,6 +975,9 @@ traverseNestedGenItemsM mapper = fullMapper ...@@ -974,6 +975,9 @@ traverseNestedGenItemsM mapper = fullMapper
fullMapper stmt = fullMapper stmt =
mapper stmt >>= traverseSinglyNestedGenItemsM fullMapper mapper stmt >>= traverseSinglyNestedGenItemsM fullMapper
traverseNestedGenItems :: Mapper GenItem -> Mapper GenItem
traverseNestedGenItems = unmonad traverseNestedGenItemsM
flattenGenBlocks :: GenItem -> [GenItem] flattenGenBlocks :: GenItem -> [GenItem]
flattenGenBlocks (GenBlock "" items) = items flattenGenBlocks (GenBlock "" items) = items
flattenGenBlocks (GenFor _ _ _ GenNull) = [] flattenGenBlocks (GenFor _ _ _ GenNull) = []
......
...@@ -35,20 +35,23 @@ convert = ...@@ -35,20 +35,23 @@ convert =
convertDescription :: Types -> Description -> Description convertDescription :: Types -> Description -> Description
convertDescription globalTypes description = convertDescription globalTypes description =
traverseModuleItems removeTypedef $ traverseModuleItems (convertTypedef types) description'
traverseModuleItems convertModuleItem $
traverseModuleItems (traverseExprs $ traverseNestedExprs $ convertExpr) $
traverseModuleItems (traverseTypes $ resolveType types) $
description
where where
description' =
traverseModuleItems (traverseGenItems convertGenItem) description
types = Map.union globalTypes $ types = Map.union globalTypes $
execWriter $ collectModuleItemsM getTypedef description execWriter $ collectModuleItemsM collectTypedefM description'
getTypedef :: ModuleItem -> Writer Types ()
getTypedef (MIPackageItem (Typedef a b)) = tell $ Map.singleton b a convertTypedef :: Types -> ModuleItem -> ModuleItem
getTypedef _ = return () convertTypedef types =
removeTypedef .
convertModuleItem .
(traverseExprs $ traverseNestedExprs $ convertExpr) .
(traverseTypes $ resolveType types)
where
removeTypedef :: ModuleItem -> ModuleItem removeTypedef :: ModuleItem -> ModuleItem
removeTypedef (MIPackageItem (Typedef _ x)) = removeTypedef (MIPackageItem (Typedef _ x)) =
MIPackageItem $ Decl $ CommentDecl $ "removed typedef: " ++ x MIPackageItem $ Decl $ CommentDecl $ "removed typedef: " ++ x
removeTypedef other = other removeTypedef other = other
convertTypeOrExpr :: TypeOrExpr -> TypeOrExpr convertTypeOrExpr :: TypeOrExpr -> TypeOrExpr
convertTypeOrExpr (Left (TypeOf (Ident x))) = convertTypeOrExpr (Left (TypeOf (Ident x))) =
...@@ -71,6 +74,27 @@ convertDescription globalTypes description = ...@@ -71,6 +74,27 @@ convertDescription globalTypes description =
where mapParam (i, v) = (i, convertTypeOrExpr v) where mapParam (i, v) = (i, convertTypeOrExpr v)
convertModuleItem other = other 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 -> (Type, Identifier) -> (Type, Identifier)
resolveItem types (t, x) = (resolveType types t, x) 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