From 3738ecf8fc636d714baa172ae71fa680e3b14fd8 Mon Sep 17 00:00:00 2001 From: "simonpj@microsoft.com" Date: Mon, 13 Nov 2006 09:05:17 +0000 Subject: [PATCH] Fixups to PelRules (esp using intResult, wordResult) In PrelRules we carefully use 'intResult' to trim off the overflow in compile-time calculations, but we were not doing so consistently. This patch fixes that, I think, and adds type signatures --- compiler/prelude/PrelRules.lhs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/prelude/PrelRules.lhs b/compiler/prelude/PrelRules.lhs index 8604647..6ca928c 100644 --- a/compiler/prelude/PrelRules.lhs +++ b/compiler/prelude/PrelRules.lhs @@ -223,39 +223,45 @@ cmpOp cmp l1 l2 -------------------------- -negOp (MachFloat 0.0) = Nothing -- can't represent -0.0 as a Rational -negOp (MachFloat f) = Just (mkFloatVal (-f)) +negOp :: Literal -> Maybe CoreExpr -- Negate +negOp (MachFloat 0.0) = Nothing -- can't represent -0.0 as a Rational +negOp (MachFloat f) = Just (mkFloatVal (-f)) negOp (MachDouble 0.0) = Nothing negOp (MachDouble d) = Just (mkDoubleVal (-d)) negOp (MachInt i) = intResult (-i) negOp l = Nothing -------------------------- +intOp2 :: (Integer->Integer->Integer) -> Literal -> Literal -> Maybe CoreExpr intOp2 op (MachInt i1) (MachInt i2) = intResult (i1 `op` i2) intOp2 op l1 l2 = Nothing -- Could find LitLit +intOp2Z :: (Integer->Integer->Integer) -> Literal -> Literal -> Maybe CoreExpr +-- Like intOp2, but Nothing if i2=0 intOp2Z op (MachInt i1) (MachInt i2) - | i2 /= 0 = Just (mkIntVal (i1 `op` i2)) + | i2 /= 0 = intResult (i1 `op` i2) intOp2Z op l1 l2 = Nothing -- LitLit or zero dividend -------------------------- #if __GLASGOW_HASKELL__ >= 500 +wordOp2 :: (Integer->Integer->Integer) -> Literal -> Literal -> Maybe CoreExpr wordOp2 op (MachWord w1) (MachWord w2) = wordResult (w1 `op` w2) wordOp2 op l1 l2 = Nothing -- Could find LitLit #endif +wordOp2Z :: (Integer->Integer->Integer) -> Literal -> Literal -> Maybe CoreExpr wordOp2Z op (MachWord w1) (MachWord w2) - | w2 /= 0 = Just (mkWordVal (w1 `op` w2)) + | w2 /= 0 = wordResult (w1 `op` w2) wordOp2Z op l1 l2 = Nothing -- LitLit or zero dividend #if __GLASGOW_HASKELL__ >= 500 wordBitOp2 op l1@(MachWord w1) l2@(MachWord w2) - = Just (mkWordVal (w1 `op` w2)) + = wordResult (w1 `op` w2) #else -- Integer is not an instance of Bits, so we operate on Word64 wordBitOp2 op l1@(MachWord w1) l2@(MachWord w2) - = Just (mkWordVal ((fromIntegral::Word64->Integer) (fromIntegral w1 `op` fromIntegral w2))) + = wordResult ((fromIntegral::Word64->Integer) (fromIntegral w1 `op` fromIntegral w2)) #endif wordBitOp2 op l1 l2 = Nothing -- Could find LitLit -- 1.7.10.4