diff options
author | Sylvain Henry <sylvain@haskus.fr> | 2021-09-15 14:50:37 +0200 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2022-02-09 20:42:23 -0500 |
commit | 2b1cced18159171a4d6e730ba1d69cd024babb8d (patch) | |
tree | 3a247c020808ecd87f9393257df114f226c8c053 | |
parent | 1dbe5b2a97c41b3f24a48372ba25faeae08e8cd6 (diff) | |
download | haskell-2b1cced18159171a4d6e730ba1d69cd024babb8d.tar.gz |
NCG: minor code factorization
-rw-r--r-- | compiler/GHC/CmmToAsm/AArch64/Ppr.hs | 25 | ||||
-rw-r--r-- | compiler/GHC/CmmToAsm/Ppr.hs | 61 |
2 files changed, 35 insertions, 51 deletions
diff --git a/compiler/GHC/CmmToAsm/AArch64/Ppr.hs b/compiler/GHC/CmmToAsm/AArch64/Ppr.hs index 5a48241c0b..124a421554 100644 --- a/compiler/GHC/CmmToAsm/AArch64/Ppr.hs +++ b/compiler/GHC/CmmToAsm/AArch64/Ppr.hs @@ -5,11 +5,6 @@ module GHC.CmmToAsm.AArch64.Ppr (pprNatCmmDecl, pprInstr) where import GHC.Prelude hiding (EQ) -import Data.Word -import qualified Data.Array.Unsafe as U ( castSTUArray ) -import Data.Array.ST -import Control.Monad.ST - import GHC.CmmToAsm.AArch64.Instr import GHC.CmmToAsm.AArch64.Regs import GHC.CmmToAsm.AArch64.Cond @@ -228,30 +223,14 @@ pprDataItem config lit ppr_item FF32 (CmmFloat r _) = let bs = floatToBytes (fromRational r) - in map (\b -> text "\t.byte\t" <> pprImm platform (ImmInt b)) bs + in map (\b -> text "\t.byte\t" <> int (fromIntegral b)) bs ppr_item FF64 (CmmFloat r _) = let bs = doubleToBytes (fromRational r) - in map (\b -> text "\t.byte\t" <> pprImm platform (ImmInt b)) bs + in map (\b -> text "\t.byte\t" <> int (fromIntegral b)) bs ppr_item _ _ = pprPanic "pprDataItem:ppr_item" (text $ show lit) -floatToBytes :: Float -> [Int] -floatToBytes f - = runST (do - arr <- newArray_ ((0::Int),3) - writeArray arr 0 f - arr <- castFloatToWord8Array arr - i0 <- readArray arr 0 - i1 <- readArray arr 1 - i2 <- readArray arr 2 - i3 <- readArray arr 3 - return (map fromIntegral [i0,i1,i2,i3]) - ) - -castFloatToWord8Array :: STUArray s Int Float -> ST s (STUArray s Int Word8) -castFloatToWord8Array = U.castSTUArray - pprImm :: Platform -> Imm -> SDoc pprImm _ (ImmInt i) = int i pprImm _ (ImmInteger i) = integer i diff --git a/compiler/GHC/CmmToAsm/Ppr.hs b/compiler/GHC/CmmToAsm/Ppr.hs index df113b45b6..441ee75348 100644 --- a/compiler/GHC/CmmToAsm/Ppr.hs +++ b/compiler/GHC/CmmToAsm/Ppr.hs @@ -11,6 +11,7 @@ module GHC.CmmToAsm.Ppr ( doubleToBytes, + floatToBytes, pprASCII, pprString, pprFileEmbed, @@ -34,7 +35,6 @@ import qualified Data.Array.Unsafe as U ( castSTUArray ) import Data.Array.ST import Control.Monad.ST - import Data.Word import Data.ByteString (ByteString) import qualified Data.ByteString as BS @@ -50,33 +50,38 @@ word8ToWord# w = w -- ----------------------------------------------------------------------------- -- Converting floating-point literals to integrals for printing -castDoubleToWord8Array :: STUArray s Int Double -> ST s (STUArray s Int Word8) -castDoubleToWord8Array = U.castSTUArray - --- floatToBytes and doubleToBytes convert to the host's byte --- order. Providing that we're not cross-compiling for a --- target with the opposite endianness, this should work ok --- on all targets. - --- ToDo: this stuff is very similar to the shenanigans in PprAbs, --- could they be merged? - -doubleToBytes :: Double -> [Int] -doubleToBytes d - = runST (do - arr <- newArray_ ((0::Int),7) - writeArray arr 0 d - arr <- castDoubleToWord8Array arr - i0 <- readArray arr 0 - i1 <- readArray arr 1 - i2 <- readArray arr 2 - i3 <- readArray arr 3 - i4 <- readArray arr 4 - i5 <- readArray arr 5 - i6 <- readArray arr 6 - i7 <- readArray arr 7 - return (map fromIntegral [i0,i1,i2,i3,i4,i5,i6,i7]) - ) +-- | Get bytes of a Float representation +floatToBytes :: Float -> [Word8] +floatToBytes f = runST $ do + arr <- newArray_ ((0::Int),3) + writeArray arr 0 f + let cast :: STUArray s Int Float -> ST s (STUArray s Int Word8) + cast = U.castSTUArray + arr <- cast arr + i0 <- readArray arr 0 + i1 <- readArray arr 1 + i2 <- readArray arr 2 + i3 <- readArray arr 3 + return [i0,i1,i2,i3] + +-- | Get bytes of a Double representation +doubleToBytes :: Double -> [Word8] +doubleToBytes d = runST $ do + arr <- newArray_ ((0::Int),7) + writeArray arr 0 d + let cast :: STUArray s Int Double -> ST s (STUArray s Int Word8) + cast = U.castSTUArray + arr <- cast arr + i0 <- readArray arr 0 + i1 <- readArray arr 1 + i2 <- readArray arr 2 + i3 <- readArray arr 3 + i4 <- readArray arr 4 + i5 <- readArray arr 5 + i6 <- readArray arr 6 + i7 <- readArray arr 7 + return [i0,i1,i2,i3,i4,i5,i6,i7] + -- --------------------------------------------------------------------------- -- Printing ASCII strings. |