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