module SPARC.CodeGen.Amode (
getAmode
)
where
import GhcPrelude
import {-# SOURCE #-} SPARC.CodeGen.Gen32
import SPARC.CodeGen.Base
import SPARC.AddrMode
import SPARC.Imm
import SPARC.Instr
import SPARC.Regs
import SPARC.Base
import NCGMonad
import Format
import Cmm
import OrdList
getAmode
:: CmmExpr
-> NatM Amode
getAmode :: CmmExpr -> NatM Amode
getAmode tree :: CmmExpr
tree@(CmmRegOff _ _)
= do DynFlags
dflags <- NatM DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
CmmExpr -> NatM Amode
getAmode (DynFlags -> CmmExpr -> CmmExpr
mangleIndexTree DynFlags
dflags CmmExpr
tree)
getAmode (CmmMachOp (MO_Sub _) [x :: CmmExpr
x, CmmLit (CmmInt i :: Integer
i _)])
| Integer -> Bool
forall a. Integral a => a -> Bool
fits13Bits (-Integer
i)
= do
(reg :: Reg
reg, code :: InstrBlock
code) <- CmmExpr -> NatM (Reg, InstrBlock)
getSomeReg CmmExpr
x
let
off :: Imm
off = Int -> Imm
ImmInt (-(Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
i))
Amode -> NatM Amode
forall (m :: * -> *) a. Monad m => a -> m a
return (AddrMode -> InstrBlock -> Amode
Amode (Reg -> Imm -> AddrMode
AddrRegImm Reg
reg Imm
off) InstrBlock
code)
getAmode (CmmMachOp (MO_Add _) [x :: CmmExpr
x, CmmLit (CmmInt i :: Integer
i _)])
| Integer -> Bool
forall a. Integral a => a -> Bool
fits13Bits Integer
i
= do
(reg :: Reg
reg, code :: InstrBlock
code) <- CmmExpr -> NatM (Reg, InstrBlock)
getSomeReg CmmExpr
x
let
off :: Imm
off = Int -> Imm
ImmInt (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
i)
Amode -> NatM Amode
forall (m :: * -> *) a. Monad m => a -> m a
return (AddrMode -> InstrBlock -> Amode
Amode (Reg -> Imm -> AddrMode
AddrRegImm Reg
reg Imm
off) InstrBlock
code)
getAmode (CmmMachOp (MO_Add _) [x :: CmmExpr
x, y :: CmmExpr
y])
= do
(regX :: Reg
regX, codeX :: InstrBlock
codeX) <- CmmExpr -> NatM (Reg, InstrBlock)
getSomeReg CmmExpr
x
(regY :: Reg
regY, codeY :: InstrBlock
codeY) <- CmmExpr -> NatM (Reg, InstrBlock)
getSomeReg CmmExpr
y
let
code :: InstrBlock
code = InstrBlock
codeX InstrBlock -> InstrBlock -> InstrBlock
forall a. OrdList a -> OrdList a -> OrdList a
`appOL` InstrBlock
codeY
Amode -> NatM Amode
forall (m :: * -> *) a. Monad m => a -> m a
return (AddrMode -> InstrBlock -> Amode
Amode (Reg -> Reg -> AddrMode
AddrRegReg Reg
regX Reg
regY) InstrBlock
code)
getAmode (CmmLit lit :: CmmLit
lit)
= do
let imm__2 :: Imm
imm__2 = CmmLit -> Imm
litToImm CmmLit
lit
Reg
tmp1 <- Format -> NatM Reg
getNewRegNat Format
II32
Reg
tmp2 <- Format -> NatM Reg
getNewRegNat Format
II32
let code :: InstrBlock
code = [Instr] -> InstrBlock
forall a. [a] -> OrdList a
toOL [ Imm -> Reg -> Instr
SETHI (Imm -> Imm
HI Imm
imm__2) Reg
tmp1
, Bool -> Reg -> RI -> Reg -> Instr
OR Bool
False Reg
tmp1 (Imm -> RI
RIImm (Imm -> Imm
LO Imm
imm__2)) Reg
tmp2]
Amode -> NatM Amode
forall (m :: * -> *) a. Monad m => a -> m a
return (AddrMode -> InstrBlock -> Amode
Amode (Reg -> Reg -> AddrMode
AddrRegReg Reg
tmp2 Reg
g0) InstrBlock
code)
getAmode other :: CmmExpr
other
= do
(reg :: Reg
reg, code :: InstrBlock
code) <- CmmExpr -> NatM (Reg, InstrBlock)
getSomeReg CmmExpr
other
let
off :: Imm
off = Int -> Imm
ImmInt 0
Amode -> NatM Amode
forall (m :: * -> *) a. Monad m => a -> m a
return (AddrMode -> InstrBlock -> Amode
Amode (Reg -> Imm -> AddrMode
AddrRegImm Reg
reg Imm
off) InstrBlock
code)