]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mips-tdep.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / mips-tdep.c
CommitLineData
c906108c
SS
1/* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 Free Software Foundation, Inc.
4 Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
5 and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b
JM
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
c906108c
SS
23
24#include "defs.h"
25#include "gdb_string.h"
26#include "frame.h"
27#include "inferior.h"
28#include "symtab.h"
29#include "value.h"
30#include "gdbcmd.h"
31#include "language.h"
32#include "gdbcore.h"
33#include "symfile.h"
34#include "objfiles.h"
35#include "gdbtypes.h"
36#include "target.h"
37
38#include "opcode/mips.h"
39
cce74817 40struct frame_extra_info
c5aa993b
JM
41 {
42 mips_extra_func_info_t proc_desc;
43 int num_args;
44 };
cce74817 45
7a292a7a
SS
46/* Some MIPS boards don't support floating point while others only
47 support single-precision floating-point operations. See also
48 FP_REGISTER_DOUBLE. */
c906108c
SS
49
50enum mips_fpu_type
c5aa993b
JM
51 {
52 MIPS_FPU_DOUBLE, /* Full double precision floating point. */
53 MIPS_FPU_SINGLE, /* Single precision floating point (R4650). */
54 MIPS_FPU_NONE /* No floating point. */
55 };
c906108c
SS
56
57#ifndef MIPS_DEFAULT_FPU_TYPE
58#define MIPS_DEFAULT_FPU_TYPE MIPS_FPU_DOUBLE
59#endif
60static int mips_fpu_type_auto = 1;
61static enum mips_fpu_type mips_fpu_type = MIPS_DEFAULT_FPU_TYPE;
62#define MIPS_FPU_TYPE mips_fpu_type
63
7a292a7a
SS
64#ifndef MIPS_SAVED_REGSIZE
65#define MIPS_SAVED_REGSIZE MIPS_REGSIZE
66#endif
c906108c
SS
67
68/* Do not use "TARGET_IS_MIPS64" to test the size of floating point registers */
7a292a7a 69#ifndef FP_REGISTER_DOUBLE
c906108c 70#define FP_REGISTER_DOUBLE (REGISTER_VIRTUAL_SIZE(FP0_REGNUM) == 8)
7a292a7a
SS
71#endif
72
73
74#define VM_MIN_ADDRESS (CORE_ADDR)0x400000
c906108c
SS
75
76#if 0
77static int mips_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
78#endif
79
80int gdb_print_insn_mips PARAMS ((bfd_vma, disassemble_info *));
81
82static void mips_print_register PARAMS ((int, int));
83
84static mips_extra_func_info_t
c5aa993b 85 heuristic_proc_desc PARAMS ((CORE_ADDR, CORE_ADDR, struct frame_info *));
c906108c
SS
86
87static CORE_ADDR heuristic_proc_start PARAMS ((CORE_ADDR));
88
89static CORE_ADDR read_next_frame_reg PARAMS ((struct frame_info *, int));
90
91void mips_set_processor_type_command PARAMS ((char *, int));
92
93int mips_set_processor_type PARAMS ((char *));
94
95static void mips_show_processor_type_command PARAMS ((char *, int));
96
97static void reinit_frame_cache_sfunc PARAMS ((char *, int,
98 struct cmd_list_element *));
99
100static mips_extra_func_info_t
c5aa993b 101 find_proc_desc PARAMS ((CORE_ADDR pc, struct frame_info * next_frame));
c906108c
SS
102
103static CORE_ADDR after_prologue PARAMS ((CORE_ADDR pc,
104 mips_extra_func_info_t proc_desc));
105
106/* This value is the model of MIPS in use. It is derived from the value
107 of the PrID register. */
108
109char *mips_processor_type;
110
111char *tmp_mips_processor_type;
112
113/* A set of original names, to be used when restoring back to generic
114 registers from a specific set. */
115
cce74817
JM
116char *mips_generic_reg_names[] = MIPS_REGISTER_NAMES;
117char **mips_processor_reg_names = mips_generic_reg_names;
118
119char *
120mips_register_name (i)
121 int i;
122{
123 return mips_processor_reg_names[i];
124}
9846de1b 125/* *INDENT-OFF* */
c906108c
SS
126/* Names of IDT R3041 registers. */
127
128char *mips_r3041_reg_names[] = {
129 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
130 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
131 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
132 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
133 "sr", "lo", "hi", "bad", "cause","pc",
134 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
135 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
136 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
137 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
138 "fsr", "fir", "fp", "",
139 "", "", "bus", "ccfg", "", "", "", "",
140 "", "", "port", "cmp", "", "", "epc", "prid",
141};
142
143/* Names of IDT R3051 registers. */
144
145char *mips_r3051_reg_names[] = {
146 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
147 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
148 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
149 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
150 "sr", "lo", "hi", "bad", "cause","pc",
151 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
152 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
153 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
154 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
155 "fsr", "fir", "fp", "",
156 "inx", "rand", "elo", "", "ctxt", "", "", "",
157 "", "", "ehi", "", "", "", "epc", "prid",
158};
159
160/* Names of IDT R3081 registers. */
161
162char *mips_r3081_reg_names[] = {
163 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
164 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
165 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
166 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
167 "sr", "lo", "hi", "bad", "cause","pc",
168 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
169 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
170 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
171 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
172 "fsr", "fir", "fp", "",
173 "inx", "rand", "elo", "cfg", "ctxt", "", "", "",
174 "", "", "ehi", "", "", "", "epc", "prid",
175};
176
177/* Names of LSI 33k registers. */
178
179char *mips_lsi33k_reg_names[] = {
180 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
181 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
182 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
183 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
184 "epc", "hi", "lo", "sr", "cause","badvaddr",
185 "dcic", "bpc", "bda", "", "", "", "", "",
186 "", "", "", "", "", "", "", "",
187 "", "", "", "", "", "", "", "",
188 "", "", "", "", "", "", "", "",
189 "", "", "", "",
190 "", "", "", "", "", "", "", "",
191 "", "", "", "", "", "", "", "",
192};
193
194struct {
195 char *name;
196 char **regnames;
197} mips_processor_type_table[] = {
198 { "generic", mips_generic_reg_names },
199 { "r3041", mips_r3041_reg_names },
200 { "r3051", mips_r3051_reg_names },
201 { "r3071", mips_r3081_reg_names },
202 { "r3081", mips_r3081_reg_names },
203 { "lsi33k", mips_lsi33k_reg_names },
204 { NULL, NULL }
205};
9846de1b 206/* *INDENT-ON* */
c906108c 207
c5aa993b
JM
208
209
210
c906108c 211/* Table to translate MIPS16 register field to actual register number. */
c5aa993b
JM
212static int mips16_to_32_reg[8] =
213{16, 17, 2, 3, 4, 5, 6, 7};
c906108c
SS
214
215/* Heuristic_proc_start may hunt through the text section for a long
216 time across a 2400 baud serial line. Allows the user to limit this
217 search. */
218
219static unsigned int heuristic_fence_post = 0;
220
c5aa993b
JM
221#define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
222#define PROC_HIGH_ADDR(proc) ((proc)->high_addr) /* upper address bound */
c906108c
SS
223#define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
224#define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
225#define PROC_FRAME_ADJUST(proc) ((proc)->frame_adjust)
226#define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
227#define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
228#define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
229#define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
230#define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
231#define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
232#define _PROC_MAGIC_ 0x0F0F0F0F
233#define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
234#define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
235
236struct linked_proc_info
c5aa993b
JM
237 {
238 struct mips_extra_func_info info;
239 struct linked_proc_info *next;
240 }
241 *linked_proc_desc_table = NULL;
c906108c 242
cce74817
JM
243void
244mips_print_extra_frame_info (fi)
245 struct frame_info *fi;
246{
247 if (fi
248 && fi->extra_info
249 && fi->extra_info->proc_desc
250 && fi->extra_info->proc_desc->pdr.framereg < NUM_REGS)
251 printf_filtered (" frame pointer is at %s+%d\n",
252 REGISTER_NAME (fi->extra_info->proc_desc->pdr.framereg),
253 fi->extra_info->proc_desc->pdr.frameoffset);
254}
c906108c
SS
255
256/* Should the upper word of 64-bit addresses be zeroed? */
257static int mask_address_p = 1;
258
259/* Should call_function allocate stack space for a struct return? */
260int
261mips_use_struct_convention (gcc_p, type)
262 int gcc_p;
263 struct type *type;
264{
265 if (MIPS_EABI)
7a292a7a 266 return (TYPE_LENGTH (type) > 2 * MIPS_SAVED_REGSIZE);
c906108c 267 else
c5aa993b 268 return 1; /* Structures are returned by ref in extra arg0 */
c906108c
SS
269}
270
271/* Tell if the program counter value in MEMADDR is in a MIPS16 function. */
272
273static int
274pc_is_mips16 (bfd_vma memaddr)
275{
276 struct minimal_symbol *sym;
277
278 /* If bit 0 of the address is set, assume this is a MIPS16 address. */
279 if (IS_MIPS16_ADDR (memaddr))
280 return 1;
281
282 /* A flag indicating that this is a MIPS16 function is stored by elfread.c in
283 the high bit of the info field. Use this to decide if the function is
284 MIPS16 or normal MIPS. */
285 sym = lookup_minimal_symbol_by_pc (memaddr);
286 if (sym)
287 return MSYMBOL_IS_SPECIAL (sym);
288 else
289 return 0;
290}
291
292
293/* This returns the PC of the first inst after the prologue. If we can't
294 find the prologue, then return 0. */
295
296static CORE_ADDR
297after_prologue (pc, proc_desc)
298 CORE_ADDR pc;
299 mips_extra_func_info_t proc_desc;
300{
301 struct symtab_and_line sal;
302 CORE_ADDR func_addr, func_end;
303
304 if (!proc_desc)
305 proc_desc = find_proc_desc (pc, NULL);
306
307 if (proc_desc)
308 {
309 /* If function is frameless, then we need to do it the hard way. I
c5aa993b 310 strongly suspect that frameless always means prologueless... */
c906108c
SS
311 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
312 && PROC_FRAME_OFFSET (proc_desc) == 0)
313 return 0;
314 }
315
316 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
317 return 0; /* Unknown */
318
319 sal = find_pc_line (func_addr, 0);
320
321 if (sal.end < func_end)
322 return sal.end;
323
324 /* The line after the prologue is after the end of the function. In this
325 case, tell the caller to find the prologue the hard way. */
326
327 return 0;
328}
329
330/* Decode a MIPS32 instruction that saves a register in the stack, and
331 set the appropriate bit in the general register mask or float register mask
332 to indicate which register is saved. This is a helper function
333 for mips_find_saved_regs. */
334
335static void
336mips32_decode_reg_save (inst, gen_mask, float_mask)
337 t_inst inst;
338 unsigned long *gen_mask;
339 unsigned long *float_mask;
340{
341 int reg;
342
343 if ((inst & 0xffe00000) == 0xafa00000 /* sw reg,n($sp) */
344 || (inst & 0xffe00000) == 0xafc00000 /* sw reg,n($r30) */
345 || (inst & 0xffe00000) == 0xffa00000) /* sd reg,n($sp) */
346 {
347 /* It might be possible to use the instruction to
c5aa993b
JM
348 find the offset, rather than the code below which
349 is based on things being in a certain order in the
350 frame, but figuring out what the instruction's offset
351 is relative to might be a little tricky. */
c906108c
SS
352 reg = (inst & 0x001f0000) >> 16;
353 *gen_mask |= (1 << reg);
354 }
355 else if ((inst & 0xffe00000) == 0xe7a00000 /* swc1 freg,n($sp) */
c5aa993b
JM
356 || (inst & 0xffe00000) == 0xe7c00000 /* swc1 freg,n($r30) */
357 || (inst & 0xffe00000) == 0xf7a00000) /* sdc1 freg,n($sp) */
c906108c
SS
358
359 {
360 reg = ((inst & 0x001f0000) >> 16);
361 *float_mask |= (1 << reg);
362 }
363}
364
365/* Decode a MIPS16 instruction that saves a register in the stack, and
366 set the appropriate bit in the general register or float register mask
367 to indicate which register is saved. This is a helper function
368 for mips_find_saved_regs. */
369
370static void
371mips16_decode_reg_save (inst, gen_mask)
372 t_inst inst;
373 unsigned long *gen_mask;
374{
c5aa993b 375 if ((inst & 0xf800) == 0xd000) /* sw reg,n($sp) */
c906108c
SS
376 {
377 int reg = mips16_to_32_reg[(inst & 0x700) >> 8];
378 *gen_mask |= (1 << reg);
379 }
c5aa993b 380 else if ((inst & 0xff00) == 0xf900) /* sd reg,n($sp) */
c906108c
SS
381 {
382 int reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
383 *gen_mask |= (1 << reg);
384 }
c5aa993b 385 else if ((inst & 0xff00) == 0x6200 /* sw $ra,n($sp) */
c906108c
SS
386 || (inst & 0xff00) == 0xfa00) /* sd $ra,n($sp) */
387 *gen_mask |= (1 << RA_REGNUM);
388}
389
390
391/* Fetch and return instruction from the specified location. If the PC
392 is odd, assume it's a MIPS16 instruction; otherwise MIPS32. */
393
394static t_inst
395mips_fetch_instruction (addr)
c5aa993b 396 CORE_ADDR addr;
c906108c
SS
397{
398 char buf[MIPS_INSTLEN];
399 int instlen;
400 int status;
401
402 if (pc_is_mips16 (addr))
403 {
404 instlen = MIPS16_INSTLEN;
405 addr = UNMAKE_MIPS16_ADDR (addr);
406 }
407 else
c5aa993b 408 instlen = MIPS_INSTLEN;
c906108c
SS
409 status = read_memory_nobpt (addr, buf, instlen);
410 if (status)
411 memory_error (status, addr);
412 return extract_unsigned_integer (buf, instlen);
413}
414
415
416/* These the fields of 32 bit mips instructions */
417#define mips32_op(x) (x >> 25)
418#define itype_op(x) (x >> 25)
419#define itype_rs(x) ((x >> 21)& 0x1f)
420#define itype_rt(x) ((x >> 16) & 0x1f)
421#define itype_immediate(x) ( x & 0xffff)
422
423#define jtype_op(x) (x >> 25)
424#define jtype_target(x) ( x & 0x03fffff)
425
426#define rtype_op(x) (x >>25)
427#define rtype_rs(x) ((x>>21) & 0x1f)
428#define rtype_rt(x) ((x>>16) & 0x1f)
c5aa993b 429#define rtype_rd(x) ((x>>11) & 0x1f)
c906108c
SS
430#define rtype_shamt(x) ((x>>6) & 0x1f)
431#define rtype_funct(x) (x & 0x3f )
432
433static CORE_ADDR
c5aa993b
JM
434mips32_relative_offset (unsigned long inst)
435{
436 long x;
437 x = itype_immediate (inst);
438 if (x & 0x8000) /* sign bit set */
c906108c 439 {
c5aa993b 440 x |= 0xffff0000; /* sign extension */
c906108c 441 }
c5aa993b
JM
442 x = x << 2;
443 return x;
c906108c
SS
444}
445
446/* Determine whate to set a single step breakpoint while considering
447 branch prediction */
448CORE_ADDR
c5aa993b
JM
449mips32_next_pc (CORE_ADDR pc)
450{
451 unsigned long inst;
452 int op;
453 inst = mips_fetch_instruction (pc);
454 if ((inst & 0xe0000000) != 0) /* Not a special, junp or branch instruction */
455 {
456 if ((inst >> 27) == 5) /* BEQL BNEZ BLEZL BGTZE , bits 0101xx */
457 {
458 op = ((inst >> 25) & 0x03);
c906108c
SS
459 switch (op)
460 {
c5aa993b
JM
461 case 0:
462 goto equal_branch; /* BEQL */
463 case 1:
464 goto neq_branch; /* BNEZ */
465 case 2:
466 goto less_branch; /* BLEZ */
467 case 3:
468 goto greater_branch; /* BGTZ */
469 default:
470 pc += 4;
c906108c
SS
471 }
472 }
c5aa993b
JM
473 else
474 pc += 4; /* Not a branch, next instruction is easy */
c906108c
SS
475 }
476 else
c5aa993b
JM
477 { /* This gets way messy */
478
c906108c 479 /* Further subdivide into SPECIAL, REGIMM and other */
c5aa993b 480 switch (op = ((inst >> 26) & 0x07)) /* extract bits 28,27,26 */
c906108c 481 {
c5aa993b
JM
482 case 0: /* SPECIAL */
483 op = rtype_funct (inst);
484 switch (op)
485 {
486 case 8: /* JR */
487 case 9: /* JALR */
488 pc = read_register (rtype_rs (inst)); /* Set PC to that address */
489 break;
490 default:
491 pc += 4;
492 }
493
494 break; /* end special */
495 case 1: /* REGIMM */
c906108c 496 {
c5aa993b
JM
497 op = jtype_op (inst); /* branch condition */
498 switch (jtype_op (inst))
c906108c 499 {
c5aa993b
JM
500 case 0: /* BLTZ */
501 case 2: /* BLTXL */
502 case 16: /* BLTZALL */
503 case 18: /* BLTZALL */
c906108c 504 less_branch:
c5aa993b
JM
505 if (read_register (itype_rs (inst)) < 0)
506 pc += mips32_relative_offset (inst) + 4;
507 else
508 pc += 8; /* after the delay slot */
509 break;
510 case 1: /* GEZ */
511 case 3: /* BGEZL */
512 case 17: /* BGEZAL */
513 case 19: /* BGEZALL */
c906108c 514 greater_equal_branch:
c5aa993b
JM
515 if (read_register (itype_rs (inst)) >= 0)
516 pc += mips32_relative_offset (inst) + 4;
517 else
518 pc += 8; /* after the delay slot */
519 break;
520 /* All of the other intructions in the REGIMM catagory */
521 default:
522 pc += 4;
c906108c
SS
523 }
524 }
c5aa993b
JM
525 break; /* end REGIMM */
526 case 2: /* J */
527 case 3: /* JAL */
528 {
529 unsigned long reg;
530 reg = jtype_target (inst) << 2;
531 pc = reg + ((pc + 4) & 0xf0000000);
c906108c
SS
532 /* Whats this mysterious 0xf000000 adjustment ??? */
533 }
c5aa993b
JM
534 break;
535 /* FIXME case JALX : */
536 {
537 unsigned long reg;
538 reg = jtype_target (inst) << 2;
539 pc = reg + ((pc + 4) & 0xf0000000) + 1; /* yes, +1 */
c906108c
SS
540 /* Add 1 to indicate 16 bit mode - Invert ISA mode */
541 }
c5aa993b
JM
542 break; /* The new PC will be alternate mode */
543 case 4: /* BEQ , BEQL */
544 equal_branch:
545 if (read_register (itype_rs (inst)) ==
546 read_register (itype_rt (inst)))
547 pc += mips32_relative_offset (inst) + 4;
548 else
549 pc += 8;
550 break;
551 case 5: /* BNE , BNEL */
552 neq_branch:
553 if (read_register (itype_rs (inst)) !=
554 read_register (itype_rs (inst)))
555 pc += mips32_relative_offset (inst) + 4;
556 else
557 pc += 8;
558 break;
559 case 6: /* BLEZ , BLEZL */
c906108c 560 less_zero_branch:
c5aa993b
JM
561 if (read_register (itype_rs (inst) <= 0))
562 pc += mips32_relative_offset (inst) + 4;
563 else
564 pc += 8;
565 break;
566 case 7:
567 greater_branch: /* BGTZ BGTZL */
568 if (read_register (itype_rs (inst) > 0))
569 pc += mips32_relative_offset (inst) + 4;
570 else
571 pc += 8;
572 break;
573 default:
574 pc += 8;
575 } /* switch */
576 } /* else */
577 return pc;
578} /* mips32_next_pc */
c906108c
SS
579
580/* Decoding the next place to set a breakpoint is irregular for the
581 mips 16 variant, but fortunatly, there fewer instructions. We have to cope
582 ith extensions for 16 bit instructions and a pair of actual 32 bit instructions.
583 We dont want to set a single step instruction on the extend instruction
584 either.
c5aa993b 585 */
c906108c
SS
586
587/* Lots of mips16 instruction formats */
588/* Predicting jumps requires itype,ritype,i8type
589 and their extensions extItype,extritype,extI8type
c5aa993b 590 */
c906108c
SS
591enum mips16_inst_fmts
592{
c5aa993b
JM
593 itype, /* 0 immediate 5,10 */
594 ritype, /* 1 5,3,8 */
595 rrtype, /* 2 5,3,3,5 */
596 rritype, /* 3 5,3,3,5 */
597 rrrtype, /* 4 5,3,3,3,2 */
598 rriatype, /* 5 5,3,3,1,4 */
599 shifttype, /* 6 5,3,3,3,2 */
600 i8type, /* 7 5,3,8 */
601 i8movtype, /* 8 5,3,3,5 */
602 i8mov32rtype, /* 9 5,3,5,3 */
603 i64type, /* 10 5,3,8 */
604 ri64type, /* 11 5,3,3,5 */
605 jalxtype, /* 12 5,1,5,5,16 - a 32 bit instruction */
606 exiItype, /* 13 5,6,5,5,1,1,1,1,1,1,5 */
607 extRitype, /* 14 5,6,5,5,3,1,1,1,5 */
608 extRRItype, /* 15 5,5,5,5,3,3,5 */
609 extRRIAtype, /* 16 5,7,4,5,3,3,1,4 */
610 EXTshifttype, /* 17 5,5,1,1,1,1,1,1,5,3,3,1,1,1,2 */
611 extI8type, /* 18 5,6,5,5,3,1,1,1,5 */
612 extI64type, /* 19 5,6,5,5,3,1,1,1,5 */
613 extRi64type, /* 20 5,6,5,5,3,3,5 */
614 extshift64type /* 21 5,5,1,1,1,1,1,1,5,1,1,1,3,5 */
615};
c906108c
SS
616/* I am heaping all the fields of the formats into one structure and then,
617 only the fields which are involved in instruction extension */
618struct upk_mips16
c5aa993b
JM
619 {
620 unsigned short inst;
621 enum mips16_inst_fmts fmt;
622 unsigned long offset;
623 unsigned int regx; /* Function in i8 type */
624 unsigned int regy;
625 };
c906108c
SS
626
627
628
c5aa993b
JM
629static void
630print_unpack (char *comment,
631 struct upk_mips16 *u)
c906108c 632{
c5aa993b
JM
633 printf ("%s %04x ,f(%d) off(%08x) (x(%x) y(%x)\n",
634 comment, u->inst, u->fmt, u->offset, u->regx, u->regy);
c906108c
SS
635}
636
637/* The EXT-I, EXT-ri nad EXT-I8 instructions all have the same
638 format for the bits which make up the immediatate extension.
c5aa993b 639 */
c906108c 640static unsigned long
c5aa993b 641extended_offset (unsigned long extension)
c906108c 642{
c5aa993b
JM
643 unsigned long value;
644 value = (extension >> 21) & 0x3f; /* * extract 15:11 */
645 value = value << 6;
646 value |= (extension >> 16) & 0x1f; /* extrace 10:5 */
647 value = value << 5;
648 value |= extension & 0x01f; /* extract 4:0 */
649 return value;
c906108c
SS
650}
651
652/* Only call this function if you know that this is an extendable
653 instruction, It wont malfunction, but why make excess remote memory references?
654 If the immediate operands get sign extended or somthing, do it after
655 the extension is performed.
c5aa993b 656 */
c906108c
SS
657/* FIXME: Every one of these cases needs to worry about sign extension
658 when the offset is to be used in relative addressing */
659
660
c5aa993b
JM
661static unsigned short
662fetch_mips_16 (CORE_ADDR pc)
c906108c 663{
c5aa993b
JM
664 char buf[8];
665 pc &= 0xfffffffe; /* clear the low order bit */
666 target_read_memory (pc, buf, 2);
667 return extract_unsigned_integer (buf, 2);
c906108c
SS
668}
669
670static void
c5aa993b
JM
671unpack_mips16 (CORE_ADDR pc,
672 struct upk_mips16 *upk)
c906108c 673{
c5aa993b
JM
674 CORE_ADDR extpc;
675 unsigned long extension;
676 int extended;
677 extpc = (pc - 4) & ~0x01; /* Extensions are 32 bit instructions */
c906108c
SS
678 /* Decrement to previous address and loose the 16bit mode flag */
679 /* return if the instruction was extendable, but not actually extended */
c5aa993b
JM
680 extended = ((mips32_op (extension) == 30) ? 1 : 0);
681 if (extended)
682 {
683 extension = mips_fetch_instruction (extpc);
684 }
c906108c
SS
685 switch (upk->fmt)
686 {
c5aa993b 687 case itype:
c906108c 688 {
c5aa993b 689 unsigned long value;
c906108c 690 if (extended)
c5aa993b
JM
691 {
692 value = extended_offset (extension);
693 value = value << 11; /* rom for the original value */
694 value |= upk->inst & 0x7ff; /* eleven bits from instruction */
c906108c
SS
695 }
696 else
c5aa993b
JM
697 {
698 value = upk->inst & 0x7ff;
699 /* FIXME : Consider sign extension */
c906108c 700 }
c5aa993b 701 upk->offset = value;
c906108c 702 }
c5aa993b
JM
703 break;
704 case ritype:
705 case i8type:
706 { /* A register identifier and an offset */
c906108c
SS
707 /* Most of the fields are the same as I type but the
708 immediate value is of a different length */
c5aa993b 709 unsigned long value;
c906108c
SS
710 if (extended)
711 {
c5aa993b
JM
712 value = extended_offset (extension);
713 value = value << 8; /* from the original instruction */
714 value |= upk->inst & 0xff; /* eleven bits from instruction */
715 upk->regx = (extension >> 8) & 0x07; /* or i8 funct */
716 if (value & 0x4000) /* test the sign bit , bit 26 */
717 {
718 value &= ~0x3fff; /* remove the sign bit */
719 value = -value;
c906108c
SS
720 }
721 }
c5aa993b
JM
722 else
723 {
724 value = upk->inst & 0xff; /* 8 bits */
725 upk->regx = (upk->inst >> 8) & 0x07; /* or i8 funct */
726 /* FIXME: Do sign extension , this format needs it */
727 if (value & 0x80) /* THIS CONFUSES ME */
728 {
729 value &= 0xef; /* remove the sign bit */
730 value = -value;
731 }
732
733 }
734 upk->offset = value;
735 break;
c906108c 736 }
c5aa993b 737 case jalxtype:
c906108c 738 {
c5aa993b
JM
739 unsigned long value;
740 unsigned short nexthalf;
741 value = ((upk->inst & 0x1f) << 5) | ((upk->inst >> 5) & 0x1f);
742 value = value << 16;
743 nexthalf = mips_fetch_instruction (pc + 2); /* low bit still set */
744 value |= nexthalf;
745 upk->offset = value;
746 break;
c906108c
SS
747 }
748 default:
c5aa993b
JM
749 printf_filtered ("Decoding unimplemented instruction format type\n");
750 break;
c906108c
SS
751 }
752 /* print_unpack("UPK",upk) ; */
753}
754
755
756#define mips16_op(x) (x >> 11)
757
758/* This is a map of the opcodes which ae known to perform branches */
759static unsigned char map16[32] =
c5aa993b
JM
760{0, 0, 1, 1, 1, 1, 0, 0,
761 0, 0, 0, 0, 1, 0, 0, 0,
762 0, 0, 0, 0, 0, 0, 0, 0,
763 0, 0, 0, 0, 0, 1, 1, 0
764};
c906108c 765
c5aa993b
JM
766static CORE_ADDR
767add_offset_16 (CORE_ADDR pc, int offset)
c906108c 768{
c5aa993b
JM
769 return ((offset << 2) | ((pc + 2) & (0xf0000000)));
770
c906108c
SS
771}
772
773
774
c5aa993b 775static struct upk_mips16 upk;
c906108c 776
c5aa993b
JM
777CORE_ADDR
778mips16_next_pc (CORE_ADDR pc)
c906108c 779{
c5aa993b
JM
780 int op;
781 t_inst inst;
c906108c 782 /* inst = mips_fetch_instruction(pc) ; - This doesnt always work */
c5aa993b
JM
783 inst = fetch_mips_16 (pc);
784 upk.inst = inst;
785 op = mips16_op (upk.inst);
c906108c
SS
786 if (map16[op])
787 {
c5aa993b 788 int reg;
c906108c
SS
789 switch (op)
790 {
c5aa993b
JM
791 case 2: /* Branch */
792 upk.fmt = itype;
793 unpack_mips16 (pc, &upk);
794 {
795 long offset;
796 offset = upk.offset;
c906108c 797 if (offset & 0x800)
c5aa993b
JM
798 {
799 offset &= 0xeff;
800 offset = -offset;
c906108c 801 }
c5aa993b 802 pc += (offset << 1) + 2;
c906108c 803 }
c5aa993b
JM
804 break;
805 case 3: /* JAL , JALX - Watch out, these are 32 bit instruction */
806 upk.fmt = jalxtype;
807 unpack_mips16 (pc, &upk);
808 pc = add_offset_16 (pc, upk.offset);
809 if ((upk.inst >> 10) & 0x01) /* Exchange mode */
810 pc = pc & ~0x01; /* Clear low bit, indicate 32 bit mode */
811 else
812 pc |= 0x01;
813 break;
814 case 4: /* beqz */
815 upk.fmt = ritype;
816 unpack_mips16 (pc, &upk);
817 reg = read_register (upk.regx);
818 if (reg == 0)
819 pc += (upk.offset << 1) + 2;
820 else
821 pc += 2;
822 break;
823 case 5: /* bnez */
824 upk.fmt = ritype;
825 unpack_mips16 (pc, &upk);
826 reg = read_register (upk.regx);
827 if (reg != 0)
828 pc += (upk.offset << 1) + 2;
829 else
830 pc += 2;
831 break;
832 case 12: /* I8 Formats btez btnez */
833 upk.fmt = i8type;
834 unpack_mips16 (pc, &upk);
835 /* upk.regx contains the opcode */
836 reg = read_register (24); /* Test register is 24 */
837 if (((upk.regx == 0) && (reg == 0)) /* BTEZ */
838 || ((upk.regx == 1) && (reg != 0))) /* BTNEZ */
839 /* pc = add_offset_16(pc,upk.offset) ; */
840 pc += (upk.offset << 1) + 2;
841 else
842 pc += 2;
843 break;
844 case 29: /* RR Formats JR, JALR, JALR-RA */
845 upk.fmt = rrtype;
846 op = upk.inst & 0x1f;
c906108c 847 if (op == 0)
c5aa993b
JM
848 {
849 upk.regx = (upk.inst >> 8) & 0x07;
850 upk.regy = (upk.inst >> 5) & 0x07;
c906108c
SS
851 switch (upk.regy)
852 {
c5aa993b
JM
853 case 0:
854 reg = upk.regx;
855 break;
856 case 1:
857 reg = 31;
858 break; /* Function return instruction */
859 case 2:
860 reg = upk.regx;
861 break;
862 default:
863 reg = 31;
864 break; /* BOGUS Guess */
c906108c 865 }
c5aa993b 866 pc = read_register (reg);
c906108c 867 }
c5aa993b
JM
868 else
869 pc += 2;
870 break;
871 case 30: /* This is an extend instruction */
872 pc += 4; /* Dont be setting breakpints on the second half */
873 break;
874 default:
875 printf ("Filtered - next PC probably incorrrect due to jump inst\n");
876 pc += 2;
877 break;
c906108c
SS
878 }
879 }
c5aa993b
JM
880 else
881 pc += 2; /* just a good old instruction */
c906108c
SS
882 /* See if we CAN actually break on the next instruction */
883 /* printf("NXTm16PC %08x\n",(unsigned long)pc) ; */
c5aa993b
JM
884 return pc;
885} /* mips16_next_pc */
c906108c
SS
886
887/* The mips_next_pc function supports single_tep when the remote target monitor or
888 stub is not developed enough to so a single_step.
889 It works by decoding the current instruction and predicting where a branch
890 will go. This isnt hard because all the data is available.
891 The MIPS32 and MIPS16 variants are quite different
c5aa993b
JM
892 */
893CORE_ADDR
894mips_next_pc (CORE_ADDR pc)
c906108c 895{
c5aa993b 896 t_inst inst;
c906108c
SS
897 /* inst = mips_fetch_instruction(pc) ; */
898 /* if (pc_is_mips16) <----- This is failing */
c5aa993b
JM
899 if (pc & 0x01)
900 return mips16_next_pc (pc);
901 else
902 return mips32_next_pc (pc);
903} /* mips_next_pc */
c906108c
SS
904
905/* Guaranteed to set fci->saved_regs to some values (it never leaves it
906 NULL). */
907
908void
909mips_find_saved_regs (fci)
910 struct frame_info *fci;
911{
912 int ireg;
913 CORE_ADDR reg_position;
914 /* r0 bit means kernel trap */
915 int kernel_trap;
916 /* What registers have been saved? Bitmasks. */
917 unsigned long gen_mask, float_mask;
918 mips_extra_func_info_t proc_desc;
919 t_inst inst;
920
921 frame_saved_regs_zalloc (fci);
922
923 /* If it is the frame for sigtramp, the saved registers are located
924 in a sigcontext structure somewhere on the stack.
925 If the stack layout for sigtramp changes we might have to change these
926 constants and the companion fixup_sigtramp in mdebugread.c */
927#ifndef SIGFRAME_BASE
928/* To satisfy alignment restrictions, sigcontext is located 4 bytes
929 above the sigtramp frame. */
930#define SIGFRAME_BASE MIPS_REGSIZE
931/* FIXME! Are these correct?? */
932#define SIGFRAME_PC_OFF (SIGFRAME_BASE + 2 * MIPS_REGSIZE)
933#define SIGFRAME_REGSAVE_OFF (SIGFRAME_BASE + 3 * MIPS_REGSIZE)
934#define SIGFRAME_FPREGSAVE_OFF \
935 (SIGFRAME_REGSAVE_OFF + MIPS_NUMREGS * MIPS_REGSIZE + 3 * MIPS_REGSIZE)
936#endif
937#ifndef SIGFRAME_REG_SIZE
938/* FIXME! Is this correct?? */
939#define SIGFRAME_REG_SIZE MIPS_REGSIZE
940#endif
941 if (fci->signal_handler_caller)
942 {
943 for (ireg = 0; ireg < MIPS_NUMREGS; ireg++)
944 {
c5aa993b
JM
945 reg_position = fci->frame + SIGFRAME_REGSAVE_OFF
946 + ireg * SIGFRAME_REG_SIZE;
947 fci->saved_regs[ireg] = reg_position;
c906108c
SS
948 }
949 for (ireg = 0; ireg < MIPS_NUMREGS; ireg++)
950 {
c5aa993b
JM
951 reg_position = fci->frame + SIGFRAME_FPREGSAVE_OFF
952 + ireg * SIGFRAME_REG_SIZE;
953 fci->saved_regs[FP0_REGNUM + ireg] = reg_position;
c906108c
SS
954 }
955 fci->saved_regs[PC_REGNUM] = fci->frame + SIGFRAME_PC_OFF;
956 return;
957 }
958
cce74817 959 proc_desc = fci->extra_info->proc_desc;
c906108c
SS
960 if (proc_desc == NULL)
961 /* I'm not sure how/whether this can happen. Normally when we can't
962 find a proc_desc, we "synthesize" one using heuristic_proc_desc
963 and set the saved_regs right away. */
964 return;
965
c5aa993b
JM
966 kernel_trap = PROC_REG_MASK (proc_desc) & 1;
967 gen_mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK (proc_desc);
968 float_mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK (proc_desc);
c906108c 969
c5aa993b
JM
970 if ( /* In any frame other than the innermost or a frame interrupted by
971 a signal, we assume that all registers have been saved.
972 This assumes that all register saves in a function happen before
973 the first function call. */
974 (fci->next == NULL || fci->next->signal_handler_caller)
c906108c 975
c5aa993b
JM
976 /* In a dummy frame we know exactly where things are saved. */
977 && !PROC_DESC_IS_DUMMY (proc_desc)
c906108c 978
c5aa993b
JM
979 /* Don't bother unless we are inside a function prologue. Outside the
980 prologue, we know where everything is. */
c906108c 981
c5aa993b 982 && in_prologue (fci->pc, PROC_LOW_ADDR (proc_desc))
c906108c 983
c5aa993b
JM
984 /* Not sure exactly what kernel_trap means, but if it means
985 the kernel saves the registers without a prologue doing it,
986 we better not examine the prologue to see whether registers
987 have been saved yet. */
988 && !kernel_trap)
c906108c
SS
989 {
990 /* We need to figure out whether the registers that the proc_desc
c5aa993b 991 claims are saved have been saved yet. */
c906108c
SS
992
993 CORE_ADDR addr;
994
995 /* Bitmasks; set if we have found a save for the register. */
996 unsigned long gen_save_found = 0;
997 unsigned long float_save_found = 0;
998 int instlen;
999
1000 /* If the address is odd, assume this is MIPS16 code. */
1001 addr = PROC_LOW_ADDR (proc_desc);
1002 instlen = pc_is_mips16 (addr) ? MIPS16_INSTLEN : MIPS_INSTLEN;
1003
1004 /* Scan through this function's instructions preceding the current
1005 PC, and look for those that save registers. */
1006 while (addr < fci->pc)
1007 {
1008 inst = mips_fetch_instruction (addr);
1009 if (pc_is_mips16 (addr))
1010 mips16_decode_reg_save (inst, &gen_save_found);
1011 else
1012 mips32_decode_reg_save (inst, &gen_save_found, &float_save_found);
1013 addr += instlen;
1014 }
1015 gen_mask = gen_save_found;
1016 float_mask = float_save_found;
1017 }
1018
1019 /* Fill in the offsets for the registers which gen_mask says
1020 were saved. */
1021 reg_position = fci->frame + PROC_REG_OFFSET (proc_desc);
c5aa993b 1022 for (ireg = MIPS_NUMREGS - 1; gen_mask; --ireg, gen_mask <<= 1)
c906108c
SS
1023 if (gen_mask & 0x80000000)
1024 {
1025 fci->saved_regs[ireg] = reg_position;
7a292a7a 1026 reg_position -= MIPS_SAVED_REGSIZE;
c906108c
SS
1027 }
1028
1029 /* The MIPS16 entry instruction saves $s0 and $s1 in the reverse order
1030 of that normally used by gcc. Therefore, we have to fetch the first
1031 instruction of the function, and if it's an entry instruction that
1032 saves $s0 or $s1, correct their saved addresses. */
1033 if (pc_is_mips16 (PROC_LOW_ADDR (proc_desc)))
1034 {
1035 inst = mips_fetch_instruction (PROC_LOW_ADDR (proc_desc));
c5aa993b 1036 if ((inst & 0xf81f) == 0xe809 && (inst & 0x700) != 0x700) /* entry */
c906108c
SS
1037 {
1038 int reg;
1039 int sreg_count = (inst >> 6) & 3;
c5aa993b 1040
c906108c
SS
1041 /* Check if the ra register was pushed on the stack. */
1042 reg_position = fci->frame + PROC_REG_OFFSET (proc_desc);
1043 if (inst & 0x20)
7a292a7a 1044 reg_position -= MIPS_SAVED_REGSIZE;
c906108c
SS
1045
1046 /* Check if the s0 and s1 registers were pushed on the stack. */
c5aa993b 1047 for (reg = 16; reg < sreg_count + 16; reg++)
c906108c
SS
1048 {
1049 fci->saved_regs[reg] = reg_position;
7a292a7a 1050 reg_position -= MIPS_SAVED_REGSIZE;
c906108c
SS
1051 }
1052 }
1053 }
1054
1055 /* Fill in the offsets for the registers which float_mask says
1056 were saved. */
1057 reg_position = fci->frame + PROC_FREG_OFFSET (proc_desc);
1058
1059 /* The freg_offset points to where the first *double* register
1060 is saved. So skip to the high-order word. */
c5aa993b 1061 if (!GDB_TARGET_IS_MIPS64)
7a292a7a 1062 reg_position += MIPS_SAVED_REGSIZE;
c906108c
SS
1063
1064 /* Fill in the offsets for the float registers which float_mask says
1065 were saved. */
c5aa993b 1066 for (ireg = MIPS_NUMREGS - 1; float_mask; --ireg, float_mask <<= 1)
c906108c
SS
1067 if (float_mask & 0x80000000)
1068 {
c5aa993b 1069 fci->saved_regs[FP0_REGNUM + ireg] = reg_position;
7a292a7a 1070 reg_position -= MIPS_SAVED_REGSIZE;
c906108c
SS
1071 }
1072
1073 fci->saved_regs[PC_REGNUM] = fci->saved_regs[RA_REGNUM];
1074}
1075
1076static CORE_ADDR
c5aa993b 1077read_next_frame_reg (fi, regno)
c906108c
SS
1078 struct frame_info *fi;
1079 int regno;
1080{
1081 for (; fi; fi = fi->next)
1082 {
1083 /* We have to get the saved sp from the sigcontext
c5aa993b 1084 if it is a signal handler frame. */
c906108c
SS
1085 if (regno == SP_REGNUM && !fi->signal_handler_caller)
1086 return fi->frame;
1087 else
1088 {
1089 if (fi->saved_regs == NULL)
1090 mips_find_saved_regs (fi);
1091 if (fi->saved_regs[regno])
c5aa993b 1092 return read_memory_integer (fi->saved_regs[regno], MIPS_SAVED_REGSIZE);
c906108c
SS
1093 }
1094 }
1095 return read_register (regno);
1096}
1097
1098/* mips_addr_bits_remove - remove useless address bits */
1099
1100CORE_ADDR
1101mips_addr_bits_remove (addr)
c5aa993b 1102 CORE_ADDR addr;
c906108c
SS
1103{
1104#if GDB_TARGET_IS_MIPS64
c5aa993b 1105 if (mask_address_p && (addr >> 32 == (CORE_ADDR) 0xffffffff))
c906108c
SS
1106 {
1107 /* This hack is a work-around for existing boards using PMON,
c5aa993b
JM
1108 the simulator, and any other 64-bit targets that doesn't have
1109 true 64-bit addressing. On these targets, the upper 32 bits
1110 of addresses are ignored by the hardware. Thus, the PC or SP
1111 are likely to have been sign extended to all 1s by instruction
1112 sequences that load 32-bit addresses. For example, a typical
1113 piece of code that loads an address is this:
1114 lui $r2, <upper 16 bits>
1115 ori $r2, <lower 16 bits>
1116 But the lui sign-extends the value such that the upper 32 bits
1117 may be all 1s. The workaround is simply to mask off these bits.
1118 In the future, gcc may be changed to support true 64-bit
1119 addressing, and this masking will have to be disabled. */
1120 addr &= (CORE_ADDR) 0xffffffff;
c906108c
SS
1121 }
1122#else
1123 /* Even when GDB is configured for some 32-bit targets (e.g. mips-elf),
1124 BFD is configured to handle 64-bit targets, so CORE_ADDR is 64 bits.
1125 So we still have to mask off useless bits from addresses. */
c5aa993b 1126 addr &= (CORE_ADDR) 0xffffffff;
c906108c
SS
1127#endif
1128
1129 return addr;
1130}
1131
1132void
1133mips_init_frame_pc_first (fromleaf, prev)
1134 int fromleaf;
1135 struct frame_info *prev;
1136{
1137 CORE_ADDR pc, tmp;
1138
1139 pc = ((fromleaf) ? SAVED_PC_AFTER_CALL (prev->next) :
c5aa993b 1140 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
c906108c 1141 tmp = mips_skip_stub (pc);
c5aa993b 1142 prev->pc = tmp ? tmp : pc;
c906108c
SS
1143}
1144
1145
1146CORE_ADDR
c5aa993b 1147mips_frame_saved_pc (frame)
c906108c
SS
1148 struct frame_info *frame;
1149{
1150 CORE_ADDR saved_pc;
cce74817 1151 mips_extra_func_info_t proc_desc = frame->extra_info->proc_desc;
c906108c
SS
1152 /* We have to get the saved pc from the sigcontext
1153 if it is a signal handler frame. */
1154 int pcreg = frame->signal_handler_caller ? PC_REGNUM
c5aa993b 1155 : (proc_desc ? PROC_PC_REG (proc_desc) : RA_REGNUM);
c906108c 1156
c5aa993b 1157 if (proc_desc && PROC_DESC_IS_DUMMY (proc_desc))
7a292a7a 1158 saved_pc = read_memory_integer (frame->frame - MIPS_SAVED_REGSIZE, MIPS_SAVED_REGSIZE);
c906108c 1159 else
7a292a7a 1160 saved_pc = read_next_frame_reg (frame, pcreg);
c906108c
SS
1161
1162 return ADDR_BITS_REMOVE (saved_pc);
1163}
1164
1165static struct mips_extra_func_info temp_proc_desc;
cce74817 1166static CORE_ADDR temp_saved_regs[NUM_REGS];
c906108c
SS
1167
1168/* Set a register's saved stack address in temp_saved_regs. If an address
1169 has already been set for this register, do nothing; this way we will
1170 only recognize the first save of a given register in a function prologue.
1171 This is a helper function for mips{16,32}_heuristic_proc_desc. */
1172
1173static void
1174set_reg_offset (regno, offset)
1175 int regno;
1176 CORE_ADDR offset;
1177{
cce74817
JM
1178 if (temp_saved_regs[regno] == 0)
1179 temp_saved_regs[regno] = offset;
c906108c
SS
1180}
1181
1182
1183/* Test whether the PC points to the return instruction at the
1184 end of a function. */
1185
c5aa993b 1186static int
c906108c
SS
1187mips_about_to_return (pc)
1188 CORE_ADDR pc;
1189{
1190 if (pc_is_mips16 (pc))
1191 /* This mips16 case isn't necessarily reliable. Sometimes the compiler
1192 generates a "jr $ra"; other times it generates code to load
1193 the return address from the stack to an accessible register (such
1194 as $a3), then a "jr" using that register. This second case
1195 is almost impossible to distinguish from an indirect jump
1196 used for switch statements, so we don't even try. */
1197 return mips_fetch_instruction (pc) == 0xe820; /* jr $ra */
1198 else
1199 return mips_fetch_instruction (pc) == 0x3e00008; /* jr $ra */
1200}
1201
1202
1203/* This fencepost looks highly suspicious to me. Removing it also
1204 seems suspicious as it could affect remote debugging across serial
1205 lines. */
1206
1207static CORE_ADDR
1208heuristic_proc_start (pc)
c5aa993b 1209 CORE_ADDR pc;
c906108c 1210{
c5aa993b
JM
1211 CORE_ADDR start_pc;
1212 CORE_ADDR fence;
1213 int instlen;
1214 int seen_adjsp = 0;
c906108c 1215
c5aa993b
JM
1216 pc = ADDR_BITS_REMOVE (pc);
1217 start_pc = pc;
1218 fence = start_pc - heuristic_fence_post;
1219 if (start_pc == 0)
1220 return 0;
c906108c 1221
c5aa993b
JM
1222 if (heuristic_fence_post == UINT_MAX
1223 || fence < VM_MIN_ADDRESS)
1224 fence = VM_MIN_ADDRESS;
c906108c 1225
c5aa993b 1226 instlen = pc_is_mips16 (pc) ? MIPS16_INSTLEN : MIPS_INSTLEN;
c906108c 1227
c5aa993b
JM
1228 /* search back for previous return */
1229 for (start_pc -= instlen;; start_pc -= instlen)
1230 if (start_pc < fence)
1231 {
1232 /* It's not clear to me why we reach this point when
1233 stop_soon_quietly, but with this test, at least we
1234 don't print out warnings for every child forked (eg, on
1235 decstation). 22apr93 rich@cygnus.com. */
1236 if (!stop_soon_quietly)
c906108c 1237 {
c5aa993b
JM
1238 static int blurb_printed = 0;
1239
1240 warning ("Warning: GDB can't find the start of the function at 0x%s.",
1241 paddr_nz (pc));
1242
1243 if (!blurb_printed)
c906108c 1244 {
c5aa993b
JM
1245 /* This actually happens frequently in embedded
1246 development, when you first connect to a board
1247 and your stack pointer and pc are nowhere in
1248 particular. This message needs to give people
1249 in that situation enough information to
1250 determine that it's no big deal. */
1251 printf_filtered ("\n\
cd0fc7c3
SS
1252 GDB is unable to find the start of the function at 0x%s\n\
1253and thus can't determine the size of that function's stack frame.\n\
1254This means that GDB may be unable to access that stack frame, or\n\
1255the frames below it.\n\
1256 This problem is most likely caused by an invalid program counter or\n\
1257stack pointer.\n\
1258 However, if you think GDB should simply search farther back\n\
1259from 0x%s for code which looks like the beginning of a\n\
1260function, you can increase the range of the search using the `set\n\
1261heuristic-fence-post' command.\n",
c5aa993b
JM
1262 paddr_nz (pc), paddr_nz (pc));
1263 blurb_printed = 1;
c906108c 1264 }
c906108c
SS
1265 }
1266
c5aa993b
JM
1267 return 0;
1268 }
1269 else if (pc_is_mips16 (start_pc))
1270 {
1271 unsigned short inst;
1272
1273 /* On MIPS16, any one of the following is likely to be the
1274 start of a function:
1275 entry
1276 addiu sp,-n
1277 daddiu sp,-n
1278 extend -n followed by 'addiu sp,+n' or 'daddiu sp,+n' */
1279 inst = mips_fetch_instruction (start_pc);
1280 if (((inst & 0xf81f) == 0xe809 && (inst & 0x700) != 0x700) /* entry */
1281 || (inst & 0xff80) == 0x6380 /* addiu sp,-n */
1282 || (inst & 0xff80) == 0xfb80 /* daddiu sp,-n */
1283 || ((inst & 0xf810) == 0xf010 && seen_adjsp)) /* extend -n */
1284 break;
1285 else if ((inst & 0xff00) == 0x6300 /* addiu sp */
1286 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
1287 seen_adjsp = 1;
1288 else
1289 seen_adjsp = 0;
1290 }
1291 else if (mips_about_to_return (start_pc))
1292 {
1293 start_pc += 2 * MIPS_INSTLEN; /* skip return, and its delay slot */
1294 break;
1295 }
1296
c906108c 1297#if 0
c5aa993b
JM
1298 /* skip nops (usually 1) 0 - is this */
1299 while (start_pc < pc && read_memory_integer (start_pc, MIPS_INSTLEN) == 0)
1300 start_pc += MIPS_INSTLEN;
c906108c 1301#endif
c5aa993b 1302 return start_pc;
c906108c
SS
1303}
1304
1305/* Fetch the immediate value from a MIPS16 instruction.
1306 If the previous instruction was an EXTEND, use it to extend
1307 the upper bits of the immediate value. This is a helper function
1308 for mips16_heuristic_proc_desc. */
1309
1310static int
1311mips16_get_imm (prev_inst, inst, nbits, scale, is_signed)
c5aa993b
JM
1312 unsigned short prev_inst; /* previous instruction */
1313 unsigned short inst; /* current instruction */
1314 int nbits; /* number of bits in imm field */
1315 int scale; /* scale factor to be applied to imm */
1316 int is_signed; /* is the imm field signed? */
c906108c
SS
1317{
1318 int offset;
1319
1320 if ((prev_inst & 0xf800) == 0xf000) /* prev instruction was EXTEND? */
1321 {
1322 offset = ((prev_inst & 0x1f) << 11) | (prev_inst & 0x7e0);
c5aa993b 1323 if (offset & 0x8000) /* check for negative extend */
c906108c
SS
1324 offset = 0 - (0x10000 - (offset & 0xffff));
1325 return offset | (inst & 0x1f);
1326 }
1327 else
1328 {
1329 int max_imm = 1 << nbits;
1330 int mask = max_imm - 1;
1331 int sign_bit = max_imm >> 1;
1332
1333 offset = inst & mask;
1334 if (is_signed && (offset & sign_bit))
1335 offset = 0 - (max_imm - offset);
1336 return offset * scale;
1337 }
1338}
1339
1340
1341/* Fill in values in temp_proc_desc based on the MIPS16 instruction
1342 stream from start_pc to limit_pc. */
1343
1344static void
c5aa993b
JM
1345mips16_heuristic_proc_desc (start_pc, limit_pc, next_frame, sp)
1346 CORE_ADDR start_pc, limit_pc;
1347 struct frame_info *next_frame;
1348 CORE_ADDR sp;
c906108c
SS
1349{
1350 CORE_ADDR cur_pc;
1351 CORE_ADDR frame_addr = 0; /* Value of $r17, used as frame pointer */
1352 unsigned short prev_inst = 0; /* saved copy of previous instruction */
1353 unsigned inst = 0; /* current instruction */
1354 unsigned entry_inst = 0; /* the entry instruction */
1355 int reg, offset;
1356
c5aa993b
JM
1357 PROC_FRAME_OFFSET (&temp_proc_desc) = 0; /* size of stack frame */
1358 PROC_FRAME_ADJUST (&temp_proc_desc) = 0; /* offset of FP from SP */
c906108c
SS
1359
1360 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS16_INSTLEN)
1361 {
1362 /* Save the previous instruction. If it's an EXTEND, we'll extract
1363 the immediate offset extension from it in mips16_get_imm. */
1364 prev_inst = inst;
1365
1366 /* Fetch and decode the instruction. */
1367 inst = (unsigned short) mips_fetch_instruction (cur_pc);
c5aa993b 1368 if ((inst & 0xff00) == 0x6300 /* addiu sp */
c906108c
SS
1369 || (inst & 0xff00) == 0xfb00) /* daddiu sp */
1370 {
1371 offset = mips16_get_imm (prev_inst, inst, 8, 8, 1);
c5aa993b
JM
1372 if (offset < 0) /* negative stack adjustment? */
1373 PROC_FRAME_OFFSET (&temp_proc_desc) -= offset;
c906108c
SS
1374 else
1375 /* Exit loop if a positive stack adjustment is found, which
1376 usually means that the stack cleanup code in the function
1377 epilogue is reached. */
1378 break;
1379 }
1380 else if ((inst & 0xf800) == 0xd000) /* sw reg,n($sp) */
1381 {
1382 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1383 reg = mips16_to_32_reg[(inst & 0x700) >> 8];
c5aa993b 1384 PROC_REG_MASK (&temp_proc_desc) |= (1 << reg);
c906108c
SS
1385 set_reg_offset (reg, sp + offset);
1386 }
1387 else if ((inst & 0xff00) == 0xf900) /* sd reg,n($sp) */
1388 {
1389 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
1390 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
c5aa993b 1391 PROC_REG_MASK (&temp_proc_desc) |= (1 << reg);
c906108c
SS
1392 set_reg_offset (reg, sp + offset);
1393 }
1394 else if ((inst & 0xff00) == 0x6200) /* sw $ra,n($sp) */
1395 {
1396 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
c5aa993b 1397 PROC_REG_MASK (&temp_proc_desc) |= (1 << RA_REGNUM);
c906108c
SS
1398 set_reg_offset (RA_REGNUM, sp + offset);
1399 }
1400 else if ((inst & 0xff00) == 0xfa00) /* sd $ra,n($sp) */
1401 {
1402 offset = mips16_get_imm (prev_inst, inst, 8, 8, 0);
c5aa993b 1403 PROC_REG_MASK (&temp_proc_desc) |= (1 << RA_REGNUM);
c906108c
SS
1404 set_reg_offset (RA_REGNUM, sp + offset);
1405 }
c5aa993b 1406 else if (inst == 0x673d) /* move $s1, $sp */
c906108c
SS
1407 {
1408 frame_addr = sp;
1409 PROC_FRAME_REG (&temp_proc_desc) = 17;
1410 }
1411 else if ((inst & 0xff00) == 0x0100) /* addiu $s1,sp,n */
1412 {
1413 offset = mips16_get_imm (prev_inst, inst, 8, 4, 0);
1414 frame_addr = sp + offset;
1415 PROC_FRAME_REG (&temp_proc_desc) = 17;
1416 PROC_FRAME_ADJUST (&temp_proc_desc) = offset;
1417 }
1418 else if ((inst & 0xFF00) == 0xd900) /* sw reg,offset($s1) */
1419 {
1420 offset = mips16_get_imm (prev_inst, inst, 5, 4, 0);
1421 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
c5aa993b 1422 PROC_REG_MASK (&temp_proc_desc) |= 1 << reg;
c906108c
SS
1423 set_reg_offset (reg, frame_addr + offset);
1424 }
1425 else if ((inst & 0xFF00) == 0x7900) /* sd reg,offset($s1) */
1426 {
1427 offset = mips16_get_imm (prev_inst, inst, 5, 8, 0);
1428 reg = mips16_to_32_reg[(inst & 0xe0) >> 5];
c5aa993b 1429 PROC_REG_MASK (&temp_proc_desc) |= 1 << reg;
c906108c
SS
1430 set_reg_offset (reg, frame_addr + offset);
1431 }
c5aa993b
JM
1432 else if ((inst & 0xf81f) == 0xe809 && (inst & 0x700) != 0x700) /* entry */
1433 entry_inst = inst; /* save for later processing */
c906108c 1434 else if ((inst & 0xf800) == 0x1800) /* jal(x) */
c5aa993b 1435 cur_pc += MIPS16_INSTLEN; /* 32-bit instruction */
c906108c
SS
1436 }
1437
c5aa993b
JM
1438 /* The entry instruction is typically the first instruction in a function,
1439 and it stores registers at offsets relative to the value of the old SP
1440 (before the prologue). But the value of the sp parameter to this
1441 function is the new SP (after the prologue has been executed). So we
1442 can't calculate those offsets until we've seen the entire prologue,
1443 and can calculate what the old SP must have been. */
1444 if (entry_inst != 0)
1445 {
1446 int areg_count = (entry_inst >> 8) & 7;
1447 int sreg_count = (entry_inst >> 6) & 3;
c906108c 1448
c5aa993b
JM
1449 /* The entry instruction always subtracts 32 from the SP. */
1450 PROC_FRAME_OFFSET (&temp_proc_desc) += 32;
c906108c 1451
c5aa993b
JM
1452 /* Now we can calculate what the SP must have been at the
1453 start of the function prologue. */
1454 sp += PROC_FRAME_OFFSET (&temp_proc_desc);
c906108c 1455
c5aa993b
JM
1456 /* Check if a0-a3 were saved in the caller's argument save area. */
1457 for (reg = 4, offset = 0; reg < areg_count + 4; reg++)
1458 {
1459 PROC_REG_MASK (&temp_proc_desc) |= 1 << reg;
1460 set_reg_offset (reg, sp + offset);
1461 offset += MIPS_SAVED_REGSIZE;
1462 }
c906108c 1463
c5aa993b
JM
1464 /* Check if the ra register was pushed on the stack. */
1465 offset = -4;
1466 if (entry_inst & 0x20)
1467 {
1468 PROC_REG_MASK (&temp_proc_desc) |= 1 << RA_REGNUM;
1469 set_reg_offset (RA_REGNUM, sp + offset);
1470 offset -= MIPS_SAVED_REGSIZE;
1471 }
c906108c 1472
c5aa993b
JM
1473 /* Check if the s0 and s1 registers were pushed on the stack. */
1474 for (reg = 16; reg < sreg_count + 16; reg++)
1475 {
1476 PROC_REG_MASK (&temp_proc_desc) |= 1 << reg;
1477 set_reg_offset (reg, sp + offset);
1478 offset -= MIPS_SAVED_REGSIZE;
1479 }
1480 }
c906108c
SS
1481}
1482
1483static void
c5aa993b
JM
1484mips32_heuristic_proc_desc (start_pc, limit_pc, next_frame, sp)
1485 CORE_ADDR start_pc, limit_pc;
1486 struct frame_info *next_frame;
1487 CORE_ADDR sp;
c906108c
SS
1488{
1489 CORE_ADDR cur_pc;
c5aa993b 1490 CORE_ADDR frame_addr = 0; /* Value of $r30. Used by gcc for frame-pointer */
c906108c 1491restart:
cce74817 1492 memset (temp_saved_regs, '\0', SIZEOF_FRAME_SAVED_REGS);
c5aa993b 1493 PROC_FRAME_OFFSET (&temp_proc_desc) = 0;
c906108c
SS
1494 PROC_FRAME_ADJUST (&temp_proc_desc) = 0; /* offset of FP from SP */
1495 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += MIPS_INSTLEN)
1496 {
1497 unsigned long inst, high_word, low_word;
1498 int reg;
1499
1500 /* Fetch the instruction. */
1501 inst = (unsigned long) mips_fetch_instruction (cur_pc);
1502
1503 /* Save some code by pre-extracting some useful fields. */
1504 high_word = (inst >> 16) & 0xffff;
1505 low_word = inst & 0xffff;
1506 reg = high_word & 0x1f;
1507
c5aa993b 1508 if (high_word == 0x27bd /* addiu $sp,$sp,-i */
c906108c
SS
1509 || high_word == 0x23bd /* addi $sp,$sp,-i */
1510 || high_word == 0x67bd) /* daddiu $sp,$sp,-i */
1511 {
1512 if (low_word & 0x8000) /* negative stack adjustment? */
c5aa993b 1513 PROC_FRAME_OFFSET (&temp_proc_desc) += 0x10000 - low_word;
c906108c
SS
1514 else
1515 /* Exit loop if a positive stack adjustment is found, which
1516 usually means that the stack cleanup code in the function
1517 epilogue is reached. */
1518 break;
1519 }
1520 else if ((high_word & 0xFFE0) == 0xafa0) /* sw reg,offset($sp) */
1521 {
c5aa993b 1522 PROC_REG_MASK (&temp_proc_desc) |= 1 << reg;
c906108c
SS
1523 set_reg_offset (reg, sp + low_word);
1524 }
1525 else if ((high_word & 0xFFE0) == 0xffa0) /* sd reg,offset($sp) */
1526 {
1527 /* Irix 6.2 N32 ABI uses sd instructions for saving $gp and $ra,
1528 but the register size used is only 32 bits. Make the address
1529 for the saved register point to the lower 32 bits. */
c5aa993b 1530 PROC_REG_MASK (&temp_proc_desc) |= 1 << reg;
c906108c
SS
1531 set_reg_offset (reg, sp + low_word + 8 - MIPS_REGSIZE);
1532 }
c5aa993b 1533 else if (high_word == 0x27be) /* addiu $30,$sp,size */
c906108c
SS
1534 {
1535 /* Old gcc frame, r30 is virtual frame pointer. */
c5aa993b
JM
1536 if ((long) low_word != PROC_FRAME_OFFSET (&temp_proc_desc))
1537 frame_addr = sp + low_word;
c906108c
SS
1538 else if (PROC_FRAME_REG (&temp_proc_desc) == SP_REGNUM)
1539 {
1540 unsigned alloca_adjust;
1541 PROC_FRAME_REG (&temp_proc_desc) = 30;
c5aa993b
JM
1542 frame_addr = read_next_frame_reg (next_frame, 30);
1543 alloca_adjust = (unsigned) (frame_addr - (sp + low_word));
c906108c
SS
1544 if (alloca_adjust > 0)
1545 {
1546 /* FP > SP + frame_size. This may be because
1547 * of an alloca or somethings similar.
1548 * Fix sp to "pre-alloca" value, and try again.
1549 */
1550 sp += alloca_adjust;
1551 goto restart;
1552 }
1553 }
1554 }
c5aa993b
JM
1555 /* move $30,$sp. With different versions of gas this will be either
1556 `addu $30,$sp,$zero' or `or $30,$sp,$zero' or `daddu 30,sp,$0'.
1557 Accept any one of these. */
c906108c
SS
1558 else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
1559 {
1560 /* New gcc frame, virtual frame pointer is at r30 + frame_size. */
1561 if (PROC_FRAME_REG (&temp_proc_desc) == SP_REGNUM)
1562 {
1563 unsigned alloca_adjust;
1564 PROC_FRAME_REG (&temp_proc_desc) = 30;
c5aa993b
JM
1565 frame_addr = read_next_frame_reg (next_frame, 30);
1566 alloca_adjust = (unsigned) (frame_addr - sp);
c906108c
SS
1567 if (alloca_adjust > 0)
1568 {
1569 /* FP > SP + frame_size. This may be because
1570 * of an alloca or somethings similar.
1571 * Fix sp to "pre-alloca" value, and try again.
1572 */
1573 sp += alloca_adjust;
1574 goto restart;
1575 }
1576 }
1577 }
c5aa993b 1578 else if ((high_word & 0xFFE0) == 0xafc0) /* sw reg,offset($30) */
c906108c 1579 {
c5aa993b 1580 PROC_REG_MASK (&temp_proc_desc) |= 1 << reg;
c906108c
SS
1581 set_reg_offset (reg, frame_addr + low_word);
1582 }
1583 }
1584}
1585
1586static mips_extra_func_info_t
c5aa993b
JM
1587heuristic_proc_desc (start_pc, limit_pc, next_frame)
1588 CORE_ADDR start_pc, limit_pc;
1589 struct frame_info *next_frame;
c906108c
SS
1590{
1591 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
1592
c5aa993b
JM
1593 if (start_pc == 0)
1594 return NULL;
1595 memset (&temp_proc_desc, '\0', sizeof (temp_proc_desc));
cce74817 1596 memset (&temp_saved_regs, '\0', SIZEOF_FRAME_SAVED_REGS);
c906108c
SS
1597 PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
1598 PROC_FRAME_REG (&temp_proc_desc) = SP_REGNUM;
1599 PROC_PC_REG (&temp_proc_desc) = RA_REGNUM;
1600
1601 if (start_pc + 200 < limit_pc)
1602 limit_pc = start_pc + 200;
1603 if (pc_is_mips16 (start_pc))
1604 mips16_heuristic_proc_desc (start_pc, limit_pc, next_frame, sp);
1605 else
1606 mips32_heuristic_proc_desc (start_pc, limit_pc, next_frame, sp);
1607 return &temp_proc_desc;
1608}
1609
1610static mips_extra_func_info_t
1611non_heuristic_proc_desc (pc, addrptr)
1612 CORE_ADDR pc;
1613 CORE_ADDR *addrptr;
1614{
1615 CORE_ADDR startaddr;
1616 mips_extra_func_info_t proc_desc;
c5aa993b 1617 struct block *b = block_for_pc (pc);
c906108c
SS
1618 struct symbol *sym;
1619
1620 find_pc_partial_function (pc, NULL, &startaddr, NULL);
1621 if (addrptr)
1622 *addrptr = startaddr;
1623 if (b == NULL || PC_IN_CALL_DUMMY (pc, 0, 0))
1624 sym = NULL;
1625 else
1626 {
1627 if (startaddr > BLOCK_START (b))
1628 /* This is the "pathological" case referred to in a comment in
1629 print_frame_info. It might be better to move this check into
1630 symbol reading. */
1631 sym = NULL;
1632 else
1633 sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE, 0, NULL);
1634 }
1635
1636 /* If we never found a PDR for this function in symbol reading, then
1637 examine prologues to find the information. */
1638 if (sym)
1639 {
1640 proc_desc = (mips_extra_func_info_t) SYMBOL_VALUE (sym);
1641 if (PROC_FRAME_REG (proc_desc) == -1)
1642 return NULL;
1643 else
1644 return proc_desc;
1645 }
1646 else
1647 return NULL;
1648}
1649
1650
1651static mips_extra_func_info_t
1652find_proc_desc (pc, next_frame)
1653 CORE_ADDR pc;
1654 struct frame_info *next_frame;
1655{
1656 mips_extra_func_info_t proc_desc;
1657 CORE_ADDR startaddr;
1658
1659 proc_desc = non_heuristic_proc_desc (pc, &startaddr);
1660
1661 if (proc_desc)
1662 {
1663 /* IF this is the topmost frame AND
1664 * (this proc does not have debugging information OR
1665 * the PC is in the procedure prologue)
1666 * THEN create a "heuristic" proc_desc (by analyzing
1667 * the actual code) to replace the "official" proc_desc.
1668 */
1669 if (next_frame == NULL)
1670 {
1671 struct symtab_and_line val;
1672 struct symbol *proc_symbol =
c5aa993b 1673 PROC_DESC_IS_DUMMY (proc_desc) ? 0 : PROC_SYMBOL (proc_desc);
c906108c
SS
1674
1675 if (proc_symbol)
1676 {
1677 val = find_pc_line (BLOCK_START
c5aa993b 1678 (SYMBOL_BLOCK_VALUE (proc_symbol)),
c906108c
SS
1679 0);
1680 val.pc = val.end ? val.end : pc;
1681 }
1682 if (!proc_symbol || pc < val.pc)
1683 {
1684 mips_extra_func_info_t found_heuristic =
c5aa993b
JM
1685 heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
1686 pc, next_frame);
c906108c
SS
1687 if (found_heuristic)
1688 proc_desc = found_heuristic;
1689 }
1690 }
1691 }
1692 else
1693 {
1694 /* Is linked_proc_desc_table really necessary? It only seems to be used
c5aa993b
JM
1695 by procedure call dummys. However, the procedures being called ought
1696 to have their own proc_descs, and even if they don't,
1697 heuristic_proc_desc knows how to create them! */
c906108c
SS
1698
1699 register struct linked_proc_info *link;
1700
1701 for (link = linked_proc_desc_table; link; link = link->next)
c5aa993b
JM
1702 if (PROC_LOW_ADDR (&link->info) <= pc
1703 && PROC_HIGH_ADDR (&link->info) > pc)
c906108c
SS
1704 return &link->info;
1705
1706 if (startaddr == 0)
1707 startaddr = heuristic_proc_start (pc);
1708
1709 proc_desc =
1710 heuristic_proc_desc (startaddr, pc, next_frame);
1711 }
1712 return proc_desc;
1713}
1714
1715static CORE_ADDR
c5aa993b
JM
1716get_frame_pointer (frame, proc_desc)
1717 struct frame_info *frame;
1718 mips_extra_func_info_t proc_desc;
c906108c
SS
1719{
1720 return ADDR_BITS_REMOVE (
c5aa993b
JM
1721 read_next_frame_reg (frame, PROC_FRAME_REG (proc_desc)) +
1722 PROC_FRAME_OFFSET (proc_desc) - PROC_FRAME_ADJUST (proc_desc));
c906108c
SS
1723}
1724
1725mips_extra_func_info_t cached_proc_desc;
1726
1727CORE_ADDR
c5aa993b
JM
1728mips_frame_chain (frame)
1729 struct frame_info *frame;
c906108c
SS
1730{
1731 mips_extra_func_info_t proc_desc;
1732 CORE_ADDR tmp;
c5aa993b 1733 CORE_ADDR saved_pc = FRAME_SAVED_PC (frame);
c906108c
SS
1734
1735 if (saved_pc == 0 || inside_entry_file (saved_pc))
1736 return 0;
1737
1738 /* Check if the PC is inside a call stub. If it is, fetch the
1739 PC of the caller of that stub. */
1740 if ((tmp = mips_skip_stub (saved_pc)) != 0)
1741 saved_pc = tmp;
1742
1743 /* Look up the procedure descriptor for this PC. */
c5aa993b 1744 proc_desc = find_proc_desc (saved_pc, frame);
c906108c
SS
1745 if (!proc_desc)
1746 return 0;
1747
1748 cached_proc_desc = proc_desc;
1749
1750 /* If no frame pointer and frame size is zero, we must be at end
1751 of stack (or otherwise hosed). If we don't check frame size,
1752 we loop forever if we see a zero size frame. */
1753 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
1754 && PROC_FRAME_OFFSET (proc_desc) == 0
c5aa993b
JM
1755 /* The previous frame from a sigtramp frame might be frameless
1756 and have frame size zero. */
c906108c
SS
1757 && !frame->signal_handler_caller)
1758 return 0;
1759 else
1760 return get_frame_pointer (frame, proc_desc);
1761}
1762
1763void
c5aa993b 1764mips_init_extra_frame_info (fromleaf, fci)
cce74817 1765 int fromleaf;
c906108c
SS
1766 struct frame_info *fci;
1767{
1768 int regnum;
1769
1770 /* Use proc_desc calculated in frame_chain */
1771 mips_extra_func_info_t proc_desc =
c5aa993b 1772 fci->next ? cached_proc_desc : find_proc_desc (fci->pc, fci->next);
c906108c 1773
cce74817
JM
1774 fci->extra_info = (struct frame_extra_info *)
1775 frame_obstack_alloc (sizeof (struct frame_extra_info));
1776
c906108c 1777 fci->saved_regs = NULL;
cce74817 1778 fci->extra_info->proc_desc =
c906108c
SS
1779 proc_desc == &temp_proc_desc ? 0 : proc_desc;
1780 if (proc_desc)
1781 {
1782 /* Fixup frame-pointer - only needed for top frame */
1783 /* This may not be quite right, if proc has a real frame register.
c5aa993b
JM
1784 Get the value of the frame relative sp, procedure might have been
1785 interrupted by a signal at it's very start. */
c906108c
SS
1786 if (fci->pc == PROC_LOW_ADDR (proc_desc)
1787 && !PROC_DESC_IS_DUMMY (proc_desc))
1788 fci->frame = read_next_frame_reg (fci->next, SP_REGNUM);
1789 else
1790 fci->frame = get_frame_pointer (fci->next, proc_desc);
1791
1792 if (proc_desc == &temp_proc_desc)
1793 {
1794 char *name;
1795
1796 /* Do not set the saved registers for a sigtramp frame,
1797 mips_find_saved_registers will do that for us.
1798 We can't use fci->signal_handler_caller, it is not yet set. */
1799 find_pc_partial_function (fci->pc, &name,
c5aa993b 1800 (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
c906108c
SS
1801 if (!IN_SIGTRAMP (fci->pc, name))
1802 {
c5aa993b 1803 frame_saved_regs_zalloc (fci);
cce74817 1804 memcpy (fci->saved_regs, temp_saved_regs, SIZEOF_FRAME_SAVED_REGS);
c906108c
SS
1805 fci->saved_regs[PC_REGNUM]
1806 = fci->saved_regs[RA_REGNUM];
1807 }
1808 }
1809
1810 /* hack: if argument regs are saved, guess these contain args */
cce74817
JM
1811 /* assume we can't tell how many args for now */
1812 fci->extra_info->num_args = -1;
c906108c
SS
1813 for (regnum = MIPS_LAST_ARG_REGNUM; regnum >= A0_REGNUM; regnum--)
1814 {
c5aa993b 1815 if (PROC_REG_MASK (proc_desc) & (1 << regnum))
c906108c 1816 {
cce74817 1817 fci->extra_info->num_args = regnum - A0_REGNUM + 1;
c906108c
SS
1818 break;
1819 }
c5aa993b 1820 }
c906108c
SS
1821 }
1822}
1823
1824/* MIPS stack frames are almost impenetrable. When execution stops,
1825 we basically have to look at symbol information for the function
1826 that we stopped in, which tells us *which* register (if any) is
1827 the base of the frame pointer, and what offset from that register
1828 the frame itself is at.
1829
1830 This presents a problem when trying to examine a stack in memory
1831 (that isn't executing at the moment), using the "frame" command. We
1832 don't have a PC, nor do we have any registers except SP.
1833
1834 This routine takes two arguments, SP and PC, and tries to make the
1835 cached frames look as if these two arguments defined a frame on the
1836 cache. This allows the rest of info frame to extract the important
1837 arguments without difficulty. */
1838
1839struct frame_info *
1840setup_arbitrary_frame (argc, argv)
1841 int argc;
1842 CORE_ADDR *argv;
1843{
1844 if (argc != 2)
1845 error ("MIPS frame specifications require two arguments: sp and pc");
1846
1847 return create_new_frame (argv[0], argv[1]);
1848}
1849
1850/*
1851 * STACK_ARGSIZE -- how many bytes does a pushed function arg take up on the stack?
1852 *
1853 * For n32 ABI, eight.
1854 * For all others, he same as the size of a general register.
1855 */
1856#if defined (_MIPS_SIM_NABI32) && _MIPS_SIM == _MIPS_SIM_NABI32
1857#define MIPS_NABI32 1
1858#define STACK_ARGSIZE 8
1859#else
1860#define MIPS_NABI32 0
7a292a7a 1861#define STACK_ARGSIZE MIPS_SAVED_REGSIZE
c906108c
SS
1862#endif
1863
1864CORE_ADDR
c5aa993b 1865mips_push_arguments (nargs, args, sp, struct_return, struct_addr)
c906108c
SS
1866 int nargs;
1867 value_ptr *args;
1868 CORE_ADDR sp;
1869 int struct_return;
1870 CORE_ADDR struct_addr;
1871{
1872 int argreg;
1873 int float_argreg;
1874 int argnum;
1875 int len = 0;
1876 int stack_offset = 0;
1877
1878 /* Macros to round N up or down to the next A boundary; A must be
1879 a power of two. */
1880#define ROUND_DOWN(n,a) ((n) & ~((a)-1))
1881#define ROUND_UP(n,a) (((n)+(a)-1) & ~((a)-1))
c5aa993b 1882
c906108c
SS
1883 /* First ensure that the stack and structure return address (if any)
1884 are properly aligned. The stack has to be at least 64-bit aligned
1885 even on 32-bit machines, because doubles must be 64-bit aligned.
1886 On at least one MIPS variant, stack frames need to be 128-bit
1887 aligned, so we round to this widest known alignment. */
1888 sp = ROUND_DOWN (sp, 16);
7a292a7a 1889 struct_addr = ROUND_DOWN (struct_addr, MIPS_SAVED_REGSIZE);
c5aa993b 1890
c906108c
SS
1891 /* Now make space on the stack for the args. We allocate more
1892 than necessary for EABI, because the first few arguments are
1893 passed in registers, but that's OK. */
1894 for (argnum = 0; argnum < nargs; argnum++)
c5aa993b 1895 len += ROUND_UP (TYPE_LENGTH (VALUE_TYPE (args[argnum])), MIPS_SAVED_REGSIZE);
c906108c
SS
1896 sp -= ROUND_UP (len, 16);
1897
1898 /* Initialize the integer and float register pointers. */
1899 argreg = A0_REGNUM;
1900 float_argreg = FPA0_REGNUM;
1901
1902 /* the struct_return pointer occupies the first parameter-passing reg */
1903 if (struct_return)
c5aa993b 1904 write_register (argreg++, struct_addr);
c906108c
SS
1905
1906 /* Now load as many as possible of the first arguments into
1907 registers, and push the rest onto the stack. Loop thru args
1908 from first to last. */
1909 for (argnum = 0; argnum < nargs; argnum++)
1910 {
1911 char *val;
1912 char valbuf[MAX_REGISTER_RAW_SIZE];
1913 value_ptr arg = args[argnum];
1914 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1915 int len = TYPE_LENGTH (arg_type);
1916 enum type_code typecode = TYPE_CODE (arg_type);
1917
1918 /* The EABI passes structures that do not fit in a register by
c5aa993b 1919 reference. In all other cases, pass the structure by value. */
7a292a7a 1920 if (MIPS_EABI && len > MIPS_SAVED_REGSIZE &&
c906108c
SS
1921 (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
1922 {
7a292a7a 1923 store_address (valbuf, MIPS_SAVED_REGSIZE, VALUE_ADDRESS (arg));
c906108c 1924 typecode = TYPE_CODE_PTR;
7a292a7a 1925 len = MIPS_SAVED_REGSIZE;
c906108c
SS
1926 val = valbuf;
1927 }
1928 else
c5aa993b 1929 val = (char *) VALUE_CONTENTS (arg);
c906108c
SS
1930
1931 /* 32-bit ABIs always start floating point arguments in an
1932 even-numbered floating point register. */
1933 if (!FP_REGISTER_DOUBLE && typecode == TYPE_CODE_FLT
c5aa993b 1934 && (float_argreg & 1))
c906108c
SS
1935 float_argreg++;
1936
1937 /* Floating point arguments passed in registers have to be
1938 treated specially. On 32-bit architectures, doubles
c5aa993b
JM
1939 are passed in register pairs; the even register gets
1940 the low word, and the odd register gets the high word.
1941 On non-EABI processors, the first two floating point arguments are
1942 also copied to general registers, because MIPS16 functions
1943 don't use float registers for arguments. This duplication of
1944 arguments in general registers can't hurt non-MIPS16 functions
1945 because those registers are normally skipped. */
c906108c
SS
1946 if (typecode == TYPE_CODE_FLT
1947 && float_argreg <= MIPS_LAST_FP_ARG_REGNUM
1948 && MIPS_FPU_TYPE != MIPS_FPU_NONE)
1949 {
1950 if (!FP_REGISTER_DOUBLE && len == 8)
1951 {
1952 int low_offset = TARGET_BYTE_ORDER == BIG_ENDIAN ? 4 : 0;
1953 unsigned long regval;
1954
1955 /* Write the low word of the double to the even register(s). */
c5aa993b 1956 regval = extract_unsigned_integer (val + low_offset, 4);
c906108c
SS
1957 write_register (float_argreg++, regval);
1958 if (!MIPS_EABI)
c5aa993b 1959 write_register (argreg + 1, regval);
c906108c
SS
1960
1961 /* Write the high word of the double to the odd register(s). */
c5aa993b 1962 regval = extract_unsigned_integer (val + 4 - low_offset, 4);
c906108c
SS
1963 write_register (float_argreg++, regval);
1964 if (!MIPS_EABI)
c5aa993b 1965 {
c906108c
SS
1966 write_register (argreg, regval);
1967 argreg += 2;
1968 }
1969
1970 }
1971 else
1972 {
1973 /* This is a floating point value that fits entirely
1974 in a single register. */
1975 CORE_ADDR regval = extract_address (val, len);
1976 write_register (float_argreg++, regval);
1977 if (!MIPS_EABI)
c5aa993b 1978 {
c906108c
SS
1979 write_register (argreg, regval);
1980 argreg += FP_REGISTER_DOUBLE ? 1 : 2;
1981 }
1982 }
1983 }
1984 else
1985 {
1986 /* Copy the argument to general registers or the stack in
1987 register-sized pieces. Large arguments are split between
1988 registers and stack. */
1989 /* Note: structs whose size is not a multiple of MIPS_REGSIZE
1990 are treated specially: Irix cc passes them in registers
1991 where gcc sometimes puts them on the stack. For maximum
1992 compatibility, we will put them in both places. */
1993
c5aa993b 1994 int odd_sized_struct = ((len > MIPS_SAVED_REGSIZE) &&
7a292a7a 1995 (len % MIPS_SAVED_REGSIZE != 0));
c906108c
SS
1996 while (len > 0)
1997 {
7a292a7a 1998 int partial_len = len < MIPS_SAVED_REGSIZE ? len : MIPS_SAVED_REGSIZE;
c906108c
SS
1999
2000 if (argreg > MIPS_LAST_ARG_REGNUM || odd_sized_struct)
2001 {
2002 /* Write this portion of the argument to the stack. */
2003 /* Should shorter than int integer values be
2004 promoted to int before being stored? */
2005
2006 int longword_offset = 0;
2007 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
7a292a7a
SS
2008 {
2009 if (STACK_ARGSIZE == 8 &&
2010 (typecode == TYPE_CODE_INT ||
2011 typecode == TYPE_CODE_PTR ||
2012 typecode == TYPE_CODE_FLT) && len <= 4)
2013 longword_offset = STACK_ARGSIZE - len;
2014 else if ((typecode == TYPE_CODE_STRUCT ||
2015 typecode == TYPE_CODE_UNION) &&
2016 TYPE_LENGTH (arg_type) < STACK_ARGSIZE)
2017 longword_offset = STACK_ARGSIZE - len;
2018 }
c5aa993b
JM
2019
2020 write_memory (sp + stack_offset + longword_offset,
c906108c
SS
2021 val, partial_len);
2022 }
2023
2024 /* Note!!! This is NOT an else clause.
c5aa993b 2025 Odd sized structs may go thru BOTH paths. */
c906108c
SS
2026 if (argreg <= MIPS_LAST_ARG_REGNUM)
2027 {
2028 CORE_ADDR regval = extract_address (val, partial_len);
2029
2030 /* A non-floating-point argument being passed in a
2031 general register. If a struct or union, and if
2032 the remaining length is smaller than the register
2033 size, we have to adjust the register value on
2034 big endian targets.
2035
2036 It does not seem to be necessary to do the
2037 same for integral types.
2038
2039 Also don't do this adjustment on EABI and O64
2040 binaries. */
2041
2042 if (!MIPS_EABI
7a292a7a 2043 && MIPS_SAVED_REGSIZE < 8
c906108c 2044 && TARGET_BYTE_ORDER == BIG_ENDIAN
7a292a7a 2045 && partial_len < MIPS_SAVED_REGSIZE
c906108c
SS
2046 && (typecode == TYPE_CODE_STRUCT ||
2047 typecode == TYPE_CODE_UNION))
c5aa993b 2048 regval <<= ((MIPS_SAVED_REGSIZE - partial_len) *
c906108c
SS
2049 TARGET_CHAR_BIT);
2050
2051 write_register (argreg, regval);
2052 argreg++;
c5aa993b 2053
c906108c
SS
2054 /* If this is the old ABI, prevent subsequent floating
2055 point arguments from being passed in floating point
2056 registers. */
2057 if (!MIPS_EABI)
2058 float_argreg = MIPS_LAST_FP_ARG_REGNUM + 1;
2059 }
c5aa993b 2060
c906108c
SS
2061 len -= partial_len;
2062 val += partial_len;
2063
2064 /* The offset onto the stack at which we will start
c5aa993b
JM
2065 copying parameters (after the registers are used up)
2066 begins at (4 * MIPS_REGSIZE) in the old ABI. This
2067 leaves room for the "home" area for register parameters.
c906108c 2068
c5aa993b
JM
2069 In the new EABI (and the NABI32), the 8 register parameters
2070 do not have "home" stack space reserved for them, so the
2071 stack offset does not get incremented until after
2072 we have used up the 8 parameter registers. */
c906108c
SS
2073
2074 if (!(MIPS_EABI || MIPS_NABI32) ||
2075 argnum >= 8)
2076 stack_offset += ROUND_UP (partial_len, STACK_ARGSIZE);
2077 }
2078 }
2079 }
2080
0f71a2f6
JM
2081 /* Return adjusted stack pointer. */
2082 return sp;
2083}
2084
2085CORE_ADDR
2086mips_push_return_address (pc, sp)
2087 CORE_ADDR pc;
2088 CORE_ADDR sp;
2089{
c906108c
SS
2090 /* Set the return address register to point to the entry
2091 point of the program, where a breakpoint lies in wait. */
c5aa993b 2092 write_register (RA_REGNUM, CALL_DUMMY_ADDRESS ());
c906108c
SS
2093 return sp;
2094}
2095
2096static void
c5aa993b 2097mips_push_register (CORE_ADDR * sp, int regno)
c906108c
SS
2098{
2099 char buffer[MAX_REGISTER_RAW_SIZE];
7a292a7a
SS
2100 int regsize;
2101 int offset;
2102 if (MIPS_SAVED_REGSIZE < REGISTER_RAW_SIZE (regno))
2103 {
2104 regsize = MIPS_SAVED_REGSIZE;
2105 offset = (TARGET_BYTE_ORDER == BIG_ENDIAN
2106 ? REGISTER_RAW_SIZE (regno) - MIPS_SAVED_REGSIZE
2107 : 0);
2108 }
2109 else
2110 {
2111 regsize = REGISTER_RAW_SIZE (regno);
2112 offset = 0;
2113 }
c906108c
SS
2114 *sp -= regsize;
2115 read_register_gen (regno, buffer);
7a292a7a 2116 write_memory (*sp, buffer + offset, regsize);
c906108c
SS
2117}
2118
2119/* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<(MIPS_NUMREGS-1). */
2120#define MASK(i,j) (((1 << ((j)+1))-1) ^ ((1 << (i))-1))
2121
2122void
7a292a7a 2123mips_push_dummy_frame ()
c906108c
SS
2124{
2125 int ireg;
c5aa993b
JM
2126 struct linked_proc_info *link = (struct linked_proc_info *)
2127 xmalloc (sizeof (struct linked_proc_info));
c906108c
SS
2128 mips_extra_func_info_t proc_desc = &link->info;
2129 CORE_ADDR sp = ADDR_BITS_REMOVE (read_register (SP_REGNUM));
2130 CORE_ADDR old_sp = sp;
2131 link->next = linked_proc_desc_table;
2132 linked_proc_desc_table = link;
2133
2134/* FIXME! are these correct ? */
c5aa993b 2135#define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
c906108c
SS
2136#define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<(MIPS_NUMREGS-1))
2137#define FLOAT_REG_SAVE_MASK MASK(0,19)
2138#define FLOAT_SINGLE_REG_SAVE_MASK \
2139 ((1<<18)|(1<<16)|(1<<14)|(1<<12)|(1<<10)|(1<<8)|(1<<6)|(1<<4)|(1<<2)|(1<<0))
2140 /*
2141 * The registers we must save are all those not preserved across
2142 * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
2143 * In addition, we must save the PC, PUSH_FP_REGNUM, MMLO/-HI
2144 * and FP Control/Status registers.
2145 *
2146 *
2147 * Dummy frame layout:
2148 * (high memory)
c5aa993b
JM
2149 * Saved PC
2150 * Saved MMHI, MMLO, FPC_CSR
2151 * Saved R31
2152 * Saved R28
2153 * ...
2154 * Saved R1
c906108c
SS
2155 * Saved D18 (i.e. F19, F18)
2156 * ...
2157 * Saved D0 (i.e. F1, F0)
c5aa993b 2158 * Argument build area and stack arguments written via mips_push_arguments
c906108c
SS
2159 * (low memory)
2160 */
2161
2162 /* Save special registers (PC, MMHI, MMLO, FPC_CSR) */
c5aa993b
JM
2163 PROC_FRAME_REG (proc_desc) = PUSH_FP_REGNUM;
2164 PROC_FRAME_OFFSET (proc_desc) = 0;
2165 PROC_FRAME_ADJUST (proc_desc) = 0;
c906108c
SS
2166 mips_push_register (&sp, PC_REGNUM);
2167 mips_push_register (&sp, HI_REGNUM);
2168 mips_push_register (&sp, LO_REGNUM);
2169 mips_push_register (&sp, MIPS_FPU_TYPE == MIPS_FPU_NONE ? 0 : FCRCS_REGNUM);
2170
2171 /* Save general CPU registers */
c5aa993b 2172 PROC_REG_MASK (proc_desc) = GEN_REG_SAVE_MASK;
c906108c 2173 /* PROC_REG_OFFSET is the offset of the first saved register from FP. */
c5aa993b
JM
2174 PROC_REG_OFFSET (proc_desc) = sp - old_sp - MIPS_SAVED_REGSIZE;
2175 for (ireg = 32; --ireg >= 0;)
2176 if (PROC_REG_MASK (proc_desc) & (1 << ireg))
c906108c
SS
2177 mips_push_register (&sp, ireg);
2178
2179 /* Save floating point registers starting with high order word */
c5aa993b 2180 PROC_FREG_MASK (proc_desc) =
c906108c
SS
2181 MIPS_FPU_TYPE == MIPS_FPU_DOUBLE ? FLOAT_REG_SAVE_MASK
2182 : MIPS_FPU_TYPE == MIPS_FPU_SINGLE ? FLOAT_SINGLE_REG_SAVE_MASK : 0;
2183 /* PROC_FREG_OFFSET is the offset of the first saved *double* register
2184 from FP. */
c5aa993b
JM
2185 PROC_FREG_OFFSET (proc_desc) = sp - old_sp - 8;
2186 for (ireg = 32; --ireg >= 0;)
2187 if (PROC_FREG_MASK (proc_desc) & (1 << ireg))
c906108c
SS
2188 mips_push_register (&sp, ireg + FP0_REGNUM);
2189
2190 /* Update the frame pointer for the call dummy and the stack pointer.
2191 Set the procedure's starting and ending addresses to point to the
2192 call dummy address at the entry point. */
2193 write_register (PUSH_FP_REGNUM, old_sp);
2194 write_register (SP_REGNUM, sp);
c5aa993b
JM
2195 PROC_LOW_ADDR (proc_desc) = CALL_DUMMY_ADDRESS ();
2196 PROC_HIGH_ADDR (proc_desc) = CALL_DUMMY_ADDRESS () + 4;
2197 SET_PROC_DESC_IS_DUMMY (proc_desc);
2198 PROC_PC_REG (proc_desc) = RA_REGNUM;
c906108c
SS
2199}
2200
2201void
c5aa993b 2202mips_pop_frame ()
c906108c
SS
2203{
2204 register int regnum;
2205 struct frame_info *frame = get_current_frame ();
2206 CORE_ADDR new_sp = FRAME_FP (frame);
2207
cce74817 2208 mips_extra_func_info_t proc_desc = frame->extra_info->proc_desc;
c906108c 2209
c5aa993b 2210 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
c906108c
SS
2211 if (frame->saved_regs == NULL)
2212 mips_find_saved_regs (frame);
2213 for (regnum = 0; regnum < NUM_REGS; regnum++)
2214 {
2215 if (regnum != SP_REGNUM && regnum != PC_REGNUM
2216 && frame->saved_regs[regnum])
2217 write_register (regnum,
2218 read_memory_integer (frame->saved_regs[regnum],
c5aa993b 2219 MIPS_SAVED_REGSIZE));
c906108c
SS
2220 }
2221 write_register (SP_REGNUM, new_sp);
2222 flush_cached_frames ();
2223
c5aa993b 2224 if (proc_desc && PROC_DESC_IS_DUMMY (proc_desc))
c906108c
SS
2225 {
2226 struct linked_proc_info *pi_ptr, *prev_ptr;
2227
2228 for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
2229 pi_ptr != NULL;
2230 prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
2231 {
2232 if (&pi_ptr->info == proc_desc)
2233 break;
2234 }
2235
2236 if (pi_ptr == NULL)
2237 error ("Can't locate dummy extra frame info\n");
2238
2239 if (prev_ptr != NULL)
2240 prev_ptr->next = pi_ptr->next;
2241 else
2242 linked_proc_desc_table = pi_ptr->next;
2243
2244 free (pi_ptr);
2245
2246 write_register (HI_REGNUM,
c5aa993b 2247 read_memory_integer (new_sp - 2 * MIPS_SAVED_REGSIZE,
7a292a7a 2248 MIPS_SAVED_REGSIZE));
c906108c 2249 write_register (LO_REGNUM,
c5aa993b 2250 read_memory_integer (new_sp - 3 * MIPS_SAVED_REGSIZE,
7a292a7a 2251 MIPS_SAVED_REGSIZE));
c906108c
SS
2252 if (MIPS_FPU_TYPE != MIPS_FPU_NONE)
2253 write_register (FCRCS_REGNUM,
c5aa993b 2254 read_memory_integer (new_sp - 4 * MIPS_SAVED_REGSIZE,
7a292a7a 2255 MIPS_SAVED_REGSIZE));
c906108c
SS
2256 }
2257}
2258
2259static void
2260mips_print_register (regnum, all)
2261 int regnum, all;
2262{
2263 char raw_buffer[MAX_REGISTER_RAW_SIZE];
2264
2265 /* Get the data in raw format. */
2266 if (read_relative_register_raw_bytes (regnum, raw_buffer))
2267 {
2268 printf_filtered ("%s: [Invalid]", REGISTER_NAME (regnum));
2269 return;
2270 }
2271
2272 /* If an even floating point register, also print as double. */
2273 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT
c5aa993b
JM
2274 && !((regnum - FP0_REGNUM) & 1))
2275 if (REGISTER_RAW_SIZE (regnum) == 4) /* this would be silly on MIPS64 or N32 (Irix 6) */
c906108c 2276 {
c5aa993b 2277 char dbuffer[2 * MAX_REGISTER_RAW_SIZE];
c906108c
SS
2278
2279 read_relative_register_raw_bytes (regnum, dbuffer);
c5aa993b 2280 read_relative_register_raw_bytes (regnum + 1, dbuffer + MIPS_REGSIZE);
c906108c
SS
2281 REGISTER_CONVERT_TO_TYPE (regnum, builtin_type_double, dbuffer);
2282
c5aa993b 2283 printf_filtered ("(d%d: ", regnum - FP0_REGNUM);
c906108c
SS
2284 val_print (builtin_type_double, dbuffer, 0, 0,
2285 gdb_stdout, 0, 1, 0, Val_pretty_default);
2286 printf_filtered ("); ");
2287 }
2288 fputs_filtered (REGISTER_NAME (regnum), gdb_stdout);
2289
2290 /* The problem with printing numeric register names (r26, etc.) is that
2291 the user can't use them on input. Probably the best solution is to
2292 fix it so that either the numeric or the funky (a2, etc.) names
2293 are accepted on input. */
2294 if (regnum < MIPS_NUMREGS)
2295 printf_filtered ("(r%d): ", regnum);
2296 else
2297 printf_filtered (": ");
2298
2299 /* If virtual format is floating, print it that way. */
2300 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
2301 if (FP_REGISTER_DOUBLE)
c5aa993b 2302 { /* show 8-byte floats as float AND double: */
c906108c
SS
2303 int offset = 4 * (TARGET_BYTE_ORDER == BIG_ENDIAN);
2304
2305 printf_filtered (" (float) ");
2306 val_print (builtin_type_float, raw_buffer + offset, 0, 0,
2307 gdb_stdout, 0, 1, 0, Val_pretty_default);
2308 printf_filtered (", (double) ");
2309 val_print (builtin_type_double, raw_buffer, 0, 0,
2310 gdb_stdout, 0, 1, 0, Val_pretty_default);
2311 }
2312 else
2313 val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0, 0,
2314 gdb_stdout, 0, 1, 0, Val_pretty_default);
2315 /* Else print as integer in hex. */
2316 else
2317 print_scalar_formatted (raw_buffer, REGISTER_VIRTUAL_TYPE (regnum),
2318 'x', 0, gdb_stdout);
2319}
2320
2321/* Replacement for generic do_registers_info.
2322 Print regs in pretty columns. */
2323
2324static int
2325do_fp_register_row (regnum)
2326 int regnum;
c5aa993b 2327{ /* do values for FP (float) regs */
c906108c
SS
2328 char *raw_buffer[2];
2329 char *dbl_buffer;
2330 /* use HI and LO to control the order of combining two flt regs */
2331 int HI = (TARGET_BYTE_ORDER == BIG_ENDIAN);
2332 int LO = (TARGET_BYTE_ORDER != BIG_ENDIAN);
2333 double doub, flt1, flt2; /* doubles extracted from raw hex data */
2334 int inv1, inv2, inv3;
c5aa993b 2335
c906108c
SS
2336 raw_buffer[0] = (char *) alloca (REGISTER_RAW_SIZE (FP0_REGNUM));
2337 raw_buffer[1] = (char *) alloca (REGISTER_RAW_SIZE (FP0_REGNUM));
2338 dbl_buffer = (char *) alloca (2 * REGISTER_RAW_SIZE (FP0_REGNUM));
2339
2340 /* Get the data in raw format. */
2341 if (read_relative_register_raw_bytes (regnum, raw_buffer[HI]))
2342 error ("can't read register %d (%s)", regnum, REGISTER_NAME (regnum));
c5aa993b 2343 if (REGISTER_RAW_SIZE (regnum) == 4)
c906108c
SS
2344 {
2345 /* 4-byte registers: we can fit two registers per row. */
2346 /* Also print every pair of 4-byte regs as an 8-byte double. */
2347 if (read_relative_register_raw_bytes (regnum + 1, raw_buffer[LO]))
c5aa993b 2348 error ("can't read register %d (%s)",
c906108c
SS
2349 regnum + 1, REGISTER_NAME (regnum + 1));
2350
2351 /* copy the two floats into one double, and unpack both */
c5aa993b
JM
2352 memcpy (dbl_buffer, raw_buffer, sizeof (dbl_buffer));
2353 flt1 = unpack_double (builtin_type_float, raw_buffer[HI], &inv1);
2354 flt2 = unpack_double (builtin_type_float, raw_buffer[LO], &inv2);
2355 doub = unpack_double (builtin_type_double, dbl_buffer, &inv3);
2356
2357 printf_filtered (inv1 ? " %-5s: <invalid float>" :
2358 " %-5s%-17.9g", REGISTER_NAME (regnum), flt1);
2359 printf_filtered (inv2 ? " %-5s: <invalid float>" :
c906108c 2360 " %-5s%-17.9g", REGISTER_NAME (regnum + 1), flt2);
c5aa993b 2361 printf_filtered (inv3 ? " dbl: <invalid double>\n" :
c906108c
SS
2362 " dbl: %-24.17g\n", doub);
2363 /* may want to do hex display here (future enhancement) */
c5aa993b 2364 regnum += 2;
c906108c
SS
2365 }
2366 else
c5aa993b 2367 { /* eight byte registers: print each one as float AND as double. */
c906108c
SS
2368 int offset = 4 * (TARGET_BYTE_ORDER == BIG_ENDIAN);
2369
c5aa993b
JM
2370 memcpy (dbl_buffer, raw_buffer[HI], sizeof (dbl_buffer));
2371 flt1 = unpack_double (builtin_type_float,
c906108c 2372 &raw_buffer[HI][offset], &inv1);
c5aa993b 2373 doub = unpack_double (builtin_type_double, dbl_buffer, &inv3);
c906108c 2374
c5aa993b 2375 printf_filtered (inv1 ? " %-5s: <invalid float>" :
c906108c 2376 " %-5s flt: %-17.9g", REGISTER_NAME (regnum), flt1);
c5aa993b 2377 printf_filtered (inv3 ? " dbl: <invalid double>\n" :
c906108c
SS
2378 " dbl: %-24.17g\n", doub);
2379 /* may want to do hex display here (future enhancement) */
2380 regnum++;
2381 }
2382 return regnum;
2383}
2384
2385/* Print a row's worth of GP (int) registers, with name labels above */
2386
2387static int
2388do_gp_register_row (regnum)
2389 int regnum;
2390{
2391 /* do values for GP (int) regs */
2392 char raw_buffer[MAX_REGISTER_RAW_SIZE];
2393 int ncols = (MIPS_REGSIZE == 8 ? 4 : 8); /* display cols per row */
2394 int col, byte;
2395 int start_regnum = regnum;
2396 int numregs = NUM_REGS;
2397
2398
2399 /* For GP registers, we print a separate row of names above the vals */
2400 printf_filtered (" ");
2401 for (col = 0; col < ncols && regnum < numregs; regnum++)
2402 {
2403 if (*REGISTER_NAME (regnum) == '\0')
c5aa993b 2404 continue; /* unused register */
c906108c 2405 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
c5aa993b
JM
2406 break; /* end the row: reached FP register */
2407 printf_filtered (MIPS_REGSIZE == 8 ? "%17s" : "%9s",
c906108c
SS
2408 REGISTER_NAME (regnum));
2409 col++;
2410 }
c5aa993b 2411 printf_filtered (start_regnum < MIPS_NUMREGS ? "\n R%-4d" : "\n ",
c906108c
SS
2412 start_regnum); /* print the R0 to R31 names */
2413
2414 regnum = start_regnum; /* go back to start of row */
2415 /* now print the values in hex, 4 or 8 to the row */
2416 for (col = 0; col < ncols && regnum < numregs; regnum++)
2417 {
2418 if (*REGISTER_NAME (regnum) == '\0')
c5aa993b 2419 continue; /* unused register */
c906108c 2420 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
c5aa993b 2421 break; /* end row: reached FP register */
c906108c
SS
2422 /* OK: get the data in raw format. */
2423 if (read_relative_register_raw_bytes (regnum, raw_buffer))
2424 error ("can't read register %d (%s)", regnum, REGISTER_NAME (regnum));
2425 /* pad small registers */
2426 for (byte = 0; byte < (MIPS_REGSIZE - REGISTER_RAW_SIZE (regnum)); byte++)
2427 printf_filtered (" ");
2428 /* Now print the register value in hex, endian order. */
2429 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
2430 for (byte = 0; byte < REGISTER_RAW_SIZE (regnum); byte++)
2431 printf_filtered ("%02x", (unsigned char) raw_buffer[byte]);
2432 else
2433 for (byte = REGISTER_RAW_SIZE (regnum) - 1; byte >= 0; byte--)
2434 printf_filtered ("%02x", (unsigned char) raw_buffer[byte]);
2435 printf_filtered (" ");
2436 col++;
2437 }
c5aa993b 2438 if (col > 0) /* ie. if we actually printed anything... */
c906108c
SS
2439 printf_filtered ("\n");
2440
2441 return regnum;
2442}
2443
2444/* MIPS_DO_REGISTERS_INFO(): called by "info register" command */
2445
2446void
2447mips_do_registers_info (regnum, fpregs)
2448 int regnum;
2449 int fpregs;
2450{
c5aa993b 2451 if (regnum != -1) /* do one specified register */
c906108c
SS
2452 {
2453 if (*(REGISTER_NAME (regnum)) == '\0')
2454 error ("Not a valid register for the current processor type");
2455
2456 mips_print_register (regnum, 0);
2457 printf_filtered ("\n");
2458 }
c5aa993b
JM
2459 else
2460 /* do all (or most) registers */
c906108c
SS
2461 {
2462 regnum = 0;
2463 while (regnum < NUM_REGS)
2464 {
c5aa993b
JM
2465 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
2466 if (fpregs) /* true for "INFO ALL-REGISTERS" command */
c906108c
SS
2467 regnum = do_fp_register_row (regnum); /* FP regs */
2468 else
2469 regnum += MIPS_NUMREGS; /* skip floating point regs */
2470 else
2471 regnum = do_gp_register_row (regnum); /* GP (int) regs */
2472 }
2473 }
2474}
2475
2476/* Return number of args passed to a frame. described by FIP.
2477 Can return -1, meaning no way to tell. */
2478
2479int
2480mips_frame_num_args (frame)
c5aa993b 2481 struct frame_info *frame;
c906108c 2482{
c5aa993b 2483#if 0 /* FIXME Use or lose this! */
c906108c
SS
2484 struct chain_info_t *p;
2485
2486 p = mips_find_cached_frame (FRAME_FP (frame));
2487 if (p->valid)
2488 return p->the_info.numargs;
2489#endif
2490 return -1;
2491}
2492
2493/* Is this a branch with a delay slot? */
2494
2495static int is_delayed PARAMS ((unsigned long));
2496
2497static int
2498is_delayed (insn)
2499 unsigned long insn;
2500{
2501 int i;
2502 for (i = 0; i < NUMOPCODES; ++i)
2503 if (mips_opcodes[i].pinfo != INSN_MACRO
2504 && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
2505 break;
2506 return (i < NUMOPCODES
2507 && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
2508 | INSN_COND_BRANCH_DELAY
2509 | INSN_COND_BRANCH_LIKELY)));
2510}
2511
2512int
2513mips_step_skips_delay (pc)
2514 CORE_ADDR pc;
2515{
2516 char buf[MIPS_INSTLEN];
2517
2518 /* There is no branch delay slot on MIPS16. */
2519 if (pc_is_mips16 (pc))
2520 return 0;
2521
2522 if (target_read_memory (pc, buf, MIPS_INSTLEN) != 0)
2523 /* If error reading memory, guess that it is not a delayed branch. */
2524 return 0;
c5aa993b 2525 return is_delayed ((unsigned long) extract_unsigned_integer (buf, MIPS_INSTLEN));
c906108c
SS
2526}
2527
2528
2529/* Skip the PC past function prologue instructions (32-bit version).
2530 This is a helper function for mips_skip_prologue. */
2531
2532static CORE_ADDR
2533mips32_skip_prologue (pc, lenient)
c5aa993b 2534 CORE_ADDR pc; /* starting PC to search from */
c906108c
SS
2535 int lenient;
2536{
c5aa993b
JM
2537 t_inst inst;
2538 CORE_ADDR end_pc;
2539 int seen_sp_adjust = 0;
2540 int load_immediate_bytes = 0;
2541
2542 /* Skip the typical prologue instructions. These are the stack adjustment
2543 instruction and the instructions that save registers on the stack
2544 or in the gcc frame. */
2545 for (end_pc = pc + 100; pc < end_pc; pc += MIPS_INSTLEN)
2546 {
2547 unsigned long high_word;
c906108c 2548
c5aa993b
JM
2549 inst = mips_fetch_instruction (pc);
2550 high_word = (inst >> 16) & 0xffff;
c906108c
SS
2551
2552#if 0
c5aa993b
JM
2553 if (lenient && is_delayed (inst))
2554 continue;
c906108c
SS
2555#endif
2556
c5aa993b
JM
2557 if (high_word == 0x27bd /* addiu $sp,$sp,offset */
2558 || high_word == 0x67bd) /* daddiu $sp,$sp,offset */
2559 seen_sp_adjust = 1;
2560 else if (inst == 0x03a1e823 || /* subu $sp,$sp,$at */
2561 inst == 0x03a8e823) /* subu $sp,$sp,$t0 */
2562 seen_sp_adjust = 1;
2563 else if (((inst & 0xFFE00000) == 0xAFA00000 /* sw reg,n($sp) */
2564 || (inst & 0xFFE00000) == 0xFFA00000) /* sd reg,n($sp) */
2565 && (inst & 0x001F0000)) /* reg != $zero */
2566 continue;
2567
2568 else if ((inst & 0xFFE00000) == 0xE7A00000) /* swc1 freg,n($sp) */
2569 continue;
2570 else if ((inst & 0xF3E00000) == 0xA3C00000 && (inst & 0x001F0000))
2571 /* sx reg,n($s8) */
2572 continue; /* reg != $zero */
2573
2574 /* move $s8,$sp. With different versions of gas this will be either
2575 `addu $s8,$sp,$zero' or `or $s8,$sp,$zero' or `daddu s8,sp,$0'.
2576 Accept any one of these. */
2577 else if (inst == 0x03A0F021 || inst == 0x03a0f025 || inst == 0x03a0f02d)
2578 continue;
2579
2580 else if ((inst & 0xFF9F07FF) == 0x00800021) /* move reg,$a0-$a3 */
2581 continue;
2582 else if (high_word == 0x3c1c) /* lui $gp,n */
2583 continue;
2584 else if (high_word == 0x279c) /* addiu $gp,$gp,n */
2585 continue;
2586 else if (inst == 0x0399e021 /* addu $gp,$gp,$t9 */
2587 || inst == 0x033ce021) /* addu $gp,$t9,$gp */
2588 continue;
2589 /* The following instructions load $at or $t0 with an immediate
2590 value in preparation for a stack adjustment via
2591 subu $sp,$sp,[$at,$t0]. These instructions could also initialize
2592 a local variable, so we accept them only before a stack adjustment
2593 instruction was seen. */
2594 else if (!seen_sp_adjust)
2595 {
2596 if (high_word == 0x3c01 || /* lui $at,n */
2597 high_word == 0x3c08) /* lui $t0,n */
2598 {
2599 load_immediate_bytes += MIPS_INSTLEN; /* FIXME!! */
2600 continue;
2601 }
2602 else if (high_word == 0x3421 || /* ori $at,$at,n */
2603 high_word == 0x3508 || /* ori $t0,$t0,n */
2604 high_word == 0x3401 || /* ori $at,$zero,n */
2605 high_word == 0x3408) /* ori $t0,$zero,n */
2606 {
2607 load_immediate_bytes += MIPS_INSTLEN; /* FIXME!! */
2608 continue;
2609 }
2610 else
2611 break;
2612 }
2613 else
2614 break;
c906108c
SS
2615 }
2616
c5aa993b
JM
2617 /* In a frameless function, we might have incorrectly
2618 skipped some load immediate instructions. Undo the skipping
2619 if the load immediate was not followed by a stack adjustment. */
2620 if (load_immediate_bytes && !seen_sp_adjust)
2621 pc -= load_immediate_bytes;
2622 return pc;
c906108c
SS
2623}
2624
2625/* Skip the PC past function prologue instructions (16-bit version).
2626 This is a helper function for mips_skip_prologue. */
2627
2628static CORE_ADDR
2629mips16_skip_prologue (pc, lenient)
c5aa993b 2630 CORE_ADDR pc; /* starting PC to search from */
c906108c
SS
2631 int lenient;
2632{
c5aa993b
JM
2633 CORE_ADDR end_pc;
2634 int extend_bytes = 0;
2635 int prev_extend_bytes;
c906108c 2636
c5aa993b
JM
2637 /* Table of instructions likely to be found in a function prologue. */
2638 static struct
c906108c
SS
2639 {
2640 unsigned short inst;
2641 unsigned short mask;
c5aa993b
JM
2642 }
2643 table[] =
2644 {
c906108c 2645 {
c5aa993b
JM
2646 0x6300, 0xff00
2647 }
2648 , /* addiu $sp,offset */
2649 {
2650 0xfb00, 0xff00
2651 }
2652 , /* daddiu $sp,offset */
2653 {
2654 0xd000, 0xf800
2655 }
2656 , /* sw reg,n($sp) */
2657 {
2658 0xf900, 0xff00
2659 }
2660 , /* sd reg,n($sp) */
2661 {
2662 0x6200, 0xff00
2663 }
2664 , /* sw $ra,n($sp) */
2665 {
2666 0xfa00, 0xff00
2667 }
2668 , /* sd $ra,n($sp) */
2669 {
2670 0x673d, 0xffff
2671 }
2672 , /* move $s1,sp */
2673 {
2674 0xd980, 0xff80
2675 }
2676 , /* sw $a0-$a3,n($s1) */
2677 {
2678 0x6704, 0xff1c
2679 }
2680 , /* move reg,$a0-$a3 */
2681 {
2682 0xe809, 0xf81f
2683 }
2684 , /* entry pseudo-op */
2685 {
2686 0x0100, 0xff00
2687 }
2688 , /* addiu $s1,$sp,n */
2689 {
2690 0, 0
2691 } /* end of table marker */
2692 };
2693
2694 /* Skip the typical prologue instructions. These are the stack adjustment
2695 instruction and the instructions that save registers on the stack
2696 or in the gcc frame. */
2697 for (end_pc = pc + 100; pc < end_pc; pc += MIPS16_INSTLEN)
2698 {
2699 unsigned short inst;
2700 int i;
c906108c 2701
c5aa993b 2702 inst = mips_fetch_instruction (pc);
c906108c 2703
c5aa993b
JM
2704 /* Normally we ignore an extend instruction. However, if it is
2705 not followed by a valid prologue instruction, we must adjust
2706 the pc back over the extend so that it won't be considered
2707 part of the prologue. */
2708 if ((inst & 0xf800) == 0xf000) /* extend */
2709 {
2710 extend_bytes = MIPS16_INSTLEN;
2711 continue;
2712 }
2713 prev_extend_bytes = extend_bytes;
2714 extend_bytes = 0;
c906108c 2715
c5aa993b
JM
2716 /* Check for other valid prologue instructions besides extend. */
2717 for (i = 0; table[i].mask != 0; i++)
2718 if ((inst & table[i].mask) == table[i].inst) /* found, get out */
2719 break;
2720 if (table[i].mask != 0) /* it was in table? */
2721 continue; /* ignore it */
2722 else
2723 /* non-prologue */
2724 {
2725 /* Return the current pc, adjusted backwards by 2 if
2726 the previous instruction was an extend. */
2727 return pc - prev_extend_bytes;
2728 }
c906108c
SS
2729 }
2730 return pc;
2731}
2732
2733/* To skip prologues, I use this predicate. Returns either PC itself
2734 if the code at PC does not look like a function prologue; otherwise
2735 returns an address that (if we're lucky) follows the prologue. If
2736 LENIENT, then we must skip everything which is involved in setting
2737 up the frame (it's OK to skip more, just so long as we don't skip
2738 anything which might clobber the registers which are being saved.
2739 We must skip more in the case where part of the prologue is in the
2740 delay slot of a non-prologue instruction). */
2741
2742CORE_ADDR
2743mips_skip_prologue (pc, lenient)
2744 CORE_ADDR pc;
2745 int lenient;
2746{
2747 /* See if we can determine the end of the prologue via the symbol table.
2748 If so, then return either PC, or the PC after the prologue, whichever
2749 is greater. */
2750
2751 CORE_ADDR post_prologue_pc = after_prologue (pc, NULL);
2752
2753 if (post_prologue_pc != 0)
2754 return max (pc, post_prologue_pc);
2755
2756 /* Can't determine prologue from the symbol table, need to examine
2757 instructions. */
2758
2759 if (pc_is_mips16 (pc))
2760 return mips16_skip_prologue (pc, lenient);
2761 else
2762 return mips32_skip_prologue (pc, lenient);
2763}
2764
2765#if 0
2766/* The lenient prologue stuff should be superseded by the code in
2767 init_extra_frame_info which looks to see whether the stores mentioned
2768 in the proc_desc have actually taken place. */
2769
2770/* Is address PC in the prologue (loosely defined) for function at
2771 STARTADDR? */
2772
2773static int
2774mips_in_lenient_prologue (startaddr, pc)
2775 CORE_ADDR startaddr;
2776 CORE_ADDR pc;
2777{
2778 CORE_ADDR end_prologue = mips_skip_prologue (startaddr, 1);
2779 return pc >= startaddr && pc < end_prologue;
2780}
2781#endif
2782
7a292a7a
SS
2783/* Determine how a return value is stored within the MIPS register
2784 file, given the return type `valtype'. */
2785
2786struct return_value_word
2787{
2788 int len;
2789 int reg;
2790 int reg_offset;
2791 int buf_offset;
2792};
2793
2794static void return_value_location PARAMS ((struct type *, struct return_value_word *, struct return_value_word *));
2795
2796static void
2797return_value_location (valtype, hi, lo)
2798 struct type *valtype;
2799 struct return_value_word *hi;
2800 struct return_value_word *lo;
2801{
2802 int len = TYPE_LENGTH (valtype);
c5aa993b 2803
7a292a7a
SS
2804 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
2805 && ((MIPS_FPU_TYPE == MIPS_FPU_DOUBLE && (len == 4 || len == 8))
2806 || (MIPS_FPU_TYPE == MIPS_FPU_SINGLE && len == 4)))
2807 {
2808 if (!FP_REGISTER_DOUBLE && len == 8)
2809 {
2810 /* We need to break a 64bit float in two 32 bit halves and
c5aa993b 2811 spread them across a floating-point register pair. */
7a292a7a
SS
2812 lo->buf_offset = TARGET_BYTE_ORDER == BIG_ENDIAN ? 4 : 0;
2813 hi->buf_offset = TARGET_BYTE_ORDER == BIG_ENDIAN ? 0 : 4;
2814 lo->reg_offset = ((TARGET_BYTE_ORDER == BIG_ENDIAN
2815 && REGISTER_RAW_SIZE (FP0_REGNUM) == 8)
2816 ? 4 : 0);
2817 hi->reg_offset = lo->reg_offset;
2818 lo->reg = FP0_REGNUM + 0;
2819 hi->reg = FP0_REGNUM + 1;
2820 lo->len = 4;
2821 hi->len = 4;
2822 }
2823 else
2824 {
2825 /* The floating point value fits in a single floating-point
c5aa993b 2826 register. */
7a292a7a
SS
2827 lo->reg_offset = ((TARGET_BYTE_ORDER == BIG_ENDIAN
2828 && REGISTER_RAW_SIZE (FP0_REGNUM) == 8
2829 && len == 4)
2830 ? 4 : 0);
2831 lo->reg = FP0_REGNUM;
2832 lo->len = len;
2833 lo->buf_offset = 0;
2834 hi->len = 0;
2835 hi->reg_offset = 0;
2836 hi->buf_offset = 0;
2837 hi->reg = 0;
2838 }
2839 }
2840 else
2841 {
2842 /* Locate a result possibly spread across two registers. */
2843 int regnum = 2;
2844 lo->reg = regnum + 0;
2845 hi->reg = regnum + 1;
2846 if (TARGET_BYTE_ORDER == BIG_ENDIAN
2847 && len < MIPS_SAVED_REGSIZE)
2848 {
2849 /* "un-left-justify" the value in the low register */
2850 lo->reg_offset = MIPS_SAVED_REGSIZE - len;
2851 lo->len = len;
2852 hi->reg_offset = 0;
2853 hi->len = 0;
2854 }
2855 else if (TARGET_BYTE_ORDER == BIG_ENDIAN
2856 && len > MIPS_SAVED_REGSIZE /* odd-size structs */
2857 && len < MIPS_SAVED_REGSIZE * 2
2858 && (TYPE_CODE (valtype) == TYPE_CODE_STRUCT ||
2859 TYPE_CODE (valtype) == TYPE_CODE_UNION))
2860 {
2861 /* "un-left-justify" the value spread across two registers. */
2862 lo->reg_offset = 2 * MIPS_SAVED_REGSIZE - len;
2863 lo->len = MIPS_SAVED_REGSIZE - lo->reg_offset;
2864 hi->reg_offset = 0;
2865 hi->len = len - lo->len;
2866 }
2867 else
2868 {
2869 /* Only perform a partial copy of the second register. */
2870 lo->reg_offset = 0;
2871 hi->reg_offset = 0;
2872 if (len > MIPS_SAVED_REGSIZE)
2873 {
2874 lo->len = MIPS_SAVED_REGSIZE;
2875 hi->len = len - MIPS_SAVED_REGSIZE;
2876 }
2877 else
2878 {
2879 lo->len = len;
2880 hi->len = 0;
2881 }
2882 }
2883 if (TARGET_BYTE_ORDER == BIG_ENDIAN
2884 && REGISTER_RAW_SIZE (regnum) == 8
2885 && MIPS_SAVED_REGSIZE == 4)
2886 {
2887 /* Account for the fact that only the least-signficant part
c5aa993b 2888 of the register is being used */
7a292a7a
SS
2889 lo->reg_offset += 4;
2890 hi->reg_offset += 4;
2891 }
2892 lo->buf_offset = 0;
2893 hi->buf_offset = lo->len;
2894 }
2895}
2896
2897/* Given a return value in `regbuf' with a type `valtype', extract and
2898 copy its value into `valbuf'. */
2899
c906108c
SS
2900void
2901mips_extract_return_value (valtype, regbuf, valbuf)
c5aa993b
JM
2902 struct type *valtype;
2903 char regbuf[REGISTER_BYTES];
2904 char *valbuf;
c906108c 2905{
7a292a7a
SS
2906 struct return_value_word lo;
2907 struct return_value_word hi;
2908 return_value_location (valtype, &lo, &hi);
2909
2910 memcpy (valbuf + lo.buf_offset,
2911 regbuf + REGISTER_BYTE (lo.reg) + lo.reg_offset,
2912 lo.len);
2913
2914 if (hi.len > 0)
2915 memcpy (valbuf + hi.buf_offset,
2916 regbuf + REGISTER_BYTE (hi.reg) + hi.reg_offset,
2917 hi.len);
2918
2919#if 0
c906108c
SS
2920 int regnum;
2921 int offset = 0;
2922 int len = TYPE_LENGTH (valtype);
c5aa993b 2923
c906108c
SS
2924 regnum = 2;
2925 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
2926 && (MIPS_FPU_TYPE == MIPS_FPU_DOUBLE
2927 || (MIPS_FPU_TYPE == MIPS_FPU_SINGLE
2928 && len <= MIPS_FPU_SINGLE_REGSIZE)))
2929 regnum = FP0_REGNUM;
2930
2931 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
c5aa993b 2932 { /* "un-left-justify" the value from the register */
c906108c
SS
2933 if (len < REGISTER_RAW_SIZE (regnum))
2934 offset = REGISTER_RAW_SIZE (regnum) - len;
c5aa993b 2935 if (len > REGISTER_RAW_SIZE (regnum) && /* odd-size structs */
c906108c
SS
2936 len < REGISTER_RAW_SIZE (regnum) * 2 &&
2937 (TYPE_CODE (valtype) == TYPE_CODE_STRUCT ||
2938 TYPE_CODE (valtype) == TYPE_CODE_UNION))
2939 offset = 2 * REGISTER_RAW_SIZE (regnum) - len;
2940 }
2941 memcpy (valbuf, regbuf + REGISTER_BYTE (regnum) + offset, len);
2942 REGISTER_CONVERT_TO_TYPE (regnum, valtype, valbuf);
7a292a7a 2943#endif
c906108c
SS
2944}
2945
7a292a7a
SS
2946/* Given a return value in `valbuf' with a type `valtype', write it's
2947 value into the appropriate register. */
2948
c906108c
SS
2949void
2950mips_store_return_value (valtype, valbuf)
c5aa993b
JM
2951 struct type *valtype;
2952 char *valbuf;
c906108c 2953{
7a292a7a
SS
2954 char raw_buffer[MAX_REGISTER_RAW_SIZE];
2955 struct return_value_word lo;
2956 struct return_value_word hi;
2957 return_value_location (valtype, &lo, &hi);
2958
2959 memset (raw_buffer, 0, sizeof (raw_buffer));
2960 memcpy (raw_buffer + lo.reg_offset, valbuf + lo.buf_offset, lo.len);
2961 write_register_bytes (REGISTER_BYTE (lo.reg),
2962 raw_buffer,
2963 REGISTER_RAW_SIZE (lo.reg));
c5aa993b 2964
7a292a7a
SS
2965 if (hi.len > 0)
2966 {
2967 memset (raw_buffer, 0, sizeof (raw_buffer));
2968 memcpy (raw_buffer + hi.reg_offset, valbuf + hi.buf_offset, hi.len);
2969 write_register_bytes (REGISTER_BYTE (hi.reg),
2970 raw_buffer,
2971 REGISTER_RAW_SIZE (hi.reg));
2972 }
2973
2974#if 0
c906108c
SS
2975 int regnum;
2976 int offset = 0;
2977 int len = TYPE_LENGTH (valtype);
2978 char raw_buffer[MAX_REGISTER_RAW_SIZE];
c5aa993b 2979
c906108c
SS
2980 regnum = 2;
2981 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
2982 && (MIPS_FPU_TYPE == MIPS_FPU_DOUBLE
2983 || (MIPS_FPU_TYPE == MIPS_FPU_SINGLE
2984 && len <= MIPS_REGSIZE)))
2985 regnum = FP0_REGNUM;
2986
2987 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
c5aa993b 2988 { /* "left-justify" the value in the register */
c906108c
SS
2989 if (len < REGISTER_RAW_SIZE (regnum))
2990 offset = REGISTER_RAW_SIZE (regnum) - len;
c5aa993b 2991 if (len > REGISTER_RAW_SIZE (regnum) && /* odd-size structs */
c906108c
SS
2992 len < REGISTER_RAW_SIZE (regnum) * 2 &&
2993 (TYPE_CODE (valtype) == TYPE_CODE_STRUCT ||
2994 TYPE_CODE (valtype) == TYPE_CODE_UNION))
2995 offset = 2 * REGISTER_RAW_SIZE (regnum) - len;
2996 }
c5aa993b
JM
2997 memcpy (raw_buffer + offset, valbuf, len);
2998 REGISTER_CONVERT_FROM_TYPE (regnum, valtype, raw_buffer);
2999 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer,
3000 len > REGISTER_RAW_SIZE (regnum) ?
3001 len : REGISTER_RAW_SIZE (regnum));
7a292a7a 3002#endif
c906108c
SS
3003}
3004
3005/* Exported procedure: Is PC in the signal trampoline code */
3006
3007int
3008in_sigtramp (pc, ignore)
3009 CORE_ADDR pc;
3010 char *ignore; /* function name */
3011{
3012 if (sigtramp_address == 0)
3013 fixup_sigtramp ();
3014 return (pc >= sigtramp_address && pc < sigtramp_end);
3015}
3016
3017/* Commands to show/set the MIPS FPU type. */
3018
3019static void show_mipsfpu_command PARAMS ((char *, int));
3020static void
3021show_mipsfpu_command (args, from_tty)
3022 char *args;
3023 int from_tty;
3024{
3025 char *msg;
3026 char *fpu;
3027 switch (MIPS_FPU_TYPE)
3028 {
3029 case MIPS_FPU_SINGLE:
3030 fpu = "single-precision";
3031 break;
3032 case MIPS_FPU_DOUBLE:
3033 fpu = "double-precision";
3034 break;
3035 case MIPS_FPU_NONE:
3036 fpu = "absent (none)";
3037 break;
3038 }
3039 if (mips_fpu_type_auto)
3040 printf_unfiltered ("The MIPS floating-point coprocessor is set automatically (currently %s)\n",
3041 fpu);
3042 else
3043 printf_unfiltered ("The MIPS floating-point coprocessor is assumed to be %s\n",
3044 fpu);
3045}
3046
3047
3048static void set_mipsfpu_command PARAMS ((char *, int));
3049static void
3050set_mipsfpu_command (args, from_tty)
3051 char *args;
3052 int from_tty;
3053{
3054 printf_unfiltered ("\"set mipsfpu\" must be followed by \"double\", \"single\",\"none\" or \"auto\".\n");
3055 show_mipsfpu_command (args, from_tty);
3056}
3057
3058static void set_mipsfpu_single_command PARAMS ((char *, int));
3059static void
3060set_mipsfpu_single_command (args, from_tty)
3061 char *args;
3062 int from_tty;
3063{
3064 mips_fpu_type = MIPS_FPU_SINGLE;
3065 mips_fpu_type_auto = 0;
3066}
3067
3068static void set_mipsfpu_double_command PARAMS ((char *, int));
3069static void
3070set_mipsfpu_double_command (args, from_tty)
3071 char *args;
3072 int from_tty;
3073{
3074 mips_fpu_type = MIPS_FPU_DOUBLE;
3075 mips_fpu_type_auto = 0;
3076}
3077
3078static void set_mipsfpu_none_command PARAMS ((char *, int));
3079static void
3080set_mipsfpu_none_command (args, from_tty)
3081 char *args;
3082 int from_tty;
3083{
3084 mips_fpu_type = MIPS_FPU_NONE;
3085 mips_fpu_type_auto = 0;
3086}
3087
3088static void set_mipsfpu_auto_command PARAMS ((char *, int));
3089static void
3090set_mipsfpu_auto_command (args, from_tty)
3091 char *args;
3092 int from_tty;
3093{
3094 mips_fpu_type_auto = 1;
3095}
3096
3097/* Command to set the processor type. */
3098
3099void
3100mips_set_processor_type_command (args, from_tty)
3101 char *args;
3102 int from_tty;
3103{
3104 int i;
3105
3106 if (tmp_mips_processor_type == NULL || *tmp_mips_processor_type == '\0')
3107 {
3108 printf_unfiltered ("The known MIPS processor types are as follows:\n\n");
3109 for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
3110 printf_unfiltered ("%s\n", mips_processor_type_table[i].name);
3111
3112 /* Restore the value. */
3113 tmp_mips_processor_type = strsave (mips_processor_type);
3114
3115 return;
3116 }
c5aa993b 3117
c906108c
SS
3118 if (!mips_set_processor_type (tmp_mips_processor_type))
3119 {
3120 error ("Unknown processor type `%s'.", tmp_mips_processor_type);
3121 /* Restore its value. */
3122 tmp_mips_processor_type = strsave (mips_processor_type);
3123 }
3124}
3125
3126static void
3127mips_show_processor_type_command (args, from_tty)
3128 char *args;
3129 int from_tty;
3130{
3131}
3132
3133/* Modify the actual processor type. */
3134
3135int
3136mips_set_processor_type (str)
3137 char *str;
3138{
3139 int i, j;
3140
3141 if (str == NULL)
3142 return 0;
3143
3144 for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
3145 {
3146 if (strcasecmp (str, mips_processor_type_table[i].name) == 0)
3147 {
3148 mips_processor_type = str;
cce74817 3149 mips_processor_reg_names = mips_processor_type_table[i].regnames;
c906108c 3150 return 1;
c906108c
SS
3151 /* FIXME tweak fpu flag too */
3152 }
3153 }
3154
3155 return 0;
3156}
3157
3158/* Attempt to identify the particular processor model by reading the
3159 processor id. */
3160
3161char *
3162mips_read_processor_type ()
3163{
3164 CORE_ADDR prid;
3165
3166 prid = read_register (PRID_REGNUM);
3167
3168 if ((prid & ~0xf) == 0x700)
c5aa993b 3169 return savestring ("r3041", strlen ("r3041"));
c906108c
SS
3170
3171 return NULL;
3172}
3173
3174/* Just like reinit_frame_cache, but with the right arguments to be
3175 callable as an sfunc. */
3176
3177static void
3178reinit_frame_cache_sfunc (args, from_tty, c)
3179 char *args;
3180 int from_tty;
3181 struct cmd_list_element *c;
3182{
3183 reinit_frame_cache ();
3184}
3185
3186int
3187gdb_print_insn_mips (memaddr, info)
3188 bfd_vma memaddr;
3189 disassemble_info *info;
3190{
3191 mips_extra_func_info_t proc_desc;
3192
3193 /* Search for the function containing this address. Set the low bit
3194 of the address when searching, in case we were given an even address
3195 that is the start of a 16-bit function. If we didn't do this,
3196 the search would fail because the symbol table says the function
3197 starts at an odd address, i.e. 1 byte past the given address. */
3198 memaddr = ADDR_BITS_REMOVE (memaddr);
3199 proc_desc = non_heuristic_proc_desc (MAKE_MIPS16_ADDR (memaddr), NULL);
3200
3201 /* Make an attempt to determine if this is a 16-bit function. If
3202 the procedure descriptor exists and the address therein is odd,
3203 it's definitely a 16-bit function. Otherwise, we have to just
3204 guess that if the address passed in is odd, it's 16-bits. */
3205 if (proc_desc)
3206 info->mach = pc_is_mips16 (PROC_LOW_ADDR (proc_desc)) ? 16 : TM_PRINT_INSN_MACH;
3207 else
3208 info->mach = pc_is_mips16 (memaddr) ? 16 : TM_PRINT_INSN_MACH;
3209
3210 /* Round down the instruction address to the appropriate boundary. */
3211 memaddr &= (info->mach == 16 ? ~1 : ~3);
c5aa993b 3212
c906108c
SS
3213 /* Call the appropriate disassembler based on the target endian-ness. */
3214 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
3215 return print_insn_big_mips (memaddr, info);
3216 else
3217 return print_insn_little_mips (memaddr, info);
3218}
3219
3220/* Old-style breakpoint macros.
3221 The IDT board uses an unusual breakpoint value, and sometimes gets
3222 confused when it sees the usual MIPS breakpoint instruction. */
3223
3224#define BIG_BREAKPOINT {0, 0x5, 0, 0xd}
3225#define LITTLE_BREAKPOINT {0xd, 0, 0x5, 0}
3226#define PMON_BIG_BREAKPOINT {0, 0, 0, 0xd}
3227#define PMON_LITTLE_BREAKPOINT {0xd, 0, 0, 0}
3228#define IDT_BIG_BREAKPOINT {0, 0, 0x0a, 0xd}
3229#define IDT_LITTLE_BREAKPOINT {0xd, 0x0a, 0, 0}
3230#define MIPS16_BIG_BREAKPOINT {0xe8, 0xa5}
3231#define MIPS16_LITTLE_BREAKPOINT {0xa5, 0xe8}
3232
3233/* This function implements the BREAKPOINT_FROM_PC macro. It uses the program
3234 counter value to determine whether a 16- or 32-bit breakpoint should be
3235 used. It returns a pointer to a string of bytes that encode a breakpoint
3236 instruction, stores the length of the string to *lenptr, and adjusts pc
3237 (if necessary) to point to the actual memory location where the
3238 breakpoint should be inserted. */
3239
c5aa993b
JM
3240unsigned char *
3241mips_breakpoint_from_pc (pcptr, lenptr)
c906108c
SS
3242 CORE_ADDR *pcptr;
3243 int *lenptr;
3244{
3245 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
3246 {
3247 if (pc_is_mips16 (*pcptr))
3248 {
3249 static char mips16_big_breakpoint[] = MIPS16_BIG_BREAKPOINT;
3250 *pcptr = UNMAKE_MIPS16_ADDR (*pcptr);
c5aa993b 3251 *lenptr = sizeof (mips16_big_breakpoint);
c906108c
SS
3252 return mips16_big_breakpoint;
3253 }
3254 else
3255 {
3256 static char big_breakpoint[] = BIG_BREAKPOINT;
3257 static char pmon_big_breakpoint[] = PMON_BIG_BREAKPOINT;
3258 static char idt_big_breakpoint[] = IDT_BIG_BREAKPOINT;
3259
c5aa993b 3260 *lenptr = sizeof (big_breakpoint);
c906108c
SS
3261
3262 if (strcmp (target_shortname, "mips") == 0)
3263 return idt_big_breakpoint;
3264 else if (strcmp (target_shortname, "ddb") == 0
3265 || strcmp (target_shortname, "pmon") == 0
3266 || strcmp (target_shortname, "lsi") == 0)
3267 return pmon_big_breakpoint;
3268 else
3269 return big_breakpoint;
3270 }
3271 }
3272 else
3273 {
3274 if (pc_is_mips16 (*pcptr))
3275 {
3276 static char mips16_little_breakpoint[] = MIPS16_LITTLE_BREAKPOINT;
3277 *pcptr = UNMAKE_MIPS16_ADDR (*pcptr);
c5aa993b 3278 *lenptr = sizeof (mips16_little_breakpoint);
c906108c
SS
3279 return mips16_little_breakpoint;
3280 }
3281 else
3282 {
3283 static char little_breakpoint[] = LITTLE_BREAKPOINT;
3284 static char pmon_little_breakpoint[] = PMON_LITTLE_BREAKPOINT;
3285 static char idt_little_breakpoint[] = IDT_LITTLE_BREAKPOINT;
3286
c5aa993b 3287 *lenptr = sizeof (little_breakpoint);
c906108c
SS
3288
3289 if (strcmp (target_shortname, "mips") == 0)
3290 return idt_little_breakpoint;
3291 else if (strcmp (target_shortname, "ddb") == 0
3292 || strcmp (target_shortname, "pmon") == 0
3293 || strcmp (target_shortname, "lsi") == 0)
3294 return pmon_little_breakpoint;
3295 else
3296 return little_breakpoint;
3297 }
3298 }
3299}
3300
3301/* If PC is in a mips16 call or return stub, return the address of the target
3302 PC, which is either the callee or the caller. There are several
3303 cases which must be handled:
3304
3305 * If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
c5aa993b 3306 target PC is in $31 ($ra).
c906108c 3307 * If the PC is in __mips16_call_stub_{1..10}, this is a call stub
c5aa993b 3308 and the target PC is in $2.
c906108c 3309 * If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
c5aa993b
JM
3310 before the jal instruction, this is effectively a call stub
3311 and the the target PC is in $2. Otherwise this is effectively
3312 a return stub and the target PC is in $18.
c906108c
SS
3313
3314 See the source code for the stubs in gcc/config/mips/mips16.S for
3315 gory details.
3316
3317 This function implements the SKIP_TRAMPOLINE_CODE macro.
c5aa993b 3318 */
c906108c
SS
3319
3320CORE_ADDR
3321mips_skip_stub (pc)
3322 CORE_ADDR pc;
3323{
3324 char *name;
3325 CORE_ADDR start_addr;
3326
3327 /* Find the starting address and name of the function containing the PC. */
3328 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
3329 return 0;
3330
3331 /* If the PC is in __mips16_ret_{d,s}f, this is a return stub and the
3332 target PC is in $31 ($ra). */
3333 if (strcmp (name, "__mips16_ret_sf") == 0
3334 || strcmp (name, "__mips16_ret_df") == 0)
3335 return read_register (RA_REGNUM);
3336
3337 if (strncmp (name, "__mips16_call_stub_", 19) == 0)
3338 {
3339 /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub
3340 and the target PC is in $2. */
3341 if (name[19] >= '0' && name[19] <= '9')
3342 return read_register (2);
3343
3344 /* If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
c5aa993b
JM
3345 before the jal instruction, this is effectively a call stub
3346 and the the target PC is in $2. Otherwise this is effectively
3347 a return stub and the target PC is in $18. */
c906108c
SS
3348 else if (name[19] == 's' || name[19] == 'd')
3349 {
3350 if (pc == start_addr)
3351 {
3352 /* Check if the target of the stub is a compiler-generated
c5aa993b
JM
3353 stub. Such a stub for a function bar might have a name
3354 like __fn_stub_bar, and might look like this:
3355 mfc1 $4,$f13
3356 mfc1 $5,$f12
3357 mfc1 $6,$f15
3358 mfc1 $7,$f14
3359 la $1,bar (becomes a lui/addiu pair)
3360 jr $1
3361 So scan down to the lui/addi and extract the target
3362 address from those two instructions. */
c906108c
SS
3363
3364 CORE_ADDR target_pc = read_register (2);
3365 t_inst inst;
3366 int i;
3367
3368 /* See if the name of the target function is __fn_stub_*. */
3369 if (find_pc_partial_function (target_pc, &name, NULL, NULL) == 0)
3370 return target_pc;
3371 if (strncmp (name, "__fn_stub_", 10) != 0
3372 && strcmp (name, "etext") != 0
3373 && strcmp (name, "_etext") != 0)
3374 return target_pc;
3375
3376 /* Scan through this _fn_stub_ code for the lui/addiu pair.
c5aa993b
JM
3377 The limit on the search is arbitrarily set to 20
3378 instructions. FIXME. */
c906108c
SS
3379 for (i = 0, pc = 0; i < 20; i++, target_pc += MIPS_INSTLEN)
3380 {
c5aa993b
JM
3381 inst = mips_fetch_instruction (target_pc);
3382 if ((inst & 0xffff0000) == 0x3c010000) /* lui $at */
3383 pc = (inst << 16) & 0xffff0000; /* high word */
3384 else if ((inst & 0xffff0000) == 0x24210000) /* addiu $at */
3385 return pc | (inst & 0xffff); /* low word */
c906108c
SS
3386 }
3387
3388 /* Couldn't find the lui/addui pair, so return stub address. */
3389 return target_pc;
3390 }
3391 else
3392 /* This is the 'return' part of a call stub. The return
3393 address is in $r18. */
3394 return read_register (18);
3395 }
3396 }
c5aa993b 3397 return 0; /* not a stub */
c906108c
SS
3398}
3399
3400
3401/* Return non-zero if the PC is inside a call thunk (aka stub or trampoline).
3402 This implements the IN_SOLIB_CALL_TRAMPOLINE macro. */
3403
3404int
3405mips_in_call_stub (pc, name)
3406 CORE_ADDR pc;
3407 char *name;
3408{
3409 CORE_ADDR start_addr;
3410
3411 /* Find the starting address of the function containing the PC. If the
3412 caller didn't give us a name, look it up at the same time. */
3413 if (find_pc_partial_function (pc, name ? NULL : &name, &start_addr, NULL) == 0)
3414 return 0;
3415
3416 if (strncmp (name, "__mips16_call_stub_", 19) == 0)
3417 {
3418 /* If the PC is in __mips16_call_stub_{1..10}, this is a call stub. */
3419 if (name[19] >= '0' && name[19] <= '9')
3420 return 1;
3421 /* If the PC at the start of __mips16_call_stub_{s,d}f_{0..10}, i.e.
c5aa993b 3422 before the jal instruction, this is effectively a call stub. */
c906108c
SS
3423 else if (name[19] == 's' || name[19] == 'd')
3424 return pc == start_addr;
3425 }
3426
c5aa993b 3427 return 0; /* not a stub */
c906108c
SS
3428}
3429
3430
3431/* Return non-zero if the PC is inside a return thunk (aka stub or trampoline).
3432 This implements the IN_SOLIB_RETURN_TRAMPOLINE macro. */
3433
3434int
3435mips_in_return_stub (pc, name)
3436 CORE_ADDR pc;
3437 char *name;
3438{
3439 CORE_ADDR start_addr;
3440
3441 /* Find the starting address of the function containing the PC. */
3442 if (find_pc_partial_function (pc, NULL, &start_addr, NULL) == 0)
3443 return 0;
3444
3445 /* If the PC is in __mips16_ret_{d,s}f, this is a return stub. */
3446 if (strcmp (name, "__mips16_ret_sf") == 0
3447 || strcmp (name, "__mips16_ret_df") == 0)
3448 return 1;
3449
3450 /* If the PC is in __mips16_call_stub_{s,d}f_{0..10} but not at the start,
c5aa993b 3451 i.e. after the jal instruction, this is effectively a return stub. */
c906108c
SS
3452 if (strncmp (name, "__mips16_call_stub_", 19) == 0
3453 && (name[19] == 's' || name[19] == 'd')
3454 && pc != start_addr)
3455 return 1;
3456
c5aa993b 3457 return 0; /* not a stub */
c906108c
SS
3458}
3459
3460
3461/* Return non-zero if the PC is in a library helper function that should
3462 be ignored. This implements the IGNORE_HELPER_CALL macro. */
3463
3464int
3465mips_ignore_helper (pc)
3466 CORE_ADDR pc;
3467{
3468 char *name;
3469
3470 /* Find the starting address and name of the function containing the PC. */
3471 if (find_pc_partial_function (pc, &name, NULL, NULL) == 0)
3472 return 0;
3473
3474 /* If the PC is in __mips16_ret_{d,s}f, this is a library helper function
3475 that we want to ignore. */
3476 return (strcmp (name, "__mips16_ret_sf") == 0
3477 || strcmp (name, "__mips16_ret_df") == 0);
3478}
3479
3480
3481/* Return a location where we can set a breakpoint that will be hit
3482 when an inferior function call returns. This is normally the
3483 program's entry point. Executables that don't have an entry
3484 point (e.g. programs in ROM) should define a symbol __CALL_DUMMY_ADDRESS
3485 whose address is the location where the breakpoint should be placed. */
3486
3487CORE_ADDR
3488mips_call_dummy_address ()
3489{
3490 struct minimal_symbol *sym;
3491
3492 sym = lookup_minimal_symbol ("__CALL_DUMMY_ADDRESS", NULL, NULL);
3493 if (sym)
3494 return SYMBOL_VALUE_ADDRESS (sym);
3495 else
3496 return entry_point_address ();
3497}
3498
3499
3500void
3501_initialize_mips_tdep ()
3502{
3503 static struct cmd_list_element *mipsfpulist = NULL;
3504 struct cmd_list_element *c;
3505
c5aa993b 3506 if (!tm_print_insn) /* Someone may have already set it */
c906108c
SS
3507 tm_print_insn = gdb_print_insn_mips;
3508
3509 /* Let the user turn off floating point and set the fence post for
3510 heuristic_proc_start. */
3511
3512 add_prefix_cmd ("mipsfpu", class_support, set_mipsfpu_command,
3513 "Set use of MIPS floating-point coprocessor.",
3514 &mipsfpulist, "set mipsfpu ", 0, &setlist);
3515 add_cmd ("single", class_support, set_mipsfpu_single_command,
3516 "Select single-precision MIPS floating-point coprocessor.",
3517 &mipsfpulist);
3518 add_cmd ("double", class_support, set_mipsfpu_double_command,
3519 "Select double-precision MIPS floating-point coprocessor .",
3520 &mipsfpulist);
3521 add_alias_cmd ("on", "double", class_support, 1, &mipsfpulist);
3522 add_alias_cmd ("yes", "double", class_support, 1, &mipsfpulist);
3523 add_alias_cmd ("1", "double", class_support, 1, &mipsfpulist);
3524 add_cmd ("none", class_support, set_mipsfpu_none_command,
3525 "Select no MIPS floating-point coprocessor.",
3526 &mipsfpulist);
3527 add_alias_cmd ("off", "none", class_support, 1, &mipsfpulist);
3528 add_alias_cmd ("no", "none", class_support, 1, &mipsfpulist);
3529 add_alias_cmd ("0", "none", class_support, 1, &mipsfpulist);
3530 add_cmd ("auto", class_support, set_mipsfpu_auto_command,
3531 "Select MIPS floating-point coprocessor automatically.",
3532 &mipsfpulist);
3533 add_cmd ("mipsfpu", class_support, show_mipsfpu_command,
3534 "Show current use of MIPS floating-point coprocessor target.",
3535 &showlist);
3536
3537 c = add_set_cmd ("processor", class_support, var_string_noescape,
3538 (char *) &tmp_mips_processor_type,
3539 "Set the type of MIPS processor in use.\n\
3540Set this to be able to access processor-type-specific registers.\n\
3541",
3542 &setlist);
3543 c->function.cfunc = mips_set_processor_type_command;
3544 c = add_show_from_set (c, &showlist);
3545 c->function.cfunc = mips_show_processor_type_command;
3546
3547 tmp_mips_processor_type = strsave (DEFAULT_MIPS_TYPE);
3548 mips_set_processor_type_command (strsave (DEFAULT_MIPS_TYPE), 0);
3549
3550 /* We really would like to have both "0" and "unlimited" work, but
3551 command.c doesn't deal with that. So make it a var_zinteger
3552 because the user can always use "999999" or some such for unlimited. */
3553 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
3554 (char *) &heuristic_fence_post,
3555 "\
3556Set the distance searched for the start of a function.\n\
3557If you are debugging a stripped executable, GDB needs to search through the\n\
3558program for the start of a function. This command sets the distance of the\n\
3559search. The only need to set it is when debugging a stripped executable.",
3560 &setlist);
3561 /* We need to throw away the frame cache when we set this, since it
3562 might change our ability to get backtraces. */
3563 c->function.sfunc = reinit_frame_cache_sfunc;
3564 add_show_from_set (c, &showlist);
3565
3566 /* Allow the user to control whether the upper bits of 64-bit
3567 addresses should be zeroed. */
3568 add_show_from_set
c5aa993b
JM
3569 (add_set_cmd ("mask-address", no_class, var_boolean, (char *) &mask_address_p,
3570 "Set zeroing of upper 32 bits of 64-bit addresses.\n\
c906108c
SS
3571Use \"on\" to enable the masking, and \"off\" to disable it.\n\
3572Without an argument, zeroing of upper address bits is enabled.", &setlist),
3573 &showlist);
3574}