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