From 24071d74acf3a8dd8ba4159e9f4978fe71ab3b1b Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Wed, 24 Jun 2020 21:49:59 -0600 Subject: [PATCH] paramtype conversion resolves dimension queries before substitution --- src/Convert/ParamType.hs | 28 +++++++++++++++++++++------- test/basic/paramtype_bits.sv | 16 ++++++++++++++++ test/basic/paramtype_bits.v | 20 ++++++++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 test/basic/paramtype_bits.sv create mode 100644 test/basic/paramtype_bits.v diff --git a/src/Convert/ParamType.hs b/src/Convert/ParamType.hs index aff0c17..2ff6565 100644 --- a/src/Convert/ParamType.hs +++ b/src/Convert/ParamType.hs @@ -219,13 +219,27 @@ exprToType _ = Nothing -- TODO: If a type parameter contains an expression, that expression should be -- substituted into the new module, or created as a new parameter. isSimpleType :: Type -> Bool -isSimpleType (IntegerVector _ _ _) = True -isSimpleType (IntegerAtom _ _ ) = True -isSimpleType (NonInteger _ ) = True -isSimpleType (Net _ _ _) = True -isSimpleType (Struct _ fields _) = all (isSimpleType . fst) fields -isSimpleType (Union _ fields _) = all (isSimpleType . fst) fields -isSimpleType _ = False +isSimpleType typ = + (not $ typeHasQueries typ) && + case typ of + IntegerVector{} -> True + IntegerAtom {} -> True + NonInteger {} -> True + Net {} -> True + Struct _ fields _ -> all (isSimpleType . fst) fields + Union _ fields _ -> all (isSimpleType . fst) fields + _ -> False + +-- returns whether a type contains any dimension queries +typeHasQueries :: Type -> Bool +typeHasQueries = + not . null . execWriter . collectTypeExprsM + (collectNestedExprsM collectUnresolvedExprM) + where + collectUnresolvedExprM :: Expr -> Writer [Expr] () + collectUnresolvedExprM (expr @ DimsFn{}) = tell [expr] + collectUnresolvedExprM (expr @ DimFn {}) = tell [expr] + collectUnresolvedExprM _ = return () -- attempt to rewrite instantiations with type parameters convertModuleItemM :: Info -> ModuleItem -> Writer Instances ModuleItem diff --git a/test/basic/paramtype_bits.sv b/test/basic/paramtype_bits.sv new file mode 100644 index 0000000..a94c2ab --- /dev/null +++ b/test/basic/paramtype_bits.sv @@ -0,0 +1,16 @@ +module Module(out); + parameter type T = logic; + output T out; + assign out = '1; +endmodule + +module top; + logic w; + logic [$bits(w)-1:0] b; + + logic o0, o1; + logic [1:0] o2; + Module m0(o0); + Module #(logic[$bits(w)-1:0]) m1(o1); + Module #(logic[$bits(b)*2-1:0]) m2(o2); +endmodule diff --git a/test/basic/paramtype_bits.v b/test/basic/paramtype_bits.v new file mode 100644 index 0000000..29a3cc7 --- /dev/null +++ b/test/basic/paramtype_bits.v @@ -0,0 +1,20 @@ +module Module_Size1(out); + output wire out; + assign out = 1'b1; +endmodule + +module Module_Size2(out); + output wire [1:0] out; + assign out = 2'b11; +endmodule + +module top; + wire w; + wire [0:0] b; + + wire o0, o1; + wire [1:0] o2; + Module_Size1 m0(o0); + Module_Size1 m1(o1); + Module_Size2 m2(o2); +endmodule