+-----------------------------------------------------------------------------
+Heap/Stack checks
+
+\begin{code}
+checkCode :: CCheckMacro -> [CAddrMode] -> StixTreeList -> UniqSM StixTreeList
+checkCode macro args assts
+ = getUniqLabelNCG `thenUs` \ ulbl_fail ->
+ getUniqLabelNCG `thenUs` \ ulbl_pass ->
+
+ let args_stix = map amodeToStix args
+ newHp wds = StIndex PtrRep stgHp wds
+ assign_hp wds = StAssign PtrRep stgHp (newHp wds)
+ hp_alloc wds = StAssign IntRep stgHpAlloc wds
+ test_hp = StPrim AddrLeOp [stgHp, stgHpLim]
+ cjmp_hp = StCondJump ulbl_pass test_hp
+
+ newSp wds = StIndex PtrRep stgSp (StPrim IntNegOp [wds])
+ test_sp_pass wds = StPrim AddrGeOp [newSp wds, stgSpLim]
+ test_sp_fail wds = StPrim AddrLtOp [newSp wds, stgSpLim]
+ cjmp_sp_pass wds = StCondJump ulbl_pass (test_sp_pass wds)
+ cjmp_sp_fail wds = StCondJump ulbl_fail (test_sp_fail wds)
+
+ assign_ret r ret = StAssign CodePtrRep r ret
+
+ fail = StLabel ulbl_fail
+ join = StLabel ulbl_pass
+
+ -- see includes/StgMacros.h for explaination of these magic consts
+ aLL_NON_PTRS
+ = IF_ARCH_alpha(16383,65535)
+
+ assign_liveness ptr_regs
+ = StAssign WordRep stgR9
+ (StPrim XorOp [StInt aLL_NON_PTRS, ptr_regs])
+ assign_reentry reentry
+ = StAssign WordRep stgR10 reentry
+ in
+
+ returnUs (
+ case macro of
+ HP_CHK_NP ->
+ let [words,ptrs] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : gc_enter ptrs : join : xs))
+
+ HP_CHK_SEQ_NP ->
+ let [words,ptrs] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : gc_seq ptrs : join : xs))
+
+ STK_CHK_NP ->
+ let [words,ptrs] = args_stix
+ in (\xs -> cjmp_sp_pass words :
+ assts (gc_enter ptrs : join : xs))
+
+ HP_STK_CHK_NP ->
+ let [sp_words,hp_words,ptrs] = args_stix
+ in (\xs -> cjmp_sp_fail sp_words :
+ assign_hp hp_words : cjmp_hp :
+ fail :
+ assts (hp_alloc hp_words : gc_enter ptrs
+ : join : xs))
+
+ HP_CHK ->
+ let [words,ret,r,ptrs] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : assign_ret r ret
+ : gc_chk ptrs : join : xs))
+
+ STK_CHK ->
+ let [words,ret,r,ptrs] = args_stix
+ in (\xs -> cjmp_sp_pass words :
+ assts (assign_ret r ret : gc_chk ptrs : join : xs))
+
+ HP_STK_CHK ->
+ let [sp_words,hp_words,ret,r,ptrs] = args_stix
+ in (\xs -> cjmp_sp_fail sp_words :
+ assign_hp hp_words : cjmp_hp :
+ fail :
+ assts (hp_alloc hp_words : assign_ret r ret
+ : gc_chk ptrs : join : xs))
+
+ HP_CHK_NOREGS ->
+ let [words] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : gc_noregs : join : xs))
+
+ HP_CHK_UNPT_R1 ->
+ let [words] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : gc_unpt_r1 : join : xs))
+
+ HP_CHK_UNBX_R1 ->
+ let [words] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : gc_unbx_r1 : join : xs))
+
+ HP_CHK_F1 ->
+ let [words] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : gc_f1 : join : xs))
+
+ HP_CHK_D1 ->
+ let [words] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : gc_d1 : join : xs))
+
+ HP_CHK_UT_ALT ->
+ let [words,ptrs,nonptrs,r,ret] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : assign_ret r ret
+ : gc_ut ptrs nonptrs
+ : join : xs))
+
+ HP_CHK_GEN ->
+ let [words,liveness,reentry] = args_stix
+ in (\xs -> assign_hp words : cjmp_hp :
+ assts (hp_alloc words : assign_liveness liveness :
+ assign_reentry reentry :
+ gc_gen : join : xs))
+ )
+
+-- Various canned heap-check routines
+
+mkStJump_to_GCentry :: String -> StixTree
+mkStJump_to_GCentry gcname
+-- | opt_Static
+ = StJump NoDestInfo (StCLbl (mkRtsGCEntryLabel gcname))
+-- | otherwise -- it's in a different DLL
+-- = StJump (StInd PtrRep (StLitLbl True sdoc))
+
+gc_chk (StInt 0) = StJump NoDestInfo (regTableEntry CodePtrRep OFFSET_stgChk0)
+gc_chk (StInt 1) = StJump NoDestInfo (regTableEntry CodePtrRep OFFSET_stgChk1)
+gc_chk (StInt n) = mkStJump_to_GCentry ("stg_chk_" ++ show n)
+
+gc_enter (StInt 1) = StJump NoDestInfo (regTableEntry CodePtrRep OFFSET_stgGCEnter1)
+gc_enter (StInt n) = mkStJump_to_GCentry ("stg_gc_enter_" ++ show n)
+
+gc_seq (StInt n) = mkStJump_to_GCentry ("stg_gc_seq_" ++ show n)
+gc_noregs = mkStJump_to_GCentry "stg_gc_noregs"
+gc_unpt_r1 = mkStJump_to_GCentry "stg_gc_unpt_r1"
+gc_unbx_r1 = mkStJump_to_GCentry "stg_gc_unbx_r1"
+gc_f1 = mkStJump_to_GCentry "stg_gc_f1"
+gc_d1 = mkStJump_to_GCentry "stg_gc_d1"
+gc_gen = mkStJump_to_GCentry "stg_gen_chk"
+gc_ut (StInt p) (StInt np)
+ = mkStJump_to_GCentry ("stg_gc_ut_" ++ show p
+ ++ "_" ++ show np)