[project @ 2005-03-27 13:41:13 by panne]
[ghc-hetmet.git] / ghc / includes / MachRegs.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-1999
4  *
5  * Registers used in STG code.  Might or might not correspond to
6  * actual machine registers.
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #ifndef MACHREGS_H
11 #define MACHREGS_H
12
13 /* This file is #included into Haskell code in the compiler: #defines
14  * only in here please.
15  */
16
17 /* 
18  * Defining NO_REGS causes no global registers to be used.  NO_REGS is
19  * typically defined by GHC, via a command-line option passed to gcc,
20  * when the -funregisterised flag is given.
21  *
22  * NB. When NO_REGS is on, calling & return conventions may be
23  * different.  For example, all function arguments will be passed on
24  * the stack, and components of an unboxed tuple will be returned on
25  * the stack rather than in registers.
26  */
27 #ifndef NO_REGS
28
29 /* NOTE: when testing the platform in this file we must test either
30  * *_HOST_ARCH and *_TARGET_ARCH, depending on whether COMPILING_GHC
31  * is set.  This is because when we're compiling the RTS and HC code,
32  * the platform we're running on is the HOST, but when compiling GHC
33  * we want to know about the register mapping on the TARGET platform.
34  */
35 #ifdef COMPILING_GHC
36 #define alpha_REGS    alpha_TARGET_ARCH
37 #define hppa1_1_REGS  hppa1_1_TARGET_ARCH
38 #define i386_REGS     i386_TARGET_ARCH
39 #define x86_64_REGS   x86_64_TARGET_ARCH
40 #define m68k_REGS     m68k_TARGET_ARCH
41 #define mips_REGS     (mipsel_TARGET_ARCH || mipseb_TARGET_ARCH)
42 #define powerpc_REGS  (powerpc_TARGET_ARCH || powerpc64_TARGET_ARCH || rs6000_TARGET_ARCH)
43 #define ia64_REGS     ia64_TARGET_ARCH
44 #define sparc_REGS    sparc_TARGET_ARCH
45 #define darwin_REGS   darwin_TARGET_OS
46 #else
47 #define alpha_REGS    alpha_HOST_ARCH
48 #define hppa1_1_REGS  hppa1_1_HOST_ARCH
49 #define i386_REGS     i386_HOST_ARCH
50 #define x86_64_REGS   x86_64_HOST_ARCH
51 #define m68k_REGS     m68k_HOST_ARCH
52 #define mips_REGS     (mipsel_HOST_ARCH || mipseb_HOST_ARCH)
53 #define powerpc_REGS  (powerpc_HOST_ARCH || powerpc64_HOST_ARCH || rs6000_HOST_ARCH)
54 #define ia64_REGS     ia64_HOST_ARCH
55 #define sparc_REGS    sparc_HOST_ARCH
56 #define darwin_REGS   darwin_HOST_OS
57 #endif
58
59 /* ----------------------------------------------------------------------------
60    Caller saves and callee-saves regs.
61    
62    Caller-saves regs have to be saved around C-calls made from STG
63    land, so this file defines CALLER_SAVES_<reg> for each <reg> that
64    is designated caller-saves in that machine's C calling convention.
65
66    Additionally, the following macros should be defined when
67
68      CALLER_SAVES_USER          one or more of R<n>, F, D
69                                 are caller-saves.
70
71      CALLER_SAVES_SYSTEM        one or more of Sp, SpLim, Hp, HpLim
72                                 are caller-saves.
73
74    This is so that the callWrapper mechanism knows which kind of
75    wrapper to generate for certain types of C call.
76    -------------------------------------------------------------------------- */
77
78 /* -----------------------------------------------------------------------------
79    The DEC Alpha register mapping
80
81    Alpha registers
82    \tr{$9}--\tr{$14} are our ``prize'' callee-save registers.  
83    \tr{$15} is the frame pointer, and \tr{$16}--\tr{$21} are argument
84    registers.  (These are off-limits.)  We can steal some of the \tr{$22}-and-up 
85    caller-save registers provided we do the appropriate save/restore stuff.
86    
87    \tr{$f2}--\tr{$f9} are some callee-save floating-point registers.
88    
89    We cannot use \tr{$23} (aka t9), \tr{$24} (aka t10), \tr{$25} (aka
90    t11), \tr{$27} (aka pv), or \tr{$28} (aka at), because they are
91    occasionally required by the assembler to handle non-primitive
92    instructions (e.g. ldb, remq).  Sigh!
93    
94    Cheat sheet for GDB:
95    
96    GDB  here    Main map
97    ===  ====    ========
98    s5   $14     R1
99    t1   $2      R2
100    t2   $3      R3
101    t3   $4      R4
102    t4   $5      R5
103    t5   $6      R6
104    t6   $7      R7
105    t7   $8      R8
106    s0   $9      Sp
107    s2   $11     SpLim
108    s3   $12     Hp   
109    s4   $13     HpLim
110    t8   $22     NCG_reserved
111    t12  $27     NCG_reserved
112    -------------------------------------------------------------------------- */
113
114 #if alpha_REGS
115 # define REG(x) __asm__("$" #x)
116
117 #  define CALLER_SAVES_R2
118 #  define CALLER_SAVES_R3
119 #  define CALLER_SAVES_R4
120 #  define CALLER_SAVES_R5
121 #  define CALLER_SAVES_R6
122 #  define CALLER_SAVES_R7
123 #  define CALLER_SAVES_R8
124   
125 #  define CALLER_SAVES_USER
126   
127 #  define REG_R1        14
128 #  define REG_R2        2
129 #  define REG_R3        3
130 #  define REG_R4        4
131 #  define REG_R5        5
132 #  define REG_R6        6
133 #  define REG_R7        7
134 #  define REG_R8        8
135   
136 #  define REG_F1        f2
137 #  define REG_F2        f3
138 #  define REG_F3        f4
139 #  define REG_F4        f5
140   
141 #  define REG_D1        f6
142 #  define REG_D2        f7
143   
144 #  define REG_Sp        9
145 #  define REG_SpLim     11
146
147 #  define REG_Hp        12
148 #  define REG_HpLim     13
149   
150 #  define NCG_Reserved_I1 22
151 #  define NCG_Reserved_I2 27
152 #  define NCG_Reserved_F1 f29
153 #  define NCG_Reserved_F2 f30
154
155 #endif /* alpha_REGS */
156
157 /* -----------------------------------------------------------------------------
158    The HP-PA register mapping
159
160    We cater for HP-PA 1.1.
161    
162    \tr{%r0}--\tr{%r1} are special.
163    \tr{%r2} is the return pointer.
164    \tr{%r3} is the frame pointer.
165    \tr{%r4}--\tr{%r18} are callee-save registers.
166    \tr{%r19} is a linkage table register for HPUX 8.0 shared libraries.
167    \tr{%r20}--\tr{%r22} are caller-save registers.
168    \tr{%r23}--\tr{%r26} are parameter registers.
169    \tr{%r27} is a global data pointer.
170    \tr{%r28}--\tr{%r29} are temporaries.
171    \tr{%r30} is the stack pointer.
172    \tr{%r31} is a temporary.
173    
174    \tr{%fr12}--\tr{%fr15} are some callee-save floating-point registers.
175    \tr{%fr8}--\tr{%fr11} are some available caller-save fl-pt registers.
176    -------------------------------------------------------------------------- */
177
178 #if hppa1_1_REGS
179
180 #define REG(x) __asm__("%" #x)
181
182 #define REG_R1          r11
183 #define REG_R2          r12
184 #define REG_R3          r13
185 #define REG_R4          r14
186 #define REG_R5          r15
187 #define REG_R6          r16
188 #define REG_R7          r17
189 #define REG_R8          r18
190
191 #define REG_F1          fr12
192 #define REG_F2          fr12R
193 #define REG_F3          fr13
194 #define REG_F4          fr13R
195
196 #define REG_D1          fr20    /* L & R */
197 #define REG_D2          fr21    /* L & R */
198
199 #define REG_Sp          r4
200 #define REG_SpLim       r6
201
202 #define REG_Hp          r7
203 #define REG_HpLim       r8
204
205 #define NCG_Reserved_I1 r28
206 #define NCG_Reserved_I2 r29
207 #define NCG_Reserved_F1 fr8
208 #define NCG_Reserved_F2 fr8R
209 #define NCG_Reserved_D1 fr10
210 #define NCG_Reserved_D2 fr11
211
212 #endif /* hppa */
213
214 /* -----------------------------------------------------------------------------
215    The x86 register mapping
216
217    Ok, we've only got 6 general purpose registers, a frame pointer and a
218    stack pointer.  \tr{%eax} and \tr{%edx} are return values from C functions,
219    hence they get trashed across ccalls and are caller saves. \tr{%ebx},
220    \tr{%esi}, \tr{%edi}, \tr{%ebp} are all callee-saves.
221
222    Reg     STG-Reg
223    ---------------
224    ebx     Base
225    ebp     Sp
226    esi     R1
227    edi     Hp
228
229    Leaving SpLim, and HpLim out of the picture.
230    -------------------------------------------------------------------------- */
231
232
233 #if i386_REGS
234
235 #define REG(x) __asm__("%" #x)
236
237 #ifndef not_doing_dynamic_linking
238 #define REG_Base    ebx
239 #endif
240 #define REG_Sp      ebp
241
242 #ifndef STOLEN_X86_REGS
243 #define STOLEN_X86_REGS 4
244 #endif
245
246 #if STOLEN_X86_REGS >= 3
247 # define REG_R1     esi
248 #endif
249
250 #if STOLEN_X86_REGS >= 4
251 # define REG_Hp     edi
252 #endif
253
254 #define MAX_REAL_VANILLA_REG 1  /* always, since it defines the entry conv */
255 #define MAX_REAL_FLOAT_REG   0
256 #define MAX_REAL_DOUBLE_REG  0
257 #define MAX_REAL_LONG_REG    0
258
259 #endif /* iX86 */
260
261 /* -----------------------------------------------------------------------------
262   The x86-64 register mapping
263
264                 callee-saves
265   %rax
266   %rbx          YES
267   %rcx
268   %rdx          (seem to be used as arg regs on x86-64)
269   %rsi          (seem to be used as arg regs on x86-64)
270   %rdi          (seem to be used as arg regs on x86-64)
271   %rbp          YES
272   %rsp          (unavailable - stack pointer)
273   %r8
274   %r9
275   %r10
276   %r11          
277   %r12          YES
278   %r13          YES
279   %r14          YES
280   %r15          YES
281   --------------------------------------------------------------------------- */
282
283 #if x86_64_REGS
284
285 #define REG(x) __asm__("%" #x)
286
287 #define REG_Base  rbx
288 #define REG_Sp    rbp
289 #define REG_Hp    r12
290 #define REG_R1    r13
291 #define REG_SpLim r14
292 #define REG_HpLim r15
293 /* ToDo: try R2/R3 instead of SpLim/HpLim? */
294
295 #define MAX_REAL_VANILLA_REG 1
296 #define MAX_REAL_FLOAT_REG   0
297 #define MAX_REAL_DOUBLE_REG  0
298 #define MAX_REAL_LONG_REG    0
299
300 #endif /* x86_64 */
301
302 /* -----------------------------------------------------------------------------
303    The Motorola 680x0 register mapping
304
305    A Sun3 (mc680x0) has eight address registers, \tr{a0} to \tr{a7}, and
306    eight data registers, \tr{d0} to \tr{d7}.  Address operations have to
307    be done through address registers; data registers are used for
308    comparison values and data.
309    
310    Here's the register-usage picture for m68k boxes with GCC.
311
312    \begin{tabular}{ll}
313    a0 & used directly by GCC \\
314    a1 & used directly by GCC \\
315    \\
316    a2..a5 & callee-saved: available for STG registers \\
317    & (a5 may be special, ``global'' register for PIC?) \\
318    \\
319    a6 & C-stack frame pointer \\
320    a7 & C-stack pointer \\
321    \\
322    d0 & used directly by GCC \\
323    d1 & used directly by GCC \\
324    d2 & really needed for local optimisation by GCC \\
325    \\
326    d3..d7 & callee-saved: available for STG registers
327    \\
328    fp0 & call-clobbered \\
329    fp1 & call-clobbered \\
330    fp2..fp7 & callee-saved: available for STG registers
331    \end{tabular}
332    -------------------------------------------------------------------------- */
333
334 #if m68k_REGS
335
336 #define REG(x) __asm__(#x)
337
338 #define REG_Base        a2
339
340 #define REG_Sp          a3
341 #define REG_SpLim       d3
342
343 #define REG_Hp          d4
344 #define REG_HpLim       d5
345
346 #define REG_R1          a5
347 #define REG_R2          d6
348 #define MAX_REAL_VANILLA_REG 2
349
350 #define REG_Ret         d7
351
352 #define REG_F1          fp2
353 #define REG_F2          fp3
354 #define REG_F3          fp4
355 #define REG_F4          fp5
356
357 #define REG_D1          fp6
358 #define REG_D2          fp7
359
360 #endif /* m68k */
361
362 /* -----------------------------------------------------------------------------
363    The DECstation (MIPS) register mapping
364
365    Here's at least some simple stuff about registers on a MIPS.
366    
367    \tr{s0}--\tr{s7} are callee-save integer registers; they are our
368    ``prize'' stolen registers.  There is also a wad of callee-save
369    floating-point registers, \tr{$f20}--\tr{$f31}; we'll use some of
370    those.
371    
372    \tr{t0}--\tr{t9} are caller-save (``temporary?'') integer registers.
373    We can steal some, but we might have to save/restore around ccalls.
374    -------------------------------------------------------------------------- */
375
376 #if mips_REGS
377
378 #define REG(x) __asm__("$" #x)
379
380 #define CALLER_SAVES_R1
381 #define CALLER_SAVES_R2
382 #define CALLER_SAVES_R3
383 #define CALLER_SAVES_R4
384 #define CALLER_SAVES_R5
385 #define CALLER_SAVES_R6
386 #define CALLER_SAVES_R7
387 #define CALLER_SAVES_R8
388
389 #define CALLER_SAVES_USER
390
391 #define REG_R1          9
392 #define REG_R2          10
393 #define REG_R3          11
394 #define REG_R4          12
395 #define REG_R5          13
396 #define REG_R6          14
397 #define REG_R7          15
398 #define REG_R8          24
399
400 #define REG_F1          f20
401 #define REG_F2          f22
402 #define REG_F3          f24
403 #define REG_F4          f26
404
405 #define REG_D1          f28
406 #define REG_D2          f30
407
408 #define REG_Sp          16
409 #define REG_SpLim       18
410
411 #define REG_Hp          19
412 #define REG_HpLim       20
413
414 #endif /* mipse[lb] */
415
416 /* -----------------------------------------------------------------------------
417    The PowerPC register mapping
418
419    0            system glue?    (caller-save, volatile)
420    1            SP              (callee-save, non-volatile)
421    2            AIX, powerpc64-linux:
422                     RTOC        (a strange special case)
423                 darwin:         
424                                 (caller-save, volatile)
425                 powerpc32-linux:
426                                 reserved for use by system
427                     
428    3-10         args/return     (caller-save, volatile)
429    11,12        system glue?    (caller-save, volatile)
430    13           on 64-bit:      reserved for thread state pointer
431                 on 32-bit:      (callee-save, non-volatile)
432    14-31                        (callee-save, non-volatile)
433    
434    f0                           (caller-save, volatile)
435    f1-f13       args/return     (caller-save, volatile)
436    f14-f31                      (callee-save, non-volatile)
437    
438    \tr{14}--\tr{31} are wonderful callee-save registers on all ppc OSes.
439    \tr{0}--\tr{12} are caller-save registers.
440    
441    \tr{%f14}--\tr{%f31} are callee-save floating-point registers.
442    
443    We can do the Whole Business with callee-save registers only!
444    -------------------------------------------------------------------------- */
445
446 #if powerpc_REGS
447
448 #define REG(x) __asm__(#x)
449
450 #define REG_R1          r14
451 #define REG_R2          r15
452 #define REG_R3          r16
453 #define REG_R4          r17
454 #define REG_R5          r18
455 #define REG_R6          r19
456 #define REG_R7          r20
457 #define REG_R8          r21
458
459 #if darwin_REGS
460
461 #define REG_F1          f14
462 #define REG_F2          f15
463 #define REG_F3          f16
464 #define REG_F4          f17
465
466 #define REG_D1          f18
467 #define REG_D2          f19
468
469 #else
470
471 #define REG_F1          fr14
472 #define REG_F2          fr15
473 #define REG_F3          fr16
474 #define REG_F4          fr17
475
476 #define REG_D1          fr18
477 #define REG_D2          fr19
478
479 #endif
480
481 #define REG_Sp          r22
482 #define REG_SpLim       r24
483
484 #define REG_Hp          r25
485 #define REG_HpLim       r26
486
487 #define REG_Base        r27
488
489 #endif /* powerpc */
490
491 /* -----------------------------------------------------------------------------
492    The IA64 register mapping
493
494    We place the general registers in the locals area of the register stack,
495    so that the call mechanism takes care of saving them for us.  We reserve
496    the first 16 for gcc's use - since gcc uses the highest used register to
497    determine the register stack frame size, this gives us a constant size
498    register stack frame.
499    
500    \tr{f16-f32} are the callee-saved floating point registers.
501    -------------------------------------------------------------------------- */
502
503 #if ia64_REGS
504
505 #define REG(x) __asm__(#x)
506
507 #define REG_R1          loc16
508 #define REG_R2          loc17
509 #define REG_R3          loc18
510 #define REG_R4          loc19
511 #define REG_R5          loc20
512 #define REG_R6          loc21
513 #define REG_R7          loc22
514 #define REG_R8          loc23
515
516 #define REG_F1          f16
517 #define REG_F2          f17
518 #define REG_F3          f18
519 #define REG_F4          f19
520
521 #define REG_D1          f20
522 #define REG_D2          f21
523
524 #define REG_Sp          loc24
525 #define REG_SpLim       loc26
526
527 #define REG_Hp          loc27
528 #define REG_HpLim       loc28
529
530 #endif /* ia64 */
531
532 /* -----------------------------------------------------------------------------
533    The Sun SPARC register mapping
534
535    The SPARC register (window) story: Remember, within the Haskell
536    Threaded World, we essentially ``shut down'' the register-window
537    mechanism---the window doesn't move at all while in this World.  It
538    *does* move, of course, if we call out to arbitrary~C...
539    
540    The %i, %l, and %o registers (8 each) are the input, local, and
541    output registers visible in one register window.  The 8 %g (global)
542    registers are visible all the time.
543    
544    %o0..%o7             not available; can be zapped by callee
545                           (%o6 is C-stack ptr; %o7 hold ret addrs)
546    %i0..%i7             available (except %i6 is used as frame ptr)
547                           (and %i7 tends to have ret-addr-ish things)
548    %l0..%l7             available
549    %g0..%g4             not available; prone to stomping by division, etc.
550    %g5..%g7             not available; reserved for the OS
551
552    Note: %g3 is *definitely* clobbered in the builtin divide code (and
553    our save/restore machinery is NOT GOOD ENOUGH for that); discretion
554    being the better part of valor, we also don't take %g4.
555
556    The paired nature of the floating point registers causes complications for
557    the native code generator.  For convenience, we pretend that the first 22
558    fp regs %f0 .. %f21 are actually 11 double regs, and the remaining 10 are
559    float (single) regs.  The NCG acts accordingly.  That means that the 
560    following FP assignment is rather fragile, and should only be changed
561    with extreme care.  The current scheme is:
562
563       %f0 /%f1    FP return from C
564       %f2 /%f3    D1
565       %f4 /%f5    D2
566       %f6 /%f7    ncg double spill tmp #1
567       %f8 /%f9    ncg double spill tmp #2
568       %f10/%f11   allocatable
569       %f12/%f13   allocatable
570       %f14/%f15   allocatable
571       %f16/%f17   allocatable
572       %f18/%f19   allocatable
573       %f20/%f21   allocatable
574
575       %f22        F1
576       %f23        F2
577       %f24        F3
578       %f25        F4
579       %f26        ncg single spill tmp #1
580       %f27        ncg single spill tmp #2
581       %f28        allocatable
582       %f29        allocatable
583       %f30        allocatable
584       %f31        allocatable
585
586    -------------------------------------------------------------------------- */
587
588 #if sparc_REGS
589
590 #define REG(x) __asm__("%" #x)
591
592 #define CALLER_SAVES_USER
593
594 #define CALLER_SAVES_F1
595 #define CALLER_SAVES_F2
596 #define CALLER_SAVES_F3
597 #define CALLER_SAVES_F4
598 #define CALLER_SAVES_D1
599 #define CALLER_SAVES_D2
600
601 #define REG_R1          l1
602 #define REG_R2          l2
603 #define REG_R3          l3
604 #define REG_R4          l4
605 #define REG_R5          l5
606 #define REG_R6          l6
607 #define REG_R7          l7
608 #define REG_R8          i5
609
610 #define REG_F1          f22
611 #define REG_F2          f23
612 #define REG_F3          f24
613 #define REG_F4          f25
614 #define REG_D1          f2
615 #define REG_D2          f4
616
617 #define REG_Sp          i0
618 #define REG_SpLim       i2
619
620 #define REG_Hp          i3
621 #define REG_HpLim       i4
622
623 #define NCG_SpillTmp_I1 g1
624 #define NCG_SpillTmp_I2 g2
625 #define NCG_SpillTmp_F1 f26
626 #define NCG_SpillTmp_F2 f27
627 #define NCG_SpillTmp_D1 f6
628 #define NCG_SpillTmp_D2 f8
629
630 #define NCG_FirstFloatReg f22
631
632 #endif /* sparc */
633
634 #endif /* NO_REGS */
635
636 /* -----------------------------------------------------------------------------
637  * These constants define how many stg registers will be used for
638  * passing arguments (and results, in the case of an unboxed-tuple
639  * return).
640  *
641  * We usually set MAX_REAL_VANILLA_REG and co. to be the number of the
642  * highest STG register to occupy a real machine register, otherwise
643  * the calling conventions will needlessly shuffle data between the
644  * stack and memory-resident STG registers.  We might occasionally
645  * set these macros to other values for testing, though.
646  *
647  * Registers above these values might still be used, for instance to
648  * communicate with PrimOps and RTS functions.
649  */
650
651 #ifndef MAX_REAL_VANILLA_REG
652 #  if   defined(REG_R8)
653 #  define MAX_REAL_VANILLA_REG 8
654 #  elif defined(REG_R7)
655 #  define MAX_REAL_VANILLA_REG 7
656 #  elif defined(REG_R6)
657 #  define MAX_REAL_VANILLA_REG 6
658 #  elif defined(REG_R5)
659 #  define MAX_REAL_VANILLA_REG 5
660 #  elif defined(REG_R4)
661 #  define MAX_REAL_VANILLA_REG 4
662 #  elif defined(REG_R3)
663 #  define MAX_REAL_VANILLA_REG 3
664 #  elif defined(REG_R2)
665 #  define MAX_REAL_VANILLA_REG 2
666 #  elif defined(REG_R1)
667 #  define MAX_REAL_VANILLA_REG 1
668 #  else
669 #  define MAX_REAL_VANILLA_REG 0
670 #  endif
671 #endif
672
673 #ifndef MAX_REAL_FLOAT_REG
674 #  if   defined(REG_F4)
675 #  define MAX_REAL_FLOAT_REG 4
676 #  elif defined(REG_F3)
677 #  define MAX_REAL_FLOAT_REG 3
678 #  elif defined(REG_F2)
679 #  define MAX_REAL_FLOAT_REG 2
680 #  elif defined(REG_F1)
681 #  define MAX_REAL_FLOAT_REG 1
682 #  else
683 #  define MAX_REAL_FLOAT_REG 0
684 #  endif
685 #endif
686
687 #ifndef MAX_REAL_DOUBLE_REG
688 #  if   defined(REG_D2)
689 #  define MAX_REAL_DOUBLE_REG 2
690 #  elif defined(REG_D1)
691 #  define MAX_REAL_DOUBLE_REG 1
692 #  else
693 #  define MAX_REAL_DOUBLE_REG 0
694 #  endif
695 #endif
696
697 #ifndef MAX_REAL_LONG_REG
698 #  if   defined(REG_L1)
699 #  define MAX_REAL_LONG_REG 1
700 #  else
701 #  define MAX_REAL_LONG_REG 0
702 #  endif
703 #endif
704
705 /* define NO_ARG_REGS if we have no argument registers at all (we can
706  * optimise certain code paths using this predicate).
707  */
708 #if MAX_REAL_VANILLA_REG < 2
709 #define NO_ARG_REGS 
710 #else
711 #undef NO_ARG_REGS
712 #endif
713
714 #endif /* MACHREGS_H */