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