summaryrefslogtreecommitdiff
path: root/compiler/GHC/StgToJS/Rts/Rts.hs
blob: a25cd6c4e7bb729a414c0df4cf221176b4661a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
{-# LANGUAGE OverloadedStrings #-}

{-# OPTIONS_GHC -O0 #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.StgToJS.Rts.Rts
-- Copyright   :  (c) The University of Glasgow 2001
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Jeffrey Young  <jeffrey.young@iohk.io>
--                Luite Stegeman <luite.stegeman@iohk.io>
--                Sylvain Henry  <sylvain.henry@iohk.io>
--                Josh Meredith  <josh.meredith@iohk.io>
-- Stability   :  experimental
--
-- Top level driver of the JavaScript Backend RTS. This file is an
-- implementation of the JS RTS for the JS backend written as an EDSL in
-- Haskell. It assumes the existence of pre-generated JS functions, included as
-- js-sources in base. These functions are similarly assumed for non-inline
-- Primops, See 'GHC.StgToJS.Prim'. Most of the elements in this module are
-- constants in Haskell Land which define pieces of the JS RTS.
--
-----------------------------------------------------------------------------

module GHC.StgToJS.Rts.Rts where

import GHC.Prelude

import GHC.JS.Unsat.Syntax
import GHC.JS.Make
import GHC.JS.Transform
import GHC.JS.Optimizer

import GHC.StgToJS.Apply
import GHC.StgToJS.Closure
import GHC.StgToJS.Heap
import GHC.StgToJS.Profiling
import GHC.StgToJS.Regs
import GHC.StgToJS.Types
import GHC.StgToJS.Stack

import GHC.StgToJS.Linker.Opt

import GHC.Data.FastString
import GHC.Types.Unique.Map

import Data.Array
import Data.Monoid
import Data.Char (toLower, toUpper)
import qualified Data.Bits          as Bits

-- | The garbageCollector resets registers and result variables.
garbageCollector :: JStat
garbageCollector =
  mconcat [ TxtI "h$resetRegisters"  ||= jLam (mconcat $ map resetRegister [minBound..maxBound])
          , TxtI "h$resetResultVars" ||= jLam (mconcat $ map resetResultVar [minBound..maxBound])
          ]

-- | Reset the register 'r' in JS Land. Note that this "resets" by setting the
-- register to a dummy variable called "null", /not/ by setting to JS's nil
-- value.
resetRegister :: StgReg -> JStat
resetRegister r = toJExpr r |= null_

-- | Reset the return variable 'r' in JS Land. Note that this "resets" by
-- setting the register to a dummy variable called "null", /not/ by setting to
-- JS's nil value.
resetResultVar :: StgRet -> JStat
resetResultVar r = toJExpr r |= null_

-- | Define closures based on size, these functions are syntactic sugar, e.g., a
-- Haskell function which generates some useful JS. Each Closure constructor
-- follows the naming convention h$cN, where N is a natural number. For example,
-- h$c (with the nat omitted) is a JS Land Constructor for a closure in JS land
-- which has a single entry function 'f', and no fields; identical to h$c0. h$c1
-- is a JS Land Constructor for a closure with an entry function 'f', and a
-- /single/ field 'x1', 'Just foo' is an example of this kind of closure. h$c2
-- is a JS Land Constructor for a closure with an entry function and two data
-- fields: 'x1' and 'x2'. And so on. Note that this has JIT performance
-- implications; you should use h$c1, h$c2, h$c3, ... h$c24 instead of making
-- objects manually so layouts and fields can be changed more easily and so the
-- JIT can optimize better.
closureConstructors :: StgToJSConfig -> JStat
closureConstructors s = BlockStat
  [ mconcat (map mkClosureCon (Nothing : map Just [0..jsClosureCount]))
  , mconcat (map mkDataFill [1..jsClosureCount])
  ]
  where
    prof = csProf s
    (ccArg,ccVal)
      -- the cc argument happens to be named just like the cc field...
      | prof      = ([TxtI closureCC_], Just (var closureCC_))
      | otherwise = ([], Nothing)
    addCCArg' as = as ++ ccArg

    traceAlloc x | csTraceRts s = appS "h$traceAlloc" [x]
                 | otherwise    = mempty

    notifyAlloc x | csDebugAlloc s = appS "h$debugAlloc_notifyAlloc" [x]
                  | otherwise      = mempty

    -- only JSVal can typically contain undefined or null
    -- although it's possible (and legal) to make other Haskell types
    -- to contain JS refs directly
    -- this can cause false positives here
    checkC :: JStat
    checkC | csAssertRts s =
      jVar $ \msg ->
      jwhenS (var "arguments" .! 0 .!==. jString "h$baseZCGHCziJSziPrimziJSVal_con_e")
                                  (loop 1 (.<. var "arguments" .^ "length")
                                          (\i ->
                                             mconcat [msg |= jString "warning: undefined or null in argument: "
                                                       + i
                                                       + jString " allocating closure: " + (var "arguments" .! 0 .^ "n")
                                                     , appS "h$log" [msg]
                                                     , jwhenS (var "console" .&&. (var "console" .^ "trace")) ((var "console" .^ "trace") `ApplStat` [msg])
                                                     , postIncrS i
                                                     ])

                                  )
           | otherwise = mempty

    -- h$d is never used for JSVal (since it's only for constructors with
    -- at least three fields, so we always warn here
    checkD | csAssertRts s =
                     loop 0 (.<. var "arguments" .^ "length")
                     (\i -> jwhenS ((var "arguments" .! i .===. null_)
                                    .||. (var "arguments" .! i .===. undefined_))
                            (jVar $ \msg ->
                                mconcat [ msg |= jString "warning: undefined or null in argument: " + i + jString " allocating fields"
                                        , jwhenS (var "console" .&&. (var "console" .^ "trace"))
                                                ((var "console" .^ "trace") `ApplStat` [msg])
                                        ]))

           | otherwise = mempty

    mkClosureCon :: Maybe Int -> JStat
    mkClosureCon n0 = jFunction funName args funBod
      where
        n | Just n' <- n0 = n'
          | Nothing <- n0 = 0
        funName | Just n' <- n0 = TxtI $ clsName n'
                | Nothing <- n0 = TxtI $ mkFastString "h$c"
        -- args are: f x1 x2 .. xn [cc]
        args   = TxtI "f" : addCCArg' (map varName [1..n])
        -- x1 goes into closureField1. All the other args are bundled into an
        -- object in closureField2: { d1 = x2, d2 = x3, ... }
        --
        vars   = map (toJExpr . varName) [1..n]

        funBod = jVar $ \x ->
            [ checkC
            , x |= newClosure (mkClosure (var "f") vars 0 ccVal)
            , notifyAlloc x
            , traceAlloc x
            , returnS x
            ]

    mkDataFill :: Int -> JStat
    mkDataFill n = jFunction funName (map TxtI ds) body
      where
        funName    = TxtI $ dataName n
        ds         = map dataFieldName [1..n]
        extra_args = ValExpr . JHash . listToUniqMap . zip ds $ map (toJExpr . TxtI) ds
        body       = (checkD <> returnS extra_args)

-- | JS Payload to perform stack manipulation in the RTS
stackManip :: JStat
stackManip = mconcat (map mkPush [1..32]) <>
             mconcat (map mkPpush [1..255])
  where
    mkPush :: Int -> JStat
    mkPush n = let funName = TxtI $ mkFastString ("h$p" ++ show n)
                   as      = map varName [1..n]
                   body    = ((sp |= sp + toJExpr n)
                               <> mconcat (zipWith (\i a -> stack .! (sp - toJExpr (n-i)) |= toJExpr a)
                                            [1..] as))
               in jFunction funName as body

    -- partial pushes, based on bitmap, increases Sp by highest bit
    mkPpush :: Integer -> JStat
    mkPpush sig | sig Bits..&. (sig+1) == 0 = mempty -- already handled by h$p
    mkPpush sig = let funName = TxtI $ mkFastString ("h$pp" ++ show sig)
                      bits    = bitsIdx sig
                      n       = length bits
                      h       = last bits
                      args    = map varName [1..n]
                      body    = mconcat [ sp |= sp + toJExpr (h+1)
                                        , mconcat (zipWith (\b a -> stack .! (sp - toJExpr (h-b)) |= toJExpr a) bits args)
                                        ]
                   in jFunction funName args body

bitsIdx :: Integer -> [Int]
bitsIdx n | n < 0 = error "bitsIdx: negative"
          | otherwise = go n 0
  where
    go 0 _ = []
    go m b | Bits.testBit m b = b : go (Bits.clearBit m b) (b+1)
           | otherwise   = go (Bits.clearBit m b) (b+1)

bhLneStats :: StgToJSConfig -> JExpr -> JExpr -> JStat
bhLneStats _s p frameSize =
   jVar $ \v ->
            mconcat [ v |= stack .! p
                    , ifS v
                      ((sp |= sp - frameSize)
                       <> ifS (v .===. var "h$blackhole")
                                (returnS $ app "h$throw" [var "h$baseZCControlziExceptionziBasezinonTermination", false_])
                                (mconcat [r1 |= v
                                         , sp |= sp - frameSize
                                         , returnStack
                                         ]))
                      ((stack .! p |= var "h$blackhole") <> returnS null_)
                    ]


-- | JS payload to declare the registers
declRegs :: JStat
declRegs =
  mconcat [ TxtI "h$regs" ||= toJExpr (JList [])
          , mconcat (map declReg (enumFromTo R1 R32))
          , regGettersSetters
          , loadRegs
          ]
    where
      declReg r = (decl . TxtI . mkFastString . ("h$"++) . map toLower . show) r
                  <> BlockStat [AssignStat (toJExpr r) (ValExpr (JInt 0))] -- [j| `r` = 0; |]

-- | JS payload to define getters and setters on the registers.
regGettersSetters :: JStat
regGettersSetters =
  mconcat [ TxtI "h$getReg" ||= jLam (\n   -> SwitchStat n getRegCases mempty)
          , TxtI "h$setReg" ||= jLam (\n v -> SwitchStat n (setRegCases v) mempty)
          ]
  where
    getRegCases =
      map (\r -> (toJExpr (jsRegToInt r) , returnS (toJExpr r))) regsFromR1
    setRegCases v =
      map (\r -> (toJExpr (jsRegToInt r), (toJExpr r |= toJExpr v) <> returnS undefined_)) regsFromR1

-- | JS payload that defines the functions to load each register
loadRegs :: JStat
loadRegs = mconcat $ map mkLoad [1..32]
  where
    mkLoad :: Int -> JStat
    mkLoad n = let args  = map varName [1..n]
                   body  = mconcat $
                           zipWith (\a r -> toJExpr r |= toJExpr a)
                           args (reverse $ take n regsFromR1)
                   fname = TxtI $ mkFastString ("h$l" ++ show n)
               in jFunction fname args body

-- | Assign registers R1 ... Rn in descending order, that is assign Rn first.
-- This function uses the 'assignRegs'' array to construct functions which set
-- the registers.
assignRegs :: StgToJSConfig -> [JExpr] -> JStat
assignRegs _ [] = mempty
assignRegs s xs
  | l <= 32 && not (csInlineLoadRegs s)
      = ApplStat (ValExpr (JVar $ assignRegs'!l)) (reverse xs)
  | otherwise = mconcat . reverse $
      zipWith (\r ex -> toJExpr r |= ex) (take l regsFromR1) xs
  where
    l = length xs

-- | JS payload which defines an array of function symbols that set N registers
-- from M parameters. For example, h$l2 compiles to:
-- @
--    function h$l4(x1, x2, x3, x4) {
--      h$r4 = x1;
--      h$r3 = x2;
--      h$r2 = x3;
--      h$r1 = x4;
--    };
-- @
assignRegs' :: Array Int Ident
assignRegs' = listArray (1,32) (map (TxtI . mkFastString . ("h$l"++) . show) [(1::Int)..32])

-- | JS payload to declare return variables.
declRets :: JStat
declRets = mconcat $ map (decl . TxtI . mkFastString . ("h$"++) . map toLower . show) (enumFrom Ret1)

-- | JS payload defining the types closures.
closureTypes :: JStat
closureTypes = mconcat (map mkClosureType (enumFromTo minBound maxBound)) <> closureTypeName
  where
    mkClosureType :: ClosureType -> JStat
    mkClosureType c = let s = TxtI . mkFastString $ "h$" ++ map toUpper (show c) ++ "_CLOSURE"
                      in  s ||= toJExpr c
    closureTypeName :: JStat
    closureTypeName =
      TxtI "h$closureTypeName" ||= jLam (\c ->
                                           mconcat (map (ifCT c) [minBound..maxBound])
                                          <> returnS (jString "InvalidClosureType"))

    ifCT :: JExpr -> ClosureType -> JStat
    ifCT arg ct = jwhenS (arg .===. toJExpr ct) (returnS (toJExpr (show ct)))

-- | JS payload declaring the RTS functions.
rtsDecls :: JStat
rtsDecls = jsSaturate (Just "h$RTSD") $
  mconcat [ TxtI "h$currentThread"   ||= null_                   -- thread state object for current thread
          , TxtI "h$stack"           ||= null_                   -- stack for the current thread
          , TxtI "h$sp"              ||= 0                       -- stack pointer for the current thread
          , TxtI "h$initStatic"      ||= toJExpr (JList [])      -- we need delayed initialization for static objects, push functions here to be initialized just before haskell runs
          , TxtI "h$staticThunks"    ||= toJExpr (jhFromList []) --  funcName -> heapidx map for srefs
          , TxtI "h$staticThunksArr" ||= toJExpr (JList [])      -- indices of updatable thunks in static heap
          , TxtI "h$CAFs"            ||= toJExpr (JList [])
          , TxtI "h$CAFsReset"       ||= toJExpr (JList [])
          -- stg registers
          , declRegs
          , declRets]

-- | print the embedded RTS to a String
rtsText :: StgToJSConfig -> String
rtsText = show . pretty . jsOptimize . satJStat . rts

-- | print the RTS declarations to a String.
rtsDeclsText :: String
rtsDeclsText = show . pretty . jsOptimize . satJStat $ rtsDecls

-- | Wrapper over the RTS to guarentee saturation, see 'GHC.JS.Transform'
rts :: StgToJSConfig -> JStat
rts = jsSaturate (Just "h$RTS") . rts'

-- | JS Payload which defines the embedded RTS.
rts' :: StgToJSConfig -> JStat
rts' s =
  mconcat [ closureConstructors s
          , garbageCollector
          , stackManip
          , TxtI "h$rts_traceForeign" ||= toJExpr (csTraceForeign s)
          , TxtI "h$rts_profiling"    ||= toJExpr (csProf s)
          , TxtI "h$ct_fun"        ||= toJExpr Fun
          , TxtI "h$ct_con"        ||= toJExpr Con
          , TxtI "h$ct_thunk"      ||= toJExpr Thunk
          , TxtI "h$ct_pap"        ||= toJExpr Pap
          , TxtI "h$ct_blackhole"  ||= toJExpr Blackhole
          , TxtI "h$ct_stackframe" ||= toJExpr StackFrame
          , TxtI "h$vt_ptr"    ||= toJExpr PtrV
          , TxtI "h$vt_void"   ||= toJExpr VoidV
          , TxtI "h$vt_double" ||= toJExpr IntV
          , TxtI "h$vt_long"   ||= toJExpr LongV
          , TxtI "h$vt_addr"   ||= toJExpr AddrV
          , TxtI "h$vt_rtsobj" ||= toJExpr RtsObjV
          , TxtI "h$vt_obj"    ||= toJExpr ObjV
          , TxtI "h$vt_arr"    ||= toJExpr ArrV
          , TxtI "h$bh"        ||= jLam (bhStats s True)
          , TxtI "h$bh_lne"    ||= jLam (\x frameSize -> bhLneStats s x frameSize)
          , closure (ClosureInfo (TxtI "h$blackhole") (CIRegs 0 []) "blackhole" (CILayoutUnknown 2) CIBlackhole mempty)
               (appS "throw" [jString "oops: entered black hole"])
          , closure (ClosureInfo (TxtI "h$blackholeTrap") (CIRegs 0 []) "blackhole" (CILayoutUnknown 2) CIThunk mempty)
               (appS "throw" [jString "oops: entered multiple times"])
          , closure (ClosureInfo (TxtI "h$done") (CIRegs 0 [PtrV]) "done" (CILayoutUnknown 0) CIStackFrame mempty)
               (appS "h$finishThread" [var "h$currentThread"] <> returnS (var "h$reschedule"))
          , closure (ClosureInfo (TxtI "h$doneMain_e") (CIRegs 0 [PtrV]) "doneMain" (CILayoutUnknown 0) CIStackFrame mempty)
               (returnS (var "h$doneMain"))
          , conClosure (TxtI "h$false_e") "GHC.Types.False" (CILayoutFixed 0 []) 1
          , conClosure (TxtI "h$true_e" ) "GHC.Types.True"  (CILayoutFixed 0 []) 2
          -- generic data constructor with 1 non-heapobj field
          , conClosure (TxtI "h$data1_e") "data1" (CILayoutFixed 1 [ObjV]) 1
          -- generic data constructor with 2 non-heapobj fields
          , conClosure (TxtI "h$data2_e") "data2" (CILayoutFixed 2 [ObjV,ObjV]) 1
          , closure (ClosureInfo (TxtI "h$noop_e") (CIRegs 1 [PtrV]) "no-op IO ()" (CILayoutFixed 0 []) (CIFun 1 0) mempty)
               (returnS (stack .! sp))
            <> (TxtI "h$noop" ||= ApplExpr (var "h$c0") (var "h$noop_e" : [jSystemCCS | csProf s]))
          , closure (ClosureInfo (TxtI "h$catch_e") (CIRegs 0 [PtrV]) "exception handler" (CILayoutFixed 2 [PtrV,IntV]) CIStackFrame mempty)
               (adjSpN' 3 <> returnS (stack .! sp))
          , closure (ClosureInfo (TxtI "h$dataToTag_e") (CIRegs 0 [PtrV]) "data to tag" (CILayoutFixed 0 []) CIStackFrame mempty)
                $ mconcat [ r1 |= if_ (r1 .===. true_) 1 (if_ (typeof r1 .===. jTyObject) (r1 .^ "f" .^ "a" - 1) 0)
                          , adjSpN' 1
                          , returnS (stack .! sp)
                          ]
          -- function application to one argument
          , closure (ClosureInfo (TxtI "h$ap1_e") (CIRegs 0 [PtrV]) "apply1" (CILayoutFixed 2 [PtrV, PtrV]) CIThunk mempty)
               (jVar $ \d1 d2 ->
                   mconcat [ d1 |= closureField1 r1
                           , d2 |= closureField2 r1
                           , appS "h$bh" []
                           , profStat s enterCostCentreThunk
                           , r1 |= d1
                           , r2 |= d2
                           , returnS (app "h$ap_1_1_fast" [])
                           ])
          -- function application to two arguments
          , closure (ClosureInfo (TxtI "h$ap2_e") (CIRegs 0 [PtrV]) "apply2" (CILayoutFixed 3 [PtrV, PtrV, PtrV]) CIThunk mempty)
               (jVar $ \d1 d2 d3 ->
                   mconcat [ d1 |= closureField1 r1
                           , d2 |= closureField2 r1 .^ "d1"
                           , d3 |= closureField2 r1 .^ "d2"
                           , appS "h$bh" []
                           , profStat s enterCostCentreThunk
                           , r1 |= d1
                           , r2 |= d2
                           , r3 |= d3
                           , returnS (app "h$ap_2_2_fast" [])
                           ])
          -- function application to three arguments
          , closure (ClosureInfo (TxtI "h$ap3_e") (CIRegs 0 [PtrV]) "apply3" (CILayoutFixed 4 [PtrV, PtrV, PtrV, PtrV]) CIThunk mempty)
               (jVar $ \d1 d2 d3 d4 ->
                   mconcat [ d1 |= closureField1 r1
                           , d2 |= closureField2 r1 .^ "d1"
                           , d3 |= closureField2 r1 .^ "d2"
                           , d4 |= closureField2 r1 .^ "d3"
                           , appS "h$bh" []
                           , r1 |= d1
                           , r2 |= d2
                           , r3 |= d3
                           , r4 |= d4
                           , returnS (app "h$ap_3_3_fast" [])
                           ])
          -- select first field
          , closure (ClosureInfo (TxtI "h$select1_e") (CIRegs 0 [PtrV]) "select1" (CILayoutFixed 1 [PtrV]) CIThunk mempty)
               (jVar $ \t ->
                   mconcat [ t |= closureField1 r1
                           , adjSp' 3
                           , stack .! (sp - 2) |= r1
                           , stack .! (sp - 1) |= var "h$upd_frame"
                           , stack .! sp |= var "h$select1_ret"
                           , closureEntry  r1 |= var "h$blackhole"
                           , closureField1 r1 |= var "h$currentThread"
                           , closureField2 r1 |= null_
                           , r1 |= t
                           , returnS (app "h$ap_0_0_fast" [])
                           ])
          , closure (ClosureInfo (TxtI "h$select1_ret") (CIRegs 0 [PtrV]) "select1ret" (CILayoutFixed 0 []) CIStackFrame mempty)
               ((r1 |= closureField1 r1)
                <> adjSpN' 1
                <> returnS (app "h$ap_0_0_fast" [])
               )
          -- select second field of a two-field constructor
          , closure (ClosureInfo (TxtI "h$select2_e") (CIRegs 0 [PtrV]) "select2" (CILayoutFixed 1 [PtrV]) CIThunk mempty)
               (jVar $ \t ->
                   mconcat [t |= closureField1 r1
                           , adjSp' 3
                           , stack .! (sp - 2) |= r1
                           , stack .! (sp - 1) |= var "h$upd_frame"
                           , stack .! sp |= var "h$select2_ret"
                           , closureEntry  r1 |= var "h$blackhole"
                           , closureField1 r1 |= var "h$currentThread"
                           , closureField2 r1 |= null_
                           , r1 |= t
                           , returnS (app "h$ap_0_0_fast" [])
                           ]
                  )
          , closure (ClosureInfo (TxtI "h$select2_ret") (CIRegs 0 [PtrV]) "select2ret" (CILayoutFixed 0 []) CIStackFrame mempty)
                        $ mconcat [ r1 |= closureField2 r1
                                  , adjSpN' 1
                                  , returnS (app "h$ap_0_0_fast" [])
                                  ]
          , closure (ClosureInfo (TxtI "h$keepAlive_e") (CIRegs 0 [PtrV]) "keepAlive" (CILayoutFixed 1 [PtrV]) CIStackFrame mempty)
                    (mconcat [ adjSpN' 2
                             , returnS (stack .! sp)
                             ]
                    )
          -- a thunk that just raises a synchronous exception
          , closure (ClosureInfo (TxtI "h$raise_e") (CIRegs 0 [PtrV]) "h$raise_e" (CILayoutFixed 0 []) CIThunk mempty)
               (returnS (app "h$throw" [closureField1 r1, false_]))
          , closure (ClosureInfo (TxtI "h$raiseAsync_e") (CIRegs 0 [PtrV]) "h$raiseAsync_e" (CILayoutFixed 0 []) CIThunk mempty)
               (returnS  (app "h$throw" [closureField1 r1, true_]))
          , closure (ClosureInfo (TxtI "h$raiseAsync_frame") (CIRegs 0 []) "h$raiseAsync_frame" (CILayoutFixed 1 []) CIStackFrame mempty)
               (jVar $ \ex ->
                   mconcat [ ex |= stack .! (sp - 1)
                           , adjSpN' 2
                           , returnS (app "h$throw" [ex, true_])
                           ])
          {- reduce result if it's a thunk, follow if it's an ind
             add this to the stack if you want the outermost result
             to always be reduced to whnf, and not an ind
          -}
          , closure (ClosureInfo (TxtI "h$reduce") (CIRegs 0 [PtrV]) "h$reduce" (CILayoutFixed 0 []) CIStackFrame mempty)
               (ifS (isThunk r1)
                    (returnS (r1 .^ "f"))
                    (adjSpN' 1 <> returnS (stack .! sp))
               )
          , rtsApply s
          , closureTypes
          , closure (ClosureInfo (TxtI "h$runio_e") (CIRegs 0 [PtrV]) "runio" (CILayoutFixed 1 [PtrV]) CIThunk mempty)
                        $ mconcat [ r1 |= closureField1 r1
                                  , stack .! PreInc sp |= var "h$ap_1_0"
                                  , returnS (var "h$ap_1_0")
                                  ]
          , closure (ClosureInfo (TxtI "h$flushStdout_e") (CIRegs 0 []) "flushStdout" (CILayoutFixed 0 []) CIThunk mempty)
                        $ mconcat [ r1 |= var "h$baseZCGHCziIOziHandlezihFlush"
                                  , r2 |= var "h$baseZCGHCziIOziHandleziFDzistdout"
                                  , returnS (app "h$ap_1_1_fast" [])
                                  ]
          , TxtI "h$flushStdout" ||= app "h$static_thunk" [var "h$flushStdout_e"]
          -- the scheduler pushes this frame when suspending a thread that
          -- has not called h$reschedule explicitly
          , closure (ClosureInfo (TxtI "h$restoreThread") (CIRegs 0 []) "restoreThread" CILayoutVariable CIStackFrame mempty)
                (jVar $ \f frameSize nregs ->
                    mconcat [f |= stack .! (sp - 2)
                            , frameSize |= stack .! (sp - 1)
                            , nregs |= frameSize - 3
                            , loop 1 (.<=. nregs)
                                     (\i -> appS "h$setReg" [i, stack .! (sp - 2 - i)] <> postIncrS i)
                            , sp |= sp - frameSize
                            , returnS f
                            ])
          -- return a closure in the stack frame to the next thing on the stack
          , closure (ClosureInfo (TxtI "h$return") (CIRegs 0 []) "return" (CILayoutFixed 1 [PtrV]) CIStackFrame mempty)
                ((r1 |= stack .! (sp - 1))
                 <> adjSpN' 2
                 <> returnS (stack .! sp))
          --  return a function in the stack frame for the next call
          , closure (ClosureInfo (TxtI "h$returnf") (CIRegs 0 [PtrV]) "returnf" (CILayoutFixed 1 [ObjV]) CIStackFrame mempty)
                (jVar $ \r ->
                    mconcat [ r |= stack .! (sp - 1)
                            , adjSpN' 2
                            , returnS r
                            ])
          -- return this function when the scheduler needs to come into action
          -- (yield, delay etc), returning thread needs to push all relevant
          -- registers to stack frame, thread will be resumed by calling the stack top
          , closure (ClosureInfo (TxtI "h$reschedule") (CIRegs 0 []) "reschedule" (CILayoutFixed 0 []) CIThunk mempty)
                (returnS $ var "h$reschedule")
          -- debug thing, insert on stack to dump current result, should be boxed
          , closure (ClosureInfo (TxtI "h$dumpRes") (CIRegs 0 [PtrV]) "dumpRes" (CILayoutFixed 1 [ObjV]) CIThunk mempty)
                (jVar $ \re ->
                    mconcat [ appS "h$log" [jString "h$dumpRes result: " + stack .! (sp-1)]
                            , appS "h$log" [r1]
                            , appS "h$log" [app "h$collectProps" [r1]]
                            , jwhenS ((r1 .^ "f") .&&. (r1 .^ "f" .^ "n"))
                                        (appS "h$log" [jString "name: " + r1 .^ "f" .^ "n"])
                            , jwhenS (ApplExpr (r1 .^ "hasOwnProperty") [jString closureField1_])
                                        (appS "h$log" [jString "d1: " + closureField1 r1])
                            , jwhenS (ApplExpr (r1 .^ "hasOwnProperty") [jString closureField2_])
                                        (appS "h$log" [jString "d2: " + closureField2 r1])
                            , jwhenS (r1 .^ "f") $ mconcat
                                [ re |= New (app "RegExp" [jString "([^\\n]+)\\n(.|\\n)*"])
                                , appS "h$log" [jString "function"
                                                + ApplExpr (ApplExpr ((jString "" + r1 .^ "f") .^ "substring") [0, 50] .^ "replace") [r1, jString "$1"]]
                                ]
                            , adjSpN' 2
                            , r1 |= null_
                            , returnS (stack .! sp)
                            ])
          , closure (ClosureInfo (TxtI "h$resume_e") (CIRegs 0 [PtrV]) "resume" (CILayoutFixed 0 []) CIThunk mempty)
                  (jVar $ \ss ->
                      mconcat [ss |= closureField1 r1
                              , updateThunk' s
                              , loop 0 (.<. ss .^ "length") (\i -> (stack .! (sp+1+i) |= ss .! i)
                                                                   <> postIncrS i)
                              , sp |= sp + ss .^ "length"
                              , r1 |= null_
                              , returnS (stack .! sp)
                              ])
          , closure (ClosureInfo (TxtI "h$unmaskFrame") (CIRegs 0 [PtrV]) "unmask" (CILayoutFixed 0 []) CIStackFrame mempty)
               ((var "h$currentThread" .^ "mask" |= 0)
                <> adjSpN' 1
                -- back to scheduler to give us async exception if pending
                <> ifS (var "h$currentThread" .^ "excep" .^ "length" .>. 0)
                    (push' s [r1, var "h$return"] <> returnS (var "h$reschedule"))
                    (returnS (stack .! sp)))
          , closure (ClosureInfo (TxtI "h$maskFrame") (CIRegs 0 [PtrV]) "mask" (CILayoutFixed 0 []) CIStackFrame mempty)
                ((var "h$currentThread" .^ "mask" |= 2)
                 <> adjSpN' 1
                 <> returnS (stack .! sp))
          , closure (ClosureInfo (TxtI "h$maskUnintFrame") (CIRegs 0 [PtrV]) "maskUnint" (CILayoutFixed 0 []) CIStackFrame mempty)
                ((var "h$currentThread" .^ "mask" |= 1)
                 <> adjSpN' 1
                 <> returnS (stack .! sp))
          , closure (ClosureInfo (TxtI "h$unboxFFIResult") (CIRegs 0 [PtrV]) "unboxFFI" (CILayoutFixed 0 []) CIStackFrame mempty)
               (jVar $ \d ->
                   mconcat [d |= closureField1 r1
                           , loop 0 (.<. d .^ "length") (\i -> appS "h$setReg" [i + 1, d .! i] <> postIncrS i)
                           , adjSpN' 1
                           , returnS (stack .! sp)
                           ])
          , closure (ClosureInfo (TxtI "h$unbox_e") (CIRegs 0 [PtrV]) "unboxed value" (CILayoutFixed 1 [DoubleV]) CIThunk mempty)
               ((r1 |= closureField1 r1) <> returnS (stack .! sp))
          , closure (ClosureInfo (TxtI "h$retryInterrupted") (CIRegs 0 [ObjV]) "retry interrupted operation" (CILayoutFixed 1 [ObjV]) CIStackFrame mempty)
               (jVar $ \a ->
                   mconcat [ a |= stack .! (sp - 1)
                           , adjSpN' 2
                           , returnS (ApplExpr (a .! 0 .^ "apply") [var "this", ApplExpr (a .^ "slice") [1]])
                           ])
          , closure (ClosureInfo (TxtI "h$atomically_e") (CIRegs 0 [PtrV]) "atomic operation" (CILayoutFixed 1 [PtrV]) CIStackFrame mempty)
               (ifS (app "h$stmValidateTransaction" [])
                    (appS "h$stmCommitTransaction" []
                     <> adjSpN' 2
                     <> returnS (stack .! sp))
                    (returnS (app "h$stmStartTransaction" [stack .! (sp - 1)])))

          , closure (ClosureInfo (TxtI "h$stmCatchRetry_e") (CIRegs 0 [PtrV]) "catch retry" (CILayoutFixed 1 [PtrV]) CIStackFrame mempty)
                        (adjSpN' 2
                         <> appS "h$stmCommitTransaction" []
                         <> returnS (stack .! sp))
          , closure (ClosureInfo (TxtI "h$catchStm_e") (CIRegs 0 [PtrV]) "STM catch" (CILayoutFixed 3 [ObjV,PtrV,ObjV]) CIStackFrame mempty)
                       (adjSpN' 4
                       <> appS "h$stmCommitTransaction" []
                       <> returnS (stack .! sp))
          , closure (ClosureInfo (TxtI "h$stmResumeRetry_e") (CIRegs 0 [PtrV]) "resume retry" (CILayoutFixed 0 []) CIStackFrame mempty)
                        (jVar $ \blocked ->
                            mconcat [ jwhenS (stack .! (sp - 2) .!==. var "h$atomically_e")
                                                 (appS "throw" [jString "h$stmResumeRetry_e: unexpected value on stack"])
                                    , blocked |= stack .! (sp - 1)
                                    , adjSpN' 2
                                    , appS "h$stmRemoveBlockedThread" [blocked, var "h$currentThread"]
                                    , returnS (app "h$stmStartTransaction" [stack .! (sp - 1)])
                                    ])
          , closure (ClosureInfo (TxtI "h$lazy_e") (CIRegs 0 [PtrV]) "generic lazy value" (CILayoutFixed 0 []) CIThunk mempty)
                        (jVar $ \x ->
                            mconcat [x |= ApplExpr (closureField1 r1) []
                                    , appS "h$bh" []
                                    , profStat s enterCostCentreThunk
                                    , r1 |= x
                                    , returnS (stack .! sp)
                                    ])
          -- Top-level statements to generate only in profiling mode
          , profStat s (closure (ClosureInfo (TxtI "h$setCcs_e") (CIRegs 0 [PtrV]) "set cost centre stack" (CILayoutFixed 1 [ObjV]) CIStackFrame mempty)
                        (appS "h$restoreCCS" [ stack .! (sp - 1)]
                         <> adjSpN' 2
                         <> returnS (stack .! sp)))
          ]