1 /* ----------------------------------------------------------------------------
2 * $Id: ClosureMacros.h,v 1.3 1999/01/13 17:25:52 simonm Exp $
4 * Macros for building and manipulating closures
6 * -------------------------------------------------------------------------- */
8 #ifndef CLOSUREMACROS_H
9 #define CLOSUREMACROS_H
11 /* -----------------------------------------------------------------------------
14 The compiler tries to abstract away from the actual value of this
16 -------------------------------------------------------------------------- */
18 #define _FHS sizeof(StgHeader)
20 /* -----------------------------------------------------------------------------
21 Info tables are slammed up against the entry code, and the label
22 for the info table is at the *end* of the table itself. This
23 inline function adjusts an info pointer to point to the beginning
24 of the table, so we can use standard C structure indexing on it.
26 Note: this works for SRT info tables as long as you don't want to
27 access the SRT, since they are laid out the same with the SRT
28 pointer as the first word in the table.
30 NOTES ABOUT MANGLED C VS. MINI-INTERPRETER:
32 A couple of definitions:
34 "info pointer" The first word of the closure. Might point
35 to either the end or the beginning of the
36 info table, depending on whether we're using
37 the mini interpretter or not. GET_INFO(c)
38 retrieves the info pointer of a closure.
40 "info table" The info table structure associated with a
41 closure. This is always a pointer to the
42 beginning of the structure, so we can
43 use standard C structure indexing to pull out
44 the fields. get_itbl(c) returns a pointer to
45 the info table for closure c.
47 An address of the form xxxx_info points to the end of the info
48 table or the beginning of the info table depending on whether we're
49 mangling or not respectively. So,
51 c->header.info = xxx_info
53 makes absolute sense, whether mangling or not.
55 -------------------------------------------------------------------------- */
57 #define INIT_INFO(i) info : &(i)
58 #define SET_INFO(c,i) ((c)->header.info = (i))
59 #define GET_INFO(c) ((c)->header.info)
61 #if USE_MINIINTERPRETER
62 #define INIT_ENTRY(e) entry : (F_)(e)
63 #define GET_ENTRY(c) ((c)->header.info->entry)
64 #define ENTRY_CODE(info) (stgCast(StgInfoTable*,info)->entry)
65 #define INFO_PTR_TO_STRUCT(info) (info)
66 #define get_itbl(c) ((c)->header.info)
67 static __inline__ StgFunPtr get_entry(const StgInfoTable *itbl) {
71 #define INIT_ENTRY(e) code : {}
72 #define GET_ENTRY(c) stgCast(StgFunPtr,((c)->header.info))
73 #define ENTRY_CODE(info) (info)
74 #define INFO_PTR_TO_STRUCT(info) (stgCast(StgInfoTable*,info) - 1)
75 #define get_itbl(c) (stgCast(StgInfoTable*,(c)->header.info) -1)
76 static __inline__ StgFunPtr get_entry(const StgInfoTable *itbl) {
77 return stgCast(StgFunPtr,itbl+1);
81 /* -----------------------------------------------------------------------------
82 Macros for distinguishing data pointers from code pointers
83 -------------------------------------------------------------------------- */
85 * We use some symbols inserted automatically by the linker to decide
86 * whether a pointer points to text, data, or user space. These tests
87 * assume that text is lower in the address space than data, which in
88 * turn is lower than user allocated memory.
90 * If this assumption is false (say on some strange architecture) then
91 * the tests IS_CODE_PTR and IS_DATA_PTR below will need to be
92 * modified (and that should be all that's necessary).
94 * _start } start of read-only text space
95 * _etext } end of read-only text space
96 * _end } end of read-write data space
99 extern StgFun TEXT_SECTION_END_MARKER_DECL;
100 extern StgFun DATA_SECTION_END_MARKER_DECL;
102 #define IS_CODE_PTR(p) ((P_)(p) < (P_)&TEXT_SECTION_END_MARKER)
103 #define IS_DATA_PTR(p) ((P_)(p) >= (P_)&TEXT_SECTION_END_MARKER && (P_)(p) < (P_)&DATA_SECTION_END_MARKER)
104 #define IS_USER_PTR(p) ((P_)(p) >= (P_)&DATA_SECTION_END_MARKER)
106 /* -----------------------------------------------------------------------------
107 Macros for distinguishing infotables from closures.
109 You'd think it'd be easy to tell an info pointer from a closure pointer:
110 closures live on the heap and infotables are in read only memory. Right?
111 Wrong! Static closures live in read only memory and Hugs allocates
112 infotables for constructors on the (writable) C heap.
114 ToDo: in the combined Hugs-GHC system, the following are but crude
115 approximations. This absolutely has to be fixed.
116 -------------------------------------------------------------------------- */
118 #ifdef USE_MINIINTERPRETER
119 /* yoiks: one of the dreaded pointer equality tests */
120 #define IS_HUGS_CONSTR_INFO(info) (stgCast(StgInfoTable*,info)->entry == stgCast(StgFunPtr,&Hugs_CONSTR_entry))
122 #define IS_HUGS_CONSTR_INFO(info) 0 /* ToDo: more than mildly bogus */
125 #ifdef USE_MINIINTERPRETER
126 /* in the mininterpreter, we put infotables on closures */
127 #define LOOKS_LIKE_GHC_INFO(info) IS_CODE_PTR(info)
129 /* otherwise we have entry pointers on closures */
130 #define LOOKS_LIKE_GHC_INFO(info) IS_CODE_PTR(info)
133 #define LOOKS_LIKE_STATIC(r) IS_DATA_PTR(r)
135 /* -----------------------------------------------------------------------------
136 Macros for calculating how big a closure will be (used during allocation)
137 -------------------------------------------------------------------------- */
139 /* ToDo: replace unsigned int by nat. The only fly in the ointment is that
140 * nat comes from Rts.h which many folk dont include. Sigh!
142 static __inline__ StgOffset AP_sizeW ( unsigned int n_args )
143 { return sizeofW(StgAP_UPD) + n_args; }
145 static __inline__ StgOffset PAP_sizeW ( unsigned int n_args )
146 { return sizeofW(StgPAP) + n_args; }
148 static __inline__ StgOffset CONSTR_sizeW( unsigned int p, unsigned int np )
149 { return sizeofW(StgHeader) + p + np; }
151 static __inline__ StgOffset BCO_sizeW ( unsigned int p, unsigned int np, unsigned int is )
152 { return sizeofW(StgBCO) + p + np + (is+sizeof(StgWord)-1)/sizeof(StgWord); }
154 static __inline__ StgOffset THUNK_SELECTOR_sizeW ( void )
155 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
157 static __inline__ StgOffset BLACKHOLE_sizeW ( void )
158 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
160 static __inline__ StgOffset CAF_sizeW ( void )
161 { return sizeofW(StgCAF); }
163 /* --------------------------------------------------------------------------
165 * ------------------------------------------------------------------------*/
167 static __inline__ StgOffset size_fromITBL( const StgInfoTable* itbl )
168 { return sizeof(StgClosure)
169 + sizeof(StgPtr) * itbl->layout.payload.ptrs
170 + sizeof(StgWord) * itbl->layout.payload.nptrs; }
172 static __inline__ StgOffset sizeW_fromITBL( const StgInfoTable* itbl )
173 { return sizeofW(StgClosure)
174 + sizeofW(StgPtr) * itbl->layout.payload.ptrs
175 + sizeofW(StgWord) * itbl->layout.payload.nptrs; }
177 static __inline__ StgOffset pap_size( StgPAP* x )
178 { return sizeof(StgPAP)
179 + sizeof(StgWord) * x->n_args; }
181 static __inline__ StgOffset pap_sizeW( StgPAP* x )
182 { return PAP_sizeW(x->n_args); }
184 /* These two functions give the same result - but have slightly
187 static __inline__ StgOffset arr_words_sizeW( StgArrWords* x )
188 { return sizeofW(StgArrWords) + x->words; }
189 static __inline__ StgOffset mut_arr_ptrs_sizeW( StgMutArrPtrs* x )
190 { return sizeofW(StgMutArrPtrs) + x->ptrs; }
192 static __inline__ StgWord bco_sizeW( StgBCO* bco )
193 { return BCO_sizeW(bco->n_ptrs,bco->n_words,bco->n_instrs); }
195 static __inline__ StgWord tso_sizeW ( StgTSO *tso )
196 { return TSO_STRUCT_SIZEW + tso->stack_size; }
198 /* -----------------------------------------------------------------------------
199 Macros for building closures
200 -------------------------------------------------------------------------- */
203 #define SET_PROF_HDR(c,ccs_) (c)->header.prof.ccs = ccs_
204 #define SET_STATIC_PROF_HDR(ccs_) prof : { ccs : ccs_ },
206 #define SET_PROF_HDR(c,ccs)
207 #define SET_STATIC_PROF_HDR(ccs)
211 #define SET_GRAN_HDR(c,pe) (c)->header.gran.procs = pe
212 #define SET_STATIC_GRAN_HDR gran : { procs : Everywhere },
214 #define SET_GRAN_HDR(c,pe)
215 #define SET_STATIC_GRAN_HDR
218 /* there is no PAR header, as far as I can tell -- SDM */
221 #define SET_PAR_HDR(c,stuff)
222 #define SET_STATIC_PAR_HDR(stuff)
224 #define SET_PAR_HDR(c,stuff)
225 #define SET_STATIC_PAR_HDR(stuff)
229 #define SET_TICKY_HDR(c,stuff) (c)->header.ticky.updated = stuff
230 #define SET_STATIC_TICKY_HDR(stuff) ticky : { updated : stuff }
232 #define SET_TICKY_HDR(c,stuff)
233 #define SET_STATIC_TICKY_HDR(stuff)
235 #define SET_HDR(c,info,ccs) \
238 SET_GRAN_HDR((StgClosure *)(c),ThisPE); \
239 SET_PAR_HDR((StgClosure *)(c),LOCAL_GA); \
240 SET_PROF_HDR((StgClosure *)(c),ccs); \
241 SET_TICKY_HDR((StgClosure *)(c),0); \
244 #define SET_ARR_HDR(c,info,costCentreStack,n_words) \
245 SET_HDR(c,info,costCentreStack); \
246 (c)->words = n_words;
248 /* -----------------------------------------------------------------------------
249 Static closures are defined as follows:
252 SET_STATIC_HDR(PrelBase_CZh_closure,PrelBase_CZh_info,costCentreStack,const);
254 The info argument must have type 'StgInfoTable' or
255 'StgSRTInfoTable', since we use '&' to get its address in the macro.
256 -------------------------------------------------------------------------- */
258 #define SET_STATIC_HDR(label,info,costCentreStack,closure_class,info_class) \
260 closure_class StgClosure label = { \
261 STATIC_HDR(info,costCentreStack)
263 #define STATIC_HDR(info,ccs) \
266 SET_STATIC_GRAN_HDR \
267 SET_STATIC_PAR_HDR(LOCAL_GA) \
268 SET_STATIC_PROF_HDR(ccs) \
269 SET_STATIC_TICKY_HDR(0) \
272 /* how to get hold of the static link field for a static closure.
274 * Note that we have to use (*cast(T*,&e)) instead of cast(T,e)
275 * because C won't let us take the address of a casted expression. Huh?
277 #define STATIC_LINK(info,p) \
278 (*stgCast(StgClosure**,&((p)->payload[info->layout.payload.ptrs + \
279 info->layout.payload.nptrs])))
280 #define STATIC_LINK2(info,p) \
281 (*stgCast(StgClosure**,&((p)->payload[info->layout.payload.ptrs + \
282 info->layout.payload.nptrs + 1])))
284 /* -----------------------------------------------------------------------------
285 INTLIKE and CHARLIKE closures.
286 -------------------------------------------------------------------------- */
288 #define CHARLIKE_CLOSURE(n) ((P_)&CHARLIKE_closure[n])
289 #define INTLIKE_CLOSURE(n) ((P_)&INTLIKE_closure[(n)-MIN_INTLIKE])
291 /* -----------------------------------------------------------------------------
293 -------------------------------------------------------------------------- */
295 #define payloadPtr( c, i ) (*stgCast(StgPtr*, ((c)->payload+(i))))
296 #define payloadCPtr( c, i ) (*stgCast(StgClosure**, ((c)->payload+(i))))
297 #define payloadWord( c, i ) (*stgCast(StgWord*, ((c)->payload+(i))))
299 /* -----------------------------------------------------------------------------
301 -------------------------------------------------------------------------- */
303 /* constructors don't have SRTs */
304 #define GET_TAG(info) (INFO_PTR_TO_STRUCT(info)->srt_len)
306 /* -----------------------------------------------------------------------------
308 -------------------------------------------------------------------------- */
310 #define bcoConstPtr( bco, i ) (*stgCast(StgPtr*, ((bco)->payload+(i))))
311 #define bcoConstCPtr( bco, i ) (*stgCast(StgClosurePtr*,((bco)->payload+(i))))
312 #define bcoConstInfoPtr( bco, i )(*stgCast(StgInfoTable**,((bco)->payload+(bco)->n_ptrs+i)))
313 #define bcoConstInt( bco, i ) (*stgCast(StgInt*, ((bco)->payload+(bco)->n_ptrs+i)))
314 #define bcoConstInt64( bco, i ) (PK_Int64(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
315 #define bcoConstWord( bco, i ) (*stgCast(StgWord*, ((bco)->payload+(bco)->n_ptrs+i)))
316 #define bcoConstAddr( bco, i ) (*stgCast(StgAddr*, ((bco)->payload+(bco)->n_ptrs+i)))
317 #define bcoConstChar( bco, i ) (*stgCast(StgChar*, ((bco)->payload+(bco)->n_ptrs+i)))
318 #define bcoConstFloat( bco, i ) (PK_FLT(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
319 #define bcoConstDouble( bco, i ) (PK_DBL(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
320 #define bcoInstr( bco, i ) (stgCast(StgNat8*, ((bco)->payload+(bco)->n_ptrs+(bco)->n_words))[i])
322 #endif /* CLOSUREMACROS_H */