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