1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2009
5 * Registers in the STG machine.
7 * Do not #include this file directly: #include "Rts.h" instead.
9 * To understand the structure of the RTS headers, see the wiki:
10 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
12 * ---------------------------------------------------------------------------*/
18 * The STG machine has a collection of "registers", each one of which
19 * may or may not correspond to an actual machine register when
22 * The register set is backed by a table in memory (struct
23 * StgRegTable). If a particular STG register is not mapped to a
24 * machine register, then the apprpriate slot in this table is used
27 * This table is itself pointed to by another register, BaseReg. If
28 * BaseReg is not in a machine register, then the register table is
29 * used from an absolute location (MainCapability).
34 StgWord stgEagerBlackholeInfo;
35 StgFunPtr stgGCEnter1;
40 * Vanilla registers are given this union type, which is purely so
41 * that we can cast the vanilla reg to a variety of types with the
42 * minimum of syntax. eg. R1.w instead of (StgWord)R1.
54 * This is the table that holds shadow-locations for all the STG
55 * registers. The shadow locations are used when:
57 * 1) the particular register isn't mapped to a real machine
58 * register, probably because there's a shortage of real registers.
59 * 2) caller-saves registers are saved across a CCall
61 typedef struct StgRegTable_ {
70 StgUnion rR9; /* used occasionally by heap/stack checks */
71 StgUnion rR10; /* used occasionally by heap/stack checks */
83 struct StgTSO_ *rCurrentTSO;
84 struct step_ *rNursery;
85 struct bdescr_ *rCurrentNursery; /* Hp/HpLim point into this block */
86 struct bdescr_ *rCurrentAlloc; /* for allocation using allocate() */
87 StgWord rHpAlloc; /* number of *bytes* being allocated in heap */
88 StgWord rRet; // holds the return code of the thread
94 * Registers Hp and HpLim are global across the entire system, and are
95 * copied into the RegTable before executing a thread.
97 * Registers Sp and SpLim are saved in the TSO for the
98 * thread, but are copied into the RegTable before executing a thread.
100 * All other registers are "general purpose", and are used for passing
101 * arguments to functions, and returning values. The code generator
102 * knows how many of these are in real registers, and avoids
103 * generating code that uses non-real registers. General purpose
104 * registers are never saved when returning to the scheduler, instead
105 * we save whatever is live at the time on the stack, and restore it
106 * later. This should reduce the context switch time, amongst other
109 * For argument passing, the stack will be used in preference to
110 * pseudo-registers if the architecture has too few general purpose
113 * Some special RTS functions like newArray and the Integer primitives
114 * expect their arguments to be in registers R1-Rn, so we use these
115 * (pseudo-)registers in those cases.
119 * Locations for saving per-thread registers.
122 #define SAVE_Sp (CurrentTSO->sp)
123 #define SAVE_SpLim (CurrentTSO->splim)
125 #define SAVE_Hp (BaseReg->rHp)
127 #define SAVE_CurrentTSO (BaseReg->rCurrentTSO)
128 #define SAVE_CurrentNursery (BaseReg->rCurrentNursery)
129 #define SAVE_HpAlloc (BaseReg->rHpAlloc)
131 /* We sometimes need to save registers across a C-call, eg. if they
132 * are clobbered in the standard calling convention. We define the
133 * save locations for all registers in the register table.
136 #define SAVE_R1 (BaseReg->rR1)
137 #define SAVE_R2 (BaseReg->rR2)
138 #define SAVE_R3 (BaseReg->rR3)
139 #define SAVE_R4 (BaseReg->rR4)
140 #define SAVE_R5 (BaseReg->rR5)
141 #define SAVE_R6 (BaseReg->rR6)
142 #define SAVE_R7 (BaseReg->rR7)
143 #define SAVE_R8 (BaseReg->rR8)
145 #define SAVE_F1 (BaseReg->rF1)
146 #define SAVE_F2 (BaseReg->rF2)
147 #define SAVE_F3 (BaseReg->rF3)
148 #define SAVE_F4 (BaseReg->rF4)
150 #define SAVE_D1 (BaseReg->rD1)
151 #define SAVE_D2 (BaseReg->rD2)
153 #define SAVE_L1 (BaseReg->rL1)
155 /* -----------------------------------------------------------------------------
156 * Emit the GCC-specific register declarations for each machine
157 * register being used. If any STG register isn't mapped to a machine
158 * register, then map it to an offset from BaseReg.
160 * First, the general purpose registers. The idea is, if a particular
161 * general-purpose STG register can't be mapped to a real machine
162 * register, it won't be used at all. Instead, we'll use the stack.
164 * This is an improvement on the way things used to be done, when all
165 * registers were mapped to locations in the register table, and stuff
166 * was being shifted from the stack to the register table and back
167 * again for no good reason (on register-poor architectures).
170 /* define NO_REGS to omit register declarations - used in RTS C code
171 * that needs all the STG definitions but not the global register
174 #define GLOBAL_REG_DECL(type,name,reg) register type name REG(reg);
176 #if defined(REG_R1) && !defined(NO_GLOBAL_REG_DECLS)
177 GLOBAL_REG_DECL(StgUnion,R1,REG_R1)
179 # define R1 (BaseReg->rR1)
182 #if defined(REG_R2) && !defined(NO_GLOBAL_REG_DECLS)
183 GLOBAL_REG_DECL(StgUnion,R2,REG_R2)
185 # define R2 (BaseReg->rR2)
188 #if defined(REG_R3) && !defined(NO_GLOBAL_REG_DECLS)
189 GLOBAL_REG_DECL(StgUnion,R3,REG_R3)
191 # define R3 (BaseReg->rR3)
194 #if defined(REG_R4) && !defined(NO_GLOBAL_REG_DECLS)
195 GLOBAL_REG_DECL(StgUnion,R4,REG_R4)
197 # define R4 (BaseReg->rR4)
200 #if defined(REG_R5) && !defined(NO_GLOBAL_REG_DECLS)
201 GLOBAL_REG_DECL(StgUnion,R5,REG_R5)
203 # define R5 (BaseReg->rR5)
206 #if defined(REG_R6) && !defined(NO_GLOBAL_REG_DECLS)
207 GLOBAL_REG_DECL(StgUnion,R6,REG_R6)
209 # define R6 (BaseReg->rR6)
212 #if defined(REG_R7) && !defined(NO_GLOBAL_REG_DECLS)
213 GLOBAL_REG_DECL(StgUnion,R7,REG_R7)
215 # define R7 (BaseReg->rR7)
218 #if defined(REG_R8) && !defined(NO_GLOBAL_REG_DECLS)
219 GLOBAL_REG_DECL(StgUnion,R8,REG_R8)
221 # define R8 (BaseReg->rR8)
224 #if defined(REG_R9) && !defined(NO_GLOBAL_REG_DECLS)
225 GLOBAL_REG_DECL(StgUnion,R9,REG_R9)
227 # define R9 (BaseReg->rR9)
230 #if defined(REG_R10) && !defined(NO_GLOBAL_REG_DECLS)
231 GLOBAL_REG_DECL(StgUnion,R10,REG_R10)
233 # define R10 (BaseReg->rR10)
236 #if defined(REG_F1) && !defined(NO_GLOBAL_REG_DECLS)
237 GLOBAL_REG_DECL(StgFloat,F1,REG_F1)
239 #define F1 (BaseReg->rF1)
242 #if defined(REG_F2) && !defined(NO_GLOBAL_REG_DECLS)
243 GLOBAL_REG_DECL(StgFloat,F2,REG_F2)
245 #define F2 (BaseReg->rF2)
248 #if defined(REG_F3) && !defined(NO_GLOBAL_REG_DECLS)
249 GLOBAL_REG_DECL(StgFloat,F3,REG_F3)
251 #define F3 (BaseReg->rF3)
254 #if defined(REG_F4) && !defined(NO_GLOBAL_REG_DECLS)
255 GLOBAL_REG_DECL(StgFloat,F4,REG_F4)
257 #define F4 (BaseReg->rF4)
260 #if defined(REG_D1) && !defined(NO_GLOBAL_REG_DECLS)
261 GLOBAL_REG_DECL(StgDouble,D1,REG_D1)
263 #define D1 (BaseReg->rD1)
266 #if defined(REG_D2) && !defined(NO_GLOBAL_REG_DECLS)
267 GLOBAL_REG_DECL(StgDouble,D2,REG_D2)
269 #define D2 (BaseReg->rD2)
272 #if defined(REG_L1) && !defined(NO_GLOBAL_REG_DECLS)
273 GLOBAL_REG_DECL(StgWord64,L1,REG_L1)
275 #define L1 (BaseReg->rL1)
279 * If BaseReg isn't mapped to a machine register, just use the global
280 * address of the current register table (CurrentRegTable in
281 * concurrent Haskell, MainRegTable otherwise).
284 /* A capability is a combination of a FunTable and a RegTable. In STG
285 * code, BaseReg normally points to the RegTable portion of this
286 * structure, so that we can index both forwards and backwards to take
287 * advantage of shorter instruction forms on some archs (eg. x86).
288 * This is a cut-down version of the Capability structure; the full
289 * version is defined in Capability.h.
291 struct PartCapability_ {
296 /* No such thing as a MainCapability under THREADED_RTS - each thread must have
297 * its own Capability.
299 #if IN_STG_CODE && !(defined(THREADED_RTS) && !defined(NOSMP))
300 extern W_ MainCapability[];
304 * Assigning to BaseReg (the ASSIGN_BaseReg macro): this happens on
305 * return from a "safe" foreign call, when the thread might be running
306 * on a new Capability. Obviously if BaseReg is not a register, then
307 * we are restricted to a single Capability (this invariant is enforced
308 * in Capability.c:initCapabilities), and assigning to BaseReg can be omitted.
311 #if defined(REG_Base) && !defined(NO_GLOBAL_REG_DECLS)
312 GLOBAL_REG_DECL(StgRegTable *,BaseReg,REG_Base)
313 #define ASSIGN_BaseReg(e) (BaseReg = (e))
315 #if defined(THREADED_RTS) && !defined(NOSMP)
316 #error BaseReg must be in a register for THREADED_RTS
318 #define BaseReg (&((struct PartCapability_ *)MainCapability)->r)
319 #define ASSIGN_BaseReg(e) (e)
322 #if defined(REG_Sp) && !defined(NO_GLOBAL_REG_DECLS)
323 GLOBAL_REG_DECL(P_,Sp,REG_Sp)
325 #define Sp (BaseReg->rSp)
328 #if defined(REG_SpLim) && !defined(NO_GLOBAL_REG_DECLS)
329 GLOBAL_REG_DECL(P_,SpLim,REG_SpLim)
331 #define SpLim (BaseReg->rSpLim)
334 #if defined(REG_Hp) && !defined(NO_GLOBAL_REG_DECLS)
335 GLOBAL_REG_DECL(P_,Hp,REG_Hp)
337 #define Hp (BaseReg->rHp)
340 #if defined(REG_HpLim) && !defined(NO_GLOBAL_REG_DECLS)
341 #error HpLim cannot be in a register
343 #define HpLim (BaseReg->rHpLim)
346 #if defined(REG_CurrentTSO) && !defined(NO_GLOBAL_REG_DECLS)
347 GLOBAL_REG_DECL(struct _StgTSO *,CurrentTSO,REG_CurrentTSO)
349 #define CurrentTSO (BaseReg->rCurrentTSO)
352 #if defined(REG_CurrentNursery) && !defined(NO_GLOBAL_REG_DECLS)
353 GLOBAL_REG_DECL(bdescr *,CurrentNursery,REG_CurrentNursery)
355 #define CurrentNursery (BaseReg->rCurrentNursery)
358 #if defined(REG_HpAlloc) && !defined(NO_GLOBAL_REG_DECLS)
359 GLOBAL_REG_DECL(bdescr *,HpAlloc,REG_HpAlloc)
361 #define HpAlloc (BaseReg->rHpAlloc)
364 /* -----------------------------------------------------------------------------
365 Get absolute function pointers from the register table, to save
374 as long as the offset is within the range of a signed byte
375 (-128..+127). So we pick some common absolute_addresses and put
376 them in the register table. As a bonus, linking time should also
379 Other possible candidates in order of importance:
382 stg_CAF_BLACKHOLE_info
385 anything else probably isn't worth the effort.
387 -------------------------------------------------------------------------- */
390 #define FunReg ((StgFunTable *)((void *)BaseReg - STG_FIELD_OFFSET(struct PartCapability_, r)))
392 #define stg_EAGER_BLACKHOLE_info (FunReg->stgEagerBlackholeInfo)
393 #define stg_gc_enter_1 (FunReg->stgGCEnter1)
394 #define stg_gc_fun (FunReg->stgGCFun)
396 /* -----------------------------------------------------------------------------
397 For any registers which are denoted "caller-saves" by the C calling
398 convention, we have to emit code to save and restore them across C
400 -------------------------------------------------------------------------- */
402 #ifdef CALLER_SAVES_R1
403 #define CALLER_SAVE_R1 SAVE_R1 = R1;
404 #define CALLER_RESTORE_R1 R1 = SAVE_R1;
406 #define CALLER_SAVE_R1 /* nothing */
407 #define CALLER_RESTORE_R1 /* nothing */
410 #ifdef CALLER_SAVES_R2
411 #define CALLER_SAVE_R2 SAVE_R2 = R2;
412 #define CALLER_RESTORE_R2 R2 = SAVE_R2;
414 #define CALLER_SAVE_R2 /* nothing */
415 #define CALLER_RESTORE_R2 /* nothing */
418 #ifdef CALLER_SAVES_R3
419 #define CALLER_SAVE_R3 SAVE_R3 = R3;
420 #define CALLER_RESTORE_R3 R3 = SAVE_R3;
422 #define CALLER_SAVE_R3 /* nothing */
423 #define CALLER_RESTORE_R3 /* nothing */
426 #ifdef CALLER_SAVES_R4
427 #define CALLER_SAVE_R4 SAVE_R4 = R4;
428 #define CALLER_RESTORE_R4 R4 = SAVE_R4;
430 #define CALLER_SAVE_R4 /* nothing */
431 #define CALLER_RESTORE_R4 /* nothing */
434 #ifdef CALLER_SAVES_R5
435 #define CALLER_SAVE_R5 SAVE_R5 = R5;
436 #define CALLER_RESTORE_R5 R5 = SAVE_R5;
438 #define CALLER_SAVE_R5 /* nothing */
439 #define CALLER_RESTORE_R5 /* nothing */
442 #ifdef CALLER_SAVES_R6
443 #define CALLER_SAVE_R6 SAVE_R6 = R6;
444 #define CALLER_RESTORE_R6 R6 = SAVE_R6;
446 #define CALLER_SAVE_R6 /* nothing */
447 #define CALLER_RESTORE_R6 /* nothing */
450 #ifdef CALLER_SAVES_R7
451 #define CALLER_SAVE_R7 SAVE_R7 = R7;
452 #define CALLER_RESTORE_R7 R7 = SAVE_R7;
454 #define CALLER_SAVE_R7 /* nothing */
455 #define CALLER_RESTORE_R7 /* nothing */
458 #ifdef CALLER_SAVES_R8
459 #define CALLER_SAVE_R8 SAVE_R8 = R8;
460 #define CALLER_RESTORE_R8 R8 = SAVE_R8;
462 #define CALLER_SAVE_R8 /* nothing */
463 #define CALLER_RESTORE_R8 /* nothing */
466 #ifdef CALLER_SAVES_R9
467 #define CALLER_SAVE_R9 SAVE_R9 = R9;
468 #define CALLER_RESTORE_R9 R9 = SAVE_R9;
470 #define CALLER_SAVE_R9 /* nothing */
471 #define CALLER_RESTORE_R9 /* nothing */
474 #ifdef CALLER_SAVES_R10
475 #define CALLER_SAVE_R10 SAVE_R10 = R10;
476 #define CALLER_RESTORE_R10 R10 = SAVE_R10;
478 #define CALLER_SAVE_R10 /* nothing */
479 #define CALLER_RESTORE_R10 /* nothing */
482 #ifdef CALLER_SAVES_F1
483 #define CALLER_SAVE_F1 SAVE_F1 = F1;
484 #define CALLER_RESTORE_F1 F1 = SAVE_F1;
486 #define CALLER_SAVE_F1 /* nothing */
487 #define CALLER_RESTORE_F1 /* nothing */
490 #ifdef CALLER_SAVES_F2
491 #define CALLER_SAVE_F2 SAVE_F2 = F2;
492 #define CALLER_RESTORE_F2 F2 = SAVE_F2;
494 #define CALLER_SAVE_F2 /* nothing */
495 #define CALLER_RESTORE_F2 /* nothing */
498 #ifdef CALLER_SAVES_F3
499 #define CALLER_SAVE_F3 SAVE_F3 = F3;
500 #define CALLER_RESTORE_F3 F3 = SAVE_F3;
502 #define CALLER_SAVE_F3 /* nothing */
503 #define CALLER_RESTORE_F3 /* nothing */
506 #ifdef CALLER_SAVES_F4
507 #define CALLER_SAVE_F4 SAVE_F4 = F4;
508 #define CALLER_RESTORE_F4 F4 = SAVE_F4;
510 #define CALLER_SAVE_F4 /* nothing */
511 #define CALLER_RESTORE_F4 /* nothing */
514 #ifdef CALLER_SAVES_D1
515 #define CALLER_SAVE_D1 SAVE_D1 = D1;
516 #define CALLER_RESTORE_D1 D1 = SAVE_D1;
518 #define CALLER_SAVE_D1 /* nothing */
519 #define CALLER_RESTORE_D1 /* nothing */
522 #ifdef CALLER_SAVES_D2
523 #define CALLER_SAVE_D2 SAVE_D2 = D2;
524 #define CALLER_RESTORE_D2 D2 = SAVE_D2;
526 #define CALLER_SAVE_D2 /* nothing */
527 #define CALLER_RESTORE_D2 /* nothing */
530 #ifdef CALLER_SAVES_L1
531 #define CALLER_SAVE_L1 SAVE_L1 = L1;
532 #define CALLER_RESTORE_L1 L1 = SAVE_L1;
534 #define CALLER_SAVE_L1 /* nothing */
535 #define CALLER_RESTORE_L1 /* nothing */
538 #ifdef CALLER_SAVES_Sp
539 #define CALLER_SAVE_Sp SAVE_Sp = Sp;
540 #define CALLER_RESTORE_Sp Sp = SAVE_Sp;
542 #define CALLER_SAVE_Sp /* nothing */
543 #define CALLER_RESTORE_Sp /* nothing */
546 #ifdef CALLER_SAVES_SpLim
547 #define CALLER_SAVE_SpLim SAVE_SpLim = SpLim;
548 #define CALLER_RESTORE_SpLim SpLim = SAVE_SpLim;
550 #define CALLER_SAVE_SpLim /* nothing */
551 #define CALLER_RESTORE_SpLim /* nothing */
554 #ifdef CALLER_SAVES_Hp
555 #define CALLER_SAVE_Hp SAVE_Hp = Hp;
556 #define CALLER_RESTORE_Hp Hp = SAVE_Hp;
558 #define CALLER_SAVE_Hp /* nothing */
559 #define CALLER_RESTORE_Hp /* nothing */
562 #ifdef CALLER_SAVES_Base
564 #error "Can't have caller-saved BaseReg with THREADED_RTS"
566 #define CALLER_SAVE_Base /* nothing */
567 #define CALLER_RESTORE_Base BaseReg = &MainRegTable;
569 #define CALLER_SAVE_Base /* nothing */
570 #define CALLER_RESTORE_Base /* nothing */
573 #ifdef CALLER_SAVES_CurrentTSO
574 #define CALLER_SAVE_CurrentTSO SAVE_CurrentTSO = CurrentTSO;
575 #define CALLER_RESTORE_CurrentTSO CurrentTSO = SAVE_CurrentTSO;
577 #define CALLER_SAVE_CurrentTSO /* nothing */
578 #define CALLER_RESTORE_CurrentTSO /* nothing */
581 #ifdef CALLER_SAVES_CurrentNursery
582 #define CALLER_SAVE_CurrentNursery SAVE_CurrentNursery = CurrentNursery;
583 #define CALLER_RESTORE_CurrentNursery CurrentNursery = SAVE_CurrentNursery;
585 #define CALLER_SAVE_CurrentNursery /* nothing */
586 #define CALLER_RESTORE_CurrentNursery /* nothing */
589 #ifdef CALLER_SAVES_HpAlloc
590 #define CALLER_SAVE_HpAlloc SAVE_HpAlloc = HpAlloc;
591 #define CALLER_RESTORE_HpAlloc HpAlloc = SAVE_HpAlloc;
593 #define CALLER_SAVE_HpAlloc /* nothing */
594 #define CALLER_RESTORE_HpAlloc /* nothing */
597 #endif /* IN_STG_CODE */
599 /* ----------------------------------------------------------------------------
600 Handy bunches of saves/restores
601 ------------------------------------------------------------------------ */
605 #define CALLER_SAVE_USER \
622 /* Save Base last, since the others may
623 be addressed relative to it */
624 #define CALLER_SAVE_SYSTEM \
628 CALLER_SAVE_CurrentTSO \
629 CALLER_SAVE_CurrentNursery \
632 #define CALLER_RESTORE_USER \
649 /* Restore Base first, since the others may
650 be addressed relative to it */
651 #define CALLER_RESTORE_SYSTEM \
652 CALLER_RESTORE_Base \
654 CALLER_RESTORE_SpLim \
656 CALLER_RESTORE_CurrentTSO \
657 CALLER_RESTORE_CurrentNursery
659 #else /* not IN_STG_CODE */
661 #define CALLER_SAVE_USER /* nothing */
662 #define CALLER_SAVE_SYSTEM /* nothing */
663 #define CALLER_RESTORE_USER /* nothing */
664 #define CALLER_RESTORE_SYSTEM /* nothing */
666 #endif /* IN_STG_CODE */
667 #define CALLER_SAVE_ALL \
671 #define CALLER_RESTORE_ALL \
672 CALLER_RESTORE_SYSTEM \