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