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