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