1 /* Target-machine dependent code for the AMD 29000
2 Copyright 1990, 1991 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. */
29 extern CORE_ADDR text_start; /* FIXME, kludge... */
31 /* Structure to hold cached info about function prologues. */
34 CORE_ADDR pc; /* First addr after fn prologue */
35 unsigned rsize, msize; /* register stack frame size, mem stack ditto */
36 unsigned mfp_used : 1; /* memory frame pointer used */
37 unsigned rsize_valid : 1; /* Validity bits for the above */
38 unsigned msize_valid : 1;
39 unsigned mfp_valid : 1;
42 /* Examine the prologue of a function which starts at PC. Return
43 the first addess past the prologue. If MSIZE is non-NULL, then
44 set *MSIZE to the memory stack frame size. If RSIZE is non-NULL,
45 then set *RSIZE to the register stack frame size (not including
46 incoming arguments and the return address & frame pointer stored
47 with them). If no prologue is found, *RSIZE is set to zero.
48 If no prologue is found, or a prologue which doesn't involve
49 allocating a memory stack frame, then set *MSIZE to zero.
51 Note that both msize and rsize are in bytes. This is not consistent
52 with the _User's Manual_ with respect to rsize, but it is much more
55 If MFP_USED is non-NULL, *MFP_USED is set to nonzero if a memory
56 frame pointer is being used. */
58 examine_prologue (pc, rsize, msize, mfp_used)
66 int misc_index = find_pc_misc_function (pc);
67 struct prologue_info *mi = 0;
70 mi = (struct prologue_info *)misc_function_vector[misc_index].misc_info;
78 valid &= mi->rsize_valid;
83 valid &= mi->msize_valid;
87 *mfp_used = mi->mfp_used;
88 valid &= mi->mfp_valid;
101 /* Prologue must start with subtracting a constant from gr1.
102 Normally this is sub gr1,gr1,<rsize * 4>. */
103 insn = read_memory_integer (p, 4);
104 if ((insn & 0xffffff00) != 0x25010100)
106 /* If the frame is large, instead of a single instruction it
107 might be a pair of instructions:
108 const <reg>, <rsize * 4>
112 /* Possible value for rsize. */
115 if ((insn & 0xff000000) != 0x03000000)
120 reg = (insn >> 8) & 0xff;
121 rsize0 = (((insn >> 8) & 0xff00) | (insn & 0xff));
123 insn = read_memory_integer (p, 4);
124 if ((insn & 0xffffff00) != 0x24010100
125 || (insn & 0xff) != reg)
136 *rsize = (insn & 0xff);
140 /* Next instruction must be asgeu V_SPILL,gr1,rab. */
141 insn = read_memory_integer (p, 4);
142 if (insn != 0x5e40017e)
149 /* Next instruction usually sets the frame pointer (lr1) by adding
150 <size * 4> from gr1. However, this can (and high C does) be
151 deferred until anytime before the first function call. So it is
152 OK if we don't see anything which sets lr1. */
153 /* Normally this is just add lr1,gr1,<size * 4>. */
154 insn = read_memory_integer (p, 4);
155 if ((insn & 0xffffff00) == 0x15810100)
159 /* However, for large frames it can be
160 const <reg>, <size *4>
166 if ((insn & 0xff000000) == 0x03000000)
168 reg = (insn >> 8) & 0xff;
170 insn = read_memory_integer (q, 4);
171 if ((insn & 0xffffff00) == 0x14810100
172 && (insn & 0xff) == reg)
177 /* Next comes "add lr{<rsize-1>},msp,0", but only if a memory
178 frame pointer is in use. We just check for add lr<anything>,msp,0;
179 we don't check this rsize against the first instruction, and
180 we don't check that the trace-back tag indicates a memory frame pointer
183 The recommended instruction is actually "sll lr<whatever>,msp,0".
184 We check for that, too. Originally Jim Kingdon's code seemed
185 to be looking for a "sub" instruction here, but the mask was set
186 up to lose all the time. */
187 insn = read_memory_integer (p, 4);
188 if (((insn & 0xff80ffff) == 0x15807d00) /* add */
189 || ((insn & 0xff80ffff) == 0x81807d00) ) /* sll */
192 if (mfp_used != NULL)
196 /* Next comes a subtraction from msp to allocate a memory frame,
197 but only if a memory frame is
198 being used. We don't check msize against the trace-back tag.
200 Normally this is just
203 insn = read_memory_integer (p, 4);
204 if ((insn & 0xffffff00) == 0x257d7d00)
208 *msize = insn & 0xff;
212 /* For large frames, instead of a single instruction it might
216 consth <reg>, <msize> ; optional
223 if ((insn & 0xff000000) == 0x03000000)
225 reg = (insn >> 8) & 0xff;
226 msize0 = ((insn >> 8) & 0xff00) | (insn & 0xff);
228 insn = read_memory_integer (q, 4);
229 /* Check for consth. */
230 if ((insn & 0xff000000) == 0x02000000
231 && (insn & 0x0000ff00) == reg)
233 msize0 |= (insn << 8) & 0xff000000;
234 msize0 |= (insn << 16) & 0x00ff0000;
236 insn = read_memory_integer (q, 4);
238 /* Check for sub msp,msp,<reg>. */
239 if ((insn & 0xffffff00) == 0x247d7d00
240 && (insn & 0xff) == reg)
254 /* Add a new cache entry. */
255 mi = (struct prologue_info *)xmalloc (sizeof (struct prologue_info));
256 misc_function_vector[misc_index].misc_info = (char *)mi;
261 /* else, cache entry exists, but info is incomplete. */
273 if (mfp_used != NULL)
275 mi->mfp_used = *mfp_used;
282 /* Advance PC across any function entry prologue instructions
283 to reach some "real" code. */
289 return examine_prologue (pc, (unsigned *)NULL, (unsigned *)NULL,
293 /* Initialize the frame. In addition to setting "extra" frame info,
294 we also set ->frame because we use it in a nonstandard way, and ->pc
295 because we need to know it to get the other stuff. See the diagram
296 of stacks and the frame cache in tm-29k.h for more detail. */
298 init_frame_info (innermost_frame, fci)
300 struct frame_info *fci;
312 fci->frame = read_register (GR1_REGNUM);
314 fci->frame = fci->next_frame + fci->next->rsize;
316 #if CALL_DUMMY_LOCATION == ON_STACK
319 if (PC_IN_CALL_DUMMY (p, 0, 0))
322 fci->rsize = DUMMY_FRAME_RSIZE;
323 /* This doesn't matter since we never try to get locals or args
324 from a dummy frame. */
326 /* Dummy frames always use a memory frame pointer. */
328 read_register_stack_integer (fci->frame + DUMMY_FRAME_RSIZE - 4, 4);
332 func = find_pc_function (p);
334 p = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
337 /* Search backward to find the trace-back tag. However,
338 do not trace back beyond the start of the text segment
339 (just as a sanity check to avoid going into never-never land). */
340 while (p >= text_start
341 && ((insn = read_memory_integer (p, 4)) & 0xff000000) != 0)
346 /* Couldn't find the trace-back tag.
347 Something strange is going on. */
354 /* Advance to the first word of the function, i.e. the word
355 after the trace-back tag. */
358 /* We've found the start of the function. Since High C interchanges
359 the meanings of bits 23 and 22 (as of Jul 90), and we
360 need to look at the prologue anyway to figure out
361 what rsize is, ignore the contents of the trace-back tag. */
362 examine_prologue (p, &rsize, &msize, &mfp_used);
367 fci->saved_msp = read_register (MSP_REGNUM) + msize;
373 read_register_stack_integer (fci->frame + rsize - 1, 4);
375 fci->saved_msp = fci->next->saved_msp + msize;
380 init_extra_frame_info (fci)
381 struct frame_info *fci;
384 /* Assume innermost frame. May produce strange results for "info frame"
385 but there isn't any way to tell the difference. */
386 init_frame_info (1, fci);
388 /* We're in get_prev_frame_info.
389 Take care of everything in init_frame_pc. */
395 init_frame_pc (fromleaf, fci)
397 struct frame_info *fci;
399 fci->pc = (fromleaf ? SAVED_PC_AFTER_CALL (fci->next) :
400 fci->next ? FRAME_SAVED_PC (fci->next) : read_pc ());
401 init_frame_info (0, fci);
404 /* Local variables (i.e. LOC_LOCAL) are on the memory stack, with their
405 offsets being relative to the memory stack pointer (high C) or
409 frame_locals_address (fi)
410 struct frame_info *fi;
412 struct block *b = block_for_pc (fi->pc);
413 /* If compiled without -g, assume GCC. */
414 if (b == NULL || BLOCK_GCC_COMPILED (b))
415 return fi->saved_msp;
417 return fi->saved_msp - fi->msize;
420 /* Routines for reading the register stack. The caller gets to treat
421 the register stack as a uniform stack in memory, from address $gr1
422 straight through $rfb and beyond. */
424 /* Analogous to read_memory except the length is understood to be 4.
425 Also, myaddr can be NULL (meaning don't bother to read), and
426 if actual_mem_addr is non-NULL, store there the address that it
427 was fetched from (or if from a register the offset within
428 registers). Set *LVAL to lval_memory or lval_register, depending
429 on where it came from. */
431 read_register_stack (memaddr, myaddr, actual_mem_addr, lval)
434 CORE_ADDR *actual_mem_addr;
435 enum lval_type *lval;
437 long rfb = read_register (RFB_REGNUM);
438 long rsp = read_register (RSP_REGNUM);
441 /* It's in a register. */
442 int regnum = (memaddr - rsp) / 4 + LR0_REGNUM;
443 if (regnum < LR0_REGNUM || regnum > LR0_REGNUM + 127)
444 error ("Attempt to read register stack out of range.");
446 read_register_gen (regnum, myaddr);
448 *lval = lval_register;
449 if (actual_mem_addr != NULL)
450 *actual_mem_addr = REGISTER_BYTE (regnum);
454 /* It's in the memory portion of the register stack. */
456 read_memory (memaddr, myaddr, 4);
459 if (actual_mem_addr != NULL)
460 *actual_mem_addr = memaddr;
464 /* Analogous to read_memory_integer
465 except the length is understood to be 4. */
467 read_register_stack_integer (memaddr, len)
472 read_register_stack (memaddr, &buf, NULL, NULL);
473 SWAP_TARGET_AND_HOST (&buf, 4);
477 /* Copy 4 bytes from GDB memory at MYADDR into inferior memory
478 at MEMADDR and put the actual address written into in
481 write_register_stack (memaddr, myaddr, actual_mem_addr)
484 CORE_ADDR *actual_mem_addr;
486 long rfb = read_register (RFB_REGNUM);
487 long rsp = read_register (RSP_REGNUM);
490 /* It's in a register. */
491 int regnum = (memaddr - rsp) / 4 + LR0_REGNUM;
492 if (regnum < LR0_REGNUM || regnum > LR0_REGNUM + 127)
493 error ("Attempt to read register stack out of range.");
495 write_register (regnum, *(long *)myaddr);
496 if (actual_mem_addr != NULL)
497 *actual_mem_addr = NULL;
501 /* It's in the memory portion of the register stack. */
503 write_memory (memaddr, myaddr, 4);
504 if (actual_mem_addr != NULL)
505 *actual_mem_addr = memaddr;
509 /* Find register number REGNUM relative to FRAME and put its
510 (raw) contents in *RAW_BUFFER. Set *OPTIMIZED if the variable
511 was optimized out (and thus can't be fetched). If the variable
512 was fetched from memory, set *ADDRP to where it was fetched from,
513 otherwise it was fetched from a register.
515 The argument RAW_BUFFER must point to aligned memory. */
517 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lvalp)
523 enum lval_type *lvalp;
525 struct frame_info *fi = get_frame_info (frame);
529 /* Once something has a register number, it doesn't get optimized out. */
530 if (optimized != NULL)
532 if (regnum == RSP_REGNUM)
534 if (raw_buffer != NULL)
535 *(CORE_ADDR *)raw_buffer = fi->frame;
540 else if (regnum == PC_REGNUM)
542 if (raw_buffer != NULL)
543 *(CORE_ADDR *)raw_buffer = fi->pc;
545 /* Not sure we have to do this. */
551 else if (regnum == MSP_REGNUM)
553 if (raw_buffer != NULL)
555 if (fi->next != NULL)
556 *(CORE_ADDR *)raw_buffer = fi->next->saved_msp;
558 *(CORE_ADDR *)raw_buffer = read_register (MSP_REGNUM);
560 /* The value may have been computed, not fetched. */
565 else if (regnum < LR0_REGNUM || regnum >= LR0_REGNUM + 128)
567 /* These registers are not saved over procedure calls,
568 so just print out the current values. */
569 if (raw_buffer != NULL)
570 *(CORE_ADDR *)raw_buffer = read_register (regnum);
572 *lvalp = lval_register;
574 *addrp = REGISTER_BYTE (regnum);
578 addr = fi->frame + (regnum - LR0_REGNUM) * 4;
579 if (raw_buffer != NULL)
580 read_register_stack (addr, raw_buffer, &addr, &lval);
587 /* Discard from the stack the innermost frame,
588 restoring all saved registers. */
593 FRAME frame = get_current_frame ();
594 struct frame_info *fi = get_frame_info (frame);
595 CORE_ADDR rfb = read_register (RFB_REGNUM);
596 CORE_ADDR gr1 = fi->frame + fi->rsize;
601 /* If popping a dummy frame, need to restore registers. */
602 if (PC_IN_CALL_DUMMY (read_register (PC_REGNUM),
603 read_register (SP_REGNUM),
606 for (i = 0; i < DUMMY_SAVE_SR128; ++i)
608 (SR_REGNUM (i + 128),
609 read_register (LR0_REGNUM + DUMMY_ARG / 4 + i));
610 for (i = 0; i < DUMMY_SAVE_GREGS; ++i)
613 read_register (LR0_REGNUM + DUMMY_ARG / 4 + DUMMY_SAVE_SR128 + i));
616 /* Restore the memory stack pointer. */
617 write_register (MSP_REGNUM, fi->saved_msp);
618 /* Restore the register stack pointer. */
619 write_register (GR1_REGNUM, gr1);
620 /* Check whether we need to fill registers. */
621 lr1 = read_register (LR0_REGNUM + 1);
625 int num_bytes = lr1 - rfb;
628 write_register (RAB_REGNUM, read_register (RAB_REGNUM) + num_bytes);
629 write_register (RFB_REGNUM, lr1);
630 for (i = 0; i < num_bytes; i += 4)
632 /* Note: word is in host byte order. */
633 word = read_memory_integer (rfb + i, 4);
634 write_register (LR0_REGNUM + ((rfb - gr1) % 0x80) + i / 4, word);
637 ret_addr = read_register (LR0_REGNUM);
638 write_register (PC_REGNUM, ret_addr);
639 write_register (NPC_REGNUM, ret_addr + 4);
640 flush_cached_frames ();
641 set_current_frame (create_new_frame (0, read_pc()));
644 /* Push an empty stack frame, to record the current PC, etc. */
651 CORE_ADDR msp = read_register (MSP_REGNUM);
655 write_register (LR0_REGNUM, read_register (PC_REGNUM));
657 /* Allocate the new frame. */
658 gr1 = read_register (GR1_REGNUM) - DUMMY_FRAME_RSIZE;
659 write_register (GR1_REGNUM, gr1);
661 rab = read_register (RAB_REGNUM);
664 /* We need to spill registers. */
665 int num_bytes = rab - gr1;
666 CORE_ADDR rfb = read_register (RFB_REGNUM);
670 write_register (RFB_REGNUM, rfb - num_bytes);
671 write_register (RAB_REGNUM, gr1);
672 for (i = 0; i < num_bytes; i += 4)
674 /* Note: word is in target byte order. */
675 read_register_gen (LR0_REGNUM + i / 4, &word, 4);
676 write_memory (rfb - num_bytes + i, &word, 4);
680 /* There are no arguments in to the dummy frame, so we don't need
681 more than rsize plus the return address and lr1. */
682 write_register (LR0_REGNUM + 1, gr1 + DUMMY_FRAME_RSIZE + 2 * 4);
684 /* Set the memory frame pointer. */
685 write_register (LR0_REGNUM + DUMMY_FRAME_RSIZE / 4 - 1, msp);
687 /* Allocate arg_slop. */
688 write_register (MSP_REGNUM, msp - 16 * 4);
690 /* Save registers. */
691 for (i = 0; i < DUMMY_SAVE_SR128; ++i)
692 write_register (LR0_REGNUM + DUMMY_ARG / 4 + i,
693 read_register (SR_REGNUM (i + 128)));
694 for (i = 0; i < DUMMY_SAVE_GREGS; ++i)
695 write_register (LR0_REGNUM + DUMMY_ARG / 4 + DUMMY_SAVE_SR128 + i,
696 read_register (RETURN_REGNUM + i));