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