Fix warnings when compiling callback.c
[deliverable/binutils-gdb.git] / gdb / mn10200-tdep.c
CommitLineData
879b9398
GN
1/* Target-dependent code for the Matsushita MN10200 for GDB, the GNU debugger.
2 Copyright 1997 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20#include "defs.h"
21#include "frame.h"
22#include "inferior.h"
23#include "obstack.h"
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"
30
a698d0d0
JL
31/* The main purpose of this file is dealing with prologues to extract
32 information about stack frames and saved registers.
33
34 For reference here's how prologues look on the mn10200:
35
36 With frame pointer:
37 mov fp,a0
38 mov sp,fp
39 add <size>,sp
40 Register saves for d2, d3, a3 as needed. Saves start
41 at fp - <size> and work towards higher addresses. Note
42 that the saves are actually done off the stack pointer
43 in the prologue! This makes for smaller code and easier
44 prologue scanning as the displacement fields will never
45 be more than 8 bits!
46
47 Without frame pointer:
48 add <size>,sp
49 Register saves for d2, d3, a3 as needed. Saves start
50 at sp and work towards higher addresses.
51
52
53 One day we might keep the stack pointer constant, that won't
54 change the code for prologues, but it will make the frame
55 pointerless case much more common. */
56
57/* Analyze the prologue to determine where registers are saved,
58 the end of the prologue, etc etc. Return the end of the prologue
59 scanned.
60
61 We store into FI (if non-null) several tidbits of information:
62
63 * stack_size -- size of this stack frame. Note that if we stop in
64 certain parts of the prologue/epilogue we may claim the size of the
65 current frame is zero. This happens when the current frame has
66 not been allocated yet or has already been deallocated.
67
68 * fsr -- Addresses of registers saved in the stack by this frame.
69
70 * status -- A (relatively) generic status indicator. It's a bitmask
71 with the following bits:
72
73 MY_FRAME_IN_SP: The base of the current frame is actually in
74 the stack pointer. This can happen for frame pointerless
75 functions, or cases where we're stopped in the prologue/epilogue
76 itself. For these cases mn10200_analyze_prologue will need up
77 update fi->frame before returning or analyzing the register
78 save instructions.
79
80 MY_FRAME_IN_FP: The base of the current frame is in the
81 frame pointer register ($a2).
82
83 CALLER_A2_IN_A0: $a2 from the caller's frame is temporarily
84 in $a0. This can happen if we're stopped in the prologue.
85
86 NO_MORE_FRAMES: Set this if the current frame is "start" or
87 if the first instruction looks like mov <imm>,sp. This tells
88 frame chain to not bother trying to unwind past this frame. */
89
90#define MY_FRAME_IN_SP 0x1
91#define MY_FRAME_IN_FP 0x2
92#define CALLER_A2_IN_A0 0x4
93#define NO_MORE_FRAMES 0x8
94
95static CORE_ADDR
96mn10200_analyze_prologue (fi, pc)
97 struct frame_info *fi;
98 CORE_ADDR pc;
879b9398 99{
a698d0d0
JL
100 CORE_ADDR func_addr, func_end, addr, stop;
101 CORE_ADDR stack_size;
102 unsigned char buf[4];
103 int status;
104 char *name;
105
106 /* Use the PC in the frame if it's provided to look up the
107 start of this function. */
108 pc = (fi ? fi->pc : pc);
109
110 /* Find the start of this function. */
111 status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
112
113 /* Do nothing if we couldn't find the start of this function or if we're
114 stopped at the first instruction in the prologue. */
115 if (status == 0)
116 return pc;
117
118 /* If we're in start, then give up. */
119 if (strcmp (name, "start") == 0)
120 {
121 fi->status = NO_MORE_FRAMES;
122 return pc;
123 }
879b9398 124
a698d0d0
JL
125 /* At the start of a function our frame is in the stack pointer. */
126 if (fi)
127 fi->status = MY_FRAME_IN_SP;
879b9398 128
a698d0d0
JL
129 /* If we're physically on an RTS instruction, then our frame has already
130 been deallocated.
879b9398 131
a698d0d0
JL
132 fi->frame is bogus, we need to fix it. */
133 if (fi && fi->pc + 1 == func_end)
134 {
135 status = target_read_memory (fi->pc, buf, 1);
136 if (status != 0)
137 {
138 fi->frame = read_sp ();
139 return fi->pc;
140 }
879b9398 141
a698d0d0
JL
142 if (buf[0] == 0xfe)
143 {
144 fi->frame = read_sp ();
145 return fi->pc;
146 }
147 }
879b9398 148
a698d0d0
JL
149 /* Similarly if we're stopped on the first insn of a prologue as our
150 frame hasn't been allocated yet. */
151 if (fi && fi->pc == func_addr)
152 {
153 fi->frame = read_sp ();
154 return fi->pc;
155 }
156
157 /* Figure out where to stop scanning. */
158 stop = fi ? fi->pc : func_end;
159
160 /* Don't walk off the end of the function. */
161 stop = stop > func_end ? func_end : stop;
162
163 /* Start scanning on the first instruction of this function. */
164 addr = func_addr;
165
166 status = target_read_memory (addr, buf, 2);
167 if (status != 0)
879b9398 168 {
a698d0d0
JL
169 if (fi && fi->status & MY_FRAME_IN_SP)
170 fi->frame = read_sp ();
171 return addr;
879b9398
GN
172 }
173
a698d0d0
JL
174 /* First see if this insn sets the stack pointer; if so, it's something
175 we won't understand, so quit now. */
176 if (buf[0] == 0xdf
177 || (buf[0] == 0xf4 && buf[1] == 0x77))
178 {
179 if (fi)
180 fi->status = NO_MORE_FRAMES;
181 return addr;
182 }
183
184 /* Now see if we have a frame pointer.
185
186 Search for mov a2,a0 (0xf278)
187 then mov a3,a2 (0xf27e). */
188
189 if (buf[0] == 0xf2 && buf[1] == 0x78)
190 {
191 /* Our caller's $a2 will be found in $a0 now. Note it for
192 our callers. */
193 if (fi)
194 fi->status |= CALLER_A2_IN_A0;
195 addr += 2;
196 if (addr >= stop)
197 {
198 /* We still haven't allocated our local stack. Handle this
199 as if we stopped on the first or last insn of a function. */
200 if (fi)
201 fi->frame = read_sp ();
202 return addr;
203 }
204
205 status = target_read_memory (addr, buf, 2);
206 if (status != 0)
207 {
208 if (fi)
209 fi->frame = read_sp ();
210 return addr;
211 }
212 if (buf[0] == 0xf2 && buf[1] == 0x7e)
213 {
214 addr += 2;
215
216 /* Our frame pointer is valid now. */
217 if (fi)
218 {
219 fi->status |= MY_FRAME_IN_FP;
220 fi->status &= ~MY_FRAME_IN_SP;
221 }
222 if (addr >= stop)
223 return addr;
224 }
225 else
226 {
227 if (fi)
228 fi->frame = read_sp ();
229 return addr;
230 }
231 }
232
233 /* Next we should allocate the local frame.
234
235 Search for add imm8,a3 (0xd3XX)
236 or add imm16,a3 (0xf70bXXXX)
237 or add imm24,a3 (0xf467XXXXXX).
238
239 If none of the above was found, then this prologue has
240 no stack, and therefore can't have any register saves,
241 so quit now. */
242 status = target_read_memory (addr, buf, 2);
243 if (status != 0)
244 {
245 if (fi && (fi->status & MY_FRAME_IN_SP))
246 fi->frame = read_sp ();
247 return addr;
248 }
249 if (buf[0] == 0xd3)
250 {
251 stack_size = extract_signed_integer (&buf[1], 1);
252 if (fi)
253 fi->stack_size = stack_size;
254 addr += 2;
255 if (addr >= stop)
256 {
257 if (fi && (fi->status & MY_FRAME_IN_SP))
258 fi->frame = read_sp () + stack_size;
259 return addr;
260 }
261 }
262 else if (buf[0] == 0xf7 && buf[1] == 0x0b)
263 {
264 status = target_read_memory (addr + 2, buf, 2);
265 if (status != 0)
266 {
267 if (fi && (fi->status & MY_FRAME_IN_SP))
268 fi->frame = read_sp ();
269 return addr;
270 }
271 stack_size = extract_signed_integer (buf, 2);
272 if (fi)
273 fi->stack_size = stack_size;
274 addr += 4;
275 if (addr >= stop)
276 {
277 if (fi && (fi->status & MY_FRAME_IN_SP))
278 fi->frame = read_sp () + stack_size;
279 return addr;
280 }
281 }
282 else if (buf[0] == 0xf4 && buf[1] == 0x67)
283 {
284 status = target_read_memory (addr + 2, buf, 3);
285 if (status != 0)
286 {
287 if (fi && (fi->status & MY_FRAME_IN_SP))
288 fi->frame = read_sp ();
289 return addr;
290 }
291 stack_size = extract_signed_integer (buf, 3);
292 if (fi)
293 fi->stack_size = stack_size;
294 addr += 5;
295 if (addr >= stop)
296 {
297 if (fi && (fi->status & MY_FRAME_IN_SP))
298 fi->frame = read_sp () + stack_size;
299 return addr;
300 }
301 }
302 else
879b9398 303 {
a698d0d0
JL
304 if (fi && (fi->status & MY_FRAME_IN_SP))
305 fi->frame = read_sp ();
306 return addr;
879b9398
GN
307 }
308
a698d0d0
JL
309 /* At this point fi->frame needs to be correct.
310
311 If MY_FRAME_IN_SP is set, then we need to fix fi->frame so
312 that backtracing, find_frame_saved_regs, etc work correctly. */
313 if (fi && (fi->status & MY_FRAME_IN_SP) != 0)
314 fi->frame = read_sp () - fi->stack_size;
315
316 /* And last we have the register saves. These are relatively
317 simple because they're physically done off the stack pointer,
318 and thus the number of different instructions we need to
319 check is greatly reduced because we know the displacements
320 will be small.
321
322 Search for movx d2,(X,a3) (0xf55eXX)
323 then movx d3,(X,a3) (0xf55fXX)
324 then mov a2,(X,a3) (0x5eXX) No frame pointer case
325 or mov a0,(X,a3) (0x5cXX) Frame pointer case. */
326
327 status = target_read_memory (addr, buf, 2);
328 if (status != 0)
329 return addr;
330 if (buf[0] == 0xf5 && buf[1] == 0x5e)
331 {
332 if (fi)
333 {
334 status = target_read_memory (addr + 2, buf, 1);
335 if (status != 0)
336 return addr;
337 fi->fsr.regs[2] = (fi->frame + stack_size
338 + extract_signed_integer (buf, 1));
339 }
340 addr += 3;
341 if (addr >= stop)
342 return addr;
343 status = target_read_memory (addr, buf, 2);
344 if (status != 0)
345 return addr;
346 }
347 if (buf[0] == 0xf5 && buf[1] == 0x5f)
348 {
349 if (fi)
350 {
351 status = target_read_memory (addr + 2, buf, 1);
352 if (status != 0)
353 return addr;
354 fi->fsr.regs[3] = (fi->frame + stack_size
355 + extract_signed_integer (buf, 1));
356 }
357 addr += 3;
358 if (addr >= stop)
359 return addr;
360 status = target_read_memory (addr, buf, 2);
361 if (status != 0)
362 return addr;
363 }
364 if (buf[0] == 0x5e || buf[0] == 0x5c)
365 {
366 if (fi)
367 {
368 status = target_read_memory (addr + 1, buf, 1);
369 if (status != 0)
370 return addr;
371 fi->fsr.regs[6] = (fi->frame + stack_size
372 + extract_signed_integer (buf, 1));
373 fi->status &= ~CALLER_A2_IN_A0;
374 }
375 addr += 2;
376 if (addr >= stop)
377 return addr;
378 return addr;
379 }
380 return addr;
879b9398 381}
a698d0d0
JL
382
383/* Function: frame_chain
384 Figure out and return the caller's frame pointer given current
385 frame_info struct.
879b9398 386
a698d0d0
JL
387 We don't handle dummy frames yet but we would probably just return the
388 stack pointer that was in use at the time the function call was made? */
879b9398
GN
389
390CORE_ADDR
a698d0d0 391mn10200_frame_chain (fi)
879b9398 392 struct frame_info *fi;
879b9398 393{
a698d0d0
JL
394 struct frame_info dummy_frame;
395
396 /* Walk through the prologue to determine the stack size,
397 location of saved registers, end of the prologue, etc. */
398 if (fi->status == 0)
399 mn10200_analyze_prologue (fi, (CORE_ADDR)0);
400
401 /* Quit now if mn10200_analyze_prologue set NO_MORE_FRAMES. */
402 if (fi->status & NO_MORE_FRAMES)
403 return 0;
404
405 /* Now that we've analyzed our prologue, determine the frame
406 pointer for our caller.
407
408 If our caller has a frame pointer, then we need to
409 find the entry value of $a2 to our function.
410
411 If CALLER_A2_IN_A0, then the chain is in $a0.
412
413 If fsr.regs[6] is nonzero, then it's at the memory
414 location pointed to by fsr.regs[6].
415
416 Else it's still in $a2.
879b9398 417
a698d0d0
JL
418 If our caller does not have a frame pointer, then his
419 frame base is fi->frame + caller's stack size + 4. */
420
421 /* The easiest way to get that info is to analyze our caller's frame.
879b9398 422
a698d0d0
JL
423 So we set up a dummy frame and call mn10200_analyze_prologue to
424 find stuff for us. */
425 dummy_frame.pc = FRAME_SAVED_PC (fi);
426 dummy_frame.frame = fi->frame;
427 memset (dummy_frame.fsr.regs, '\000', sizeof dummy_frame.fsr.regs);
428 dummy_frame.status = 0;
429 dummy_frame.stack_size = 0;
430 mn10200_analyze_prologue (&dummy_frame);
431
432 if (dummy_frame.status & MY_FRAME_IN_FP)
433 {
434 /* Our caller has a frame pointer. So find the frame in $a2, $a0,
435 or in the stack. */
436 if (fi->fsr.regs[6])
437 return (read_memory_integer (fi->fsr.regs[FP_REGNUM], REGISTER_SIZE)
438 & 0xffffff);
439 else if (fi->status & CALLER_A2_IN_A0)
440 return read_register (4);
441 else
442 return read_register (FP_REGNUM);
443 }
444 else
445 {
446 /* Our caller does not have a frame pointer. So his frame starts
447 at the base of our frame (fi->frame) + <his size> + 4 (saved pc). */
448 return fi->frame + dummy_frame.stack_size + 4;
449 }
879b9398
GN
450}
451
452/* Function: skip_prologue
a698d0d0 453 Return the address of the first inst past the prologue of the function. */
879b9398
GN
454
455CORE_ADDR
456mn10200_skip_prologue (pc)
457 CORE_ADDR pc;
458{
459 CORE_ADDR func_addr, func_end;
460
a698d0d0
JL
461 /* First check the symbol table. That'll be faster than scanning
462 the prologue instructions if we have debug sybmols. */
879b9398
GN
463 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
464 {
465 struct symtab_and_line sal;
466
467 sal = find_pc_line (func_addr, 0);
468
469 if (sal.line != 0 && sal.end < func_end)
470 return sal.end;
a698d0d0
JL
471
472 return mn10200_analyze_prologue (NULL, pc);
879b9398
GN
473 }
474
a698d0d0 475 /* We couldn't find the start of this function, do nothing. */
879b9398
GN
476 return pc;
477}
478
479/* Function: pop_frame
480 This routine gets called when either the user uses the `return'
481 command, or the call dummy breakpoint gets hit. */
482
483void
484mn10200_pop_frame (frame)
485 struct frame_info *frame;
486{
487 int regnum;
488
879b9398
GN
489 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
490 generic_pop_dummy_frame ();
491 else
492 {
493 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
494
a698d0d0 495 /* Restore any saved registers. */
879b9398
GN
496 for (regnum = 0; regnum < NUM_REGS; regnum++)
497 if (frame->fsr.regs[regnum] != 0)
a698d0d0
JL
498 {
499 ULONGEST value;
500
501 value = read_memory_unsigned_integer (frame->fsr.regs[regnum],
502 REGISTER_RAW_SIZE (regnum));
503 write_register (regnum, value);
504 }
879b9398 505
a698d0d0 506 /* Actually cut back the stack. */
879b9398 507 write_register (SP_REGNUM, FRAME_FP (frame));
a698d0d0
JL
508
509 /* Don't we need to set the PC?!? XXX FIXME. */
879b9398
GN
510 }
511
a698d0d0 512 /* Throw away any cached frame information. */
879b9398 513 flush_cached_frames ();
879b9398
GN
514}
515
516/* Function: push_arguments
517 Setup arguments for a call to the target. Arguments go in
a698d0d0 518 order on the stack. */
879b9398
GN
519
520CORE_ADDR
521mn10200_push_arguments (nargs, args, sp, struct_return, struct_addr)
522 int nargs;
523 value_ptr *args;
524 CORE_ADDR sp;
525 unsigned char struct_return;
526 CORE_ADDR struct_addr;
527{
528 int argnum = 0;
529 int len = 0;
a698d0d0 530 int stack_offset = 0;
879b9398 531
a698d0d0
JL
532 /* This should be a nop, but align the stack just in case something
533 went wrong. */
879b9398
GN
534 sp &= ~3;
535
a698d0d0
JL
536 /* Now make space on the stack for the args.
537
538 XXX This doesn't appear to handle pass-by-invisible reference
539 arguments. */
879b9398 540 for (argnum = 0; argnum < nargs; argnum++)
a698d0d0 541 len += ((TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 3) & ~3);
879b9398 542
a698d0d0 543 /* Allocate stack space. */
879b9398
GN
544 sp -= len;
545
546 /* Push all arguments onto the stack. */
547 for (argnum = 0; argnum < nargs; argnum++)
548 {
549 int len;
550 char *val;
551
a698d0d0
JL
552 /* XXX Check this. What about UNIONS? Size check looks
553 wrong too. */
879b9398
GN
554 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
555 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
556 {
a698d0d0 557 /* XXX Wrong, we want a pointer to this argument. */
879b9398
GN
558 len = TYPE_LENGTH (VALUE_TYPE (*args));
559 val = (char *)VALUE_CONTENTS (*args);
560 }
561 else
562 {
563 len = TYPE_LENGTH (VALUE_TYPE (*args));
564 val = (char *)VALUE_CONTENTS (*args);
565 }
566
567 while (len > 0)
568 {
a698d0d0 569 /* XXX This looks wrong; we can have one and two byte args. */
879b9398
GN
570 write_memory (sp + stack_offset, val, 4);
571
572 len -= 4;
573 val += 4;
574 stack_offset += 4;
575 }
576 args++;
577 }
578
879b9398
GN
579 return sp;
580}
581
582/* Function: push_return_address (pc)
583 Set up the return address for the inferior function call.
584 Needed for targets where we don't actually execute a JSR/BSR instruction */
585
586CORE_ADDR
587mn10200_push_return_address (pc, sp)
588 CORE_ADDR pc;
589 CORE_ADDR sp;
590{
879b9398 591
879b9398
GN
592 return sp;
593}
594
595/* Function: frame_saved_pc
596 Find the caller of this frame. We do this by seeing if RP_REGNUM
597 is saved in the stack anywhere, otherwise we get it from the
598 registers. If the inner frame is a dummy frame, return its PC
599 instead of RP, because that's where "caller" of the dummy-frame
600 will be found. */
601
602CORE_ADDR
603mn10200_frame_saved_pc (fi)
604 struct frame_info *fi;
605{
a698d0d0
JL
606 /* The saved PC will always be at the base of the current frame. */
607 return (read_memory_integer (fi->frame, REGISTER_SIZE) & 0xffffff);
879b9398
GN
608}
609
610void
611get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
612 char *raw_buffer;
613 int *optimized;
614 CORE_ADDR *addrp;
615 struct frame_info *frame;
616 int regnum;
617 enum lval_type *lval;
618{
879b9398
GN
619 generic_get_saved_register (raw_buffer, optimized, addrp,
620 frame, regnum, lval);
621}
622
623/* Function: init_extra_frame_info
624 Setup the frame's frame pointer, pc, and frame addresses for saved
a698d0d0 625 registers. Most of the work is done in mn10200_analyze_prologue().
879b9398
GN
626
627 Note that when we are called for the last frame (currently active frame),
628 that fi->pc and fi->frame will already be setup. However, fi->frame will
629 be valid only if this routine uses FP. For previous frames, fi-frame will
a698d0d0
JL
630 always be correct. mn10200_analyze_prologue will fix fi->frame if
631 it's not valid.
879b9398
GN
632
633 We can be called with the PC in the call dummy under two circumstances.
634 First, during normal backtracing, second, while figuring out the frame
a698d0d0 635 pointer just prior to calling the target function (see run_stack_dummy). */
879b9398
GN
636
637void
638mn10200_init_extra_frame_info (fi)
639 struct frame_info *fi;
640{
879b9398
GN
641 if (fi->next)
642 fi->pc = FRAME_SAVED_PC (fi->next);
643
644 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
a698d0d0
JL
645 fi->status = 0;
646 fi->stack_size = 0;
879b9398 647
a698d0d0 648 mn10200_analyze_prologue (fi, 0);
879b9398
GN
649}
650
651void
652_initialize_mn10200_tdep ()
653{
879b9398
GN
654 tm_print_insn = print_insn_mn10200;
655}
656
This page took 0.050359 seconds and 4 git commands to generate.