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