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