From 0a65abd61400d63876070ac121f942ab198d750e Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Sat, 29 May 2021 22:23:20 -0400 Subject: [PATCH] full support for package and class subroutine invocations --- src/Language/SystemVerilog/Parser/Parse.y | 2 ++ .../SystemVerilog/Parser/ParseDecl.hs | 16 +++++++--- test/basic/subroutine.sv | 31 +++++++++++++++++++ test/basic/subroutine.v | 7 +++++ 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test/basic/subroutine.sv create mode 100644 test/basic/subroutine.v diff --git a/src/Language/SystemVerilog/Parser/Parse.y b/src/Language/SystemVerilog/Parser/Parse.y index bee3552..b060ce9 100644 --- a/src/Language/SystemVerilog/Parser/Parse.y +++ b/src/Language/SystemVerilog/Parser/Parse.y @@ -1008,6 +1008,8 @@ StmtAsgn :: { Stmt } | LHS CallArgs ";" { Subroutine (lhsToExpr $1) $2 } | Identifier "::" Identifier ";" { Subroutine (PSIdent $1 $3) (Args [] []) } | Identifier "::" Identifier CallArgs ";" { Subroutine (PSIdent $1 $3) $4 } + | Identifier ParamBindings "::" Identifier ";" { Subroutine (CSIdent $1 $2 $4) (Args [] []) } + | Identifier ParamBindings "::" Identifier CallArgs ";" { Subroutine (CSIdent $1 $2 $4) $5 } StmtNonAsgn :: { Stmt } : StmtBlock(BlockKWSeq, end ) { $1 } | StmtBlock(BlockKWPar, join) { $1 } diff --git a/src/Language/SystemVerilog/Parser/ParseDecl.hs b/src/Language/SystemVerilog/Parser/ParseDecl.hs index deddf71..30032b8 100644 --- a/src/Language/SystemVerilog/Parser/ParseDecl.hs +++ b/src/Language/SystemVerilog/Parser/ParseDecl.hs @@ -265,10 +265,8 @@ parseDTsAsDeclOrStmt tokens = pos = tokPos $ last tokens stmt = case last tokens of DTAsgn _ op mt e -> Asgn op mt lhs e - DTInstance _ args -> Subroutine (lhsToExpr lhs) (instanceToArgs args) - _ -> case takeLHS tokens of - Just fullLHS -> Subroutine (lhsToExpr fullLHS) (Args [] []) - _ -> error $ "invalid block item decl or stmt: " ++ show tokens + DTInstance _ args -> asSubroutine lhsToks (instanceToArgs args) + _ -> asSubroutine tokens (Args [] []) lhsToks = init tokens lhs = case takeLHS lhsToks of Nothing -> error $ "could not parse as LHS: " ++ show lhsToks @@ -282,6 +280,16 @@ parseDTsAsDeclOrStmt tokens = traceStmt :: Position -> Stmt traceStmt pos = CommentStmt $ "Trace: " ++ show pos +-- read the given tokens as the root of a subroutine invocation +asSubroutine :: [DeclToken] -> Args -> Stmt +asSubroutine [DTIdent _ x] = Subroutine $ Ident x +asSubroutine [DTPSIdent _ p x] = Subroutine $ PSIdent p x +asSubroutine [DTCSIdent _ c p x] = Subroutine $ CSIdent c p x +asSubroutine tokens = + case takeLHS tokens of + Just lhs -> Subroutine $ lhsToExpr lhs + Nothing -> error $ "invalid block item decl or stmt: " ++ show tokens + -- converts port bindings to call args instanceToArgs :: [PortBinding] -> Args instanceToArgs bindings = diff --git a/test/basic/subroutine.sv b/test/basic/subroutine.sv new file mode 100644 index 0000000..0c70225 --- /dev/null +++ b/test/basic/subroutine.sv @@ -0,0 +1,31 @@ +class C #( + parameter X = 1 +); + // TODO: this should be static + task dump; + $display("C#(%0d)::dump()", X); + endtask +endclass + +package P; + task dump; + $display("P::dump()"); + endtask +endpackage + +module top; + task dump; + $display("dump()"); + endtask + +`define TEST(subroutine) \ + initial begin subroutine; end \ + initial begin subroutine(); end \ + initial begin ; subroutine; end \ + initial begin ; subroutine(); end + + `TEST(dump) + `TEST(P::dump) + `TEST(C#(1)::dump) + +endmodule diff --git a/test/basic/subroutine.v b/test/basic/subroutine.v new file mode 100644 index 0000000..5cc5fec --- /dev/null +++ b/test/basic/subroutine.v @@ -0,0 +1,7 @@ +module top; +`define TEST(subroutine) \ + initial repeat (4) $display(`"subroutine()`"); + `TEST(dump) + `TEST(P::dump) + `TEST(C#(1)::dump) +endmodule