module SPARC.Stack (
        spRel,
        fpRel,
        spillSlotToOffset,
        maxSpillSlots
)

where

import GhcPrelude

import SPARC.AddrMode
import SPARC.Regs
import SPARC.Base
import SPARC.Imm

import DynFlags
import Outputable

-- | Get an AddrMode relative to the address in sp.
--      This gives us a stack relative addressing mode for volatile
--      temporaries and for excess call arguments.
--
spRel :: Int            -- ^ stack offset in words, positive or negative
      -> AddrMode

spRel :: Int -> AddrMode
spRel n :: Int
n = Reg -> Imm -> AddrMode
AddrRegImm Reg
sp (Int -> Imm
ImmInt (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
wordLength))


-- | Get an address relative to the frame pointer.
--      This doesn't work work for offsets greater than 13 bits; we just hope for the best
--
fpRel :: Int -> AddrMode
fpRel :: Int -> AddrMode
fpRel n :: Int
n
        = Reg -> Imm -> AddrMode
AddrRegImm Reg
fp (Int -> Imm
ImmInt (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
wordLength))


-- | Convert a spill slot number to a *byte* offset, with no sign.
--
spillSlotToOffset :: DynFlags -> Int -> Int
spillSlotToOffset :: DynFlags -> Int -> Int
spillSlotToOffset dflags :: DynFlags
dflags slot :: Int
slot
        | Int
slot Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0 Bool -> Bool -> Bool
&& Int
slot Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< DynFlags -> Int
maxSpillSlots DynFlags
dflags
        = 64 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
spillSlotSize Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
slot

        | Bool
otherwise
        = String -> SDoc -> Int
forall a. HasCallStack => String -> SDoc -> a
pprPanic "spillSlotToOffset:"
                      (   String -> SDoc
text "invalid spill location: " SDoc -> SDoc -> SDoc
<> Int -> SDoc
int Int
slot
                      SDoc -> SDoc -> SDoc
$$  String -> SDoc
text "maxSpillSlots:          " SDoc -> SDoc -> SDoc
<> Int -> SDoc
int (DynFlags -> Int
maxSpillSlots DynFlags
dflags))


-- | The maximum number of spill slots available on the C stack.
--      If we use up all of the slots, then we're screwed.
--
--      Why do we reserve 64 bytes, instead of using the whole thing??
--              -- BL 2009/02/15
--
maxSpillSlots :: DynFlags -> Int
maxSpillSlots :: DynFlags -> Int
maxSpillSlots dflags :: DynFlags
dflags
        = ((DynFlags -> Int
spillAreaLength DynFlags
dflags Int -> Int -> Int
forall a. Num a => a -> a -> a
- 64) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
spillSlotSize) Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1