GHC.Cmm.Switch

ghc-lib-parser-9.10.1.20250103: The GHC API, decoupled from GHC versions

Safe Haskell | Ignore
Language | GHC2021

GHC.Cmm.Switch

Synopsis

- data SwitchTargets
- mkSwitchTargets :: Bool -> (Integer, Integer) -> Maybe Label -> Map Integer Label -> SwitchTargets
- switchTargetsCases :: SwitchTargets -> [(Integer, Label)]
- switchTargetsDefault :: SwitchTargets -> Maybe Label
- switchTargetsRange :: SwitchTargets -> (Integer, Integer)
- switchTargetsSigned :: SwitchTargets -> Bool
- mapSwitchTargets :: (Label -> Label) -> SwitchTargets -> SwitchTargets
- switchTargetsToTable :: SwitchTargets -> (Int, [Maybe Label])
- switchTargetsFallThrough :: SwitchTargets -> ([(NonEmpty Integer, Label)], Maybe Label)
- switchTargetsToList :: SwitchTargets -> [Label]
- eqSwitchTargetWith :: (Label -> Label -> Bool) -> SwitchTargets -> SwitchTargets -> Bool
- data SwitchPlan
  - = Unconditionally Label
  - | IfEqual Integer Label SwitchPlan
  - | IfLT Bool Integer SwitchPlan SwitchPlan
  - | JumpTable SwitchTargets
- backendHasNativeSwitch :: Backend -> Bool
- createSwitchPlan :: SwitchTargets -> SwitchPlan

# Documentation

data SwitchTargets #

A value of type SwitchTargets contains the alternatives for a `CmmSwitch` value, and knows whether the value is signed, the possible range, an optional default value and a map from values to jump labels.

#### Instances

Instances details

Show SwitchTargets # |
Instance details Defined in GHC.Cmm.Switch Methods showsPrec :: Int -> SwitchTargets -> ShowS # show :: SwitchTargets -> String # showList :: [SwitchTargets] -> ShowS #
Eq SwitchTargets # |
Instance details Defined in GHC.Cmm.Switch Methods (==) :: SwitchTargets -> SwitchTargets -> Bool # (/=) :: SwitchTargets -> SwitchTargets -> Bool #

mkSwitchTargets :: Bool -> (Integer, Integer) -> Maybe Label -> Map Integer Label -> SwitchTargets #

The smart constructor mkSwitchTargets normalises the map a bit: * No entries outside the range * No entries equal to the default * No default if all elements have explicit values

switchTargetsCases :: SwitchTargets -> [(Integer, Label)] #

Returns the list of non-default branches of the SwitchTargets value

switchTargetsDefault :: SwitchTargets -> Maybe Label #

Return the default label of the SwitchTargets value

switchTargetsRange :: SwitchTargets -> (Integer, Integer) #

Return the range of the SwitchTargets value

switchTargetsSigned :: SwitchTargets -> Bool #

Return whether this is used for a signed value

mapSwitchTargets :: (Label -> Label) -> SwitchTargets -> SwitchTargets #

Changes all labels mentioned in the SwitchTargets value

switchTargetsToTable :: SwitchTargets -> (Int, [Maybe Label]) #

switchTargetsToTable creates a dense jump table, usable for code generation.

Also returns an offset to add to the value; the list is 0-based on the result of that addition.

The conversion from Integer to Int is a bit of a wart, as the actual scrutinee might be an unsigned word, but it just works, due to wrap-around arithmetic (as verified by the CmmSwitchTest test case).

switchTargetsFallThrough :: SwitchTargets -> ([(NonEmpty Integer, Label)], Maybe Label) #

Groups cases with equal targets, suitable for pretty-printing to a c-like switch statement with fall-through semantics.

switchTargetsToList :: SwitchTargets -> [Label] #

The list of all labels occurring in the SwitchTargets value.

eqSwitchTargetWith :: (Label -> Label -> Bool) -> SwitchTargets -> SwitchTargets -> Bool #

Custom equality helper, needed for GHC.Cmm.CommonBlockElim

data SwitchPlan #

A SwitchPlan abstractly describes how a Switch statement ought to be implemented. See Note [createSwitchPlan]

Constructors

Unconditionally Label |
IfEqual Integer Label SwitchPlan |
IfLT Bool Integer SwitchPlan SwitchPlan |
JumpTable SwitchTargets |

#### Instances

Instances details

Show SwitchPlan # |
Instance details Defined in GHC.Cmm.Switch Methods showsPrec :: Int -> SwitchPlan -> ShowS # show :: SwitchPlan -> String # showList :: [SwitchPlan] -> ShowS #

backendHasNativeSwitch :: Backend -> Bool #

The back end targets a technology that implements `switch` natively. (For example, LLVM or C.) Therefore it is not necessary for GHC to ccompile a Cmm `Switch` form into a decision tree with jump tables at the leaves.

createSwitchPlan :: SwitchTargets -> SwitchPlan #

This function creates a SwitchPlan from a SwitchTargets value, breaking it down into smaller pieces suitable for code generation.