2003-01-13 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / gdb / mn10200-tdep.c
CommitLineData
c906108c 1/* Target-dependent code for the Matsushita MN10200 for GDB, the GNU debugger.
1bac305b
AC
2
3 Copyright 1997, 1998, 1999, 2000, 2001, 2003 Free Software
4 Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
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.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "frame.h"
25#include "inferior.h"
c906108c
SS
26#include "target.h"
27#include "value.h"
28#include "bfd.h"
29#include "gdb_string.h"
30#include "gdbcore.h"
31#include "symfile.h"
4e052eda 32#include "regcache.h"
c906108c 33
c5aa993b 34
c906108c
SS
35/* Should call_function allocate stack space for a struct return? */
36int
fba45db2 37mn10200_use_struct_convention (int gcc_p, struct type *type)
c906108c
SS
38{
39 return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8);
40}
9846de1b 41/* *INDENT-OFF* */
c906108c
SS
42/* The main purpose of this file is dealing with prologues to extract
43 information about stack frames and saved registers.
44
45 For reference here's how prologues look on the mn10200:
46
47 With frame pointer:
48 mov fp,a0
49 mov sp,fp
50 add <size>,sp
51 Register saves for d2, d3, a1, a2 as needed. Saves start
52 at fp - <size> + <outgoing_args_size> and work towards higher
53 addresses. Note that the saves are actually done off the stack
54 pointer in the prologue! This makes for smaller code and easier
55 prologue scanning as the displacement fields will unlikely
56 be more than 8 bits!
57
58 Without frame pointer:
59 add <size>,sp
60 Register saves for d2, d3, a1, a2 as needed. Saves start
61 at sp + <outgoing_args_size> and work towards higher addresses.
62
63 Out of line prologue:
64 add <local size>,sp -- optional
65 jsr __prologue
66 add <outgoing_size>,sp -- optional
67
68 The stack pointer remains constant throughout the life of most
69 functions. As a result the compiler will usually omit the
70 frame pointer, so we must handle frame pointerless functions. */
71
72/* Analyze the prologue to determine where registers are saved,
73 the end of the prologue, etc etc. Return the end of the prologue
74 scanned.
75
76 We store into FI (if non-null) several tidbits of information:
77
78 * stack_size -- size of this stack frame. Note that if we stop in
79 certain parts of the prologue/epilogue we may claim the size of the
80 current frame is zero. This happens when the current frame has
81 not been allocated yet or has already been deallocated.
82
83 * fsr -- Addresses of registers saved in the stack by this frame.
84
85 * status -- A (relatively) generic status indicator. It's a bitmask
86 with the following bits:
87
88 MY_FRAME_IN_SP: The base of the current frame is actually in
89 the stack pointer. This can happen for frame pointerless
90 functions, or cases where we're stopped in the prologue/epilogue
91 itself. For these cases mn10200_analyze_prologue will need up
92 update fi->frame before returning or analyzing the register
93 save instructions.
94
95 MY_FRAME_IN_FP: The base of the current frame is in the
96 frame pointer register ($a2).
97
98 CALLER_A2_IN_A0: $a2 from the caller's frame is temporarily
99 in $a0. This can happen if we're stopped in the prologue.
100
101 NO_MORE_FRAMES: Set this if the current frame is "start" or
102 if the first instruction looks like mov <imm>,sp. This tells
103 frame chain to not bother trying to unwind past this frame. */
9846de1b 104/* *INDENT-ON* */
c906108c 105
c5aa993b
JM
106
107
108
c906108c
SS
109#define MY_FRAME_IN_SP 0x1
110#define MY_FRAME_IN_FP 0x2
111#define CALLER_A2_IN_A0 0x4
112#define NO_MORE_FRAMES 0x8
c5aa993b 113
c906108c 114static CORE_ADDR
fba45db2 115mn10200_analyze_prologue (struct frame_info *fi, CORE_ADDR pc)
c906108c
SS
116{
117 CORE_ADDR func_addr, func_end, addr, stop;
93d56215 118 CORE_ADDR stack_size = 0;
c906108c
SS
119 unsigned char buf[4];
120 int status;
121 char *name;
122 int out_of_line_prologue = 0;
123
124 /* Use the PC in the frame if it's provided to look up the
125 start of this function. */
50abf9e5 126 pc = (fi ? get_frame_pc (fi) : pc);
c906108c
SS
127
128 /* Find the start of this function. */
129 status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
130
131 /* Do nothing if we couldn't find the start of this function or if we're
132 stopped at the first instruction in the prologue. */
133 if (status == 0)
134 return pc;
135
136 /* If we're in start, then give up. */
137 if (strcmp (name, "start") == 0)
138 {
139 if (fi)
c5aa993b 140 fi->status = NO_MORE_FRAMES;
c906108c
SS
141 return pc;
142 }
143
144 /* At the start of a function our frame is in the stack pointer. */
145 if (fi)
146 fi->status = MY_FRAME_IN_SP;
147
148 /* If we're physically on an RTS instruction, then our frame has already
149 been deallocated.
150
151 fi->frame is bogus, we need to fix it. */
50abf9e5 152 if (fi && get_frame_pc (fi) + 1 == func_end)
c906108c 153 {
50abf9e5 154 status = target_read_memory (get_frame_pc (fi), buf, 1);
c906108c
SS
155 if (status != 0)
156 {
11c02a10 157 if (get_next_frame (fi) == NULL)
8ccd593b 158 deprecated_update_frame_base_hack (fi, read_sp ());
50abf9e5 159 return get_frame_pc (fi);
c906108c
SS
160 }
161
162 if (buf[0] == 0xfe)
163 {
11c02a10 164 if (get_next_frame (fi) == NULL)
8ccd593b 165 deprecated_update_frame_base_hack (fi, read_sp ());
50abf9e5 166 return get_frame_pc (fi);
c906108c
SS
167 }
168 }
169
170 /* Similarly if we're stopped on the first insn of a prologue as our
171 frame hasn't been allocated yet. */
50abf9e5 172 if (fi && get_frame_pc (fi) == func_addr)
c906108c 173 {
11c02a10 174 if (get_next_frame (fi) == NULL)
8ccd593b 175 deprecated_update_frame_base_hack (fi, read_sp ());
50abf9e5 176 return get_frame_pc (fi);
c906108c
SS
177 }
178
179 /* Figure out where to stop scanning. */
50abf9e5 180 stop = fi ? get_frame_pc (fi) : func_end;
c906108c
SS
181
182 /* Don't walk off the end of the function. */
183 stop = stop > func_end ? func_end : stop;
184
185 /* Start scanning on the first instruction of this function. */
186 addr = func_addr;
187
188 status = target_read_memory (addr, buf, 2);
189 if (status != 0)
190 {
11c02a10 191 if (fi && get_next_frame (fi) == NULL && fi->status & MY_FRAME_IN_SP)
8ccd593b 192 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
193 return addr;
194 }
195
196 /* First see if this insn sets the stack pointer; if so, it's something
197 we won't understand, so quit now. */
198 if (buf[0] == 0xdf
199 || (buf[0] == 0xf4 && buf[1] == 0x77))
200 {
201 if (fi)
202 fi->status = NO_MORE_FRAMES;
203 return addr;
204 }
205
206 /* Now see if we have a frame pointer.
c5aa993b 207
c906108c 208 Search for mov a2,a0 (0xf278)
c5aa993b 209 then mov a3,a2 (0xf27e). */
c906108c
SS
210
211 if (buf[0] == 0xf2 && buf[1] == 0x78)
212 {
213 /* Our caller's $a2 will be found in $a0 now. Note it for
c5aa993b 214 our callers. */
c906108c
SS
215 if (fi)
216 fi->status |= CALLER_A2_IN_A0;
217 addr += 2;
218 if (addr >= stop)
219 {
220 /* We still haven't allocated our local stack. Handle this
221 as if we stopped on the first or last insn of a function. */
11c02a10 222 if (fi && get_next_frame (fi) == NULL)
8ccd593b 223 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
224 return addr;
225 }
226
227 status = target_read_memory (addr, buf, 2);
228 if (status != 0)
229 {
11c02a10 230 if (fi && get_next_frame (fi) == NULL)
8ccd593b 231 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
232 return addr;
233 }
234 if (buf[0] == 0xf2 && buf[1] == 0x7e)
235 {
236 addr += 2;
237
238 /* Our frame pointer is valid now. */
239 if (fi)
240 {
241 fi->status |= MY_FRAME_IN_FP;
242 fi->status &= ~MY_FRAME_IN_SP;
243 }
244 if (addr >= stop)
245 return addr;
246 }
247 else
248 {
11c02a10 249 if (fi && get_next_frame (fi) == NULL)
8ccd593b 250 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
251 return addr;
252 }
253 }
254
255 /* Next we should allocate the local frame.
c5aa993b 256
c906108c 257 Search for add imm8,a3 (0xd3XX)
c5aa993b
JM
258 or add imm16,a3 (0xf70bXXXX)
259 or add imm24,a3 (0xf467XXXXXX).
260
c906108c
SS
261 If none of the above was found, then this prologue has
262 no stack, and therefore can't have any register saves,
263 so quit now. */
264 status = target_read_memory (addr, buf, 2);
265 if (status != 0)
266 {
11c02a10 267 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 268 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
269 return addr;
270 }
271 if (buf[0] == 0xd3)
272 {
273 stack_size = extract_signed_integer (&buf[1], 1);
274 if (fi)
275 fi->stack_size = stack_size;
276 addr += 2;
277 if (addr >= stop)
278 {
11c02a10 279 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 280 deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
c906108c
SS
281 return addr;
282 }
283 }
284 else if (buf[0] == 0xf7 && buf[1] == 0x0b)
285 {
286 status = target_read_memory (addr + 2, buf, 2);
287 if (status != 0)
288 {
11c02a10 289 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 290 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
291 return addr;
292 }
293 stack_size = extract_signed_integer (buf, 2);
294 if (fi)
295 fi->stack_size = stack_size;
296 addr += 4;
297 if (addr >= stop)
298 {
11c02a10 299 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 300 deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
c906108c
SS
301 return addr;
302 }
303 }
304 else if (buf[0] == 0xf4 && buf[1] == 0x67)
305 {
306 status = target_read_memory (addr + 2, buf, 3);
307 if (status != 0)
308 {
11c02a10 309 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 310 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
311 return addr;
312 }
313 stack_size = extract_signed_integer (buf, 3);
314 if (fi)
315 fi->stack_size = stack_size;
316 addr += 5;
317 if (addr >= stop)
318 {
11c02a10 319 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 320 deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
c906108c
SS
321 return addr;
322 }
323 }
324
325 /* Now see if we have a call to __prologue for an out of line
326 prologue. */
327 status = target_read_memory (addr, buf, 2);
328 if (status != 0)
329 return addr;
330
331 /* First check for 16bit pc-relative call to __prologue. */
332 if (buf[0] == 0xfd)
333 {
334 CORE_ADDR temp;
335 status = target_read_memory (addr + 1, buf, 2);
336 if (status != 0)
337 {
11c02a10 338 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 339 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
340 return addr;
341 }
c5aa993b 342
c906108c
SS
343 /* Get the PC this instruction will branch to. */
344 temp = (extract_signed_integer (buf, 2) + addr + 3) & 0xffffff;
345
346 /* Get the name of the function at the target address. */
347 status = find_pc_partial_function (temp, &name, NULL, NULL);
348 if (status == 0)
349 {
11c02a10 350 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 351 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
352 return addr;
353 }
354
355 /* Note if it is an out of line prologue. */
356 out_of_line_prologue = (strcmp (name, "__prologue") == 0);
357
358 /* This sucks up 3 bytes of instruction space. */
359 if (out_of_line_prologue)
360 addr += 3;
361
362 if (addr >= stop)
363 {
11c02a10 364 if (fi && get_next_frame (fi) == NULL)
c906108c
SS
365 {
366 fi->stack_size -= 16;
8ccd593b 367 deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
c906108c
SS
368 }
369 return addr;
370 }
371 }
372 /* Now check for the 24bit pc-relative call to __prologue. */
373 else if (buf[0] == 0xf4 && buf[1] == 0xe1)
374 {
375 CORE_ADDR temp;
376 status = target_read_memory (addr + 2, buf, 3);
377 if (status != 0)
378 {
11c02a10 379 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 380 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
381 return addr;
382 }
c5aa993b 383
c906108c
SS
384 /* Get the PC this instruction will branch to. */
385 temp = (extract_signed_integer (buf, 3) + addr + 5) & 0xffffff;
386
387 /* Get the name of the function at the target address. */
388 status = find_pc_partial_function (temp, &name, NULL, NULL);
389 if (status == 0)
390 {
11c02a10 391 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
8ccd593b 392 deprecated_update_frame_base_hack (fi, read_sp ());
c906108c
SS
393 return addr;
394 }
395
396 /* Note if it is an out of line prologue. */
397 out_of_line_prologue = (strcmp (name, "__prologue") == 0);
398
399 /* This sucks up 5 bytes of instruction space. */
400 if (out_of_line_prologue)
401 addr += 5;
402
403 if (addr >= stop)
404 {
11c02a10 405 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
c906108c
SS
406 {
407 fi->stack_size -= 16;
8ccd593b 408 deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
c906108c
SS
409 }
410 return addr;
411 }
412 }
413
414 /* Now actually handle the out of line prologue. */
415 if (out_of_line_prologue)
416 {
417 int outgoing_args_size = 0;
418
419 /* First adjust the stack size for this function. The out of
c5aa993b 420 line prologue saves 4 registers (16bytes of data). */
c906108c
SS
421 if (fi)
422 fi->stack_size -= 16;
423
424 /* Update fi->frame if necessary. */
11c02a10 425 if (fi && get_next_frame (fi) == NULL)
8ccd593b 426 deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
c906108c
SS
427
428 /* After the out of line prologue, there may be another
c5aa993b
JM
429 stack adjustment for the outgoing arguments.
430
431 Search for add imm8,a3 (0xd3XX)
432 or add imm16,a3 (0xf70bXXXX)
433 or add imm24,a3 (0xf467XXXXXX). */
c906108c 434
c906108c
SS
435 status = target_read_memory (addr, buf, 2);
436 if (status != 0)
437 {
438 if (fi)
439 {
1e2330ba
AC
440 fi->fsr.regs[2] = get_frame_base (fi) + fi->stack_size + 4;
441 fi->fsr.regs[3] = get_frame_base (fi) + fi->stack_size + 8;
442 fi->fsr.regs[5] = get_frame_base (fi) + fi->stack_size + 12;
443 fi->fsr.regs[6] = get_frame_base (fi) + fi->stack_size + 16;
c906108c
SS
444 }
445 return addr;
446 }
447
448 if (buf[0] == 0xd3)
449 {
450 outgoing_args_size = extract_signed_integer (&buf[1], 1);
451 addr += 2;
452 }
453 else if (buf[0] == 0xf7 && buf[1] == 0x0b)
454 {
455 status = target_read_memory (addr + 2, buf, 2);
456 if (status != 0)
457 {
458 if (fi)
459 {
1e2330ba
AC
460 fi->fsr.regs[2] = get_frame_base (fi) + fi->stack_size + 4;
461 fi->fsr.regs[3] = get_frame_base (fi) + fi->stack_size + 8;
462 fi->fsr.regs[5] = get_frame_base (fi) + fi->stack_size + 12;
463 fi->fsr.regs[6] = get_frame_base (fi) + fi->stack_size + 16;
c906108c
SS
464 }
465 return addr;
466 }
467 outgoing_args_size = extract_signed_integer (buf, 2);
468 addr += 4;
469 }
470 else if (buf[0] == 0xf4 && buf[1] == 0x67)
471 {
472 status = target_read_memory (addr + 2, buf, 3);
473 if (status != 0)
474 {
11c02a10 475 if (fi && get_next_frame (fi) == NULL)
c906108c 476 {
1e2330ba
AC
477 fi->fsr.regs[2] = get_frame_base (fi) + fi->stack_size + 4;
478 fi->fsr.regs[3] = get_frame_base (fi) + fi->stack_size + 8;
479 fi->fsr.regs[5] = get_frame_base (fi) + fi->stack_size + 12;
480 fi->fsr.regs[6] = get_frame_base (fi) + fi->stack_size + 16;
c906108c
SS
481 }
482 return addr;
483 }
484 outgoing_args_size = extract_signed_integer (buf, 3);
485 addr += 5;
486 }
487 else
488 outgoing_args_size = 0;
489
490 /* Now that we know the size of the outgoing arguments, fix
c5aa993b 491 fi->frame again if this is the innermost frame. */
11c02a10 492 if (fi && get_next_frame (fi) == NULL)
1e2330ba 493 deprecated_update_frame_base_hack (fi, get_frame_base (fi) - outgoing_args_size);
c906108c
SS
494
495 /* Note the register save information and update the stack
c5aa993b 496 size for this frame too. */
c906108c
SS
497 if (fi)
498 {
1e2330ba
AC
499 fi->fsr.regs[2] = get_frame_base (fi) + fi->stack_size + 4;
500 fi->fsr.regs[3] = get_frame_base (fi) + fi->stack_size + 8;
501 fi->fsr.regs[5] = get_frame_base (fi) + fi->stack_size + 12;
502 fi->fsr.regs[6] = get_frame_base (fi) + fi->stack_size + 16;
c906108c
SS
503 fi->stack_size += outgoing_args_size;
504 }
505 /* There can be no more prologue insns, so return now. */
506 return addr;
507 }
508
509 /* At this point fi->frame needs to be correct.
510
511 If MY_FRAME_IN_SP is set and we're the innermost frame, then we
512 need to fix fi->frame so that backtracing, find_frame_saved_regs,
513 etc work correctly. */
11c02a10 514 if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP) != 0)
8ccd593b 515 deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
c906108c
SS
516
517 /* And last we have the register saves. These are relatively
518 simple because they're physically done off the stack pointer,
519 and thus the number of different instructions we need to
520 check is greatly reduced because we know the displacements
521 will be small.
c5aa993b 522
c906108c 523 Search for movx d2,(X,a3) (0xf55eXX)
c5aa993b
JM
524 then movx d3,(X,a3) (0xf55fXX)
525 then mov a1,(X,a3) (0x5dXX) No frame pointer case
526 then mov a2,(X,a3) (0x5eXX) No frame pointer case
527 or mov a0,(X,a3) (0x5cXX) Frame pointer case. */
c906108c
SS
528
529 status = target_read_memory (addr, buf, 2);
530 if (status != 0)
531 return addr;
532 if (buf[0] == 0xf5 && buf[1] == 0x5e)
533 {
534 if (fi)
535 {
536 status = target_read_memory (addr + 2, buf, 1);
537 if (status != 0)
538 return addr;
1e2330ba 539 fi->fsr.regs[2] = (get_frame_base (fi) + stack_size
c906108c
SS
540 + extract_signed_integer (buf, 1));
541 }
542 addr += 3;
543 if (addr >= stop)
544 return addr;
545 status = target_read_memory (addr, buf, 2);
546 if (status != 0)
547 return addr;
548 }
549 if (buf[0] == 0xf5 && buf[1] == 0x5f)
550 {
551 if (fi)
552 {
553 status = target_read_memory (addr + 2, buf, 1);
554 if (status != 0)
555 return addr;
1e2330ba 556 fi->fsr.regs[3] = (get_frame_base (fi) + stack_size
c906108c
SS
557 + extract_signed_integer (buf, 1));
558 }
559 addr += 3;
560 if (addr >= stop)
561 return addr;
562 status = target_read_memory (addr, buf, 2);
563 if (status != 0)
564 return addr;
565 }
566 if (buf[0] == 0x5d)
567 {
568 if (fi)
569 {
570 status = target_read_memory (addr + 1, buf, 1);
571 if (status != 0)
572 return addr;
1e2330ba 573 fi->fsr.regs[5] = (get_frame_base (fi) + stack_size
c906108c
SS
574 + extract_signed_integer (buf, 1));
575 }
576 addr += 2;
577 if (addr >= stop)
578 return addr;
579 status = target_read_memory (addr, buf, 2);
580 if (status != 0)
581 return addr;
582 }
583 if (buf[0] == 0x5e || buf[0] == 0x5c)
584 {
585 if (fi)
586 {
587 status = target_read_memory (addr + 1, buf, 1);
588 if (status != 0)
589 return addr;
1e2330ba 590 fi->fsr.regs[6] = (get_frame_base (fi) + stack_size
c906108c
SS
591 + extract_signed_integer (buf, 1));
592 fi->status &= ~CALLER_A2_IN_A0;
593 }
594 addr += 2;
595 if (addr >= stop)
596 return addr;
597 return addr;
598 }
599 return addr;
600}
c5aa993b 601
c906108c
SS
602/* Function: frame_chain
603 Figure out and return the caller's frame pointer given current
604 frame_info struct.
605
606 We don't handle dummy frames yet but we would probably just return the
607 stack pointer that was in use at the time the function call was made? */
608
609CORE_ADDR
fba45db2 610mn10200_frame_chain (struct frame_info *fi)
c906108c 611{
f6c609c4
AC
612 struct frame_info *dummy_frame = deprecated_frame_xmalloc ();
613 struct cleanup *old_chain = make_cleanup (xfree, dummy_frame);
614 CORE_ADDR ret;
c906108c
SS
615
616 /* Walk through the prologue to determine the stack size,
617 location of saved registers, end of the prologue, etc. */
618 if (fi->status == 0)
c5aa993b 619 mn10200_analyze_prologue (fi, (CORE_ADDR) 0);
c906108c
SS
620
621 /* Quit now if mn10200_analyze_prologue set NO_MORE_FRAMES. */
622 if (fi->status & NO_MORE_FRAMES)
623 return 0;
624
625 /* Now that we've analyzed our prologue, determine the frame
626 pointer for our caller.
627
c5aa993b
JM
628 If our caller has a frame pointer, then we need to
629 find the entry value of $a2 to our function.
c906108c 630
c5aa993b 631 If CALLER_A2_IN_A0, then the chain is in $a0.
c906108c 632
c5aa993b
JM
633 If fsr.regs[6] is nonzero, then it's at the memory
634 location pointed to by fsr.regs[6].
c906108c 635
c5aa993b
JM
636 Else it's still in $a2.
637
638 If our caller does not have a frame pointer, then his
639 frame base is fi->frame + -caller's stack size + 4. */
c906108c 640
c906108c
SS
641 /* The easiest way to get that info is to analyze our caller's frame.
642
643 So we set up a dummy frame and call mn10200_analyze_prologue to
644 find stuff for us. */
f6c609c4 645 deprecated_update_frame_pc_hack (dummy_frame, FRAME_SAVED_PC (fi));
1e2330ba 646 deprecated_update_frame_base_hack (dummy_frame, get_frame_base (fi));
f6c609c4
AC
647 memset (dummy_frame->fsr.regs, '\000', sizeof dummy_frame->fsr.regs);
648 dummy_frame->status = 0;
649 dummy_frame->stack_size = 0;
650 mn10200_analyze_prologue (dummy_frame, 0);
651
652 if (dummy_frame->status & MY_FRAME_IN_FP)
c906108c
SS
653 {
654 /* Our caller has a frame pointer. So find the frame in $a2, $a0,
c5aa993b 655 or in the stack. */
c906108c 656 if (fi->fsr.regs[6])
f6c609c4
AC
657 ret = (read_memory_integer (fi->fsr.regs[FP_REGNUM], REGISTER_SIZE)
658 & 0xffffff);
c906108c 659 else if (fi->status & CALLER_A2_IN_A0)
f6c609c4 660 ret = read_register (4);
c906108c 661 else
f6c609c4 662 ret = read_register (FP_REGNUM);
c906108c
SS
663 }
664 else
665 {
666 /* Our caller does not have a frame pointer. So his frame starts
c5aa993b 667 at the base of our frame (fi->frame) + <his size> + 4 (saved pc). */
1e2330ba 668 ret = get_frame_base (fi) + -dummy_frame->stack_size + 4;
c906108c 669 }
f6c609c4
AC
670 do_cleanups (old_chain);
671 return ret;
c906108c
SS
672}
673
674/* Function: skip_prologue
675 Return the address of the first inst past the prologue of the function. */
676
677CORE_ADDR
fba45db2 678mn10200_skip_prologue (CORE_ADDR pc)
c906108c
SS
679{
680 /* We used to check the debug symbols, but that can lose if
681 we have a null prologue. */
682 return mn10200_analyze_prologue (NULL, pc);
683}
684
685/* Function: pop_frame
686 This routine gets called when either the user uses the `return'
687 command, or the call dummy breakpoint gets hit. */
688
689void
fba45db2 690mn10200_pop_frame (struct frame_info *frame)
c906108c
SS
691{
692 int regnum;
693
1e2330ba
AC
694 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
695 get_frame_base (frame),
696 get_frame_base (frame)))
c906108c
SS
697 generic_pop_dummy_frame ();
698 else
699 {
700 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
701
702 /* Restore any saved registers. */
703 for (regnum = 0; regnum < NUM_REGS; regnum++)
704 if (frame->fsr.regs[regnum] != 0)
705 {
706 ULONGEST value;
707
708 value = read_memory_unsigned_integer (frame->fsr.regs[regnum],
c5aa993b 709 REGISTER_RAW_SIZE (regnum));
c906108c
SS
710 write_register (regnum, value);
711 }
712
713 /* Actually cut back the stack. */
c193f6ac 714 write_register (SP_REGNUM, get_frame_base (frame));
c906108c
SS
715
716 /* Don't we need to set the PC?!? XXX FIXME. */
717 }
718
719 /* Throw away any cached frame information. */
720 flush_cached_frames ();
721}
722
723/* Function: push_arguments
724 Setup arguments for a call to the target. Arguments go in
725 order on the stack. */
726
727CORE_ADDR
ea7c478f 728mn10200_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
fba45db2 729 unsigned char struct_return, CORE_ADDR struct_addr)
c906108c
SS
730{
731 int argnum = 0;
732 int len = 0;
733 int stack_offset = 0;
734 int regsused = struct_return ? 1 : 0;
735
736 /* This should be a nop, but align the stack just in case something
737 went wrong. Stacks are two byte aligned on the mn10200. */
738 sp &= ~1;
739
740 /* Now make space on the stack for the args.
741
742 XXX This doesn't appear to handle pass-by-invisible reference
743 arguments. */
744 for (argnum = 0; argnum < nargs; argnum++)
745 {
746 int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 1) & ~1;
747
748 /* If we've used all argument registers, then this argument is
c5aa993b 749 pushed. */
c906108c
SS
750 if (regsused >= 2 || arg_length > 4)
751 {
752 regsused = 2;
753 len += arg_length;
754 }
755 /* We know we've got some arg register space left. If this argument
c5aa993b 756 will fit entirely in regs, then put it there. */
c906108c 757 else if (arg_length <= 2
c5aa993b 758 || TYPE_CODE (VALUE_TYPE (args[argnum])) == TYPE_CODE_PTR)
c906108c
SS
759 {
760 regsused++;
761 }
762 else if (regsused == 0)
763 {
764 regsused = 2;
765 }
766 else
767 {
768 regsused = 2;
769 len += arg_length;
770 }
771 }
772
773 /* Allocate stack space. */
774 sp -= len;
775
776 regsused = struct_return ? 1 : 0;
777 /* Push all arguments onto the stack. */
778 for (argnum = 0; argnum < nargs; argnum++)
779 {
780 int len;
781 char *val;
782
783 /* XXX Check this. What about UNIONS? */
784 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
785 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
786 {
787 /* XXX Wrong, we want a pointer to this argument. */
c5aa993b
JM
788 len = TYPE_LENGTH (VALUE_TYPE (*args));
789 val = (char *) VALUE_CONTENTS (*args);
c906108c
SS
790 }
791 else
792 {
793 len = TYPE_LENGTH (VALUE_TYPE (*args));
c5aa993b 794 val = (char *) VALUE_CONTENTS (*args);
c906108c
SS
795 }
796
797 if (regsused < 2
798 && (len <= 2
799 || TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_PTR))
800 {
801 write_register (regsused, extract_unsigned_integer (val, 4));
802 regsused++;
803 }
804 else if (regsused == 0 && len == 4)
805 {
806 write_register (regsused, extract_unsigned_integer (val, 2));
807 write_register (regsused + 1, extract_unsigned_integer (val + 2, 2));
808 regsused = 2;
809 }
810 else
811 {
812 regsused = 2;
813 while (len > 0)
814 {
815 write_memory (sp + stack_offset, val, 2);
816
817 len -= 2;
818 val += 2;
819 stack_offset += 2;
820 }
821 }
822 args++;
823 }
824
825 return sp;
826}
827
828/* Function: push_return_address (pc)
829 Set up the return address for the inferior function call.
830 Needed for targets where we don't actually execute a JSR/BSR instruction */
c5aa993b 831
c906108c 832CORE_ADDR
fba45db2 833mn10200_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
c906108c
SS
834{
835 unsigned char buf[4];
836
837 store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
838 write_memory (sp - 4, buf, 4);
839 return sp - 4;
840}
841
842/* Function: store_struct_return (addr,sp)
843 Store the structure value return address for an inferior function
844 call. */
c5aa993b 845
c906108c 846CORE_ADDR
fba45db2 847mn10200_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
c906108c
SS
848{
849 /* The structure return address is passed as the first argument. */
850 write_register (0, addr);
851 return sp;
852}
c5aa993b 853
c906108c
SS
854/* Function: frame_saved_pc
855 Find the caller of this frame. We do this by seeing if RP_REGNUM
856 is saved in the stack anywhere, otherwise we get it from the
857 registers. If the inner frame is a dummy frame, return its PC
858 instead of RP, because that's where "caller" of the dummy-frame
859 will be found. */
860
861CORE_ADDR
fba45db2 862mn10200_frame_saved_pc (struct frame_info *fi)
c906108c
SS
863{
864 /* The saved PC will always be at the base of the current frame. */
1e2330ba 865 return (read_memory_integer (get_frame_base (fi), REGISTER_SIZE) & 0xffffff);
c906108c
SS
866}
867
c906108c
SS
868/* Function: init_extra_frame_info
869 Setup the frame's frame pointer, pc, and frame addresses for saved
870 registers. Most of the work is done in mn10200_analyze_prologue().
871
872 Note that when we are called for the last frame (currently active frame),
50abf9e5 873 that get_frame_pc (fi) and fi->frame will already be setup. However, fi->frame will
c906108c
SS
874 be valid only if this routine uses FP. For previous frames, fi-frame will
875 always be correct. mn10200_analyze_prologue will fix fi->frame if
876 it's not valid.
877
878 We can be called with the PC in the call dummy under two circumstances.
879 First, during normal backtracing, second, while figuring out the frame
880 pointer just prior to calling the target function (see run_stack_dummy). */
881
882void
fba45db2 883mn10200_init_extra_frame_info (struct frame_info *fi)
c906108c 884{
11c02a10
AC
885 if (get_next_frame (fi))
886 deprecated_update_frame_pc_hack (fi, FRAME_SAVED_PC (get_next_frame (fi)));
c906108c
SS
887
888 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
889 fi->status = 0;
890 fi->stack_size = 0;
891
892 mn10200_analyze_prologue (fi, 0);
893}
894
895void
fba45db2 896_initialize_mn10200_tdep (void)
c906108c
SS
897{
898 tm_print_insn = print_insn_mn10200;
899}
This page took 0.287425 seconds and 4 git commands to generate.