* utils.c (floatformat_from_doublest): Handle infinity properly.
[deliverable/binutils-gdb.git] / gdb / mn10200-tdep.c
1 /* Target-dependent code for the Matsushita MN10200 for GDB, the GNU debugger.
2 Copyright 1997 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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, 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
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, a1, a2 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, a1, a2 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
95 static CORE_ADDR
96 mn10200_analyze_prologue (fi, pc)
97 struct frame_info *fi;
98 CORE_ADDR pc;
99 {
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 }
124
125 /* At the start of a function our frame is in the stack pointer. */
126 if (fi)
127 fi->status = MY_FRAME_IN_SP;
128
129 /* If we're physically on an RTS instruction, then our frame has already
130 been deallocated.
131
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 if (fi->next == NULL)
139 fi->frame = read_sp ();
140 return fi->pc;
141 }
142
143 if (buf[0] == 0xfe)
144 {
145 if (fi->next == NULL)
146 fi->frame = read_sp ();
147 return fi->pc;
148 }
149 }
150
151 /* Similarly if we're stopped on the first insn of a prologue as our
152 frame hasn't been allocated yet. */
153 if (fi && fi->pc == func_addr)
154 {
155 if (fi->next == NULL)
156 fi->frame = read_sp ();
157 return fi->pc;
158 }
159
160 /* Figure out where to stop scanning. */
161 stop = fi ? fi->pc : func_end;
162
163 /* Don't walk off the end of the function. */
164 stop = stop > func_end ? func_end : stop;
165
166 /* Start scanning on the first instruction of this function. */
167 addr = func_addr;
168
169 status = target_read_memory (addr, buf, 2);
170 if (status != 0)
171 {
172 if (fi && fi->next == NULL && fi->status & MY_FRAME_IN_SP)
173 fi->frame = read_sp ();
174 return addr;
175 }
176
177 /* First see if this insn sets the stack pointer; if so, it's something
178 we won't understand, so quit now. */
179 if (buf[0] == 0xdf
180 || (buf[0] == 0xf4 && buf[1] == 0x77))
181 {
182 if (fi)
183 fi->status = NO_MORE_FRAMES;
184 return addr;
185 }
186
187 /* Now see if we have a frame pointer.
188
189 Search for mov a2,a0 (0xf278)
190 then mov a3,a2 (0xf27e). */
191
192 if (buf[0] == 0xf2 && buf[1] == 0x78)
193 {
194 /* Our caller's $a2 will be found in $a0 now. Note it for
195 our callers. */
196 if (fi)
197 fi->status |= CALLER_A2_IN_A0;
198 addr += 2;
199 if (addr >= stop)
200 {
201 /* We still haven't allocated our local stack. Handle this
202 as if we stopped on the first or last insn of a function. */
203 if (fi && fi->next == NULL)
204 fi->frame = read_sp ();
205 return addr;
206 }
207
208 status = target_read_memory (addr, buf, 2);
209 if (status != 0)
210 {
211 if (fi && fi->next == NULL)
212 fi->frame = read_sp ();
213 return addr;
214 }
215 if (buf[0] == 0xf2 && buf[1] == 0x7e)
216 {
217 addr += 2;
218
219 /* Our frame pointer is valid now. */
220 if (fi)
221 {
222 fi->status |= MY_FRAME_IN_FP;
223 fi->status &= ~MY_FRAME_IN_SP;
224 }
225 if (addr >= stop)
226 return addr;
227 }
228 else
229 {
230 if (fi && fi->next == NULL)
231 fi->frame = read_sp ();
232 return addr;
233 }
234 }
235
236 /* Next we should allocate the local frame.
237
238 Search for add imm8,a3 (0xd3XX)
239 or add imm16,a3 (0xf70bXXXX)
240 or add imm24,a3 (0xf467XXXXXX).
241
242 If none of the above was found, then this prologue has
243 no stack, and therefore can't have any register saves,
244 so quit now. */
245 status = target_read_memory (addr, buf, 2);
246 if (status != 0)
247 {
248 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
249 fi->frame = read_sp ();
250 return addr;
251 }
252 if (buf[0] == 0xd3)
253 {
254 stack_size = extract_signed_integer (&buf[1], 1);
255 if (fi)
256 fi->stack_size = stack_size;
257 addr += 2;
258 if (addr >= stop)
259 {
260 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
261 fi->frame = read_sp () - stack_size;
262 return addr;
263 }
264 }
265 else if (buf[0] == 0xf7 && buf[1] == 0x0b)
266 {
267 status = target_read_memory (addr + 2, buf, 2);
268 if (status != 0)
269 {
270 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
271 fi->frame = read_sp ();
272 return addr;
273 }
274 stack_size = extract_signed_integer (buf, 2);
275 if (fi)
276 fi->stack_size = stack_size;
277 addr += 4;
278 if (addr >= stop)
279 {
280 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
281 fi->frame = read_sp () - stack_size;
282 return addr;
283 }
284 }
285 else if (buf[0] == 0xf4 && buf[1] == 0x67)
286 {
287 status = target_read_memory (addr + 2, buf, 3);
288 if (status != 0)
289 {
290 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
291 fi->frame = read_sp ();
292 return addr;
293 }
294 stack_size = extract_signed_integer (buf, 3);
295 if (fi)
296 fi->stack_size = stack_size;
297 addr += 5;
298 if (addr >= stop)
299 {
300 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
301 fi->frame = read_sp () - stack_size;
302 return addr;
303 }
304 }
305 else
306 {
307 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP))
308 fi->frame = read_sp ();
309 return addr;
310 }
311
312 /* At this point fi->frame needs to be correct.
313
314 If MY_FRAME_IN_SP is set and we're the innermost frame, then we
315 need to fix fi->frame so that backtracing, find_frame_saved_regs,
316 etc work correctly. */
317 if (fi && fi->next == NULL && (fi->status & MY_FRAME_IN_SP) != 0)
318 fi->frame = read_sp () - fi->stack_size;
319
320 /* And last we have the register saves. These are relatively
321 simple because they're physically done off the stack pointer,
322 and thus the number of different instructions we need to
323 check is greatly reduced because we know the displacements
324 will be small.
325
326 Search for movx d2,(X,a3) (0xf55eXX)
327 then movx d3,(X,a3) (0xf55fXX)
328 then mov a1,(X,a3) (0x5dXX) No frame pointer case
329 then mov a2,(X,a3) (0x5eXX) No frame pointer case
330 or mov a0,(X,a3) (0x5cXX) Frame pointer case. */
331
332 status = target_read_memory (addr, buf, 2);
333 if (status != 0)
334 return addr;
335 if (buf[0] == 0xf5 && buf[1] == 0x5e)
336 {
337 if (fi)
338 {
339 status = target_read_memory (addr + 2, buf, 1);
340 if (status != 0)
341 return addr;
342 fi->fsr.regs[2] = (fi->frame + stack_size
343 + extract_signed_integer (buf, 1));
344 }
345 addr += 3;
346 if (addr >= stop)
347 return addr;
348 status = target_read_memory (addr, buf, 2);
349 if (status != 0)
350 return addr;
351 }
352 if (buf[0] == 0xf5 && buf[1] == 0x5f)
353 {
354 if (fi)
355 {
356 status = target_read_memory (addr + 2, buf, 1);
357 if (status != 0)
358 return addr;
359 fi->fsr.regs[3] = (fi->frame + stack_size
360 + extract_signed_integer (buf, 1));
361 }
362 addr += 3;
363 if (addr >= stop)
364 return addr;
365 status = target_read_memory (addr, buf, 2);
366 if (status != 0)
367 return addr;
368 }
369 if (buf[0] == 0x5d)
370 {
371 if (fi)
372 {
373 status = target_read_memory (addr + 1, buf, 1);
374 if (status != 0)
375 return addr;
376 fi->fsr.regs[5] = (fi->frame + stack_size
377 + extract_signed_integer (buf, 1));
378 }
379 addr += 2;
380 if (addr >= stop)
381 return addr;
382 status = target_read_memory (addr, buf, 2);
383 if (status != 0)
384 return addr;
385 }
386 if (buf[0] == 0x5e || buf[0] == 0x5c)
387 {
388 if (fi)
389 {
390 status = target_read_memory (addr + 1, buf, 1);
391 if (status != 0)
392 return addr;
393 fi->fsr.regs[6] = (fi->frame + stack_size
394 + extract_signed_integer (buf, 1));
395 fi->status &= ~CALLER_A2_IN_A0;
396 }
397 addr += 2;
398 if (addr >= stop)
399 return addr;
400 return addr;
401 }
402 return addr;
403 }
404
405 /* Function: frame_chain
406 Figure out and return the caller's frame pointer given current
407 frame_info struct.
408
409 We don't handle dummy frames yet but we would probably just return the
410 stack pointer that was in use at the time the function call was made? */
411
412 CORE_ADDR
413 mn10200_frame_chain (fi)
414 struct frame_info *fi;
415 {
416 struct frame_info dummy_frame;
417
418 /* Walk through the prologue to determine the stack size,
419 location of saved registers, end of the prologue, etc. */
420 if (fi->status == 0)
421 mn10200_analyze_prologue (fi, (CORE_ADDR)0);
422
423 /* Quit now if mn10200_analyze_prologue set NO_MORE_FRAMES. */
424 if (fi->status & NO_MORE_FRAMES)
425 return 0;
426
427 /* Now that we've analyzed our prologue, determine the frame
428 pointer for our caller.
429
430 If our caller has a frame pointer, then we need to
431 find the entry value of $a2 to our function.
432
433 If CALLER_A2_IN_A0, then the chain is in $a0.
434
435 If fsr.regs[6] is nonzero, then it's at the memory
436 location pointed to by fsr.regs[6].
437
438 Else it's still in $a2.
439
440 If our caller does not have a frame pointer, then his
441 frame base is fi->frame + -caller's stack size + 4. */
442
443 /* The easiest way to get that info is to analyze our caller's frame.
444
445 So we set up a dummy frame and call mn10200_analyze_prologue to
446 find stuff for us. */
447 dummy_frame.pc = FRAME_SAVED_PC (fi);
448 dummy_frame.frame = fi->frame;
449 memset (dummy_frame.fsr.regs, '\000', sizeof dummy_frame.fsr.regs);
450 dummy_frame.status = 0;
451 dummy_frame.stack_size = 0;
452 mn10200_analyze_prologue (&dummy_frame);
453
454 if (dummy_frame.status & MY_FRAME_IN_FP)
455 {
456 /* Our caller has a frame pointer. So find the frame in $a2, $a0,
457 or in the stack. */
458 if (fi->fsr.regs[6])
459 return (read_memory_integer (fi->fsr.regs[FP_REGNUM], REGISTER_SIZE)
460 & 0xffffff);
461 else if (fi->status & CALLER_A2_IN_A0)
462 return read_register (4);
463 else
464 return read_register (FP_REGNUM);
465 }
466 else
467 {
468 /* Our caller does not have a frame pointer. So his frame starts
469 at the base of our frame (fi->frame) + <his size> + 4 (saved pc). */
470 return fi->frame + -dummy_frame.stack_size + 4;
471 }
472 }
473
474 /* Function: skip_prologue
475 Return the address of the first inst past the prologue of the function. */
476
477 CORE_ADDR
478 mn10200_skip_prologue (pc)
479 CORE_ADDR pc;
480 {
481 CORE_ADDR func_addr, func_end;
482
483 /* First check the symbol table. That'll be faster than scanning
484 the prologue instructions if we have debug sybmols. */
485 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
486 {
487 struct symtab_and_line sal;
488
489 sal = find_pc_line (func_addr, 0);
490
491 if (sal.line != 0 && sal.end < func_end)
492 return sal.end;
493
494 return mn10200_analyze_prologue (NULL, pc);
495 }
496
497 /* We couldn't find the start of this function, do nothing. */
498 return pc;
499 }
500
501 /* Function: pop_frame
502 This routine gets called when either the user uses the `return'
503 command, or the call dummy breakpoint gets hit. */
504
505 void
506 mn10200_pop_frame (frame)
507 struct frame_info *frame;
508 {
509 int regnum;
510
511 if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
512 generic_pop_dummy_frame ();
513 else
514 {
515 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
516
517 /* Restore any saved registers. */
518 for (regnum = 0; regnum < NUM_REGS; regnum++)
519 if (frame->fsr.regs[regnum] != 0)
520 {
521 ULONGEST value;
522
523 value = read_memory_unsigned_integer (frame->fsr.regs[regnum],
524 REGISTER_RAW_SIZE (regnum));
525 write_register (regnum, value);
526 }
527
528 /* Actually cut back the stack. */
529 write_register (SP_REGNUM, FRAME_FP (frame));
530
531 /* Don't we need to set the PC?!? XXX FIXME. */
532 }
533
534 /* Throw away any cached frame information. */
535 flush_cached_frames ();
536 }
537
538 /* Function: push_arguments
539 Setup arguments for a call to the target. Arguments go in
540 order on the stack. */
541
542 CORE_ADDR
543 mn10200_push_arguments (nargs, args, sp, struct_return, struct_addr)
544 int nargs;
545 value_ptr *args;
546 CORE_ADDR sp;
547 unsigned char struct_return;
548 CORE_ADDR struct_addr;
549 {
550 int argnum = 0;
551 int len = 0;
552 int stack_offset = 0;
553 int regsused = struct_return ? 1 : 0;
554
555 /* This should be a nop, but align the stack just in case something
556 went wrong. Stacks are two byte aligned on the mn10200. */
557 sp &= ~1;
558
559 /* Now make space on the stack for the args.
560
561 XXX This doesn't appear to handle pass-by-invisible reference
562 arguments. */
563 for (argnum = 0; argnum < nargs; argnum++)
564 {
565 int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 1) & ~1;
566
567 /* If we've used all argument registers, then this argument is
568 pushed. */
569 if (regsused >= 2 || arg_length > 4)
570 {
571 regsused = 2;
572 len += arg_length;
573 }
574 /* We know we've got some arg register space left. If this argument
575 will fit entirely in regs, then put it there. */
576 else if (arg_length <= 2
577 || TYPE_CODE (VALUE_TYPE (args[argnum])) == TYPE_CODE_PTR)
578 {
579 regsused++;
580 }
581 else if (regsused == 0)
582 {
583 regsused = 2;
584 }
585 else
586 {
587 regsused = 2;
588 len += arg_length;
589 }
590 }
591
592 /* Allocate stack space. */
593 sp -= len;
594
595 regsused = struct_return ? 1 : 0;
596 /* Push all arguments onto the stack. */
597 for (argnum = 0; argnum < nargs; argnum++)
598 {
599 int len;
600 char *val;
601
602 /* XXX Check this. What about UNIONS? */
603 if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
604 && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
605 {
606 /* XXX Wrong, we want a pointer to this argument. */
607 len = TYPE_LENGTH (VALUE_TYPE (*args));
608 val = (char *)VALUE_CONTENTS (*args);
609 }
610 else
611 {
612 len = TYPE_LENGTH (VALUE_TYPE (*args));
613 val = (char *)VALUE_CONTENTS (*args);
614 }
615
616 if (regsused < 2
617 && (len <= 2
618 || TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_PTR))
619 {
620 write_register (regsused, extract_unsigned_integer (val, 4));
621 regsused++;
622 }
623 else if (regsused == 0 && len == 4)
624 {
625 write_register (regsused, extract_unsigned_integer (val, 2));
626 write_register (regsused + 1, extract_unsigned_integer (val + 2, 2));
627 regsused = 2;
628 }
629 else
630 {
631 regsused = 2;
632 while (len > 0)
633 {
634 write_memory (sp + stack_offset, val, 2);
635
636 len -= 2;
637 val += 2;
638 stack_offset += 2;
639 }
640 }
641 args++;
642 }
643
644 return sp;
645 }
646
647 /* Function: push_return_address (pc)
648 Set up the return address for the inferior function call.
649 Needed for targets where we don't actually execute a JSR/BSR instruction */
650
651 CORE_ADDR
652 mn10200_push_return_address (pc, sp)
653 CORE_ADDR pc;
654 CORE_ADDR sp;
655 {
656 unsigned char buf[4];
657
658 store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
659 write_memory (sp - 4, buf, 4);
660 return sp - 4;
661 }
662
663 /* Function: store_struct_return (addr,sp)
664 Store the structure value return address for an inferior function
665 call. */
666
667 CORE_ADDR
668 mn10200_store_struct_return (addr, sp)
669 CORE_ADDR addr;
670 CORE_ADDR sp;
671 {
672 /* The structure return address is passed as the first argument. */
673 write_register (0, addr);
674 return sp;
675 }
676
677 /* Function: frame_saved_pc
678 Find the caller of this frame. We do this by seeing if RP_REGNUM
679 is saved in the stack anywhere, otherwise we get it from the
680 registers. If the inner frame is a dummy frame, return its PC
681 instead of RP, because that's where "caller" of the dummy-frame
682 will be found. */
683
684 CORE_ADDR
685 mn10200_frame_saved_pc (fi)
686 struct frame_info *fi;
687 {
688 /* The saved PC will always be at the base of the current frame. */
689 return (read_memory_integer (fi->frame, REGISTER_SIZE) & 0xffffff);
690 }
691
692 void
693 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
694 char *raw_buffer;
695 int *optimized;
696 CORE_ADDR *addrp;
697 struct frame_info *frame;
698 int regnum;
699 enum lval_type *lval;
700 {
701 generic_get_saved_register (raw_buffer, optimized, addrp,
702 frame, regnum, lval);
703 }
704
705 /* Function: init_extra_frame_info
706 Setup the frame's frame pointer, pc, and frame addresses for saved
707 registers. Most of the work is done in mn10200_analyze_prologue().
708
709 Note that when we are called for the last frame (currently active frame),
710 that fi->pc and fi->frame will already be setup. However, fi->frame will
711 be valid only if this routine uses FP. For previous frames, fi-frame will
712 always be correct. mn10200_analyze_prologue will fix fi->frame if
713 it's not valid.
714
715 We can be called with the PC in the call dummy under two circumstances.
716 First, during normal backtracing, second, while figuring out the frame
717 pointer just prior to calling the target function (see run_stack_dummy). */
718
719 void
720 mn10200_init_extra_frame_info (fi)
721 struct frame_info *fi;
722 {
723 if (fi->next)
724 fi->pc = FRAME_SAVED_PC (fi->next);
725
726 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
727 fi->status = 0;
728 fi->stack_size = 0;
729
730 mn10200_analyze_prologue (fi, 0);
731 }
732
733 void
734 _initialize_mn10200_tdep ()
735 {
736 tm_print_insn = print_insn_mn10200;
737 }
738
This page took 0.043381 seconds and 4 git commands to generate.