Re-working of the breakpoint support
[ghc-hetmet.git] / rts / PrimOps.cmm
index e0823e4..bb9fadd 100644 (file)
@@ -970,22 +970,6 @@ isCurrentThreadBoundzh_fast
 
 // Catch retry frame ------------------------------------------------------------
 
-#define CATCH_RETRY_FRAME_ERROR(label) \
-  label { foreign "C" barf("catch_retry_frame incorrectly entered!"); }
-
-CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_0_ret)
-CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_1_ret)
-CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_2_ret)
-CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_3_ret)
-CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_4_ret)
-CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_5_ret)
-CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_6_ret)
-CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_7_ret)
-
-#if MAX_VECTORED_RTN > 8
-#error MAX_VECTORED_RTN has changed: please modify stg_catch_retry_frame too.
-#endif
-
 #if defined(PROFILING)
 #define CATCH_RETRY_FRAME_BITMAP 7
 #define CATCH_RETRY_FRAME_WORDS  5
@@ -996,15 +980,7 @@ CATCH_RETRY_FRAME_ERROR(stg_catch_retry_frame_7_ret)
 
 INFO_TABLE_RET(stg_catch_retry_frame,
               CATCH_RETRY_FRAME_WORDS, CATCH_RETRY_FRAME_BITMAP,
-              CATCH_RETRY_FRAME,
-              stg_catch_retry_frame_0_ret,
-              stg_catch_retry_frame_1_ret,
-              stg_catch_retry_frame_2_ret,
-              stg_catch_retry_frame_3_ret,
-              stg_catch_retry_frame_4_ret,
-              stg_catch_retry_frame_5_ret,
-              stg_catch_retry_frame_6_ret,
-              stg_catch_retry_frame_7_ret)
+              CATCH_RETRY_FRAME)
 {
    W_ r, frame, trec, outer;
    IF_NOT_REG_R1(W_ rval;  rval = Sp(0);  Sp_adj(1); )
@@ -1034,24 +1010,7 @@ INFO_TABLE_RET(stg_catch_retry_frame,
 }
 
 
-// Atomically frame -------------------------------------------------------------
-
-
-#define ATOMICALLY_FRAME_ERROR(label) \
-  label { foreign "C" barf("atomically_frame incorrectly entered!"); }
-
-ATOMICALLY_FRAME_ERROR(stg_atomically_frame_0_ret)
-ATOMICALLY_FRAME_ERROR(stg_atomically_frame_1_ret)
-ATOMICALLY_FRAME_ERROR(stg_atomically_frame_2_ret)
-ATOMICALLY_FRAME_ERROR(stg_atomically_frame_3_ret)
-ATOMICALLY_FRAME_ERROR(stg_atomically_frame_4_ret)
-ATOMICALLY_FRAME_ERROR(stg_atomically_frame_5_ret)
-ATOMICALLY_FRAME_ERROR(stg_atomically_frame_6_ret)
-ATOMICALLY_FRAME_ERROR(stg_atomically_frame_7_ret)
-
-#if MAX_VECTORED_RTN > 8
-#error MAX_VECTORED_RTN has changed: please modify stg_atomically_frame too.
-#endif
+// Atomically frame ------------------------------------------------------------
 
 #if defined(PROFILING)
 #define ATOMICALLY_FRAME_BITMAP 3
@@ -1061,18 +1020,9 @@ ATOMICALLY_FRAME_ERROR(stg_atomically_frame_7_ret)
 #define ATOMICALLY_FRAME_WORDS  2
 #endif
 
-
 INFO_TABLE_RET(stg_atomically_frame,
               ATOMICALLY_FRAME_WORDS, ATOMICALLY_FRAME_BITMAP,
-              ATOMICALLY_FRAME,
-              stg_atomically_frame_0_ret,
-              stg_atomically_frame_1_ret,
-              stg_atomically_frame_2_ret,
-              stg_atomically_frame_3_ret,
-              stg_atomically_frame_4_ret,
-              stg_atomically_frame_5_ret,
-              stg_atomically_frame_6_ret,
-              stg_atomically_frame_7_ret)
+              ATOMICALLY_FRAME)
 {
   W_ frame, trec, valid, next_invariant, q, outer;
   IF_NOT_REG_R1(W_ rval;  rval = Sp(0);  Sp_adj(1); )
@@ -1134,15 +1084,7 @@ INFO_TABLE_RET(stg_atomically_frame,
 
 INFO_TABLE_RET(stg_atomically_waiting_frame,
               ATOMICALLY_FRAME_WORDS, ATOMICALLY_FRAME_BITMAP,
-              ATOMICALLY_FRAME,
-              stg_atomically_frame_0_ret,
-              stg_atomically_frame_1_ret,
-              stg_atomically_frame_2_ret,
-              stg_atomically_frame_3_ret,
-              stg_atomically_frame_4_ret,
-              stg_atomically_frame_5_ret,
-              stg_atomically_frame_6_ret,
-              stg_atomically_frame_7_ret)
+              ATOMICALLY_FRAME)
 {
   W_ frame, trec, valid;
   IF_NOT_REG_R1(W_ rval;  rval = Sp(0);  Sp_adj(1); )
@@ -1169,50 +1111,12 @@ INFO_TABLE_RET(stg_atomically_waiting_frame,
 
 // STM catch frame --------------------------------------------------------------
 
-#define CATCH_STM_FRAME_ENTRY_TEMPLATE(label,ret)                                              \
-   label                                                                                       \
-   {                                                                                           \
-      IF_NOT_REG_R1(W_ rval;  rval = Sp(0);  Sp_adj(1); )                                      \
-      W_ r, frame, trec, outer;                                                                \
-      frame = Sp;                                                                              \
-      trec = StgTSO_trec(CurrentTSO);                                                          \
-      "ptr" outer = foreign "C" stmGetEnclosingTRec(trec "ptr") [];                            \
-      r = foreign "C" stmCommitNestedTransaction(MyCapability() "ptr", trec "ptr") [];         \
-      if (r != 0) {                                                                            \
-        /* Commit succeeded */                                                                 \
-        StgTSO_trec(CurrentTSO) = outer;                                                       \
-        Sp = Sp + SIZEOF_StgCatchSTMFrame;                                                     \
-        IF_NOT_REG_R1(Sp_adj(-1); Sp(0) = rval;)                                               \
-        jump ret;                                                                              \
-      } else {                                                                                 \
-        /* Commit failed */                                                                    \
-        W_ new_trec;                                                                           \
-        "ptr" new_trec = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];        \
-        StgTSO_trec(CurrentTSO) = new_trec;                                                    \
-        R1 = StgCatchSTMFrame_code(frame);                                                     \
-        jump stg_ap_v_fast;                                                                    \
-      }                                                                                                \
-   }
-
 #ifdef REG_R1
 #define SP_OFF 0
 #else
 #define SP_OFF 1
 #endif
 
-CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_0_ret,%RET_VEC(Sp(SP_OFF),0))
-CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_1_ret,%RET_VEC(Sp(SP_OFF),1))
-CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_2_ret,%RET_VEC(Sp(SP_OFF),2))
-CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_3_ret,%RET_VEC(Sp(SP_OFF),3))
-CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_4_ret,%RET_VEC(Sp(SP_OFF),4))
-CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_5_ret,%RET_VEC(Sp(SP_OFF),5))
-CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_6_ret,%RET_VEC(Sp(SP_OFF),6))
-CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_7_ret,%RET_VEC(Sp(SP_OFF),7))
-
-#if MAX_VECTORED_RTN > 8
-#error MAX_VECTORED_RTN has changed: please modify stg_catch_stm_frame too.
-#endif
-
 #if defined(PROFILING)
 #define CATCH_STM_FRAME_BITMAP 3
 #define CATCH_STM_FRAME_WORDS  4
@@ -1228,16 +1132,29 @@ CATCH_STM_FRAME_ENTRY_TEMPLATE(stg_catch_stm_frame_7_ret,%RET_VEC(Sp(SP_OFF),7))
 
 INFO_TABLE_RET(stg_catch_stm_frame,
               CATCH_STM_FRAME_WORDS, CATCH_STM_FRAME_BITMAP,
-              CATCH_STM_FRAME,
-              stg_catch_stm_frame_0_ret,
-              stg_catch_stm_frame_1_ret,
-              stg_catch_stm_frame_2_ret,
-              stg_catch_stm_frame_3_ret,
-              stg_catch_stm_frame_4_ret,
-              stg_catch_stm_frame_5_ret,
-              stg_catch_stm_frame_6_ret,
-              stg_catch_stm_frame_7_ret)
-CATCH_STM_FRAME_ENTRY_TEMPLATE(,%ENTRY_CODE(Sp(SP_OFF)))
+              CATCH_STM_FRAME)
+   {
+      IF_NOT_REG_R1(W_ rval;  rval = Sp(0);  Sp_adj(1); )
+      W_ r, frame, trec, outer;
+      frame = Sp;
+      trec = StgTSO_trec(CurrentTSO);
+      "ptr" outer = foreign "C" stmGetEnclosingTRec(trec "ptr") [];
+      r = foreign "C" stmCommitNestedTransaction(MyCapability() "ptr", trec "ptr") [];
+      if (r != 0) {
+        /* Commit succeeded */
+        StgTSO_trec(CurrentTSO) = outer;
+        Sp = Sp + SIZEOF_StgCatchSTMFrame;
+        IF_NOT_REG_R1(Sp_adj(-1); Sp(0) = rval;)
+        jump Sp(SP_OFF);
+      } else {
+        /* Commit failed */
+        W_ new_trec;
+        "ptr" new_trec = foreign "C" stmStartTransaction(MyCapability() "ptr", outer "ptr") [];
+        StgTSO_trec(CurrentTSO) = new_trec;
+        R1 = StgCatchSTMFrame_code(frame);
+        jump stg_ap_v_fast;
+      }
+   }
 
 
 // Primop definition ------------------------------------------------------------
@@ -1900,17 +1817,17 @@ newBCOzh_fast
     /* R1 = instrs
        R2 = literals
        R3 = ptrs
-       R4 = itbls
-       R5 = arity
-       R6 = bitmap array
+       R4 = arity
+       R5 = bitmap array
     */
     W_ bco, bitmap_arr, bytes, words;
     
-    bitmap_arr = R6;
+    bitmap_arr = R5;
+
     words = BYTES_TO_WDS(SIZEOF_StgBCO) + StgArrWords_words(bitmap_arr);
     bytes = WDS(words);
 
-    ALLOC_PRIM( bytes, R1_PTR&R2_PTR&R3_PTR&R4_PTR&R6_PTR, newBCOzh_fast );
+    ALLOC_PRIM( bytes, R1_PTR&R2_PTR&R3_PTR&R5_PTR, newBCOzh_fast );
 
     bco = Hp - bytes + WDS(1);
     SET_HDR(bco, stg_BCO_info, W_[CCCS]);
@@ -1918,8 +1835,7 @@ newBCOzh_fast
     StgBCO_instrs(bco)     = R1;
     StgBCO_literals(bco)   = R2;
     StgBCO_ptrs(bco)       = R3;
-    StgBCO_itbls(bco)      = R4;
-    StgBCO_arity(bco)      = HALF_W_(R5);
+    StgBCO_arity(bco)      = HALF_W_(R4);
     StgBCO_size(bco)       = HALF_W_(words);
     
     // Copy the arity/bitmap info into the BCO
@@ -1961,34 +1877,48 @@ mkApUpd0zh_fast
     RET_P(ap);
 }
 
-infoPtrzh_fast
-{
-/* args: R1 = closure to analyze */
-   
-  MAYBE_GC(R1_PTR, infoPtrzh_fast);
-
-  W_ info;
-  info = %GET_STD_INFO(R1);
-  RET_N(info);
-}
-
-closurePayloadzh_fast
+unpackClosurezh_fast
 {
 /* args: R1 = closure to analyze */
 // TODO: Consider the absence of ptrs or nonptrs as a special case ?
 
-    MAYBE_GC(R1_PTR, closurePayloadzh_fast);
-
     W_ info, ptrs, nptrs, p, ptrs_arr, nptrs_arr;
     info  = %GET_STD_INFO(R1);
     ptrs  = TO_W_(%INFO_PTRS(info)); 
     nptrs = TO_W_(%INFO_NPTRS(info));
-    p = 0;
 
-    ALLOC_PRIM (SIZEOF_StgMutArrPtrs + WDS(ptrs), R1_PTR, closurePayloadzh_fast);
-    ptrs_arr = Hp - SIZEOF_StgMutArrPtrs - WDS(ptrs) + WDS(1);
+    // Some closures have non-standard layout, so we omit those here.
+    W_ type;
+    type = TO_W_(%INFO_TYPE(info));
+    switch [0 .. N_CLOSURE_TYPES] type {
+    case THUNK_SELECTOR : {
+        ptrs = 1;
+        nptrs = 0;
+        goto out;
+    }
+    case THUNK, THUNK_1_0, THUNK_0_1, THUNK_2_0, THUNK_1_1, 
+         THUNK_0_2, THUNK_STATIC, AP, PAP, AP_STACK, BCO : {
+        ptrs = 0;
+        nptrs = 0;
+        goto out;
+    }
+    default: {
+        goto out;
+    }}
+out:
+
+    W_ ptrs_arr_sz, nptrs_arr_sz;
+    nptrs_arr_sz = SIZEOF_StgArrWords   + WDS(nptrs);
+    ptrs_arr_sz  = SIZEOF_StgMutArrPtrs + WDS(ptrs);
+
+    ALLOC_PRIM (ptrs_arr_sz + nptrs_arr_sz, R1_PTR, unpackClosurezh_fast);
+
+    ptrs_arr  = Hp - nptrs_arr_sz - ptrs_arr_sz + WDS(1);
+    nptrs_arr = Hp - nptrs_arr_sz + WDS(1);
+
     SET_HDR(ptrs_arr, stg_MUT_ARR_PTRS_FROZEN_info, W_[CCCS]);
     StgMutArrPtrs_ptrs(ptrs_arr) = ptrs;
+    p = 0;
 for:
     if(p < ptrs) {
         W_[ptrs_arr + SIZEOF_StgMutArrPtrs + WDS(p)] = StgClosure_payload(R1,p);
@@ -1996,8 +1926,6 @@ for:
         goto for;
     }
     
-    ALLOC_PRIM (SIZEOF_StgArrWords + WDS(nptrs), R1_PTR, closurePayloadzh_fast);
-    nptrs_arr = Hp - SIZEOF_StgArrWords - WDS(nptrs) + WDS(1);
     SET_HDR(nptrs_arr, stg_ARR_WORDS_info, W_[CCCS]);
     StgArrWords_words(nptrs_arr) = nptrs;
     p = 0;
@@ -2007,7 +1935,7 @@ for2:
         p = p + 1;
         goto for2;
     }
-    RET_PP(ptrs_arr, nptrs_arr);
+    RET_NPP(info, ptrs_arr, nptrs_arr);
 }
 
 /* -----------------------------------------------------------------------------
@@ -2216,20 +2144,34 @@ asyncDoProczh_fast
 }
 #endif
 
-/* -----------------------------------------------------------------------------
-  ** temporary **
+// noDuplicate# tries to ensure that none of the thunks under
+// evaluation by the current thread are also under evaluation by
+// another thread.  It relies on *both* threads doing noDuplicate#;
+// the second one will get blocked if they are duplicating some work.
+noDuplicatezh_fast
+{
+    SAVE_THREAD_STATE();
+    ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
+    foreign "C" threadPaused (MyCapability() "ptr", CurrentTSO "ptr") [];
+    
+    if (StgTSO_what_next(CurrentTSO) == ThreadKilled::I16) {
+        jump stg_threadFinished;
+    } else {
+        LOAD_THREAD_STATE();
+        ASSERT(StgTSO_what_next(CurrentTSO) == ThreadRunGHC::I16);
+        jump %ENTRY_CODE(Sp(0));
+    }
+}
+
+getApStackValzh_fast
+{
+   W_ ap_stack, offset, val;
 
-   classes CCallable and CReturnable don't really exist, but the
-   compiler insists on generating dictionaries containing references
-   to GHC_ZcCCallable_static_info etc., so we provide dummy symbols
-   for these.  Some C compilers can't cope with zero-length static arrays,
-   so we have to make these one element long.
-  --------------------------------------------------------------------------- */
+   /* args: R1 = tso, R2 = offset */
+   ap_stack = R1;
+   offset   = R2;
 
-section "rodata" {
-  GHC_ZCCCallable_static_info:   W_ 0;
-}
+   val = StgClosure_payload(ap_stack,offset); 
 
-section "rodata" {
-  GHC_ZCCReturnable_static_info: W_ 0;
+   RET_P(val);
 }