-- | Generating C symbol names emitted by the compiler.
module CPrim
    ( atomicReadLabel
    , atomicWriteLabel
    , atomicRMWLabel
    , cmpxchgLabel
    , popCntLabel
    , pdepLabel
    , pextLabel
    , bSwapLabel
    , bRevLabel
    , clzLabel
    , ctzLabel
    , word2FloatLabel
    ) where

import GhcPrelude

import CmmType
import CmmMachOp
import Outputable

popCntLabel :: Width -> String
popCntLabel :: Width -> String
popCntLabel Width
w = String
"hs_popcnt" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"popCntLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

pdepLabel :: Width -> String
pdepLabel :: Width -> String
pdepLabel Width
w = String
"hs_pdep" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"pdepLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

pextLabel :: Width -> String
pextLabel :: Width -> String
pextLabel Width
w = String
"hs_pext" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"pextLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

bSwapLabel :: Width -> String
bSwapLabel :: Width -> String
bSwapLabel Width
w = String
"hs_bswap" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"bSwapLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

bRevLabel :: Width -> String
bRevLabel :: Width -> String
bRevLabel Width
w = String
"hs_bitrev" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"bRevLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

clzLabel :: Width -> String
clzLabel :: Width -> String
clzLabel Width
w = String
"hs_clz" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"clzLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

ctzLabel :: Width -> String
ctzLabel :: Width -> String
ctzLabel Width
w = String
"hs_ctz" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"ctzLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

word2FloatLabel :: Width -> String
word2FloatLabel :: Width -> String
word2FloatLabel Width
w = String
"hs_word2float" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"word2FloatLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

atomicRMWLabel :: Width -> AtomicMachOp -> String
atomicRMWLabel :: Width -> AtomicMachOp -> String
atomicRMWLabel Width
w AtomicMachOp
amop = String
"hs_atomic_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ AtomicMachOp -> String
pprFunName AtomicMachOp
amop String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"atomicRMWLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

    pprFunName :: AtomicMachOp -> String
pprFunName AtomicMachOp
AMO_Add  = String
"add"
    pprFunName AtomicMachOp
AMO_Sub  = String
"sub"
    pprFunName AtomicMachOp
AMO_And  = String
"and"
    pprFunName AtomicMachOp
AMO_Nand = String
"nand"
    pprFunName AtomicMachOp
AMO_Or   = String
"or"
    pprFunName AtomicMachOp
AMO_Xor  = String
"xor"

cmpxchgLabel :: Width -> String
cmpxchgLabel :: Width -> String
cmpxchgLabel Width
w = String
"hs_cmpxchg" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"cmpxchgLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

atomicReadLabel :: Width -> String
atomicReadLabel :: Width -> String
atomicReadLabel Width
w = String
"hs_atomicread" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"atomicReadLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

atomicWriteLabel :: Width -> String
atomicWriteLabel :: Width -> String
atomicWriteLabel Width
w = String
"hs_atomicwrite" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
pprWidth Width
w
  where
    pprWidth :: Width -> String
pprWidth Width
W8  = String
"8"
    pprWidth Width
W16 = String
"16"
    pprWidth Width
W32 = String
"32"
    pprWidth Width
W64 = String
"64"
    pprWidth Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"atomicWriteLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)