module X86.Cond (
        Cond(..),
        condUnsigned,
        condToSigned,
        condToUnsigned,
        maybeFlipCond,
        maybeInvertCond
)

where

import GhcPrelude

data Cond
        = ALWAYS        -- What's really used? ToDo
        | EQQ
        | GE
        | GEU
        | GTT
        | GU
        | LE
        | LEU
        | LTT
        | LU
        | NE
        | NEG
        | POS
        | CARRY
        | OFLO
        | PARITY
        | NOTPARITY
        deriving Cond -> Cond -> Bool
(Cond -> Cond -> Bool) -> (Cond -> Cond -> Bool) -> Eq Cond
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Cond -> Cond -> Bool
$c/= :: Cond -> Cond -> Bool
== :: Cond -> Cond -> Bool
$c== :: Cond -> Cond -> Bool
Eq

condUnsigned :: Cond -> Bool
condUnsigned :: Cond -> Bool
condUnsigned Cond
GU  = Bool
True
condUnsigned Cond
LU  = Bool
True
condUnsigned Cond
GEU = Bool
True
condUnsigned Cond
LEU = Bool
True
condUnsigned Cond
_   = Bool
False


condToSigned :: Cond -> Cond
condToSigned :: Cond -> Cond
condToSigned Cond
GU  = Cond
GTT
condToSigned Cond
LU  = Cond
LTT
condToSigned Cond
GEU = Cond
GE
condToSigned Cond
LEU = Cond
LE
condToSigned Cond
x   = Cond
x


condToUnsigned :: Cond -> Cond
condToUnsigned :: Cond -> Cond
condToUnsigned Cond
GTT = Cond
GU
condToUnsigned Cond
LTT = Cond
LU
condToUnsigned Cond
GE  = Cond
GEU
condToUnsigned Cond
LE  = Cond
LEU
condToUnsigned Cond
x   = Cond
x

-- | @maybeFlipCond c@ returns @Just c'@ if it is possible to flip the
-- arguments to the conditional @c@, and the new condition should be @c'@.
maybeFlipCond :: Cond -> Maybe Cond
maybeFlipCond :: Cond -> Maybe Cond
maybeFlipCond Cond
cond  = case Cond
cond of
        Cond
EQQ   -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
EQQ
        Cond
NE    -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
NE
        Cond
LU    -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GU
        Cond
GU    -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
LU
        Cond
LEU   -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GEU
        Cond
GEU   -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
LEU
        Cond
LTT   -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GTT
        Cond
GTT   -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
LTT
        Cond
LE    -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GE
        Cond
GE    -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
LE
        Cond
_other -> Maybe Cond
forall a. Maybe a
Nothing

-- | If we apply @maybeInvertCond@ to the condition of a jump we turn
-- jumps taken into jumps not taken and vice versa.
--
-- Careful! If the used comparison and the conditional jump
-- don't match the above behaviour will NOT hold.
-- When used for FP comparisons this does not consider unordered
-- numbers.
-- Also inverting twice might return a synonym for the original condition.
maybeInvertCond :: Cond -> Maybe Cond
maybeInvertCond :: Cond -> Maybe Cond
maybeInvertCond Cond
cond  = case Cond
cond of
        Cond
ALWAYS  -> Maybe Cond
forall a. Maybe a
Nothing
        Cond
EQQ     -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
NE
        Cond
NE      -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
EQQ

        Cond
NEG     -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
POS
        Cond
POS     -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
NEG

        Cond
GEU     -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
LU
        Cond
LU      -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GEU

        Cond
GE      -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
LTT
        Cond
LTT     -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GE

        Cond
GTT     -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
LE
        Cond
LE      -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GTT

        Cond
GU      -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
LEU
        Cond
LEU     -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GU

        --GEU "==" NOTCARRY, they are synonyms
        --at the assembly level
        Cond
CARRY   -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
GEU

        Cond
OFLO    -> Maybe Cond
forall a. Maybe a
Nothing

        Cond
PARITY  -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
NOTPARITY
        Cond
NOTPARITY -> Cond -> Maybe Cond
forall a. a -> Maybe a
Just Cond
PARITY