OSDN Git Service

2003-09-19 Andrew Cagney <cagney@redhat.com>
[pf3gnuchains/pf3gnuchains4x.git] / gdb / frv-tdep.c
1 /* Target-dependent code for the Fujitsu FR-V, for GDB, the GNU Debugger.
2    Copyright 2002, 2003 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 "gdb_string.h"
23 #include "inferior.h"
24 #include "symfile.h"            /* for entry_point_address */
25 #include "gdbcore.h"
26 #include "arch-utils.h"
27 #include "regcache.h"
28 #include "frame.h"
29 #include "frame-unwind.h"
30 #include "frame-base.h"
31 #include "trad-frame.h"
32 #include "dis-asm.h"
33
34 extern void _initialize_frv_tdep (void);
35
36 static gdbarch_init_ftype frv_gdbarch_init;
37
38 static gdbarch_register_name_ftype frv_register_name;
39 static gdbarch_breakpoint_from_pc_ftype frv_breakpoint_from_pc;
40 static gdbarch_skip_prologue_ftype frv_skip_prologue;
41 static gdbarch_deprecated_extract_return_value_ftype frv_extract_return_value;
42 static gdbarch_deprecated_extract_struct_value_address_ftype frv_extract_struct_value_address;
43 static gdbarch_frameless_function_invocation_ftype frv_frameless_function_invocation;
44 static gdbarch_deprecated_push_arguments_ftype frv_push_arguments;
45 static gdbarch_deprecated_saved_pc_after_call_ftype frv_saved_pc_after_call;
46
47 /* Register numbers.  You can change these as needed, but don't forget
48    to update the simulator accordingly.  */
49 enum {
50   /* The total number of registers we know exist.  */
51   frv_num_regs = 147,
52
53   /* Register numbers 0 -- 63 are always reserved for general-purpose
54      registers.  The chip at hand may have less.  */
55   first_gpr_regnum = 0,
56   sp_regnum = 1,
57   fp_regnum = 2,
58   struct_return_regnum = 3,
59   last_gpr_regnum = 63,
60
61   /* Register numbers 64 -- 127 are always reserved for floating-point
62      registers.  The chip at hand may have less.  */
63   first_fpr_regnum = 64,
64   last_fpr_regnum = 127,
65
66   /* Register numbers 128 on up are always reserved for special-purpose
67      registers.  */
68   first_spr_regnum = 128,
69   pc_regnum = 128,
70   psr_regnum = 129,
71   ccr_regnum = 130,
72   cccr_regnum = 131,
73   tbr_regnum = 135,
74   brr_regnum = 136,
75   dbar0_regnum = 137,
76   dbar1_regnum = 138,
77   dbar2_regnum = 139,
78   dbar3_regnum = 140,
79   lr_regnum = 145,
80   lcr_regnum = 146,
81   last_spr_regnum = 146
82 };
83
84 static LONGEST frv_call_dummy_words[] =
85 {0};
86
87
88 struct frv_unwind_cache         /* was struct frame_extra_info */
89   {
90     /* The previous frame's inner-most stack address.  Used as this
91        frame ID's stack_addr.  */
92     CORE_ADDR prev_sp;
93
94     /* The frame's base, optionally used by the high-level debug info.  */
95     CORE_ADDR base;
96
97     /* Table indicating the location of each and every register.  */
98     struct trad_frame_saved_reg *saved_regs;
99   };
100
101
102 /* A structure describing a particular variant of the FRV.
103    We allocate and initialize one of these structures when we create
104    the gdbarch object for a variant.
105
106    At the moment, all the FR variants we support differ only in which
107    registers are present; the portable code of GDB knows that
108    registers whose names are the empty string don't exist, so the
109    `register_names' array captures all the per-variant information we
110    need.
111
112    in the future, if we need to have per-variant maps for raw size,
113    virtual type, etc., we should replace register_names with an array
114    of structures, each of which gives all the necessary info for one
115    register.  Don't stick parallel arrays in here --- that's so
116    Fortran.  */
117 struct gdbarch_tdep
118 {
119   /* How many general-purpose registers does this variant have?  */
120   int num_gprs;
121
122   /* How many floating-point registers does this variant have?  */
123   int num_fprs;
124
125   /* How many hardware watchpoints can it support?  */
126   int num_hw_watchpoints;
127
128   /* How many hardware breakpoints can it support?  */
129   int num_hw_breakpoints;
130
131   /* Register names.  */
132   char **register_names;
133 };
134
135 #define CURRENT_VARIANT (gdbarch_tdep (current_gdbarch))
136
137
138 /* Allocate a new variant structure, and set up default values for all
139    the fields.  */
140 static struct gdbarch_tdep *
141 new_variant (void)
142 {
143   struct gdbarch_tdep *var;
144   int r;
145   char buf[20];
146
147   var = xmalloc (sizeof (*var));
148   memset (var, 0, sizeof (*var));
149   
150   var->num_gprs = 64;
151   var->num_fprs = 64;
152   var->num_hw_watchpoints = 0;
153   var->num_hw_breakpoints = 0;
154
155   /* By default, don't supply any general-purpose or floating-point
156      register names.  */
157   var->register_names = (char **) xmalloc (frv_num_regs * sizeof (char *));
158   for (r = 0; r < frv_num_regs; r++)
159     var->register_names[r] = "";
160
161   /* Do, however, supply default names for the special-purpose
162      registers.  */
163   for (r = first_spr_regnum; r <= last_spr_regnum; ++r)
164     {
165       sprintf (buf, "x%d", r);
166       var->register_names[r] = xstrdup (buf);
167     }
168
169   var->register_names[pc_regnum] = "pc";
170   var->register_names[lr_regnum] = "lr";
171   var->register_names[lcr_regnum] = "lcr";
172      
173   var->register_names[psr_regnum] = "psr";
174   var->register_names[ccr_regnum] = "ccr";
175   var->register_names[cccr_regnum] = "cccr";
176   var->register_names[tbr_regnum] = "tbr";
177
178   /* Debug registers.  */
179   var->register_names[brr_regnum] = "brr";
180   var->register_names[dbar0_regnum] = "dbar0";
181   var->register_names[dbar1_regnum] = "dbar1";
182   var->register_names[dbar2_regnum] = "dbar2";
183   var->register_names[dbar3_regnum] = "dbar3";
184
185   return var;
186 }
187
188
189 /* Indicate that the variant VAR has NUM_GPRS general-purpose
190    registers, and fill in the names array appropriately.  */
191 static void
192 set_variant_num_gprs (struct gdbarch_tdep *var, int num_gprs)
193 {
194   int r;
195
196   var->num_gprs = num_gprs;
197
198   for (r = 0; r < num_gprs; ++r)
199     {
200       char buf[20];
201
202       sprintf (buf, "gr%d", r);
203       var->register_names[first_gpr_regnum + r] = xstrdup (buf);
204     }
205 }
206
207
208 /* Indicate that the variant VAR has NUM_FPRS floating-point
209    registers, and fill in the names array appropriately.  */
210 static void
211 set_variant_num_fprs (struct gdbarch_tdep *var, int num_fprs)
212 {
213   int r;
214
215   var->num_fprs = num_fprs;
216
217   for (r = 0; r < num_fprs; ++r)
218     {
219       char buf[20];
220
221       sprintf (buf, "fr%d", r);
222       var->register_names[first_fpr_regnum + r] = xstrdup (buf);
223     }
224 }
225
226
227 static const char *
228 frv_register_name (int reg)
229 {
230   if (reg < 0)
231     return "?toosmall?";
232   if (reg >= frv_num_regs)
233     return "?toolarge?";
234
235   return CURRENT_VARIANT->register_names[reg];
236 }
237
238
239 static int
240 frv_register_raw_size (int reg)
241 {
242   return 4;
243 }
244
245 static int
246 frv_register_virtual_size (int reg)
247 {
248   return 4;
249 }
250
251 static struct type *
252 frv_register_virtual_type (int reg)
253 {
254   if (reg >= 64 && reg <= 127)
255     return builtin_type_float;
256   else
257     return builtin_type_int;
258 }
259
260 static int
261 frv_register_byte (int reg)
262 {
263   return (reg * 4);
264 }
265
266 static const unsigned char *
267 frv_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenp)
268 {
269   static unsigned char breakpoint[] = {0xc0, 0x70, 0x00, 0x01};
270   *lenp = sizeof (breakpoint);
271   return breakpoint;
272 }
273
274
275 /* Return true if REG is a caller-saves ("scratch") register,
276    false otherwise.  */
277 static int
278 is_caller_saves_reg (int reg)
279 {
280   return ((4 <= reg && reg <= 7)
281           || (14 <= reg && reg <= 15)
282           || (32 <= reg && reg <= 47));
283 }
284
285
286 /* Return true if REG is a callee-saves register, false otherwise.  */
287 static int
288 is_callee_saves_reg (int reg)
289 {
290   return ((16 <= reg && reg <= 31)
291           || (48 <= reg && reg <= 63));
292 }
293
294
295 /* Return true if REG is an argument register, false otherwise.  */
296 static int
297 is_argument_reg (int reg)
298 {
299   return (8 <= reg && reg <= 13);
300 }
301
302
303 /* Scan an FR-V prologue, starting at PC, until frame->PC.
304    If FRAME is non-zero, fill in its saved_regs with appropriate addresses.
305    We assume FRAME's saved_regs array has already been allocated and cleared.
306    Return the first PC value after the prologue.
307
308    Note that, for unoptimized code, we almost don't need this function
309    at all; all arguments and locals live on the stack, so we just need
310    the FP to find everything.  The catch: structures passed by value
311    have their addresses living in registers; they're never spilled to
312    the stack.  So if you ever want to be able to get to these
313    arguments in any frame but the top, you'll need to do this serious
314    prologue analysis.  */
315 static CORE_ADDR
316 frv_analyze_prologue (CORE_ADDR pc, struct frame_info *next_frame,
317                       struct frv_unwind_cache *info)
318 {
319   /* When writing out instruction bitpatterns, we use the following
320      letters to label instruction fields:
321      P - The parallel bit.  We don't use this.
322      J - The register number of GRj in the instruction description.
323      K - The register number of GRk in the instruction description.
324      I - The register number of GRi.
325      S - a signed imediate offset.
326      U - an unsigned immediate offset.
327
328      The dots below the numbers indicate where hex digit boundaries
329      fall, to make it easier to check the numbers.  */
330
331   /* Non-zero iff we've seen the instruction that initializes the
332      frame pointer for this function's frame.  */
333   int fp_set = 0;
334
335   /* If fp_set is non_zero, then this is the distance from
336      the stack pointer to frame pointer: fp = sp + fp_offset.  */
337   int fp_offset = 0;
338
339   /* Total size of frame prior to any alloca operations. */
340   int framesize = 0;
341
342   /* Flag indicating if lr has been saved on the stack.  */
343   int lr_saved_on_stack = 0;
344
345   /* The number of the general-purpose register we saved the return
346      address ("link register") in, or -1 if we haven't moved it yet.  */
347   int lr_save_reg = -1;
348
349   /* Offset (from sp) at which lr has been saved on the stack.  */
350
351   int lr_sp_offset = 0;
352
353   /* If gr_saved[i] is non-zero, then we've noticed that general
354      register i has been saved at gr_sp_offset[i] from the stack
355      pointer.  */
356   char gr_saved[64];
357   int gr_sp_offset[64];
358
359   memset (gr_saved, 0, sizeof (gr_saved));
360
361   while (! next_frame || pc < frame_pc_unwind (next_frame))
362     {
363       LONGEST op = read_memory_integer (pc, 4);
364
365       /* The tests in this chain of ifs should be in order of
366          decreasing selectivity, so that more particular patterns get
367          to fire before less particular patterns.  */
368
369       /* Setting the FP from the SP:
370          ori sp, 0, fp
371          P 000010 0100010 000001 000000000000 = 0x04881000
372          0 111111 1111111 111111 111111111111 = 0x7fffffff
373              .    .   .    .   .    .   .   .
374          We treat this as part of the prologue.  */
375       if ((op & 0x7fffffff) == 0x04881000)
376         {
377           fp_set = 1;
378           fp_offset = 0;
379         }
380
381       /* Move the link register to the scratch register grJ, before saving:
382          movsg lr, grJ
383          P 000100 0000011 010000 000111 JJJJJJ = 0x080d01c0
384          0 111111 1111111 111111 111111 000000 = 0x7fffffc0
385              .    .   .    .   .    .    .   .
386          We treat this as part of the prologue.  */
387       else if ((op & 0x7fffffc0) == 0x080d01c0)
388         {
389           int gr_j = op & 0x3f;
390
391           /* If we're moving it to a scratch register, that's fine.  */
392           if (is_caller_saves_reg (gr_j))
393             lr_save_reg = gr_j;
394           /* Otherwise it's not a prologue instruction that we
395              recognize.  */
396           else
397             break;
398         }
399
400       /* To save multiple callee-saves registers on the stack, at
401          offset zero:
402
403          std grK,@(sp,gr0)
404          P KKKKKK 0000011 000001 000011 000000 = 0x000c10c0
405          0 000000 1111111 111111 111111 111111 = 0x01ffffff
406
407          stq grK,@(sp,gr0)
408          P KKKKKK 0000011 000001 000100 000000 = 0x000c1100
409          0 000000 1111111 111111 111111 111111 = 0x01ffffff
410              .    .   .    .   .    .    .   .
411          We treat this as part of the prologue, and record the register's
412          saved address in the frame structure.  */
413       else if ((op & 0x01ffffff) == 0x000c10c0
414             || (op & 0x01ffffff) == 0x000c1100)
415         {
416           int gr_k = ((op >> 25) & 0x3f);
417           int ope  = ((op >> 6)  & 0x3f);
418           int count;
419           int i;
420
421           /* Is it an std or an stq?  */
422           if (ope == 0x03)
423             count = 2;
424           else
425             count = 4;
426
427           /* Is it really a callee-saves register?  */
428           if (is_callee_saves_reg (gr_k))
429             {
430               for (i = 0; i < count; i++)
431                 {
432                   gr_saved[gr_k + i] = 1;
433                   gr_sp_offset[gr_k + i] = 4 * i;
434                 }
435             }
436           else
437             /* It's not a prologue instruction.  */
438             break;
439         }
440
441       /* Adjusting the stack pointer.  (The stack pointer is GR1.)
442          addi sp, S, sp
443          P 000001 0010000 000001 SSSSSSSSSSSS = 0x02401000
444          0 111111 1111111 111111 000000000000 = 0x7ffff000
445              .    .   .    .   .    .   .   .
446          We treat this as part of the prologue.  */
447       else if ((op & 0x7ffff000) == 0x02401000)
448         {
449           /* Sign-extend the twelve-bit field.
450              (Isn't there a better way to do this?)  */
451           int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
452
453           framesize -= s;
454         }
455
456       /* Setting the FP to a constant distance from the SP:
457          addi sp, S, fp
458          P 000010 0010000 000001 SSSSSSSSSSSS = 0x04401000
459          0 111111 1111111 111111 000000000000 = 0x7ffff000
460              .    .   .    .   .    .   .   .
461          We treat this as part of the prologue.  */
462       else if ((op & 0x7ffff000) == 0x04401000)
463         {
464           /* Sign-extend the twelve-bit field.
465              (Isn't there a better way to do this?)  */
466           int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
467           fp_set = 1;
468           fp_offset = s;
469         }
470
471       /* To spill an argument register to a scratch register:
472             ori GRi, 0, GRk
473          P KKKKKK 0100010 IIIIII 000000000000 = 0x00880000
474          0 000000 1111111 000000 111111111111 = 0x01fc0fff
475              .    .   .    .   .    .   .   .
476          For the time being, we treat this as a prologue instruction,
477          assuming that GRi is an argument register.  This one's kind
478          of suspicious, because it seems like it could be part of a
479          legitimate body instruction.  But we only come here when the
480          source info wasn't helpful, so we have to do the best we can.
481          Hopefully once GCC and GDB agree on how to emit line number
482          info for prologues, then this code will never come into play.  */
483       else if ((op & 0x01fc0fff) == 0x00880000)
484         {
485           int gr_i = ((op >> 12) & 0x3f);
486
487           /* If the source isn't an arg register, then this isn't a
488              prologue instruction.  */
489           if (! is_argument_reg (gr_i))
490             break;
491         }
492
493       /* To spill 16-bit values to the stack:
494              sthi GRk, @(fp, s)
495          P KKKKKK 1010001 000010 SSSSSSSSSSSS = 0x01442000
496          0 000000 1111111 111111 000000000000 = 0x01fff000
497              .    .   .    .   .    .   .   . 
498          And for 8-bit values, we use STB instructions.
499              stbi GRk, @(fp, s)
500          P KKKKKK 1010000 000010 SSSSSSSSSSSS = 0x01402000
501          0 000000 1111111 111111 000000000000 = 0x01fff000
502              .    .   .    .   .    .   .   .
503          We check that GRk is really an argument register, and treat
504          all such as part of the prologue.  */
505       else if (   (op & 0x01fff000) == 0x01442000
506                || (op & 0x01fff000) == 0x01402000)
507         {
508           int gr_k = ((op >> 25) & 0x3f);
509
510           if (! is_argument_reg (gr_k))
511             break;              /* Source isn't an arg register.  */
512         }
513
514       /* To save multiple callee-saves register on the stack, at a
515          non-zero offset:
516
517          stdi GRk, @(sp, s)
518          P KKKKKK 1010011 000001 SSSSSSSSSSSS = 0x014c1000
519          0 000000 1111111 111111 000000000000 = 0x01fff000
520              .    .   .    .   .    .   .   .
521          stqi GRk, @(sp, s)
522          P KKKKKK 1010100 000001 SSSSSSSSSSSS = 0x01501000
523          0 000000 1111111 111111 000000000000 = 0x01fff000
524              .    .   .    .   .    .   .   .
525          We treat this as part of the prologue, and record the register's
526          saved address in the frame structure.  */
527       else if ((op & 0x01fff000) == 0x014c1000
528             || (op & 0x01fff000) == 0x01501000)
529         {
530           int gr_k = ((op >> 25) & 0x3f);
531           int count;
532           int i;
533
534           /* Is it a stdi or a stqi?  */
535           if ((op & 0x01fff000) == 0x014c1000)
536             count = 2;
537           else
538             count = 4;
539
540           /* Is it really a callee-saves register?  */
541           if (is_callee_saves_reg (gr_k))
542             {
543               /* Sign-extend the twelve-bit field.
544                  (Isn't there a better way to do this?)  */
545               int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
546
547               for (i = 0; i < count; i++)
548                 {
549                   gr_saved[gr_k + i] = 1;
550                   gr_sp_offset[gr_k + i] = s + (4 * i);
551                 }
552             }
553           else
554             /* It's not a prologue instruction.  */
555             break;
556         }
557
558       /* Storing any kind of integer register at any constant offset
559          from any other register.
560
561          st GRk, @(GRi, gr0)
562          P KKKKKK 0000011 IIIIII 000010 000000 = 0x000c0080
563          0 000000 1111111 000000 111111 111111 = 0x01fc0fff
564              .    .   .    .   .    .    .   .
565          sti GRk, @(GRi, d12)
566          P KKKKKK 1010010 IIIIII SSSSSSSSSSSS = 0x01480000
567          0 000000 1111111 000000 000000000000 = 0x01fc0000
568              .    .   .    .   .    .   .   .
569          These could be almost anything, but a lot of prologue
570          instructions fall into this pattern, so let's decode the
571          instruction once, and then work at a higher level.  */
572       else if (((op & 0x01fc0fff) == 0x000c0080)
573             || ((op & 0x01fc0000) == 0x01480000))
574         {
575           int gr_k = ((op >> 25) & 0x3f);
576           int gr_i = ((op >> 12) & 0x3f);
577           int offset;
578
579           /* Are we storing with gr0 as an offset, or using an
580              immediate value?  */
581           if ((op & 0x01fc0fff) == 0x000c0080)
582             offset = 0;
583           else
584             offset = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
585
586           /* If the address isn't relative to the SP or FP, it's not a
587              prologue instruction.  */
588           if (gr_i != sp_regnum && gr_i != fp_regnum)
589             break;
590
591           /* Saving the old FP in the new frame (relative to the SP).  */
592           if (gr_k == fp_regnum && gr_i == sp_regnum)
593             {
594               gr_saved[fp_regnum] = 1;
595               gr_sp_offset[fp_regnum] = offset;
596             }
597
598           /* Saving callee-saves register(s) on the stack, relative to
599              the SP.  */
600           else if (gr_i == sp_regnum
601                    && is_callee_saves_reg (gr_k))
602             {
603               gr_saved[gr_k] = 1;
604               if (gr_i == sp_regnum)
605                 gr_sp_offset[gr_k] = offset;
606               else
607                 gr_sp_offset[gr_k] = offset + fp_offset;
608             }
609
610           /* Saving the scratch register holding the return address.  */
611           else if (lr_save_reg != -1
612                    && gr_k == lr_save_reg)
613             {
614               lr_saved_on_stack = 1;
615               if (gr_i == sp_regnum)
616                 lr_sp_offset = offset;
617               else
618                 lr_sp_offset = offset + fp_offset;
619             }
620
621           /* Spilling int-sized arguments to the stack.  */
622           else if (is_argument_reg (gr_k))
623             ;
624
625           /* It's not a store instruction we recognize, so this must
626              be the end of the prologue.  */
627           else
628             break;
629         }
630
631       /* It's not any instruction we recognize, so this must be the end
632          of the prologue.  */
633       else
634         break;
635
636       pc += 4;
637     }
638
639   if (next_frame && info)
640     {
641       int i;
642       ULONGEST this_base;
643
644       /* If we know the relationship between the stack and frame
645          pointers, record the addresses of the registers we noticed.
646          Note that we have to do this as a separate step at the end,
647          because instructions may save relative to the SP, but we need
648          their addresses relative to the FP.  */
649       if (fp_set)
650           frame_unwind_unsigned_register (next_frame, fp_regnum, &this_base);
651       else
652           frame_unwind_unsigned_register (next_frame, sp_regnum, &this_base);
653
654       for (i = 0; i < 64; i++)
655         if (gr_saved[i])
656           info->saved_regs[i].addr = this_base - fp_offset + gr_sp_offset[i];
657
658       info->prev_sp = this_base - fp_offset + framesize;
659       info->base = this_base;
660
661       /* If LR was saved on the stack, record its location.  */
662       if (lr_saved_on_stack)
663         info->saved_regs[lr_regnum].addr = this_base - fp_offset + lr_sp_offset;
664
665       /* The call instruction moves the caller's PC in the callee's LR.
666          Since this is an unwind, do the reverse.  Copy the location of LR
667          into PC (the address / regnum) so that a request for PC will be
668          converted into a request for the LR.  */
669       info->saved_regs[pc_regnum] = info->saved_regs[lr_regnum];
670
671       /* Save the previous frame's computed SP value.  */
672       trad_frame_set_value (info->saved_regs, sp_regnum, info->prev_sp);
673     }
674
675   return pc;
676 }
677
678
679 static CORE_ADDR
680 frv_skip_prologue (CORE_ADDR pc)
681 {
682   CORE_ADDR func_addr, func_end, new_pc;
683
684   new_pc = pc;
685
686   /* If the line table has entry for a line *within* the function
687      (i.e., not in the prologue, and not past the end), then that's
688      our location.  */
689   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
690     {
691       struct symtab_and_line sal;
692
693       sal = find_pc_line (func_addr, 0);
694
695       if (sal.line != 0 && sal.end < func_end)
696         {
697           new_pc = sal.end;
698         }
699     }
700
701   /* The FR-V prologue is at least five instructions long (twenty bytes).
702      If we didn't find a real source location past that, then
703      do a full analysis of the prologue.  */
704   if (new_pc < pc + 20)
705     new_pc = frv_analyze_prologue (pc, 0, 0);
706
707   return new_pc;
708 }
709
710
711 static struct frv_unwind_cache *
712 frv_frame_unwind_cache (struct frame_info *next_frame,
713                          void **this_prologue_cache)
714 {
715   struct gdbarch *gdbarch = get_frame_arch (next_frame);
716   CORE_ADDR pc;
717   ULONGEST prev_sp;
718   ULONGEST this_base;
719   struct frv_unwind_cache *info;
720
721   if ((*this_prologue_cache))
722     return (*this_prologue_cache);
723
724   info = FRAME_OBSTACK_ZALLOC (struct frv_unwind_cache);
725   (*this_prologue_cache) = info;
726   info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
727
728   /* Prologue analysis does the rest...  */
729   frv_analyze_prologue (frame_func_unwind (next_frame), next_frame, info);
730
731   return info;
732 }
733
734 static void
735 frv_extract_return_value (struct type *type, char *regbuf, char *valbuf)
736 {
737   memcpy (valbuf, (regbuf
738                    + frv_register_byte (8)
739                    + (TYPE_LENGTH (type) < 4 ? 4 - TYPE_LENGTH (type) : 0)),
740                    TYPE_LENGTH (type));
741 }
742
743 static CORE_ADDR
744 frv_extract_struct_value_address (char *regbuf)
745 {
746   return extract_unsigned_integer (regbuf + 
747                                    frv_register_byte (struct_return_regnum),
748                                    4);
749 }
750
751 static void
752 frv_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
753 {
754   write_register (struct_return_regnum, addr);
755 }
756
757 static int
758 frv_frameless_function_invocation (struct frame_info *frame)
759 {
760   return frameless_look_for_prologue (frame);
761 }
762
763 static CORE_ADDR
764 frv_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
765 {
766   /* Require dword alignment.  */
767   return align_down (sp, 8);
768 }
769
770 static CORE_ADDR
771 frv_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
772                      struct regcache *regcache, CORE_ADDR bp_addr,
773                      int nargs, struct value **args, CORE_ADDR sp,
774                      int struct_return, CORE_ADDR struct_addr)
775 {
776   int argreg;
777   int argnum;
778   char *val;
779   char valbuf[4];
780   struct value *arg;
781   struct type *arg_type;
782   int len;
783   enum type_code typecode;
784   CORE_ADDR regval;
785   int stack_space;
786   int stack_offset;
787
788 #if 0
789   printf("Push %d args at sp = %x, struct_return=%d (%x)\n",
790          nargs, (int) sp, struct_return, struct_addr);
791 #endif
792
793   stack_space = 0;
794   for (argnum = 0; argnum < nargs; ++argnum)
795     stack_space += align_up (TYPE_LENGTH (VALUE_TYPE (args[argnum])), 4);
796
797   stack_space -= (6 * 4);
798   if (stack_space > 0)
799     sp -= stack_space;
800
801   /* Make sure stack is dword aligned. */
802   sp = align_down (sp, 8);
803
804   stack_offset = 0;
805
806   argreg = 8;
807
808   if (struct_return)
809     regcache_cooked_write_unsigned (regcache, struct_return_regnum,
810                                     struct_addr);
811
812   for (argnum = 0; argnum < nargs; ++argnum)
813     {
814       arg = args[argnum];
815       arg_type = check_typedef (VALUE_TYPE (arg));
816       len = TYPE_LENGTH (arg_type);
817       typecode = TYPE_CODE (arg_type);
818
819       if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
820         {
821           store_unsigned_integer (valbuf, 4, VALUE_ADDRESS (arg));
822           typecode = TYPE_CODE_PTR;
823           len = 4;
824           val = valbuf;
825         }
826       else
827         {
828           val = (char *) VALUE_CONTENTS (arg);
829         }
830
831       while (len > 0)
832         {
833           int partial_len = (len < 4 ? len : 4);
834
835           if (argreg < 14)
836             {
837               regval = extract_unsigned_integer (val, partial_len);
838 #if 0
839               printf("  Argnum %d data %x -> reg %d\n",
840                      argnum, (int) regval, argreg);
841 #endif
842               regcache_cooked_write_unsigned (regcache, argreg, regval);
843               ++argreg;
844             }
845           else
846             {
847 #if 0
848               printf("  Argnum %d data %x -> offset %d (%x)\n",
849                      argnum, *((int *)val), stack_offset, (int) (sp + stack_offset));
850 #endif
851               write_memory (sp + stack_offset, val, partial_len);
852               stack_offset += align_up (partial_len, 4);
853             }
854           len -= partial_len;
855           val += partial_len;
856         }
857     }
858
859   /* Set the return address.  For the frv, the return breakpoint is
860      always at BP_ADDR.  */
861   regcache_cooked_write_unsigned (regcache, lr_regnum, bp_addr);
862
863   /* Finally, update the SP register.  */
864   regcache_cooked_write_unsigned (regcache, sp_regnum, sp);
865
866   return sp;
867 }
868
869 static void
870 frv_store_return_value (struct type *type, char *valbuf)
871 {
872   int length = TYPE_LENGTH (type);
873   int reg8_offset = frv_register_byte (8);
874
875   if (length <= 4)
876     deprecated_write_register_bytes (reg8_offset + (4 - length), valbuf,
877                                      length);
878   else if (length == 8)
879     deprecated_write_register_bytes (reg8_offset, valbuf, length);
880   else
881     internal_error (__FILE__, __LINE__,
882                     "Don't know how to return a %d-byte value.", length);
883 }
884
885
886 /* Hardware watchpoint / breakpoint support for the FR500
887    and FR400.  */
888
889 int
890 frv_check_watch_resources (int type, int cnt, int ot)
891 {
892   struct gdbarch_tdep *var = CURRENT_VARIANT;
893
894   /* Watchpoints not supported on simulator.  */
895   if (strcmp (target_shortname, "sim") == 0)
896     return 0;
897
898   if (type == bp_hardware_breakpoint)
899     {
900       if (var->num_hw_breakpoints == 0)
901         return 0;
902       else if (cnt <= var->num_hw_breakpoints)
903         return 1;
904     }
905   else
906     {
907       if (var->num_hw_watchpoints == 0)
908         return 0;
909       else if (ot)
910         return -1;
911       else if (cnt <= var->num_hw_watchpoints)
912         return 1;
913     }
914   return -1;
915 }
916
917
918 CORE_ADDR
919 frv_stopped_data_address (void)
920 {
921   CORE_ADDR brr, dbar0, dbar1, dbar2, dbar3;
922
923   brr = read_register (brr_regnum);
924   dbar0 = read_register (dbar0_regnum);
925   dbar1 = read_register (dbar1_regnum);
926   dbar2 = read_register (dbar2_regnum);
927   dbar3 = read_register (dbar3_regnum);
928
929   if (brr & (1<<11))
930     return dbar0;
931   else if (brr & (1<<10))
932     return dbar1;
933   else if (brr & (1<<9))
934     return dbar2;
935   else if (brr & (1<<8))
936     return dbar3;
937   else
938     return 0;
939 }
940
941 static CORE_ADDR
942 frv_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
943 {
944   return frame_unwind_register_unsigned (next_frame, pc_regnum);
945 }
946
947 /* Given a GDB frame, determine the address of the calling function's
948    frame.  This will be used to create a new GDB frame struct.  */
949
950 static void
951 frv_frame_this_id (struct frame_info *next_frame,
952                     void **this_prologue_cache, struct frame_id *this_id)
953 {
954   struct frv_unwind_cache *info
955     = frv_frame_unwind_cache (next_frame, this_prologue_cache);
956   CORE_ADDR base;
957   CORE_ADDR func;
958   struct minimal_symbol *msym_stack;
959   struct frame_id id;
960
961   /* The FUNC is easy.  */
962   func = frame_func_unwind (next_frame);
963
964   /* This is meant to halt the backtrace at "_start".  Make sure we
965      don't halt it at a generic dummy frame. */
966   if (deprecated_inside_entry_file (func))
967     return;
968
969   /* Check if the stack is empty.  */
970   msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
971   if (msym_stack && info->base == SYMBOL_VALUE_ADDRESS (msym_stack))
972     return;
973
974   /* Hopefully the prologue analysis either correctly determined the
975      frame's base (which is the SP from the previous frame), or set
976      that base to "NULL".  */
977   base = info->prev_sp;
978   if (base == 0)
979     return;
980
981   id = frame_id_build (base, func);
982
983   /* Check that we're not going round in circles with the same frame
984      ID (but avoid applying the test to sentinel frames which do go
985      round in circles).  Can't use frame_id_eq() as that doesn't yet
986      compare the frame's PC value.  */
987   if (frame_relative_level (next_frame) >= 0
988       && get_frame_type (next_frame) != DUMMY_FRAME
989       && frame_id_eq (get_frame_id (next_frame), id))
990     return;
991
992   (*this_id) = id;
993 }
994
995 static void
996 frv_frame_prev_register (struct frame_info *next_frame,
997                           void **this_prologue_cache,
998                           int regnum, int *optimizedp,
999                           enum lval_type *lvalp, CORE_ADDR *addrp,
1000                           int *realnump, void *bufferp)
1001 {
1002   struct frv_unwind_cache *info
1003     = frv_frame_unwind_cache (next_frame, this_prologue_cache);
1004   trad_frame_prev_register (next_frame, info->saved_regs, regnum,
1005                             optimizedp, lvalp, addrp, realnump, bufferp);
1006 }
1007
1008 static const struct frame_unwind frv_frame_unwind = {
1009   NORMAL_FRAME,
1010   frv_frame_this_id,
1011   frv_frame_prev_register
1012 };
1013
1014 static const struct frame_unwind *
1015 frv_frame_sniffer (struct frame_info *next_frame)
1016 {
1017   return &frv_frame_unwind;
1018 }
1019
1020 static CORE_ADDR
1021 frv_frame_base_address (struct frame_info *next_frame, void **this_cache)
1022 {
1023   struct frv_unwind_cache *info
1024     = frv_frame_unwind_cache (next_frame, this_cache);
1025   return info->base;
1026 }
1027
1028 static const struct frame_base frv_frame_base = {
1029   &frv_frame_unwind,
1030   frv_frame_base_address,
1031   frv_frame_base_address,
1032   frv_frame_base_address
1033 };
1034
1035 static CORE_ADDR
1036 frv_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1037 {
1038   return frame_unwind_register_unsigned (next_frame, sp_regnum);
1039 }
1040
1041
1042 /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
1043    dummy frame.  The frame ID's base needs to match the TOS value
1044    saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1045    breakpoint.  */
1046
1047 static struct frame_id
1048 frv_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1049 {
1050   return frame_id_build (frv_unwind_sp (gdbarch, next_frame),
1051                          frame_pc_unwind (next_frame));
1052 }
1053
1054
1055 static struct gdbarch *
1056 frv_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1057 {
1058   struct gdbarch *gdbarch;
1059   struct gdbarch_tdep *var;
1060
1061   /* Check to see if we've already built an appropriate architecture
1062      object for this executable.  */
1063   arches = gdbarch_list_lookup_by_info (arches, &info);
1064   if (arches)
1065     return arches->gdbarch;
1066
1067   /* Select the right tdep structure for this variant.  */
1068   var = new_variant ();
1069   switch (info.bfd_arch_info->mach)
1070     {
1071     case bfd_mach_frv:
1072     case bfd_mach_frvsimple:
1073     case bfd_mach_fr500:
1074     case bfd_mach_frvtomcat:
1075       set_variant_num_gprs (var, 64);
1076       set_variant_num_fprs (var, 64);
1077       break;
1078
1079     case bfd_mach_fr400:
1080       set_variant_num_gprs (var, 32);
1081       set_variant_num_fprs (var, 32);
1082       break;
1083
1084     default:
1085       /* Never heard of this variant.  */
1086       return 0;
1087     }
1088   
1089   gdbarch = gdbarch_alloc (&info, var);
1090
1091   set_gdbarch_short_bit (gdbarch, 16);
1092   set_gdbarch_int_bit (gdbarch, 32);
1093   set_gdbarch_long_bit (gdbarch, 32);
1094   set_gdbarch_long_long_bit (gdbarch, 64);
1095   set_gdbarch_float_bit (gdbarch, 32);
1096   set_gdbarch_double_bit (gdbarch, 64);
1097   set_gdbarch_long_double_bit (gdbarch, 64);
1098   set_gdbarch_ptr_bit (gdbarch, 32);
1099
1100   set_gdbarch_num_regs (gdbarch, frv_num_regs);
1101   set_gdbarch_sp_regnum (gdbarch, sp_regnum);
1102   set_gdbarch_deprecated_fp_regnum (gdbarch, fp_regnum);
1103   set_gdbarch_pc_regnum (gdbarch, pc_regnum);
1104
1105   set_gdbarch_register_name (gdbarch, frv_register_name);
1106   set_gdbarch_deprecated_register_size (gdbarch, 4);
1107   set_gdbarch_deprecated_register_bytes (gdbarch, frv_num_regs * 4);
1108   set_gdbarch_deprecated_register_byte (gdbarch, frv_register_byte);
1109   set_gdbarch_deprecated_register_raw_size (gdbarch, frv_register_raw_size);
1110   set_gdbarch_deprecated_max_register_raw_size (gdbarch, 4);
1111   set_gdbarch_deprecated_register_virtual_size (gdbarch, frv_register_virtual_size);
1112   set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 4);
1113   set_gdbarch_deprecated_register_virtual_type (gdbarch, frv_register_virtual_type);
1114
1115   set_gdbarch_skip_prologue (gdbarch, frv_skip_prologue);
1116   set_gdbarch_breakpoint_from_pc (gdbarch, frv_breakpoint_from_pc);
1117
1118   set_gdbarch_frame_args_skip (gdbarch, 0);
1119   set_gdbarch_frameless_function_invocation (gdbarch, frv_frameless_function_invocation);
1120
1121   set_gdbarch_use_struct_convention (gdbarch, always_use_struct_convention);
1122   set_gdbarch_deprecated_extract_return_value (gdbarch, frv_extract_return_value);
1123
1124   set_gdbarch_deprecated_store_struct_return (gdbarch, frv_store_struct_return);
1125   set_gdbarch_deprecated_store_return_value (gdbarch, frv_store_return_value);
1126   set_gdbarch_deprecated_extract_struct_value_address (gdbarch, frv_extract_struct_value_address);
1127
1128   /* Frame stuff.  */
1129   set_gdbarch_unwind_pc (gdbarch, frv_unwind_pc);
1130   set_gdbarch_unwind_sp (gdbarch, frv_unwind_sp);
1131   set_gdbarch_frame_align (gdbarch, frv_frame_align);
1132   frame_unwind_append_sniffer (gdbarch, frv_frame_sniffer);
1133   frame_base_set_default (gdbarch, &frv_frame_base);
1134
1135   /* Settings for calling functions in the inferior.  */
1136   set_gdbarch_push_dummy_call (gdbarch, frv_push_dummy_call);
1137   set_gdbarch_unwind_dummy_id (gdbarch, frv_unwind_dummy_id);
1138
1139   /* Settings that should be unnecessary.  */
1140   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1141
1142   set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
1143
1144   set_gdbarch_decr_pc_after_break (gdbarch, 0);
1145   set_gdbarch_function_start_offset (gdbarch, 0);
1146
1147   set_gdbarch_remote_translate_xfer_address
1148     (gdbarch, generic_remote_translate_xfer_address);
1149
1150   /* Hardware watchpoint / breakpoint support.  */
1151   switch (info.bfd_arch_info->mach)
1152     {
1153     case bfd_mach_frv:
1154     case bfd_mach_frvsimple:
1155     case bfd_mach_fr500:
1156     case bfd_mach_frvtomcat:
1157       /* fr500-style hardware debugging support.  */
1158       var->num_hw_watchpoints = 4;
1159       var->num_hw_breakpoints = 4;
1160       break;
1161
1162     case bfd_mach_fr400:
1163       /* fr400-style hardware debugging support.  */
1164       var->num_hw_watchpoints = 2;
1165       var->num_hw_breakpoints = 4;
1166       break;
1167
1168     default:
1169       /* Otherwise, assume we don't have hardware debugging support.  */
1170       var->num_hw_watchpoints = 0;
1171       var->num_hw_breakpoints = 0;
1172       break;
1173     }
1174
1175   set_gdbarch_print_insn (gdbarch, print_insn_frv);
1176
1177   return gdbarch;
1178 }
1179
1180 void
1181 _initialize_frv_tdep (void)
1182 {
1183   register_gdbarch_init (bfd_arch_frv, frv_gdbarch_init);
1184 }