{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnliftedFFITypes #-}
module GHC.Debugger.Runtime.FFIInspect where
import GHC.Base (StackSnapshot#)
import GHC.Stack.CloneStack

foreign import ccall unsafe "stack.h" bco_args_offset :: StackSnapshot# -> Word -> Int
foreign import ccall unsafe "stack.h" stack_bco_frame_selftest :: IO Bool


-- | Takes a frame location for a continuation RET_BCO frame.
--   Returns the offset of `bcoArgs` in the AP_STACK object that you'd get if stopping at the beginning of the continuation BCO.
bcoArgsOffset :: StackSnapshot -> Int -> Maybe Word
bcoArgsOffset :: StackSnapshot -> Int -> Maybe Word
bcoArgsOffset (StackSnapshot StackSnapshot#
ss) Int
frame_offset =
  case StackSnapshot# -> Word -> Int
bco_args_offset StackSnapshot#
ss (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
frame_offset) of
    Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 -> Maybe Word
forall a. Maybe a
Nothing
      | Bool
otherwise -> Word -> Maybe Word
forall a. a -> Maybe a
Just (Word -> Maybe Word) -> Word -> Maybe Word
forall a b. (a -> b) -> a -> b
$ Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i