Turn on dwarf2 unwinding for Renesas RX architecture
[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_PSW_REGNUM = 18,
49   RX_PC_REGNUM = 19,
50   RX_ACC_REGNUM = 25,
51   RX_NUM_REGS = 26
52 };
53
54 /* Architecture specific data.  */
55 struct gdbarch_tdep
56 {
57   /* The ELF header flags specify the multilib used.  */
58   int elf_flags;
59 };
60
61 /* This structure holds the results of a prologue analysis.  */
62 struct rx_prologue
63 {
64   /* The offset from the frame base to the stack pointer --- always
65      zero or negative.
66
67      Calling this a "size" is a bit misleading, but given that the
68      stack grows downwards, using offsets for everything keeps one
69      from going completely sign-crazy: you never change anything's
70      sign for an ADD instruction; always change the second operand's
71      sign for a SUB instruction; and everything takes care of
72      itself.  */
73   int frame_size;
74
75   /* Non-zero if this function has initialized the frame pointer from
76      the stack pointer, zero otherwise.  */
77   int has_frame_ptr;
78
79   /* If has_frame_ptr is non-zero, this is the offset from the frame
80      base to where the frame pointer points.  This is always zero or
81      negative.  */
82   int frame_ptr_offset;
83
84   /* The address of the first instruction at which the frame has been
85      set up and the arguments are where the debug info says they are
86      --- as best as we can tell.  */
87   CORE_ADDR prologue_end;
88
89   /* reg_offset[R] is the offset from the CFA at which register R is
90      saved, or 1 if register R has not been saved.  (Real values are
91      always zero or negative.)  */
92   int reg_offset[RX_NUM_REGS];
93 };
94
95 /* Implement the "register_name" gdbarch method.  */
96 static const char *
97 rx_register_name (struct gdbarch *gdbarch, int regnr)
98 {
99   static const char *const reg_names[] = {
100     "r0",
101     "r1",
102     "r2",
103     "r3",
104     "r4",
105     "r5",
106     "r6",
107     "r7",
108     "r8",
109     "r9",
110     "r10",
111     "r11",
112     "r12",
113     "r13",
114     "r14",
115     "r15",
116     "usp",
117     "isp",
118     "psw",
119     "pc",
120     "intb",
121     "bpsw",
122     "bpc",
123     "fintv",
124     "fpsw",
125     "acc"
126   };
127
128   return reg_names[regnr];
129 }
130
131 /* Implement the "register_type" gdbarch method.  */
132 static struct type *
133 rx_register_type (struct gdbarch *gdbarch, int reg_nr)
134 {
135   if (reg_nr == RX_PC_REGNUM)
136     return builtin_type (gdbarch)->builtin_func_ptr;
137   else if (reg_nr == RX_ACC_REGNUM)
138     return builtin_type (gdbarch)->builtin_unsigned_long_long;
139   else
140     return builtin_type (gdbarch)->builtin_unsigned_long;
141 }
142
143
144 /* Function for finding saved registers in a 'struct pv_area'; this
145    function is passed to pv_area_scan.
146
147    If VALUE is a saved register, ADDR says it was saved at a constant
148    offset from the frame base, and SIZE indicates that the whole
149    register was saved, record its offset.  */
150 static void
151 check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
152 {
153   struct rx_prologue *result = (struct rx_prologue *) result_untyped;
154
155   if (value.kind == pvk_register
156       && value.k == 0
157       && pv_is_register (addr, RX_SP_REGNUM)
158       && size == register_size (target_gdbarch (), value.reg))
159     result->reg_offset[value.reg] = addr.k;
160 }
161
162 /* Define a "handle" struct for fetching the next opcode.  */
163 struct rx_get_opcode_byte_handle
164 {
165   CORE_ADDR pc;
166 };
167
168 /* Fetch a byte on behalf of the opcode decoder.  HANDLE contains
169    the memory address of the next byte to fetch.  If successful,
170    the address in the handle is updated and the byte fetched is
171    returned as the value of the function.  If not successful, -1
172    is returned.  */
173 static int
174 rx_get_opcode_byte (void *handle)
175 {
176   struct rx_get_opcode_byte_handle *opcdata = handle;
177   int status;
178   gdb_byte byte;
179
180   status = target_read_memory (opcdata->pc, &byte, 1);
181   if (status == 0)
182     {
183       opcdata->pc += 1;
184       return byte;
185     }
186   else
187     return -1;
188 }
189
190 /* Analyze a prologue starting at START_PC, going no further than
191    LIMIT_PC.  Fill in RESULT as appropriate.  */
192 static void
193 rx_analyze_prologue (CORE_ADDR start_pc,
194                      CORE_ADDR limit_pc, struct rx_prologue *result)
195 {
196   CORE_ADDR pc, next_pc;
197   int rn;
198   pv_t reg[RX_NUM_REGS];
199   struct pv_area *stack;
200   struct cleanup *back_to;
201   CORE_ADDR after_last_frame_setup_insn = start_pc;
202
203   memset (result, 0, sizeof (*result));
204
205   for (rn = 0; rn < RX_NUM_REGS; rn++)
206     {
207       reg[rn] = pv_register (rn, 0);
208       result->reg_offset[rn] = 1;
209     }
210
211   stack = make_pv_area (RX_SP_REGNUM, gdbarch_addr_bit (target_gdbarch ()));
212   back_to = make_cleanup_free_pv_area (stack);
213
214   /* The call instruction has saved the return address on the stack.  */
215   reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
216   pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[RX_PC_REGNUM]);
217
218   pc = start_pc;
219   while (pc < limit_pc)
220     {
221       int bytes_read;
222       struct rx_get_opcode_byte_handle opcode_handle;
223       RX_Opcode_Decoded opc;
224
225       opcode_handle.pc = pc;
226       bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
227                                      &opcode_handle);
228       next_pc = pc + bytes_read;
229
230       if (opc.id == RXO_pushm   /* pushm r1, r2 */
231           && opc.op[1].type == RX_Operand_Register
232           && opc.op[2].type == RX_Operand_Register)
233         {
234           int r1, r2;
235           int r;
236
237           r1 = opc.op[1].reg;
238           r2 = opc.op[2].reg;
239           for (r = r2; r >= r1; r--)
240             {
241               reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
242               pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[r]);
243             }
244           after_last_frame_setup_insn = next_pc;
245         }
246       else if (opc.id == RXO_mov        /* mov.l rdst, rsrc */
247                && opc.op[0].type == RX_Operand_Register
248                && opc.op[1].type == RX_Operand_Register
249                && opc.size == RX_Long)
250         {
251           int rdst, rsrc;
252
253           rdst = opc.op[0].reg;
254           rsrc = opc.op[1].reg;
255           reg[rdst] = reg[rsrc];
256           if (rdst == RX_FP_REGNUM && rsrc == RX_SP_REGNUM)
257             after_last_frame_setup_insn = next_pc;
258         }
259       else if (opc.id == RXO_mov        /* mov.l rsrc, [-SP] */
260                && opc.op[0].type == RX_Operand_Predec
261                && opc.op[0].reg == RX_SP_REGNUM
262                && opc.op[1].type == RX_Operand_Register
263                && opc.size == RX_Long)
264         {
265           int rsrc;
266
267           rsrc = opc.op[1].reg;
268           reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
269           pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[rsrc]);
270           after_last_frame_setup_insn = next_pc;
271         }
272       else if (opc.id == RXO_add        /* add #const, rsrc, rdst */
273                && opc.op[0].type == RX_Operand_Register
274                && opc.op[1].type == RX_Operand_Immediate
275                && opc.op[2].type == RX_Operand_Register)
276         {
277           int rdst = opc.op[0].reg;
278           int addend = opc.op[1].addend;
279           int rsrc = opc.op[2].reg;
280           reg[rdst] = pv_add_constant (reg[rsrc], addend);
281           /* Negative adjustments to the stack pointer or frame pointer
282              are (most likely) part of the prologue.  */
283           if ((rdst == RX_SP_REGNUM || rdst == RX_FP_REGNUM) && addend < 0)
284             after_last_frame_setup_insn = next_pc;
285         }
286       else if (opc.id == RXO_mov
287                && opc.op[0].type == RX_Operand_Indirect
288                && opc.op[1].type == RX_Operand_Register
289                && opc.size == RX_Long
290                && (opc.op[0].reg == RX_SP_REGNUM
291                    || opc.op[0].reg == RX_FP_REGNUM)
292                && (RX_R1_REGNUM <= opc.op[1].reg
293                    && opc.op[1].reg <= RX_R4_REGNUM))
294         {
295           /* This moves an argument register to the stack.  Don't
296              record it, but allow it to be a part of the prologue.  */
297         }
298       else if (opc.id == RXO_branch
299                && opc.op[0].type == RX_Operand_Immediate
300                && next_pc < opc.op[0].addend)
301         {
302           /* When a loop appears as the first statement of a function
303              body, gcc 4.x will use a BRA instruction to branch to the
304              loop condition checking code.  This BRA instruction is
305              marked as part of the prologue.  We therefore set next_pc
306              to this branch target and also stop the prologue scan.
307              The instructions at and beyond the branch target should
308              no longer be associated with the prologue.
309
310              Note that we only consider forward branches here.  We
311              presume that a forward branch is being used to skip over
312              a loop body.
313
314              A backwards branch is covered by the default case below.
315              If we were to encounter a backwards branch, that would
316              most likely mean that we've scanned through a loop body.
317              We definitely want to stop the prologue scan when this
318              happens and that is precisely what is done by the default
319              case below.  */
320
321           after_last_frame_setup_insn = opc.op[0].addend;
322           break;                /* Scan no further if we hit this case.  */
323         }
324       else
325         {
326           /* Terminate the prologue scan.  */
327           break;
328         }
329
330       pc = next_pc;
331     }
332
333   /* Is the frame size (offset, really) a known constant?  */
334   if (pv_is_register (reg[RX_SP_REGNUM], RX_SP_REGNUM))
335     result->frame_size = reg[RX_SP_REGNUM].k;
336
337   /* Was the frame pointer initialized?  */
338   if (pv_is_register (reg[RX_FP_REGNUM], RX_SP_REGNUM))
339     {
340       result->has_frame_ptr = 1;
341       result->frame_ptr_offset = reg[RX_FP_REGNUM].k;
342     }
343
344   /* Record where all the registers were saved.  */
345   pv_area_scan (stack, check_for_saved, (void *) result);
346
347   result->prologue_end = after_last_frame_setup_insn;
348
349   do_cleanups (back_to);
350 }
351
352
353 /* Implement the "skip_prologue" gdbarch method.  */
354 static CORE_ADDR
355 rx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
356 {
357   const char *name;
358   CORE_ADDR func_addr, func_end;
359   struct rx_prologue p;
360
361   /* Try to find the extent of the function that contains PC.  */
362   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
363     return pc;
364
365   rx_analyze_prologue (pc, func_end, &p);
366   return p.prologue_end;
367 }
368
369 /* Given a frame described by THIS_FRAME, decode the prologue of its
370    associated function if there is not cache entry as specified by
371    THIS_PROLOGUE_CACHE.  Save the decoded prologue in the cache and
372    return that struct as the value of this function.  */
373 static struct rx_prologue *
374 rx_analyze_frame_prologue (struct frame_info *this_frame,
375                            void **this_prologue_cache)
376 {
377   if (!*this_prologue_cache)
378     {
379       CORE_ADDR func_start, stop_addr;
380
381       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct rx_prologue);
382
383       func_start = get_frame_func (this_frame);
384       stop_addr = get_frame_pc (this_frame);
385
386       /* If we couldn't find any function containing the PC, then
387          just initialize the prologue cache, but don't do anything.  */
388       if (!func_start)
389         stop_addr = func_start;
390
391       rx_analyze_prologue (func_start, stop_addr, *this_prologue_cache);
392     }
393
394   return *this_prologue_cache;
395 }
396
397 /* Given the next frame and a prologue cache, return this frame's
398    base.  */
399 static CORE_ADDR
400 rx_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
401 {
402   struct rx_prologue *p
403     = rx_analyze_frame_prologue (this_frame, this_prologue_cache);
404
405   /* In functions that use alloca, the distance between the stack
406      pointer and the frame base varies dynamically, so we can't use
407      the SP plus static information like prologue analysis to find the
408      frame base.  However, such functions must have a frame pointer,
409      to be able to restore the SP on exit.  So whenever we do have a
410      frame pointer, use that to find the base.  */
411   if (p->has_frame_ptr)
412     {
413       CORE_ADDR fp = get_frame_register_unsigned (this_frame, RX_FP_REGNUM);
414       return fp - p->frame_ptr_offset;
415     }
416   else
417     {
418       CORE_ADDR sp = get_frame_register_unsigned (this_frame, RX_SP_REGNUM);
419       return sp - p->frame_size;
420     }
421 }
422
423 /* Implement the "frame_this_id" method for unwinding frames.  */
424 static void
425 rx_frame_this_id (struct frame_info *this_frame,
426                   void **this_prologue_cache, struct frame_id *this_id)
427 {
428   *this_id = frame_id_build (rx_frame_base (this_frame, this_prologue_cache),
429                              get_frame_func (this_frame));
430 }
431
432 /* Implement the "frame_prev_register" method for unwinding frames.  */
433 static struct value *
434 rx_frame_prev_register (struct frame_info *this_frame,
435                         void **this_prologue_cache, int regnum)
436 {
437   struct rx_prologue *p
438     = rx_analyze_frame_prologue (this_frame, this_prologue_cache);
439   CORE_ADDR frame_base = rx_frame_base (this_frame, this_prologue_cache);
440   int reg_size = register_size (get_frame_arch (this_frame), regnum);
441
442   if (regnum == RX_SP_REGNUM)
443     return frame_unwind_got_constant (this_frame, regnum, frame_base);
444
445   /* If prologue analysis says we saved this register somewhere,
446      return a description of the stack slot holding it.  */
447   else if (p->reg_offset[regnum] != 1)
448     return frame_unwind_got_memory (this_frame, regnum,
449                                     frame_base + p->reg_offset[regnum]);
450
451   /* Otherwise, presume we haven't changed the value of this
452      register, and get it from the next frame.  */
453   else
454     return frame_unwind_got_register (this_frame, regnum, regnum);
455 }
456
457 static const struct frame_unwind rx_frame_unwind = {
458   NORMAL_FRAME,
459   default_frame_unwind_stop_reason,
460   rx_frame_this_id,
461   rx_frame_prev_register,
462   NULL,
463   default_frame_sniffer
464 };
465
466 /* Implement the "unwind_pc" gdbarch method.  */
467 static CORE_ADDR
468 rx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
469 {
470   ULONGEST pc;
471
472   pc = frame_unwind_register_unsigned (this_frame, RX_PC_REGNUM);
473   return pc;
474 }
475
476 /* Implement the "unwind_sp" gdbarch method.  */
477 static CORE_ADDR
478 rx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
479 {
480   ULONGEST sp;
481
482   sp = frame_unwind_register_unsigned (this_frame, RX_SP_REGNUM);
483   return sp;
484 }
485
486 /* Implement the "dummy_id" gdbarch method.  */
487 static struct frame_id
488 rx_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
489 {
490   return
491     frame_id_build (get_frame_register_unsigned (this_frame, RX_SP_REGNUM),
492                     get_frame_pc (this_frame));
493 }
494
495 /* Implement the "push_dummy_call" gdbarch method.  */
496 static CORE_ADDR
497 rx_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
498                     struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
499                     struct value **args, CORE_ADDR sp, int struct_return,
500                     CORE_ADDR struct_addr)
501 {
502   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
503   int write_pass;
504   int sp_off = 0;
505   CORE_ADDR cfa;
506   int num_register_candidate_args;
507
508   struct type *func_type = value_type (function);
509
510   /* Dereference function pointer types.  */
511   while (TYPE_CODE (func_type) == TYPE_CODE_PTR)
512     func_type = TYPE_TARGET_TYPE (func_type);
513
514   /* The end result had better be a function or a method.  */
515   gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC
516               || TYPE_CODE (func_type) == TYPE_CODE_METHOD);
517
518   /* Functions with a variable number of arguments have all of their
519      variable arguments and the last non-variable argument passed
520      on the stack.
521
522      Otherwise, we can pass up to four arguments on the stack.
523
524      Once computed, we leave this value alone.  I.e. we don't update
525      it in case of a struct return going in a register or an argument
526      requiring multiple registers, etc.  We rely instead on the value
527      of the ``arg_reg'' variable to get these other details correct.  */
528
529   if (TYPE_VARARGS (func_type))
530     num_register_candidate_args = TYPE_NFIELDS (func_type) - 1;
531   else
532     num_register_candidate_args = 4;
533
534   /* We make two passes; the first does the stack allocation,
535      the second actually stores the arguments.  */
536   for (write_pass = 0; write_pass <= 1; write_pass++)
537     {
538       int i;
539       int arg_reg = RX_R1_REGNUM;
540
541       if (write_pass)
542         sp = align_down (sp - sp_off, 4);
543       sp_off = 0;
544
545       if (struct_return)
546         {
547           struct type *return_type = TYPE_TARGET_TYPE (func_type);
548
549           gdb_assert (TYPE_CODE (return_type) == TYPE_CODE_STRUCT
550                       || TYPE_CODE (func_type) == TYPE_CODE_UNION);
551
552           if (TYPE_LENGTH (return_type) > 16
553               || TYPE_LENGTH (return_type) % 4 != 0)
554             {
555               if (write_pass)
556                 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
557                                                 struct_addr);
558             }
559         }
560
561       /* Push the arguments.  */
562       for (i = 0; i < nargs; i++)
563         {
564           struct value *arg = args[i];
565           const gdb_byte *arg_bits = value_contents_all (arg);
566           struct type *arg_type = check_typedef (value_type (arg));
567           ULONGEST arg_size = TYPE_LENGTH (arg_type);
568
569           if (i == 0 && struct_addr != 0 && !struct_return
570               && TYPE_CODE (arg_type) == TYPE_CODE_PTR
571               && extract_unsigned_integer (arg_bits, 4,
572                                            byte_order) == struct_addr)
573             {
574               /* This argument represents the address at which C++ (and
575                  possibly other languages) store their return value.
576                  Put this value in R15.  */
577               if (write_pass)
578                 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
579                                                 struct_addr);
580             }
581           else if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT
582                    && TYPE_CODE (arg_type) != TYPE_CODE_UNION)
583             {
584               /* Argument is a scalar.  */
585               if (arg_size == 8)
586                 {
587                   if (i < num_register_candidate_args
588                       && arg_reg <= RX_R4_REGNUM - 1)
589                     {
590                       /* If argument registers are going to be used to pass
591                          an 8 byte scalar, the ABI specifies that two registers
592                          must be available.  */
593                       if (write_pass)
594                         {
595                           regcache_cooked_write_unsigned (regcache, arg_reg,
596                                                           extract_unsigned_integer
597                                                           (arg_bits, 4,
598                                                            byte_order));
599                           regcache_cooked_write_unsigned (regcache,
600                                                           arg_reg + 1,
601                                                           extract_unsigned_integer
602                                                           (arg_bits + 4, 4,
603                                                            byte_order));
604                         }
605                       arg_reg += 2;
606                     }
607                   else
608                     {
609                       sp_off = align_up (sp_off, 4);
610                       /* Otherwise, pass the 8 byte scalar on the stack.  */
611                       if (write_pass)
612                         write_memory (sp + sp_off, arg_bits, 8);
613                       sp_off += 8;
614                     }
615                 }
616               else
617                 {
618                   ULONGEST u;
619
620                   gdb_assert (arg_size <= 4);
621
622                   u =
623                     extract_unsigned_integer (arg_bits, arg_size, byte_order);
624
625                   if (i < num_register_candidate_args
626                       && arg_reg <= RX_R4_REGNUM)
627                     {
628                       if (write_pass)
629                         regcache_cooked_write_unsigned (regcache, arg_reg, u);
630                       arg_reg += 1;
631                     }
632                   else
633                     {
634                       int p_arg_size = 4;
635
636                       if (TYPE_PROTOTYPED (func_type)
637                           && i < TYPE_NFIELDS (func_type))
638                         {
639                           struct type *p_arg_type =
640                             TYPE_FIELD_TYPE (func_type, i);
641                           p_arg_size = TYPE_LENGTH (p_arg_type);
642                         }
643
644                       sp_off = align_up (sp_off, p_arg_size);
645
646                       if (write_pass)
647                         write_memory_unsigned_integer (sp + sp_off,
648                                                        p_arg_size, byte_order,
649                                                        u);
650                       sp_off += p_arg_size;
651                     }
652                 }
653             }
654           else
655             {
656               /* Argument is a struct or union.  Pass as much of the struct
657                  in registers, if possible.  Pass the rest on the stack.  */
658               while (arg_size > 0)
659                 {
660                   if (i < num_register_candidate_args
661                       && arg_reg <= RX_R4_REGNUM
662                       && arg_size <= 4 * (RX_R4_REGNUM - arg_reg + 1)
663                       && arg_size % 4 == 0)
664                     {
665                       int len = min (arg_size, 4);
666
667                       if (write_pass)
668                         regcache_cooked_write_unsigned (regcache, arg_reg,
669                                                         extract_unsigned_integer
670                                                         (arg_bits, len,
671                                                          byte_order));
672                       arg_bits += len;
673                       arg_size -= len;
674                       arg_reg++;
675                     }
676                   else
677                     {
678                       sp_off = align_up (sp_off, 4);
679                       if (write_pass)
680                         write_memory (sp + sp_off, arg_bits, arg_size);
681                       sp_off += align_up (arg_size, 4);
682                       arg_size = 0;
683                     }
684                 }
685             }
686         }
687     }
688
689   /* Keep track of the stack address prior to pushing the return address.
690      This is the value that we'll return.  */
691   cfa = sp;
692
693   /* Push the return address.  */
694   sp = sp - 4;
695   write_memory_unsigned_integer (sp, 4, byte_order, bp_addr);
696
697   /* Update the stack pointer.  */
698   regcache_cooked_write_unsigned (regcache, RX_SP_REGNUM, sp);
699
700   return cfa;
701 }
702
703 /* Implement the "return_value" gdbarch method.  */
704 static enum return_value_convention
705 rx_return_value (struct gdbarch *gdbarch,
706                  struct value *function,
707                  struct type *valtype,
708                  struct regcache *regcache,
709                  gdb_byte *readbuf, const gdb_byte *writebuf)
710 {
711   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
712   ULONGEST valtype_len = TYPE_LENGTH (valtype);
713
714   if (TYPE_LENGTH (valtype) > 16
715       || ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
716            || TYPE_CODE (valtype) == TYPE_CODE_UNION)
717           && TYPE_LENGTH (valtype) % 4 != 0))
718     return RETURN_VALUE_STRUCT_CONVENTION;
719
720   if (readbuf)
721     {
722       ULONGEST u;
723       int argreg = RX_R1_REGNUM;
724       int offset = 0;
725
726       while (valtype_len > 0)
727         {
728           int len = min (valtype_len, 4);
729
730           regcache_cooked_read_unsigned (regcache, argreg, &u);
731           store_unsigned_integer (readbuf + offset, len, byte_order, u);
732           valtype_len -= len;
733           offset += len;
734           argreg++;
735         }
736     }
737
738   if (writebuf)
739     {
740       ULONGEST u;
741       int argreg = RX_R1_REGNUM;
742       int offset = 0;
743
744       while (valtype_len > 0)
745         {
746           int len = min (valtype_len, 4);
747
748           u = extract_unsigned_integer (writebuf + offset, len, byte_order);
749           regcache_cooked_write_unsigned (regcache, argreg, u);
750           valtype_len -= len;
751           offset += len;
752           argreg++;
753         }
754     }
755
756   return RETURN_VALUE_REGISTER_CONVENTION;
757 }
758
759 /* Implement the "breakpoint_from_pc" gdbarch method.  */
760 static const gdb_byte *
761 rx_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
762 {
763   static gdb_byte breakpoint[] = { 0x00 };
764   *lenptr = sizeof breakpoint;
765   return breakpoint;
766 }
767
768 /* Implement the dwarf_reg_to_regnum" gdbarch method.  */
769
770 static int
771 rx_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
772 {
773   if (0 <= reg && reg <= 15)
774     return reg;
775   else if (reg == 16)
776     return RX_PSW_REGNUM;
777   else if (reg == 17)
778     return RX_PC_REGNUM;
779   else
780     internal_error (__FILE__, __LINE__,
781                     _("Undefined dwarf2 register mapping of reg %d"),
782                     reg);
783 }
784
785 /* Allocate and initialize a gdbarch object.  */
786 static struct gdbarch *
787 rx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
788 {
789   struct gdbarch *gdbarch;
790   struct gdbarch_tdep *tdep;
791   int elf_flags;
792
793   /* Extract the elf_flags if available.  */
794   if (info.abfd != NULL
795       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
796     elf_flags = elf_elfheader (info.abfd)->e_flags;
797   else
798     elf_flags = 0;
799
800
801   /* Try to find the architecture in the list of already defined
802      architectures.  */
803   for (arches = gdbarch_list_lookup_by_info (arches, &info);
804        arches != NULL;
805        arches = gdbarch_list_lookup_by_info (arches->next, &info))
806     {
807       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
808         continue;
809
810       return arches->gdbarch;
811     }
812
813   /* None found, create a new architecture from the information
814      provided.  */
815   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
816   gdbarch = gdbarch_alloc (&info, tdep);
817   tdep->elf_flags = elf_flags;
818
819   set_gdbarch_num_regs (gdbarch, RX_NUM_REGS);
820   set_gdbarch_num_pseudo_regs (gdbarch, 0);
821   set_gdbarch_register_name (gdbarch, rx_register_name);
822   set_gdbarch_register_type (gdbarch, rx_register_type);
823   set_gdbarch_pc_regnum (gdbarch, RX_PC_REGNUM);
824   set_gdbarch_sp_regnum (gdbarch, RX_SP_REGNUM);
825   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
826   set_gdbarch_decr_pc_after_break (gdbarch, 1);
827   set_gdbarch_breakpoint_from_pc (gdbarch, rx_breakpoint_from_pc);
828   set_gdbarch_skip_prologue (gdbarch, rx_skip_prologue);
829
830   set_gdbarch_print_insn (gdbarch, print_insn_rx);
831
832   set_gdbarch_unwind_pc (gdbarch, rx_unwind_pc);
833   set_gdbarch_unwind_sp (gdbarch, rx_unwind_sp);
834
835   /* Target builtin data types.  */
836   set_gdbarch_char_signed (gdbarch, 0);
837   set_gdbarch_short_bit (gdbarch, 16);
838   set_gdbarch_int_bit (gdbarch, 32);
839   set_gdbarch_long_bit (gdbarch, 32);
840   set_gdbarch_long_long_bit (gdbarch, 64);
841   set_gdbarch_ptr_bit (gdbarch, 32);
842   set_gdbarch_float_bit (gdbarch, 32);
843   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
844   if (elf_flags & E_FLAG_RX_64BIT_DOUBLES)
845     {
846       set_gdbarch_double_bit (gdbarch, 64);
847       set_gdbarch_long_double_bit (gdbarch, 64);
848       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
849       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
850     }
851   else
852     {
853       set_gdbarch_double_bit (gdbarch, 32);
854       set_gdbarch_long_double_bit (gdbarch, 32);
855       set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
856       set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
857     }
858
859   /* DWARF register mapping.  */
860   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rx_dwarf_reg_to_regnum);
861
862   /* Frame unwinding.  */
863   dwarf2_append_unwinders (gdbarch);
864   frame_unwind_append_unwinder (gdbarch, &rx_frame_unwind);
865
866   /* Methods for saving / extracting a dummy frame's ID.
867      The ID's stack address must match the SP value returned by
868      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
869   set_gdbarch_dummy_id (gdbarch, rx_dummy_id);
870   set_gdbarch_push_dummy_call (gdbarch, rx_push_dummy_call);
871   set_gdbarch_return_value (gdbarch, rx_return_value);
872
873   /* Virtual tables.  */
874   set_gdbarch_vbit_in_delta (gdbarch, 1);
875
876   return gdbarch;
877 }
878
879 /* -Wmissing-prototypes */
880 extern initialize_file_ftype _initialize_rx_tdep;
881
882 /* Register the above initialization routine.  */
883
884 void
885 _initialize_rx_tdep (void)
886 {
887   register_gdbarch_init (bfd_arch_rx, rx_gdbarch_init);
888 }