[project @ 1999-03-03 19:27:23 by sof]
[ghc-hetmet.git] / ghc / includes / ClosureMacros.h
1 /* ----------------------------------------------------------------------------
2  * $Id: ClosureMacros.h,v 1.7 1999/03/03 19:27:23 sof Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Macros for building and manipulating closures
7  *
8  * -------------------------------------------------------------------------- */
9
10 #ifndef CLOSUREMACROS_H
11 #define CLOSUREMACROS_H
12
13 /* -----------------------------------------------------------------------------
14    Fixed Header Size
15
16    The compiler tries to abstract away from the actual value of this
17    constant.
18    -------------------------------------------------------------------------- */
19
20 #define _FHS  sizeof(StgHeader)
21
22 /* -----------------------------------------------------------------------------
23    Info tables are slammed up against the entry code, and the label
24    for the info table is at the *end* of the table itself.  This
25    inline function adjusts an info pointer to point to the beginning
26    of the table, so we can use standard C structure indexing on it.
27
28    Note: this works for SRT info tables as long as you don't want to
29    access the SRT, since they are laid out the same with the SRT
30    pointer as the first word in the table.
31
32    NOTES ABOUT MANGLED C VS. MINI-INTERPRETER:
33
34    A couple of definitions:
35
36        "info pointer"    The first word of the closure.  Might point
37                          to either the end or the beginning of the
38                          info table, depending on whether we're using
39                          the mini interpretter or not.  GET_INFO(c)
40                          retrieves the info pointer of a closure.
41
42        "info table"      The info table structure associated with a
43                          closure.  This is always a pointer to the
44                          beginning of the structure, so we can
45                          use standard C structure indexing to pull out
46                          the fields.  get_itbl(c) returns a pointer to
47                          the info table for closure c.
48
49    An address of the form xxxx_info points to the end of the info
50    table or the beginning of the info table depending on whether we're
51    mangling or not respectively.  So, 
52
53          c->header.info = xxx_info 
54
55    makes absolute sense, whether mangling or not.
56  
57    -------------------------------------------------------------------------- */
58
59 #define INIT_INFO(i)  info : &(i)
60 #define SET_INFO(c,i) ((c)->header.info = (i))
61 #define GET_INFO(c)   ((c)->header.info)
62
63 #if USE_MINIINTERPRETER
64 #define INIT_ENTRY(e)    entry : (F_)(e)
65 #define GET_ENTRY(c)     ((c)->header.info->entry)
66 #define ENTRY_CODE(info) (stgCast(StgInfoTable*,info)->entry)
67 #define INFO_PTR_TO_STRUCT(info) (info)
68 #define get_itbl(c)      ((c)->header.info)
69 static __inline__ StgFunPtr get_entry(const StgInfoTable *itbl) {
70     return itbl->entry;
71 }
72 #else
73 #define INIT_ENTRY(e)    code : {}
74 #define GET_ENTRY(c)     stgCast(StgFunPtr,((c)->header.info))
75 #define ENTRY_CODE(info) (info)
76 #define INFO_PTR_TO_STRUCT(info) (stgCast(StgInfoTable*,info) - 1)
77 #define get_itbl(c)      (stgCast(StgInfoTable*,(c)->header.info) -1)
78 static __inline__ StgFunPtr get_entry(const StgInfoTable *itbl) {
79     return stgCast(StgFunPtr,itbl+1);
80 }
81 #endif
82
83 /* -----------------------------------------------------------------------------
84    Macros for distinguishing data pointers from code pointers
85    -------------------------------------------------------------------------- */
86 /*
87  * We use some symbols inserted automatically by the linker to decide
88  * whether a pointer points to text, data, or user space.  These tests
89  * assume that text is lower in the address space than data, which in
90  * turn is lower than user allocated memory.  
91  *
92  * If this assumption is false (say on some strange architecture) then
93  * the tests IS_CODE_PTR and IS_DATA_PTR below will need to be
94  * modified (and that should be all that's necessary).
95  *
96  * _start      } start of read-only text space
97  * _etext      } end   of read-only text space
98  * _end } end of read-write data space 
99  */
100 extern StgFun start;
101 extern StgFun TEXT_SECTION_END_MARKER_DECL;
102 extern StgFun DATA_SECTION_END_MARKER_DECL;
103
104 #define IS_CODE_PTR(p) ((P_)(p) < (P_)&TEXT_SECTION_END_MARKER)
105 #define IS_DATA_PTR(p) ((P_)(p) >= (P_)&TEXT_SECTION_END_MARKER && (P_)(p) < (P_)&DATA_SECTION_END_MARKER)
106 #define IS_USER_PTR(p) ((P_)(p) >= (P_)&DATA_SECTION_END_MARKER)
107
108 #ifdef HAVE_WIN32_DLL_SUPPORT
109 extern int is_heap_alloced(const void* x);
110 # define HEAP_ALLOCED(x)  (is_heap_alloced(x))
111 #else
112 # define HEAP_ALLOCED(x)  IS_USER_PTR(x)
113 #endif
114
115 /* When working with Win32 DLLs, static closures are identified by
116    being prefixed with a zero word. This is needed so that we can
117    distinguish between pointers to static closures and (reversed!)
118    info tables.
119
120    This 'scheme' breaks down for closure tables such as CHARLIKE,
121    so we catch these separately.
122    
123    LOOKS_LIKE_STATIC_CLOSURE() 
124        - discriminates between static closures and info tbls
125          (needed by LOOKS_LIKE_GHC_INFO() below - [Win32 DLLs only.])
126    LOOKS_LIKE_STATIC() 
127        - distinguishes between static and heap allocated data.
128  */
129 #ifdef HAVE_WIN32_DLL_SUPPORT
130 #define LOOKS_LIKE_STATIC(r) (!(HEAP_ALLOCED(r)))
131
132 /* Tiresome predicates needed to check for pointers into the closure tables */
133 #define IS_BOOL_CLOSURE(p)  ( stgCast(StgPtr,p) >= stgCast(StgPtr,PrelBase_Bool_closure_tbl) && stgCast(char*,p) <= (stgCast(char*,PrelBase_Bool_closure_tbl) + 2 * sizeof(StgClosure)))
134 #define IS_CHARLIKE_CLOSURE(p)  ( stgCast(StgPtr,p) >= stgCast(StgPtr,CHARLIKE_closure) && stgCast(char*,p) <= (stgCast(char*,CHARLIKE_closure) + 255 * sizeof(StgIntCharlikeClosure)))
135 #define IS_INTLIKE_CLOSURE(p)  ( stgCast(StgPtr,p) >= stgCast(StgPtr,INTLIKE_closure) && stgCast(char*,p) <= (stgCast(char*,INTLIKE_closure) + 32 * sizeof(StgIntCharlikeClosure)))
136
137 #define LOOKS_LIKE_STATIC_CLOSURE(r) (((*(((unsigned long *)(r))-1)) == 0) || IS_CHARLIKE_CLOSURE(r) || IS_INTLIKE_CLOSURE(r) || IS_BOOL_CLOSURE(r))
138 #else
139 #define LOOKS_LIKE_STATIC(r) IS_DATA_PTR(r)
140 #define LOOKS_LIKE_STATIC_CLOSURE(r) IS_DATA_PTR(r)
141 #endif
142
143
144 /* -----------------------------------------------------------------------------
145    Macros for distinguishing infotables from closures.
146    
147    You'd think it'd be easy to tell an info pointer from a closure pointer:
148    closures live on the heap and infotables are in read only memory.  Right?
149    Wrong!  Static closures live in read only memory and Hugs allocates
150    infotables for constructors on the (writable) C heap.
151
152    ToDo: in the combined Hugs-GHC system, the following are but crude
153    approximations.  This absolutely has to be fixed.
154    -------------------------------------------------------------------------- */
155
156 #ifdef USE_MINIINTERPRETER
157 /* yoiks: one of the dreaded pointer equality tests */
158 #define IS_HUGS_CONSTR_INFO(info) (stgCast(StgInfoTable*,info)->entry == stgCast(StgFunPtr,&Hugs_CONSTR_entry))
159 #else
160 #define IS_HUGS_CONSTR_INFO(info) 0 /* ToDo: more than mildly bogus */
161 #endif
162
163 #ifdef USE_MINIINTERPRETER
164 /* in the mininterpreter, we put infotables on closures */
165 #define LOOKS_LIKE_GHC_INFO(info) IS_CODE_PTR(info)
166 #else
167 /* otherwise we have entry pointers on closures */
168 # ifdef HAVE_WIN32_DLL_SUPPORT
169 #  define LOOKS_LIKE_GHC_INFO(info) (!HEAP_ALLOCED(info) && !LOOKS_LIKE_STATIC_CLOSURE(info))
170 # else
171 #  define LOOKS_LIKE_GHC_INFO(info) IS_CODE_PTR(info)
172 # endif
173 #endif
174
175 /* -----------------------------------------------------------------------------
176    Macros for calculating how big a closure will be (used during allocation)
177    -------------------------------------------------------------------------- */
178
179 /* ToDo: replace unsigned int by nat.  The only fly in the ointment is that
180  * nat comes from Rts.h which many folk dont include.  Sigh!
181  */
182 static __inline__ StgOffset AP_sizeW    ( unsigned int n_args )              
183 { return sizeofW(StgAP_UPD) + n_args; }
184
185 static __inline__ StgOffset PAP_sizeW   ( unsigned int n_args )              
186 { return sizeofW(StgPAP)    + n_args; }
187
188 static __inline__ StgOffset CONSTR_sizeW( unsigned int p, unsigned int np )  
189 { return sizeofW(StgHeader) + p + np; }
190
191 static __inline__ StgOffset BCO_sizeW   ( unsigned int p, unsigned int np, unsigned int is ) 
192 { return sizeofW(StgBCO) + p + np + (is+sizeof(StgWord)-1)/sizeof(StgWord); }
193
194 static __inline__ StgOffset THUNK_SELECTOR_sizeW ( void )                    
195 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
196
197 static __inline__ StgOffset BLACKHOLE_sizeW ( void )                    
198 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
199
200 static __inline__ StgOffset CAF_sizeW ( void )                    
201 { return sizeofW(StgCAF); }
202
203 /* --------------------------------------------------------------------------
204  * Sizes of closures
205  * ------------------------------------------------------------------------*/
206
207 static __inline__ StgOffset size_fromITBL( const StgInfoTable* itbl ) 
208 { return sizeof(StgClosure) 
209        + sizeof(StgPtr)  * itbl->layout.payload.ptrs 
210        + sizeof(StgWord) * itbl->layout.payload.nptrs; }
211
212 static __inline__ StgOffset sizeW_fromITBL( const StgInfoTable* itbl ) 
213 { return sizeofW(StgClosure) 
214        + sizeofW(StgPtr)  * itbl->layout.payload.ptrs 
215        + sizeofW(StgWord) * itbl->layout.payload.nptrs; }
216
217 static __inline__ StgOffset pap_size( StgPAP* x )
218 { return sizeof(StgPAP) 
219        + sizeof(StgWord)  * x->n_args; }
220
221 static __inline__ StgOffset pap_sizeW( StgPAP* x )
222 { return PAP_sizeW(x->n_args); }
223
224 /* These two functions give the same result - but have slightly
225  * different types. 
226  */
227 static __inline__ StgOffset arr_words_sizeW( StgArrWords* x )
228 { return sizeofW(StgArrWords) + x->words; }
229 static __inline__ StgOffset mut_arr_ptrs_sizeW( StgMutArrPtrs* x )
230 { return sizeofW(StgMutArrPtrs) + x->ptrs; }
231
232 static __inline__ StgWord bco_sizeW( StgBCO* bco )
233 { return BCO_sizeW(bco->n_ptrs,bco->n_words,bco->n_instrs); }
234
235 static __inline__ StgWord tso_sizeW ( StgTSO *tso )
236 { return TSO_STRUCT_SIZEW + tso->stack_size; }
237
238 /* -----------------------------------------------------------------------------
239    Macros for building closures
240    -------------------------------------------------------------------------- */
241
242 #ifdef PROFILING
243 #define SET_PROF_HDR(c,ccs_)            (c)->header.prof.ccs = ccs_
244 #define SET_STATIC_PROF_HDR(ccs_)       prof : { ccs : ccs_ },
245 #else
246 #define SET_PROF_HDR(c,ccs)
247 #define SET_STATIC_PROF_HDR(ccs)
248 #endif
249
250 #ifdef GRAN
251 #define SET_GRAN_HDR(c,pe)              (c)->header.gran.procs = pe
252 #define SET_STATIC_GRAN_HDR             gran : { procs : Everywhere },
253 #else
254 #define SET_GRAN_HDR(c,pe)
255 #define SET_STATIC_GRAN_HDR
256 #endif
257
258 /* there is no PAR header, as far as I can tell -- SDM */
259
260 #ifdef PAR
261 #define SET_PAR_HDR(c,stuff)
262 #define SET_STATIC_PAR_HDR(stuff)
263 #else
264 #define SET_PAR_HDR(c,stuff)
265 #define SET_STATIC_PAR_HDR(stuff)
266 #endif
267
268 #ifdef TICKY
269 #define SET_TICKY_HDR(c,stuff)          (c)->header.ticky.updated = stuff
270 #define SET_STATIC_TICKY_HDR(stuff)     ticky : { updated : stuff }
271 #else
272 #define SET_TICKY_HDR(c,stuff)
273 #define SET_STATIC_TICKY_HDR(stuff)
274 #endif
275 #define SET_HDR(c,info,ccs) \
276    {                                    \
277         SET_INFO(c,info);                               \
278         SET_GRAN_HDR((StgClosure *)(c),ThisPE);         \
279         SET_PAR_HDR((StgClosure *)(c),LOCAL_GA);        \
280         SET_PROF_HDR((StgClosure *)(c),ccs);            \
281         SET_TICKY_HDR((StgClosure *)(c),0);             \
282    }
283
284 #define SET_ARR_HDR(c,info,costCentreStack,n_words) \
285    SET_HDR(c,info,costCentreStack); \
286    (c)->words = n_words;
287
288 /* -----------------------------------------------------------------------------
289    Static closures are defined as follows:
290
291
292 SET_STATIC_HDR(PrelBase_CZh_closure,PrelBase_CZh_info,costCentreStack,const);
293
294    The info argument must have type 'StgInfoTable' or
295    'StgSRTInfoTable', since we use '&' to get its address in the macro.
296    -------------------------------------------------------------------------- */
297
298 #define SET_STATIC_HDR(label,info,costCentreStack,closure_class,info_class) \
299    info_class info;                        \
300    closure_class StgClosure label = {                   \
301    STATIC_HDR(info,costCentreStack)
302
303 #define STATIC_HDR(info,ccs) \
304         header : {                            \
305                 INIT_INFO(info),              \
306                 SET_STATIC_GRAN_HDR           \
307                 SET_STATIC_PAR_HDR(LOCAL_GA)  \
308                 SET_STATIC_PROF_HDR(ccs)       \
309                 SET_STATIC_TICKY_HDR(0)       \
310         }
311
312 /* how to get hold of the static link field for a static closure.
313  *
314  * Note that we have to use (*cast(T*,&e)) instead of cast(T,e)
315  * because C won't let us take the address of a casted expression. Huh?
316  */
317 #define STATIC_LINK(info,p) \
318    (*stgCast(StgClosure**,&((p)->payload[info->layout.payload.ptrs + \
319                                         info->layout.payload.nptrs])))
320 #define STATIC_LINK2(info,p) \
321    (*stgCast(StgClosure**,&((p)->payload[info->layout.payload.ptrs + \
322                                         info->layout.payload.nptrs + 1])))
323
324 /* -----------------------------------------------------------------------------
325    INTLIKE and CHARLIKE closures.
326    -------------------------------------------------------------------------- */
327
328 #define CHARLIKE_CLOSURE(n) ((P_)&CHARLIKE_closure[n])
329 #define INTLIKE_CLOSURE(n)  ((P_)&INTLIKE_closure[(n)-MIN_INTLIKE])
330
331 /* -----------------------------------------------------------------------------
332    Payload access
333    -------------------------------------------------------------------------- */
334
335 #define payloadPtr( c, i )    (*stgCast(StgPtr*,       ((c)->payload+(i))))
336 #define payloadCPtr( c, i )   (*stgCast(StgClosure**,  ((c)->payload+(i))))
337 #define payloadWord( c, i )   (*stgCast(StgWord*,      ((c)->payload+(i))))
338
339 /* -----------------------------------------------------------------------------
340    CONSTRs.
341    -------------------------------------------------------------------------- */
342
343 /* constructors don't have SRTs */
344 #define GET_TAG(info) (INFO_PTR_TO_STRUCT(info)->srt_len)
345
346 /* -----------------------------------------------------------------------------
347    BCOs.
348    -------------------------------------------------------------------------- */
349
350 #define bcoConstPtr( bco, i )    (*stgCast(StgPtr*,       ((bco)->payload+(i))))
351 #define bcoConstCPtr( bco, i )   (*stgCast(StgClosurePtr*,((bco)->payload+(i))))
352 #define bcoConstInfoPtr( bco, i )(*stgCast(StgInfoTable**,((bco)->payload+(bco)->n_ptrs+i)))
353 #define bcoConstInt( bco, i )    (*stgCast(StgInt*,       ((bco)->payload+(bco)->n_ptrs+i)))
354 #define bcoConstInt64( bco, i )  (PK_Int64(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
355 #define bcoConstWord( bco, i )   (*stgCast(StgWord*,      ((bco)->payload+(bco)->n_ptrs+i)))
356 #define bcoConstAddr( bco, i )   (*stgCast(StgAddr*,      ((bco)->payload+(bco)->n_ptrs+i)))
357 #define bcoConstChar( bco, i )   (*stgCast(StgChar*,      ((bco)->payload+(bco)->n_ptrs+i)))
358 #define bcoConstFloat( bco, i )  (PK_FLT(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
359 #define bcoConstDouble( bco, i ) (PK_DBL(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
360 #define bcoInstr( bco, i )       (stgCast(StgWord8*,      ((bco)->payload+(bco)->n_ptrs+(bco)->n_words))[i])
361
362 #endif /* CLOSUREMACROS_H */