{-# LANGUAGE CPP, DeriveFunctor, GADTs, PatternSynonyms #-}
{-# LANGUAGE LambdaCase #-}
module GHC.CmmToC (
writeC
) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Cmm.BlockId
import GHC.Cmm.CLabel
import GHC.Types.ForeignCall
import GHC.Cmm hiding (pprBBlock)
import GHC.Cmm.Ppr ()
import GHC.Cmm.Dataflow.Block
import GHC.Cmm.Dataflow.Collections
import GHC.Cmm.Dataflow.Graph
import GHC.Cmm.Utils
import GHC.Cmm.Switch
import GHC.CmmToAsm.CPrim
import GHC.Driver.Session
import GHC.Data.FastString
import GHC.Utils.Outputable
import GHC.Platform
import GHC.Types.Unique.Set
import GHC.Types.Unique.FM
import GHC.Types.Unique
import GHC.Utils.Misc
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Control.Monad.ST
import Data.Bits
import Data.Char
import Data.List (intersperse)
import Data.Map (Map)
import Data.Word
import System.IO
import qualified Data.Map as Map
import Control.Monad (ap)
import qualified Data.Array.Unsafe as U ( castSTUArray )
import Data.Array.ST
writeC :: DynFlags -> Handle -> RawCmmGroup -> IO ()
writeC :: DynFlags -> Handle -> RawCmmGroup -> IO ()
writeC DynFlags
dflags Handle
handle RawCmmGroup
cmm = DynFlags -> Handle -> SDoc -> IO ()
printForC DynFlags
dflags Handle
handle (DynFlags -> RawCmmGroup -> SDoc
pprC DynFlags
dflags RawCmmGroup
cmm SDoc -> SDoc -> SDoc
$$ SDoc
blankLine)
pprC :: DynFlags -> RawCmmGroup -> SDoc
pprC :: DynFlags -> RawCmmGroup -> SDoc
pprC DynFlags
dflags RawCmmGroup
tops = [SDoc] -> SDoc
vcat ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
intersperse SDoc
blankLine ([SDoc] -> [SDoc]) -> [SDoc] -> [SDoc]
forall a b. (a -> b) -> a -> b
$ (RawCmmDecl -> SDoc) -> RawCmmGroup -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (DynFlags -> RawCmmDecl -> SDoc
pprTop DynFlags
dflags) RawCmmGroup
tops
pprTop :: DynFlags -> RawCmmDecl -> SDoc
pprTop :: DynFlags -> RawCmmDecl -> SDoc
pprTop DynFlags
dflags = \case
(CmmProc LabelMap RawCmmStatics
infos CLabel
clbl [GlobalReg]
_in_live_regs CmmGraph
graph) ->
(case KeyOf LabelMap -> LabelMap RawCmmStatics -> Maybe RawCmmStatics
forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> map a -> Maybe a
mapLookup (CmmGraph -> BlockId
forall (n :: Extensibility -> Extensibility -> *).
GenCmmGraph n -> BlockId
g_entry CmmGraph
graph) LabelMap RawCmmStatics
infos of
Maybe RawCmmStatics
Nothing -> SDoc
empty
Just (CmmStaticsRaw CLabel
info_clbl [CmmStatic]
info_dat) ->
Platform -> [CmmStatic] -> SDoc
pprDataExterns Platform
platform [CmmStatic]
info_dat SDoc -> SDoc -> SDoc
$$
DynFlags -> Bool -> CLabel -> [CmmStatic] -> SDoc
pprWordArray DynFlags
dflags Bool
info_is_in_rodata CLabel
info_clbl [CmmStatic]
info_dat) SDoc -> SDoc -> SDoc
$$
([SDoc] -> SDoc
vcat [
SDoc
blankLine,
SDoc
extern_decls,
(if (CLabel -> Bool
externallyVisibleCLabel CLabel
clbl)
then SDoc -> SDoc
mkFN_ else SDoc -> SDoc
mkIF_) (CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr CLabel
clbl) SDoc -> SDoc -> SDoc
<+> SDoc
lbrace,
Int -> SDoc -> SDoc
nest Int
8 SDoc
temp_decls,
[SDoc] -> SDoc
vcat ((CmmBlock -> SDoc) -> [CmmBlock] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (DynFlags -> CmmBlock -> SDoc
pprBBlock DynFlags
dflags) [CmmBlock]
blocks),
SDoc
rbrace ]
)
where
info_is_in_rodata :: Bool
info_is_in_rodata = Bool
True
blocks :: [CmmBlock]
blocks = CmmGraph -> [CmmBlock]
toBlockListEntryFirst CmmGraph
graph
(SDoc
temp_decls, SDoc
extern_decls) = Platform -> [CmmBlock] -> (SDoc, SDoc)
pprTempAndExternDecls Platform
platform [CmmBlock]
blocks
(CmmData Section
section (CmmStaticsRaw CLabel
lbl [CmmString ByteString
str])) ->
Platform -> CLabel -> SDoc
pprExternDecl Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
$$
[SDoc] -> SDoc
hcat [
CLabel -> SDoc
pprLocalness CLabel
lbl, Bool -> SDoc
pprConstness (Section -> Bool
isSecConstant Section
section), String -> SDoc
text String
"char ", CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr CLabel
lbl,
String -> SDoc
text String
"[] = ", ByteString -> SDoc
pprStringInCStyle ByteString
str, SDoc
semi
]
(CmmData Section
section (CmmStaticsRaw CLabel
lbl [CmmUninitialised Int
size])) ->
Platform -> CLabel -> SDoc
pprExternDecl Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
$$
[SDoc] -> SDoc
hcat [
CLabel -> SDoc
pprLocalness CLabel
lbl, Bool -> SDoc
pprConstness (Section -> Bool
isSecConstant Section
section), String -> SDoc
text String
"char ", CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr CLabel
lbl,
SDoc -> SDoc
brackets (Int -> SDoc
int Int
size), SDoc
semi
]
(CmmData Section
section (CmmStaticsRaw CLabel
lbl [CmmStatic]
lits)) ->
Platform -> [CmmStatic] -> SDoc
pprDataExterns Platform
platform [CmmStatic]
lits SDoc -> SDoc -> SDoc
$$
DynFlags -> Bool -> CLabel -> [CmmStatic] -> SDoc
pprWordArray DynFlags
dflags (Section -> Bool
isSecConstant Section
section) CLabel
lbl [CmmStatic]
lits
where
isSecConstant :: Section -> Bool
isSecConstant Section
section = case Section -> SectionProtection
sectionProtection Section
section of
SectionProtection
ReadOnlySection -> Bool
True
SectionProtection
WriteProtectedSection -> Bool
True
SectionProtection
_ -> Bool
False
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
pprBBlock :: DynFlags -> CmmBlock -> SDoc
pprBBlock :: DynFlags -> CmmBlock -> SDoc
pprBBlock DynFlags
dflags CmmBlock
block =
Int -> SDoc -> SDoc
nest Int
4 (BlockId -> SDoc
pprBlockId (CmmBlock -> BlockId
forall (thing :: Extensibility -> Extensibility -> *)
(x :: Extensibility).
NonLocal thing =>
thing C x -> BlockId
entryLabel CmmBlock
block) SDoc -> SDoc -> SDoc
<> SDoc
colon) SDoc -> SDoc -> SDoc
$$
Int -> SDoc -> SDoc
nest Int
8 ([SDoc] -> SDoc
vcat ((CmmNode O O -> SDoc) -> [CmmNode O O] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (DynFlags -> CmmNode O O -> SDoc
forall (e :: Extensibility) (x :: Extensibility).
DynFlags -> CmmNode e x -> SDoc
pprStmt DynFlags
dflags) (Block CmmNode O O -> [CmmNode O O]
forall (n :: Extensibility -> Extensibility -> *).
Block n O O -> [n O O]
blockToList Block CmmNode O O
nodes)) SDoc -> SDoc -> SDoc
$$ DynFlags -> CmmNode O C -> SDoc
forall (e :: Extensibility) (x :: Extensibility).
DynFlags -> CmmNode e x -> SDoc
pprStmt DynFlags
dflags CmmNode O C
last)
where
(CmmNode C O
_, Block CmmNode O O
nodes, CmmNode O C
last) = CmmBlock -> (CmmNode C O, Block CmmNode O O, CmmNode O C)
forall (n :: Extensibility -> Extensibility -> *).
Block n C C -> (n C O, Block n O O, n O C)
blockSplit CmmBlock
block
pprWordArray :: DynFlags -> Bool -> CLabel -> [CmmStatic] -> SDoc
pprWordArray :: DynFlags -> Bool -> CLabel -> [CmmStatic] -> SDoc
pprWordArray DynFlags
dflags Bool
is_ro CLabel
lbl [CmmStatic]
ds
=
Platform -> CLabel -> SDoc
pprExternDecl Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
$$
[SDoc] -> SDoc
hcat [ CLabel -> SDoc
pprLocalness CLabel
lbl, Bool -> SDoc
pprConstness Bool
is_ro, String -> SDoc
text String
"StgWord"
, SDoc
space, CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr CLabel
lbl, String -> SDoc
text String
"[]"
, Width -> SDoc
pprAlignment (Platform -> Width
wordWidth Platform
platform)
, String -> SDoc
text String
"= {" ]
SDoc -> SDoc -> SDoc
$$ Int -> SDoc -> SDoc
nest Int
8 ([SDoc] -> SDoc
commafy (DynFlags -> [CmmStatic] -> [SDoc]
pprStatics DynFlags
dflags [CmmStatic]
ds))
SDoc -> SDoc -> SDoc
$$ String -> SDoc
text String
"};"
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
pprAlignment :: Width -> SDoc
pprAlignment :: Width -> SDoc
pprAlignment Width
words =
String -> SDoc
text String
"__attribute__((aligned(" SDoc -> SDoc -> SDoc
<> Int -> SDoc
int (Width -> Int
widthInBytes Width
words) SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
")))"
pprLocalness :: CLabel -> SDoc
pprLocalness :: CLabel -> SDoc
pprLocalness CLabel
lbl | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ CLabel -> Bool
externallyVisibleCLabel CLabel
lbl = String -> SDoc
text String
"static "
| Bool
otherwise = SDoc
empty
pprConstness :: Bool -> SDoc
pprConstness :: Bool -> SDoc
pprConstness Bool
is_ro | Bool
is_ro = String -> SDoc
text String
"const "
| Bool
otherwise = SDoc
empty
pprStmt :: DynFlags -> CmmNode e x -> SDoc
pprStmt :: forall (e :: Extensibility) (x :: Extensibility).
DynFlags -> CmmNode e x -> SDoc
pprStmt DynFlags
dflags CmmNode e x
stmt =
case CmmNode e x
stmt of
CmmEntry{} -> SDoc
empty
CmmComment FastString
_ -> SDoc
empty
CmmTick CmmTickish
_ -> SDoc
empty
CmmUnwind{} -> SDoc
empty
CmmAssign CmmReg
dest CmmExpr
src -> DynFlags -> CmmReg -> CmmExpr -> SDoc
pprAssign DynFlags
dflags CmmReg
dest CmmExpr
src
CmmStore CmmExpr
dest CmmExpr
src
| CmmType -> Width
typeWidth CmmType
rep Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W64 Bool -> Bool -> Bool
&& Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
/= Width
W64
-> (if CmmType -> Bool
isFloatType CmmType
rep then String -> SDoc
text String
"ASSIGN_DBL"
else PtrString -> SDoc
ptext (String -> PtrString
sLit (String
"ASSIGN_Word64"))) SDoc -> SDoc -> SDoc
<>
SDoc -> SDoc
parens (SDoc
mkP_ SDoc -> SDoc -> SDoc
<> DynFlags -> CmmExpr -> SDoc
pprExpr1 DynFlags
dflags CmmExpr
dest SDoc -> SDoc -> SDoc
<> SDoc
comma SDoc -> SDoc -> SDoc
<> DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
src) SDoc -> SDoc -> SDoc
<> SDoc
semi
| Bool
otherwise
-> [SDoc] -> SDoc
hsep [ DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags (CmmExpr -> CmmType -> CmmExpr
CmmLoad CmmExpr
dest CmmType
rep), SDoc
equals, DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
src SDoc -> SDoc -> SDoc
<> SDoc
semi ]
where
rep :: CmmType
rep = Platform -> CmmExpr -> CmmType
cmmExprType Platform
platform CmmExpr
src
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
CmmUnsafeForeignCall target :: ForeignTarget
target@(ForeignTarget CmmExpr
fn ForeignConvention
conv) [LocalReg]
results [CmmExpr]
args ->
SDoc
fnCall
where
([ForeignHint]
res_hints, [ForeignHint]
arg_hints) = ForeignTarget -> ([ForeignHint], [ForeignHint])
foreignTargetHints ForeignTarget
target
hresults :: [(LocalReg, ForeignHint)]
hresults = [LocalReg] -> [ForeignHint] -> [(LocalReg, ForeignHint)]
forall a b. [a] -> [b] -> [(a, b)]
zip [LocalReg]
results [ForeignHint]
res_hints
hargs :: [(CmmExpr, ForeignHint)]
hargs = [CmmExpr] -> [ForeignHint] -> [(CmmExpr, ForeignHint)]
forall a b. [a] -> [b] -> [(a, b)]
zip [CmmExpr]
args [ForeignHint]
arg_hints
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
ForeignConvention CCallConv
cconv [ForeignHint]
_ [ForeignHint]
_ CmmReturnInfo
ret = ForeignConvention
conv
cast_fn :: SDoc
cast_fn = SDoc -> SDoc
parens (DynFlags -> SDoc -> CmmExpr -> SDoc
cCast DynFlags
dflags (Platform
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCFunType Platform
platform (Char -> SDoc
char Char
'*') CCallConv
cconv [(LocalReg, ForeignHint)]
hresults [(CmmExpr, ForeignHint)]
hargs) CmmExpr
fn)
fnCall :: SDoc
fnCall =
case CmmExpr
fn of
CmmLit (CmmLabel CLabel
lbl)
| CCallConv
StdCallConv <- CCallConv
cconv ->
DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCall DynFlags
dflags (CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr CLabel
lbl) CCallConv
cconv [(LocalReg, ForeignHint)]
hresults [(CmmExpr, ForeignHint)]
hargs
| CmmReturnInfo
CmmNeverReturns <- CmmReturnInfo
ret ->
DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCall DynFlags
dflags SDoc
cast_fn CCallConv
cconv [(LocalReg, ForeignHint)]
hresults [(CmmExpr, ForeignHint)]
hargs SDoc -> SDoc -> SDoc
<> SDoc
semi
| Bool -> Bool
not (CLabel -> Bool
isMathFun CLabel
lbl) ->
DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprForeignCall DynFlags
dflags (CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr CLabel
lbl) CCallConv
cconv [(LocalReg, ForeignHint)]
hresults [(CmmExpr, ForeignHint)]
hargs
CmmExpr
_ ->
DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCall DynFlags
dflags SDoc
cast_fn CCallConv
cconv [(LocalReg, ForeignHint)]
hresults [(CmmExpr, ForeignHint)]
hargs SDoc -> SDoc -> SDoc
<> SDoc
semi
CmmUnsafeForeignCall (PrimTarget CallishMachOp
MO_Touch) [LocalReg]
_results [CmmExpr]
_args -> SDoc
empty
CmmUnsafeForeignCall (PrimTarget (MO_Prefetch_Data Int
_)) [LocalReg]
_results [CmmExpr]
_args -> SDoc
empty
CmmUnsafeForeignCall target :: ForeignTarget
target@(PrimTarget CallishMachOp
op) [LocalReg]
results [CmmExpr]
args ->
SDoc
fn_call
where
cconv :: CCallConv
cconv = CCallConv
CCallConv
fn :: SDoc
fn = CallishMachOp -> SDoc
pprCallishMachOp_for_C CallishMachOp
op
([ForeignHint]
res_hints, [ForeignHint]
arg_hints) = ForeignTarget -> ([ForeignHint], [ForeignHint])
foreignTargetHints ForeignTarget
target
hresults :: [(LocalReg, ForeignHint)]
hresults = [LocalReg] -> [ForeignHint] -> [(LocalReg, ForeignHint)]
forall a b. [a] -> [b] -> [(a, b)]
zip [LocalReg]
results [ForeignHint]
res_hints
hargs :: [(CmmExpr, ForeignHint)]
hargs = [CmmExpr] -> [ForeignHint] -> [(CmmExpr, ForeignHint)]
forall a b. [a] -> [b] -> [(a, b)]
zip [CmmExpr]
args [ForeignHint]
arg_hints
fn_call :: SDoc
fn_call
| Just Int
_align <- CallishMachOp -> Maybe Int
machOpMemcpyishAlign CallishMachOp
op
= (String -> SDoc
text String
";EFF_(" SDoc -> SDoc -> SDoc
<> SDoc
fn SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
')' SDoc -> SDoc -> SDoc
<> SDoc
semi) SDoc -> SDoc -> SDoc
$$
DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprForeignCall DynFlags
dflags SDoc
fn CCallConv
cconv [(LocalReg, ForeignHint)]
hresults [(CmmExpr, ForeignHint)]
hargs
| Bool
otherwise
= DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCall DynFlags
dflags SDoc
fn CCallConv
cconv [(LocalReg, ForeignHint)]
hresults [(CmmExpr, ForeignHint)]
hargs
CmmBranch BlockId
ident -> BlockId -> SDoc
pprBranch BlockId
ident
CmmCondBranch CmmExpr
expr BlockId
yes BlockId
no Maybe Bool
_ -> DynFlags -> CmmExpr -> BlockId -> BlockId -> SDoc
pprCondBranch DynFlags
dflags CmmExpr
expr BlockId
yes BlockId
no
CmmCall { cml_target :: CmmNode O C -> CmmExpr
cml_target = CmmExpr
expr } -> SDoc -> SDoc
mkJMP_ (DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
expr) SDoc -> SDoc -> SDoc
<> SDoc
semi
CmmSwitch CmmExpr
arg SwitchTargets
ids -> DynFlags -> CmmExpr -> SwitchTargets -> SDoc
pprSwitch DynFlags
dflags CmmExpr
arg SwitchTargets
ids
CmmNode e x
_other -> String -> SDoc -> SDoc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"PprC.pprStmt" (CmmNode e x -> SDoc
forall a. Outputable a => a -> SDoc
ppr CmmNode e x
stmt)
type Hinted a = (a, ForeignHint)
pprForeignCall :: DynFlags -> SDoc -> CCallConv -> [Hinted CmmFormal] -> [Hinted CmmActual]
-> SDoc
pprForeignCall :: DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprForeignCall DynFlags
dflags SDoc
fn CCallConv
cconv [(LocalReg, ForeignHint)]
results [(CmmExpr, ForeignHint)]
args = SDoc
fn_call
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
fn_call :: SDoc
fn_call = SDoc -> SDoc
braces (
Platform
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCFunType Platform
platform (Char -> SDoc
char Char
'*' SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"ghcFunPtr") CCallConv
cconv [(LocalReg, ForeignHint)]
results [(CmmExpr, ForeignHint)]
args SDoc -> SDoc -> SDoc
<> SDoc
semi
SDoc -> SDoc -> SDoc
$$ String -> SDoc
text String
"ghcFunPtr" SDoc -> SDoc -> SDoc
<+> SDoc
equals SDoc -> SDoc -> SDoc
<+> SDoc
cast_fn SDoc -> SDoc -> SDoc
<> SDoc
semi
SDoc -> SDoc -> SDoc
$$ DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCall DynFlags
dflags (String -> SDoc
text String
"ghcFunPtr") CCallConv
cconv [(LocalReg, ForeignHint)]
results [(CmmExpr, ForeignHint)]
args SDoc -> SDoc -> SDoc
<> SDoc
semi
)
cast_fn :: SDoc
cast_fn = SDoc -> SDoc
parens (SDoc -> SDoc
parens (Platform
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCFunType Platform
platform (Char -> SDoc
char Char
'*') CCallConv
cconv [(LocalReg, ForeignHint)]
results [(CmmExpr, ForeignHint)]
args) SDoc -> SDoc -> SDoc
<> SDoc
fn)
pprCFunType :: Platform -> SDoc -> CCallConv -> [Hinted CmmFormal] -> [Hinted CmmActual] -> SDoc
pprCFunType :: Platform
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCFunType Platform
platform SDoc
ppr_fn CCallConv
cconv [(LocalReg, ForeignHint)]
ress [(CmmExpr, ForeignHint)]
args
= let res_type :: [(LocalReg, ForeignHint)] -> SDoc
res_type [] = String -> SDoc
text String
"void"
res_type [(LocalReg
one, ForeignHint
hint)] = Platform -> CmmType -> ForeignHint -> SDoc
machRepHintCType Platform
platform (LocalReg -> CmmType
localRegType LocalReg
one) ForeignHint
hint
res_type [(LocalReg, ForeignHint)]
_ = String -> SDoc
forall a. String -> a
panic String
"pprCFunType: only void or 1 return value supported"
arg_type :: (CmmExpr, ForeignHint) -> SDoc
arg_type (CmmExpr
expr, ForeignHint
hint) = Platform -> CmmType -> ForeignHint -> SDoc
machRepHintCType Platform
platform (Platform -> CmmExpr -> CmmType
cmmExprType Platform
platform CmmExpr
expr) ForeignHint
hint
in [(LocalReg, ForeignHint)] -> SDoc
res_type [(LocalReg, ForeignHint)]
ress SDoc -> SDoc -> SDoc
<+>
SDoc -> SDoc
parens (CCallConv -> SDoc
ccallConvAttribute CCallConv
cconv SDoc -> SDoc -> SDoc
<> SDoc
ppr_fn) SDoc -> SDoc -> SDoc
<>
SDoc -> SDoc
parens ([SDoc] -> SDoc
commafy (((CmmExpr, ForeignHint) -> SDoc)
-> [(CmmExpr, ForeignHint)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (CmmExpr, ForeignHint) -> SDoc
arg_type [(CmmExpr, ForeignHint)]
args))
pprBranch :: BlockId -> SDoc
pprBranch :: BlockId -> SDoc
pprBranch BlockId
ident = String -> SDoc
text String
"goto" SDoc -> SDoc -> SDoc
<+> BlockId -> SDoc
pprBlockId BlockId
ident SDoc -> SDoc -> SDoc
<> SDoc
semi
pprCondBranch :: DynFlags -> CmmExpr -> BlockId -> BlockId -> SDoc
pprCondBranch :: DynFlags -> CmmExpr -> BlockId -> BlockId -> SDoc
pprCondBranch DynFlags
dflags CmmExpr
expr BlockId
yes BlockId
no
= [SDoc] -> SDoc
hsep [ String -> SDoc
text String
"if" , SDoc -> SDoc
parens(DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
expr) ,
String -> SDoc
text String
"goto", BlockId -> SDoc
pprBlockId BlockId
yes SDoc -> SDoc -> SDoc
<> SDoc
semi,
String -> SDoc
text String
"else goto", BlockId -> SDoc
pprBlockId BlockId
no SDoc -> SDoc -> SDoc
<> SDoc
semi ]
pprSwitch :: DynFlags -> CmmExpr -> SwitchTargets -> SDoc
pprSwitch :: DynFlags -> CmmExpr -> SwitchTargets -> SDoc
pprSwitch DynFlags
dflags CmmExpr
e SwitchTargets
ids
= (SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"switch" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
parens ( DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
e ) SDoc -> SDoc -> SDoc
<+> SDoc
lbrace)
Int
4 ([SDoc] -> SDoc
vcat ( (([Integer], BlockId) -> SDoc) -> [([Integer], BlockId)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map ([Integer], BlockId) -> SDoc
caseify [([Integer], BlockId)]
pairs ) SDoc -> SDoc -> SDoc
$$ SDoc
def)) SDoc -> SDoc -> SDoc
$$ SDoc
rbrace
where
([([Integer], BlockId)]
pairs, Maybe BlockId
mbdef) = SwitchTargets -> ([([Integer], BlockId)], Maybe BlockId)
switchTargetsFallThrough SwitchTargets
ids
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
caseify :: ([Integer], BlockId) -> SDoc
caseify (Integer
ix:[Integer]
ixs, BlockId
ident) = [SDoc] -> SDoc
vcat ((Integer -> SDoc) -> [Integer] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> SDoc
do_fallthrough [Integer]
ixs) SDoc -> SDoc -> SDoc
$$ Integer -> SDoc
final_branch Integer
ix
where
do_fallthrough :: Integer -> SDoc
do_fallthrough Integer
ix =
[SDoc] -> SDoc
hsep [ String -> SDoc
text String
"case" , DynFlags -> Integer -> Width -> SDoc
pprHexVal DynFlags
dflags Integer
ix (Platform -> Width
wordWidth Platform
platform) SDoc -> SDoc -> SDoc
<> SDoc
colon ,
String -> SDoc
text String
"/* fall through */" ]
final_branch :: Integer -> SDoc
final_branch Integer
ix =
[SDoc] -> SDoc
hsep [ String -> SDoc
text String
"case" , DynFlags -> Integer -> Width -> SDoc
pprHexVal DynFlags
dflags Integer
ix (Platform -> Width
wordWidth Platform
platform) SDoc -> SDoc -> SDoc
<> SDoc
colon ,
String -> SDoc
text String
"goto" , (BlockId -> SDoc
pprBlockId BlockId
ident) SDoc -> SDoc -> SDoc
<> SDoc
semi ]
caseify ([Integer]
_ , BlockId
_ ) = String -> SDoc
forall a. String -> a
panic String
"pprSwitch: switch with no cases!"
def :: SDoc
def | Just BlockId
l <- Maybe BlockId
mbdef = String -> SDoc
text String
"default: goto" SDoc -> SDoc -> SDoc
<+> BlockId -> SDoc
pprBlockId BlockId
l SDoc -> SDoc -> SDoc
<> SDoc
semi
| Bool
otherwise = SDoc
empty
pprExpr :: DynFlags -> CmmExpr -> SDoc
pprExpr :: DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
e = case CmmExpr
e of
CmmLit CmmLit
lit -> DynFlags -> CmmLit -> SDoc
pprLit DynFlags
dflags CmmLit
lit
CmmLoad CmmExpr
e CmmType
ty -> DynFlags -> CmmExpr -> CmmType -> SDoc
pprLoad DynFlags
dflags CmmExpr
e CmmType
ty
CmmReg CmmReg
reg -> CmmReg -> SDoc
pprCastReg CmmReg
reg
CmmRegOff CmmReg
reg Int
0 -> CmmReg -> SDoc
pprCastReg CmmReg
reg
CmmRegOff CmmReg
reg Int
i -> CmmReg -> SDoc
pprCastReg CmmReg
reg SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
'+' SDoc -> SDoc -> SDoc
<>
DynFlags -> Integer -> Width -> SDoc
pprHexVal DynFlags
dflags (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i) (Platform -> Width
wordWidth Platform
platform)
CmmMachOp MachOp
mop [CmmExpr]
args -> DynFlags -> MachOp -> [CmmExpr] -> SDoc
pprMachOpApp DynFlags
dflags MachOp
mop [CmmExpr]
args
CmmStackSlot Area
_ Int
_ -> String -> SDoc
forall a. String -> a
panic String
"pprExpr: CmmStackSlot not supported!"
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
pprLoad :: DynFlags -> CmmExpr -> CmmType -> SDoc
pprLoad :: DynFlags -> CmmExpr -> CmmType -> SDoc
pprLoad DynFlags
dflags CmmExpr
e CmmType
ty
| Width
width Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W64, Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
/= Width
W64
= (if CmmType -> Bool
isFloatType CmmType
ty then String -> SDoc
text String
"PK_DBL"
else String -> SDoc
text String
"PK_Word64")
SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens (SDoc
mkP_ SDoc -> SDoc -> SDoc
<> DynFlags -> CmmExpr -> SDoc
pprExpr1 DynFlags
dflags CmmExpr
e)
| Bool
otherwise
= case CmmExpr
e of
CmmReg CmmReg
r | CmmReg -> Bool
isPtrReg CmmReg
r Bool -> Bool -> Bool
&& Width
width Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Platform -> Width
wordWidth Platform
platform Bool -> Bool -> Bool
&& Bool -> Bool
not (CmmType -> Bool
isFloatType CmmType
ty)
-> Char -> SDoc
char Char
'*' SDoc -> SDoc -> SDoc
<> CmmReg -> SDoc
pprAsPtrReg CmmReg
r
CmmRegOff CmmReg
r Int
0 | CmmReg -> Bool
isPtrReg CmmReg
r Bool -> Bool -> Bool
&& Width
width Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Platform -> Width
wordWidth Platform
platform Bool -> Bool -> Bool
&& Bool -> Bool
not (CmmType -> Bool
isFloatType CmmType
ty)
-> Char -> SDoc
char Char
'*' SDoc -> SDoc -> SDoc
<> CmmReg -> SDoc
pprAsPtrReg CmmReg
r
CmmRegOff CmmReg
r Int
off | CmmReg -> Bool
isPtrReg CmmReg
r Bool -> Bool -> Bool
&& Width
width Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Platform -> Width
wordWidth Platform
platform
, Int
off Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Platform -> Int
platformWordSizeInBytes Platform
platform Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& Bool -> Bool
not (CmmType -> Bool
isFloatType CmmType
ty)
-> CmmReg -> SDoc
pprAsPtrReg CmmReg
r SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
brackets (Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Int
off Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Platform -> Int
wordShift Platform
platform))
CmmExpr
_other -> DynFlags -> CmmExpr -> CmmType -> SDoc
cLoad DynFlags
dflags CmmExpr
e CmmType
ty
where
width :: Width
width = CmmType -> Width
typeWidth CmmType
ty
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
pprExpr1 :: DynFlags -> CmmExpr -> SDoc
pprExpr1 :: DynFlags -> CmmExpr -> SDoc
pprExpr1 DynFlags
dflags CmmExpr
e = case CmmExpr
e of
CmmLit CmmLit
lit -> DynFlags -> CmmLit -> SDoc
pprLit1 DynFlags
dflags CmmLit
lit
CmmReg CmmReg
_reg -> DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
e
CmmExpr
_ -> SDoc -> SDoc
parens (DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
e)
pprMachOpApp :: DynFlags -> MachOp -> [CmmExpr] -> SDoc
pprMachOpApp :: DynFlags -> MachOp -> [CmmExpr] -> SDoc
pprMachOpApp DynFlags
dflags MachOp
op [CmmExpr]
args
| MachOp -> Bool
isMulMayOfloOp MachOp
op
= String -> SDoc
text String
"mulIntMayOflo" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens ([SDoc] -> SDoc
commafy ((CmmExpr -> SDoc) -> [CmmExpr] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags) [CmmExpr]
args))
where isMulMayOfloOp :: MachOp -> Bool
isMulMayOfloOp (MO_U_MulMayOflo Width
_) = Bool
True
isMulMayOfloOp (MO_S_MulMayOflo Width
_) = Bool
True
isMulMayOfloOp MachOp
_ = Bool
False
pprMachOpApp DynFlags
dflags MachOp
mop [CmmExpr]
args
| Just SDoc
ty <- MachOp -> Maybe SDoc
machOpNeedsCast MachOp
mop
= SDoc
ty SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens (DynFlags -> MachOp -> [CmmExpr] -> SDoc
pprMachOpApp' DynFlags
dflags MachOp
mop [CmmExpr]
args)
| Bool
otherwise
= DynFlags -> MachOp -> [CmmExpr] -> SDoc
pprMachOpApp' DynFlags
dflags MachOp
mop [CmmExpr]
args
machOpNeedsCast :: MachOp -> Maybe SDoc
machOpNeedsCast :: MachOp -> Maybe SDoc
machOpNeedsCast MachOp
mop
| MachOp -> Bool
isComparisonMachOp MachOp
mop = SDoc -> Maybe SDoc
forall a. a -> Maybe a
Just SDoc
mkW_
| Bool
otherwise = Maybe SDoc
forall a. Maybe a
Nothing
pprMachOpApp' :: DynFlags -> MachOp -> [CmmExpr] -> SDoc
pprMachOpApp' :: DynFlags -> MachOp -> [CmmExpr] -> SDoc
pprMachOpApp' DynFlags
dflags MachOp
mop [CmmExpr]
args
= case [CmmExpr]
args of
[CmmExpr
x,CmmExpr
y] -> CmmExpr -> SDoc
pprArg CmmExpr
x SDoc -> SDoc -> SDoc
<+> Platform -> MachOp -> SDoc
pprMachOp_for_C Platform
platform MachOp
mop SDoc -> SDoc -> SDoc
<+> CmmExpr -> SDoc
pprArg CmmExpr
y
[CmmExpr
x] -> Platform -> MachOp -> SDoc
pprMachOp_for_C Platform
platform MachOp
mop SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens (CmmExpr -> SDoc
pprArg CmmExpr
x)
[CmmExpr]
_ -> String -> SDoc
forall a. String -> a
panic String
"PprC.pprMachOp : machop with wrong number of args"
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
pprArg :: CmmExpr -> SDoc
pprArg CmmExpr
e | MachOp -> Bool
signedOp MachOp
mop = DynFlags -> SDoc -> CmmExpr -> SDoc
cCast DynFlags
dflags (Platform -> Width -> SDoc
machRep_S_CType Platform
platform (CmmType -> Width
typeWidth (Platform -> CmmExpr -> CmmType
cmmExprType Platform
platform CmmExpr
e))) CmmExpr
e
| MachOp -> Bool
needsFCasts MachOp
mop = DynFlags -> SDoc -> CmmExpr -> SDoc
cCast DynFlags
dflags (Width -> SDoc
machRep_F_CType (CmmType -> Width
typeWidth (Platform -> CmmExpr -> CmmType
cmmExprType Platform
platform CmmExpr
e))) CmmExpr
e
| Bool
otherwise = DynFlags -> CmmExpr -> SDoc
pprExpr1 DynFlags
dflags CmmExpr
e
needsFCasts :: MachOp -> Bool
needsFCasts (MO_F_Eq Width
_) = Bool
False
needsFCasts (MO_F_Ne Width
_) = Bool
False
needsFCasts (MO_F_Neg Width
_) = Bool
True
needsFCasts (MO_F_Quot Width
_) = Bool
True
needsFCasts MachOp
mop = MachOp -> Bool
floatComparison MachOp
mop
pprLit :: DynFlags -> CmmLit -> SDoc
pprLit :: DynFlags -> CmmLit -> SDoc
pprLit DynFlags
dflags CmmLit
lit = case CmmLit
lit of
CmmInt Integer
i Width
rep -> DynFlags -> Integer -> Width -> SDoc
pprHexVal DynFlags
dflags Integer
i Width
rep
CmmFloat Rational
f Width
w -> SDoc -> SDoc
parens (Width -> SDoc
machRep_F_CType Width
w) SDoc -> SDoc -> SDoc
<> SDoc
str
where d :: Double
d = Rational -> Double
forall a. Fractional a => Rational -> a
fromRational Rational
f :: Double
str :: SDoc
str | Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
d Bool -> Bool -> Bool
&& Double
d Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
0 = String -> SDoc
text String
"-INFINITY"
| Double -> Bool
forall a. RealFloat a => a -> Bool
isInfinite Double
d = String -> SDoc
text String
"INFINITY"
| Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
d = String -> SDoc
text String
"NAN"
| Bool
otherwise = String -> SDoc
text (Double -> String
forall a. Show a => a -> String
show Double
d)
CmmVec {} -> String -> SDoc
forall a. String -> a
panic String
"PprC printing vector literal"
CmmBlock BlockId
bid -> SDoc
mkW_ SDoc -> SDoc -> SDoc
<> CLabel -> SDoc
forall a. Outputable a => a -> SDoc
pprCLabelAddr (BlockId -> CLabel
infoTblLbl BlockId
bid)
CmmLit
CmmHighStackMark -> String -> SDoc
forall a. String -> a
panic String
"PprC printing high stack mark"
CmmLabel CLabel
clbl -> SDoc
mkW_ SDoc -> SDoc -> SDoc
<> CLabel -> SDoc
forall a. Outputable a => a -> SDoc
pprCLabelAddr CLabel
clbl
CmmLabelOff CLabel
clbl Int
i -> SDoc
mkW_ SDoc -> SDoc -> SDoc
<> CLabel -> SDoc
forall a. Outputable a => a -> SDoc
pprCLabelAddr CLabel
clbl SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
'+' SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
i
CmmLabelDiffOff CLabel
clbl1 CLabel
_ Int
i Width
_
-> SDoc
mkW_ SDoc -> SDoc -> SDoc
<> CLabel -> SDoc
forall a. Outputable a => a -> SDoc
pprCLabelAddr CLabel
clbl1 SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
'+' SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
i
where
pprCLabelAddr :: a -> SDoc
pprCLabelAddr a
lbl = Char -> SDoc
char Char
'&' SDoc -> SDoc -> SDoc
<> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
lbl
pprLit1 :: DynFlags -> CmmLit -> SDoc
pprLit1 :: DynFlags -> CmmLit -> SDoc
pprLit1 DynFlags
dflags CmmLit
lit = case CmmLit
lit of
(CmmLabelOff CLabel
_ Int
_) -> SDoc -> SDoc
parens (DynFlags -> CmmLit -> SDoc
pprLit DynFlags
dflags CmmLit
lit)
(CmmLabelDiffOff CLabel
_ CLabel
_ Int
_ Width
_) -> SDoc -> SDoc
parens (DynFlags -> CmmLit -> SDoc
pprLit DynFlags
dflags CmmLit
lit)
(CmmFloat Rational
_ Width
_) -> SDoc -> SDoc
parens (DynFlags -> CmmLit -> SDoc
pprLit DynFlags
dflags CmmLit
lit)
CmmLit
_ -> DynFlags -> CmmLit -> SDoc
pprLit DynFlags
dflags CmmLit
lit
pprStatics :: DynFlags -> [CmmStatic] -> [SDoc]
pprStatics :: DynFlags -> [CmmStatic] -> [SDoc]
pprStatics DynFlags
dflags = [CmmStatic] -> [SDoc]
pprStatics'
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
pprStatics' :: [CmmStatic] -> [SDoc]
pprStatics' = \case
[] -> []
(CmmStaticLit (CmmFloat Rational
f Width
W32) : [CmmStatic]
rest)
| Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W64, CmmStaticLit (CmmInt Integer
0 Width
W32) : [CmmStatic]
rest' <- [CmmStatic]
rest
-> DynFlags -> CmmLit -> SDoc
pprLit1 DynFlags
dflags (Platform -> Rational -> CmmLit
floatToWord Platform
platform Rational
f) SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
: [CmmStatic] -> [SDoc]
pprStatics' [CmmStatic]
rest'
| Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W64, CmmStaticLit (CmmFloat Rational
g Width
W32) : [CmmStatic]
rest' <- [CmmStatic]
rest
-> DynFlags -> CmmLit -> SDoc
pprLit1 DynFlags
dflags (Platform -> Rational -> Rational -> CmmLit
floatPairToWord Platform
platform Rational
f Rational
g) SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
: [CmmStatic] -> [SDoc]
pprStatics' [CmmStatic]
rest'
| Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W32
-> DynFlags -> CmmLit -> SDoc
pprLit1 DynFlags
dflags (Platform -> Rational -> CmmLit
floatToWord Platform
platform Rational
f) SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
: [CmmStatic] -> [SDoc]
pprStatics' [CmmStatic]
rest
| Bool
otherwise
-> String -> SDoc -> [SDoc]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"pprStatics: float" ([SDoc] -> SDoc
vcat ((CmmStatic -> SDoc) -> [CmmStatic] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map CmmStatic -> SDoc
ppr' [CmmStatic]
rest))
where ppr' :: CmmStatic -> SDoc
ppr' (CmmStaticLit CmmLit
l) = CmmType -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Platform -> CmmLit -> CmmType
cmmLitType Platform
platform CmmLit
l)
ppr' CmmStatic
_other = String -> SDoc
text String
"bad static!"
(CmmStaticLit (CmmFloat Rational
f Width
W64) : [CmmStatic]
rest)
-> (CmmLit -> SDoc) -> [CmmLit] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (DynFlags -> CmmLit -> SDoc
pprLit1 DynFlags
dflags) (Platform -> Rational -> [CmmLit]
doubleToWords Platform
platform Rational
f) [SDoc] -> [SDoc] -> [SDoc]
forall a. [a] -> [a] -> [a]
++ [CmmStatic] -> [SDoc]
pprStatics' [CmmStatic]
rest
(CmmStaticLit (CmmInt Integer
i Width
W64) : [CmmStatic]
rest)
| Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W32
-> case Platform -> ByteOrder
platformByteOrder Platform
platform of
ByteOrder
BigEndian -> [CmmStatic] -> [SDoc]
pprStatics' (CmmLit -> CmmStatic
CmmStaticLit (Integer -> Width -> CmmLit
CmmInt Integer
q Width
W32) CmmStatic -> [CmmStatic] -> [CmmStatic]
forall a. a -> [a] -> [a]
:
CmmLit -> CmmStatic
CmmStaticLit (Integer -> Width -> CmmLit
CmmInt Integer
r Width
W32) CmmStatic -> [CmmStatic] -> [CmmStatic]
forall a. a -> [a] -> [a]
: [CmmStatic]
rest)
ByteOrder
LittleEndian -> [CmmStatic] -> [SDoc]
pprStatics' (CmmLit -> CmmStatic
CmmStaticLit (Integer -> Width -> CmmLit
CmmInt Integer
r Width
W32) CmmStatic -> [CmmStatic] -> [CmmStatic]
forall a. a -> [a] -> [a]
:
CmmLit -> CmmStatic
CmmStaticLit (Integer -> Width -> CmmLit
CmmInt Integer
q Width
W32) CmmStatic -> [CmmStatic] -> [CmmStatic]
forall a. a -> [a] -> [a]
: [CmmStatic]
rest)
where r :: Integer
r = Integer
i Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. Integer
0xffffffff
q :: Integer
q = Integer
i Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftR` Int
32
(CmmStaticLit (CmmInt Integer
a Width
W32) : CmmStaticLit (CmmInt Integer
b Width
W32) : [CmmStatic]
rest)
| Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W64
-> case Platform -> ByteOrder
platformByteOrder Platform
platform of
ByteOrder
BigEndian -> [CmmStatic] -> [SDoc]
pprStatics' (CmmLit -> CmmStatic
CmmStaticLit (Integer -> Width -> CmmLit
CmmInt ((Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
shiftL Integer
a Int
32) Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
b) Width
W64) CmmStatic -> [CmmStatic] -> [CmmStatic]
forall a. a -> [a] -> [a]
: [CmmStatic]
rest)
ByteOrder
LittleEndian -> [CmmStatic] -> [SDoc]
pprStatics' (CmmLit -> CmmStatic
CmmStaticLit (Integer -> Width -> CmmLit
CmmInt ((Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
shiftL Integer
b Int
32) Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
a) Width
W64) CmmStatic -> [CmmStatic] -> [CmmStatic]
forall a. a -> [a] -> [a]
: [CmmStatic]
rest)
(CmmStaticLit (CmmInt Integer
a Width
W16) : CmmStaticLit (CmmInt Integer
b Width
W16) : [CmmStatic]
rest)
| Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W32
-> case Platform -> ByteOrder
platformByteOrder Platform
platform of
ByteOrder
BigEndian -> [CmmStatic] -> [SDoc]
pprStatics' (CmmLit -> CmmStatic
CmmStaticLit (Integer -> Width -> CmmLit
CmmInt ((Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
shiftL Integer
a Int
16) Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
b) Width
W32) CmmStatic -> [CmmStatic] -> [CmmStatic]
forall a. a -> [a] -> [a]
: [CmmStatic]
rest)
ByteOrder
LittleEndian -> [CmmStatic] -> [SDoc]
pprStatics' (CmmLit -> CmmStatic
CmmStaticLit (Integer -> Width -> CmmLit
CmmInt ((Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
shiftL Integer
b Int
16) Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
a) Width
W32) CmmStatic -> [CmmStatic] -> [CmmStatic]
forall a. a -> [a] -> [a]
: [CmmStatic]
rest)
(CmmStaticLit (CmmInt Integer
_ Width
w) : [CmmStatic]
_)
| Width
w Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
/= Platform -> Width
wordWidth Platform
platform
-> String -> SDoc -> [SDoc]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"pprStatics: cannot emit a non-word-sized static literal" (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)
(CmmStaticLit CmmLit
lit : [CmmStatic]
rest)
-> DynFlags -> CmmLit -> SDoc
pprLit1 DynFlags
dflags CmmLit
lit SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
: [CmmStatic] -> [SDoc]
pprStatics' [CmmStatic]
rest
(CmmStatic
other : [CmmStatic]
_)
-> String -> SDoc -> [SDoc]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"pprStatics: other" (DynFlags -> CmmStatic -> SDoc
pprStatic DynFlags
dflags CmmStatic
other)
pprStatic :: DynFlags -> CmmStatic -> SDoc
pprStatic :: DynFlags -> CmmStatic -> SDoc
pprStatic DynFlags
dflags CmmStatic
s = case CmmStatic
s of
CmmStaticLit CmmLit
lit -> Int -> SDoc -> SDoc
nest Int
4 (DynFlags -> CmmLit -> SDoc
pprLit DynFlags
dflags CmmLit
lit)
CmmUninitialised Int
i -> Int -> SDoc -> SDoc
nest Int
4 (SDoc
mkC_ SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
brackets (Int -> SDoc
int Int
i))
CmmString ByteString
s' -> Int -> SDoc -> SDoc
nest Int
4 (SDoc
mkW_ SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens(ByteString -> SDoc
pprStringInCStyle ByteString
s'))
CmmFileEmbed {} -> String -> SDoc
forall a. String -> a
panic String
"Unexpected CmmFileEmbed literal"
pprBlockId :: BlockId -> SDoc
pprBlockId :: BlockId -> SDoc
pprBlockId BlockId
b = Char -> SDoc
char Char
'_' SDoc -> SDoc -> SDoc
<> Unique -> SDoc
forall a. Outputable a => a -> SDoc
ppr (BlockId -> Unique
forall a. Uniquable a => a -> Unique
getUnique BlockId
b)
pprMachOp_for_C :: Platform -> MachOp -> SDoc
pprMachOp_for_C :: Platform -> MachOp -> SDoc
pprMachOp_for_C Platform
platform MachOp
mop = case MachOp
mop of
MO_Add Width
_ -> Char -> SDoc
char Char
'+'
MO_Sub Width
_ -> Char -> SDoc
char Char
'-'
MO_Eq Width
_ -> String -> SDoc
text String
"=="
MO_Ne Width
_ -> String -> SDoc
text String
"!="
MO_Mul Width
_ -> Char -> SDoc
char Char
'*'
MO_S_Quot Width
_ -> Char -> SDoc
char Char
'/'
MO_S_Rem Width
_ -> Char -> SDoc
char Char
'%'
MO_S_Neg Width
_ -> Char -> SDoc
char Char
'-'
MO_U_Quot Width
_ -> Char -> SDoc
char Char
'/'
MO_U_Rem Width
_ -> Char -> SDoc
char Char
'%'
MO_F_Add Width
_ -> Char -> SDoc
char Char
'+'
MO_F_Sub Width
_ -> Char -> SDoc
char Char
'-'
MO_F_Neg Width
_ -> Char -> SDoc
char Char
'-'
MO_F_Mul Width
_ -> Char -> SDoc
char Char
'*'
MO_F_Quot Width
_ -> Char -> SDoc
char Char
'/'
MO_S_Ge Width
_ -> String -> SDoc
text String
">="
MO_S_Le Width
_ -> String -> SDoc
text String
"<="
MO_S_Gt Width
_ -> Char -> SDoc
char Char
'>'
MO_S_Lt Width
_ -> Char -> SDoc
char Char
'<'
MO_U_Ge Width
_ -> String -> SDoc
text String
">="
MO_U_Le Width
_ -> String -> SDoc
text String
"<="
MO_U_Gt Width
_ -> Char -> SDoc
char Char
'>'
MO_U_Lt Width
_ -> Char -> SDoc
char Char
'<'
MO_F_Eq Width
_ -> String -> SDoc
text String
"=="
MO_F_Ne Width
_ -> String -> SDoc
text String
"!="
MO_F_Ge Width
_ -> String -> SDoc
text String
">="
MO_F_Le Width
_ -> String -> SDoc
text String
"<="
MO_F_Gt Width
_ -> Char -> SDoc
char Char
'>'
MO_F_Lt Width
_ -> Char -> SDoc
char Char
'<'
MO_And Width
_ -> Char -> SDoc
char Char
'&'
MO_Or Width
_ -> Char -> SDoc
char Char
'|'
MO_Xor Width
_ -> Char -> SDoc
char Char
'^'
MO_Not Width
_ -> Char -> SDoc
char Char
'~'
MO_Shl Width
_ -> String -> SDoc
text String
"<<"
MO_U_Shr Width
_ -> String -> SDoc
text String
">>"
MO_S_Shr Width
_ -> String -> SDoc
text String
">>"
MO_UU_Conv Width
from Width
to | Width
from Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
to -> SDoc
empty
MO_UU_Conv Width
_from Width
to -> SDoc -> SDoc
parens (Platform -> Width -> SDoc
machRep_U_CType Platform
platform Width
to)
MO_SS_Conv Width
from Width
to | Width
from Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
to -> SDoc
empty
MO_SS_Conv Width
_from Width
to -> SDoc -> SDoc
parens (Platform -> Width -> SDoc
machRep_S_CType Platform
platform Width
to)
MO_XX_Conv Width
from Width
to | Width
from Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
to -> SDoc
empty
MO_XX_Conv Width
_from Width
to -> SDoc -> SDoc
parens (Platform -> Width -> SDoc
machRep_U_CType Platform
platform Width
to)
MO_FF_Conv Width
from Width
to | Width
from Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
to -> SDoc
empty
MO_FF_Conv Width
_from Width
to -> SDoc -> SDoc
parens (Width -> SDoc
machRep_F_CType Width
to)
MO_SF_Conv Width
_from Width
to -> SDoc -> SDoc
parens (Width -> SDoc
machRep_F_CType Width
to)
MO_FS_Conv Width
_from Width
to -> SDoc -> SDoc
parens (Platform -> Width -> SDoc
machRep_S_CType Platform
platform Width
to)
MO_S_MulMayOflo Width
_ -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_S_MulMayOflo")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_S_MulMayOflo"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_U_MulMayOflo Width
_ -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_U_MulMayOflo")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_U_MulMayOflo"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_V_Insert {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_V_Insert")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_V_Insert"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_V_Extract {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_V_Extract")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_V_Extract"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_V_Add {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_V_Add")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_V_Add"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_V_Sub {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_V_Sub")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_V_Sub"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_V_Mul {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_V_Mul")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_V_Mul"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VS_Quot {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VS_Quot")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VS_Quot"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VS_Rem {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VS_Rem")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VS_Rem"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VS_Neg {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VS_Neg")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VS_Neg"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VU_Quot {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VU_Quot")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VU_Quot"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VU_Rem {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VU_Rem")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VU_Rem"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VF_Insert {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VF_Insert")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VF_Insert"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VF_Extract {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VF_Extract")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VF_Extract"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VF_Add {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VF_Add")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VF_Add"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VF_Sub {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VF_Sub")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VF_Sub"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VF_Neg {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VF_Neg")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VF_Neg"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VF_Mul {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VF_Mul")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VF_Mul"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_VF_Quot {} -> String -> SDoc -> SDoc -> SDoc
forall a. String -> SDoc -> a -> a
pprTrace String
"offending mop:"
(String -> SDoc
text String
"MO_VF_Quot")
(String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"PprC.pprMachOp_for_C: MO_VF_Quot"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" should have been handled earlier!")
MO_AlignmentCheck {} -> String -> SDoc
forall a. String -> a
panic String
"-falignment-santisation not supported by unregisterised backend"
signedOp :: MachOp -> Bool
signedOp :: MachOp -> Bool
signedOp (MO_S_Quot Width
_) = Bool
True
signedOp (MO_S_Rem Width
_) = Bool
True
signedOp (MO_S_Neg Width
_) = Bool
True
signedOp (MO_S_Ge Width
_) = Bool
True
signedOp (MO_S_Le Width
_) = Bool
True
signedOp (MO_S_Gt Width
_) = Bool
True
signedOp (MO_S_Lt Width
_) = Bool
True
signedOp (MO_S_Shr Width
_) = Bool
True
signedOp (MO_SS_Conv Width
_ Width
_) = Bool
True
signedOp (MO_SF_Conv Width
_ Width
_) = Bool
True
signedOp MachOp
_ = Bool
False
floatComparison :: MachOp -> Bool
floatComparison :: MachOp -> Bool
floatComparison (MO_F_Eq Width
_) = Bool
True
floatComparison (MO_F_Ne Width
_) = Bool
True
floatComparison (MO_F_Ge Width
_) = Bool
True
floatComparison (MO_F_Le Width
_) = Bool
True
floatComparison (MO_F_Gt Width
_) = Bool
True
floatComparison (MO_F_Lt Width
_) = Bool
True
floatComparison MachOp
_ = Bool
False
pprCallishMachOp_for_C :: CallishMachOp -> SDoc
pprCallishMachOp_for_C :: CallishMachOp -> SDoc
pprCallishMachOp_for_C CallishMachOp
mop
= case CallishMachOp
mop of
CallishMachOp
MO_F64_Pwr -> String -> SDoc
text String
"pow"
CallishMachOp
MO_F64_Sin -> String -> SDoc
text String
"sin"
CallishMachOp
MO_F64_Cos -> String -> SDoc
text String
"cos"
CallishMachOp
MO_F64_Tan -> String -> SDoc
text String
"tan"
CallishMachOp
MO_F64_Sinh -> String -> SDoc
text String
"sinh"
CallishMachOp
MO_F64_Cosh -> String -> SDoc
text String
"cosh"
CallishMachOp
MO_F64_Tanh -> String -> SDoc
text String
"tanh"
CallishMachOp
MO_F64_Asin -> String -> SDoc
text String
"asin"
CallishMachOp
MO_F64_Acos -> String -> SDoc
text String
"acos"
CallishMachOp
MO_F64_Atanh -> String -> SDoc
text String
"atanh"
CallishMachOp
MO_F64_Asinh -> String -> SDoc
text String
"asinh"
CallishMachOp
MO_F64_Acosh -> String -> SDoc
text String
"acosh"
CallishMachOp
MO_F64_Atan -> String -> SDoc
text String
"atan"
CallishMachOp
MO_F64_Log -> String -> SDoc
text String
"log"
CallishMachOp
MO_F64_Log1P -> String -> SDoc
text String
"log1p"
CallishMachOp
MO_F64_Exp -> String -> SDoc
text String
"exp"
CallishMachOp
MO_F64_ExpM1 -> String -> SDoc
text String
"expm1"
CallishMachOp
MO_F64_Sqrt -> String -> SDoc
text String
"sqrt"
CallishMachOp
MO_F64_Fabs -> String -> SDoc
text String
"fabs"
CallishMachOp
MO_F32_Pwr -> String -> SDoc
text String
"powf"
CallishMachOp
MO_F32_Sin -> String -> SDoc
text String
"sinf"
CallishMachOp
MO_F32_Cos -> String -> SDoc
text String
"cosf"
CallishMachOp
MO_F32_Tan -> String -> SDoc
text String
"tanf"
CallishMachOp
MO_F32_Sinh -> String -> SDoc
text String
"sinhf"
CallishMachOp
MO_F32_Cosh -> String -> SDoc
text String
"coshf"
CallishMachOp
MO_F32_Tanh -> String -> SDoc
text String
"tanhf"
CallishMachOp
MO_F32_Asin -> String -> SDoc
text String
"asinf"
CallishMachOp
MO_F32_Acos -> String -> SDoc
text String
"acosf"
CallishMachOp
MO_F32_Atan -> String -> SDoc
text String
"atanf"
CallishMachOp
MO_F32_Asinh -> String -> SDoc
text String
"asinhf"
CallishMachOp
MO_F32_Acosh -> String -> SDoc
text String
"acoshf"
CallishMachOp
MO_F32_Atanh -> String -> SDoc
text String
"atanhf"
CallishMachOp
MO_F32_Log -> String -> SDoc
text String
"logf"
CallishMachOp
MO_F32_Log1P -> String -> SDoc
text String
"log1pf"
CallishMachOp
MO_F32_Exp -> String -> SDoc
text String
"expf"
CallishMachOp
MO_F32_ExpM1 -> String -> SDoc
text String
"expm1f"
CallishMachOp
MO_F32_Sqrt -> String -> SDoc
text String
"sqrtf"
CallishMachOp
MO_F32_Fabs -> String -> SDoc
text String
"fabsf"
CallishMachOp
MO_ReadBarrier -> String -> SDoc
text String
"load_load_barrier"
CallishMachOp
MO_WriteBarrier -> String -> SDoc
text String
"write_barrier"
MO_Memcpy Int
_ -> String -> SDoc
text String
"memcpy"
MO_Memset Int
_ -> String -> SDoc
text String
"memset"
MO_Memmove Int
_ -> String -> SDoc
text String
"memmove"
MO_Memcmp Int
_ -> String -> SDoc
text String
"memcmp"
(MO_BSwap Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
bSwapLabel Width
w)
(MO_BRev Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
bRevLabel Width
w)
(MO_PopCnt Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
popCntLabel Width
w)
(MO_Pext Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
pextLabel Width
w)
(MO_Pdep Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
pdepLabel Width
w)
(MO_Clz Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
clzLabel Width
w)
(MO_Ctz Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
ctzLabel Width
w)
(MO_AtomicRMW Width
w AtomicMachOp
amop) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> AtomicMachOp -> String
atomicRMWLabel Width
w AtomicMachOp
amop)
(MO_Cmpxchg Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
cmpxchgLabel Width
w)
(MO_Xchg Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
xchgLabel Width
w)
(MO_AtomicRead Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
atomicReadLabel Width
w)
(MO_AtomicWrite Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
atomicWriteLabel Width
w)
(MO_UF_Conv Width
w) -> PtrString -> SDoc
ptext (String -> PtrString
sLit (String -> PtrString) -> String -> PtrString
forall a b. (a -> b) -> a -> b
$ Width -> String
word2FloatLabel Width
w)
MO_S_Mul2 {} -> SDoc
unsupported
MO_S_QuotRem {} -> SDoc
unsupported
MO_U_QuotRem {} -> SDoc
unsupported
MO_U_QuotRem2 {} -> SDoc
unsupported
MO_Add2 {} -> SDoc
unsupported
MO_AddWordC {} -> SDoc
unsupported
MO_SubWordC {} -> SDoc
unsupported
MO_AddIntC {} -> SDoc
unsupported
MO_SubIntC {} -> SDoc
unsupported
MO_U_Mul2 {} -> SDoc
unsupported
CallishMachOp
MO_Touch -> SDoc
unsupported
(MO_Prefetch_Data Int
_ ) -> SDoc
unsupported
where unsupported :: SDoc
unsupported = String -> SDoc
forall a. String -> a
panic (String
"pprCallishMachOp_for_C: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ CallishMachOp -> String
forall a. Show a => a -> String
show CallishMachOp
mop
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" not supported!")
mkJMP_, mkFN_, mkIF_ :: SDoc -> SDoc
mkJMP_ :: SDoc -> SDoc
mkJMP_ SDoc
i = String -> SDoc
text String
"JMP_" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens SDoc
i
mkFN_ :: SDoc -> SDoc
mkFN_ SDoc
i = String -> SDoc
text String
"FN_" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens SDoc
i
mkIF_ :: SDoc -> SDoc
mkIF_ SDoc
i = String -> SDoc
text String
"IF_" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens SDoc
i
mkC_,mkW_,mkP_ :: SDoc
mkC_ :: SDoc
mkC_ = String -> SDoc
text String
"(C_)"
mkW_ :: SDoc
mkW_ = String -> SDoc
text String
"(W_)"
mkP_ :: SDoc
mkP_ = String -> SDoc
text String
"(P_)"
pprAssign :: DynFlags -> CmmReg -> CmmExpr -> SDoc
pprAssign :: DynFlags -> CmmReg -> CmmExpr -> SDoc
pprAssign DynFlags
_ CmmReg
r1 (CmmReg CmmReg
r2)
| CmmReg -> Bool
isPtrReg CmmReg
r1 Bool -> Bool -> Bool
&& CmmReg -> Bool
isPtrReg CmmReg
r2
= [SDoc] -> SDoc
hcat [ CmmReg -> SDoc
pprAsPtrReg CmmReg
r1, SDoc
equals, CmmReg -> SDoc
pprAsPtrReg CmmReg
r2, SDoc
semi ]
pprAssign DynFlags
dflags CmmReg
r1 (CmmRegOff CmmReg
r2 Int
off)
| CmmReg -> Bool
isPtrReg CmmReg
r1 Bool -> Bool -> Bool
&& CmmReg -> Bool
isPtrReg CmmReg
r2 Bool -> Bool -> Bool
&& (Int
off Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Platform -> Int
platformWordSizeInBytes Platform
platform Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)
= [SDoc] -> SDoc
hcat [ CmmReg -> SDoc
pprAsPtrReg CmmReg
r1, SDoc
equals, CmmReg -> SDoc
pprAsPtrReg CmmReg
r2, SDoc
op, Int -> SDoc
int Int
off', SDoc
semi ]
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
off1 :: Int
off1 = Int
off Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Platform -> Int
wordShift Platform
platform
(SDoc
op,Int
off') | Int
off Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 = (Char -> SDoc
char Char
'+', Int
off1)
| Bool
otherwise = (Char -> SDoc
char Char
'-', -Int
off1)
pprAssign DynFlags
dflags CmmReg
r1 CmmExpr
r2
| CmmReg -> Bool
isFixedPtrReg CmmReg
r1 = SDoc -> SDoc
mkAssign (SDoc
mkP_ SDoc -> SDoc -> SDoc
<> DynFlags -> CmmExpr -> SDoc
pprExpr1 DynFlags
dflags CmmExpr
r2)
| Just SDoc
ty <- CmmReg -> Maybe SDoc
strangeRegType CmmReg
r1 = SDoc -> SDoc
mkAssign (SDoc -> SDoc
parens SDoc
ty SDoc -> SDoc -> SDoc
<> DynFlags -> CmmExpr -> SDoc
pprExpr1 DynFlags
dflags CmmExpr
r2)
| Bool
otherwise = SDoc -> SDoc
mkAssign (DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
r2)
where mkAssign :: SDoc -> SDoc
mkAssign SDoc
x = if CmmReg
r1 CmmReg -> CmmReg -> Bool
forall a. Eq a => a -> a -> Bool
== GlobalReg -> CmmReg
CmmGlobal GlobalReg
BaseReg
then String -> SDoc
text String
"ASSIGN_BaseReg" SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens SDoc
x SDoc -> SDoc -> SDoc
<> SDoc
semi
else CmmReg -> SDoc
pprReg CmmReg
r1 SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
" = " SDoc -> SDoc -> SDoc
<> SDoc
x SDoc -> SDoc -> SDoc
<> SDoc
semi
pprCastReg :: CmmReg -> SDoc
pprCastReg :: CmmReg -> SDoc
pprCastReg CmmReg
reg
| CmmReg -> Bool
isStrangeTypeReg CmmReg
reg = SDoc
mkW_ SDoc -> SDoc -> SDoc
<> CmmReg -> SDoc
pprReg CmmReg
reg
| Bool
otherwise = CmmReg -> SDoc
pprReg CmmReg
reg
isFixedPtrReg :: CmmReg -> Bool
isFixedPtrReg :: CmmReg -> Bool
isFixedPtrReg (CmmLocal LocalReg
_) = Bool
False
isFixedPtrReg (CmmGlobal GlobalReg
r) = GlobalReg -> Bool
isFixedPtrGlobalReg GlobalReg
r
isPtrReg :: CmmReg -> Bool
isPtrReg :: CmmReg -> Bool
isPtrReg (CmmLocal LocalReg
_) = Bool
False
isPtrReg (CmmGlobal (VanillaReg Int
_ VGcPtr
VGcPtr)) = Bool
True
isPtrReg (CmmGlobal (VanillaReg Int
_ VGcPtr
VNonGcPtr)) = Bool
False
isPtrReg (CmmGlobal GlobalReg
reg) = GlobalReg -> Bool
isFixedPtrGlobalReg GlobalReg
reg
isFixedPtrGlobalReg :: GlobalReg -> Bool
isFixedPtrGlobalReg :: GlobalReg -> Bool
isFixedPtrGlobalReg GlobalReg
Sp = Bool
True
isFixedPtrGlobalReg GlobalReg
Hp = Bool
True
isFixedPtrGlobalReg GlobalReg
HpLim = Bool
True
isFixedPtrGlobalReg GlobalReg
SpLim = Bool
True
isFixedPtrGlobalReg GlobalReg
_ = Bool
False
isStrangeTypeReg :: CmmReg -> Bool
isStrangeTypeReg :: CmmReg -> Bool
isStrangeTypeReg (CmmLocal LocalReg
_) = Bool
False
isStrangeTypeReg (CmmGlobal GlobalReg
g) = GlobalReg -> Bool
isStrangeTypeGlobal GlobalReg
g
isStrangeTypeGlobal :: GlobalReg -> Bool
isStrangeTypeGlobal :: GlobalReg -> Bool
isStrangeTypeGlobal GlobalReg
CCCS = Bool
True
isStrangeTypeGlobal GlobalReg
CurrentTSO = Bool
True
isStrangeTypeGlobal GlobalReg
CurrentNursery = Bool
True
isStrangeTypeGlobal GlobalReg
BaseReg = Bool
True
isStrangeTypeGlobal GlobalReg
r = GlobalReg -> Bool
isFixedPtrGlobalReg GlobalReg
r
strangeRegType :: CmmReg -> Maybe SDoc
strangeRegType :: CmmReg -> Maybe SDoc
strangeRegType (CmmGlobal GlobalReg
CCCS) = SDoc -> Maybe SDoc
forall a. a -> Maybe a
Just (String -> SDoc
text String
"struct CostCentreStack_ *")
strangeRegType (CmmGlobal GlobalReg
CurrentTSO) = SDoc -> Maybe SDoc
forall a. a -> Maybe a
Just (String -> SDoc
text String
"struct StgTSO_ *")
strangeRegType (CmmGlobal GlobalReg
CurrentNursery) = SDoc -> Maybe SDoc
forall a. a -> Maybe a
Just (String -> SDoc
text String
"struct bdescr_ *")
strangeRegType (CmmGlobal GlobalReg
BaseReg) = SDoc -> Maybe SDoc
forall a. a -> Maybe a
Just (String -> SDoc
text String
"struct StgRegTable_ *")
strangeRegType CmmReg
_ = Maybe SDoc
forall a. Maybe a
Nothing
pprReg :: CmmReg -> SDoc
pprReg :: CmmReg -> SDoc
pprReg CmmReg
r = case CmmReg
r of
CmmLocal LocalReg
local -> LocalReg -> SDoc
pprLocalReg LocalReg
local
CmmGlobal GlobalReg
global -> GlobalReg -> SDoc
pprGlobalReg GlobalReg
global
pprAsPtrReg :: CmmReg -> SDoc
pprAsPtrReg :: CmmReg -> SDoc
pprAsPtrReg (CmmGlobal (VanillaReg Int
n VGcPtr
gcp))
= WARN( gcp /= VGcPtr, ppr n ) char 'R' <> int n <> text ".p"
pprAsPtrReg CmmReg
other_reg = CmmReg -> SDoc
pprReg CmmReg
other_reg
pprGlobalReg :: GlobalReg -> SDoc
pprGlobalReg :: GlobalReg -> SDoc
pprGlobalReg GlobalReg
gr = case GlobalReg
gr of
VanillaReg Int
n VGcPtr
_ -> Char -> SDoc
char Char
'R' SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
n SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
".w"
FloatReg Int
n -> Char -> SDoc
char Char
'F' SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
n
DoubleReg Int
n -> Char -> SDoc
char Char
'D' SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
n
LongReg Int
n -> Char -> SDoc
char Char
'L' SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
n
GlobalReg
Sp -> String -> SDoc
text String
"Sp"
GlobalReg
SpLim -> String -> SDoc
text String
"SpLim"
GlobalReg
Hp -> String -> SDoc
text String
"Hp"
GlobalReg
HpLim -> String -> SDoc
text String
"HpLim"
GlobalReg
CCCS -> String -> SDoc
text String
"CCCS"
GlobalReg
CurrentTSO -> String -> SDoc
text String
"CurrentTSO"
GlobalReg
CurrentNursery -> String -> SDoc
text String
"CurrentNursery"
GlobalReg
HpAlloc -> String -> SDoc
text String
"HpAlloc"
GlobalReg
BaseReg -> String -> SDoc
text String
"BaseReg"
GlobalReg
EagerBlackholeInfo -> String -> SDoc
text String
"stg_EAGER_BLACKHOLE_info"
GlobalReg
GCEnter1 -> String -> SDoc
text String
"stg_gc_enter_1"
GlobalReg
GCFun -> String -> SDoc
text String
"stg_gc_fun"
GlobalReg
other -> String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"pprGlobalReg: Unsupported register: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ GlobalReg -> String
forall a. Show a => a -> String
show GlobalReg
other
pprLocalReg :: LocalReg -> SDoc
pprLocalReg :: LocalReg -> SDoc
pprLocalReg (LocalReg Unique
uniq CmmType
_) = Char -> SDoc
char Char
'_' SDoc -> SDoc -> SDoc
<> Unique -> SDoc
forall a. Outputable a => a -> SDoc
ppr Unique
uniq
pprCall :: DynFlags -> SDoc -> CCallConv -> [Hinted CmmFormal] -> [Hinted CmmActual] -> SDoc
pprCall :: DynFlags
-> SDoc
-> CCallConv
-> [(LocalReg, ForeignHint)]
-> [(CmmExpr, ForeignHint)]
-> SDoc
pprCall DynFlags
dflags SDoc
ppr_fn CCallConv
cconv [(LocalReg, ForeignHint)]
results [(CmmExpr, ForeignHint)]
args
| Bool -> Bool
not (CCallConv -> Bool
is_cishCC CCallConv
cconv)
= String -> SDoc
forall a. String -> a
panic (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ String
"pprCall: unknown calling convention"
| Bool
otherwise
=
[(LocalReg, ForeignHint)] -> SDoc -> SDoc
ppr_assign [(LocalReg, ForeignHint)]
results (SDoc
ppr_fn SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens ([SDoc] -> SDoc
commafy (((CmmExpr, ForeignHint) -> SDoc)
-> [(CmmExpr, ForeignHint)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (CmmExpr, ForeignHint) -> SDoc
pprArg [(CmmExpr, ForeignHint)]
args))) SDoc -> SDoc -> SDoc
<> SDoc
semi
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
ppr_assign :: [(LocalReg, ForeignHint)] -> SDoc -> SDoc
ppr_assign [] SDoc
rhs = SDoc
rhs
ppr_assign [(LocalReg
one,ForeignHint
hint)] SDoc
rhs
= LocalReg -> SDoc
pprLocalReg LocalReg
one SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
" = "
SDoc -> SDoc -> SDoc
<> ForeignHint -> CmmType -> SDoc
pprUnHint ForeignHint
hint (LocalReg -> CmmType
localRegType LocalReg
one) SDoc -> SDoc -> SDoc
<> SDoc
rhs
ppr_assign [(LocalReg, ForeignHint)]
_other SDoc
_rhs = String -> SDoc
forall a. String -> a
panic String
"pprCall: multiple results"
pprArg :: (CmmExpr, ForeignHint) -> SDoc
pprArg (CmmExpr
expr, ForeignHint
AddrHint)
= DynFlags -> SDoc -> CmmExpr -> SDoc
cCast DynFlags
dflags (String -> SDoc
text String
"void *") CmmExpr
expr
pprArg (CmmExpr
expr, ForeignHint
SignedHint)
= DynFlags -> SDoc -> CmmExpr -> SDoc
cCast DynFlags
dflags (Platform -> Width -> SDoc
machRep_S_CType Platform
platform (Width -> SDoc) -> Width -> SDoc
forall a b. (a -> b) -> a -> b
$ CmmType -> Width
typeWidth (CmmType -> Width) -> CmmType -> Width
forall a b. (a -> b) -> a -> b
$ Platform -> CmmExpr -> CmmType
cmmExprType Platform
platform CmmExpr
expr) CmmExpr
expr
pprArg (CmmExpr
expr, ForeignHint
_other)
= DynFlags -> CmmExpr -> SDoc
pprExpr DynFlags
dflags CmmExpr
expr
pprUnHint :: ForeignHint -> CmmType -> SDoc
pprUnHint ForeignHint
AddrHint CmmType
rep = SDoc -> SDoc
parens (Platform -> CmmType -> SDoc
machRepCType Platform
platform CmmType
rep)
pprUnHint ForeignHint
SignedHint CmmType
rep = SDoc -> SDoc
parens (Platform -> CmmType -> SDoc
machRepCType Platform
platform CmmType
rep)
pprUnHint ForeignHint
_ CmmType
_ = SDoc
empty
is_cishCC :: CCallConv -> Bool
is_cishCC :: CCallConv -> Bool
is_cishCC CCallConv
CCallConv = Bool
True
is_cishCC CCallConv
CApiConv = Bool
True
is_cishCC CCallConv
StdCallConv = Bool
True
is_cishCC CCallConv
PrimCallConv = Bool
False
is_cishCC CCallConv
JavaScriptCallConv = Bool
False
pprTempAndExternDecls :: Platform -> [CmmBlock] -> (SDoc, SDoc)
pprTempAndExternDecls :: Platform -> [CmmBlock] -> (SDoc, SDoc)
pprTempAndExternDecls Platform
platform [CmmBlock]
stmts
= (UniqFM LocalReg LocalReg -> ([LocalReg] -> SDoc) -> SDoc
forall key a. UniqFM key a -> ([a] -> SDoc) -> SDoc
pprUFM (UniqSet LocalReg -> UniqFM LocalReg LocalReg
forall a. UniqSet a -> UniqFM a a
getUniqSet UniqSet LocalReg
temps) ([SDoc] -> SDoc
vcat ([SDoc] -> SDoc) -> ([LocalReg] -> [SDoc]) -> [LocalReg] -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LocalReg -> SDoc) -> [LocalReg] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (Platform -> LocalReg -> SDoc
pprTempDecl Platform
platform)),
[SDoc] -> SDoc
vcat ((CLabel -> SDoc) -> [CLabel] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (Platform -> CLabel -> SDoc
pprExternDecl Platform
platform) (Map CLabel () -> [CLabel]
forall k a. Map k a -> [k]
Map.keys Map CLabel ()
lbls)))
where (UniqSet LocalReg
temps, Map CLabel ()
lbls) = TE () -> (UniqSet LocalReg, Map CLabel ())
runTE ((CmmBlock -> TE ()) -> [CmmBlock] -> TE ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CmmBlock -> TE ()
te_BB [CmmBlock]
stmts)
pprDataExterns :: Platform -> [CmmStatic] -> SDoc
pprDataExterns :: Platform -> [CmmStatic] -> SDoc
pprDataExterns Platform
platform [CmmStatic]
statics
= [SDoc] -> SDoc
vcat ((CLabel -> SDoc) -> [CLabel] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (Platform -> CLabel -> SDoc
pprExternDecl Platform
platform) (Map CLabel () -> [CLabel]
forall k a. Map k a -> [k]
Map.keys Map CLabel ()
lbls))
where (UniqSet LocalReg
_, Map CLabel ()
lbls) = TE () -> (UniqSet LocalReg, Map CLabel ())
runTE ((CmmStatic -> TE ()) -> [CmmStatic] -> TE ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CmmStatic -> TE ()
te_Static [CmmStatic]
statics)
pprTempDecl :: Platform -> LocalReg -> SDoc
pprTempDecl :: Platform -> LocalReg -> SDoc
pprTempDecl Platform
platform l :: LocalReg
l@(LocalReg Unique
_ CmmType
rep)
= [SDoc] -> SDoc
hcat [ Platform -> CmmType -> SDoc
machRepCType Platform
platform CmmType
rep, SDoc
space, LocalReg -> SDoc
pprLocalReg LocalReg
l, SDoc
semi ]
pprExternDecl :: Platform -> CLabel -> SDoc
pprExternDecl :: Platform -> CLabel -> SDoc
pprExternDecl Platform
platform CLabel
lbl
| Bool -> Bool
not (CLabel -> Bool
needsCDecl CLabel
lbl) = SDoc
empty
| Just Int
sz <- CLabel -> Maybe Int
foreignLabelStdcallInfo CLabel
lbl = Int -> SDoc
stdcall_decl Int
sz
| Bool
otherwise =
[SDoc] -> SDoc
hcat [ SDoc
visibility, CLabel -> SDoc
label_type CLabel
lbl , SDoc
lparen, CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr CLabel
lbl, String -> SDoc
text String
");"
]
where
label_type :: CLabel -> SDoc
label_type CLabel
lbl | CLabel -> Bool
isBytesLabel CLabel
lbl = String -> SDoc
text String
"B_"
| CLabel -> Bool
isForeignLabel CLabel
lbl Bool -> Bool -> Bool
&& CLabel -> Bool
isCFunctionLabel CLabel
lbl
= String -> SDoc
text String
"FF_"
| CLabel -> Bool
isCFunctionLabel CLabel
lbl = String -> SDoc
text String
"F_"
| CLabel -> Bool
isStaticClosureLabel CLabel
lbl = String -> SDoc
text String
"C_"
| CLabel -> Bool
isSomeRODataLabel CLabel
lbl = String -> SDoc
text String
"RO_"
| Bool
otherwise = String -> SDoc
text String
"RW_"
visibility :: SDoc
visibility
| CLabel -> Bool
externallyVisibleCLabel CLabel
lbl = Char -> SDoc
char Char
'E'
| Bool
otherwise = Char -> SDoc
char Char
'I'
stdcall_decl :: Int -> SDoc
stdcall_decl Int
sz =
String -> SDoc
text String
"extern __attribute__((stdcall)) void " SDoc -> SDoc -> SDoc
<> CLabel -> SDoc
forall a. Outputable a => a -> SDoc
ppr CLabel
lbl
SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens ([SDoc] -> SDoc
commafy (Int -> SDoc -> [SDoc]
forall a. Int -> a -> [a]
replicate (Int
sz Int -> Int -> Int
forall a. Integral a => a -> a -> a
`quot` Platform -> Int
platformWordSizeInBytes Platform
platform) (Platform -> Width -> SDoc
machRep_U_CType Platform
platform (Platform -> Width
wordWidth Platform
platform))))
SDoc -> SDoc -> SDoc
<> SDoc
semi
type TEState = (UniqSet LocalReg, Map CLabel ())
newtype TE a = TE { forall a.
TE a
-> (UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ()))
unTE :: TEState -> (a, TEState) } deriving ((forall a b. (a -> b) -> TE a -> TE b)
-> (forall a b. a -> TE b -> TE a) -> Functor TE
forall a b. a -> TE b -> TE a
forall a b. (a -> b) -> TE a -> TE b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> TE b -> TE a
$c<$ :: forall a b. a -> TE b -> TE a
fmap :: forall a b. (a -> b) -> TE a -> TE b
$cfmap :: forall a b. (a -> b) -> TE a -> TE b
Functor)
instance Applicative TE where
pure :: forall a. a -> TE a
pure a
a = ((UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ())))
-> TE a
forall a.
((UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ())))
-> TE a
TE (((UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ())))
-> TE a)
-> ((UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ())))
-> TE a
forall a b. (a -> b) -> a -> b
$ \(UniqSet LocalReg, Map CLabel ())
s -> (a
a, (UniqSet LocalReg, Map CLabel ())
s)
<*> :: forall a b. TE (a -> b) -> TE a -> TE b
(<*>) = TE (a -> b) -> TE a -> TE b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance Monad TE where
TE (UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ()))
m >>= :: forall a b. TE a -> (a -> TE b) -> TE b
>>= a -> TE b
k = ((UniqSet LocalReg, Map CLabel ())
-> (b, (UniqSet LocalReg, Map CLabel ())))
-> TE b
forall a.
((UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ())))
-> TE a
TE (((UniqSet LocalReg, Map CLabel ())
-> (b, (UniqSet LocalReg, Map CLabel ())))
-> TE b)
-> ((UniqSet LocalReg, Map CLabel ())
-> (b, (UniqSet LocalReg, Map CLabel ())))
-> TE b
forall a b. (a -> b) -> a -> b
$ \(UniqSet LocalReg, Map CLabel ())
s -> case (UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ()))
m (UniqSet LocalReg, Map CLabel ())
s of (a
a, (UniqSet LocalReg, Map CLabel ())
s') -> TE b
-> (UniqSet LocalReg, Map CLabel ())
-> (b, (UniqSet LocalReg, Map CLabel ()))
forall a.
TE a
-> (UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ()))
unTE (a -> TE b
k a
a) (UniqSet LocalReg, Map CLabel ())
s'
te_lbl :: CLabel -> TE ()
te_lbl :: CLabel -> TE ()
te_lbl CLabel
lbl = ((UniqSet LocalReg, Map CLabel ())
-> ((), (UniqSet LocalReg, Map CLabel ())))
-> TE ()
forall a.
((UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ())))
-> TE a
TE (((UniqSet LocalReg, Map CLabel ())
-> ((), (UniqSet LocalReg, Map CLabel ())))
-> TE ())
-> ((UniqSet LocalReg, Map CLabel ())
-> ((), (UniqSet LocalReg, Map CLabel ())))
-> TE ()
forall a b. (a -> b) -> a -> b
$ \(UniqSet LocalReg
temps,Map CLabel ()
lbls) -> ((), (UniqSet LocalReg
temps, CLabel -> () -> Map CLabel () -> Map CLabel ()
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert CLabel
lbl () Map CLabel ()
lbls))
te_temp :: LocalReg -> TE ()
te_temp :: LocalReg -> TE ()
te_temp LocalReg
r = ((UniqSet LocalReg, Map CLabel ())
-> ((), (UniqSet LocalReg, Map CLabel ())))
-> TE ()
forall a.
((UniqSet LocalReg, Map CLabel ())
-> (a, (UniqSet LocalReg, Map CLabel ())))
-> TE a
TE (((UniqSet LocalReg, Map CLabel ())
-> ((), (UniqSet LocalReg, Map CLabel ())))
-> TE ())
-> ((UniqSet LocalReg, Map CLabel ())
-> ((), (UniqSet LocalReg, Map CLabel ())))
-> TE ()
forall a b. (a -> b) -> a -> b
$ \(UniqSet LocalReg
temps,Map CLabel ()
lbls) -> ((), (UniqSet LocalReg -> LocalReg -> UniqSet LocalReg
forall a. Uniquable a => UniqSet a -> a -> UniqSet a
addOneToUniqSet UniqSet LocalReg
temps LocalReg
r, Map CLabel ()
lbls))
runTE :: TE () -> TEState
runTE :: TE () -> (UniqSet LocalReg, Map CLabel ())
runTE (TE (UniqSet LocalReg, Map CLabel ())
-> ((), (UniqSet LocalReg, Map CLabel ()))
m) = ((), (UniqSet LocalReg, Map CLabel ()))
-> (UniqSet LocalReg, Map CLabel ())
forall a b. (a, b) -> b
snd ((UniqSet LocalReg, Map CLabel ())
-> ((), (UniqSet LocalReg, Map CLabel ()))
m (UniqSet LocalReg
forall a. UniqSet a
emptyUniqSet, Map CLabel ()
forall k a. Map k a
Map.empty))
te_Static :: CmmStatic -> TE ()
te_Static :: CmmStatic -> TE ()
te_Static (CmmStaticLit CmmLit
lit) = CmmLit -> TE ()
te_Lit CmmLit
lit
te_Static CmmStatic
_ = () -> TE ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
te_BB :: CmmBlock -> TE ()
te_BB :: CmmBlock -> TE ()
te_BB CmmBlock
block = (CmmNode O O -> TE ()) -> [CmmNode O O] -> TE ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CmmNode O O -> TE ()
forall (e :: Extensibility) (x :: Extensibility).
CmmNode e x -> TE ()
te_Stmt (Block CmmNode O O -> [CmmNode O O]
forall (n :: Extensibility -> Extensibility -> *).
Block n O O -> [n O O]
blockToList Block CmmNode O O
mid) TE () -> TE () -> TE ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CmmNode O C -> TE ()
forall (e :: Extensibility) (x :: Extensibility).
CmmNode e x -> TE ()
te_Stmt CmmNode O C
last
where (CmmNode C O
_, Block CmmNode O O
mid, CmmNode O C
last) = CmmBlock -> (CmmNode C O, Block CmmNode O O, CmmNode O C)
forall (n :: Extensibility -> Extensibility -> *).
Block n C C -> (n C O, Block n O O, n O C)
blockSplit CmmBlock
block
te_Lit :: CmmLit -> TE ()
te_Lit :: CmmLit -> TE ()
te_Lit (CmmLabel CLabel
l) = CLabel -> TE ()
te_lbl CLabel
l
te_Lit (CmmLabelOff CLabel
l Int
_) = CLabel -> TE ()
te_lbl CLabel
l
te_Lit (CmmLabelDiffOff CLabel
l1 CLabel
_ Int
_ Width
_) = CLabel -> TE ()
te_lbl CLabel
l1
te_Lit CmmLit
_ = () -> TE ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
te_Stmt :: CmmNode e x -> TE ()
te_Stmt :: forall (e :: Extensibility) (x :: Extensibility).
CmmNode e x -> TE ()
te_Stmt (CmmAssign CmmReg
r CmmExpr
e) = CmmReg -> TE ()
te_Reg CmmReg
r TE () -> TE () -> TE ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CmmExpr -> TE ()
te_Expr CmmExpr
e
te_Stmt (CmmStore CmmExpr
l CmmExpr
r) = CmmExpr -> TE ()
te_Expr CmmExpr
l TE () -> TE () -> TE ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> CmmExpr -> TE ()
te_Expr CmmExpr
r
te_Stmt (CmmUnsafeForeignCall ForeignTarget
target [LocalReg]
rs [CmmExpr]
es)
= do ForeignTarget -> TE ()
te_Target ForeignTarget
target
(LocalReg -> TE ()) -> [LocalReg] -> TE ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ LocalReg -> TE ()
te_temp [LocalReg]
rs
(CmmExpr -> TE ()) -> [CmmExpr] -> TE ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CmmExpr -> TE ()
te_Expr [CmmExpr]
es
te_Stmt (CmmCondBranch CmmExpr
e BlockId
_ BlockId
_ Maybe Bool
_) = CmmExpr -> TE ()
te_Expr CmmExpr
e
te_Stmt (CmmSwitch CmmExpr
e SwitchTargets
_) = CmmExpr -> TE ()
te_Expr CmmExpr
e
te_Stmt (CmmCall { cml_target :: CmmNode O C -> CmmExpr
cml_target = CmmExpr
e }) = CmmExpr -> TE ()
te_Expr CmmExpr
e
te_Stmt CmmNode e x
_ = () -> TE ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
te_Target :: ForeignTarget -> TE ()
te_Target :: ForeignTarget -> TE ()
te_Target (ForeignTarget CmmExpr
e ForeignConvention
_) = CmmExpr -> TE ()
te_Expr CmmExpr
e
te_Target (PrimTarget{}) = () -> TE ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
te_Expr :: CmmExpr -> TE ()
te_Expr :: CmmExpr -> TE ()
te_Expr (CmmLit CmmLit
lit) = CmmLit -> TE ()
te_Lit CmmLit
lit
te_Expr (CmmLoad CmmExpr
e CmmType
_) = CmmExpr -> TE ()
te_Expr CmmExpr
e
te_Expr (CmmReg CmmReg
r) = CmmReg -> TE ()
te_Reg CmmReg
r
te_Expr (CmmMachOp MachOp
_ [CmmExpr]
es) = (CmmExpr -> TE ()) -> [CmmExpr] -> TE ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ CmmExpr -> TE ()
te_Expr [CmmExpr]
es
te_Expr (CmmRegOff CmmReg
r Int
_) = CmmReg -> TE ()
te_Reg CmmReg
r
te_Expr (CmmStackSlot Area
_ Int
_) = String -> TE ()
forall a. String -> a
panic String
"te_Expr: CmmStackSlot not supported!"
te_Reg :: CmmReg -> TE ()
te_Reg :: CmmReg -> TE ()
te_Reg (CmmLocal LocalReg
l) = LocalReg -> TE ()
te_temp LocalReg
l
te_Reg CmmReg
_ = () -> TE ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
cCast :: DynFlags -> SDoc -> CmmExpr -> SDoc
cCast :: DynFlags -> SDoc -> CmmExpr -> SDoc
cCast DynFlags
dflags SDoc
ty CmmExpr
expr = SDoc -> SDoc
parens SDoc
ty SDoc -> SDoc -> SDoc
<> DynFlags -> CmmExpr -> SDoc
pprExpr1 DynFlags
dflags CmmExpr
expr
cLoad :: DynFlags -> CmmExpr -> CmmType -> SDoc
cLoad :: DynFlags -> CmmExpr -> CmmType -> SDoc
cLoad DynFlags
dflags CmmExpr
expr CmmType
rep
= if Arch -> Bool
bewareLoadStoreAlignment (Platform -> Arch
platformArch Platform
platform)
then let decl :: SDoc
decl = Platform -> CmmType -> SDoc
machRepCType Platform
platform CmmType
rep SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"x" SDoc -> SDoc -> SDoc
<> SDoc
semi
struct :: SDoc
struct = String -> SDoc
text String
"struct" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
braces (SDoc
decl)
packed_attr :: SDoc
packed_attr = String -> SDoc
text String
"__attribute__((packed))"
cast :: SDoc
cast = SDoc -> SDoc
parens (SDoc
struct SDoc -> SDoc -> SDoc
<+> SDoc
packed_attr SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
'*')
in SDoc -> SDoc
parens (SDoc
cast SDoc -> SDoc -> SDoc
<+> DynFlags -> CmmExpr -> SDoc
pprExpr1 DynFlags
dflags CmmExpr
expr) SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"->x"
else Char -> SDoc
char Char
'*' SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens (DynFlags -> SDoc -> CmmExpr -> SDoc
cCast DynFlags
dflags (Platform -> CmmType -> SDoc
machRepPtrCType Platform
platform CmmType
rep) CmmExpr
expr)
where
bewareLoadStoreAlignment :: Arch -> Bool
bewareLoadStoreAlignment Arch
ArchAlpha = Bool
True
bewareLoadStoreAlignment Arch
ArchMipseb = Bool
True
bewareLoadStoreAlignment Arch
ArchMipsel = Bool
True
bewareLoadStoreAlignment (ArchARM {}) = Bool
True
bewareLoadStoreAlignment Arch
ArchAArch64 = Bool
True
bewareLoadStoreAlignment Arch
ArchSPARC = Bool
True
bewareLoadStoreAlignment Arch
ArchSPARC64 = Bool
True
bewareLoadStoreAlignment Arch
ArchUnknown = Bool
True
bewareLoadStoreAlignment Arch
_ = Bool
False
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
isCmmWordType :: Platform -> CmmType -> Bool
isCmmWordType :: Platform -> CmmType -> Bool
isCmmWordType Platform
platform CmmType
ty = Bool -> Bool
not (CmmType -> Bool
isFloatType CmmType
ty)
Bool -> Bool -> Bool
&& CmmType -> Width
typeWidth CmmType
ty Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Platform -> Width
wordWidth Platform
platform
machRepHintCType :: Platform -> CmmType -> ForeignHint -> SDoc
machRepHintCType :: Platform -> CmmType -> ForeignHint -> SDoc
machRepHintCType Platform
platform CmmType
rep = \case
ForeignHint
AddrHint -> String -> SDoc
text String
"void *"
ForeignHint
SignedHint -> Platform -> Width -> SDoc
machRep_S_CType Platform
platform (CmmType -> Width
typeWidth CmmType
rep)
ForeignHint
_other -> Platform -> CmmType -> SDoc
machRepCType Platform
platform CmmType
rep
machRepPtrCType :: Platform -> CmmType -> SDoc
machRepPtrCType :: Platform -> CmmType -> SDoc
machRepPtrCType Platform
platform CmmType
r
= if Platform -> CmmType -> Bool
isCmmWordType Platform
platform CmmType
r
then String -> SDoc
text String
"P_"
else Platform -> CmmType -> SDoc
machRepCType Platform
platform CmmType
r SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
'*'
machRepCType :: Platform -> CmmType -> SDoc
machRepCType :: Platform -> CmmType -> SDoc
machRepCType Platform
platform CmmType
ty
| CmmType -> Bool
isFloatType CmmType
ty = Width -> SDoc
machRep_F_CType Width
w
| Bool
otherwise = Platform -> Width -> SDoc
machRep_U_CType Platform
platform Width
w
where
w :: Width
w = CmmType -> Width
typeWidth CmmType
ty
machRep_F_CType :: Width -> SDoc
machRep_F_CType :: Width -> SDoc
machRep_F_CType Width
W32 = String -> SDoc
text String
"StgFloat"
machRep_F_CType Width
W64 = String -> SDoc
text String
"StgDouble"
machRep_F_CType Width
_ = String -> SDoc
forall a. String -> a
panic String
"machRep_F_CType"
machRep_U_CType :: Platform -> Width -> SDoc
machRep_U_CType :: Platform -> Width -> SDoc
machRep_U_CType Platform
platform Width
w
= case Width
w of
Width
_ | Width
w Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Platform -> Width
wordWidth Platform
platform -> String -> SDoc
text String
"W_"
Width
W8 -> String -> SDoc
text String
"StgWord8"
Width
W16 -> String -> SDoc
text String
"StgWord16"
Width
W32 -> String -> SDoc
text String
"StgWord32"
Width
W64 -> String -> SDoc
text String
"StgWord64"
Width
_ -> String -> SDoc
forall a. String -> a
panic String
"machRep_U_CType"
machRep_S_CType :: Platform -> Width -> SDoc
machRep_S_CType :: Platform -> Width -> SDoc
machRep_S_CType Platform
platform Width
w
= case Width
w of
Width
_ | Width
w Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Platform -> Width
wordWidth Platform
platform -> String -> SDoc
text String
"I_"
Width
W8 -> String -> SDoc
text String
"StgInt8"
Width
W16 -> String -> SDoc
text String
"StgInt16"
Width
W32 -> String -> SDoc
text String
"StgInt32"
Width
W64 -> String -> SDoc
text String
"StgInt64"
Width
_ -> String -> SDoc
forall a. String -> a
panic String
"machRep_S_CType"
pprStringInCStyle :: ByteString -> SDoc
pprStringInCStyle :: ByteString -> SDoc
pprStringInCStyle ByteString
s = SDoc -> SDoc
doubleQuotes (String -> SDoc
text ((Word8 -> String) -> [Word8] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Word8 -> String
charToC (ByteString -> [Word8]
BS.unpack ByteString
s)))
castFloatToWord32Array :: STUArray s Int Float -> ST s (STUArray s Int Word32)
castFloatToWord32Array :: forall s. STUArray s Int Float -> ST s (STUArray s Int Word32)
castFloatToWord32Array = STUArray s Int Float -> ST s (STUArray s Int Word32)
forall s ix a b. STUArray s ix a -> ST s (STUArray s ix b)
U.castSTUArray
castDoubleToWord64Array :: STUArray s Int Double -> ST s (STUArray s Int Word64)
castDoubleToWord64Array :: forall s. STUArray s Int Double -> ST s (STUArray s Int Word64)
castDoubleToWord64Array = STUArray s Int Double -> ST s (STUArray s Int Word64)
forall s ix a b. STUArray s ix a -> ST s (STUArray s ix b)
U.castSTUArray
floatToWord :: Platform -> Rational -> CmmLit
floatToWord :: Platform -> Rational -> CmmLit
floatToWord Platform
platform Rational
r
= (forall s. ST s CmmLit) -> CmmLit
forall a. (forall s. ST s a) -> a
runST (do
STUArray s Int Float
arr <- (Int, Int) -> ST s (STUArray s Int Float)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ ((Int
0::Int),Int
0)
STUArray s Int Float -> Int -> Float -> ST s ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Float
arr Int
0 (Rational -> Float
forall a. Fractional a => Rational -> a
fromRational Rational
r)
STUArray s Int Word32
arr' <- STUArray s Int Float -> ST s (STUArray s Int Word32)
forall s. STUArray s Int Float -> ST s (STUArray s Int Word32)
castFloatToWord32Array STUArray s Int Float
arr
Word32
w32 <- STUArray s Int Word32 -> Int -> ST s Word32
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Word32
arr' Int
0
CmmLit -> ST s CmmLit
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> Width -> CmmLit
CmmInt (Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Word32
w32 Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftL` Int
wo) (Platform -> Width
wordWidth Platform
platform))
)
where wo :: Int
wo | Platform -> Width
wordWidth Platform
platform Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W64
, ByteOrder
BigEndian <- Platform -> ByteOrder
platformByteOrder Platform
platform
= Int
32
| Bool
otherwise
= Int
0
floatPairToWord :: Platform -> Rational -> Rational -> CmmLit
floatPairToWord :: Platform -> Rational -> Rational -> CmmLit
floatPairToWord Platform
platform Rational
r1 Rational
r2
= (forall s. ST s CmmLit) -> CmmLit
forall a. (forall s. ST s a) -> a
runST (do
STUArray s Int Float
arr <- (Int, Int) -> ST s (STUArray s Int Float)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ ((Int
0::Int),Int
1)
STUArray s Int Float -> Int -> Float -> ST s ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Float
arr Int
0 (Rational -> Float
forall a. Fractional a => Rational -> a
fromRational Rational
r1)
STUArray s Int Float -> Int -> Float -> ST s ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Float
arr Int
1 (Rational -> Float
forall a. Fractional a => Rational -> a
fromRational Rational
r2)
STUArray s Int Word32
arr' <- STUArray s Int Float -> ST s (STUArray s Int Word32)
forall s. STUArray s Int Float -> ST s (STUArray s Int Word32)
castFloatToWord32Array STUArray s Int Float
arr
Word32
w32_1 <- STUArray s Int Word32 -> Int -> ST s Word32
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Word32
arr' Int
0
Word32
w32_2 <- STUArray s Int Word32 -> Int -> ST s Word32
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Word32
arr' Int
1
CmmLit -> ST s CmmLit
forall (m :: * -> *) a. Monad m => a -> m a
return (Word32 -> Word32 -> CmmLit
pprWord32Pair Word32
w32_1 Word32
w32_2)
)
where pprWord32Pair :: Word32 -> Word32 -> CmmLit
pprWord32Pair Word32
w32_1 Word32
w32_2
| ByteOrder
BigEndian <- Platform -> ByteOrder
platformByteOrder Platform
platform =
Integer -> Width -> CmmLit
CmmInt ((Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
shiftL Integer
i1 Int
32) Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
i2) Width
W64
| Bool
otherwise =
Integer -> Width -> CmmLit
CmmInt ((Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
shiftL Integer
i2 Int
32) Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
i1) Width
W64
where i1 :: Integer
i1 = Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Word32
w32_1
i2 :: Integer
i2 = Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Word32
w32_2
doubleToWords :: Platform -> Rational -> [CmmLit]
doubleToWords :: Platform -> Rational -> [CmmLit]
doubleToWords Platform
platform Rational
r
= (forall s. ST s [CmmLit]) -> [CmmLit]
forall a. (forall s. ST s a) -> a
runST (do
STUArray s Int Double
arr <- (Int, Int) -> ST s (STUArray s Int Double)
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ ((Int
0::Int),Int
1)
STUArray s Int Double -> Int -> Double -> ST s ()
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> e -> m ()
writeArray STUArray s Int Double
arr Int
0 (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational Rational
r)
STUArray s Int Word64
arr' <- STUArray s Int Double -> ST s (STUArray s Int Word64)
forall s. STUArray s Int Double -> ST s (STUArray s Int Word64)
castDoubleToWord64Array STUArray s Int Double
arr
Word64
w64 <- STUArray s Int Word64 -> Int -> ST s Word64
forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray STUArray s Int Word64
arr' Int
0
[CmmLit] -> ST s [CmmLit]
forall (m :: * -> *) a. Monad m => a -> m a
return (Word64 -> [CmmLit]
pprWord64 Word64
w64)
)
where targetWidth :: Width
targetWidth = Platform -> Width
wordWidth Platform
platform
pprWord64 :: Word64 -> [CmmLit]
pprWord64 Word64
w64
| Width
targetWidth Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W64 =
[ Integer -> Width -> CmmLit
CmmInt (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
w64) Width
targetWidth ]
| Width
targetWidth Width -> Width -> Bool
forall a. Eq a => a -> a -> Bool
== Width
W32 =
[ Integer -> Width -> CmmLit
CmmInt (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
targetW1) Width
targetWidth
, Integer -> Width -> CmmLit
CmmInt (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
targetW2) Width
targetWidth
]
| Bool
otherwise = String -> [CmmLit]
forall a. String -> a
panic String
"doubleToWords.pprWord64"
where (Word64
targetW1, Word64
targetW2) = case Platform -> ByteOrder
platformByteOrder Platform
platform of
ByteOrder
BigEndian -> (Word64
wHi, Word64
wLo)
ByteOrder
LittleEndian -> (Word64
wLo, Word64
wHi)
wHi :: Word64
wHi = Word64
w64 Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`shiftR` Int
32
wLo :: Word64
wLo = Word64
w64 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xFFFFffff
wordShift :: Platform -> Int
wordShift :: Platform -> Int
wordShift Platform
platform = Width -> Int
widthInLog (Platform -> Width
wordWidth Platform
platform)
commafy :: [SDoc] -> SDoc
commafy :: [SDoc] -> SDoc
commafy [SDoc]
xs = [SDoc] -> SDoc
hsep ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma [SDoc]
xs
pprHexVal :: DynFlags -> Integer -> Width -> SDoc
pprHexVal :: DynFlags -> Integer -> Width -> SDoc
pprHexVal DynFlags
dflags Integer
w Width
rep
| Integer
w Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = SDoc -> SDoc
parens (Char -> SDoc
char Char
'-' SDoc -> SDoc -> SDoc
<>
String -> SDoc
text String
"0x" SDoc -> SDoc -> SDoc
<> Integer -> SDoc
intToDoc (-Integer
w) SDoc -> SDoc -> SDoc
<> Width -> SDoc
repsuffix Width
rep)
| Bool
otherwise = String -> SDoc
text String
"0x" SDoc -> SDoc -> SDoc
<> Integer -> SDoc
intToDoc Integer
w SDoc -> SDoc -> SDoc
<> Width -> SDoc
repsuffix Width
rep
where
repsuffix :: Width -> SDoc
repsuffix Width
W64 =
if DynFlags -> Int
cINT_SIZE DynFlags
dflags Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
8 then Char -> SDoc
char Char
'U'
else if DynFlags -> Int
cLONG_SIZE DynFlags
dflags Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
8 then String -> SDoc
text String
"UL"
else if DynFlags -> Int
cLONG_LONG_SIZE DynFlags
dflags Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
8 then String -> SDoc
text String
"ULL"
else String -> SDoc
forall a. String -> a
panic String
"pprHexVal: Can't find a 64-bit type"
repsuffix Width
_ = Char -> SDoc
char Char
'U'
intToDoc :: Integer -> SDoc
intToDoc :: Integer -> SDoc
intToDoc Integer
i = case Integer -> Integer
truncInt Integer
i of
Integer
0 -> Char -> SDoc
char Char
'0'
Integer
v -> Integer -> SDoc
go Integer
v
truncInt :: Integer -> Integer
truncInt :: Integer -> Integer
truncInt Integer
i =
case Width
rep of
Width
W8 -> Integer
i Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`rem` (Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
8 :: Int))
Width
W16 -> Integer
i Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`rem` (Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
16 :: Int))
Width
W32 -> Integer
i Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`rem` (Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
32 :: Int))
Width
W64 -> Integer
i Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`rem` (Integer
2Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
64 :: Int))
Width
_ -> String -> Integer
forall a. String -> a
panic (String
"pprHexVal/truncInt: C backend can't encode "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Width -> String
forall a. Show a => a -> String
show Width
rep String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" literals")
go :: Integer -> SDoc
go Integer
0 = SDoc
empty
go Integer
w' = Integer -> SDoc
go Integer
q SDoc -> SDoc -> SDoc
<> SDoc
dig
where
(Integer
q,Integer
r) = Integer
w' Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
16
dig :: SDoc
dig | Integer
r Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
10 = Char -> SDoc
char (Int -> Char
chr (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
r Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Int
ord Char
'0'))
| Bool
otherwise = Char -> SDoc
char (Int -> Char
chr (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
10 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Char -> Int
ord Char
'a'))