Commit 46be0edb by Zachary Snow

simplify cast scoper lookup and removal

parent 1311e449
...@@ -30,6 +30,7 @@ module Convert.Cast (convert) where ...@@ -30,6 +30,7 @@ module Convert.Cast (convert) where
import Control.Monad.Writer.Strict import Control.Monad.Writer.Strict
import Data.List (isPrefixOf) import Data.List (isPrefixOf)
import Data.Maybe (isJust)
import Convert.ExprUtils import Convert.ExprUtils
import Convert.Scoper import Convert.Scoper
...@@ -46,9 +47,9 @@ convertDescription description = ...@@ -46,9 +47,9 @@ convertDescription description =
traverseDeclM traverseModuleItemM traverseGenItemM traverseStmtM traverseDeclM traverseModuleItemM traverseGenItemM traverseStmtM
description description
type ST = Scoper Expr type SC = Scoper ()
traverseDeclM :: Decl -> ST Decl traverseDeclM :: Decl -> SC Decl
traverseDeclM decl = do traverseDeclM decl = do
decl' <- case decl of decl' <- case decl of
Variable d t x a e -> do Variable d t x a e -> do
...@@ -59,16 +60,16 @@ traverseDeclM decl = do ...@@ -59,16 +60,16 @@ traverseDeclM decl = do
if isPrefixOf "sv2v_cast_" x && details /= Nothing if isPrefixOf "sv2v_cast_" x && details /= Nothing
then return $ Variable Local t DuplicateTag [] Nil then return $ Variable Local t DuplicateTag [] Nil
else do else do
insertElem x Nil insertElem x ()
return $ Variable d t x a e' return $ Variable d t x a e'
Net d n s t x a e -> do Net d n s t x a e -> do
enterStmt enterStmt
e' <- traverseExprM e e' <- traverseExprM e
exitStmt exitStmt
insertElem x Nil insertElem x ()
return $ Net d n s t x a e' return $ Net d n s t x a e'
Param _ _ x _ -> Param _ _ x _ ->
insertElem x Nil >> return decl insertElem x () >> return decl
ParamType _ _ _ -> return decl ParamType _ _ _ -> return decl
CommentDecl _ -> return decl CommentDecl _ -> return decl
traverseDeclExprsM traverseExprM decl' traverseDeclExprsM traverseExprM decl'
...@@ -81,23 +82,23 @@ dropDuplicateCaster (MIPackageItem (Function _ _ DuplicateTag _ _)) = ...@@ -81,23 +82,23 @@ dropDuplicateCaster (MIPackageItem (Function _ _ DuplicateTag _ _)) =
Generate [] Generate []
dropDuplicateCaster other = other dropDuplicateCaster other = other
traverseModuleItemM :: ModuleItem -> ST ModuleItem traverseModuleItemM :: ModuleItem -> SC ModuleItem
traverseModuleItemM (Genvar x) = traverseModuleItemM (Genvar x) =
insertElem x Nil >> return (Genvar x) insertElem x () >> return (Genvar x)
traverseModuleItemM item = traverseModuleItemM item =
traverseExprsM traverseExprM item traverseExprsM traverseExprM item
traverseGenItemM :: GenItem -> ST GenItem traverseGenItemM :: GenItem -> SC GenItem
traverseGenItemM = traverseGenItemExprsM traverseExprM traverseGenItemM = traverseGenItemExprsM traverseExprM
traverseStmtM :: Stmt -> ST Stmt traverseStmtM :: Stmt -> SC Stmt
traverseStmtM stmt = do traverseStmtM stmt = do
enterStmt enterStmt
stmt' <- traverseStmtExprsM traverseExprM stmt stmt' <- traverseStmtExprsM traverseExprM stmt
exitStmt exitStmt
return stmt' return stmt'
traverseExprM :: Expr -> ST Expr traverseExprM :: Expr -> SC Expr
traverseExprM (Cast (Left (IntegerVector _ sg rs)) value) = do traverseExprM (Cast (Left (IntegerVector _ sg rs)) value) = do
value' <- traverseExprM value value' <- traverseExprM value
size' <- traverseExprM size size' <- traverseExprM size
...@@ -108,7 +109,7 @@ traverseExprM (Cast (Left (IntegerVector _ sg rs)) value) = do ...@@ -108,7 +109,7 @@ traverseExprM (Cast (Left (IntegerVector _ sg rs)) value) = do
traverseExprM other = traverseExprM other =
traverseSinglyNestedExprsM traverseExprM other traverseSinglyNestedExprsM traverseExprM other
convertCastM :: Expr -> Expr -> Bool -> ST Expr convertCastM :: Expr -> Expr -> Bool -> SC Expr
convertCastM (Number size) (Number value) signed = convertCastM (Number size) (Number value) signed =
return $ Number $ return $ Number $
case numberToInteger size of case numberToInteger size of
...@@ -132,7 +133,7 @@ convertCastM size value signed = do ...@@ -132,7 +133,7 @@ convertCastM size value signed = do
return $ Call (Ident name) (Args [value'] []) return $ Call (Ident name) (Args [value'] [])
else do else do
name <- castDeclName 0 name <- castDeclName 0
insertElem name Nil insertElem name ()
useVar <- withinStmt useVar <- withinStmt
injectDecl $ castDecl useVar name value' size signed injectDecl $ castDecl useVar name value' size signed
return $ Ident name return $ Ident name
...@@ -191,7 +192,7 @@ castDecl useVar name value size signed = ...@@ -191,7 +192,7 @@ castDecl useVar name value size signed =
else Param Localparam t name value else Param Localparam t name value
where t = castType size signed where t = castType size signed
castDeclName :: Int -> ST String castDeclName :: Int -> SC String
castDeclName counter = do castDeclName counter = do
details <- lookupElemM name details <- lookupElemM name
if details == Nothing if details == Nothing
...@@ -204,19 +205,15 @@ castDeclName counter = do ...@@ -204,19 +205,15 @@ castDeclName counter = do
prefix = "sv2v_tmp_cast" prefix = "sv2v_tmp_cast"
-- track whether procedural casts should use variables -- track whether procedural casts should use variables
pattern WithinStmt :: Identifier withinStmtKey :: Identifier
pattern WithinStmt = ":within_stmt:" withinStmtKey = ":within_stmt:"
withinStmt :: ST Bool withinStmt :: SC Bool
withinStmt = do withinStmt = fmap isJust $ lookupElemM withinStmtKey
details <- lookupElemM WithinStmt enterStmt :: SC ()
return $ case details of
Just (_, _, t) -> t /= Nil
Nothing -> False
enterStmt :: ST ()
enterStmt = do enterStmt = do
inProcedure <- withinProcedureM inProcedure <- withinProcedureM
when inProcedure $ insertElem WithinStmt (RawNum 1) when inProcedure $ insertElem withinStmtKey ()
exitStmt :: ST () exitStmt :: SC ()
exitStmt = do exitStmt = do
inProcedure <- withinProcedureM inProcedure <- withinProcedureM
when inProcedure $ insertElem WithinStmt Nil when inProcedure $ removeElem withinStmtKey
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