module PIC (
cmmMakeDynamicReference,
CmmMakeDynamicReferenceM(..),
ReferenceKind(..),
needImportedSymbols,
pprImportedSymbol,
pprGotDeclaration,
initializePicBase_ppc,
initializePicBase_x86
)
where
import GhcPrelude
import qualified PPC.Instr as PPC
import qualified PPC.Regs as PPC
import qualified X86.Instr as X86
import GHC.Platform
import Instruction
import Reg
import NCGMonad
import Hoopl.Collections
import Cmm
import CLabel ( CLabel, ForeignLabelSource(..), pprCLabel,
mkDynamicLinkerLabel, DynamicLinkerLabelInfo(..),
dynamicLinkerLabelInfo, mkPicBaseLabel,
labelDynamic, externallyVisibleCLabel )
import CLabel ( mkForeignLabel )
import BasicTypes
import Module
import Outputable
import DynFlags
import FastString
data ReferenceKind
= DataReference
| CallReference
| JumpReference
deriving(ReferenceKind -> ReferenceKind -> Bool
(ReferenceKind -> ReferenceKind -> Bool)
-> (ReferenceKind -> ReferenceKind -> Bool) -> Eq ReferenceKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ReferenceKind -> ReferenceKind -> Bool
$c/= :: ReferenceKind -> ReferenceKind -> Bool
== :: ReferenceKind -> ReferenceKind -> Bool
$c== :: ReferenceKind -> ReferenceKind -> Bool
Eq)
class Monad m => CmmMakeDynamicReferenceM m where
addImport :: CLabel -> m ()
getThisModule :: m Module
instance CmmMakeDynamicReferenceM NatM where
addImport :: CLabel -> NatM ()
addImport = CLabel -> NatM ()
addImportNat
getThisModule :: NatM Module
getThisModule = NatM Module
getThisModuleNat
cmmMakeDynamicReference
:: CmmMakeDynamicReferenceM m
=> DynFlags
-> ReferenceKind
-> CLabel
-> m CmmExpr
cmmMakeDynamicReference :: DynFlags -> ReferenceKind -> CLabel -> m CmmExpr
cmmMakeDynamicReference DynFlags
dflags ReferenceKind
referenceKind CLabel
lbl
| Just (DynamicLinkerLabelInfo, CLabel)
_ <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
lbl
= CmmExpr -> m CmmExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
forall a b. (a -> b) -> a -> b
$ CmmLit -> CmmExpr
CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
forall a b. (a -> b) -> a -> b
$ CLabel -> CmmLit
CmmLabel CLabel
lbl
| Bool
otherwise
= do Module
this_mod <- m Module
forall (m :: * -> *). CmmMakeDynamicReferenceM m => m Module
getThisModule
case DynFlags
-> Arch
-> OS
-> Module
-> ReferenceKind
-> CLabel
-> LabelAccessStyle
howToAccessLabel
DynFlags
dflags
(Platform -> Arch
platformArch (Platform -> Arch) -> Platform -> Arch
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags)
(Platform -> OS
platformOS (Platform -> OS) -> Platform -> OS
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags)
Module
this_mod
ReferenceKind
referenceKind CLabel
lbl of
LabelAccessStyle
AccessViaStub -> do
let stub :: CLabel
stub = DynamicLinkerLabelInfo -> CLabel -> CLabel
mkDynamicLinkerLabel DynamicLinkerLabelInfo
CodeStub CLabel
lbl
CLabel -> m ()
forall (m :: * -> *). CmmMakeDynamicReferenceM m => CLabel -> m ()
addImport CLabel
stub
CmmExpr -> m CmmExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
forall a b. (a -> b) -> a -> b
$ CmmLit -> CmmExpr
CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
forall a b. (a -> b) -> a -> b
$ CLabel -> CmmLit
CmmLabel CLabel
stub
LabelAccessStyle
AccessViaSymbolPtr -> do
let symbolPtr :: CLabel
symbolPtr = DynamicLinkerLabelInfo -> CLabel -> CLabel
mkDynamicLinkerLabel DynamicLinkerLabelInfo
SymbolPtr CLabel
lbl
CLabel -> m ()
forall (m :: * -> *). CmmMakeDynamicReferenceM m => CLabel -> m ()
addImport CLabel
symbolPtr
CmmExpr -> m CmmExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
forall a b. (a -> b) -> a -> b
$ CmmExpr -> CmmType -> CmmExpr
CmmLoad (DynFlags -> CLabel -> CmmExpr
cmmMakePicReference DynFlags
dflags CLabel
symbolPtr) (DynFlags -> CmmType
bWord DynFlags
dflags)
LabelAccessStyle
AccessDirectly -> case ReferenceKind
referenceKind of
ReferenceKind
DataReference -> CmmExpr -> m CmmExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
forall a b. (a -> b) -> a -> b
$ DynFlags -> CLabel -> CmmExpr
cmmMakePicReference DynFlags
dflags CLabel
lbl
ReferenceKind
_ -> CmmExpr -> m CmmExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
forall a b. (a -> b) -> a -> b
$ CmmLit -> CmmExpr
CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
forall a b. (a -> b) -> a -> b
$ CLabel -> CmmLit
CmmLabel CLabel
lbl
cmmMakePicReference :: DynFlags -> CLabel -> CmmExpr
cmmMakePicReference :: DynFlags -> CLabel -> CmmExpr
cmmMakePicReference DynFlags
dflags CLabel
lbl
| OS
OSMinGW32 <- Platform -> OS
platformOS (Platform -> OS) -> Platform -> OS
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags
= CmmLit -> CmmExpr
CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
forall a b. (a -> b) -> a -> b
$ CLabel -> CmmLit
CmmLabel CLabel
lbl
| OS
OSAIX <- Platform -> OS
platformOS (Platform -> OS) -> Platform -> OS
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags
= MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Width -> MachOp
MO_Add Width
W32)
[ CmmReg -> CmmExpr
CmmReg (GlobalReg -> CmmReg
CmmGlobal GlobalReg
PicBaseReg)
, CmmLit -> CmmExpr
CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
forall a b. (a -> b) -> a -> b
$ DynFlags -> Arch -> OS -> CLabel -> CmmLit
picRelative DynFlags
dflags
(Platform -> Arch
platformArch (Platform -> Arch) -> Platform -> Arch
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags)
(Platform -> OS
platformOS (Platform -> OS) -> Platform -> OS
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags)
CLabel
lbl ]
| ArchPPC_64 PPC_64ABI
_ <- Platform -> Arch
platformArch (Platform -> Arch) -> Platform -> Arch
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags
= MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Width -> MachOp
MO_Add Width
W32)
[ CmmReg -> CmmExpr
CmmReg (GlobalReg -> CmmReg
CmmGlobal GlobalReg
PicBaseReg)
, CmmLit -> CmmExpr
CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
forall a b. (a -> b) -> a -> b
$ DynFlags -> Arch -> OS -> CLabel -> CmmLit
picRelative DynFlags
dflags
(Platform -> Arch
platformArch (Platform -> Arch) -> Platform -> Arch
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags)
(Platform -> OS
platformOS (Platform -> OS) -> Platform -> OS
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags)
CLabel
lbl ]
| (DynFlags -> Bool
positionIndependent DynFlags
dflags Bool -> Bool -> Bool
|| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalDynamicRefs DynFlags
dflags)
Bool -> Bool -> Bool
&& CLabel -> Bool
absoluteLabel CLabel
lbl
= MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp (Width -> MachOp
MO_Add (DynFlags -> Width
wordWidth DynFlags
dflags))
[ CmmReg -> CmmExpr
CmmReg (GlobalReg -> CmmReg
CmmGlobal GlobalReg
PicBaseReg)
, CmmLit -> CmmExpr
CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
forall a b. (a -> b) -> a -> b
$ DynFlags -> Arch -> OS -> CLabel -> CmmLit
picRelative DynFlags
dflags
(Platform -> Arch
platformArch (Platform -> Arch) -> Platform -> Arch
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags)
(Platform -> OS
platformOS (Platform -> OS) -> Platform -> OS
forall a b. (a -> b) -> a -> b
$ DynFlags -> Platform
targetPlatform DynFlags
dflags)
CLabel
lbl ]
| Bool
otherwise
= CmmLit -> CmmExpr
CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
forall a b. (a -> b) -> a -> b
$ CLabel -> CmmLit
CmmLabel CLabel
lbl
absoluteLabel :: CLabel -> Bool
absoluteLabel :: CLabel -> Bool
absoluteLabel CLabel
lbl
= case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
lbl of
Just (DynamicLinkerLabelInfo
GotSymbolPtr, CLabel
_) -> Bool
False
Just (DynamicLinkerLabelInfo
GotSymbolOffset, CLabel
_) -> Bool
False
Maybe (DynamicLinkerLabelInfo, CLabel)
_ -> Bool
True
data LabelAccessStyle
= AccessViaStub
| AccessViaSymbolPtr
| AccessDirectly
howToAccessLabel
:: DynFlags -> Arch -> OS -> Module -> ReferenceKind -> CLabel -> LabelAccessStyle
howToAccessLabel :: DynFlags
-> Arch
-> OS
-> Module
-> ReferenceKind
-> CLabel
-> LabelAccessStyle
howToAccessLabel DynFlags
dflags Arch
_ OS
OSMinGW32 Module
this_mod ReferenceKind
_ CLabel
lbl
| Bool -> Bool
not (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalDynamicRefs DynFlags
dflags)
= LabelAccessStyle
AccessDirectly
| DynFlags -> Module -> CLabel -> Bool
labelDynamic DynFlags
dflags Module
this_mod CLabel
lbl
= LabelAccessStyle
AccessViaSymbolPtr
| Bool
otherwise
= LabelAccessStyle
AccessDirectly
howToAccessLabel DynFlags
dflags Arch
arch OS
OSDarwin Module
this_mod ReferenceKind
DataReference CLabel
lbl
| DynFlags -> Module -> CLabel -> Bool
labelDynamic DynFlags
dflags Module
this_mod CLabel
lbl
= LabelAccessStyle
AccessViaSymbolPtr
| Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= Arch
ArchX86_64
, DynFlags -> Bool
positionIndependent DynFlags
dflags Bool -> Bool -> Bool
&& CLabel -> Bool
externallyVisibleCLabel CLabel
lbl
= LabelAccessStyle
AccessViaSymbolPtr
| Bool
otherwise
= LabelAccessStyle
AccessDirectly
howToAccessLabel DynFlags
dflags Arch
arch OS
OSDarwin Module
this_mod ReferenceKind
JumpReference CLabel
lbl
| Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
== Arch
ArchX86 Bool -> Bool -> Bool
|| Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
== Arch
ArchX86_64
, DynFlags -> Module -> CLabel -> Bool
labelDynamic DynFlags
dflags Module
this_mod CLabel
lbl
= LabelAccessStyle
AccessViaSymbolPtr
howToAccessLabel DynFlags
dflags Arch
arch OS
OSDarwin Module
this_mod ReferenceKind
_ CLabel
lbl
| Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= Arch
ArchX86_64
, DynFlags -> Module -> CLabel -> Bool
labelDynamic DynFlags
dflags Module
this_mod CLabel
lbl
= LabelAccessStyle
AccessViaStub
| Bool
otherwise
= LabelAccessStyle
AccessDirectly
howToAccessLabel DynFlags
_dflags Arch
_arch OS
OSAIX Module
_this_mod ReferenceKind
kind CLabel
_lbl
= case ReferenceKind
kind of
ReferenceKind
DataReference -> LabelAccessStyle
AccessViaSymbolPtr
ReferenceKind
CallReference -> LabelAccessStyle
AccessDirectly
ReferenceKind
JumpReference -> LabelAccessStyle
AccessDirectly
howToAccessLabel DynFlags
_ (ArchPPC_64 PPC_64ABI
_) OS
os Module
_ ReferenceKind
kind CLabel
_
| OS -> Bool
osElfTarget OS
os
= case ReferenceKind
kind of
ReferenceKind
DataReference -> LabelAccessStyle
AccessViaSymbolPtr
ReferenceKind
JumpReference -> LabelAccessStyle
AccessViaSymbolPtr
ReferenceKind
_ -> LabelAccessStyle
AccessDirectly
howToAccessLabel DynFlags
dflags Arch
_ OS
os Module
_ ReferenceKind
_ CLabel
_
| OS -> Bool
osElfTarget OS
os
, Bool -> Bool
not (DynFlags -> Bool
positionIndependent DynFlags
dflags) Bool -> Bool -> Bool
&&
Bool -> Bool
not (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalDynamicRefs DynFlags
dflags)
= LabelAccessStyle
AccessDirectly
howToAccessLabel DynFlags
dflags Arch
arch OS
os Module
this_mod ReferenceKind
DataReference CLabel
lbl
| OS -> Bool
osElfTarget OS
os
= case () of
()
_ | DynFlags -> Module -> CLabel -> Bool
labelDynamic DynFlags
dflags Module
this_mod CLabel
lbl
-> LabelAccessStyle
AccessViaSymbolPtr
| Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
== Arch
ArchPPC
, DynFlags -> Bool
positionIndependent DynFlags
dflags
-> LabelAccessStyle
AccessViaSymbolPtr
| Bool
otherwise
-> LabelAccessStyle
AccessDirectly
howToAccessLabel DynFlags
dflags Arch
arch OS
os Module
this_mod ReferenceKind
CallReference CLabel
lbl
| OS -> Bool
osElfTarget OS
os
, DynFlags -> Module -> CLabel -> Bool
labelDynamic DynFlags
dflags Module
this_mod CLabel
lbl Bool -> Bool -> Bool
&& Bool -> Bool
not (DynFlags -> Bool
positionIndependent DynFlags
dflags)
= LabelAccessStyle
AccessDirectly
| OS -> Bool
osElfTarget OS
os
, Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= Arch
ArchX86
, DynFlags -> Module -> CLabel -> Bool
labelDynamic DynFlags
dflags Module
this_mod CLabel
lbl
, DynFlags -> Bool
positionIndependent DynFlags
dflags
= LabelAccessStyle
AccessViaStub
howToAccessLabel DynFlags
dflags Arch
_ OS
os Module
this_mod ReferenceKind
_ CLabel
lbl
| OS -> Bool
osElfTarget OS
os
= if DynFlags -> Module -> CLabel -> Bool
labelDynamic DynFlags
dflags Module
this_mod CLabel
lbl
then LabelAccessStyle
AccessViaSymbolPtr
else LabelAccessStyle
AccessDirectly
howToAccessLabel DynFlags
dflags Arch
_ OS
_ Module
_ ReferenceKind
_ CLabel
_
| Bool -> Bool
not (DynFlags -> Bool
positionIndependent DynFlags
dflags)
= LabelAccessStyle
AccessDirectly
| Bool
otherwise
= String -> LabelAccessStyle
forall a. String -> a
panic String
"howToAccessLabel: PIC not defined for this platform"
picRelative :: DynFlags -> Arch -> OS -> CLabel -> CmmLit
picRelative :: DynFlags -> Arch -> OS -> CLabel -> CmmLit
picRelative DynFlags
dflags Arch
arch OS
OSDarwin CLabel
lbl
| Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= Arch
ArchX86_64
= CLabel -> CLabel -> Int -> Width -> CmmLit
CmmLabelDiffOff CLabel
lbl CLabel
mkPicBaseLabel Int
0 (DynFlags -> Width
wordWidth DynFlags
dflags)
picRelative DynFlags
dflags Arch
_ OS
OSAIX CLabel
lbl
= CLabel -> CLabel -> Int -> Width -> CmmLit
CmmLabelDiffOff CLabel
lbl CLabel
gotLabel Int
0 (DynFlags -> Width
wordWidth DynFlags
dflags)
picRelative DynFlags
dflags Arch
ArchPPC OS
os CLabel
lbl
| OS -> Bool
osElfTarget OS
os
= CLabel -> CLabel -> Int -> Width -> CmmLit
CmmLabelDiffOff CLabel
lbl CLabel
gotLabel Int
0 (DynFlags -> Width
wordWidth DynFlags
dflags)
picRelative DynFlags
_ Arch
arch OS
os CLabel
lbl
| OS -> Bool
osElfTarget OS
os Bool -> Bool -> Bool
|| (OS
os OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
OSDarwin Bool -> Bool -> Bool
&& Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
== Arch
ArchX86_64)
= let result :: CmmLit
result
| Just (DynamicLinkerLabelInfo
SymbolPtr, CLabel
lbl') <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
lbl
= CLabel -> CmmLit
CmmLabel (CLabel -> CmmLit) -> CLabel -> CmmLit
forall a b. (a -> b) -> a -> b
$ DynamicLinkerLabelInfo -> CLabel -> CLabel
mkDynamicLinkerLabel DynamicLinkerLabelInfo
GotSymbolPtr CLabel
lbl'
| Bool
otherwise
= CLabel -> CmmLit
CmmLabel (CLabel -> CmmLit) -> CLabel -> CmmLit
forall a b. (a -> b) -> a -> b
$ DynamicLinkerLabelInfo -> CLabel -> CLabel
mkDynamicLinkerLabel DynamicLinkerLabelInfo
GotSymbolOffset CLabel
lbl
in CmmLit
result
picRelative DynFlags
_ Arch
_ OS
_ CLabel
_
= String -> CmmLit
forall a. String -> a
panic String
"PositionIndependentCode.picRelative undefined for this platform"
needImportedSymbols :: DynFlags -> Arch -> OS -> Bool
needImportedSymbols :: DynFlags -> Arch -> OS -> Bool
needImportedSymbols DynFlags
dflags Arch
arch OS
os
| OS
os OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
OSDarwin
, Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= Arch
ArchX86_64
= Bool
True
| OS
os OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
OSAIX
= Bool
True
| OS -> Bool
osElfTarget OS
os
, Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
== Arch
ArchPPC
= DynFlags -> Bool
positionIndependent DynFlags
dflags Bool -> Bool -> Bool
|| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalDynamicRefs DynFlags
dflags
| OS -> Bool
osElfTarget OS
os
, Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
== PPC_64ABI -> Arch
ArchPPC_64 PPC_64ABI
ELF_V1 Bool -> Bool -> Bool
|| Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
== PPC_64ABI -> Arch
ArchPPC_64 PPC_64ABI
ELF_V2
= Bool
True
| OS -> Bool
osElfTarget OS
os
, Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= PPC_64ABI -> Arch
ArchPPC_64 PPC_64ABI
ELF_V1 Bool -> Bool -> Bool
&& Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= PPC_64ABI -> Arch
ArchPPC_64 PPC_64ABI
ELF_V2
= GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalDynamicRefs DynFlags
dflags Bool -> Bool -> Bool
&&
Bool -> Bool
not (DynFlags -> Bool
positionIndependent DynFlags
dflags)
| Bool
otherwise
= Bool
False
gotLabel :: CLabel
gotLabel :: CLabel
gotLabel
= FastString
-> Maybe Int -> ForeignLabelSource -> FunctionOrData -> CLabel
mkForeignLabel
(String -> FastString
fsLit String
".LCTOC1")
Maybe Int
forall a. Maybe a
Nothing ForeignLabelSource
ForeignLabelInThisPackage FunctionOrData
IsData
pprGotDeclaration :: DynFlags -> Arch -> OS -> SDoc
pprGotDeclaration :: DynFlags -> Arch -> OS -> SDoc
pprGotDeclaration DynFlags
dflags Arch
ArchX86 OS
OSDarwin
| DynFlags -> Bool
positionIndependent DynFlags
dflags
= [SDoc] -> SDoc
vcat [
String -> SDoc
text String
".section __TEXT,__textcoal_nt,coalesced,no_toc",
String -> SDoc
text String
".weak_definition ___i686.get_pc_thunk.ax",
String -> SDoc
text String
".private_extern ___i686.get_pc_thunk.ax",
String -> SDoc
text String
"___i686.get_pc_thunk.ax:",
String -> SDoc
text String
"\tmovl (%esp), %eax",
String -> SDoc
text String
"\tret" ]
pprGotDeclaration DynFlags
_ Arch
_ OS
OSDarwin
= SDoc
empty
pprGotDeclaration DynFlags
_ Arch
_ OS
OSAIX
= [SDoc] -> SDoc
vcat ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ [ String -> SDoc
text String
".toc"
, String -> SDoc
text String
".tc ghc_toc_table[TC],.LCTOC1"
, String -> SDoc
text String
".csect ghc_toc_table[RW]"
, String -> SDoc
text String
".set .LCTOC1,$+0x8000"
]
pprGotDeclaration DynFlags
_ (ArchPPC_64 PPC_64ABI
ELF_V1) OS
_
= String -> SDoc
text String
".section \".toc\",\"aw\""
pprGotDeclaration DynFlags
_ (ArchPPC_64 PPC_64ABI
ELF_V2) OS
_
= [SDoc] -> SDoc
vcat [ String -> SDoc
text String
".abiversion 2",
String -> SDoc
text String
".section \".toc\",\"aw\""
]
pprGotDeclaration DynFlags
dflags Arch
arch OS
os
| OS -> Bool
osElfTarget OS
os
, Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= PPC_64ABI -> Arch
ArchPPC_64 PPC_64ABI
ELF_V1 Bool -> Bool -> Bool
&& Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= PPC_64ABI -> Arch
ArchPPC_64 PPC_64ABI
ELF_V2
, Bool -> Bool
not (DynFlags -> Bool
positionIndependent DynFlags
dflags)
= SDoc
empty
| OS -> Bool
osElfTarget OS
os
, Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= PPC_64ABI -> Arch
ArchPPC_64 PPC_64ABI
ELF_V1 Bool -> Bool -> Bool
&& Arch
arch Arch -> Arch -> Bool
forall a. Eq a => a -> a -> Bool
/= PPC_64ABI -> Arch
ArchPPC_64 PPC_64ABI
ELF_V2
= [SDoc] -> SDoc
vcat [
String -> SDoc
text String
".section \".got2\",\"aw\"",
String -> SDoc
text String
".LCTOC1 = .+32768" ]
pprGotDeclaration DynFlags
_ Arch
_ OS
_
= String -> SDoc
forall a. String -> a
panic String
"pprGotDeclaration: no match"
pprImportedSymbol :: DynFlags -> Platform -> CLabel -> SDoc
pprImportedSymbol :: DynFlags -> Platform -> CLabel -> SDoc
pprImportedSymbol DynFlags
dflags (Platform { platformMini :: Platform -> PlatformMini
platformMini = PlatformMini { platformMini_arch :: PlatformMini -> Arch
platformMini_arch = Arch
ArchX86, platformMini_os :: PlatformMini -> OS
platformMini_os = OS
OSDarwin } }) CLabel
importedLbl
| Just (DynamicLinkerLabelInfo
CodeStub, CLabel
lbl) <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl
= case DynFlags -> Bool
positionIndependent DynFlags
dflags of
Bool
False ->
[SDoc] -> SDoc
vcat [
String -> SDoc
text String
".symbol_stub",
String -> SDoc
text String
"L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl SDoc -> SDoc -> SDoc
<> PtrString -> SDoc
ptext (String -> PtrString
sLit String
"$stub:"),
String -> SDoc
text String
"\t.indirect_symbol" SDoc -> SDoc -> SDoc
<+> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl,
String -> SDoc
text String
"\tjmp *L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"$lazy_ptr",
String -> SDoc
text String
"L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"$stub_binder:",
String -> SDoc
text String
"\tpushl $L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"$lazy_ptr",
String -> SDoc
text String
"\tjmp dyld_stub_binding_helper"
]
Bool
True ->
[SDoc] -> SDoc
vcat [
String -> SDoc
text String
".section __TEXT,__picsymbolstub2,"
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"symbol_stubs,pure_instructions,25",
String -> SDoc
text String
"L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl SDoc -> SDoc -> SDoc
<> PtrString -> SDoc
ptext (String -> PtrString
sLit String
"$stub:"),
String -> SDoc
text String
"\t.indirect_symbol" SDoc -> SDoc -> SDoc
<+> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl,
String -> SDoc
text String
"\tcall ___i686.get_pc_thunk.ax",
String -> SDoc
text String
"1:",
String -> SDoc
text String
"\tmovl L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"$lazy_ptr-1b(%eax),%edx",
String -> SDoc
text String
"\tjmp *%edx",
String -> SDoc
text String
"L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"$stub_binder:",
String -> SDoc
text String
"\tlea L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"$lazy_ptr-1b(%eax),%eax",
String -> SDoc
text String
"\tpushl %eax",
String -> SDoc
text String
"\tjmp dyld_stub_binding_helper"
]
SDoc -> SDoc -> SDoc
$+$ [SDoc] -> SDoc
vcat [ String -> SDoc
text String
".section __DATA, __la_sym_ptr"
SDoc -> SDoc -> SDoc
<> (if DynFlags -> Bool
positionIndependent DynFlags
dflags then Int -> SDoc
int Int
2 else Int -> SDoc
int Int
3)
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
",lazy_symbol_pointers",
String -> SDoc
text String
"L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl SDoc -> SDoc -> SDoc
<> PtrString -> SDoc
ptext (String -> PtrString
sLit String
"$lazy_ptr:"),
String -> SDoc
text String
"\t.indirect_symbol" SDoc -> SDoc -> SDoc
<+> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl,
String -> SDoc
text String
"\t.long L" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"$stub_binder"]
| Just (DynamicLinkerLabelInfo
SymbolPtr, CLabel
lbl) <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl
= [SDoc] -> SDoc
vcat [
String -> SDoc
text String
".non_lazy_symbol_pointer",
Char -> SDoc
char Char
'L' SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"$non_lazy_ptr:",
String -> SDoc
text String
"\t.indirect_symbol" SDoc -> SDoc -> SDoc
<+> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl,
String -> SDoc
text String
"\t.long\t0"]
| Bool
otherwise
= SDoc
empty
pprImportedSymbol DynFlags
_ (Platform { platformMini :: Platform -> PlatformMini
platformMini = PlatformMini { platformMini_os :: PlatformMini -> OS
platformMini_os = OS
OSDarwin } }) CLabel
_
= SDoc
empty
pprImportedSymbol DynFlags
dflags (Platform { platformMini :: Platform -> PlatformMini
platformMini = PlatformMini { platformMini_os :: PlatformMini -> OS
platformMini_os = OS
OSAIX } }) CLabel
importedLbl
= case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl of
Just (DynamicLinkerLabelInfo
SymbolPtr, CLabel
lbl)
-> [SDoc] -> SDoc
vcat [
String -> SDoc
text String
"LC.." SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
':',
String -> SDoc
text String
"\t.long" SDoc -> SDoc -> SDoc
<+> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl ]
Maybe (DynamicLinkerLabelInfo, CLabel)
_ -> SDoc
empty
pprImportedSymbol DynFlags
dflags platform :: Platform
platform@(Platform { platformMini :: Platform -> PlatformMini
platformMini = PlatformMini { platformMini_arch :: PlatformMini -> Arch
platformMini_arch = ArchPPC_64 PPC_64ABI
_ } })
CLabel
importedLbl
| OS -> Bool
osElfTarget (Platform -> OS
platformOS Platform
platform)
= case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl of
Just (DynamicLinkerLabelInfo
SymbolPtr, CLabel
lbl)
-> [SDoc] -> SDoc
vcat [
String -> SDoc
text String
".section \".toc\", \"aw\"",
String -> SDoc
text String
".LC_" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
':',
String -> SDoc
text String
"\t.quad" SDoc -> SDoc -> SDoc
<+> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl ]
Maybe (DynamicLinkerLabelInfo, CLabel)
_ -> SDoc
empty
pprImportedSymbol DynFlags
dflags Platform
platform CLabel
importedLbl
| OS -> Bool
osElfTarget (Platform -> OS
platformOS Platform
platform)
= case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl of
Just (DynamicLinkerLabelInfo
SymbolPtr, CLabel
lbl)
-> let symbolSize :: PtrString
symbolSize = case DynFlags -> Width
wordWidth DynFlags
dflags of
Width
W32 -> String -> PtrString
sLit String
"\t.long"
Width
W64 -> String -> PtrString
sLit String
"\t.quad"
Width
_ -> String -> PtrString
forall a. String -> a
panic String
"Unknown wordRep in pprImportedSymbol"
in [SDoc] -> SDoc
vcat [
String -> SDoc
text String
".section \".got2\", \"aw\"",
String -> SDoc
text String
".LC_" SDoc -> SDoc -> SDoc
<> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
':',
PtrString -> SDoc
ptext PtrString
symbolSize SDoc -> SDoc -> SDoc
<+> DynFlags -> CLabel -> SDoc
pprCLabel DynFlags
dflags CLabel
lbl ]
Maybe (DynamicLinkerLabelInfo, CLabel)
_ -> SDoc
empty
pprImportedSymbol DynFlags
_ Platform
_ CLabel
_
= String -> SDoc
forall a. String -> a
panic String
"PIC.pprImportedSymbol: no match"
initializePicBase_ppc
:: Arch -> OS -> Reg
-> [NatCmmDecl CmmStatics PPC.Instr]
-> NatM [NatCmmDecl CmmStatics PPC.Instr]
initializePicBase_ppc :: Arch
-> OS
-> Reg
-> [NatCmmDecl CmmStatics Instr]
-> NatM [NatCmmDecl CmmStatics Instr]
initializePicBase_ppc Arch
ArchPPC OS
os Reg
picReg
(CmmProc LabelMap CmmStatics
info CLabel
lab [GlobalReg]
live (ListGraph [GenBasicBlock Instr]
blocks) : [NatCmmDecl CmmStatics Instr]
statics)
| OS -> Bool
osElfTarget OS
os
= do
let
gotOffset :: Imm
gotOffset = Imm -> Imm -> Imm
PPC.ImmConstantDiff
(CLabel -> Imm
PPC.ImmCLbl CLabel
gotLabel)
(CLabel -> Imm
PPC.ImmCLbl CLabel
mkPicBaseLabel)
blocks' :: [GenBasicBlock Instr]
blocks' = case [GenBasicBlock Instr]
blocks of
[] -> []
(GenBasicBlock Instr
b:[GenBasicBlock Instr]
bs) -> GenBasicBlock Instr -> GenBasicBlock Instr
fetchPC GenBasicBlock Instr
b GenBasicBlock Instr
-> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
forall a. a -> [a] -> [a]
: (GenBasicBlock Instr -> GenBasicBlock Instr)
-> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
forall a b. (a -> b) -> [a] -> [b]
map GenBasicBlock Instr -> GenBasicBlock Instr
maybeFetchPC [GenBasicBlock Instr]
bs
maybeFetchPC :: GenBasicBlock Instr -> GenBasicBlock Instr
maybeFetchPC b :: GenBasicBlock Instr
b@(BasicBlock BlockId
bID [Instr]
_)
| KeyOf LabelMap
BlockId
bID KeyOf LabelMap -> LabelMap CmmStatics -> Bool
forall (map :: * -> *) a. IsMap map => KeyOf map -> map a -> Bool
`mapMember` LabelMap CmmStatics
info = GenBasicBlock Instr -> GenBasicBlock Instr
fetchPC GenBasicBlock Instr
b
| Bool
otherwise = GenBasicBlock Instr
b
fetchPC :: GenBasicBlock Instr -> GenBasicBlock Instr
fetchPC (BasicBlock BlockId
bID [Instr]
insns) =
BlockId -> [Instr] -> GenBasicBlock Instr
forall i. BlockId -> [i] -> GenBasicBlock i
BasicBlock BlockId
bID (Reg -> Instr
PPC.FETCHPC Reg
picReg
Instr -> [Instr] -> [Instr]
forall a. a -> [a] -> [a]
: Reg -> Reg -> Imm -> Instr
PPC.ADDIS Reg
picReg Reg
picReg (Imm -> Imm
PPC.HA Imm
gotOffset)
Instr -> [Instr] -> [Instr]
forall a. a -> [a] -> [a]
: Reg -> Reg -> RI -> Instr
PPC.ADD Reg
picReg Reg
picReg
(Imm -> RI
PPC.RIImm (Imm -> Imm
PPC.LO Imm
gotOffset))
Instr -> [Instr] -> [Instr]
forall a. a -> [a] -> [a]
: Reg -> Reg -> Instr
PPC.MR Reg
PPC.r30 Reg
picReg
Instr -> [Instr] -> [Instr]
forall a. a -> [a] -> [a]
: [Instr]
insns)
[NatCmmDecl CmmStatics Instr] -> NatM [NatCmmDecl CmmStatics Instr]
forall (m :: * -> *) a. Monad m => a -> m a
return (LabelMap CmmStatics
-> CLabel
-> [GlobalReg]
-> ListGraph Instr
-> NatCmmDecl CmmStatics Instr
forall d h g. h -> CLabel -> [GlobalReg] -> g -> GenCmmDecl d h g
CmmProc LabelMap CmmStatics
info CLabel
lab [GlobalReg]
live ([GenBasicBlock Instr] -> ListGraph Instr
forall i. [GenBasicBlock i] -> ListGraph i
ListGraph [GenBasicBlock Instr]
blocks') NatCmmDecl CmmStatics Instr
-> [NatCmmDecl CmmStatics Instr] -> [NatCmmDecl CmmStatics Instr]
forall a. a -> [a] -> [a]
: [NatCmmDecl CmmStatics Instr]
statics)
initializePicBase_ppc Arch
_ OS
_ Reg
_ [NatCmmDecl CmmStatics Instr]
_
= String -> NatM [NatCmmDecl CmmStatics Instr]
forall a. String -> a
panic String
"initializePicBase_ppc: not needed"
initializePicBase_x86
:: Arch -> OS -> Reg
-> [NatCmmDecl (Alignment, CmmStatics) X86.Instr]
-> NatM [NatCmmDecl (Alignment, CmmStatics) X86.Instr]
initializePicBase_x86 :: Arch
-> OS
-> Reg
-> [NatCmmDecl (Alignment, CmmStatics) Instr]
-> NatM [NatCmmDecl (Alignment, CmmStatics) Instr]
initializePicBase_x86 Arch
ArchX86 OS
os Reg
picReg
(CmmProc LabelMap CmmStatics
info CLabel
lab [GlobalReg]
live (ListGraph [GenBasicBlock Instr]
blocks) : [NatCmmDecl (Alignment, CmmStatics) Instr]
statics)
| OS -> Bool
osElfTarget OS
os
= [NatCmmDecl (Alignment, CmmStatics) Instr]
-> NatM [NatCmmDecl (Alignment, CmmStatics) Instr]
forall (m :: * -> *) a. Monad m => a -> m a
return (LabelMap CmmStatics
-> CLabel
-> [GlobalReg]
-> ListGraph Instr
-> NatCmmDecl (Alignment, CmmStatics) Instr
forall d h g. h -> CLabel -> [GlobalReg] -> g -> GenCmmDecl d h g
CmmProc LabelMap CmmStatics
info CLabel
lab [GlobalReg]
live ([GenBasicBlock Instr] -> ListGraph Instr
forall i. [GenBasicBlock i] -> ListGraph i
ListGraph [GenBasicBlock Instr]
blocks') NatCmmDecl (Alignment, CmmStatics) Instr
-> [NatCmmDecl (Alignment, CmmStatics) Instr]
-> [NatCmmDecl (Alignment, CmmStatics) Instr]
forall a. a -> [a] -> [a]
: [NatCmmDecl (Alignment, CmmStatics) Instr]
statics)
where blocks' :: [GenBasicBlock Instr]
blocks' = case [GenBasicBlock Instr]
blocks of
[] -> []
(GenBasicBlock Instr
b:[GenBasicBlock Instr]
bs) -> GenBasicBlock Instr -> GenBasicBlock Instr
fetchGOT GenBasicBlock Instr
b GenBasicBlock Instr
-> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
forall a. a -> [a] -> [a]
: (GenBasicBlock Instr -> GenBasicBlock Instr)
-> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
forall a b. (a -> b) -> [a] -> [b]
map GenBasicBlock Instr -> GenBasicBlock Instr
maybeFetchGOT [GenBasicBlock Instr]
bs
maybeFetchGOT :: GenBasicBlock Instr -> GenBasicBlock Instr
maybeFetchGOT b :: GenBasicBlock Instr
b@(BasicBlock BlockId
bID [Instr]
_)
| KeyOf LabelMap
BlockId
bID KeyOf LabelMap -> LabelMap CmmStatics -> Bool
forall (map :: * -> *) a. IsMap map => KeyOf map -> map a -> Bool
`mapMember` LabelMap CmmStatics
info = GenBasicBlock Instr -> GenBasicBlock Instr
fetchGOT GenBasicBlock Instr
b
| Bool
otherwise = GenBasicBlock Instr
b
fetchGOT :: GenBasicBlock Instr -> GenBasicBlock Instr
fetchGOT (BasicBlock BlockId
bID [Instr]
insns) =
BlockId -> [Instr] -> GenBasicBlock Instr
forall i. BlockId -> [i] -> GenBasicBlock i
BasicBlock BlockId
bID (Reg -> Instr
X86.FETCHGOT Reg
picReg Instr -> [Instr] -> [Instr]
forall a. a -> [a] -> [a]
: [Instr]
insns)
initializePicBase_x86 Arch
ArchX86 OS
OSDarwin Reg
picReg
(CmmProc LabelMap CmmStatics
info CLabel
lab [GlobalReg]
live (ListGraph (GenBasicBlock Instr
entry:[GenBasicBlock Instr]
blocks)) : [NatCmmDecl (Alignment, CmmStatics) Instr]
statics)
= [NatCmmDecl (Alignment, CmmStatics) Instr]
-> NatM [NatCmmDecl (Alignment, CmmStatics) Instr]
forall (m :: * -> *) a. Monad m => a -> m a
return (LabelMap CmmStatics
-> CLabel
-> [GlobalReg]
-> ListGraph Instr
-> NatCmmDecl (Alignment, CmmStatics) Instr
forall d h g. h -> CLabel -> [GlobalReg] -> g -> GenCmmDecl d h g
CmmProc LabelMap CmmStatics
info CLabel
lab [GlobalReg]
live ([GenBasicBlock Instr] -> ListGraph Instr
forall i. [GenBasicBlock i] -> ListGraph i
ListGraph (GenBasicBlock Instr
block'GenBasicBlock Instr
-> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
forall a. a -> [a] -> [a]
:[GenBasicBlock Instr]
blocks)) NatCmmDecl (Alignment, CmmStatics) Instr
-> [NatCmmDecl (Alignment, CmmStatics) Instr]
-> [NatCmmDecl (Alignment, CmmStatics) Instr]
forall a. a -> [a] -> [a]
: [NatCmmDecl (Alignment, CmmStatics) Instr]
statics)
where BasicBlock BlockId
bID [Instr]
insns = GenBasicBlock Instr
entry
block' :: GenBasicBlock Instr
block' = BlockId -> [Instr] -> GenBasicBlock Instr
forall i. BlockId -> [i] -> GenBasicBlock i
BasicBlock BlockId
bID (Reg -> Instr
X86.FETCHPC Reg
picReg Instr -> [Instr] -> [Instr]
forall a. a -> [a] -> [a]
: [Instr]
insns)
initializePicBase_x86 Arch
_ OS
_ Reg
_ [NatCmmDecl (Alignment, CmmStatics) Instr]
_
= String -> NatM [NatCmmDecl (Alignment, CmmStatics) Instr]
forall a. String -> a
panic String
"initializePicBase_x86: not needed"