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