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