Commit 2081f6a3 by Zachary Snow

support attributes on port declarations

parent d137fd3d
...@@ -646,6 +646,7 @@ PortDeclTokens(delim) :: { [DeclToken] } ...@@ -646,6 +646,7 @@ PortDeclTokens(delim) :: { [DeclToken] }
: DeclTokensBase(PortDeclTokens(delim), delim) { $1 } : DeclTokensBase(PortDeclTokens(delim), delim) { $1 }
| GenericInterfaceDecl PortDeclTokens(delim) { $1 ++ $2} | GenericInterfaceDecl PortDeclTokens(delim) { $1 ++ $2}
| GenericInterfaceDecl delim { $1 } | GenericInterfaceDecl delim { $1 }
| AttributeInstance PortDeclTokens(delim) {% posInject \p -> DTAttr p $1 : $2 }
GenericInterfaceDecl :: { [DeclToken] } GenericInterfaceDecl :: { [DeclToken] }
: "interface" Identifier {% posInject \p -> [DTType p (\Unspecified -> InterfaceT "" Nothing), DTIdent p $2] } : "interface" Identifier {% posInject \p -> [DTType p (\Unspecified -> InterfaceT "" Nothing), DTIdent p $2] }
......
...@@ -69,6 +69,7 @@ data DeclToken ...@@ -69,6 +69,7 @@ data DeclToken
| DTDot Position Identifier | DTDot Position Identifier
| DTSigning Position Signing | DTSigning Position Signing
| DTLifetime Position Lifetime | DTLifetime Position Lifetime
| DTAttr Position Attr
deriving (Show, Eq) deriving (Show, Eq)
-- entrypoints besides `parseDTsAsDeclOrStmt` use this to disallow `DTAsgn` with -- entrypoints besides `parseDTsAsDeclOrStmt` use this to disallow `DTAsgn` with
...@@ -93,7 +94,7 @@ parseDTsAsPortDecls pieces = ...@@ -93,7 +94,7 @@ parseDTsAsPortDecls pieces =
forbidNonEqAsgn pieces $ forbidNonEqAsgn pieces $
if isSimpleList if isSimpleList
then (simpleIdents, []) then (simpleIdents, [])
else (portNames declarations, map (MIPackageItem . Decl) declarations) else (portNames declarations, applyAttrs [] pieces declarations)
where where
commaIdxs = findIndices isComma pieces commaIdxs = findIndices isComma pieces
identIdxs = findIndices isIdent pieces identIdxs = findIndices isIdent pieces
...@@ -104,10 +105,15 @@ parseDTsAsPortDecls pieces = ...@@ -104,10 +105,15 @@ parseDTsAsPortDecls pieces =
length pieces == length commaIdxs + length identIdxs length pieces == length commaIdxs + length identIdxs
simpleIdents = map extractIdent $ filter isIdent pieces simpleIdents = map extractIdent $ filter isIdent pieces
declarations = propagateDirections Input $ parseDTsAsDecls pieces declarations = propagateDirections Input $ parseDTsAsDecls pieces'
extractIdent = \(DTIdent _ x) -> x extractIdent = \(DTIdent _ x) -> x
pieces' = filter (not . isDTAttr) pieces
isDTAttr :: DeclToken -> Bool
isDTAttr DTAttr{} = True
isDTAttr _ = False
propagateDirections :: Direction -> [Decl] -> [Decl] propagateDirections :: Direction -> [Decl] -> [Decl]
propagateDirections dir (decl @ (Variable _ InterfaceT{} _ _ _) : decls) = propagateDirections dir (decl @ (Variable _ InterfaceT{} _ _ _) : decls) =
decl : propagateDirections dir decls decl : propagateDirections dir decls
...@@ -128,6 +134,23 @@ parseDTsAsPortDecls pieces = ...@@ -128,6 +134,23 @@ parseDTsAsPortDecls pieces =
portName decl = portName decl =
error $ "unexpected non-variable port declaration: " ++ (show decl) error $ "unexpected non-variable port declaration: " ++ (show decl)
applyAttrs :: [Attr] -> [DeclToken] -> [Decl] -> [ModuleItem]
applyAttrs _ [] [] = []
applyAttrs _ tokens (CommentDecl c : decls) =
MIPackageItem (Decl $ CommentDecl c) : applyAttrs [] tokens decls
applyAttrs attrs (DTAttr _ attr : tokens) decls =
applyAttrs (attr : attrs) tokens decls
applyAttrs attrs [] [decl] =
[wrapDecl attrs decl]
applyAttrs attrs (DTComma{} : tokens) (decl : decls) =
wrapDecl attrs decl : applyAttrs attrs tokens decls
applyAttrs attrs (_ : tokens) decls =
applyAttrs attrs tokens decls
applyAttrs _ [] _ = error "applyAttrs internal invariant failed"
wrapDecl :: [Attr] -> Decl -> ModuleItem
wrapDecl attrs decl = foldr MIAttr (MIPackageItem $ Decl decl) attrs
-- [PUBLIC]: parser for single (semicolon-terminated) declarations (including -- [PUBLIC]: parser for single (semicolon-terminated) declarations (including
-- parameters) and module instantiations -- parameters) and module instantiations
...@@ -494,3 +517,4 @@ tokPos (DTStream p _ _ _) = p ...@@ -494,3 +517,4 @@ tokPos (DTStream p _ _ _) = p
tokPos (DTDot p _) = p tokPos (DTDot p _) = p
tokPos (DTSigning p _) = p tokPos (DTSigning p _) = p
tokPos (DTLifetime p _) = p tokPos (DTLifetime p _) = p
tokPos (DTAttr p _) = p
module Example(
input a, b,
(* who=1 *) output c,
(* what=2 *) output d, e,
input f,
(* when=3 *) (* where=4 *) output g, h, i
);
endmodule
(* a=1 *) module top; (* a=1 *) module top;
(* foo="bar" *) logic x; (* foo="bar" *) reg x;
initial begin initial begin
x = 1; x = 1;
$display(x); $display(x);
end end
reg a, b;
wire c;
wire d, e;
reg f;
wire g, h, i;
Example m(a,b,c,d,e,f,g,h,i);
endmodule endmodule
(* a=1 *) module top; `include "attr.sv"
(* foo="bar" *) reg x;
initial begin
x = 1;
$display(x);
end
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