* inftarg.c (child_thread_alive): New function to see if a
[deliverable/binutils-gdb.git] / gdb / mips-tdep.c
CommitLineData
7d9884b9 1/* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
18b46e7c 2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995
0434c1a0 3 Free Software Foundation, Inc.
bd5635a1
RP
4 Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU
5 and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
6
7This file is part of GDB.
8
361bf6ee 9This program is free software; you can redistribute it and/or modify
bd5635a1 10it under the terms of the GNU General Public License as published by
361bf6ee
JG
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
bd5635a1 13
361bf6ee 14This program is distributed in the hope that it will be useful,
bd5635a1
RP
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
361bf6ee
JG
20along with this program; if not, write to the Free Software
21Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 22
bd5635a1 23#include "defs.h"
bd5635a1
RP
24#include "frame.h"
25#include "inferior.h"
26#include "symtab.h"
27#include "value.h"
28#include "gdbcmd.h"
ef08856f 29#include "language.h"
bd5635a1 30#include "gdbcore.h"
62a469e1
SG
31#include "symfile.h"
32#include "objfiles.h"
3fed1c4a 33#include "gdbtypes.h"
bd5635a1 34
ee5fb959
JK
35#include "opcode/mips.h"
36
bd5635a1 37#define VM_MIN_ADDRESS (unsigned)0x400000
70126bf9
KH
38
39/* FIXME: Put this declaration in frame.h. */
40extern struct obstack frame_cache_obstack;
bd5635a1 41\f
002a422b 42#if 0
ee5fb959 43static int mips_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
002a422b 44#endif
ee5fb959 45
9f9f94aa
SS
46void mips_set_processor_type_command PARAMS ((char *, int));
47
1d9489c1
JK
48int mips_set_processor_type PARAMS ((char *));
49
50static void mips_show_processor_type_command PARAMS ((char *, int));
51
9f9f94aa
SS
52static void reinit_frame_cache_sfunc PARAMS ((char *, int,
53 struct cmd_list_element *));
54
55/* This value is the model of MIPS in use. It is derived from the value
56 of the PrID register. */
57
58char *mips_processor_type;
59
60char *tmp_mips_processor_type;
61
c2a0f1cb
ILT
62/* Some MIPS boards don't support floating point, so we permit the
63 user to turn it off. */
9f9f94aa 64
c2a0f1cb
ILT
65int mips_fpu = 1;
66
9f9f94aa
SS
67/* A set of original names, to be used when restoring back to generic
68 registers from a specific set. */
69
70char *mips_generic_reg_names[] = REGISTER_NAMES;
71
72/* Names of IDT R3041 registers. */
73
74char *mips_r3041_reg_names[] = {
75 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
76 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
77 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
78 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
79 "sr", "lo", "hi", "bad", "cause","pc",
80 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
81 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
82 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
83 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
84 "fsr", "fir", "fp", "",
85 "", "", "bus", "ccfg", "", "", "", "",
86 "", "", "port", "cmp", "", "", "epc", "prid",
87};
88
89/* Names of IDT R3051 registers. */
90
91char *mips_r3051_reg_names[] = {
92 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
93 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
94 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
95 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
96 "sr", "lo", "hi", "bad", "cause","pc",
97 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
98 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
99 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
100 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
101 "fsr", "fir", "fp", "",
102 "inx", "rand", "elo", "", "ctxt", "", "", "",
103 "", "", "ehi", "", "", "", "epc", "prid",
104};
105
106/* Names of IDT R3081 registers. */
107
108char *mips_r3081_reg_names[] = {
109 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
110 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
111 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
112 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
113 "sr", "lo", "hi", "bad", "cause","pc",
114 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
115 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
116 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
117 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
118 "fsr", "fir", "fp", "",
119 "inx", "rand", "elo", "cfg", "ctxt", "", "", "",
120 "", "", "ehi", "", "", "", "epc", "prid",
121};
122
123struct {
124 char *name;
125 char **regnames;
126} mips_processor_type_table[] = {
127 { "generic", mips_generic_reg_names },
128 { "r3041", mips_r3041_reg_names },
129 { "r3051", mips_r3051_reg_names },
130 { "r3071", mips_r3081_reg_names },
131 { "r3081", mips_r3081_reg_names },
132 { NULL, NULL }
133};
134
3127785a
RP
135/* Heuristic_proc_start may hunt through the text section for a long
136 time across a 2400 baud serial line. Allows the user to limit this
137 search. */
9f9f94aa 138
3127785a
RP
139static unsigned int heuristic_fence_post = 0;
140
0f552c5f
JG
141#define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
142#define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
143#define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
144#define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
145#define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
146#define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
147#define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
148#define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
149#define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
150#define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
bd5635a1 151#define _PROC_MAGIC_ 0x0F0F0F0F
0f552c5f
JG
152#define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
153#define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
bd5635a1
RP
154
155struct linked_proc_info
156{
157 struct mips_extra_func_info info;
158 struct linked_proc_info *next;
dac4929a 159} *linked_proc_desc_table = NULL;
bd5635a1
RP
160
161\f
70126bf9
KH
162/* Guaranteed to set fci->saved_regs to some values (it never leaves it
163 NULL). */
164
165void
166mips_find_saved_regs (fci)
9f9f94aa 167 struct frame_info *fci;
70126bf9
KH
168{
169 int ireg;
170 CORE_ADDR reg_position;
171 /* r0 bit means kernel trap */
172 int kernel_trap;
173 /* What registers have been saved? Bitmasks. */
174 unsigned long gen_mask, float_mask;
175 mips_extra_func_info_t proc_desc;
176
177 fci->saved_regs = (struct frame_saved_regs *)
178 obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
179 memset (fci->saved_regs, 0, sizeof (struct frame_saved_regs));
180
181 proc_desc = fci->proc_desc;
182 if (proc_desc == NULL)
183 /* I'm not sure how/whether this can happen. Normally when we can't
184 find a proc_desc, we "synthesize" one using heuristic_proc_desc
185 and set the saved_regs right away. */
186 return;
187
188 kernel_trap = PROC_REG_MASK(proc_desc) & 1;
189 gen_mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc);
190 float_mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc);
191
192 if (/* In any frame other than the innermost, we assume that all
193 registers have been saved. This assumes that all register
194 saves in a function happen before the first function
195 call. */
196 fci->next == NULL
197
198 /* In a dummy frame we know exactly where things are saved. */
199 && !PROC_DESC_IS_DUMMY (proc_desc)
200
201 /* Not sure exactly what kernel_trap means, but if it means
202 the kernel saves the registers without a prologue doing it,
203 we better not examine the prologue to see whether registers
204 have been saved yet. */
205 && !kernel_trap)
206 {
207 /* We need to figure out whether the registers that the proc_desc
208 claims are saved have been saved yet. */
209
210 CORE_ADDR addr;
211 int status;
212 char buf[4];
213 unsigned long inst;
214
215 /* Bitmasks; set if we have found a save for the register. */
216 unsigned long gen_save_found = 0;
217 unsigned long float_save_found = 0;
218
219 for (addr = PROC_LOW_ADDR (proc_desc);
220 addr < fci->pc /*&& (gen_mask != gen_save_found
221 || float_mask != float_save_found)*/;
222 addr += 4)
223 {
224 status = read_memory_nobpt (addr, buf, 4);
225 if (status)
226 memory_error (status, addr);
227 inst = extract_unsigned_integer (buf, 4);
228 if (/* sw reg,n($sp) */
229 (inst & 0xffe00000) == 0xafa00000
230
231 /* sw reg,n($r30) */
232 || (inst & 0xffe00000) == 0xafc00000
233
234 /* sd reg,n($sp) */
235 || (inst & 0xffe00000) == 0xffa00000)
236 {
237 /* It might be possible to use the instruction to
238 find the offset, rather than the code below which
239 is based on things being in a certain order in the
240 frame, but figuring out what the instruction's offset
241 is relative to might be a little tricky. */
242 int reg = (inst & 0x001f0000) >> 16;
243 gen_save_found |= (1 << reg);
244 }
245 else if (/* swc1 freg,n($sp) */
246 (inst & 0xffe00000) == 0xe7a00000
247
248 /* swc1 freg,n($r30) */
249 || (inst & 0xffe00000) == 0xe7c00000
250
251 /* sdc1 freg,n($sp) */
252 || (inst & 0xffe00000) == 0xf7a00000)
253
254 {
255 int reg = ((inst & 0x001f0000) >> 16);
256 float_save_found |= (1 << reg);
257 }
258 }
259 gen_mask = gen_save_found;
260 float_mask = float_save_found;
261 }
262
263 /* Fill in the offsets for the registers which gen_mask says
264 were saved. */
265 reg_position = fci->frame + PROC_REG_OFFSET (proc_desc);
266 for (ireg= 31; gen_mask; --ireg, gen_mask <<= 1)
267 if (gen_mask & 0x80000000)
268 {
269 fci->saved_regs->regs[ireg] = reg_position;
270 reg_position -= MIPS_REGSIZE;
271 }
272 /* Fill in the offsets for the registers which float_mask says
273 were saved. */
274 reg_position = fci->frame + PROC_FREG_OFFSET (proc_desc);
275
276 /* The freg_offset points to where the first *double* register
277 is saved. So skip to the high-order word. */
278 reg_position += 4;
279 for (ireg = 31; float_mask; --ireg, float_mask <<= 1)
280 if (float_mask & 0x80000000)
281 {
282 fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
283 reg_position -= MIPS_REGSIZE;
284 }
285
286 fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
287}
bd5635a1 288
0f552c5f 289static int
bd5635a1 290read_next_frame_reg(fi, regno)
9f9f94aa 291 struct frame_info *fi;
bd5635a1
RP
292 int regno;
293{
e157305c 294 /* If it is the frame for sigtramp we have a complete sigcontext
0434c1a0 295 somewhere above the frame and we get the saved registers from there.
e157305c 296 If the stack layout for sigtramp changes we might have to change these
e03c0cc6 297 constants and the companion fixup_sigtramp in mdebugread.c */
1b71de8e 298#ifndef SIGFRAME_BASE
0434c1a0
PS
299/* To satisfy alignment restrictions the sigcontext is located 4 bytes
300 above the sigtramp frame. */
301#define SIGFRAME_BASE 4
302#define SIGFRAME_PC_OFF (SIGFRAME_BASE + 2 * 4)
303#define SIGFRAME_REGSAVE_OFF (SIGFRAME_BASE + 3 * 4)
5efd597b
PS
304#endif
305#ifndef SIGFRAME_REG_SIZE
e03c0cc6 306#define SIGFRAME_REG_SIZE 4
1b71de8e 307#endif
bd5635a1 308 for (; fi; fi = fi->next)
70126bf9
KH
309 {
310 if (fi->signal_handler_caller)
311 {
bd5635a1
RP
312 int offset;
313 if (regno == PC_REGNUM) offset = SIGFRAME_PC_OFF;
e03c0cc6
ILT
314 else if (regno < 32) offset = (SIGFRAME_REGSAVE_OFF
315 + regno * SIGFRAME_REG_SIZE);
bd5635a1 316 else return 0;
70126bf9
KH
317 return read_memory_integer(fi->frame + offset, MIPS_REGSIZE);
318 }
bd5635a1 319 else if (regno == SP_REGNUM) return fi->frame;
70126bf9
KH
320 else
321 {
322 if (fi->saved_regs == NULL)
323 mips_find_saved_regs (fi);
324 if (fi->saved_regs->regs[regno])
325 return read_memory_integer(fi->saved_regs->regs[regno], MIPS_REGSIZE);
326 }
327 }
328 return read_register (regno);
bd5635a1
RP
329}
330
331int
332mips_frame_saved_pc(frame)
9f9f94aa 333 struct frame_info *frame;
bd5635a1 334{
0f552c5f 335 mips_extra_func_info_t proc_desc = frame->proc_desc;
0434c1a0
PS
336 /* We have to get the saved pc from the sigcontext
337 if it is a signal handler frame. */
338 int pcreg = frame->signal_handler_caller ? PC_REGNUM
339 : (proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM);
0f552c5f 340
bd5635a1
RP
341 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
342 return read_memory_integer(frame->frame - 4, 4);
0f552c5f 343
bd5635a1
RP
344 return read_next_frame_reg(frame, pcreg);
345}
346
347static struct mips_extra_func_info temp_proc_desc;
348static struct frame_saved_regs temp_saved_regs;
349
a8172eea
RP
350/* This fencepost looks highly suspicious to me. Removing it also
351 seems suspicious as it could affect remote debugging across serial
3127785a 352 lines. */
a8172eea 353
0f552c5f
JG
354static CORE_ADDR
355heuristic_proc_start(pc)
bd5635a1
RP
356 CORE_ADDR pc;
357{
bd5635a1 358 CORE_ADDR start_pc = pc;
3127785a 359 CORE_ADDR fence = start_pc - heuristic_fence_post;
0f552c5f
JG
360
361 if (start_pc == 0) return 0;
3127785a
RP
362
363 if (heuristic_fence_post == UINT_MAX
364 || fence < VM_MIN_ADDRESS)
365 fence = VM_MIN_ADDRESS;
0f552c5f 366
bd5635a1
RP
367 /* search back for previous return */
368 for (start_pc -= 4; ; start_pc -= 4)
a8172eea
RP
369 if (start_pc < fence)
370 {
3127785a
RP
371 /* It's not clear to me why we reach this point when
372 stop_soon_quietly, but with this test, at least we
373 don't print out warnings for every child forked (eg, on
374 decstation). 22apr93 rich@cygnus.com. */
375 if (!stop_soon_quietly)
376 {
23d35572
JK
377 static int blurb_printed = 0;
378
3127785a
RP
379 if (fence == VM_MIN_ADDRESS)
380 warning("Hit beginning of text section without finding");
381 else
382 warning("Hit heuristic-fence-post without finding");
383
23d35572
JK
384 warning("enclosing function for address 0x%x", pc);
385 if (!blurb_printed)
386 {
387 printf_filtered ("\
388This warning occurs if you are debugging a function without any symbols\n\
389(for example, in a stripped executable). In that case, you may wish to\n\
390increase the size of the search with the `set heuristic-fence-post' command.\n\
391\n\
392Otherwise, you told GDB there was a function where there isn't one, or\n\
393(more likely) you have encountered a bug in GDB.\n");
394 blurb_printed = 1;
395 }
3127785a
RP
396 }
397
a8172eea
RP
398 return 0;
399 }
bd5635a1
RP
400 else if (ABOUT_TO_RETURN(start_pc))
401 break;
402
403 start_pc += 8; /* skip return, and its delay slot */
404#if 0
405 /* skip nops (usually 1) 0 - is this */
406 while (start_pc < pc && read_memory_integer (start_pc, 4) == 0)
407 start_pc += 4;
408#endif
409 return start_pc;
410}
411
0f552c5f 412static mips_extra_func_info_t
bd5635a1
RP
413heuristic_proc_desc(start_pc, limit_pc, next_frame)
414 CORE_ADDR start_pc, limit_pc;
9f9f94aa 415 struct frame_info *next_frame;
bd5635a1 416{
ac57e5ad 417 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
bd5635a1
RP
418 CORE_ADDR cur_pc;
419 int frame_size;
420 int has_frame_reg = 0;
e03c0cc6 421 int reg30 = 0; /* Value of $r30. Used by gcc for frame-pointer */
bd5635a1
RP
422 unsigned long reg_mask = 0;
423
424 if (start_pc == 0) return NULL;
9f9f94aa
SS
425 memset (&temp_proc_desc, '\0', sizeof(temp_proc_desc));
426 memset (&temp_saved_regs, '\0', sizeof(struct frame_saved_regs));
427 PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
a70dc898 428
9f9f94aa
SS
429 if (start_pc + 200 < limit_pc)
430 limit_pc = start_pc + 200;
bd5635a1
RP
431 restart:
432 frame_size = 0;
433 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4) {
34df79fc 434 char buf[4];
bd5635a1
RP
435 unsigned long word;
436 int status;
437
34df79fc
JK
438 status = read_memory_nobpt (cur_pc, buf, 4);
439 if (status) memory_error (status, cur_pc);
440 word = extract_unsigned_integer (buf, 4);
441
bd5635a1
RP
442 if ((word & 0xFFFF0000) == 0x27bd0000) /* addiu $sp,$sp,-i */
443 frame_size += (-word) & 0xFFFF;
444 else if ((word & 0xFFFF0000) == 0x23bd0000) /* addu $sp,$sp,-i */
445 frame_size += (-word) & 0xFFFF;
446 else if ((word & 0xFFE00000) == 0xafa00000) { /* sw reg,offset($sp) */
447 int reg = (word & 0x001F0000) >> 16;
448 reg_mask |= 1 << reg;
002a422b 449 temp_saved_regs.regs[reg] = sp + (word & 0xffff);
bd5635a1
RP
450 }
451 else if ((word & 0xFFFF0000) == 0x27be0000) { /* addiu $30,$sp,size */
002a422b
JK
452 if ((word & 0xffff) != frame_size)
453 reg30 = sp + (word & 0xffff);
bd5635a1
RP
454 else if (!has_frame_reg) {
455 int alloca_adjust;
456 has_frame_reg = 1;
457 reg30 = read_next_frame_reg(next_frame, 30);
002a422b 458 alloca_adjust = reg30 - (sp + (word & 0xffff));
bd5635a1
RP
459 if (alloca_adjust > 0) {
460 /* FP > SP + frame_size. This may be because
e03c0cc6 461 * of an alloca or somethings similar.
bd5635a1
RP
462 * Fix sp to "pre-alloca" value, and try again.
463 */
464 sp += alloca_adjust;
465 goto restart;
466 }
467 }
468 }
469 else if ((word & 0xFFE00000) == 0xafc00000) { /* sw reg,offset($30) */
470 int reg = (word & 0x001F0000) >> 16;
471 reg_mask |= 1 << reg;
002a422b 472 temp_saved_regs.regs[reg] = reg30 + (word & 0xffff);
bd5635a1
RP
473 }
474 }
475 if (has_frame_reg) {
476 PROC_FRAME_REG(&temp_proc_desc) = 30;
477 PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
478 }
479 else {
480 PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
481 PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
482 }
483 PROC_REG_MASK(&temp_proc_desc) = reg_mask;
484 PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
485 return &temp_proc_desc;
486}
487
0f552c5f 488static mips_extra_func_info_t
9f9f94aa
SS
489find_proc_desc (pc, next_frame)
490 CORE_ADDR pc;
491 struct frame_info *next_frame;
bd5635a1
RP
492{
493 mips_extra_func_info_t proc_desc;
0f552c5f 494 struct block *b = block_for_pc(pc);
48be4c35
JK
495 struct symbol *sym;
496 CORE_ADDR startaddr;
497
498 find_pc_partial_function (pc, NULL, &startaddr, NULL);
499 if (b == NULL)
500 sym = NULL;
501 else
502 {
503 if (startaddr > BLOCK_START (b))
504 /* This is the "pathological" case referred to in a comment in
505 print_frame_info. It might be better to move this check into
506 symbol reading. */
507 sym = NULL;
508 else
509 sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
510 0, NULL);
511 }
0f552c5f
JG
512
513 if (sym)
bd5635a1
RP
514 {
515 /* IF this is the topmost frame AND
516 * (this proc does not have debugging information OR
517 * the PC is in the procedure prologue)
be772100 518 * THEN create a "heuristic" proc_desc (by analyzing
bd5635a1
RP
519 * the actual code) to replace the "official" proc_desc.
520 */
9f9f94aa 521 proc_desc = (mips_extra_func_info_t) SYMBOL_VALUE (sym);
bd5635a1
RP
522 if (next_frame == NULL) {
523 struct symtab_and_line val;
524 struct symbol *proc_symbol =
525 PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc);
0f552c5f 526
bd5635a1
RP
527 if (proc_symbol) {
528 val = find_pc_line (BLOCK_START
529 (SYMBOL_BLOCK_VALUE(proc_symbol)),
530 0);
531 val.pc = val.end ? val.end : pc;
532 }
533 if (!proc_symbol || pc < val.pc) {
534 mips_extra_func_info_t found_heuristic =
9f9f94aa
SS
535 heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
536 pc, next_frame);
537 if (found_heuristic)
538 proc_desc = found_heuristic;
bd5635a1
RP
539 }
540 }
541 }
542 else
543 {
0f552c5f
JG
544 /* Is linked_proc_desc_table really necessary? It only seems to be used
545 by procedure call dummys. However, the procedures being called ought
546 to have their own proc_descs, and even if they don't,
547 heuristic_proc_desc knows how to create them! */
548
bd5635a1 549 register struct linked_proc_info *link;
9f9f94aa 550
bd5635a1 551 for (link = linked_proc_desc_table; link; link = link->next)
9f9f94aa
SS
552 if (PROC_LOW_ADDR(&link->info) <= pc
553 && PROC_HIGH_ADDR(&link->info) > pc)
554 return &link->info;
23d35572 555
48be4c35
JK
556 if (startaddr == 0)
557 startaddr = heuristic_proc_start (pc);
558
bd5635a1 559 proc_desc =
48be4c35 560 heuristic_proc_desc (startaddr, pc, next_frame);
bd5635a1
RP
561 }
562 return proc_desc;
563}
564
565mips_extra_func_info_t cached_proc_desc;
566
9f9f94aa 567CORE_ADDR
0f552c5f 568mips_frame_chain(frame)
9f9f94aa 569 struct frame_info *frame;
bd5635a1 570{
bd5635a1
RP
571 mips_extra_func_info_t proc_desc;
572 CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
be772100 573
0f552c5f
JG
574 if (saved_pc == 0 || inside_entry_file (saved_pc))
575 return 0;
576
bd5635a1 577 proc_desc = find_proc_desc(saved_pc, frame);
0f552c5f
JG
578 if (!proc_desc)
579 return 0;
580
bd5635a1 581 cached_proc_desc = proc_desc;
e797b4bc
JK
582
583 /* If no frame pointer and frame size is zero, we must be at end
584 of stack (or otherwise hosed). If we don't check frame size,
585 we loop forever if we see a zero size frame. */
586 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
3f528883 587 && PROC_FRAME_OFFSET (proc_desc) == 0
199b2450
TL
588 /* The previous frame from a sigtramp frame might be frameless
589 and have frame size zero. */
590 && !frame->signal_handler_caller)
bdef72d2
JK
591 return 0;
592 else
593 return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
594 + PROC_FRAME_OFFSET(proc_desc);
bd5635a1
RP
595}
596
597void
598init_extra_frame_info(fci)
599 struct frame_info *fci;
600{
bd5635a1 601 /* Use proc_desc calculated in frame_chain */
ee5fb959
JK
602 mips_extra_func_info_t proc_desc =
603 fci->next ? cached_proc_desc : find_proc_desc(fci->pc, fci->next);
0f552c5f 604
70126bf9 605 fci->saved_regs = NULL;
bd5635a1 606 fci->proc_desc =
ee5fb959 607 proc_desc == &temp_proc_desc ? 0 : proc_desc;
bd5635a1
RP
608 if (proc_desc)
609 {
c2a0f1cb 610 /* Fixup frame-pointer - only needed for top frame */
5efd597b
PS
611 /* This may not be quite right, if proc has a real frame register.
612 Get the value of the frame relative sp, procedure might have been
613 interrupted by a signal at it's very start. */
70126bf9
KH
614 if (fci->pc == PROC_LOW_ADDR (proc_desc)
615 && !PROC_DESC_IS_DUMMY (proc_desc))
616 fci->frame = read_next_frame_reg (fci->next, SP_REGNUM);
c2a0f1cb 617 else
70126bf9
KH
618 fci->frame =
619 read_next_frame_reg (fci->next, PROC_FRAME_REG (proc_desc))
620 + PROC_FRAME_OFFSET (proc_desc);
bd5635a1 621
48be4c35 622 if (proc_desc == &temp_proc_desc)
ee5fb959 623 {
70126bf9
KH
624 fci->saved_regs = (struct frame_saved_regs*)
625 obstack_alloc (&frame_cache_obstack,
626 sizeof (struct frame_saved_regs));
627 *fci->saved_regs = temp_saved_regs;
628 fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
ee5fb959 629 }
bd5635a1
RP
630
631 /* hack: if argument regs are saved, guess these contain args */
632 if ((PROC_REG_MASK(proc_desc) & 0xF0) == 0) fci->num_args = -1;
633 else if ((PROC_REG_MASK(proc_desc) & 0x80) == 0) fci->num_args = 4;
634 else if ((PROC_REG_MASK(proc_desc) & 0x40) == 0) fci->num_args = 3;
635 else if ((PROC_REG_MASK(proc_desc) & 0x20) == 0) fci->num_args = 2;
636 else if ((PROC_REG_MASK(proc_desc) & 0x10) == 0) fci->num_args = 1;
bd5635a1 637 }
bd5635a1
RP
638}
639
a70dc898
RP
640/* MIPS stack frames are almost impenetrable. When execution stops,
641 we basically have to look at symbol information for the function
642 that we stopped in, which tells us *which* register (if any) is
643 the base of the frame pointer, and what offset from that register
644 the frame itself is at.
645
646 This presents a problem when trying to examine a stack in memory
647 (that isn't executing at the moment), using the "frame" command. We
648 don't have a PC, nor do we have any registers except SP.
649
650 This routine takes two arguments, SP and PC, and tries to make the
651 cached frames look as if these two arguments defined a frame on the
652 cache. This allows the rest of info frame to extract the important
653 arguments without difficulty. */
654
9f9f94aa 655struct frame_info *
c2a0f1cb
ILT
656setup_arbitrary_frame (argc, argv)
657 int argc;
9f9f94aa 658 CORE_ADDR *argv;
a70dc898 659{
c2a0f1cb
ILT
660 if (argc != 2)
661 error ("MIPS frame specifications require two arguments: sp and pc");
662
663 return create_new_frame (argv[0], argv[1]);
a70dc898
RP
664}
665
bd5635a1 666
0f552c5f
JG
667CORE_ADDR
668mips_push_arguments(nargs, args, sp, struct_return, struct_addr)
bd5635a1 669 int nargs;
ac57e5ad 670 value_ptr *args;
bd5635a1
RP
671 CORE_ADDR sp;
672 int struct_return;
673 CORE_ADDR struct_addr;
674{
bd5635a1 675 register i;
70126bf9 676 int accumulate_size = struct_return ? MIPS_REGSIZE : 0;
bd5635a1
RP
677 struct mips_arg { char *contents; int len; int offset; };
678 struct mips_arg *mips_args =
70126bf9 679 (struct mips_arg*)alloca((nargs + 4) * sizeof(struct mips_arg));
bd5635a1 680 register struct mips_arg *m_arg;
70126bf9
KH
681 int fake_args = 0;
682
bd5635a1 683 for (i = 0, m_arg = mips_args; i < nargs; i++, m_arg++) {
ac57e5ad 684 value_ptr arg = value_arg_coerce (args[i]);
bd5635a1
RP
685 m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
686 /* This entire mips-specific routine is because doubles must be aligned
687 * on 8-byte boundaries. It still isn't quite right, because MIPS decided
688 * to align 'struct {int a, b}' on 4-byte boundaries (even though this
689 * breaks their varargs implementation...). A correct solution
690 * requires an simulation of gcc's 'alignof' (and use of 'alignof'
691 * in stdarg.h/varargs.h).
70126bf9
KH
692 * On the 64 bit r4000 we always pass the first four arguments
693 * using eight bytes each, so that we can load them up correctly
694 * in CALL_DUMMY.
bd5635a1 695 */
70126bf9
KH
696 if (m_arg->len > 4)
697 accumulate_size = (accumulate_size + 7) & -8;
bd5635a1 698 m_arg->offset = accumulate_size;
bd5635a1 699 m_arg->contents = VALUE_CONTENTS(arg);
9f9f94aa
SS
700 if (! GDB_TARGET_IS_MIPS64)
701 accumulate_size = (accumulate_size + m_arg->len + 3) & -4;
70126bf9
KH
702 else
703 {
9f9f94aa
SS
704 if (accumulate_size >= 4 * MIPS_REGSIZE)
705 accumulate_size = (accumulate_size + m_arg->len + 3) &~ 4;
706 else
70126bf9 707 {
9f9f94aa
SS
708 static char zeroes[8] = { 0 };
709 int len = m_arg->len;
710
711 if (len < 8)
712 {
713 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
714 m_arg->offset += 8 - len;
715 ++m_arg;
716 m_arg->len = 8 - len;
717 m_arg->contents = zeroes;
718 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
719 m_arg->offset = accumulate_size;
720 else
721 m_arg->offset = accumulate_size + len;
722 ++fake_args;
723 }
724 accumulate_size = (accumulate_size + len + 7) & ~8;
70126bf9 725 }
70126bf9 726 }
bd5635a1
RP
727 }
728 accumulate_size = (accumulate_size + 7) & (-8);
70126bf9
KH
729 if (accumulate_size < 4 * MIPS_REGSIZE)
730 accumulate_size = 4 * MIPS_REGSIZE;
bd5635a1 731 sp -= accumulate_size;
70126bf9 732 for (i = nargs + fake_args; m_arg--, --i >= 0; )
bd5635a1 733 write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
5efd597b
PS
734 if (struct_return)
735 {
736 char buf[TARGET_PTR_BIT / HOST_CHAR_BIT];
737
738 store_address (buf, sizeof buf, struct_addr);
739 write_memory (sp, buf, sizeof buf);
740 }
bd5635a1
RP
741 return sp;
742}
743
744/* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
e03c0cc6 745#define MASK(i,j) (((1 << ((j)+1))-1) ^ ((1 << (i))-1))
bd5635a1
RP
746
747void
748mips_push_dummy_frame()
749{
5efd597b 750 char buffer[MAX_REGISTER_RAW_SIZE];
bd5635a1
RP
751 int ireg;
752 struct linked_proc_info *link = (struct linked_proc_info*)
753 xmalloc(sizeof(struct linked_proc_info));
754 mips_extra_func_info_t proc_desc = &link->info;
755 CORE_ADDR sp = read_register (SP_REGNUM);
756 CORE_ADDR save_address;
bd5635a1
RP
757 link->next = linked_proc_desc_table;
758 linked_proc_desc_table = link;
759#define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
760#define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<31)
761#define GEN_REG_SAVE_COUNT 22
762#define FLOAT_REG_SAVE_MASK MASK(0,19)
763#define FLOAT_REG_SAVE_COUNT 20
764#define SPECIAL_REG_SAVE_COUNT 4
765 /*
766 * The registers we must save are all those not preserved across
767 * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
768 * In addition, we must save the PC, and PUSH_FP_REGNUM.
769 * (Ideally, we should also save MDLO/-HI and FP Control/Status reg.)
770 *
771 * Dummy frame layout:
772 * (high memory)
773 * Saved PC
774 * Saved MMHI, MMLO, FPC_CSR
775 * Saved R31
776 * Saved R28
777 * ...
778 * Saved R1
779 * Saved D18 (i.e. F19, F18)
780 * ...
781 * Saved D0 (i.e. F1, F0)
c2a0f1cb 782 * CALL_DUMMY (subroutine stub; see tm-mips.h)
bd5635a1
RP
783 * Parameter build area (not yet implemented)
784 * (low memory)
785 */
786 PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
c2a0f1cb 787 PROC_FREG_MASK(proc_desc) = mips_fpu ? FLOAT_REG_SAVE_MASK : 0;
bd5635a1
RP
788 PROC_REG_OFFSET(proc_desc) = /* offset of (Saved R31) from FP */
789 -sizeof(long) - 4 * SPECIAL_REG_SAVE_COUNT;
790 PROC_FREG_OFFSET(proc_desc) = /* offset of (Saved D18) from FP */
791 -sizeof(double) - 4 * (SPECIAL_REG_SAVE_COUNT + GEN_REG_SAVE_COUNT);
792 /* save general registers */
793 save_address = sp + PROC_REG_OFFSET(proc_desc);
794 for (ireg = 32; --ireg >= 0; )
795 if (PROC_REG_MASK(proc_desc) & (1 << ireg))
796 {
3fed1c4a
JK
797 read_register_gen (ireg, buffer);
798
799 /* Need to fix the save_address decrement below, and also make sure
800 that we don't run into problems with the size of the dummy frame
801 or any of the offsets within it. */
802 if (REGISTER_RAW_SIZE (ireg) > 4)
803 error ("Cannot call functions on mips64");
804
5efd597b 805 write_memory (save_address, buffer, REGISTER_RAW_SIZE (ireg));
bd5635a1
RP
806 save_address -= 4;
807 }
0b0d6c3f
PS
808 /* save floating-points registers starting with high order word */
809 save_address = sp + PROC_FREG_OFFSET(proc_desc) + 4;
bd5635a1
RP
810 for (ireg = 32; --ireg >= 0; )
811 if (PROC_FREG_MASK(proc_desc) & (1 << ireg))
812 {
3fed1c4a
JK
813 read_register_gen (ireg + FP0_REGNUM, buffer);
814
815 if (REGISTER_RAW_SIZE (ireg + FP0_REGNUM) > 4)
816 error ("Cannot call functions on mips64");
817
818 write_memory (save_address, buffer,
819 REGISTER_RAW_SIZE (ireg + FP0_REGNUM));
bd5635a1
RP
820 save_address -= 4;
821 }
822 write_register (PUSH_FP_REGNUM, sp);
823 PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM;
824 PROC_FRAME_OFFSET(proc_desc) = 0;
3fed1c4a 825 read_register_gen (PC_REGNUM, buffer);
5efd597b 826 write_memory (sp - 4, buffer, REGISTER_RAW_SIZE (PC_REGNUM));
3fed1c4a 827 read_register_gen (HI_REGNUM, buffer);
5efd597b 828 write_memory (sp - 8, buffer, REGISTER_RAW_SIZE (HI_REGNUM));
3fed1c4a 829 read_register_gen (LO_REGNUM, buffer);
5efd597b 830 write_memory (sp - 12, buffer, REGISTER_RAW_SIZE (LO_REGNUM));
3fed1c4a
JK
831 if (mips_fpu)
832 read_register_gen (FCRCS_REGNUM, buffer);
833 else
834 memset (buffer, 0, REGISTER_RAW_SIZE (FCRCS_REGNUM));
5efd597b 835 write_memory (sp - 16, buffer, REGISTER_RAW_SIZE (FCRCS_REGNUM));
c2a0f1cb
ILT
836 sp -= 4 * (GEN_REG_SAVE_COUNT
837 + (mips_fpu ? FLOAT_REG_SAVE_COUNT : 0)
838 + SPECIAL_REG_SAVE_COUNT);
bd5635a1
RP
839 write_register (SP_REGNUM, sp);
840 PROC_LOW_ADDR(proc_desc) = sp - CALL_DUMMY_SIZE + CALL_DUMMY_START_OFFSET;
841 PROC_HIGH_ADDR(proc_desc) = sp;
842 SET_PROC_DESC_IS_DUMMY(proc_desc);
843 PROC_PC_REG(proc_desc) = RA_REGNUM;
844}
845
846void
847mips_pop_frame()
dac4929a
SG
848{
849 register int regnum;
9f9f94aa
SS
850 struct frame_info *frame = get_current_frame ();
851 CORE_ADDR new_sp = FRAME_FP (frame);
dac4929a 852
a70dc898 853 mips_extra_func_info_t proc_desc = frame->proc_desc;
dac4929a
SG
854
855 write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
70126bf9
KH
856 if (frame->saved_regs == NULL)
857 mips_find_saved_regs (frame);
dac4929a
SG
858 if (proc_desc)
859 {
860 for (regnum = 32; --regnum >= 0; )
861 if (PROC_REG_MASK(proc_desc) & (1 << regnum))
862 write_register (regnum,
863 read_memory_integer (frame->saved_regs->regs[regnum],
864 4));
865 for (regnum = 32; --regnum >= 0; )
866 if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
867 write_register (regnum + FP0_REGNUM,
868 read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 4));
869 }
870 write_register (SP_REGNUM, new_sp);
871 flush_cached_frames ();
dac4929a 872
199b2450 873 if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
bd5635a1 874 {
dac4929a
SG
875 struct linked_proc_info *pi_ptr, *prev_ptr;
876
877 for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
878 pi_ptr != NULL;
879 prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
880 {
881 if (&pi_ptr->info == proc_desc)
882 break;
883 }
884
885 if (pi_ptr == NULL)
886 error ("Can't locate dummy extra frame info\n");
887
888 if (prev_ptr != NULL)
889 prev_ptr->next = pi_ptr->next;
890 else
891 linked_proc_desc_table = pi_ptr->next;
892
893 free (pi_ptr);
894
bd5635a1
RP
895 write_register (HI_REGNUM, read_memory_integer(new_sp - 8, 4));
896 write_register (LO_REGNUM, read_memory_integer(new_sp - 12, 4));
c2a0f1cb
ILT
897 if (mips_fpu)
898 write_register (FCRCS_REGNUM, read_memory_integer(new_sp - 16, 4));
bd5635a1 899 }
bd5635a1
RP
900}
901
0f552c5f 902static void
a70dc898 903mips_print_register (regnum, all)
bd5635a1
RP
904 int regnum, all;
905{
48be4c35 906 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
3fed1c4a
JK
907 struct type *our_type =
908 init_type (TYPE_CODE_INT,
909 /* We will fill in the length for each register. */
910 0,
911 TYPE_FLAG_UNSIGNED,
912 NULL,
913 NULL);
bd5635a1 914
48be4c35
JK
915 /* Get the data in raw format. */
916 if (read_relative_register_raw_bytes (regnum, raw_buffer))
917 {
918 printf_filtered ("%s: [Invalid]", reg_names[regnum]);
919 return;
920 }
921
922 /* If an even floating pointer register, also print as double. */
923 if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM+32
924 && !((regnum-FP0_REGNUM) & 1)) {
925 char dbuffer[MAX_REGISTER_RAW_SIZE];
926
927 read_relative_register_raw_bytes (regnum, dbuffer);
928 read_relative_register_raw_bytes (regnum+1, dbuffer+4);
ac8cf67d 929#ifdef REGISTER_CONVERT_TO_TYPE
48be4c35 930 REGISTER_CONVERT_TO_TYPE(regnum, builtin_type_double, dbuffer);
ac8cf67d 931#endif
48be4c35
JK
932 printf_filtered ("(d%d: ", regnum-FP0_REGNUM);
933 val_print (builtin_type_double, dbuffer, 0,
199b2450 934 gdb_stdout, 0, 1, 0, Val_pretty_default);
48be4c35
JK
935 printf_filtered ("); ");
936 }
199b2450 937 fputs_filtered (reg_names[regnum], gdb_stdout);
48be4c35
JK
938
939 /* The problem with printing numeric register names (r26, etc.) is that
940 the user can't use them on input. Probably the best solution is to
941 fix it so that either the numeric or the funky (a2, etc.) names
942 are accepted on input. */
943 if (regnum < 32)
944 printf_filtered ("(r%d): ", regnum);
945 else
946 printf_filtered (": ");
bd5635a1 947
48be4c35 948 /* If virtual format is floating, print it that way. */
ac57e5ad 949 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT)
48be4c35 950 val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0,
199b2450 951 gdb_stdout, 0, 1, 0, Val_pretty_default);
48be4c35
JK
952 /* Else print as integer in hex. */
953 else
ac57e5ad
SS
954 print_scalar_formatted (raw_buffer, REGISTER_VIRTUAL_TYPE (regnum),
955 'x', 0, gdb_stdout);
bd5635a1
RP
956}
957
d8b3b00e 958/* Replacement for generic do_registers_info. */
9f9f94aa 959
0f552c5f 960void
361bf6ee 961mips_do_registers_info (regnum, fpregs)
bd5635a1 962 int regnum;
361bf6ee 963 int fpregs;
bd5635a1 964{
9f9f94aa
SS
965 if (regnum != -1)
966 {
967 if (*(reg_names[regnum]) == '\0')
968 error ("Not a valid register for the current processor type");
969
bd5635a1
RP
970 mips_print_register (regnum, 0);
971 printf_filtered ("\n");
9f9f94aa
SS
972 }
973 else
974 {
975 int did_newline;
976
977 for (regnum = 0; regnum < NUM_REGS; )
978 {
979 if (((!fpregs) && regnum >= FP0_REGNUM && regnum <= FCRIR_REGNUM)
980 || *(reg_names[regnum]) == '\0')
981 {
982 regnum++;
983 continue;
984 }
bd5635a1
RP
985 mips_print_register (regnum, 1);
986 regnum++;
9f9f94aa
SS
987 printf_filtered ("; ");
988 did_newline = 0;
989 if ((regnum & 3) == 0)
990 {
991 printf_filtered ("\n");
992 did_newline = 1;
993 }
994 }
995 if (!did_newline)
996 printf_filtered ("\n");
997 }
bd5635a1 998}
9f9f94aa 999
bd5635a1
RP
1000/* Return number of args passed to a frame. described by FIP.
1001 Can return -1, meaning no way to tell. */
1002
0f552c5f 1003int
9f9f94aa
SS
1004mips_frame_num_args (frame)
1005 struct frame_info *frame;
bd5635a1 1006{
9f9f94aa
SS
1007#if 0 /* FIXME Use or lose this! */
1008 struct chain_info_t *p;
bd5635a1 1009
9f9f94aa
SS
1010 p = mips_find_cached_frame (FRAME_FP (frame));
1011 if (p->valid)
1012 return p->the_info.numargs;
bd5635a1 1013#endif
9f9f94aa 1014 return -1;
bd5635a1 1015}
407a8389 1016\f
427fec5d 1017/* Is this a branch with a delay slot? */
9f9f94aa 1018
b5aff268
JK
1019static int is_delayed PARAMS ((unsigned long));
1020
ee5fb959
JK
1021static int
1022is_delayed (insn)
1023 unsigned long insn;
1024{
1025 int i;
1026 for (i = 0; i < NUMOPCODES; ++i)
1027 if (mips_opcodes[i].pinfo != INSN_MACRO
1028 && (insn & mips_opcodes[i].mask) == mips_opcodes[i].match)
1029 break;
427fec5d
JK
1030 return (i < NUMOPCODES
1031 && (mips_opcodes[i].pinfo & (INSN_UNCOND_BRANCH_DELAY
1032 | INSN_COND_BRANCH_DELAY
1033 | INSN_COND_BRANCH_LIKELY)));
ee5fb959 1034}
b5aff268
JK
1035
1036int
1037mips_step_skips_delay (pc)
1038 CORE_ADDR pc;
1039{
1040 char buf[4];
1041
1042 if (target_read_memory (pc, buf, 4) != 0)
1043 /* If error reading memory, guess that it is not a delayed branch. */
1044 return 0;
1045 return is_delayed (extract_unsigned_integer (buf, 4));
1046}
ee5fb959
JK
1047
1048/* To skip prologues, I use this predicate. Returns either PC itself
1049 if the code at PC does not look like a function prologue; otherwise
1050 returns an address that (if we're lucky) follows the prologue. If
1051 LENIENT, then we must skip everything which is involved in setting
1052 up the frame (it's OK to skip more, just so long as we don't skip
1053 anything which might clobber the registers which are being saved.
1054 We must skip more in the case where part of the prologue is in the
1055 delay slot of a non-prologue instruction). */
bd5635a1 1056
be772100 1057CORE_ADDR
ee5fb959 1058mips_skip_prologue (pc, lenient)
bd5635a1 1059 CORE_ADDR pc;
ee5fb959 1060 int lenient;
bd5635a1 1061{
bd5635a1 1062 unsigned long inst;
d747e0af 1063 int offset;
0b0d6c3f 1064 int seen_sp_adjust = 0;
ac57e5ad 1065 int load_immediate_bytes = 0;
bd5635a1 1066
e157305c
PS
1067 /* Skip the typical prologue instructions. These are the stack adjustment
1068 instruction and the instructions that save registers on the stack
1069 or in the gcc frame. */
ee5fb959
JK
1070 for (offset = 0; offset < 100; offset += 4)
1071 {
1072 char buf[4];
1073 int status;
1074
1075 status = read_memory_nobpt (pc + offset, buf, 4);
1076 if (status)
1077 memory_error (status, pc + offset);
1078 inst = extract_unsigned_integer (buf, 4);
1079
002a422b 1080#if 0
ee5fb959
JK
1081 if (lenient && is_delayed (inst))
1082 continue;
002a422b 1083#endif
ee5fb959 1084
e157305c 1085 if ((inst & 0xffff0000) == 0x27bd0000) /* addiu $sp,$sp,offset */
0b0d6c3f 1086 seen_sp_adjust = 1;
ac57e5ad
SS
1087 else if (inst == 0x03a1e823 || /* subu $sp,$sp,$at */
1088 inst == 0x03a8e823) /* subu $sp,$sp,$t0 */
1089 seen_sp_adjust = 1;
e157305c
PS
1090 else if ((inst & 0xFFE00000) == 0xAFA00000 && (inst & 0x001F0000))
1091 continue; /* sw reg,n($sp) */
1092 /* reg != $zero */
1093 else if ((inst & 0xFFE00000) == 0xE7A00000) /* swc1 freg,n($sp) */
1094 continue;
1095 else if ((inst & 0xF3E00000) == 0xA3C00000 && (inst & 0x001F0000))
1096 /* sx reg,n($s8) */
1097 continue; /* reg != $zero */
1d9489c1
JK
1098
1099 /* move $s8,$sp. With different versions of gas this will be either
1100 `addu $s8,$sp,$zero' or `or $s8,$sp,$zero'. Accept either. */
1101 else if (inst == 0x03A0F021 || inst == 0x03a0f025)
0b0d6c3f 1102 continue;
1d9489c1 1103
1b71de8e
PS
1104 else if ((inst & 0xFF9F07FF) == 0x00800021) /* move reg,$a0-$a3 */
1105 continue;
e03c0cc6
ILT
1106 else if ((inst & 0xffff0000) == 0x3c1c0000) /* lui $gp,n */
1107 continue;
1108 else if ((inst & 0xffff0000) == 0x279c0000) /* addiu $gp,$gp,n */
1109 continue;
1110 else if (inst == 0x0399e021 /* addu $gp,$gp,$t9 */
1111 || inst == 0x033ce021) /* addu $gp,$t9,$gp */
1112 continue;
ac57e5ad
SS
1113 /* The following instructions load $at or $t0 with an immediate
1114 value in preparation for a stack adjustment via
1115 subu $sp,$sp,[$at,$t0]. These instructions could also initialize
1116 a local variable, so we accept them only before a stack adjustment
1117 instruction was seen. */
1118 else if (!seen_sp_adjust)
1119 {
1120 if ((inst & 0xffff0000) == 0x3c010000 || /* lui $at,n */
1121 (inst & 0xffff0000) == 0x3c080000) /* lui $t0,n */
1122 {
1123 load_immediate_bytes += 4;
1124 continue;
1125 }
1126 else if ((inst & 0xffff0000) == 0x34210000 || /* ori $at,$at,n */
1127 (inst & 0xffff0000) == 0x35080000 || /* ori $t0,$t0,n */
1128 (inst & 0xffff0000) == 0x34010000 || /* ori $at,$zero,n */
1129 (inst & 0xffff0000) == 0x34080000) /* ori $t0,$zero,n */
1130 {
1131 load_immediate_bytes += 4;
1132 continue;
1133 }
1134 else
1135 break;
1136 }
0b0d6c3f 1137 else
ac57e5ad 1138 break;
d747e0af 1139 }
e157305c 1140
ac57e5ad
SS
1141 /* In a frameless function, we might have incorrectly
1142 skipped some load immediate instructions. Undo the skipping
1143 if the load immediate was not followed by a stack adjustment. */
1144 if (load_immediate_bytes && !seen_sp_adjust)
1145 offset -= load_immediate_bytes;
1146 return pc + offset;
bd5635a1 1147}
c2a0f1cb 1148
002a422b
JK
1149#if 0
1150/* The lenient prologue stuff should be superceded by the code in
1151 init_extra_frame_info which looks to see whether the stores mentioned
1152 in the proc_desc have actually taken place. */
1153
ee5fb959
JK
1154/* Is address PC in the prologue (loosely defined) for function at
1155 STARTADDR? */
1156
1157static int
1158mips_in_lenient_prologue (startaddr, pc)
1159 CORE_ADDR startaddr;
1160 CORE_ADDR pc;
1161{
1162 CORE_ADDR end_prologue = mips_skip_prologue (startaddr, 1);
1163 return pc >= startaddr && pc < end_prologue;
1164}
002a422b 1165#endif
ee5fb959 1166
ac8cf67d
PS
1167/* Given a return value in `regbuf' with a type `valtype',
1168 extract and copy its value into `valbuf'. */
1169void
1170mips_extract_return_value (valtype, regbuf, valbuf)
1171 struct type *valtype;
1172 char regbuf[REGISTER_BYTES];
1173 char *valbuf;
1174{
1175 int regnum;
1176
1177 regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT && mips_fpu ? FP0_REGNUM : 2;
1178
1179 memcpy (valbuf, regbuf + REGISTER_BYTE (regnum), TYPE_LENGTH (valtype));
1180#ifdef REGISTER_CONVERT_TO_TYPE
1181 REGISTER_CONVERT_TO_TYPE(regnum, valtype, valbuf);
1182#endif
1183}
1184
1185/* Given a return value in `regbuf' with a type `valtype',
1186 write it's value into the appropriate register. */
1187void
1188mips_store_return_value (valtype, valbuf)
1189 struct type *valtype;
1190 char *valbuf;
1191{
1192 int regnum;
1193 char raw_buffer[MAX_REGISTER_RAW_SIZE];
1194
1195 regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT && mips_fpu ? FP0_REGNUM : 2;
1196 memcpy(raw_buffer, valbuf, TYPE_LENGTH (valtype));
1197
1198#ifdef REGISTER_CONVERT_FROM_TYPE
1199 REGISTER_CONVERT_FROM_TYPE(regnum, valtype, raw_buffer);
1200#endif
1201
1202 write_register_bytes(REGISTER_BYTE (regnum), raw_buffer, TYPE_LENGTH (valtype));
1203}
1204
e03c0cc6
ILT
1205/* These exist in mdebugread.c. */
1206extern CORE_ADDR sigtramp_address, sigtramp_end;
1207extern void fixup_sigtramp PARAMS ((void));
1208
1209/* Exported procedure: Is PC in the signal trampoline code */
1210
1211int
1212in_sigtramp (pc, ignore)
1213 CORE_ADDR pc;
1214 char *ignore; /* function name */
1215{
1216 if (sigtramp_address == 0)
1217 fixup_sigtramp ();
1218 return (pc >= sigtramp_address && pc < sigtramp_end);
1219}
1220
9f9f94aa
SS
1221/* Command to set the processor type. */
1222
1223void
1224mips_set_processor_type_command (args, from_tty)
1225 char *args;
1226 int from_tty;
1227{
1228 int i;
1229
1230 if (tmp_mips_processor_type == NULL || *tmp_mips_processor_type == '\0')
1231 {
1232 printf_unfiltered ("The known MIPS processor types are as follows:\n\n");
1233 for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
1234 printf_unfiltered ("%s\n", mips_processor_type_table[i].name);
1235
1236 /* Restore the value. */
1237 tmp_mips_processor_type = strsave (mips_processor_type);
1238
1239 return;
1240 }
1241
1242 if (!mips_set_processor_type (tmp_mips_processor_type))
1243 {
1244 error ("Unknown processor type `%s'.", tmp_mips_processor_type);
1245 /* Restore its value. */
1246 tmp_mips_processor_type = strsave (mips_processor_type);
1247 }
1248}
1249
1250static void
1251mips_show_processor_type_command (args, from_tty)
1252 char *args;
1253 int from_tty;
1254{
1255}
1256
1257/* Modify the actual processor type. */
1258
1259int
1260mips_set_processor_type (str)
1261 char *str;
1262{
1263 int i, j;
1264
1265 if (str == NULL)
1d9489c1 1266 return 0;
9f9f94aa
SS
1267
1268 for (i = 0; mips_processor_type_table[i].name != NULL; ++i)
1269 {
1270 if (strcasecmp (str, mips_processor_type_table[i].name) == 0)
1271 {
1272 mips_processor_type = str;
1273
1274 for (j = 0; j < NUM_REGS; ++j)
1275 reg_names[j] = mips_processor_type_table[i].regnames[j];
1276
1277 return 1;
1278
1279 /* FIXME tweak fpu flag too */
1280 }
1281 }
1282
1283 return 0;
1284}
1285
1286/* Attempt to identify the particular processor model by reading the
1287 processor id. */
1288
1289char *
1290mips_read_processor_type ()
1291{
1292 int prid;
1293
1294 prid = read_register (PRID_REGNUM);
1295
1296 if (prid & ~0xf == 0x700)
1297 return savestring ("r3041", strlen("r3041"));
1298
1299 return NULL;
1300}
427fec5d
JK
1301
1302/* Just like reinit_frame_cache, but with the right arguments to be
1303 callable as an sfunc. */
9f9f94aa 1304
427fec5d
JK
1305static void
1306reinit_frame_cache_sfunc (args, from_tty, c)
1307 char *args;
1308 int from_tty;
1309 struct cmd_list_element *c;
1310{
1311 reinit_frame_cache ();
1312}
c2a0f1cb 1313
18b46e7c
SS
1314int
1315gdb_print_insn_mips (memaddr, info)
1316 bfd_vma memaddr;
1317 disassemble_info *info;
1318{
1319 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1320 return print_insn_big_mips (memaddr, info);
1321 else
1322 return print_insn_little_mips (memaddr, info);
1323}
1324
c2a0f1cb
ILT
1325void
1326_initialize_mips_tdep ()
1327{
427fec5d
JK
1328 struct cmd_list_element *c;
1329
18b46e7c
SS
1330 tm_print_insn = gdb_print_insn_mips;
1331
427fec5d
JK
1332 /* Let the user turn off floating point and set the fence post for
1333 heuristic_proc_start. */
1334
c2a0f1cb 1335 add_show_from_set
a8172eea 1336 (add_set_cmd ("mipsfpu", class_support, var_boolean,
c2a0f1cb
ILT
1337 (char *) &mips_fpu,
1338 "Set use of floating point coprocessor.\n\
1339Turn off to avoid using floating point instructions when calling functions\n\
1340or dealing with return values.", &setlist),
1341 &showlist);
3127785a 1342
9f9f94aa
SS
1343 c = add_set_cmd ("processor", class_support, var_string_noescape,
1344 (char *) &tmp_mips_processor_type,
1345 "Set the type of MIPS processor in use.\n\
1346Set this to be able to access processor-type-specific registers.\n\
1347",
1348 &setlist);
1349 c->function.cfunc = mips_set_processor_type_command;
1350 c = add_show_from_set (c, &showlist);
1351 c->function.cfunc = mips_show_processor_type_command;
1352
1353 tmp_mips_processor_type = strsave (DEFAULT_MIPS_TYPE);
1354 mips_set_processor_type_command (strsave (DEFAULT_MIPS_TYPE), 0);
1355
bdef72d2
JK
1356 /* We really would like to have both "0" and "unlimited" work, but
1357 command.c doesn't deal with that. So make it a var_zinteger
1358 because the user can always use "999999" or some such for unlimited. */
1359 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
427fec5d
JK
1360 (char *) &heuristic_fence_post,
1361 "\
23d35572
JK
1362Set the distance searched for the start of a function.\n\
1363If you are debugging a stripped executable, GDB needs to search through the\n\
1364program for the start of a function. This command sets the distance of the\n\
1365search. The only need to set it is when debugging a stripped executable.",
427fec5d
JK
1366 &setlist);
1367 /* We need to throw away the frame cache when we set this, since it
1368 might change our ability to get backtraces. */
1369 c->function.sfunc = reinit_frame_cache_sfunc;
1370 add_show_from_set (c, &showlist);
c2a0f1cb 1371}
This page took 0.45445 seconds and 4 git commands to generate.