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 /* Structure to hold cached info about function prologues. */
33 CORE_ADDR pc; /* First addr after fn prologue */
34 unsigned rsize, msize; /* register stack frame size, mem stack ditto */
35 unsigned mfp_used : 1; /* memory frame pointer used */
36 unsigned rsize_valid : 1; /* Validity bits for the above */
37 unsigned msize_valid : 1;
38 unsigned mfp_valid : 1;
41 /* Examine the prologue of a function which starts at PC. Return
42 the first addess past the prologue. If MSIZE is non-NULL, then
43 set *MSIZE to the memory stack frame size. If RSIZE is non-NULL,
44 then set *RSIZE to the register stack frame size (not including
45 incoming arguments and the return address & frame pointer stored
46 with them). If no prologue is found, *RSIZE is set to zero.
47 If no prologue is found, or a prologue which doesn't involve
48 allocating a memory stack frame, then set *MSIZE to zero.
50 Note that both msize and rsize are in bytes. This is not consistent
51 with the _User's Manual_ with respect to rsize, but it is much more
54 If MFP_USED is non-NULL, *MFP_USED is set to nonzero if a memory
55 frame pointer is being used. */
57 examine_prologue (pc, rsize, msize, mfp_used)
65 int misc_index = find_pc_misc_function (pc);
66 struct prologue_info *mi = 0;
69 mi = (struct prologue_info *)misc_function_vector[misc_index].misc_info;
77 valid &= mi->rsize_valid;
82 valid &= mi->msize_valid;
86 *mfp_used = mi->mfp_used;
87 valid &= mi->mfp_valid;
100 /* Prologue must start with subtracting a constant from gr1.
101 Normally this is sub gr1,gr1,<rsize * 4>. */
102 insn = read_memory_integer (p, 4);
103 if ((insn & 0xffffff00) != 0x25010100)
105 /* If the frame is large, instead of a single instruction it
106 might be a pair of instructions:
107 const <reg>, <rsize * 4>
111 /* Possible value for rsize. */
114 if ((insn & 0xff000000) != 0x03000000)
119 reg = (insn >> 8) & 0xff;
120 rsize0 = (((insn >> 8) & 0xff00) | (insn & 0xff));
122 insn = read_memory_integer (p, 4);
123 if ((insn & 0xffffff00) != 0x24010100
124 || (insn & 0xff) != reg)
135 *rsize = (insn & 0xff);
139 /* Next instruction must be asgeu V_SPILL,gr1,rab. */
140 insn = read_memory_integer (p, 4);
141 if (insn != 0x5e40017e)
148 /* Next instruction usually sets the frame pointer (lr1) by adding
149 <size * 4> from gr1. However, this can (and high C does) be
150 deferred until anytime before the first function call. So it is
151 OK if we don't see anything which sets lr1. */
152 /* Normally this is just add lr1,gr1,<size * 4>. */
153 insn = read_memory_integer (p, 4);
154 if ((insn & 0xffffff00) == 0x15810100)
158 /* However, for large frames it can be
159 const <reg>, <size *4>
165 if ((insn & 0xff000000) == 0x03000000)
167 reg = (insn >> 8) & 0xff;
169 insn = read_memory_integer (q, 4);
170 if ((insn & 0xffffff00) == 0x14810100
171 && (insn & 0xff) == reg)
176 /* Next comes "add lr{<rsize-1>},msp,0", but only if a memory
177 frame pointer is in use. We just check for add lr<anything>,msp,0;
178 we don't check this rsize against the first instruction, and
179 we don't check that the trace-back tag indicates a memory frame pointer
182 The recommended instruction is actually "sll lr<whatever>,msp,0".
183 We check for that, too. Originally Jim Kingdon's code seemed
184 to be looking for a "sub" instruction here, but the mask was set
185 up to lose all the time. */
186 insn = read_memory_integer (p, 4);
187 if (((insn & 0xff80ffff) == 0x15807d00) /* add */
188 || ((insn & 0xff80ffff) == 0x81807d00) ) /* sll */
191 if (mfp_used != NULL)
195 /* Next comes a subtraction from msp to allocate a memory frame,
196 but only if a memory frame is
197 being used. We don't check msize against the trace-back tag.
199 Normally this is just
202 insn = read_memory_integer (p, 4);
203 if ((insn & 0xffffff00) == 0x257d7d00)
207 *msize = insn & 0xff;
211 /* For large frames, instead of a single instruction it might
215 consth <reg>, <msize> ; optional
222 if ((insn & 0xff000000) == 0x03000000)
224 reg = (insn >> 8) & 0xff;
225 msize0 = ((insn >> 8) & 0xff00) | (insn & 0xff);
227 insn = read_memory_integer (q, 4);
228 /* Check for consth. */
229 if ((insn & 0xff000000) == 0x02000000
230 && (insn & 0x0000ff00) == reg)
232 msize0 |= (insn << 8) & 0xff000000;
233 msize0 |= (insn << 16) & 0x00ff0000;
235 insn = read_memory_integer (q, 4);
237 /* Check for sub msp,msp,<reg>. */
238 if ((insn & 0xffffff00) == 0x247d7d00
239 && (insn & 0xff) == reg)
253 /* Add a new cache entry. */
254 mi = (struct prologue_info *)xmalloc (sizeof (struct prologue_info));
255 misc_function_vector[misc_index].misc_info = (char *)mi;
260 /* else, cache entry exists, but info is incomplete. */
272 if (mfp_used != NULL)
274 mi->mfp_used = *mfp_used;
281 /* Advance PC across any function entry prologue instructions
282 to reach some "real" code. */
288 return examine_prologue (pc, (unsigned *)NULL, (unsigned *)NULL,
292 /* Initialize the frame. In addition to setting "extra" frame info,
293 we also set ->frame because we use it in a nonstandard way, and ->pc
294 because we need to know it to get the other stuff. See the diagram
295 of stacks and the frame cache in tm-29k.h for more detail. */
297 init_frame_info (innermost_frame, fci)
299 struct frame_info *fci;
311 fci->frame = read_register (GR1_REGNUM);
313 fci->frame = fci->next_frame + fci->next->rsize;
315 #if CALL_DUMMY_LOCATION == ON_STACK
318 if (PC_IN_CALL_DUMMY (p, 0, 0))
321 fci->rsize = DUMMY_FRAME_RSIZE;
322 /* This doesn't matter since we never try to get locals or args
323 from a dummy frame. */
325 /* Dummy frames always use a memory frame pointer. */
327 read_register_stack_integer (fci->frame + DUMMY_FRAME_RSIZE - 4, 4);
331 func = find_pc_function (p);
333 p = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
336 /* Search backward to find the trace-back tag. However,
337 do not trace back beyond the start of the text segment
338 (just as a sanity check to avoid going into never-never land). */
339 while (p >= text_start
340 && ((insn = read_memory_integer (p, 4)) & 0xff000000) != 0)
345 /* Couldn't find the trace-back tag.
346 Something strange is going on. */
353 /* Advance to the first word of the function, i.e. the word
354 after the trace-back tag. */
357 /* We've found the start of the function. Since High C interchanges
358 the meanings of bits 23 and 22 (as of Jul 90), and we
359 need to look at the prologue anyway to figure out
360 what rsize is, ignore the contents of the trace-back tag. */
361 examine_prologue (p, &rsize, &msize, &mfp_used);
366 fci->saved_msp = read_register (MSP_REGNUM) + msize;
372 read_register_stack_integer (fci->frame + rsize - 1, 4);
374 fci->saved_msp = fci->next->saved_msp + msize;
379 init_extra_frame_info (fci)
380 struct frame_info *fci;
383 /* Assume innermost frame. May produce strange results for "info frame"
384 but there isn't any way to tell the difference. */
385 init_frame_info (1, fci);
387 /* We're in get_prev_frame_info.
388 Take care of everything in init_frame_pc. */
393 init_frame_pc (fromleaf, fci)
395 struct frame_info *fci;
397 fci->pc = (fromleaf ? SAVED_PC_AFTER_CALL (fci->next) :
398 fci->next ? FRAME_SAVED_PC (fci->next) : read_pc ());
399 init_frame_info (0, fci);
402 /* Local variables (i.e. LOC_LOCAL) are on the memory stack, with their
403 offsets being relative to the memory stack pointer (high C) or
407 frame_locals_address (fi)
408 struct frame_info *fi;
410 struct block *b = block_for_pc (fi->pc);
411 /* If compiled without -g, assume GCC. */
412 if (b == NULL || BLOCK_GCC_COMPILED (b))
413 return fi->saved_msp;
415 return fi->saved_msp - fi->msize;
418 /* Routines for reading the register stack. The caller gets to treat
419 the register stack as a uniform stack in memory, from address $gr1
420 straight through $rfb and beyond. */
422 /* Analogous to read_memory except the length is understood to be 4.
423 Also, myaddr can be NULL (meaning don't bother to read), and
424 if actual_mem_addr is non-NULL, store there the address that it
425 was fetched from (or if from a register the offset within
426 registers). Set *LVAL to lval_memory or lval_register, depending
427 on where it came from. */
429 read_register_stack (memaddr, myaddr, actual_mem_addr, lval)
432 CORE_ADDR *actual_mem_addr;
433 enum lval_type *lval;
435 long rfb = read_register (RFB_REGNUM);
436 long rsp = read_register (RSP_REGNUM);
439 /* It's in a register. */
440 int regnum = (memaddr - rsp) / 4 + LR0_REGNUM;
441 if (regnum < LR0_REGNUM || regnum > LR0_REGNUM + 127)
442 error ("Attempt to read register stack out of range.");
444 read_register_gen (regnum, myaddr);
446 *lval = lval_register;
447 if (actual_mem_addr != NULL)
448 *actual_mem_addr = REGISTER_BYTE (regnum);
452 /* It's in the memory portion of the register stack. */
454 read_memory (memaddr, myaddr, 4);
457 if (actual_mem_addr != NULL)
458 *actual_mem_addr == memaddr;
462 /* Analogous to read_memory_integer
463 except the length is understood to be 4. */
465 read_register_stack_integer (memaddr, len)
470 read_register_stack (memaddr, &buf, NULL, NULL);
471 SWAP_TARGET_AND_HOST (&buf, 4);
475 /* Copy 4 bytes from GDB memory at MYADDR into inferior memory
476 at MEMADDR and put the actual address written into in
479 write_register_stack (memaddr, myaddr, actual_mem_addr)
482 CORE_ADDR *actual_mem_addr;
484 long rfb = read_register (RFB_REGNUM);
485 long rsp = read_register (RSP_REGNUM);
488 /* It's in a register. */
489 int regnum = (memaddr - rsp) / 4 + LR0_REGNUM;
490 if (regnum < LR0_REGNUM || regnum > LR0_REGNUM + 127)
491 error ("Attempt to read register stack out of range.");
493 write_register (regnum, *(long *)myaddr);
494 if (actual_mem_addr != NULL)
495 *actual_mem_addr = NULL;
499 /* It's in the memory portion of the register stack. */
501 write_memory (memaddr, myaddr, 4);
502 if (actual_mem_addr != NULL)
503 *actual_mem_addr == memaddr;
507 /* Find register number REGNUM relative to FRAME and put its
508 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
509 was optimized out (and thus can't be fetched). If the variable
510 was fetched from memory, set *ADDRP to where it was fetched from,
511 otherwise it was fetched from a register.
513 The argument RAW_BUFFER must point to aligned memory. */
515 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lvalp)
521 enum lval_type *lvalp;
523 struct frame_info *fi = get_frame_info (frame);
527 /* Once something has a register number, it doesn't get optimized out. */
528 if (optimized != NULL)
530 if (regnum == RSP_REGNUM)
532 if (raw_buffer != NULL)
533 *(CORE_ADDR *)raw_buffer = fi->frame;
538 else if (regnum == PC_REGNUM)
540 if (raw_buffer != NULL)
541 *(CORE_ADDR *)raw_buffer = fi->pc;
543 /* Not sure we have to do this. */
549 else if (regnum == MSP_REGNUM)
551 if (raw_buffer != NULL)
553 if (fi->next != NULL)
554 *(CORE_ADDR *)raw_buffer = fi->next->saved_msp;
556 *(CORE_ADDR *)raw_buffer = read_register (MSP_REGNUM);
558 /* The value may have been computed, not fetched. */
563 else if (regnum < LR0_REGNUM || regnum >= LR0_REGNUM + 128)
565 /* These registers are not saved over procedure calls,
566 so just print out the current values. */
567 if (raw_buffer != NULL)
568 *(CORE_ADDR *)raw_buffer = read_register (regnum);
570 *lvalp = lval_register;
572 *addrp = REGISTER_BYTE (regnum);
576 addr = fi->frame + (regnum - LR0_REGNUM) * 4;
577 if (raw_buffer != NULL)
578 read_register_stack (addr, raw_buffer, &addr, &lval);
585 /* Discard from the stack the innermost frame,
586 restoring all saved registers. */
591 FRAME frame = get_current_frame ();
592 struct frame_info *fi = get_frame_info (frame);
593 CORE_ADDR rfb = read_register (RFB_REGNUM);
594 CORE_ADDR gr1 = fi->frame + fi->rsize;
599 /* If popping a dummy frame, need to restore registers. */
600 if (PC_IN_CALL_DUMMY (read_register (PC_REGNUM),
601 read_register (SP_REGNUM),
604 for (i = 0; i < DUMMY_SAVE_SR128; ++i)
606 (SR_REGNUM (i + 128),
607 read_register (LR0_REGNUM + DUMMY_ARG / 4 + i));
608 for (i = 0; i < DUMMY_SAVE_GR96; ++i)
611 read_register (LR0_REGNUM + DUMMY_ARG / 4 + DUMMY_SAVE_SR128 + i));
614 /* Restore the memory stack pointer. */
615 write_register (MSP_REGNUM, fi->saved_msp);
616 /* Restore the register stack pointer. */
617 write_register (GR1_REGNUM, gr1);
618 /* Check whether we need to fill registers. */
619 lr1 = read_register (LR0_REGNUM + 1);
623 int num_bytes = lr1 - rfb;
626 write_register (RAB_REGNUM, read_register (RAB_REGNUM) + num_bytes);
627 write_register (RFB_REGNUM, lr1);
628 for (i = 0; i < num_bytes; i += 4)
630 /* Note: word is in host byte order. */
631 word = read_memory_integer (rfb + i, 4);
632 write_register (LR0_REGNUM + ((rfb - gr1) % 0x80) + i / 4, word);
635 ret_addr = read_register (LR0_REGNUM);
636 write_register (PC_REGNUM, ret_addr);
637 write_register (NPC_REGNUM, ret_addr + 4);
638 flush_cached_frames ();
639 set_current_frame (create_new_frame (0, read_pc()));
642 /* Push an empty stack frame, to record the current PC, etc. */
649 CORE_ADDR msp = read_register (MSP_REGNUM);
653 write_register (LR0_REGNUM, read_register (PC_REGNUM));
655 /* Allocate the new frame. */
656 gr1 = read_register (GR1_REGNUM) - DUMMY_FRAME_RSIZE;
657 write_register (GR1_REGNUM, gr1);
659 rab = read_register (RAB_REGNUM);
662 /* We need to spill registers. */
663 int num_bytes = rab - gr1;
664 CORE_ADDR rfb = read_register (RFB_REGNUM);
668 write_register (RFB_REGNUM, rfb - num_bytes);
669 write_register (RAB_REGNUM, gr1);
670 for (i = 0; i < num_bytes; i += 4)
672 /* Note: word is in target byte order. */
673 read_register_gen (LR0_REGNUM + i / 4, &word, 4);
674 write_memory (rfb - num_bytes + i, &word, 4);
678 /* There are no arguments in to the dummy frame, so we don't need
679 more than rsize plus the return address and lr1. */
680 write_register (LR0_REGNUM + 1, gr1 + DUMMY_FRAME_RSIZE + 2 * 4);
682 /* Set the memory frame pointer. */
683 write_register (LR0_REGNUM + DUMMY_FRAME_RSIZE / 4 - 1, msp);
685 /* Allocate arg_slop. */
686 write_register (MSP_REGNUM, msp - 16 * 4);
688 /* Save registers. */
689 for (i = 0; i < DUMMY_SAVE_SR128; ++i)
690 write_register (LR0_REGNUM + DUMMY_ARG / 4 + i,
691 read_register (SR_REGNUM (i + 128)));
692 for (i = 0; i < DUMMY_SAVE_GR96; ++i)
693 write_register (LR0_REGNUM + DUMMY_ARG / 4 + DUMMY_SAVE_SR128 + i,
694 read_register (GR96_REGNUM + i));