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