Skip to content

Commit

Permalink
disambiguate struct names that shadow module names
Browse files Browse the repository at this point in the history
  • Loading branch information
zachjs committed Dec 9, 2024
1 parent 7808819 commit c56a91b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
declarations tagged with an attribute
* Fixed stray attributes producing invalid nested output when attached to
inlined interfaces and interface-bounds modules
* Fixed conversion of struct variables that shadow their module's name
* Fixed `` `resetall `` not resetting the `` `default_nettype ``

### Other Enhancements
Expand Down
28 changes: 15 additions & 13 deletions src/Convert/Struct.hs
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,15 @@ convertLHS l = do
-- try expression conversion by looking at the *innermost* type first
convertSubExpr :: Scopes Type -> Expr -> (Type, Expr)
convertSubExpr scopes (Dot e x) =
if isntStruct subExprType then
if isntStruct subExprType || isHier then
fallbackType scopes $ Dot e' x
else if structIsntReady subExprType then
(fieldType, Dot e' x)
else
(fieldType, undottedWithSign)
where
(subExprType, e') = convertSubExpr scopes e
(fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
(isHier, fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
base = fst bounds
len = rangeSize bounds
undotted = if null dims || rangeSize (head dims) == RawNum 1
Expand All @@ -403,7 +403,7 @@ convertSubExpr scopes (Dot e x) =
else undotted

convertSubExpr scopes (Range (Dot e x) NonIndexed rOuter) =
if isntStruct subExprType then
if isntStruct subExprType || isHier then
(UnknownType, orig')
else if structIsntReady subExprType then
(replaceInnerTypeRange NonIndexed rOuter' fieldType, orig')
Expand All @@ -420,7 +420,7 @@ convertSubExpr scopes (Range (Dot e x) NonIndexed rOuter) =
(_, roRight') = convertSubExpr scopes roRight
rOuter' = (roLeft', roRight')
orig' = Range (Dot e' x) NonIndexed rOuter'
(fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
(isHier, fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
[dim] = dims
rangeLeft = ( BinOp Sub (fst bounds) $ BinOp Sub (fst dim) roLeft'
, BinOp Sub (fst bounds) $ BinOp Sub (fst dim) roRight' )
Expand All @@ -429,7 +429,7 @@ convertSubExpr scopes (Range (Dot e x) NonIndexed rOuter) =
undotted = Range e' NonIndexed $
endianCondRange dim rangeLeft rangeRight
convertSubExpr scopes (Range (Dot e x) mode (baseO, lenO)) =
if isntStruct subExprType then
if isntStruct subExprType || isHier then
(UnknownType, orig')
else if structIsntReady subExprType then
(replaceInnerTypeRange mode (baseO', lenO') fieldType, orig')
Expand All @@ -444,7 +444,7 @@ convertSubExpr scopes (Range (Dot e x) mode (baseO, lenO)) =
(_, baseO') = convertSubExpr scopes baseO
(_, lenO') = convertSubExpr scopes lenO
orig' = Range (Dot e' x) mode (baseO', lenO')
(fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
(isHier, fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
[dim] = dims
baseLeft = BinOp Sub (fst bounds) $ BinOp Sub (fst dim) baseO'
baseRight = BinOp Add (snd bounds) $ BinOp Sub (snd dim) baseO'
Expand All @@ -463,7 +463,7 @@ convertSubExpr scopes (Range e mode (left, right)) =
(_, right') = convertSubExpr scopes right
r' = (left', right')
convertSubExpr scopes (Bit (Dot e x) i) =
if isntStruct subExprType then
if isntStruct subExprType || isHier then
(dropInnerTypeRange backupType, orig')
else if structIsntReady subExprType then
(dropInnerTypeRange fieldType, orig')
Expand All @@ -477,7 +477,7 @@ convertSubExpr scopes (Bit (Dot e x) i) =
(_, i') = convertSubExpr scopes i
(backupType, _) = fallbackType scopes $ Dot e' x
orig' = Bit (Dot e' x) i'
(fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
(isHier, fieldType, bounds, dims) = lookupFieldInfo scopes subExprType e' x
[dim] = dims
left = BinOp Sub (fst bounds) $ BinOp Sub (fst dim) i'
right = BinOp Add (snd bounds) $ BinOp Sub (snd dim) i'
Expand Down Expand Up @@ -536,20 +536,22 @@ isntStruct = (== Nothing) . getFields

-- get the field type, flattened bounds, and original type dimensions
lookupFieldInfo :: Scopes Type -> Type -> Expr -> Identifier
-> (Type, Range, [Range])
-> (Bool, Type, Range, [Range])
lookupFieldInfo scopes struct base fieldName =
if maybeFieldType == Nothing
then scopedError scopes $ "field '" ++ fieldName ++ "' not found in "
++ show struct ++ ", in expression "
++ show (Dot base fieldName)
else (fieldType, bounds, dims)
then (isHier, err, err, err)
else (False, fieldType, bounds, dims)
where
Just fields = getFields struct
maybeFieldType = lookup fieldName $ map swap fields
Just fieldType = maybeFieldType
dims = snd $ typeRanges fieldType
Just (_, unstructRanges) = convertStruct struct
Just bounds = lookup fieldName unstructRanges
err = scopedError scopes $ "field '" ++ fieldName ++ "' not found in "
++ show struct ++ ", in expression "
++ show (Dot base fieldName)
isHier = lookupElem scopes (Dot base fieldName) /= Nothing

-- attempts to convert based on the assignment-like contexts of TF arguments
convertCall :: Scopes Type -> Expr -> Args -> Args
Expand Down
5 changes: 4 additions & 1 deletion src/Convert/TypeOf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ isStringParam _ = return False
-- checks if referring to part.wire is needlessly explicit
unneededModuleScope :: Identifier -> Identifier -> ST Bool
unneededModuleScope part wire = do
partDetails <- lookupElemM part
accessesLocal <- localAccessesM wire
if accessesLocal == accessesTop then
if partDetails /= Nothing then
return False
else if accessesLocal == accessesTop then
return True
else if head accessesLocal == head accessesTop then do
details <- lookupElemM wire
Expand Down
19 changes: 19 additions & 0 deletions test/core/struct_hier_shadow.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package P;
localparam [31:0] L = 8;
typedef struct packed {
logic [L + L[0] + L[1:0] + L[0+:1] - 1:0] x;
} S;
endpackage
module top;
P::S top;
logic [2:0] x;
assign x = '0;
assign top.x = '1;
initial begin
#1;
$display("%b %b", x, top.x);
$display("%b %b", x[0], top.x[0]);
$display("%b %b", x[1:0], top.x[1:0]);
$display("%b %b", x[0+:1], top.x[0+:1]);
end
endmodule
13 changes: 13 additions & 0 deletions test/core/struct_hier_shadow.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module top;
wire [7:0] top;
wire [2:0] x;
assign x = 0;
assign top = 8'hFF;
initial begin
#1;
$display("%b %b", x, top);
$display("%b %b", x[0], top[0]);
$display("%b %b", x[1:0], top[1:0]);
$display("%b %b", x[0+:1], top[0+:1]);
end
endmodule

0 comments on commit c56a91b

Please sign in to comment.