/* ----------------------------------------------------------------------------- * * (c) The University of Glasgow, 1998-2004 * * Canned "Standard Form" Thunks * * This file is written in a subset of C--, extended with various * features specific to GHC. It is compiled by GHC directly. For the * syntax of .cmm files, see the parser in ghc/compiler/GHC/Cmm/Parser.y. * * ---------------------------------------------------------------------------*/ #include "Cmm.h" #include "Updates.h" import ghczmprim_GHCziCString_unpackCStringzh_info; import ghczmprim_GHCziCString_unpackCStringUtf8zh_info; /* ----------------------------------------------------------------------------- The code for a thunk that simply extracts a field from a single-constructor datatype depends only on the offset of the field to be selected. Here we define some canned "selector" thunks that do just that; any selector thunk appearing in a program will refer to one of these instead of being compiled independently. The garbage collector spots selector thunks and reduces them if possible, in order to avoid space leaks resulting from lazy pattern matching. -------------------------------------------------------------------------- */ #if defined(PROFILING) #define SAVE_CCS W_ saved_ccs; saved_ccs = CCCS; #define RESTORE_CCS CCCS = saved_ccs; #else #define SAVE_CCS /* nothing */ #define RESTORE_CCS /* nothing */ #endif /* * TODO: On return, we can use a more efficient * untagging (we know the constructor tag). * * When entering stg_sel_#_upd, we know R1 points to its closure, * so it's untagged. * The payload might be a thunk or a constructor, * so we enter it. * * When returning, we know for sure it is a constructor, * so we untag it before accessing the field. * */ #if defined(PROFILING) /* When profiling, we cannot shortcut by checking the tag, * because LDV profiling relies on entering closures to mark them as * "used". * * Note [untag for prof] * ~~~~~~~~~~~~~~~~~~~~~ * When we enter a closure, the convention is * that the closure pointer passed in the first argument is * *untagged*. Without profiling we don't have to worry about this, * because we never enter a tagged pointer. */ #define NEED_EVAL(__x__) 1 #else #define NEED_EVAL(__x__) GETTAG(__x__) == 0 #endif #define SELECTOR_CODE_UPD(offset) \ INFO_TABLE_SELECTOR(stg_sel_##offset##_upd, offset, THUNK_SELECTOR, "stg_sel_upd", "stg_sel_upd") \ (P_ node) \ { \ P_ selectee, field, dest; \ TICK_ENT_DYN_THK(); \ STK_CHK_NP(node); \ UPD_BH_UPDATABLE(node); \ LDV_ENTER(node); \ selectee = StgThunk_payload(node,0); \ push (UPDATE_FRAME_FIELDS(,,stg_upd_frame_info,CCCS,0,node)) { \ ENTER_CCS_THUNK(node); \ if (NEED_EVAL(selectee)) { \ SAVE_CCS; \ dest = UNTAG_IF_PROF(selectee); /* Note [untag for prof] */ \ (P_ constr) = call %GET_ENTRY(dest) (dest); \ RESTORE_CCS; \ selectee = constr; \ } \ field = StgClosure_payload(UNTAG(selectee),offset); \ jump stg_ap_0_fast(field); \ } \ } /* NOTE: no need to ENTER() here, we know the closure cannot evaluate to a function, because we're going to do a field selection on the result. */ SELECTOR_CODE_UPD(0) SELECTOR_CODE_UPD(1) SELECTOR_CODE_UPD(2) SELECTOR_CODE_UPD(3) SELECTOR_CODE_UPD(4) SELECTOR_CODE_UPD(5) SELECTOR_CODE_UPD(6) SELECTOR_CODE_UPD(7) SELECTOR_CODE_UPD(8) SELECTOR_CODE_UPD(9) SELECTOR_CODE_UPD(10) SELECTOR_CODE_UPD(11) SELECTOR_CODE_UPD(12) SELECTOR_CODE_UPD(13) SELECTOR_CODE_UPD(14) SELECTOR_CODE_UPD(15) #define SELECTOR_CODE_NOUPD(offset) \ INFO_TABLE_SELECTOR(stg_sel_##offset##_noupd, offset, THUNK_SELECTOR, "stg_sel_noupd", "stg_sel_noupd") \ (P_ node) \ { \ P_ selectee, field, dest; \ TICK_ENT_DYN_THK(); \ STK_CHK_NP(node); \ UPD_BH_UPDATABLE(node); \ LDV_ENTER(node); \ selectee = StgThunk_payload(node,0); \ ENTER_CCS_THUNK(node); \ if (NEED_EVAL(selectee)) { \ SAVE_CCS; \ dest = UNTAG_IF_PROF(selectee); /* Note [untag for prof] */ \ (P_ constr) = call %GET_ENTRY(dest) (dest); \ RESTORE_CCS; \ selectee = constr; \ } \ field = StgClosure_payload(UNTAG(selectee),offset); \ jump stg_ap_0_fast(field); \ } SELECTOR_CODE_NOUPD(0) SELECTOR_CODE_NOUPD(1) SELECTOR_CODE_NOUPD(2) SELECTOR_CODE_NOUPD(3) SELECTOR_CODE_NOUPD(4) SELECTOR_CODE_NOUPD(5) SELECTOR_CODE_NOUPD(6) SELECTOR_CODE_NOUPD(7) SELECTOR_CODE_NOUPD(8) SELECTOR_CODE_NOUPD(9) SELECTOR_CODE_NOUPD(10) SELECTOR_CODE_NOUPD(11) SELECTOR_CODE_NOUPD(12) SELECTOR_CODE_NOUPD(13) SELECTOR_CODE_NOUPD(14) SELECTOR_CODE_NOUPD(15) /* ----------------------------------------------------------------------------- Apply thunks An apply thunk is a thunk of the form let z = [x1...xn] \u x1...xn in ... We pre-compile some of these because the code is always the same. These have to be independent of the update frame size, so the code works when profiling etc. -------------------------------------------------------------------------- */ /* stg_ap_1_upd_info is a bit redundant, but there appears to be a bug * in the compiler that means stg_ap_1 is generated occasionally (ToDo) */ INFO_TABLE(stg_ap_1_upd,1,0,THUNK_1_0,"stg_ap_1_upd_info","stg_ap_1_upd_info") (P_ node) { TICK_ENT_DYN_THK(); STK_CHK_NP(node); UPD_BH_UPDATABLE(node); LDV_ENTER(node); push (UPDATE_FRAME_FIELDS(,,stg_upd_frame_info, CCCS, 0, node)) { ENTER_CCS_THUNK(node); jump stg_ap_0_fast (StgThunk_payload(node,0)); } } INFO_TABLE(stg_ap_2_upd,2,0,THUNK_2_0,"stg_ap_2_upd_info","stg_ap_2_upd_info") (P_ node) { TICK_ENT_DYN_THK(); STK_CHK_NP(node); UPD_BH_UPDATABLE(node); LDV_ENTER(node); push (UPDATE_FRAME_FIELDS(,,stg_upd_frame_info, CCCS, 0, node)) { ENTER_CCS_THUNK(node); jump stg_ap_p_fast (StgThunk_payload(node,0), StgThunk_payload(node,1)); } } INFO_TABLE(stg_ap_3_upd,3,0,THUNK,"stg_ap_3_upd_info","stg_ap_3_upd_info") (P_ node) { TICK_ENT_DYN_THK(); STK_CHK_NP(node); UPD_BH_UPDATABLE(node); LDV_ENTER(node); push (UPDATE_FRAME_FIELDS(,,stg_upd_frame_info, CCCS, 0, node)) { ENTER_CCS_THUNK(node); jump stg_ap_pp_fast (StgThunk_payload(node,0), StgThunk_payload(node,1), StgThunk_payload(node,2)); } } INFO_TABLE(stg_ap_4_upd,4,0,THUNK,"stg_ap_4_upd_info","stg_ap_4_upd_info") (P_ node) { TICK_ENT_DYN_THK(); STK_CHK_NP(node); UPD_BH_UPDATABLE(node); LDV_ENTER(node); push (UPDATE_FRAME_FIELDS(,,stg_upd_frame_info, CCCS, 0, node)) { ENTER_CCS_THUNK(node); jump stg_ap_ppp_fast (StgThunk_payload(node,0), StgThunk_payload(node,1), StgThunk_payload(node,2), StgThunk_payload(node,3)); } } INFO_TABLE(stg_ap_5_upd,5,0,THUNK,"stg_ap_5_upd_info","stg_ap_5_upd_info") (P_ node) { TICK_ENT_DYN_THK(); STK_CHK_NP(node); UPD_BH_UPDATABLE(node); LDV_ENTER(node); push (UPDATE_FRAME_FIELDS(,,stg_upd_frame_info, CCCS, 0, node)) { ENTER_CCS_THUNK(node); jump stg_ap_pppp_fast (StgThunk_payload(node,0), StgThunk_payload(node,1), StgThunk_payload(node,2), StgThunk_payload(node,3), StgThunk_payload(node,4)); } } INFO_TABLE(stg_ap_6_upd,6,0,THUNK,"stg_ap_6_upd_info","stg_ap_6_upd_info") (P_ node) { TICK_ENT_DYN_THK(); STK_CHK_NP(node); UPD_BH_UPDATABLE(node); LDV_ENTER(node); push (UPDATE_FRAME_FIELDS(,,stg_upd_frame_info, CCCS, 0, node)) { ENTER_CCS_THUNK(node); jump stg_ap_ppppp_fast (StgThunk_payload(node,0), StgThunk_payload(node,1), StgThunk_payload(node,2), StgThunk_payload(node,3), StgThunk_payload(node,4), StgThunk_payload(node,5)); } } INFO_TABLE(stg_ap_7_upd,7,0,THUNK,"stg_ap_7_upd_info","stg_ap_7_upd_info") (P_ node) { TICK_ENT_DYN_THK(); STK_CHK_NP(node); UPD_BH_UPDATABLE(node); LDV_ENTER(node); push (UPDATE_FRAME_FIELDS(,,stg_upd_frame_info, CCCS, 0, node)) { ENTER_CCS_THUNK(node); jump stg_ap_pppppp_fast (StgThunk_payload(node,0), StgThunk_payload(node,1), StgThunk_payload(node,2), StgThunk_payload(node,3), StgThunk_payload(node,4), StgThunk_payload(node,5), StgThunk_payload(node,6)); } } /* ----------------------------------------------------------------------------- Making strings -------------------------------------------------------------------------- */ /* * Note [unpack_cstring closures] * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Strings are extremely common. In Core they will typically manifest as the * a pair of top-level bindings: * * s :: String * s = unpackCString# s# * * s# :: Addr# * s# = "hello world"# * * It turns out that `s` is a non-trivial amount of code which is duplicated * for every `String` literal. To avoid this duplicate, we have a standard * string-unpacking closure, unpack_cstring. Note that currently we only do * this for ASCII strings; strings mentioning non-ASCII characters are * represented by CAF applications of unpackCStringUtf8# as before. * * unpack_cstring closures are similar to standard THUNK_STATIC closures but * with a non-GC pointer to a C-string at the end (the "extra" pointer). * We must place this extra pointer at the end of the closure to ensure that * it has a similar layout to a normal THUNK_STATIC closure, which has no space * for free variables (since these would be contained in the thunk's code and SRT). * * When it is evaluated, an stg_unpack_cstring closure is updated to be an * indirection to the resulting [Char], just as a normal unpackCString# thunk * would be. * * Closure layout: * * ┌───────────────────┐ ┌──► ┌──────────────────────────┐ * │ stg_unpack_cstring│ │ │ "hello world ..." │ * ├───────────────────┤ │ └──────────────────────────┘ * │ indirectee │ │ * ├───────────────────┤ │ * │ static_link │ │ * ├───────────────────┤ │ * │ saved_info │ │ * ├───────────────────┤ │ * │ the_string ─┼───────┘ * └───────────────────┘ * */ stg_do_unpack_cstring(P_ node, P_ newCAF_ret) { STK_CHK_PP(WDS(SIZEOF_StgUpdateFrame), stg_do_unpack_cstring, node, newCAF_ret); W_ str; str = StgThunk_payload(node, 2); push (UPDATE_FRAME_FIELDS(,,stg_bh_upd_frame_info, CCCS, 0, newCAF_ret)) { jump %ENTRY_CODE(ghczmprim_GHCziCString_unpackCStringzh_info)(node, str); } } INFO_TABLE(stg_unpack_cstring, 0, 0, THUNK_STATIC, "stg_unpack_cstring", "stg_unpack_cstring") (P_ node) { W_ newCAF_ret; (newCAF_ret) = ccall newCAF(BaseReg "ptr", node "ptr"); if (newCAF_ret == 0) { // We raced with another thread to evaluate the CAF and they won; // `node` should now be an indirection. ENTER(node); } else { jump stg_do_unpack_cstring(node, newCAF_ret); } } stg_do_unpack_cstring_utf8(P_ node, P_ newCAF_ret) { STK_CHK_PP(WDS(SIZEOF_StgUpdateFrame), stg_do_unpack_cstring_utf8, node, newCAF_ret); W_ str; str = StgThunk_payload(node, 2); push (UPDATE_FRAME_FIELDS(,,stg_bh_upd_frame_info, CCCS, 0, newCAF_ret)) { jump %ENTRY_CODE(ghczmprim_GHCziCString_unpackCStringUtf8zh_info)(node, str); } } INFO_TABLE(stg_unpack_cstring_utf8, 0, 0, THUNK_STATIC, "stg_unpack_cstring_utf8", "stg_unpack_cstring_utf8") (P_ node) { W_ newCAF_ret; (newCAF_ret) = ccall newCAF(BaseReg "ptr", node "ptr"); if (newCAF_ret == 0) { // We raced with another thread to evaluate the CAF and they won; // `node` should now be an indirection. ENTER(node); } else { jump stg_do_unpack_cstring_utf8(node, newCAF_ret); } }