add option to skip preprocessing

This commit is contained in:
Zachary Snow 2020-03-18 20:02:20 -04:00
parent f5881919c1
commit f44e3e808a
5 changed files with 38 additions and 16 deletions

View File

@ -77,8 +77,9 @@ Preprocessing:
-D --define=NAME[=VALUE] Define a macro for preprocessing
--siloed Lex input files separately, so macros from
earlier files are not defined in later files
--skip-preprocessor Disable preprocessor
Conversion:
-E --exclude=CONV Exclude a particular conversion (always,
-E --exclude=CONV Exclude a particular conversion (always, assert,
interface, or logic)
-v --verbose Retain certain conversion artifacts
Other:

View File

@ -26,6 +26,7 @@ data Job = Job
, incdir :: [FilePath]
, define :: [String]
, siloed :: Bool
, skipPreprocessor :: Bool
, exclude :: [Exclude]
, verbose :: Bool
} deriving (Show, Typeable, Data)
@ -45,8 +46,10 @@ defaultJob = Job
&= help "Define a macro for preprocessing"
, siloed = nam_ "siloed" &= help ("Lex input files separately, so"
++ " macros from earlier files are not defined in later files")
, skipPreprocessor = nam_ "skip-preprocessor" &= help "Disable preprocessor"
, exclude = nam_ "exclude" &= name "E" &= typ "CONV"
&= help "Exclude a particular conversion (always, assert, interface, or logic)"
&= help ("Exclude a particular conversion (always, assert, interface,"
++ " or logic)")
&= groupname "Conversion"
, verbose = nam "verbose" &= help "Retain certain conversion artifacts"
}

View File

@ -11,29 +11,30 @@ import qualified Data.Map.Strict as Map
import Language.SystemVerilog.AST (AST)
import Language.SystemVerilog.Parser.Lex (lexStr)
import Language.SystemVerilog.Parser.Parse (parse)
import Language.SystemVerilog.Parser.Preprocess (preprocess, Env)
import Language.SystemVerilog.Parser.Preprocess (preprocess, annotate, Env)
import Language.SystemVerilog.Parser.Tokens (Position(..), tokenPosition)
-- parses a compilation unit given include search paths and predefined macros
parseFiles :: [FilePath] -> [(String, String)] -> Bool -> [FilePath] -> IO (Either String [AST])
parseFiles includePaths defines siloed paths = do
parseFiles :: [FilePath] -> [(String, String)] -> Bool -> Bool -> [FilePath] -> IO (Either String [AST])
parseFiles includePaths defines siloed skipPreprocessor paths = do
let env = Map.map (\a -> (a, [])) $ Map.fromList defines
runExceptT (parseFiles' includePaths env siloed paths)
runExceptT (parseFiles' includePaths env siloed skipPreprocessor paths)
-- parses a compilation unit given include search paths and predefined macros
parseFiles' :: [FilePath] -> Env -> Bool -> [FilePath] -> ExceptT String IO [AST]
parseFiles' _ _ _ [] = return []
parseFiles' includePaths env siloed (path : paths) = do
(ast, envEnd) <- parseFile' includePaths env path
parseFiles' :: [FilePath] -> Env -> Bool -> Bool -> [FilePath] -> ExceptT String IO [AST]
parseFiles' _ _ _ _ [] = return []
parseFiles' includePaths env siloed skipPreprocessor (path : paths) = do
(ast, envEnd) <- parseFile' includePaths env skipPreprocessor path
let envNext = if siloed then env else envEnd
asts <- parseFiles' includePaths envNext siloed paths
asts <- parseFiles' includePaths envNext siloed skipPreprocessor paths
return $ ast : asts
-- parses a file given include search paths, a table of predefined macros, and
-- the file path
parseFile' :: [String] -> Env -> FilePath -> ExceptT String IO (AST, Env)
parseFile' includePaths env path = do
preResult <- liftIO $ preprocess includePaths env path
parseFile' :: [String] -> Env -> Bool -> FilePath -> ExceptT String IO (AST, Env)
parseFile' includePaths env skipPreprocessor path = do
let runner = if skipPreprocessor then annotate else preprocess
preResult <- liftIO $ runner includePaths env path
(contents, env') <- liftEither preResult
result <- liftIO $ uncurry lexStr $ unzip contents
tokens <- liftEither result

View File

@ -9,6 +9,7 @@
-}
module Language.SystemVerilog.Parser.Preprocess
( preprocess
, annotate
, Env
) where
@ -66,6 +67,21 @@ preprocess includePaths env path = do
output = reverse $ ppOutput finalState
env' = ppEnv finalState
-- position annotator entrypoint used for files that don't need any
-- preprocessing
annotate :: [String] -> Env -> FilePath -> IO (Either String ([(Char, Position)], Env))
annotate _ env path = do
contents <-
if path == "-"
then getContents
else loadFile path
let positions = scanl advance (Position path 1 1) contents
return $ Right (zip contents positions, env)
where
advance :: Position -> Char -> Position
advance (Position f l _) '\n' = Position f (l + 1) 1
advance (Position f l c) _ = Position f l (c + 1)
-- read in the given file
loadFile :: FilePath -> IO String
loadFile path = do

View File

@ -8,7 +8,7 @@ import System.IO
import System.Exit
import Data.List (elemIndex)
import Job (readJob, files, exclude, incdir, define, siloed)
import Job (readJob, files, exclude, incdir, define, siloed, skipPreprocessor)
import Convert (convert)
import Language.SystemVerilog.Parser (parseFiles)
@ -23,7 +23,8 @@ main = do
job <- readJob
-- parse the input files
let defines = map splitDefine $ define job
result <- parseFiles (incdir job) defines (siloed job) (files job)
result <- parseFiles (incdir job) defines (siloed job)
(skipPreprocessor job) (files job)
case result of
Left msg -> do
hPutStr stderr $ msg ++ "\n"