summaryrefslogtreecommitdiff
path: root/compiler/GHC/CmmToAsm/Wasm/Utils.hs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/GHC/CmmToAsm/Wasm/Utils.hs')
-rw-r--r--compiler/GHC/CmmToAsm/Wasm/Utils.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/compiler/GHC/CmmToAsm/Wasm/Utils.hs b/compiler/GHC/CmmToAsm/Wasm/Utils.hs
new file mode 100644
index 0000000000..b794c7f5b7
--- /dev/null
+++ b/compiler/GHC/CmmToAsm/Wasm/Utils.hs
@@ -0,0 +1,35 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE Strict #-}
+
+module GHC.CmmToAsm.Wasm.Utils
+ ( naturalNarrowing,
+ widthMax,
+ detEltsUFM,
+ detEltsUniqMap,
+ builderCommas,
+ )
+where
+
+import Data.ByteString.Builder
+import Data.List (intersperse, sortOn)
+import GHC.Cmm
+import GHC.Prelude
+import GHC.Types.Unique.FM
+import GHC.Types.Unique.Map
+
+naturalNarrowing :: Width -> Integer -> Integer
+naturalNarrowing w i
+ | i < 0 = narrowS w i
+ | otherwise = narrowU w i
+
+widthMax :: Width -> Integer
+widthMax w = (1 `shiftL` widthInBits w) - 1
+
+detEltsUFM :: Ord k => UniqFM k0 (k, a) -> [(k, a)]
+detEltsUFM = sortOn fst . nonDetEltsUFM
+
+detEltsUniqMap :: Ord k => UniqMap k a -> [(k, a)]
+detEltsUniqMap = sortOn fst . nonDetEltsUniqMap
+
+builderCommas :: (a -> Builder) -> [a] -> Builder
+builderCommas f xs = mconcat (intersperse ", " (map f xs))