Commit | Line | Data |
---|---|---|
7d9884b9 | 1 | /* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger. |
5e2e79f8 | 2 | Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | Contributed by Alessandro Forin(af@cs.cmu.edu) at CMU |
4 | and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
361bf6ee | 8 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 9 | it under the terms of the GNU General Public License as published by |
361bf6ee JG |
10 | the Free Software Foundation; either version 2 of the License, or |
11 | (at your option) any later version. | |
bd5635a1 | 12 | |
361bf6ee | 13 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
361bf6ee JG |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 21 | |
bd5635a1 | 22 | #include "defs.h" |
bd5635a1 RP |
23 | #include "frame.h" |
24 | #include "inferior.h" | |
25 | #include "symtab.h" | |
26 | #include "value.h" | |
27 | #include "gdbcmd.h" | |
ef08856f | 28 | #include "language.h" |
bd5635a1 RP |
29 | |
30 | #ifdef USG | |
31 | #include <sys/types.h> | |
32 | #endif | |
33 | ||
34 | #include <sys/param.h> | |
35 | #include <sys/dir.h> | |
36 | #include <signal.h> | |
37 | #include <sys/ioctl.h> | |
38 | ||
407a8389 SG |
39 | #ifdef sgi |
40 | /* Must do it this way only for SGIs, as other mips platforms get their | |
41 | JB_ symbols from machine/pcb.h (included via sys/user.h). */ | |
42 | #include <setjmp.h> | |
43 | #endif | |
44 | ||
bd5635a1 | 45 | #include "gdbcore.h" |
62a469e1 SG |
46 | #include "symfile.h" |
47 | #include "objfiles.h" | |
bd5635a1 RP |
48 | |
49 | #ifndef MIPSMAGIC | |
50 | #ifdef MIPSEL | |
51 | #define MIPSMAGIC MIPSELMAGIC | |
52 | #else | |
53 | #define MIPSMAGIC MIPSEBMAGIC | |
54 | #endif | |
55 | #endif | |
56 | ||
57 | #define VM_MIN_ADDRESS (unsigned)0x400000 | |
58 | ||
59 | #include <sys/user.h> /* After a.out.h */ | |
60 | #include <sys/file.h> | |
61 | #include <sys/stat.h> | |
62 | ||
63 | \f | |
0f552c5f JG |
64 | #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */ |
65 | #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */ | |
66 | #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset) | |
67 | #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg) | |
68 | #define PROC_REG_MASK(proc) ((proc)->pdr.regmask) | |
69 | #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask) | |
70 | #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset) | |
71 | #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset) | |
72 | #define PROC_PC_REG(proc) ((proc)->pdr.pcreg) | |
73 | #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym) | |
bd5635a1 | 74 | #define _PROC_MAGIC_ 0x0F0F0F0F |
0f552c5f JG |
75 | #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_) |
76 | #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_) | |
bd5635a1 RP |
77 | |
78 | struct linked_proc_info | |
79 | { | |
80 | struct mips_extra_func_info info; | |
81 | struct linked_proc_info *next; | |
dac4929a | 82 | } *linked_proc_desc_table = NULL; |
bd5635a1 RP |
83 | |
84 | \f | |
85 | #define READ_FRAME_REG(fi, regno) read_next_frame_reg((fi)->next, regno) | |
86 | ||
0f552c5f | 87 | static int |
bd5635a1 RP |
88 | read_next_frame_reg(fi, regno) |
89 | FRAME fi; | |
90 | int regno; | |
91 | { | |
92 | #define SIGFRAME_BASE sizeof(struct sigcontext) | |
93 | #define SIGFRAME_PC_OFF (-SIGFRAME_BASE+ 2*sizeof(int)) | |
94 | #define SIGFRAME_SP_OFF (-SIGFRAME_BASE+32*sizeof(int)) | |
95 | #define SIGFRAME_RA_OFF (-SIGFRAME_BASE+34*sizeof(int)) | |
96 | for (; fi; fi = fi->next) | |
97 | if (in_sigtramp(fi->pc, 0)) { | |
98 | /* No idea if this code works. --PB. */ | |
99 | int offset; | |
100 | if (regno == PC_REGNUM) offset = SIGFRAME_PC_OFF; | |
101 | else if (regno == RA_REGNUM) offset = SIGFRAME_RA_OFF; | |
102 | else if (regno == SP_REGNUM) offset = SIGFRAME_SP_OFF; | |
103 | else return 0; | |
104 | return read_memory_integer(fi->frame + offset, 4); | |
105 | } | |
106 | else if (regno == SP_REGNUM) return fi->frame; | |
107 | else if (fi->saved_regs->regs[regno]) | |
108 | return read_memory_integer(fi->saved_regs->regs[regno], 4); | |
109 | return read_register(regno); | |
110 | } | |
111 | ||
112 | int | |
113 | mips_frame_saved_pc(frame) | |
114 | FRAME frame; | |
115 | { | |
0f552c5f | 116 | mips_extra_func_info_t proc_desc = frame->proc_desc; |
bd5635a1 | 117 | int pcreg = proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM; |
0f552c5f | 118 | |
bd5635a1 RP |
119 | if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc)) |
120 | return read_memory_integer(frame->frame - 4, 4); | |
0f552c5f | 121 | |
bd5635a1 RP |
122 | return read_next_frame_reg(frame, pcreg); |
123 | } | |
124 | ||
125 | static struct mips_extra_func_info temp_proc_desc; | |
126 | static struct frame_saved_regs temp_saved_regs; | |
127 | ||
0f552c5f JG |
128 | static CORE_ADDR |
129 | heuristic_proc_start(pc) | |
bd5635a1 RP |
130 | CORE_ADDR pc; |
131 | { | |
bd5635a1 | 132 | CORE_ADDR start_pc = pc; |
5e2e79f8 | 133 | CORE_ADDR fence = start_pc - 200; |
0f552c5f JG |
134 | |
135 | if (start_pc == 0) return 0; | |
bd5635a1 | 136 | if (fence < VM_MIN_ADDRESS) fence = VM_MIN_ADDRESS; |
0f552c5f | 137 | |
bd5635a1 RP |
138 | /* search back for previous return */ |
139 | for (start_pc -= 4; ; start_pc -= 4) | |
140 | if (start_pc < fence) return 0; | |
141 | else if (ABOUT_TO_RETURN(start_pc)) | |
142 | break; | |
143 | ||
144 | start_pc += 8; /* skip return, and its delay slot */ | |
145 | #if 0 | |
146 | /* skip nops (usually 1) 0 - is this */ | |
147 | while (start_pc < pc && read_memory_integer (start_pc, 4) == 0) | |
148 | start_pc += 4; | |
149 | #endif | |
150 | return start_pc; | |
151 | } | |
152 | ||
0f552c5f | 153 | static mips_extra_func_info_t |
bd5635a1 RP |
154 | heuristic_proc_desc(start_pc, limit_pc, next_frame) |
155 | CORE_ADDR start_pc, limit_pc; | |
156 | FRAME next_frame; | |
157 | { | |
158 | CORE_ADDR sp = next_frame ? next_frame->frame : read_register (SP_REGNUM); | |
159 | CORE_ADDR cur_pc; | |
160 | int frame_size; | |
161 | int has_frame_reg = 0; | |
162 | int reg30; /* Value of $r30. Used by gcc for frame-pointer */ | |
163 | unsigned long reg_mask = 0; | |
164 | ||
165 | if (start_pc == 0) return NULL; | |
166 | bzero(&temp_proc_desc, sizeof(temp_proc_desc)); | |
167 | bzero(&temp_saved_regs, sizeof(struct frame_saved_regs)); | |
a70dc898 RP |
168 | PROC_LOW_ADDR(&temp_proc_desc) = start_pc; |
169 | ||
bd5635a1 RP |
170 | if (start_pc + 200 < limit_pc) limit_pc = start_pc + 200; |
171 | restart: | |
172 | frame_size = 0; | |
173 | for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4) { | |
174 | unsigned long word; | |
175 | int status; | |
176 | ||
a70dc898 | 177 | status = read_memory_nobpt (cur_pc, (char *)&word, 4); |
bd5635a1 | 178 | if (status) memory_error (status, cur_pc); |
5e2e79f8 | 179 | SWAP_TARGET_AND_HOST (&word, sizeof (word)); |
bd5635a1 RP |
180 | if ((word & 0xFFFF0000) == 0x27bd0000) /* addiu $sp,$sp,-i */ |
181 | frame_size += (-word) & 0xFFFF; | |
182 | else if ((word & 0xFFFF0000) == 0x23bd0000) /* addu $sp,$sp,-i */ | |
183 | frame_size += (-word) & 0xFFFF; | |
184 | else if ((word & 0xFFE00000) == 0xafa00000) { /* sw reg,offset($sp) */ | |
185 | int reg = (word & 0x001F0000) >> 16; | |
186 | reg_mask |= 1 << reg; | |
187 | temp_saved_regs.regs[reg] = sp + (short)word; | |
188 | } | |
189 | else if ((word & 0xFFFF0000) == 0x27be0000) { /* addiu $30,$sp,size */ | |
190 | if ((unsigned short)word != frame_size) | |
191 | reg30 = sp + (unsigned short)word; | |
192 | else if (!has_frame_reg) { | |
193 | int alloca_adjust; | |
194 | has_frame_reg = 1; | |
195 | reg30 = read_next_frame_reg(next_frame, 30); | |
196 | alloca_adjust = reg30 - (sp + (unsigned short)word); | |
197 | if (alloca_adjust > 0) { | |
198 | /* FP > SP + frame_size. This may be because | |
199 | /* of an alloca or somethings similar. | |
200 | * Fix sp to "pre-alloca" value, and try again. | |
201 | */ | |
202 | sp += alloca_adjust; | |
203 | goto restart; | |
204 | } | |
205 | } | |
206 | } | |
207 | else if ((word & 0xFFE00000) == 0xafc00000) { /* sw reg,offset($30) */ | |
208 | int reg = (word & 0x001F0000) >> 16; | |
209 | reg_mask |= 1 << reg; | |
210 | temp_saved_regs.regs[reg] = reg30 + (short)word; | |
211 | } | |
212 | } | |
213 | if (has_frame_reg) { | |
214 | PROC_FRAME_REG(&temp_proc_desc) = 30; | |
215 | PROC_FRAME_OFFSET(&temp_proc_desc) = 0; | |
216 | } | |
217 | else { | |
218 | PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM; | |
219 | PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size; | |
220 | } | |
221 | PROC_REG_MASK(&temp_proc_desc) = reg_mask; | |
222 | PROC_PC_REG(&temp_proc_desc) = RA_REGNUM; | |
223 | return &temp_proc_desc; | |
224 | } | |
225 | ||
0f552c5f | 226 | static mips_extra_func_info_t |
bd5635a1 RP |
227 | find_proc_desc(pc, next_frame) |
228 | CORE_ADDR pc; | |
229 | FRAME next_frame; | |
230 | { | |
231 | mips_extra_func_info_t proc_desc; | |
0f552c5f | 232 | struct block *b = block_for_pc(pc); |
bd5635a1 | 233 | struct symbol *sym = |
dac4929a | 234 | b ? lookup_symbol(MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE, 0, NULL) : NULL; |
0f552c5f JG |
235 | |
236 | if (sym) | |
bd5635a1 RP |
237 | { |
238 | /* IF this is the topmost frame AND | |
239 | * (this proc does not have debugging information OR | |
240 | * the PC is in the procedure prologue) | |
be772100 | 241 | * THEN create a "heuristic" proc_desc (by analyzing |
bd5635a1 RP |
242 | * the actual code) to replace the "official" proc_desc. |
243 | */ | |
0f552c5f | 244 | proc_desc = (mips_extra_func_info_t)SYMBOL_VALUE(sym); |
bd5635a1 RP |
245 | if (next_frame == NULL) { |
246 | struct symtab_and_line val; | |
247 | struct symbol *proc_symbol = | |
248 | PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc); | |
0f552c5f | 249 | |
bd5635a1 RP |
250 | if (proc_symbol) { |
251 | val = find_pc_line (BLOCK_START | |
252 | (SYMBOL_BLOCK_VALUE(proc_symbol)), | |
253 | 0); | |
254 | val.pc = val.end ? val.end : pc; | |
255 | } | |
256 | if (!proc_symbol || pc < val.pc) { | |
257 | mips_extra_func_info_t found_heuristic = | |
258 | heuristic_proc_desc(PROC_LOW_ADDR(proc_desc), | |
259 | pc, next_frame); | |
260 | if (found_heuristic) proc_desc = found_heuristic; | |
261 | } | |
262 | } | |
263 | } | |
264 | else | |
265 | { | |
0f552c5f JG |
266 | /* Is linked_proc_desc_table really necessary? It only seems to be used |
267 | by procedure call dummys. However, the procedures being called ought | |
268 | to have their own proc_descs, and even if they don't, | |
269 | heuristic_proc_desc knows how to create them! */ | |
270 | ||
bd5635a1 RP |
271 | register struct linked_proc_info *link; |
272 | for (link = linked_proc_desc_table; link; link = link->next) | |
273 | if (PROC_LOW_ADDR(&link->info) <= pc | |
274 | && PROC_HIGH_ADDR(&link->info) > pc) | |
275 | return &link->info; | |
276 | proc_desc = | |
277 | heuristic_proc_desc(heuristic_proc_start(pc), pc, next_frame); | |
278 | } | |
279 | return proc_desc; | |
280 | } | |
281 | ||
282 | mips_extra_func_info_t cached_proc_desc; | |
283 | ||
0f552c5f JG |
284 | FRAME_ADDR |
285 | mips_frame_chain(frame) | |
bd5635a1 RP |
286 | FRAME frame; |
287 | { | |
bd5635a1 RP |
288 | mips_extra_func_info_t proc_desc; |
289 | CORE_ADDR saved_pc = FRAME_SAVED_PC(frame); | |
be772100 | 290 | |
0f552c5f JG |
291 | if (saved_pc == 0 || inside_entry_file (saved_pc)) |
292 | return 0; | |
293 | ||
bd5635a1 | 294 | proc_desc = find_proc_desc(saved_pc, frame); |
0f552c5f JG |
295 | if (!proc_desc) |
296 | return 0; | |
297 | ||
bd5635a1 RP |
298 | cached_proc_desc = proc_desc; |
299 | return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc)) | |
0f552c5f | 300 | + PROC_FRAME_OFFSET(proc_desc); |
bd5635a1 RP |
301 | } |
302 | ||
303 | void | |
304 | init_extra_frame_info(fci) | |
305 | struct frame_info *fci; | |
306 | { | |
307 | extern struct obstack frame_cache_obstack; | |
308 | /* Use proc_desc calculated in frame_chain */ | |
309 | mips_extra_func_info_t proc_desc = fci->next ? cached_proc_desc : | |
310 | find_proc_desc(fci->pc, fci->next); | |
0f552c5f | 311 | |
bd5635a1 RP |
312 | fci->saved_regs = (struct frame_saved_regs*) |
313 | obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs)); | |
314 | bzero(fci->saved_regs, sizeof(struct frame_saved_regs)); | |
315 | fci->proc_desc = | |
0f552c5f | 316 | proc_desc == &temp_proc_desc ? 0 : proc_desc; |
bd5635a1 RP |
317 | if (proc_desc) |
318 | { | |
319 | int ireg; | |
320 | CORE_ADDR reg_position; | |
321 | unsigned long mask; | |
322 | /* r0 bit means kernel trap */ | |
323 | int kernel_trap = PROC_REG_MASK(proc_desc) & 1; | |
324 | ||
a70dc898 RP |
325 | if (fci->frame == 0) |
326 | { | |
327 | /* Fixup frame-pointer - only needed for top frame */ | |
328 | /* This may not be quite right, if proc has a real frame register */ | |
329 | if (fci->pc == PROC_LOW_ADDR(proc_desc)) | |
330 | fci->frame = read_register (SP_REGNUM); | |
331 | else | |
bd5635a1 | 332 | fci->frame = READ_FRAME_REG(fci, PROC_FRAME_REG(proc_desc)) |
dac4929a | 333 | + PROC_FRAME_OFFSET(proc_desc); |
a70dc898 | 334 | } |
bd5635a1 RP |
335 | |
336 | if (proc_desc == &temp_proc_desc) | |
337 | *fci->saved_regs = temp_saved_regs; | |
338 | else | |
339 | { | |
340 | /* find which general-purpose registers were saved */ | |
341 | reg_position = fci->frame + PROC_REG_OFFSET(proc_desc); | |
342 | mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc); | |
343 | for (ireg= 31; mask; --ireg, mask <<= 1) | |
344 | if (mask & 0x80000000) | |
345 | { | |
346 | fci->saved_regs->regs[ireg] = reg_position; | |
347 | reg_position -= 4; | |
348 | } | |
349 | /* find which floating-point registers were saved */ | |
350 | reg_position = fci->frame + PROC_FREG_OFFSET(proc_desc); | |
351 | /* The freg_offset points to where the first *double* register is saved. | |
352 | * So skip to the high-order word. */ | |
353 | reg_position += 4; | |
354 | mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc); | |
355 | for (ireg = 31; mask; --ireg, mask <<= 1) | |
356 | if (mask & 0x80000000) | |
357 | { | |
d747e0af | 358 | fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position; |
bd5635a1 RP |
359 | reg_position -= 4; |
360 | } | |
361 | } | |
362 | ||
363 | /* hack: if argument regs are saved, guess these contain args */ | |
364 | if ((PROC_REG_MASK(proc_desc) & 0xF0) == 0) fci->num_args = -1; | |
365 | else if ((PROC_REG_MASK(proc_desc) & 0x80) == 0) fci->num_args = 4; | |
366 | else if ((PROC_REG_MASK(proc_desc) & 0x40) == 0) fci->num_args = 3; | |
367 | else if ((PROC_REG_MASK(proc_desc) & 0x20) == 0) fci->num_args = 2; | |
368 | else if ((PROC_REG_MASK(proc_desc) & 0x10) == 0) fci->num_args = 1; | |
369 | ||
370 | fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM]; | |
371 | } | |
bd5635a1 RP |
372 | } |
373 | ||
a70dc898 RP |
374 | /* MIPS stack frames are almost impenetrable. When execution stops, |
375 | we basically have to look at symbol information for the function | |
376 | that we stopped in, which tells us *which* register (if any) is | |
377 | the base of the frame pointer, and what offset from that register | |
378 | the frame itself is at. | |
379 | ||
380 | This presents a problem when trying to examine a stack in memory | |
381 | (that isn't executing at the moment), using the "frame" command. We | |
382 | don't have a PC, nor do we have any registers except SP. | |
383 | ||
384 | This routine takes two arguments, SP and PC, and tries to make the | |
385 | cached frames look as if these two arguments defined a frame on the | |
386 | cache. This allows the rest of info frame to extract the important | |
387 | arguments without difficulty. */ | |
388 | ||
389 | FRAME | |
390 | setup_arbitrary_frame (stack, pc) | |
391 | FRAME_ADDR stack; | |
392 | CORE_ADDR pc; | |
393 | { | |
394 | return create_new_frame (stack, pc); | |
395 | } | |
396 | ||
bd5635a1 | 397 | |
0f552c5f JG |
398 | CORE_ADDR |
399 | mips_push_arguments(nargs, args, sp, struct_return, struct_addr) | |
bd5635a1 RP |
400 | int nargs; |
401 | value *args; | |
402 | CORE_ADDR sp; | |
403 | int struct_return; | |
404 | CORE_ADDR struct_addr; | |
405 | { | |
406 | CORE_ADDR buf; | |
407 | register i; | |
408 | int accumulate_size = struct_return ? 4 : 0; | |
409 | struct mips_arg { char *contents; int len; int offset; }; | |
410 | struct mips_arg *mips_args = | |
411 | (struct mips_arg*)alloca(nargs * sizeof(struct mips_arg)); | |
412 | register struct mips_arg *m_arg; | |
413 | for (i = 0, m_arg = mips_args; i < nargs; i++, m_arg++) { | |
414 | extern value value_arg_coerce(); | |
415 | value arg = value_arg_coerce (args[i]); | |
416 | m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg)); | |
417 | /* This entire mips-specific routine is because doubles must be aligned | |
418 | * on 8-byte boundaries. It still isn't quite right, because MIPS decided | |
419 | * to align 'struct {int a, b}' on 4-byte boundaries (even though this | |
420 | * breaks their varargs implementation...). A correct solution | |
421 | * requires an simulation of gcc's 'alignof' (and use of 'alignof' | |
422 | * in stdarg.h/varargs.h). | |
423 | */ | |
424 | if (m_arg->len > 4) accumulate_size = (accumulate_size + 7) & -8; | |
425 | m_arg->offset = accumulate_size; | |
426 | accumulate_size = (accumulate_size + m_arg->len + 3) & -4; | |
427 | m_arg->contents = VALUE_CONTENTS(arg); | |
428 | } | |
429 | accumulate_size = (accumulate_size + 7) & (-8); | |
430 | if (accumulate_size < 16) accumulate_size = 16; | |
431 | sp -= accumulate_size; | |
432 | for (i = nargs; m_arg--, --i >= 0; ) | |
433 | write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len); | |
434 | if (struct_return) { | |
435 | buf = struct_addr; | |
a70dc898 RP |
436 | write_memory(sp, (char *)&buf, sizeof(CORE_ADDR)); |
437 | } | |
bd5635a1 RP |
438 | return sp; |
439 | } | |
440 | ||
441 | /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */ | |
442 | #define MASK(i,j) ((1 << (j)+1)-1 ^ (1 << (i))-1) | |
443 | ||
444 | void | |
445 | mips_push_dummy_frame() | |
446 | { | |
447 | int ireg; | |
448 | struct linked_proc_info *link = (struct linked_proc_info*) | |
449 | xmalloc(sizeof(struct linked_proc_info)); | |
450 | mips_extra_func_info_t proc_desc = &link->info; | |
451 | CORE_ADDR sp = read_register (SP_REGNUM); | |
452 | CORE_ADDR save_address; | |
453 | REGISTER_TYPE buffer; | |
454 | link->next = linked_proc_desc_table; | |
455 | linked_proc_desc_table = link; | |
456 | #define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */ | |
457 | #define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<31) | |
458 | #define GEN_REG_SAVE_COUNT 22 | |
459 | #define FLOAT_REG_SAVE_MASK MASK(0,19) | |
460 | #define FLOAT_REG_SAVE_COUNT 20 | |
461 | #define SPECIAL_REG_SAVE_COUNT 4 | |
462 | /* | |
463 | * The registers we must save are all those not preserved across | |
464 | * procedure calls. Dest_Reg (see tm-mips.h) must also be saved. | |
465 | * In addition, we must save the PC, and PUSH_FP_REGNUM. | |
466 | * (Ideally, we should also save MDLO/-HI and FP Control/Status reg.) | |
467 | * | |
468 | * Dummy frame layout: | |
469 | * (high memory) | |
470 | * Saved PC | |
471 | * Saved MMHI, MMLO, FPC_CSR | |
472 | * Saved R31 | |
473 | * Saved R28 | |
474 | * ... | |
475 | * Saved R1 | |
476 | * Saved D18 (i.e. F19, F18) | |
477 | * ... | |
478 | * Saved D0 (i.e. F1, F0) | |
479 | * CALL_DUMMY (subroutine stub; see m-mips.h) | |
480 | * Parameter build area (not yet implemented) | |
481 | * (low memory) | |
482 | */ | |
483 | PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK; | |
484 | PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK; | |
485 | PROC_REG_OFFSET(proc_desc) = /* offset of (Saved R31) from FP */ | |
486 | -sizeof(long) - 4 * SPECIAL_REG_SAVE_COUNT; | |
487 | PROC_FREG_OFFSET(proc_desc) = /* offset of (Saved D18) from FP */ | |
488 | -sizeof(double) - 4 * (SPECIAL_REG_SAVE_COUNT + GEN_REG_SAVE_COUNT); | |
489 | /* save general registers */ | |
490 | save_address = sp + PROC_REG_OFFSET(proc_desc); | |
491 | for (ireg = 32; --ireg >= 0; ) | |
492 | if (PROC_REG_MASK(proc_desc) & (1 << ireg)) | |
493 | { | |
494 | buffer = read_register (ireg); | |
a70dc898 | 495 | write_memory (save_address, (char *)&buffer, sizeof(REGISTER_TYPE)); |
bd5635a1 RP |
496 | save_address -= 4; |
497 | } | |
498 | /* save floating-points registers */ | |
499 | save_address = sp + PROC_FREG_OFFSET(proc_desc); | |
500 | for (ireg = 32; --ireg >= 0; ) | |
501 | if (PROC_FREG_MASK(proc_desc) & (1 << ireg)) | |
502 | { | |
7d9884b9 | 503 | buffer = read_register (ireg + FP0_REGNUM); |
a70dc898 | 504 | write_memory (save_address, (char *)&buffer, 4); |
bd5635a1 RP |
505 | save_address -= 4; |
506 | } | |
507 | write_register (PUSH_FP_REGNUM, sp); | |
508 | PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM; | |
509 | PROC_FRAME_OFFSET(proc_desc) = 0; | |
510 | buffer = read_register (PC_REGNUM); | |
a70dc898 | 511 | write_memory (sp - 4, (char *)&buffer, sizeof(REGISTER_TYPE)); |
bd5635a1 | 512 | buffer = read_register (HI_REGNUM); |
a70dc898 | 513 | write_memory (sp - 8, (char *)&buffer, sizeof(REGISTER_TYPE)); |
bd5635a1 | 514 | buffer = read_register (LO_REGNUM); |
a70dc898 | 515 | write_memory (sp - 12, (char *)&buffer, sizeof(REGISTER_TYPE)); |
bd5635a1 | 516 | buffer = read_register (FCRCS_REGNUM); |
a70dc898 | 517 | write_memory (sp - 16, (char *)&buffer, sizeof(REGISTER_TYPE)); |
bd5635a1 RP |
518 | sp -= 4 * (GEN_REG_SAVE_COUNT+FLOAT_REG_SAVE_COUNT+SPECIAL_REG_SAVE_COUNT); |
519 | write_register (SP_REGNUM, sp); | |
520 | PROC_LOW_ADDR(proc_desc) = sp - CALL_DUMMY_SIZE + CALL_DUMMY_START_OFFSET; | |
521 | PROC_HIGH_ADDR(proc_desc) = sp; | |
522 | SET_PROC_DESC_IS_DUMMY(proc_desc); | |
523 | PROC_PC_REG(proc_desc) = RA_REGNUM; | |
524 | } | |
525 | ||
526 | void | |
527 | mips_pop_frame() | |
dac4929a SG |
528 | { |
529 | register int regnum; | |
bd5635a1 RP |
530 | FRAME frame = get_current_frame (); |
531 | CORE_ADDR new_sp = frame->frame; | |
dac4929a | 532 | |
a70dc898 | 533 | mips_extra_func_info_t proc_desc = frame->proc_desc; |
dac4929a SG |
534 | |
535 | write_register (PC_REGNUM, FRAME_SAVED_PC(frame)); | |
536 | if (proc_desc) | |
537 | { | |
538 | for (regnum = 32; --regnum >= 0; ) | |
539 | if (PROC_REG_MASK(proc_desc) & (1 << regnum)) | |
540 | write_register (regnum, | |
541 | read_memory_integer (frame->saved_regs->regs[regnum], | |
542 | 4)); | |
543 | for (regnum = 32; --regnum >= 0; ) | |
544 | if (PROC_FREG_MASK(proc_desc) & (1 << regnum)) | |
545 | write_register (regnum + FP0_REGNUM, | |
546 | read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 4)); | |
547 | } | |
548 | write_register (SP_REGNUM, new_sp); | |
549 | flush_cached_frames (); | |
550 | /* We let mips_init_extra_frame_info figure out the frame pointer */ | |
551 | set_current_frame (create_new_frame (0, read_pc ())); | |
552 | ||
bd5635a1 RP |
553 | if (PROC_DESC_IS_DUMMY(proc_desc)) |
554 | { | |
dac4929a SG |
555 | struct linked_proc_info *pi_ptr, *prev_ptr; |
556 | ||
557 | for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL; | |
558 | pi_ptr != NULL; | |
559 | prev_ptr = pi_ptr, pi_ptr = pi_ptr->next) | |
560 | { | |
561 | if (&pi_ptr->info == proc_desc) | |
562 | break; | |
563 | } | |
564 | ||
565 | if (pi_ptr == NULL) | |
566 | error ("Can't locate dummy extra frame info\n"); | |
567 | ||
568 | if (prev_ptr != NULL) | |
569 | prev_ptr->next = pi_ptr->next; | |
570 | else | |
571 | linked_proc_desc_table = pi_ptr->next; | |
572 | ||
573 | free (pi_ptr); | |
574 | ||
bd5635a1 RP |
575 | write_register (HI_REGNUM, read_memory_integer(new_sp - 8, 4)); |
576 | write_register (LO_REGNUM, read_memory_integer(new_sp - 12, 4)); | |
577 | write_register (FCRCS_REGNUM, read_memory_integer(new_sp - 16, 4)); | |
578 | } | |
bd5635a1 RP |
579 | } |
580 | ||
0f552c5f | 581 | static void |
a70dc898 | 582 | mips_print_register (regnum, all) |
bd5635a1 RP |
583 | int regnum, all; |
584 | { | |
a70dc898 | 585 | unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE * 2]; /* *2 for doubles */ |
bd5635a1 RP |
586 | REGISTER_TYPE val; |
587 | ||
5e2e79f8 FF |
588 | /* Get the data in raw format. */ |
589 | if (read_relative_register_raw_bytes (regnum, raw_buffer)) | |
590 | { | |
591 | printf_filtered ("%s: [Invalid]", reg_names[regnum]); | |
592 | return; | |
593 | } | |
594 | ||
d747e0af MT |
595 | /* If an even floating pointer register, also print as double. */ |
596 | if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM+32 | |
597 | && !((regnum-FP0_REGNUM) & 1)) { | |
bd5635a1 | 598 | read_relative_register_raw_bytes (regnum+1, raw_buffer+4); |
d747e0af | 599 | printf_filtered ("(d%d: ", regnum-FP0_REGNUM); |
bd5635a1 RP |
600 | val_print (builtin_type_double, raw_buffer, 0, |
601 | stdout, 0, 1, 0, Val_pretty_default); | |
d747e0af | 602 | printf_filtered ("); "); |
bd5635a1 RP |
603 | } |
604 | fputs_filtered (reg_names[regnum], stdout); | |
605 | #ifndef NUMERIC_REG_NAMES | |
606 | if (regnum < 32) | |
607 | printf_filtered ("(r%d): ", regnum); | |
608 | else | |
609 | #endif | |
610 | printf_filtered (": "); | |
611 | ||
612 | /* If virtual format is floating, print it that way. */ | |
613 | if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT | |
614 | && ! INVALID_FLOAT (raw_buffer, REGISTER_VIRTUAL_SIZE(regnum))) { | |
615 | val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0, | |
616 | stdout, 0, 1, 0, Val_pretty_default); | |
617 | } | |
618 | /* Else print as integer in hex. */ | |
619 | else | |
620 | { | |
621 | long val; | |
622 | ||
623 | bcopy (raw_buffer, &val, sizeof (long)); | |
5e2e79f8 | 624 | SWAP_TARGET_AND_HOST ((char *)&val, sizeof (long)); |
bd5635a1 RP |
625 | if (val == 0) |
626 | printf_filtered ("0"); | |
627 | else if (all) | |
d8b3b00e | 628 | printf_filtered (local_hex_format(), val); |
bd5635a1 | 629 | else |
d8b3b00e | 630 | printf_filtered ("%s=%d", local_hex_string(val), val); |
bd5635a1 RP |
631 | } |
632 | } | |
633 | ||
d8b3b00e | 634 | /* Replacement for generic do_registers_info. */ |
0f552c5f | 635 | void |
361bf6ee | 636 | mips_do_registers_info (regnum, fpregs) |
bd5635a1 | 637 | int regnum; |
361bf6ee | 638 | int fpregs; |
bd5635a1 RP |
639 | { |
640 | if (regnum != -1) { | |
641 | mips_print_register (regnum, 0); | |
642 | printf_filtered ("\n"); | |
643 | } | |
644 | else { | |
645 | for (regnum = 0; regnum < NUM_REGS; ) { | |
d8b3b00e JG |
646 | if ((!fpregs) && regnum >= FP0_REGNUM && regnum <= FCRIR_REGNUM) { |
647 | regnum++; | |
648 | continue; | |
649 | } | |
bd5635a1 RP |
650 | mips_print_register (regnum, 1); |
651 | regnum++; | |
652 | if ((regnum & 3) == 0 || regnum == NUM_REGS) | |
653 | printf_filtered (";\n"); | |
654 | else | |
655 | printf_filtered ("; "); | |
656 | } | |
657 | } | |
658 | } | |
659 | /* Return number of args passed to a frame. described by FIP. | |
660 | Can return -1, meaning no way to tell. */ | |
661 | ||
0f552c5f | 662 | int |
bd5635a1 RP |
663 | mips_frame_num_args(fip) |
664 | FRAME fip; | |
665 | { | |
666 | #if 0 | |
667 | struct chain_info_t *p; | |
668 | ||
669 | p = mips_find_cached_frame(FRAME_FP(fip)); | |
670 | if (p->valid) | |
671 | return p->the_info.numargs; | |
672 | #endif | |
673 | return -1; | |
674 | } | |
675 | ||
676 | \f | |
677 | /* Bad floats: Returns 0 if P points to a valid IEEE floating point number, | |
678 | 1 if P points to a denormalized number or a NaN. LEN says whether this is | |
679 | a single-precision or double-precision float */ | |
680 | #define SINGLE_EXP_BITS 8 | |
681 | #define DOUBLE_EXP_BITS 11 | |
682 | int | |
683 | isa_NAN(p, len) | |
684 | int *p, len; | |
685 | { | |
686 | int exponent; | |
687 | if (len == 4) | |
688 | { | |
689 | exponent = *p; | |
690 | exponent = exponent << 1 >> (32 - SINGLE_EXP_BITS - 1); | |
691 | return ((exponent == -1) || (! exponent && *p)); | |
692 | } | |
693 | else if (len == 8) | |
694 | { | |
695 | exponent = *(p+1); | |
696 | exponent = exponent << 1 >> (32 - DOUBLE_EXP_BITS - 1); | |
697 | return ((exponent == -1) || (! exponent && *p * *(p+1))); | |
698 | } | |
699 | else return 1; | |
700 | } | |
407a8389 | 701 | \f |
bd5635a1 RP |
702 | /* To skip prologues, I use this predicate. Returns either PC |
703 | itself if the code at PC does not look like a function prologue, | |
704 | PC+4 if it does (our caller does not need anything more fancy). */ | |
705 | ||
be772100 JG |
706 | CORE_ADDR |
707 | mips_skip_prologue(pc) | |
bd5635a1 RP |
708 | CORE_ADDR pc; |
709 | { | |
710 | struct symbol *f; | |
711 | struct block *b; | |
712 | unsigned long inst; | |
d747e0af | 713 | int offset; |
bd5635a1 RP |
714 | |
715 | /* For -g modules and most functions anyways the | |
d747e0af MT |
716 | first instruction adjusts the stack. |
717 | But we allow some number of stores before the stack adjustment. | |
718 | (These are emitted by varags functions compiled by gcc-2.0. */ | |
719 | for (offset = 0; offset < 100; offset += 4) { | |
720 | inst = read_memory_integer(pc + offset, 4); | |
721 | if ((inst & 0xffff0000) == 0x27bd0000) /* addiu $sp,$sp,offset */ | |
722 | return pc + offset + 4; | |
723 | if ((inst & 0xFFE00000) != 0xAFA00000) /* sw reg,n($sp) */ | |
724 | break; | |
725 | } | |
bd5635a1 RP |
726 | |
727 | /* Well, it looks like a frameless. Let's make sure. | |
728 | Note that we are not called on the current PC, | |
729 | but on the function`s start PC, and I have definitely | |
730 | seen optimized code that adjusts the SP quite later */ | |
731 | b = block_for_pc(pc); | |
732 | if (!b) return pc; | |
733 | ||
dac4929a | 734 | f = lookup_symbol(MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE, 0, NULL); |
bd5635a1 RP |
735 | if (!f) return pc; |
736 | /* Ideally, I would like to use the adjusted info | |
737 | from mips_frame_info(), but for all practical | |
738 | purposes it will not matter (and it would require | |
739 | a different definition of SKIP_PROLOGUE()) | |
740 | ||
741 | Actually, it would not hurt to skip the storing | |
742 | of arguments on the stack as well. */ | |
0f552c5f | 743 | if (((mips_extra_func_info_t)SYMBOL_VALUE(f))->pdr.frameoffset) |
bd5635a1 RP |
744 | return pc + 4; |
745 | ||
746 | return pc; | |
747 | } | |
d747e0af MT |
748 | |
749 | /* Figure out where the longjmp will land. | |
750 | We expect the first arg to be a pointer to the jmp_buf structure from which | |
751 | we extract the pc (JB_PC) that we will land at. The pc is copied into PC. | |
752 | This routine returns true on success. */ | |
753 | ||
754 | int | |
755 | get_longjmp_target(pc) | |
756 | CORE_ADDR *pc; | |
757 | { | |
758 | CORE_ADDR jb_addr; | |
759 | ||
760 | jb_addr = read_register(A0_REGNUM); | |
761 | ||
762 | if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, pc, | |
763 | sizeof(CORE_ADDR))) | |
764 | return 0; | |
765 | ||
766 | SWAP_TARGET_AND_HOST(pc, sizeof(CORE_ADDR)); | |
767 | ||
768 | return 1; | |
769 | } |