PR symtab/17391 gdb internal error: assertion fails in regcache.c:178
[external/binutils.git] / gdb / rx-tdep.c
1 /* Target-dependent code for the Renesas RX for GDB, the GNU debugger.
2
3    Copyright (C) 2008-2015 Free Software Foundation, Inc.
4
5    Contributed by Red Hat, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "arch-utils.h"
24 #include "prologue-value.h"
25 #include "target.h"
26 #include "regcache.h"
27 #include "opcode/rx.h"
28 #include "dis-asm.h"
29 #include "gdbtypes.h"
30 #include "frame.h"
31 #include "frame-unwind.h"
32 #include "frame-base.h"
33 #include "value.h"
34 #include "gdbcore.h"
35 #include "dwarf2-frame.h"
36
37 #include "elf/rx.h"
38 #include "elf-bfd.h"
39
40 /* Certain important register numbers.  */
41 enum
42 {
43   RX_SP_REGNUM = 0,
44   RX_R1_REGNUM = 1,
45   RX_R4_REGNUM = 4,
46   RX_FP_REGNUM = 6,
47   RX_R15_REGNUM = 15,
48   RX_USP_REGNUM = 16,
49   RX_PSW_REGNUM = 18,
50   RX_PC_REGNUM = 19,
51   RX_BPSW_REGNUM = 21,
52   RX_BPC_REGNUM = 22,
53   RX_FPSW_REGNUM = 24,
54   RX_ACC_REGNUM = 25,
55   RX_NUM_REGS = 26
56 };
57
58 /* RX frame types.  */
59 enum rx_frame_type {
60   RX_FRAME_TYPE_NORMAL,
61   RX_FRAME_TYPE_EXCEPTION,
62   RX_FRAME_TYPE_FAST_INTERRUPT
63 };
64
65 /* Architecture specific data.  */
66 struct gdbarch_tdep
67 {
68   /* The ELF header flags specify the multilib used.  */
69   int elf_flags;
70
71   /* Type of PSW and BPSW.  */
72   struct type *rx_psw_type;
73
74   /* Type of FPSW.  */
75   struct type *rx_fpsw_type;
76 };
77
78 /* This structure holds the results of a prologue analysis.  */
79 struct rx_prologue
80 {
81   /* Frame type, either a normal frame or one of two types of exception
82      frames.  */
83   enum rx_frame_type frame_type;
84
85   /* The offset from the frame base to the stack pointer --- always
86      zero or negative.
87
88      Calling this a "size" is a bit misleading, but given that the
89      stack grows downwards, using offsets for everything keeps one
90      from going completely sign-crazy: you never change anything's
91      sign for an ADD instruction; always change the second operand's
92      sign for a SUB instruction; and everything takes care of
93      itself.  */
94   int frame_size;
95
96   /* Non-zero if this function has initialized the frame pointer from
97      the stack pointer, zero otherwise.  */
98   int has_frame_ptr;
99
100   /* If has_frame_ptr is non-zero, this is the offset from the frame
101      base to where the frame pointer points.  This is always zero or
102      negative.  */
103   int frame_ptr_offset;
104
105   /* The address of the first instruction at which the frame has been
106      set up and the arguments are where the debug info says they are
107      --- as best as we can tell.  */
108   CORE_ADDR prologue_end;
109
110   /* reg_offset[R] is the offset from the CFA at which register R is
111      saved, or 1 if register R has not been saved.  (Real values are
112      always zero or negative.)  */
113   int reg_offset[RX_NUM_REGS];
114 };
115
116 /* Implement the "register_name" gdbarch method.  */
117 static const char *
118 rx_register_name (struct gdbarch *gdbarch, int regnr)
119 {
120   static const char *const reg_names[] = {
121     "r0",
122     "r1",
123     "r2",
124     "r3",
125     "r4",
126     "r5",
127     "r6",
128     "r7",
129     "r8",
130     "r9",
131     "r10",
132     "r11",
133     "r12",
134     "r13",
135     "r14",
136     "r15",
137     "usp",
138     "isp",
139     "psw",
140     "pc",
141     "intb",
142     "bpsw",
143     "bpc",
144     "fintv",
145     "fpsw",
146     "acc"
147   };
148
149   return reg_names[regnr];
150 }
151
152 /* Implement the "register_type" gdbarch method.  */
153 static struct type *
154 rx_register_type (struct gdbarch *gdbarch, int reg_nr)
155 {
156   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
157
158   if (reg_nr == RX_PC_REGNUM)
159     return builtin_type (gdbarch)->builtin_func_ptr;
160   else if (reg_nr == RX_PSW_REGNUM || reg_nr == RX_BPSW_REGNUM)
161     return tdep->rx_psw_type;
162   else if (reg_nr == RX_FPSW_REGNUM)
163     return tdep->rx_fpsw_type;
164   else if (reg_nr == RX_ACC_REGNUM)
165     return builtin_type (gdbarch)->builtin_unsigned_long_long;
166   else
167     return builtin_type (gdbarch)->builtin_unsigned_long;
168 }
169
170
171 /* Function for finding saved registers in a 'struct pv_area'; this
172    function is passed to pv_area_scan.
173
174    If VALUE is a saved register, ADDR says it was saved at a constant
175    offset from the frame base, and SIZE indicates that the whole
176    register was saved, record its offset.  */
177 static void
178 check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
179 {
180   struct rx_prologue *result = (struct rx_prologue *) result_untyped;
181
182   if (value.kind == pvk_register
183       && value.k == 0
184       && pv_is_register (addr, RX_SP_REGNUM)
185       && size == register_size (target_gdbarch (), value.reg))
186     result->reg_offset[value.reg] = addr.k;
187 }
188
189 /* Define a "handle" struct for fetching the next opcode.  */
190 struct rx_get_opcode_byte_handle
191 {
192   CORE_ADDR pc;
193 };
194
195 /* Fetch a byte on behalf of the opcode decoder.  HANDLE contains
196    the memory address of the next byte to fetch.  If successful,
197    the address in the handle is updated and the byte fetched is
198    returned as the value of the function.  If not successful, -1
199    is returned.  */
200 static int
201 rx_get_opcode_byte (void *handle)
202 {
203   struct rx_get_opcode_byte_handle *opcdata
204     = (struct rx_get_opcode_byte_handle *) handle;
205   int status;
206   gdb_byte byte;
207
208   status = target_read_code (opcdata->pc, &byte, 1);
209   if (status == 0)
210     {
211       opcdata->pc += 1;
212       return byte;
213     }
214   else
215     return -1;
216 }
217
218 /* Analyze a prologue starting at START_PC, going no further than
219    LIMIT_PC.  Fill in RESULT as appropriate.  */
220
221 static void
222 rx_analyze_prologue (CORE_ADDR start_pc, CORE_ADDR limit_pc,
223                      enum rx_frame_type frame_type,
224                      struct rx_prologue *result)
225 {
226   CORE_ADDR pc, next_pc;
227   int rn;
228   pv_t reg[RX_NUM_REGS];
229   struct pv_area *stack;
230   struct cleanup *back_to;
231   CORE_ADDR after_last_frame_setup_insn = start_pc;
232
233   memset (result, 0, sizeof (*result));
234
235   result->frame_type = frame_type;
236
237   for (rn = 0; rn < RX_NUM_REGS; rn++)
238     {
239       reg[rn] = pv_register (rn, 0);
240       result->reg_offset[rn] = 1;
241     }
242
243   stack = make_pv_area (RX_SP_REGNUM, gdbarch_addr_bit (target_gdbarch ()));
244   back_to = make_cleanup_free_pv_area (stack);
245
246   if (frame_type == RX_FRAME_TYPE_FAST_INTERRUPT)
247     {
248       /* This code won't do anything useful at present, but this is
249          what happens for fast interrupts.  */
250       reg[RX_BPSW_REGNUM] = reg[RX_PSW_REGNUM];
251       reg[RX_BPC_REGNUM] = reg[RX_PC_REGNUM];
252     }
253   else
254     {
255       /* When an exception occurs, the PSW is saved to the interrupt stack
256          first.  */
257       if (frame_type == RX_FRAME_TYPE_EXCEPTION)
258         {
259           reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
260           pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[RX_PSW_REGNUM]);
261         }
262
263       /* The call instruction (or an exception/interrupt) has saved the return
264           address on the stack.  */
265       reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
266       pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[RX_PC_REGNUM]);
267
268     }
269
270
271   pc = start_pc;
272   while (pc < limit_pc)
273     {
274       int bytes_read;
275       struct rx_get_opcode_byte_handle opcode_handle;
276       RX_Opcode_Decoded opc;
277
278       opcode_handle.pc = pc;
279       bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
280                                      &opcode_handle);
281       next_pc = pc + bytes_read;
282
283       if (opc.id == RXO_pushm   /* pushm r1, r2 */
284           && opc.op[1].type == RX_Operand_Register
285           && opc.op[2].type == RX_Operand_Register)
286         {
287           int r1, r2;
288           int r;
289
290           r1 = opc.op[1].reg;
291           r2 = opc.op[2].reg;
292           for (r = r2; r >= r1; r--)
293             {
294               reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
295               pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[r]);
296             }
297           after_last_frame_setup_insn = next_pc;
298         }
299       else if (opc.id == RXO_mov        /* mov.l rdst, rsrc */
300                && opc.op[0].type == RX_Operand_Register
301                && opc.op[1].type == RX_Operand_Register
302                && opc.size == RX_Long)
303         {
304           int rdst, rsrc;
305
306           rdst = opc.op[0].reg;
307           rsrc = opc.op[1].reg;
308           reg[rdst] = reg[rsrc];
309           if (rdst == RX_FP_REGNUM && rsrc == RX_SP_REGNUM)
310             after_last_frame_setup_insn = next_pc;
311         }
312       else if (opc.id == RXO_mov        /* mov.l rsrc, [-SP] */
313                && opc.op[0].type == RX_Operand_Predec
314                && opc.op[0].reg == RX_SP_REGNUM
315                && opc.op[1].type == RX_Operand_Register
316                && opc.size == RX_Long)
317         {
318           int rsrc;
319
320           rsrc = opc.op[1].reg;
321           reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
322           pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[rsrc]);
323           after_last_frame_setup_insn = next_pc;
324         }
325       else if (opc.id == RXO_add        /* add #const, rsrc, rdst */
326                && opc.op[0].type == RX_Operand_Register
327                && opc.op[1].type == RX_Operand_Immediate
328                && opc.op[2].type == RX_Operand_Register)
329         {
330           int rdst = opc.op[0].reg;
331           int addend = opc.op[1].addend;
332           int rsrc = opc.op[2].reg;
333           reg[rdst] = pv_add_constant (reg[rsrc], addend);
334           /* Negative adjustments to the stack pointer or frame pointer
335              are (most likely) part of the prologue.  */
336           if ((rdst == RX_SP_REGNUM || rdst == RX_FP_REGNUM) && addend < 0)
337             after_last_frame_setup_insn = next_pc;
338         }
339       else if (opc.id == RXO_mov
340                && opc.op[0].type == RX_Operand_Indirect
341                && opc.op[1].type == RX_Operand_Register
342                && opc.size == RX_Long
343                && (opc.op[0].reg == RX_SP_REGNUM
344                    || opc.op[0].reg == RX_FP_REGNUM)
345                && (RX_R1_REGNUM <= opc.op[1].reg
346                    && opc.op[1].reg <= RX_R4_REGNUM))
347         {
348           /* This moves an argument register to the stack.  Don't
349              record it, but allow it to be a part of the prologue.  */
350         }
351       else if (opc.id == RXO_branch
352                && opc.op[0].type == RX_Operand_Immediate
353                && next_pc < opc.op[0].addend)
354         {
355           /* When a loop appears as the first statement of a function
356              body, gcc 4.x will use a BRA instruction to branch to the
357              loop condition checking code.  This BRA instruction is
358              marked as part of the prologue.  We therefore set next_pc
359              to this branch target and also stop the prologue scan.
360              The instructions at and beyond the branch target should
361              no longer be associated with the prologue.
362
363              Note that we only consider forward branches here.  We
364              presume that a forward branch is being used to skip over
365              a loop body.
366
367              A backwards branch is covered by the default case below.
368              If we were to encounter a backwards branch, that would
369              most likely mean that we've scanned through a loop body.
370              We definitely want to stop the prologue scan when this
371              happens and that is precisely what is done by the default
372              case below.  */
373
374           after_last_frame_setup_insn = opc.op[0].addend;
375           break;                /* Scan no further if we hit this case.  */
376         }
377       else
378         {
379           /* Terminate the prologue scan.  */
380           break;
381         }
382
383       pc = next_pc;
384     }
385
386   /* Is the frame size (offset, really) a known constant?  */
387   if (pv_is_register (reg[RX_SP_REGNUM], RX_SP_REGNUM))
388     result->frame_size = reg[RX_SP_REGNUM].k;
389
390   /* Was the frame pointer initialized?  */
391   if (pv_is_register (reg[RX_FP_REGNUM], RX_SP_REGNUM))
392     {
393       result->has_frame_ptr = 1;
394       result->frame_ptr_offset = reg[RX_FP_REGNUM].k;
395     }
396
397   /* Record where all the registers were saved.  */
398   pv_area_scan (stack, check_for_saved, (void *) result);
399
400   result->prologue_end = after_last_frame_setup_insn;
401
402   do_cleanups (back_to);
403 }
404
405
406 /* Implement the "skip_prologue" gdbarch method.  */
407 static CORE_ADDR
408 rx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
409 {
410   const char *name;
411   CORE_ADDR func_addr, func_end;
412   struct rx_prologue p;
413
414   /* Try to find the extent of the function that contains PC.  */
415   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
416     return pc;
417
418   /* The frame type doesn't matter here, since we only care about
419      where the prologue ends.  We'll use RX_FRAME_TYPE_NORMAL.  */
420   rx_analyze_prologue (pc, func_end, RX_FRAME_TYPE_NORMAL, &p);
421   return p.prologue_end;
422 }
423
424 /* Given a frame described by THIS_FRAME, decode the prologue of its
425    associated function if there is not cache entry as specified by
426    THIS_PROLOGUE_CACHE.  Save the decoded prologue in the cache and
427    return that struct as the value of this function.  */
428
429 static struct rx_prologue *
430 rx_analyze_frame_prologue (struct frame_info *this_frame,
431                            enum rx_frame_type frame_type,
432                            void **this_prologue_cache)
433 {
434   if (!*this_prologue_cache)
435     {
436       CORE_ADDR func_start, stop_addr;
437
438       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct rx_prologue);
439
440       func_start = get_frame_func (this_frame);
441       stop_addr = get_frame_pc (this_frame);
442
443       /* If we couldn't find any function containing the PC, then
444          just initialize the prologue cache, but don't do anything.  */
445       if (!func_start)
446         stop_addr = func_start;
447
448       rx_analyze_prologue (func_start, stop_addr, frame_type,
449                            (struct rx_prologue *) *this_prologue_cache);
450     }
451
452   return (struct rx_prologue *) *this_prologue_cache;
453 }
454
455 /* Determine type of frame by scanning the function for a return
456    instruction.  */
457
458 static enum rx_frame_type
459 rx_frame_type (struct frame_info *this_frame, void **this_cache)
460 {
461   const char *name;
462   CORE_ADDR pc, start_pc, lim_pc;
463   int bytes_read;
464   struct rx_get_opcode_byte_handle opcode_handle;
465   RX_Opcode_Decoded opc;
466
467   gdb_assert (this_cache != NULL);
468
469   /* If we have a cached value, return it.  */
470
471   if (*this_cache != NULL)
472     {
473       struct rx_prologue *p = (struct rx_prologue *) *this_cache;
474
475       return p->frame_type;
476     }
477
478   /* No cached value; scan the function.  The frame type is cached in
479      rx_analyze_prologue / rx_analyze_frame_prologue.  */
480   
481   pc = get_frame_pc (this_frame);
482   
483   /* Attempt to find the last address in the function.  If it cannot
484      be determined, set the limit to be a short ways past the frame's
485      pc.  */
486   if (!find_pc_partial_function (pc, &name, &start_pc, &lim_pc))
487     lim_pc = pc + 20;
488
489   while (pc < lim_pc)
490     {
491       opcode_handle.pc = pc;
492       bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
493                                      &opcode_handle);
494
495       if (bytes_read <= 0 || opc.id == RXO_rts)
496         return RX_FRAME_TYPE_NORMAL;
497       else if (opc.id == RXO_rtfi)
498         return RX_FRAME_TYPE_FAST_INTERRUPT;
499       else if (opc.id == RXO_rte)
500         return RX_FRAME_TYPE_EXCEPTION;
501
502       pc += bytes_read;
503     }
504
505   return RX_FRAME_TYPE_NORMAL;
506 }
507
508
509 /* Given the next frame and a prologue cache, return this frame's
510    base.  */
511
512 static CORE_ADDR
513 rx_frame_base (struct frame_info *this_frame, void **this_cache)
514 {
515   enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
516   struct rx_prologue *p
517     = rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
518
519   /* In functions that use alloca, the distance between the stack
520      pointer and the frame base varies dynamically, so we can't use
521      the SP plus static information like prologue analysis to find the
522      frame base.  However, such functions must have a frame pointer,
523      to be able to restore the SP on exit.  So whenever we do have a
524      frame pointer, use that to find the base.  */
525   if (p->has_frame_ptr)
526     {
527       CORE_ADDR fp = get_frame_register_unsigned (this_frame, RX_FP_REGNUM);
528       return fp - p->frame_ptr_offset;
529     }
530   else
531     {
532       CORE_ADDR sp = get_frame_register_unsigned (this_frame, RX_SP_REGNUM);
533       return sp - p->frame_size;
534     }
535 }
536
537 /* Implement the "frame_this_id" method for unwinding frames.  */
538
539 static void
540 rx_frame_this_id (struct frame_info *this_frame, void **this_cache,
541                   struct frame_id *this_id)
542 {
543   *this_id = frame_id_build (rx_frame_base (this_frame, this_cache),
544                              get_frame_func (this_frame));
545 }
546
547 /* Implement the "frame_prev_register" method for unwinding frames.  */
548
549 static struct value *
550 rx_frame_prev_register (struct frame_info *this_frame, void **this_cache,
551                         int regnum)
552 {
553   enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
554   struct rx_prologue *p
555     = rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
556   CORE_ADDR frame_base = rx_frame_base (this_frame, this_cache);
557
558   if (regnum == RX_SP_REGNUM)
559     {
560       if (frame_type == RX_FRAME_TYPE_EXCEPTION)
561         {
562           struct value *psw_val;
563           CORE_ADDR psw;
564
565           psw_val = rx_frame_prev_register (this_frame, this_cache,
566                                             RX_PSW_REGNUM);
567           psw = extract_unsigned_integer (value_contents_all (psw_val), 4, 
568                                           gdbarch_byte_order (
569                                             get_frame_arch (this_frame)));
570
571           if ((psw & 0x20000 /* U bit */) != 0)
572             return rx_frame_prev_register (this_frame, this_cache,
573                                            RX_USP_REGNUM);
574
575           /* Fall through for the case where U bit is zero.  */
576         }
577
578       return frame_unwind_got_constant (this_frame, regnum, frame_base);
579     }
580
581   if (frame_type == RX_FRAME_TYPE_FAST_INTERRUPT)
582     {
583       if (regnum == RX_PC_REGNUM)
584         return rx_frame_prev_register (this_frame, this_cache,
585                                        RX_BPC_REGNUM);
586       if (regnum == RX_PSW_REGNUM)
587         return rx_frame_prev_register (this_frame, this_cache,
588                                        RX_BPSW_REGNUM);
589     }
590
591   /* If prologue analysis says we saved this register somewhere,
592      return a description of the stack slot holding it.  */
593   if (p->reg_offset[regnum] != 1)
594     return frame_unwind_got_memory (this_frame, regnum,
595                                     frame_base + p->reg_offset[regnum]);
596
597   /* Otherwise, presume we haven't changed the value of this
598      register, and get it from the next frame.  */
599   return frame_unwind_got_register (this_frame, regnum, regnum);
600 }
601
602 /* Return TRUE if the frame indicated by FRAME_TYPE is a normal frame.  */
603
604 static int
605 normal_frame_p (enum rx_frame_type frame_type)
606 {
607   return (frame_type == RX_FRAME_TYPE_NORMAL);
608 }
609
610 /* Return TRUE if the frame indicated by FRAME_TYPE is an exception
611    frame.  */
612
613 static int
614 exception_frame_p (enum rx_frame_type frame_type)
615 {
616   return (frame_type == RX_FRAME_TYPE_EXCEPTION
617           || frame_type == RX_FRAME_TYPE_FAST_INTERRUPT);
618 }
619
620 /* Common code used by both normal and exception frame sniffers.  */
621
622 static int
623 rx_frame_sniffer_common (const struct frame_unwind *self,
624                          struct frame_info *this_frame,
625                          void **this_cache,
626                          int (*sniff_p)(enum rx_frame_type) )
627 {
628   gdb_assert (this_cache != NULL);
629
630   if (*this_cache == NULL)
631     {
632       enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
633
634       if (sniff_p (frame_type))
635         {
636           /* The call below will fill in the cache, including the frame
637              type.  */
638           (void) rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
639
640           return 1;
641         }
642       else
643         return 0;
644     }
645   else
646     {
647       struct rx_prologue *p = (struct rx_prologue *) *this_cache;
648
649       return sniff_p (p->frame_type);
650     }
651 }
652
653 /* Frame sniffer for normal (non-exception) frames.  */
654
655 static int
656 rx_frame_sniffer (const struct frame_unwind *self,
657                   struct frame_info *this_frame,
658                   void **this_cache)
659 {
660   return rx_frame_sniffer_common (self, this_frame, this_cache,
661                                   normal_frame_p);
662 }
663
664 /* Frame sniffer for exception frames.  */
665
666 static int
667 rx_exception_sniffer (const struct frame_unwind *self,
668                              struct frame_info *this_frame,
669                              void **this_cache)
670 {
671   return rx_frame_sniffer_common (self, this_frame, this_cache,
672                                   exception_frame_p);
673 }
674
675 /* Data structure for normal code using instruction-based prologue
676    analyzer.  */
677
678 static const struct frame_unwind rx_frame_unwind = {
679   NORMAL_FRAME,
680   default_frame_unwind_stop_reason,
681   rx_frame_this_id,
682   rx_frame_prev_register,
683   NULL,
684   rx_frame_sniffer
685 };
686
687 /* Data structure for exception code using instruction-based prologue
688    analyzer.  */
689
690 static const struct frame_unwind rx_exception_unwind = {
691   /* SIGTRAMP_FRAME could be used here, but backtraces are less informative.  */
692   NORMAL_FRAME,
693   default_frame_unwind_stop_reason,
694   rx_frame_this_id,
695   rx_frame_prev_register,
696   NULL,
697   rx_exception_sniffer
698 };
699
700 /* Implement the "unwind_pc" gdbarch method.  */
701 static CORE_ADDR
702 rx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
703 {
704   ULONGEST pc;
705
706   pc = frame_unwind_register_unsigned (this_frame, RX_PC_REGNUM);
707   return pc;
708 }
709
710 /* Implement the "unwind_sp" gdbarch method.  */
711 static CORE_ADDR
712 rx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
713 {
714   ULONGEST sp;
715
716   sp = frame_unwind_register_unsigned (this_frame, RX_SP_REGNUM);
717   return sp;
718 }
719
720 /* Implement the "dummy_id" gdbarch method.  */
721 static struct frame_id
722 rx_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
723 {
724   return
725     frame_id_build (get_frame_register_unsigned (this_frame, RX_SP_REGNUM),
726                     get_frame_pc (this_frame));
727 }
728
729 /* Implement the "push_dummy_call" gdbarch method.  */
730 static CORE_ADDR
731 rx_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
732                     struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
733                     struct value **args, CORE_ADDR sp, int struct_return,
734                     CORE_ADDR struct_addr)
735 {
736   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
737   int write_pass;
738   int sp_off = 0;
739   CORE_ADDR cfa;
740   int num_register_candidate_args;
741
742   struct type *func_type = value_type (function);
743
744   /* Dereference function pointer types.  */
745   while (TYPE_CODE (func_type) == TYPE_CODE_PTR)
746     func_type = TYPE_TARGET_TYPE (func_type);
747
748   /* The end result had better be a function or a method.  */
749   gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC
750               || TYPE_CODE (func_type) == TYPE_CODE_METHOD);
751
752   /* Functions with a variable number of arguments have all of their
753      variable arguments and the last non-variable argument passed
754      on the stack.
755
756      Otherwise, we can pass up to four arguments on the stack.
757
758      Once computed, we leave this value alone.  I.e. we don't update
759      it in case of a struct return going in a register or an argument
760      requiring multiple registers, etc.  We rely instead on the value
761      of the ``arg_reg'' variable to get these other details correct.  */
762
763   if (TYPE_VARARGS (func_type))
764     num_register_candidate_args = TYPE_NFIELDS (func_type) - 1;
765   else
766     num_register_candidate_args = 4;
767
768   /* We make two passes; the first does the stack allocation,
769      the second actually stores the arguments.  */
770   for (write_pass = 0; write_pass <= 1; write_pass++)
771     {
772       int i;
773       int arg_reg = RX_R1_REGNUM;
774
775       if (write_pass)
776         sp = align_down (sp - sp_off, 4);
777       sp_off = 0;
778
779       if (struct_return)
780         {
781           struct type *return_type = TYPE_TARGET_TYPE (func_type);
782
783           gdb_assert (TYPE_CODE (return_type) == TYPE_CODE_STRUCT
784                       || TYPE_CODE (func_type) == TYPE_CODE_UNION);
785
786           if (TYPE_LENGTH (return_type) > 16
787               || TYPE_LENGTH (return_type) % 4 != 0)
788             {
789               if (write_pass)
790                 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
791                                                 struct_addr);
792             }
793         }
794
795       /* Push the arguments.  */
796       for (i = 0; i < nargs; i++)
797         {
798           struct value *arg = args[i];
799           const gdb_byte *arg_bits = value_contents_all (arg);
800           struct type *arg_type = check_typedef (value_type (arg));
801           ULONGEST arg_size = TYPE_LENGTH (arg_type);
802
803           if (i == 0 && struct_addr != 0 && !struct_return
804               && TYPE_CODE (arg_type) == TYPE_CODE_PTR
805               && extract_unsigned_integer (arg_bits, 4,
806                                            byte_order) == struct_addr)
807             {
808               /* This argument represents the address at which C++ (and
809                  possibly other languages) store their return value.
810                  Put this value in R15.  */
811               if (write_pass)
812                 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
813                                                 struct_addr);
814             }
815           else if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT
816                    && TYPE_CODE (arg_type) != TYPE_CODE_UNION)
817             {
818               /* Argument is a scalar.  */
819               if (arg_size == 8)
820                 {
821                   if (i < num_register_candidate_args
822                       && arg_reg <= RX_R4_REGNUM - 1)
823                     {
824                       /* If argument registers are going to be used to pass
825                          an 8 byte scalar, the ABI specifies that two registers
826                          must be available.  */
827                       if (write_pass)
828                         {
829                           regcache_cooked_write_unsigned (regcache, arg_reg,
830                                                           extract_unsigned_integer
831                                                           (arg_bits, 4,
832                                                            byte_order));
833                           regcache_cooked_write_unsigned (regcache,
834                                                           arg_reg + 1,
835                                                           extract_unsigned_integer
836                                                           (arg_bits + 4, 4,
837                                                            byte_order));
838                         }
839                       arg_reg += 2;
840                     }
841                   else
842                     {
843                       sp_off = align_up (sp_off, 4);
844                       /* Otherwise, pass the 8 byte scalar on the stack.  */
845                       if (write_pass)
846                         write_memory (sp + sp_off, arg_bits, 8);
847                       sp_off += 8;
848                     }
849                 }
850               else
851                 {
852                   ULONGEST u;
853
854                   gdb_assert (arg_size <= 4);
855
856                   u =
857                     extract_unsigned_integer (arg_bits, arg_size, byte_order);
858
859                   if (i < num_register_candidate_args
860                       && arg_reg <= RX_R4_REGNUM)
861                     {
862                       if (write_pass)
863                         regcache_cooked_write_unsigned (regcache, arg_reg, u);
864                       arg_reg += 1;
865                     }
866                   else
867                     {
868                       int p_arg_size = 4;
869
870                       if (TYPE_PROTOTYPED (func_type)
871                           && i < TYPE_NFIELDS (func_type))
872                         {
873                           struct type *p_arg_type =
874                             TYPE_FIELD_TYPE (func_type, i);
875                           p_arg_size = TYPE_LENGTH (p_arg_type);
876                         }
877
878                       sp_off = align_up (sp_off, p_arg_size);
879
880                       if (write_pass)
881                         write_memory_unsigned_integer (sp + sp_off,
882                                                        p_arg_size, byte_order,
883                                                        u);
884                       sp_off += p_arg_size;
885                     }
886                 }
887             }
888           else
889             {
890               /* Argument is a struct or union.  Pass as much of the struct
891                  in registers, if possible.  Pass the rest on the stack.  */
892               while (arg_size > 0)
893                 {
894                   if (i < num_register_candidate_args
895                       && arg_reg <= RX_R4_REGNUM
896                       && arg_size <= 4 * (RX_R4_REGNUM - arg_reg + 1)
897                       && arg_size % 4 == 0)
898                     {
899                       int len = min (arg_size, 4);
900
901                       if (write_pass)
902                         regcache_cooked_write_unsigned (regcache, arg_reg,
903                                                         extract_unsigned_integer
904                                                         (arg_bits, len,
905                                                          byte_order));
906                       arg_bits += len;
907                       arg_size -= len;
908                       arg_reg++;
909                     }
910                   else
911                     {
912                       sp_off = align_up (sp_off, 4);
913                       if (write_pass)
914                         write_memory (sp + sp_off, arg_bits, arg_size);
915                       sp_off += align_up (arg_size, 4);
916                       arg_size = 0;
917                     }
918                 }
919             }
920         }
921     }
922
923   /* Keep track of the stack address prior to pushing the return address.
924      This is the value that we'll return.  */
925   cfa = sp;
926
927   /* Push the return address.  */
928   sp = sp - 4;
929   write_memory_unsigned_integer (sp, 4, byte_order, bp_addr);
930
931   /* Update the stack pointer.  */
932   regcache_cooked_write_unsigned (regcache, RX_SP_REGNUM, sp);
933
934   return cfa;
935 }
936
937 /* Implement the "return_value" gdbarch method.  */
938 static enum return_value_convention
939 rx_return_value (struct gdbarch *gdbarch,
940                  struct value *function,
941                  struct type *valtype,
942                  struct regcache *regcache,
943                  gdb_byte *readbuf, const gdb_byte *writebuf)
944 {
945   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
946   ULONGEST valtype_len = TYPE_LENGTH (valtype);
947
948   if (TYPE_LENGTH (valtype) > 16
949       || ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
950            || TYPE_CODE (valtype) == TYPE_CODE_UNION)
951           && TYPE_LENGTH (valtype) % 4 != 0))
952     return RETURN_VALUE_STRUCT_CONVENTION;
953
954   if (readbuf)
955     {
956       ULONGEST u;
957       int argreg = RX_R1_REGNUM;
958       int offset = 0;
959
960       while (valtype_len > 0)
961         {
962           int len = min (valtype_len, 4);
963
964           regcache_cooked_read_unsigned (regcache, argreg, &u);
965           store_unsigned_integer (readbuf + offset, len, byte_order, u);
966           valtype_len -= len;
967           offset += len;
968           argreg++;
969         }
970     }
971
972   if (writebuf)
973     {
974       ULONGEST u;
975       int argreg = RX_R1_REGNUM;
976       int offset = 0;
977
978       while (valtype_len > 0)
979         {
980           int len = min (valtype_len, 4);
981
982           u = extract_unsigned_integer (writebuf + offset, len, byte_order);
983           regcache_cooked_write_unsigned (regcache, argreg, u);
984           valtype_len -= len;
985           offset += len;
986           argreg++;
987         }
988     }
989
990   return RETURN_VALUE_REGISTER_CONVENTION;
991 }
992
993 /* Implement the "breakpoint_from_pc" gdbarch method.  */
994 static const gdb_byte *
995 rx_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
996 {
997   static gdb_byte breakpoint[] = { 0x00 };
998   *lenptr = sizeof breakpoint;
999   return breakpoint;
1000 }
1001
1002 /* Implement the dwarf_reg_to_regnum" gdbarch method.  */
1003
1004 static int
1005 rx_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
1006 {
1007   if (0 <= reg && reg <= 15)
1008     return reg;
1009   else if (reg == 16)
1010     return RX_PSW_REGNUM;
1011   else if (reg == 17)
1012     return RX_PC_REGNUM;
1013   else
1014     return -1;
1015 }
1016
1017 /* Allocate and initialize a gdbarch object.  */
1018 static struct gdbarch *
1019 rx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1020 {
1021   struct gdbarch *gdbarch;
1022   struct gdbarch_tdep *tdep;
1023   int elf_flags;
1024
1025   /* Extract the elf_flags if available.  */
1026   if (info.abfd != NULL
1027       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
1028     elf_flags = elf_elfheader (info.abfd)->e_flags;
1029   else
1030     elf_flags = 0;
1031
1032
1033   /* Try to find the architecture in the list of already defined
1034      architectures.  */
1035   for (arches = gdbarch_list_lookup_by_info (arches, &info);
1036        arches != NULL;
1037        arches = gdbarch_list_lookup_by_info (arches->next, &info))
1038     {
1039       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
1040         continue;
1041
1042       return arches->gdbarch;
1043     }
1044
1045   /* None found, create a new architecture from the information
1046      provided.  */
1047   tdep = XNEW (struct gdbarch_tdep);
1048   gdbarch = gdbarch_alloc (&info, tdep);
1049   tdep->elf_flags = elf_flags;
1050
1051   /* Initialize the flags type for PSW and BPSW.  */
1052
1053   tdep->rx_psw_type = arch_flags_type (gdbarch, "rx_psw_type", 4);
1054   append_flags_type_flag (tdep->rx_psw_type, 0, "C");
1055   append_flags_type_flag (tdep->rx_psw_type, 1, "Z");
1056   append_flags_type_flag (tdep->rx_psw_type, 2, "S");
1057   append_flags_type_flag (tdep->rx_psw_type, 3, "O");
1058   append_flags_type_flag (tdep->rx_psw_type, 16, "I");
1059   append_flags_type_flag (tdep->rx_psw_type, 17, "U");
1060   append_flags_type_flag (tdep->rx_psw_type, 20, "PM");
1061   append_flags_type_flag (tdep->rx_psw_type, 24, "IPL0");
1062   append_flags_type_flag (tdep->rx_psw_type, 25, "IPL1");
1063   append_flags_type_flag (tdep->rx_psw_type, 26, "IPL2");
1064   append_flags_type_flag (tdep->rx_psw_type, 27, "IPL3");
1065
1066   /* Initialize flags type for FPSW.  */
1067
1068   tdep->rx_fpsw_type = arch_flags_type (gdbarch, "rx_fpsw_type", 4);
1069   append_flags_type_flag (tdep->rx_fpsw_type, 0, "RM0");
1070   append_flags_type_flag (tdep->rx_fpsw_type, 1, "RM1");
1071   append_flags_type_flag (tdep->rx_fpsw_type, 2, "CV");
1072   append_flags_type_flag (tdep->rx_fpsw_type, 3, "CO");
1073   append_flags_type_flag (tdep->rx_fpsw_type, 4, "CZ");
1074   append_flags_type_flag (tdep->rx_fpsw_type, 5, "CU");
1075   append_flags_type_flag (tdep->rx_fpsw_type, 6, "CX");
1076   append_flags_type_flag (tdep->rx_fpsw_type, 7, "CE");
1077   append_flags_type_flag (tdep->rx_fpsw_type, 8, "DN");
1078   append_flags_type_flag (tdep->rx_fpsw_type, 10, "EV");
1079   append_flags_type_flag (tdep->rx_fpsw_type, 11, "EO");
1080   append_flags_type_flag (tdep->rx_fpsw_type, 12, "EZ");
1081   append_flags_type_flag (tdep->rx_fpsw_type, 13, "EU");
1082   append_flags_type_flag (tdep->rx_fpsw_type, 14, "EX");
1083   append_flags_type_flag (tdep->rx_fpsw_type, 26, "FV");
1084   append_flags_type_flag (tdep->rx_fpsw_type, 27, "FO");
1085   append_flags_type_flag (tdep->rx_fpsw_type, 28, "FZ");
1086   append_flags_type_flag (tdep->rx_fpsw_type, 29, "FU");
1087   append_flags_type_flag (tdep->rx_fpsw_type, 30, "FX");
1088   append_flags_type_flag (tdep->rx_fpsw_type, 31, "FS");
1089
1090   set_gdbarch_num_regs (gdbarch, RX_NUM_REGS);
1091   set_gdbarch_num_pseudo_regs (gdbarch, 0);
1092   set_gdbarch_register_name (gdbarch, rx_register_name);
1093   set_gdbarch_register_type (gdbarch, rx_register_type);
1094   set_gdbarch_pc_regnum (gdbarch, RX_PC_REGNUM);
1095   set_gdbarch_sp_regnum (gdbarch, RX_SP_REGNUM);
1096   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1097   set_gdbarch_decr_pc_after_break (gdbarch, 1);
1098   set_gdbarch_breakpoint_from_pc (gdbarch, rx_breakpoint_from_pc);
1099   set_gdbarch_skip_prologue (gdbarch, rx_skip_prologue);
1100
1101   set_gdbarch_print_insn (gdbarch, print_insn_rx);
1102
1103   set_gdbarch_unwind_pc (gdbarch, rx_unwind_pc);
1104   set_gdbarch_unwind_sp (gdbarch, rx_unwind_sp);
1105
1106   /* Target builtin data types.  */
1107   set_gdbarch_char_signed (gdbarch, 0);
1108   set_gdbarch_short_bit (gdbarch, 16);
1109   set_gdbarch_int_bit (gdbarch, 32);
1110   set_gdbarch_long_bit (gdbarch, 32);
1111   set_gdbarch_long_long_bit (gdbarch, 64);
1112   set_gdbarch_ptr_bit (gdbarch, 32);
1113   set_gdbarch_float_bit (gdbarch, 32);
1114   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
1115   if (elf_flags & E_FLAG_RX_64BIT_DOUBLES)
1116     {
1117       set_gdbarch_double_bit (gdbarch, 64);
1118       set_gdbarch_long_double_bit (gdbarch, 64);
1119       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
1120       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
1121     }
1122   else
1123     {
1124       set_gdbarch_double_bit (gdbarch, 32);
1125       set_gdbarch_long_double_bit (gdbarch, 32);
1126       set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
1127       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
1128     }
1129
1130   /* DWARF register mapping.  */
1131   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rx_dwarf_reg_to_regnum);
1132
1133   /* Frame unwinding.  */
1134   frame_unwind_append_unwinder (gdbarch, &rx_exception_unwind);
1135   dwarf2_append_unwinders (gdbarch);
1136   frame_unwind_append_unwinder (gdbarch, &rx_frame_unwind);
1137
1138   /* Methods for saving / extracting a dummy frame's ID.
1139      The ID's stack address must match the SP value returned by
1140      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
1141   set_gdbarch_dummy_id (gdbarch, rx_dummy_id);
1142   set_gdbarch_push_dummy_call (gdbarch, rx_push_dummy_call);
1143   set_gdbarch_return_value (gdbarch, rx_return_value);
1144
1145   /* Virtual tables.  */
1146   set_gdbarch_vbit_in_delta (gdbarch, 1);
1147
1148   return gdbarch;
1149 }
1150
1151 /* -Wmissing-prototypes */
1152 extern initialize_file_ftype _initialize_rx_tdep;
1153
1154 /* Register the above initialization routine.  */
1155
1156 void
1157 _initialize_rx_tdep (void)
1158 {
1159   register_gdbarch_init (bfd_arch_rx, rx_gdbarch_init);
1160 }