Commit 19711ba1 by Zachary Snow

minor scoper performance tweaks

parent 642803a7
...@@ -117,46 +117,32 @@ enterScope :: Monad m => Identifier -> Identifier -> ScoperT a m () ...@@ -117,46 +117,32 @@ enterScope :: Monad m => Identifier -> Identifier -> ScoperT a m ()
enterScope name index = do enterScope name index = do
s <- get s <- get
let current' = sCurrent s ++ [Tier name index] let current' = sCurrent s ++ [Tier name index]
existingResult <- lookupElemM name let existingResult = lookupLocalIdent s name
let existingElement = fmap thd3 existingResult let existingElement = fmap thd3 existingResult
let entry = Entry existingElement index Map.empty let entry = Entry existingElement index Map.empty
let mapping' = setScope current' entry $ sMapping s let mapping' = setScope current' entry $ sMapping s
put $ s { sCurrent = current', sMapping = mapping'} put $ s { sCurrent = current', sMapping = mapping'}
where thd3 (_, _, c) = c where thd3 (_, _, c) = c
exitScope :: Monad m => Identifier -> Identifier -> ScoperT a m () exitScope :: Monad m => ScoperT a m ()
exitScope name index = do exitScope = modify' $ \s -> s { sCurrent = init $ sCurrent s }
let tier = Tier name index
s <- get
let current = sCurrent s
if null current || last current /= tier
then error "exitScope invariant violated"
else put $ s { sCurrent = init current}
enterProcedure :: Monad m => ScoperT a m () enterProcedure :: Monad m => ScoperT a m ()
enterProcedure = do enterProcedure = modify' $ \s -> s { sProcedure = True }
s <- get
if sProcedure s
then error "enterProcedure invariant failed"
else put $ s { sProcedure = True }
exitProcedure :: Monad m => ScoperT a m () exitProcedure :: Monad m => ScoperT a m ()
exitProcedure = do exitProcedure = modify' $ \s -> s { sProcedure = False }
s <- get
if not (sProcedure s) exprToAccesses :: [Access] -> Expr -> Maybe [Access]
then error "exitProcedure invariant failed" exprToAccesses accesses (Ident x) =
else put $ s { sProcedure = False } Just $ Access x Nil : accesses
exprToAccesses accesses (Bit (Ident x) y) =
exprToAccesses :: Expr -> Maybe [Access] Just $ Access x y : accesses
exprToAccesses (Ident x) = Just [Access x Nil] exprToAccesses accesses (Bit (Dot e x) y) =
exprToAccesses (Bit (Ident x) y) = Just [Access x y] exprToAccesses (Access x y : accesses) e
exprToAccesses (Bit (Dot e x) y) = do exprToAccesses accesses (Dot e x) =
accesses <- exprToAccesses e exprToAccesses (Access x Nil : accesses) e
Just $ accesses ++ [Access x y] exprToAccesses _ _ = Nothing
exprToAccesses (Dot e x) = do
accesses <- exprToAccesses e
Just $ accesses ++ [Access x Nil]
exprToAccesses _ = Nothing
accessesToExpr :: [Access] -> Expr accessesToExpr :: [Access] -> Expr
accessesToExpr accesses = accessesToExpr accesses =
...@@ -277,7 +263,7 @@ class ScopeKey k where ...@@ -277,7 +263,7 @@ class ScopeKey k where
lookupElemM = embedScopes lookupElem lookupElemM = embedScopes lookupElem
instance ScopeKey Expr where instance ScopeKey Expr where
lookupElem scopes = join . fmap (lookupAccesses scopes) . exprToAccesses lookupElem scopes = join . fmap (lookupAccesses scopes) . exprToAccesses []
instance ScopeKey LHS where instance ScopeKey LHS where
lookupElem scopes = lookupElem scopes . lhsToExpr lookupElem scopes = lookupElem scopes . lhsToExpr
...@@ -364,9 +350,7 @@ runScoperT declMapper moduleItemMapper genItemMapper stmtMapper topName items = ...@@ -364,9 +350,7 @@ runScoperT declMapper moduleItemMapper genItemMapper stmtMapper topName items =
operation :: ScoperT a m [ModuleItem] operation :: ScoperT a m [ModuleItem]
operation = do operation = do
enterScope topName "" enterScope topName ""
items' <- mapM wrappedModuleItemMapper items mapM wrappedModuleItemMapper items
exitScope topName ""
return items'
initialState = Scopes [] Map.empty False [] [] initialState = Scopes [] Map.empty False [] []
wrappedModuleItemMapper = scopeModuleItemT wrappedModuleItemMapper = scopeModuleItemT
...@@ -388,7 +372,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper = ...@@ -388,7 +372,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper =
enterScope name "" enterScope name ""
decls' <- fmap concat $ mapM declMapper' decls decls' <- fmap concat $ mapM declMapper' decls
stmts' <- mapM fullStmtMapper stmts stmts' <- mapM fullStmtMapper stmts
exitScope name "" exitScope
return $ Block kw name decls' stmts' return $ Block kw name decls' stmts'
-- TODO: Do we need to support the various procedural loops? -- TODO: Do we need to support the various procedural loops?
fullStmtMapper stmt = do fullStmtMapper stmt = do
...@@ -438,10 +422,8 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper = ...@@ -438,10 +422,8 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper =
redirectTFDecl :: Type -> Identifier -> ScoperT a m (Type, Identifier) redirectTFDecl :: Type -> Identifier -> ScoperT a m (Type, Identifier)
redirectTFDecl typ ident = do redirectTFDecl typ ident = do
res <- declMapper $ Variable Local typ ident [] Nil res <- declMapper $ Variable Local typ ident [] Nil
case res of let Variable Local newType newName [] Nil = res
Variable Local newType newName [] Nil ->
return (newType, newName) return (newType, newName)
_ -> error $ "redirected func ret traverse failed: " ++ show res
wrappedModuleItemMapper :: ModuleItem -> ScoperT a m ModuleItem wrappedModuleItemMapper :: ModuleItem -> ScoperT a m ModuleItem
wrappedModuleItemMapper item = do wrappedModuleItemMapper item = do
...@@ -459,7 +441,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper = ...@@ -459,7 +441,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper =
enterScope x' "" enterScope x' ""
decls' <- mapTFDecls decls decls' <- mapTFDecls decls
stmts' <- mapM fullStmtMapper stmts stmts' <- mapM fullStmtMapper stmts
exitScope x' "" exitScope
exitProcedure exitProcedure
return $ MIPackageItem $ Function ml t' x' decls' stmts' return $ MIPackageItem $ Function ml t' x' decls' stmts'
fullModuleItemMapper (MIPackageItem (Task ml x decls stmts)) = do fullModuleItemMapper (MIPackageItem (Task ml x decls stmts)) = do
...@@ -468,7 +450,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper = ...@@ -468,7 +450,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper =
enterScope x' "" enterScope x' ""
decls' <- mapTFDecls decls decls' <- mapTFDecls decls
stmts' <- mapM fullStmtMapper stmts stmts' <- mapM fullStmtMapper stmts
exitScope x' "" exitScope
exitProcedure exitProcedure
return $ MIPackageItem $ Task ml x' decls' stmts' return $ MIPackageItem $ Task ml x' decls' stmts'
fullModuleItemMapper (MIPackageItem (Decl decl)) = fullModuleItemMapper (MIPackageItem (Decl decl)) =
...@@ -518,7 +500,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper = ...@@ -518,7 +500,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper =
scopeGenItemMapper (GenBlock name genItems) = do scopeGenItemMapper (GenBlock name genItems) = do
enterScope name "" enterScope name ""
genItems' <- mapM fullGenItemMapper genItems genItems' <- mapM fullGenItemMapper genItems
exitScope name "" exitScope
return $ GenBlock name genItems' return $ GenBlock name genItems'
scopeGenItemMapper (GenModuleItem moduleItem) = scopeGenItemMapper (GenModuleItem moduleItem) =
wrappedModuleItemMapper moduleItem >>= return . GenModuleItem wrappedModuleItemMapper moduleItem >>= return . GenModuleItem
...@@ -529,12 +511,12 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper = ...@@ -529,12 +511,12 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper =
scopeGenItemBranchMapper index (GenBlock name genItems) = do scopeGenItemBranchMapper index (GenBlock name genItems) = do
enterScope name index enterScope name index
genItems' <- mapM fullGenItemMapper genItems genItems' <- mapM fullGenItemMapper genItems
exitScope name index exitScope
return $ GenBlock name genItems' return $ GenBlock name genItems'
scopeGenItemBranchMapper index genItem = do scopeGenItemBranchMapper index genItem = do
enterScope "" index enterScope "" index
genItem' <- fullGenItemMapper genItem genItem' <- fullGenItemMapper genItem
exitScope "" index exitScope
return genItem' return genItem'
partScoper partScoper
......
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