Commit c7375d90 by Zachary Snow

fix pass-through codegen and parsing issues

- automatic pass-through test suite coverage
- fix codegen for interface based typedefs
- allow declaration of generic interface ports in module body
- fix codegen for comments within for loop initialization
- fix codegen of class param decls
parent d95a5628
...@@ -10,12 +10,14 @@ module Language.SystemVerilog.AST.Decl ...@@ -10,12 +10,14 @@ module Language.SystemVerilog.AST.Decl
( Decl (..) ( Decl (..)
, Direction (..) , Direction (..)
, ParamScope (..) , ParamScope (..)
, showDecls
) where ) where
import Data.List (intercalate)
import Text.Printf (printf) import Text.Printf (printf)
import Language.SystemVerilog.AST.ShowHelp (showPad, unlines') import Language.SystemVerilog.AST.ShowHelp (showPad, unlines')
import Language.SystemVerilog.AST.Type (Type(UnpackedType), Identifier, pattern UnknownType) import Language.SystemVerilog.AST.Type (Type(TypedefRef, UnpackedType), Identifier, pattern UnknownType)
import Language.SystemVerilog.AST.Expr (Expr, Range, showRanges, showAssignment) import Language.SystemVerilog.AST.Expr (Expr, Range, showRanges, showAssignment)
data Decl data Decl
...@@ -28,6 +30,8 @@ data Decl ...@@ -28,6 +30,8 @@ data Decl
instance Show Decl where instance Show Decl where
showList l _ = unlines' $ map show l showList l _ = unlines' $ map show l
show (Param s t x e) = printf "%s %s%s%s;" (show s) (showPad t) x (showAssignment e) show (Param s t x e) = printf "%s %s%s%s;" (show s) (showPad t) x (showAssignment e)
show (ParamType Localparam x (TypedefRef e)) =
printf "typedef %s %s;" (show e) x
show (ParamType Localparam x (UnpackedType t rs)) = show (ParamType Localparam x (UnpackedType t rs)) =
printf "typedef %s %s%s;" (show t) x (showRanges rs) printf "typedef %s %s%s;" (show t) x (showRanges rs)
show (ParamType s x t) = printf "%s type %s%s;" (show s) x tStr show (ParamType s x t) = printf "%s type %s%s;" (show s) x tStr
...@@ -38,6 +42,20 @@ instance Show Decl where ...@@ -38,6 +42,20 @@ instance Show Decl where
then "// " ++ show c then "// " ++ show c
else "// " ++ c else "// " ++ c
showDecls :: Char -> String -> [Decl] -> String
showDecls delim whitespace =
dropDelim . intercalate whitespace . map showDecl
where
dropDelim :: String -> String
dropDelim [] = []
dropDelim [x] = if x == delim then [] else [x]
dropDelim (x : xs) = x : dropDelim xs
showDecl (CommentDecl c) =
if whitespace == " "
then "/* " ++ c ++ " */"
else show $ CommentDecl c
showDecl decl = (init $ show decl) ++ [delim]
data Direction data Direction
= Input = Input
| Output | Output
......
...@@ -19,7 +19,7 @@ import Text.Printf (printf) ...@@ -19,7 +19,7 @@ import Text.Printf (printf)
import Language.SystemVerilog.AST.ShowHelp import Language.SystemVerilog.AST.ShowHelp
import Language.SystemVerilog.AST.Attr (Attr) import Language.SystemVerilog.AST.Attr (Attr)
import Language.SystemVerilog.AST.Decl (Decl) import Language.SystemVerilog.AST.Decl (Decl, showDecls)
import Language.SystemVerilog.AST.Stmt (Stmt) import Language.SystemVerilog.AST.Stmt (Stmt)
import Language.SystemVerilog.AST.Type (Type, Identifier) import Language.SystemVerilog.AST.Type (Type, Identifier)
import {-# SOURCE #-} Language.SystemVerilog.AST.ModuleItem (ModuleItem) import {-# SOURCE #-} Language.SystemVerilog.AST.ModuleItem (ModuleItem)
...@@ -53,13 +53,17 @@ instance Show Description where ...@@ -53,13 +53,17 @@ instance Show Description where
where where
bodyStr = indent $ unlines' $ map show items bodyStr = indent $ unlines' $ map show items
show (Class lifetime name decls items) = show (Class lifetime name decls items) =
printf "class %s%s;\n%s\nendclass" printf "class %s%s%s;\n%s\nendclass"
(showPad lifetime) name bodyStr (showPad lifetime) name (showParamDecls decls) bodyStr
where where
bodyStr = indent $ unlines' $ map showClassItem items' bodyStr = indent $ unlines' $ map showClassItem items
items' = (map (\decl -> (QNone, Decl decl)) decls) ++ items
show (PackageItem i) = show i show (PackageItem i) = show i
showParamDecls :: [Decl] -> String
showParamDecls [] = ""
showParamDecls decls = " #(\n\t" ++ str ++ "\n)"
where str = showDecls ',' "\n\t" decls
data PackageItem data PackageItem
= Function Lifetime Type Identifier [Decl] [Stmt] = Function Lifetime Type Identifier [Decl] [Stmt]
| Task Lifetime Identifier [Decl] [Stmt] | Task Lifetime Identifier [Decl] [Stmt]
......
...@@ -27,7 +27,7 @@ import Text.Printf (printf) ...@@ -27,7 +27,7 @@ import Text.Printf (printf)
import Language.SystemVerilog.AST.ShowHelp (commas, indent, unlines', showPad, showBlock) import Language.SystemVerilog.AST.ShowHelp (commas, indent, unlines', showPad, showBlock)
import Language.SystemVerilog.AST.Attr (Attr) import Language.SystemVerilog.AST.Attr (Attr)
import Language.SystemVerilog.AST.Decl (Decl) import Language.SystemVerilog.AST.Decl (Decl, showDecls)
import Language.SystemVerilog.AST.Expr (Expr(Nil), Args(..)) import Language.SystemVerilog.AST.Expr (Expr(Nil), Args(..))
import Language.SystemVerilog.AST.LHS (LHS) import Language.SystemVerilog.AST.LHS (LHS)
import Language.SystemVerilog.AST.Op (AsgnOp(AsgnOpEq)) import Language.SystemVerilog.AST.Op (AsgnOp(AsgnOpEq))
...@@ -78,7 +78,7 @@ instance Show Stmt where ...@@ -78,7 +78,7 @@ instance Show Stmt where
(indent $ show stmt) (indent $ show stmt)
where where
showInits :: Either [Decl] [(LHS, Expr)] -> String showInits :: Either [Decl] [(LHS, Expr)] -> String
showInits (Left decls) = commas $ map (init . show) decls showInits (Left decls) = showDecls ',' " " decls
showInits (Right asgns) = commas $ map showInit asgns showInits (Right asgns) = commas $ map showInit asgns
where showInit (l, e) = showAssign (l, AsgnOpEq, e) where showInit (l, e) = showAssign (l, AsgnOpEq, e)
showAssign :: (LHS, AsgnOp, Expr) -> String showAssign :: (LHS, AsgnOp, Expr) -> String
......
...@@ -662,6 +662,10 @@ PortDeclTokens(delim) :: { [DeclToken] } ...@@ -662,6 +662,10 @@ PortDeclTokens(delim) :: { [DeclToken] }
| 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 } | AttributeInstance PortDeclTokens(delim) {% posInject \p -> DTAttr p $1 : $2 }
ModuleDeclTokens(delim) :: { [DeclToken] }
: DeclTokensBase(ModuleDeclTokens(delim), delim) { $1 }
| GenericInterfaceDecl ModuleDeclTokens(delim) { $1 ++ $2}
| GenericInterfaceDecl delim { $1 }
GenericInterfaceDecl :: { [DeclToken] } GenericInterfaceDecl :: { [DeclToken] }
: "interface" Identifier {% posInject \p -> [DTType p (\Unspecified -> InterfaceT "" ""), DTIdent p $2] } : "interface" Identifier {% posInject \p -> [DTType p (\Unspecified -> InterfaceT "" ""), DTIdent p $2] }
...@@ -688,7 +692,7 @@ ModuleItem :: { [ModuleItem] } ...@@ -688,7 +692,7 @@ ModuleItem :: { [ModuleItem] }
| "generate" GenItems endgenerate { [Generate $2] } | "generate" GenItems endgenerate { [Generate $2] }
NonGenerateModuleItem :: { [ModuleItem] } NonGenerateModuleItem :: { [ModuleItem] }
-- This item covers module instantiations and all declarations -- This item covers module instantiations and all declarations
: DeclTokens(";") { parseDTsAsModuleItems $1 } : ModuleDeclTokens(";") { parseDTsAsModuleItems $1 }
| ParameterDecl(";") { map (MIPackageItem . Decl) $1 } | ParameterDecl(";") { map (MIPackageItem . Decl) $1 }
| "defparam" LHSAsgns ";" { map (uncurry Defparam) $2 } | "defparam" LHSAsgns ";" { map (uncurry Defparam) $2 }
| "assign" AssignOption LHSAsgns ";" { map (uncurry $ Assign $2) $3 } | "assign" AssignOption LHSAsgns ";" { map (uncurry $ Assign $2) $3 }
......
...@@ -45,14 +45,26 @@ simulate() { ...@@ -45,14 +45,26 @@ simulate() {
assertConverts() { assertConverts() {
ac_file=$1 ac_file=$1
ac_tmpa=$SHUNIT_TMPDIR/ac-conv-tmpa.v ac_tmpa=$SHUNIT_TMPDIR/ac-conv-tmpa.v
$SV2V $ac_file 2> /dev/null > $ac_tmpa $SV2V $ac_file 2> /dev/null > $ac_tmpa
assertTrue "1st conversion of $ac_file failed" $? assertTrue "1st conversion of $ac_file failed" $?
ac_tmpb=$SHUNIT_TMPDIR/ac-conv-tmpb.v ac_tmpb=$SHUNIT_TMPDIR/ac-conv-tmpb.v
$SV2V $ac_tmpa 2> /dev/null > $ac_tmpb $SV2V $ac_tmpa 2> /dev/null > $ac_tmpb
assertTrue "2nd conversion of $ac_file failed" $? assertTrue "2nd conversion of $ac_file failed" $?
diff $ac_tmpa $ac_tmpb > /dev/null diff $ac_tmpa $ac_tmpb > /dev/null
assertTrue "conversion of $ac_file not stable after the first iteration" $? assertTrue "conversion of $ac_file not stable after the first iteration" $?
ac_tmpc=$SHUNIT_TMPDIR/ac-conv-tmpc.v
$SV2V --pass-through $ac_file 2> /dev/null > $ac_tmpc
assertTrue "pass through of $ac_file failed" $?
ac_tmpd=$SHUNIT_TMPDIR/ac-conv-tmpd.v
$SV2V $ac_tmpc 2> /dev/null > $ac_tmpd
assertTrue "conversion of pass through of $ac_file failed" $?
diff $ac_tmpa $ac_tmpd > /dev/null
assertTrue "pass through then conversion differs for $ac_file" $?
# using sed to remove quoted strings # using sed to remove quoted strings
filtered=`sed -E 's/"([^"]|\")+"//g' $ac_tmpa` filtered=`sed -E 's/"([^"]|\")+"//g' $ac_tmpa`
# check for various things iverilog accepts which we don't want to output # check for various things iverilog accepts which we don't want to output
......
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