support for interface based typedefs

This commit is contained in:
Zachary Snow 2021-04-15 11:35:20 -04:00
parent 1ba5ab2739
commit ecee8b3358
7 changed files with 38 additions and 2 deletions

View File

@ -107,8 +107,8 @@ Other:
## Supported Features ## Supported Features
sv2v supports most synthesizable SystemVerilog features. Current notable sv2v supports most synthesizable SystemVerilog features. Current notable
exceptions include `defparam` on interface instances and references to typedefs exceptions include `defparam` on interface instances and certain synthesizable
within interface instances. Assertions are also supported, but are simply usages of parameterized classes. Assertions are also supported, but are simply
dropped during conversion. dropped during conversion.
If you find a bug or have a feature request, please create an issue. Preference If you find a bug or have a feature request, please create an issue. Preference

View File

@ -821,6 +821,7 @@ traverseSinglyNestedTypesM mapper = tm
tm (IntegerAtom kw sg ) = return $ IntegerAtom kw sg tm (IntegerAtom kw sg ) = return $ IntegerAtom kw sg
tm (NonInteger kw ) = return $ NonInteger kw tm (NonInteger kw ) = return $ NonInteger kw
tm (TypeOf expr ) = return $ TypeOf expr tm (TypeOf expr ) = return $ TypeOf expr
tm (TypedefRef expr ) = return $ TypedefRef expr
tm (InterfaceT x y r) = return $ InterfaceT x y r tm (InterfaceT x y r) = return $ InterfaceT x y r
tm (Enum t vals r) = do tm (Enum t vals r) = do
t' <- mapper t t' <- mapper t
@ -879,6 +880,7 @@ traverseTypeExprsM exprMapper =
typeOrExprMapper (Right e) = exprMapper e >>= return . Right typeOrExprMapper (Right e) = exprMapper e >>= return . Right
typeMapper (TypeOf expr) = typeMapper (TypeOf expr) =
exprMapper expr >>= return . TypeOf exprMapper expr >>= return . TypeOf
-- TypedefRef is excluded because it isn't really an expression
typeMapper (CSAlias ps pm xx rs) = do typeMapper (CSAlias ps pm xx rs) = do
vals' <- mapM typeOrExprMapper $ map snd pm vals' <- mapM typeOrExprMapper $ map snd pm
let pm' = zip (map fst pm) vals' let pm' = zip (map fst pm) vals'

View File

@ -95,6 +95,11 @@ traverseTypeM (Alias st rs1) = do
Just (_, _, UnknownType) -> Alias st rs1' Just (_, _, UnknownType) -> Alias st rs1'
Just (_, _, typ) -> tf $ rs1' ++ rs2 Just (_, _, typ) -> tf $ rs1' ++ rs2
where (tf, rs2) = typeRanges typ where (tf, rs2) = typeRanges typ
traverseTypeM (TypedefRef expr) = do
details <- lookupElemM expr
return $ case details of
Nothing -> TypedefRef expr
Just (_, _, typ) -> typ
traverseTypeM other = traverseTypeM other =
traverseSinglyNestedTypesM traverseTypeM other traverseSinglyNestedTypesM traverseTypeM other
>>= traverseTypeExprsM traverseExprM >>= traverseTypeExprsM traverseExprM

View File

@ -51,6 +51,7 @@ data Type
| Union Packing [Field] [Range] | Union Packing [Field] [Range]
| InterfaceT Identifier Identifier [Range] | InterfaceT Identifier Identifier [Range]
| TypeOf Expr | TypeOf Expr
| TypedefRef Expr
| UnpackedType Type [Range] -- used internally | UnpackedType Type [Range] -- used internally
deriving (Eq, Ord) deriving (Eq, Ord)
@ -74,6 +75,7 @@ instance Show Type where
show (Union p items r) = printf "union %s{\n%s\n}%s" (showPad p) (showFields items) (showRanges r) show (Union p items r) = printf "union %s{\n%s\n}%s" (showPad p) (showFields items) (showRanges r)
show (TypeOf expr) = printf "type(%s)" (show expr) show (TypeOf expr) = printf "type(%s)" (show expr)
show (UnpackedType t rs) = printf "UnpackedType(%s, %s)" (show t) (showRanges rs) show (UnpackedType t rs) = printf "UnpackedType(%s, %s)" (show t) (showRanges rs)
show (TypedefRef e) = show e
showFields :: [Field] -> String showFields :: [Field] -> String
showFields items = itemsStr showFields items = itemsStr
@ -112,6 +114,7 @@ typeRanges typ =
IntegerAtom kw sg -> (nullRange $ IntegerAtom kw sg, []) IntegerAtom kw sg -> (nullRange $ IntegerAtom kw sg, [])
NonInteger kw -> (nullRange $ NonInteger kw , []) NonInteger kw -> (nullRange $ NonInteger kw , [])
TypeOf expr -> (nullRange $ TypeOf expr, []) TypeOf expr -> (nullRange $ TypeOf expr, [])
TypedefRef expr -> (nullRange $ TypedefRef expr, [])
nullRange :: Type -> ([Range] -> Type) nullRange :: Type -> ([Range] -> Type)
nullRange t [] = t nullRange t [] = t

View File

@ -848,6 +848,7 @@ PackageItem :: { [PackageItem] }
NonDeclPackageItem :: { [PackageItem] } NonDeclPackageItem :: { [PackageItem] }
: "typedef" Type Identifier ";" { [Decl $ ParamType Localparam $3 $2] } : "typedef" Type Identifier ";" { [Decl $ ParamType Localparam $3 $2] }
| "typedef" Type Identifier DimensionsNonEmpty ";" { [Decl $ ParamType Localparam $3 (UnpackedType $2 $4)] } | "typedef" Type Identifier DimensionsNonEmpty ";" { [Decl $ ParamType Localparam $3 (UnpackedType $2 $4)] }
| "typedef" TypedefRef Identifier ";" { [Decl $ ParamType Localparam $3 $2] }
| "function" Lifetime FuncRetAndName TFItems DeclsAndStmts endfunction opt(Tag) { [Function $2 (fst $3) (snd $3) (map makeInput $4 ++ fst $5) (snd $5)] } | "function" Lifetime FuncRetAndName TFItems DeclsAndStmts endfunction opt(Tag) { [Function $2 (fst $3) (snd $3) (map makeInput $4 ++ fst $5) (snd $5)] }
| "function" Lifetime "void" Identifier TFItems DeclsAndStmts endfunction opt(Tag) { [Task $2 $4 ($5 ++ fst $6) (snd $6)] } | "function" Lifetime "void" Identifier TFItems DeclsAndStmts endfunction opt(Tag) { [Task $2 $4 ($5 ++ fst $6) (snd $6)] }
| "task" Lifetime Identifier TFItems DeclsAndStmts endtask opt(Tag) { [Task $2 $3 ($4 ++ fst $5) (snd $5)] } | "task" Lifetime Identifier TFItems DeclsAndStmts endtask opt(Tag) { [Task $2 $3 ($4 ++ fst $5) (snd $5)] }
@ -857,6 +858,9 @@ NonDeclPackageItem :: { [PackageItem] }
| ForwardTypedef ";" { $1 } | ForwardTypedef ";" { $1 }
| TimeunitsDeclaration { $1 } | TimeunitsDeclaration { $1 }
| Directive { [Directive $1] } | Directive { [Directive $1] }
TypedefRef :: { Type }
: Identifier "." Identifier { TypedefRef $ Dot (Ident $1) $3 }
| Identifier "[" Expr "]" "." Identifier { TypedefRef $ Dot (Bit (Ident $1) $3) $6 }
ForwardTypedef :: { [PackageItem] } ForwardTypedef :: { [PackageItem] }
: "typedef" Identifier { [] } : "typedef" Identifier { [] }
| "typedef" "enum" Identifier { [] } | "typedef" "enum" Identifier { [] }

View File

@ -0,0 +1,18 @@
interface intf_i;
typedef int data_t;
endinterface
module sub(intf_i p, intf_i q [2]);
typedef p.data_t p_data_t; // interface based typedef
typedef q[0].data_t q_data_t; // interface based typedef
p_data_t p_data;
q_data_t q_data;
initial $display("p %0d %b", $bits(p_data), p_data);
initial $display("q %0d %b", $bits(q_data), q_data);
endmodule
module top;
intf_i p();
intf_i q[2]();
sub s(p, q);
endmodule

View File

@ -0,0 +1,4 @@
module top;
initial $display("p %0d %b", 32, 32'bx);
initial $display("q %0d %b", 32, 32'bx);
endmodule