use the new "prim %write_barrier()" in .cmm instead of calls to wb()
[ghc-hetmet.git] / includes / Regs.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2004
4  *
5  * Registers in the STG machine.
6  *
7  * The STG machine has a collection of "registers", each one of which
8  * may or may not correspond to an actual machine register when
9  * running code.  
10  *
11  * The register set is backed by a table in memory (struct
12  * StgRegTable).  If a particular STG register is not mapped to a
13  * machine register, then the apprpriate slot in this table is used
14  * instead.  
15  *
16  * This table is itself pointed to by another register, BaseReg.  If
17  * BaseReg is not in a machine register, then the register table is
18  * used from an absolute location (MainCapability).
19  *
20  * ---------------------------------------------------------------------------*/
21
22 #ifndef REGS_H
23 #define REGS_H
24
25 #include "gmp.h" // Needs MP_INT definition 
26
27 /*
28  * Spark pools: used to store pending sparks 
29  *  (THREADED_RTS & PARALLEL_HASKELL only)
30  * This is a circular buffer.  Invariants:
31  *    - base <= hd < lim
32  *    - base <= tl < lim
33  *    - if hd==tl, then the pool is empty.
34  *    - if hd == tl+1, then the pool is full.
35  * Adding to the pool is done by assigning to *tl++ (wrapping round as
36  * necessary).  When adding to a full pool, we have the option of
37  * throwing away either the oldest (hd++) or the most recent (tl--) entry.
38  */
39 typedef struct StgSparkPool_ {
40   StgClosure **base;
41   StgClosure **lim;
42   StgClosure **hd;
43   StgClosure **tl;
44 } StgSparkPool;
45
46 #define ASSERT_SPARK_POOL_INVARIANTS(p)         \
47   ASSERT((p)->base <= (p)->hd);                 \
48   ASSERT((p)->hd < (p)->lim);                   \
49   ASSERT((p)->base <= (p)->tl);                 \
50   ASSERT((p)->tl < (p)->lim);
51
52 typedef struct {
53   StgFunPtr      stgGCEnter1;
54   StgFunPtr      stgGCFun;
55 } StgFunTable;
56
57 /*
58  * Vanilla registers are given this union type, which is purely so
59  * that we can cast the vanilla reg to a variety of types with the
60  * minimum of syntax.  eg.  R1.w instead of (StgWord)R1.
61  */
62 typedef union {
63     StgWord        w;
64     StgAddr        a;
65     StgChar        c;
66     StgInt8        i8;
67     StgFloat       f;
68     StgInt         i;
69     StgPtr         p;
70     StgClosurePtr  cl;
71     StgStackOffset offset;      /* unused? */
72     StgByteArray   b;
73     StgTSOPtr      t;
74 } StgUnion;
75
76 /* 
77  * This is the table that holds shadow-locations for all the STG
78  * registers.  The shadow locations are used when:
79  *
80  *     1) the particular register isn't mapped to a real machine
81  *        register, probably because there's a shortage of real registers.
82  *     2) caller-saves registers are saved across a CCall
83  */
84 typedef struct StgRegTable_ {
85   StgUnion        rR1;
86   StgUnion        rR2;
87   StgUnion        rR3;
88   StgUnion        rR4;
89   StgUnion        rR5;
90   StgUnion        rR6;
91   StgUnion        rR7;
92   StgUnion        rR8;
93   StgUnion        rR9;          /* used occasionally by heap/stack checks */
94   StgUnion        rR10;         /* used occasionally by heap/stack checks */
95   StgFloat        rF1;
96   StgFloat        rF2;
97   StgFloat        rF3;
98   StgFloat        rF4;
99   StgDouble       rD1;
100   StgDouble       rD2;
101   StgWord64       rL1;
102   StgPtr          rSp;
103   StgPtr          rSpLim;
104   StgPtr          rHp;
105   StgPtr          rHpLim;
106   struct StgTSO_ *rCurrentTSO;
107   struct step_   *rNursery;
108   struct bdescr_ *rCurrentNursery; /* Hp/HpLim point into this block */
109   struct bdescr_ *rCurrentAlloc;   /* for allocation using allocate() */
110   StgWord         rHpAlloc;     /* number of *bytes* being allocated in heap */
111   // rmp_tmp1..rmp_result2 are only used in THREADED_RTS builds to
112   // avoid per-thread temps in bss, but currently always incldue here
113   // so we just run mkDerivedConstants once
114   StgWord         rmp_tmp_w;
115   MP_INT          rmp_tmp1;      
116   MP_INT          rmp_tmp2;      
117   MP_INT          rmp_result1;
118   MP_INT          rmp_result2;
119   StgWord         rRet;  // holds the return code of the thread
120 #if defined(THREADED_RTS) || defined(PAR)
121   StgSparkPool    rSparks;      /* per-task spark pool */
122 #endif
123 } StgRegTable;
124
125 #if IN_STG_CODE
126
127 /*
128  * Registers Hp and HpLim are global across the entire system, and are
129  * copied into the RegTable before executing a thread.
130  *
131  * Registers Sp and SpLim are saved in the TSO for the
132  * thread, but are copied into the RegTable before executing a thread.
133  *
134  * All other registers are "general purpose", and are used for passing
135  * arguments to functions, and returning values.  The code generator
136  * knows how many of these are in real registers, and avoids
137  * generating code that uses non-real registers.  General purpose
138  * registers are never saved when returning to the scheduler, instead
139  * we save whatever is live at the time on the stack, and restore it
140  * later.  This should reduce the context switch time, amongst other
141  * things.
142  *
143  * For argument passing, the stack will be used in preference to
144  * pseudo-registers if the architecture has too few general purpose
145  * registers.
146  *
147  * Some special RTS functions like newArray and the Integer primitives
148  * expect their arguments to be in registers R1-Rn, so we use these
149  * (pseudo-)registers in those cases.
150  */
151
152 /* 
153  * Locations for saving per-thread registers.
154  */
155
156 #define SAVE_Sp             (CurrentTSO->sp)
157 #define SAVE_SpLim          (CurrentTSO->splim)
158
159 #define SAVE_Hp             (BaseReg->rHp)
160 #define SAVE_HpLim          (BaseReg->rHpLim)
161
162 #define SAVE_CurrentTSO     (BaseReg->rCurrentTSO)
163 #define SAVE_CurrentNursery (BaseReg->rCurrentNursery)
164 #define SAVE_HpAlloc        (BaseReg->rHpAlloc)
165 #define SAVE_SparkHd        (BaseReg->rSparks.hd)
166 #define SAVE_SparkTl        (BaseReg->rSparks.tl)
167 #define SAVE_SparkBase      (BaseReg->rSparks.base)
168 #define SAVE_SparkLim       (BaseReg->rSparks.lim)
169
170 /* We sometimes need to save registers across a C-call, eg. if they
171  * are clobbered in the standard calling convention.  We define the
172  * save locations for all registers in the register table.
173  */
174
175 #define SAVE_R1             (BaseReg->rR1)
176 #define SAVE_R2             (BaseReg->rR2)
177 #define SAVE_R3             (BaseReg->rR3)
178 #define SAVE_R4             (BaseReg->rR4)
179 #define SAVE_R5             (BaseReg->rR5)
180 #define SAVE_R6             (BaseReg->rR6)
181 #define SAVE_R7             (BaseReg->rR7)
182 #define SAVE_R8             (BaseReg->rR8)
183  
184 #define SAVE_F1             (BaseReg->rF1)
185 #define SAVE_F2             (BaseReg->rF2)
186 #define SAVE_F3             (BaseReg->rF3)
187 #define SAVE_F4             (BaseReg->rF4)
188
189 #define SAVE_D1             (BaseReg->rD1)
190 #define SAVE_D2             (BaseReg->rD2)
191
192 #define SAVE_L1             (BaseReg->rL1)
193
194 /* -----------------------------------------------------------------------------
195  * Emit the GCC-specific register declarations for each machine
196  * register being used.  If any STG register isn't mapped to a machine
197  * register, then map it to an offset from BaseReg.
198  *
199  * First, the general purpose registers.  The idea is, if a particular
200  * general-purpose STG register can't be mapped to a real machine
201  * register, it won't be used at all.  Instead, we'll use the stack.
202  *
203  * This is an improvement on the way things used to be done, when all
204  * registers were mapped to locations in the register table, and stuff
205  * was being shifted from the stack to the register table and back
206  * again for no good reason (on register-poor architectures).
207  */
208
209 /* define NO_REGS to omit register declarations - used in RTS C code
210  * that needs all the STG definitions but not the global register 
211  * settings.
212  */
213 #define GLOBAL_REG_DECL(type,name,reg) register type name REG(reg);
214
215 #if defined(REG_R1) && !defined(NO_GLOBAL_REG_DECLS)
216 GLOBAL_REG_DECL(StgUnion,R1,REG_R1)
217 #else
218 # define R1 (BaseReg->rR1)
219 #endif
220
221 #if defined(REG_R2) && !defined(NO_GLOBAL_REG_DECLS)
222 GLOBAL_REG_DECL(StgUnion,R2,REG_R2)
223 #else
224 # define R2 (BaseReg->rR2)
225 #endif
226
227 #if defined(REG_R3) && !defined(NO_GLOBAL_REG_DECLS)
228 GLOBAL_REG_DECL(StgUnion,R3,REG_R3)
229 #else
230 # define R3 (BaseReg->rR3)
231 #endif
232
233 #if defined(REG_R4) && !defined(NO_GLOBAL_REG_DECLS)
234 GLOBAL_REG_DECL(StgUnion,R4,REG_R4)
235 #else
236 # define R4 (BaseReg->rR4)
237 #endif
238
239 #if defined(REG_R5) && !defined(NO_GLOBAL_REG_DECLS)
240 GLOBAL_REG_DECL(StgUnion,R5,REG_R5)
241 #else
242 # define R5 (BaseReg->rR5)
243 #endif
244
245 #if defined(REG_R6) && !defined(NO_GLOBAL_REG_DECLS)
246 GLOBAL_REG_DECL(StgUnion,R6,REG_R6)
247 #else
248 # define R6 (BaseReg->rR6)
249 #endif
250
251 #if defined(REG_R7) && !defined(NO_GLOBAL_REG_DECLS)
252 GLOBAL_REG_DECL(StgUnion,R7,REG_R7)
253 #else
254 # define R7 (BaseReg->rR7)
255 #endif
256
257 #if defined(REG_R8) && !defined(NO_GLOBAL_REG_DECLS)
258 GLOBAL_REG_DECL(StgUnion,R8,REG_R8)
259 #else
260 # define R8 (BaseReg->rR8)
261 #endif
262
263 #if defined(REG_R9) && !defined(NO_GLOBAL_REG_DECLS)
264 GLOBAL_REG_DECL(StgUnion,R9,REG_R9)
265 #else
266 # define R9 (BaseReg->rR9)
267 #endif
268
269 #if defined(REG_R10) && !defined(NO_GLOBAL_REG_DECLS)
270 GLOBAL_REG_DECL(StgUnion,R10,REG_R10)
271 #else
272 # define R10 (BaseReg->rR10)
273 #endif
274
275 #if defined(REG_F1) && !defined(NO_GLOBAL_REG_DECLS)
276 GLOBAL_REG_DECL(StgFloat,F1,REG_F1)
277 #else
278 #define F1 (BaseReg->rF1)
279 #endif
280
281 #if defined(REG_F2) && !defined(NO_GLOBAL_REG_DECLS)
282 GLOBAL_REG_DECL(StgFloat,F2,REG_F2)
283 #else
284 #define F2 (BaseReg->rF2)
285 #endif
286
287 #if defined(REG_F3) && !defined(NO_GLOBAL_REG_DECLS)
288 GLOBAL_REG_DECL(StgFloat,F3,REG_F3)
289 #else
290 #define F3 (BaseReg->rF3)
291 #endif
292
293 #if defined(REG_F4) && !defined(NO_GLOBAL_REG_DECLS)
294 GLOBAL_REG_DECL(StgFloat,F4,REG_F4)
295 #else
296 #define F4 (BaseReg->rF4)
297 #endif
298
299 #if defined(REG_D1) && !defined(NO_GLOBAL_REG_DECLS)
300 GLOBAL_REG_DECL(StgDouble,D1,REG_D1)
301 #else
302 #define D1 (BaseReg->rD1)
303 #endif
304
305 #if defined(REG_D2) && !defined(NO_GLOBAL_REG_DECLS)
306 GLOBAL_REG_DECL(StgDouble,D2,REG_D2)
307 #else
308 #define D2 (BaseReg->rD2)
309 #endif
310
311 #if defined(REG_L1) && !defined(NO_GLOBAL_REG_DECLS)
312 GLOBAL_REG_DECL(StgWord64,L1,REG_L1)
313 #else
314 #define L1 (BaseReg->rL1)
315 #endif
316
317 /*
318  * If BaseReg isn't mapped to a machine register, just use the global
319  * address of the current register table (CurrentRegTable in
320  * concurrent Haskell, MainRegTable otherwise).
321  */
322
323 /* A capability is a combination of a FunTable and a RegTable.  In STG
324  * code, BaseReg normally points to the RegTable portion of this
325  * structure, so that we can index both forwards and backwards to take
326  * advantage of shorter instruction forms on some archs (eg. x86).
327  * This is a cut-down version of the Capability structure; the full
328  * version is defined in Capability.h.
329  */
330 struct PartCapability_ {
331     StgFunTable f;
332     StgRegTable r;
333 };
334
335 /* No such thing as a MainCapability under THREADED_RTS - each thread must have
336  * its own Capability.
337  */
338 #if IN_STG_CODE && !defined(THREADED_RTS)
339 extern W_ MainCapability[];
340 #endif
341
342 /*
343  * Assigning to BaseReg (the ASSIGN_BaseReg macro): this happens on
344  * return from a "safe" foreign call, when the thread might be running
345  * on a new Capability.  Obviously if BaseReg is not a register, then
346  * we are restricted to a single Capability (this invariant is enforced
347  * in Capability.c:initCapabilities), and assigning to BaseReg can be omitted.
348  */
349
350 #if defined(REG_Base) && !defined(NO_GLOBAL_REG_DECLS)
351 GLOBAL_REG_DECL(StgRegTable *,BaseReg,REG_Base)
352 #define ASSIGN_BaseReg(e) (BaseReg = (e))
353 #else
354 #ifdef THREADED_RTS
355 #error BaseReg must be in a register for THREADED_RTS
356 #endif
357 #define BaseReg (&((struct PartCapability_ *)MainCapability)->r)
358 #define ASSIGN_BaseReg(e) (e)
359 #endif
360
361 #if defined(REG_Sp) && !defined(NO_GLOBAL_REG_DECLS)
362 GLOBAL_REG_DECL(P_,Sp,REG_Sp)
363 #else
364 #define Sp (BaseReg->rSp)
365 #endif
366
367 #if defined(REG_SpLim) && !defined(NO_GLOBAL_REG_DECLS)
368 GLOBAL_REG_DECL(P_,SpLim,REG_SpLim)
369 #else
370 #define SpLim (BaseReg->rSpLim)
371 #endif
372
373 #if defined(REG_Hp) && !defined(NO_GLOBAL_REG_DECLS)
374 GLOBAL_REG_DECL(P_,Hp,REG_Hp)
375 #else
376 #define Hp (BaseReg->rHp)
377 #endif
378
379 #if defined(REG_HpLim) && !defined(NO_GLOBAL_REG_DECLS)
380 GLOBAL_REG_DECL(P_,HpLim,REG_HpLim)
381 #else
382 #define HpLim (BaseReg->rHpLim)
383 #endif
384
385 #if defined(REG_CurrentTSO) && !defined(NO_GLOBAL_REG_DECLS)
386 GLOBAL_REG_DECL(struct _StgTSO *,CurrentTSO,REG_CurrentTSO)
387 #else
388 #define CurrentTSO (BaseReg->rCurrentTSO)
389 #endif
390
391 #if defined(REG_CurrentNursery) && !defined(NO_GLOBAL_REG_DECLS)
392 GLOBAL_REG_DECL(bdescr *,CurrentNursery,REG_CurrentNursery)
393 #else
394 #define CurrentNursery (BaseReg->rCurrentNursery)
395 #endif
396
397 #if defined(REG_HpAlloc) && !defined(NO_GLOBAL_REG_DECLS)
398 GLOBAL_REG_DECL(bdescr *,HpAlloc,REG_HpAlloc)
399 #else
400 #define HpAlloc (BaseReg->rHpAlloc)
401 #endif
402
403 #if defined(REG_SparkHd) && !defined(NO_GLOBAL_REG_DECLS)
404 GLOBAL_REG_DECL(bdescr *,SparkHd,REG_SparkHd)
405 #else
406 #define SparkHd (BaseReg->rSparks.hd)
407 #endif
408
409 #if defined(REG_SparkTl) && !defined(NO_GLOBAL_REG_DECLS)
410 GLOBAL_REG_DECL(bdescr *,SparkTl,REG_SparkTl)
411 #else
412 #define SparkTl (BaseReg->rSparks.tl)
413 #endif
414
415 #if defined(REG_SparkBase) && !defined(NO_GLOBAL_REG_DECLS)
416 GLOBAL_REG_DECL(bdescr *,SparkBase,REG_SparkBase)
417 #else
418 #define SparkBase (BaseReg->rSparks.base)
419 #endif
420
421 #if defined(REG_SparkLim) && !defined(NO_GLOBAL_REG_DECLS)
422 GLOBAL_REG_DECL(bdescr *,SparkLim,REG_SparkLim)
423 #else
424 #define SparkLim (BaseReg->rSparks.lim)
425 #endif
426
427 /* -----------------------------------------------------------------------------
428    Get absolute function pointers from the register table, to save
429    code space.  On x86, 
430
431        jmp  *-12(%ebx)
432
433    is shorter than
434    
435        jmp absolute_address
436
437    as long as the offset is within the range of a signed byte
438    (-128..+127).  So we pick some common absolute_addresses and put
439    them in the register table.  As a bonus, linking time should also
440    be reduced.
441
442    Other possible candidates in order of importance:
443       
444      stg_upd_frame_info
445      stg_CAF_BLACKHOLE_info
446      stg_IND_STATIC_info
447
448    anything else probably isn't worth the effort.
449
450    -------------------------------------------------------------------------- */
451
452
453 #define FunReg ((StgFunTable *)((void *)BaseReg - sizeof(StgFunTable)))
454
455 #define stg_gc_enter_1     (FunReg->stgGCEnter1)
456 #define stg_gc_fun         (FunReg->stgGCFun)
457
458 /* -----------------------------------------------------------------------------
459    For any registers which are denoted "caller-saves" by the C calling
460    convention, we have to emit code to save and restore them across C
461    calls.
462    -------------------------------------------------------------------------- */
463
464 #ifdef CALLER_SAVES_R1
465 #define CALLER_SAVE_R1          SAVE_R1 = R1;
466 #define CALLER_RESTORE_R1       R1 = SAVE_R1;
467 #else
468 #define CALLER_SAVE_R1          /* nothing */
469 #define CALLER_RESTORE_R1       /* nothing */
470 #endif
471
472 #ifdef CALLER_SAVES_R2
473 #define CALLER_SAVE_R2          SAVE_R2 = R2;
474 #define CALLER_RESTORE_R2       R2 = SAVE_R2;
475 #else
476 #define CALLER_SAVE_R2          /* nothing */
477 #define CALLER_RESTORE_R2       /* nothing */
478 #endif
479
480 #ifdef CALLER_SAVES_R3
481 #define CALLER_SAVE_R3          SAVE_R3 = R3;
482 #define CALLER_RESTORE_R3       R3 = SAVE_R3;
483 #else
484 #define CALLER_SAVE_R3          /* nothing */
485 #define CALLER_RESTORE_R3       /* nothing */
486 #endif
487
488 #ifdef CALLER_SAVES_R4
489 #define CALLER_SAVE_R4          SAVE_R4 = R4;
490 #define CALLER_RESTORE_R4       R4 = SAVE_R4;
491 #else
492 #define CALLER_SAVE_R4          /* nothing */
493 #define CALLER_RESTORE_R4       /* nothing */
494 #endif
495
496 #ifdef CALLER_SAVES_R5
497 #define CALLER_SAVE_R5          SAVE_R5 = R5;
498 #define CALLER_RESTORE_R5       R5 = SAVE_R5;
499 #else
500 #define CALLER_SAVE_R5          /* nothing */
501 #define CALLER_RESTORE_R5       /* nothing */
502 #endif
503
504 #ifdef CALLER_SAVES_R6
505 #define CALLER_SAVE_R6          SAVE_R6 = R6;
506 #define CALLER_RESTORE_R6       R6 = SAVE_R6;
507 #else
508 #define CALLER_SAVE_R6          /* nothing */
509 #define CALLER_RESTORE_R6       /* nothing */
510 #endif
511
512 #ifdef CALLER_SAVES_R7
513 #define CALLER_SAVE_R7          SAVE_R7 = R7;
514 #define CALLER_RESTORE_R7       R7 = SAVE_R7;
515 #else
516 #define CALLER_SAVE_R7          /* nothing */
517 #define CALLER_RESTORE_R7       /* nothing */
518 #endif
519
520 #ifdef CALLER_SAVES_R8
521 #define CALLER_SAVE_R8          SAVE_R8 = R8;
522 #define CALLER_RESTORE_R8       R8 = SAVE_R8;
523 #else
524 #define CALLER_SAVE_R8          /* nothing */
525 #define CALLER_RESTORE_R8       /* nothing */
526 #endif
527
528 #ifdef CALLER_SAVES_R9
529 #define CALLER_SAVE_R9          SAVE_R9 = R9;
530 #define CALLER_RESTORE_R9       R9 = SAVE_R9;
531 #else
532 #define CALLER_SAVE_R9          /* nothing */
533 #define CALLER_RESTORE_R9       /* nothing */
534 #endif
535
536 #ifdef CALLER_SAVES_R10
537 #define CALLER_SAVE_R10         SAVE_R10 = R10;
538 #define CALLER_RESTORE_R10      R10 = SAVE_R10;
539 #else
540 #define CALLER_SAVE_R10         /* nothing */
541 #define CALLER_RESTORE_R10      /* nothing */
542 #endif
543
544 #ifdef CALLER_SAVES_F1
545 #define CALLER_SAVE_F1          SAVE_F1 = F1;
546 #define CALLER_RESTORE_F1       F1 = SAVE_F1;
547 #else
548 #define CALLER_SAVE_F1          /* nothing */
549 #define CALLER_RESTORE_F1       /* nothing */
550 #endif
551
552 #ifdef CALLER_SAVES_F2
553 #define CALLER_SAVE_F2          SAVE_F2 = F2;
554 #define CALLER_RESTORE_F2       F2 = SAVE_F2;
555 #else
556 #define CALLER_SAVE_F2          /* nothing */
557 #define CALLER_RESTORE_F2       /* nothing */
558 #endif
559
560 #ifdef CALLER_SAVES_F3
561 #define CALLER_SAVE_F3          SAVE_F3 = F3;
562 #define CALLER_RESTORE_F3       F3 = SAVE_F3;
563 #else
564 #define CALLER_SAVE_F3          /* nothing */
565 #define CALLER_RESTORE_F3       /* nothing */
566 #endif
567
568 #ifdef CALLER_SAVES_F4
569 #define CALLER_SAVE_F4          SAVE_F4 = F4;
570 #define CALLER_RESTORE_F4       F4 = SAVE_F4;
571 #else
572 #define CALLER_SAVE_F4          /* nothing */
573 #define CALLER_RESTORE_F4       /* nothing */
574 #endif
575
576 #ifdef CALLER_SAVES_D1
577 #define CALLER_SAVE_D1          SAVE_D1 = D1;
578 #define CALLER_RESTORE_D1       D1 = SAVE_D1;
579 #else
580 #define CALLER_SAVE_D1          /* nothing */
581 #define CALLER_RESTORE_D1       /* nothing */
582 #endif
583
584 #ifdef CALLER_SAVES_D2
585 #define CALLER_SAVE_D2          SAVE_D2 = D2;
586 #define CALLER_RESTORE_D2       D2 = SAVE_D2;
587 #else
588 #define CALLER_SAVE_D2          /* nothing */
589 #define CALLER_RESTORE_D2       /* nothing */
590 #endif
591
592 #ifdef CALLER_SAVES_L1
593 #define CALLER_SAVE_L1          SAVE_L1 = L1;
594 #define CALLER_RESTORE_L1       L1 = SAVE_L1;
595 #else
596 #define CALLER_SAVE_L1          /* nothing */
597 #define CALLER_RESTORE_L1       /* nothing */
598 #endif
599
600 #ifdef CALLER_SAVES_Sp
601 #define CALLER_SAVE_Sp          SAVE_Sp = Sp;
602 #define CALLER_RESTORE_Sp       Sp = SAVE_Sp;
603 #else
604 #define CALLER_SAVE_Sp          /* nothing */
605 #define CALLER_RESTORE_Sp       /* nothing */
606 #endif
607
608 #ifdef CALLER_SAVES_SpLim
609 #define CALLER_SAVE_SpLim       SAVE_SpLim = SpLim;
610 #define CALLER_RESTORE_SpLim    SpLim = SAVE_SpLim;
611 #else
612 #define CALLER_SAVE_SpLim       /* nothing */
613 #define CALLER_RESTORE_SpLim    /* nothing */
614 #endif
615
616 #ifdef CALLER_SAVES_Hp
617 #define CALLER_SAVE_Hp          SAVE_Hp = Hp;
618 #define CALLER_RESTORE_Hp       Hp = SAVE_Hp;
619 #else
620 #define CALLER_SAVE_Hp          /* nothing */
621 #define CALLER_RESTORE_Hp       /* nothing */
622 #endif
623
624 #ifdef CALLER_SAVES_HpLim
625 #define CALLER_SAVE_HpLim       SAVE_HpLim = HpLim;
626 #define CALLER_RESTORE_HpLim    HpLim = SAVE_HpLim;
627 #else
628 #define CALLER_SAVE_HpLim       /* nothing */
629 #define CALLER_RESTORE_HpLim    /* nothing */
630 #endif
631
632 #ifdef CALLER_SAVES_Base
633 #ifdef THREADED_RTS
634 #error "Can't have caller-saved BaseReg with THREADED_RTS"
635 #endif
636 #define CALLER_SAVE_Base        /* nothing */
637 #define CALLER_RESTORE_Base     BaseReg = &MainRegTable;
638 #else
639 #define CALLER_SAVE_Base        /* nothing */
640 #define CALLER_RESTORE_Base     /* nothing */
641 #endif
642
643 #ifdef CALLER_SAVES_CurrentTSO
644 #define CALLER_SAVE_CurrentTSO          SAVE_CurrentTSO = CurrentTSO;
645 #define CALLER_RESTORE_CurrentTSO       CurrentTSO = SAVE_CurrentTSO;
646 #else
647 #define CALLER_SAVE_CurrentTSO          /* nothing */
648 #define CALLER_RESTORE_CurrentTSO       /* nothing */
649 #endif
650
651 #ifdef CALLER_SAVES_CurrentNursery
652 #define CALLER_SAVE_CurrentNursery      SAVE_CurrentNursery = CurrentNursery;
653 #define CALLER_RESTORE_CurrentNursery   CurrentNursery = SAVE_CurrentNursery;
654 #else
655 #define CALLER_SAVE_CurrentNursery      /* nothing */
656 #define CALLER_RESTORE_CurrentNursery   /* nothing */
657 #endif
658
659 #ifdef CALLER_SAVES_HpAlloc
660 #define CALLER_SAVE_HpAlloc             SAVE_HpAlloc = HpAlloc;
661 #define CALLER_RESTORE_HpAlloc          HpAlloc = SAVE_HpAlloc;
662 #else
663 #define CALLER_SAVE_HpAlloc             /* nothing */
664 #define CALLER_RESTORE_HpAlloc          /* nothing */
665 #endif
666
667 #ifdef CALLER_SAVES_SparkHd
668 #define CALLER_SAVE_SparkHd             SAVE_SparkHd = SparkHd;
669 #define CALLER_RESTORE_SparkHd          SparkHd = SAVE_SparkHd;
670 #else
671 #define CALLER_SAVE_SparkHd             /* nothing */
672 #define CALLER_RESTORE_SparkHd          /* nothing */
673 #endif
674
675 #ifdef CALLER_SAVES_SparkTl
676 #define CALLER_SAVE_SparkTl             SAVE_SparkTl = SparkTl;
677 #define CALLER_RESTORE_SparkTl          SparkTl = SAVE_SparkTl;
678 #else
679 #define CALLER_SAVE_SparkTl             /* nothing */
680 #define CALLER_RESTORE_SparkTl          /* nothing */
681 #endif
682
683 #ifdef CALLER_SAVES_SparkBase
684 #define CALLER_SAVE_SparkBase           SAVE_SparkBase = SparkBase;
685 #define CALLER_RESTORE_SparkBase        SparkBase = SAVE_SparkBase;
686 #else
687 #define CALLER_SAVE_SparkBase           /* nothing */
688 #define CALLER_RESTORE_SparkBase        /* nothing */
689 #endif
690
691 #ifdef CALLER_SAVES_SparkLim
692 #define CALLER_SAVE_SparkLim            SAVE_SparkLim = SparkLim;
693 #define CALLER_RESTORE_SparkLim         SparkLim = SAVE_SparkLim;
694 #else
695 #define CALLER_SAVE_SparkLim            /* nothing */
696 #define CALLER_RESTORE_SparkLim         /* nothing */
697 #endif
698
699 #endif /* IN_STG_CODE */
700
701 /* ----------------------------------------------------------------------------
702    Handy bunches of saves/restores 
703    ------------------------------------------------------------------------  */
704
705 #if IN_STG_CODE
706
707 #define CALLER_SAVE_USER                        \
708   CALLER_SAVE_R1                                \
709   CALLER_SAVE_R2                                \
710   CALLER_SAVE_R3                                \
711   CALLER_SAVE_R4                                \
712   CALLER_SAVE_R5                                \
713   CALLER_SAVE_R6                                \
714   CALLER_SAVE_R7                                \
715   CALLER_SAVE_R8                                \
716   CALLER_SAVE_F1                                \
717   CALLER_SAVE_F2                                \
718   CALLER_SAVE_F3                                \
719   CALLER_SAVE_F4                                \
720   CALLER_SAVE_D1                                \
721   CALLER_SAVE_D2                                \
722   CALLER_SAVE_L1
723
724      /* Save Base last, since the others may
725         be addressed relative to it */
726 #define CALLER_SAVE_SYSTEM                      \
727   CALLER_SAVE_Sp                                \
728   CALLER_SAVE_SpLim                             \
729   CALLER_SAVE_Hp                                \
730   CALLER_SAVE_HpLim                             \
731   CALLER_SAVE_CurrentTSO                        \
732   CALLER_SAVE_CurrentNursery                    \
733   CALLER_SAVE_SparkHd                           \
734   CALLER_SAVE_SparkTl                           \
735   CALLER_SAVE_SparkBase                         \
736   CALLER_SAVE_SparkLim                          \
737   CALLER_SAVE_Base
738
739 #define CALLER_RESTORE_USER                     \
740   CALLER_RESTORE_R1                             \
741   CALLER_RESTORE_R2                             \
742   CALLER_RESTORE_R3                             \
743   CALLER_RESTORE_R4                             \
744   CALLER_RESTORE_R5                             \
745   CALLER_RESTORE_R6                             \
746   CALLER_RESTORE_R7                             \
747   CALLER_RESTORE_R8                             \
748   CALLER_RESTORE_F1                             \
749   CALLER_RESTORE_F2                             \
750   CALLER_RESTORE_F3                             \
751   CALLER_RESTORE_F4                             \
752   CALLER_RESTORE_D1                             \
753   CALLER_RESTORE_D2                             \
754   CALLER_RESTORE_L1
755
756      /* Restore Base first, since the others may
757         be addressed relative to it */
758 #define CALLER_RESTORE_SYSTEM                   \
759   CALLER_RESTORE_Base                           \
760   CALLER_RESTORE_Sp                             \
761   CALLER_RESTORE_SpLim                          \
762   CALLER_RESTORE_Hp                             \
763   CALLER_RESTORE_HpLim                          \
764   CALLER_RESTORE_CurrentTSO                     \
765   CALLER_RESTORE_CurrentNursery                 \
766   CALLER_RESTORE_SparkHd                        \
767   CALLER_RESTORE_SparkTl                        \
768   CALLER_RESTORE_SparkBase                      \
769   CALLER_RESTORE_SparkLim
770
771 #else /* not IN_STG_CODE */
772
773 #define CALLER_SAVE_USER       /* nothing */
774 #define CALLER_SAVE_SYSTEM     /* nothing */
775 #define CALLER_RESTORE_USER    /* nothing */
776 #define CALLER_RESTORE_SYSTEM  /* nothing */
777
778 #endif /* IN_STG_CODE */
779 #define CALLER_SAVE_ALL                         \
780   CALLER_SAVE_SYSTEM                            \
781   CALLER_SAVE_USER
782
783 #define CALLER_RESTORE_ALL                      \
784   CALLER_RESTORE_SYSTEM                         \
785   CALLER_RESTORE_USER
786
787 #endif /* REGS_H */