OSDN Git Service

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