use unbounded integers

This commit is contained in:
Zachary Snow 2020-07-02 23:33:03 -06:00
parent cd8af036a0
commit 1903bc190d
4 changed files with 31 additions and 24 deletions

View File

@ -66,9 +66,7 @@ traverseExprM =
where where
str = (show size) ++ "'d" ++ (show num) str = (show size) ++ "'d" ++ (show num)
size = s' size = s'
num = if size >= 32 num = n' `mod` (2 ^ s')
then n' -- already read as 32 bits
else n' `mod` (2 ^ s')
_ -> convertCastM (Number s) (Number n) _ -> convertCastM (Number s) (Number n)
convertExprM (orig @ (Cast (Right DimsFn{}) _)) = convertExprM (orig @ (Cast (Right DimsFn{}) _)) =
return orig return orig

View File

@ -12,7 +12,6 @@
module Convert.TypeOf (convert) where module Convert.TypeOf (convert) where
import Data.List (elemIndex) import Data.List (elemIndex)
import Data.Int (Int32)
import Data.Tuple (swap) import Data.Tuple (swap)
import qualified Data.Map.Strict as Map import qualified Data.Map.Strict as Map
@ -140,7 +139,7 @@ typeof (Repeat reps exprs) = return $ typeOfSize size
typeof other = lookupTypeOf other typeof other = lookupTypeOf other
-- determines the size and sign of a number literal -- determines the size and sign of a number literal
parseNumber :: String -> (Int32, Signing) parseNumber :: String -> (Integer, Signing)
parseNumber s = parseNumber s =
case elemIndex '\'' s of case elemIndex '\'' s of
Nothing -> (32, Signed) Nothing -> (32, Signed)

View File

@ -27,9 +27,7 @@ module Language.SystemVerilog.AST.Expr
) where ) where
import Data.Bits (shiftL, shiftR) import Data.Bits (shiftL, shiftR)
import Data.Int (Int32)
import Data.List (intercalate) import Data.List (intercalate)
import Data.Word (Word32)
import Numeric (readHex) import Numeric (readHex)
import Text.Printf (printf) import Text.Printf (printf)
import Text.Read (readMaybe) import Text.Read (readMaybe)
@ -191,12 +189,12 @@ showExprOrRange :: ExprOrRange -> String
showExprOrRange (Left x) = show x showExprOrRange (Left x) = show x
showExprOrRange (Right x) = show x showExprOrRange (Right x) = show x
clog2Help :: Int32 -> Int32 -> Int32 clog2Help :: Integer -> Integer -> Integer
clog2Help p n = if p >= n then 0 else 1 + clog2Help (p*2) n clog2Help p n = if p >= n then 0 else 1 + clog2Help (p*2) n
clog2 :: Int32 -> Int32 clog2 :: Integer -> Integer
clog2 n = if n < 2 then 0 else clog2Help 1 n clog2 n = if n < 2 then 0 else clog2Help 1 n
readNumber :: String -> Maybe Int32 readNumber :: String -> Maybe Integer
readNumber ('3' : '2' : '\'' : 'd' : rest) = readMaybe rest readNumber ('3' : '2' : '\'' : 'd' : rest) = readMaybe rest
readNumber ( '\'' : 'd' : rest) = readMaybe rest readNumber ( '\'' : 'd' : rest) = readMaybe rest
readNumber ('3' : '2' : '\'' : 'h' : rest) = readNumber ('3' : '2' : '\'' : 'h' : rest) =
@ -207,13 +205,7 @@ readNumber ('\'' : 'h' : rest) =
case readHex rest of case readHex rest of
[(v, _)] -> Just v [(v, _)] -> Just v
_ -> Nothing _ -> Nothing
readNumber n = readNumber n = readMaybe n
case readMaybe n of
Nothing -> Nothing
Just res ->
if show res == n
then Just res
else Nothing
showUniOpPrec :: Expr -> ShowS showUniOpPrec :: Expr -> ShowS
showUniOpPrec (e @ UniOp{}) = (showParen True . shows) e showUniOpPrec (e @ UniOp{}) = (showParen True . shows) e
@ -308,16 +300,14 @@ simplify (BinOp op e1 e2) =
(ShiftAL, Just x, Just y) -> Number $ show $ shiftL x (toInt y) (ShiftAL, Just x, Just y) -> Number $ show $ shiftL x (toInt y)
(ShiftAR, Just x, Just y) -> Number $ show $ shiftR x (toInt y) (ShiftAR, Just x, Just y) -> Number $ show $ shiftR x (toInt y)
(ShiftL , Just x, Just y) -> Number $ show $ shiftL x (toInt y) (ShiftL , Just x, Just y) -> Number $ show $ shiftL x (toInt y)
(ShiftR , Just x, Just y) -> -- does not sign extend (ShiftR , Just x, Just y) ->
Number $ show $ toInt32 $ shiftR (toWord32 x) (toInt y) if x < 0 && y > 0
then BinOp ShiftR (Number a) (Number b)
else Number $ show $ shiftR x (toInt y)
_ -> BinOp op e1' e2' _ -> BinOp op e1' e2'
where where
toInt :: Int32 -> Int toInt :: Integer -> Int
toInt = fromIntegral toInt = fromIntegral
toWord32 :: Int32 -> Word32
toWord32 = fromIntegral
toInt32 :: Word32 -> Int32
toInt32 = fromIntegral
(Add, BinOp Add e (Number a), Number b) -> (Add, BinOp Add e (Number a), Number b) ->
case (readNumber a, readNumber b) of case (readNumber a, readNumber b) of
(Just x, Just y) -> BinOp Add e $ Number $ show (x + y) (Just x, Just y) -> BinOp Add e $ Number $ show (x + y)

View File

@ -26,5 +26,25 @@ module top;
`TEST(2, 1); `TEST(2, 1);
`TEST(2, 2); `TEST(2, 2);
`TEST(2, 3); `TEST(2, 3);
`TEST(-8589934592, 0);
`TEST(-8589934592, 1);
`TEST(-8589934592, 2);
`TEST(-8589934592, 3);
`TEST(-8589934593, 0);
`TEST(-8589934593, 1);
`TEST(-8589934593, 2);
`TEST(-8589934593, 3);
`TEST(8589934592, 0);
`TEST(8589934592, 1);
`TEST(8589934592, 2);
`TEST(8589934592, 3);
`TEST(8589934593, 0);
`TEST(8589934593, 1);
`TEST(8589934593, 2);
`TEST(8589934593, 3);
end end
endmodule endmodule