* frv-tdep.c (dis-asm.h): Include.
[external/binutils.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 #define ROUND_UP(n,a) (((n)+(a)-1) & ~((a)-1))
764 #define ROUND_DOWN(n,a) ((n) & ~((a)-1))
765
766 static CORE_ADDR
767 frv_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
768 {
769   /* Require dword alignment.  */
770   return ROUND_DOWN (sp, 8);
771 }
772
773 static CORE_ADDR
774 frv_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
775                      struct regcache *regcache, CORE_ADDR bp_addr,
776                      int nargs, struct value **args, CORE_ADDR sp,
777                      int struct_return, CORE_ADDR struct_addr)
778 {
779   int argreg;
780   int argnum;
781   char *val;
782   char valbuf[4];
783   struct value *arg;
784   struct type *arg_type;
785   int len;
786   enum type_code typecode;
787   CORE_ADDR regval;
788   int stack_space;
789   int stack_offset;
790
791 #if 0
792   printf("Push %d args at sp = %x, struct_return=%d (%x)\n",
793          nargs, (int) sp, struct_return, struct_addr);
794 #endif
795
796   stack_space = 0;
797   for (argnum = 0; argnum < nargs; ++argnum)
798     stack_space += ROUND_UP (TYPE_LENGTH (VALUE_TYPE (args[argnum])), 4);
799
800   stack_space -= (6 * 4);
801   if (stack_space > 0)
802     sp -= stack_space;
803
804   /* Make sure stack is dword aligned. */
805   sp = ROUND_DOWN (sp, 8);
806
807   stack_offset = 0;
808
809   argreg = 8;
810
811   if (struct_return)
812     regcache_cooked_write_unsigned (regcache, struct_return_regnum,
813                                     struct_addr);
814
815   for (argnum = 0; argnum < nargs; ++argnum)
816     {
817       arg = args[argnum];
818       arg_type = check_typedef (VALUE_TYPE (arg));
819       len = TYPE_LENGTH (arg_type);
820       typecode = TYPE_CODE (arg_type);
821
822       if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
823         {
824           store_unsigned_integer (valbuf, 4, VALUE_ADDRESS (arg));
825           typecode = TYPE_CODE_PTR;
826           len = 4;
827           val = valbuf;
828         }
829       else
830         {
831           val = (char *) VALUE_CONTENTS (arg);
832         }
833
834       while (len > 0)
835         {
836           int partial_len = (len < 4 ? len : 4);
837
838           if (argreg < 14)
839             {
840               regval = extract_unsigned_integer (val, partial_len);
841 #if 0
842               printf("  Argnum %d data %x -> reg %d\n",
843                      argnum, (int) regval, argreg);
844 #endif
845               regcache_cooked_write_unsigned (regcache, argreg, regval);
846               ++argreg;
847             }
848           else
849             {
850 #if 0
851               printf("  Argnum %d data %x -> offset %d (%x)\n",
852                      argnum, *((int *)val), stack_offset, (int) (sp + stack_offset));
853 #endif
854               write_memory (sp + stack_offset, val, partial_len);
855               stack_offset += ROUND_UP(partial_len, 4);
856             }
857           len -= partial_len;
858           val += partial_len;
859         }
860     }
861
862   /* Set the return address.  For the frv, the return breakpoint is
863      always at BP_ADDR.  */
864   regcache_cooked_write_unsigned (regcache, lr_regnum, bp_addr);
865
866   /* Finally, update the SP register.  */
867   regcache_cooked_write_unsigned (regcache, sp_regnum, sp);
868
869   return sp;
870 }
871
872 static void
873 frv_store_return_value (struct type *type, char *valbuf)
874 {
875   int length = TYPE_LENGTH (type);
876   int reg8_offset = frv_register_byte (8);
877
878   if (length <= 4)
879     deprecated_write_register_bytes (reg8_offset + (4 - length), valbuf,
880                                      length);
881   else if (length == 8)
882     deprecated_write_register_bytes (reg8_offset, valbuf, length);
883   else
884     internal_error (__FILE__, __LINE__,
885                     "Don't know how to return a %d-byte value.", length);
886 }
887
888
889 /* Hardware watchpoint / breakpoint support for the FR500
890    and FR400.  */
891
892 int
893 frv_check_watch_resources (int type, int cnt, int ot)
894 {
895   struct gdbarch_tdep *var = CURRENT_VARIANT;
896
897   /* Watchpoints not supported on simulator.  */
898   if (strcmp (target_shortname, "sim") == 0)
899     return 0;
900
901   if (type == bp_hardware_breakpoint)
902     {
903       if (var->num_hw_breakpoints == 0)
904         return 0;
905       else if (cnt <= var->num_hw_breakpoints)
906         return 1;
907     }
908   else
909     {
910       if (var->num_hw_watchpoints == 0)
911         return 0;
912       else if (ot)
913         return -1;
914       else if (cnt <= var->num_hw_watchpoints)
915         return 1;
916     }
917   return -1;
918 }
919
920
921 CORE_ADDR
922 frv_stopped_data_address (void)
923 {
924   CORE_ADDR brr, dbar0, dbar1, dbar2, dbar3;
925
926   brr = read_register (brr_regnum);
927   dbar0 = read_register (dbar0_regnum);
928   dbar1 = read_register (dbar1_regnum);
929   dbar2 = read_register (dbar2_regnum);
930   dbar3 = read_register (dbar3_regnum);
931
932   if (brr & (1<<11))
933     return dbar0;
934   else if (brr & (1<<10))
935     return dbar1;
936   else if (brr & (1<<9))
937     return dbar2;
938   else if (brr & (1<<8))
939     return dbar3;
940   else
941     return 0;
942 }
943
944 static CORE_ADDR
945 frv_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
946 {
947   return frame_unwind_register_unsigned (next_frame, pc_regnum);
948 }
949
950 /* Given a GDB frame, determine the address of the calling function's
951    frame.  This will be used to create a new GDB frame struct.  */
952
953 static void
954 frv_frame_this_id (struct frame_info *next_frame,
955                     void **this_prologue_cache, struct frame_id *this_id)
956 {
957   struct frv_unwind_cache *info
958     = frv_frame_unwind_cache (next_frame, this_prologue_cache);
959   CORE_ADDR base;
960   CORE_ADDR func;
961   struct minimal_symbol *msym_stack;
962   struct frame_id id;
963
964   /* The FUNC is easy.  */
965   func = frame_func_unwind (next_frame);
966
967   /* This is meant to halt the backtrace at "_start".  Make sure we
968      don't halt it at a generic dummy frame. */
969   if (inside_entry_file (func))
970     return;
971
972   /* Check if the stack is empty.  */
973   msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
974   if (msym_stack && info->base == SYMBOL_VALUE_ADDRESS (msym_stack))
975     return;
976
977   /* Hopefully the prologue analysis either correctly determined the
978      frame's base (which is the SP from the previous frame), or set
979      that base to "NULL".  */
980   base = info->prev_sp;
981   if (base == 0)
982     return;
983
984   id = frame_id_build (base, func);
985
986   /* Check that we're not going round in circles with the same frame
987      ID (but avoid applying the test to sentinel frames which do go
988      round in circles).  Can't use frame_id_eq() as that doesn't yet
989      compare the frame's PC value.  */
990   if (frame_relative_level (next_frame) >= 0
991       && get_frame_type (next_frame) != DUMMY_FRAME
992       && frame_id_eq (get_frame_id (next_frame), id))
993     return;
994
995   (*this_id) = id;
996 }
997
998 static void
999 frv_frame_prev_register (struct frame_info *next_frame,
1000                           void **this_prologue_cache,
1001                           int regnum, int *optimizedp,
1002                           enum lval_type *lvalp, CORE_ADDR *addrp,
1003                           int *realnump, void *bufferp)
1004 {
1005   struct frv_unwind_cache *info
1006     = frv_frame_unwind_cache (next_frame, this_prologue_cache);
1007   trad_frame_prev_register (next_frame, info->saved_regs, regnum,
1008                             optimizedp, lvalp, addrp, realnump, bufferp);
1009 }
1010
1011 static const struct frame_unwind frv_frame_unwind = {
1012   NORMAL_FRAME,
1013   frv_frame_this_id,
1014   frv_frame_prev_register
1015 };
1016
1017 static const struct frame_unwind *
1018 frv_frame_sniffer (struct frame_info *next_frame)
1019 {
1020   return &frv_frame_unwind;
1021 }
1022
1023 static CORE_ADDR
1024 frv_frame_base_address (struct frame_info *next_frame, void **this_cache)
1025 {
1026   struct frv_unwind_cache *info
1027     = frv_frame_unwind_cache (next_frame, this_cache);
1028   return info->base;
1029 }
1030
1031 static const struct frame_base frv_frame_base = {
1032   &frv_frame_unwind,
1033   frv_frame_base_address,
1034   frv_frame_base_address,
1035   frv_frame_base_address
1036 };
1037
1038 static CORE_ADDR
1039 frv_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1040 {
1041   return frame_unwind_register_unsigned (next_frame, sp_regnum);
1042 }
1043
1044
1045 /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
1046    dummy frame.  The frame ID's base needs to match the TOS value
1047    saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1048    breakpoint.  */
1049
1050 static struct frame_id
1051 frv_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1052 {
1053   return frame_id_build (frv_unwind_sp (gdbarch, next_frame),
1054                          frame_pc_unwind (next_frame));
1055 }
1056
1057
1058 static struct gdbarch *
1059 frv_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1060 {
1061   struct gdbarch *gdbarch;
1062   struct gdbarch_tdep *var;
1063
1064   /* Check to see if we've already built an appropriate architecture
1065      object for this executable.  */
1066   arches = gdbarch_list_lookup_by_info (arches, &info);
1067   if (arches)
1068     return arches->gdbarch;
1069
1070   /* Select the right tdep structure for this variant.  */
1071   var = new_variant ();
1072   switch (info.bfd_arch_info->mach)
1073     {
1074     case bfd_mach_frv:
1075     case bfd_mach_frvsimple:
1076     case bfd_mach_fr500:
1077     case bfd_mach_frvtomcat:
1078       set_variant_num_gprs (var, 64);
1079       set_variant_num_fprs (var, 64);
1080       break;
1081
1082     case bfd_mach_fr400:
1083       set_variant_num_gprs (var, 32);
1084       set_variant_num_fprs (var, 32);
1085       break;
1086
1087     default:
1088       /* Never heard of this variant.  */
1089       return 0;
1090     }
1091   
1092   gdbarch = gdbarch_alloc (&info, var);
1093
1094   set_gdbarch_short_bit (gdbarch, 16);
1095   set_gdbarch_int_bit (gdbarch, 32);
1096   set_gdbarch_long_bit (gdbarch, 32);
1097   set_gdbarch_long_long_bit (gdbarch, 64);
1098   set_gdbarch_float_bit (gdbarch, 32);
1099   set_gdbarch_double_bit (gdbarch, 64);
1100   set_gdbarch_long_double_bit (gdbarch, 64);
1101   set_gdbarch_ptr_bit (gdbarch, 32);
1102
1103   set_gdbarch_num_regs (gdbarch, frv_num_regs);
1104   set_gdbarch_sp_regnum (gdbarch, sp_regnum);
1105   set_gdbarch_deprecated_fp_regnum (gdbarch, fp_regnum);
1106   set_gdbarch_pc_regnum (gdbarch, pc_regnum);
1107
1108   set_gdbarch_register_name (gdbarch, frv_register_name);
1109   set_gdbarch_deprecated_register_size (gdbarch, 4);
1110   set_gdbarch_deprecated_register_bytes (gdbarch, frv_num_regs * 4);
1111   set_gdbarch_deprecated_register_byte (gdbarch, frv_register_byte);
1112   set_gdbarch_deprecated_register_raw_size (gdbarch, frv_register_raw_size);
1113   set_gdbarch_deprecated_max_register_raw_size (gdbarch, 4);
1114   set_gdbarch_deprecated_register_virtual_size (gdbarch, frv_register_virtual_size);
1115   set_gdbarch_deprecated_max_register_virtual_size (gdbarch, 4);
1116   set_gdbarch_deprecated_register_virtual_type (gdbarch, frv_register_virtual_type);
1117
1118   set_gdbarch_skip_prologue (gdbarch, frv_skip_prologue);
1119   set_gdbarch_breakpoint_from_pc (gdbarch, frv_breakpoint_from_pc);
1120
1121   set_gdbarch_frame_args_skip (gdbarch, 0);
1122   set_gdbarch_frameless_function_invocation (gdbarch, frv_frameless_function_invocation);
1123
1124   set_gdbarch_use_struct_convention (gdbarch, always_use_struct_convention);
1125   set_gdbarch_deprecated_extract_return_value (gdbarch, frv_extract_return_value);
1126
1127   set_gdbarch_deprecated_store_struct_return (gdbarch, frv_store_struct_return);
1128   set_gdbarch_deprecated_store_return_value (gdbarch, frv_store_return_value);
1129   set_gdbarch_deprecated_extract_struct_value_address (gdbarch, frv_extract_struct_value_address);
1130
1131   /* Frame stuff.  */
1132   set_gdbarch_unwind_pc (gdbarch, frv_unwind_pc);
1133   set_gdbarch_unwind_sp (gdbarch, frv_unwind_sp);
1134   set_gdbarch_frame_align (gdbarch, frv_frame_align);
1135   frame_unwind_append_sniffer (gdbarch, frv_frame_sniffer);
1136   frame_base_set_default (gdbarch, &frv_frame_base);
1137
1138   /* Settings for calling functions in the inferior.  */
1139   set_gdbarch_push_dummy_call (gdbarch, frv_push_dummy_call);
1140   set_gdbarch_unwind_dummy_id (gdbarch, frv_unwind_dummy_id);
1141
1142   /* Settings that should be unnecessary.  */
1143   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1144
1145   set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
1146
1147   set_gdbarch_decr_pc_after_break (gdbarch, 0);
1148   set_gdbarch_function_start_offset (gdbarch, 0);
1149
1150   set_gdbarch_remote_translate_xfer_address
1151     (gdbarch, generic_remote_translate_xfer_address);
1152
1153   /* Hardware watchpoint / breakpoint support.  */
1154   switch (info.bfd_arch_info->mach)
1155     {
1156     case bfd_mach_frv:
1157     case bfd_mach_frvsimple:
1158     case bfd_mach_fr500:
1159     case bfd_mach_frvtomcat:
1160       /* fr500-style hardware debugging support.  */
1161       var->num_hw_watchpoints = 4;
1162       var->num_hw_breakpoints = 4;
1163       break;
1164
1165     case bfd_mach_fr400:
1166       /* fr400-style hardware debugging support.  */
1167       var->num_hw_watchpoints = 2;
1168       var->num_hw_breakpoints = 4;
1169       break;
1170
1171     default:
1172       /* Otherwise, assume we don't have hardware debugging support.  */
1173       var->num_hw_watchpoints = 0;
1174       var->num_hw_breakpoints = 0;
1175       break;
1176     }
1177
1178   set_gdbarch_print_insn (gdbarch, print_insn_frv);
1179
1180   return gdbarch;
1181 }
1182
1183 void
1184 _initialize_frv_tdep (void)
1185 {
1186   register_gdbarch_init (bfd_arch_frv, frv_gdbarch_init);
1187 }