* gdbint.texinfo (POP_FRAME): Document use by return_command.
[deliverable/binutils-gdb.git] / gdb / fr30-tdep.c
CommitLineData
c906108c
SS
1/* Target-dependent code for the Fujitsu FR30.
2 Copyright 1999, 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
392a587b
JM
32/* An expression that tells us whether the function invocation represented
33 by FI does not have a frame on the stack associated with it. */
34int
fba45db2 35fr30_frameless_function_invocation (struct frame_info *fi)
392a587b
JM
36{
37 int frameless;
38 CORE_ADDR func_start, after_prologue;
39 func_start = (get_pc_function_start ((fi)->pc) +
40 FUNCTION_START_OFFSET);
41 after_prologue = func_start;
42 after_prologue = SKIP_PROLOGUE (after_prologue);
43 frameless = (after_prologue == func_start);
44 return frameless;
45}
46
c906108c
SS
47/* Function: pop_frame
48 This routine gets called when either the user uses the `return'
49 command, or the call dummy breakpoint gets hit. */
50
51void
fba45db2 52fr30_pop_frame (void)
c906108c 53{
c5aa993b 54 struct frame_info *frame = get_current_frame ();
c906108c 55 int regnum;
c5aa993b 56 CORE_ADDR sp = read_register (SP_REGNUM);
c906108c 57
c5aa993b 58 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
c906108c
SS
59 generic_pop_dummy_frame ();
60 else
61 {
62 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
63
64 for (regnum = 0; regnum < NUM_REGS; regnum++)
c5aa993b
JM
65 if (frame->fsr.regs[regnum] != 0)
66 {
67 write_register (regnum,
68 read_memory_unsigned_integer (frame->fsr.regs[regnum],
69 REGISTER_RAW_SIZE (regnum)));
70 }
c906108c
SS
71 write_register (SP_REGNUM, sp + frame->framesize);
72 }
73 flush_cached_frames ();
74}
75
7a292a7a
SS
76
77/* Function: fr30_store_return_value
78 Put a value where a caller expects to see it. Used by the 'return'
79 command. */
80void
81fr30_store_return_value (struct type *type,
82 char *valbuf)
83{
84 /* Here's how the FR30 returns values (gleaned from gcc/config/
85 fr30/fr30.h):
86
87 If the return value is 32 bits long or less, it goes in r4.
88
89 If the return value is 64 bits long or less, it goes in r4 (most
90 significant word) and r5 (least significant word.
91
92 If the function returns a structure, of any size, the caller
93 passes the function an invisible first argument where the callee
94 should store the value. But GDB doesn't let you do that anyway.
95
96 If you're returning a value smaller than a word, it's not really
97 necessary to zero the upper bytes of the register; the caller is
98 supposed to ignore them. However, the FR30 typically keeps its
99 values extended to the full register width, so we should emulate
100 that. */
101
102 /* The FR30 is big-endian, so if we return a small value (like a
103 short or a char), we need to position it correctly within the
104 register. We round the size up to a register boundary, and then
105 adjust the offset so as to place the value at the right end. */
106 int value_size = TYPE_LENGTH (type);
107 int returned_size = (value_size + FR30_REGSIZE - 1) & ~(FR30_REGSIZE - 1);
108 int offset = (REGISTER_BYTE (RETVAL_REG)
109 + (returned_size - value_size));
110 char *zeros = alloca (returned_size);
111 memset (zeros, 0, returned_size);
112
113 write_register_bytes (REGISTER_BYTE (RETVAL_REG), zeros, returned_size);
114 write_register_bytes (offset, valbuf, value_size);
115}
116
117
c906108c
SS
118/* Function: skip_prologue
119 Return the address of the first code past the prologue of the function. */
120
121CORE_ADDR
c5aa993b 122fr30_skip_prologue (CORE_ADDR pc)
c906108c
SS
123{
124 CORE_ADDR func_addr, func_end;
125
126 /* See what the symbol table says */
127
128 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
129 {
130 struct symtab_and_line sal;
131
132 sal = find_pc_line (func_addr, 0);
133
c5aa993b
JM
134 if (sal.line != 0 && sal.end < func_end)
135 {
136 return sal.end;
c906108c
SS
137 }
138 }
139
140/* Either we didn't find the start of this function (nothing we can do),
141 or there's no line info, or the line after the prologue is after
142 the end of the function (there probably isn't a prologue). */
143
144 return pc;
145}
146
147
148/* Function: push_arguments
149 Setup arguments and RP for a call to the target. First four args
150 go in FIRST_ARGREG -> LAST_ARGREG, subsequent args go on stack...
151 Structs are passed by reference. XXX not right now Z.R.
152 64 bit quantities (doubles and long longs) may be split between
153 the regs and the stack.
154 When calling a function that returns a struct, a pointer to the struct
155 is passed in as a secret first argument (always in FIRST_ARGREG).
156
157 Stack space for the args has NOT been allocated: that job is up to us.
c5aa993b 158 */
c906108c
SS
159
160CORE_ADDR
fba45db2
KB
161fr30_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
162 int struct_return, CORE_ADDR struct_addr)
c906108c
SS
163{
164 int argreg;
165 int argnum;
166 int stack_offset;
c5aa993b
JM
167 struct stack_arg
168 {
c906108c
SS
169 char *val;
170 int len;
171 int offset;
172 };
173 struct stack_arg *stack_args =
c5aa993b 174 (struct stack_arg *) alloca (nargs * sizeof (struct stack_arg));
c906108c
SS
175 int nstack_args = 0;
176
177 argreg = FIRST_ARGREG;
178
179 /* the struct_return pointer occupies the first parameter-passing reg */
180 if (struct_return)
c5aa993b 181 write_register (argreg++, struct_addr);
c906108c
SS
182
183 stack_offset = 0;
184
185 /* Process args from left to right. Store as many as allowed in
c5aa993b
JM
186 registers, save the rest to be pushed on the stack */
187 for (argnum = 0; argnum < nargs; argnum++)
c906108c 188 {
c5aa993b
JM
189 char *val;
190 value_ptr arg = args[argnum];
191 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
192 struct type *target_type = TYPE_TARGET_TYPE (arg_type);
193 int len = TYPE_LENGTH (arg_type);
c906108c 194 enum type_code typecode = TYPE_CODE (arg_type);
c5aa993b 195 CORE_ADDR regval;
c906108c
SS
196 int newarg;
197
198 val = (char *) VALUE_CONTENTS (arg);
199
c906108c 200 {
c5aa993b
JM
201 /* Copy the argument to general registers or the stack in
202 register-sized pieces. Large arguments are split between
203 registers and stack. */
204 while (len > 0)
205 {
206 if (argreg <= LAST_ARGREG)
207 {
208 int partial_len = len < REGISTER_SIZE ? len : REGISTER_SIZE;
209 regval = extract_address (val, partial_len);
210
211 /* It's a simple argument being passed in a general
212 register. */
213 write_register (argreg, regval);
214 argreg++;
215 len -= partial_len;
216 val += partial_len;
217 }
218 else
219 {
220 /* keep for later pushing */
221 stack_args[nstack_args].val = val;
222 stack_args[nstack_args++].len = len;
223 break;
224 }
225 }
c906108c 226 }
c5aa993b
JM
227 }
228 /* now do the real stack pushing, process args right to left */
229 while (nstack_args--)
230 {
231 sp -= stack_args[nstack_args].len;
232 write_memory (sp, stack_args[nstack_args].val,
233 stack_args[nstack_args].len);
234 }
c906108c
SS
235
236 /* Return adjusted stack pointer. */
237 return sp;
238}
239
a14ed312 240void _initialize_fr30_tdep (void);
c906108c 241
7a292a7a 242void
fba45db2 243_initialize_fr30_tdep (void)
7a292a7a 244{
c5aa993b 245 extern int print_insn_fr30 (bfd_vma, disassemble_info *);
7a292a7a 246 tm_print_insn = print_insn_fr30;
c906108c
SS
247}
248
249/* Function: check_prologue_cache
250 Check if prologue for this frame's PC has already been scanned.
251 If it has, copy the relevant information about that prologue and
252 return non-zero. Otherwise do not copy anything and return zero.
253
254 The information saved in the cache includes:
c5aa993b
JM
255 * the frame register number;
256 * the size of the stack frame;
257 * the offsets of saved regs (relative to the old SP); and
258 * the offset from the stack pointer to the frame pointer
c906108c
SS
259
260 The cache contains only one entry, since this is adequate
261 for the typical sequence of prologue scan requests we get.
262 When performing a backtrace, GDB will usually ask to scan
263 the same function twice in a row (once to get the frame chain,
264 and once to fill in the extra frame information).
c5aa993b 265 */
c906108c
SS
266
267static struct frame_info prologue_cache;
268
269static int
fba45db2 270check_prologue_cache (struct frame_info *fi)
c906108c
SS
271{
272 int i;
273
274 if (fi->pc == prologue_cache.pc)
275 {
276 fi->framereg = prologue_cache.framereg;
277 fi->framesize = prologue_cache.framesize;
278 fi->frameoffset = prologue_cache.frameoffset;
279 for (i = 0; i <= NUM_REGS; i++)
280 fi->fsr.regs[i] = prologue_cache.fsr.regs[i];
281 return 1;
282 }
283 else
284 return 0;
285}
286
287
288/* Function: save_prologue_cache
289 Copy the prologue information from fi to the prologue cache.
c5aa993b 290 */
c906108c
SS
291
292static void
fba45db2 293save_prologue_cache (struct frame_info *fi)
c906108c
SS
294{
295 int i;
296
c5aa993b
JM
297 prologue_cache.pc = fi->pc;
298 prologue_cache.framereg = fi->framereg;
299 prologue_cache.framesize = fi->framesize;
c906108c 300 prologue_cache.frameoffset = fi->frameoffset;
c5aa993b
JM
301
302 for (i = 0; i <= NUM_REGS; i++)
303 {
304 prologue_cache.fsr.regs[i] = fi->fsr.regs[i];
305 }
c906108c
SS
306}
307
308
309/* Function: scan_prologue
310 Scan the prologue of the function that contains PC, and record what
311 we find in PI. PI->fsr must be zeroed by the called. Returns the
312 pc after the prologue. Note that the addresses saved in pi->fsr
313 are actually just frame relative (negative offsets from the frame
314 pointer). This is because we don't know the actual value of the
315 frame pointer yet. In some circumstances, the frame pointer can't
316 be determined till after we have scanned the prologue. */
317
318static void
fba45db2 319fr30_scan_prologue (struct frame_info *fi)
c906108c
SS
320{
321 int sp_offset, fp_offset;
322 CORE_ADDR prologue_start, prologue_end, current_pc;
323
324 /* Check if this function is already in the cache of frame information. */
325 if (check_prologue_cache (fi))
326 return;
327
328 /* Assume there is no frame until proven otherwise. */
c5aa993b
JM
329 fi->framereg = SP_REGNUM;
330 fi->framesize = 0;
c906108c
SS
331 fi->frameoffset = 0;
332
333 /* Find the function prologue. If we can't find the function in
334 the symbol table, peek in the stack frame to find the PC. */
335 if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
336 {
337 /* Assume the prologue is everything between the first instruction
338 in the function and the first source line. */
339 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
340
c5aa993b 341 if (sal.line == 0) /* no line info, use current PC */
c906108c
SS
342 prologue_end = fi->pc;
343 else if (sal.end < prologue_end) /* next line begins after fn end */
c5aa993b 344 prologue_end = sal.end; /* (probably means no prologue) */
c906108c
SS
345 }
346 else
347 {
348 /* XXX Z.R. What now??? The following is entirely bogus */
349 prologue_start = (read_memory_integer (fi->frame, 4) & 0x03fffffc) - 12;
350 prologue_end = prologue_start + 40;
351 }
352
353 /* Now search the prologue looking for instructions that set up the
354 frame pointer, adjust the stack pointer, and save registers. */
355
356 sp_offset = fp_offset = 0;
357 for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 2)
358 {
359 unsigned int insn;
360
361 insn = read_memory_unsigned_integer (current_pc, 2);
362
c5aa993b 363 if ((insn & 0xfe00) == 0x8e00) /* stm0 or stm1 */
c906108c
SS
364 {
365 int reg, mask = insn & 0xff;
366
367 /* scan in one sweep - create virtual 16-bit mask from either insn's mask */
c5aa993b 368 if ((insn & 0x0100) == 0)
c906108c 369 {
c5aa993b 370 mask <<= 8; /* stm0 - move to upper byte in virtual mask */
c906108c
SS
371 }
372
373 /* Calculate offsets of saved registers (to be turned later into addresses). */
374 for (reg = R4_REGNUM; reg <= R11_REGNUM; reg++)
375 if (mask & (1 << (15 - reg)))
376 {
377 sp_offset -= 4;
378 fi->fsr.regs[reg] = sp_offset;
379 }
380 }
c5aa993b
JM
381 else if ((insn & 0xfff0) == 0x1700) /* st rx,@-r15 */
382 {
c906108c
SS
383 int reg = insn & 0xf;
384
385 sp_offset -= 4;
386 fi->fsr.regs[reg] = sp_offset;
387 }
c5aa993b
JM
388 else if ((insn & 0xff00) == 0x0f00) /* enter */
389 {
c906108c
SS
390 fp_offset = fi->fsr.regs[FP_REGNUM] = sp_offset - 4;
391 sp_offset -= 4 * (insn & 0xff);
392 fi->framereg = FP_REGNUM;
393 }
c5aa993b 394 else if (insn == 0x1781) /* st rp,@-sp */
c906108c 395 {
c5aa993b
JM
396 sp_offset -= 4;
397 fi->fsr.regs[RP_REGNUM] = sp_offset;
c906108c 398 }
c5aa993b 399 else if (insn == 0x170e) /* st fp,@-sp */
c906108c 400 {
c5aa993b
JM
401 sp_offset -= 4;
402 fi->fsr.regs[FP_REGNUM] = sp_offset;
c906108c 403 }
c5aa993b 404 else if (insn == 0x8bfe) /* mov sp,fp */
c906108c
SS
405 {
406 fi->framereg = FP_REGNUM;
407 }
c5aa993b 408 else if ((insn & 0xff00) == 0xa300) /* addsp xx */
c906108c 409 {
c5aa993b 410 sp_offset += 4 * (signed char) (insn & 0xff);
c906108c 411 }
c5aa993b
JM
412 else if ((insn & 0xff0f) == 0x9b00 && /* ldi:20 xx,r0 */
413 read_memory_unsigned_integer (current_pc + 4, 2)
414 == 0xac0f) /* sub r0,sp */
c906108c
SS
415 {
416 /* large stack adjustment */
c5aa993b 417 sp_offset -= (((insn & 0xf0) << 12) | read_memory_unsigned_integer (current_pc + 2, 2));
c906108c
SS
418 current_pc += 4;
419 }
c5aa993b
JM
420 else if (insn == 0x9f80 && /* ldi:32 xx,r0 */
421 read_memory_unsigned_integer (current_pc + 6, 2)
422 == 0xac0f) /* sub r0,sp */
c906108c
SS
423 {
424 /* large stack adjustment */
425 sp_offset -=
c5aa993b
JM
426 (read_memory_unsigned_integer (current_pc + 2, 2) << 16 |
427 read_memory_unsigned_integer (current_pc + 4, 2));
c906108c
SS
428 current_pc += 6;
429 }
430 }
431
432 /* The frame size is just the negative of the offset (from the original SP)
433 of the last thing thing we pushed on the stack. The frame offset is
434 [new FP] - [new SP]. */
435 fi->framesize = -sp_offset;
436 fi->frameoffset = fp_offset - sp_offset;
c5aa993b 437
c906108c
SS
438 save_prologue_cache (fi);
439}
440
441/* Function: init_extra_frame_info
442 Setup the frame's frame pointer, pc, and frame addresses for saved
443 registers. Most of the work is done in scan_prologue().
444
445 Note that when we are called for the last frame (currently active frame),
446 that fi->pc and fi->frame will already be setup. However, fi->frame will
447 be valid only if this routine uses FP. For previous frames, fi-frame will
448 always be correct (since that is derived from fr30_frame_chain ()).
449
450 We can be called with the PC in the call dummy under two circumstances.
451 First, during normal backtracing, second, while figuring out the frame
452 pointer just prior to calling the target function (see run_stack_dummy). */
453
454void
fba45db2 455fr30_init_extra_frame_info (struct frame_info *fi)
c906108c
SS
456{
457 int reg;
458
459 if (fi->next)
460 fi->pc = FRAME_SAVED_PC (fi->next);
461
462 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
463
464 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
465 {
466 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
c5aa993b
JM
467 by assuming it's always FP. */
468 fi->frame = generic_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
469 fi->framesize = 0;
c906108c
SS
470 fi->frameoffset = 0;
471 return;
472 }
c5aa993b
JM
473 fr30_scan_prologue (fi);
474
475 if (!fi->next) /* this is the innermost frame? */
476 fi->frame = read_register (fi->framereg);
477 else
478 /* not the innermost frame */
0fb34c3a
MS
479 /* If we have an FP, the callee saved it. */
480 if (fi->framereg == FP_REGNUM)
481 if (fi->next->fsr.regs[fi->framereg] != 0)
482 fi->frame = read_memory_integer (fi->next->fsr.regs[fi->framereg], 4);
483
c5aa993b
JM
484 /* Calculate actual addresses of saved registers using offsets determined
485 by fr30_scan_prologue. */
486 for (reg = 0; reg < NUM_REGS; reg++)
487 if (fi->fsr.regs[reg] != 0)
488 {
489 fi->fsr.regs[reg] += fi->frame + fi->framesize - fi->frameoffset;
490 }
c906108c
SS
491}
492
493/* Function: find_callers_reg
494 Find REGNUM on the stack. Otherwise, it's in an active register.
495 One thing we might want to do here is to check REGNUM against the
496 clobber mask, and somehow flag it as invalid if it isn't saved on
497 the stack somewhere. This would provide a graceful failure mode
498 when trying to get the value of caller-saves registers for an inner
499 frame. */
500
501CORE_ADDR
fba45db2 502fr30_find_callers_reg (struct frame_info *fi, int regnum)
c906108c
SS
503{
504 for (; fi; fi = fi->next)
505 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
506 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
507 else if (fi->fsr.regs[regnum] != 0)
c5aa993b
JM
508 return read_memory_unsigned_integer (fi->fsr.regs[regnum],
509 REGISTER_RAW_SIZE (regnum));
c906108c
SS
510
511 return read_register (regnum);
512}
513
514
515/* Function: frame_chain
516 Figure out the frame prior to FI. Unfortunately, this involves
517 scanning the prologue of the caller, which will also be done
518 shortly by fr30_init_extra_frame_info. For the dummy frame, we
519 just return the stack pointer that was in use at the time the
520 function call was made. */
521
522
523CORE_ADDR
fba45db2 524fr30_frame_chain (struct frame_info *fi)
c906108c
SS
525{
526 CORE_ADDR fn_start, callers_pc, fp;
527 struct frame_info caller_fi;
528 int framereg;
529
530 /* is this a dummy frame? */
531 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
c5aa993b 532 return fi->frame; /* dummy frame same as caller's frame */
c906108c
SS
533
534 /* is caller-of-this a dummy frame? */
c5aa993b 535 callers_pc = FRAME_SAVED_PC (fi); /* find out who called us: */
c906108c 536 fp = fr30_find_callers_reg (fi, FP_REGNUM);
c5aa993b
JM
537 if (PC_IN_CALL_DUMMY (callers_pc, fp, fp))
538 return fp; /* dummy frame's frame may bear no relation to ours */
c906108c
SS
539
540 if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
541 if (fn_start == entry_point_address ())
c5aa993b 542 return 0; /* in _start fn, don't chain further */
c906108c
SS
543
544 framereg = fi->framereg;
545
546 /* If the caller is the startup code, we're at the end of the chain. */
547 if (find_pc_partial_function (callers_pc, 0, &fn_start, 0))
548 if (fn_start == entry_point_address ())
549 return 0;
550
c5aa993b 551 memset (&caller_fi, 0, sizeof (caller_fi));
c906108c 552 caller_fi.pc = callers_pc;
c5aa993b 553 fr30_scan_prologue (&caller_fi);
c906108c
SS
554 framereg = caller_fi.framereg;
555
556 /* If the caller used a frame register, return its value.
557 Otherwise, return the caller's stack pointer. */
558 if (framereg == FP_REGNUM)
559 return fr30_find_callers_reg (fi, framereg);
560 else
561 return fi->frame + fi->framesize;
562}
563
564/* Function: frame_saved_pc
565 Find the caller of this frame. We do this by seeing if RP_REGNUM
566 is saved in the stack anywhere, otherwise we get it from the
567 registers. If the inner frame is a dummy frame, return its PC
568 instead of RP, because that's where "caller" of the dummy-frame
569 will be found. */
570
571CORE_ADDR
fba45db2 572fr30_frame_saved_pc (struct frame_info *fi)
c906108c 573{
c5aa993b
JM
574 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
575 return generic_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
c906108c
SS
576 else
577 return fr30_find_callers_reg (fi, RP_REGNUM);
578}
579
580/* Function: fix_call_dummy
581 Pokes the callee function's address into the CALL_DUMMY assembly stub.
582 Assumes that the CALL_DUMMY looks like this:
c5aa993b
JM
583 jarl <offset24>, r31
584 trap
585 */
c906108c
SS
586
587int
fba45db2
KB
588fr30_fix_call_dummy (char *dummy, CORE_ADDR sp, CORE_ADDR fun, int nargs,
589 value_ptr *args, struct type *type, int gcc_p)
c906108c
SS
590{
591 long offset24;
592
593 offset24 = (long) fun - (long) entry_point_address ();
594 offset24 &= 0x3fffff;
595 offset24 |= 0xff800000; /* jarl <offset24>, r31 */
596
c5aa993b
JM
597 store_unsigned_integer ((unsigned int *) &dummy[2], 2, offset24 & 0xffff);
598 store_unsigned_integer ((unsigned int *) &dummy[0], 2, offset24 >> 16);
c906108c
SS
599 return 0;
600}
This page took 0.107402 seconds and 4 git commands to generate.