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