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

import GhcPrelude

import CmmType
import CmmMachOp
import Outputable

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

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

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

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

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

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

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

atomicRMWLabel :: Width -> AtomicMachOp -> String
atomicRMWLabel :: Width -> AtomicMachOp -> String
atomicRMWLabel w :: Width
w amop :: AtomicMachOp
amop = "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 W8  = "8"
    pprWidth W16 = "16"
    pprWidth W32 = "32"
    pprWidth W64 = "64"
    pprWidth w :: Width
w   = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic "atomicRMWLabel: Unsupported word width " (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

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

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

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

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