Commit 5c263298 by Zachary Snow

simplify substitution avoids shadowing

parent 490d96ba
...@@ -92,10 +92,13 @@ traverseStmtM stmt = do ...@@ -92,10 +92,13 @@ traverseStmtM stmt = do
return stmt' return stmt'
traverseExprM :: Expr -> ST Expr traverseExprM :: Expr -> ST Expr
traverseExprM (Cast (Left (IntegerVector _ sg rs)) e) = do traverseExprM (Cast (Left (IntegerVector _ sg rs)) value) = do
e' <- traverseExprM e value' <- traverseExprM value
convertCastM (dimensionsSize rs) e' signed size' <- traverseExprM size
where signed = sg == Signed convertCastM size' value' signed
where
signed = sg == Signed
size = dimensionsSize rs
traverseExprM other = traverseExprM other =
traverseSinglyNestedExprsM traverseExprM other traverseSinglyNestedExprsM traverseExprM other
......
...@@ -52,6 +52,8 @@ simplifyStep (Mux (Number n) e1 e2) = ...@@ -52,6 +52,8 @@ simplifyStep (Mux (Number n) e1 e2) =
Just _ -> e1 Just _ -> e1
Nothing -> Mux (Number n) e1 e2 Nothing -> Mux (Number n) e1 e2
simplifyStep (Call (Ident "$clog2") (Args [SizDec k] [])) =
simplifyStep $ Call (Ident "$clog2") (Args [RawNum k] [])
simplifyStep (Call (Ident "$clog2") (Args [Dec k] [])) = simplifyStep (Call (Ident "$clog2") (Args [Dec k] [])) =
toDec $ clog2 k toDec $ clog2 k
where where
......
...@@ -32,22 +32,29 @@ convertDescription = ...@@ -32,22 +32,29 @@ convertDescription =
traverseDeclM :: Decl -> Scoper Expr Decl traverseDeclM :: Decl -> Scoper Expr Decl
traverseDeclM decl = do traverseDeclM decl = do
case decl of decl' <- traverseDeclExprsM traverseExprM decl
Param Localparam _ x e -> case decl' of
when (isSimpleExpr e) $ insertElem x e Param Localparam UnknownType x e ->
insertExpr x e
Param Localparam (Implicit Signed [(RawNum 31, RawNum 0)]) x e ->
insertExpr x e
Param Localparam (Implicit sg rs) x e ->
insertExpr x $ Cast (Left t) e
where t = IntegerVector TLogic sg rs
Param Localparam t x e ->
insertExpr x $ Cast (Left t) e
_ -> return () _ -> return ()
let mi = MIPackageItem $ Decl decl
mi' <- traverseModuleItemM mi
let MIPackageItem (Decl decl') = mi'
return decl' return decl'
insertExpr :: Identifier -> Expr -> Scoper Expr ()
insertExpr ident expr = do
expr' <- substituteExprM expr
when (isSimpleExpr expr') $ insertElem ident expr'
isSimpleExpr :: Expr -> Bool isSimpleExpr :: Expr -> Bool
isSimpleExpr Ident{} = True
isSimpleExpr Number{} = True isSimpleExpr Number{} = True
isSimpleExpr String{} = True isSimpleExpr String{} = True
isSimpleExpr (Dot e _ ) = isSimpleExpr e isSimpleExpr (Cast Left{} e) = isSimpleExpr e
isSimpleExpr (Bit e _ ) = isSimpleExpr e
isSimpleExpr (Range e _ _) = isSimpleExpr e
isSimpleExpr _ = False isSimpleExpr _ = False
traverseModuleItemM :: ModuleItem -> Scoper Expr ModuleItem traverseModuleItemM :: ModuleItem -> Scoper Expr ModuleItem
...@@ -117,11 +124,12 @@ convertExpr info expr = ...@@ -117,11 +124,12 @@ convertExpr info expr =
substitute :: Scopes Expr -> Expr -> Expr substitute :: Scopes Expr -> Expr -> Expr
substitute scopes expr = substitute scopes expr =
traverseNestedExprs substitute' expr substitute' expr
where where
substitute' :: Expr -> Expr substitute' :: Expr -> Expr
substitute' (Ident x) = substitute' (Ident x) =
case lookupElem scopes x of case lookupElem scopes x of
Nothing -> Ident x Nothing -> Ident x
Just (_, _, e) -> e Just (_, _, e) -> e
substitute' other = other substitute' other =
traverseSinglyNestedExprs substitute' other
...@@ -115,7 +115,8 @@ traverseExprM (Cast (Right (Ident x)) expr) = do ...@@ -115,7 +115,8 @@ traverseExprM (Cast (Right (Ident x)) expr) = do
else elaborateSizeCast (Ident x) expr' else elaborateSizeCast (Ident x) expr'
traverseExprM (Cast (Right size) expr) = do traverseExprM (Cast (Right size) expr) = do
expr' <- traverseExprM expr expr' <- traverseExprM expr
elaborateSizeCast size expr' size' <- traverseExprM size
elaborateSizeCast size' expr'
traverseExprM other = traverseExprM other =
traverseExprTypesM traverseTypeM other traverseExprTypesM traverseTypeM other
>>= traverseSinglyNestedExprsM traverseExprM >>= traverseSinglyNestedExprsM traverseExprM
......
module top;
localparam A = 1;
localparam B = A;
if (A) begin : blk
localparam A = 1'sbx;
initial $display("%b", $clog2(B + 1 + 1));
end
endmodule
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