1 /* -----------------------------------------------------------------------------
2 * $Id: Regs.h,v 1.11 2002/01/24 00:40:28 sof Exp $
4 * (c) The GHC Team, 1998-1999
6 * Registers used in STG code. Might or might not correspond to
7 * actual machine registers.
9 * ---------------------------------------------------------------------------*/
15 * This file should do the right thing if we have no machine-registers
16 * defined, i.e. everything lives in the RegTable.
20 * This is the table that holds shadow-locations for all the STG
21 * registers. The shadow locations are used when:
23 * 1) the particular register isn't mapped to a real machine
24 * register, probably because there's a shortage of real registers.
25 * 2) caller-saves registers are saved across a CCall
28 typedef struct StgSparkPool_ {
38 StgFunPtr stgGCEnter1;
39 StgFunPtr stgUpdatePAP;
42 typedef struct StgRegTable_ {
51 StgUnion rR9; // used occasionally by heap/stack checks
52 StgUnion rR10; // used occasionally by heap/stack checks
66 struct _bdescr *rNursery;
67 struct _bdescr *rCurrentNursery;
68 StgWord rHpAlloc; // number of words being allocated in heap
69 #if defined(SMP) || defined(PAR)
70 StgSparkPool rSparks; // per-task spark pool
75 /* A capability is a combination of a FunTable and a RegTable. In STG
76 * code, BaseReg normally points to the RegTable portion of this
77 * structure, so that we can index both forwards and backwards to take
78 * advantage of shorter instruction forms on some archs (eg. x86).
80 typedef struct Capability_ {
84 struct Capability_ *link; /* per-task register tables are linked together */
88 /* No such thing as a MainRegTable under SMP - each thread must
89 * have its own MainRegTable.
92 extern DLL_IMPORT_RTS Capability MainCapability;
98 * Registers Hp and HpLim are global across the entire system, and are
99 * copied into the RegTable before executing a thread.
101 * Registers Sp, Su, and SpLim are saved in the TSO for the
102 * thread, but are copied into the RegTable before executing a thread.
104 * All other registers are "general purpose", and are used for passing
105 * arguments to functions, and returning values. The code generator
106 * knows how many of these are in real registers, and avoids
107 * generating code that uses non-real registers. General purpose
108 * registers are never saved when returning to the scheduler, instead
109 * we save whatever is live at the time on the stack, and restore it
110 * later. This should reduce the context switch time, amongst other
113 * For argument passing, the stack will be used in preference to
114 * pseudo-registers if the architecture has too few general purpose
117 * Some special RTS functions like newArray and the Integer primitives
118 * expect their arguments to be in registers R1-Rn, so we use these
119 * (pseudo-)registers in those cases.
123 * Locations for saving per-thread registers.
126 #define SAVE_Sp (CurrentTSO->sp)
127 #define SAVE_Su (CurrentTSO->su)
128 #define SAVE_SpLim (CurrentTSO->splim)
130 #define SAVE_Hp (BaseReg->rHp)
131 #define SAVE_HpLim (BaseReg->rHpLim)
133 #define SAVE_CurrentTSO (BaseReg->rCurrentTSO)
134 #define SAVE_CurrentNursery (BaseReg->rCurrentNursery)
135 #define SAVE_HpAlloc (BaseReg->rHpAlloc)
136 #if defined(SMP) || defined(PAR)
137 #define SAVE_SparkHd (BaseReg->rSparks.hd)
138 #define SAVE_SparkTl (BaseReg->rSparks.tl)
139 #define SAVE_SparkBase (BaseReg->rSparks.base)
140 #define SAVE_SparkLim (BaseReg->rSparks.lim)
143 /* We sometimes need to save registers across a C-call, eg. if they
144 * are clobbered in the standard calling convention. We define the
145 * save locations for all registers in the register table.
148 #define SAVE_R1 (BaseReg->rR1)
149 #define SAVE_R2 (BaseReg->rR2)
150 #define SAVE_R3 (BaseReg->rR3)
151 #define SAVE_R4 (BaseReg->rR4)
152 #define SAVE_R5 (BaseReg->rR5)
153 #define SAVE_R6 (BaseReg->rR6)
154 #define SAVE_R7 (BaseReg->rR7)
155 #define SAVE_R8 (BaseReg->rR8)
157 #define SAVE_F1 (BaseReg->rF1)
158 #define SAVE_F2 (BaseReg->rF2)
159 #define SAVE_F3 (BaseReg->rF3)
160 #define SAVE_F4 (BaseReg->rF4)
162 #define SAVE_D1 (BaseReg->rD1)
163 #define SAVE_D2 (BaseReg->rD2)
165 #define SAVE_L1 (BaseReg->rL1)
167 /* -----------------------------------------------------------------------------
168 * Emit the GCC-specific register declarations for each machine
169 * register being used. If any STG register isn't mapped to a machine
170 * register, then map it to an offset from BaseReg.
172 * First, the general purpose registers. The idea is, if a particular
173 * general-purpose STG register can't be mapped to a real machine
174 * register, it won't be used at all. Instead, we'll use the stack.
176 * This is an improvement on the way things used to be done, when all
177 * registers were mapped to locations in the register table, and stuff
178 * was being shifted from the stack to the register table and back
179 * again for no good reason (on register-poor architectures).
182 #define GLOBAL_REG_DECL(type,name,reg) register type name REG(reg);
185 GLOBAL_REG_DECL(StgUnion,R1,REG_R1)
187 #define R1 (BaseReg->rR1)
191 GLOBAL_REG_DECL(StgUnion,R2,REG_R2)
193 #define R2 (BaseReg->rR2)
197 GLOBAL_REG_DECL(StgUnion,R3,REG_R3)
199 # define R3 (BaseReg->rR3)
203 GLOBAL_REG_DECL(StgUnion,R4,REG_R4)
205 # define R4 (BaseReg->rR4)
209 GLOBAL_REG_DECL(StgUnion,R5,REG_R5)
211 # define R5 (BaseReg->rR5)
215 GLOBAL_REG_DECL(StgUnion,R6,REG_R6)
217 # define R6 (BaseReg->rR6)
221 GLOBAL_REG_DECL(StgUnion,R7,REG_R7)
223 # define R7 (BaseReg->rR7)
227 GLOBAL_REG_DECL(StgUnion,R8,REG_R8)
229 # define R8 (BaseReg->rR8)
233 GLOBAL_REG_DECL(StgUnion,R9,REG_R9)
235 # define R9 (BaseReg->rR9)
239 GLOBAL_REG_DECL(StgUnion,R10,REG_R10)
241 # define R10 (BaseReg->rR10)
245 GLOBAL_REG_DECL(StgFloat,F1,REG_F1)
247 #define F1 (BaseReg->rF1)
251 GLOBAL_REG_DECL(StgFloat,F2,REG_F2)
253 #define F2 (BaseReg->rF2)
257 GLOBAL_REG_DECL(StgFloat,F3,REG_F3)
259 #define F3 (BaseReg->rF3)
263 GLOBAL_REG_DECL(StgFloat,F4,REG_F4)
265 #define F4 (BaseReg->rF4)
269 GLOBAL_REG_DECL(StgDouble,D1,REG_D1)
271 #define D1 (BaseReg->rD1)
275 GLOBAL_REG_DECL(StgDouble,D2,REG_D2)
277 #define D2 (BaseReg->rD2)
281 GLOBAL_REG_DECL(StgWord64,L1,REG_L1)
283 #define L1 (BaseReg->rL1)
287 * If BaseReg isn't mapped to a machine register, just use the global
288 * address of the current register table (CurrentRegTable in
289 * concurrent Haskell, MainRegTable otherwise).
293 GLOBAL_REG_DECL(StgRegTable *,BaseReg,REG_Base)
296 #error BaseReg must be in a register for SMP
298 #define BaseReg (&MainCapability.r)
302 GLOBAL_REG_DECL(P_,Sp,REG_Sp)
304 #define Sp (BaseReg->rSp)
308 GLOBAL_REG_DECL(StgUpdateFrame *,Su,REG_Su)
310 #define Su (BaseReg->rSu)
314 GLOBAL_REG_DECL(P_,SpLim,REG_SpLim)
316 #define SpLim (BaseReg->rSpLim)
320 GLOBAL_REG_DECL(P_,Hp,REG_Hp)
322 #define Hp (BaseReg->rHp)
326 GLOBAL_REG_DECL(P_,HpLim,REG_HpLim)
328 #define HpLim (BaseReg->rHpLim)
331 #ifdef REG_CurrentTSO
332 GLOBAL_REG_DECL(StgTSO *,CurrentTSO,REG_CurrentTSO)
334 #define CurrentTSO (BaseReg->rCurrentTSO)
337 #ifdef REG_CurrentNursery
338 GLOBAL_REG_DECL(bdescr *,CurrentNursery,REG_CurrentNursery)
340 #define CurrentNursery (BaseReg->rCurrentNursery)
344 GLOBAL_REG_DECL(bdescr *,HpAlloc,REG_HpAlloc)
346 #define HpAlloc (BaseReg->rHpAlloc)
350 GLOBAL_REG_DECL(bdescr *,SparkHd,REG_SparkHd)
352 #define SparkHd (BaseReg->rSparks.hd)
356 GLOBAL_REG_DECL(bdescr *,SparkTl,REG_SparkTl)
358 #define SparkTl (BaseReg->rSparks.tl)
362 GLOBAL_REG_DECL(bdescr *,SparkBase,REG_SparkBase)
364 #define SparkBase (BaseReg->rSparks.base)
368 GLOBAL_REG_DECL(bdescr *,SparkLim,REG_SparkLim)
370 #define SparkLim (BaseReg->rSparks.lim)
373 /* -----------------------------------------------------------------------------
374 Get absolute function pointers from the register table, to save
383 as long as the offset is within the range of a signed byte
384 (-128..+127). So we pick some common absolute_addresses and put
385 them in the register table. As a bonus, linking time should also
388 Other possible candidates in order of importance:
391 stg_CAF_BLACKHOLE_info
394 anything else probably isn't worth the effort.
396 -------------------------------------------------------------------------- */
399 #define FunReg ((StgFunTable *)((void *)BaseReg - sizeof(StgFunTable)))
401 #define stg_chk_0 (FunReg->stgChk0)
402 #define stg_chk_1 (FunReg->stgChk1)
403 #define stg_gc_enter_1 (FunReg->stgGCEnter1)
404 #define stg_update_PAP (FunReg->stgUpdatePAP)
406 /* -----------------------------------------------------------------------------
407 For any registers which are denoted "caller-saves" by the C calling
408 convention, we have to emit code to save and restore them across C
410 -------------------------------------------------------------------------- */
412 #ifdef CALLER_SAVES_R1
413 #define CALLER_SAVE_R1 SAVE_R1 = R1;
414 #define CALLER_RESTORE_R1 R1 = SAVE_R1;
416 #define CALLER_SAVE_R1 /* nothing */
417 #define CALLER_RESTORE_R1 /* nothing */
420 #ifdef CALLER_SAVES_R2
421 #define CALLER_SAVE_R2 SAVE_R2 = R2;
422 #define CALLER_RESTORE_R2 R2 = SAVE_R2;
424 #define CALLER_SAVE_R2 /* nothing */
425 #define CALLER_RESTORE_R2 /* nothing */
428 #ifdef CALLER_SAVES_R3
429 #define CALLER_SAVE_R3 SAVE_R3 = R3;
430 #define CALLER_RESTORE_R3 R3 = SAVE_R3;
432 #define CALLER_SAVE_R3 /* nothing */
433 #define CALLER_RESTORE_R3 /* nothing */
436 #ifdef CALLER_SAVES_R4
437 #define CALLER_SAVE_R4 SAVE_R4 = R4;
438 #define CALLER_RESTORE_R4 R4 = SAVE_R4;
440 #define CALLER_SAVE_R4 /* nothing */
441 #define CALLER_RESTORE_R4 /* nothing */
444 #ifdef CALLER_SAVES_R5
445 #define CALLER_SAVE_R5 SAVE_R5 = R5;
446 #define CALLER_RESTORE_R5 R5 = SAVE_R5;
448 #define CALLER_SAVE_R5 /* nothing */
449 #define CALLER_RESTORE_R5 /* nothing */
452 #ifdef CALLER_SAVES_R6
453 #define CALLER_SAVE_R6 SAVE_R6 = R6;
454 #define CALLER_RESTORE_R6 R6 = SAVE_R6;
456 #define CALLER_SAVE_R6 /* nothing */
457 #define CALLER_RESTORE_R6 /* nothing */
460 #ifdef CALLER_SAVES_R7
461 #define CALLER_SAVE_R7 SAVE_R7 = R7;
462 #define CALLER_RESTORE_R7 R7 = SAVE_R7;
464 #define CALLER_SAVE_R7 /* nothing */
465 #define CALLER_RESTORE_R7 /* nothing */
468 #ifdef CALLER_SAVES_R8
469 #define CALLER_SAVE_R8 SAVE_R8 = R8;
470 #define CALLER_RESTORE_R8 R8 = SAVE_R8;
472 #define CALLER_SAVE_R8 /* nothing */
473 #define CALLER_RESTORE_R8 /* nothing */
476 #ifdef CALLER_SAVES_R9
477 #define CALLER_SAVE_R9 SAVE_R9 = R9;
478 #define CALLER_RESTORE_R9 R9 = SAVE_R9;
480 #define CALLER_SAVE_R9 /* nothing */
481 #define CALLER_RESTORE_R9 /* nothing */
484 #ifdef CALLER_SAVES_R10
485 #define CALLER_SAVE_R10 SAVE_R10 = R10;
486 #define CALLER_RESTORE_R10 R10 = SAVE_R10;
488 #define CALLER_SAVE_R10 /* nothing */
489 #define CALLER_RESTORE_R10 /* nothing */
492 #ifdef CALLER_SAVES_F1
493 #define CALLER_SAVE_F1 SAVE_F1 = F1;
494 #define CALLER_RESTORE_F1 F1 = SAVE_F1;
496 #define CALLER_SAVE_F1 /* nothing */
497 #define CALLER_RESTORE_F1 /* nothing */
500 #ifdef CALLER_SAVES_F2
501 #define CALLER_SAVE_F2 SAVE_F2 = F2;
502 #define CALLER_RESTORE_F2 F2 = SAVE_F2;
504 #define CALLER_SAVE_F2 /* nothing */
505 #define CALLER_RESTORE_F2 /* nothing */
508 #ifdef CALLER_SAVES_F3
509 #define CALLER_SAVE_F3 SAVE_F3 = F3;
510 #define CALLER_RESTORE_F3 F3 = SAVE_F3;
512 #define CALLER_SAVE_F3 /* nothing */
513 #define CALLER_RESTORE_F3 /* nothing */
516 #ifdef CALLER_SAVES_F4
517 #define CALLER_SAVE_F4 SAVE_F4 = F4;
518 #define CALLER_RESTORE_F4 F4 = SAVE_F4;
520 #define CALLER_SAVE_F4 /* nothing */
521 #define CALLER_RESTORE_F4 /* nothing */
524 #ifdef CALLER_SAVES_D1
525 #define CALLER_SAVE_D1 SAVE_D1 = D1;
526 #define CALLER_RESTORE_D1 D1 = SAVE_D1;
528 #define CALLER_SAVE_D1 /* nothing */
529 #define CALLER_RESTORE_D1 /* nothing */
532 #ifdef CALLER_SAVES_D2
533 #define CALLER_SAVE_D2 SAVE_D2 = D2;
534 #define CALLER_RESTORE_D2 D2 = SAVE_D2;
536 #define CALLER_SAVE_D2 /* nothing */
537 #define CALLER_RESTORE_D2 /* nothing */
540 #ifdef CALLER_SAVES_L1
541 #define CALLER_SAVE_L1 SAVE_L1 = L1;
542 #define CALLER_RESTORE_L1 L1 = SAVE_L1;
544 #define CALLER_SAVE_L1 /* nothing */
545 #define CALLER_RESTORE_L1 /* nothing */
548 #ifdef CALLER_SAVES_Sp
549 #define CALLER_SAVE_Sp SAVE_Sp = Sp;
550 #define CALLER_RESTORE_Sp Sp = SAVE_Sp;
552 #define CALLER_SAVE_Sp /* nothing */
553 #define CALLER_RESTORE_Sp /* nothing */
556 #ifdef CALLER_SAVES_Su
557 #define CALLER_SAVE_Su SAVE_Su = Su;
558 #define CALLER_RESTORE_Su Su = SAVE_Su;
560 #define CALLER_SAVE_Su /* nothing */
561 #define CALLER_RESTORE_Su /* nothing */
564 #ifdef CALLER_SAVES_SpLim
565 #define CALLER_SAVE_SpLim SAVE_SpLim = SpLim;
566 #define CALLER_RESTORE_SpLim SpLim = SAVE_SpLim;
568 #define CALLER_SAVE_SpLim /* nothing */
569 #define CALLER_RESTORE_SpLim /* nothing */
572 #ifdef CALLER_SAVES_Hp
573 #define CALLER_SAVE_Hp SAVE_Hp = Hp;
574 #define CALLER_RESTORE_Hp Hp = SAVE_Hp;
576 #define CALLER_SAVE_Hp /* nothing */
577 #define CALLER_RESTORE_Hp /* nothing */
580 #ifdef CALLER_SAVES_HpLim
581 #define CALLER_SAVE_HpLim SAVE_HpLim = HpLim;
582 #define CALLER_RESTORE_HpLim HpLim = SAVE_HpLim;
584 #define CALLER_SAVE_HpLim /* nothing */
585 #define CALLER_RESTORE_HpLim /* nothing */
588 #ifdef CALLER_SAVES_Base
590 #error "Can't have caller-saved BaseReg with SMP"
592 #define CALLER_SAVE_Base /* nothing */
593 #define CALLER_RESTORE_Base BaseReg = &MainRegTable;
595 #define CALLER_SAVE_Base /* nothing */
596 #define CALLER_RESTORE_Base /* nothing */
599 #ifdef CALLER_SAVES_CurrentTSO
600 #define CALLER_SAVE_CurrentTSO SAVE_CurrentTSO = CurrentTSO;
601 #define CALLER_RESTORE_CurrentTSO CurrentTSO = SAVE_CurrentTSO;
603 #define CALLER_SAVE_CurrentTSO /* nothing */
604 #define CALLER_RESTORE_CurrentTSO /* nothing */
607 #ifdef CALLER_SAVES_CurrentNursery
608 #define CALLER_SAVE_CurrentNursery SAVE_CurrentNursery = CurrentNursery;
609 #define CALLER_RESTORE_CurrentNursery CurrentNursery = SAVE_CurrentNursery;
611 #define CALLER_SAVE_CurrentNursery /* nothing */
612 #define CALLER_RESTORE_CurrentNursery /* nothing */
615 #ifdef CALLER_SAVES_HpAlloc
616 #define CALLER_SAVE_HpAlloc SAVE_HpAlloc = HpAlloc;
617 #define CALLER_RESTORE_HpAlloc HpAlloc = SAVE_HpAlloc;
619 #define CALLER_SAVE_HpAlloc /* nothing */
620 #define CALLER_RESTORE_HpAlloc /* nothing */
623 #ifdef CALLER_SAVES_SparkHd
624 #define CALLER_SAVE_SparkHd SAVE_SparkHd = SparkHd;
625 #define CALLER_RESTORE_SparkHd SparkHd = SAVE_SparkHd;
627 #define CALLER_SAVE_SparkHd /* nothing */
628 #define CALLER_RESTORE_SparkHd /* nothing */
631 #ifdef CALLER_SAVES_SparkTl
632 #define CALLER_SAVE_SparkTl SAVE_SparkTl = SparkTl;
633 #define CALLER_RESTORE_SparkTl SparkTl = SAVE_SparkTl;
635 #define CALLER_SAVE_SparkTl /* nothing */
636 #define CALLER_RESTORE_SparkTl /* nothing */
639 #ifdef CALLER_SAVES_SparkBase
640 #define CALLER_SAVE_SparkBase SAVE_SparkBase = SparkBase;
641 #define CALLER_RESTORE_SparkBase SparkBase = SAVE_SparkBase;
643 #define CALLER_SAVE_SparkBase /* nothing */
644 #define CALLER_RESTORE_SparkBase /* nothing */
647 #ifdef CALLER_SAVES_SparkLim
648 #define CALLER_SAVE_SparkLim SAVE_SparkLim = SparkLim;
649 #define CALLER_RESTORE_SparkLim SparkLim = SAVE_SparkLim;
651 #define CALLER_SAVE_SparkLim /* nothing */
652 #define CALLER_RESTORE_SparkLim /* nothing */
655 #endif /* IN_STG_CODE */
657 /* ----------------------------------------------------------------------------
658 Handy bunches of saves/restores
659 ------------------------------------------------------------------------ */
663 #define CALLER_SAVE_USER \
680 /* Save Base last, since the others may
681 be addressed relative to it */
682 #define CALLER_SAVE_SYSTEM \
688 CALLER_SAVE_CurrentTSO \
689 CALLER_SAVE_CurrentNursery \
690 CALLER_SAVE_SparkHd \
691 CALLER_SAVE_SparkTl \
692 CALLER_SAVE_SparkBase \
693 CALLER_SAVE_SparkLim \
696 #define CALLER_RESTORE_USER \
713 /* Restore Base first, since the others may
714 be addressed relative to it */
715 #define CALLER_RESTORE_SYSTEM \
716 CALLER_RESTORE_Base \
719 CALLER_RESTORE_SpLim \
721 CALLER_RESTORE_HpLim \
722 CALLER_RESTORE_CurrentTSO \
723 CALLER_RESTORE_CurrentNursery \
724 CALLER_RESTORE_SparkHd \
725 CALLER_RESTORE_SparkTl \
726 CALLER_RESTORE_SparkBase \
727 CALLER_RESTORE_SparkLim
729 #else /* not IN_STG_CODE */
731 #define CALLER_SAVE_USER /* nothing */
732 #define CALLER_SAVE_SYSTEM /* nothing */
733 #define CALLER_RESTORE_USER /* nothing */
734 #define CALLER_RESTORE_SYSTEM /* nothing */
736 #endif /* IN_STG_CODE */
738 #define CALLER_SAVE_ALL \
742 #define CALLER_RESTORE_ALL \
743 CALLER_RESTORE_SYSTEM \