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