1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * Registers in the STG machine.
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
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
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).
20 * ---------------------------------------------------------------------------*/
26 StgWord stgEagerBlackholeInfo;
27 StgFunPtr stgGCEnter1;
32 * Vanilla registers are given this union type, which is purely so
33 * that we can cast the vanilla reg to a variety of types with the
34 * minimum of syntax. eg. R1.w instead of (StgWord)R1.
45 StgStackOffset offset; /* unused? */
50 // Urgh.. we don't know the size of an MP_INT here because we haven't
51 // #included gmp.h. We should really autoconf this, but GMP may not
52 // be available at ./configure time if we're building it (GMP) locally.
53 #define MP_INT_WORDS 3
56 * This is the table that holds shadow-locations for all the STG
57 * registers. The shadow locations are used when:
59 * 1) the particular register isn't mapped to a real machine
60 * register, probably because there's a shortage of real registers.
61 * 2) caller-saves registers are saved across a CCall
63 typedef struct StgRegTable_ {
72 StgUnion rR9; /* used occasionally by heap/stack checks */
73 StgUnion rR10; /* used occasionally by heap/stack checks */
85 struct StgTSO_ *rCurrentTSO;
86 struct step_ *rNursery;
87 struct bdescr_ *rCurrentNursery; /* Hp/HpLim point into this block */
88 struct bdescr_ *rCurrentAlloc; /* for allocation using allocate() */
89 StgWord rHpAlloc; /* number of *bytes* being allocated in heap */
90 // rmp_tmp1..rmp_result2 are only used in THREADED_RTS builds to
91 // avoid per-thread temps in bss, but currently always incldue here
92 // so we just run mkDerivedConstants once
93 StgWord rmp_tmp_w[MP_INT_WORDS];
94 StgWord rmp_tmp1[MP_INT_WORDS];
95 StgWord rmp_tmp2[MP_INT_WORDS];
96 StgWord rmp_result1[MP_INT_WORDS];
97 StgWord rmp_result2[MP_INT_WORDS];
98 StgWord rRet; // holds the return code of the thread
104 * Registers Hp and HpLim are global across the entire system, and are
105 * copied into the RegTable before executing a thread.
107 * Registers Sp and SpLim are saved in the TSO for the
108 * thread, but are copied into the RegTable before executing a thread.
110 * All other registers are "general purpose", and are used for passing
111 * arguments to functions, and returning values. The code generator
112 * knows how many of these are in real registers, and avoids
113 * generating code that uses non-real registers. General purpose
114 * registers are never saved when returning to the scheduler, instead
115 * we save whatever is live at the time on the stack, and restore it
116 * later. This should reduce the context switch time, amongst other
119 * For argument passing, the stack will be used in preference to
120 * pseudo-registers if the architecture has too few general purpose
123 * Some special RTS functions like newArray and the Integer primitives
124 * expect their arguments to be in registers R1-Rn, so we use these
125 * (pseudo-)registers in those cases.
129 * Locations for saving per-thread registers.
132 #define SAVE_Sp (CurrentTSO->sp)
133 #define SAVE_SpLim (CurrentTSO->splim)
135 #define SAVE_Hp (BaseReg->rHp)
136 #define SAVE_HpLim (BaseReg->rHpLim)
138 #define SAVE_CurrentTSO (BaseReg->rCurrentTSO)
139 #define SAVE_CurrentNursery (BaseReg->rCurrentNursery)
140 #define SAVE_HpAlloc (BaseReg->rHpAlloc)
142 /* We sometimes need to save registers across a C-call, eg. if they
143 * are clobbered in the standard calling convention. We define the
144 * save locations for all registers in the register table.
147 #define SAVE_R1 (BaseReg->rR1)
148 #define SAVE_R2 (BaseReg->rR2)
149 #define SAVE_R3 (BaseReg->rR3)
150 #define SAVE_R4 (BaseReg->rR4)
151 #define SAVE_R5 (BaseReg->rR5)
152 #define SAVE_R6 (BaseReg->rR6)
153 #define SAVE_R7 (BaseReg->rR7)
154 #define SAVE_R8 (BaseReg->rR8)
156 #define SAVE_F1 (BaseReg->rF1)
157 #define SAVE_F2 (BaseReg->rF2)
158 #define SAVE_F3 (BaseReg->rF3)
159 #define SAVE_F4 (BaseReg->rF4)
161 #define SAVE_D1 (BaseReg->rD1)
162 #define SAVE_D2 (BaseReg->rD2)
164 #define SAVE_L1 (BaseReg->rL1)
166 /* -----------------------------------------------------------------------------
167 * Emit the GCC-specific register declarations for each machine
168 * register being used. If any STG register isn't mapped to a machine
169 * register, then map it to an offset from BaseReg.
171 * First, the general purpose registers. The idea is, if a particular
172 * general-purpose STG register can't be mapped to a real machine
173 * register, it won't be used at all. Instead, we'll use the stack.
175 * This is an improvement on the way things used to be done, when all
176 * registers were mapped to locations in the register table, and stuff
177 * was being shifted from the stack to the register table and back
178 * again for no good reason (on register-poor architectures).
181 /* define NO_REGS to omit register declarations - used in RTS C code
182 * that needs all the STG definitions but not the global register
185 #define GLOBAL_REG_DECL(type,name,reg) register type name REG(reg);
187 #if defined(REG_R1) && !defined(NO_GLOBAL_REG_DECLS)
188 GLOBAL_REG_DECL(StgUnion,R1,REG_R1)
190 # define R1 (BaseReg->rR1)
193 #if defined(REG_R2) && !defined(NO_GLOBAL_REG_DECLS)
194 GLOBAL_REG_DECL(StgUnion,R2,REG_R2)
196 # define R2 (BaseReg->rR2)
199 #if defined(REG_R3) && !defined(NO_GLOBAL_REG_DECLS)
200 GLOBAL_REG_DECL(StgUnion,R3,REG_R3)
202 # define R3 (BaseReg->rR3)
205 #if defined(REG_R4) && !defined(NO_GLOBAL_REG_DECLS)
206 GLOBAL_REG_DECL(StgUnion,R4,REG_R4)
208 # define R4 (BaseReg->rR4)
211 #if defined(REG_R5) && !defined(NO_GLOBAL_REG_DECLS)
212 GLOBAL_REG_DECL(StgUnion,R5,REG_R5)
214 # define R5 (BaseReg->rR5)
217 #if defined(REG_R6) && !defined(NO_GLOBAL_REG_DECLS)
218 GLOBAL_REG_DECL(StgUnion,R6,REG_R6)
220 # define R6 (BaseReg->rR6)
223 #if defined(REG_R7) && !defined(NO_GLOBAL_REG_DECLS)
224 GLOBAL_REG_DECL(StgUnion,R7,REG_R7)
226 # define R7 (BaseReg->rR7)
229 #if defined(REG_R8) && !defined(NO_GLOBAL_REG_DECLS)
230 GLOBAL_REG_DECL(StgUnion,R8,REG_R8)
232 # define R8 (BaseReg->rR8)
235 #if defined(REG_R9) && !defined(NO_GLOBAL_REG_DECLS)
236 GLOBAL_REG_DECL(StgUnion,R9,REG_R9)
238 # define R9 (BaseReg->rR9)
241 #if defined(REG_R10) && !defined(NO_GLOBAL_REG_DECLS)
242 GLOBAL_REG_DECL(StgUnion,R10,REG_R10)
244 # define R10 (BaseReg->rR10)
247 #if defined(REG_F1) && !defined(NO_GLOBAL_REG_DECLS)
248 GLOBAL_REG_DECL(StgFloat,F1,REG_F1)
250 #define F1 (BaseReg->rF1)
253 #if defined(REG_F2) && !defined(NO_GLOBAL_REG_DECLS)
254 GLOBAL_REG_DECL(StgFloat,F2,REG_F2)
256 #define F2 (BaseReg->rF2)
259 #if defined(REG_F3) && !defined(NO_GLOBAL_REG_DECLS)
260 GLOBAL_REG_DECL(StgFloat,F3,REG_F3)
262 #define F3 (BaseReg->rF3)
265 #if defined(REG_F4) && !defined(NO_GLOBAL_REG_DECLS)
266 GLOBAL_REG_DECL(StgFloat,F4,REG_F4)
268 #define F4 (BaseReg->rF4)
271 #if defined(REG_D1) && !defined(NO_GLOBAL_REG_DECLS)
272 GLOBAL_REG_DECL(StgDouble,D1,REG_D1)
274 #define D1 (BaseReg->rD1)
277 #if defined(REG_D2) && !defined(NO_GLOBAL_REG_DECLS)
278 GLOBAL_REG_DECL(StgDouble,D2,REG_D2)
280 #define D2 (BaseReg->rD2)
283 #if defined(REG_L1) && !defined(NO_GLOBAL_REG_DECLS)
284 GLOBAL_REG_DECL(StgWord64,L1,REG_L1)
286 #define L1 (BaseReg->rL1)
290 * If BaseReg isn't mapped to a machine register, just use the global
291 * address of the current register table (CurrentRegTable in
292 * concurrent Haskell, MainRegTable otherwise).
295 /* A capability is a combination of a FunTable and a RegTable. In STG
296 * code, BaseReg normally points to the RegTable portion of this
297 * structure, so that we can index both forwards and backwards to take
298 * advantage of shorter instruction forms on some archs (eg. x86).
299 * This is a cut-down version of the Capability structure; the full
300 * version is defined in Capability.h.
302 struct PartCapability_ {
307 /* No such thing as a MainCapability under THREADED_RTS - each thread must have
308 * its own Capability.
310 #if IN_STG_CODE && !(defined(THREADED_RTS) && !defined(NOSMP))
311 extern W_ MainCapability[];
315 * Assigning to BaseReg (the ASSIGN_BaseReg macro): this happens on
316 * return from a "safe" foreign call, when the thread might be running
317 * on a new Capability. Obviously if BaseReg is not a register, then
318 * we are restricted to a single Capability (this invariant is enforced
319 * in Capability.c:initCapabilities), and assigning to BaseReg can be omitted.
322 #if defined(REG_Base) && !defined(NO_GLOBAL_REG_DECLS)
323 GLOBAL_REG_DECL(StgRegTable *,BaseReg,REG_Base)
324 #define ASSIGN_BaseReg(e) (BaseReg = (e))
326 #if defined(THREADED_RTS) && !defined(NOSMP)
327 #error BaseReg must be in a register for THREADED_RTS
329 #define BaseReg (&((struct PartCapability_ *)MainCapability)->r)
330 #define ASSIGN_BaseReg(e) (e)
333 #if defined(REG_Sp) && !defined(NO_GLOBAL_REG_DECLS)
334 GLOBAL_REG_DECL(P_,Sp,REG_Sp)
336 #define Sp (BaseReg->rSp)
339 #if defined(REG_SpLim) && !defined(NO_GLOBAL_REG_DECLS)
340 GLOBAL_REG_DECL(P_,SpLim,REG_SpLim)
342 #define SpLim (BaseReg->rSpLim)
345 #if defined(REG_Hp) && !defined(NO_GLOBAL_REG_DECLS)
346 GLOBAL_REG_DECL(P_,Hp,REG_Hp)
348 #define Hp (BaseReg->rHp)
351 #if defined(REG_HpLim) && !defined(NO_GLOBAL_REG_DECLS)
352 GLOBAL_REG_DECL(P_,HpLim,REG_HpLim)
354 #define HpLim (BaseReg->rHpLim)
357 #if defined(REG_CurrentTSO) && !defined(NO_GLOBAL_REG_DECLS)
358 GLOBAL_REG_DECL(struct _StgTSO *,CurrentTSO,REG_CurrentTSO)
360 #define CurrentTSO (BaseReg->rCurrentTSO)
363 #if defined(REG_CurrentNursery) && !defined(NO_GLOBAL_REG_DECLS)
364 GLOBAL_REG_DECL(bdescr *,CurrentNursery,REG_CurrentNursery)
366 #define CurrentNursery (BaseReg->rCurrentNursery)
369 #if defined(REG_HpAlloc) && !defined(NO_GLOBAL_REG_DECLS)
370 GLOBAL_REG_DECL(bdescr *,HpAlloc,REG_HpAlloc)
372 #define HpAlloc (BaseReg->rHpAlloc)
375 /* -----------------------------------------------------------------------------
376 Get absolute function pointers from the register table, to save
385 as long as the offset is within the range of a signed byte
386 (-128..+127). So we pick some common absolute_addresses and put
387 them in the register table. As a bonus, linking time should also
390 Other possible candidates in order of importance:
393 stg_CAF_BLACKHOLE_info
396 anything else probably isn't worth the effort.
398 -------------------------------------------------------------------------- */
401 #define FunReg ((StgFunTable *)((void *)BaseReg - FIELD_OFFSET(struct PartCapability_, r)))
403 #define stg_EAGER_BLACKHOLE_info (FunReg->stgEagerBlackholeInfo)
404 #define stg_gc_enter_1 (FunReg->stgGCEnter1)
405 #define stg_gc_fun (FunReg->stgGCFun)
407 /* -----------------------------------------------------------------------------
408 For any registers which are denoted "caller-saves" by the C calling
409 convention, we have to emit code to save and restore them across C
411 -------------------------------------------------------------------------- */
413 #ifdef CALLER_SAVES_R1
414 #define CALLER_SAVE_R1 SAVE_R1 = R1;
415 #define CALLER_RESTORE_R1 R1 = SAVE_R1;
417 #define CALLER_SAVE_R1 /* nothing */
418 #define CALLER_RESTORE_R1 /* nothing */
421 #ifdef CALLER_SAVES_R2
422 #define CALLER_SAVE_R2 SAVE_R2 = R2;
423 #define CALLER_RESTORE_R2 R2 = SAVE_R2;
425 #define CALLER_SAVE_R2 /* nothing */
426 #define CALLER_RESTORE_R2 /* nothing */
429 #ifdef CALLER_SAVES_R3
430 #define CALLER_SAVE_R3 SAVE_R3 = R3;
431 #define CALLER_RESTORE_R3 R3 = SAVE_R3;
433 #define CALLER_SAVE_R3 /* nothing */
434 #define CALLER_RESTORE_R3 /* nothing */
437 #ifdef CALLER_SAVES_R4
438 #define CALLER_SAVE_R4 SAVE_R4 = R4;
439 #define CALLER_RESTORE_R4 R4 = SAVE_R4;
441 #define CALLER_SAVE_R4 /* nothing */
442 #define CALLER_RESTORE_R4 /* nothing */
445 #ifdef CALLER_SAVES_R5
446 #define CALLER_SAVE_R5 SAVE_R5 = R5;
447 #define CALLER_RESTORE_R5 R5 = SAVE_R5;
449 #define CALLER_SAVE_R5 /* nothing */
450 #define CALLER_RESTORE_R5 /* nothing */
453 #ifdef CALLER_SAVES_R6
454 #define CALLER_SAVE_R6 SAVE_R6 = R6;
455 #define CALLER_RESTORE_R6 R6 = SAVE_R6;
457 #define CALLER_SAVE_R6 /* nothing */
458 #define CALLER_RESTORE_R6 /* nothing */
461 #ifdef CALLER_SAVES_R7
462 #define CALLER_SAVE_R7 SAVE_R7 = R7;
463 #define CALLER_RESTORE_R7 R7 = SAVE_R7;
465 #define CALLER_SAVE_R7 /* nothing */
466 #define CALLER_RESTORE_R7 /* nothing */
469 #ifdef CALLER_SAVES_R8
470 #define CALLER_SAVE_R8 SAVE_R8 = R8;
471 #define CALLER_RESTORE_R8 R8 = SAVE_R8;
473 #define CALLER_SAVE_R8 /* nothing */
474 #define CALLER_RESTORE_R8 /* nothing */
477 #ifdef CALLER_SAVES_R9
478 #define CALLER_SAVE_R9 SAVE_R9 = R9;
479 #define CALLER_RESTORE_R9 R9 = SAVE_R9;
481 #define CALLER_SAVE_R9 /* nothing */
482 #define CALLER_RESTORE_R9 /* nothing */
485 #ifdef CALLER_SAVES_R10
486 #define CALLER_SAVE_R10 SAVE_R10 = R10;
487 #define CALLER_RESTORE_R10 R10 = SAVE_R10;
489 #define CALLER_SAVE_R10 /* nothing */
490 #define CALLER_RESTORE_R10 /* nothing */
493 #ifdef CALLER_SAVES_F1
494 #define CALLER_SAVE_F1 SAVE_F1 = F1;
495 #define CALLER_RESTORE_F1 F1 = SAVE_F1;
497 #define CALLER_SAVE_F1 /* nothing */
498 #define CALLER_RESTORE_F1 /* nothing */
501 #ifdef CALLER_SAVES_F2
502 #define CALLER_SAVE_F2 SAVE_F2 = F2;
503 #define CALLER_RESTORE_F2 F2 = SAVE_F2;
505 #define CALLER_SAVE_F2 /* nothing */
506 #define CALLER_RESTORE_F2 /* nothing */
509 #ifdef CALLER_SAVES_F3
510 #define CALLER_SAVE_F3 SAVE_F3 = F3;
511 #define CALLER_RESTORE_F3 F3 = SAVE_F3;
513 #define CALLER_SAVE_F3 /* nothing */
514 #define CALLER_RESTORE_F3 /* nothing */
517 #ifdef CALLER_SAVES_F4
518 #define CALLER_SAVE_F4 SAVE_F4 = F4;
519 #define CALLER_RESTORE_F4 F4 = SAVE_F4;
521 #define CALLER_SAVE_F4 /* nothing */
522 #define CALLER_RESTORE_F4 /* nothing */
525 #ifdef CALLER_SAVES_D1
526 #define CALLER_SAVE_D1 SAVE_D1 = D1;
527 #define CALLER_RESTORE_D1 D1 = SAVE_D1;
529 #define CALLER_SAVE_D1 /* nothing */
530 #define CALLER_RESTORE_D1 /* nothing */
533 #ifdef CALLER_SAVES_D2
534 #define CALLER_SAVE_D2 SAVE_D2 = D2;
535 #define CALLER_RESTORE_D2 D2 = SAVE_D2;
537 #define CALLER_SAVE_D2 /* nothing */
538 #define CALLER_RESTORE_D2 /* nothing */
541 #ifdef CALLER_SAVES_L1
542 #define CALLER_SAVE_L1 SAVE_L1 = L1;
543 #define CALLER_RESTORE_L1 L1 = SAVE_L1;
545 #define CALLER_SAVE_L1 /* nothing */
546 #define CALLER_RESTORE_L1 /* nothing */
549 #ifdef CALLER_SAVES_Sp
550 #define CALLER_SAVE_Sp SAVE_Sp = Sp;
551 #define CALLER_RESTORE_Sp Sp = SAVE_Sp;
553 #define CALLER_SAVE_Sp /* nothing */
554 #define CALLER_RESTORE_Sp /* nothing */
557 #ifdef CALLER_SAVES_SpLim
558 #define CALLER_SAVE_SpLim SAVE_SpLim = SpLim;
559 #define CALLER_RESTORE_SpLim SpLim = SAVE_SpLim;
561 #define CALLER_SAVE_SpLim /* nothing */
562 #define CALLER_RESTORE_SpLim /* nothing */
565 #ifdef CALLER_SAVES_Hp
566 #define CALLER_SAVE_Hp SAVE_Hp = Hp;
567 #define CALLER_RESTORE_Hp Hp = SAVE_Hp;
569 #define CALLER_SAVE_Hp /* nothing */
570 #define CALLER_RESTORE_Hp /* nothing */
573 #ifdef CALLER_SAVES_HpLim
574 #define CALLER_SAVE_HpLim SAVE_HpLim = HpLim;
575 #define CALLER_RESTORE_HpLim HpLim = SAVE_HpLim;
577 #define CALLER_SAVE_HpLim /* nothing */
578 #define CALLER_RESTORE_HpLim /* nothing */
581 #ifdef CALLER_SAVES_Base
583 #error "Can't have caller-saved BaseReg with THREADED_RTS"
585 #define CALLER_SAVE_Base /* nothing */
586 #define CALLER_RESTORE_Base BaseReg = &MainRegTable;
588 #define CALLER_SAVE_Base /* nothing */
589 #define CALLER_RESTORE_Base /* nothing */
592 #ifdef CALLER_SAVES_CurrentTSO
593 #define CALLER_SAVE_CurrentTSO SAVE_CurrentTSO = CurrentTSO;
594 #define CALLER_RESTORE_CurrentTSO CurrentTSO = SAVE_CurrentTSO;
596 #define CALLER_SAVE_CurrentTSO /* nothing */
597 #define CALLER_RESTORE_CurrentTSO /* nothing */
600 #ifdef CALLER_SAVES_CurrentNursery
601 #define CALLER_SAVE_CurrentNursery SAVE_CurrentNursery = CurrentNursery;
602 #define CALLER_RESTORE_CurrentNursery CurrentNursery = SAVE_CurrentNursery;
604 #define CALLER_SAVE_CurrentNursery /* nothing */
605 #define CALLER_RESTORE_CurrentNursery /* nothing */
608 #ifdef CALLER_SAVES_HpAlloc
609 #define CALLER_SAVE_HpAlloc SAVE_HpAlloc = HpAlloc;
610 #define CALLER_RESTORE_HpAlloc HpAlloc = SAVE_HpAlloc;
612 #define CALLER_SAVE_HpAlloc /* nothing */
613 #define CALLER_RESTORE_HpAlloc /* nothing */
616 #endif /* IN_STG_CODE */
618 /* ----------------------------------------------------------------------------
619 Handy bunches of saves/restores
620 ------------------------------------------------------------------------ */
624 #define CALLER_SAVE_USER \
641 /* Save Base last, since the others may
642 be addressed relative to it */
643 #define CALLER_SAVE_SYSTEM \
648 CALLER_SAVE_CurrentTSO \
649 CALLER_SAVE_CurrentNursery \
652 #define CALLER_RESTORE_USER \
669 /* Restore Base first, since the others may
670 be addressed relative to it */
671 #define CALLER_RESTORE_SYSTEM \
672 CALLER_RESTORE_Base \
674 CALLER_RESTORE_SpLim \
676 CALLER_RESTORE_HpLim \
677 CALLER_RESTORE_CurrentTSO \
678 CALLER_RESTORE_CurrentNursery
680 #else /* not IN_STG_CODE */
682 #define CALLER_SAVE_USER /* nothing */
683 #define CALLER_SAVE_SYSTEM /* nothing */
684 #define CALLER_RESTORE_USER /* nothing */
685 #define CALLER_RESTORE_SYSTEM /* nothing */
687 #endif /* IN_STG_CODE */
688 #define CALLER_SAVE_ALL \
692 #define CALLER_RESTORE_ALL \
693 CALLER_RESTORE_SYSTEM \