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