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