Mon Jun 30 15:30:38 1997 Michael Snyder (msnyder@cleaver.cygnus.com)
[deliverable/binutils-gdb.git] / gdb / mips-tdep.c
CommitLineData
7d9884b9 1/* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
97e091b2 2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
0434c1a0 3 Free Software Foundation, Inc.
bd5635a1
RP
4 Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
5 and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
6
7This file is part of GDB.
8
361bf6ee 9This program is free software; you can redistribute it and/or modify
bd5635a1 10it under the terms of the GNU General Public License as published by
361bf6ee
JG
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
bd5635a1 13
361bf6ee 14This program is distributed in the hope that it will be useful,
bd5635a1
RP
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
361bf6ee 20along with this program; if not, write to the Free Software
09af5868 21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bd5635a1 22
bd5635a1 23#include "defs.h"
09af5868 24#include "gdb_string.h"
bd5635a1
RP
25#include "frame.h"
26#include "inferior.h"
27#include "symtab.h"
28#include "value.h"
29#include "gdbcmd.h"
ef08856f 30#include "language.h"
bd5635a1 31#include "gdbcore.h"
62a469e1
SG
32#include "symfile.h"
33#include "objfiles.h"
3fed1c4a 34#include "gdbtypes.h"
28444bf3 35#include "target.h"
bd5635a1 36
ee5fb959
JK
37#include "opcode/mips.h"
38
28444bf3 39#define VM_MIN_ADDRESS (CORE_ADDR)0x400000
70126bf9
KH
40
41/* FIXME: Put this declaration in frame.h. */
42extern struct obstack frame_cache_obstack;
28444bf3 43
002a422b 44#if 0
ee5fb959 45static int mips_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
002a422b 46#endif
ee5fb959 47
28444bf3
DP
48static int gdb_print_insn_mips PARAMS ((bfd_vma, disassemble_info *));
49
f2bf454e
FF
50static void mips_print_register PARAMS ((int, int));
51
52static mips_extra_func_info_t
53heuristic_proc_desc PARAMS ((CORE_ADDR, CORE_ADDR, struct frame_info *));
54
55static CORE_ADDR heuristic_proc_start PARAMS ((CORE_ADDR));
56
28444bf3 57static CORE_ADDR read_next_frame_reg PARAMS ((struct frame_info *, int));
f2bf454e 58
e4dbd248
PS
59static void mips_set_fpu_command PARAMS ((char *, int,
60 struct cmd_list_element *));
61
62static void mips_show_fpu_command PARAMS ((char *, int,
63 struct cmd_list_element *));
64
9f9f94aa
SS
65void mips_set_processor_type_command PARAMS ((char *, int));
66
1d9489c1
JK
67int mips_set_processor_type PARAMS ((char *));
68
69static void mips_show_processor_type_command PARAMS ((char *, int));
70
9f9f94aa
SS
71static void reinit_frame_cache_sfunc PARAMS ((char *, int,
72 struct cmd_list_element *));
73
f2bf454e
FF
74static mips_extra_func_info_t
75 find_proc_desc PARAMS ((CORE_ADDR pc, struct frame_info *next_frame));
76
77static CORE_ADDR after_prologue PARAMS ((CORE_ADDR pc,
78 mips_extra_func_info_t proc_desc));
79
9f9f94aa
SS
80/* This value is the model of MIPS in use. It is derived from the value
81 of the PrID register. */
82
83char *mips_processor_type;
84
85char *tmp_mips_processor_type;
86
c2a0f1cb
ILT
87/* Some MIPS boards don't support floating point, so we permit the
88 user to turn it off. */
9f9f94aa 89
e4dbd248
PS
90enum mips_fpu_type mips_fpu;
91
92static char *mips_fpu_string;
c2a0f1cb 93
9f9f94aa
SS
94/* A set of original names, to be used when restoring back to generic
95 registers from a specific set. */
96
97char *mips_generic_reg_names[] = REGISTER_NAMES;
98
99/* Names of IDT R3041 registers. */
100
101char *mips_r3041_reg_names[] = {
102 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
103 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
104 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
105 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
106 "sr", "lo", "hi", "bad", "cause","pc",
107 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
108 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
109 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
110 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
111 "fsr", "fir", "fp", "",
112 "", "", "bus", "ccfg", "", "", "", "",
113 "", "", "port", "cmp", "", "", "epc", "prid",
114};
115
116/* Names of IDT R3051 registers. */
117
118char *mips_r3051_reg_names[] = {
119 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
120 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
121 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
122 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
123 "sr", "lo", "hi", "bad", "cause","pc",
124 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
125 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
126 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
127 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
128 "fsr", "fir", "fp", "",
129 "inx", "rand", "elo", "", "ctxt", "", "", "",
130 "", "", "ehi", "", "", "", "epc", "prid",
131};
132
133/* Names of IDT R3081 registers. */
134
135char *mips_r3081_reg_names[] = {
136 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
137 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
138 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
139 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
140 "sr", "lo", "hi", "bad", "cause","pc",
141 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
142 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
143 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
144 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
145 "fsr", "fir", "fp", "",
146 "inx", "rand", "elo", "cfg", "ctxt", "", "", "",
147 "", "", "ehi", "", "", "", "epc", "prid",
148};
149
e4dbd248
PS
150/* Names of LSI 33k registers. */
151
152char *mips_lsi33k_reg_names[] = {
153 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
154 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
155 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
156 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
157 "epc", "hi", "lo", "sr", "cause","badvaddr",
158 "dcic", "bpc", "bda", "", "", "", "", "",
159 "", "", "", "", "", "", "", "",
160 "", "", "", "", "", "", "", "",
161 "", "", "", "", "", "", "", "",
162 "", "", "", "",
163 "", "", "", "", "", "", "", "",
164 "", "", "", "", "", "", "", "",
165};
166
9f9f94aa
SS
167struct {
168 char *name;
169 char **regnames;
170} mips_processor_type_table[] = {
171 { "generic", mips_generic_reg_names },
172 { "r3041", mips_r3041_reg_names },
173 { "r3051", mips_r3051_reg_names },
174 { "r3071", mips_r3081_reg_names },
175 { "r3081", mips_r3081_reg_names },
e4dbd248 176 { "lsi33k", mips_lsi33k_reg_names },
9f9f94aa
SS
177 { NULL, NULL }
178};
179
f9f8c14b
MA
180/* Table to translate MIPS16 register field to actual register number. */
181static int mips16_to_32_reg[8] = { 16, 17, 2, 3, 4, 5, 6, 7 };
182
3127785a
RP
183/* Heuristic_proc_start may hunt through the text section for a long
184 time across a 2400 baud serial line. Allows the user to limit this
185 search. */
9f9f94aa 186
3127785a
RP
187static unsigned int heuristic_fence_post = 0;
188
0f552c5f 189#define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
28444bf3 190#define PROC_HIGH_ADDR(proc) ((proc)->high_addr) /* upper address bound */
0f552c5f
JG
191#define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
192#define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
193#define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
194#define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
195#define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
196#define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
197#define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
198#define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
bd5635a1 199#define _PROC_MAGIC_ 0x0F0F0F0F
0f552c5f
JG
200#define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
201#define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
bd5635a1
RP
202
203struct linked_proc_info
204{
205 struct mips_extra_func_info info;
206 struct linked_proc_info *next;
dac4929a 207} *linked_proc_desc_table = NULL;
bd5635a1 208
f2bf454e
FF
209
210/* This returns the PC of the first inst after the prologue. If we can't
211 find the prologue, then return 0. */
212
213static CORE_ADDR
214after_prologue (pc, proc_desc)
215 CORE_ADDR pc;
216 mips_extra_func_info_t proc_desc;
217{
218 struct symtab_and_line sal;
219 CORE_ADDR func_addr, func_end;
220
221 if (!proc_desc)
222 proc_desc = find_proc_desc (pc, NULL);
223
224 if (proc_desc)
225 {
226 /* If function is frameless, then we need to do it the hard way. I
227 strongly suspect that frameless always means prologueless... */
228 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
229 && PROC_FRAME_OFFSET (proc_desc) == 0)
230 return 0;
231 }
232
233 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
234 return 0; /* Unknown */
235
236 sal = find_pc_line (func_addr, 0);
237
238 if (sal.end < func_end)
239 return sal.end;
240
241 /* The line after the prologue is after the end of the function. In this
242 case, tell the caller to find the prologue the hard way. */
243
244 return 0;
245}
246
f9f8c14b
MA
247/* Decode a MIPS32 instruction that saves a register in the stack, and
248 set the appropriate bit in the general register mask or float register mask
249 to indicate which register is saved. This is a helper function
250 for mips_find_saved_regs. */
251
252static void
253mips32_decode_reg_save (inst, gen_mask, float_mask)
254 t_inst inst;
255 unsigned long *gen_mask;
256 unsigned long *float_mask;
257{
258 int reg;
259
260 if ((inst & 0xffe00000) == 0xafa00000 /* sw reg,n($sp) */
261 || (inst & 0xffe00000) == 0xafc00000 /* sw reg,n($r30) */
262 || (inst & 0xffe00000) == 0xffa00000) /* sd reg,n($sp) */
263 {
264 /* It might be possible to use the instruction to
265 find the offset, rather than the code below which
266 is based on things being in a certain order in the
267 frame, but figuring out what the instruction's offset
268 is relative to might be a little tricky. */
269 reg = (inst & 0x001f0000) >> 16;
270 *gen_mask |= (1 << reg);
271 }
272 else if ((inst & 0xffe00000) == 0xe7a00000 /* swc1 freg,n($sp) */
273 || (inst & 0xffe00000) == 0xe7c00000 /* swc1 freg,n($r30) */
274 || (inst & 0xffe00000) == 0xf7a00000)/* sdc1 freg,n($sp) */
275
276 {
277 reg = ((inst & 0x001f0000) >> 16);
278 *float_mask |= (1 << reg);
279 }
280}
281
282/* Decode a MIPS16 instruction that saves a register in the stack, and
283 set the appropriate bit in the general register or float register mask
284 to indicate which register is saved. This is a helper function
285 for mips_find_saved_regs. */
286
287static void
288mips16_decode_reg_save (inst, gen_mask)
289 t_inst inst;
290 unsigned long *gen_mask;
291{
b348b9fd 292 if ((inst & 0xf800) == 0xd000) /* sw reg,n($sp) */
f9f8c14b 293 {
b348b9fd
MA
294 int reg = mips16_to_32_reg[(inst & 0x700) >> 8];
295 *gen_mask |= (1 << reg);
296 }
297 else if ((inst & 0xff00) == 0xf900) /* sd reg,n($sp) */
298 {
299 int reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
f9f8c14b
MA
300 *gen_mask |= (1 << reg);
301 }
302 else if ((inst & 0xff00) == 0x6200 /* sw $ra,n($sp) */
303 || (inst & 0xff00) == 0xfa00) /* sd $ra,n($sp) */
304 *gen_mask |= (1 << 31);
305}
306
c81a76b3
MA
307
308/* Fetch and return instruction from the specified location. If the PC
309 is odd, assume it's a MIPS16 instruction; otherwise MIPS32. */
310
311static t_inst
312mips_fetch_instruction (addr)
313 CORE_ADDR addr;
314{
315 char buf[MIPS_INSTLEN];
316 int instlen;
317 int status;
318
319 if (IS_MIPS16_ADDR (addr))
320 {
321 instlen = MIPS16_INSTLEN;
322 addr = UNMAKE_MIPS16_ADDR (addr);
323 }
324 else
325 instlen = MIPS_INSTLEN;
326 status = read_memory_nobpt (addr, buf, instlen);
327 if (status)
328 memory_error (status, addr);
329 return extract_unsigned_integer (buf, instlen);
330}
331
332
70126bf9
KH
333/* Guaranteed to set fci->saved_regs to some values (it never leaves it
334 NULL). */
335
336void
337mips_find_saved_regs (fci)
9f9f94aa 338 struct frame_info *fci;
70126bf9
KH
339{
340 int ireg;
341 CORE_ADDR reg_position;
342 /* r0 bit means kernel trap */
343 int kernel_trap;
344 /* What registers have been saved? Bitmasks. */
345 unsigned long gen_mask, float_mask;
346 mips_extra_func_info_t proc_desc;
c81a76b3 347 t_inst inst;
70126bf9
KH
348
349 fci->saved_regs = (struct frame_saved_regs *)
350 obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
351 memset (fci->saved_regs, 0, sizeof (struct frame_saved_regs));
352
e4dbd248
PS
353 /* If it is the frame for sigtramp, the saved registers are located
354 in a sigcontext structure somewhere on the stack.
355 If the stack layout for sigtramp changes we might have to change these
356 constants and the companion fixup_sigtramp in mdebugread.c */
357#ifndef SIGFRAME_BASE
358/* To satisfy alignment restrictions, sigcontext is located 4 bytes
359 above the sigtramp frame. */
28444bf3
DP
360#define SIGFRAME_BASE MIPS_REGSIZE
361/* FIXME! Are these correct?? */
362#define SIGFRAME_PC_OFF (SIGFRAME_BASE + 2 * MIPS_REGSIZE)
363#define SIGFRAME_REGSAVE_OFF (SIGFRAME_BASE + 3 * MIPS_REGSIZE)
364#define SIGFRAME_FPREGSAVE_OFF \
365 (SIGFRAME_REGSAVE_OFF + MIPS_NUMREGS * MIPS_REGSIZE + 3 * MIPS_REGSIZE)
e4dbd248
PS
366#endif
367#ifndef SIGFRAME_REG_SIZE
28444bf3
DP
368/* FIXME! Is this correct?? */
369#define SIGFRAME_REG_SIZE MIPS_REGSIZE
e4dbd248
PS
370#endif
371 if (fci->signal_handler_caller)
372 {
28444bf3 373 for (ireg = 0; ireg < MIPS_NUMREGS; ireg++)
e4dbd248
PS
374 {
375 reg_position = fci->frame + SIGFRAME_REGSAVE_OFF
376 + ireg * SIGFRAME_REG_SIZE;
377 fci->saved_regs->regs[ireg] = reg_position;
378 }
28444bf3 379 for (ireg = 0; ireg < MIPS_NUMREGS; ireg++)
e4dbd248
PS
380 {
381 reg_position = fci->frame + SIGFRAME_FPREGSAVE_OFF
382 + ireg * SIGFRAME_REG_SIZE;
383 fci->saved_regs->regs[FP0_REGNUM + ireg] = reg_position;
384 }
385 fci->saved_regs->regs[PC_REGNUM] = fci->frame + SIGFRAME_PC_OFF;
386 return;
387 }
388
70126bf9
KH
389 proc_desc = fci->proc_desc;
390 if (proc_desc == NULL)
391 /* I'm not sure how/whether this can happen. Normally when we can't
392 find a proc_desc, we "synthesize" one using heuristic_proc_desc
393 and set the saved_regs right away. */
394 return;
395
396 kernel_trap = PROC_REG_MASK(proc_desc) & 1;
397 gen_mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc);
398 float_mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc);
399
f9f8c14b
MA
400 if (/* In any frame other than the innermost or a frame interrupted by
401 a signal, we assume that all registers have been saved.
402 This assumes that all register saves in a function happen before
403 the first function call. */
404 (fci->next == NULL || fci->next->signal_handler_caller)
70126bf9
KH
405
406 /* In a dummy frame we know exactly where things are saved. */
407 && !PROC_DESC_IS_DUMMY (proc_desc)
408
f2bf454e
FF
409 /* Don't bother unless we are inside a function prologue. Outside the
410 prologue, we know where everything is. */
411
412 && in_prologue (fci->pc, PROC_LOW_ADDR (proc_desc))
413
70126bf9
KH
414 /* Not sure exactly what kernel_trap means, but if it means
415 the kernel saves the registers without a prologue doing it,
416 we better not examine the prologue to see whether registers
417 have been saved yet. */
418 && !kernel_trap)
419 {
420 /* We need to figure out whether the registers that the proc_desc
421 claims are saved have been saved yet. */
422
423 CORE_ADDR addr;
70126bf9
KH
424
425 /* Bitmasks; set if we have found a save for the register. */
426 unsigned long gen_save_found = 0;
427 unsigned long float_save_found = 0;
c81a76b3 428 int instlen;
70126bf9 429
c1fc0935
MA
430 /* If the address is odd, assume this is MIPS16 code. */
431 addr = PROC_LOW_ADDR (proc_desc);
c81a76b3 432 instlen = IS_MIPS16_ADDR (addr) ? MIPS16_INSTLEN : MIPS_INSTLEN;
f9f8c14b 433
20fa0902
MA
434 /* Scan through this function's instructions preceding the current
435 PC, and look for those that save registers. */
f9f8c14b
MA
436 while (addr < fci->pc)
437 {
c81a76b3
MA
438 inst = mips_fetch_instruction (addr);
439 if (IS_MIPS16_ADDR (addr))
f9f8c14b
MA
440 mips16_decode_reg_save (inst, &gen_save_found);
441 else
442 mips32_decode_reg_save (inst, &gen_save_found, &float_save_found);
443 addr += instlen;
70126bf9
KH
444 }
445 gen_mask = gen_save_found;
446 float_mask = float_save_found;
447 }
448
449 /* Fill in the offsets for the registers which gen_mask says
450 were saved. */
451 reg_position = fci->frame + PROC_REG_OFFSET (proc_desc);
28444bf3 452 for (ireg= MIPS_NUMREGS-1; gen_mask; --ireg, gen_mask <<= 1)
70126bf9
KH
453 if (gen_mask & 0x80000000)
454 {
455 fci->saved_regs->regs[ireg] = reg_position;
456 reg_position -= MIPS_REGSIZE;
457 }
c81a76b3
MA
458
459 /* The MIPS16 entry instruction saves $s0 and $s1 in the reverse order
460 of that normally used by gcc. Therefore, we have to fetch the first
461 instruction of the function, and if it's an entry instruction that
462 saves $s0 or $s1, correct their saved addresses. */
463 if (IS_MIPS16_ADDR (PROC_LOW_ADDR (proc_desc)))
464 {
465 inst = mips_fetch_instruction (PROC_LOW_ADDR (proc_desc));
466 if ((inst & 0xf81f) == 0xe809 && (inst & 0x700) != 0x700) /* entry */
467 {
468 int reg;
469 int sreg_count = (inst >> 6) & 3;
470
471 /* Check if the ra register was pushed on the stack. */
472 reg_position = fci->frame + PROC_REG_OFFSET (proc_desc);
473 if (inst & 0x20)
474 reg_position -= MIPS_REGSIZE;
475
476 /* Check if the s0 and s1 registers were pushed on the stack. */
477 for (reg = 16; reg < sreg_count+16; reg++)
478 {
479 fci->saved_regs->regs[reg] = reg_position;
480 reg_position -= MIPS_REGSIZE;
481 }
482 }
483 }
484
70126bf9
KH
485 /* Fill in the offsets for the registers which float_mask says
486 were saved. */
487 reg_position = fci->frame + PROC_FREG_OFFSET (proc_desc);
488
489 /* The freg_offset points to where the first *double* register
490 is saved. So skip to the high-order word. */
28444bf3 491 if (! GDB_TARGET_IS_MIPS64)
c81a76b3 492 reg_position += MIPS_REGSIZE;
28444bf3 493
20fa0902
MA
494 /* Fill in the offsets for the float registers which float_mask says
495 were saved. */
28444bf3 496 for (ireg = MIPS_NUMREGS-1; float_mask; --ireg, float_mask <<= 1)
70126bf9
KH
497 if (float_mask & 0x80000000)
498 {
499 fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
500 reg_position -= MIPS_REGSIZE;
501 }
502
503 fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
504}
bd5635a1 505
28444bf3 506static CORE_ADDR
bd5635a1 507read_next_frame_reg(fi, regno)
9f9f94aa 508 struct frame_info *fi;
bd5635a1
RP
509 int regno;
510{
bd5635a1 511 for (; fi; fi = fi->next)
70126bf9 512 {
e4dbd248
PS
513 /* We have to get the saved sp from the sigcontext
514 if it is a signal handler frame. */
515 if (regno == SP_REGNUM && !fi->signal_handler_caller)
516 return fi->frame;
70126bf9
KH
517 else
518 {
519 if (fi->saved_regs == NULL)
520 mips_find_saved_regs (fi);
521 if (fi->saved_regs->regs[regno])
522 return read_memory_integer(fi->saved_regs->regs[regno], MIPS_REGSIZE);
523 }
524 }
525 return read_register (regno);
bd5635a1
RP
526}
527
96431497
MA
528/* mips_addr_bits_remove - remove useless address bits */
529
530CORE_ADDR
531mips_addr_bits_remove (addr)
532 CORE_ADDR addr;
533{
97e091b2
MA
534#if GDB_TARGET_IS_MIPS64
535 if ((addr >> 32 == (CORE_ADDR)0xffffffff)
f781fe93
MA
536 && (strcmp (target_shortname,"pmon")==0
537 || strcmp (target_shortname,"ddb")==0
538 || strcmp (target_shortname,"sim")==0))
96431497
MA
539 {
540 /* This hack is a work-around for existing boards using PMON,
541 the simulator, and any other 64-bit targets that doesn't have
542 true 64-bit addressing. On these targets, the upper 32 bits
543 of addresses are ignored by the hardware. Thus, the PC or SP
544 are likely to have been sign extended to all 1s by instruction
545 sequences that load 32-bit addresses. For example, a typical
546 piece of code that loads an address is this:
547 lui $r2, <upper 16 bits>
548 ori $r2, <lower 16 bits>
549 But the lui sign-extends the value such that the upper 32 bits
550 may be all 1s. The workaround is simply to mask off these bits.
551 In the future, gcc may be changed to support true 64-bit
552 addressing, and this masking will have to be disabled. */
553 addr &= (CORE_ADDR)0xffffffff;
554 }
f9f8c14b
MA
555#else
556 /* Even when GDB is configured for some 32-bit targets (e.g. mips-elf),
557 BFD is configured to handle 64-bit targets, so CORE_ADDR is 64 bits.
558 So we still have to mask off useless bits from addresses. */
559 addr &= (CORE_ADDR)0xffffffff;
97e091b2 560#endif
96431497
MA
561
562 return addr;
563}
564
f781fe93
MA
565void
566mips_init_frame_pc_first (fromleaf, prev)
567 int fromleaf;
568 struct frame_info *prev;
569{
570 CORE_ADDR pc, tmp;
571
572 pc = ((fromleaf) ? SAVED_PC_AFTER_CALL (prev->next) :
573 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
574 tmp = mips_skip_stub (pc);
575 prev->pc = tmp ? tmp : pc;
576}
577
578
28444bf3 579CORE_ADDR
bd5635a1 580mips_frame_saved_pc(frame)
9f9f94aa 581 struct frame_info *frame;
bd5635a1 582{
28444bf3 583 CORE_ADDR saved_pc;
0f552c5f 584 mips_extra_func_info_t proc_desc = frame->proc_desc;
0434c1a0
PS
585 /* We have to get the saved pc from the sigcontext
586 if it is a signal handler frame. */
587 int pcreg = frame->signal_handler_caller ? PC_REGNUM
588 : (proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM);
0f552c5f 589
bd5635a1 590 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
28444bf3
DP
591 saved_pc = read_memory_integer(frame->frame - MIPS_REGSIZE, MIPS_REGSIZE);
592 else
593 saved_pc = read_next_frame_reg(frame, pcreg);
0f552c5f 594
96431497 595 return ADDR_BITS_REMOVE (saved_pc);
bd5635a1
RP
596}
597
598static struct mips_extra_func_info temp_proc_desc;
599static struct frame_saved_regs temp_saved_regs;
600
a8172eea
RP
601/* This fencepost looks highly suspicious to me. Removing it also
602 seems suspicious as it could affect remote debugging across serial
3127785a 603 lines. */
a8172eea 604
0f552c5f
JG
605static CORE_ADDR
606heuristic_proc_start(pc)
bd5635a1
RP
607 CORE_ADDR pc;
608{
32dab603
MA
609 CORE_ADDR start_pc;
610 CORE_ADDR fence;
20fa0902 611 int instlen;
b348b9fd 612 int seen_adjsp = 0;
0f552c5f 613
32dab603
MA
614 pc = ADDR_BITS_REMOVE (pc);
615 start_pc = pc;
616 fence = start_pc - heuristic_fence_post;
0f552c5f 617 if (start_pc == 0) return 0;
3127785a
RP
618
619 if (heuristic_fence_post == UINT_MAX
620 || fence < VM_MIN_ADDRESS)
621 fence = VM_MIN_ADDRESS;
0f552c5f 622
c1fc0935 623 instlen = IS_MIPS16_ADDR (pc) ? MIPS16_INSTLEN : MIPS_INSTLEN;
20fa0902 624
bd5635a1 625 /* search back for previous return */
20fa0902 626 for (start_pc -= instlen; ; start_pc -= instlen)
a8172eea
RP
627 if (start_pc < fence)
628 {
3127785a
RP
629 /* It's not clear to me why we reach this point when
630 stop_soon_quietly, but with this test, at least we
631 don't print out warnings for every child forked (eg, on
632 decstation). 22apr93 rich@cygnus.com. */
633 if (!stop_soon_quietly)
634 {
23d35572
JK
635 static int blurb_printed = 0;
636
3127785a
RP
637 if (fence == VM_MIN_ADDRESS)
638 warning("Hit beginning of text section without finding");
639 else
640 warning("Hit heuristic-fence-post without finding");
641
f781fe93 642 warning("enclosing function for address 0x%s", paddr_nz (pc));
23d35572
JK
643 if (!blurb_printed)
644 {
645 printf_filtered ("\
646This warning occurs if you are debugging a function without any symbols\n\
647(for example, in a stripped executable). In that case, you may wish to\n\
648increase the size of the search with the `set heuristic-fence-post' command.\n\
649\n\
650Otherwise, you told GDB there was a function where there isn't one, or\n\
651(more likely) you have encountered a bug in GDB.\n");
652 blurb_printed = 1;
653 }
3127785a
RP
654 }
655
a8172eea
RP
656 return 0;
657 }
c1fc0935 658 else if (IS_MIPS16_ADDR (start_pc))
20fa0902 659 {
c1fc0935
MA
660 unsigned short inst;
661
b348b9fd
MA
662 /* On MIPS16, any one of the following is likely to be the
663 start of a function:
664 entry
665 addiu sp,-n
666 daddiu sp,-n
667 extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n' */
c81a76b3 668 inst = mips_fetch_instruction (start_pc);
b348b9fd
MA
669 if (((inst & 0xf81f) == 0xe809 && (inst & 0x700) != 0x700) /* entry */
670 || (inst & 0xff80) == 0x6380 /* addiu sp,-n */
671 || (inst & 0xff80) == 0xfb80 /* daddiu sp,-n */
672 || ((inst & 0xf810) == 0xf010 && seen_adjsp)) /* extend -n */
673 break;
674 else if ((inst & 0xff00) == 0x6300 /* addiu sp */
675 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
676 seen_adjsp = 1;
677 else
678 seen_adjsp = 0;
20fa0902 679 }
bd5635a1 680 else if (ABOUT_TO_RETURN(start_pc))
20fa0902
MA
681 {
682 start_pc += 2 * MIPS_INSTLEN; /* skip return, and its delay slot */
bd5635a1 683 break;
20fa0902 684 }
bd5635a1 685
bd5635a1
RP
686#if 0
687 /* skip nops (usually 1) 0 - is this */
28444bf3
DP
688 while (start_pc < pc && read_memory_integer (start_pc, MIPS_INSTLEN) == 0)
689 start_pc += MIPS_INSTLEN;
bd5635a1
RP
690#endif
691 return start_pc;
692}
693
c81a76b3 694/* Fetch the immediate value from a MIPS16 instruction.
b348b9fd
MA
695 If the previous instruction was an EXTEND, use it to extend
696 the upper bits of the immediate value. This is a helper function
697 for mips16_heuristic_proc_desc. */
698
699static int
700mips16_get_imm (prev_inst, inst, nbits, scale, is_signed)
701 unsigned short prev_inst; /* previous instruction */
702 unsigned short inst; /* current current instruction */
703 int nbits; /* number of bits in imm field */
704 int scale; /* scale factor to be applied to imm */
705 int is_signed; /* is the imm field signed? */
706{
707 int offset;
708
709 if ((prev_inst & 0xf800) == 0xf000) /* prev instruction was EXTEND? */
710 {
7d9e8fac 711 offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
b348b9fd
MA
712 if (offset & 0x8000) /* check for negative extend */
713 offset = 0 - (0x10000 - (offset & 0xffff));
714 return offset | (inst & 0x1f);
715 }
716 else
717 {
718 int max_imm = 1 << nbits;
719 int mask = max_imm - 1;
720 int sign_bit = max_imm >> 1;
721
722 offset = inst & mask;
723 if (is_signed && (offset & sign_bit))
724 offset = 0 - (max_imm - offset);
725 return offset * scale;
726 }
727}
728
729
730/* Fill in values in temp_proc_desc based on the MIPS16 instruction
731 stream from start_pc to limit_pc. */
732
733static void
734mips16_heuristic_proc_desc(start_pc, limit_pc, next_frame, sp)
bd5635a1 735 CORE_ADDR start_pc, limit_pc;
9f9f94aa 736 struct frame_info *next_frame;
b348b9fd 737 CORE_ADDR sp;
bd5635a1 738{
b348b9fd
MA
739 CORE_ADDR cur_pc;
740 CORE_ADDR frame_addr = 0; /* Value of $r17, used as frame pointer */
741 unsigned short prev_inst = 0; /* saved copy of previous instruction */
742 unsigned inst = 0; /* current instruction */
bd5635a1 743
b348b9fd 744 PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
34df79fc 745
b348b9fd
MA
746 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS16_INSTLEN)
747 {
c81a76b3 748 int reg, offset;
b348b9fd
MA
749
750 /* Save the previous instruction. If it's an EXTEND, we'll extract
751 the immediate offset extension from it in mips16_get_imm. */
752 prev_inst = inst;
753
c81a76b3
MA
754 /* Fetch and decode the instruction. */
755 inst = (unsigned short) mips_fetch_instruction (cur_pc);
b348b9fd
MA
756 if ((inst & 0xff00) == 0x6300 /* addiu sp */
757 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
758 {
759 offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
760 if (offset < 0) /* negative stack adjustment? */
761 PROC_FRAME_OFFSET(&temp_proc_desc) -= offset;
762 else
763 /* Exit loop if a positive stack adjustment is found, which
764 usually means that the stack cleanup code in the function
765 epilogue is reached. */
766 break;
767 }
768 else if ((inst & 0xf800) == 0xd000) /* sw reg,n($sp) */
769 {
770 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
771 reg = mips16_to_32_reg[(inst & 0x700) >> 8];
772 PROC_REG_MASK(&temp_proc_desc) |= (1 << reg);
773 temp_saved_regs.regs[reg] = sp + offset;
774 }
775 else if ((inst & 0xff00) == 0xf900) /* sd reg,n($sp) */
776 {
777 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
778 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
779 PROC_REG_MASK(&temp_proc_desc) |= (1 << reg);
780 temp_saved_regs.regs[reg] = sp + offset;
781 }
782 else if ((inst & 0xff00) == 0x6200) /* sw $ra,n($sp) */
783 {
784 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
785 PROC_REG_MASK(&temp_proc_desc) |= (1 << 31);
786 temp_saved_regs.regs[31] = sp + offset;
97e091b2 787 }
b348b9fd
MA
788 else if ((inst & 0xff00) == 0xfa00) /* sd $ra,n($sp) */
789 {
790 offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
791 PROC_REG_MASK(&temp_proc_desc) |= (1 << 31);
792 temp_saved_regs.regs[31] = sp + offset;
bd5635a1 793 }
b348b9fd
MA
794 else if (inst == 0x673d) /* move $s1, $sp */
795 {
796 frame_addr = read_next_frame_reg(next_frame, 30);
797 PROC_FRAME_REG (&temp_proc_desc) = 17;
97e091b2 798 }
32dab603
MA
799 else if ((inst & 0xff00) == 0x0100) /* addiu $s1,sp,n */
800 {
801 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
802 frame_addr = sp + offset;
803 PROC_FRAME_REG (&temp_proc_desc) = 17;
804 }
b348b9fd
MA
805 else if ((inst & 0xFF00) == 0xd900) /* sw reg,offset($s1) */
806 {
807 offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
808 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
809 PROC_REG_MASK(&temp_proc_desc) |= 1 << reg;
810 temp_saved_regs.regs[reg] = frame_addr + offset;
811 }
812 else if ((inst & 0xFF00) == 0x7900) /* sd reg,offset($s1) */
813 {
814 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
815 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
816 PROC_REG_MASK(&temp_proc_desc) |= 1 << reg;
817 temp_saved_regs.regs[reg] = frame_addr + offset;
818 }
819 else if ((inst & 0xf81f) == 0xe809 && (inst & 0x700) != 0x700) /* entry */
820 {
821 int areg_count = (inst >> 8) & 7;
822 int sreg_count = (inst >> 6) & 3;
823
824 /* The entry instruction always subtracts 32 from the SP. */
825 PROC_FRAME_OFFSET(&temp_proc_desc) += 32;
826
827 /* Check if a0-a3 were saved in the caller's argument save area. */
c81a76b3 828 for (reg = 4, offset = 32; reg < areg_count+4; reg++)
b348b9fd
MA
829 {
830 PROC_REG_MASK(&temp_proc_desc) |= 1 << reg;
831 temp_saved_regs.regs[reg] = sp + offset;
c81a76b3 832 offset -= MIPS_REGSIZE;
b348b9fd
MA
833 }
834
835 /* Check if the ra register was pushed on the stack. */
836 offset = 28;
837 if (inst & 0x20)
838 {
839 PROC_REG_MASK(&temp_proc_desc) |= 1 << 31;
840 temp_saved_regs.regs[31] = sp + offset;
c81a76b3 841 offset -= MIPS_REGSIZE;
b348b9fd
MA
842 }
843
844 /* Check if the s0 and s1 registers were pushed on the stack. */
c81a76b3 845 for (reg = 16; reg < sreg_count+16; reg++)
b348b9fd
MA
846 {
847 PROC_REG_MASK(&temp_proc_desc) |= 1 << reg;
848 temp_saved_regs.regs[reg] = sp + offset;
c81a76b3 849 offset -= MIPS_REGSIZE;
b348b9fd
MA
850 }
851 }
7d9e8fac
MA
852 else if ((inst & 0xf800) == 0x1800) /* jal(x) */
853 cur_pc += MIPS16_INSTLEN; /* 32-bit instruction */
b348b9fd
MA
854 }
855}
856
857static void
858mips32_heuristic_proc_desc(start_pc, limit_pc, next_frame, sp)
859 CORE_ADDR start_pc, limit_pc;
860 struct frame_info *next_frame;
861 CORE_ADDR sp;
862{
863 CORE_ADDR cur_pc;
864 CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for frame-pointer */
865restart:
866 PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
867 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSTLEN)
868 {
b348b9fd 869 unsigned long inst, high_word, low_word;
c81a76b3 870 int reg;
b348b9fd
MA
871
872 /* Fetch the instruction. */
c81a76b3 873 inst = (unsigned long) mips_fetch_instruction (cur_pc);
b348b9fd
MA
874
875 /* Save some code by pre-extracting some useful fields. */
876 high_word = (inst >> 16) & 0xffff;
877 low_word = inst & 0xffff;
878 reg = high_word & 0x1f;
879
880 if (high_word == 0x27bd /* addiu $sp,$sp,-i */
881 || high_word == 0x23bd /* addi $sp,$sp,-i */
882 || high_word == 0x67bd) /* daddiu $sp,$sp,-i */
883 {
884 if (low_word & 0x8000) /* negative stack adjustment? */
885 PROC_FRAME_OFFSET(&temp_proc_desc) += 0x10000 - low_word;
886 else
887 /* Exit loop if a positive stack adjustment is found, which
888 usually means that the stack cleanup code in the function
889 epilogue is reached. */
890 break;
891 }
892 else if ((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
893 {
894 PROC_REG_MASK(&temp_proc_desc) |= 1 << reg;
895 temp_saved_regs.regs[reg] = sp + low_word;
896 }
897 else if ((high_word & 0xFFE0) == 0xffa0) /* sd reg,offset($sp) */
898 {
899 /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra,
900 but the register size used is only 32 bits. Make the address
901 for the saved register point to the lower 32 bits. */
902 PROC_REG_MASK(&temp_proc_desc) |= 1 << reg;
903 temp_saved_regs.regs[reg] = sp + low_word + 8 - MIPS_REGSIZE;
904 }
905 else if (high_word == 0x27be) /* addiu $30,$sp,size */
906 {
907 /* Old gcc frame, r30 is virtual frame pointer. */
32dab603 908 if ((long)low_word != PROC_FRAME_OFFSET(&temp_proc_desc))
b348b9fd
MA
909 frame_addr = sp + low_word;
910 else if (PROC_FRAME_REG (&temp_proc_desc) == SP_REGNUM)
911 {
912 unsigned alloca_adjust;
913 PROC_FRAME_REG (&temp_proc_desc) = 30;
914 frame_addr = read_next_frame_reg(next_frame, 30);
915 alloca_adjust = (unsigned)(frame_addr - (sp + low_word));
916 if (alloca_adjust > 0)
917 {
918 /* FP > SP + frame_size. This may be because
919 * of an alloca or somethings similar.
920 * Fix sp to "pre-alloca" value, and try again.
921 */
922 sp += alloca_adjust;
923 goto restart;
bd5635a1
RP
924 }
925 }
926 }
b348b9fd
MA
927 /* move $30,$sp. With different versions of gas this will be either
928 `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
929 Accept any one of these. */
930 else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
931 {
932 /* New gcc frame, virtual frame pointer is at r30 + frame_size. */
933 if (PROC_FRAME_REG (&temp_proc_desc) == SP_REGNUM)
934 {
935 unsigned alloca_adjust;
936 PROC_FRAME_REG (&temp_proc_desc) = 30;
937 frame_addr = read_next_frame_reg(next_frame, 30);
938 alloca_adjust = (unsigned)(frame_addr - sp);
939 if (alloca_adjust > 0)
940 {
941 /* FP > SP + frame_size. This may be because
942 * of an alloca or somethings similar.
943 * Fix sp to "pre-alloca" value, and try again.
944 */
945 sp += alloca_adjust;
946 goto restart;
97e091b2
MA
947 }
948 }
949 }
b348b9fd
MA
950 else if ((high_word & 0xFFE0) == 0xafc0) /* sw reg,offset($30) */
951 {
952 PROC_REG_MASK(&temp_proc_desc) |= 1 << reg;
953 temp_saved_regs.regs[reg] = frame_addr + low_word;
bd5635a1
RP
954 }
955 }
b348b9fd
MA
956}
957
958static mips_extra_func_info_t
959heuristic_proc_desc(start_pc, limit_pc, next_frame)
960 CORE_ADDR start_pc, limit_pc;
961 struct frame_info *next_frame;
962{
963 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
964
965 if (start_pc == 0) return NULL;
966 memset (&temp_proc_desc, '\0', sizeof(temp_proc_desc));
967 memset (&temp_saved_regs, '\0', sizeof(struct frame_saved_regs));
968 PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
969 PROC_FRAME_REG (&temp_proc_desc) = SP_REGNUM;
970 PROC_PC_REG (&temp_proc_desc) = RA_REGNUM;
971
972 if (start_pc + 200 < limit_pc)
973 limit_pc = start_pc + 200;
c1fc0935 974 if (IS_MIPS16_ADDR (start_pc))
b348b9fd
MA
975 mips16_heuristic_proc_desc (start_pc, limit_pc, next_frame, sp);
976 else
977 mips32_heuristic_proc_desc (start_pc, limit_pc, next_frame, sp);
978 return &temp_proc_desc;
bd5635a1
RP
979}
980
0f552c5f 981static mips_extra_func_info_t
c1fc0935 982non_heuristic_proc_desc (pc, addrptr)
9f9f94aa 983 CORE_ADDR pc;
c1fc0935 984 CORE_ADDR *addrptr;
bd5635a1 985{
c1fc0935 986 CORE_ADDR startaddr;
bd5635a1 987 mips_extra_func_info_t proc_desc;
0f552c5f 988 struct block *b = block_for_pc(pc);
48be4c35 989 struct symbol *sym;
48be4c35
JK
990
991 find_pc_partial_function (pc, NULL, &startaddr, NULL);
c1fc0935
MA
992 if (addrptr)
993 *addrptr = startaddr;
97e091b2 994 if (b == NULL || PC_IN_CALL_DUMMY (pc, 0, 0))
48be4c35
JK
995 sym = NULL;
996 else
997 {
998 if (startaddr > BLOCK_START (b))
999 /* This is the "pathological" case referred to in a comment in
1000 print_frame_info. It might be better to move this check into
1001 symbol reading. */
1002 sym = NULL;
1003 else
c1fc0935 1004 sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE, 0, NULL);
48be4c35 1005 }
0f552c5f 1006
e4dbd248
PS
1007 /* If we never found a PDR for this function in symbol reading, then
1008 examine prologues to find the information. */
0f552c5f 1009 if (sym)
bd5635a1 1010 {
c1fc0935
MA
1011 proc_desc = (mips_extra_func_info_t) SYMBOL_VALUE (sym);
1012 if (PROC_FRAME_REG (proc_desc) == -1)
1013 return NULL;
1014 else
1015 return proc_desc;
1016 }
1017 else
1018 return NULL;
1019}
1020
1021
1022static mips_extra_func_info_t
1023find_proc_desc (pc, next_frame)
1024 CORE_ADDR pc;
1025 struct frame_info *next_frame;
1026{
1027 mips_extra_func_info_t proc_desc;
1028 CORE_ADDR startaddr;
1029
1030 proc_desc = non_heuristic_proc_desc (pc, &startaddr);
1031
1032 if (proc_desc)
1033 {
1034 /* IF this is the topmost frame AND
1035 * (this proc does not have debugging information OR
1036 * the PC is in the procedure prologue)
1037 * THEN create a "heuristic" proc_desc (by analyzing
1038 * the actual code) to replace the "official" proc_desc.
1039 */
1040 if (next_frame == NULL)
1041 {
1042 struct symtab_and_line val;
1043 struct symbol *proc_symbol =
1044 PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc);
1045
1046 if (proc_symbol)
1047 {
1048 val = find_pc_line (BLOCK_START
1049 (SYMBOL_BLOCK_VALUE(proc_symbol)),
1050 0);
1051 val.pc = val.end ? val.end : pc;
bd5635a1 1052 }
c1fc0935
MA
1053 if (!proc_symbol || pc < val.pc)
1054 {
1055 mips_extra_func_info_t found_heuristic =
1056 heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
1057 pc, next_frame);
1058 if (found_heuristic)
1059 proc_desc = found_heuristic;
bd5635a1
RP
1060 }
1061 }
1062 }
1063 else
1064 {
0f552c5f
JG
1065 /* Is linked_proc_desc_table really necessary? It only seems to be used
1066 by procedure call dummys. However, the procedures being called ought
1067 to have their own proc_descs, and even if they don't,
1068 heuristic_proc_desc knows how to create them! */
1069
bd5635a1 1070 register struct linked_proc_info *link;
9f9f94aa 1071
bd5635a1 1072 for (link = linked_proc_desc_table; link; link = link->next)
9f9f94aa
SS
1073 if (PROC_LOW_ADDR(&link->info) <= pc
1074 && PROC_HIGH_ADDR(&link->info) > pc)
1075 return &link->info;
23d35572 1076
48be4c35
JK
1077 if (startaddr == 0)
1078 startaddr = heuristic_proc_start (pc);
1079
bd5635a1 1080 proc_desc =
48be4c35 1081 heuristic_proc_desc (startaddr, pc, next_frame);
bd5635a1
RP
1082 }
1083 return proc_desc;
1084}
1085
96431497
MA
1086static CORE_ADDR
1087get_frame_pointer(frame, proc_desc)
1088 struct frame_info *frame;
1089 mips_extra_func_info_t proc_desc;
1090{
1091 return ADDR_BITS_REMOVE (read_next_frame_reg (frame,
1092 PROC_FRAME_REG(proc_desc)) + PROC_FRAME_OFFSET(proc_desc));
1093}
1094
bd5635a1
RP
1095mips_extra_func_info_t cached_proc_desc;
1096
9f9f94aa 1097CORE_ADDR
0f552c5f 1098mips_frame_chain(frame)
9f9f94aa 1099 struct frame_info *frame;
bd5635a1 1100{
f781fe93
MA
1101 mips_extra_func_info_t proc_desc;
1102 CORE_ADDR tmp;
1103 CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
1104
1105 if (saved_pc == 0 || inside_entry_file (saved_pc))
1106 return 0;
1107
1108 /* Check if the PC is inside a call stub. If it is, fetch the
1109 PC of the caller of that stub. */
1110 if ((tmp = mips_skip_stub (saved_pc)) != 0)
1111 saved_pc = tmp;
1112
1113 /* Look up the procedure descriptor for this PC. */
1114 proc_desc = find_proc_desc(saved_pc, frame);
1115 if (!proc_desc)
1116 return 0;
1117
1118 cached_proc_desc = proc_desc;
1119
1120 /* If no frame pointer and frame size is zero, we must be at end
1121 of stack (or otherwise hosed). If we don't check frame size,
1122 we loop forever if we see a zero size frame. */
1123 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
1124 && PROC_FRAME_OFFSET (proc_desc) == 0
1125 /* The previous frame from a sigtramp frame might be frameless
1126 and have frame size zero. */
1127 && !frame->signal_handler_caller)
1128 return 0;
1129 else
1130 return get_frame_pointer (frame, proc_desc);
bd5635a1
RP
1131}
1132
1133void
1134init_extra_frame_info(fci)
1135 struct frame_info *fci;
1136{
96431497
MA
1137 int regnum;
1138
bd5635a1 1139 /* Use proc_desc calculated in frame_chain */
ee5fb959
JK
1140 mips_extra_func_info_t proc_desc =
1141 fci->next ? cached_proc_desc : find_proc_desc(fci->pc, fci->next);
0f552c5f 1142
70126bf9 1143 fci->saved_regs = NULL;
bd5635a1 1144 fci->proc_desc =
ee5fb959 1145 proc_desc == &temp_proc_desc ? 0 : proc_desc;
bd5635a1
RP
1146 if (proc_desc)
1147 {
c2a0f1cb 1148 /* Fixup frame-pointer - only needed for top frame */
5efd597b
PS
1149 /* This may not be quite right, if proc has a real frame register.
1150 Get the value of the frame relative sp, procedure might have been
1151 interrupted by a signal at it's very start. */
70126bf9
KH
1152 if (fci->pc == PROC_LOW_ADDR (proc_desc)
1153 && !PROC_DESC_IS_DUMMY (proc_desc))
1154 fci->frame = read_next_frame_reg (fci->next, SP_REGNUM);
c2a0f1cb 1155 else
96431497 1156 fci->frame = get_frame_pointer (fci->next, proc_desc);
bd5635a1 1157
48be4c35 1158 if (proc_desc == &temp_proc_desc)
ee5fb959 1159 {
09af5868
PS
1160 char *name;
1161
1162 /* Do not set the saved registers for a sigtramp frame,
1163 mips_find_saved_registers will do that for us.
1164 We can't use fci->signal_handler_caller, it is not yet set. */
1165 find_pc_partial_function (fci->pc, &name,
1166 (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
1167 if (!IN_SIGTRAMP (fci->pc, name))
1168 {
1169 fci->saved_regs = (struct frame_saved_regs*)
1170 obstack_alloc (&frame_cache_obstack,
1171 sizeof (struct frame_saved_regs));
1172 *fci->saved_regs = temp_saved_regs;
1173 fci->saved_regs->regs[PC_REGNUM]
1174 = fci->saved_regs->regs[RA_REGNUM];
1175 }
ee5fb959 1176 }
bd5635a1
RP
1177
1178 /* hack: if argument regs are saved, guess these contain args */
96431497
MA
1179 fci->num_args = -1; /* assume we can't tell how many args for now */
1180 for (regnum = MIPS_LAST_ARG_REGNUM; regnum >= A0_REGNUM; regnum--)
1181 {
1182 if (PROC_REG_MASK(proc_desc) & (1 << regnum))
1183 {
1184 fci->num_args = regnum - A0_REGNUM + 1;
1185 break;
1186 }
1187 }
bd5635a1 1188 }
bd5635a1
RP
1189}
1190
a70dc898
RP
1191/* MIPS stack frames are almost impenetrable. When execution stops,
1192 we basically have to look at symbol information for the function
1193 that we stopped in, which tells us *which* register (if any) is
1194 the base of the frame pointer, and what offset from that register
1195 the frame itself is at.
1196
1197 This presents a problem when trying to examine a stack in memory
1198 (that isn't executing at the moment), using the "frame" command. We
1199 don't have a PC, nor do we have any registers except SP.
1200
1201 This routine takes two arguments, SP and PC, and tries to make the
1202 cached frames look as if these two arguments defined a frame on the
1203 cache. This allows the rest of info frame to extract the important
1204 arguments without difficulty. */
1205
9f9f94aa 1206struct frame_info *
c2a0f1cb
ILT
1207setup_arbitrary_frame (argc, argv)
1208 int argc;
9f9f94aa 1209 CORE_ADDR *argv;
a70dc898 1210{
c2a0f1cb
ILT
1211 if (argc != 2)
1212 error ("MIPS frame specifications require two arguments: sp and pc");
1213
1214 return create_new_frame (argv[0], argv[1]);
a70dc898
RP
1215}
1216
0f552c5f
JG
1217CORE_ADDR
1218mips_push_arguments(nargs, args, sp, struct_return, struct_addr)
97e091b2
MA
1219 int nargs;
1220 value_ptr *args;
1221 CORE_ADDR sp;
1222 int struct_return;
1223 CORE_ADDR struct_addr;
bd5635a1 1224{
97e091b2
MA
1225 int argreg;
1226 int float_argreg;
1227 int argnum;
1228 int len = 0;
1229 int stack_offset;
1230
1231 /* Macros to round N up or down to the next A boundary; A must be
1232 a power of two. */
1233#define ROUND_DOWN(n,a) ((n) & ~((a)-1))
1234#define ROUND_UP(n,a) (((n)+(a)-1) & ~((a)-1))
28444bf3
DP
1235
1236 /* First ensure that the stack and structure return address (if any)
97e091b2
MA
1237 are properly aligned. The stack has to be 64-bit aligned even
1238 on 32-bit machines, because doubles must be 64-bit aligned. */
1239 sp = ROUND_DOWN (sp, 8);
1240 struct_addr = ROUND_DOWN (struct_addr, MIPS_REGSIZE);
28444bf3 1241
97e091b2
MA
1242 /* Now make space on the stack for the args. We allocate more
1243 than necessary for EABI, because the first few arguments are
1244 passed in registers, but that's OK. */
1245 for (argnum = 0; argnum < nargs; argnum++)
1246 len += ROUND_UP (TYPE_LENGTH(VALUE_TYPE(args[argnum])), MIPS_REGSIZE);
61d2d19a 1247 sp -= ROUND_UP (len, 8);
97e091b2
MA
1248
1249 /* Initialize the integer and float register pointers. */
1250 argreg = A0_REGNUM;
1251 float_argreg = FPA0_REGNUM;
1252
1253 /* the struct_return pointer occupies the first parameter-passing reg */
5efd597b 1254 if (struct_return)
97e091b2
MA
1255 write_register (argreg++, struct_addr);
1256
1257 /* The offset onto the stack at which we will start copying parameters
1258 (after the registers are used up) begins at 16 in the old ABI.
1259 This leaves room for the "home" area for register parameters. */
1260 stack_offset = MIPS_EABI ? 0 : MIPS_REGSIZE * 4;
1261
1262 /* Now load as many as possible of the first arguments into
1263 registers, and push the rest onto the stack. Loop thru args
1264 from first to last. */
1265 for (argnum = 0; argnum < nargs; argnum++)
5efd597b 1266 {
97e091b2
MA
1267 char *val;
1268 char valbuf[REGISTER_RAW_SIZE(A0_REGNUM)];
1269 value_ptr arg = args[argnum];
1270 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1271 int len = TYPE_LENGTH (arg_type);
1272 enum type_code typecode = TYPE_CODE (arg_type);
1273
61d2d19a
PS
1274 /* The EABI passes structures that do not fit in a register by
1275 reference. In all other cases, pass the structure by value. */
1276 if (typecode == TYPE_CODE_STRUCT && MIPS_EABI && len > MIPS_REGSIZE)
97e091b2
MA
1277 {
1278 store_address (valbuf, MIPS_REGSIZE, VALUE_ADDRESS (arg));
08d8f995 1279 typecode = TYPE_CODE_PTR;
97e091b2
MA
1280 len = MIPS_REGSIZE;
1281 val = valbuf;
1282 }
1283 else
1284 val = (char *)VALUE_CONTENTS (arg);
1285
1286 /* 32-bit ABIs always start floating point arguments in an
1287 even-numbered floating point register. */
1288 if (!GDB_TARGET_IS_MIPS64 && typecode == TYPE_CODE_FLT
1289 && (float_argreg & 1))
1290 float_argreg++;
1291
1292 /* Floating point arguments passed in registers have to be
1293 treated specially. On 32-bit architectures, doubles
1294 are passed in register pairs; the even register gets
c81a76b3
MA
1295 the low word, and the odd register gets the high word.
1296 On non-EABI processors, the first two floating point arguments are
1297 also copied to general registers, because MIPS16 functions
1298 don't use float registers for arguments. This duplication of
1299 arguments in general registers can't hurt non-MIPS16 functions
1300 because those registers are normally skipped. */
97e091b2
MA
1301 if (typecode == TYPE_CODE_FLT
1302 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM
1303 && mips_fpu != MIPS_FPU_NONE)
1304 {
1305 if (!GDB_TARGET_IS_MIPS64 && len == 8)
1306 {
1307 int low_offset = TARGET_BYTE_ORDER == BIG_ENDIAN ? 4 : 0;
1308 unsigned long regval;
1309
c81a76b3 1310 /* Write the low word of the double to the even register(s). */
97e091b2 1311 regval = extract_unsigned_integer (val+low_offset, 4);
c81a76b3
MA
1312 write_register (float_argreg++, regval);
1313 if (!MIPS_EABI)
1314 write_register (argreg+1, regval);
1315
1316 /* Write the high word of the double to the odd register(s). */
97e091b2 1317 regval = extract_unsigned_integer (val+4-low_offset, 4);
c81a76b3
MA
1318 write_register (float_argreg++, regval);
1319 if (!MIPS_EABI)
1320 {
1321 write_register (argreg, regval);
1322 argreg += 2;
1323 }
97e091b2
MA
1324
1325 }
1326 else
1327 {
c81a76b3
MA
1328 /* This is a floating point value that fits entirely
1329 in a single register. */
97e091b2
MA
1330 CORE_ADDR regval = extract_address (val, len);
1331 write_register (float_argreg++, regval);
c81a76b3
MA
1332 if (!MIPS_EABI)
1333 {
1334 write_register (argreg, regval);
1335 argreg += GDB_TARGET_IS_MIPS64 ? 1 : 2;
1336 }
97e091b2 1337 }
97e091b2
MA
1338 }
1339 else
1340 {
1341 /* Copy the argument to general registers or the stack in
1342 register-sized pieces. Large arguments are split between
1343 registers and stack. */
1344 while (len > 0)
1345 {
1346 int partial_len = len < MIPS_REGSIZE ? len : MIPS_REGSIZE;
61d2d19a 1347
97e091b2
MA
1348 if (argreg <= MIPS_LAST_ARG_REGNUM)
1349 {
61d2d19a
PS
1350 CORE_ADDR regval = extract_address (val, partial_len);
1351
158bda58
MS
1352 /* A simple argument being passed in a general register.
1353 If the length is smaller than the register size, we
1354 have to adjust the alignment on big endian targets.
1355
1356 For structs, it appears that we have to
1357 do the same even in little endian mode.
1358
b348b9fd 1359 But don't do this adjustment on EABI targets. */
158bda58
MS
1360
1361 if (!MIPS_EABI &&
1362 TYPE_LENGTH (arg_type) < MIPS_REGSIZE &&
1363 (TARGET_BYTE_ORDER == BIG_ENDIAN ||
1364 typecode == TYPE_CODE_STRUCT ||
1365 typecode == TYPE_CODE_UNION))
1366 regval <<= ((MIPS_REGSIZE - partial_len) *
1367 TARGET_CHAR_BIT);
1368
97e091b2
MA
1369 write_register (argreg, regval);
1370 argreg++;
1371
1372 /* If this is the old ABI, prevent subsequent floating
1373 point arguments from being passed in floating point
1374 registers. */
1375 if (!MIPS_EABI)
1376 float_argreg = MIPS_LAST_FP_ARG_REGNUM + 1;
1377 }
1378 else
1379 {
61d2d19a 1380 /* Write this portion of the argument to the stack. */
08d8f995
MS
1381 int longword_offset;
1382
61d2d19a 1383 partial_len = len;
08d8f995
MS
1384 longword_offset = 0;
1385 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1386 if (MIPS_REGSIZE == 8 &&
1387 (typecode == TYPE_CODE_INT ||
1388 typecode == TYPE_CODE_PTR ||
1389 typecode == TYPE_CODE_FLT) && len <= 4)
1390 longword_offset = 4;
1391 else if ((typecode == TYPE_CODE_STRUCT ||
1392 typecode == TYPE_CODE_UNION) &&
1393 len < MIPS_REGSIZE)
1394 longword_offset = MIPS_REGSIZE - len;
08d8f995
MS
1395 write_memory (sp + stack_offset + longword_offset,
1396 val, partial_len);
61d2d19a 1397 stack_offset += ROUND_UP (partial_len, MIPS_REGSIZE);
97e091b2
MA
1398 }
1399
1400 len -= partial_len;
1401 val += partial_len;
1402 }
1403 }
5efd597b 1404 }
97e091b2
MA
1405
1406 /* Set the return address register to point to the entry
1407 point of the program, where a breakpoint lies in wait. */
1408 write_register (RA_REGNUM, CALL_DUMMY_ADDRESS());
1409
1410 /* Return adjusted stack pointer. */
bd5635a1
RP
1411 return sp;
1412}
1413
c81a76b3 1414static void
28444bf3
DP
1415mips_push_register(CORE_ADDR *sp, int regno)
1416{
1417 char buffer[MAX_REGISTER_RAW_SIZE];
1418 int regsize = REGISTER_RAW_SIZE (regno);
1419
1420 *sp -= regsize;
1421 read_register_gen (regno, buffer);
1422 write_memory (*sp, buffer, regsize);
1423}
1424
1425/* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<(MIPS_NUMREGS-1). */
e03c0cc6 1426#define MASK(i,j) (((1 << ((j)+1))-1) ^ ((1 << (i))-1))
bd5635a1
RP
1427
1428void
1429mips_push_dummy_frame()
1430{
1431 int ireg;
1432 struct linked_proc_info *link = (struct linked_proc_info*)
1433 xmalloc(sizeof(struct linked_proc_info));
1434 mips_extra_func_info_t proc_desc = &link->info;
96431497 1435 CORE_ADDR sp = ADDR_BITS_REMOVE (read_register (SP_REGNUM));
28444bf3 1436 CORE_ADDR old_sp = sp;
bd5635a1
RP
1437 link->next = linked_proc_desc_table;
1438 linked_proc_desc_table = link;
28444bf3
DP
1439
1440/* FIXME! are these correct ? */
bd5635a1 1441#define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
28444bf3 1442#define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<(MIPS_NUMREGS-1))
bd5635a1 1443#define FLOAT_REG_SAVE_MASK MASK(0,19)
e4dbd248
PS
1444#define FLOAT_SINGLE_REG_SAVE_MASK \
1445 ((1<<18)|(1<<16)|(1<<14)|(1<<12)|(1<<10)|(1<<8)|(1<<6)|(1<<4)|(1<<2)|(1<<0))
bd5635a1
RP
1446 /*
1447 * The registers we must save are all those not preserved across
1448 * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
28444bf3
DP
1449 * In addition, we must save the PC, PUSH_FP_REGNUM, MMLO/-HI
1450 * and FP Control/Status registers.
1451 *
bd5635a1
RP
1452 *
1453 * Dummy frame layout:
1454 * (high memory)
1455 * Saved PC
1456 * Saved MMHI, MMLO, FPC_CSR
1457 * Saved R31
1458 * Saved R28
1459 * ...
1460 * Saved R1
1461 * Saved D18 (i.e. F19, F18)
1462 * ...
1463 * Saved D0 (i.e. F1, F0)
f9f8c14b 1464 * Argument build area and stack arguments written via mips_push_arguments
bd5635a1
RP
1465 * (low memory)
1466 */
28444bf3
DP
1467
1468 /* Save special registers (PC, MMHI, MMLO, FPC_CSR) */
28444bf3
DP
1469 PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM;
1470 PROC_FRAME_OFFSET(proc_desc) = 0;
1471 mips_push_register (&sp, PC_REGNUM);
1472 mips_push_register (&sp, HI_REGNUM);
1473 mips_push_register (&sp, LO_REGNUM);
1474 mips_push_register (&sp, mips_fpu == MIPS_FPU_NONE ? 0 : FCRCS_REGNUM);
1475
1476 /* Save general CPU registers */
bd5635a1 1477 PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
28444bf3 1478 PROC_REG_OFFSET(proc_desc) = sp - old_sp; /* offset of (Saved R31) from FP */
bd5635a1
RP
1479 for (ireg = 32; --ireg >= 0; )
1480 if (PROC_REG_MASK(proc_desc) & (1 << ireg))
28444bf3 1481 mips_push_register (&sp, ireg);
3fed1c4a 1482
28444bf3
DP
1483 /* Save floating point registers starting with high order word */
1484 PROC_FREG_MASK(proc_desc) =
1485 mips_fpu == MIPS_FPU_DOUBLE ? FLOAT_REG_SAVE_MASK
1486 : mips_fpu == MIPS_FPU_SINGLE ? FLOAT_SINGLE_REG_SAVE_MASK : 0;
1487 PROC_FREG_OFFSET(proc_desc) = sp - old_sp; /* offset of (Saved D18) from FP */
bd5635a1
RP
1488 for (ireg = 32; --ireg >= 0; )
1489 if (PROC_FREG_MASK(proc_desc) & (1 << ireg))
28444bf3 1490 mips_push_register (&sp, ireg + FP0_REGNUM);
3fed1c4a 1491
f9f8c14b
MA
1492 /* Update the frame pointer for the call dummy and the stack pointer.
1493 Set the procedure's starting and ending addresses to point to the
1494 call dummy address at the entry point. */
1495 write_register (PUSH_FP_REGNUM, old_sp);
bd5635a1 1496 write_register (SP_REGNUM, sp);
97e091b2
MA
1497 PROC_LOW_ADDR(proc_desc) = CALL_DUMMY_ADDRESS();
1498 PROC_HIGH_ADDR(proc_desc) = CALL_DUMMY_ADDRESS() + 4;
bd5635a1
RP
1499 SET_PROC_DESC_IS_DUMMY(proc_desc);
1500 PROC_PC_REG(proc_desc) = RA_REGNUM;
1501}
1502
1503void
1504mips_pop_frame()
dac4929a
SG
1505{
1506 register int regnum;
9f9f94aa
SS
1507 struct frame_info *frame = get_current_frame ();
1508 CORE_ADDR new_sp = FRAME_FP (frame);
dac4929a 1509
a70dc898 1510 mips_extra_func_info_t proc_desc = frame->proc_desc;
dac4929a
SG
1511
1512 write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
70126bf9
KH
1513 if (frame->saved_regs == NULL)
1514 mips_find_saved_regs (frame);
61d2d19a 1515 for (regnum = 0; regnum < NUM_REGS; regnum++)
dac4929a 1516 {
61d2d19a
PS
1517 if (regnum != SP_REGNUM && regnum != PC_REGNUM
1518 && frame->saved_regs->regs[regnum])
1519 write_register (regnum,
1520 read_memory_integer (frame->saved_regs->regs[regnum],
1521 MIPS_REGSIZE));
dac4929a
SG
1522 }
1523 write_register (SP_REGNUM, new_sp);
1524 flush_cached_frames ();
dac4929a 1525
199b2450 1526 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
bd5635a1 1527 {
dac4929a
SG
1528 struct linked_proc_info *pi_ptr, *prev_ptr;
1529
1530 for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
1531 pi_ptr != NULL;
1532 prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
1533 {
1534 if (&pi_ptr->info == proc_desc)
1535 break;
1536 }
1537
1538 if (pi_ptr == NULL)
1539 error ("Can't locate dummy extra frame info\n");
1540
1541 if (prev_ptr != NULL)
1542 prev_ptr->next = pi_ptr->next;
1543 else
1544 linked_proc_desc_table = pi_ptr->next;
1545
1546 free (pi_ptr);
1547
28444bf3
DP
1548 write_register (HI_REGNUM,
1549 read_memory_integer (new_sp - 2*MIPS_REGSIZE, MIPS_REGSIZE));
1550 write_register (LO_REGNUM,
1551 read_memory_integer (new_sp - 3*MIPS_REGSIZE, MIPS_REGSIZE));
e4dbd248 1552 if (mips_fpu != MIPS_FPU_NONE)
28444bf3
DP
1553 write_register (FCRCS_REGNUM,
1554 read_memory_integer (new_sp - 4*MIPS_REGSIZE, MIPS_REGSIZE));
bd5635a1 1555 }
bd5635a1
RP
1556}
1557
0f552c5f 1558static void
a70dc898 1559mips_print_register (regnum, all)
bd5635a1
RP
1560 int regnum, all;
1561{
e4dbd248 1562 char raw_buffer[MAX_REGISTER_RAW_SIZE];
bd5635a1 1563
48be4c35
JK
1564 /* Get the data in raw format. */
1565 if (read_relative_register_raw_bytes (regnum, raw_buffer))
1566 {
1567 printf_filtered ("%s: [Invalid]", reg_names[regnum]);
1568 return;
1569 }
1570
1571 /* If an even floating pointer register, also print as double. */
28444bf3 1572 if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM+MIPS_NUMREGS
e4dbd248
PS
1573 && !((regnum-FP0_REGNUM) & 1))
1574 {
f781fe93 1575 char dbuffer[2 * MAX_REGISTER_RAW_SIZE];
48be4c35 1576
f781fe93
MA
1577 read_relative_register_raw_bytes (regnum, dbuffer);
1578 read_relative_register_raw_bytes (regnum+1, dbuffer+MIPS_REGSIZE);
1579 REGISTER_CONVERT_TO_TYPE (regnum, builtin_type_double, dbuffer);
c81a76b3 1580
e4dbd248
PS
1581 printf_filtered ("(d%d: ", regnum-FP0_REGNUM);
1582 val_print (builtin_type_double, dbuffer, 0,
1583 gdb_stdout, 0, 1, 0, Val_pretty_default);
1584 printf_filtered ("); ");
1585 }
199b2450 1586 fputs_filtered (reg_names[regnum], gdb_stdout);
48be4c35
JK
1587
1588 /* The problem with printing numeric register names (r26, etc.) is that
1589 the user can't use them on input. Probably the best solution is to
1590 fix it so that either the numeric or the funky (a2, etc.) names
1591 are accepted on input. */
28444bf3 1592 if (regnum < MIPS_NUMREGS)
48be4c35
JK
1593 printf_filtered ("(r%d): ", regnum);
1594 else
1595 printf_filtered (": ");
bd5635a1 1596
48be4c35 1597 /* If virtual format is floating, print it that way. */
ac57e5ad 1598 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
48be4c35 1599 val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0,
199b2450 1600 gdb_stdout, 0, 1, 0, Val_pretty_default);
48be4c35
JK
1601 /* Else print as integer in hex. */
1602 else
ac57e5ad
SS
1603 print_scalar_formatted (raw_buffer, REGISTER_VIRTUAL_TYPE (regnum),
1604 'x', 0, gdb_stdout);
bd5635a1
RP
1605}
1606
d8b3b00e 1607/* Replacement for generic do_registers_info. */
9f9f94aa 1608
0f552c5f 1609void
361bf6ee 1610mips_do_registers_info (regnum, fpregs)
bd5635a1 1611 int regnum;
361bf6ee 1612 int fpregs;
bd5635a1 1613{
9f9f94aa
SS
1614 if (regnum != -1)
1615 {
1616 if (*(reg_names[regnum]) == '\0')
1617 error ("Not a valid register for the current processor type");
1618
bd5635a1
RP
1619 mips_print_register (regnum, 0);
1620 printf_filtered ("\n");
9f9f94aa
SS
1621 }
1622 else
1623 {
09af5868 1624 int did_newline = 0;
9f9f94aa
SS
1625
1626 for (regnum = 0; regnum < NUM_REGS; )
1627 {
1628 if (((!fpregs) && regnum >= FP0_REGNUM && regnum <= FCRIR_REGNUM)
1629 || *(reg_names[regnum]) == '\0')
1630 {
1631 regnum++;
1632 continue;
1633 }
bd5635a1
RP
1634 mips_print_register (regnum, 1);
1635 regnum++;
9f9f94aa
SS
1636 printf_filtered ("; ");
1637 did_newline = 0;
1638 if ((regnum & 3) == 0)
1639 {
1640 printf_filtered ("\n");
1641 did_newline = 1;
1642 }
1643 }
1644 if (!did_newline)
1645 printf_filtered ("\n");
1646 }
bd5635a1 1647}
9f9f94aa 1648
bd5635a1
RP
1649/* Return number of args passed to a frame. described by FIP.
1650 Can return -1, meaning no way to tell. */
1651
0f552c5f 1652int
9f9f94aa
SS
1653mips_frame_num_args (frame)
1654 struct frame_info *frame;
bd5635a1 1655{
9f9f94aa
SS
1656#if 0 /* FIXME Use or lose this! */
1657 struct chain_info_t *p;
bd5635a1 1658
9f9f94aa
SS
1659 p = mips_find_cached_frame (FRAME_FP (frame));
1660 if (p->valid)
1661 return p->the_info.numargs;
bd5635a1 1662#endif
9f9f94aa 1663 return -1;
bd5635a1 1664}
96431497 1665
427fec5d 1666/* Is this a branch with a delay slot? */
9f9f94aa 1667
b5aff268
JK
1668static int is_delayed PARAMS ((unsigned long));
1669
ee5fb959
JK
1670static int
1671is_delayed (insn)
1672 unsigned long insn;
1673{
1674 int i;
1675 for (i = 0; i < NUMOPCODES; ++i)
1676 if (mips_opcodes[i].pinfo != INSN_MACRO
1677 && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
1678 break;
427fec5d
JK
1679 return (i < NUMOPCODES
1680 && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
1681 | INSN_COND_BRANCH_DELAY
1682 | INSN_COND_BRANCH_LIKELY)));
ee5fb959 1683}
b5aff268
JK
1684
1685int
1686mips_step_skips_delay (pc)
1687 CORE_ADDR pc;
1688{
96431497 1689 char buf[MIPS_INSTLEN];
b5aff268 1690
f9f8c14b 1691 /* There is no branch delay slot on MIPS16. */
c1fc0935 1692 if (IS_MIPS16_ADDR (pc))
f9f8c14b
MA
1693 return 0;
1694
96431497 1695 if (target_read_memory (pc, buf, MIPS_INSTLEN) != 0)
b5aff268
JK
1696 /* If error reading memory, guess that it is not a delayed branch. */
1697 return 0;
96431497 1698 return is_delayed ((unsigned long)extract_unsigned_integer (buf, MIPS_INSTLEN));
b5aff268 1699}
ee5fb959 1700
bd5635a1 1701
20fa0902
MA
1702/* Skip the PC past function prologue instructions (32-bit version).
1703 This is a helper function for mips_skip_prologue. */
1704
1705static CORE_ADDR
1706mips32_skip_prologue (pc, lenient)
1707 CORE_ADDR pc; /* starting PC to search from */
ee5fb959 1708 int lenient;
bd5635a1 1709{
28444bf3 1710 t_inst inst;
20fa0902 1711 CORE_ADDR end_pc;
0b0d6c3f 1712 int seen_sp_adjust = 0;
ac57e5ad 1713 int load_immediate_bytes = 0;
bd5635a1 1714
e157305c
PS
1715 /* Skip the typical prologue instructions. These are the stack adjustment
1716 instruction and the instructions that save registers on the stack
1717 or in the gcc frame. */
20fa0902 1718 for (end_pc = pc + 100; pc < end_pc; pc += MIPS_INSTLEN)
ee5fb959 1719 {
b348b9fd 1720 unsigned long high_word;
ee5fb959 1721
c81a76b3 1722 inst = mips_fetch_instruction (pc);
b348b9fd 1723 high_word = (inst >> 16) & 0xffff;
ee5fb959 1724
002a422b 1725#if 0
ee5fb959
JK
1726 if (lenient && is_delayed (inst))
1727 continue;
002a422b 1728#endif
ee5fb959 1729
b348b9fd
MA
1730 if (high_word == 0x27bd /* addiu $sp,$sp,offset */
1731 || high_word == 0x67bd) /* daddiu $sp,$sp,offset */
0b0d6c3f 1732 seen_sp_adjust = 1;
ac57e5ad
SS
1733 else if (inst == 0x03a1e823 || /* subu $sp,$sp,$at */
1734 inst == 0x03a8e823) /* subu $sp,$sp,$t0 */
1735 seen_sp_adjust = 1;
97e091b2
MA
1736 else if (((inst & 0xFFE00000) == 0xAFA00000 /* sw reg,n($sp) */
1737 || (inst & 0xFFE00000) == 0xFFA00000) /* sd reg,n($sp) */
1738 && (inst & 0x001F0000)) /* reg != $zero */
1739 continue;
1740
e157305c
PS
1741 else if ((inst & 0xFFE00000) == 0xE7A00000) /* swc1 freg,n($sp) */
1742 continue;
1743 else if ((inst & 0xF3E00000) == 0xA3C00000 && (inst & 0x001F0000))
1744 /* sx reg,n($s8) */
1745 continue; /* reg != $zero */
1d9489c1
JK
1746
1747 /* move $s8,$sp. With different versions of gas this will be either
20fa0902
MA
1748 `addu $s8,$sp,$zero' or `or $s8,$sp,$zero' or `daddu s8,sp,$0'.
1749 Accept any one of these. */
1750 else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
0b0d6c3f 1751 continue;
1d9489c1 1752
1b71de8e
PS
1753 else if ((inst & 0xFF9F07FF) == 0x00800021) /* move reg,$a0-$a3 */
1754 continue;
b348b9fd 1755 else if (high_word == 0x3c1c) /* lui $gp,n */
e03c0cc6 1756 continue;
b348b9fd 1757 else if (high_word == 0x279c) /* addiu $gp,$gp,n */
e03c0cc6
ILT
1758 continue;
1759 else if (inst == 0x0399e021 /* addu $gp,$gp,$t9 */
1760 || inst == 0x033ce021) /* addu $gp,$t9,$gp */
1761 continue;
ac57e5ad
SS
1762 /* The following instructions load $at or $t0 with an immediate
1763 value in preparation for a stack adjustment via
1764 subu $sp,$sp,[$at,$t0]. These instructions could also initialize
1765 a local variable, so we accept them only before a stack adjustment
1766 instruction was seen. */
1767 else if (!seen_sp_adjust)
1768 {
b348b9fd
MA
1769 if (high_word == 0x3c01 || /* lui $at,n */
1770 high_word == 0x3c08) /* lui $t0,n */
ac57e5ad 1771 {
28444bf3 1772 load_immediate_bytes += MIPS_INSTLEN; /* FIXME!! */
ac57e5ad
SS
1773 continue;
1774 }
b348b9fd
MA
1775 else if (high_word == 0x3421 || /* ori $at,$at,n */
1776 high_word == 0x3508 || /* ori $t0,$t0,n */
1777 high_word == 0x3401 || /* ori $at,$zero,n */
1778 high_word == 0x3408) /* ori $t0,$zero,n */
ac57e5ad 1779 {
28444bf3 1780 load_immediate_bytes += MIPS_INSTLEN; /* FIXME!! */
ac57e5ad
SS
1781 continue;
1782 }
1783 else
1784 break;
1785 }
0b0d6c3f 1786 else
ac57e5ad 1787 break;
d747e0af 1788 }
e157305c 1789
ac57e5ad
SS
1790 /* In a frameless function, we might have incorrectly
1791 skipped some load immediate instructions. Undo the skipping
1792 if the load immediate was not followed by a stack adjustment. */
1793 if (load_immediate_bytes && !seen_sp_adjust)
20fa0902
MA
1794 pc -= load_immediate_bytes;
1795 return pc;
1796}
1797
1798/* Skip the PC past function prologue instructions (16-bit version).
1799 This is a helper function for mips_skip_prologue. */
1800
1801static CORE_ADDR
1802mips16_skip_prologue (pc, lenient)
1803 CORE_ADDR pc; /* starting PC to search from */
1804 int lenient;
1805{
1806 CORE_ADDR end_pc;
c81a76b3
MA
1807 int extend_bytes = 0;
1808 int prev_extend_bytes;
20fa0902
MA
1809
1810 /* Table of instructions likely to be found in a function prologue. */
1811 static struct
1812 {
1813 unsigned short inst;
1814 unsigned short mask;
1815 } table[] =
1816 {
1817 { 0x6300, 0xff00 }, /* addiu $sp,offset */
1818 { 0xfb00, 0xff00 }, /* daddiu $sp,offset */
1819 { 0xd000, 0xf800 }, /* sw reg,n($sp) */
1820 { 0xf900, 0xff00 }, /* sd reg,n($sp) */
1821 { 0x6200, 0xff00 }, /* sw $ra,n($sp) */
1822 { 0xfa00, 0xff00 }, /* sd $ra,n($sp) */
1823 { 0x673d, 0xffff }, /* move $s1,sp */
1824 { 0xd980, 0xff80 }, /* sw $a0-$a3,n($s1) */
1825 { 0x6704, 0xff1c }, /* move reg,$a0-$a3 */
1826 { 0xe809, 0xf81f }, /* entry pseudo-op */
32dab603 1827 { 0x0100, 0xff00 }, /* addiu $s1,$sp,n */
20fa0902
MA
1828 { 0, 0 } /* end of table marker */
1829 };
1830
1831 /* Skip the typical prologue instructions. These are the stack adjustment
1832 instruction and the instructions that save registers on the stack
1833 or in the gcc frame. */
1834 for (end_pc = pc + 100; pc < end_pc; pc += MIPS16_INSTLEN)
1835 {
20fa0902 1836 unsigned short inst;
20fa0902
MA
1837 int i;
1838
c81a76b3 1839 inst = mips_fetch_instruction (pc);
20fa0902
MA
1840
1841 /* Normally we ignore an extend instruction. However, if it is
1842 not followed by a valid prologue instruction, we must adjust
1843 the pc back over the extend so that it won't be considered
1844 part of the prologue. */
1845 if ((inst & 0xf800) == 0xf000) /* extend */
1846 {
1847 extend_bytes = MIPS16_INSTLEN;
1848 continue;
1849 }
1850 prev_extend_bytes = extend_bytes;
1851 extend_bytes = 0;
1852
1853 /* Check for other valid prologue instructions besides extend. */
1854 for (i = 0; table[i].mask != 0; i++)
1855 if ((inst & table[i].mask) == table[i].inst) /* found, get out */
1856 break;
1857 if (table[i].mask != 0) /* it was in table? */
32dab603 1858 continue; /* ignore it */
20fa0902
MA
1859 else /* non-prologue */
1860 {
1861 /* Return the current pc, adjusted backwards by 2 if
1862 the previous instruction was an extend. */
1863 return pc - prev_extend_bytes;
1864 }
1865 }
32dab603 1866 return pc;
20fa0902
MA
1867}
1868
1869/* To skip prologues, I use this predicate. Returns either PC itself
1870 if the code at PC does not look like a function prologue; otherwise
1871 returns an address that (if we're lucky) follows the prologue. If
1872 LENIENT, then we must skip everything which is involved in setting
1873 up the frame (it's OK to skip more, just so long as we don't skip
1874 anything which might clobber the registers which are being saved.
1875 We must skip more in the case where part of the prologue is in the
1876 delay slot of a non-prologue instruction). */
1877
1878CORE_ADDR
1879mips_skip_prologue (pc, lenient)
1880 CORE_ADDR pc;
1881 int lenient;
1882{
1883 /* See if we can determine the end of the prologue via the symbol table.
1884 If so, then return either PC, or the PC after the prologue, whichever
1885 is greater. */
1886
1887 CORE_ADDR post_prologue_pc = after_prologue (pc, NULL);
1888
1889 if (post_prologue_pc != 0)
1890 return max (pc, post_prologue_pc);
1891
1892 /* Can't determine prologue from the symbol table, need to examine
1893 instructions. */
1894
c1fc0935 1895 if (IS_MIPS16_ADDR (pc))
20fa0902
MA
1896 return mips16_skip_prologue (pc, lenient);
1897 else
1898 return mips32_skip_prologue (pc, lenient);
bd5635a1 1899}
c2a0f1cb 1900
002a422b 1901#if 0
20fa0902 1902/* The lenient prologue stuff should be superseded by the code in
002a422b
JK
1903 init_extra_frame_info which looks to see whether the stores mentioned
1904 in the proc_desc have actually taken place. */
1905
ee5fb959
JK
1906/* Is address PC in the prologue (loosely defined) for function at
1907 STARTADDR? */
1908
1909static int
1910mips_in_lenient_prologue (startaddr, pc)
1911 CORE_ADDR startaddr;
1912 CORE_ADDR pc;
1913{
1914 CORE_ADDR end_prologue = mips_skip_prologue (startaddr, 1);
1915 return pc >= startaddr && pc < end_prologue;
1916}
002a422b 1917#endif
ee5fb959 1918
ac8cf67d
PS
1919/* Given a return value in `regbuf' with a type `valtype',
1920 extract and copy its value into `valbuf'. */
1921void
1922mips_extract_return_value (valtype, regbuf, valbuf)
1923 struct type *valtype;
1924 char regbuf[REGISTER_BYTES];
1925 char *valbuf;
1926{
1927 int regnum;
92a6d600 1928 int offset = 0;
c81a76b3 1929 int len = TYPE_LENGTH (valtype);
ac8cf67d 1930
e4dbd248
PS
1931 regnum = 2;
1932 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
0af60e03
MS
1933 && (mips_fpu == MIPS_FPU_DOUBLE
1934 || (mips_fpu == MIPS_FPU_SINGLE && len <= MIPS_REGSIZE)))
f781fe93 1935 regnum = FP0_REGNUM;
ac8cf67d 1936
0af60e03
MS
1937 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1938 { /* "un-left-justify" the value from the register */
1939 if (len < REGISTER_RAW_SIZE (regnum) &&
1940 TYPE_CODE (valtype) != TYPE_CODE_FLT)
1941 offset = REGISTER_RAW_SIZE (regnum) - len;
1942 if (len > REGISTER_RAW_SIZE (regnum) && /* odd-size structs */
1943 len < REGISTER_RAW_SIZE (regnum) * 2 &&
1944 (TYPE_CODE (valtype) == TYPE_CODE_STRUCT ||
1945 TYPE_CODE (valtype) == TYPE_CODE_UNION))
1946 offset = 2 * REGISTER_RAW_SIZE (regnum) - len;
1947 }
c81a76b3 1948 memcpy (valbuf, regbuf + REGISTER_BYTE (regnum) + offset, len);
f781fe93 1949 REGISTER_CONVERT_TO_TYPE (regnum, valtype, valbuf);
ac8cf67d
PS
1950}
1951
1952/* Given a return value in `regbuf' with a type `valtype',
1953 write it's value into the appropriate register. */
1954void
1955mips_store_return_value (valtype, valbuf)
1956 struct type *valtype;
1957 char *valbuf;
1958{
1959 int regnum;
0af60e03
MS
1960 int offset = 0;
1961 int len = TYPE_LENGTH (valtype);
ac8cf67d
PS
1962 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1963
e4dbd248
PS
1964 regnum = 2;
1965 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
0af60e03
MS
1966 && (mips_fpu == MIPS_FPU_DOUBLE
1967 || (mips_fpu == MIPS_FPU_SINGLE && len <= MIPS_REGSIZE)))
e4dbd248
PS
1968 regnum = FP0_REGNUM;
1969
0af60e03
MS
1970 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1971 { /* "left-justify" the value in the register */
1972 if (len < REGISTER_RAW_SIZE (regnum))
1973 offset = REGISTER_RAW_SIZE (regnum) - len;
1974 if (len > REGISTER_RAW_SIZE (regnum) && /* odd-size structs */
1975 len < REGISTER_RAW_SIZE (regnum) * 2 &&
1976 (TYPE_CODE (valtype) == TYPE_CODE_STRUCT ||
1977 TYPE_CODE (valtype) == TYPE_CODE_UNION))
1978 offset = 2 * REGISTER_RAW_SIZE (regnum) - len;
1979 }
1980 memcpy(raw_buffer + offset, valbuf, len);
ac8cf67d 1981 REGISTER_CONVERT_FROM_TYPE(regnum, valtype, raw_buffer);
0af60e03
MS
1982 write_register_bytes(REGISTER_BYTE (regnum), raw_buffer,
1983 len > REGISTER_RAW_SIZE (regnum) ?
1984 len : REGISTER_RAW_SIZE (regnum));
ac8cf67d
PS
1985}
1986
e03c0cc6
ILT
1987/* Exported procedure: Is PC in the signal trampoline code */
1988
1989int
1990in_sigtramp (pc, ignore)
1991 CORE_ADDR pc;
1992 char *ignore; /* function name */
1993{
1994 if (sigtramp_address == 0)
1995 fixup_sigtramp ();
1996 return (pc >= sigtramp_address && pc < sigtramp_end);
1997}
1998
e4dbd248
PS
1999/* Command to set FPU type. mips_fpu_string will have been set to the
2000 user's argument. Set mips_fpu based on mips_fpu_string, and then
2001 canonicalize mips_fpu_string. */
2002
2003/*ARGSUSED*/
2004static void
2005mips_set_fpu_command (args, from_tty, c)
2006 char *args;
2007 int from_tty;
2008 struct cmd_list_element *c;
2009{
2010 char *err = NULL;
2011
2012 if (mips_fpu_string == NULL || *mips_fpu_string == '\0')
2013 mips_fpu = MIPS_FPU_DOUBLE;
2014 else if (strcasecmp (mips_fpu_string, "double") == 0
2015 || strcasecmp (mips_fpu_string, "on") == 0
2016 || strcasecmp (mips_fpu_string, "1") == 0
2017 || strcasecmp (mips_fpu_string, "yes") == 0)
2018 mips_fpu = MIPS_FPU_DOUBLE;
2019 else if (strcasecmp (mips_fpu_string, "none") == 0
2020 || strcasecmp (mips_fpu_string, "off") == 0
2021 || strcasecmp (mips_fpu_string, "0") == 0
2022 || strcasecmp (mips_fpu_string, "no") == 0)
2023 mips_fpu = MIPS_FPU_NONE;
2024 else if (strcasecmp (mips_fpu_string, "single") == 0)
2025 mips_fpu = MIPS_FPU_SINGLE;
2026 else
2027 err = strsave (mips_fpu_string);
2028
2029 if (mips_fpu_string != NULL)
2030 free (mips_fpu_string);
2031
2032 switch (mips_fpu)
2033 {
2034 case MIPS_FPU_DOUBLE:
2035 mips_fpu_string = strsave ("double");
2036 break;
2037 case MIPS_FPU_SINGLE:
2038 mips_fpu_string = strsave ("single");
2039 break;
2040 case MIPS_FPU_NONE:
2041 mips_fpu_string = strsave ("none");
2042 break;
2043 }
2044
2045 if (err != NULL)
2046 {
2047 struct cleanup *cleanups = make_cleanup (free, err);
2048 error ("Unknown FPU type `%s'. Use `double', `none', or `single'.",
2049 err);
2050 do_cleanups (cleanups);
2051 }
2052}
2053
2054static void
2055mips_show_fpu_command (args, from_tty, c)
2056 char *args;
2057 int from_tty;
2058 struct cmd_list_element *c;
2059{
2060}
2061
9f9f94aa
SS
2062/* Command to set the processor type. */
2063
2064void
2065mips_set_processor_type_command (args, from_tty)
2066 char *args;
2067 int from_tty;
2068{
2069 int i;
2070
2071 if (tmp_mips_processor_type == NULL || *tmp_mips_processor_type == '\0')
2072 {
2073 printf_unfiltered ("The known MIPS processor types are as follows:\n\n");
2074 for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
2075 printf_unfiltered ("%s\n", mips_processor_type_table[i].name);
2076
2077 /* Restore the value. */
2078 tmp_mips_processor_type = strsave (mips_processor_type);
2079
2080 return;
2081 }
2082
2083 if (!mips_set_processor_type (tmp_mips_processor_type))
2084 {
2085 error ("Unknown processor type `%s'.", tmp_mips_processor_type);
2086 /* Restore its value. */
2087 tmp_mips_processor_type = strsave (mips_processor_type);
2088 }
2089}
2090
2091static void
2092mips_show_processor_type_command (args, from_tty)
2093 char *args;
2094 int from_tty;
2095{
2096}
2097
2098/* Modify the actual processor type. */
2099
2100int
2101mips_set_processor_type (str)
2102 char *str;
2103{
2104 int i, j;
2105
2106 if (str == NULL)
1d9489c1 2107 return 0;
9f9f94aa
SS
2108
2109 for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
2110 {
2111 if (strcasecmp (str, mips_processor_type_table[i].name) == 0)
2112 {
2113 mips_processor_type = str;
2114
2115 for (j = 0; j < NUM_REGS; ++j)
2116 reg_names[j] = mips_processor_type_table[i].regnames[j];
2117
2118 return 1;
2119
2120 /* FIXME tweak fpu flag too */
2121 }
2122 }
2123
2124 return 0;
2125}
2126
2127/* Attempt to identify the particular processor model by reading the
2128 processor id. */
2129
2130char *
2131mips_read_processor_type ()
2132{
28444bf3 2133 CORE_ADDR prid;
9f9f94aa
SS
2134
2135 prid = read_register (PRID_REGNUM);
2136
80e0e92a 2137 if ((prid & ~0xf) == 0x700)
9f9f94aa
SS
2138 return savestring ("r3041", strlen("r3041"));
2139
2140 return NULL;
2141}
427fec5d
JK
2142
2143/* Just like reinit_frame_cache, but with the right arguments to be
2144 callable as an sfunc. */
9f9f94aa 2145
427fec5d
JK
2146static void
2147reinit_frame_cache_sfunc (args, from_tty, c)
2148 char *args;
2149 int from_tty;
2150 struct cmd_list_element *c;
2151{
2152 reinit_frame_cache ();
2153}
c2a0f1cb 2154
28444bf3 2155static int
18b46e7c
SS
2156gdb_print_insn_mips (memaddr, info)
2157 bfd_vma memaddr;
2158 disassemble_info *info;
2159{
f9f8c14b
MA
2160 mips_extra_func_info_t proc_desc;
2161
2162 /* Search for the function containing this address. Set the low bit
2163 of the address when searching, in case we were given an even address
2164 that is the start of a 16-bit function. If we didn't do this,
2165 the search would fail because the symbol table says the function
2166 starts at an odd address, i.e. 1 byte past the given address. */
c1fc0935
MA
2167 memaddr = ADDR_BITS_REMOVE (memaddr);
2168 proc_desc = non_heuristic_proc_desc (MAKE_MIPS16_ADDR (memaddr), NULL);
f9f8c14b
MA
2169
2170 /* Make an attempt to determine if this is a 16-bit function. If
2171 the procedure descriptor exists and the address therein is odd,
2172 it's definitely a 16-bit function. Otherwise, we have to just
2173 guess that if the address passed in is odd, it's 16-bits. */
2174 if (proc_desc)
c1fc0935 2175 info->mach = IS_MIPS16_ADDR (PROC_LOW_ADDR (proc_desc)) ? 16 : 0;
f9f8c14b 2176 else
c1fc0935 2177 info->mach = IS_MIPS16_ADDR (memaddr) ? 16 : 0;
f9f8c14b 2178
c1fc0935 2179 /* Round down the instruction address to the appropriate boundary. */
f9f8c14b
MA
2180 memaddr &= (info->mach == 16 ? ~1 : ~3);
2181
2182 /* Call the appropriate disassembler based on the target endian-ness. */
18b46e7c
SS
2183 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
2184 return print_insn_big_mips (memaddr, info);
2185 else
2186 return print_insn_little_mips (memaddr, info);
2187}
2188
f9f8c14b
MA
2189/* This function implements the BREAKPOINT_FROM_PC macro. It uses the program
2190 counter value to determine whether a 16- or 32-bit breakpoint should be
2191 used. It returns a pointer to a string of bytes that encode a breakpoint
2192 instruction, stores the length of the string to *lenptr, and adjusts pc
2193 (if necessary) to point to the actual memory location where the
2194 breakpoint should be inserted. */
2195
2196unsigned char *mips_breakpoint_from_pc (pcptr, lenptr)
2197 CORE_ADDR *pcptr;
2198 int *lenptr;
2199{
2200 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
2201 {
c1fc0935 2202 if (IS_MIPS16_ADDR (*pcptr))
f9f8c14b
MA
2203 {
2204 static char mips16_big_breakpoint[] = MIPS16_BIG_BREAKPOINT;
c1fc0935 2205 *pcptr = UNMAKE_MIPS16_ADDR (*pcptr);
f9f8c14b
MA
2206 *lenptr = sizeof(mips16_big_breakpoint);
2207 return mips16_big_breakpoint;
2208 }
2209 else
2210 {
2211 static char big_breakpoint[] = BIG_BREAKPOINT;
f781fe93
MA
2212 static char pmon_big_breakpoint[] = PMON_BIG_BREAKPOINT;
2213 static char idt_big_breakpoint[] = IDT_BIG_BREAKPOINT;
2214
f9f8c14b 2215 *lenptr = sizeof(big_breakpoint);
f781fe93
MA
2216
2217 if (strcmp (target_shortname, "mips") == 0)
2218 return idt_big_breakpoint;
2219 else if (strcmp (target_shortname, "ddb") == 0
2220 || strcmp (target_shortname, "pmon") == 0
2221 || strcmp (target_shortname, "lsi") == 0)
2222 return pmon_big_breakpoint;
2223 else
2224 return big_breakpoint;
f9f8c14b
MA
2225 }
2226 }
2227 else
2228 {
c1fc0935 2229 if (IS_MIPS16_ADDR (*pcptr))
f9f8c14b
MA
2230 {
2231 static char mips16_little_breakpoint[] = MIPS16_LITTLE_BREAKPOINT;
c1fc0935 2232 *pcptr = UNMAKE_MIPS16_ADDR (*pcptr);
f9f8c14b
MA
2233 *lenptr = sizeof(mips16_little_breakpoint);
2234 return mips16_little_breakpoint;
2235 }
2236 else
2237 {
2238 static char little_breakpoint[] = LITTLE_BREAKPOINT;
f781fe93
MA
2239 static char pmon_little_breakpoint[] = PMON_LITTLE_BREAKPOINT;
2240 static char idt_little_breakpoint[] = IDT_LITTLE_BREAKPOINT;
2241
f9f8c14b 2242 *lenptr = sizeof(little_breakpoint);
f781fe93
MA
2243
2244 if (strcmp (target_shortname, "mips") == 0)
2245 return idt_little_breakpoint;
2246 else if (strcmp (target_shortname, "ddb") == 0
2247 || strcmp (target_shortname, "pmon") == 0
2248 || strcmp (target_shortname, "lsi") == 0)
2249 return pmon_little_breakpoint;
2250 else
2251 return little_breakpoint;
f9f8c14b
MA
2252 }
2253 }
2254}
2255
2256/* Test whether the PC points to the return instruction at the
2257 end of a function. This implements the ABOUT_TO_RETURN macro. */
2258
2259int
2260mips_about_to_return (pc)
2261 CORE_ADDR pc;
2262{
c1fc0935 2263 if (IS_MIPS16_ADDR (pc))
f9f8c14b
MA
2264 /* This mips16 case isn't necessarily reliable. Sometimes the compiler
2265 generates a "jr $ra"; other times it generates code to load
2266 the return address from the stack to an accessible register (such
2267 as $a3), then a "jr" using that register. This second case
2268 is almost impossible to distinguish from an indirect jump
2269 used for switch statements, so we don't even try. */
c81a76b3 2270 return mips_fetch_instruction (pc) == 0xe820; /* jr $ra */
f9f8c14b 2271 else
c81a76b3 2272 return mips_fetch_instruction (pc) == 0x3e00008; /* jr $ra */
f9f8c14b
MA
2273}
2274
2275
f781fe93
MA
2276/* If PC is in a mips16 call or return stub, return the address of the target
2277 PC, which is either the callee or the caller. There are several
2278 cases which must be handled:
2279
2280 * If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
2281 target PC is in $31 ($ra).
2282 * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
2283 and the target PC is in $2.
2284 * If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
2285 before the jal instruction, this is effectively a call stub
2286 and the the target PC is in $2. Otherwise this is effectively
2287 a return stub and the target PC is in $18.
2288
2289 See the source code for the stubs in gcc/config/mips/mips16.S for
2290 gory details.
2291
2292 This function implements the SKIP_TRAMPOLINE_CODE macro.
2293*/
2294
2295CORE_ADDR
2296mips_skip_stub (pc)
2297 CORE_ADDR pc;
2298{
2299 char *name;
2300 CORE_ADDR start_addr;
2301
2302 /* Find the starting address and name of the function containing the PC. */
2303 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
2304 return 0;
2305
2306 /* If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
2307 target PC is in $31 ($ra). */
2308 if (strcmp (name, "__mips16_ret_sf") == 0
2309 || strcmp (name, "__mips16_ret_df") == 0)
2310 return read_register (RA_REGNUM);
2311
2312 if (strncmp (name, "__mips16_call_stub_", 19) == 0)
2313 {
2314 /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
2315 and the target PC is in $2. */
2316 if (name[19] >= '0' && name[19] <= '9')
2317 return read_register (2);
2318
2319 /* If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
2320 before the jal instruction, this is effectively a call stub
2321 and the the target PC is in $2. Otherwise this is effectively
2322 a return stub and the target PC is in $18. */
2323 else if (name[19] == 's' || name[19] == 'd')
2324 {
2325 if (pc == start_addr)
2326 {
2327 /* Check if the target of the stub is a compiler-generated
2328 stub. Such a stub for a function bar might have a name
2329 like __fn_stub_bar, and might look like this:
2330 mfc1 $4,$f13
2331 mfc1 $5,$f12
2332 mfc1 $6,$f15
2333 mfc1 $7,$f14
2334 la $1,bar (becomes a lui/addiu pair)
2335 jr $1
2336 So scan down to the lui/addi and extract the target
2337 address from those two instructions. */
2338
2339 CORE_ADDR target_pc = read_register (2);
2340 t_inst inst;
2341 int i;
2342
2343 /* See if the name of the target function is __fn_stub_*. */
2344 if (find_pc_partial_function (target_pc, &name, NULL, NULL) == 0)
2345 return target_pc;
2346 if (strncmp (name, "__fn_stub_", 10) != 0
2347 && strcmp (name, "etext") != 0
2348 && strcmp (name, "_etext") != 0)
2349 return target_pc;
2350
2351 /* Scan through this _fn_stub_ code for the lui/addiu pair.
2352 The limit on the search is arbitrarily set to 20
2353 instructions. FIXME. */
2354 for (i = 0, pc = 0; i < 20; i++, target_pc += MIPS_INSTLEN)
2355 {
2356 inst = mips_fetch_instruction (target_pc);
2357 if ((inst & 0xffff0000) == 0x3c010000) /* lui $at */
2358 pc = (inst << 16) & 0xffff0000; /* high word */
2359 else if ((inst & 0xffff0000) == 0x24210000) /* addiu $at */
2360 return pc | (inst & 0xffff); /* low word */
2361 }
2362
2363 /* Couldn't find the lui/addui pair, so return stub address. */
2364 return target_pc;
2365 }
2366 else
2367 /* This is the 'return' part of a call stub. The return
2368 address is in $r18. */
2369 return read_register (18);
2370 }
2371 }
2372 return 0; /* not a stub */
2373}
2374
2375
2376/* Return non-zero if the PC is inside a call thunk (aka stub or trampoline).
2377 This implements the IN_SOLIB_CALL_TRAMPOLINE macro. */
2378
2379int
2380mips_in_call_stub (pc, name)
2381 CORE_ADDR pc;
2382 char *name;
2383{
2384 CORE_ADDR start_addr;
2385
2386 /* Find the starting address of the function containing the PC. If the
2387 caller didn't give us a name, look it up at the same time. */
2388 if (find_pc_partial_function (pc, name ? NULL : &name, &start_addr, NULL) == 0)
2389 return 0;
2390
2391 if (strncmp (name, "__mips16_call_stub_", 19) == 0)
2392 {
2393 /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub. */
2394 if (name[19] >= '0' && name[19] <= '9')
2395 return 1;
2396 /* If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
2397 before the jal instruction, this is effectively a call stub. */
2398 else if (name[19] == 's' || name[19] == 'd')
2399 return pc == start_addr;
2400 }
2401
2402 return 0; /* not a stub */
2403}
2404
2405
2406/* Return non-zero if the PC is inside a return thunk (aka stub or trampoline).
2407 This implements the IN_SOLIB_RETURN_TRAMPOLINE macro. */
2408
2409int
2410mips_in_return_stub (pc, name)
2411 CORE_ADDR pc;
2412 char *name;
2413{
2414 CORE_ADDR start_addr;
2415
2416 /* Find the starting address of the function containing the PC. */
2417 if (find_pc_partial_function (pc, NULL, &start_addr, NULL) == 0)
2418 return 0;
2419
2420 /* If the PC is in __mips16_ret_{d,s}f, this is a return stub. */
2421 if (strcmp (name, "__mips16_ret_sf") == 0
2422 || strcmp (name, "__mips16_ret_df") == 0)
2423 return 1;
2424
2425 /* If the PC is in __mips16_call_stub_{s,d}f_{0..10} but not at the start,
2426 i.e. after the jal instruction, this is effectively a return stub. */
2427 if (strncmp (name, "__mips16_call_stub_", 19) == 0
2428 && (name[19] == 's' || name[19] == 'd')
2429 && pc != start_addr)
2430 return 1;
2431
2432 return 0; /* not a stub */
2433}
2434
2435
2436/* Return non-zero if the PC is in a library helper function that should
2437 be ignored. This implements the IGNORE_HELPER_CALL macro. */
2438
2439int
2440mips_ignore_helper (pc)
2441 CORE_ADDR pc;
2442{
2443 char *name;
2444
2445 /* Find the starting address and name of the function containing the PC. */
2446 if (find_pc_partial_function (pc, &name, NULL, NULL) == 0)
2447 return 0;
2448
2449 /* If the PC is in __mips16_ret_{d,s}f, this is a library helper function
2450 that we want to ignore. */
2451 return (strcmp (name, "__mips16_ret_sf") == 0
2452 || strcmp (name, "__mips16_ret_df") == 0);
2453}
2454
2455
c2a0f1cb
ILT
2456void
2457_initialize_mips_tdep ()
2458{
427fec5d
JK
2459 struct cmd_list_element *c;
2460
18b46e7c
SS
2461 tm_print_insn = gdb_print_insn_mips;
2462
427fec5d
JK
2463 /* Let the user turn off floating point and set the fence post for
2464 heuristic_proc_start. */
2465
e4dbd248
PS
2466 c = add_set_cmd ("mipsfpu", class_support, var_string_noescape,
2467 (char *) &mips_fpu_string,
2468 "Set use of floating point coprocessor.\n\
2469Set to `none' to avoid using floating point instructions when calling\n\
2470functions or dealing with return values. Set to `single' to use only\n\
2471single precision floating point as on the R4650. Set to `double' for\n\
2472normal floating point support.",
2473 &setlist);
2474 c->function.sfunc = mips_set_fpu_command;
2475 c = add_show_from_set (c, &showlist);
2476 c->function.sfunc = mips_show_fpu_command;
2477
2478 mips_fpu = MIPS_FPU_DOUBLE;
2479 mips_fpu_string = strsave ("double");
3127785a 2480
9f9f94aa
SS
2481 c = add_set_cmd ("processor", class_support, var_string_noescape,
2482 (char *) &tmp_mips_processor_type,
2483 "Set the type of MIPS processor in use.\n\
2484Set this to be able to access processor-type-specific registers.\n\
2485",
2486 &setlist);
2487 c->function.cfunc = mips_set_processor_type_command;
2488 c = add_show_from_set (c, &showlist);
2489 c->function.cfunc = mips_show_processor_type_command;
2490
2491 tmp_mips_processor_type = strsave (DEFAULT_MIPS_TYPE);
2492 mips_set_processor_type_command (strsave (DEFAULT_MIPS_TYPE), 0);
2493
bdef72d2
JK
2494 /* We really would like to have both "0" and "unlimited" work, but
2495 command.c doesn't deal with that. So make it a var_zinteger
2496 because the user can always use "999999" or some such for unlimited. */
2497 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
427fec5d
JK
2498 (char *) &heuristic_fence_post,
2499 "\
23d35572
JK
2500Set the distance searched for the start of a function.\n\
2501If you are debugging a stripped executable, GDB needs to search through the\n\
2502program for the start of a function. This command sets the distance of the\n\
2503search. The only need to set it is when debugging a stripped executable.",
427fec5d
JK
2504 &setlist);
2505 /* We need to throw away the frame cache when we set this, since it
2506 might change our ability to get backtraces. */
2507 c->function.sfunc = reinit_frame_cache_sfunc;
2508 add_show_from_set (c, &showlist);
c2a0f1cb 2509}
This page took 0.543904 seconds and 4 git commands to generate.