[project @ 1999-10-27 09:57:48 by simonmar]
[ghc-hetmet.git] / ghc / includes / ClosureMacros.h
1 /* ----------------------------------------------------------------------------
2  * $Id: ClosureMacros.h,v 1.20 1999/10/27 09:57:48 simonmar 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 #define GET_ENTRY(c)  (ENTRY_CODE(GET_INFO(c)))
63 #define get_itbl(c)   (INFO_PTR_TO_STRUCT((c)->header.info))
64
65 #ifdef TABLES_NEXT_TO_CODE
66 #define INIT_ENTRY(e)    code : {}
67 #define ENTRY_CODE(info) (info)
68 #define INFO_PTR_TO_STRUCT(info) ((StgInfoTable *)(info) - 1)
69 static __inline__ StgFunPtr get_entry(const StgInfoTable *itbl) {
70     return (StgFunPtr)(itbl+1);
71 }
72 #else
73 #define INIT_ENTRY(e)    entry : (F_)(e)
74 #define ENTRY_CODE(info) (((StgInfoTable *)info)->entry)
75 #define INFO_PTR_TO_STRUCT(info) ((StgInfoTable *)info)
76 static __inline__ StgFunPtr get_entry(const StgInfoTable *itbl) {
77     return itbl->entry;
78 }
79 #endif
80
81 /* -----------------------------------------------------------------------------
82    Macros for distinguishing data pointers from code pointers
83    -------------------------------------------------------------------------- */
84 /*
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.  
89  *
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).
93  *
94  * _start      } start of read-only text space
95  * _etext      } end   of read-only text space
96  * _end } end of read-write data space 
97  */
98 extern StgFun start;
99
100 extern void* TEXT_SECTION_END_MARKER_DECL;
101 extern void* DATA_SECTION_END_MARKER_DECL;
102
103 #ifdef INTERPRETER
104 /* Take into account code sections in dynamically loaded object files. */
105 #define IS_CODE_PTR(p) (  ((P_)(p) < (P_)&TEXT_SECTION_END_MARKER) \
106                        || is_dynamically_loaded_code_or_rodata_ptr(p) )
107 #define IS_DATA_PTR(p) ( ((P_)(p) >= (P_)&TEXT_SECTION_END_MARKER && \
108                           (P_)(p) < (P_)&DATA_SECTION_END_MARKER) \
109                        || is_dynamically_loaded_rwdata_ptr(p) )
110 #define IS_USER_PTR(p) ( ((P_)(p) >= (P_)&DATA_SECTION_END_MARKER) \
111                        && is_not_dynamically_loaded_ptr(p) )
112 #else
113 #define IS_CODE_PTR(p) ((P_)(p) < (P_)&TEXT_SECTION_END_MARKER)
114 #define IS_DATA_PTR(p) ((P_)(p) >= (P_)&TEXT_SECTION_END_MARKER && (P_)(p) < (P_)&DATA_SECTION_END_MARKER)
115 #define IS_USER_PTR(p) ((P_)(p) >= (P_)&DATA_SECTION_END_MARKER)
116 #endif
117
118
119
120 #ifdef HAVE_WIN32_DLL_SUPPORT
121 extern int is_heap_alloced(const void* x);
122 # define HEAP_ALLOCED(x)  (is_heap_alloced(x))
123 #else
124 # define HEAP_ALLOCED(x)  IS_USER_PTR(x)
125 #endif
126
127 /* When working with Win32 DLLs, static closures are identified by
128    being prefixed with a zero word. This is needed so that we can
129    distinguish between pointers to static closures and (reversed!)
130    info tables.
131
132    This 'scheme' breaks down for closure tables such as CHARLIKE,
133    so we catch these separately.
134    
135    LOOKS_LIKE_STATIC_CLOSURE() 
136        - discriminates between static closures and info tbls
137          (needed by LOOKS_LIKE_GHC_INFO() below - [Win32 DLLs only.])
138    LOOKS_LIKE_STATIC() 
139        - distinguishes between static and heap allocated data.
140  */
141 #ifdef HAVE_WIN32_DLL_SUPPORT
142 #define LOOKS_LIKE_STATIC(r) (!(HEAP_ALLOCED(r)))
143
144 /* Tiresome predicates needed to check for pointers into the closure tables */
145 #define IS_CHARLIKE_CLOSURE(p)  ( (P_)(p) >= (P_)CHARLIKE_closure && (char*)(p) <= ((char*)CHARLIKE_closure + 255 * sizeof(StgIntCharlikeClosure)) )
146 #define IS_INTLIKE_CLOSURE(p)  ( (P_)(p) >= (P_)INTLIKE_closure && (char*)(p) <= ((char*)INTLIKE_closure + 32 * sizeof(StgIntCharlikeClosure)) )
147
148 #define LOOKS_LIKE_STATIC_CLOSURE(r) (((*(((unsigned long *)(r))-1)) == 0) || IS_CHARLIKE_CLOSURE(r) || IS_INTLIKE_CLOSURE(r))
149 #else
150 #define LOOKS_LIKE_STATIC(r) IS_DATA_PTR(r)
151 #define LOOKS_LIKE_STATIC_CLOSURE(r) IS_DATA_PTR(r)
152 #endif
153
154
155 /* -----------------------------------------------------------------------------
156    Macros for distinguishing infotables from closures.
157    
158    You'd think it'd be easy to tell an info pointer from a closure pointer:
159    closures live on the heap and infotables are in read only memory.  Right?
160    Wrong!  Static closures live in read only memory and Hugs allocates
161    infotables for constructors on the (writable) C heap.
162
163    ToDo: in the combined Hugs-GHC system, the following are but crude
164    approximations.  This absolutely has to be fixed.
165    -------------------------------------------------------------------------- */
166
167 #ifdef INTERPRETER
168 #ifdef USE_MINIINTERPRETER
169 /* yoiks: one of the dreaded pointer equality tests */
170 #define IS_HUGS_CONSTR_INFO(info) (((StgInfoTable *)(info))->entry == (StgFunPtr)&Hugs_CONSTR_entry)
171 #else
172 #define IS_HUGS_CONSTR_INFO(info) 0 /* ToDo: more than mildly bogus */
173 #endif
174 #else
175 #define IS_HUGS_CONSTR_INFO(info) 0 /* ToDo: more than mildly bogus */
176 #endif
177
178 #ifdef HAVE_WIN32_DLL_SUPPORT
179 # define LOOKS_LIKE_GHC_INFO(info) (!HEAP_ALLOCED(info) && !LOOKS_LIKE_STATIC_CLOSURE(info))
180 #else
181 # define LOOKS_LIKE_GHC_INFO(info) IS_CODE_PTR(info)
182 #endif
183
184 /* -----------------------------------------------------------------------------
185    Macros for calculating how big a closure will be (used during allocation)
186    -------------------------------------------------------------------------- */
187
188 /* ToDo: replace unsigned int by nat.  The only fly in the ointment is that
189  * nat comes from Rts.h which many folk dont include.  Sigh!
190  */
191 static __inline__ StgOffset AP_sizeW    ( unsigned int n_args )              
192 { return sizeofW(StgAP_UPD) + n_args; }
193
194 static __inline__ StgOffset PAP_sizeW   ( unsigned int n_args )              
195 { return sizeofW(StgPAP)    + n_args; }
196
197 static __inline__ StgOffset CONSTR_sizeW( unsigned int p, unsigned int np )  
198 { return sizeofW(StgHeader) + p + np; }
199
200 static __inline__ StgOffset BCO_sizeW   ( unsigned int p, unsigned int np, unsigned int is ) 
201 { return sizeofW(StgBCO) + p + np + (is+sizeof(StgWord)-1)/sizeof(StgWord); }
202
203 static __inline__ StgOffset THUNK_SELECTOR_sizeW ( void )                    
204 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
205
206 static __inline__ StgOffset BLACKHOLE_sizeW ( void )                    
207 { return sizeofW(StgHeader) + MIN_UPD_SIZE; }
208
209 static __inline__ StgOffset CAF_sizeW ( void )                    
210 { return sizeofW(StgCAF); }
211
212 /* --------------------------------------------------------------------------
213  * Sizes of closures
214  * ------------------------------------------------------------------------*/
215
216 static __inline__ StgOffset size_fromITBL( const StgInfoTable* itbl ) 
217 { return sizeof(StgClosure) 
218        + sizeof(StgPtr)  * itbl->layout.payload.ptrs 
219        + sizeof(StgWord) * itbl->layout.payload.nptrs; }
220
221 static __inline__ StgOffset sizeW_fromITBL( const StgInfoTable* itbl ) 
222 { return sizeofW(StgClosure) 
223        + sizeofW(StgPtr)  * itbl->layout.payload.ptrs 
224        + sizeofW(StgWord) * itbl->layout.payload.nptrs; }
225
226 static __inline__ StgOffset pap_size( StgPAP* x )
227 { return sizeof(StgPAP) 
228        + sizeof(StgWord)  * x->n_args; }
229
230 static __inline__ StgOffset pap_sizeW( StgPAP* x )
231 { return PAP_sizeW(x->n_args); }
232
233 /* These two functions give the same result - but have slightly
234  * different types. 
235  */
236 static __inline__ StgOffset arr_words_sizeW( StgArrWords* x )
237 { return sizeofW(StgArrWords) + x->words; }
238 static __inline__ StgOffset mut_arr_ptrs_sizeW( StgMutArrPtrs* x )
239 { return sizeofW(StgMutArrPtrs) + x->ptrs; }
240
241 static __inline__ StgWord bco_sizeW( StgBCO* bco )
242 { return BCO_sizeW(bco->n_ptrs,bco->n_words,bco->n_instrs); }
243
244 static __inline__ StgWord tso_sizeW ( StgTSO *tso )
245 { return TSO_STRUCT_SIZEW + tso->stack_size; }
246
247 /* -----------------------------------------------------------------------------
248    Macros for building closures
249    -------------------------------------------------------------------------- */
250
251 #ifdef PROFILING
252 #define SET_PROF_HDR(c,ccs_)            (c)->header.prof.ccs = ccs_
253 #define SET_STATIC_PROF_HDR(ccs_)       prof : { ccs : ccs_ },
254 #else
255 #define SET_PROF_HDR(c,ccs)
256 #define SET_STATIC_PROF_HDR(ccs)
257 #endif
258
259 #ifdef GRAN
260 #define SET_GRAN_HDR(c,pe)              (c)->header.gran.procs = pe
261 #define SET_STATIC_GRAN_HDR             gran : { procs : Everywhere },
262 #else
263 #define SET_GRAN_HDR(c,pe)
264 #define SET_STATIC_GRAN_HDR
265 #endif
266
267 /* there is no PAR header, as far as I can tell -- SDM */
268
269 #ifdef PAR
270 #define SET_PAR_HDR(c,stuff)
271 #define SET_STATIC_PAR_HDR(stuff)
272 #else
273 #define SET_PAR_HDR(c,stuff)
274 #define SET_STATIC_PAR_HDR(stuff)
275 #endif
276
277 #ifdef TICKY_TICKY
278 #define SET_TICKY_HDR(c,stuff)          /* old: (c)->header.ticky.updated = stuff */
279 #define SET_STATIC_TICKY_HDR(stuff)     /* old: ticky : { updated : stuff } */
280 #else
281 #define SET_TICKY_HDR(c,stuff)
282 #define SET_STATIC_TICKY_HDR(stuff)
283 #endif
284 #define SET_HDR(c,info,ccs) \
285    {                                    \
286         SET_INFO(c,info);                               \
287         SET_GRAN_HDR((StgClosure *)(c),ThisPE);         \
288         SET_PAR_HDR((StgClosure *)(c),LOCAL_GA);        \
289         SET_PROF_HDR((StgClosure *)(c),ccs);            \
290         SET_TICKY_HDR((StgClosure *)(c),0);             \
291    }
292
293 #define SET_ARR_HDR(c,info,costCentreStack,n_words) \
294    SET_HDR(c,info,costCentreStack); \
295    (c)->words = n_words;
296
297 /* -----------------------------------------------------------------------------
298    Static closures are defined as follows:
299
300
301 SET_STATIC_HDR(PrelBase_CZh_closure,PrelBase_CZh_info,costCentreStack,const);
302
303    The info argument must have type 'StgInfoTable' or
304    'StgSRTInfoTable', since we use '&' to get its address in the macro.
305    -------------------------------------------------------------------------- */
306
307 #define SET_STATIC_HDR(label,info,costCentreStack,closure_class,info_class) \
308    info_class info;                        \
309    closure_class StgClosure label = {                   \
310    STATIC_HDR(info,costCentreStack)
311
312 #define STATIC_HDR(info,ccs) \
313         header : {                            \
314                 INIT_INFO(info),              \
315                 SET_STATIC_GRAN_HDR           \
316                 SET_STATIC_PAR_HDR(LOCAL_GA)  \
317                 SET_STATIC_PROF_HDR(ccs)       \
318                 SET_STATIC_TICKY_HDR(0)       \
319         }
320
321 /* how to get hold of the static link field for a static closure.
322  *
323  * Note that we have to use (*cast(T*,&e)) instead of cast(T,e)
324  * because C won't let us take the address of a casted expression. Huh?
325  */
326 #define STATIC_LINK(info,p) \
327    (*stgCast(StgClosure**,&((p)->payload[info->layout.payload.ptrs + \
328                                         info->layout.payload.nptrs])))
329 /* These macros are optimised versions of the above for certain
330  * closure types.  They *must* be equivalent to the generic
331  * STATIC_LINK.
332  */
333 #define FUN_STATIC_LINK(p)   ((p)->payload[0])
334 #define THUNK_STATIC_LINK(p) ((p)->payload[2])
335 #define IND_STATIC_LINK(p)   ((p)->payload[1])
336
337 #define STATIC_LINK2(info,p) \
338    (*stgCast(StgClosure**,&((p)->payload[info->layout.payload.ptrs + \
339                                         info->layout.payload.nptrs + 1])))
340
341 /* -----------------------------------------------------------------------------
342    INTLIKE and CHARLIKE closures.
343    -------------------------------------------------------------------------- */
344
345 #define CHARLIKE_CLOSURE(n) ((P_)&CHARLIKE_closure[n])
346 #define INTLIKE_CLOSURE(n)  ((P_)&INTLIKE_closure[(n)-MIN_INTLIKE])
347
348 /* -----------------------------------------------------------------------------
349    Closure Tables (for enumerated data types)
350    -------------------------------------------------------------------------- */
351
352 #define CLOSURE_TBL(lbl) const StgClosure *lbl[] = {
353
354 /* -----------------------------------------------------------------------------
355    Payload access
356    -------------------------------------------------------------------------- */
357
358 #define payloadPtr( c, i )    (*stgCast(StgPtr*,       ((c)->payload+(i))))
359 #define payloadCPtr( c, i )   (*stgCast(StgClosure**,  ((c)->payload+(i))))
360 #define payloadWord( c, i )   (*stgCast(StgWord*,      ((c)->payload+(i))))
361
362 /* -----------------------------------------------------------------------------
363    CONSTRs.
364    -------------------------------------------------------------------------- */
365
366 /* constructors don't have SRTs */
367 #define GET_TAG(info) (INFO_PTR_TO_STRUCT(info)->srt_len)
368
369 /* -----------------------------------------------------------------------------
370    BCOs.
371    -------------------------------------------------------------------------- */
372
373 #define bcoConstPtr( bco, i )    (*stgCast(StgPtr*,       ((bco)->payload+(i))))
374 #define bcoConstCPtr( bco, i )   (*stgCast(StgClosurePtr*,((bco)->payload+(i))))
375 #define bcoConstInfoPtr( bco, i )(*stgCast(StgInfoTable**,((bco)->payload+(bco)->n_ptrs+i)))
376 #define bcoConstInt( bco, i )    (*stgCast(StgInt*,       ((bco)->payload+(bco)->n_ptrs+i)))
377 #define bcoConstInt64( bco, i )  (PK_Int64(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
378 #define bcoConstWord( bco, i )   (*stgCast(StgWord*,      ((bco)->payload+(bco)->n_ptrs+i)))
379 #define bcoConstAddr( bco, i )   (*stgCast(StgAddr*,      ((bco)->payload+(bco)->n_ptrs+i)))
380 #define bcoConstChar( bco, i )   (*stgCast(StgChar*,      ((bco)->payload+(bco)->n_ptrs+i)))
381 #define bcoConstFloat( bco, i )  (PK_FLT(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
382 #define bcoConstDouble( bco, i ) (PK_DBL(stgCast(StgWord*,(bco)->payload+(bco)->n_ptrs+i)))
383 #define bcoInstr( bco, i )       (stgCast(StgWord8*,      ((bco)->payload+(bco)->n_ptrs+(bco)->n_words))[i])
384 static __inline__ StgInt bcoInstr16 ( StgBCO* bco, unsigned int i )
385 { StgInt x = (bcoInstr(bco,i) << 8) + bcoInstr(bco,i+1); return x; }
386
387 #endif /* CLOSUREMACROS_H */