Lightweight ticky-ticky profiling
[ghc-hetmet.git] / rts / Updates.cmm
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2004
4  *
5  * Code to perform updates.
6  *
7  * This file is written in a subset of C--, extended with various
8  * features specific to GHC.  It is compiled by GHC directly.  For the
9  * syntax of .cmm files, see the parser in ghc/compiler/cmm/CmmParse.y.
10  *
11  * ---------------------------------------------------------------------------*/
12
13
14 #include "Cmm.h"
15 #include "Updates.h"
16 #include "StgLdvProf.h"
17
18 /*
19   The update frame return address must be *polymorphic*, that means
20   we have to cope with both vectored and non-vectored returns.  This
21   is done by putting the return vector right before the info table, and
22   having a standard direct return address after the info table (pointed
23   to by the return address itself, as usual).
24
25   Each entry in the vector table points to a specialised entry code fragment
26   that knows how to return after doing the update.  It would be possible to
27   use a single generic piece of code that simply entered the return value
28   to return, but it's quicker this way.  The direct return code of course
29   just does another direct return when it's finished.
30 */
31
32 /* on entry to the update code
33    (1) R1 points to the closure being returned
34    (2) Sp points to the update frame
35 */
36
37 /* The update fragment has been tuned so as to generate good
38    code with gcc, which accounts for some of the strangeness in the
39    way it is written.  
40
41    In particular, the JMP_(ret) bit is passed down and pinned on the
42    end of each branch (there end up being two major branches in the
43    code), since we don't mind duplicating this jump.
44 */
45
46 #define UPD_FRAME_ENTRY_TEMPLATE(label,ind_info,ret)                    \
47         label                                                           \
48         {                                                               \
49           W_ updatee;                                                   \
50                                                                         \
51           updatee = StgUpdateFrame_updatee(Sp);                         \
52                                                                         \
53           /* remove the update frame from the stack */                  \
54           Sp = Sp + SIZEOF_StgUpdateFrame;                              \
55                                                                         \
56           /* ToDo: it might be a PAP, so we should check... */          \
57           TICK_UPD_CON_IN_NEW(sizeW_fromITBL(%GET_STD_INFO(updatee)));  \
58                                                                         \
59           UPD_SPEC_IND(updatee, ind_info, R1, jump (ret));              \
60         }
61
62 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_0_ret,stg_IND_0_info,%RET_VEC(Sp(0),0))
63 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_1_ret,stg_IND_1_info,%RET_VEC(Sp(0),1))
64 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_2_ret,stg_IND_2_info,%RET_VEC(Sp(0),2))
65 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_3_ret,stg_IND_3_info,%RET_VEC(Sp(0),3))
66 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_4_ret,stg_IND_4_info,%RET_VEC(Sp(0),4))
67 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_5_ret,stg_IND_5_info,%RET_VEC(Sp(0),5))
68 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_6_ret,stg_IND_6_info,%RET_VEC(Sp(0),6))
69 UPD_FRAME_ENTRY_TEMPLATE(stg_upd_frame_7_ret,stg_IND_7_info,%RET_VEC(Sp(0),7))
70
71 #if MAX_VECTORED_RTN > 8
72 #error MAX_VECTORED_RTN has changed: please modify stg_upd_frame too.
73 #endif
74
75 /*
76   Make sure this table is big enough to handle the maximum vectored
77   return size!
78   */
79
80 #if defined(PROFILING)
81 #define UPD_FRAME_BITMAP 3
82 #define UPD_FRAME_WORDS  3
83 #else
84 #define UPD_FRAME_BITMAP 0
85 #define UPD_FRAME_WORDS  1
86 #endif
87
88 /* this bitmap indicates that the first word of an update frame is a
89  * non-pointer - this is the update frame link.  (for profiling,
90  * there's a cost-centre-stack in there too).
91  */
92
93 INFO_TABLE_RET( stg_upd_frame, 
94             UPD_FRAME_WORDS, UPD_FRAME_BITMAP, UPDATE_FRAME,
95             stg_upd_frame_0_ret,
96             stg_upd_frame_1_ret,
97             stg_upd_frame_2_ret,
98             stg_upd_frame_3_ret,
99             stg_upd_frame_4_ret,
100             stg_upd_frame_5_ret,
101             stg_upd_frame_6_ret,
102             stg_upd_frame_7_ret
103             )
104 UPD_FRAME_ENTRY_TEMPLATE(,stg_IND_direct_info,%ENTRY_CODE(Sp(0)))
105
106
107 INFO_TABLE_RET( stg_marked_upd_frame, 
108             UPD_FRAME_WORDS, UPD_FRAME_BITMAP, UPDATE_FRAME,
109             stg_upd_frame_0_ret,
110             stg_upd_frame_1_ret,
111             stg_upd_frame_2_ret,
112             stg_upd_frame_3_ret,
113             stg_upd_frame_4_ret,
114             stg_upd_frame_5_ret,
115             stg_upd_frame_6_ret,
116             stg_upd_frame_7_ret
117             )
118 UPD_FRAME_ENTRY_TEMPLATE(,stg_IND_direct_info,%ENTRY_CODE(Sp(0)))
119
120 /*-----------------------------------------------------------------------------
121   Seq frames 
122
123   We don't have a primitive seq# operator: it is just a 'case'
124   expression whose scrutinee has either a polymorphic or function type
125   (constructor types can be handled by normal 'case' expressions).
126
127   To handle a polymorphic/function typed seq, we push a SEQ frame on
128   the stack.  This is a polymorphic activation record that just pops
129   itself and returns (in a non-vectored way) when entered.  The
130   purpose of the SEQ frame is to avoid having to make a polymorphic return
131   point for each polymorphic case expression.  
132
133   Another way of looking at it: the SEQ frame turns a vectored return
134   into a direct one.
135   -------------------------------------------------------------------------- */
136
137 #if MAX_VECTORED_RTN > 8
138 #error MAX_VECTORED_RTN has changed: please modify stg_seq_frame too.
139 #endif
140
141 INFO_TABLE_RET( stg_seq_frame, 0/* words */, 0/* bitmap */, RET_SMALL,
142         RET_LBL(stg_seq_frame), /* 0 */
143         RET_LBL(stg_seq_frame), /* 1 */
144         RET_LBL(stg_seq_frame), /* 2 */
145         RET_LBL(stg_seq_frame), /* 3 */
146         RET_LBL(stg_seq_frame), /* 4 */
147         RET_LBL(stg_seq_frame), /* 5 */
148         RET_LBL(stg_seq_frame), /* 6 */
149         RET_LBL(stg_seq_frame)  /* 7 */
150         )
151 {
152    Sp_adj(1);
153    jump %ENTRY_CODE(Sp(0));
154 }