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