gdb/m32c-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Renesas M32C target-dependent code for GDB, the GNU debugger.

  2.    Copyright (C) 2004-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 "elf-bfd.h"
  16. #include "elf/m32c.h"
  17. #include "gdb/sim-m32c.h"
  18. #include "dis-asm.h"
  19. #include "gdbtypes.h"
  20. #include "regcache.h"
  21. #include "arch-utils.h"
  22. #include "frame.h"
  23. #include "frame-unwind.h"
  24. #include "dwarf2-frame.h"
  25. #include "dwarf2expr.h"
  26. #include "symtab.h"
  27. #include "gdbcore.h"
  28. #include "value.h"
  29. #include "reggroups.h"
  30. #include "prologue-value.h"
  31. #include "target.h"
  32. #include "objfiles.h"


  33. /* The m32c tdep structure.  */

  34. static struct reggroup *m32c_dma_reggroup;

  35. struct m32c_reg;

  36. /* The type of a function that moves the value of REG between CACHE or
  37.    BUF --- in either direction.  */
  38. typedef enum register_status (m32c_move_reg_t) (struct m32c_reg *reg,
  39.                                                 struct regcache *cache,
  40.                                                 void *buf);

  41. struct m32c_reg
  42. {
  43.   /* The name of this register.  */
  44.   const char *name;

  45.   /* Its type.  */
  46.   struct type *type;

  47.   /* The architecture this register belongs to.  */
  48.   struct gdbarch *arch;

  49.   /* Its GDB register number.  */
  50.   int num;

  51.   /* Its sim register number.  */
  52.   int sim_num;

  53.   /* Its DWARF register number, or -1 if it doesn't have one.  */
  54.   int dwarf_num;

  55.   /* Register group memberships.  */
  56.   unsigned int general_p : 1;
  57.   unsigned int dma_p : 1;
  58.   unsigned int system_p : 1;
  59.   unsigned int save_restore_p : 1;

  60.   /* Functions to read its value from a regcache, and write its value
  61.      to a regcache.  */
  62.   m32c_move_reg_t *read, *write;

  63.   /* Data for READ and WRITE functions.  The exact meaning depends on
  64.      the specific functions selected; see the comments for those
  65.      functions.  */
  66.   struct m32c_reg *rx, *ry;
  67.   int n;
  68. };


  69. /* An overestimate of the number of raw and pseudoregisters we will
  70.    have.  The exact answer depends on the variant of the architecture
  71.    at hand, but we can use this to declare statically allocated
  72.    arrays, and bump it up when needed.  */
  73. #define M32C_MAX_NUM_REGS (75)

  74. /* The largest assigned DWARF register number.  */
  75. #define M32C_MAX_DWARF_REGNUM (40)


  76. struct gdbarch_tdep
  77. {
  78.   /* All the registers for this variant, indexed by GDB register
  79.      number, and the number of registers present.  */
  80.   struct m32c_reg regs[M32C_MAX_NUM_REGS];

  81.   /* The number of valid registers.  */
  82.   int num_regs;

  83.   /* Interesting registers.  These are pointers into REGS.  */
  84.   struct m32c_reg *pc, *flg;
  85.   struct m32c_reg *r0, *r1, *r2, *r3, *a0, *a1;
  86.   struct m32c_reg *r2r0, *r3r2r1r0, *r3r1r2r0;
  87.   struct m32c_reg *sb, *fb, *sp;

  88.   /* A table indexed by DWARF register numbers, pointing into
  89.      REGS.  */
  90.   struct m32c_reg *dwarf_regs[M32C_MAX_DWARF_REGNUM + 1];

  91.   /* Types for this architecture.  We can't use the builtin_type_foo
  92.      types, because they're not initialized when building a gdbarch
  93.      structure.  */
  94.   struct type *voyd, *ptr_voyd, *func_voyd;
  95.   struct type *uint8, *uint16;
  96.   struct type *int8, *int16, *int32, *int64;

  97.   /* The types for data address and code address registers.  */
  98.   struct type *data_addr_reg_type, *code_addr_reg_type;

  99.   /* The number of bytes a return address pushed by a 'jsr' instruction
  100.      occupies on the stack.  */
  101.   int ret_addr_bytes;

  102.   /* The number of bytes an address register occupies on the stack
  103.      when saved by an 'enter' or 'pushm' instruction.  */
  104.   int push_addr_bytes;
  105. };


  106. /* Types.  */

  107. static void
  108. make_types (struct gdbarch *arch)
  109. {
  110.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  111.   unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
  112.   int data_addr_reg_bits, code_addr_reg_bits;
  113.   char type_name[50];

  114. #if 0
  115.   /* This is used to clip CORE_ADDR values, so this value is
  116.      appropriate both on the m32c, where pointers are 32 bits long,
  117.      and on the m16c, where pointers are sixteen bits long, but there
  118.      may be code above the 64k boundary.  */
  119.   set_gdbarch_addr_bit (arch, 24);
  120. #else
  121.   /* GCC uses 32 bits for addrs in the dwarf info, even though
  122.      only 16/24 bits are used.  Setting addr_bit to 24 causes
  123.      errors in reading the dwarf addresses.  */
  124.   set_gdbarch_addr_bit (arch, 32);
  125. #endif

  126.   set_gdbarch_int_bit (arch, 16);
  127.   switch (mach)
  128.     {
  129.     case bfd_mach_m16c:
  130.       data_addr_reg_bits = 16;
  131.       code_addr_reg_bits = 24;
  132.       set_gdbarch_ptr_bit (arch, 16);
  133.       tdep->ret_addr_bytes = 3;
  134.       tdep->push_addr_bytes = 2;
  135.       break;

  136.     case bfd_mach_m32c:
  137.       data_addr_reg_bits = 24;
  138.       code_addr_reg_bits = 24;
  139.       set_gdbarch_ptr_bit (arch, 32);
  140.       tdep->ret_addr_bytes = 4;
  141.       tdep->push_addr_bytes = 4;
  142.       break;

  143.     default:
  144.       gdb_assert_not_reached ("unexpected mach");
  145.     }

  146.   /* The builtin_type_mumble variables are sometimes uninitialized when
  147.      this is called, so we avoid using them.  */
  148.   tdep->voyd = arch_type (arch, TYPE_CODE_VOID, 1, "void");
  149.   tdep->ptr_voyd
  150.     = arch_type (arch, TYPE_CODE_PTR, gdbarch_ptr_bit (arch) / TARGET_CHAR_BIT,
  151.                  NULL);
  152.   TYPE_TARGET_TYPE (tdep->ptr_voyd) = tdep->voyd;
  153.   TYPE_UNSIGNED (tdep->ptr_voyd) = 1;
  154.   tdep->func_voyd = lookup_function_type (tdep->voyd);

  155.   xsnprintf (type_name, sizeof (type_name), "%s_data_addr_t",
  156.              gdbarch_bfd_arch_info (arch)->printable_name);
  157.   tdep->data_addr_reg_type
  158.     = arch_type (arch, TYPE_CODE_PTR, data_addr_reg_bits / TARGET_CHAR_BIT,
  159.                  xstrdup (type_name));
  160.   TYPE_TARGET_TYPE (tdep->data_addr_reg_type) = tdep->voyd;
  161.   TYPE_UNSIGNED (tdep->data_addr_reg_type) = 1;

  162.   xsnprintf (type_name, sizeof (type_name), "%s_code_addr_t",
  163.              gdbarch_bfd_arch_info (arch)->printable_name);
  164.   tdep->code_addr_reg_type
  165.     = arch_type (arch, TYPE_CODE_PTR, code_addr_reg_bits / TARGET_CHAR_BIT,
  166.                  xstrdup (type_name));
  167.   TYPE_TARGET_TYPE (tdep->code_addr_reg_type) = tdep->func_voyd;
  168.   TYPE_UNSIGNED (tdep->code_addr_reg_type) = 1;

  169.   tdep->uint8  = arch_integer_type (arch,  8, 1, "uint8_t");
  170.   tdep->uint16 = arch_integer_type (arch, 16, 1, "uint16_t");
  171.   tdep->int8   = arch_integer_type (arch,  8, 0, "int8_t");
  172.   tdep->int16  = arch_integer_type (arch, 16, 0, "int16_t");
  173.   tdep->int32  = arch_integer_type (arch, 32, 0, "int32_t");
  174.   tdep->int64  = arch_integer_type (arch, 64, 0, "int64_t");
  175. }



  176. /* Register set.  */

  177. static const char *
  178. m32c_register_name (struct gdbarch *gdbarch, int num)
  179. {
  180.   return gdbarch_tdep (gdbarch)->regs[num].name;
  181. }


  182. static struct type *
  183. m32c_register_type (struct gdbarch *arch, int reg_nr)
  184. {
  185.   return gdbarch_tdep (arch)->regs[reg_nr].type;
  186. }


  187. static int
  188. m32c_register_sim_regno (struct gdbarch *gdbarch, int reg_nr)
  189. {
  190.   return gdbarch_tdep (gdbarch)->regs[reg_nr].sim_num;
  191. }


  192. static int
  193. m32c_debug_info_reg_to_regnum (struct gdbarch *gdbarch, int reg_nr)
  194. {
  195.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  196.   if (0 <= reg_nr && reg_nr <= M32C_MAX_DWARF_REGNUM
  197.       && tdep->dwarf_regs[reg_nr])
  198.     return tdep->dwarf_regs[reg_nr]->num;
  199.   else
  200.     /* The DWARF CFI code expects to see -1 for invalid register
  201.        numbers.  */
  202.     return -1;
  203. }


  204. static int
  205. m32c_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  206.                           struct reggroup *group)
  207. {
  208.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  209.   struct m32c_reg *reg = &tdep->regs[regnum];

  210.   /* The anonymous raw registers aren't in any groups.  */
  211.   if (! reg->name)
  212.     return 0;

  213.   if (group == all_reggroup)
  214.     return 1;

  215.   if (group == general_reggroup
  216.       && reg->general_p)
  217.     return 1;

  218.   if (group == m32c_dma_reggroup
  219.       && reg->dma_p)
  220.     return 1;

  221.   if (group == system_reggroup
  222.       && reg->system_p)
  223.     return 1;

  224.   /* Since the m32c DWARF register numbers refer to cooked registers, not
  225.      raw registers, and frame_pop depends on the save and restore groups
  226.      containing registers the DWARF CFI will actually mention, our save
  227.      and restore groups are cooked registers, not raw registers.  (This is
  228.      why we can't use the default reggroup function.)  */
  229.   if ((group == save_reggroup
  230.        || group == restore_reggroup)
  231.       && reg->save_restore_p)
  232.     return 1;

  233.   return 0;
  234. }


  235. /* Register move functions.  We declare them here using
  236.    m32c_move_reg_t to check the types.  */
  237. static m32c_move_reg_t m32c_raw_read,      m32c_raw_write;
  238. static m32c_move_reg_t m32c_banked_read,   m32c_banked_write;
  239. static m32c_move_reg_t m32c_sb_read,            m32c_sb_write;
  240. static m32c_move_reg_t m32c_part_read,     m32c_part_write;
  241. static m32c_move_reg_t m32c_cat_read,      m32c_cat_write;
  242. static m32c_move_reg_t m32c_r3r2r1r0_read, m32c_r3r2r1r0_write;


  243. /* Copy the value of the raw register REG from CACHE to BUF.  */
  244. static enum register_status
  245. m32c_raw_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
  246. {
  247.   return regcache_raw_read (cache, reg->num, buf);
  248. }


  249. /* Copy the value of the raw register REG from BUF to CACHE.  */
  250. static enum register_status
  251. m32c_raw_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
  252. {
  253.   regcache_raw_write (cache, reg->num, (const void *) buf);

  254.   return REG_VALID;
  255. }


  256. /* Return the value of the 'flg' register in CACHE.  */
  257. static int
  258. m32c_read_flg (struct regcache *cache)
  259. {
  260.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (cache));
  261.   ULONGEST flg;
  262.   regcache_raw_read_unsigned (cache, tdep->flg->num, &flg);
  263.   return flg & 0xffff;
  264. }


  265. /* Evaluate the real register number of a banked register.  */
  266. static struct m32c_reg *
  267. m32c_banked_register (struct m32c_reg *reg, struct regcache *cache)
  268. {
  269.   return ((m32c_read_flg (cache) & reg->n) ? reg->ry : reg->rx);
  270. }


  271. /* Move the value of a banked register from CACHE to BUF.
  272.    If the value of the 'flg' register in CACHE has any of the bits
  273.    masked in REG->n set, then read REG->ry.  Otherwise, read
  274.    REG->rx.  */
  275. static enum register_status
  276. m32c_banked_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
  277. {
  278.   struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
  279.   return regcache_raw_read (cache, bank_reg->num, buf);
  280. }


  281. /* Move the value of a banked register from BUF to CACHE.
  282.    If the value of the 'flg' register in CACHE has any of the bits
  283.    masked in REG->n set, then write REG->ry.  Otherwise, write
  284.    REG->rx.  */
  285. static enum register_status
  286. m32c_banked_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
  287. {
  288.   struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
  289.   regcache_raw_write (cache, bank_reg->num, (const void *) buf);

  290.   return REG_VALID;
  291. }


  292. /* Move the value of SB from CACHE to BUF.  On bfd_mach_m32c, SB is a
  293.    banked register; on bfd_mach_m16c, it's not.  */
  294. static enum register_status
  295. m32c_sb_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
  296. {
  297.   if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
  298.     return m32c_raw_read (reg->rx, cache, buf);
  299.   else
  300.     return m32c_banked_read (reg, cache, buf);
  301. }


  302. /* Move the value of SB from BUF to CACHE.  On bfd_mach_m32c, SB is a
  303.    banked register; on bfd_mach_m16c, it's not.  */
  304. static enum register_status
  305. m32c_sb_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
  306. {
  307.   if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
  308.     m32c_raw_write (reg->rx, cache, buf);
  309.   else
  310.     m32c_banked_write (reg, cache, buf);

  311.   return REG_VALID;
  312. }


  313. /* Assuming REG uses m32c_part_read and m32c_part_write, set *OFFSET_P
  314.    and *LEN_P to the offset and length, in bytes, of the part REG
  315.    occupies in its underlying register.  The offset is from the
  316.    lower-addressed end, regardless of the architecture's endianness.
  317.    (The M32C family is always little-endian, but let's keep those
  318.    assumptions out of here.)  */
  319. static void
  320. m32c_find_part (struct m32c_reg *reg, int *offset_p, int *len_p)
  321. {
  322.   /* The length of the containing register, of which REG is one part.  */
  323.   int containing_len = TYPE_LENGTH (reg->rx->type);

  324.   /* The length of one "element" in our imaginary array.  */
  325.   int elt_len = TYPE_LENGTH (reg->type);

  326.   /* The offset of REG's "element" from the least significant end of
  327.      the containing register.  */
  328.   int elt_offset = reg->n * elt_len;

  329.   /* If we extend off the end, trim the length of the element.  */
  330.   if (elt_offset + elt_len > containing_len)
  331.     {
  332.       elt_len = containing_len - elt_offset;
  333.       /* We shouldn't be declaring partial registers that go off the
  334.          end of their containing registers.  */
  335.       gdb_assert (elt_len > 0);
  336.     }

  337.   /* Flip the offset around if we're big-endian.  */
  338.   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  339.     elt_offset = TYPE_LENGTH (reg->rx->type) - elt_offset - elt_len;

  340.   *offset_p = elt_offset;
  341.   *len_p = elt_len;
  342. }


  343. /* Move the value of a partial register (r0h, intbl, etc.) from CACHE
  344.    to BUF.  Treating the value of the register REG->rx as an array of
  345.    REG->type values, where higher indices refer to more significant
  346.    bits, read the value of the REG->n'th element.  */
  347. static enum register_status
  348. m32c_part_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
  349. {
  350.   int offset, len;

  351.   memset (buf, 0, TYPE_LENGTH (reg->type));
  352.   m32c_find_part (reg, &offset, &len);
  353.   return regcache_cooked_read_part (cache, reg->rx->num, offset, len, buf);
  354. }


  355. /* Move the value of a banked register from BUF to CACHE.
  356.    Treating the value of the register REG->rx as an array of REG->type
  357.    values, where higher indices refer to more significant bits, write
  358.    the value of the REG->n'th element.  */
  359. static enum register_status
  360. m32c_part_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
  361. {
  362.   int offset, len;

  363.   m32c_find_part (reg, &offset, &len);
  364.   regcache_cooked_write_part (cache, reg->rx->num, offset, len, buf);

  365.   return REG_VALID;
  366. }


  367. /* Move the value of REG from CACHE to BUF.  REG's value is the
  368.    concatenation of the values of the registers REG->rx and REG->ry,
  369.    with REG->rx contributing the more significant bits.  */
  370. static enum register_status
  371. m32c_cat_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
  372. {
  373.   int high_bytes = TYPE_LENGTH (reg->rx->type);
  374.   int low_bytes  = TYPE_LENGTH (reg->ry->type);
  375.   /* For address arithmetic.  */
  376.   unsigned char *cbuf = buf;
  377.   enum register_status status;

  378.   gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);

  379.   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  380.     {
  381.       status = regcache_cooked_read (cache, reg->rx->num, cbuf);
  382.       if (status == REG_VALID)
  383.         status = regcache_cooked_read (cache, reg->ry->num, cbuf + high_bytes);
  384.     }
  385.   else
  386.     {
  387.       status = regcache_cooked_read (cache, reg->rx->num, cbuf + low_bytes);
  388.       if (status == REG_VALID)
  389.         status = regcache_cooked_read (cache, reg->ry->num, cbuf);
  390.     }

  391.   return status;
  392. }


  393. /* Move the value of REG from CACHE to BUF.  REG's value is the
  394.    concatenation of the values of the registers REG->rx and REG->ry,
  395.    with REG->rx contributing the more significant bits.  */
  396. static enum register_status
  397. m32c_cat_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
  398. {
  399.   int high_bytes = TYPE_LENGTH (reg->rx->type);
  400.   int low_bytes  = TYPE_LENGTH (reg->ry->type);
  401.   /* For address arithmetic.  */
  402.   unsigned char *cbuf = buf;

  403.   gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);

  404.   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  405.     {
  406.       regcache_cooked_write (cache, reg->rx->num, cbuf);
  407.       regcache_cooked_write (cache, reg->ry->num, cbuf + high_bytes);
  408.     }
  409.   else
  410.     {
  411.       regcache_cooked_write (cache, reg->rx->num, cbuf + low_bytes);
  412.       regcache_cooked_write (cache, reg->ry->num, cbuf);
  413.     }

  414.   return REG_VALID;
  415. }


  416. /* Copy the value of the raw register REG from CACHE to BUF.  REG is
  417.    the concatenation (from most significant to least) of r3, r2, r1,
  418.    and r0.  */
  419. static enum register_status
  420. m32c_r3r2r1r0_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
  421. {
  422.   struct gdbarch_tdep *tdep = gdbarch_tdep (reg->arch);
  423.   int len = TYPE_LENGTH (tdep->r0->type);
  424.   enum register_status status;

  425.   /* For address arithmetic.  */
  426.   unsigned char *cbuf = buf;

  427.   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  428.     {
  429.       status = regcache_cooked_read (cache, tdep->r0->num, cbuf + len * 3);
  430.       if (status == REG_VALID)
  431.         status = regcache_cooked_read (cache, tdep->r1->num, cbuf + len * 2);
  432.       if (status == REG_VALID)
  433.         status = regcache_cooked_read (cache, tdep->r2->num, cbuf + len * 1);
  434.       if (status == REG_VALID)
  435.         status = regcache_cooked_read (cache, tdep->r3->num, cbuf);
  436.     }
  437.   else
  438.     {
  439.       status = regcache_cooked_read (cache, tdep->r0->num, cbuf);
  440.       if (status == REG_VALID)
  441.         status = regcache_cooked_read (cache, tdep->r1->num, cbuf + len * 1);
  442.       if (status == REG_VALID)
  443.         status = regcache_cooked_read (cache, tdep->r2->num, cbuf + len * 2);
  444.       if (status == REG_VALID)
  445.         status = regcache_cooked_read (cache, tdep->r3->num, cbuf + len * 3);
  446.     }

  447.   return status;
  448. }


  449. /* Copy the value of the raw register REG from BUF to CACHE.  REG is
  450.    the concatenation (from most significant to least) of r3, r2, r1,
  451.    and r0.  */
  452. static enum register_status
  453. m32c_r3r2r1r0_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
  454. {
  455.   struct gdbarch_tdep *tdep = gdbarch_tdep (reg->arch);
  456.   int len = TYPE_LENGTH (tdep->r0->type);

  457.   /* For address arithmetic.  */
  458.   unsigned char *cbuf = buf;

  459.   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  460.     {
  461.       regcache_cooked_write (cache, tdep->r0->num, cbuf + len * 3);
  462.       regcache_cooked_write (cache, tdep->r1->num, cbuf + len * 2);
  463.       regcache_cooked_write (cache, tdep->r2->num, cbuf + len * 1);
  464.       regcache_cooked_write (cache, tdep->r3->num, cbuf);
  465.     }
  466.   else
  467.     {
  468.       regcache_cooked_write (cache, tdep->r0->num, cbuf);
  469.       regcache_cooked_write (cache, tdep->r1->num, cbuf + len * 1);
  470.       regcache_cooked_write (cache, tdep->r2->num, cbuf + len * 2);
  471.       regcache_cooked_write (cache, tdep->r3->num, cbuf + len * 3);
  472.     }

  473.   return REG_VALID;
  474. }


  475. static enum register_status
  476. m32c_pseudo_register_read (struct gdbarch *arch,
  477.                            struct regcache *cache,
  478.                            int cookednum,
  479.                            gdb_byte *buf)
  480. {
  481.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  482.   struct m32c_reg *reg;

  483.   gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
  484.   gdb_assert (arch == get_regcache_arch (cache));
  485.   gdb_assert (arch == tdep->regs[cookednum].arch);
  486.   reg = &tdep->regs[cookednum];

  487.   return reg->read (reg, cache, buf);
  488. }


  489. static void
  490. m32c_pseudo_register_write (struct gdbarch *arch,
  491.                             struct regcache *cache,
  492.                             int cookednum,
  493.                             const gdb_byte *buf)
  494. {
  495.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  496.   struct m32c_reg *reg;

  497.   gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
  498.   gdb_assert (arch == get_regcache_arch (cache));
  499.   gdb_assert (arch == tdep->regs[cookednum].arch);
  500.   reg = &tdep->regs[cookednum];

  501.   reg->write (reg, cache, (void *) buf);
  502. }


  503. /* Add a register with the given fields to the end of ARCH's table.
  504.    Return a pointer to the newly added register.  */
  505. static struct m32c_reg *
  506. add_reg (struct gdbarch *arch,
  507.          const char *name,
  508.          struct type *type,
  509.          int sim_num,
  510.          m32c_move_reg_t *read,
  511.          m32c_move_reg_t *write,
  512.          struct m32c_reg *rx,
  513.          struct m32c_reg *ry,
  514.          int n)
  515. {
  516.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  517.   struct m32c_reg *r = &tdep->regs[tdep->num_regs];

  518.   gdb_assert (tdep->num_regs < M32C_MAX_NUM_REGS);

  519.   r->name           = name;
  520.   r->type           = type;
  521.   r->arch           = arch;
  522.   r->num            = tdep->num_regs;
  523.   r->sim_num        = sim_num;
  524.   r->dwarf_num      = -1;
  525.   r->general_p      = 0;
  526.   r->dma_p          = 0;
  527.   r->system_p       = 0;
  528.   r->save_restore_p = 0;
  529.   r->read           = read;
  530.   r->write          = write;
  531.   r->rx             = rx;
  532.   r->ry             = ry;
  533.   r->n              = n;

  534.   tdep->num_regs++;

  535.   return r;
  536. }


  537. /* Record NUM as REG's DWARF register number.  */
  538. static void
  539. set_dwarf_regnum (struct m32c_reg *reg, int num)
  540. {
  541.   gdb_assert (num < M32C_MAX_NUM_REGS);

  542.   /* Update the reg->DWARF mapping.  Only count the first number
  543.      assigned to this register.  */
  544.   if (reg->dwarf_num == -1)
  545.     reg->dwarf_num = num;

  546.   /* Update the DWARF->reg mapping.  */
  547.   gdbarch_tdep (reg->arch)->dwarf_regs[num] = reg;
  548. }


  549. /* Mark REG as a general-purpose register, and return it.  */
  550. static struct m32c_reg *
  551. mark_general (struct m32c_reg *reg)
  552. {
  553.   reg->general_p = 1;
  554.   return reg;
  555. }


  556. /* Mark REG as a DMA register, and return it.  */
  557. static struct m32c_reg *
  558. mark_dma (struct m32c_reg *reg)
  559. {
  560.   reg->dma_p = 1;
  561.   return reg;
  562. }


  563. /* Mark REG as a SYSTEM register, and return it.  */
  564. static struct m32c_reg *
  565. mark_system (struct m32c_reg *reg)
  566. {
  567.   reg->system_p = 1;
  568.   return reg;
  569. }


  570. /* Mark REG as a save-restore register, and return it.  */
  571. static struct m32c_reg *
  572. mark_save_restore (struct m32c_reg *reg)
  573. {
  574.   reg->save_restore_p = 1;
  575.   return reg;
  576. }


  577. #define FLAGBIT_B        0x0010
  578. #define FLAGBIT_U        0x0080

  579. /* Handy macros for declaring registers.  These all evaluate to
  580.    pointers to the register declared.  Macros that define two
  581.    registers evaluate to a pointer to the first.  */

  582. /* A raw register named NAME, with type TYPE and sim number SIM_NUM.  */
  583. #define R(name, type, sim_num)                                        \
  584.   (add_reg (arch, (name), (type), (sim_num),                        \
  585.             m32c_raw_read, m32c_raw_write, NULL, NULL, 0))

  586. /* The simulator register number for a raw register named NAME.  */
  587. #define SIM(name) (m32c_sim_reg_ ## name)

  588. /* A raw unsigned 16-bit data register named NAME.
  589.    NAME should be an identifier, not a string.  */
  590. #define R16U(name)                                                \
  591.   (R(#name, tdep->uint16, SIM (name)))

  592. /* A raw data address register named NAME.
  593.    NAME should be an identifier, not a string.  */
  594. #define RA(name)                                                \
  595.   (R(#name, tdep->data_addr_reg_type, SIM (name)))

  596. /* A raw code address register named NAMENAME should
  597.    be an identifier, not a string.  */
  598. #define RC(name)                                                \
  599.   (R(#name, tdep->code_addr_reg_type, SIM (name)))

  600. /* A pair of raw registers named NAME0 and NAME1, with type TYPE.
  601.    NAME should be an identifier, not a string.  */
  602. #define RP(name, type)                                \
  603.   (R(#name "0", (type), SIM (name ## 0)),        \
  604.    R(#name "1", (type), SIM (name ## 1)) - 1)

  605. /* A raw banked general-purpose data register named NAME.
  606.    NAME should be an identifier, not a string.  */
  607. #define RBD(name)                                                \
  608.   (R(NULL, tdep->int16, SIM (name ## _bank0)),                \
  609.    R(NULL, tdep->int16, SIM (name ## _bank1)) - 1)

  610. /* A raw banked data address register named NAME.
  611.    NAME should be an identifier, not a string.  */
  612. #define RBA(name)                                                \
  613.   (R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank0)),        \
  614.    R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank1)) - 1)

  615. /* A cooked register named NAME referring to a raw banked register
  616.    from the bank selected by the current value of FLG.  RAW_PAIR
  617.    should be a pointer to the first register in the banked pair.
  618.    NAME must be an identifier, not a string.  */
  619. #define CB(name, raw_pair)                                \
  620.   (add_reg (arch, #name, (raw_pair)->type, 0,                \
  621.             m32c_banked_read, m32c_banked_write,        \
  622.             (raw_pair), (raw_pair + 1), FLAGBIT_B))

  623. /* A pair of registers named NAMEH and NAMEL, of type TYPE, that
  624.    access the top and bottom halves of the register pointed to by
  625.    NAMENAME should be an identifier.  */
  626. #define CHL(name, type)                                                        \
  627.   (add_reg (arch, #name "h", (type), 0,                                        \
  628.             m32c_part_read, m32c_part_write, name, NULL, 1),                \
  629.    add_reg (arch, #name "l", (type), 0,                                        \
  630.             m32c_part_read, m32c_part_write, name, NULL, 0) - 1)

  631. /* A register constructed by concatenating the two registers HIGH and
  632.    LOW, whose name is HIGHLOW and whose type is TYPE.  */
  633. #define CCAT(high, low, type)                                        \
  634.   (add_reg (arch, #high #low, (type), 0,                        \
  635.             m32c_cat_read, m32c_cat_write, (high), (low), 0))

  636. /* Abbreviations for marking register group membership.  */
  637. #define G(reg)   (mark_general (reg))
  638. #define S(reg)   (mark_system  (reg))
  639. #define DMA(reg) (mark_dma     (reg))


  640. /* Construct the register set for ARCH.  */
  641. static void
  642. make_regs (struct gdbarch *arch)
  643. {
  644.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  645.   int mach = gdbarch_bfd_arch_info (arch)->mach;
  646.   int num_raw_regs;
  647.   int num_cooked_regs;

  648.   struct m32c_reg *r0;
  649.   struct m32c_reg *r1;
  650.   struct m32c_reg *r2;
  651.   struct m32c_reg *r3;
  652.   struct m32c_reg *a0;
  653.   struct m32c_reg *a1;
  654.   struct m32c_reg *fb;
  655.   struct m32c_reg *sb;
  656.   struct m32c_reg *sp;
  657.   struct m32c_reg *r0hl;
  658.   struct m32c_reg *r1hl;
  659.   struct m32c_reg *r2hl;
  660.   struct m32c_reg *r3hl;
  661.   struct m32c_reg *intbhl;
  662.   struct m32c_reg *r2r0;
  663.   struct m32c_reg *r3r1;
  664.   struct m32c_reg *r3r1r2r0;
  665.   struct m32c_reg *r3r2r1r0;
  666.   struct m32c_reg *a1a0;

  667.   struct m32c_reg *raw_r0_pair = RBD (r0);
  668.   struct m32c_reg *raw_r1_pair = RBD (r1);
  669.   struct m32c_reg *raw_r2_pair = RBD (r2);
  670.   struct m32c_reg *raw_r3_pair = RBD (r3);
  671.   struct m32c_reg *raw_a0_pair = RBA (a0);
  672.   struct m32c_reg *raw_a1_pair = RBA (a1);
  673.   struct m32c_reg *raw_fb_pair = RBA (fb);

  674.   /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
  675.      We always declare both raw registers, and deal with the distinction
  676.      in the pseudoregister.  */
  677.   struct m32c_reg *raw_sb_pair = RBA (sb);

  678.   struct m32c_reg *usp         = S (RA (usp));
  679.   struct m32c_reg *isp         = S (RA (isp));
  680.   struct m32c_reg *intb        = S (RC (intb));
  681.   struct m32c_reg *pc          = G (RC (pc));
  682.   struct m32c_reg *flg         = G (R16U (flg));

  683.   if (mach == bfd_mach_m32c)
  684.     {
  685.       struct m32c_reg *svf     = S (R16U (svf));
  686.       struct m32c_reg *svp     = S (RC (svp));
  687.       struct m32c_reg *vct     = S (RC (vct));

  688.       struct m32c_reg *dmd01   = DMA (RP (dmd, tdep->uint8));
  689.       struct m32c_reg *dct01   = DMA (RP (dct, tdep->uint16));
  690.       struct m32c_reg *drc01   = DMA (RP (drc, tdep->uint16));
  691.       struct m32c_reg *dma01   = DMA (RP (dma, tdep->data_addr_reg_type));
  692.       struct m32c_reg *dsa01   = DMA (RP (dsa, tdep->data_addr_reg_type));
  693.       struct m32c_reg *dra01   = DMA (RP (dra, tdep->data_addr_reg_type));
  694.     }

  695.   num_raw_regs = tdep->num_regs;

  696.   r0               = G (CB (r0, raw_r0_pair));
  697.   r1               = G (CB (r1, raw_r1_pair));
  698.   r2          = G (CB (r2, raw_r2_pair));
  699.   r3          = G (CB (r3, raw_r3_pair));
  700.   a0          = G (CB (a0, raw_a0_pair));
  701.   a1          = G (CB (a1, raw_a1_pair));
  702.   fb          = G (CB (fb, raw_fb_pair));

  703.   /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
  704.      Specify custom read/write functions that do the right thing.  */
  705.   sb          = G (add_reg (arch, "sb", raw_sb_pair->type, 0,
  706.                             m32c_sb_read, m32c_sb_write,
  707.                             raw_sb_pair, raw_sb_pair + 1, 0));

  708.   /* The current sp is either usp or isp, depending on the value of
  709.      the FLG register's U bit.  */
  710.   sp          = G (add_reg (arch, "sp", usp->type, 0,
  711.                             m32c_banked_read, m32c_banked_write,
  712.                             isp, usp, FLAGBIT_U));

  713.   r0hl        = CHL (r0, tdep->int8);
  714.   r1hl        = CHL (r1, tdep->int8);
  715.   r2hl        = CHL (r2, tdep->int8);
  716.   r3hl        = CHL (r3, tdep->int8);
  717.   intbhl      = CHL (intb, tdep->int16);

  718.   r2r0        = CCAT (r2,   r0,   tdep->int32);
  719.   r3r1        = CCAT (r3,   r1,   tdep->int32);
  720.   r3r1r2r0    = CCAT (r3r1, r2r0, tdep->int64);

  721.   r3r2r1r0
  722.     = add_reg (arch, "r3r2r1r0", tdep->int64, 0,
  723.                m32c_r3r2r1r0_read, m32c_r3r2r1r0_write, NULL, NULL, 0);

  724.   if (mach == bfd_mach_m16c)
  725.     a1a0 = CCAT (a1, a0, tdep->int32);
  726.   else
  727.     a1a0 = NULL;

  728.   num_cooked_regs = tdep->num_regs - num_raw_regs;

  729.   tdep->pc            = pc;
  730.   tdep->flg           = flg;
  731.   tdep->r0            = r0;
  732.   tdep->r1            = r1;
  733.   tdep->r2            = r2;
  734.   tdep->r3            = r3;
  735.   tdep->r2r0          = r2r0;
  736.   tdep->r3r2r1r0 = r3r2r1r0;
  737.   tdep->r3r1r2r0 = r3r1r2r0;
  738.   tdep->a0       = a0;
  739.   tdep->a1       = a1;
  740.   tdep->sb       = sb;
  741.   tdep->fb            = fb;
  742.   tdep->sp            = sp;

  743.   /* Set up the DWARF register table.  */
  744.   memset (tdep->dwarf_regs, 0, sizeof (tdep->dwarf_regs));
  745.   set_dwarf_regnum (r0hl + 1, 0x01);
  746.   set_dwarf_regnum (r0hl + 0, 0x02);
  747.   set_dwarf_regnum (r1hl + 1, 0x03);
  748.   set_dwarf_regnum (r1hl + 0, 0x04);
  749.   set_dwarf_regnum (r0,       0x05);
  750.   set_dwarf_regnum (r1,       0x06);
  751.   set_dwarf_regnum (r2,       0x07);
  752.   set_dwarf_regnum (r3,       0x08);
  753.   set_dwarf_regnum (a0,       0x09);
  754.   set_dwarf_regnum (a1,       0x0a);
  755.   set_dwarf_regnum (fb,       0x0b);
  756.   set_dwarf_regnum (sp,       0x0c);
  757.   set_dwarf_regnum (pc,       0x0d); /* GCC's invention */
  758.   set_dwarf_regnum (sb,       0x13);
  759.   set_dwarf_regnum (r2r0,     0x15);
  760.   set_dwarf_regnum (r3r1,     0x16);
  761.   if (a1a0)
  762.     set_dwarf_regnum (a1a0,   0x17);

  763.   /* Enumerate the save/restore register group.

  764.      The regcache_save and regcache_restore functions apply their read
  765.      function to each register in this group.

  766.      Since frame_pop supplies frame_unwind_register as its read
  767.      function, the registers meaningful to the Dwarf unwinder need to
  768.      be in this group.

  769.      On the other hand, when we make inferior calls, save_inferior_status
  770.      and restore_inferior_status use them to preserve the current register
  771.      values across the inferior call.  For this, you'd kind of like to
  772.      preserve all the raw registers, to protect the interrupted code from
  773.      any sort of bank switching the callee might have done.  But we handle
  774.      those cases so badly anyway --- for example, it matters whether we
  775.      restore FLG before or after we restore the general-purpose registers,
  776.      but there's no way to express that --- that it isn't worth worrying
  777.      about.

  778.      We omit control registers like inthl: if you call a function that
  779.      changes those, it's probably because you wanted that change to be
  780.      visible to the interrupted code.  */
  781.   mark_save_restore (r0);
  782.   mark_save_restore (r1);
  783.   mark_save_restore (r2);
  784.   mark_save_restore (r3);
  785.   mark_save_restore (a0);
  786.   mark_save_restore (a1);
  787.   mark_save_restore (sb);
  788.   mark_save_restore (fb);
  789.   mark_save_restore (sp);
  790.   mark_save_restore (pc);
  791.   mark_save_restore (flg);

  792.   set_gdbarch_num_regs (arch, num_raw_regs);
  793.   set_gdbarch_num_pseudo_regs (arch, num_cooked_regs);
  794.   set_gdbarch_pc_regnum (arch, pc->num);
  795.   set_gdbarch_sp_regnum (arch, sp->num);
  796.   set_gdbarch_register_name (arch, m32c_register_name);
  797.   set_gdbarch_register_type (arch, m32c_register_type);
  798.   set_gdbarch_pseudo_register_read (arch, m32c_pseudo_register_read);
  799.   set_gdbarch_pseudo_register_write (arch, m32c_pseudo_register_write);
  800.   set_gdbarch_register_sim_regno (arch, m32c_register_sim_regno);
  801.   set_gdbarch_stab_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
  802.   set_gdbarch_dwarf2_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
  803.   set_gdbarch_register_reggroup_p (arch, m32c_register_reggroup_p);

  804.   reggroup_add (arch, general_reggroup);
  805.   reggroup_add (arch, all_reggroup);
  806.   reggroup_add (arch, save_reggroup);
  807.   reggroup_add (arch, restore_reggroup);
  808.   reggroup_add (arch, system_reggroup);
  809.   reggroup_add (arch, m32c_dma_reggroup);
  810. }



  811. /* Breakpoints.  */

  812. static const unsigned char *
  813. m32c_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
  814. {
  815.   static unsigned char break_insn[] = { 0x00 };        /* brk */

  816.   *len = sizeof (break_insn);
  817.   return break_insn;
  818. }



  819. /* Prologue analysis.  */

  820. struct m32c_prologue
  821. {
  822.   /* For consistency with the DWARF 2 .debug_frame info generated by
  823.      GCC, a frame's CFA is the address immediately after the saved
  824.      return address.  */

  825.   /* The architecture for which we generated this prologue info.  */
  826.   struct gdbarch *arch;

  827.   enum {
  828.     /* This function uses a frame pointer.  */
  829.     prologue_with_frame_ptr,

  830.     /* This function has no frame pointer.  */
  831.     prologue_sans_frame_ptr,

  832.     /* This function sets up the stack, so its frame is the first
  833.        frame on the stack.  */
  834.     prologue_first_frame

  835.   } kind;

  836.   /* If KIND is prologue_with_frame_ptr, this is the offset from the
  837.      CFA to where the frame pointer points.  This is always zero or
  838.      negative.  */
  839.   LONGEST frame_ptr_offset;

  840.   /* If KIND is prologue_sans_frame_ptr, the offset from the CFA to
  841.      the stack pointer --- always zero or negative.

  842.      Calling this a "size" is a bit misleading, but given that the
  843.      stack grows downwards, using offsets for everything keeps one
  844.      from going completely sign-crazy: you never change anything's
  845.      sign for an ADD instruction; always change the second operand's
  846.      sign for a SUB instruction; and everything takes care of
  847.      itself.

  848.      Functions that use alloca don't have a constant frame size.  But
  849.      they always have frame pointers, so we must use that to find the
  850.      CFA (and perhaps to unwind the stack pointer).  */
  851.   LONGEST frame_size;

  852.   /* The address of the first instruction at which the frame has been
  853.      set up and the arguments are where the debug info says they are
  854.      --- as best as we can tell.  */
  855.   CORE_ADDR prologue_end;

  856.   /* reg_offset[R] is the offset from the CFA at which register R is
  857.      saved, or 1 if register R has not been saved.  (Real values are
  858.      always zero or negative.)  */
  859.   LONGEST reg_offset[M32C_MAX_NUM_REGS];
  860. };


  861. /* The longest I've seen, anyway.  */
  862. #define M32C_MAX_INSN_LEN (9)

  863. /* Processor state, for the prologue analyzer.  */
  864. struct m32c_pv_state
  865. {
  866.   struct gdbarch *arch;
  867.   pv_t r0, r1, r2, r3;
  868.   pv_t a0, a1;
  869.   pv_t sb, fb, sp;
  870.   pv_t pc;
  871.   struct pv_area *stack;

  872.   /* Bytes from the current PC, the address they were read from,
  873.      and the address of the next unconsumed byte.  */
  874.   gdb_byte insn[M32C_MAX_INSN_LEN];
  875.   CORE_ADDR scan_pc, next_addr;
  876. };


  877. /* Push VALUE on STATE's stack, occupying SIZE bytes.  Return zero if
  878.    all went well, or non-zero if simulating the action would trash our
  879.    state.  */
  880. static int
  881. m32c_pv_push (struct m32c_pv_state *state, pv_t value, int size)
  882. {
  883.   if (pv_area_store_would_trash (state->stack, state->sp))
  884.     return 1;

  885.   state->sp = pv_add_constant (state->sp, -size);
  886.   pv_area_store (state->stack, state->sp, size, value);

  887.   return 0;
  888. }


  889. /* A source or destination location for an m16c or m32c
  890.    instruction.  */
  891. struct srcdest
  892. {
  893.   /* If srcdest_reg, the location is a register pointed to by REG.
  894.      If srcdest_partial_reg, the location is part of a register pointed
  895.      to by REG.  We don't try to handle this too well.
  896.      If srcdest_mem, the location is memory whose address is ADDR.  */
  897.   enum { srcdest_reg, srcdest_partial_reg, srcdest_mem } kind;
  898.   pv_t *reg, addr;
  899. };


  900. /* Return the SIZE-byte value at LOC in STATE.  */
  901. static pv_t
  902. m32c_srcdest_fetch (struct m32c_pv_state *state, struct srcdest loc, int size)
  903. {
  904.   if (loc.kind == srcdest_mem)
  905.     return pv_area_fetch (state->stack, loc.addr, size);
  906.   else if (loc.kind == srcdest_partial_reg)
  907.     return pv_unknown ();
  908.   else
  909.     return *loc.reg;
  910. }


  911. /* Write VALUE, a SIZE-byte value, to LOC in STATE.  Return zero if
  912.    all went well, or non-zero if simulating the store would trash our
  913.    state.  */
  914. static int
  915. m32c_srcdest_store (struct m32c_pv_state *state, struct srcdest loc,
  916.                     pv_t value, int size)
  917. {
  918.   if (loc.kind == srcdest_mem)
  919.     {
  920.       if (pv_area_store_would_trash (state->stack, loc.addr))
  921.         return 1;
  922.       pv_area_store (state->stack, loc.addr, size, value);
  923.     }
  924.   else if (loc.kind == srcdest_partial_reg)
  925.     *loc.reg = pv_unknown ();
  926.   else
  927.     *loc.reg = value;

  928.   return 0;
  929. }


  930. static int
  931. m32c_sign_ext (int v, int bits)
  932. {
  933.   int mask = 1 << (bits - 1);
  934.   return (v ^ mask) - mask;
  935. }

  936. static unsigned int
  937. m32c_next_byte (struct m32c_pv_state *st)
  938. {
  939.   gdb_assert (st->next_addr - st->scan_pc < sizeof (st->insn));
  940.   return st->insn[st->next_addr++ - st->scan_pc];
  941. }

  942. static int
  943. m32c_udisp8 (struct m32c_pv_state *st)
  944. {
  945.   return m32c_next_byte (st);
  946. }


  947. static int
  948. m32c_sdisp8 (struct m32c_pv_state *st)
  949. {
  950.   return m32c_sign_ext (m32c_next_byte (st), 8);
  951. }


  952. static int
  953. m32c_udisp16 (struct m32c_pv_state *st)
  954. {
  955.   int low  = m32c_next_byte (st);
  956.   int high = m32c_next_byte (st);

  957.   return low + (high << 8);
  958. }


  959. static int
  960. m32c_sdisp16 (struct m32c_pv_state *st)
  961. {
  962.   int low  = m32c_next_byte (st);
  963.   int high = m32c_next_byte (st);

  964.   return m32c_sign_ext (low + (high << 8), 16);
  965. }


  966. static int
  967. m32c_udisp24 (struct m32c_pv_state *st)
  968. {
  969.   int low  = m32c_next_byte (st);
  970.   int mid  = m32c_next_byte (st);
  971.   int high = m32c_next_byte (st);

  972.   return low + (mid << 8) + (high << 16);
  973. }


  974. /* Extract the 'source' field from an m32c MOV.size:G-format instruction.  */
  975. static int
  976. m32c_get_src23 (unsigned char *i)
  977. {
  978.   return (((i[0] & 0x70) >> 2)
  979.           | ((i[1] & 0x30) >> 4));
  980. }


  981. /* Extract the 'dest' field from an m32c MOV.size:G-format instruction.  */
  982. static int
  983. m32c_get_dest23 (unsigned char *i)
  984. {
  985.   return (((i[0] & 0x0e) << 1)
  986.           | ((i[1] & 0xc0) >> 6));
  987. }


  988. static struct srcdest
  989. m32c_decode_srcdest4 (struct m32c_pv_state *st,
  990.                       int code, int size)
  991. {
  992.   struct srcdest sd;

  993.   if (code < 6)
  994.     sd.kind = (size == 2 ? srcdest_reg : srcdest_partial_reg);
  995.   else
  996.     sd.kind = srcdest_mem;

  997.   sd.addr = pv_unknown ();
  998.   sd.reg = 0;

  999.   switch (code)
  1000.     {
  1001.     case 0x0: sd.reg = (size == 1 ? &st->r0 : &st->r0); break;
  1002.     case 0x1: sd.reg = (size == 1 ? &st->r0 : &st->r1); break;
  1003.     case 0x2: sd.reg = (size == 1 ? &st->r1 : &st->r2); break;
  1004.     case 0x3: sd.reg = (size == 1 ? &st->r1 : &st->r3); break;

  1005.     case 0x4: sd.reg = &st->a0; break;
  1006.     case 0x5: sd.reg = &st->a1; break;

  1007.     case 0x6: sd.addr = st->a0; break;
  1008.     case 0x7: sd.addr = st->a1; break;

  1009.     case 0x8: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
  1010.     case 0x9: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
  1011.     case 0xa: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
  1012.     case 0xb: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;

  1013.     case 0xc: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
  1014.     case 0xd: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
  1015.     case 0xe: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
  1016.     case 0xf: sd.addr = pv_constant (m32c_udisp16 (st)); break;

  1017.     default:
  1018.       gdb_assert_not_reached ("unexpected srcdest4");
  1019.     }

  1020.   return sd;
  1021. }


  1022. static struct srcdest
  1023. m32c_decode_sd23 (struct m32c_pv_state *st, int code, int size, int ind)
  1024. {
  1025.   struct srcdest sd;

  1026.   sd.addr = pv_unknown ();
  1027.   sd.reg = 0;

  1028.   switch (code)
  1029.     {
  1030.     case 0x12:
  1031.     case 0x13:
  1032.     case 0x10:
  1033.     case 0x11:
  1034.       sd.kind = (size == 1) ? srcdest_partial_reg : srcdest_reg;
  1035.       break;

  1036.     case 0x02:
  1037.     case 0x03:
  1038.       sd.kind = (size == 4) ? srcdest_reg : srcdest_partial_reg;
  1039.       break;

  1040.     default:
  1041.       sd.kind = srcdest_mem;
  1042.       break;

  1043.     }

  1044.   switch (code)
  1045.     {
  1046.     case 0x12: sd.reg = &st->r0; break;
  1047.     case 0x13: sd.reg = &st->r1; break;
  1048.     case 0x10: sd.reg = ((size == 1) ? &st->r0 : &st->r2); break;
  1049.     case 0x11: sd.reg = ((size == 1) ? &st->r1 : &st->r3); break;
  1050.     case 0x02: sd.reg = &st->a0; break;
  1051.     case 0x03: sd.reg = &st->a1; break;

  1052.     case 0x00: sd.addr = st->a0; break;
  1053.     case 0x01: sd.addr = st->a1; break;
  1054.     case 0x04: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
  1055.     case 0x05: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
  1056.     case 0x06: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
  1057.     case 0x07: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
  1058.     case 0x08: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
  1059.     case 0x09: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
  1060.     case 0x0a: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
  1061.     case 0x0b: sd.addr = pv_add_constant (st->fb, m32c_sdisp16 (st)); break;
  1062.     case 0x0c: sd.addr = pv_add_constant (st->a0, m32c_udisp24 (st)); break;
  1063.     case 0x0d: sd.addr = pv_add_constant (st->a1, m32c_udisp24 (st)); break;
  1064.     case 0x0f: sd.addr = pv_constant (m32c_udisp16 (st)); break;
  1065.     case 0x0e: sd.addr = pv_constant (m32c_udisp24 (st)); break;
  1066.     default:
  1067.       gdb_assert_not_reached ("unexpected sd23");
  1068.     }

  1069.   if (ind)
  1070.     {
  1071.       sd.addr = m32c_srcdest_fetch (st, sd, 4);
  1072.       sd.kind = srcdest_mem;
  1073.     }

  1074.   return sd;
  1075. }


  1076. /* The r16c and r32c machines have instructions with similar
  1077.    semantics, but completely different machine language encodings.  So
  1078.    we break out the semantics into their own functions, and leave
  1079.    machine-specific decoding in m32c_analyze_prologue.

  1080.    The following functions all expect their arguments already decoded,
  1081.    and they all return zero if analysis should continue past this
  1082.    instruction, or non-zero if analysis should stop.  */


  1083. /* Simulate an 'enter SIZE' instruction in STATE.  */
  1084. static int
  1085. m32c_pv_enter (struct m32c_pv_state *state, int size)
  1086. {
  1087.   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);

  1088.   /* If simulating this store would require us to forget
  1089.      everything we know about the stack frame in the name of
  1090.      accuracy, it would be better to just quit now.  */
  1091.   if (pv_area_store_would_trash (state->stack, state->sp))
  1092.     return 1;

  1093.   if (m32c_pv_push (state, state->fb, tdep->push_addr_bytes))
  1094.     return 1;
  1095.   state->fb = state->sp;
  1096.   state->sp = pv_add_constant (state->sp, -size);

  1097.   return 0;
  1098. }


  1099. static int
  1100. m32c_pv_pushm_one (struct m32c_pv_state *state, pv_t reg,
  1101.                    int bit, int src, int size)
  1102. {
  1103.   if (bit & src)
  1104.     {
  1105.       if (m32c_pv_push (state, reg, size))
  1106.         return 1;
  1107.     }

  1108.   return 0;
  1109. }


  1110. /* Simulate a 'pushm SRC' instruction in STATE.  */
  1111. static int
  1112. m32c_pv_pushm (struct m32c_pv_state *state, int src)
  1113. {
  1114.   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);

  1115.   /* The bits in SRC indicating which registers to save are:
  1116.      r0 r1 r2 r3 a0 a1 sb fb */
  1117.   return
  1118.     (   m32c_pv_pushm_one (state, state->fb, 0x01, src, tdep->push_addr_bytes)
  1119.      || m32c_pv_pushm_one (state, state->sb, 0x02, src, tdep->push_addr_bytes)
  1120.      || m32c_pv_pushm_one (state, state->a1, 0x04, src, tdep->push_addr_bytes)
  1121.      || m32c_pv_pushm_one (state, state->a0, 0x08, src, tdep->push_addr_bytes)
  1122.      || m32c_pv_pushm_one (state, state->r3, 0x10, src, 2)
  1123.      || m32c_pv_pushm_one (state, state->r2, 0x20, src, 2)
  1124.      || m32c_pv_pushm_one (state, state->r1, 0x40, src, 2)
  1125.      || m32c_pv_pushm_one (state, state->r0, 0x80, src, 2));
  1126. }

  1127. /* Return non-zero if VALUE is the first incoming argument register.  */

  1128. static int
  1129. m32c_is_1st_arg_reg (struct m32c_pv_state *state, pv_t value)
  1130. {
  1131.   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
  1132.   return (value.kind == pvk_register
  1133.           && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
  1134.               ? (value.reg == tdep->r1->num)
  1135.               : (value.reg == tdep->r0->num))
  1136.           && value.k == 0);
  1137. }

  1138. /* Return non-zero if VALUE is an incoming argument register.  */

  1139. static int
  1140. m32c_is_arg_reg (struct m32c_pv_state *state, pv_t value)
  1141. {
  1142.   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
  1143.   return (value.kind == pvk_register
  1144.           && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
  1145.               ? (value.reg == tdep->r1->num || value.reg == tdep->r2->num)
  1146.               : (value.reg == tdep->r0->num))
  1147.           && value.k == 0);
  1148. }

  1149. /* Return non-zero if a store of VALUE to LOC is probably spilling an
  1150.    argument register to its stack slot in STATE.  Such instructions
  1151.    should be included in the prologue, if possible.

  1152.    The store is a spill if:
  1153.    - the value being stored is the original value of an argument register;
  1154.    - the value has not already been stored somewhere in STACK; and
  1155.    - LOC is a stack slot (e.g., a memory location whose address is
  1156.      relative to the original value of the SP).  */

  1157. static int
  1158. m32c_is_arg_spill (struct m32c_pv_state *st,
  1159.                    struct srcdest loc,
  1160.                    pv_t value)
  1161. {
  1162.   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);

  1163.   return (m32c_is_arg_reg (st, value)
  1164.           && loc.kind == srcdest_mem
  1165.           && pv_is_register (loc.addr, tdep->sp->num)
  1166.           && ! pv_area_find_reg (st->stack, st->arch, value.reg, 0));
  1167. }

  1168. /* Return non-zero if a store of VALUE to LOC is probably
  1169.    copying the struct return address into an address register
  1170.    for immediate use.  This is basically a "spill" into the
  1171.    address register, instead of onto the stack.

  1172.    The prerequisites are:
  1173.    - value being stored is original value of the FIRST arg register;
  1174.    - value has not already been stored on stack; and
  1175.    - LOC is an address register (a0 or a1).  */

  1176. static int
  1177. m32c_is_struct_return (struct m32c_pv_state *st,
  1178.                        struct srcdest loc,
  1179.                        pv_t value)
  1180. {
  1181.   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);

  1182.   return (m32c_is_1st_arg_reg (st, value)
  1183.           && !pv_area_find_reg (st->stack, st->arch, value.reg, 0)
  1184.           && loc.kind == srcdest_reg
  1185.           && (pv_is_register (*loc.reg, tdep->a0->num)
  1186.               || pv_is_register (*loc.reg, tdep->a1->num)));
  1187. }

  1188. /* Return non-zero if a 'pushm' saving the registers indicated by SRC
  1189.    was a register save:
  1190.    - all the named registers should have their original values, and
  1191.    - the stack pointer should be at a constant offset from the
  1192.      original stack pointer.  */
  1193. static int
  1194. m32c_pushm_is_reg_save (struct m32c_pv_state *st, int src)
  1195. {
  1196.   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);
  1197.   /* The bits in SRC indicating which registers to save are:
  1198.      r0 r1 r2 r3 a0 a1 sb fb */
  1199.   return
  1200.     (pv_is_register (st->sp, tdep->sp->num)
  1201.      && (! (src & 0x01) || pv_is_register_k (st->fb, tdep->fb->num, 0))
  1202.      && (! (src & 0x02) || pv_is_register_k (st->sb, tdep->sb->num, 0))
  1203.      && (! (src & 0x04) || pv_is_register_k (st->a1, tdep->a1->num, 0))
  1204.      && (! (src & 0x08) || pv_is_register_k (st->a0, tdep->a0->num, 0))
  1205.      && (! (src & 0x10) || pv_is_register_k (st->r3, tdep->r3->num, 0))
  1206.      && (! (src & 0x20) || pv_is_register_k (st->r2, tdep->r2->num, 0))
  1207.      && (! (src & 0x40) || pv_is_register_k (st->r1, tdep->r1->num, 0))
  1208.      && (! (src & 0x80) || pv_is_register_k (st->r0, tdep->r0->num, 0)));
  1209. }


  1210. /* Function for finding saved registers in a 'struct pv_area'; we pass
  1211.    this to pv_area_scan.

  1212.    If VALUE is a saved register, ADDR says it was saved at a constant
  1213.    offset from the frame base, and SIZE indicates that the whole
  1214.    register was saved, record its offset in RESULT_UNTYPED.  */
  1215. static void
  1216. check_for_saved (void *prologue_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  1217. {
  1218.   struct m32c_prologue *prologue = (struct m32c_prologue *) prologue_untyped;
  1219.   struct gdbarch *arch = prologue->arch;
  1220.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);

  1221.   /* Is this the unchanged value of some register being saved on the
  1222.      stack?  */
  1223.   if (value.kind == pvk_register
  1224.       && value.k == 0
  1225.       && pv_is_register (addr, tdep->sp->num))
  1226.     {
  1227.       /* Some registers require special handling: they're saved as a
  1228.          larger value than the register itself.  */
  1229.       CORE_ADDR saved_size = register_size (arch, value.reg);

  1230.       if (value.reg == tdep->pc->num)
  1231.         saved_size = tdep->ret_addr_bytes;
  1232.       else if (register_type (arch, value.reg)
  1233.                == tdep->data_addr_reg_type)
  1234.         saved_size = tdep->push_addr_bytes;

  1235.       if (size == saved_size)
  1236.         {
  1237.           /* Find which end of the saved value corresponds to our
  1238.              register.  */
  1239.           if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
  1240.             prologue->reg_offset[value.reg]
  1241.               = (addr.k + saved_size - register_size (arch, value.reg));
  1242.           else
  1243.             prologue->reg_offset[value.reg] = addr.k;
  1244.         }
  1245.     }
  1246. }


  1247. /* Analyze the function prologue for ARCH at START, going no further
  1248.    than LIMIT, and place a description of what we found in
  1249.    PROLOGUE.  */
  1250. static void
  1251. m32c_analyze_prologue (struct gdbarch *arch,
  1252.                        CORE_ADDR start, CORE_ADDR limit,
  1253.                        struct m32c_prologue *prologue)
  1254. {
  1255.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  1256.   unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
  1257.   CORE_ADDR after_last_frame_related_insn;
  1258.   struct cleanup *back_to;
  1259.   struct m32c_pv_state st;

  1260.   st.arch = arch;
  1261.   st.r0 = pv_register (tdep->r0->num, 0);
  1262.   st.r1 = pv_register (tdep->r1->num, 0);
  1263.   st.r2 = pv_register (tdep->r2->num, 0);
  1264.   st.r3 = pv_register (tdep->r3->num, 0);
  1265.   st.a0 = pv_register (tdep->a0->num, 0);
  1266.   st.a1 = pv_register (tdep->a1->num, 0);
  1267.   st.sb = pv_register (tdep->sb->num, 0);
  1268.   st.fb = pv_register (tdep->fb->num, 0);
  1269.   st.sp = pv_register (tdep->sp->num, 0);
  1270.   st.pc = pv_register (tdep->pc->num, 0);
  1271.   st.stack = make_pv_area (tdep->sp->num, gdbarch_addr_bit (arch));
  1272.   back_to = make_cleanup_free_pv_area (st.stack);

  1273.   /* Record that the call instruction has saved the return address on
  1274.      the stack.  */
  1275.   m32c_pv_push (&st, st.pc, tdep->ret_addr_bytes);

  1276.   memset (prologue, 0, sizeof (*prologue));
  1277.   prologue->arch = arch;
  1278.   {
  1279.     int i;
  1280.     for (i = 0; i < M32C_MAX_NUM_REGS; i++)
  1281.       prologue->reg_offset[i] = 1;
  1282.   }

  1283.   st.scan_pc = after_last_frame_related_insn = start;

  1284.   while (st.scan_pc < limit)
  1285.     {
  1286.       pv_t pre_insn_fb = st.fb;
  1287.       pv_t pre_insn_sp = st.sp;

  1288.       /* In theory we could get in trouble by trying to read ahead
  1289.          here, when we only know we're expecting one byte.  In
  1290.          practice I doubt anyone will care, and it makes the rest of
  1291.          the code easier.  */
  1292.       if (target_read_memory (st.scan_pc, st.insn, sizeof (st.insn)))
  1293.         /* If we can't fetch the instruction from memory, stop here
  1294.            and hope for the best.  */
  1295.         break;
  1296.       st.next_addr = st.scan_pc;

  1297.       /* The assembly instructions are written as they appear in the
  1298.          section of the processor manuals that describe the
  1299.          instruction encodings.

  1300.          When a single assembly language instruction has several
  1301.          different machine-language encodings, the manual
  1302.          distinguishes them by a number in parens, before the
  1303.          mnemonic.  Those numbers are included, as well.

  1304.          The srcdest decoding instructions have the same names as the
  1305.          analogous functions in the simulator.  */
  1306.       if (mach == bfd_mach_m16c)
  1307.         {
  1308.           /* (1) ENTER #imm8 */
  1309.           if (st.insn[0] == 0x7c && st.insn[1] == 0xf2)
  1310.             {
  1311.               if (m32c_pv_enter (&st, st.insn[2]))
  1312.                 break;
  1313.               st.next_addr += 3;
  1314.             }
  1315.           /* (1) PUSHM src */
  1316.           else if (st.insn[0] == 0xec)
  1317.             {
  1318.               int src = st.insn[1];
  1319.               if (m32c_pv_pushm (&st, src))
  1320.                 break;
  1321.               st.next_addr += 2;

  1322.               if (m32c_pushm_is_reg_save (&st, src))
  1323.                 after_last_frame_related_insn = st.next_addr;
  1324.             }

  1325.           /* (6) MOV.size:G src, dest */
  1326.           else if ((st.insn[0] & 0xfe) == 0x72)
  1327.             {
  1328.               int size = (st.insn[0] & 0x01) ? 2 : 1;
  1329.               struct srcdest src;
  1330.               struct srcdest dest;
  1331.               pv_t src_value;
  1332.               st.next_addr += 2;

  1333.               src
  1334.                 = m32c_decode_srcdest4 (&st, (st.insn[1] >> 4) & 0xf, size);
  1335.               dest
  1336.                 = m32c_decode_srcdest4 (&st, st.insn[1] & 0xf, size);
  1337.               src_value = m32c_srcdest_fetch (&st, src, size);

  1338.               if (m32c_is_arg_spill (&st, dest, src_value))
  1339.                 after_last_frame_related_insn = st.next_addr;
  1340.               else if (m32c_is_struct_return (&st, dest, src_value))
  1341.                 after_last_frame_related_insn = st.next_addr;

  1342.               if (m32c_srcdest_store (&st, dest, src_value, size))
  1343.                 break;
  1344.             }

  1345.           /* (1) LDC #IMM16, sp */
  1346.           else if (st.insn[0] == 0xeb
  1347.                    && st.insn[1] == 0x50)
  1348.             {
  1349.               st.next_addr += 2;
  1350.               st.sp = pv_constant (m32c_udisp16 (&st));
  1351.             }

  1352.           else
  1353.             /* We've hit some instruction we don't know how to simulate.
  1354.                Strictly speaking, we should set every value we're
  1355.                tracking to "unknown".  But we'll be optimistic, assume
  1356.                that we have enough information already, and stop
  1357.                analysis here.  */
  1358.             break;
  1359.         }
  1360.       else
  1361.         {
  1362.           int src_indirect = 0;
  1363.           int dest_indirect = 0;
  1364.           int i = 0;

  1365.           gdb_assert (mach == bfd_mach_m32c);

  1366.           /* Check for prefix bytes indicating indirect addressing.  */
  1367.           if (st.insn[0] == 0x41)
  1368.             {
  1369.               src_indirect = 1;
  1370.               i++;
  1371.             }
  1372.           else if (st.insn[0] == 0x09)
  1373.             {
  1374.               dest_indirect = 1;
  1375.               i++;
  1376.             }
  1377.           else if (st.insn[0] == 0x49)
  1378.             {
  1379.               src_indirect = dest_indirect = 1;
  1380.               i++;
  1381.             }

  1382.           /* (1) ENTER #imm8 */
  1383.           if (st.insn[i] == 0xec)
  1384.             {
  1385.               if (m32c_pv_enter (&st, st.insn[i + 1]))
  1386.                 break;
  1387.               st.next_addr += 2;
  1388.             }

  1389.           /* (1) PUSHM src */
  1390.           else if (st.insn[i] == 0x8f)
  1391.             {
  1392.               int src = st.insn[i + 1];
  1393.               if (m32c_pv_pushm (&st, src))
  1394.                 break;
  1395.               st.next_addr += 2;

  1396.               if (m32c_pushm_is_reg_save (&st, src))
  1397.                 after_last_frame_related_insn = st.next_addr;
  1398.             }

  1399.           /* (7) MOV.size:G src, dest */
  1400.           else if ((st.insn[i] & 0x80) == 0x80
  1401.                    && (st.insn[i + 1] & 0x0f) == 0x0b
  1402.                    && m32c_get_src23 (&st.insn[i]) < 20
  1403.                    && m32c_get_dest23 (&st.insn[i]) < 20)
  1404.             {
  1405.               struct srcdest src;
  1406.               struct srcdest dest;
  1407.               pv_t src_value;
  1408.               int bw = st.insn[i] & 0x01;
  1409.               int size = bw ? 2 : 1;
  1410.               st.next_addr += 2;

  1411.               src
  1412.                 = m32c_decode_sd23 (&st, m32c_get_src23 (&st.insn[i]),
  1413.                                     size, src_indirect);
  1414.               dest
  1415.                 = m32c_decode_sd23 (&st, m32c_get_dest23 (&st.insn[i]),
  1416.                                     size, dest_indirect);
  1417.               src_value = m32c_srcdest_fetch (&st, src, size);

  1418.               if (m32c_is_arg_spill (&st, dest, src_value))
  1419.                 after_last_frame_related_insn = st.next_addr;

  1420.               if (m32c_srcdest_store (&st, dest, src_value, size))
  1421.                 break;
  1422.             }
  1423.           /* (2) LDC #IMM24, sp */
  1424.           else if (st.insn[i] == 0xd5
  1425.                    && st.insn[i + 1] == 0x29)
  1426.             {
  1427.               st.next_addr += 2;
  1428.               st.sp = pv_constant (m32c_udisp24 (&st));
  1429.             }
  1430.           else
  1431.             /* We've hit some instruction we don't know how to simulate.
  1432.                Strictly speaking, we should set every value we're
  1433.                tracking to "unknown".  But we'll be optimistic, assume
  1434.                that we have enough information already, and stop
  1435.                analysis here.  */
  1436.             break;
  1437.         }

  1438.       /* If this instruction changed the FB or decreased the SP (i.e.,
  1439.          allocated more stack space), then this may be a good place to
  1440.          declare the prologue finished.  However, there are some
  1441.          exceptions:

  1442.          - If the instruction just changed the FB back to its original
  1443.            value, then that's probably a restore instruction.  The
  1444.            prologue should definitely end before that.

  1445.          - If the instruction increased the value of the SP (that is,
  1446.            shrunk the frame), then it's probably part of a frame
  1447.            teardown sequence, and the prologue should end before
  1448.            that.  */

  1449.       if (! pv_is_identical (st.fb, pre_insn_fb))
  1450.         {
  1451.           if (! pv_is_register_k (st.fb, tdep->fb->num, 0))
  1452.             after_last_frame_related_insn = st.next_addr;
  1453.         }
  1454.       else if (! pv_is_identical (st.sp, pre_insn_sp))
  1455.         {
  1456.           /* The comparison of the constants looks odd, there, because
  1457.              .k is unsigned.  All it really means is that the SP is
  1458.              lower than it was before the instruction.  */
  1459.           if (   pv_is_register (pre_insn_sp, tdep->sp->num)
  1460.               && pv_is_register (st.sp,       tdep->sp->num)
  1461.               && ((pre_insn_sp.k - st.sp.k) < (st.sp.k - pre_insn_sp.k)))
  1462.             after_last_frame_related_insn = st.next_addr;
  1463.         }

  1464.       st.scan_pc = st.next_addr;
  1465.     }

  1466.   /* Did we load a constant value into the stack pointer?  */
  1467.   if (pv_is_constant (st.sp))
  1468.     prologue->kind = prologue_first_frame;

  1469.   /* Alternatively, did we initialize the frame pointer?  Remember
  1470.      that the CFA is the address after the return address.  */
  1471.   if (pv_is_register (st.fb, tdep->sp->num))
  1472.     {
  1473.       prologue->kind = prologue_with_frame_ptr;
  1474.       prologue->frame_ptr_offset = st.fb.k;
  1475.     }

  1476.   /* Is the frame size a known constant?  Remember that frame_size is
  1477.      actually the offset from the CFA to the SP (i.e., a negative
  1478.      value).  */
  1479.   else if (pv_is_register (st.sp, tdep->sp->num))
  1480.     {
  1481.       prologue->kind = prologue_sans_frame_ptr;
  1482.       prologue->frame_size = st.sp.k;
  1483.     }

  1484.   /* We haven't been able to make sense of this function's frame.  Treat
  1485.      it as the first frame.  */
  1486.   else
  1487.     prologue->kind = prologue_first_frame;

  1488.   /* Record where all the registers were saved.  */
  1489.   pv_area_scan (st.stack, check_for_saved, (void *) prologue);

  1490.   prologue->prologue_end = after_last_frame_related_insn;

  1491.   do_cleanups (back_to);
  1492. }


  1493. static CORE_ADDR
  1494. m32c_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR ip)
  1495. {
  1496.   const char *name;
  1497.   CORE_ADDR func_addr, func_end, sal_end;
  1498.   struct m32c_prologue p;

  1499.   /* Try to find the extent of the function that contains IP.  */
  1500.   if (! find_pc_partial_function (ip, &name, &func_addr, &func_end))
  1501.     return ip;

  1502.   /* Find end by prologue analysis.  */
  1503.   m32c_analyze_prologue (gdbarch, ip, func_end, &p);
  1504.   /* Find end by line info.  */
  1505.   sal_end = skip_prologue_using_sal (gdbarch, ip);
  1506.   /* Return whichever is lower.  */
  1507.   if (sal_end != 0 && sal_end != ip && sal_end < p.prologue_end)
  1508.     return sal_end;
  1509.   else
  1510.     return p.prologue_end;
  1511. }



  1512. /* Stack unwinding.  */

  1513. static struct m32c_prologue *
  1514. m32c_analyze_frame_prologue (struct frame_info *this_frame,
  1515.                              void **this_prologue_cache)
  1516. {
  1517.   if (! *this_prologue_cache)
  1518.     {
  1519.       CORE_ADDR func_start = get_frame_func (this_frame);
  1520.       CORE_ADDR stop_addr = get_frame_pc (this_frame);

  1521.       /* If we couldn't find any function containing the PC, then
  1522.          just initialize the prologue cache, but don't do anything.  */
  1523.       if (! func_start)
  1524.         stop_addr = func_start;

  1525.       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct m32c_prologue);
  1526.       m32c_analyze_prologue (get_frame_arch (this_frame),
  1527.                              func_start, stop_addr, *this_prologue_cache);
  1528.     }

  1529.   return *this_prologue_cache;
  1530. }


  1531. static CORE_ADDR
  1532. m32c_frame_base (struct frame_info *this_frame,
  1533.                 void **this_prologue_cache)
  1534. {
  1535.   struct m32c_prologue *p
  1536.     = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
  1537.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));

  1538.   /* In functions that use alloca, the distance between the stack
  1539.      pointer and the frame base varies dynamically, so we can't use
  1540.      the SP plus static information like prologue analysis to find the
  1541.      frame base.  However, such functions must have a frame pointer,
  1542.      to be able to restore the SP on exit.  So whenever we do have a
  1543.      frame pointer, use that to find the base.  */
  1544.   switch (p->kind)
  1545.     {
  1546.     case prologue_with_frame_ptr:
  1547.       {
  1548.         CORE_ADDR fb
  1549.           = get_frame_register_unsigned (this_frame, tdep->fb->num);
  1550.         return fb - p->frame_ptr_offset;
  1551.       }

  1552.     case prologue_sans_frame_ptr:
  1553.       {
  1554.         CORE_ADDR sp
  1555.           = get_frame_register_unsigned (this_frame, tdep->sp->num);
  1556.         return sp - p->frame_size;
  1557.       }

  1558.     case prologue_first_frame:
  1559.       return 0;

  1560.     default:
  1561.       gdb_assert_not_reached ("unexpected prologue kind");
  1562.     }
  1563. }


  1564. static void
  1565. m32c_this_id (struct frame_info *this_frame,
  1566.               void **this_prologue_cache,
  1567.               struct frame_id *this_id)
  1568. {
  1569.   CORE_ADDR base = m32c_frame_base (this_frame, this_prologue_cache);

  1570.   if (base)
  1571.     *this_id = frame_id_build (base, get_frame_func (this_frame));
  1572.   /* Otherwise, leave it unset, and that will terminate the backtrace.  */
  1573. }


  1574. static struct value *
  1575. m32c_prev_register (struct frame_info *this_frame,
  1576.                     void **this_prologue_cache, int regnum)
  1577. {
  1578.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
  1579.   struct m32c_prologue *p
  1580.     = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
  1581.   CORE_ADDR frame_base = m32c_frame_base (this_frame, this_prologue_cache);
  1582.   int reg_size = register_size (get_frame_arch (this_frame), regnum);

  1583.   if (regnum == tdep->sp->num)
  1584.     return frame_unwind_got_constant (this_frame, regnum, frame_base);

  1585.   /* If prologue analysis says we saved this register somewhere,
  1586.      return a description of the stack slot holding it.  */
  1587.   if (p->reg_offset[regnum] != 1)
  1588.     return frame_unwind_got_memory (this_frame, regnum,
  1589.                                     frame_base + p->reg_offset[regnum]);

  1590.   /* Otherwise, presume we haven't changed the value of this
  1591.      register, and get it from the next frame.  */
  1592.   return frame_unwind_got_register (this_frame, regnum, regnum);
  1593. }


  1594. static const struct frame_unwind m32c_unwind = {
  1595.   NORMAL_FRAME,
  1596.   default_frame_unwind_stop_reason,
  1597.   m32c_this_id,
  1598.   m32c_prev_register,
  1599.   NULL,
  1600.   default_frame_sniffer
  1601. };


  1602. static CORE_ADDR
  1603. m32c_unwind_pc (struct gdbarch *arch, struct frame_info *next_frame)
  1604. {
  1605.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  1606.   return frame_unwind_register_unsigned (next_frame, tdep->pc->num);
  1607. }


  1608. static CORE_ADDR
  1609. m32c_unwind_sp (struct gdbarch *arch, struct frame_info *next_frame)
  1610. {
  1611.   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
  1612.   return frame_unwind_register_unsigned (next_frame, tdep->sp->num);
  1613. }


  1614. /* Inferior calls.  */

  1615. /* The calling conventions, according to GCC:

  1616.    r8c, m16c
  1617.    ---------
  1618.    First arg may be passed in r1l or r1 if it (1) fits (QImode or
  1619.    HImode), (2) is named, and (3) is an integer or pointer type (no
  1620.    structs, floats, etc).  Otherwise, it's passed on the stack.

  1621.    Second arg may be passed in r2, same restrictions (but not QImode),
  1622.    even if the first arg is passed on the stack.

  1623.    Third and further args are passed on the stack.  No padding is
  1624.    used, stack "alignment" is 8 bits.

  1625.    m32cm, m32c
  1626.    -----------

  1627.    First arg may be passed in r0l or r0, same restrictions as above.

  1628.    Second and further args are passed on the stack.  Padding is used
  1629.    after QImode parameters (i.e. lower-addressed byte is the value,
  1630.    higher-addressed byte is the padding), stack "alignment" is 16
  1631.    bits.  */


  1632. /* Return true if TYPE is a type that can be passed in registers.  (We
  1633.    ignore the size, and pay attention only to the type code;
  1634.    acceptable sizes depends on which register is being considered to
  1635.    hold it.)  */
  1636. static int
  1637. m32c_reg_arg_type (struct type *type)
  1638. {
  1639.   enum type_code code = TYPE_CODE (type);

  1640.   return (code == TYPE_CODE_INT
  1641.           || code == TYPE_CODE_ENUM
  1642.           || code == TYPE_CODE_PTR
  1643.           || code == TYPE_CODE_REF
  1644.           || code == TYPE_CODE_BOOL
  1645.           || code == TYPE_CODE_CHAR);
  1646. }


  1647. static CORE_ADDR
  1648. m32c_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1649.                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  1650.                       struct value **args, CORE_ADDR sp, int struct_return,
  1651.                       CORE_ADDR struct_addr)
  1652. {
  1653.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1654.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1655.   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  1656.   CORE_ADDR cfa;
  1657.   int i;

  1658.   /* The number of arguments given in this function's prototype, or
  1659.      zero if it has a non-prototyped function type.  The m32c ABI
  1660.      passes arguments mentioned in the prototype differently from
  1661.      those in the ellipsis of a varargs function, or from those passed
  1662.      to a non-prototyped function.  */
  1663.   int num_prototyped_args = 0;

  1664.   {
  1665.     struct type *func_type = value_type (function);

  1666.     /* Dereference function pointer types.  */
  1667.     if (TYPE_CODE (func_type) == TYPE_CODE_PTR)
  1668.       func_type = TYPE_TARGET_TYPE (func_type);

  1669.     gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC ||
  1670.                 TYPE_CODE (func_type) == TYPE_CODE_METHOD);

  1671. #if 0
  1672.     /* The ABI description in gcc/config/m32c/m32c.abi says that
  1673.        we need to handle prototyped and non-prototyped functions
  1674.        separately, but the code in GCC doesn't actually do so.  */
  1675.     if (TYPE_PROTOTYPED (func_type))
  1676. #endif
  1677.       num_prototyped_args = TYPE_NFIELDS (func_type);
  1678.   }

  1679.   /* First, if the function returns an aggregate by value, push a
  1680.      pointer to a buffer for it.  This doesn't affect the way
  1681.      subsequent arguments are allocated to registers.  */
  1682.   if (struct_return)
  1683.     {
  1684.       int ptr_len = TYPE_LENGTH (tdep->ptr_voyd);
  1685.       sp -= ptr_len;
  1686.       write_memory_unsigned_integer (sp, ptr_len, byte_order, struct_addr);
  1687.     }

  1688.   /* Push the arguments.  */
  1689.   for (i = nargs - 1; i >= 0; i--)
  1690.     {
  1691.       struct value *arg = args[i];
  1692.       const gdb_byte *arg_bits = value_contents (arg);
  1693.       struct type *arg_type = value_type (arg);
  1694.       ULONGEST arg_size = TYPE_LENGTH (arg_type);

  1695.       /* Can it go in r1 or r1l (for m16c) or r0 or r0l (for m32c)?  */
  1696.       if (i == 0
  1697.           && arg_size <= 2
  1698.           && i < num_prototyped_args
  1699.           && m32c_reg_arg_type (arg_type))
  1700.         {
  1701.           /* Extract and re-store as an integer as a terse way to make
  1702.              sure it ends up in the least significant end of r1.  (GDB
  1703.              should avoid assuming endianness, even on uni-endian
  1704.              processors.)  */
  1705.           ULONGEST u = extract_unsigned_integer (arg_bits, arg_size,
  1706.                                                  byte_order);
  1707.           struct m32c_reg *reg = (mach == bfd_mach_m16c) ? tdep->r1 : tdep->r0;
  1708.           regcache_cooked_write_unsigned (regcache, reg->num, u);
  1709.         }

  1710.       /* Can it go in r2?  */
  1711.       else if (mach == bfd_mach_m16c
  1712.                && i == 1
  1713.                && arg_size == 2
  1714.                && i < num_prototyped_args
  1715.                && m32c_reg_arg_type (arg_type))
  1716.         regcache_cooked_write (regcache, tdep->r2->num, arg_bits);

  1717.       /* Everything else goes on the stack.  */
  1718.       else
  1719.         {
  1720.           sp -= arg_size;

  1721.           /* Align the stack.  */
  1722.           if (mach == bfd_mach_m32c)
  1723.             sp &= ~1;

  1724.           write_memory (sp, arg_bits, arg_size);
  1725.         }
  1726.     }

  1727.   /* This is the CFA we use to identify the dummy frame.  */
  1728.   cfa = sp;

  1729.   /* Push the return address.  */
  1730.   sp -= tdep->ret_addr_bytes;
  1731.   write_memory_unsigned_integer (sp, tdep->ret_addr_bytes, byte_order,
  1732.                                  bp_addr);

  1733.   /* Update the stack pointer.  */
  1734.   regcache_cooked_write_unsigned (regcache, tdep->sp->num, sp);

  1735.   /* We need to borrow an odd trick from the i386 target here.

  1736.      The value we return from this function gets used as the stack
  1737.      address (the CFA) for the dummy frame's ID.  The obvious thing is
  1738.      to return the new TOS.  However, that points at the return
  1739.      address, saved on the stack, which is inconsistent with the CFA's
  1740.      described by GCC's DWARF 2 .debug_frame information: DWARF 2
  1741.      .debug_frame info uses the address immediately after the saved
  1742.      return address.  So you end up with a dummy frame whose CFA
  1743.      points at the return address, but the frame for the function
  1744.      being called has a CFA pointing after the return address: the
  1745.      younger CFA is *greater than* the older CFA.  The sanity checks
  1746.      in frame.c don't like that.

  1747.      So we try to be consistent with the CFA's used by DWARF 2.
  1748.      Having a dummy frame and a real frame with the *same* CFA is
  1749.      tolerable.  */
  1750.   return cfa;
  1751. }


  1752. static struct frame_id
  1753. m32c_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1754. {
  1755.   /* This needs to return a frame ID whose PC is the return address
  1756.      passed to m32c_push_dummy_call, and whose stack_addr is the SP
  1757.      m32c_push_dummy_call returned.

  1758.      m32c_unwind_sp gives us the CFA, which is the value the SP had
  1759.      before the return address was pushed.  */
  1760.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1761.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, tdep->sp->num);
  1762.   return frame_id_build (sp, get_frame_pc (this_frame));
  1763. }



  1764. /* Return values.  */

  1765. /* Return value conventions, according to GCC:

  1766.    r8c, m16c
  1767.    ---------

  1768.    QImode in r0l
  1769.    HImode in r0
  1770.    SImode in r2r0
  1771.    near pointer in r0
  1772.    far pointer in r2r0

  1773.    Aggregate values (regardless of size) are returned by pushing a
  1774.    pointer to a temporary area on the stack after the args are pushed.
  1775.    The function fills in this area with the value.  Note that this
  1776.    pointer on the stack does not affect how register arguments, if any,
  1777.    are configured.

  1778.    m32cm, m32c
  1779.    -----------
  1780.    Same.  */

  1781. /* Return non-zero if values of type TYPE are returned by storing them
  1782.    in a buffer whose address is passed on the stack, ahead of the
  1783.    other arguments.  */
  1784. static int
  1785. m32c_return_by_passed_buf (struct type *type)
  1786. {
  1787.   enum type_code code = TYPE_CODE (type);

  1788.   return (code == TYPE_CODE_STRUCT
  1789.           || code == TYPE_CODE_UNION);
  1790. }

  1791. static enum return_value_convention
  1792. m32c_return_value (struct gdbarch *gdbarch,
  1793.                    struct value *function,
  1794.                    struct type *valtype,
  1795.                    struct regcache *regcache,
  1796.                    gdb_byte *readbuf,
  1797.                    const gdb_byte *writebuf)
  1798. {
  1799.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1800.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1801.   enum return_value_convention conv;
  1802.   ULONGEST valtype_len = TYPE_LENGTH (valtype);

  1803.   if (m32c_return_by_passed_buf (valtype))
  1804.     conv = RETURN_VALUE_STRUCT_CONVENTION;
  1805.   else
  1806.     conv = RETURN_VALUE_REGISTER_CONVENTION;

  1807.   if (readbuf)
  1808.     {
  1809.       /* We should never be called to find values being returned by
  1810.          RETURN_VALUE_STRUCT_CONVENTION.  Those can't be located,
  1811.          unless we made the call ourselves.  */
  1812.       gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);

  1813.       gdb_assert (valtype_len <= 8);

  1814.       /* Anything that fits in r0 is returned there.  */
  1815.       if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
  1816.         {
  1817.           ULONGEST u;
  1818.           regcache_cooked_read_unsigned (regcache, tdep->r0->num, &u);
  1819.           store_unsigned_integer (readbuf, valtype_len, byte_order, u);
  1820.         }
  1821.       else
  1822.         {
  1823.           /* Everything else is passed in mem0, using as many bytes as
  1824.              needed.  This is not what the Renesas tools do, but it's
  1825.              what GCC does at the moment.  */
  1826.           struct bound_minimal_symbol mem0
  1827.             = lookup_minimal_symbol ("mem0", NULL, NULL);

  1828.           if (! mem0.minsym)
  1829.             error (_("The return value is stored in memory at 'mem0', "
  1830.                      "but GDB cannot find\n"
  1831.                      "its address."));
  1832.           read_memory (BMSYMBOL_VALUE_ADDRESS (mem0), readbuf, valtype_len);
  1833.         }
  1834.     }

  1835.   if (writebuf)
  1836.     {
  1837.       /* We should never be called to store values to be returned
  1838.          using RETURN_VALUE_STRUCT_CONVENTION.  We have no way of
  1839.          finding the buffer, unless we made the call ourselves.  */
  1840.       gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);

  1841.       gdb_assert (valtype_len <= 8);

  1842.       /* Anything that fits in r0 is returned there.  */
  1843.       if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
  1844.         {
  1845.           ULONGEST u = extract_unsigned_integer (writebuf, valtype_len,
  1846.                                                  byte_order);
  1847.           regcache_cooked_write_unsigned (regcache, tdep->r0->num, u);
  1848.         }
  1849.       else
  1850.         {
  1851.           /* Everything else is passed in mem0, using as many bytes as
  1852.              needed.  This is not what the Renesas tools do, but it's
  1853.              what GCC does at the moment.  */
  1854.           struct bound_minimal_symbol mem0
  1855.             = lookup_minimal_symbol ("mem0", NULL, NULL);

  1856.           if (! mem0.minsym)
  1857.             error (_("The return value is stored in memory at 'mem0', "
  1858.                      "but GDB cannot find\n"
  1859.                      " its address."));
  1860.           write_memory (BMSYMBOL_VALUE_ADDRESS (mem0), writebuf, valtype_len);
  1861.         }
  1862.     }

  1863.   return conv;
  1864. }



  1865. /* Trampolines.  */

  1866. /* The m16c and m32c use a trampoline function for indirect function
  1867.    calls.  An indirect call looks like this:

  1868.              ... push arguments ...
  1869.              ... push target function address ...
  1870.              jsr.a m32c_jsri16

  1871.    The code for m32c_jsri16 looks like this:

  1872.      m32c_jsri16:

  1873.              # Save return address.
  1874.              pop.w        m32c_jsri_ret
  1875.              pop.b        m32c_jsri_ret+2

  1876.              # Store target function address.
  1877.              pop.w        m32c_jsri_addr

  1878.              # Re-push return address.
  1879.              push.b        m32c_jsri_ret+2
  1880.              push.w        m32c_jsri_ret

  1881.              # Call the target function.
  1882.              jmpi.a        m32c_jsri_addr

  1883.    Without further information, GDB will treat calls to m32c_jsri16
  1884.    like calls to any other function.  Since m32c_jsri16 doesn't have
  1885.    debugging information, that normally means that GDB sets a step-
  1886.    resume breakpoint and lets the program continue --- which is not
  1887.    what the user wanted.  (Giving the trampoline debugging info
  1888.    doesn't help: the user expects the program to stop in the function
  1889.    their program is calling, not in some trampoline code they've never
  1890.    seen before.)

  1891.    The gdbarch_skip_trampoline_code method tells GDB how to step
  1892.    through such trampoline functions transparently to the user.  When
  1893.    given the address of a trampoline function's first instruction,
  1894.    gdbarch_skip_trampoline_code should return the address of the first
  1895.    instruction of the function really being called.  If GDB decides it
  1896.    wants to step into that function, it will set a breakpoint there
  1897.    and silently continue to it.

  1898.    We recognize the trampoline by name, and extract the target address
  1899.    directly from the stack.  This isn't great, but recognizing by its
  1900.    code sequence seems more fragile.  */

  1901. static CORE_ADDR
  1902. m32c_skip_trampoline_code (struct frame_info *frame, CORE_ADDR stop_pc)
  1903. {
  1904.   struct gdbarch *gdbarch = get_frame_arch (frame);
  1905.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1906.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1907.   /* It would be nicer to simply look up the addresses of known
  1908.      trampolines once, and then compare stop_pc with them.  However,
  1909.      we'd need to ensure that that cached address got invalidated when
  1910.      someone loaded a new executable, and I'm not quite sure of the
  1911.      best way to do that.  find_pc_partial_function does do some
  1912.      caching, so we'll see how this goes.  */
  1913.   const char *name;
  1914.   CORE_ADDR start, end;

  1915.   if (find_pc_partial_function (stop_pc, &name, &start, &end))
  1916.     {
  1917.       /* Are we stopped at the beginning of the trampoline function?  */
  1918.       if (strcmp (name, "m32c_jsri16") == 0
  1919.           && stop_pc == start)
  1920.         {
  1921.           /* Get the stack pointer.  The return address is at the top,
  1922.              and the target function's address is just below that.  We
  1923.              know it's a two-byte address, since the trampoline is
  1924.              m32c_jsri*16*.  */
  1925.           CORE_ADDR sp = get_frame_sp (get_current_frame ());
  1926.           CORE_ADDR target
  1927.             = read_memory_unsigned_integer (sp + tdep->ret_addr_bytes,
  1928.                                             2, byte_order);

  1929.           /* What we have now is the address of a jump instruction.
  1930.              What we need is the destination of that jump.
  1931.              The opcode is 1 byte, and the destination is the next 3 bytes.  */

  1932.           target = read_memory_unsigned_integer (target + 1, 3, byte_order);
  1933.           return target;
  1934.         }
  1935.     }

  1936.   return 0;
  1937. }


  1938. /* Address/pointer conversions.  */

  1939. /* On the m16c, there is a 24-bit address space, but only a very few
  1940.    instructions can generate addresses larger than 0xffff: jumps,
  1941.    jumps to subroutines, and the lde/std (load/store extended)
  1942.    instructions.

  1943.    Since GCC can only support one size of pointer, we can't have
  1944.    distinct 'near' and 'far' pointer types; we have to pick one size
  1945.    for everything.  If we wanted to use 24-bit pointers, then GCC
  1946.    would have to use lde and ste for all memory references, which
  1947.    would be terrible for performance and code size.  So the GNU
  1948.    toolchain uses 16-bit pointers for everything, and gives up the
  1949.    ability to have pointers point outside the first 64k of memory.

  1950.    However, as a special hack, we let the linker place functions at
  1951.    addresses above 0xffff, as long as it also places a trampoline in
  1952.    the low 64k for every function whose address is taken.  Each
  1953.    trampoline consists of a single jmp.a instruction that jumps to the
  1954.    function's real entry point.  Pointers to functions can be 16 bits
  1955.    long, even though the functions themselves are at higher addresses:
  1956.    the pointers refer to the trampolines, not the functions.

  1957.    This complicates things for GDB, however: given the address of a
  1958.    function (from debug info or linker symbols, say) which could be
  1959.    anywhere in the 24-bit address space, how can we find an
  1960.    appropriate 16-bit value to use as a pointer to it?

  1961.    If the linker has not generated a trampoline for the function,
  1962.    we're out of luck.  Well, I guess we could malloc some space and
  1963.    write a jmp.a instruction to it, but I'm not going to get into that
  1964.    at the moment.

  1965.    If the linker has generated a trampoline for the function, then it
  1966.    also emitted a symbol for the trampoline: if the function's linker
  1967.    symbol is named NAME, then the function's trampoline's linker
  1968.    symbol is named NAME.plt.

  1969.    So, given a code address:
  1970.    - We try to find a linker symbol at that address.
  1971.    - If we find such a symbol named NAME, we look for a linker symbol
  1972.      named NAME.plt.
  1973.    - If we find such a symbol, we assume it is a trampoline, and use
  1974.      its address as the pointer value.

  1975.    And, given a function pointer:
  1976.    - We try to find a linker symbol at that address named NAME.plt.
  1977.    - If we find such a symbol, we look for a linker symbol named NAME.
  1978.    - If we find that, we provide that as the function's address.
  1979.    - If any of the above steps fail, we return the original address
  1980.      unchanged; it might really be a function in the low 64k.

  1981.    See?  You *knew* there was a reason you wanted to be a computer
  1982.    programmer!  :)  */

  1983. static void
  1984. m32c_m16c_address_to_pointer (struct gdbarch *gdbarch,
  1985.                               struct type *type, gdb_byte *buf, CORE_ADDR addr)
  1986. {
  1987.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1988.   enum type_code target_code;
  1989.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_PTR ||
  1990.               TYPE_CODE (type) == TYPE_CODE_REF);

  1991.   target_code = TYPE_CODE (TYPE_TARGET_TYPE (type));

  1992.   if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
  1993.     {
  1994.       const char *func_name;
  1995.       char *tramp_name;
  1996.       struct bound_minimal_symbol tramp_msym;

  1997.       /* Try to find a linker symbol at this address.  */
  1998.       struct bound_minimal_symbol func_msym
  1999.         = lookup_minimal_symbol_by_pc (addr);

  2000.       if (! func_msym.minsym)
  2001.         error (_("Cannot convert code address %s to function pointer:\n"
  2002.                "couldn't find a symbol at that address, to find trampoline."),
  2003.                paddress (gdbarch, addr));

  2004.       func_name = MSYMBOL_LINKAGE_NAME (func_msym.minsym);
  2005.       tramp_name = xmalloc (strlen (func_name) + 5);
  2006.       strcpy (tramp_name, func_name);
  2007.       strcat (tramp_name, ".plt");

  2008.       /* Try to find a linker symbol for the trampoline.  */
  2009.       tramp_msym = lookup_minimal_symbol (tramp_name, NULL, NULL);

  2010.       /* We've either got another copy of the name now, or don't need
  2011.          the name any more.  */
  2012.       xfree (tramp_name);

  2013.       if (! tramp_msym.minsym)
  2014.         {
  2015.           CORE_ADDR ptrval;

  2016.           /* No PLT entry found.  Mask off the upper bits of the address
  2017.              to make a pointer.  As noted in the warning to the user
  2018.              below, this value might be useful if converted back into
  2019.              an address by GDB, but will otherwise, almost certainly,
  2020.              be garbage.

  2021.              Using this masked result does seem to be useful
  2022.              in gdb.cp/cplusfuncs.exp in which ~40 FAILs turn into
  2023.              PASSes.  These results appear to be correct as well.

  2024.              We print a warning here so that the user can make a
  2025.              determination about whether the result is useful or not.  */
  2026.           ptrval = addr & 0xffff;

  2027.           warning (_("Cannot convert code address %s to function pointer:\n"
  2028.                    "couldn't find trampoline named '%s.plt'.\n"
  2029.                    "Returning pointer value %s instead; this may produce\n"
  2030.                    "a useful result if converted back into an address by GDB,\n"
  2031.                    "but will most likely not be useful otherwise.\n"),
  2032.                    paddress (gdbarch, addr), func_name,
  2033.                    paddress (gdbarch, ptrval));

  2034.           addr = ptrval;

  2035.         }
  2036.       else
  2037.         {
  2038.           /* The trampoline's address is our pointer.  */
  2039.           addr = BMSYMBOL_VALUE_ADDRESS (tramp_msym);
  2040.         }
  2041.     }

  2042.   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  2043. }


  2044. static CORE_ADDR
  2045. m32c_m16c_pointer_to_address (struct gdbarch *gdbarch,
  2046.                               struct type *type, const gdb_byte *buf)
  2047. {
  2048.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2049.   CORE_ADDR ptr;
  2050.   enum type_code target_code;

  2051.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_PTR ||
  2052.               TYPE_CODE (type) == TYPE_CODE_REF);

  2053.   ptr = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);

  2054.   target_code = TYPE_CODE (TYPE_TARGET_TYPE (type));

  2055.   if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
  2056.     {
  2057.       /* See if there is a minimal symbol at that address whose name is
  2058.          "NAME.plt".  */
  2059.       struct bound_minimal_symbol ptr_msym = lookup_minimal_symbol_by_pc (ptr);

  2060.       if (ptr_msym.minsym)
  2061.         {
  2062.           const char *ptr_msym_name = MSYMBOL_LINKAGE_NAME (ptr_msym.minsym);
  2063.           int len = strlen (ptr_msym_name);

  2064.           if (len > 4
  2065.               && strcmp (ptr_msym_name + len - 4, ".plt") == 0)
  2066.             {
  2067.               struct bound_minimal_symbol func_msym;
  2068.               /* We have a .plt symbol; try to find the symbol for the
  2069.                  corresponding function.

  2070.                  Since the trampoline contains a jump instruction, we
  2071.                  could also just extract the jump's target addressI
  2072.                  don't see much advantage one way or the other.  */
  2073.               char *func_name = xmalloc (len - 4 + 1);
  2074.               memcpy (func_name, ptr_msym_name, len - 4);
  2075.               func_name[len - 4] = '\0';
  2076.               func_msym
  2077.                 = lookup_minimal_symbol (func_name, NULL, NULL);

  2078.               /* If we do have such a symbol, return its value as the
  2079.                  function's true address.  */
  2080.               if (func_msym.minsym)
  2081.                 ptr = BMSYMBOL_VALUE_ADDRESS (func_msym);
  2082.             }
  2083.         }
  2084.       else
  2085.         {
  2086.           int aspace;

  2087.           for (aspace = 1; aspace <= 15; aspace++)
  2088.             {
  2089.               ptr_msym = lookup_minimal_symbol_by_pc ((aspace << 16) | ptr);

  2090.               if (ptr_msym.minsym)
  2091.                 ptr |= aspace << 16;
  2092.             }
  2093.         }
  2094.     }

  2095.   return ptr;
  2096. }

  2097. static void
  2098. m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
  2099.                             int *frame_regnum,
  2100.                             LONGEST *frame_offset)
  2101. {
  2102.   const char *name;
  2103.   CORE_ADDR func_addr, func_end;
  2104.   struct m32c_prologue p;

  2105.   struct regcache *regcache = get_current_regcache ();
  2106.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  2107.   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  2108.     internal_error (__FILE__, __LINE__,
  2109.                     _("No virtual frame pointer available"));

  2110.   m32c_analyze_prologue (gdbarch, func_addr, pc, &p);
  2111.   switch (p.kind)
  2112.     {
  2113.     case prologue_with_frame_ptr:
  2114.       *frame_regnum = m32c_banked_register (tdep->fb, regcache)->num;
  2115.       *frame_offset = p.frame_ptr_offset;
  2116.       break;
  2117.     case prologue_sans_frame_ptr:
  2118.       *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
  2119.       *frame_offset = p.frame_size;
  2120.       break;
  2121.     default:
  2122.       *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
  2123.       *frame_offset = 0;
  2124.       break;
  2125.     }
  2126.   /* Sanity check */
  2127.   if (*frame_regnum > gdbarch_num_regs (gdbarch))
  2128.     internal_error (__FILE__, __LINE__,
  2129.                     _("No virtual frame pointer available"));
  2130. }


  2131. /* Initialization.  */

  2132. static struct gdbarch *
  2133. m32c_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  2134. {
  2135.   struct gdbarch *arch;
  2136.   struct gdbarch_tdep *tdep;
  2137.   unsigned long mach = info.bfd_arch_info->mach;

  2138.   /* Find a candidate among the list of architectures we've created
  2139.      already.  */
  2140.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  2141.        arches != NULL;
  2142.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  2143.     return arches->gdbarch;

  2144.   tdep = xcalloc (1, sizeof (*tdep));
  2145.   arch = gdbarch_alloc (&info, tdep);

  2146.   /* Essential types.  */
  2147.   make_types (arch);

  2148.   /* Address/pointer conversions.  */
  2149.   if (mach == bfd_mach_m16c)
  2150.     {
  2151.       set_gdbarch_address_to_pointer (arch, m32c_m16c_address_to_pointer);
  2152.       set_gdbarch_pointer_to_address (arch, m32c_m16c_pointer_to_address);
  2153.     }

  2154.   /* Register set.  */
  2155.   make_regs (arch);

  2156.   /* Disassembly.  */
  2157.   set_gdbarch_print_insn (arch, print_insn_m32c);

  2158.   /* Breakpoints.  */
  2159.   set_gdbarch_breakpoint_from_pc (arch, m32c_breakpoint_from_pc);

  2160.   /* Prologue analysis and unwinding.  */
  2161.   set_gdbarch_inner_than (arch, core_addr_lessthan);
  2162.   set_gdbarch_skip_prologue (arch, m32c_skip_prologue);
  2163.   set_gdbarch_unwind_pc (arch, m32c_unwind_pc);
  2164.   set_gdbarch_unwind_sp (arch, m32c_unwind_sp);
  2165. #if 0
  2166.   /* I'm dropping the dwarf2 sniffer because it has a few problems.
  2167.      They may be in the dwarf2 cfi code in GDB, or they may be in
  2168.      the debug info emitted by the upstream toolchain.  I don't
  2169.      know which, but I do know that the prologue analyzer works better.
  2170.      MVS 04/13/06  */
  2171.   dwarf2_append_sniffers (arch);
  2172. #endif
  2173.   frame_unwind_append_unwinder (arch, &m32c_unwind);

  2174.   /* Inferior calls.  */
  2175.   set_gdbarch_push_dummy_call (arch, m32c_push_dummy_call);
  2176.   set_gdbarch_return_value (arch, m32c_return_value);
  2177.   set_gdbarch_dummy_id (arch, m32c_dummy_id);

  2178.   /* Trampolines.  */
  2179.   set_gdbarch_skip_trampoline_code (arch, m32c_skip_trampoline_code);

  2180.   set_gdbarch_virtual_frame_pointer (arch, m32c_virtual_frame_pointer);

  2181.   /* m32c function boundary addresses are not necessarily even.
  2182.      Therefore, the `vbit', which indicates a pointer to a virtual
  2183.      member function, is stored in the delta field, rather than as
  2184.      the low bit of a function pointer address.

  2185.      In order to verify this, see the definition of
  2186.      TARGET_PTRMEMFUNC_VBIT_LOCATION in gcc/defaults.h along with the
  2187.      definition of FUNCTION_BOUNDARY in gcc/config/m32c/m32c.h.  */
  2188.   set_gdbarch_vbit_in_delta (arch, 1);

  2189.   return arch;
  2190. }

  2191. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2192. extern initialize_file_ftype _initialize_m32c_tdep;

  2193. void
  2194. _initialize_m32c_tdep (void)
  2195. {
  2196.   register_gdbarch_init (bfd_arch_m32c, m32c_gdbarch_init);

  2197.   m32c_dma_reggroup = reggroup_new ("dma", USER_REGGROUP);
  2198. }