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