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