1 /* -----------------------------------------------------------------------------
2 * $Id: Regs.h,v 1.12 2002/12/11 15:36:37 simonmar 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_ {
36 StgFunPtr stgGCEnter1;
40 typedef struct StgRegTable_ {
49 StgUnion rR9; // used occasionally by heap/stack checks
50 StgUnion rR10; // used occasionally by heap/stack checks
63 struct _bdescr *rNursery;
64 struct _bdescr *rCurrentNursery;
65 StgWord rHpAlloc; // number of words being allocated in heap
66 #if defined(SMP) || defined(PAR)
67 StgSparkPool rSparks; // per-task spark pool
72 /* A capability is a combination of a FunTable and a RegTable. In STG
73 * code, BaseReg normally points to the RegTable portion of this
74 * structure, so that we can index both forwards and backwards to take
75 * advantage of shorter instruction forms on some archs (eg. x86).
77 typedef struct Capability_ {
81 struct Capability_ *link; /* per-task register tables are linked together */
85 /* No such thing as a MainRegTable under SMP - each thread must
86 * have its own MainRegTable.
89 extern DLL_IMPORT_RTS Capability MainCapability;
95 * Registers Hp and HpLim are global across the entire system, and are
96 * copied into the RegTable before executing a thread.
98 * Registers Sp and SpLim are saved in the TSO for the
99 * thread, but are copied into the RegTable before executing a thread.
101 * All other registers are "general purpose", and are used for passing
102 * arguments to functions, and returning values. The code generator
103 * knows how many of these are in real registers, and avoids
104 * generating code that uses non-real registers. General purpose
105 * registers are never saved when returning to the scheduler, instead
106 * we save whatever is live at the time on the stack, and restore it
107 * later. This should reduce the context switch time, amongst other
110 * For argument passing, the stack will be used in preference to
111 * pseudo-registers if the architecture has too few general purpose
114 * Some special RTS functions like newArray and the Integer primitives
115 * expect their arguments to be in registers R1-Rn, so we use these
116 * (pseudo-)registers in those cases.
120 * Locations for saving per-thread registers.
123 #define SAVE_Sp (CurrentTSO->sp)
124 #define SAVE_SpLim (CurrentTSO->splim)
126 #define SAVE_Hp (BaseReg->rHp)
127 #define SAVE_HpLim (BaseReg->rHpLim)
129 #define SAVE_CurrentTSO (BaseReg->rCurrentTSO)
130 #define SAVE_CurrentNursery (BaseReg->rCurrentNursery)
131 #define SAVE_HpAlloc (BaseReg->rHpAlloc)
132 #if defined(SMP) || defined(PAR)
133 #define SAVE_SparkHd (BaseReg->rSparks.hd)
134 #define SAVE_SparkTl (BaseReg->rSparks.tl)
135 #define SAVE_SparkBase (BaseReg->rSparks.base)
136 #define SAVE_SparkLim (BaseReg->rSparks.lim)
139 /* We sometimes need to save registers across a C-call, eg. if they
140 * are clobbered in the standard calling convention. We define the
141 * save locations for all registers in the register table.
144 #define SAVE_R1 (BaseReg->rR1)
145 #define SAVE_R2 (BaseReg->rR2)
146 #define SAVE_R3 (BaseReg->rR3)
147 #define SAVE_R4 (BaseReg->rR4)
148 #define SAVE_R5 (BaseReg->rR5)
149 #define SAVE_R6 (BaseReg->rR6)
150 #define SAVE_R7 (BaseReg->rR7)
151 #define SAVE_R8 (BaseReg->rR8)
153 #define SAVE_F1 (BaseReg->rF1)
154 #define SAVE_F2 (BaseReg->rF2)
155 #define SAVE_F3 (BaseReg->rF3)
156 #define SAVE_F4 (BaseReg->rF4)
158 #define SAVE_D1 (BaseReg->rD1)
159 #define SAVE_D2 (BaseReg->rD2)
161 #define SAVE_L1 (BaseReg->rL1)
163 /* -----------------------------------------------------------------------------
164 * Emit the GCC-specific register declarations for each machine
165 * register being used. If any STG register isn't mapped to a machine
166 * register, then map it to an offset from BaseReg.
168 * First, the general purpose registers. The idea is, if a particular
169 * general-purpose STG register can't be mapped to a real machine
170 * register, it won't be used at all. Instead, we'll use the stack.
172 * This is an improvement on the way things used to be done, when all
173 * registers were mapped to locations in the register table, and stuff
174 * was being shifted from the stack to the register table and back
175 * again for no good reason (on register-poor architectures).
178 #define GLOBAL_REG_DECL(type,name,reg) register type name REG(reg);
181 GLOBAL_REG_DECL(StgUnion,R1,REG_R1)
183 #define R1 (BaseReg->rR1)
187 GLOBAL_REG_DECL(StgUnion,R2,REG_R2)
189 #define R2 (BaseReg->rR2)
193 GLOBAL_REG_DECL(StgUnion,R3,REG_R3)
195 # define R3 (BaseReg->rR3)
199 GLOBAL_REG_DECL(StgUnion,R4,REG_R4)
201 # define R4 (BaseReg->rR4)
205 GLOBAL_REG_DECL(StgUnion,R5,REG_R5)
207 # define R5 (BaseReg->rR5)
211 GLOBAL_REG_DECL(StgUnion,R6,REG_R6)
213 # define R6 (BaseReg->rR6)
217 GLOBAL_REG_DECL(StgUnion,R7,REG_R7)
219 # define R7 (BaseReg->rR7)
223 GLOBAL_REG_DECL(StgUnion,R8,REG_R8)
225 # define R8 (BaseReg->rR8)
229 GLOBAL_REG_DECL(StgUnion,R9,REG_R9)
231 # define R9 (BaseReg->rR9)
235 GLOBAL_REG_DECL(StgUnion,R10,REG_R10)
237 # define R10 (BaseReg->rR10)
241 GLOBAL_REG_DECL(StgFloat,F1,REG_F1)
243 #define F1 (BaseReg->rF1)
247 GLOBAL_REG_DECL(StgFloat,F2,REG_F2)
249 #define F2 (BaseReg->rF2)
253 GLOBAL_REG_DECL(StgFloat,F3,REG_F3)
255 #define F3 (BaseReg->rF3)
259 GLOBAL_REG_DECL(StgFloat,F4,REG_F4)
261 #define F4 (BaseReg->rF4)
265 GLOBAL_REG_DECL(StgDouble,D1,REG_D1)
267 #define D1 (BaseReg->rD1)
271 GLOBAL_REG_DECL(StgDouble,D2,REG_D2)
273 #define D2 (BaseReg->rD2)
277 GLOBAL_REG_DECL(StgWord64,L1,REG_L1)
279 #define L1 (BaseReg->rL1)
283 * If BaseReg isn't mapped to a machine register, just use the global
284 * address of the current register table (CurrentRegTable in
285 * concurrent Haskell, MainRegTable otherwise).
289 GLOBAL_REG_DECL(StgRegTable *,BaseReg,REG_Base)
292 #error BaseReg must be in a register for SMP
294 #define BaseReg (&MainCapability.r)
298 GLOBAL_REG_DECL(P_,Sp,REG_Sp)
300 #define Sp (BaseReg->rSp)
304 GLOBAL_REG_DECL(P_,SpLim,REG_SpLim)
306 #define SpLim (BaseReg->rSpLim)
310 GLOBAL_REG_DECL(P_,Hp,REG_Hp)
312 #define Hp (BaseReg->rHp)
316 GLOBAL_REG_DECL(P_,HpLim,REG_HpLim)
318 #define HpLim (BaseReg->rHpLim)
321 #ifdef REG_CurrentTSO
322 GLOBAL_REG_DECL(StgTSO *,CurrentTSO,REG_CurrentTSO)
324 #define CurrentTSO (BaseReg->rCurrentTSO)
327 #ifdef REG_CurrentNursery
328 GLOBAL_REG_DECL(bdescr *,CurrentNursery,REG_CurrentNursery)
330 #define CurrentNursery (BaseReg->rCurrentNursery)
334 GLOBAL_REG_DECL(bdescr *,HpAlloc,REG_HpAlloc)
336 #define HpAlloc (BaseReg->rHpAlloc)
340 GLOBAL_REG_DECL(bdescr *,SparkHd,REG_SparkHd)
342 #define SparkHd (BaseReg->rSparks.hd)
346 GLOBAL_REG_DECL(bdescr *,SparkTl,REG_SparkTl)
348 #define SparkTl (BaseReg->rSparks.tl)
352 GLOBAL_REG_DECL(bdescr *,SparkBase,REG_SparkBase)
354 #define SparkBase (BaseReg->rSparks.base)
358 GLOBAL_REG_DECL(bdescr *,SparkLim,REG_SparkLim)
360 #define SparkLim (BaseReg->rSparks.lim)
363 /* -----------------------------------------------------------------------------
364 Get absolute function pointers from the register table, to save
373 as long as the offset is within the range of a signed byte
374 (-128..+127). So we pick some common absolute_addresses and put
375 them in the register table. As a bonus, linking time should also
378 Other possible candidates in order of importance:
381 stg_CAF_BLACKHOLE_info
384 anything else probably isn't worth the effort.
386 -------------------------------------------------------------------------- */
389 #define FunReg ((StgFunTable *)((void *)BaseReg - sizeof(StgFunTable)))
391 #define stg_gc_enter_1 (FunReg->stgGCEnter1)
392 #define stg_gc_fun (FunReg->stgGCFun)
394 /* -----------------------------------------------------------------------------
395 For any registers which are denoted "caller-saves" by the C calling
396 convention, we have to emit code to save and restore them across C
398 -------------------------------------------------------------------------- */
400 #ifdef CALLER_SAVES_R1
401 #define CALLER_SAVE_R1 SAVE_R1 = R1;
402 #define CALLER_RESTORE_R1 R1 = SAVE_R1;
404 #define CALLER_SAVE_R1 /* nothing */
405 #define CALLER_RESTORE_R1 /* nothing */
408 #ifdef CALLER_SAVES_R2
409 #define CALLER_SAVE_R2 SAVE_R2 = R2;
410 #define CALLER_RESTORE_R2 R2 = SAVE_R2;
412 #define CALLER_SAVE_R2 /* nothing */
413 #define CALLER_RESTORE_R2 /* nothing */
416 #ifdef CALLER_SAVES_R3
417 #define CALLER_SAVE_R3 SAVE_R3 = R3;
418 #define CALLER_RESTORE_R3 R3 = SAVE_R3;
420 #define CALLER_SAVE_R3 /* nothing */
421 #define CALLER_RESTORE_R3 /* nothing */
424 #ifdef CALLER_SAVES_R4
425 #define CALLER_SAVE_R4 SAVE_R4 = R4;
426 #define CALLER_RESTORE_R4 R4 = SAVE_R4;
428 #define CALLER_SAVE_R4 /* nothing */
429 #define CALLER_RESTORE_R4 /* nothing */
432 #ifdef CALLER_SAVES_R5
433 #define CALLER_SAVE_R5 SAVE_R5 = R5;
434 #define CALLER_RESTORE_R5 R5 = SAVE_R5;
436 #define CALLER_SAVE_R5 /* nothing */
437 #define CALLER_RESTORE_R5 /* nothing */
440 #ifdef CALLER_SAVES_R6
441 #define CALLER_SAVE_R6 SAVE_R6 = R6;
442 #define CALLER_RESTORE_R6 R6 = SAVE_R6;
444 #define CALLER_SAVE_R6 /* nothing */
445 #define CALLER_RESTORE_R6 /* nothing */
448 #ifdef CALLER_SAVES_R7
449 #define CALLER_SAVE_R7 SAVE_R7 = R7;
450 #define CALLER_RESTORE_R7 R7 = SAVE_R7;
452 #define CALLER_SAVE_R7 /* nothing */
453 #define CALLER_RESTORE_R7 /* nothing */
456 #ifdef CALLER_SAVES_R8
457 #define CALLER_SAVE_R8 SAVE_R8 = R8;
458 #define CALLER_RESTORE_R8 R8 = SAVE_R8;
460 #define CALLER_SAVE_R8 /* nothing */
461 #define CALLER_RESTORE_R8 /* nothing */
464 #ifdef CALLER_SAVES_R9
465 #define CALLER_SAVE_R9 SAVE_R9 = R9;
466 #define CALLER_RESTORE_R9 R9 = SAVE_R9;
468 #define CALLER_SAVE_R9 /* nothing */
469 #define CALLER_RESTORE_R9 /* nothing */
472 #ifdef CALLER_SAVES_R10
473 #define CALLER_SAVE_R10 SAVE_R10 = R10;
474 #define CALLER_RESTORE_R10 R10 = SAVE_R10;
476 #define CALLER_SAVE_R10 /* nothing */
477 #define CALLER_RESTORE_R10 /* nothing */
480 #ifdef CALLER_SAVES_F1
481 #define CALLER_SAVE_F1 SAVE_F1 = F1;
482 #define CALLER_RESTORE_F1 F1 = SAVE_F1;
484 #define CALLER_SAVE_F1 /* nothing */
485 #define CALLER_RESTORE_F1 /* nothing */
488 #ifdef CALLER_SAVES_F2
489 #define CALLER_SAVE_F2 SAVE_F2 = F2;
490 #define CALLER_RESTORE_F2 F2 = SAVE_F2;
492 #define CALLER_SAVE_F2 /* nothing */
493 #define CALLER_RESTORE_F2 /* nothing */
496 #ifdef CALLER_SAVES_F3
497 #define CALLER_SAVE_F3 SAVE_F3 = F3;
498 #define CALLER_RESTORE_F3 F3 = SAVE_F3;
500 #define CALLER_SAVE_F3 /* nothing */
501 #define CALLER_RESTORE_F3 /* nothing */
504 #ifdef CALLER_SAVES_F4
505 #define CALLER_SAVE_F4 SAVE_F4 = F4;
506 #define CALLER_RESTORE_F4 F4 = SAVE_F4;
508 #define CALLER_SAVE_F4 /* nothing */
509 #define CALLER_RESTORE_F4 /* nothing */
512 #ifdef CALLER_SAVES_D1
513 #define CALLER_SAVE_D1 SAVE_D1 = D1;
514 #define CALLER_RESTORE_D1 D1 = SAVE_D1;
516 #define CALLER_SAVE_D1 /* nothing */
517 #define CALLER_RESTORE_D1 /* nothing */
520 #ifdef CALLER_SAVES_D2
521 #define CALLER_SAVE_D2 SAVE_D2 = D2;
522 #define CALLER_RESTORE_D2 D2 = SAVE_D2;
524 #define CALLER_SAVE_D2 /* nothing */
525 #define CALLER_RESTORE_D2 /* nothing */
528 #ifdef CALLER_SAVES_L1
529 #define CALLER_SAVE_L1 SAVE_L1 = L1;
530 #define CALLER_RESTORE_L1 L1 = SAVE_L1;
532 #define CALLER_SAVE_L1 /* nothing */
533 #define CALLER_RESTORE_L1 /* nothing */
536 #ifdef CALLER_SAVES_Sp
537 #define CALLER_SAVE_Sp SAVE_Sp = Sp;
538 #define CALLER_RESTORE_Sp Sp = SAVE_Sp;
540 #define CALLER_SAVE_Sp /* nothing */
541 #define CALLER_RESTORE_Sp /* nothing */
544 #ifdef CALLER_SAVES_SpLim
545 #define CALLER_SAVE_SpLim SAVE_SpLim = SpLim;
546 #define CALLER_RESTORE_SpLim SpLim = SAVE_SpLim;
548 #define CALLER_SAVE_SpLim /* nothing */
549 #define CALLER_RESTORE_SpLim /* nothing */
552 #ifdef CALLER_SAVES_Hp
553 #define CALLER_SAVE_Hp SAVE_Hp = Hp;
554 #define CALLER_RESTORE_Hp Hp = SAVE_Hp;
556 #define CALLER_SAVE_Hp /* nothing */
557 #define CALLER_RESTORE_Hp /* nothing */
560 #ifdef CALLER_SAVES_HpLim
561 #define CALLER_SAVE_HpLim SAVE_HpLim = HpLim;
562 #define CALLER_RESTORE_HpLim HpLim = SAVE_HpLim;
564 #define CALLER_SAVE_HpLim /* nothing */
565 #define CALLER_RESTORE_HpLim /* nothing */
568 #ifdef CALLER_SAVES_Base
570 #error "Can't have caller-saved BaseReg with SMP"
572 #define CALLER_SAVE_Base /* nothing */
573 #define CALLER_RESTORE_Base BaseReg = &MainRegTable;
575 #define CALLER_SAVE_Base /* nothing */
576 #define CALLER_RESTORE_Base /* nothing */
579 #ifdef CALLER_SAVES_CurrentTSO
580 #define CALLER_SAVE_CurrentTSO SAVE_CurrentTSO = CurrentTSO;
581 #define CALLER_RESTORE_CurrentTSO CurrentTSO = SAVE_CurrentTSO;
583 #define CALLER_SAVE_CurrentTSO /* nothing */
584 #define CALLER_RESTORE_CurrentTSO /* nothing */
587 #ifdef CALLER_SAVES_CurrentNursery
588 #define CALLER_SAVE_CurrentNursery SAVE_CurrentNursery = CurrentNursery;
589 #define CALLER_RESTORE_CurrentNursery CurrentNursery = SAVE_CurrentNursery;
591 #define CALLER_SAVE_CurrentNursery /* nothing */
592 #define CALLER_RESTORE_CurrentNursery /* nothing */
595 #ifdef CALLER_SAVES_HpAlloc
596 #define CALLER_SAVE_HpAlloc SAVE_HpAlloc = HpAlloc;
597 #define CALLER_RESTORE_HpAlloc HpAlloc = SAVE_HpAlloc;
599 #define CALLER_SAVE_HpAlloc /* nothing */
600 #define CALLER_RESTORE_HpAlloc /* nothing */
603 #ifdef CALLER_SAVES_SparkHd
604 #define CALLER_SAVE_SparkHd SAVE_SparkHd = SparkHd;
605 #define CALLER_RESTORE_SparkHd SparkHd = SAVE_SparkHd;
607 #define CALLER_SAVE_SparkHd /* nothing */
608 #define CALLER_RESTORE_SparkHd /* nothing */
611 #ifdef CALLER_SAVES_SparkTl
612 #define CALLER_SAVE_SparkTl SAVE_SparkTl = SparkTl;
613 #define CALLER_RESTORE_SparkTl SparkTl = SAVE_SparkTl;
615 #define CALLER_SAVE_SparkTl /* nothing */
616 #define CALLER_RESTORE_SparkTl /* nothing */
619 #ifdef CALLER_SAVES_SparkBase
620 #define CALLER_SAVE_SparkBase SAVE_SparkBase = SparkBase;
621 #define CALLER_RESTORE_SparkBase SparkBase = SAVE_SparkBase;
623 #define CALLER_SAVE_SparkBase /* nothing */
624 #define CALLER_RESTORE_SparkBase /* nothing */
627 #ifdef CALLER_SAVES_SparkLim
628 #define CALLER_SAVE_SparkLim SAVE_SparkLim = SparkLim;
629 #define CALLER_RESTORE_SparkLim SparkLim = SAVE_SparkLim;
631 #define CALLER_SAVE_SparkLim /* nothing */
632 #define CALLER_RESTORE_SparkLim /* nothing */
635 #endif /* IN_STG_CODE */
637 /* ----------------------------------------------------------------------------
638 Handy bunches of saves/restores
639 ------------------------------------------------------------------------ */
643 #define CALLER_SAVE_USER \
660 /* Save Base last, since the others may
661 be addressed relative to it */
662 #define CALLER_SAVE_SYSTEM \
667 CALLER_SAVE_CurrentTSO \
668 CALLER_SAVE_CurrentNursery \
669 CALLER_SAVE_SparkHd \
670 CALLER_SAVE_SparkTl \
671 CALLER_SAVE_SparkBase \
672 CALLER_SAVE_SparkLim \
675 #define CALLER_RESTORE_USER \
692 /* Restore Base first, since the others may
693 be addressed relative to it */
694 #define CALLER_RESTORE_SYSTEM \
695 CALLER_RESTORE_Base \
697 CALLER_RESTORE_SpLim \
699 CALLER_RESTORE_HpLim \
700 CALLER_RESTORE_CurrentTSO \
701 CALLER_RESTORE_CurrentNursery \
702 CALLER_RESTORE_SparkHd \
703 CALLER_RESTORE_SparkTl \
704 CALLER_RESTORE_SparkBase \
705 CALLER_RESTORE_SparkLim
707 #else /* not IN_STG_CODE */
709 #define CALLER_SAVE_USER /* nothing */
710 #define CALLER_SAVE_SYSTEM /* nothing */
711 #define CALLER_RESTORE_USER /* nothing */
712 #define CALLER_RESTORE_SYSTEM /* nothing */
714 #endif /* IN_STG_CODE */
716 #define CALLER_SAVE_ALL \
720 #define CALLER_RESTORE_ALL \
721 CALLER_RESTORE_SYSTEM \