gdb/m32r-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for Renesas M32R, for GDB.

  2.    Copyright (C) 1996-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "frame-unwind.h"
  17. #include "frame-base.h"
  18. #include "symtab.h"
  19. #include "gdbtypes.h"
  20. #include "gdbcmd.h"
  21. #include "gdbcore.h"
  22. #include "value.h"
  23. #include "inferior.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "osabi.h"
  27. #include "language.h"
  28. #include "arch-utils.h"
  29. #include "regcache.h"
  30. #include "trad-frame.h"
  31. #include "dis-asm.h"
  32. #include "objfiles.h"

  33. #include "m32r-tdep.h"

  34. /* Local functions */

  35. extern void _initialize_m32r_tdep (void);

  36. static CORE_ADDR
  37. m32r_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  38. {
  39.   /* Align to the size of an instruction (so that they can safely be
  40.      pushed onto the stack.  */
  41.   return sp & ~3;
  42. }


  43. /* Breakpoints

  44.    The little endian mode of M32R is unique.  In most of architectures,
  45.    two 16-bit instructions, A and B, are placed as the following:

  46.    Big endian:
  47.    A0 A1 B0 B1

  48.    Little endian:
  49.    A1 A0 B1 B0

  50.    In M32R, they are placed like this:

  51.    Big endian:
  52.    A0 A1 B0 B1

  53.    Little endian:
  54.    B1 B0 A1 A0

  55.    This is because M32R always fetches instructions in 32-bit.

  56.    The following functions take care of this behavior.  */

  57. static int
  58. m32r_memory_insert_breakpoint (struct gdbarch *gdbarch,
  59.                                struct bp_target_info *bp_tgt)
  60. {
  61.   CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
  62.   int val;
  63.   gdb_byte buf[4];
  64.   gdb_byte contents_cache[4];
  65.   gdb_byte bp_entry[] = { 0x10, 0xf1 };        /* dpt */

  66.   /* Save the memory contents.  */
  67.   val = target_read_memory (addr & 0xfffffffc, contents_cache, 4);
  68.   if (val != 0)
  69.     return val;                        /* return error */

  70.   memcpy (bp_tgt->shadow_contents, contents_cache, 4);
  71.   bp_tgt->placed_size = bp_tgt->shadow_len = 4;

  72.   /* Determine appropriate breakpoint contents and size for this address.  */
  73.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  74.     {
  75.       if ((addr & 3) == 0)
  76.         {
  77.           buf[0] = bp_entry[0];
  78.           buf[1] = bp_entry[1];
  79.           buf[2] = contents_cache[2] & 0x7f;
  80.           buf[3] = contents_cache[3];
  81.         }
  82.       else
  83.         {
  84.           buf[0] = contents_cache[0];
  85.           buf[1] = contents_cache[1];
  86.           buf[2] = bp_entry[0];
  87.           buf[3] = bp_entry[1];
  88.         }
  89.     }
  90.   else                                /* little-endian */
  91.     {
  92.       if ((addr & 3) == 0)
  93.         {
  94.           buf[0] = contents_cache[0];
  95.           buf[1] = contents_cache[1] & 0x7f;
  96.           buf[2] = bp_entry[1];
  97.           buf[3] = bp_entry[0];
  98.         }
  99.       else
  100.         {
  101.           buf[0] = bp_entry[1];
  102.           buf[1] = bp_entry[0];
  103.           buf[2] = contents_cache[2];
  104.           buf[3] = contents_cache[3];
  105.         }
  106.     }

  107.   /* Write the breakpoint.  */
  108.   val = target_write_memory (addr & 0xfffffffc, buf, 4);
  109.   return val;
  110. }

  111. static int
  112. m32r_memory_remove_breakpoint (struct gdbarch *gdbarch,
  113.                                struct bp_target_info *bp_tgt)
  114. {
  115.   CORE_ADDR addr = bp_tgt->placed_address;
  116.   int val;
  117.   gdb_byte buf[4];
  118.   gdb_byte *contents_cache = bp_tgt->shadow_contents;

  119.   buf[0] = contents_cache[0];
  120.   buf[1] = contents_cache[1];
  121.   buf[2] = contents_cache[2];
  122.   buf[3] = contents_cache[3];

  123.   /* Remove parallel bit.  */
  124.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  125.     {
  126.       if ((buf[0] & 0x80) == 0 && (buf[2] & 0x80) != 0)
  127.         buf[2] &= 0x7f;
  128.     }
  129.   else                                /* little-endian */
  130.     {
  131.       if ((buf[3] & 0x80) == 0 && (buf[1] & 0x80) != 0)
  132.         buf[1] &= 0x7f;
  133.     }

  134.   /* Write contents.  */
  135.   val = target_write_raw_memory (addr & 0xfffffffc, buf, 4);
  136.   return val;
  137. }

  138. static const gdb_byte *
  139. m32r_breakpoint_from_pc (struct gdbarch *gdbarch,
  140.                          CORE_ADDR *pcptr, int *lenptr)
  141. {
  142.   static gdb_byte be_bp_entry[] = {
  143.     0x10, 0xf1, 0x70, 0x00
  144.   };        /* dpt -> nop */
  145.   static gdb_byte le_bp_entry[] = {
  146.     0x00, 0x70, 0xf1, 0x10
  147.   };        /* dpt -> nop */
  148.   gdb_byte *bp;

  149.   /* Determine appropriate breakpoint.  */
  150.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  151.     {
  152.       if ((*pcptr & 3) == 0)
  153.         {
  154.           bp = be_bp_entry;
  155.           *lenptr = 4;
  156.         }
  157.       else
  158.         {
  159.           bp = be_bp_entry;
  160.           *lenptr = 2;
  161.         }
  162.     }
  163.   else
  164.     {
  165.       if ((*pcptr & 3) == 0)
  166.         {
  167.           bp = le_bp_entry;
  168.           *lenptr = 4;
  169.         }
  170.       else
  171.         {
  172.           bp = le_bp_entry + 2;
  173.           *lenptr = 2;
  174.         }
  175.     }

  176.   return bp;
  177. }


  178. char *m32r_register_names[] = {
  179.   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  180.   "r8", "r9", "r10", "r11", "r12", "fp", "lr", "sp",
  181.   "psw", "cbr", "spi", "spu", "bpc", "pc", "accl", "acch",
  182.   "evb"
  183. };

  184. static const char *
  185. m32r_register_name (struct gdbarch *gdbarch, int reg_nr)
  186. {
  187.   if (reg_nr < 0)
  188.     return NULL;
  189.   if (reg_nr >= M32R_NUM_REGS)
  190.     return NULL;
  191.   return m32r_register_names[reg_nr];
  192. }


  193. /* Return the GDB type object for the "standard" data type
  194.    of data in register N.  */

  195. static struct type *
  196. m32r_register_type (struct gdbarch *gdbarch, int reg_nr)
  197. {
  198.   if (reg_nr == M32R_PC_REGNUM)
  199.     return builtin_type (gdbarch)->builtin_func_ptr;
  200.   else if (reg_nr == M32R_SP_REGNUM || reg_nr == M32R_FP_REGNUM)
  201.     return builtin_type (gdbarch)->builtin_data_ptr;
  202.   else
  203.     return builtin_type (gdbarch)->builtin_int32;
  204. }


  205. /* Write into appropriate registers a function return value
  206.    of type TYPE, given in virtual format.

  207.    Things always get returned in RET1_REGNUM, RET2_REGNUM.  */

  208. static void
  209. m32r_store_return_value (struct type *type, struct regcache *regcache,
  210.                          const void *valbuf)
  211. {
  212.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  213.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  214.   CORE_ADDR regval;
  215.   int len = TYPE_LENGTH (type);

  216.   regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
  217.   regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);

  218.   if (len > 4)
  219.     {
  220.       regval = extract_unsigned_integer ((gdb_byte *) valbuf + 4,
  221.                                          len - 4, byte_order);
  222.       regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
  223.     }
  224. }

  225. /* This is required by skip_prologue.  The results of decoding a prologue
  226.    should be cached because this thrashing is getting nuts.  */

  227. static int
  228. decode_prologue (struct gdbarch *gdbarch,
  229.                  CORE_ADDR start_pc, CORE_ADDR scan_limit,
  230.                  CORE_ADDR *pl_endptr, unsigned long *framelength)
  231. {
  232.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  233.   unsigned long framesize;
  234.   int insn;
  235.   int op1;
  236.   CORE_ADDR after_prologue = 0;
  237.   CORE_ADDR after_push = 0;
  238.   CORE_ADDR after_stack_adjust = 0;
  239.   CORE_ADDR current_pc;
  240.   LONGEST return_value;

  241.   framesize = 0;
  242.   after_prologue = 0;

  243.   for (current_pc = start_pc; current_pc < scan_limit; current_pc += 2)
  244.     {
  245.       /* Check if current pc's location is readable.  */
  246.       if (!safe_read_memory_integer (current_pc, 2, byte_order, &return_value))
  247.         return -1;

  248.       insn = read_memory_unsigned_integer (current_pc, 2, byte_order);

  249.       if (insn == 0x0000)
  250.         break;

  251.       /* If this is a 32 bit instruction, we dont want to examine its
  252.          immediate data as though it were an instruction.  */
  253.       if (current_pc & 0x02)
  254.         {
  255.           /* Decode this instruction further.  */
  256.           insn &= 0x7fff;
  257.         }
  258.       else
  259.         {
  260.           if (insn & 0x8000)
  261.             {
  262.               if (current_pc == scan_limit)
  263.                 scan_limit += 2;        /* extend the search */

  264.               current_pc += 2;        /* skip the immediate data */

  265.               /* Check if current pc's location is readable.  */
  266.               if (!safe_read_memory_integer (current_pc, 2, byte_order,
  267.                                              &return_value))
  268.                 return -1;

  269.               if (insn == 0x8faf)        /* add3 sp, sp, xxxx */
  270.                 /* add 16 bit sign-extended offset */
  271.                 {
  272.                   framesize +=
  273.                     -((short) read_memory_unsigned_integer (current_pc,
  274.                                                             2, byte_order));
  275.                 }
  276.               else
  277.                 {
  278.                   if (((insn >> 8) == 0xe4) /* ld24 r4, xxxxxx; sub sp, r4 */
  279.                       && safe_read_memory_integer (current_pc + 2,
  280.                                                    2, byte_order,
  281.                                                    &return_value)
  282.                       && read_memory_unsigned_integer (current_pc + 2,
  283.                                                        2, byte_order)
  284.                          == 0x0f24)
  285.                     {
  286.                       /* Subtract 24 bit sign-extended negative-offset.  */
  287.                       insn = read_memory_unsigned_integer (current_pc - 2,
  288.                                                            4, byte_order);
  289.                       if (insn & 0x00800000)        /* sign extend */
  290.                         insn |= 0xff000000;        /* negative */
  291.                       else
  292.                         insn &= 0x00ffffff;        /* positive */
  293.                       framesize += insn;
  294.                     }
  295.                 }
  296.               after_push = current_pc + 2;
  297.               continue;
  298.             }
  299.         }
  300.       op1 = insn & 0xf000;        /* Isolate just the first nibble.  */

  301.       if ((insn & 0xf0ff) == 0x207f)
  302.         {                        /* st reg, @-sp */
  303.           int regno;
  304.           framesize += 4;
  305.           regno = ((insn >> 8) & 0xf);
  306.           after_prologue = 0;
  307.           continue;
  308.         }
  309.       if ((insn >> 8) == 0x4f)        /* addi sp, xx */
  310.         /* Add 8 bit sign-extended offset.  */
  311.         {
  312.           int stack_adjust = (signed char) (insn & 0xff);

  313.           /* there are probably two of these stack adjustments:
  314.              1) A negative one in the prologue, and
  315.              2) A positive one in the epilogue.
  316.              We are only interested in the first one.  */

  317.           if (stack_adjust < 0)
  318.             {
  319.               framesize -= stack_adjust;
  320.               after_prologue = 0;
  321.               /* A frameless function may have no "mv fp, sp".
  322.                  In that case, this is the end of the prologue.  */
  323.               after_stack_adjust = current_pc + 2;
  324.             }
  325.           continue;
  326.         }
  327.       if (insn == 0x1d8f)
  328.         {                        /* mv fp, sp */
  329.           after_prologue = current_pc + 2;
  330.           break;                /* end of stack adjustments */
  331.         }

  332.       /* Nop looks like a branch, continue explicitly.  */
  333.       if (insn == 0x7000)
  334.         {
  335.           after_prologue = current_pc + 2;
  336.           continue;                /* nop occurs between pushes.  */
  337.         }
  338.       /* End of prolog if any of these are trap instructions.  */
  339.       if ((insn & 0xfff0) == 0x10f0)
  340.         {
  341.           after_prologue = current_pc;
  342.           break;
  343.         }
  344.       /* End of prolog if any of these are branch instructions.  */
  345.       if ((op1 == 0x7000) || (op1 == 0xb000) || (op1 == 0xf000))
  346.         {
  347.           after_prologue = current_pc;
  348.           continue;
  349.         }
  350.       /* Some of the branch instructions are mixed with other types.  */
  351.       if (op1 == 0x1000)
  352.         {
  353.           int subop = insn & 0x0ff0;
  354.           if ((subop == 0x0ec0) || (subop == 0x0fc0))
  355.             {
  356.               after_prologue = current_pc;
  357.               continue;                /* jmp , jl */
  358.             }
  359.         }
  360.     }

  361.   if (framelength)
  362.     *framelength = framesize;

  363.   if (current_pc >= scan_limit)
  364.     {
  365.       if (pl_endptr)
  366.         {
  367.           if (after_stack_adjust != 0)
  368.             /* We did not find a "mv fp,sp", but we DID find
  369.                a stack_adjust.  Is it safe to use that as the
  370.                end of the prologue?  I just don't know.  */
  371.             {
  372.               *pl_endptr = after_stack_adjust;
  373.             }
  374.           else if (after_push != 0)
  375.             /* We did not find a "mv fp,sp", but we DID find
  376.                a push.  Is it safe to use that as the
  377.                end of the prologue?  I just don't know.  */
  378.             {
  379.               *pl_endptr = after_push;
  380.             }
  381.           else
  382.             /* We reached the end of the loop without finding the end
  383.                of the prologue.  No way to win -- we should report
  384.                failure.  The way we do that is to return the original
  385.                start_pc.  GDB will set a breakpoint at the start of
  386.                the function (etc.)  */
  387.             *pl_endptr = start_pc;
  388.         }
  389.       return 0;
  390.     }

  391.   if (after_prologue == 0)
  392.     after_prologue = current_pc;

  393.   if (pl_endptr)
  394.     *pl_endptr = after_prologue;

  395.   return 0;
  396. }                                /*  decode_prologue */

  397. /* Function: skip_prologue
  398.    Find end of function prologue.  */

  399. #define DEFAULT_SEARCH_LIMIT 128

  400. static CORE_ADDR
  401. m32r_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  402. {
  403.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  404.   CORE_ADDR func_addr, func_end;
  405.   struct symtab_and_line sal;
  406.   LONGEST return_value;

  407.   /* See what the symbol table says.  */

  408.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  409.     {
  410.       sal = find_pc_line (func_addr, 0);

  411.       if (sal.line != 0 && sal.end <= func_end)
  412.         {
  413.           func_end = sal.end;
  414.         }
  415.       else
  416.         /* Either there's no line info, or the line after the prologue is after
  417.            the end of the function.  In this case, there probably isn't a
  418.            prologue.  */
  419.         {
  420.           func_end = min (func_end, func_addr + DEFAULT_SEARCH_LIMIT);
  421.         }
  422.     }
  423.   else
  424.     func_end = pc + DEFAULT_SEARCH_LIMIT;

  425.   /* If pc's location is not readable, just quit.  */
  426.   if (!safe_read_memory_integer (pc, 4, byte_order, &return_value))
  427.     return pc;

  428.   /* Find the end of prologue.  */
  429.   if (decode_prologue (gdbarch, pc, func_end, &sal.end, NULL) < 0)
  430.     return pc;

  431.   return sal.end;
  432. }

  433. struct m32r_unwind_cache
  434. {
  435.   /* The previous frame's inner most stack address.  Used as this
  436.      frame ID's stack_addr.  */
  437.   CORE_ADDR prev_sp;
  438.   /* The frame's base, optionally used by the high-level debug info.  */
  439.   CORE_ADDR base;
  440.   int size;
  441.   /* How far the SP and r13 (FP) have been offset from the start of
  442.      the stack frame (as defined by the previous frame's stack
  443.      pointer).  */
  444.   LONGEST sp_offset;
  445.   LONGEST r13_offset;
  446.   int uses_frame;
  447.   /* Table indicating the location of each and every register.  */
  448.   struct trad_frame_saved_reg *saved_regs;
  449. };

  450. /* Put here the code to store, into fi->saved_regs, the addresses of
  451.    the saved registers of frame described by FRAME_INFO.  This
  452.    includes special registers such as pc and fp saved in special ways
  453.    in the stack frame.  sp is even more special: the address we return
  454.    for it IS the sp for the next frame.  */

  455. static struct m32r_unwind_cache *
  456. m32r_frame_unwind_cache (struct frame_info *this_frame,
  457.                          void **this_prologue_cache)
  458. {
  459.   CORE_ADDR pc, scan_limit;
  460.   ULONGEST prev_sp;
  461.   ULONGEST this_base;
  462.   unsigned long op;
  463.   int i;
  464.   struct m32r_unwind_cache *info;


  465.   if ((*this_prologue_cache))
  466.     return (*this_prologue_cache);

  467.   info = FRAME_OBSTACK_ZALLOC (struct m32r_unwind_cache);
  468.   (*this_prologue_cache) = info;
  469.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  470.   info->size = 0;
  471.   info->sp_offset = 0;
  472.   info->uses_frame = 0;

  473.   scan_limit = get_frame_pc (this_frame);
  474.   for (pc = get_frame_func (this_frame);
  475.        pc > 0 && pc < scan_limit; pc += 2)
  476.     {
  477.       if ((pc & 2) == 0)
  478.         {
  479.           op = get_frame_memory_unsigned (this_frame, pc, 4);
  480.           if ((op & 0x80000000) == 0x80000000)
  481.             {
  482.               /* 32-bit instruction */
  483.               if ((op & 0xffff0000) == 0x8faf0000)
  484.                 {
  485.                   /* add3 sp,sp,xxxx */
  486.                   short n = op & 0xffff;
  487.                   info->sp_offset += n;
  488.                 }
  489.               else if (((op >> 8) == 0xe4)
  490.                        && get_frame_memory_unsigned (this_frame, pc + 2,
  491.                                                      2) == 0x0f24)
  492.                 {
  493.                   /* ld24 r4, xxxxxx; sub sp, r4 */
  494.                   unsigned long n = op & 0xffffff;
  495.                   info->sp_offset += n;
  496.                   pc += 2;        /* skip sub instruction */
  497.                 }

  498.               if (pc == scan_limit)
  499.                 scan_limit += 2;        /* extend the search */
  500.               pc += 2;                /* skip the immediate data */
  501.               continue;
  502.             }
  503.         }

  504.       /* 16-bit instructions */
  505.       op = get_frame_memory_unsigned (this_frame, pc, 2) & 0x7fff;
  506.       if ((op & 0xf0ff) == 0x207f)
  507.         {
  508.           /* st rn, @-sp */
  509.           int regno = ((op >> 8) & 0xf);
  510.           info->sp_offset -= 4;
  511.           info->saved_regs[regno].addr = info->sp_offset;
  512.         }
  513.       else if ((op & 0xff00) == 0x4f00)
  514.         {
  515.           /* addi sp, xx */
  516.           int n = (signed char) (op & 0xff);
  517.           info->sp_offset += n;
  518.         }
  519.       else if (op == 0x1d8f)
  520.         {
  521.           /* mv fp, sp */
  522.           info->uses_frame = 1;
  523.           info->r13_offset = info->sp_offset;
  524.           break;                /* end of stack adjustments */
  525.         }
  526.       else if ((op & 0xfff0) == 0x10f0)
  527.         {
  528.           /* End of prologue if this is a trap instruction.  */
  529.           break;                /* End of stack adjustments.  */
  530.         }
  531.     }

  532.   info->size = -info->sp_offset;

  533.   /* Compute the previous frame's stack pointer (which is also the
  534.      frame's ID's stack address), and this frame's base pointer.  */
  535.   if (info->uses_frame)
  536.     {
  537.       /* The SP was moved to the FP.  This indicates that a new frame
  538.          was created.  Get THIS frame's FP value by unwinding it from
  539.          the next frame.  */
  540.       this_base = get_frame_register_unsigned (this_frame, M32R_FP_REGNUM);
  541.       /* The FP points at the last saved register.  Adjust the FP back
  542.          to before the first saved register giving the SP.  */
  543.       prev_sp = this_base + info->size;
  544.     }
  545.   else
  546.     {
  547.       /* Assume that the FP is this frame's SP but with that pushed
  548.          stack space added back.  */
  549.       this_base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
  550.       prev_sp = this_base + info->size;
  551.     }

  552.   /* Convert that SP/BASE into real addresses.  */
  553.   info->prev_sp = prev_sp;
  554.   info->base = this_base;

  555.   /* Adjust all the saved registers so that they contain addresses and
  556.      not offsets.  */
  557.   for (i = 0; i < gdbarch_num_regs (get_frame_arch (this_frame)) - 1; i++)
  558.     if (trad_frame_addr_p (info->saved_regs, i))
  559.       info->saved_regs[i].addr = (info->prev_sp + info->saved_regs[i].addr);

  560.   /* The call instruction moves the caller's PC in the callee's LR.
  561.      Since this is an unwind, do the reverse.  Copy the location of LR
  562.      into PC (the address / regnum) so that a request for PC will be
  563.      converted into a request for the LR.  */
  564.   info->saved_regs[M32R_PC_REGNUM] = info->saved_regs[LR_REGNUM];

  565.   /* The previous frame's SP needed to be computed.  Save the computed
  566.      value.  */
  567.   trad_frame_set_value (info->saved_regs, M32R_SP_REGNUM, prev_sp);

  568.   return info;
  569. }

  570. static CORE_ADDR
  571. m32r_read_pc (struct regcache *regcache)
  572. {
  573.   ULONGEST pc;
  574.   regcache_cooked_read_unsigned (regcache, M32R_PC_REGNUM, &pc);
  575.   return pc;
  576. }

  577. static CORE_ADDR
  578. m32r_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  579. {
  580.   return frame_unwind_register_unsigned (next_frame, M32R_SP_REGNUM);
  581. }


  582. static CORE_ADDR
  583. m32r_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  584.                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  585.                       struct value **args, CORE_ADDR sp, int struct_return,
  586.                       CORE_ADDR struct_addr)
  587. {
  588.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  589.   int stack_offset, stack_alloc;
  590.   int argreg = ARG1_REGNUM;
  591.   int argnum;
  592.   struct type *type;
  593.   enum type_code typecode;
  594.   CORE_ADDR regval;
  595.   gdb_byte *val;
  596.   gdb_byte valbuf[MAX_REGISTER_SIZE];
  597.   int len;

  598.   /* First force sp to a 4-byte alignment.  */
  599.   sp = sp & ~3;

  600.   /* Set the return address.  For the m32r, the return breakpoint is
  601.      always at BP_ADDR.  */
  602.   regcache_cooked_write_unsigned (regcache, LR_REGNUM, bp_addr);

  603.   /* If STRUCT_RETURN is true, then the struct return address (in
  604.      STRUCT_ADDR) will consume the first argument-passing register.
  605.      Both adjust the register count and store that value.  */
  606.   if (struct_return)
  607.     {
  608.       regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
  609.       argreg++;
  610.     }

  611.   /* Now make sure there's space on the stack.  */
  612.   for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
  613.     stack_alloc += ((TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3);
  614.   sp -= stack_alloc;                /* Make room on stack for args.  */

  615.   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
  616.     {
  617.       type = value_type (args[argnum]);
  618.       typecode = TYPE_CODE (type);
  619.       len = TYPE_LENGTH (type);

  620.       memset (valbuf, 0, sizeof (valbuf));

  621.       /* Passes structures that do not fit in 2 registers by reference.  */
  622.       if (len > 8
  623.           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
  624.         {
  625.           store_unsigned_integer (valbuf, 4, byte_order,
  626.                                   value_address (args[argnum]));
  627.           typecode = TYPE_CODE_PTR;
  628.           len = 4;
  629.           val = valbuf;
  630.         }
  631.       else if (len < 4)
  632.         {
  633.           /* Value gets right-justified in the register or stack word.  */
  634.           memcpy (valbuf + (register_size (gdbarch, argreg) - len),
  635.                   (gdb_byte *) value_contents (args[argnum]), len);
  636.           val = valbuf;
  637.         }
  638.       else
  639.         val = (gdb_byte *) value_contents (args[argnum]);

  640.       while (len > 0)
  641.         {
  642.           if (argreg > ARGN_REGNUM)
  643.             {
  644.               /* Must go on the stack.  */
  645.               write_memory (sp + stack_offset, val, 4);
  646.               stack_offset += 4;
  647.             }
  648.           else if (argreg <= ARGN_REGNUM)
  649.             {
  650.               /* There's room in a register.  */
  651.               regval =
  652.                 extract_unsigned_integer (val,
  653.                                           register_size (gdbarch, argreg),
  654.                                           byte_order);
  655.               regcache_cooked_write_unsigned (regcache, argreg++, regval);
  656.             }

  657.           /* Store the value 4 bytes at a time.  This means that things
  658.              larger than 4 bytes may go partly in registers and partly
  659.              on the stack.  */
  660.           len -= register_size (gdbarch, argreg);
  661.           val += register_size (gdbarch, argreg);
  662.         }
  663.     }

  664.   /* Finally, update the SP register.  */
  665.   regcache_cooked_write_unsigned (regcache, M32R_SP_REGNUM, sp);

  666.   return sp;
  667. }


  668. /* Given a return value in `regbuf' with a type `valtype',
  669.    extract and copy its value into `valbuf'.  */

  670. static void
  671. m32r_extract_return_value (struct type *type, struct regcache *regcache,
  672.                            void *dst)
  673. {
  674.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  675.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  676.   bfd_byte *valbuf = dst;
  677.   int len = TYPE_LENGTH (type);
  678.   ULONGEST tmp;

  679.   /* By using store_unsigned_integer we avoid having to do
  680.      anything special for small big-endian values.  */
  681.   regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &tmp);
  682.   store_unsigned_integer (valbuf, (len > 4 ? len - 4 : len), byte_order, tmp);

  683.   /* Ignore return values more than 8 bytes in size because the m32r
  684.      returns anything more than 8 bytes in the stack.  */
  685.   if (len > 4)
  686.     {
  687.       regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
  688.       store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp);
  689.     }
  690. }

  691. static enum return_value_convention
  692. m32r_return_value (struct gdbarch *gdbarch, struct value *function,
  693.                    struct type *valtype, struct regcache *regcache,
  694.                    gdb_byte *readbuf, const gdb_byte *writebuf)
  695. {
  696.   if (TYPE_LENGTH (valtype) > 8)
  697.     return RETURN_VALUE_STRUCT_CONVENTION;
  698.   else
  699.     {
  700.       if (readbuf != NULL)
  701.         m32r_extract_return_value (valtype, regcache, readbuf);
  702.       if (writebuf != NULL)
  703.         m32r_store_return_value (valtype, regcache, writebuf);
  704.       return RETURN_VALUE_REGISTER_CONVENTION;
  705.     }
  706. }



  707. static CORE_ADDR
  708. m32r_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  709. {
  710.   return frame_unwind_register_unsigned (next_frame, M32R_PC_REGNUM);
  711. }

  712. /* Given a GDB frame, determine the address of the calling function's
  713.    frame.  This will be used to create a new GDB frame struct.  */

  714. static void
  715. m32r_frame_this_id (struct frame_info *this_frame,
  716.                     void **this_prologue_cache, struct frame_id *this_id)
  717. {
  718.   struct m32r_unwind_cache *info
  719.     = m32r_frame_unwind_cache (this_frame, this_prologue_cache);
  720.   CORE_ADDR base;
  721.   CORE_ADDR func;
  722.   struct bound_minimal_symbol msym_stack;
  723.   struct frame_id id;

  724.   /* The FUNC is easy.  */
  725.   func = get_frame_func (this_frame);

  726.   /* Check if the stack is empty.  */
  727.   msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
  728.   if (msym_stack.minsym && info->base == BMSYMBOL_VALUE_ADDRESS (msym_stack))
  729.     return;

  730.   /* Hopefully the prologue analysis either correctly determined the
  731.      frame's base (which is the SP from the previous frame), or set
  732.      that base to "NULL".  */
  733.   base = info->prev_sp;
  734.   if (base == 0)
  735.     return;

  736.   id = frame_id_build (base, func);
  737.   (*this_id) = id;
  738. }

  739. static struct value *
  740. m32r_frame_prev_register (struct frame_info *this_frame,
  741.                           void **this_prologue_cache, int regnum)
  742. {
  743.   struct m32r_unwind_cache *info
  744.     = m32r_frame_unwind_cache (this_frame, this_prologue_cache);
  745.   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  746. }

  747. static const struct frame_unwind m32r_frame_unwind = {
  748.   NORMAL_FRAME,
  749.   default_frame_unwind_stop_reason,
  750.   m32r_frame_this_id,
  751.   m32r_frame_prev_register,
  752.   NULL,
  753.   default_frame_sniffer
  754. };

  755. static CORE_ADDR
  756. m32r_frame_base_address (struct frame_info *this_frame, void **this_cache)
  757. {
  758.   struct m32r_unwind_cache *info
  759.     = m32r_frame_unwind_cache (this_frame, this_cache);
  760.   return info->base;
  761. }

  762. static const struct frame_base m32r_frame_base = {
  763.   &m32r_frame_unwind,
  764.   m32r_frame_base_address,
  765.   m32r_frame_base_address,
  766.   m32r_frame_base_address
  767. };

  768. /* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
  769.    frame.  The frame ID's base needs to match the TOS value saved by
  770.    save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint.  */

  771. static struct frame_id
  772. m32r_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  773. {
  774.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
  775.   return frame_id_build (sp, get_frame_pc (this_frame));
  776. }


  777. static gdbarch_init_ftype m32r_gdbarch_init;

  778. static struct gdbarch *
  779. m32r_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  780. {
  781.   struct gdbarch *gdbarch;
  782.   struct gdbarch_tdep *tdep;

  783.   /* If there is already a candidate, use it.  */
  784.   arches = gdbarch_list_lookup_by_info (arches, &info);
  785.   if (arches != NULL)
  786.     return arches->gdbarch;

  787.   /* Allocate space for the new architecture.  */
  788.   tdep = XNEW (struct gdbarch_tdep);
  789.   gdbarch = gdbarch_alloc (&info, tdep);

  790.   set_gdbarch_read_pc (gdbarch, m32r_read_pc);
  791.   set_gdbarch_unwind_sp (gdbarch, m32r_unwind_sp);

  792.   set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS);
  793.   set_gdbarch_pc_regnum (gdbarch, M32R_PC_REGNUM);
  794.   set_gdbarch_sp_regnum (gdbarch, M32R_SP_REGNUM);
  795.   set_gdbarch_register_name (gdbarch, m32r_register_name);
  796.   set_gdbarch_register_type (gdbarch, m32r_register_type);

  797.   set_gdbarch_push_dummy_call (gdbarch, m32r_push_dummy_call);
  798.   set_gdbarch_return_value (gdbarch, m32r_return_value);

  799.   set_gdbarch_skip_prologue (gdbarch, m32r_skip_prologue);
  800.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  801.   set_gdbarch_breakpoint_from_pc (gdbarch, m32r_breakpoint_from_pc);
  802.   set_gdbarch_memory_insert_breakpoint (gdbarch,
  803.                                         m32r_memory_insert_breakpoint);
  804.   set_gdbarch_memory_remove_breakpoint (gdbarch,
  805.                                         m32r_memory_remove_breakpoint);

  806.   set_gdbarch_frame_align (gdbarch, m32r_frame_align);

  807.   frame_base_set_default (gdbarch, &m32r_frame_base);

  808.   /* Methods for saving / extracting a dummy frame's ID.  The ID's
  809.      stack address must match the SP value returned by
  810.      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
  811.   set_gdbarch_dummy_id (gdbarch, m32r_dummy_id);

  812.   /* Return the unwound PC value.  */
  813.   set_gdbarch_unwind_pc (gdbarch, m32r_unwind_pc);

  814.   set_gdbarch_print_insn (gdbarch, print_insn_m32r);

  815.   /* Hook in ABI-specific overrides, if they have been registered.  */
  816.   gdbarch_init_osabi (info, gdbarch);

  817.   /* Hook in the default unwinders.  */
  818.   frame_unwind_append_unwinder (gdbarch, &m32r_frame_unwind);

  819.   /* Support simple overlay manager.  */
  820.   set_gdbarch_overlay_update (gdbarch, simple_overlay_update);

  821.   return gdbarch;
  822. }

  823. void
  824. _initialize_m32r_tdep (void)
  825. {
  826.   register_gdbarch_init (bfd_arch_m32r, m32r_gdbarch_init);
  827. }