Commit 5c263298 by Zachary Snow

simplify substitution avoids shadowing

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