OSDN Git Service

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