]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/mn10300-tdep.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / mn10300-tdep.c
CommitLineData
c906108c
SS
1/* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
2 Copyright 1996, 1997, 1998 Free Software Foundation, Inc.
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"
24#include "obstack.h"
25#include "target.h"
26#include "value.h"
27#include "bfd.h"
28#include "gdb_string.h"
29#include "gdbcore.h"
30#include "symfile.h"
31
c5aa993b
JM
32static CORE_ADDR mn10300_analyze_prologue PARAMS ((struct frame_info * fi,
33 CORE_ADDR pc));
c906108c
SS
34
35/* Additional info used by the frame */
36
37struct frame_extra_info
c5aa993b
JM
38 {
39 int status;
40 int stack_size;
41 };
c906108c 42
0f71a2f6 43
c5aa993b
JM
44static char *mn10300_generic_register_names[] =
45{"d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
46 "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
47 "", "", "", "", "", "", "", "",
48 "", "", "", "", "", "", "", "fp"};
0f71a2f6
JM
49
50static char **mn10300_register_names = mn10300_generic_register_names;
51
52char *
53mn10300_register_name (i)
54 int i;
55{
56 return mn10300_register_names[i];
57}
58
59CORE_ADDR
60mn10300_saved_pc_after_call (fi)
61 struct frame_info *fi;
62{
63 return read_memory_integer (read_register (SP_REGNUM), 4);
64}
65
66void
67mn10300_extract_return_value (type, regbuf, valbuf)
68 struct type *type;
69 char *regbuf;
70 char *valbuf;
71{
72 if (TYPE_CODE (type) == TYPE_CODE_PTR)
73 memcpy (valbuf, regbuf + REGISTER_BYTE (4), TYPE_LENGTH (type));
74 else
75 memcpy (valbuf, regbuf + REGISTER_BYTE (0), TYPE_LENGTH (type));
76}
77
78CORE_ADDR
79mn10300_extract_struct_value_address (regbuf)
80 char *regbuf;
81{
82 return extract_address (regbuf + REGISTER_BYTE (4),
83 REGISTER_RAW_SIZE (4));
84}
85
86void
87mn10300_store_return_value (type, valbuf)
88 struct type *type;
89 char *valbuf;
90{
91 if (TYPE_CODE (type) == TYPE_CODE_PTR)
92 write_register_bytes (REGISTER_BYTE (4), valbuf, TYPE_LENGTH (type));
93 else
94 write_register_bytes (REGISTER_BYTE (0), valbuf, TYPE_LENGTH (type));
95}
96
c906108c
SS
97static struct frame_info *analyze_dummy_frame PARAMS ((CORE_ADDR, CORE_ADDR));
98static struct frame_info *
99analyze_dummy_frame (pc, frame)
100 CORE_ADDR pc;
101 CORE_ADDR frame;
102{
103 static struct frame_info *dummy = NULL;
104 if (dummy == NULL)
105 {
106 dummy = xmalloc (sizeof (struct frame_info));
107 dummy->saved_regs = xmalloc (SIZEOF_FRAME_SAVED_REGS);
108 dummy->extra_info = xmalloc (sizeof (struct frame_extra_info));
109 }
110 dummy->next = NULL;
111 dummy->prev = NULL;
112 dummy->pc = pc;
113 dummy->frame = frame;
114 dummy->extra_info->status = 0;
115 dummy->extra_info->stack_size = 0;
116 memset (dummy->saved_regs, '\000', SIZEOF_FRAME_SAVED_REGS);
117 mn10300_analyze_prologue (dummy, 0);
118 return dummy;
119}
120
121/* Values for frame_info.status */
122
123#define MY_FRAME_IN_SP 0x1
124#define MY_FRAME_IN_FP 0x2
125#define NO_MORE_FRAMES 0x4
126
127
128/* Should call_function allocate stack space for a struct return? */
129int
130mn10300_use_struct_convention (gcc_p, type)
131 int gcc_p;
132 struct type *type;
133{
134 return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8);
135}
136
137/* The breakpoint instruction must be the same size as the smallest
138 instruction in the instruction set.
139
140 The Matsushita mn10x00 processors have single byte instructions
141 so we need a single byte breakpoint. Matsushita hasn't defined
142 one, so we defined it ourselves. */
143
144unsigned char *
145mn10300_breakpoint_from_pc (bp_addr, bp_size)
146 CORE_ADDR *bp_addr;
147 int *bp_size;
148{
c5aa993b
JM
149 static char breakpoint[] =
150 {0xff};
c906108c
SS
151 *bp_size = 1;
152 return breakpoint;
153}
154
155
156/* Fix fi->frame if it's bogus at this point. This is a helper
157 function for mn10300_analyze_prologue. */
158
159static void
160fix_frame_pointer (fi, stack_size)
c5aa993b
JM
161 struct frame_info *fi;
162 int stack_size;
c906108c
SS
163{
164 if (fi && fi->next == NULL)
165 {
166 if (fi->extra_info->status & MY_FRAME_IN_SP)
167 fi->frame = read_sp () - stack_size;
168 else if (fi->extra_info->status & MY_FRAME_IN_FP)
169 fi->frame = read_register (A3_REGNUM);
170 }
171}
172
173
174/* Set offsets of registers saved by movm instruction.
175 This is a helper function for mn10300_analyze_prologue. */
176
177static void
178set_movm_offsets (fi, movm_args)
c5aa993b
JM
179 struct frame_info *fi;
180 int movm_args;
c906108c
SS
181{
182 int offset = 0;
183
184 if (fi == NULL || movm_args == 0)
185 return;
186
187 if (movm_args & 0x10)
188 {
189 fi->saved_regs[A3_REGNUM] = fi->frame + offset;
190 offset += 4;
191 }
192 if (movm_args & 0x20)
193 {
194 fi->saved_regs[A2_REGNUM] = fi->frame + offset;
195 offset += 4;
196 }
197 if (movm_args & 0x40)
198 {
199 fi->saved_regs[D3_REGNUM] = fi->frame + offset;
200 offset += 4;
201 }
202 if (movm_args & 0x80)
203 {
204 fi->saved_regs[D2_REGNUM] = fi->frame + offset;
205 offset += 4;
206 }
207}
208
209
210/* The main purpose of this file is dealing with prologues to extract
211 information about stack frames and saved registers.
212
213 For reference here's how prologues look on the mn10300:
214
c5aa993b
JM
215 With frame pointer:
216 movm [d2,d3,a2,a3],sp
217 mov sp,a3
218 add <size>,sp
c906108c 219
c5aa993b
JM
220 Without frame pointer:
221 movm [d2,d3,a2,a3],sp (if needed)
222 add <size>,sp
c906108c
SS
223
224 One day we might keep the stack pointer constant, that won't
225 change the code for prologues, but it will make the frame
226 pointerless case much more common. */
c5aa993b 227
c906108c
SS
228/* Analyze the prologue to determine where registers are saved,
229 the end of the prologue, etc etc. Return the end of the prologue
230 scanned.
231
232 We store into FI (if non-null) several tidbits of information:
233
c5aa993b
JM
234 * stack_size -- size of this stack frame. Note that if we stop in
235 certain parts of the prologue/epilogue we may claim the size of the
236 current frame is zero. This happens when the current frame has
237 not been allocated yet or has already been deallocated.
c906108c 238
c5aa993b 239 * fsr -- Addresses of registers saved in the stack by this frame.
c906108c 240
c5aa993b
JM
241 * status -- A (relatively) generic status indicator. It's a bitmask
242 with the following bits:
c906108c 243
c5aa993b
JM
244 MY_FRAME_IN_SP: The base of the current frame is actually in
245 the stack pointer. This can happen for frame pointerless
246 functions, or cases where we're stopped in the prologue/epilogue
247 itself. For these cases mn10300_analyze_prologue will need up
248 update fi->frame before returning or analyzing the register
249 save instructions.
c906108c 250
c5aa993b
JM
251 MY_FRAME_IN_FP: The base of the current frame is in the
252 frame pointer register ($a2).
c906108c 253
c5aa993b
JM
254 NO_MORE_FRAMES: Set this if the current frame is "start" or
255 if the first instruction looks like mov <imm>,sp. This tells
256 frame chain to not bother trying to unwind past this frame. */
c906108c
SS
257
258static CORE_ADDR
259mn10300_analyze_prologue (fi, pc)
c5aa993b
JM
260 struct frame_info *fi;
261 CORE_ADDR pc;
c906108c
SS
262{
263 CORE_ADDR func_addr, func_end, addr, stop;
264 CORE_ADDR stack_size;
265 int imm_size;
266 unsigned char buf[4];
267 int status, movm_args = 0;
268 char *name;
269
270 /* Use the PC in the frame if it's provided to look up the
271 start of this function. */
272 pc = (fi ? fi->pc : pc);
273
274 /* Find the start of this function. */
275 status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
276
277 /* Do nothing if we couldn't find the start of this function or if we're
278 stopped at the first instruction in the prologue. */
279 if (status == 0)
43ff13b4
JM
280 {
281 return pc;
282 }
c906108c
SS
283
284 /* If we're in start, then give up. */
285 if (strcmp (name, "start") == 0)
286 {
287 if (fi != NULL)
288 fi->extra_info->status = NO_MORE_FRAMES;
289 return pc;
290 }
291
292 /* At the start of a function our frame is in the stack pointer. */
293 if (fi)
294 fi->extra_info->status = MY_FRAME_IN_SP;
295
296 /* Get the next two bytes into buf, we need two because rets is a two
297 byte insn and the first isn't enough to uniquely identify it. */
298 status = read_memory_nobpt (pc, buf, 2);
299 if (status != 0)
300 return pc;
301
302 /* If we're physically on an "rets" instruction, then our frame has
303 already been deallocated. Note this can also be true for retf
304 and ret if they specify a size of zero.
305
306 In this case fi->frame is bogus, we need to fix it. */
307 if (fi && buf[0] == 0xf0 && buf[1] == 0xfc)
308 {
309 if (fi->next == NULL)
310 fi->frame = read_sp ();
311 return fi->pc;
312 }
313
314 /* Similarly if we're stopped on the first insn of a prologue as our
315 frame hasn't been allocated yet. */
316 if (fi && fi->pc == func_addr)
317 {
318 if (fi->next == NULL)
319 fi->frame = read_sp ();
320 return fi->pc;
321 }
322
323 /* Figure out where to stop scanning. */
324 stop = fi ? fi->pc : func_end;
325
326 /* Don't walk off the end of the function. */
327 stop = stop > func_end ? func_end : stop;
328
329 /* Start scanning on the first instruction of this function. */
330 addr = func_addr;
331
332 /* Suck in two bytes. */
333 status = read_memory_nobpt (addr, buf, 2);
334 if (status != 0)
335 {
336 fix_frame_pointer (fi, 0);
337 return addr;
338 }
339
340 /* First see if this insn sets the stack pointer; if so, it's something
341 we won't understand, so quit now. */
342 if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0)
343 {
344 if (fi)
345 fi->extra_info->status = NO_MORE_FRAMES;
346 return addr;
347 }
348
349 /* Now look for movm [regs],sp, which saves the callee saved registers.
350
351 At this time we don't know if fi->frame is valid, so we only note
352 that we encountered a movm instruction. Later, we'll set the entries
353 in fsr.regs as needed. */
354 if (buf[0] == 0xcf)
355 {
356 /* Extract the register list for the movm instruction. */
357 status = read_memory_nobpt (addr + 1, buf, 1);
358 movm_args = *buf;
359
360 addr += 2;
361
362 /* Quit now if we're beyond the stop point. */
363 if (addr >= stop)
364 {
365 /* Fix fi->frame since it's bogus at this point. */
366 if (fi && fi->next == NULL)
367 fi->frame = read_sp ();
368
369 /* Note if/where callee saved registers were saved. */
370 set_movm_offsets (fi, movm_args);
371 return addr;
372 }
373
374 /* Get the next two bytes so the prologue scan can continue. */
375 status = read_memory_nobpt (addr, buf, 2);
376 if (status != 0)
377 {
378 /* Fix fi->frame since it's bogus at this point. */
379 if (fi && fi->next == NULL)
380 fi->frame = read_sp ();
381
382 /* Note if/where callee saved registers were saved. */
383 set_movm_offsets (fi, movm_args);
384 return addr;
385 }
386 }
387
388 /* Now see if we set up a frame pointer via "mov sp,a3" */
389 if (buf[0] == 0x3f)
390 {
391 addr += 1;
392
393 /* The frame pointer is now valid. */
394 if (fi)
395 {
396 fi->extra_info->status |= MY_FRAME_IN_FP;
397 fi->extra_info->status &= ~MY_FRAME_IN_SP;
398 }
399
400 /* Quit now if we're beyond the stop point. */
401 if (addr >= stop)
402 {
403 /* Fix fi->frame if it's bogus at this point. */
404 fix_frame_pointer (fi, 0);
405
406 /* Note if/where callee saved registers were saved. */
407 set_movm_offsets (fi, movm_args);
408 return addr;
409 }
410
411 /* Get two more bytes so scanning can continue. */
412 status = read_memory_nobpt (addr, buf, 2);
413 if (status != 0)
414 {
415 /* Fix fi->frame if it's bogus at this point. */
416 fix_frame_pointer (fi, 0);
417
418 /* Note if/where callee saved registers were saved. */
419 set_movm_offsets (fi, movm_args);
420 return addr;
421 }
422 }
c5aa993b 423
c906108c
SS
424 /* Next we should allocate the local frame. No more prologue insns
425 are found after allocating the local frame.
c5aa993b 426
c906108c 427 Search for add imm8,sp (0xf8feXX)
c5aa993b
JM
428 or add imm16,sp (0xfafeXXXX)
429 or add imm32,sp (0xfcfeXXXXXXXX).
430
c906108c
SS
431 If none of the above was found, then this prologue has no
432 additional stack. */
433
434 status = read_memory_nobpt (addr, buf, 2);
435 if (status != 0)
436 {
437 /* Fix fi->frame if it's bogus at this point. */
438 fix_frame_pointer (fi, 0);
439
440 /* Note if/where callee saved registers were saved. */
441 set_movm_offsets (fi, movm_args);
442 return addr;
443 }
444
445 imm_size = 0;
446 if (buf[0] == 0xf8 && buf[1] == 0xfe)
447 imm_size = 1;
448 else if (buf[0] == 0xfa && buf[1] == 0xfe)
449 imm_size = 2;
450 else if (buf[0] == 0xfc && buf[1] == 0xfe)
451 imm_size = 4;
452
453 if (imm_size != 0)
454 {
455 /* Suck in imm_size more bytes, they'll hold the size of the
456 current frame. */
457 status = read_memory_nobpt (addr + 2, buf, imm_size);
458 if (status != 0)
459 {
460 /* Fix fi->frame if it's bogus at this point. */
461 fix_frame_pointer (fi, 0);
462
463 /* Note if/where callee saved registers were saved. */
464 set_movm_offsets (fi, movm_args);
465 return addr;
466 }
467
468 /* Note the size of the stack in the frame info structure. */
469 stack_size = extract_signed_integer (buf, imm_size);
470 if (fi)
471 fi->extra_info->stack_size = stack_size;
472
473 /* We just consumed 2 + imm_size bytes. */
474 addr += 2 + imm_size;
475
476 /* No more prologue insns follow, so begin preparation to return. */
477 /* Fix fi->frame if it's bogus at this point. */
478 fix_frame_pointer (fi, stack_size);
479
480 /* Note if/where callee saved registers were saved. */
481 set_movm_offsets (fi, movm_args);
482 return addr;
483 }
484
485 /* We never found an insn which allocates local stack space, regardless
486 this is the end of the prologue. */
487 /* Fix fi->frame if it's bogus at this point. */
488 fix_frame_pointer (fi, 0);
489
490 /* Note if/where callee saved registers were saved. */
491 set_movm_offsets (fi, movm_args);
492 return addr;
493}
c5aa993b 494
c906108c
SS
495/* Function: frame_chain
496 Figure out and return the caller's frame pointer given current
497 frame_info struct.
498
499 We don't handle dummy frames yet but we would probably just return the
500 stack pointer that was in use at the time the function call was made? */
501
502CORE_ADDR
503mn10300_frame_chain (fi)
504 struct frame_info *fi;
505{
506 struct frame_info *dummy;
507 /* Walk through the prologue to determine the stack size,
508 location of saved registers, end of the prologue, etc. */
509 if (fi->extra_info->status == 0)
c5aa993b 510 mn10300_analyze_prologue (fi, (CORE_ADDR) 0);
c906108c
SS
511
512 /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES. */
513 if (fi->extra_info->status & NO_MORE_FRAMES)
514 return 0;
515
516 /* Now that we've analyzed our prologue, determine the frame
517 pointer for our caller.
518
c5aa993b
JM
519 If our caller has a frame pointer, then we need to
520 find the entry value of $a3 to our function.
521
522 If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory
523 location pointed to by fsr.regs[A3_REGNUM].
c906108c 524
c5aa993b 525 Else it's still in $a3.
c906108c 526
c5aa993b
JM
527 If our caller does not have a frame pointer, then his
528 frame base is fi->frame + -caller's stack size. */
c906108c 529
c906108c
SS
530 /* The easiest way to get that info is to analyze our caller's frame.
531 So we set up a dummy frame and call mn10300_analyze_prologue to
532 find stuff for us. */
533 dummy = analyze_dummy_frame (FRAME_SAVED_PC (fi), fi->frame);
534
535 if (dummy->extra_info->status & MY_FRAME_IN_FP)
536 {
537 /* Our caller has a frame pointer. So find the frame in $a3 or
538 in the stack. */
539 if (fi->saved_regs[A3_REGNUM])
540 return (read_memory_integer (fi->saved_regs[A3_REGNUM], REGISTER_SIZE));
541 else
542 return read_register (A3_REGNUM);
543 }
544 else
545 {
546 int adjust = 0;
547
548 adjust += (fi->saved_regs[D2_REGNUM] ? 4 : 0);
549 adjust += (fi->saved_regs[D3_REGNUM] ? 4 : 0);
550 adjust += (fi->saved_regs[A2_REGNUM] ? 4 : 0);
551 adjust += (fi->saved_regs[A3_REGNUM] ? 4 : 0);
552
553 /* Our caller does not have a frame pointer. So his frame starts
c5aa993b
JM
554 at the base of our frame (fi->frame) + register save space
555 + <his size>. */
c906108c
SS
556 return fi->frame + adjust + -dummy->extra_info->stack_size;
557 }
558}
559
560/* Function: skip_prologue
561 Return the address of the first inst past the prologue of the function. */
562
563CORE_ADDR
564mn10300_skip_prologue (pc)
565 CORE_ADDR pc;
566{
567 /* We used to check the debug symbols, but that can lose if
568 we have a null prologue. */
569 return mn10300_analyze_prologue (NULL, pc);
570}
571
572
573/* Function: pop_frame
574 This routine gets called when either the user uses the `return'
575 command, or the call dummy breakpoint gets hit. */
576
577void
578mn10300_pop_frame (frame)
579 struct frame_info *frame;
580{
581 int regnum;
582
c5aa993b 583 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
c906108c
SS
584 generic_pop_dummy_frame ();
585 else
586 {
587 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
588
589 /* Restore any saved registers. */
590 for (regnum = 0; regnum < NUM_REGS; regnum++)
591 if (frame->saved_regs[regnum] != 0)
592 {
593 ULONGEST value;
594
595 value = read_memory_unsigned_integer (frame->saved_regs[regnum],
c5aa993b 596 REGISTER_RAW_SIZE (regnum));
c906108c
SS
597 write_register (regnum, value);
598 }
599
600 /* Actually cut back the stack. */
601 write_register (SP_REGNUM, FRAME_FP (frame));
602
603 /* Don't we need to set the PC?!? XXX FIXME. */
604 }
605
606 /* Throw away any cached frame information. */
607 flush_cached_frames ();
608}
609
610/* Function: push_arguments
611 Setup arguments for a call to the target. Arguments go in
612 order on the stack. */
613
614CORE_ADDR
615mn10300_push_arguments (nargs, args, sp, struct_return, struct_addr)
616 int nargs;
617 value_ptr *args;
618 CORE_ADDR sp;
619 unsigned char struct_return;
620 CORE_ADDR struct_addr;
621{
622 int argnum = 0;
623 int len = 0;
624 int stack_offset = 0;
625 int regsused = struct_return ? 1 : 0;
626
627 /* This should be a nop, but align the stack just in case something
628 went wrong. Stacks are four byte aligned on the mn10300. */
629 sp &= ~3;
630
631 /* Now make space on the stack for the args.
632
633 XXX This doesn't appear to handle pass-by-invisible reference
634 arguments. */
635 for (argnum = 0; argnum < nargs; argnum++)
636 {
637 int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 3) & ~3;
638
639 while (regsused < 2 && arg_length > 0)
640 {
641 regsused++;
642 arg_length -= 4;
643 }
644 len += arg_length;
645 }
646
647 /* Allocate stack space. */
648 sp -= len;
649
650 regsused = struct_return ? 1 : 0;
651 /* Push all arguments onto the stack. */
652 for (argnum = 0; argnum < nargs; argnum++)
653 {
654 int len;
655 char *val;
656
657 /* XXX Check this. What about UNIONS? */
658 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
659 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
660 {
661 /* XXX Wrong, we want a pointer to this argument. */
c5aa993b
JM
662 len = TYPE_LENGTH (VALUE_TYPE (*args));
663 val = (char *) VALUE_CONTENTS (*args);
c906108c
SS
664 }
665 else
666 {
667 len = TYPE_LENGTH (VALUE_TYPE (*args));
c5aa993b 668 val = (char *) VALUE_CONTENTS (*args);
c906108c
SS
669 }
670
671 while (regsused < 2 && len > 0)
672 {
673 write_register (regsused, extract_unsigned_integer (val, 4));
674 val += 4;
675 len -= 4;
676 regsused++;
677 }
678
679 while (len > 0)
680 {
681 write_memory (sp + stack_offset, val, 4);
682 len -= 4;
683 val += 4;
684 stack_offset += 4;
685 }
686
687 args++;
688 }
689
690 /* Make space for the flushback area. */
691 sp -= 8;
692 return sp;
693}
694
695/* Function: push_return_address (pc)
696 Set up the return address for the inferior function call.
697 Needed for targets where we don't actually execute a JSR/BSR instruction */
c5aa993b 698
c906108c
SS
699CORE_ADDR
700mn10300_push_return_address (pc, sp)
701 CORE_ADDR pc;
702 CORE_ADDR sp;
703{
704 unsigned char buf[4];
705
706 store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
707 write_memory (sp - 4, buf, 4);
708 return sp - 4;
709}
710
711/* Function: store_struct_return (addr,sp)
712 Store the structure value return address for an inferior function
713 call. */
c5aa993b 714
c906108c
SS
715CORE_ADDR
716mn10300_store_struct_return (addr, sp)
717 CORE_ADDR addr;
718 CORE_ADDR sp;
719{
720 /* The structure return address is passed as the first argument. */
721 write_register (0, addr);
722 return sp;
723}
c5aa993b 724
c906108c
SS
725/* Function: frame_saved_pc
726 Find the caller of this frame. We do this by seeing if RP_REGNUM
727 is saved in the stack anywhere, otherwise we get it from the
728 registers. If the inner frame is a dummy frame, return its PC
729 instead of RP, because that's where "caller" of the dummy-frame
730 will be found. */
731
732CORE_ADDR
733mn10300_frame_saved_pc (fi)
734 struct frame_info *fi;
735{
736 int adjust = 0;
737
738 adjust += (fi->saved_regs[D2_REGNUM] ? 4 : 0);
739 adjust += (fi->saved_regs[D3_REGNUM] ? 4 : 0);
740 adjust += (fi->saved_regs[A2_REGNUM] ? 4 : 0);
741 adjust += (fi->saved_regs[A3_REGNUM] ? 4 : 0);
742
743 return (read_memory_integer (fi->frame + adjust, REGISTER_SIZE));
744}
745
c906108c
SS
746/* Function: mn10300_init_extra_frame_info
747 Setup the frame's frame pointer, pc, and frame addresses for saved
748 registers. Most of the work is done in mn10300_analyze_prologue().
749
750 Note that when we are called for the last frame (currently active frame),
751 that fi->pc and fi->frame will already be setup. However, fi->frame will
752 be valid only if this routine uses FP. For previous frames, fi-frame will
753 always be correct. mn10300_analyze_prologue will fix fi->frame if
754 it's not valid.
755
756 We can be called with the PC in the call dummy under two circumstances.
757 First, during normal backtracing, second, while figuring out the frame
758 pointer just prior to calling the target function (see run_stack_dummy). */
759
760void
761mn10300_init_extra_frame_info (fi)
762 struct frame_info *fi;
763{
764 if (fi->next)
765 fi->pc = FRAME_SAVED_PC (fi->next);
766
767 frame_saved_regs_zalloc (fi);
768 fi->extra_info = (struct frame_extra_info *)
769 frame_obstack_alloc (sizeof (struct frame_extra_info));
770
771 fi->extra_info->status = 0;
772 fi->extra_info->stack_size = 0;
773
774 mn10300_analyze_prologue (fi, 0);
775}
776
777/* Function: mn10300_virtual_frame_pointer
778 Return the register that the function uses for a frame pointer,
779 plus any necessary offset to be applied to the register before
780 any frame pointer offsets. */
781
782void
783mn10300_virtual_frame_pointer (pc, reg, offset)
784 CORE_ADDR pc;
785 long *reg;
786 long *offset;
787{
788 struct frame_info *dummy = analyze_dummy_frame (pc, 0);
789 /* Set up a dummy frame_info, Analyze the prolog and fill in the
790 extra info. */
791 /* Results will tell us which type of frame it uses. */
792 if (dummy->extra_info->status & MY_FRAME_IN_SP)
793 {
c5aa993b 794 *reg = SP_REGNUM;
c906108c
SS
795 *offset = -(dummy->extra_info->stack_size);
796 }
797 else
798 {
c5aa993b 799 *reg = A3_REGNUM;
c906108c
SS
800 *offset = 0;
801 }
802}
c5aa993b 803
c906108c
SS
804/* This can be made more generic later. */
805static void
806set_machine_hook (filename)
807 char *filename;
808{
809 int i;
810
811 if (bfd_get_mach (exec_bfd) == bfd_mach_mn10300
812 || bfd_get_mach (exec_bfd) == 0)
813 {
814 mn10300_register_names = mn10300_generic_register_names;
815 }
816
817}
818
819void
820_initialize_mn10300_tdep ()
821{
822/* printf("_initialize_mn10300_tdep\n"); */
823
824 tm_print_insn = print_insn_mn10300;
825
826 specify_exec_file_hook (set_machine_hook);
827}