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 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 dflags :: DynFlags
dflags referenceKind :: ReferenceKind
referenceKind lbl :: CLabel
lbl
| Just _ <- 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
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
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)
AccessDirectly -> case ReferenceKind
referenceKind of
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
_ -> 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 dflags :: DynFlags
dflags lbl :: 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 _ <- 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 lbl :: CLabel
lbl
= case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
lbl of
Just (GotSymbolPtr, _) -> Bool
False
Just (GotSymbolOffset, _) -> Bool
False
_ -> Bool
True
data LabelAccessStyle
= AccessViaStub
| AccessViaSymbolPtr
| AccessDirectly
howToAccessLabel
:: DynFlags -> Arch -> OS -> Module -> ReferenceKind -> CLabel -> LabelAccessStyle
howToAccessLabel :: DynFlags
-> Arch
-> OS
-> Module
-> ReferenceKind
-> CLabel
-> LabelAccessStyle
howToAccessLabel dflags :: DynFlags
dflags _ OSMinGW32 this_mod :: Module
this_mod _ lbl :: 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 dflags :: DynFlags
dflags arch :: Arch
arch OSDarwin this_mod :: Module
this_mod DataReference lbl :: 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 dflags :: DynFlags
dflags arch :: Arch
arch OSDarwin this_mod :: Module
this_mod JumpReference lbl :: 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 dflags :: DynFlags
dflags arch :: Arch
arch OSDarwin this_mod :: Module
this_mod _ lbl :: 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 _dflags :: DynFlags
_dflags _arch :: Arch
_arch OSAIX _this_mod :: Module
_this_mod kind :: ReferenceKind
kind _lbl :: CLabel
_lbl
= case ReferenceKind
kind of
DataReference -> LabelAccessStyle
AccessViaSymbolPtr
CallReference -> LabelAccessStyle
AccessDirectly
JumpReference -> LabelAccessStyle
AccessDirectly
howToAccessLabel _ (ArchPPC_64 _) os :: OS
os _ kind :: ReferenceKind
kind _
| OS -> Bool
osElfTarget OS
os
= case ReferenceKind
kind of
DataReference -> LabelAccessStyle
AccessViaSymbolPtr
JumpReference -> LabelAccessStyle
AccessViaSymbolPtr
_ -> LabelAccessStyle
AccessDirectly
howToAccessLabel dflags :: DynFlags
dflags _ os :: OS
os _ _ _
| 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 dflags :: DynFlags
dflags arch :: Arch
arch os :: OS
os this_mod :: Module
this_mod DataReference lbl :: 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 dflags :: DynFlags
dflags arch :: Arch
arch os :: OS
os this_mod :: Module
this_mod CallReference lbl :: 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 dflags :: DynFlags
dflags _ os :: OS
os this_mod :: Module
this_mod _ lbl :: 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 dflags :: DynFlags
dflags _ _ _ _ _
| Bool -> Bool
not (DynFlags -> Bool
positionIndependent DynFlags
dflags)
= LabelAccessStyle
AccessDirectly
| Bool
otherwise
= String -> LabelAccessStyle
forall a. String -> a
panic "howToAccessLabel: PIC not defined for this platform"
picRelative :: DynFlags -> Arch -> OS -> CLabel -> CmmLit
picRelative :: DynFlags -> Arch -> OS -> CLabel -> CmmLit
picRelative dflags :: DynFlags
dflags arch :: Arch
arch OSDarwin lbl :: 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 0 (DynFlags -> Width
wordWidth DynFlags
dflags)
picRelative dflags :: DynFlags
dflags _ OSAIX lbl :: CLabel
lbl
= CLabel -> CLabel -> Int -> Width -> CmmLit
CmmLabelDiffOff CLabel
lbl CLabel
gotLabel 0 (DynFlags -> Width
wordWidth DynFlags
dflags)
picRelative dflags :: DynFlags
dflags ArchPPC os :: OS
os lbl :: CLabel
lbl
| OS -> Bool
osElfTarget OS
os
= CLabel -> CLabel -> Int -> Width -> CmmLit
CmmLabelDiffOff CLabel
lbl CLabel
gotLabel 0 (DynFlags -> Width
wordWidth DynFlags
dflags)
picRelative _ arch :: Arch
arch os :: OS
os lbl :: 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 (SymbolPtr, lbl' :: 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 _ _ _ _
= String -> CmmLit
forall a. String -> a
panic "PositionIndependentCode.picRelative undefined for this platform"
needImportedSymbols :: DynFlags -> Arch -> OS -> Bool
needImportedSymbols :: DynFlags -> Arch -> OS -> Bool
needImportedSymbols dflags :: DynFlags
dflags arch :: Arch
arch os :: 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 ".LCTOC1")
Maybe Int
forall a. Maybe a
Nothing ForeignLabelSource
ForeignLabelInThisPackage FunctionOrData
IsData
pprGotDeclaration :: DynFlags -> Arch -> OS -> SDoc
pprGotDeclaration :: DynFlags -> Arch -> OS -> SDoc
pprGotDeclaration dflags :: DynFlags
dflags ArchX86 OSDarwin
| DynFlags -> Bool
positionIndependent DynFlags
dflags
= [SDoc] -> SDoc
vcat [
String -> SDoc
text ".section __TEXT,__textcoal_nt,coalesced,no_toc",
String -> SDoc
text ".weak_definition ___i686.get_pc_thunk.ax",
String -> SDoc
text ".private_extern ___i686.get_pc_thunk.ax",
String -> SDoc
text "___i686.get_pc_thunk.ax:",
String -> SDoc
text "\tmovl (%esp), %eax",
String -> SDoc
text "\tret" ]
pprGotDeclaration _ _ OSDarwin
= SDoc
empty
pprGotDeclaration _ _ OSAIX
= [SDoc] -> SDoc
vcat ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ [ String -> SDoc
text ".toc"
, String -> SDoc
text ".tc ghc_toc_table[TC],.LCTOC1"
, String -> SDoc
text ".csect ghc_toc_table[RW]"
, String -> SDoc
text ".set .LCTOC1,$+0x8000"
]
pprGotDeclaration _ (ArchPPC_64 ELF_V1) _
= String -> SDoc
text ".section \".toc\",\"aw\""
pprGotDeclaration _ (ArchPPC_64 ELF_V2) _
= [SDoc] -> SDoc
vcat [ String -> SDoc
text ".abiversion 2",
String -> SDoc
text ".section \".toc\",\"aw\""
]
pprGotDeclaration dflags :: DynFlags
dflags arch :: Arch
arch os :: 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 ".section \".got2\",\"aw\"",
String -> SDoc
text ".LCTOC1 = .+32768" ]
pprGotDeclaration _ _ _
= String -> SDoc
forall a. String -> a
panic "pprGotDeclaration: no match"
pprImportedSymbol :: DynFlags -> Platform -> CLabel -> SDoc
pprImportedSymbol :: DynFlags -> Platform -> CLabel -> SDoc
pprImportedSymbol dflags :: DynFlags
dflags platform :: Platform
platform@(Platform { platformArch :: Platform -> Arch
platformArch = Arch
ArchX86, platformOS :: Platform -> OS
platformOS = OS
OSDarwin }) importedLbl :: CLabel
importedLbl
| Just (CodeStub, lbl :: CLabel
lbl) <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl
= case DynFlags -> Bool
positionIndependent DynFlags
dflags of
False ->
[SDoc] -> SDoc
vcat [
String -> SDoc
text ".symbol_stub",
String -> SDoc
text "L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
<> PtrString -> SDoc
ptext (String -> PtrString
sLit "$stub:"),
String -> SDoc
text "\t.indirect_symbol" SDoc -> SDoc -> SDoc
<+> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl,
String -> SDoc
text "\tjmp *L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text "$lazy_ptr",
String -> SDoc
text "L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text "$stub_binder:",
String -> SDoc
text "\tpushl $L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text "$lazy_ptr",
String -> SDoc
text "\tjmp dyld_stub_binding_helper"
]
True ->
[SDoc] -> SDoc
vcat [
String -> SDoc
text ".section __TEXT,__picsymbolstub2,"
SDoc -> SDoc -> SDoc
<> String -> SDoc
text "symbol_stubs,pure_instructions,25",
String -> SDoc
text "L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
<> PtrString -> SDoc
ptext (String -> PtrString
sLit "$stub:"),
String -> SDoc
text "\t.indirect_symbol" SDoc -> SDoc -> SDoc
<+> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl,
String -> SDoc
text "\tcall ___i686.get_pc_thunk.ax",
String -> SDoc
text "1:",
String -> SDoc
text "\tmovl L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text "$lazy_ptr-1b(%eax),%edx",
String -> SDoc
text "\tjmp *%edx",
String -> SDoc
text "L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text "$stub_binder:",
String -> SDoc
text "\tlea L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text "$lazy_ptr-1b(%eax),%eax",
String -> SDoc
text "\tpushl %eax",
String -> SDoc
text "\tjmp dyld_stub_binding_helper"
]
SDoc -> SDoc -> SDoc
$+$ [SDoc] -> SDoc
vcat [ String -> SDoc
text ".section __DATA, __la_sym_ptr"
SDoc -> SDoc -> SDoc
<> (if DynFlags -> Bool
positionIndependent DynFlags
dflags then Int -> SDoc
int 2 else Int -> SDoc
int 3)
SDoc -> SDoc -> SDoc
<> String -> SDoc
text ",lazy_symbol_pointers",
String -> SDoc
text "L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
<> PtrString -> SDoc
ptext (String -> PtrString
sLit "$lazy_ptr:"),
String -> SDoc
text "\t.indirect_symbol" SDoc -> SDoc -> SDoc
<+> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl,
String -> SDoc
text "\t.long L" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl
SDoc -> SDoc -> SDoc
<> String -> SDoc
text "$stub_binder"]
| Just (SymbolPtr, lbl :: CLabel
lbl) <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl
= [SDoc] -> SDoc
vcat [
String -> SDoc
text ".non_lazy_symbol_pointer",
Char -> SDoc
char 'L' SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
<> String -> SDoc
text "$non_lazy_ptr:",
String -> SDoc
text "\t.indirect_symbol" SDoc -> SDoc -> SDoc
<+> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl,
String -> SDoc
text "\t.long\t0"]
| Bool
otherwise
= SDoc
empty
pprImportedSymbol _ (Platform { platformOS :: Platform -> OS
platformOS = OS
OSDarwin }) _
= SDoc
empty
pprImportedSymbol _ platform :: Platform
platform@(Platform { platformOS :: Platform -> OS
platformOS = OS
OSAIX }) importedLbl :: CLabel
importedLbl
= case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl of
Just (SymbolPtr, lbl :: CLabel
lbl)
-> [SDoc] -> SDoc
vcat [
String -> SDoc
text "LC.." SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
<> Char -> SDoc
char ':',
String -> SDoc
text "\t.long" SDoc -> SDoc -> SDoc
<+> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl ]
_ -> SDoc
empty
pprImportedSymbol _ platform :: Platform
platform@(Platform { platformArch :: Platform -> Arch
platformArch = ArchPPC_64 _ })
importedLbl :: CLabel
importedLbl
| OS -> Bool
osElfTarget (Platform -> OS
platformOS Platform
platform)
= case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl of
Just (SymbolPtr, lbl :: CLabel
lbl)
-> [SDoc] -> SDoc
vcat [
String -> SDoc
text ".section \".toc\", \"aw\"",
String -> SDoc
text ".LC_" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
<> Char -> SDoc
char ':',
String -> SDoc
text "\t.quad" SDoc -> SDoc -> SDoc
<+> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl ]
_ -> SDoc
empty
pprImportedSymbol dflags :: DynFlags
dflags platform :: Platform
platform importedLbl :: CLabel
importedLbl
| OS -> Bool
osElfTarget (Platform -> OS
platformOS Platform
platform)
= case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
dynamicLinkerLabelInfo CLabel
importedLbl of
Just (SymbolPtr, lbl :: CLabel
lbl)
-> let symbolSize :: PtrString
symbolSize = case DynFlags -> Width
wordWidth DynFlags
dflags of
W32 -> String -> PtrString
sLit "\t.long"
W64 -> String -> PtrString
sLit "\t.quad"
_ -> String -> PtrString
forall a. String -> a
panic "Unknown wordRep in pprImportedSymbol"
in [SDoc] -> SDoc
vcat [
String -> SDoc
text ".section \".got2\", \"aw\"",
String -> SDoc
text ".LC_" SDoc -> SDoc -> SDoc
<> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl SDoc -> SDoc -> SDoc
<> Char -> SDoc
char ':',
PtrString -> SDoc
ptext PtrString
symbolSize SDoc -> SDoc -> SDoc
<+> Platform -> CLabel -> SDoc
pprCLabel Platform
platform CLabel
lbl ]
_ -> SDoc
empty
pprImportedSymbol _ _ _
= String -> SDoc
forall a. String -> a
panic "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 ArchPPC os :: OS
os picReg :: Reg
picReg
(CmmProc info :: LabelMap CmmStatics
info lab :: CLabel
lab live :: [GlobalReg]
live (ListGraph blocks :: [GenBasicBlock Instr]
blocks) : statics :: [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
[] -> []
(b :: GenBasicBlock Instr
b:bs :: [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 bID :: BlockId
bID _)
| 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 bID :: BlockId
bID insns :: [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 _ _ _ _
= String -> NatM [NatCmmDecl CmmStatics Instr]
forall a. String -> a
panic "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 (Int, CmmStatics) Instr]
-> NatM [NatCmmDecl (Int, CmmStatics) Instr]
initializePicBase_x86 ArchX86 os :: OS
os picReg :: Reg
picReg
(CmmProc info :: LabelMap CmmStatics
info lab :: CLabel
lab live :: [GlobalReg]
live (ListGraph blocks :: [GenBasicBlock Instr]
blocks) : statics :: [NatCmmDecl (Int, CmmStatics) Instr]
statics)
| OS -> Bool
osElfTarget OS
os
= [NatCmmDecl (Int, CmmStatics) Instr]
-> NatM [NatCmmDecl (Int, CmmStatics) Instr]
forall (m :: * -> *) a. Monad m => a -> m a
return (LabelMap CmmStatics
-> CLabel
-> [GlobalReg]
-> ListGraph Instr
-> NatCmmDecl (Int, 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 (Int, CmmStatics) Instr
-> [NatCmmDecl (Int, CmmStatics) Instr]
-> [NatCmmDecl (Int, CmmStatics) Instr]
forall a. a -> [a] -> [a]
: [NatCmmDecl (Int, CmmStatics) Instr]
statics)
where blocks' :: [GenBasicBlock Instr]
blocks' = case [GenBasicBlock Instr]
blocks of
[] -> []
(b :: GenBasicBlock Instr
b:bs :: [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 bID :: BlockId
bID _)
| 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 bID :: BlockId
bID insns :: [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 ArchX86 OSDarwin picReg :: Reg
picReg
(CmmProc info :: LabelMap CmmStatics
info lab :: CLabel
lab live :: [GlobalReg]
live (ListGraph (entry :: GenBasicBlock Instr
entry:blocks :: [GenBasicBlock Instr]
blocks)) : statics :: [NatCmmDecl (Int, CmmStatics) Instr]
statics)
= [NatCmmDecl (Int, CmmStatics) Instr]
-> NatM [NatCmmDecl (Int, CmmStatics) Instr]
forall (m :: * -> *) a. Monad m => a -> m a
return (LabelMap CmmStatics
-> CLabel
-> [GlobalReg]
-> ListGraph Instr
-> NatCmmDecl (Int, 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 (Int, CmmStatics) Instr
-> [NatCmmDecl (Int, CmmStatics) Instr]
-> [NatCmmDecl (Int, CmmStatics) Instr]
forall a. a -> [a] -> [a]
: [NatCmmDecl (Int, CmmStatics) Instr]
statics)
where BasicBlock bID :: BlockId
bID insns :: [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 _ _ _ _
= String -> NatM [NatCmmDecl (Int, CmmStatics) Instr]
forall a. String -> a
panic "initializePicBase_x86: not needed"