From a7673c55fd85f57e15ab337dfddc3112e1c61e77 Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Wed, 8 Jul 2020 22:10:18 -0600 Subject: [PATCH] automatically fix simple declaration order issues --- src/Convert.hs | 3 ++- src/Convert/NestPI.hs | 34 ++++++++++++++++++++++++++++------ test/basic/reorder.sv | 5 +++++ test/basic/reorder.v | 5 +++++ 4 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 test/basic/reorder.sv create mode 100644 test/basic/reorder.v diff --git a/src/Convert.hs b/src/Convert.hs index d835ae8..6493d7f 100644 --- a/src/Convert.hs +++ b/src/Convert.hs @@ -104,7 +104,8 @@ run :: [Job.Exclude] -> Phase run excludes = foldr (.) id $ phases excludes convert :: [Job.Exclude] -> Phase -convert excludes = convert' +convert excludes = + convert' . Convert.NestPI.reorder where convert' :: Phase convert' descriptions = diff --git a/src/Convert/NestPI.hs b/src/Convert/NestPI.hs index c001be9..1f21567 100644 --- a/src/Convert/NestPI.hs +++ b/src/Convert/NestPI.hs @@ -4,9 +4,10 @@ - Conversion for moving top-level package items into modules -} -module Convert.NestPI (convert) where +module Convert.NestPI (convert, reorder) where import Control.Monad.Writer +import Data.Maybe (mapMaybe) import qualified Data.Map.Strict as Map import qualified Data.Set as Set @@ -29,6 +30,9 @@ convert = isPI (PackageItem item) = piName item /= "" isPI _ = False +reorder :: [AST] -> [AST] +reorder = map $ traverseDescriptions reorderDescription + -- collects packages items missing collectDescriptionM :: Description -> Writer PIs () collectDescriptionM (PackageItem item) = do @@ -48,6 +52,18 @@ convertDescription pis (orig @ Part{}) = items' = addItems pis Set.empty items convertDescription _ other = other +-- attempt to fix simple declaration order issues +reorderDescription :: Description -> Description +reorderDescription (Part attrs extern kw lifetime name ports items) = + Part attrs extern kw lifetime name ports items' + where + items' = addItems localPIs Set.empty items + localPIs = Map.fromList $ mapMaybe toPIElem items + toPIElem :: ModuleItem -> Maybe (Identifier, PackageItem) + toPIElem (MIPackageItem item) = Just (piName item, item) + toPIElem _ = Nothing +reorderDescription other = other + -- iteratively inserts missing package items exactly where they are needed addItems :: PIs -> Idents -> [ModuleItem] -> [ModuleItem] addItems pis existingPIs (item : items) = @@ -66,7 +82,8 @@ addItems pis existingPIs (item : items) = usedPIs = Set.unions $ map runner [ collectStmtsM collectSubroutinesM , collectTypesM $ collectNestedTypesM collectTypenamesM - , collectExprsM $ collectNestedExprsM collectIdentsM + , collectExprsM $ collectNestedExprsM collectExprIdentsM + , collectLHSsM $ collectNestedLHSsM collectLHSIdentsM ] neededPIs = Set.difference (Set.difference usedPIs existingPIs) thisPI itemsToAdd = map MIPackageItem $ Map.elems $ @@ -87,10 +104,15 @@ collectSubroutinesM (Subroutine (Ident f) _) = tell $ Set.singleton f collectSubroutinesM _ = return () -- writes down the names of function calls and identifiers -collectIdentsM :: Expr -> Writer Idents () -collectIdentsM (Call (Ident x) _) = tell $ Set.singleton x -collectIdentsM (Ident x) = tell $ Set.singleton x -collectIdentsM _ = return () +collectExprIdentsM :: Expr -> Writer Idents () +collectExprIdentsM (Call (Ident x) _) = tell $ Set.singleton x +collectExprIdentsM (Ident x) = tell $ Set.singleton x +collectExprIdentsM _ = return () + +-- writes down the names of identifiers +collectLHSIdentsM :: LHS -> Writer Idents () +collectLHSIdentsM (LHSIdent x) = tell $ Set.singleton x +collectLHSIdentsM _ = return () -- writes down aliased typenames collectTypenamesM :: Type -> Writer Idents () diff --git a/test/basic/reorder.sv b/test/basic/reorder.sv new file mode 100644 index 0000000..ab416f4 --- /dev/null +++ b/test/basic/reorder.sv @@ -0,0 +1,5 @@ +module top; + assign arr[0][0] = 1; + logic [1:0][2:0] arr; + initial $display("%b", arr); +endmodule diff --git a/test/basic/reorder.v b/test/basic/reorder.v new file mode 100644 index 0000000..4b351ef --- /dev/null +++ b/test/basic/reorder.v @@ -0,0 +1,5 @@ +module top; + wire [5:0] arr; + assign arr[0] = 1; + initial $display("%b", arr); +endmodule