Commit | Line | Data |
---|---|---|
aac71672 MS |
1 | /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger. |
2 | Prologue analysis module, extracted from mn10300-tdep.c, Oct. 1, 2004. | |
3 | ||
4 | Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free | |
5 | Software Foundation, Inc. | |
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, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | #include "defs.h" | |
25 | #include "symtab.h" | |
26 | #include "inferior.h" | |
27 | #include "gdbcore.h" | |
28 | #include "gdb_string.h" | |
29 | #include "trad-frame.h" | |
30 | #include "mn10300-tdep.h" | |
31 | ||
32 | enum movm_register_bits { | |
33 | movm_exother_bit = 0x01, | |
34 | movm_exreg1_bit = 0x02, | |
35 | movm_exreg0_bit = 0x04, | |
36 | movm_other_bit = 0x08, | |
37 | movm_a3_bit = 0x10, | |
38 | movm_a2_bit = 0x20, | |
39 | movm_d3_bit = 0x40, | |
40 | movm_d2_bit = 0x80 | |
41 | }; | |
42 | ||
43 | /* Values for frame_info.status */ | |
44 | ||
45 | enum frame_kind { | |
46 | MY_FRAME_IN_SP = 0x1, | |
47 | MY_FRAME_IN_FP = 0x2, | |
48 | NO_MORE_FRAMES = 0x4 | |
49 | }; | |
50 | ||
51 | /* | |
52 | * Frame Extra Info: | |
53 | * | |
54 | * status -- actually frame type (SP, FP, or last frame) | |
55 | * stack size -- offset to the next frame | |
56 | * | |
57 | * The former might ultimately be stored in the frame_base. | |
58 | * Seems like there'd be a way to store the later too. | |
59 | * | |
60 | * Temporarily supply empty stub functions as place holders. | |
61 | */ | |
62 | ||
63 | static void | |
64 | my_frame_is_in_sp (struct frame_info *fi, void **this_cache) | |
65 | { | |
66 | struct trad_frame_cache *cache = mn10300_frame_unwind_cache (fi, this_cache); | |
67 | trad_frame_set_this_base (cache, | |
68 | frame_unwind_register_unsigned (fi, | |
69 | E_SP_REGNUM)); | |
70 | } | |
71 | ||
72 | static void | |
73 | my_frame_is_in_fp (struct frame_info *fi, void **this_cache) | |
74 | { | |
75 | struct trad_frame_cache *cache = mn10300_frame_unwind_cache (fi, this_cache); | |
76 | trad_frame_set_this_base (cache, | |
77 | frame_unwind_register_unsigned (fi, | |
78 | E_A3_REGNUM)); | |
79 | } | |
80 | ||
81 | static void | |
82 | my_frame_is_last (struct frame_info *fi) | |
83 | { | |
84 | } | |
85 | ||
86 | static int | |
87 | is_my_frame_in_sp (struct frame_info *fi) | |
88 | { | |
89 | return 0; | |
90 | } | |
91 | ||
92 | static int | |
93 | is_my_frame_in_fp (struct frame_info *fi) | |
94 | { | |
95 | return 0; | |
96 | } | |
97 | ||
98 | static int | |
99 | is_my_frame_last (struct frame_info *fi) | |
100 | { | |
101 | return 0; | |
102 | } | |
103 | ||
104 | static void | |
105 | set_my_stack_size (struct frame_info *fi, CORE_ADDR size) | |
106 | { | |
107 | } | |
108 | ||
109 | ||
aac71672 MS |
110 | /* Set offsets of registers saved by movm instruction. |
111 | This is a helper function for mn10300_analyze_prologue. */ | |
112 | ||
113 | static void | |
114 | set_movm_offsets (struct frame_info *fi, | |
115 | void **this_cache, | |
116 | int movm_args) | |
117 | { | |
118 | struct trad_frame_cache *cache; | |
119 | int offset = 0; | |
120 | CORE_ADDR base; | |
121 | ||
9b3c083c | 122 | if (fi == NULL || this_cache == NULL) |
aac71672 MS |
123 | return; |
124 | ||
125 | cache = mn10300_frame_unwind_cache (fi, this_cache); | |
9b3c083c MS |
126 | if (cache == NULL) |
127 | return; | |
128 | ||
aac71672 MS |
129 | base = trad_frame_get_this_base (cache); |
130 | if (movm_args & movm_other_bit) | |
131 | { | |
132 | /* The `other' bit leaves a blank area of four bytes at the | |
133 | beginning of its block of saved registers, making it 32 bytes | |
134 | long in total. */ | |
135 | trad_frame_set_reg_addr (cache, E_LAR_REGNUM, base + offset + 4); | |
136 | trad_frame_set_reg_addr (cache, E_LIR_REGNUM, base + offset + 8); | |
137 | trad_frame_set_reg_addr (cache, E_MDR_REGNUM, base + offset + 12); | |
138 | trad_frame_set_reg_addr (cache, E_A0_REGNUM + 1, base + offset + 16); | |
139 | trad_frame_set_reg_addr (cache, E_A0_REGNUM, base + offset + 20); | |
140 | trad_frame_set_reg_addr (cache, E_D0_REGNUM + 1, base + offset + 24); | |
141 | trad_frame_set_reg_addr (cache, E_D0_REGNUM, base + offset + 28); | |
142 | offset += 32; | |
143 | } | |
144 | ||
145 | if (movm_args & movm_a3_bit) | |
146 | { | |
147 | trad_frame_set_reg_addr (cache, E_A3_REGNUM, base + offset); | |
148 | offset += 4; | |
149 | } | |
150 | if (movm_args & movm_a2_bit) | |
151 | { | |
152 | trad_frame_set_reg_addr (cache, E_A2_REGNUM, base + offset); | |
153 | offset += 4; | |
154 | } | |
155 | if (movm_args & movm_d3_bit) | |
156 | { | |
157 | trad_frame_set_reg_addr (cache, E_D3_REGNUM, base + offset); | |
158 | offset += 4; | |
159 | } | |
160 | if (movm_args & movm_d2_bit) | |
161 | { | |
162 | trad_frame_set_reg_addr (cache, E_D2_REGNUM, base + offset); | |
163 | offset += 4; | |
164 | } | |
165 | if (AM33_MODE) | |
166 | { | |
167 | if (movm_args & movm_exother_bit) | |
168 | { | |
169 | trad_frame_set_reg_addr (cache, E_MCVF_REGNUM, base + offset); | |
170 | trad_frame_set_reg_addr (cache, E_MCRL_REGNUM, base + offset + 4); | |
171 | trad_frame_set_reg_addr (cache, E_MCRH_REGNUM, base + offset + 8); | |
172 | trad_frame_set_reg_addr (cache, E_MDRQ_REGNUM, base + offset + 12); | |
173 | trad_frame_set_reg_addr (cache, E_E1_REGNUM, base + offset + 16); | |
174 | trad_frame_set_reg_addr (cache, E_E0_REGNUM, base + offset + 20); | |
175 | offset += 24; | |
176 | } | |
177 | if (movm_args & movm_exreg1_bit) | |
178 | { | |
179 | trad_frame_set_reg_addr (cache, E_E7_REGNUM, base + offset); | |
180 | trad_frame_set_reg_addr (cache, E_E6_REGNUM, base + offset + 4); | |
181 | trad_frame_set_reg_addr (cache, E_E5_REGNUM, base + offset + 8); | |
182 | trad_frame_set_reg_addr (cache, E_E4_REGNUM, base + offset + 12); | |
183 | offset += 16; | |
184 | } | |
185 | if (movm_args & movm_exreg0_bit) | |
186 | { | |
187 | trad_frame_set_reg_addr (cache, E_E3_REGNUM, base + offset); | |
188 | trad_frame_set_reg_addr (cache, E_E2_REGNUM, base + offset + 4); | |
189 | offset += 8; | |
190 | } | |
191 | } | |
192 | /* The last (or first) thing on the stack will be the PC. */ | |
193 | trad_frame_set_reg_addr (cache, E_PC_REGNUM, base + offset); | |
194 | /* Save the SP in the 'traditional' way. | |
195 | This will be the same location where the PC is saved. */ | |
196 | trad_frame_set_reg_value (cache, E_SP_REGNUM, base + offset); | |
197 | } | |
198 | ||
199 | /* The main purpose of this file is dealing with prologues to extract | |
200 | information about stack frames and saved registers. | |
201 | ||
202 | In gcc/config/mn13000/mn10300.c, the expand_prologue prologue | |
203 | function is pretty readable, and has a nice explanation of how the | |
204 | prologue is generated. The prologues generated by that code will | |
205 | have the following form (NOTE: the current code doesn't handle all | |
206 | this!): | |
207 | ||
208 | + If this is an old-style varargs function, then its arguments | |
209 | need to be flushed back to the stack: | |
210 | ||
211 | mov d0,(4,sp) | |
212 | mov d1,(4,sp) | |
213 | ||
214 | + If we use any of the callee-saved registers, save them now. | |
215 | ||
216 | movm [some callee-saved registers],(sp) | |
217 | ||
218 | + If we have any floating-point registers to save: | |
219 | ||
220 | - Decrement the stack pointer to reserve space for the registers. | |
221 | If the function doesn't need a frame pointer, we may combine | |
222 | this with the adjustment that reserves space for the frame. | |
223 | ||
224 | add -SIZE, sp | |
225 | ||
226 | - Save the floating-point registers. We have two possible | |
227 | strategies: | |
228 | ||
229 | . Save them at fixed offset from the SP: | |
230 | ||
231 | fmov fsN,(OFFSETN,sp) | |
232 | fmov fsM,(OFFSETM,sp) | |
233 | ... | |
234 | ||
235 | Note that, if OFFSETN happens to be zero, you'll get the | |
236 | different opcode: fmov fsN,(sp) | |
237 | ||
238 | . Or, set a0 to the start of the save area, and then use | |
239 | post-increment addressing to save the FP registers. | |
240 | ||
241 | mov sp, a0 | |
242 | add SIZE, a0 | |
243 | fmov fsN,(a0+) | |
244 | fmov fsM,(a0+) | |
245 | ... | |
246 | ||
247 | + If the function needs a frame pointer, we set it here. | |
248 | ||
249 | mov sp, a3 | |
250 | ||
251 | + Now we reserve space for the stack frame proper. This could be | |
252 | merged into the `add -SIZE, sp' instruction for FP saves up | |
253 | above, unless we needed to set the frame pointer in the previous | |
254 | step, or the frame is so large that allocating the whole thing at | |
255 | once would put the FP register save slots out of reach of the | |
256 | addressing mode (128 bytes). | |
257 | ||
258 | add -SIZE, sp | |
259 | ||
260 | One day we might keep the stack pointer constant, that won't | |
261 | change the code for prologues, but it will make the frame | |
262 | pointerless case much more common. */ | |
263 | ||
264 | /* Analyze the prologue to determine where registers are saved, | |
265 | the end of the prologue, etc etc. Return the end of the prologue | |
266 | scanned. | |
267 | ||
268 | We store into FI (if non-null) several tidbits of information: | |
269 | ||
270 | * stack_size -- size of this stack frame. Note that if we stop in | |
271 | certain parts of the prologue/epilogue we may claim the size of the | |
272 | current frame is zero. This happens when the current frame has | |
273 | not been allocated yet or has already been deallocated. | |
274 | ||
275 | * fsr -- Addresses of registers saved in the stack by this frame. | |
276 | ||
277 | * status -- A (relatively) generic status indicator. It's a bitmask | |
278 | with the following bits: | |
279 | ||
280 | MY_FRAME_IN_SP: The base of the current frame is actually in | |
281 | the stack pointer. This can happen for frame pointerless | |
282 | functions, or cases where we're stopped in the prologue/epilogue | |
283 | itself. For these cases mn10300_analyze_prologue will need up | |
284 | update fi->frame before returning or analyzing the register | |
285 | save instructions. | |
286 | ||
287 | MY_FRAME_IN_FP: The base of the current frame is in the | |
288 | frame pointer register ($a3). | |
289 | ||
290 | NO_MORE_FRAMES: Set this if the current frame is "start" or | |
291 | if the first instruction looks like mov <imm>,sp. This tells | |
292 | frame chain to not bother trying to unwind past this frame. */ | |
293 | ||
294 | CORE_ADDR | |
295 | mn10300_analyze_prologue (struct frame_info *fi, | |
296 | void **this_cache, | |
297 | CORE_ADDR pc) | |
298 | { | |
299 | CORE_ADDR func_addr, func_end, addr, stop; | |
300 | long stack_size; | |
301 | int imm_size; | |
302 | unsigned char buf[4]; | |
303 | int status, movm_args = 0; | |
304 | char *name; | |
305 | ||
306 | /* Use the PC in the frame if it's provided to look up the | |
307 | start of this function. | |
308 | ||
309 | Note: kevinb/2003-07-16: We used to do the following here: | |
310 | pc = (fi ? get_frame_pc (fi) : pc); | |
311 | But this is (now) badly broken when called from analyze_dummy_frame(). | |
312 | */ | |
6a069e57 MS |
313 | if (fi) |
314 | { | |
315 | pc = (pc ? pc : get_frame_pc (fi)); | |
316 | /* At the start of a function our frame is in the stack pointer. */ | |
317 | my_frame_is_in_sp (fi, this_cache); | |
318 | } | |
aac71672 MS |
319 | |
320 | /* Find the start of this function. */ | |
321 | status = find_pc_partial_function (pc, &name, &func_addr, &func_end); | |
322 | ||
6a069e57 MS |
323 | /* Do nothing if we couldn't find the start of this function |
324 | ||
325 | MVS: comment went on to say "or if we're stopped at the first | |
326 | instruction in the prologue" -- but code doesn't reflect that, | |
327 | and I don't want to do that anyway. */ | |
aac71672 MS |
328 | if (status == 0) |
329 | { | |
330 | return pc; | |
331 | } | |
332 | ||
333 | /* If we're in start, then give up. */ | |
334 | if (strcmp (name, "start") == 0) | |
335 | { | |
336 | if (fi != NULL) | |
337 | my_frame_is_last (fi); | |
338 | return pc; | |
339 | } | |
340 | ||
aac71672 MS |
341 | #if 0 |
342 | /* Get the next two bytes into buf, we need two because rets is a two | |
343 | byte insn and the first isn't enough to uniquely identify it. */ | |
344 | status = deprecated_read_memory_nobpt (pc, buf, 2); | |
345 | if (status != 0) | |
346 | return pc; | |
347 | ||
348 | /* Note: kevinb/2003-07-16: We shouldn't be making these sorts of | |
349 | changes to the frame in prologue examination code. */ | |
350 | /* If we're physically on an "rets" instruction, then our frame has | |
351 | already been deallocated. Note this can also be true for retf | |
352 | and ret if they specify a size of zero. | |
353 | ||
354 | In this case fi->frame is bogus, we need to fix it. */ | |
355 | if (fi && buf[0] == 0xf0 && buf[1] == 0xfc) | |
356 | { | |
357 | if (get_next_frame (fi) == NULL) | |
358 | deprecated_update_frame_base_hack (fi, read_sp ()); | |
359 | return get_frame_pc (fi); | |
360 | } | |
361 | ||
362 | /* Similarly if we're stopped on the first insn of a prologue as our | |
363 | frame hasn't been allocated yet. */ | |
364 | if (fi && get_frame_pc (fi) == func_addr) | |
365 | { | |
366 | if (get_next_frame (fi) == NULL) | |
367 | deprecated_update_frame_base_hack (fi, read_sp ()); | |
368 | return get_frame_pc (fi); | |
369 | } | |
370 | #endif | |
371 | ||
6a069e57 MS |
372 | /* NOTE: from here on, we don't want to return without jumping to |
373 | finish_prologue. */ | |
374 | ||
375 | ||
aac71672 MS |
376 | /* Figure out where to stop scanning. */ |
377 | stop = fi ? pc : func_end; | |
378 | ||
379 | /* Don't walk off the end of the function. */ | |
380 | stop = stop > func_end ? func_end : stop; | |
381 | ||
382 | /* Start scanning on the first instruction of this function. */ | |
383 | addr = func_addr; | |
384 | ||
385 | /* Suck in two bytes. */ | |
386 | if (addr + 2 >= stop | |
387 | || (status = deprecated_read_memory_nobpt (addr, buf, 2)) != 0) | |
6a069e57 | 388 | goto finish_prologue; |
aac71672 MS |
389 | |
390 | /* First see if this insn sets the stack pointer from a register; if | |
391 | so, it's probably the initialization of the stack pointer in _start, | |
392 | so mark this as the bottom-most frame. */ | |
393 | if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0) | |
394 | { | |
395 | if (fi) | |
396 | my_frame_is_last (fi); | |
6a069e57 | 397 | goto finish_prologue; |
aac71672 MS |
398 | } |
399 | ||
400 | /* Now look for movm [regs],sp, which saves the callee saved registers. | |
401 | ||
402 | At this time we don't know if fi->frame is valid, so we only note | |
403 | that we encountered a movm instruction. Later, we'll set the entries | |
404 | in fsr.regs as needed. */ | |
405 | if (buf[0] == 0xcf) | |
406 | { | |
407 | /* Extract the register list for the movm instruction. */ | |
408 | movm_args = buf[1]; | |
409 | ||
410 | addr += 2; | |
411 | ||
412 | /* Quit now if we're beyond the stop point. */ | |
413 | if (addr >= stop) | |
6a069e57 | 414 | goto finish_prologue; |
aac71672 MS |
415 | |
416 | /* Get the next two bytes so the prologue scan can continue. */ | |
417 | status = deprecated_read_memory_nobpt (addr, buf, 2); | |
418 | if (status != 0) | |
6a069e57 | 419 | goto finish_prologue; |
aac71672 MS |
420 | } |
421 | ||
422 | /* Now see if we set up a frame pointer via "mov sp,a3" */ | |
423 | if (buf[0] == 0x3f) | |
424 | { | |
425 | addr += 1; | |
426 | ||
427 | /* The frame pointer is now valid. */ | |
428 | if (fi) | |
429 | { | |
430 | my_frame_is_in_fp (fi, this_cache); | |
431 | } | |
432 | ||
433 | /* Quit now if we're beyond the stop point. */ | |
434 | if (addr >= stop) | |
6a069e57 | 435 | goto finish_prologue; |
aac71672 MS |
436 | |
437 | /* Get two more bytes so scanning can continue. */ | |
438 | status = deprecated_read_memory_nobpt (addr, buf, 2); | |
439 | if (status != 0) | |
6a069e57 | 440 | goto finish_prologue; |
aac71672 MS |
441 | } |
442 | ||
443 | /* Next we should allocate the local frame. No more prologue insns | |
444 | are found after allocating the local frame. | |
445 | ||
446 | Search for add imm8,sp (0xf8feXX) | |
447 | or add imm16,sp (0xfafeXXXX) | |
448 | or add imm32,sp (0xfcfeXXXXXXXX). | |
449 | ||
450 | If none of the above was found, then this prologue has no | |
451 | additional stack. */ | |
452 | ||
453 | imm_size = 0; | |
454 | if (buf[0] == 0xf8 && buf[1] == 0xfe) | |
455 | imm_size = 1; | |
456 | else if (buf[0] == 0xfa && buf[1] == 0xfe) | |
457 | imm_size = 2; | |
458 | else if (buf[0] == 0xfc && buf[1] == 0xfe) | |
459 | imm_size = 4; | |
460 | ||
461 | if (imm_size != 0) | |
462 | { | |
463 | /* Suck in imm_size more bytes, they'll hold the size of the | |
464 | current frame. */ | |
465 | status = deprecated_read_memory_nobpt (addr + 2, buf, imm_size); | |
466 | if (status != 0) | |
6a069e57 | 467 | goto finish_prologue; |
aac71672 MS |
468 | |
469 | /* Note the size of the stack in the frame info structure. */ | |
470 | stack_size = extract_signed_integer (buf, imm_size); | |
471 | if (fi) | |
472 | set_my_stack_size (fi, stack_size); | |
473 | ||
474 | /* We just consumed 2 + imm_size bytes. */ | |
475 | addr += 2 + imm_size; | |
476 | ||
477 | /* No more prologue insns follow, so begin preparation to return. */ | |
6a069e57 | 478 | goto finish_prologue; |
aac71672 | 479 | } |
6a069e57 MS |
480 | /* Do the essentials and get out of here. */ |
481 | finish_prologue: | |
aac71672 | 482 | /* Note if/where callee saved registers were saved. */ |
6a069e57 MS |
483 | if (fi) |
484 | set_movm_offsets (fi, this_cache, movm_args); | |
aac71672 MS |
485 | return addr; |
486 | } | |
487 | ||
488 |