From 407ba59042edf779bc50dfe886f20af8917508a3 Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Sun, 5 Sep 2021 19:54:36 -0400 Subject: [PATCH] add internal --dump-prefix utility --- README.md | 2 ++ src/Convert.hs | 47 ++++++++++++++++++++++++++++++++++++++--------- src/Job.hs | 4 ++++ src/sv2v.hs | 6 +++--- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 88038bd..826b2af 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,8 @@ Conversion: Other: --oversized-numbers Disable standard-imposed 32-bit limit on unsized number literals (e.g., 'h1_ffff_ffff, 4294967296) + --dump-prefix=PATH Create intermediate output files with the given + path prefix; used for internal debugging --help Display help message --version Print version information --numeric-version Print just the version number diff --git a/src/Convert.hs b/src/Convert.hs index 73ad9ac..62e236e 100644 --- a/src/Convert.hs +++ b/src/Convert.hs @@ -6,6 +6,8 @@ module Convert (convert) where +import Control.Monad ((>=>)) + import Language.SystemVerilog.AST import qualified Job (Exclude(..)) @@ -53,6 +55,7 @@ import qualified Convert.Unsigned import qualified Convert.Wildcard type Phase = [AST] -> [AST] +type IOPhase = [AST] -> IO [AST] type Selector = Job.Exclude -> Phase -> Phase finalPhases :: Selector -> [Phase] @@ -109,22 +112,48 @@ initialPhases selectExclude = , Convert.UnnamedGenBlock.convert ] -convert :: [Job.Exclude] -> Phase -convert excludes = - final . loopMain . initial +convert :: FilePath -> [Job.Exclude] -> IOPhase +convert dumpPrefix excludes = + step "parse" id >=> + step "initial" initial >=> + loop 1 "main" main >=> + step "final" final where final = combine $ finalPhases selectExclude main = combine $ mainPhases selectExclude initial = combine $ initialPhases selectExclude combine = foldr1 (.) - loopMain :: Phase - loopMain descriptions = - if descriptions == descriptions' - then descriptions - else loopMain descriptions' - where descriptions' = main descriptions + selectExclude :: Selector selectExclude exclude phase = if elem exclude excludes then id else phase + + dumper :: String -> IOPhase + dumper = + if null dumpPrefix + then const return + else fileDumper dumpPrefix + + -- add debug dumping to a phase + step :: String -> Phase -> IOPhase + step key = (dumper key .) + + -- add convergence and debug dumping to a phase + loop :: Int -> String -> Phase -> IOPhase + loop idx key phase files = + if files == files' + then return files + else dumper key' files' >>= loop (idx + 1) key phase + where + files' = phase files + key' = key ++ "_" ++ show idx + +-- pass through dumper which writes ASTs to a file +fileDumper :: String -> String -> IOPhase +fileDumper prefix key files = do + let path = prefix ++ key ++ ".sv" + let output = show $ concat files + writeFile path output + return files diff --git a/src/Job.hs b/src/Job.hs index 50e05c0..c23be4e 100644 --- a/src/Job.hs +++ b/src/Job.hs @@ -44,6 +44,7 @@ data Job = Job , write :: Write , writeRaw :: String , oversizedNumbers :: Bool + , dumpPrefix :: FilePath } deriving (Typeable, Data) version :: String @@ -76,6 +77,9 @@ defaultJob = Job &= help ("Disable standard-imposed 32-bit limit on unsized number" ++ " literals (e.g., 'h1_ffff_ffff, 4294967296)") &= groupname "Other" + , dumpPrefix = def &= name "dump-prefix" &= explicit &= typ "PATH" + &= help ("Create intermediate output files with the given path prefix;" + ++ " used for internal debugging") } &= program "sv2v" &= summary ("sv2v " ++ version) diff --git a/src/sv2v.hs b/src/sv2v.hs index 427e992..1fb48d8 100644 --- a/src/sv2v.hs +++ b/src/sv2v.hs @@ -89,9 +89,9 @@ main = do exitFailure Right asts -> do -- convert the files if requested - let asts' = if passThrough job - then asts - else convert (exclude job) asts + asts' <- if passThrough job + then return asts + else convert (dumpPrefix job) (exclude job) asts emptyWarnings (concat asts) (concat asts') -- write the converted files out writeOutput (write job) (files job) asts'