1 /* Target-machine dependent code for the AMD 29000
2 Copyright (C) 1990 Free Software Foundation, Inc.
3 Contributed by Cygnus Support. Written by Jim Kingdon.
5 This file is part of GDB.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
30 extern CORE_ADDR text_start; /* FIXME, kludge... */
32 /* Structure to hold cached info about function prologues. */
35 CORE_ADDR pc; /* First addr after fn prologue */
36 unsigned rsize, msize; /* register stack frame size, mem stack ditto */
37 unsigned mfp_used : 1; /* memory frame pointer used */
38 unsigned rsize_valid : 1; /* Validity bits for the above */
39 unsigned msize_valid : 1;
40 unsigned mfp_valid : 1;
43 /* Examine the prologue of a function which starts at PC. Return
44 the first addess past the prologue. If MSIZE is non-NULL, then
45 set *MSIZE to the memory stack frame size. If RSIZE is non-NULL,
46 then set *RSIZE to the register stack frame size (not including
47 incoming arguments and the return address & frame pointer stored
48 with them). If no prologue is found, *RSIZE is set to zero.
49 If no prologue is found, or a prologue which doesn't involve
50 allocating a memory stack frame, then set *MSIZE to zero.
52 Note that both msize and rsize are in bytes. This is not consistent
53 with the _User's Manual_ with respect to rsize, but it is much more
56 If MFP_USED is non-NULL, *MFP_USED is set to nonzero if a memory
57 frame pointer is being used. */
59 examine_prologue (pc, rsize, msize, mfp_used)
67 int misc_index = find_pc_misc_function (pc);
68 struct prologue_info *mi = 0;
71 mi = (struct prologue_info *)misc_function_vector[misc_index].misc_info;
79 valid &= mi->rsize_valid;
84 valid &= mi->msize_valid;
88 *mfp_used = mi->mfp_used;
89 valid &= mi->mfp_valid;
102 /* Prologue must start with subtracting a constant from gr1.
103 Normally this is sub gr1,gr1,<rsize * 4>. */
104 insn = read_memory_integer (p, 4);
105 if ((insn & 0xffffff00) != 0x25010100)
107 /* If the frame is large, instead of a single instruction it
108 might be a pair of instructions:
109 const <reg>, <rsize * 4>
113 /* Possible value for rsize. */
116 if ((insn & 0xff000000) != 0x03000000)
121 reg = (insn >> 8) & 0xff;
122 rsize0 = (((insn >> 8) & 0xff00) | (insn & 0xff));
124 insn = read_memory_integer (p, 4);
125 if ((insn & 0xffffff00) != 0x24010100
126 || (insn & 0xff) != reg)
137 *rsize = (insn & 0xff);
141 /* Next instruction must be asgeu V_SPILL,gr1,rab. */
142 insn = read_memory_integer (p, 4);
143 if (insn != 0x5e40017e)
150 /* Next instruction usually sets the frame pointer (lr1) by adding
151 <size * 4> from gr1. However, this can (and high C does) be
152 deferred until anytime before the first function call. So it is
153 OK if we don't see anything which sets lr1. */
154 /* Normally this is just add lr1,gr1,<size * 4>. */
155 insn = read_memory_integer (p, 4);
156 if ((insn & 0xffffff00) == 0x15810100)
160 /* However, for large frames it can be
161 const <reg>, <size *4>
167 if ((insn & 0xff000000) == 0x03000000)
169 reg = (insn >> 8) & 0xff;
171 insn = read_memory_integer (q, 4);
172 if ((insn & 0xffffff00) == 0x14810100
173 && (insn & 0xff) == reg)
178 /* Next comes "add lr{<rsize-1>},msp,0", but only if a memory
179 frame pointer is in use. We just check for add lr<anything>,msp,0;
180 we don't check this rsize against the first instruction, and
181 we don't check that the trace-back tag indicates a memory frame pointer
184 The recommended instruction is actually "sll lr<whatever>,msp,0".
185 We check for that, too. Originally Jim Kingdon's code seemed
186 to be looking for a "sub" instruction here, but the mask was set
187 up to lose all the time. */
188 insn = read_memory_integer (p, 4);
189 if (((insn & 0xff80ffff) == 0x15807d00) /* add */
190 || ((insn & 0xff80ffff) == 0x81807d00) ) /* sll */
193 if (mfp_used != NULL)
197 /* Next comes a subtraction from msp to allocate a memory frame,
198 but only if a memory frame is
199 being used. We don't check msize against the trace-back tag.
201 Normally this is just
204 insn = read_memory_integer (p, 4);
205 if ((insn & 0xffffff00) == 0x257d7d00)
209 *msize = insn & 0xff;
213 /* For large frames, instead of a single instruction it might
217 consth <reg>, <msize> ; optional
224 if ((insn & 0xff000000) == 0x03000000)
226 reg = (insn >> 8) & 0xff;
227 msize0 = ((insn >> 8) & 0xff00) | (insn & 0xff);
229 insn = read_memory_integer (q, 4);
230 /* Check for consth. */
231 if ((insn & 0xff000000) == 0x02000000
232 && (insn & 0x0000ff00) == reg)
234 msize0 |= (insn << 8) & 0xff000000;
235 msize0 |= (insn << 16) & 0x00ff0000;
237 insn = read_memory_integer (q, 4);
239 /* Check for sub msp,msp,<reg>. */
240 if ((insn & 0xffffff00) == 0x247d7d00
241 && (insn & 0xff) == reg)
255 /* Add a new cache entry. */
256 mi = (struct prologue_info *)xmalloc (sizeof (struct prologue_info));
257 misc_function_vector[misc_index].misc_info = (char *)mi;
262 /* else, cache entry exists, but info is incomplete. */
274 if (mfp_used != NULL)
276 mi->mfp_used = *mfp_used;
283 /* Advance PC across any function entry prologue instructions
284 to reach some "real" code. */
290 return examine_prologue (pc, (unsigned *)NULL, (unsigned *)NULL,
294 /* Initialize the frame. In addition to setting "extra" frame info,
295 we also set ->frame because we use it in a nonstandard way, and ->pc
296 because we need to know it to get the other stuff. See the diagram
297 of stacks and the frame cache in tm-29k.h for more detail. */
299 init_frame_info (innermost_frame, fci)
301 struct frame_info *fci;
313 fci->frame = read_register (GR1_REGNUM);
315 fci->frame = fci->next_frame + fci->next->rsize;
317 #if CALL_DUMMY_LOCATION == ON_STACK
320 if (PC_IN_CALL_DUMMY (p, 0, 0))
323 fci->rsize = DUMMY_FRAME_RSIZE;
324 /* This doesn't matter since we never try to get locals or args
325 from a dummy frame. */
327 /* Dummy frames always use a memory frame pointer. */
329 read_register_stack_integer (fci->frame + DUMMY_FRAME_RSIZE - 4, 4);
333 func = find_pc_function (p);
335 p = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
338 /* Search backward to find the trace-back tag. However,
339 do not trace back beyond the start of the text segment
340 (just as a sanity check to avoid going into never-never land). */
341 while (p >= text_start
342 && ((insn = read_memory_integer (p, 4)) & 0xff000000) != 0)
347 /* Couldn't find the trace-back tag.
348 Something strange is going on. */
355 /* Advance to the first word of the function, i.e. the word
356 after the trace-back tag. */
359 /* We've found the start of the function. Since High C interchanges
360 the meanings of bits 23 and 22 (as of Jul 90), and we
361 need to look at the prologue anyway to figure out
362 what rsize is, ignore the contents of the trace-back tag. */
363 examine_prologue (p, &rsize, &msize, &mfp_used);
368 fci->saved_msp = read_register (MSP_REGNUM) + msize;
374 read_register_stack_integer (fci->frame + rsize - 1, 4);
376 fci->saved_msp = fci->next->saved_msp + msize;
381 init_extra_frame_info (fci)
382 struct frame_info *fci;
385 /* Assume innermost frame. May produce strange results for "info frame"
386 but there isn't any way to tell the difference. */
387 init_frame_info (1, fci);
389 /* We're in get_prev_frame_info.
390 Take care of everything in init_frame_pc. */
396 init_frame_pc (fromleaf, fci)
398 struct frame_info *fci;
400 fci->pc = (fromleaf ? SAVED_PC_AFTER_CALL (fci->next) :
401 fci->next ? FRAME_SAVED_PC (fci->next) : read_pc ());
402 init_frame_info (0, fci);
405 /* Local variables (i.e. LOC_LOCAL) are on the memory stack, with their
406 offsets being relative to the memory stack pointer (high C) or
410 frame_locals_address (fi)
411 struct frame_info *fi;
413 struct block *b = block_for_pc (fi->pc);
414 /* If compiled without -g, assume GCC. */
415 if (b == NULL || BLOCK_GCC_COMPILED (b))
416 return fi->saved_msp;
418 return fi->saved_msp - fi->msize;
421 /* Routines for reading the register stack. The caller gets to treat
422 the register stack as a uniform stack in memory, from address $gr1
423 straight through $rfb and beyond. */
425 /* Analogous to read_memory except the length is understood to be 4.
426 Also, myaddr can be NULL (meaning don't bother to read), and
427 if actual_mem_addr is non-NULL, store there the address that it
428 was fetched from (or if from a register the offset within
429 registers). Set *LVAL to lval_memory or lval_register, depending
430 on where it came from. */
432 read_register_stack (memaddr, myaddr, actual_mem_addr, lval)
435 CORE_ADDR *actual_mem_addr;
436 enum lval_type *lval;
438 long rfb = read_register (RFB_REGNUM);
439 long rsp = read_register (RSP_REGNUM);
442 /* It's in a register. */
443 int regnum = (memaddr - rsp) / 4 + LR0_REGNUM;
444 if (regnum < LR0_REGNUM || regnum > LR0_REGNUM + 127)
445 error ("Attempt to read register stack out of range.");
447 read_register_gen (regnum, myaddr);
449 *lval = lval_register;
450 if (actual_mem_addr != NULL)
451 *actual_mem_addr = REGISTER_BYTE (regnum);
455 /* It's in the memory portion of the register stack. */
457 read_memory (memaddr, myaddr, 4);
460 if (actual_mem_addr != NULL)
461 *actual_mem_addr = memaddr;
465 /* Analogous to read_memory_integer
466 except the length is understood to be 4. */
468 read_register_stack_integer (memaddr, len)
473 read_register_stack (memaddr, &buf, NULL, NULL);
474 SWAP_TARGET_AND_HOST (&buf, 4);
478 /* Copy 4 bytes from GDB memory at MYADDR into inferior memory
479 at MEMADDR and put the actual address written into in
482 write_register_stack (memaddr, myaddr, actual_mem_addr)
485 CORE_ADDR *actual_mem_addr;
487 long rfb = read_register (RFB_REGNUM);
488 long rsp = read_register (RSP_REGNUM);
491 /* It's in a register. */
492 int regnum = (memaddr - rsp) / 4 + LR0_REGNUM;
493 if (regnum < LR0_REGNUM || regnum > LR0_REGNUM + 127)
494 error ("Attempt to read register stack out of range.");
496 write_register (regnum, *(long *)myaddr);
497 if (actual_mem_addr != NULL)
498 *actual_mem_addr = NULL;
502 /* It's in the memory portion of the register stack. */
504 write_memory (memaddr, myaddr, 4);
505 if (actual_mem_addr != NULL)
506 *actual_mem_addr = memaddr;
510 /* Find register number REGNUM relative to FRAME and put its
511 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
512 was optimized out (and thus can't be fetched). If the variable
513 was fetched from memory, set *ADDRP to where it was fetched from,
514 otherwise it was fetched from a register.
516 The argument RAW_BUFFER must point to aligned memory. */
518 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lvalp)
524 enum lval_type *lvalp;
526 struct frame_info *fi = get_frame_info (frame);
530 /* Once something has a register number, it doesn't get optimized out. */
531 if (optimized != NULL)
533 if (regnum == RSP_REGNUM)
535 if (raw_buffer != NULL)
536 *(CORE_ADDR *)raw_buffer = fi->frame;
541 else if (regnum == PC_REGNUM)
543 if (raw_buffer != NULL)
544 *(CORE_ADDR *)raw_buffer = fi->pc;
546 /* Not sure we have to do this. */
552 else if (regnum == MSP_REGNUM)
554 if (raw_buffer != NULL)
556 if (fi->next != NULL)
557 *(CORE_ADDR *)raw_buffer = fi->next->saved_msp;
559 *(CORE_ADDR *)raw_buffer = read_register (MSP_REGNUM);
561 /* The value may have been computed, not fetched. */
566 else if (regnum < LR0_REGNUM || regnum >= LR0_REGNUM + 128)
568 /* These registers are not saved over procedure calls,
569 so just print out the current values. */
570 if (raw_buffer != NULL)
571 *(CORE_ADDR *)raw_buffer = read_register (regnum);
573 *lvalp = lval_register;
575 *addrp = REGISTER_BYTE (regnum);
579 addr = fi->frame + (regnum - LR0_REGNUM) * 4;
580 if (raw_buffer != NULL)
581 read_register_stack (addr, raw_buffer, &addr, &lval);
588 /* Discard from the stack the innermost frame,
589 restoring all saved registers. */
594 FRAME frame = get_current_frame ();
595 struct frame_info *fi = get_frame_info (frame);
596 CORE_ADDR rfb = read_register (RFB_REGNUM);
597 CORE_ADDR gr1 = fi->frame + fi->rsize;
602 /* If popping a dummy frame, need to restore registers. */
603 if (PC_IN_CALL_DUMMY (read_register (PC_REGNUM),
604 read_register (SP_REGNUM),
607 for (i = 0; i < DUMMY_SAVE_SR128; ++i)
609 (SR_REGNUM (i + 128),
610 read_register (LR0_REGNUM + DUMMY_ARG / 4 + i));
611 for (i = 0; i < DUMMY_SAVE_GREGS; ++i)
614 read_register (LR0_REGNUM + DUMMY_ARG / 4 + DUMMY_SAVE_SR128 + i));
617 /* Restore the memory stack pointer. */
618 write_register (MSP_REGNUM, fi->saved_msp);
619 /* Restore the register stack pointer. */
620 write_register (GR1_REGNUM, gr1);
621 /* Check whether we need to fill registers. */
622 lr1 = read_register (LR0_REGNUM + 1);
626 int num_bytes = lr1 - rfb;
629 write_register (RAB_REGNUM, read_register (RAB_REGNUM) + num_bytes);
630 write_register (RFB_REGNUM, lr1);
631 for (i = 0; i < num_bytes; i += 4)
633 /* Note: word is in host byte order. */
634 word = read_memory_integer (rfb + i, 4);
635 write_register (LR0_REGNUM + ((rfb - gr1) % 0x80) + i / 4, word);
638 ret_addr = read_register (LR0_REGNUM);
639 write_register (PC_REGNUM, ret_addr);
640 write_register (NPC_REGNUM, ret_addr + 4);
641 flush_cached_frames ();
642 set_current_frame (create_new_frame (0, read_pc()));
645 /* Push an empty stack frame, to record the current PC, etc. */
652 CORE_ADDR msp = read_register (MSP_REGNUM);
656 write_register (LR0_REGNUM, read_register (PC_REGNUM));
658 /* Allocate the new frame. */
659 gr1 = read_register (GR1_REGNUM) - DUMMY_FRAME_RSIZE;
660 write_register (GR1_REGNUM, gr1);
662 rab = read_register (RAB_REGNUM);
665 /* We need to spill registers. */
666 int num_bytes = rab - gr1;
667 CORE_ADDR rfb = read_register (RFB_REGNUM);
671 write_register (RFB_REGNUM, rfb - num_bytes);
672 write_register (RAB_REGNUM, gr1);
673 for (i = 0; i < num_bytes; i += 4)
675 /* Note: word is in target byte order. */
676 read_register_gen (LR0_REGNUM + i / 4, &word, 4);
677 write_memory (rfb - num_bytes + i, &word, 4);
681 /* There are no arguments in to the dummy frame, so we don't need
682 more than rsize plus the return address and lr1. */
683 write_register (LR0_REGNUM + 1, gr1 + DUMMY_FRAME_RSIZE + 2 * 4);
685 /* Set the memory frame pointer. */
686 write_register (LR0_REGNUM + DUMMY_FRAME_RSIZE / 4 - 1, msp);
688 /* Allocate arg_slop. */
689 write_register (MSP_REGNUM, msp - 16 * 4);
691 /* Save registers. */
692 for (i = 0; i < DUMMY_SAVE_SR128; ++i)
693 write_register (LR0_REGNUM + DUMMY_ARG / 4 + i,
694 read_register (SR_REGNUM (i + 128)));
695 for (i = 0; i < DUMMY_SAVE_GREGS; ++i)
696 write_register (LR0_REGNUM + DUMMY_ARG / 4 + DUMMY_SAVE_SR128 + i,
697 read_register (RETURN_REGNUM + i));