Revert previous ser-unix change. Locks up serial device.
[deliverable/binutils-gdb.git] / gdb / rs6000-tdep.c
CommitLineData
c906108c 1/* Target-dependent code for GDB, the GNU debugger.
c877c8e6 2 Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
c906108c
SS
3 Free Software Foundation, Inc.
4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "frame.h"
24#include "inferior.h"
25#include "symtab.h"
26#include "target.h"
27#include "gdbcore.h"
28#include "gdbcmd.h"
29#include "symfile.h"
30#include "objfiles.h"
31#include "xcoffsolib.h"
7a78ae4e
ND
32#include "arch-utils.h"
33
34#include "bfd/libbfd.h" /* for bfd_default_set_arch_mach */
35#include "coff/internal.h" /* for libcoff.h */
36#include "bfd/libcoff.h" /* for xcoff_data */
37
38/* Some important register numbers. Keep these in the same order as in
39 /usr/mstsave.h `mstsave' structure, for easier processing. */
40
41#define GP0_REGNUM 0 /* GPR register 0 */
42#define TOC_REGNUM 2 /* TOC register */
43#define PS_REGNUM 65 /* Processor (or machine) status (%msr) */
44#define CR_REGNUM 66 /* Condition register */
45#define LR_REGNUM 67 /* Link register */
46#define CTR_REGNUM 68 /* Count register */
47
48/* If the kernel has to deliver a signal, it pushes a sigcontext
49 structure on the stack and then calls the signal handler, passing
50 the address of the sigcontext in an argument register. Usually
51 the signal handler doesn't save this register, so we have to
52 access the sigcontext structure via an offset from the signal handler
53 frame.
54 The following constants were determined by experimentation on AIX 3.2. */
55#define SIG_FRAME_PC_OFFSET 96
56#define SIG_FRAME_LR_OFFSET 108
57#define SIG_FRAME_FP_OFFSET 284
58
59/* Default offset from SP where the LR is stored */
60#define DEFAULT_LR_SAVE 8
61
62/* To be used by skip_prologue. */
63
64struct rs6000_framedata
65 {
66 int offset; /* total size of frame --- the distance
67 by which we decrement sp to allocate
68 the frame */
69 int saved_gpr; /* smallest # of saved gpr */
70 int saved_fpr; /* smallest # of saved fpr */
71 int alloca_reg; /* alloca register number (frame ptr) */
72 char frameless; /* true if frameless functions. */
73 char nosavedpc; /* true if pc not saved. */
74 int gpr_offset; /* offset of saved gprs from prev sp */
75 int fpr_offset; /* offset of saved fprs from prev sp */
76 int lr_offset; /* offset of saved lr */
77 int cr_offset; /* offset of saved cr */
78 };
79
80/* Description of a single register. */
81
82struct reg
83 {
84 char *name; /* name of register */
85 unsigned char sz32; /* size on 32-bit arch, 0 if nonextant */
86 unsigned char sz64; /* size on 64-bit arch, 0 if nonextant */
87 unsigned char fpr; /* whether register is floating-point */
88 };
89
90/* Private data that this module attaches to struct gdbarch. */
91
92struct gdbarch_tdep
93 {
94 int wordsize; /* size in bytes of fixed-point word */
95 int *regoff; /* byte offsets in register arrays */
96 const struct reg *regs; /* from current variant */
97 };
c906108c 98
7a78ae4e
ND
99/* Return the current architecture's gdbarch_tdep structure. */
100
101#define TDEP gdbarch_tdep (current_gdbarch)
c906108c
SS
102
103/* Breakpoint shadows for the single step instructions will be kept here. */
104
c5aa993b
JM
105static struct sstep_breaks
106 {
107 /* Address, or 0 if this is not in use. */
108 CORE_ADDR address;
109 /* Shadow contents. */
110 char data[4];
111 }
112stepBreaks[2];
c906108c
SS
113
114/* Hook for determining the TOC address when calling functions in the
115 inferior under AIX. The initialization code in rs6000-nat.c sets
116 this hook to point to find_toc_address. */
117
7a78ae4e
ND
118CORE_ADDR (*rs6000_find_toc_address_hook) (CORE_ADDR) = NULL;
119
120/* Hook to set the current architecture when starting a child process.
121 rs6000-nat.c sets this. */
122
123void (*rs6000_set_host_arch_hook) (int) = NULL;
c906108c
SS
124
125/* Static function prototypes */
126
a14ed312
KB
127static CORE_ADDR branch_dest (int opcode, int instr, CORE_ADDR pc,
128 CORE_ADDR safety);
7a78ae4e
ND
129static CORE_ADDR skip_prologue (CORE_ADDR, struct rs6000_framedata *);
130static void frame_get_saved_regs (struct frame_info * fi,
131 struct rs6000_framedata * fdatap);
132static CORE_ADDR frame_initial_stack_address (struct frame_info *);
c906108c 133
7a78ae4e 134/* Read a LEN-byte address from debugged memory address MEMADDR. */
c906108c 135
7a78ae4e
ND
136static CORE_ADDR
137read_memory_addr (CORE_ADDR memaddr, int len)
138{
139 return read_memory_unsigned_integer (memaddr, len);
140}
c906108c 141
7a78ae4e
ND
142static CORE_ADDR
143rs6000_skip_prologue (CORE_ADDR pc)
b83266a0
SS
144{
145 struct rs6000_framedata frame;
146 pc = skip_prologue (pc, &frame);
147 return pc;
148}
149
150
c906108c
SS
151/* Fill in fi->saved_regs */
152
153struct frame_extra_info
154{
155 /* Functions calling alloca() change the value of the stack
156 pointer. We need to use initial stack pointer (which is saved in
157 r31 by gcc) in such cases. If a compiler emits traceback table,
158 then we should use the alloca register specified in traceback
159 table. FIXME. */
c5aa993b 160 CORE_ADDR initial_sp; /* initial stack pointer. */
c906108c
SS
161};
162
7a78ae4e
ND
163static void
164rs6000_init_extra_frame_info (int fromleaf, struct frame_info *fi)
c906108c 165{
c5aa993b 166 fi->extra_info = (struct frame_extra_info *)
c906108c
SS
167 frame_obstack_alloc (sizeof (struct frame_extra_info));
168 fi->extra_info->initial_sp = 0;
169 if (fi->next != (CORE_ADDR) 0
170 && fi->pc < TEXT_SEGMENT_BASE)
7a292a7a 171 /* We're in get_prev_frame */
c906108c
SS
172 /* and this is a special signal frame. */
173 /* (fi->pc will be some low address in the kernel, */
174 /* to which the signal handler returns). */
175 fi->signal_handler_caller = 1;
176}
177
7a78ae4e
ND
178/* Put here the code to store, into a struct frame_saved_regs,
179 the addresses of the saved registers of frame described by FRAME_INFO.
180 This includes special registers such as pc and fp saved in special
181 ways in the stack frame. sp is even more special:
182 the address we return for it IS the sp for the next frame. */
c906108c 183
7a78ae4e
ND
184/* In this implementation for RS/6000, we do *not* save sp. I am
185 not sure if it will be needed. The following function takes care of gpr's
186 and fpr's only. */
187
188static void
189rs6000_frame_init_saved_regs (struct frame_info *fi)
c906108c
SS
190{
191 frame_get_saved_regs (fi, NULL);
192}
193
7a78ae4e
ND
194static CORE_ADDR
195rs6000_frame_args_address (struct frame_info *fi)
c906108c
SS
196{
197 if (fi->extra_info->initial_sp != 0)
198 return fi->extra_info->initial_sp;
199 else
200 return frame_initial_stack_address (fi);
201}
202
7a78ae4e
ND
203/* Immediately after a function call, return the saved pc.
204 Can't go through the frames for this because on some machines
205 the new frame is not set up until the new function executes
206 some instructions. */
207
208static CORE_ADDR
209rs6000_saved_pc_after_call (struct frame_info *fi)
210{
211 return read_register (LR_REGNUM);
212}
c906108c
SS
213
214/* Calculate the destination of a branch/jump. Return -1 if not a branch. */
215
216static CORE_ADDR
7a78ae4e 217branch_dest (int opcode, int instr, CORE_ADDR pc, CORE_ADDR safety)
c906108c
SS
218{
219 CORE_ADDR dest;
220 int immediate;
221 int absolute;
222 int ext_op;
223
224 absolute = (int) ((instr >> 1) & 1);
225
c5aa993b
JM
226 switch (opcode)
227 {
228 case 18:
229 immediate = ((instr & ~3) << 6) >> 6; /* br unconditional */
230 if (absolute)
231 dest = immediate;
232 else
233 dest = pc + immediate;
234 break;
235
236 case 16:
237 immediate = ((instr & ~3) << 16) >> 16; /* br conditional */
238 if (absolute)
239 dest = immediate;
240 else
241 dest = pc + immediate;
242 break;
243
244 case 19:
245 ext_op = (instr >> 1) & 0x3ff;
246
247 if (ext_op == 16) /* br conditional register */
248 {
249 dest = read_register (LR_REGNUM) & ~3;
250
251 /* If we are about to return from a signal handler, dest is
252 something like 0x3c90. The current frame is a signal handler
253 caller frame, upon completion of the sigreturn system call
254 execution will return to the saved PC in the frame. */
255 if (dest < TEXT_SEGMENT_BASE)
256 {
257 struct frame_info *fi;
258
259 fi = get_current_frame ();
260 if (fi != NULL)
7a78ae4e
ND
261 dest = read_memory_addr (fi->frame + SIG_FRAME_PC_OFFSET,
262 TDEP->wordsize);
c5aa993b
JM
263 }
264 }
265
266 else if (ext_op == 528) /* br cond to count reg */
267 {
268 dest = read_register (CTR_REGNUM) & ~3;
269
270 /* If we are about to execute a system call, dest is something
271 like 0x22fc or 0x3b00. Upon completion the system call
272 will return to the address in the link register. */
273 if (dest < TEXT_SEGMENT_BASE)
c906108c 274 dest = read_register (LR_REGNUM) & ~3;
c5aa993b
JM
275 }
276 else
277 return -1;
278 break;
c906108c 279
c5aa993b
JM
280 default:
281 return -1;
282 }
c906108c
SS
283 return (dest < TEXT_SEGMENT_BASE) ? safety : dest;
284}
285
286
287/* Sequence of bytes for breakpoint instruction. */
288
289#define BIG_BREAKPOINT { 0x7d, 0x82, 0x10, 0x08 }
290#define LITTLE_BREAKPOINT { 0x08, 0x10, 0x82, 0x7d }
291
7a78ae4e
ND
292static unsigned char *
293rs6000_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)
c906108c
SS
294{
295 static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
296 static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
297 *bp_size = 4;
298 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
299 return big_breakpoint;
300 else
301 return little_breakpoint;
302}
303
304
305/* AIX does not support PT_STEP. Simulate it. */
306
307void
7a78ae4e 308rs6000_software_single_step (unsigned int signal, int insert_breakpoints_p)
c906108c
SS
309{
310#define INSNLEN(OPCODE) 4
311
312 static char le_breakp[] = LITTLE_BREAKPOINT;
313 static char be_breakp[] = BIG_BREAKPOINT;
314 char *breakp = TARGET_BYTE_ORDER == BIG_ENDIAN ? be_breakp : le_breakp;
315 int ii, insn;
316 CORE_ADDR loc;
317 CORE_ADDR breaks[2];
318 int opcode;
319
c5aa993b
JM
320 if (insert_breakpoints_p)
321 {
c906108c 322
c5aa993b 323 loc = read_pc ();
c906108c 324
c5aa993b 325 insn = read_memory_integer (loc, 4);
c906108c 326
c5aa993b
JM
327 breaks[0] = loc + INSNLEN (insn);
328 opcode = insn >> 26;
329 breaks[1] = branch_dest (opcode, insn, loc, breaks[0]);
c906108c 330
c5aa993b
JM
331 /* Don't put two breakpoints on the same address. */
332 if (breaks[1] == breaks[0])
333 breaks[1] = -1;
c906108c 334
c5aa993b 335 stepBreaks[1].address = 0;
c906108c 336
c5aa993b
JM
337 for (ii = 0; ii < 2; ++ii)
338 {
c906108c 339
c5aa993b
JM
340 /* ignore invalid breakpoint. */
341 if (breaks[ii] == -1)
342 continue;
c906108c 343
c5aa993b 344 read_memory (breaks[ii], stepBreaks[ii].data, 4);
c906108c 345
c5aa993b
JM
346 write_memory (breaks[ii], breakp, 4);
347 stepBreaks[ii].address = breaks[ii];
348 }
c906108c 349
c5aa993b
JM
350 }
351 else
352 {
c906108c 353
c5aa993b
JM
354 /* remove step breakpoints. */
355 for (ii = 0; ii < 2; ++ii)
356 if (stepBreaks[ii].address != 0)
357 write_memory
358 (stepBreaks[ii].address, stepBreaks[ii].data, 4);
c906108c 359
c5aa993b 360 }
c906108c 361 errno = 0; /* FIXME, don't ignore errors! */
c5aa993b 362 /* What errors? {read,write}_memory call error(). */
c906108c
SS
363}
364
365
366/* return pc value after skipping a function prologue and also return
367 information about a function frame.
368
369 in struct rs6000_framedata fdata:
c5aa993b
JM
370 - frameless is TRUE, if function does not have a frame.
371 - nosavedpc is TRUE, if function does not save %pc value in its frame.
372 - offset is the initial size of this stack frame --- the amount by
373 which we decrement the sp to allocate the frame.
374 - saved_gpr is the number of the first saved gpr.
375 - saved_fpr is the number of the first saved fpr.
376 - alloca_reg is the number of the register used for alloca() handling.
377 Otherwise -1.
378 - gpr_offset is the offset of the first saved gpr from the previous frame.
379 - fpr_offset is the offset of the first saved fpr from the previous frame.
380 - lr_offset is the offset of the saved lr
381 - cr_offset is the offset of the saved cr
382 */
c906108c
SS
383
384#define SIGNED_SHORT(x) \
385 ((sizeof (short) == 2) \
386 ? ((int)(short)(x)) \
387 : ((int)((((x) & 0xffff) ^ 0x8000) - 0x8000)))
388
389#define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
390
7a78ae4e 391static CORE_ADDR
ddb20c56 392skip_prologue (CORE_ADDR pc, struct rs6000_framedata *fdata)
c906108c
SS
393{
394 CORE_ADDR orig_pc = pc;
ddb20c56 395 CORE_ADDR last_prologue_pc;
c906108c
SS
396 char buf[4];
397 unsigned long op;
398 long offset = 0;
482ca3f5
KB
399 int lr_reg = -1;
400 int cr_reg = -1;
c906108c
SS
401 int reg;
402 int framep = 0;
403 int minimal_toc_loaded = 0;
ddb20c56 404 int prev_insn_was_prologue_insn = 1;
c906108c 405
ddb20c56 406 memset (fdata, 0, sizeof (struct rs6000_framedata));
c906108c
SS
407 fdata->saved_gpr = -1;
408 fdata->saved_fpr = -1;
409 fdata->alloca_reg = -1;
410 fdata->frameless = 1;
411 fdata->nosavedpc = 1;
412
c906108c
SS
413 pc -= 4;
414 for (;;)
415 {
416 pc += 4;
ddb20c56
KB
417
418 /* Sometimes it isn't clear if an instruction is a prologue
419 instruction or not. When we encounter one of these ambiguous
420 cases, we'll set prev_insn_was_prologue_insn to 0 (false).
421 Otherwise, we'll assume that it really is a prologue instruction. */
422 if (prev_insn_was_prologue_insn)
423 last_prologue_pc = pc;
424 prev_insn_was_prologue_insn = 1;
425
426 if (target_read_memory (pc, buf, 4))
427 break;
428 op = extract_signed_integer (buf, 4);
c906108c 429
c5aa993b
JM
430 if ((op & 0xfc1fffff) == 0x7c0802a6)
431 { /* mflr Rx */
432 lr_reg = (op & 0x03e00000) | 0x90010000;
433 continue;
c906108c 434
c5aa993b
JM
435 }
436 else if ((op & 0xfc1fffff) == 0x7c000026)
437 { /* mfcr Rx */
438 cr_reg = (op & 0x03e00000) | 0x90010000;
439 continue;
c906108c 440
c906108c 441 }
c5aa993b
JM
442 else if ((op & 0xfc1f0000) == 0xd8010000)
443 { /* stfd Rx,NUM(r1) */
444 reg = GET_SRC_REG (op);
445 if (fdata->saved_fpr == -1 || fdata->saved_fpr > reg)
446 {
447 fdata->saved_fpr = reg;
448 fdata->fpr_offset = SIGNED_SHORT (op) + offset;
449 }
450 continue;
c906108c 451
c5aa993b
JM
452 }
453 else if (((op & 0xfc1f0000) == 0xbc010000) || /* stm Rx, NUM(r1) */
7a78ae4e
ND
454 (((op & 0xfc1f0000) == 0x90010000 || /* st rx,NUM(r1) */
455 (op & 0xfc1f0003) == 0xf8010000) && /* std rx,NUM(r1) */
456 (op & 0x03e00000) >= 0x01a00000)) /* rx >= r13 */
c5aa993b
JM
457 {
458
459 reg = GET_SRC_REG (op);
460 if (fdata->saved_gpr == -1 || fdata->saved_gpr > reg)
461 {
462 fdata->saved_gpr = reg;
7a78ae4e
ND
463 if ((op & 0xfc1f0003) == 0xf8010000)
464 op = (op >> 1) << 1;
c5aa993b
JM
465 fdata->gpr_offset = SIGNED_SHORT (op) + offset;
466 }
467 continue;
c906108c 468
ddb20c56
KB
469 }
470 else if ((op & 0xffff0000) == 0x60000000)
471 {
472 /* nop */
473 /* Allow nops in the prologue, but do not consider them to
474 be part of the prologue unless followed by other prologue
475 instructions. */
476 prev_insn_was_prologue_insn = 0;
477 continue;
478
c906108c 479 }
c5aa993b
JM
480 else if ((op & 0xffff0000) == 0x3c000000)
481 { /* addis 0,0,NUM, used
482 for >= 32k frames */
483 fdata->offset = (op & 0x0000ffff) << 16;
484 fdata->frameless = 0;
485 continue;
486
487 }
488 else if ((op & 0xffff0000) == 0x60000000)
489 { /* ori 0,0,NUM, 2nd ha
490 lf of >= 32k frames */
491 fdata->offset |= (op & 0x0000ffff);
492 fdata->frameless = 0;
493 continue;
494
495 }
482ca3f5 496 else if (lr_reg != -1 && (op & 0xffff0000) == lr_reg)
c5aa993b
JM
497 { /* st Rx,NUM(r1)
498 where Rx == lr */
499 fdata->lr_offset = SIGNED_SHORT (op) + offset;
500 fdata->nosavedpc = 0;
501 lr_reg = 0;
502 continue;
503
504 }
482ca3f5 505 else if (cr_reg != -1 && (op & 0xffff0000) == cr_reg)
c5aa993b
JM
506 { /* st Rx,NUM(r1)
507 where Rx == cr */
508 fdata->cr_offset = SIGNED_SHORT (op) + offset;
509 cr_reg = 0;
510 continue;
511
512 }
513 else if (op == 0x48000005)
514 { /* bl .+4 used in
515 -mrelocatable */
516 continue;
517
518 }
519 else if (op == 0x48000004)
520 { /* b .+4 (xlc) */
521 break;
522
523 }
524 else if (((op & 0xffff0000) == 0x801e0000 || /* lwz 0,NUM(r30), used
c906108c 525 in V.4 -mrelocatable */
c5aa993b
JM
526 op == 0x7fc0f214) && /* add r30,r0,r30, used
527 in V.4 -mrelocatable */
528 lr_reg == 0x901e0000)
529 {
530 continue;
c906108c 531
c5aa993b
JM
532 }
533 else if ((op & 0xffff0000) == 0x3fc00000 || /* addis 30,0,foo@ha, used
c906108c 534 in V.4 -mminimal-toc */
c5aa993b
JM
535 (op & 0xffff0000) == 0x3bde0000)
536 { /* addi 30,30,foo@l */
537 continue;
c906108c 538
c5aa993b
JM
539 }
540 else if ((op & 0xfc000001) == 0x48000001)
541 { /* bl foo,
542 to save fprs??? */
c906108c 543
c5aa993b
JM
544 fdata->frameless = 0;
545 /* Don't skip over the subroutine call if it is not within the first
546 three instructions of the prologue. */
547 if ((pc - orig_pc) > 8)
548 break;
549
550 op = read_memory_integer (pc + 4, 4);
551
552 /* At this point, make sure this is not a trampoline function
553 (a function that simply calls another functions, and nothing else).
554 If the next is not a nop, this branch was part of the function
555 prologue. */
556
557 if (op == 0x4def7b82 || op == 0) /* crorc 15, 15, 15 */
558 break; /* don't skip over
559 this branch */
560 continue;
561
562 /* update stack pointer */
563 }
7a78ae4e
ND
564 else if ((op & 0xffff0000) == 0x94210000 || /* stu r1,NUM(r1) */
565 (op & 0xffff0003) == 0xf8210001) /* stdu r1,NUM(r1) */
566 {
c5aa993b 567 fdata->frameless = 0;
7a78ae4e
ND
568 if ((op & 0xffff0003) == 0xf8210001)
569 op = (op >> 1) << 1;
c5aa993b
JM
570 fdata->offset = SIGNED_SHORT (op);
571 offset = fdata->offset;
572 continue;
573
574 }
575 else if (op == 0x7c21016e)
576 { /* stwux 1,1,0 */
577 fdata->frameless = 0;
578 offset = fdata->offset;
579 continue;
580
581 /* Load up minimal toc pointer */
582 }
583 else if ((op >> 22) == 0x20f
584 && !minimal_toc_loaded)
585 { /* l r31,... or l r30,... */
586 minimal_toc_loaded = 1;
587 continue;
588
f6077098
KB
589 /* move parameters from argument registers to local variable
590 registers */
591 }
592 else if ((op & 0xfc0007fe) == 0x7c000378 && /* mr(.) Rx,Ry */
593 (((op >> 21) & 31) >= 3) && /* R3 >= Ry >= R10 */
594 (((op >> 21) & 31) <= 10) &&
595 (((op >> 16) & 31) >= fdata->saved_gpr)) /* Rx: local var reg */
596 {
597 continue;
598
c5aa993b
JM
599 /* store parameters in stack */
600 }
601 else if ((op & 0xfc1f0000) == 0x90010000 || /* st rx,NUM(r1) */
7a78ae4e 602 (op & 0xfc1f0003) == 0xf8010000 || /* std rx,NUM(r1) */
c5aa993b 603 (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */
7a78ae4e
ND
604 (op & 0xfc1f0000) == 0xfc010000) /* frsp, fp?,NUM(r1) */
605 {
c5aa993b 606 continue;
c906108c 607
c5aa993b
JM
608 /* store parameters in stack via frame pointer */
609 }
610 else if (framep &&
611 ((op & 0xfc1f0000) == 0x901f0000 || /* st rx,NUM(r1) */
612 (op & 0xfc1f0000) == 0xd81f0000 || /* stfd Rx,NUM(r1) */
613 (op & 0xfc1f0000) == 0xfc1f0000))
614 { /* frsp, fp?,NUM(r1) */
615 continue;
616
617 /* Set up frame pointer */
618 }
619 else if (op == 0x603f0000 /* oril r31, r1, 0x0 */
620 || op == 0x7c3f0b78)
621 { /* mr r31, r1 */
622 fdata->frameless = 0;
623 framep = 1;
624 fdata->alloca_reg = 31;
625 continue;
626
627 /* Another way to set up the frame pointer. */
628 }
629 else if ((op & 0xfc1fffff) == 0x38010000)
630 { /* addi rX, r1, 0x0 */
631 fdata->frameless = 0;
632 framep = 1;
633 fdata->alloca_reg = (op & ~0x38010000) >> 21;
634 continue;
635
636 }
637 else
638 {
639 break;
640 }
c906108c
SS
641 }
642
643#if 0
644/* I have problems with skipping over __main() that I need to address
645 * sometime. Previously, I used to use misc_function_vector which
646 * didn't work as well as I wanted to be. -MGO */
647
648 /* If the first thing after skipping a prolog is a branch to a function,
649 this might be a call to an initializer in main(), introduced by gcc2.
650 We'd like to skip over it as well. Fortunately, xlc does some extra
651 work before calling a function right after a prologue, thus we can
652 single out such gcc2 behaviour. */
c906108c 653
c906108c 654
c5aa993b
JM
655 if ((op & 0xfc000001) == 0x48000001)
656 { /* bl foo, an initializer function? */
657 op = read_memory_integer (pc + 4, 4);
658
659 if (op == 0x4def7b82)
660 { /* cror 0xf, 0xf, 0xf (nop) */
c906108c 661
c5aa993b
JM
662 /* check and see if we are in main. If so, skip over this initializer
663 function as well. */
c906108c 664
c5aa993b
JM
665 tmp = find_pc_misc_function (pc);
666 if (tmp >= 0 && STREQ (misc_function_vector[tmp].name, "main"))
667 return pc + 8;
668 }
c906108c 669 }
c906108c 670#endif /* 0 */
c5aa993b
JM
671
672 fdata->offset = -fdata->offset;
ddb20c56 673 return last_prologue_pc;
c906108c
SS
674}
675
676
677/*************************************************************************
f6077098 678 Support for creating pushing a dummy frame into the stack, and popping
c906108c
SS
679 frames, etc.
680*************************************************************************/
681
c906108c
SS
682extern int stop_stack_dummy;
683
c906108c 684
7a78ae4e 685/* Pop the innermost frame, go back to the caller. */
c5aa993b 686
c906108c 687static void
7a78ae4e 688rs6000_pop_frame (void)
c906108c 689{
c5aa993b 690 CORE_ADDR pc, lr, sp, prev_sp; /* %pc, %lr, %sp */
c906108c
SS
691 struct rs6000_framedata fdata;
692 struct frame_info *frame = get_current_frame ();
7a78ae4e 693 int addr, ii, wordsize;
c906108c
SS
694
695 pc = read_pc ();
696 sp = FRAME_FP (frame);
697
698 if (stop_stack_dummy)
699 {
7a78ae4e
ND
700 generic_pop_dummy_frame ();
701 flush_cached_frames ();
702 return;
c906108c
SS
703 }
704
705 /* Make sure that all registers are valid. */
706 read_register_bytes (0, NULL, REGISTER_BYTES);
707
708 /* figure out previous %pc value. If the function is frameless, it is
709 still in the link register, otherwise walk the frames and retrieve the
710 saved %pc value in the previous frame. */
711
712 addr = get_pc_function_start (frame->pc);
713 (void) skip_prologue (addr, &fdata);
714
7a78ae4e 715 wordsize = TDEP->wordsize;
c906108c
SS
716 if (fdata.frameless)
717 prev_sp = sp;
718 else
7a78ae4e 719 prev_sp = read_memory_addr (sp, wordsize);
c906108c
SS
720 if (fdata.lr_offset == 0)
721 lr = read_register (LR_REGNUM);
722 else
7a78ae4e 723 lr = read_memory_addr (prev_sp + fdata.lr_offset, wordsize);
c906108c
SS
724
725 /* reset %pc value. */
726 write_register (PC_REGNUM, lr);
727
728 /* reset register values if any was saved earlier. */
729
730 if (fdata.saved_gpr != -1)
731 {
732 addr = prev_sp + fdata.gpr_offset;
c5aa993b
JM
733 for (ii = fdata.saved_gpr; ii <= 31; ++ii)
734 {
7a78ae4e
ND
735 read_memory (addr, &registers[REGISTER_BYTE (ii)], wordsize);
736 addr += wordsize;
c5aa993b 737 }
c906108c
SS
738 }
739
740 if (fdata.saved_fpr != -1)
741 {
742 addr = prev_sp + fdata.fpr_offset;
c5aa993b
JM
743 for (ii = fdata.saved_fpr; ii <= 31; ++ii)
744 {
745 read_memory (addr, &registers[REGISTER_BYTE (ii + FP0_REGNUM)], 8);
746 addr += 8;
747 }
c906108c
SS
748 }
749
750 write_register (SP_REGNUM, prev_sp);
751 target_store_registers (-1);
752 flush_cached_frames ();
753}
754
7a78ae4e
ND
755/* Fixup the call sequence of a dummy function, with the real function
756 address. Its arguments will be passed by gdb. */
c906108c 757
7a78ae4e
ND
758static void
759rs6000_fix_call_dummy (char *dummyname, CORE_ADDR pc, CORE_ADDR fun,
760 int nargs, value_ptr *args, struct type *type,
761 int gcc_p)
c906108c
SS
762{
763#define TOC_ADDR_OFFSET 20
764#define TARGET_ADDR_OFFSET 28
765
766 int ii;
767 CORE_ADDR target_addr;
768
7a78ae4e 769 if (rs6000_find_toc_address_hook != NULL)
f6077098 770 {
7a78ae4e
ND
771 CORE_ADDR tocvalue = (*rs6000_find_toc_address_hook) (fun);
772 write_register (TOC_REGNUM, tocvalue);
f6077098 773 }
c906108c
SS
774}
775
7a78ae4e 776/* Pass the arguments in either registers, or in the stack. In RS/6000,
c906108c
SS
777 the first eight words of the argument list (that might be less than
778 eight parameters if some parameters occupy more than one word) are
7a78ae4e 779 passed in r3..r10 registers. float and double parameters are
c906108c
SS
780 passed in fpr's, in addition to that. Rest of the parameters if any
781 are passed in user stack. There might be cases in which half of the
782 parameter is copied into registers, the other half is pushed into
783 stack.
784
7a78ae4e
ND
785 Stack must be aligned on 64-bit boundaries when synthesizing
786 function calls.
787
c906108c
SS
788 If the function is returning a structure, then the return address is passed
789 in r3, then the first 7 words of the parameters can be passed in registers,
790 starting from r4. */
791
7a78ae4e
ND
792static CORE_ADDR
793rs6000_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
794 int struct_return, CORE_ADDR struct_addr)
c906108c
SS
795{
796 int ii;
797 int len = 0;
c5aa993b
JM
798 int argno; /* current argument number */
799 int argbytes; /* current argument byte */
800 char tmp_buffer[50];
801 int f_argno = 0; /* current floating point argno */
7a78ae4e 802 int wordsize = TDEP->wordsize;
c906108c
SS
803
804 value_ptr arg = 0;
805 struct type *type;
806
807 CORE_ADDR saved_sp;
808
c906108c
SS
809 /* The first eight words of ther arguments are passed in registers. Copy
810 them appropriately.
811
812 If the function is returning a `struct', then the first word (which
813 will be passed in r3) is used for struct return address. In that
814 case we should advance one word and start from r4 register to copy
815 parameters. */
816
c5aa993b 817 ii = struct_return ? 1 : 0;
c906108c
SS
818
819/*
c5aa993b
JM
820 effectively indirect call... gcc does...
821
822 return_val example( float, int);
823
824 eabi:
825 float in fp0, int in r3
826 offset of stack on overflow 8/16
827 for varargs, must go by type.
828 power open:
829 float in r3&r4, int in r5
830 offset of stack on overflow different
831 both:
832 return in r3 or f0. If no float, must study how gcc emulates floats;
833 pay attention to arg promotion.
834 User may have to cast\args to handle promotion correctly
835 since gdb won't know if prototype supplied or not.
836 */
c906108c 837
c5aa993b
JM
838 for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
839 {
f6077098 840 int reg_size = REGISTER_RAW_SIZE (ii + 3);
c5aa993b
JM
841
842 arg = args[argno];
843 type = check_typedef (VALUE_TYPE (arg));
844 len = TYPE_LENGTH (type);
845
846 if (TYPE_CODE (type) == TYPE_CODE_FLT)
847 {
848
849 /* floating point arguments are passed in fpr's, as well as gpr's.
850 There are 13 fpr's reserved for passing parameters. At this point
851 there is no way we would run out of them. */
852
853 if (len > 8)
854 printf_unfiltered (
855 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
856
857 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM + 1 + f_argno)],
858 VALUE_CONTENTS (arg),
859 len);
860 ++f_argno;
861 }
862
f6077098 863 if (len > reg_size)
c5aa993b
JM
864 {
865
866 /* Argument takes more than one register. */
867 while (argbytes < len)
868 {
f6077098 869 memset (&registers[REGISTER_BYTE (ii + 3)], 0, reg_size);
c5aa993b
JM
870 memcpy (&registers[REGISTER_BYTE (ii + 3)],
871 ((char *) VALUE_CONTENTS (arg)) + argbytes,
f6077098
KB
872 (len - argbytes) > reg_size
873 ? reg_size : len - argbytes);
874 ++ii, argbytes += reg_size;
c5aa993b
JM
875
876 if (ii >= 8)
877 goto ran_out_of_registers_for_arguments;
878 }
879 argbytes = 0;
880 --ii;
881 }
882 else
883 { /* Argument can fit in one register. No problem. */
f6077098
KB
884 int adj = TARGET_BYTE_ORDER == BIG_ENDIAN ? reg_size - len : 0;
885 memset (&registers[REGISTER_BYTE (ii + 3)], 0, reg_size);
886 memcpy ((char *)&registers[REGISTER_BYTE (ii + 3)] + adj,
887 VALUE_CONTENTS (arg), len);
c5aa993b
JM
888 }
889 ++argno;
c906108c 890 }
c906108c
SS
891
892ran_out_of_registers_for_arguments:
893
7a78ae4e 894 saved_sp = read_sp ();
f6077098 895#ifndef ELF_OBJECT_FORMAT
7a78ae4e
ND
896 /* location for 8 parameters are always reserved. */
897 sp -= wordsize * 8;
f6077098 898
7a78ae4e
ND
899 /* another six words for back chain, TOC register, link register, etc. */
900 sp -= wordsize * 6;
f6077098 901
7a78ae4e
ND
902 /* stack pointer must be quadword aligned */
903 sp &= -16;
f6077098 904#endif
c906108c 905
c906108c
SS
906 /* if there are more arguments, allocate space for them in
907 the stack, then push them starting from the ninth one. */
908
c5aa993b
JM
909 if ((argno < nargs) || argbytes)
910 {
911 int space = 0, jj;
c906108c 912
c5aa993b
JM
913 if (argbytes)
914 {
915 space += ((len - argbytes + 3) & -4);
916 jj = argno + 1;
917 }
918 else
919 jj = argno;
c906108c 920
c5aa993b
JM
921 for (; jj < nargs; ++jj)
922 {
923 value_ptr val = args[jj];
924 space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
925 }
c906108c 926
c5aa993b 927 /* add location required for the rest of the parameters */
f6077098 928 space = (space + 15) & -16;
c5aa993b 929 sp -= space;
c906108c 930
c5aa993b
JM
931 /* This is another instance we need to be concerned about securing our
932 stack space. If we write anything underneath %sp (r1), we might conflict
933 with the kernel who thinks he is free to use this area. So, update %sp
934 first before doing anything else. */
c906108c 935
c5aa993b 936 write_register (SP_REGNUM, sp);
c906108c 937
c5aa993b
JM
938 /* if the last argument copied into the registers didn't fit there
939 completely, push the rest of it into stack. */
c906108c 940
c5aa993b
JM
941 if (argbytes)
942 {
943 write_memory (sp + 24 + (ii * 4),
944 ((char *) VALUE_CONTENTS (arg)) + argbytes,
945 len - argbytes);
946 ++argno;
947 ii += ((len - argbytes + 3) & -4) / 4;
948 }
c906108c 949
c5aa993b
JM
950 /* push the rest of the arguments into stack. */
951 for (; argno < nargs; ++argno)
952 {
c906108c 953
c5aa993b
JM
954 arg = args[argno];
955 type = check_typedef (VALUE_TYPE (arg));
956 len = TYPE_LENGTH (type);
c906108c
SS
957
958
c5aa993b
JM
959 /* float types should be passed in fpr's, as well as in the stack. */
960 if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
961 {
c906108c 962
c5aa993b
JM
963 if (len > 8)
964 printf_unfiltered (
965 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
c906108c 966
c5aa993b
JM
967 memcpy (&registers[REGISTER_BYTE (FP0_REGNUM + 1 + f_argno)],
968 VALUE_CONTENTS (arg),
969 len);
970 ++f_argno;
971 }
c906108c 972
c5aa993b
JM
973 write_memory (sp + 24 + (ii * 4), (char *) VALUE_CONTENTS (arg), len);
974 ii += ((len + 3) & -4) / 4;
975 }
c906108c 976 }
c906108c
SS
977 else
978 /* Secure stack areas first, before doing anything else. */
979 write_register (SP_REGNUM, sp);
980
c906108c
SS
981 /* set back chain properly */
982 store_address (tmp_buffer, 4, saved_sp);
983 write_memory (sp, tmp_buffer, 4);
984
985 target_store_registers (-1);
986 return sp;
987}
c906108c
SS
988
989/* Function: ppc_push_return_address (pc, sp)
990 Set up the return address for the inferior function call. */
991
7a78ae4e
ND
992static CORE_ADDR
993ppc_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
c906108c
SS
994{
995 write_register (LR_REGNUM, CALL_DUMMY_ADDRESS ());
996 return sp;
997}
998
7a78ae4e
ND
999/* Extract a function return value of type TYPE from raw register array
1000 REGBUF, and copy that return value into VALBUF in virtual format. */
c906108c 1001
7a78ae4e
ND
1002static void
1003rs6000_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
c906108c
SS
1004{
1005 int offset = 0;
1006
c5aa993b
JM
1007 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1008 {
c906108c 1009
c5aa993b
JM
1010 double dd;
1011 float ff;
1012 /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes.
1013 We need to truncate the return value into float size (4 byte) if
1014 necessary. */
c906108c 1015
c5aa993b
JM
1016 if (TYPE_LENGTH (valtype) > 4) /* this is a double */
1017 memcpy (valbuf,
1018 &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)],
1019 TYPE_LENGTH (valtype));
1020 else
1021 { /* float */
1022 memcpy (&dd, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], 8);
1023 ff = (float) dd;
1024 memcpy (valbuf, &ff, sizeof (float));
1025 }
1026 }
1027 else
1028 {
1029 /* return value is copied starting from r3. */
1030 if (TARGET_BYTE_ORDER == BIG_ENDIAN
1031 && TYPE_LENGTH (valtype) < REGISTER_RAW_SIZE (3))
1032 offset = REGISTER_RAW_SIZE (3) - TYPE_LENGTH (valtype);
1033
1034 memcpy (valbuf,
1035 regbuf + REGISTER_BYTE (3) + offset,
c906108c 1036 TYPE_LENGTH (valtype));
c906108c 1037 }
c906108c
SS
1038}
1039
7a78ae4e 1040/* Keep structure return address in this variable.
c906108c
SS
1041 FIXME: This is a horrid kludge which should not be allowed to continue
1042 living. This only allows a single nested call to a structure-returning
1043 function. Come on, guys! -- gnu@cygnus.com, Aug 92 */
1044
7a78ae4e 1045static CORE_ADDR rs6000_struct_return_address;
c906108c
SS
1046
1047/* Indirect function calls use a piece of trampoline code to do context
1048 switching, i.e. to set the new TOC table. Skip such code if we are on
1049 its first instruction (as when we have single-stepped to here).
1050 Also skip shared library trampoline code (which is different from
1051 indirect function call trampolines).
1052 Result is desired PC to step until, or NULL if we are not in
1053 trampoline code. */
1054
1055CORE_ADDR
7a78ae4e 1056rs6000_skip_trampoline_code (CORE_ADDR pc)
c906108c
SS
1057{
1058 register unsigned int ii, op;
1059 CORE_ADDR solib_target_pc;
1060
c5aa993b
JM
1061 static unsigned trampoline_code[] =
1062 {
1063 0x800b0000, /* l r0,0x0(r11) */
1064 0x90410014, /* st r2,0x14(r1) */
1065 0x7c0903a6, /* mtctr r0 */
1066 0x804b0004, /* l r2,0x4(r11) */
1067 0x816b0008, /* l r11,0x8(r11) */
1068 0x4e800420, /* bctr */
1069 0x4e800020, /* br */
1070 0
c906108c
SS
1071 };
1072
1073 /* If pc is in a shared library trampoline, return its target. */
1074 solib_target_pc = find_solib_trampoline_target (pc);
1075 if (solib_target_pc)
1076 return solib_target_pc;
1077
c5aa993b
JM
1078 for (ii = 0; trampoline_code[ii]; ++ii)
1079 {
1080 op = read_memory_integer (pc + (ii * 4), 4);
1081 if (op != trampoline_code[ii])
1082 return 0;
1083 }
1084 ii = read_register (11); /* r11 holds destination addr */
7a78ae4e 1085 pc = read_memory_addr (ii, TDEP->wordsize); /* (r11) value */
c906108c
SS
1086 return pc;
1087}
1088
1089/* Determines whether the function FI has a frame on the stack or not. */
1090
7a78ae4e 1091static int
c877c8e6 1092rs6000_frameless_function_invocation (struct frame_info *fi)
c906108c
SS
1093{
1094 CORE_ADDR func_start;
1095 struct rs6000_framedata fdata;
1096
1097 /* Don't even think about framelessness except on the innermost frame
1098 or if the function was interrupted by a signal. */
1099 if (fi->next != NULL && !fi->next->signal_handler_caller)
1100 return 0;
c5aa993b 1101
c906108c
SS
1102 func_start = get_pc_function_start (fi->pc);
1103
1104 /* If we failed to find the start of the function, it is a mistake
1105 to inspect the instructions. */
1106
1107 if (!func_start)
1108 {
1109 /* A frame with a zero PC is usually created by dereferencing a NULL
c5aa993b
JM
1110 function pointer, normally causing an immediate core dump of the
1111 inferior. Mark function as frameless, as the inferior has no chance
1112 of setting up a stack frame. */
c906108c
SS
1113 if (fi->pc == 0)
1114 return 1;
1115 else
1116 return 0;
1117 }
1118
1119 (void) skip_prologue (func_start, &fdata);
1120 return fdata.frameless;
1121}
1122
1123/* Return the PC saved in a frame */
1124
7a78ae4e 1125static CORE_ADDR
c877c8e6 1126rs6000_frame_saved_pc (struct frame_info *fi)
c906108c
SS
1127{
1128 CORE_ADDR func_start;
1129 struct rs6000_framedata fdata;
7a78ae4e 1130 int wordsize = TDEP->wordsize;
c906108c
SS
1131
1132 if (fi->signal_handler_caller)
7a78ae4e 1133 return read_memory_addr (fi->frame + SIG_FRAME_PC_OFFSET, wordsize);
c906108c 1134
7a78ae4e
ND
1135 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1136 return generic_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
c906108c
SS
1137
1138 func_start = get_pc_function_start (fi->pc);
1139
1140 /* If we failed to find the start of the function, it is a mistake
1141 to inspect the instructions. */
1142 if (!func_start)
1143 return 0;
1144
1145 (void) skip_prologue (func_start, &fdata);
1146
1147 if (fdata.lr_offset == 0 && fi->next != NULL)
1148 {
1149 if (fi->next->signal_handler_caller)
7a78ae4e
ND
1150 return read_memory_addr (fi->next->frame + SIG_FRAME_LR_OFFSET,
1151 wordsize);
c906108c 1152 else
7a78ae4e
ND
1153 return read_memory_addr (FRAME_CHAIN (fi) + DEFAULT_LR_SAVE,
1154 wordsize);
c906108c
SS
1155 }
1156
1157 if (fdata.lr_offset == 0)
1158 return read_register (LR_REGNUM);
1159
7a78ae4e 1160 return read_memory_addr (FRAME_CHAIN (fi) + fdata.lr_offset, wordsize);
c906108c
SS
1161}
1162
1163/* If saved registers of frame FI are not known yet, read and cache them.
1164 &FDATAP contains rs6000_framedata; TDATAP can be NULL,
1165 in which case the framedata are read. */
1166
1167static void
7a78ae4e 1168frame_get_saved_regs (struct frame_info *fi, struct rs6000_framedata *fdatap)
c906108c 1169{
c5aa993b 1170 CORE_ADDR frame_addr;
c906108c 1171 struct rs6000_framedata work_fdata;
7a78ae4e 1172 int wordsize = TDEP->wordsize;
c906108c
SS
1173
1174 if (fi->saved_regs)
1175 return;
c5aa993b 1176
c906108c
SS
1177 if (fdatap == NULL)
1178 {
1179 fdatap = &work_fdata;
1180 (void) skip_prologue (get_pc_function_start (fi->pc), fdatap);
1181 }
1182
1183 frame_saved_regs_zalloc (fi);
1184
1185 /* If there were any saved registers, figure out parent's stack
1186 pointer. */
1187 /* The following is true only if the frame doesn't have a call to
1188 alloca(), FIXME. */
1189
1190 if (fdatap->saved_fpr == 0 && fdatap->saved_gpr == 0
1191 && fdatap->lr_offset == 0 && fdatap->cr_offset == 0)
1192 frame_addr = 0;
1193 else if (fi->prev && fi->prev->frame)
1194 frame_addr = fi->prev->frame;
1195 else
7a78ae4e 1196 frame_addr = read_memory_addr (fi->frame, wordsize);
c5aa993b 1197
c906108c
SS
1198 /* if != -1, fdatap->saved_fpr is the smallest number of saved_fpr.
1199 All fpr's from saved_fpr to fp31 are saved. */
1200
1201 if (fdatap->saved_fpr >= 0)
1202 {
1203 int i;
7a78ae4e 1204 CORE_ADDR fpr_addr = frame_addr + fdatap->fpr_offset;
c906108c
SS
1205 for (i = fdatap->saved_fpr; i < 32; i++)
1206 {
7a78ae4e
ND
1207 fi->saved_regs[FP0_REGNUM + i] = fpr_addr;
1208 fpr_addr += 8;
c906108c
SS
1209 }
1210 }
1211
1212 /* if != -1, fdatap->saved_gpr is the smallest number of saved_gpr.
1213 All gpr's from saved_gpr to gpr31 are saved. */
1214
1215 if (fdatap->saved_gpr >= 0)
1216 {
1217 int i;
7a78ae4e 1218 CORE_ADDR gpr_addr = frame_addr + fdatap->gpr_offset;
c906108c
SS
1219 for (i = fdatap->saved_gpr; i < 32; i++)
1220 {
7a78ae4e
ND
1221 fi->saved_regs[i] = gpr_addr;
1222 gpr_addr += wordsize;
c906108c
SS
1223 }
1224 }
1225
1226 /* If != 0, fdatap->cr_offset is the offset from the frame that holds
1227 the CR. */
1228 if (fdatap->cr_offset != 0)
c5aa993b 1229 fi->saved_regs[CR_REGNUM] = frame_addr + fdatap->cr_offset;
c906108c
SS
1230
1231 /* If != 0, fdatap->lr_offset is the offset from the frame that holds
1232 the LR. */
1233 if (fdatap->lr_offset != 0)
c5aa993b 1234 fi->saved_regs[LR_REGNUM] = frame_addr + fdatap->lr_offset;
c906108c
SS
1235}
1236
1237/* Return the address of a frame. This is the inital %sp value when the frame
1238 was first allocated. For functions calling alloca(), it might be saved in
1239 an alloca register. */
1240
1241static CORE_ADDR
7a78ae4e 1242frame_initial_stack_address (struct frame_info *fi)
c906108c
SS
1243{
1244 CORE_ADDR tmpaddr;
1245 struct rs6000_framedata fdata;
1246 struct frame_info *callee_fi;
1247
1248 /* if the initial stack pointer (frame address) of this frame is known,
1249 just return it. */
1250
1251 if (fi->extra_info->initial_sp)
1252 return fi->extra_info->initial_sp;
1253
1254 /* find out if this function is using an alloca register.. */
1255
1256 (void) skip_prologue (get_pc_function_start (fi->pc), &fdata);
1257
1258 /* if saved registers of this frame are not known yet, read and cache them. */
1259
1260 if (!fi->saved_regs)
1261 frame_get_saved_regs (fi, &fdata);
1262
1263 /* If no alloca register used, then fi->frame is the value of the %sp for
1264 this frame, and it is good enough. */
1265
1266 if (fdata.alloca_reg < 0)
1267 {
1268 fi->extra_info->initial_sp = fi->frame;
1269 return fi->extra_info->initial_sp;
1270 }
1271
1272 /* This function has an alloca register. If this is the top-most frame
1273 (with the lowest address), the value in alloca register is good. */
1274
1275 if (!fi->next)
c5aa993b 1276 return fi->extra_info->initial_sp = read_register (fdata.alloca_reg);
c906108c
SS
1277
1278 /* Otherwise, this is a caller frame. Callee has usually already saved
1279 registers, but there are exceptions (such as when the callee
1280 has no parameters). Find the address in which caller's alloca
1281 register is saved. */
1282
c5aa993b
JM
1283 for (callee_fi = fi->next; callee_fi; callee_fi = callee_fi->next)
1284 {
c906108c 1285
c5aa993b
JM
1286 if (!callee_fi->saved_regs)
1287 frame_get_saved_regs (callee_fi, NULL);
c906108c 1288
c5aa993b 1289 /* this is the address in which alloca register is saved. */
c906108c 1290
c5aa993b
JM
1291 tmpaddr = callee_fi->saved_regs[fdata.alloca_reg];
1292 if (tmpaddr)
1293 {
7a78ae4e
ND
1294 fi->extra_info->initial_sp =
1295 read_memory_addr (tmpaddr, TDEP->wordsize);
c5aa993b
JM
1296 return fi->extra_info->initial_sp;
1297 }
c906108c 1298
c5aa993b
JM
1299 /* Go look into deeper levels of the frame chain to see if any one of
1300 the callees has saved alloca register. */
1301 }
c906108c
SS
1302
1303 /* If alloca register was not saved, by the callee (or any of its callees)
1304 then the value in the register is still good. */
1305
1306 fi->extra_info->initial_sp = read_register (fdata.alloca_reg);
1307 return fi->extra_info->initial_sp;
1308}
1309
7a78ae4e
ND
1310/* Describe the pointer in each stack frame to the previous stack frame
1311 (its caller). */
1312
1313/* FRAME_CHAIN takes a frame's nominal address
1314 and produces the frame's chain-pointer. */
1315
1316/* In the case of the RS/6000, the frame's nominal address
1317 is the address of a 4-byte word containing the calling frame's address. */
1318
1319static CORE_ADDR
1320rs6000_frame_chain (struct frame_info *thisframe)
c906108c 1321{
7a78ae4e
ND
1322 CORE_ADDR fp, fpp, lr;
1323 int wordsize = TDEP->wordsize;
c906108c 1324
7a78ae4e
ND
1325 if (PC_IN_CALL_DUMMY (thisframe->pc, thisframe->frame, thisframe->frame))
1326 return thisframe->frame; /* dummy frame same as caller's frame */
c906108c 1327
c5aa993b 1328 if (inside_entry_file (thisframe->pc) ||
c906108c
SS
1329 thisframe->pc == entry_point_address ())
1330 return 0;
1331
1332 if (thisframe->signal_handler_caller)
7a78ae4e
ND
1333 fp = read_memory_addr (thisframe->frame + SIG_FRAME_FP_OFFSET,
1334 wordsize);
c906108c
SS
1335 else if (thisframe->next != NULL
1336 && thisframe->next->signal_handler_caller
c877c8e6 1337 && FRAMELESS_FUNCTION_INVOCATION (thisframe))
c906108c
SS
1338 /* A frameless function interrupted by a signal did not change the
1339 frame pointer. */
1340 fp = FRAME_FP (thisframe);
1341 else
7a78ae4e 1342 fp = read_memory_addr ((thisframe)->frame, wordsize);
c906108c 1343
7a78ae4e
ND
1344 lr = read_register (LR_REGNUM);
1345 if (lr == entry_point_address ())
1346 if (fp != 0 && (fpp = read_memory_addr (fp, wordsize)) != 0)
1347 if (PC_IN_CALL_DUMMY (lr, fpp, fpp))
1348 return fpp;
1349
1350 return fp;
1351}
1352
1353/* Return the size of register REG when words are WORDSIZE bytes long. If REG
1354 isn't available with that word size, return 0. */
1355
1356static int
1357regsize (const struct reg *reg, int wordsize)
1358{
1359 return wordsize == 8 ? reg->sz64 : reg->sz32;
1360}
1361
1362/* Return the name of register number N, or null if no such register exists
1363 in the current architecture. */
1364
1365static char *
1366rs6000_register_name (int n)
1367{
1368 struct gdbarch_tdep *tdep = TDEP;
1369 const struct reg *reg = tdep->regs + n;
1370
1371 if (!regsize (reg, tdep->wordsize))
1372 return NULL;
1373 return reg->name;
1374}
1375
1376/* Index within `registers' of the first byte of the space for
1377 register N. */
1378
1379static int
1380rs6000_register_byte (int n)
1381{
1382 return TDEP->regoff[n];
1383}
1384
1385/* Return the number of bytes of storage in the actual machine representation
1386 for register N if that register is available, else return 0. */
1387
1388static int
1389rs6000_register_raw_size (int n)
1390{
1391 struct gdbarch_tdep *tdep = TDEP;
1392 const struct reg *reg = tdep->regs + n;
1393 return regsize (reg, tdep->wordsize);
1394}
1395
1396/* Number of bytes of storage in the program's representation
1397 for register N. */
1398
1399static int
1400rs6000_register_virtual_size (int n)
1401{
1402 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (n));
1403}
1404
1405/* Return the GDB type object for the "standard" data type
1406 of data in register N. */
1407
1408static struct type *
1409rs6000_register_virtual_type (n)
1410{
1411 struct gdbarch_tdep *tdep = TDEP;
1412 const struct reg *reg = tdep->regs + n;
1413
1414 return reg->fpr ? builtin_type_double :
1415 regsize (reg, tdep->wordsize) == 8 ? builtin_type_int64 :
1416 builtin_type_int32;
1417}
1418
1419/* For the PowerPC, it appears that the debug info marks float parameters as
1420 floats regardless of whether the function is prototyped, but the actual
1421 values are always passed in as doubles. Tell gdb to always assume that
1422 floats are passed as doubles and then converted in the callee. */
1423
1424static int
1425rs6000_coerce_float_to_double (struct type *formal, struct type *actual)
1426{
1427 return 1;
1428}
1429
1430/* Return whether register N requires conversion when moving from raw format
1431 to virtual format.
1432
1433 The register format for RS/6000 floating point registers is always
1434 double, we need a conversion if the memory format is float. */
1435
1436static int
1437rs6000_register_convertible (int n)
1438{
1439 const struct reg *reg = TDEP->regs + n;
1440 return reg->fpr;
1441}
1442
1443/* Convert data from raw format for register N in buffer FROM
1444 to virtual format with type TYPE in buffer TO. */
1445
1446static void
1447rs6000_register_convert_to_virtual (int n, struct type *type,
1448 char *from, char *to)
1449{
1450 if (TYPE_LENGTH (type) != REGISTER_RAW_SIZE (n))
7a292a7a 1451 {
7a78ae4e
ND
1452 double val = extract_floating (from, REGISTER_RAW_SIZE (n));
1453 store_floating (to, TYPE_LENGTH (type), val);
1454 }
1455 else
1456 memcpy (to, from, REGISTER_RAW_SIZE (n));
1457}
1458
1459/* Convert data from virtual format with type TYPE in buffer FROM
1460 to raw format for register N in buffer TO. */
7a292a7a 1461
7a78ae4e
ND
1462static void
1463rs6000_register_convert_to_raw (struct type *type, int n,
1464 char *from, char *to)
1465{
1466 if (TYPE_LENGTH (type) != REGISTER_RAW_SIZE (n))
1467 {
1468 double val = extract_floating (from, TYPE_LENGTH (type));
1469 store_floating (to, REGISTER_RAW_SIZE (n), val);
7a292a7a 1470 }
7a78ae4e
ND
1471 else
1472 memcpy (to, from, REGISTER_RAW_SIZE (n));
1473}
c906108c 1474
7a78ae4e
ND
1475/* Store the address of the place in which to copy the structure the
1476 subroutine will return. This is called from call_function.
1477
1478 In RS/6000, struct return addresses are passed as an extra parameter in r3.
1479 In function return, callee is not responsible of returning this address
1480 back. Since gdb needs to find it, we will store in a designated variable
1481 `rs6000_struct_return_address'. */
1482
1483static void
1484rs6000_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1485{
1486 write_register (3, addr);
1487 rs6000_struct_return_address = addr;
1488}
1489
1490/* Write into appropriate registers a function return value
1491 of type TYPE, given in virtual format. */
1492
1493static void
1494rs6000_store_return_value (struct type *type, char *valbuf)
1495{
1496 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1497
1498 /* Floating point values are returned starting from FPR1 and up.
1499 Say a double_double_double type could be returned in
1500 FPR1/FPR2/FPR3 triple. */
1501
1502 write_register_bytes (REGISTER_BYTE (FP0_REGNUM + 1), valbuf,
1503 TYPE_LENGTH (type));
1504 else
1505 /* Everything else is returned in GPR3 and up. */
1506 write_register_bytes (REGISTER_BYTE (GP0_REGNUM + 3), valbuf,
1507 TYPE_LENGTH (type));
1508}
1509
1510/* Extract from an array REGBUF containing the (raw) register state
1511 the address in which a function should return its structure value,
1512 as a CORE_ADDR (or an expression that can be used as one). */
1513
1514static CORE_ADDR
1515rs6000_extract_struct_value_address (char *regbuf)
1516{
1517 return rs6000_struct_return_address;
1518}
1519
1520/* Return whether PC is in a dummy function call.
1521
1522 FIXME: This just checks for the end of the stack, which is broken
1523 for things like stepping through gcc nested function stubs. */
1524
1525static int
1526rs6000_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp)
1527{
1528 return sp < pc && pc < fp;
1529}
1530
1531/* Hook called when a new child process is started. */
1532
1533void
1534rs6000_create_inferior (int pid)
1535{
1536 if (rs6000_set_host_arch_hook)
1537 rs6000_set_host_arch_hook (pid);
c906108c
SS
1538}
1539\f
7a78ae4e
ND
1540/* Support for CONVERT_FROM_FUNC_PTR_ADDR(ADDR).
1541
1542 Usually a function pointer's representation is simply the address
1543 of the function. On the RS/6000 however, a function pointer is
1544 represented by a pointer to a TOC entry. This TOC entry contains
1545 three words, the first word is the address of the function, the
1546 second word is the TOC pointer (r2), and the third word is the
1547 static chain value. Throughout GDB it is currently assumed that a
1548 function pointer contains the address of the function, which is not
1549 easy to fix. In addition, the conversion of a function address to
1550 a function pointer would require allocation of a TOC entry in the
1551 inferior's memory space, with all its drawbacks. To be able to
1552 call C++ virtual methods in the inferior (which are called via
1553 function pointers), find_function_addr uses this macro to get the
1554 function address from a function pointer. */
1555
c906108c
SS
1556/* Return nonzero if ADDR (a function pointer) is in the data space and
1557 is therefore a special function pointer. */
1558
7a78ae4e
ND
1559CORE_ADDR
1560rs6000_convert_from_func_ptr_addr (CORE_ADDR addr)
c906108c
SS
1561{
1562 struct obj_section *s;
1563
1564 s = find_pc_section (addr);
1565 if (s && s->the_bfd_section->flags & SEC_CODE)
7a78ae4e 1566 return addr;
c906108c 1567
7a78ae4e
ND
1568 /* ADDR is in the data space, so it's a special function pointer. */
1569 return read_memory_addr (addr, TDEP->wordsize);
c906108c 1570}
c906108c 1571\f
c5aa993b 1572
7a78ae4e 1573/* Handling the various POWER/PowerPC variants. */
c906108c
SS
1574
1575
7a78ae4e
ND
1576/* The arrays here called registers_MUMBLE hold information about available
1577 registers.
c906108c
SS
1578
1579 For each family of PPC variants, I've tried to isolate out the
1580 common registers and put them up front, so that as long as you get
1581 the general family right, GDB will correctly identify the registers
1582 common to that family. The common register sets are:
1583
1584 For the 60x family: hid0 hid1 iabr dabr pir
1585
1586 For the 505 and 860 family: eie eid nri
1587
1588 For the 403 and 403GC: icdbdr esr dear evpr cdbcr tsr tcr pit tbhi
c5aa993b
JM
1589 tblo srr2 srr3 dbsr dbcr iac1 iac2 dac1 dac2 dccr iccr pbl1
1590 pbu1 pbl2 pbu2
c906108c
SS
1591
1592 Most of these register groups aren't anything formal. I arrived at
1593 them by looking at the registers that occurred in more than one
7a78ae4e
ND
1594 processor. */
1595
1596/* Convenience macros for populating register arrays. */
1597
1598/* Within another macro, convert S to a string. */
1599
1600#define STR(s) #s
1601
1602/* Return a struct reg defining register NAME that's 32 bits on 32-bit systems
1603 and 64 bits on 64-bit systems. */
1604#define R(name) { STR(name), 4, 8, 0 }
1605
1606/* Return a struct reg defining register NAME that's 32 bits on all
1607 systems. */
1608#define R4(name) { STR(name), 4, 4, 0 }
1609
1610/* Return a struct reg defining register NAME that's 64 bits on all
1611 systems. */
1612#define R8(name) { STR(name), 8, 8, 0 }
1613
1614/* Return a struct reg defining floating-point register NAME. */
1615#define F(name) { STR(name), 8, 8, 1 }
1616
1617/* Return a struct reg defining register NAME that's 32 bits on 32-bit
1618 systems and that doesn't exist on 64-bit systems. */
1619#define R32(name) { STR(name), 4, 0, 0 }
1620
1621/* Return a struct reg defining register NAME that's 64 bits on 64-bit
1622 systems and that doesn't exist on 32-bit systems. */
1623#define R64(name) { STR(name), 0, 8, 0 }
1624
1625/* Return a struct reg placeholder for a register that doesn't exist. */
1626#define R0 { 0, 0, 0, 0 }
1627
1628/* UISA registers common across all architectures, including POWER. */
1629
1630#define COMMON_UISA_REGS \
1631 /* 0 */ R(r0), R(r1), R(r2), R(r3), R(r4), R(r5), R(r6), R(r7), \
1632 /* 8 */ R(r8), R(r9), R(r10),R(r11),R(r12),R(r13),R(r14),R(r15), \
1633 /* 16 */ R(r16),R(r17),R(r18),R(r19),R(r20),R(r21),R(r22),R(r23), \
1634 /* 24 */ R(r24),R(r25),R(r26),R(r27),R(r28),R(r29),R(r30),R(r31), \
1635 /* 32 */ F(f0), F(f1), F(f2), F(f3), F(f4), F(f5), F(f6), F(f7), \
1636 /* 40 */ F(f8), F(f9), F(f10),F(f11),F(f12),F(f13),F(f14),F(f15), \
1637 /* 48 */ F(f16),F(f17),F(f18),F(f19),F(f20),F(f21),F(f22),F(f23), \
1638 /* 56 */ F(f24),F(f25),F(f26),F(f27),F(f28),F(f29),F(f30),F(f31), \
1639 /* 64 */ R(pc), R(ps)
1640
1641/* UISA-level SPRs for PowerPC. */
1642#define PPC_UISA_SPRS \
1643 /* 66 */ R4(cr), R(lr), R(ctr), R4(xer), R0
1644
1645/* Segment registers, for PowerPC. */
1646#define PPC_SEGMENT_REGS \
1647 /* 71 */ R32(sr0), R32(sr1), R32(sr2), R32(sr3), \
1648 /* 75 */ R32(sr4), R32(sr5), R32(sr6), R32(sr7), \
1649 /* 79 */ R32(sr8), R32(sr9), R32(sr10), R32(sr11), \
1650 /* 83 */ R32(sr12), R32(sr13), R32(sr14), R32(sr15)
1651
1652/* OEA SPRs for PowerPC. */
1653#define PPC_OEA_SPRS \
1654 /* 87 */ R4(pvr), \
1655 /* 88 */ R(ibat0u), R(ibat0l), R(ibat1u), R(ibat1l), \
1656 /* 92 */ R(ibat2u), R(ibat2l), R(ibat3u), R(ibat3l), \
1657 /* 96 */ R(dbat0u), R(dbat0l), R(dbat1u), R(dbat1l), \
1658 /* 100 */ R(dbat2u), R(dbat2l), R(dbat3u), R(dbat3l), \
1659 /* 104 */ R(sdr1), R64(asr), R(dar), R4(dsisr), \
1660 /* 108 */ R(sprg0), R(sprg1), R(sprg2), R(sprg3), \
1661 /* 112 */ R(srr0), R(srr1), R(tbl), R(tbu), \
1662 /* 116 */ R4(dec), R(dabr), R4(ear)
1663
1664/* IBM POWER (pre-PowerPC) architecture, user-level view. We only cover
1665 user-level SPR's. */
1666static const struct reg registers_power[] =
c906108c 1667{
7a78ae4e
ND
1668 COMMON_UISA_REGS,
1669 /* 66 */ R4(cnd), R(lr), R(cnt), R4(xer), R4(mq)
c906108c
SS
1670};
1671
7a78ae4e
ND
1672/* PowerPC UISA - a PPC processor as viewed by user-level code. A UISA-only
1673 view of the PowerPC. */
1674static const struct reg registers_powerpc[] =
c906108c 1675{
7a78ae4e
ND
1676 COMMON_UISA_REGS,
1677 PPC_UISA_SPRS
c906108c
SS
1678};
1679
7a78ae4e
ND
1680/* IBM PowerPC 403. */
1681static const struct reg registers_403[] =
c5aa993b 1682{
7a78ae4e
ND
1683 COMMON_UISA_REGS,
1684 PPC_UISA_SPRS,
1685 PPC_SEGMENT_REGS,
1686 PPC_OEA_SPRS,
1687 /* 119 */ R(icdbdr), R(esr), R(dear), R(evpr),
1688 /* 123 */ R(cdbcr), R(tsr), R(tcr), R(pit),
1689 /* 127 */ R(tbhi), R(tblo), R(srr2), R(srr3),
1690 /* 131 */ R(dbsr), R(dbcr), R(iac1), R(iac2),
1691 /* 135 */ R(dac1), R(dac2), R(dccr), R(iccr),
1692 /* 139 */ R(pbl1), R(pbu1), R(pbl2), R(pbu2)
c906108c
SS
1693};
1694
7a78ae4e
ND
1695/* IBM PowerPC 403GC. */
1696static const struct reg registers_403GC[] =
c5aa993b 1697{
7a78ae4e
ND
1698 COMMON_UISA_REGS,
1699 PPC_UISA_SPRS,
1700 PPC_SEGMENT_REGS,
1701 PPC_OEA_SPRS,
1702 /* 119 */ R(icdbdr), R(esr), R(dear), R(evpr),
1703 /* 123 */ R(cdbcr), R(tsr), R(tcr), R(pit),
1704 /* 127 */ R(tbhi), R(tblo), R(srr2), R(srr3),
1705 /* 131 */ R(dbsr), R(dbcr), R(iac1), R(iac2),
1706 /* 135 */ R(dac1), R(dac2), R(dccr), R(iccr),
1707 /* 139 */ R(pbl1), R(pbu1), R(pbl2), R(pbu2),
1708 /* 143 */ R(zpr), R(pid), R(sgr), R(dcwr),
1709 /* 147 */ R(tbhu), R(tblu)
c906108c
SS
1710};
1711
7a78ae4e
ND
1712/* Motorola PowerPC 505. */
1713static const struct reg registers_505[] =
c5aa993b 1714{
7a78ae4e
ND
1715 COMMON_UISA_REGS,
1716 PPC_UISA_SPRS,
1717 PPC_SEGMENT_REGS,
1718 PPC_OEA_SPRS,
1719 /* 119 */ R(eie), R(eid), R(nri)
c906108c
SS
1720};
1721
7a78ae4e
ND
1722/* Motorola PowerPC 860 or 850. */
1723static const struct reg registers_860[] =
c5aa993b 1724{
7a78ae4e
ND
1725 COMMON_UISA_REGS,
1726 PPC_UISA_SPRS,
1727 PPC_SEGMENT_REGS,
1728 PPC_OEA_SPRS,
1729 /* 119 */ R(eie), R(eid), R(nri), R(cmpa),
1730 /* 123 */ R(cmpb), R(cmpc), R(cmpd), R(icr),
1731 /* 127 */ R(der), R(counta), R(countb), R(cmpe),
1732 /* 131 */ R(cmpf), R(cmpg), R(cmph), R(lctrl1),
1733 /* 135 */ R(lctrl2), R(ictrl), R(bar), R(ic_cst),
1734 /* 139 */ R(ic_adr), R(ic_dat), R(dc_cst), R(dc_adr),
1735 /* 143 */ R(dc_dat), R(dpdr), R(dpir), R(immr),
1736 /* 147 */ R(mi_ctr), R(mi_ap), R(mi_epn), R(mi_twc),
1737 /* 151 */ R(mi_rpn), R(md_ctr), R(m_casid), R(md_ap),
1738 /* 155 */ R(md_epn), R(md_twb), R(md_twc), R(md_rpn),
1739 /* 159 */ R(m_tw), R(mi_dbcam), R(mi_dbram0), R(mi_dbram1),
1740 /* 163 */ R(md_dbcam), R(md_dbram0), R(md_dbram1)
c906108c
SS
1741};
1742
7a78ae4e
ND
1743/* Motorola PowerPC 601. Note that the 601 has different register numbers
1744 for reading and writing RTCU and RTCL. However, how one reads and writes a
c906108c 1745 register is the stub's problem. */
7a78ae4e 1746static const struct reg registers_601[] =
c5aa993b 1747{
7a78ae4e
ND
1748 COMMON_UISA_REGS,
1749 PPC_UISA_SPRS,
1750 PPC_SEGMENT_REGS,
1751 PPC_OEA_SPRS,
1752 /* 119 */ R(hid0), R(hid1), R(iabr), R(dabr),
1753 /* 123 */ R(pir), R(mq), R(rtcu), R(rtcl)
c906108c
SS
1754};
1755
7a78ae4e
ND
1756/* Motorola PowerPC 602. */
1757static const struct reg registers_602[] =
c5aa993b 1758{
7a78ae4e
ND
1759 COMMON_UISA_REGS,
1760 PPC_UISA_SPRS,
1761 PPC_SEGMENT_REGS,
1762 PPC_OEA_SPRS,
1763 /* 119 */ R(hid0), R(hid1), R(iabr), R0,
1764 /* 123 */ R0, R(tcr), R(ibr), R(esassr),
1765 /* 127 */ R(sebr), R(ser), R(sp), R(lt)
c906108c
SS
1766};
1767
7a78ae4e
ND
1768/* Motorola/IBM PowerPC 603 or 603e. */
1769static const struct reg registers_603[] =
c5aa993b 1770{
7a78ae4e
ND
1771 COMMON_UISA_REGS,
1772 PPC_UISA_SPRS,
1773 PPC_SEGMENT_REGS,
1774 PPC_OEA_SPRS,
1775 /* 119 */ R(hid0), R(hid1), R(iabr), R0,
1776 /* 123 */ R0, R(dmiss), R(dcmp), R(hash1),
1777 /* 127 */ R(hash2), R(imiss), R(icmp), R(rpa)
c906108c
SS
1778};
1779
7a78ae4e
ND
1780/* Motorola PowerPC 604 or 604e. */
1781static const struct reg registers_604[] =
c5aa993b 1782{
7a78ae4e
ND
1783 COMMON_UISA_REGS,
1784 PPC_UISA_SPRS,
1785 PPC_SEGMENT_REGS,
1786 PPC_OEA_SPRS,
1787 /* 119 */ R(hid0), R(hid1), R(iabr), R(dabr),
1788 /* 123 */ R(pir), R(mmcr0), R(pmc1), R(pmc2),
1789 /* 127 */ R(sia), R(sda)
c906108c
SS
1790};
1791
7a78ae4e
ND
1792/* Motorola/IBM PowerPC 750 or 740. */
1793static const struct reg registers_750[] =
c5aa993b 1794{
7a78ae4e
ND
1795 COMMON_UISA_REGS,
1796 PPC_UISA_SPRS,
1797 PPC_SEGMENT_REGS,
1798 PPC_OEA_SPRS,
1799 /* 119 */ R(hid0), R(hid1), R(iabr), R(dabr),
1800 /* 123 */ R0, R(ummcr0), R(upmc1), R(upmc2),
1801 /* 127 */ R(usia), R(ummcr1), R(upmc3), R(upmc4),
1802 /* 131 */ R(mmcr0), R(pmc1), R(pmc2), R(sia),
1803 /* 135 */ R(mmcr1), R(pmc3), R(pmc4), R(l2cr),
1804 /* 139 */ R(ictc), R(thrm1), R(thrm2), R(thrm3)
c906108c
SS
1805};
1806
1807
1808/* Information about a particular processor variant. */
7a78ae4e 1809
c906108c 1810struct variant
c5aa993b
JM
1811 {
1812 /* Name of this variant. */
1813 char *name;
c906108c 1814
c5aa993b
JM
1815 /* English description of the variant. */
1816 char *description;
c906108c 1817
7a78ae4e
ND
1818 /* bfd_arch_info.arch corresponding to variant. */
1819 enum bfd_architecture arch;
1820
1821 /* bfd_arch_info.mach corresponding to variant. */
1822 unsigned long mach;
1823
c5aa993b
JM
1824 /* Table of register names; registers[R] is the name of the register
1825 number R. */
7a78ae4e
ND
1826 int nregs;
1827 const struct reg *regs;
c5aa993b 1828 };
c906108c
SS
1829
1830#define num_registers(list) (sizeof (list) / sizeof((list)[0]))
1831
1832
1833/* Information in this table comes from the following web sites:
1834 IBM: http://www.chips.ibm.com:80/products/embedded/
1835 Motorola: http://www.mot.com/SPS/PowerPC/
1836
1837 I'm sure I've got some of the variant descriptions not quite right.
1838 Please report any inaccuracies you find to GDB's maintainer.
1839
1840 If you add entries to this table, please be sure to allow the new
1841 value as an argument to the --with-cpu flag, in configure.in. */
1842
7a78ae4e 1843static const struct variant variants[] =
c906108c 1844{
7a78ae4e
ND
1845 {"powerpc", "PowerPC user-level", bfd_arch_powerpc,
1846 bfd_mach_ppc, num_registers (registers_powerpc), registers_powerpc},
1847 {"power", "POWER user-level", bfd_arch_rs6000,
1848 bfd_mach_rs6k, num_registers (registers_power), registers_power},
1849 {"403", "IBM PowerPC 403", bfd_arch_powerpc,
1850 bfd_mach_ppc_403, num_registers (registers_403), registers_403},
1851 {"601", "Motorola PowerPC 601", bfd_arch_powerpc,
1852 bfd_mach_ppc_601, num_registers (registers_601), registers_601},
1853 {"602", "Motorola PowerPC 602", bfd_arch_powerpc,
1854 bfd_mach_ppc_602, num_registers (registers_602), registers_602},
1855 {"603", "Motorola/IBM PowerPC 603 or 603e", bfd_arch_powerpc,
1856 bfd_mach_ppc_603, num_registers (registers_603), registers_603},
1857 {"604", "Motorola PowerPC 604 or 604e", bfd_arch_powerpc,
1858 604, num_registers (registers_604), registers_604},
1859 {"403GC", "IBM PowerPC 403GC", bfd_arch_powerpc,
1860 bfd_mach_ppc_403gc, num_registers (registers_403GC), registers_403GC},
1861 {"505", "Motorola PowerPC 505", bfd_arch_powerpc,
1862 bfd_mach_ppc_505, num_registers (registers_505), registers_505},
1863 {"860", "Motorola PowerPC 860 or 850", bfd_arch_powerpc,
1864 bfd_mach_ppc_860, num_registers (registers_860), registers_860},
1865 {"750", "Motorola/IBM PowerPC 750 or 740", bfd_arch_powerpc,
1866 bfd_mach_ppc_750, num_registers (registers_750), registers_750},
1867
1868 /* FIXME: I haven't checked the register sets of the following. */
1869 {"620", "Motorola PowerPC 620", bfd_arch_powerpc,
1870 bfd_mach_ppc_620, num_registers (registers_powerpc), registers_powerpc},
1871 {"a35", "PowerPC A35", bfd_arch_powerpc,
1872 bfd_mach_ppc_a35, num_registers (registers_powerpc), registers_powerpc},
1873 {"rs1", "IBM POWER RS1", bfd_arch_rs6000,
1874 bfd_mach_rs6k_rs1, num_registers (registers_power), registers_power},
1875 {"rsc", "IBM POWER RSC", bfd_arch_rs6000,
1876 bfd_mach_rs6k_rsc, num_registers (registers_power), registers_power},
1877 {"rs2", "IBM POWER RS2", bfd_arch_rs6000,
1878 bfd_mach_rs6k_rs2, num_registers (registers_power), registers_power},
1879
c5aa993b 1880 {0, 0, 0, 0}
c906108c
SS
1881};
1882
7a78ae4e 1883#undef num_registers
c906108c 1884
7a78ae4e
ND
1885/* Look up the variant named NAME in the `variants' table. Return a
1886 pointer to the struct variant, or null if we couldn't find it. */
c906108c 1887
7a78ae4e
ND
1888static const struct variant *
1889find_variant_by_name (char *name)
c906108c 1890{
7a78ae4e 1891 const struct variant *v;
c906108c 1892
7a78ae4e
ND
1893 for (v = variants; v->name; v++)
1894 if (!strcmp (name, v->name))
1895 return v;
c906108c 1896
7a78ae4e 1897 return NULL;
c906108c
SS
1898}
1899
7a78ae4e
ND
1900/* Return the variant corresponding to architecture ARCH and machine number
1901 MACH. If no such variant exists, return null. */
c906108c 1902
7a78ae4e
ND
1903static const struct variant *
1904find_variant_by_arch (enum bfd_architecture arch, unsigned long mach)
c906108c 1905{
7a78ae4e 1906 const struct variant *v;
c5aa993b 1907
7a78ae4e
ND
1908 for (v = variants; v->name; v++)
1909 if (arch == v->arch && mach == v->mach)
1910 return v;
c906108c 1911
7a78ae4e 1912 return NULL;
c906108c
SS
1913}
1914
7a78ae4e 1915\f
c906108c 1916
7a78ae4e
ND
1917/* Initialize the current architecture based on INFO. If possible, re-use an
1918 architecture from ARCHES, which is a list of architectures already created
1919 during this debugging session.
c906108c 1920
7a78ae4e
ND
1921 Called e.g. at program startup, when reading a core file, and when reading
1922 a binary file. */
c906108c 1923
7a78ae4e
ND
1924static struct gdbarch *
1925rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1926{
1927 struct gdbarch *gdbarch;
1928 struct gdbarch_tdep *tdep;
1929 int wordsize, fromexec, power, i, off;
1930 struct reg *regs;
1931 const struct variant *v;
1932 enum bfd_architecture arch;
1933 unsigned long mach;
1934 bfd abfd;
1935
1936 fromexec = info.abfd && info.abfd->format == bfd_object &&
1937 bfd_get_flavour (info.abfd) == bfd_target_xcoff_flavour;
1938
1939 /* Check word size. If INFO is from a binary file, infer it from that,
1940 else use the previously-inferred size. */
1941 if (fromexec)
c906108c 1942 {
7a78ae4e
ND
1943 if (xcoff_data (info.abfd)->xcoff64)
1944 wordsize = 8;
1945 else
1946 wordsize = 4;
c906108c
SS
1947 }
1948 else
7a78ae4e
ND
1949 {
1950 tdep = TDEP;
1951 if (tdep)
1952 wordsize = tdep->wordsize;
1953 else
1954 wordsize = 4;
1955 }
c906108c 1956
7a78ae4e
ND
1957 /* Find a candidate among extant architectures. */
1958 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1959 arches != NULL;
1960 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1961 {
1962 /* Word size in the various PowerPC bfd_arch_info structs isn't
1963 meaningful, because 64-bit CPUs can run in 32-bit mode. So, perform
1964 separate word size check. */
1965 tdep = gdbarch_tdep (arches->gdbarch);
1966 if (tdep && tdep->wordsize == wordsize)
1967 return arches->gdbarch;
1968 }
c906108c 1969
7a78ae4e
ND
1970 /* None found, create a new architecture from INFO, whose bfd_arch_info
1971 validity depends on the source:
1972 - executable useless
1973 - rs6000_host_arch() good
1974 - core file good
1975 - "set arch" trust blindly
1976 - GDB startup useless but harmless */
c906108c 1977
7a78ae4e 1978 if (!fromexec)
c906108c 1979 {
7a78ae4e
ND
1980 arch = info.bfd_architecture;
1981 mach = info.bfd_arch_info->mach;
c906108c 1982 }
7a78ae4e 1983 else
c906108c 1984 {
7a78ae4e
ND
1985 arch = bfd_arch_powerpc;
1986 mach = 0;
1987 bfd_default_set_arch_mach (&abfd, arch, mach);
1988 info.bfd_arch_info = bfd_get_arch_info (&abfd);
1989 }
1990 tdep = xmalloc (sizeof (struct gdbarch_tdep));
1991 tdep->wordsize = wordsize;
1992 gdbarch = gdbarch_alloc (&info, tdep);
1993 power = arch == bfd_arch_rs6000;
1994
1995 /* Select instruction printer. */
1996 tm_print_insn = arch == power ? print_insn_rs6000 :
1997 info.byte_order == BIG_ENDIAN ? print_insn_big_powerpc :
1998 print_insn_little_powerpc;
1999
2000 /* Choose variant. */
2001 v = find_variant_by_arch (arch, mach);
2002 if (!v)
2003 v = find_variant_by_name (power ? "power" : "powerpc");
2004 tdep->regs = v->regs;
2005
2006 /* Calculate byte offsets in raw register array. */
2007 tdep->regoff = xmalloc (v->nregs * sizeof (int));
2008 for (i = off = 0; i < v->nregs; i++)
2009 {
2010 tdep->regoff[i] = off;
2011 off += regsize (v->regs + i, wordsize);
c906108c
SS
2012 }
2013
7a78ae4e
ND
2014 set_gdbarch_read_pc (gdbarch, generic_target_read_pc);
2015 set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
2016 set_gdbarch_read_fp (gdbarch, generic_target_read_fp);
2017 set_gdbarch_write_fp (gdbarch, generic_target_write_fp);
2018 set_gdbarch_read_sp (gdbarch, generic_target_read_sp);
2019 set_gdbarch_write_sp (gdbarch, generic_target_write_sp);
2020
2021 set_gdbarch_num_regs (gdbarch, v->nregs);
2022 set_gdbarch_sp_regnum (gdbarch, 1);
2023 set_gdbarch_fp_regnum (gdbarch, 1);
2024 set_gdbarch_pc_regnum (gdbarch, 64);
2025 set_gdbarch_register_name (gdbarch, rs6000_register_name);
2026 set_gdbarch_register_size (gdbarch, wordsize);
2027 set_gdbarch_register_bytes (gdbarch, off);
2028 set_gdbarch_register_byte (gdbarch, rs6000_register_byte);
2029 set_gdbarch_register_raw_size (gdbarch, rs6000_register_raw_size);
2030 set_gdbarch_max_register_raw_size (gdbarch, 8);
2031 set_gdbarch_register_virtual_size (gdbarch, rs6000_register_virtual_size);
2032 set_gdbarch_max_register_virtual_size (gdbarch, 8);
2033 set_gdbarch_register_virtual_type (gdbarch, rs6000_register_virtual_type);
2034
2035 set_gdbarch_ptr_bit (gdbarch, wordsize * TARGET_CHAR_BIT);
2036 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
2037 set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2038 set_gdbarch_long_bit (gdbarch, wordsize * TARGET_CHAR_BIT);
2039 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2040 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2041 set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2042 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2043
2044 set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
2045 set_gdbarch_call_dummy_length (gdbarch, 0);
2046 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
2047 set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
2048 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
2049 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
2050 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
2051 set_gdbarch_pc_in_call_dummy (gdbarch, rs6000_pc_in_call_dummy);
2052 set_gdbarch_call_dummy_p (gdbarch, 1);
2053 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
2054 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
2055 set_gdbarch_fix_call_dummy (gdbarch, rs6000_fix_call_dummy);
2056 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
2057 set_gdbarch_push_return_address (gdbarch, ppc_push_return_address);
2058 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2059 set_gdbarch_coerce_float_to_double (gdbarch, rs6000_coerce_float_to_double);
2060
2061 set_gdbarch_register_convertible (gdbarch, rs6000_register_convertible);
2062 set_gdbarch_register_convert_to_virtual (gdbarch, rs6000_register_convert_to_virtual);
2063 set_gdbarch_register_convert_to_raw (gdbarch, rs6000_register_convert_to_raw);
2064
2065 set_gdbarch_extract_return_value (gdbarch, rs6000_extract_return_value);
2066 set_gdbarch_push_arguments (gdbarch, rs6000_push_arguments);
2067
2068 set_gdbarch_store_struct_return (gdbarch, rs6000_store_struct_return);
2069 set_gdbarch_store_return_value (gdbarch, rs6000_store_return_value);
2070 set_gdbarch_extract_struct_value_address (gdbarch, rs6000_extract_struct_value_address);
2071 set_gdbarch_use_struct_convention (gdbarch, generic_use_struct_convention);
2072
2073 set_gdbarch_frame_init_saved_regs (gdbarch, rs6000_frame_init_saved_regs);
2074 set_gdbarch_init_extra_frame_info (gdbarch, rs6000_init_extra_frame_info);
2075
2076 set_gdbarch_pop_frame (gdbarch, rs6000_pop_frame);
2077
2078 set_gdbarch_skip_prologue (gdbarch, rs6000_skip_prologue);
2079 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2080 set_gdbarch_decr_pc_after_break (gdbarch, 0);
2081 set_gdbarch_function_start_offset (gdbarch, 0);
2082 set_gdbarch_breakpoint_from_pc (gdbarch, rs6000_breakpoint_from_pc);
2083
2084 /* Not sure on this. FIXMEmgo */
2085 set_gdbarch_frame_args_skip (gdbarch, 8);
2086
2087 set_gdbarch_frameless_function_invocation (gdbarch, rs6000_frameless_function_invocation);
2088 set_gdbarch_frame_chain (gdbarch, rs6000_frame_chain);
2089 set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid);
2090 set_gdbarch_frame_saved_pc (gdbarch, rs6000_frame_saved_pc);
2091 set_gdbarch_frame_args_address (gdbarch, rs6000_frame_args_address);
2092 set_gdbarch_frame_locals_address (gdbarch, rs6000_frame_args_address);
2093 set_gdbarch_saved_pc_after_call (gdbarch, rs6000_saved_pc_after_call);
2094
2095 /* We can't tell how many args there are
2096 now that the C compiler delays popping them. */
2097 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
2098
2099 return gdbarch;
c906108c
SS
2100}
2101
c906108c
SS
2102/* Initialization code. */
2103
2104void
2105_initialize_rs6000_tdep ()
2106{
7a78ae4e
ND
2107 register_gdbarch_init (bfd_arch_rs6000, rs6000_gdbarch_init);
2108 register_gdbarch_init (bfd_arch_powerpc, rs6000_gdbarch_init);
c906108c 2109}
This page took 0.245701 seconds and 4 git commands to generate.