HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleghc-9.10.3GHC2021

GHC.StgToJS.Stack

Utilities and wrappers for Stack manipulation in JS Land.

In general, functions suffixed with a tick do the actual work, functions suffixed with an I are identical to the non-I versions but work on Idents

The stack in JS land is held in the special JS array 'h$stack' and the stack pointer is held in 'h$sp'. The top of the stack thus exists at 'h$stack[h$sp]'. h$stack[h$sp + i] where i > 0, moves deeper into the stack into older entries, whereas h$stack[h$sp - i] moves towards the top of the stack.

The stack layout algorithm is slightly peculiar. It makes an effort to remember recently popped things so that if these values need to be pushed then they can be quickly. The implementation for this is storing these values above the stack pointer, and the pushing will skip slots that we know we will use and fill in slots marked as unknown. Thus, you may find that our push and pop functions do some non-traditional stack manipulation such as adding slots in pop or removing slots in push.

  • 27 values
  • Packageghc-9.10.3
  • Exports27
  • LanguageGHC2021
  • LicenceBSD-3-Clause
  • SourceStack.hs
valueresetSlots :: G a -> G a
#

Run the action, m, with no stack info

valueisolateSlots :: G a -> G a
#

run the action, m, with current stack info, but don't let modifications propagate

valuedropSlots :: Int -> G ()
#

drop n slots from our stack knowledge

valueadjSpN :: Int -> G JStgStat
#

Shrink the stack and stack pointer. NB: This function is unsafe when the input n, is negative. This function wraps around adjSpN which actually performs the work.

valueadjSpN' :: Int -> JStgStat
#

Shrink the stack pointer by n. The stack grows downward so substract

valueadjSp' :: Int -> JStgStat
#

Grow the stack pointer by n without modifying the stack depth. The stack is just a JS array so we add to grow (instead of the traditional subtract)

valueadjSp :: Int -> G JStgStat
#

Wrapper which adjusts the stack pointer and modifies the stack depth tracked in G. See also adjSp' which actually does the stack pointer manipulation.

valuepushNN :: Array Integer Ident
#

Partial Push functions. Like pushN except these push functions skip slots. For example, function h$pp33(x1, x2) { h$sp += 6; h$stack[(h$sp - 5)] = x1; h$stack[(h$sp - 0)] = x2; };

The 33rd entry skips slots 1-4 to bind the top of the stack and the 6th slot. See pushOptimized and pushOptimized' for use cases.

valuepushN' :: Array Int JStgExpr
#

Convert all function symbols in pushN to global top-level functions. This is a hack which converts the function symbols to variables. This hack is caught in GHC.StgToJS.Printer.prettyBlock' to turn these into global functions.

valuepushN :: Array Int Ident
#

A constant array that holds global function symbols which do N pushes onto the stack. For example: function h$p1(x1) { ++h$sp; h$stack[(h$sp - 0)] = x1; }; function h$p2(x1, x2) { h$sp += 2; h$stack[(h$sp - 1)] = x1; h$stack[(h$sp - 0)] = x2; };

and so on up to 32.

valuepushOptimized
  1. :: [(JStgExpr, Bool)]

    contents of the slots, True if same value is already there

  2. -> G JStgStat
#

optimized push that reuses existing values on stack automatically chooses an optimized partial push (h$ppN) function when possible.

valuepopSkip
  1. :: Int

    number of slots to skip

  2. -> [JStgExpr]

    assign stack slot values to these

  3. -> JStgStat
#

Pop things, don't update the stack knowledge in G

valueloadSkip :: Int -> [JStgExpr] -> JStgStat
#

Load 'length (xs :: [JStgExpr])' things from the stack at offset 'n :: Int'. This function does no stack pointer manipulation, it merely indexes into the stack and loads payloads into xs.

Thunk update

3 declarations