GDB (API)
/home/stan/gdb/src/gdb/m32c-tdep.c
Go to the documentation of this file.
00001 /* Renesas M32C target-dependent code for GDB, the GNU debugger.
00002 
00003    Copyright (C) 2004-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 
00022 #include <stdarg.h>
00023 
00024 #if defined (HAVE_STRING_H)
00025 #include <string.h>
00026 #endif
00027 
00028 #include "gdb_assert.h"
00029 #include "elf-bfd.h"
00030 #include "elf/m32c.h"
00031 #include "gdb/sim-m32c.h"
00032 #include "dis-asm.h"
00033 #include "gdbtypes.h"
00034 #include "regcache.h"
00035 #include "arch-utils.h"
00036 #include "frame.h"
00037 #include "frame-unwind.h"
00038 #include "dwarf2-frame.h"
00039 #include "dwarf2expr.h"
00040 #include "symtab.h"
00041 #include "gdbcore.h"
00042 #include "value.h"
00043 #include "reggroups.h"
00044 #include "prologue-value.h"
00045 #include "target.h"
00046 
00047 
00048 /* The m32c tdep structure.  */
00049 
00050 static struct reggroup *m32c_dma_reggroup;
00051 
00052 struct m32c_reg;
00053 
00054 /* The type of a function that moves the value of REG between CACHE or
00055    BUF --- in either direction.  */
00056 typedef enum register_status (m32c_move_reg_t) (struct m32c_reg *reg,
00057                                                 struct regcache *cache,
00058                                                 void *buf);
00059 
00060 struct m32c_reg
00061 {
00062   /* The name of this register.  */
00063   const char *name;
00064 
00065   /* Its type.  */
00066   struct type *type;
00067 
00068   /* The architecture this register belongs to.  */
00069   struct gdbarch *arch;
00070 
00071   /* Its GDB register number.  */
00072   int num;
00073 
00074   /* Its sim register number.  */
00075   int sim_num;
00076 
00077   /* Its DWARF register number, or -1 if it doesn't have one.  */
00078   int dwarf_num;
00079 
00080   /* Register group memberships.  */
00081   unsigned int general_p : 1;
00082   unsigned int dma_p : 1;
00083   unsigned int system_p : 1;
00084   unsigned int save_restore_p : 1;
00085 
00086   /* Functions to read its value from a regcache, and write its value
00087      to a regcache.  */
00088   m32c_move_reg_t *read, *write;
00089 
00090   /* Data for READ and WRITE functions.  The exact meaning depends on
00091      the specific functions selected; see the comments for those
00092      functions.  */
00093   struct m32c_reg *rx, *ry;
00094   int n;
00095 };
00096 
00097 
00098 /* An overestimate of the number of raw and pseudoregisters we will
00099    have.  The exact answer depends on the variant of the architecture
00100    at hand, but we can use this to declare statically allocated
00101    arrays, and bump it up when needed.  */
00102 #define M32C_MAX_NUM_REGS (75)
00103 
00104 /* The largest assigned DWARF register number.  */
00105 #define M32C_MAX_DWARF_REGNUM (40)
00106 
00107 
00108 struct gdbarch_tdep
00109 {
00110   /* All the registers for this variant, indexed by GDB register
00111      number, and the number of registers present.  */
00112   struct m32c_reg regs[M32C_MAX_NUM_REGS];
00113 
00114   /* The number of valid registers.  */
00115   int num_regs;
00116 
00117   /* Interesting registers.  These are pointers into REGS.  */
00118   struct m32c_reg *pc, *flg;
00119   struct m32c_reg *r0, *r1, *r2, *r3, *a0, *a1;
00120   struct m32c_reg *r2r0, *r3r2r1r0, *r3r1r2r0;
00121   struct m32c_reg *sb, *fb, *sp;
00122 
00123   /* A table indexed by DWARF register numbers, pointing into
00124      REGS.  */
00125   struct m32c_reg *dwarf_regs[M32C_MAX_DWARF_REGNUM + 1];
00126 
00127   /* Types for this architecture.  We can't use the builtin_type_foo
00128      types, because they're not initialized when building a gdbarch
00129      structure.  */
00130   struct type *voyd, *ptr_voyd, *func_voyd;
00131   struct type *uint8, *uint16;
00132   struct type *int8, *int16, *int32, *int64;
00133 
00134   /* The types for data address and code address registers.  */
00135   struct type *data_addr_reg_type, *code_addr_reg_type;
00136 
00137   /* The number of bytes a return address pushed by a 'jsr' instruction
00138      occupies on the stack.  */
00139   int ret_addr_bytes;
00140 
00141   /* The number of bytes an address register occupies on the stack
00142      when saved by an 'enter' or 'pushm' instruction.  */
00143   int push_addr_bytes;
00144 };
00145 
00146 
00147 /* Types.  */
00148 
00149 static void
00150 make_types (struct gdbarch *arch)
00151 {
00152   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
00153   unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
00154   int data_addr_reg_bits, code_addr_reg_bits;
00155   char type_name[50];
00156 
00157 #if 0
00158   /* This is used to clip CORE_ADDR values, so this value is
00159      appropriate both on the m32c, where pointers are 32 bits long,
00160      and on the m16c, where pointers are sixteen bits long, but there
00161      may be code above the 64k boundary.  */
00162   set_gdbarch_addr_bit (arch, 24);
00163 #else
00164   /* GCC uses 32 bits for addrs in the dwarf info, even though
00165      only 16/24 bits are used.  Setting addr_bit to 24 causes
00166      errors in reading the dwarf addresses.  */
00167   set_gdbarch_addr_bit (arch, 32);
00168 #endif
00169 
00170   set_gdbarch_int_bit (arch, 16);
00171   switch (mach)
00172     {
00173     case bfd_mach_m16c:
00174       data_addr_reg_bits = 16;
00175       code_addr_reg_bits = 24;
00176       set_gdbarch_ptr_bit (arch, 16);
00177       tdep->ret_addr_bytes = 3;
00178       tdep->push_addr_bytes = 2;
00179       break;
00180 
00181     case bfd_mach_m32c:
00182       data_addr_reg_bits = 24;
00183       code_addr_reg_bits = 24;
00184       set_gdbarch_ptr_bit (arch, 32);
00185       tdep->ret_addr_bytes = 4;
00186       tdep->push_addr_bytes = 4;
00187       break;
00188 
00189     default:
00190       gdb_assert_not_reached ("unexpected mach");
00191     }
00192 
00193   /* The builtin_type_mumble variables are sometimes uninitialized when
00194      this is called, so we avoid using them.  */
00195   tdep->voyd = arch_type (arch, TYPE_CODE_VOID, 1, "void");
00196   tdep->ptr_voyd
00197     = arch_type (arch, TYPE_CODE_PTR, gdbarch_ptr_bit (arch) / TARGET_CHAR_BIT,
00198                  NULL);
00199   TYPE_TARGET_TYPE (tdep->ptr_voyd) = tdep->voyd;
00200   TYPE_UNSIGNED (tdep->ptr_voyd) = 1;
00201   tdep->func_voyd = lookup_function_type (tdep->voyd);
00202 
00203   xsnprintf (type_name, sizeof (type_name), "%s_data_addr_t",
00204              gdbarch_bfd_arch_info (arch)->printable_name);
00205   tdep->data_addr_reg_type
00206     = arch_type (arch, TYPE_CODE_PTR, data_addr_reg_bits / TARGET_CHAR_BIT,
00207                  xstrdup (type_name));
00208   TYPE_TARGET_TYPE (tdep->data_addr_reg_type) = tdep->voyd;
00209   TYPE_UNSIGNED (tdep->data_addr_reg_type) = 1;
00210 
00211   xsnprintf (type_name, sizeof (type_name), "%s_code_addr_t",
00212              gdbarch_bfd_arch_info (arch)->printable_name);
00213   tdep->code_addr_reg_type
00214     = arch_type (arch, TYPE_CODE_PTR, code_addr_reg_bits / TARGET_CHAR_BIT,
00215                  xstrdup (type_name));
00216   TYPE_TARGET_TYPE (tdep->code_addr_reg_type) = tdep->func_voyd;
00217   TYPE_UNSIGNED (tdep->code_addr_reg_type) = 1;
00218 
00219   tdep->uint8  = arch_integer_type (arch,  8, 1, "uint8_t");
00220   tdep->uint16 = arch_integer_type (arch, 16, 1, "uint16_t");
00221   tdep->int8   = arch_integer_type (arch,  8, 0, "int8_t");
00222   tdep->int16  = arch_integer_type (arch, 16, 0, "int16_t");
00223   tdep->int32  = arch_integer_type (arch, 32, 0, "int32_t");
00224   tdep->int64  = arch_integer_type (arch, 64, 0, "int64_t");
00225 }
00226 
00227 
00228 
00229 /* Register set.  */
00230 
00231 static const char *
00232 m32c_register_name (struct gdbarch *gdbarch, int num)
00233 {
00234   return gdbarch_tdep (gdbarch)->regs[num].name;
00235 }
00236 
00237 
00238 static struct type *
00239 m32c_register_type (struct gdbarch *arch, int reg_nr)
00240 {
00241   return gdbarch_tdep (arch)->regs[reg_nr].type;
00242 }
00243 
00244 
00245 static int
00246 m32c_register_sim_regno (struct gdbarch *gdbarch, int reg_nr)
00247 {
00248   return gdbarch_tdep (gdbarch)->regs[reg_nr].sim_num;
00249 }
00250 
00251 
00252 static int
00253 m32c_debug_info_reg_to_regnum (struct gdbarch *gdbarch, int reg_nr)
00254 {
00255   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00256   if (0 <= reg_nr && reg_nr <= M32C_MAX_DWARF_REGNUM
00257       && tdep->dwarf_regs[reg_nr])
00258     return tdep->dwarf_regs[reg_nr]->num;
00259   else
00260     /* The DWARF CFI code expects to see -1 for invalid register
00261        numbers.  */
00262     return -1;
00263 }
00264 
00265 
00266 static int
00267 m32c_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
00268                           struct reggroup *group)
00269 {
00270   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00271   struct m32c_reg *reg = &tdep->regs[regnum];
00272 
00273   /* The anonymous raw registers aren't in any groups.  */
00274   if (! reg->name)
00275     return 0;
00276 
00277   if (group == all_reggroup)
00278     return 1;
00279 
00280   if (group == general_reggroup
00281       && reg->general_p)
00282     return 1;
00283 
00284   if (group == m32c_dma_reggroup
00285       && reg->dma_p)
00286     return 1;
00287 
00288   if (group == system_reggroup
00289       && reg->system_p)
00290     return 1;
00291 
00292   /* Since the m32c DWARF register numbers refer to cooked registers, not
00293      raw registers, and frame_pop depends on the save and restore groups
00294      containing registers the DWARF CFI will actually mention, our save
00295      and restore groups are cooked registers, not raw registers.  (This is
00296      why we can't use the default reggroup function.)  */
00297   if ((group == save_reggroup
00298        || group == restore_reggroup)
00299       && reg->save_restore_p)
00300     return 1;
00301 
00302   return 0;
00303 }
00304 
00305 
00306 /* Register move functions.  We declare them here using
00307    m32c_move_reg_t to check the types.  */
00308 static m32c_move_reg_t m32c_raw_read,      m32c_raw_write;
00309 static m32c_move_reg_t m32c_banked_read,   m32c_banked_write;
00310 static m32c_move_reg_t m32c_sb_read,       m32c_sb_write;
00311 static m32c_move_reg_t m32c_part_read,     m32c_part_write;
00312 static m32c_move_reg_t m32c_cat_read,      m32c_cat_write;
00313 static m32c_move_reg_t m32c_r3r2r1r0_read, m32c_r3r2r1r0_write;
00314 
00315 
00316 /* Copy the value of the raw register REG from CACHE to BUF.  */
00317 static enum register_status
00318 m32c_raw_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
00319 {
00320   return regcache_raw_read (cache, reg->num, buf);
00321 }
00322 
00323 
00324 /* Copy the value of the raw register REG from BUF to CACHE.  */
00325 static enum register_status
00326 m32c_raw_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
00327 {
00328   regcache_raw_write (cache, reg->num, (const void *) buf);
00329 
00330   return REG_VALID;
00331 }
00332 
00333 
00334 /* Return the value of the 'flg' register in CACHE.  */
00335 static int
00336 m32c_read_flg (struct regcache *cache)
00337 {
00338   struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (cache));
00339   ULONGEST flg;
00340   regcache_raw_read_unsigned (cache, tdep->flg->num, &flg);
00341   return flg & 0xffff;
00342 }
00343 
00344 
00345 /* Evaluate the real register number of a banked register.  */
00346 static struct m32c_reg *
00347 m32c_banked_register (struct m32c_reg *reg, struct regcache *cache)
00348 {
00349   return ((m32c_read_flg (cache) & reg->n) ? reg->ry : reg->rx);
00350 }
00351 
00352 
00353 /* Move the value of a banked register from CACHE to BUF.
00354    If the value of the 'flg' register in CACHE has any of the bits
00355    masked in REG->n set, then read REG->ry.  Otherwise, read
00356    REG->rx.  */
00357 static enum register_status
00358 m32c_banked_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
00359 {
00360   struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
00361   return regcache_raw_read (cache, bank_reg->num, buf);
00362 }
00363 
00364 
00365 /* Move the value of a banked register from BUF to CACHE.
00366    If the value of the 'flg' register in CACHE has any of the bits
00367    masked in REG->n set, then write REG->ry.  Otherwise, write
00368    REG->rx.  */
00369 static enum register_status
00370 m32c_banked_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
00371 {
00372   struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
00373   regcache_raw_write (cache, bank_reg->num, (const void *) buf);
00374 
00375   return REG_VALID;
00376 }
00377 
00378 
00379 /* Move the value of SB from CACHE to BUF.  On bfd_mach_m32c, SB is a
00380    banked register; on bfd_mach_m16c, it's not.  */
00381 static enum register_status
00382 m32c_sb_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
00383 {
00384   if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
00385     return m32c_raw_read (reg->rx, cache, buf);
00386   else
00387     return m32c_banked_read (reg, cache, buf);
00388 }
00389 
00390 
00391 /* Move the value of SB from BUF to CACHE.  On bfd_mach_m32c, SB is a
00392    banked register; on bfd_mach_m16c, it's not.  */
00393 static enum register_status
00394 m32c_sb_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
00395 {
00396   if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
00397     m32c_raw_write (reg->rx, cache, buf);
00398   else
00399     m32c_banked_write (reg, cache, buf);
00400 
00401   return REG_VALID;
00402 }
00403 
00404 
00405 /* Assuming REG uses m32c_part_read and m32c_part_write, set *OFFSET_P
00406    and *LEN_P to the offset and length, in bytes, of the part REG
00407    occupies in its underlying register.  The offset is from the
00408    lower-addressed end, regardless of the architecture's endianness.
00409    (The M32C family is always little-endian, but let's keep those
00410    assumptions out of here.)  */
00411 static void
00412 m32c_find_part (struct m32c_reg *reg, int *offset_p, int *len_p)
00413 {
00414   /* The length of the containing register, of which REG is one part.  */
00415   int containing_len = TYPE_LENGTH (reg->rx->type);
00416 
00417   /* The length of one "element" in our imaginary array.  */
00418   int elt_len = TYPE_LENGTH (reg->type);
00419 
00420   /* The offset of REG's "element" from the least significant end of
00421      the containing register.  */
00422   int elt_offset = reg->n * elt_len;
00423 
00424   /* If we extend off the end, trim the length of the element.  */
00425   if (elt_offset + elt_len > containing_len)
00426     {
00427       elt_len = containing_len - elt_offset;
00428       /* We shouldn't be declaring partial registers that go off the
00429          end of their containing registers.  */
00430       gdb_assert (elt_len > 0);
00431     }
00432 
00433   /* Flip the offset around if we're big-endian.  */
00434   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
00435     elt_offset = TYPE_LENGTH (reg->rx->type) - elt_offset - elt_len;
00436 
00437   *offset_p = elt_offset;
00438   *len_p = elt_len;
00439 }
00440 
00441 
00442 /* Move the value of a partial register (r0h, intbl, etc.) from CACHE
00443    to BUF.  Treating the value of the register REG->rx as an array of
00444    REG->type values, where higher indices refer to more significant
00445    bits, read the value of the REG->n'th element.  */
00446 static enum register_status
00447 m32c_part_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
00448 {
00449   int offset, len;
00450 
00451   memset (buf, 0, TYPE_LENGTH (reg->type));
00452   m32c_find_part (reg, &offset, &len);
00453   return regcache_cooked_read_part (cache, reg->rx->num, offset, len, buf);
00454 }
00455 
00456 
00457 /* Move the value of a banked register from BUF to CACHE.
00458    Treating the value of the register REG->rx as an array of REG->type
00459    values, where higher indices refer to more significant bits, write
00460    the value of the REG->n'th element.  */
00461 static enum register_status
00462 m32c_part_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
00463 {
00464   int offset, len;
00465 
00466   m32c_find_part (reg, &offset, &len);
00467   regcache_cooked_write_part (cache, reg->rx->num, offset, len, buf);
00468 
00469   return REG_VALID;
00470 }
00471 
00472 
00473 /* Move the value of REG from CACHE to BUF.  REG's value is the
00474    concatenation of the values of the registers REG->rx and REG->ry,
00475    with REG->rx contributing the more significant bits.  */
00476 static enum register_status
00477 m32c_cat_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
00478 {
00479   int high_bytes = TYPE_LENGTH (reg->rx->type);
00480   int low_bytes  = TYPE_LENGTH (reg->ry->type);
00481   /* For address arithmetic.  */
00482   unsigned char *cbuf = buf;
00483   enum register_status status;
00484 
00485   gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
00486 
00487   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
00488     {
00489       status = regcache_cooked_read (cache, reg->rx->num, cbuf);
00490       if (status == REG_VALID)
00491         status = regcache_cooked_read (cache, reg->ry->num, cbuf + high_bytes);
00492     }
00493   else
00494     {
00495       status = regcache_cooked_read (cache, reg->rx->num, cbuf + low_bytes);
00496       if (status == REG_VALID)
00497         status = regcache_cooked_read (cache, reg->ry->num, cbuf);
00498     }
00499 
00500   return status;
00501 }
00502 
00503 
00504 /* Move the value of REG from CACHE to BUF.  REG's value is the
00505    concatenation of the values of the registers REG->rx and REG->ry,
00506    with REG->rx contributing the more significant bits.  */
00507 static enum register_status
00508 m32c_cat_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
00509 {
00510   int high_bytes = TYPE_LENGTH (reg->rx->type);
00511   int low_bytes  = TYPE_LENGTH (reg->ry->type);
00512   /* For address arithmetic.  */
00513   unsigned char *cbuf = buf;
00514 
00515   gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
00516 
00517   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
00518     {
00519       regcache_cooked_write (cache, reg->rx->num, cbuf);
00520       regcache_cooked_write (cache, reg->ry->num, cbuf + high_bytes);
00521     }
00522   else
00523     {
00524       regcache_cooked_write (cache, reg->rx->num, cbuf + low_bytes);
00525       regcache_cooked_write (cache, reg->ry->num, cbuf);
00526     }
00527 
00528   return REG_VALID;
00529 }
00530 
00531 
00532 /* Copy the value of the raw register REG from CACHE to BUF.  REG is
00533    the concatenation (from most significant to least) of r3, r2, r1,
00534    and r0.  */
00535 static enum register_status
00536 m32c_r3r2r1r0_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
00537 {
00538   struct gdbarch_tdep *tdep = gdbarch_tdep (reg->arch);
00539   int len = TYPE_LENGTH (tdep->r0->type);
00540   enum register_status status;
00541 
00542   /* For address arithmetic.  */
00543   unsigned char *cbuf = buf;
00544 
00545   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
00546     {
00547       status = regcache_cooked_read (cache, tdep->r0->num, cbuf + len * 3);
00548       if (status == REG_VALID)
00549         status = regcache_cooked_read (cache, tdep->r1->num, cbuf + len * 2);
00550       if (status == REG_VALID)
00551         status = regcache_cooked_read (cache, tdep->r2->num, cbuf + len * 1);
00552       if (status == REG_VALID)
00553         status = regcache_cooked_read (cache, tdep->r3->num, cbuf);
00554     }
00555   else
00556     {
00557       status = regcache_cooked_read (cache, tdep->r0->num, cbuf);
00558       if (status == REG_VALID)
00559         status = regcache_cooked_read (cache, tdep->r1->num, cbuf + len * 1);
00560       if (status == REG_VALID)
00561         status = regcache_cooked_read (cache, tdep->r2->num, cbuf + len * 2);
00562       if (status == REG_VALID)
00563         status = regcache_cooked_read (cache, tdep->r3->num, cbuf + len * 3);
00564     }
00565 
00566   return status;
00567 }
00568 
00569 
00570 /* Copy the value of the raw register REG from BUF to CACHE.  REG is
00571    the concatenation (from most significant to least) of r3, r2, r1,
00572    and r0.  */
00573 static enum register_status
00574 m32c_r3r2r1r0_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
00575 {
00576   struct gdbarch_tdep *tdep = gdbarch_tdep (reg->arch);
00577   int len = TYPE_LENGTH (tdep->r0->type);
00578 
00579   /* For address arithmetic.  */
00580   unsigned char *cbuf = buf;
00581 
00582   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
00583     {
00584       regcache_cooked_write (cache, tdep->r0->num, cbuf + len * 3);
00585       regcache_cooked_write (cache, tdep->r1->num, cbuf + len * 2);
00586       regcache_cooked_write (cache, tdep->r2->num, cbuf + len * 1);
00587       regcache_cooked_write (cache, tdep->r3->num, cbuf);
00588     }
00589   else
00590     {
00591       regcache_cooked_write (cache, tdep->r0->num, cbuf);
00592       regcache_cooked_write (cache, tdep->r1->num, cbuf + len * 1);
00593       regcache_cooked_write (cache, tdep->r2->num, cbuf + len * 2);
00594       regcache_cooked_write (cache, tdep->r3->num, cbuf + len * 3);
00595     }
00596 
00597   return REG_VALID;
00598 }
00599 
00600 
00601 static enum register_status
00602 m32c_pseudo_register_read (struct gdbarch *arch,
00603                            struct regcache *cache,
00604                            int cookednum,
00605                            gdb_byte *buf)
00606 {
00607   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
00608   struct m32c_reg *reg;
00609 
00610   gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
00611   gdb_assert (arch == get_regcache_arch (cache));
00612   gdb_assert (arch == tdep->regs[cookednum].arch);
00613   reg = &tdep->regs[cookednum];
00614 
00615   return reg->read (reg, cache, buf);
00616 }
00617 
00618 
00619 static void
00620 m32c_pseudo_register_write (struct gdbarch *arch,
00621                             struct regcache *cache,
00622                             int cookednum,
00623                             const gdb_byte *buf)
00624 {
00625   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
00626   struct m32c_reg *reg;
00627 
00628   gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
00629   gdb_assert (arch == get_regcache_arch (cache));
00630   gdb_assert (arch == tdep->regs[cookednum].arch);
00631   reg = &tdep->regs[cookednum];
00632 
00633   reg->write (reg, cache, (void *) buf);
00634 }
00635 
00636 
00637 /* Add a register with the given fields to the end of ARCH's table.
00638    Return a pointer to the newly added register.  */
00639 static struct m32c_reg *
00640 add_reg (struct gdbarch *arch,
00641          const char *name,
00642          struct type *type,
00643          int sim_num,
00644          m32c_move_reg_t *read,
00645          m32c_move_reg_t *write,
00646          struct m32c_reg *rx,
00647          struct m32c_reg *ry,
00648          int n)
00649 {
00650   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
00651   struct m32c_reg *r = &tdep->regs[tdep->num_regs];
00652 
00653   gdb_assert (tdep->num_regs < M32C_MAX_NUM_REGS);
00654 
00655   r->name           = name;
00656   r->type           = type;
00657   r->arch           = arch;
00658   r->num            = tdep->num_regs;
00659   r->sim_num        = sim_num;
00660   r->dwarf_num      = -1;
00661   r->general_p      = 0;
00662   r->dma_p          = 0;
00663   r->system_p       = 0;
00664   r->save_restore_p = 0;
00665   r->read           = read;
00666   r->write          = write;
00667   r->rx             = rx;
00668   r->ry             = ry;
00669   r->n              = n;
00670 
00671   tdep->num_regs++;
00672 
00673   return r;
00674 }
00675 
00676 
00677 /* Record NUM as REG's DWARF register number.  */
00678 static void
00679 set_dwarf_regnum (struct m32c_reg *reg, int num)
00680 {
00681   gdb_assert (num < M32C_MAX_NUM_REGS);
00682 
00683   /* Update the reg->DWARF mapping.  Only count the first number
00684      assigned to this register.  */
00685   if (reg->dwarf_num == -1)
00686     reg->dwarf_num = num;
00687 
00688   /* Update the DWARF->reg mapping.  */
00689   gdbarch_tdep (reg->arch)->dwarf_regs[num] = reg;
00690 }
00691 
00692 
00693 /* Mark REG as a general-purpose register, and return it.  */
00694 static struct m32c_reg *
00695 mark_general (struct m32c_reg *reg)
00696 {
00697   reg->general_p = 1;
00698   return reg;
00699 }
00700 
00701 
00702 /* Mark REG as a DMA register, and return it.  */
00703 static struct m32c_reg *
00704 mark_dma (struct m32c_reg *reg)
00705 {
00706   reg->dma_p = 1;
00707   return reg;
00708 }
00709 
00710 
00711 /* Mark REG as a SYSTEM register, and return it.  */
00712 static struct m32c_reg *
00713 mark_system (struct m32c_reg *reg)
00714 {
00715   reg->system_p = 1;
00716   return reg;
00717 }
00718 
00719 
00720 /* Mark REG as a save-restore register, and return it.  */
00721 static struct m32c_reg *
00722 mark_save_restore (struct m32c_reg *reg)
00723 {
00724   reg->save_restore_p = 1;
00725   return reg;
00726 }
00727 
00728 
00729 #define FLAGBIT_B       0x0010
00730 #define FLAGBIT_U       0x0080
00731 
00732 /* Handy macros for declaring registers.  These all evaluate to
00733    pointers to the register declared.  Macros that define two
00734    registers evaluate to a pointer to the first.  */
00735 
00736 /* A raw register named NAME, with type TYPE and sim number SIM_NUM.  */
00737 #define R(name, type, sim_num)                                  \
00738   (add_reg (arch, (name), (type), (sim_num),                    \
00739             m32c_raw_read, m32c_raw_write, NULL, NULL, 0))
00740 
00741 /* The simulator register number for a raw register named NAME.  */
00742 #define SIM(name) (m32c_sim_reg_ ## name)
00743 
00744 /* A raw unsigned 16-bit data register named NAME.
00745    NAME should be an identifier, not a string.  */
00746 #define R16U(name)                                              \
00747   (R(#name, tdep->uint16, SIM (name)))
00748 
00749 /* A raw data address register named NAME.
00750    NAME should be an identifier, not a string.  */
00751 #define RA(name)                                                \
00752   (R(#name, tdep->data_addr_reg_type, SIM (name)))
00753 
00754 /* A raw code address register named NAME.  NAME should
00755    be an identifier, not a string.  */
00756 #define RC(name)                                                \
00757   (R(#name, tdep->code_addr_reg_type, SIM (name)))
00758 
00759 /* A pair of raw registers named NAME0 and NAME1, with type TYPE.
00760    NAME should be an identifier, not a string.  */
00761 #define RP(name, type)                          \
00762   (R(#name "0", (type), SIM (name ## 0)),       \
00763    R(#name "1", (type), SIM (name ## 1)) - 1)
00764 
00765 /* A raw banked general-purpose data register named NAME.
00766    NAME should be an identifier, not a string.  */
00767 #define RBD(name)                                               \
00768   (R(NULL, tdep->int16, SIM (name ## _bank0)),          \
00769    R(NULL, tdep->int16, SIM (name ## _bank1)) - 1)
00770 
00771 /* A raw banked data address register named NAME.
00772    NAME should be an identifier, not a string.  */
00773 #define RBA(name)                                               \
00774   (R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank0)),     \
00775    R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank1)) - 1)
00776 
00777 /* A cooked register named NAME referring to a raw banked register
00778    from the bank selected by the current value of FLG.  RAW_PAIR
00779    should be a pointer to the first register in the banked pair.
00780    NAME must be an identifier, not a string.  */
00781 #define CB(name, raw_pair)                              \
00782   (add_reg (arch, #name, (raw_pair)->type, 0,           \
00783             m32c_banked_read, m32c_banked_write,        \
00784             (raw_pair), (raw_pair + 1), FLAGBIT_B))
00785 
00786 /* A pair of registers named NAMEH and NAMEL, of type TYPE, that
00787    access the top and bottom halves of the register pointed to by
00788    NAME.  NAME should be an identifier.  */
00789 #define CHL(name, type)                                                 \
00790   (add_reg (arch, #name "h", (type), 0,                                 \
00791             m32c_part_read, m32c_part_write, name, NULL, 1),            \
00792    add_reg (arch, #name "l", (type), 0,                                 \
00793             m32c_part_read, m32c_part_write, name, NULL, 0) - 1)
00794 
00795 /* A register constructed by concatenating the two registers HIGH and
00796    LOW, whose name is HIGHLOW and whose type is TYPE.  */
00797 #define CCAT(high, low, type)                                   \
00798   (add_reg (arch, #high #low, (type), 0,                        \
00799             m32c_cat_read, m32c_cat_write, (high), (low), 0))
00800 
00801 /* Abbreviations for marking register group membership.  */
00802 #define G(reg)   (mark_general (reg))
00803 #define S(reg)   (mark_system  (reg))
00804 #define DMA(reg) (mark_dma     (reg))
00805 
00806 
00807 /* Construct the register set for ARCH.  */
00808 static void
00809 make_regs (struct gdbarch *arch)
00810 {
00811   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
00812   int mach = gdbarch_bfd_arch_info (arch)->mach;
00813   int num_raw_regs;
00814   int num_cooked_regs;
00815 
00816   struct m32c_reg *r0;
00817   struct m32c_reg *r1;
00818   struct m32c_reg *r2;
00819   struct m32c_reg *r3;
00820   struct m32c_reg *a0;
00821   struct m32c_reg *a1;
00822   struct m32c_reg *fb;
00823   struct m32c_reg *sb;
00824   struct m32c_reg *sp;
00825   struct m32c_reg *r0hl;
00826   struct m32c_reg *r1hl;
00827   struct m32c_reg *r2hl;
00828   struct m32c_reg *r3hl;
00829   struct m32c_reg *intbhl;
00830   struct m32c_reg *r2r0;
00831   struct m32c_reg *r3r1;
00832   struct m32c_reg *r3r1r2r0;
00833   struct m32c_reg *r3r2r1r0;
00834   struct m32c_reg *a1a0;
00835 
00836   struct m32c_reg *raw_r0_pair = RBD (r0);
00837   struct m32c_reg *raw_r1_pair = RBD (r1);
00838   struct m32c_reg *raw_r2_pair = RBD (r2);
00839   struct m32c_reg *raw_r3_pair = RBD (r3);
00840   struct m32c_reg *raw_a0_pair = RBA (a0);
00841   struct m32c_reg *raw_a1_pair = RBA (a1);
00842   struct m32c_reg *raw_fb_pair = RBA (fb);
00843 
00844   /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
00845      We always declare both raw registers, and deal with the distinction
00846      in the pseudoregister.  */
00847   struct m32c_reg *raw_sb_pair = RBA (sb);
00848 
00849   struct m32c_reg *usp         = S (RA (usp));
00850   struct m32c_reg *isp         = S (RA (isp));
00851   struct m32c_reg *intb        = S (RC (intb));
00852   struct m32c_reg *pc          = G (RC (pc));
00853   struct m32c_reg *flg         = G (R16U (flg));
00854 
00855   if (mach == bfd_mach_m32c)
00856     {
00857       struct m32c_reg *svf     = S (R16U (svf));
00858       struct m32c_reg *svp     = S (RC (svp));
00859       struct m32c_reg *vct     = S (RC (vct));
00860 
00861       struct m32c_reg *dmd01   = DMA (RP (dmd, tdep->uint8));
00862       struct m32c_reg *dct01   = DMA (RP (dct, tdep->uint16));
00863       struct m32c_reg *drc01   = DMA (RP (drc, tdep->uint16));
00864       struct m32c_reg *dma01   = DMA (RP (dma, tdep->data_addr_reg_type));
00865       struct m32c_reg *dsa01   = DMA (RP (dsa, tdep->data_addr_reg_type));
00866       struct m32c_reg *dra01   = DMA (RP (dra, tdep->data_addr_reg_type));
00867     }
00868 
00869   num_raw_regs = tdep->num_regs;
00870 
00871   r0          = G (CB (r0, raw_r0_pair));
00872   r1          = G (CB (r1, raw_r1_pair));
00873   r2          = G (CB (r2, raw_r2_pair));
00874   r3          = G (CB (r3, raw_r3_pair));
00875   a0          = G (CB (a0, raw_a0_pair));
00876   a1          = G (CB (a1, raw_a1_pair));
00877   fb          = G (CB (fb, raw_fb_pair));
00878 
00879   /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
00880      Specify custom read/write functions that do the right thing.  */
00881   sb          = G (add_reg (arch, "sb", raw_sb_pair->type, 0,
00882                             m32c_sb_read, m32c_sb_write,
00883                             raw_sb_pair, raw_sb_pair + 1, 0));
00884 
00885   /* The current sp is either usp or isp, depending on the value of
00886      the FLG register's U bit.  */
00887   sp          = G (add_reg (arch, "sp", usp->type, 0,
00888                             m32c_banked_read, m32c_banked_write,
00889                             isp, usp, FLAGBIT_U));
00890 
00891   r0hl        = CHL (r0, tdep->int8);
00892   r1hl        = CHL (r1, tdep->int8);
00893   r2hl        = CHL (r2, tdep->int8);
00894   r3hl        = CHL (r3, tdep->int8);
00895   intbhl      = CHL (intb, tdep->int16);
00896 
00897   r2r0        = CCAT (r2,   r0,   tdep->int32);
00898   r3r1        = CCAT (r3,   r1,   tdep->int32);
00899   r3r1r2r0    = CCAT (r3r1, r2r0, tdep->int64);
00900 
00901   r3r2r1r0
00902     = add_reg (arch, "r3r2r1r0", tdep->int64, 0,
00903                m32c_r3r2r1r0_read, m32c_r3r2r1r0_write, NULL, NULL, 0);
00904 
00905   if (mach == bfd_mach_m16c)
00906     a1a0 = CCAT (a1, a0, tdep->int32);
00907   else
00908     a1a0 = NULL;
00909 
00910   num_cooked_regs = tdep->num_regs - num_raw_regs;
00911 
00912   tdep->pc       = pc;
00913   tdep->flg      = flg;
00914   tdep->r0       = r0;
00915   tdep->r1       = r1;
00916   tdep->r2       = r2;
00917   tdep->r3       = r3;
00918   tdep->r2r0     = r2r0;
00919   tdep->r3r2r1r0 = r3r2r1r0;
00920   tdep->r3r1r2r0 = r3r1r2r0;
00921   tdep->a0       = a0;
00922   tdep->a1       = a1;
00923   tdep->sb       = sb;
00924   tdep->fb       = fb;
00925   tdep->sp       = sp;
00926 
00927   /* Set up the DWARF register table.  */
00928   memset (tdep->dwarf_regs, 0, sizeof (tdep->dwarf_regs));
00929   set_dwarf_regnum (r0hl + 1, 0x01);
00930   set_dwarf_regnum (r0hl + 0, 0x02);
00931   set_dwarf_regnum (r1hl + 1, 0x03);
00932   set_dwarf_regnum (r1hl + 0, 0x04);
00933   set_dwarf_regnum (r0,       0x05);
00934   set_dwarf_regnum (r1,       0x06);
00935   set_dwarf_regnum (r2,       0x07);
00936   set_dwarf_regnum (r3,       0x08);
00937   set_dwarf_regnum (a0,       0x09);
00938   set_dwarf_regnum (a1,       0x0a);
00939   set_dwarf_regnum (fb,       0x0b);
00940   set_dwarf_regnum (sp,       0x0c);
00941   set_dwarf_regnum (pc,       0x0d); /* GCC's invention */
00942   set_dwarf_regnum (sb,       0x13);
00943   set_dwarf_regnum (r2r0,     0x15);
00944   set_dwarf_regnum (r3r1,     0x16);
00945   if (a1a0)
00946     set_dwarf_regnum (a1a0,   0x17);
00947 
00948   /* Enumerate the save/restore register group.
00949 
00950      The regcache_save and regcache_restore functions apply their read
00951      function to each register in this group.
00952 
00953      Since frame_pop supplies frame_unwind_register as its read
00954      function, the registers meaningful to the Dwarf unwinder need to
00955      be in this group.
00956 
00957      On the other hand, when we make inferior calls, save_inferior_status
00958      and restore_inferior_status use them to preserve the current register
00959      values across the inferior call.  For this, you'd kind of like to
00960      preserve all the raw registers, to protect the interrupted code from
00961      any sort of bank switching the callee might have done.  But we handle
00962      those cases so badly anyway --- for example, it matters whether we
00963      restore FLG before or after we restore the general-purpose registers,
00964      but there's no way to express that --- that it isn't worth worrying
00965      about.
00966 
00967      We omit control registers like inthl: if you call a function that
00968      changes those, it's probably because you wanted that change to be
00969      visible to the interrupted code.  */
00970   mark_save_restore (r0);
00971   mark_save_restore (r1);
00972   mark_save_restore (r2);
00973   mark_save_restore (r3);
00974   mark_save_restore (a0);
00975   mark_save_restore (a1);
00976   mark_save_restore (sb);
00977   mark_save_restore (fb);
00978   mark_save_restore (sp);
00979   mark_save_restore (pc);
00980   mark_save_restore (flg);
00981 
00982   set_gdbarch_num_regs (arch, num_raw_regs);
00983   set_gdbarch_num_pseudo_regs (arch, num_cooked_regs);
00984   set_gdbarch_pc_regnum (arch, pc->num);
00985   set_gdbarch_sp_regnum (arch, sp->num);
00986   set_gdbarch_register_name (arch, m32c_register_name);
00987   set_gdbarch_register_type (arch, m32c_register_type);
00988   set_gdbarch_pseudo_register_read (arch, m32c_pseudo_register_read);
00989   set_gdbarch_pseudo_register_write (arch, m32c_pseudo_register_write);
00990   set_gdbarch_register_sim_regno (arch, m32c_register_sim_regno);
00991   set_gdbarch_stab_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
00992   set_gdbarch_dwarf2_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
00993   set_gdbarch_register_reggroup_p (arch, m32c_register_reggroup_p);
00994 
00995   reggroup_add (arch, general_reggroup);
00996   reggroup_add (arch, all_reggroup);
00997   reggroup_add (arch, save_reggroup);
00998   reggroup_add (arch, restore_reggroup);
00999   reggroup_add (arch, system_reggroup);
01000   reggroup_add (arch, m32c_dma_reggroup);
01001 }
01002 
01003 
01004 
01005 /* Breakpoints.  */
01006 
01007 static const unsigned char *
01008 m32c_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
01009 {
01010   static unsigned char break_insn[] = { 0x00 }; /* brk */
01011 
01012   *len = sizeof (break_insn);
01013   return break_insn;
01014 }
01015 
01016 
01017 
01018 /* Prologue analysis.  */
01019 
01020 struct m32c_prologue
01021 {
01022   /* For consistency with the DWARF 2 .debug_frame info generated by
01023      GCC, a frame's CFA is the address immediately after the saved
01024      return address.  */
01025 
01026   /* The architecture for which we generated this prologue info.  */
01027   struct gdbarch *arch;
01028 
01029   enum {
01030     /* This function uses a frame pointer.  */
01031     prologue_with_frame_ptr,
01032 
01033     /* This function has no frame pointer.  */
01034     prologue_sans_frame_ptr,
01035 
01036     /* This function sets up the stack, so its frame is the first
01037        frame on the stack.  */
01038     prologue_first_frame
01039 
01040   } kind;
01041 
01042   /* If KIND is prologue_with_frame_ptr, this is the offset from the
01043      CFA to where the frame pointer points.  This is always zero or
01044      negative.  */
01045   LONGEST frame_ptr_offset;
01046 
01047   /* If KIND is prologue_sans_frame_ptr, the offset from the CFA to
01048      the stack pointer --- always zero or negative.
01049 
01050      Calling this a "size" is a bit misleading, but given that the
01051      stack grows downwards, using offsets for everything keeps one
01052      from going completely sign-crazy: you never change anything's
01053      sign for an ADD instruction; always change the second operand's
01054      sign for a SUB instruction; and everything takes care of
01055      itself.
01056 
01057      Functions that use alloca don't have a constant frame size.  But
01058      they always have frame pointers, so we must use that to find the
01059      CFA (and perhaps to unwind the stack pointer).  */
01060   LONGEST frame_size;
01061 
01062   /* The address of the first instruction at which the frame has been
01063      set up and the arguments are where the debug info says they are
01064      --- as best as we can tell.  */
01065   CORE_ADDR prologue_end;
01066 
01067   /* reg_offset[R] is the offset from the CFA at which register R is
01068      saved, or 1 if register R has not been saved.  (Real values are
01069      always zero or negative.)  */
01070   LONGEST reg_offset[M32C_MAX_NUM_REGS];
01071 };
01072 
01073 
01074 /* The longest I've seen, anyway.  */
01075 #define M32C_MAX_INSN_LEN (9)
01076 
01077 /* Processor state, for the prologue analyzer.  */
01078 struct m32c_pv_state
01079 {
01080   struct gdbarch *arch;
01081   pv_t r0, r1, r2, r3;
01082   pv_t a0, a1;
01083   pv_t sb, fb, sp;
01084   pv_t pc;
01085   struct pv_area *stack;
01086 
01087   /* Bytes from the current PC, the address they were read from,
01088      and the address of the next unconsumed byte.  */
01089   gdb_byte insn[M32C_MAX_INSN_LEN];
01090   CORE_ADDR scan_pc, next_addr;
01091 };
01092 
01093 
01094 /* Push VALUE on STATE's stack, occupying SIZE bytes.  Return zero if
01095    all went well, or non-zero if simulating the action would trash our
01096    state.  */
01097 static int
01098 m32c_pv_push (struct m32c_pv_state *state, pv_t value, int size)
01099 {
01100   if (pv_area_store_would_trash (state->stack, state->sp))
01101     return 1;
01102 
01103   state->sp = pv_add_constant (state->sp, -size);
01104   pv_area_store (state->stack, state->sp, size, value);
01105 
01106   return 0;
01107 }
01108 
01109 
01110 /* A source or destination location for an m16c or m32c
01111    instruction.  */
01112 struct srcdest
01113 {
01114   /* If srcdest_reg, the location is a register pointed to by REG.
01115      If srcdest_partial_reg, the location is part of a register pointed
01116      to by REG.  We don't try to handle this too well.
01117      If srcdest_mem, the location is memory whose address is ADDR.  */
01118   enum { srcdest_reg, srcdest_partial_reg, srcdest_mem } kind;
01119   pv_t *reg, addr;
01120 };
01121 
01122 
01123 /* Return the SIZE-byte value at LOC in STATE.  */
01124 static pv_t
01125 m32c_srcdest_fetch (struct m32c_pv_state *state, struct srcdest loc, int size)
01126 {
01127   if (loc.kind == srcdest_mem)
01128     return pv_area_fetch (state->stack, loc.addr, size);
01129   else if (loc.kind == srcdest_partial_reg)
01130     return pv_unknown ();
01131   else
01132     return *loc.reg;
01133 }
01134 
01135 
01136 /* Write VALUE, a SIZE-byte value, to LOC in STATE.  Return zero if
01137    all went well, or non-zero if simulating the store would trash our
01138    state.  */
01139 static int
01140 m32c_srcdest_store (struct m32c_pv_state *state, struct srcdest loc,
01141                     pv_t value, int size)
01142 {
01143   if (loc.kind == srcdest_mem)
01144     {
01145       if (pv_area_store_would_trash (state->stack, loc.addr))
01146         return 1;
01147       pv_area_store (state->stack, loc.addr, size, value);
01148     }
01149   else if (loc.kind == srcdest_partial_reg)
01150     *loc.reg = pv_unknown ();
01151   else
01152     *loc.reg = value;
01153 
01154   return 0;
01155 }
01156 
01157 
01158 static int
01159 m32c_sign_ext (int v, int bits)
01160 {
01161   int mask = 1 << (bits - 1);
01162   return (v ^ mask) - mask;
01163 }
01164 
01165 static unsigned int
01166 m32c_next_byte (struct m32c_pv_state *st)
01167 {
01168   gdb_assert (st->next_addr - st->scan_pc < sizeof (st->insn));
01169   return st->insn[st->next_addr++ - st->scan_pc];
01170 }
01171 
01172 static int
01173 m32c_udisp8 (struct m32c_pv_state *st)
01174 {
01175   return m32c_next_byte (st);
01176 }
01177 
01178 
01179 static int
01180 m32c_sdisp8 (struct m32c_pv_state *st)
01181 {
01182   return m32c_sign_ext (m32c_next_byte (st), 8);
01183 }
01184 
01185 
01186 static int
01187 m32c_udisp16 (struct m32c_pv_state *st)
01188 {
01189   int low  = m32c_next_byte (st);
01190   int high = m32c_next_byte (st);
01191 
01192   return low + (high << 8);
01193 }
01194 
01195 
01196 static int
01197 m32c_sdisp16 (struct m32c_pv_state *st)
01198 {
01199   int low  = m32c_next_byte (st);
01200   int high = m32c_next_byte (st);
01201 
01202   return m32c_sign_ext (low + (high << 8), 16);
01203 }
01204 
01205 
01206 static int
01207 m32c_udisp24 (struct m32c_pv_state *st)
01208 {
01209   int low  = m32c_next_byte (st);
01210   int mid  = m32c_next_byte (st);
01211   int high = m32c_next_byte (st);
01212 
01213   return low + (mid << 8) + (high << 16);
01214 }
01215 
01216 
01217 /* Extract the 'source' field from an m32c MOV.size:G-format instruction.  */
01218 static int
01219 m32c_get_src23 (unsigned char *i)
01220 {
01221   return (((i[0] & 0x70) >> 2)
01222           | ((i[1] & 0x30) >> 4));
01223 }
01224 
01225 
01226 /* Extract the 'dest' field from an m32c MOV.size:G-format instruction.  */
01227 static int
01228 m32c_get_dest23 (unsigned char *i)
01229 {
01230   return (((i[0] & 0x0e) << 1)
01231           | ((i[1] & 0xc0) >> 6));
01232 }
01233 
01234 
01235 static struct srcdest
01236 m32c_decode_srcdest4 (struct m32c_pv_state *st,
01237                       int code, int size)
01238 {
01239   struct srcdest sd;
01240 
01241   if (code < 6)
01242     sd.kind = (size == 2 ? srcdest_reg : srcdest_partial_reg);
01243   else
01244     sd.kind = srcdest_mem;
01245 
01246   sd.addr = pv_unknown ();
01247   sd.reg = 0;
01248 
01249   switch (code)
01250     {
01251     case 0x0: sd.reg = (size == 1 ? &st->r0 : &st->r0); break;
01252     case 0x1: sd.reg = (size == 1 ? &st->r0 : &st->r1); break;
01253     case 0x2: sd.reg = (size == 1 ? &st->r1 : &st->r2); break;
01254     case 0x3: sd.reg = (size == 1 ? &st->r1 : &st->r3); break;
01255 
01256     case 0x4: sd.reg = &st->a0; break;
01257     case 0x5: sd.reg = &st->a1; break;
01258 
01259     case 0x6: sd.addr = st->a0; break;
01260     case 0x7: sd.addr = st->a1; break;
01261 
01262     case 0x8: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
01263     case 0x9: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
01264     case 0xa: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
01265     case 0xb: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
01266 
01267     case 0xc: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
01268     case 0xd: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
01269     case 0xe: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
01270     case 0xf: sd.addr = pv_constant (m32c_udisp16 (st)); break;
01271 
01272     default:
01273       gdb_assert_not_reached ("unexpected srcdest4");
01274     }
01275 
01276   return sd;
01277 }
01278 
01279 
01280 static struct srcdest
01281 m32c_decode_sd23 (struct m32c_pv_state *st, int code, int size, int ind)
01282 {
01283   struct srcdest sd;
01284 
01285   sd.addr = pv_unknown ();
01286   sd.reg = 0;
01287 
01288   switch (code)
01289     {
01290     case 0x12:
01291     case 0x13:
01292     case 0x10:
01293     case 0x11:
01294       sd.kind = (size == 1) ? srcdest_partial_reg : srcdest_reg;
01295       break;
01296 
01297     case 0x02:
01298     case 0x03:
01299       sd.kind = (size == 4) ? srcdest_reg : srcdest_partial_reg;
01300       break;
01301 
01302     default:
01303       sd.kind = srcdest_mem;
01304       break;
01305 
01306     }
01307 
01308   switch (code)
01309     {
01310     case 0x12: sd.reg = &st->r0; break;
01311     case 0x13: sd.reg = &st->r1; break;
01312     case 0x10: sd.reg = ((size == 1) ? &st->r0 : &st->r2); break;
01313     case 0x11: sd.reg = ((size == 1) ? &st->r1 : &st->r3); break;
01314     case 0x02: sd.reg = &st->a0; break;
01315     case 0x03: sd.reg = &st->a1; break;
01316 
01317     case 0x00: sd.addr = st->a0; break;
01318     case 0x01: sd.addr = st->a1; break;
01319     case 0x04: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
01320     case 0x05: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
01321     case 0x06: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
01322     case 0x07: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
01323     case 0x08: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
01324     case 0x09: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
01325     case 0x0a: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
01326     case 0x0b: sd.addr = pv_add_constant (st->fb, m32c_sdisp16 (st)); break;
01327     case 0x0c: sd.addr = pv_add_constant (st->a0, m32c_udisp24 (st)); break;
01328     case 0x0d: sd.addr = pv_add_constant (st->a1, m32c_udisp24 (st)); break;
01329     case 0x0f: sd.addr = pv_constant (m32c_udisp16 (st)); break;
01330     case 0x0e: sd.addr = pv_constant (m32c_udisp24 (st)); break;
01331     default:
01332       gdb_assert_not_reached ("unexpected sd23");
01333     }
01334 
01335   if (ind)
01336     {
01337       sd.addr = m32c_srcdest_fetch (st, sd, 4);
01338       sd.kind = srcdest_mem;
01339     }
01340 
01341   return sd;
01342 }
01343 
01344 
01345 /* The r16c and r32c machines have instructions with similar
01346    semantics, but completely different machine language encodings.  So
01347    we break out the semantics into their own functions, and leave
01348    machine-specific decoding in m32c_analyze_prologue.
01349 
01350    The following functions all expect their arguments already decoded,
01351    and they all return zero if analysis should continue past this
01352    instruction, or non-zero if analysis should stop.  */
01353 
01354 
01355 /* Simulate an 'enter SIZE' instruction in STATE.  */
01356 static int
01357 m32c_pv_enter (struct m32c_pv_state *state, int size)
01358 {
01359   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
01360 
01361   /* If simulating this store would require us to forget
01362      everything we know about the stack frame in the name of
01363      accuracy, it would be better to just quit now.  */
01364   if (pv_area_store_would_trash (state->stack, state->sp))
01365     return 1;
01366 
01367   if (m32c_pv_push (state, state->fb, tdep->push_addr_bytes))
01368     return 1;
01369   state->fb = state->sp;
01370   state->sp = pv_add_constant (state->sp, -size);
01371 
01372   return 0;
01373 }
01374 
01375 
01376 static int
01377 m32c_pv_pushm_one (struct m32c_pv_state *state, pv_t reg,
01378                    int bit, int src, int size)
01379 {
01380   if (bit & src)
01381     {
01382       if (m32c_pv_push (state, reg, size))
01383         return 1;
01384     }
01385 
01386   return 0;
01387 }
01388 
01389 
01390 /* Simulate a 'pushm SRC' instruction in STATE.  */
01391 static int
01392 m32c_pv_pushm (struct m32c_pv_state *state, int src)
01393 {
01394   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
01395 
01396   /* The bits in SRC indicating which registers to save are:
01397      r0 r1 r2 r3 a0 a1 sb fb */
01398   return
01399     (   m32c_pv_pushm_one (state, state->fb, 0x01, src, tdep->push_addr_bytes)
01400      || m32c_pv_pushm_one (state, state->sb, 0x02, src, tdep->push_addr_bytes)
01401      || m32c_pv_pushm_one (state, state->a1, 0x04, src, tdep->push_addr_bytes)
01402      || m32c_pv_pushm_one (state, state->a0, 0x08, src, tdep->push_addr_bytes)
01403      || m32c_pv_pushm_one (state, state->r3, 0x10, src, 2)
01404      || m32c_pv_pushm_one (state, state->r2, 0x20, src, 2)
01405      || m32c_pv_pushm_one (state, state->r1, 0x40, src, 2)
01406      || m32c_pv_pushm_one (state, state->r0, 0x80, src, 2));
01407 }
01408 
01409 /* Return non-zero if VALUE is the first incoming argument register.  */
01410 
01411 static int
01412 m32c_is_1st_arg_reg (struct m32c_pv_state *state, pv_t value)
01413 {
01414   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
01415   return (value.kind == pvk_register
01416           && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
01417               ? (value.reg == tdep->r1->num)
01418               : (value.reg == tdep->r0->num))
01419           && value.k == 0);
01420 }
01421 
01422 /* Return non-zero if VALUE is an incoming argument register.  */
01423 
01424 static int
01425 m32c_is_arg_reg (struct m32c_pv_state *state, pv_t value)
01426 {
01427   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
01428   return (value.kind == pvk_register
01429           && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
01430               ? (value.reg == tdep->r1->num || value.reg == tdep->r2->num)
01431               : (value.reg == tdep->r0->num))
01432           && value.k == 0);
01433 }
01434 
01435 /* Return non-zero if a store of VALUE to LOC is probably spilling an
01436    argument register to its stack slot in STATE.  Such instructions
01437    should be included in the prologue, if possible.
01438 
01439    The store is a spill if:
01440    - the value being stored is the original value of an argument register;
01441    - the value has not already been stored somewhere in STACK; and
01442    - LOC is a stack slot (e.g., a memory location whose address is
01443      relative to the original value of the SP).  */
01444 
01445 static int
01446 m32c_is_arg_spill (struct m32c_pv_state *st, 
01447                    struct srcdest loc, 
01448                    pv_t value)
01449 {
01450   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);
01451 
01452   return (m32c_is_arg_reg (st, value)
01453           && loc.kind == srcdest_mem
01454           && pv_is_register (loc.addr, tdep->sp->num)
01455           && ! pv_area_find_reg (st->stack, st->arch, value.reg, 0));
01456 }
01457 
01458 /* Return non-zero if a store of VALUE to LOC is probably 
01459    copying the struct return address into an address register
01460    for immediate use.  This is basically a "spill" into the
01461    address register, instead of onto the stack. 
01462 
01463    The prerequisites are:
01464    - value being stored is original value of the FIRST arg register;
01465    - value has not already been stored on stack; and
01466    - LOC is an address register (a0 or a1).  */
01467 
01468 static int
01469 m32c_is_struct_return (struct m32c_pv_state *st,
01470                        struct srcdest loc, 
01471                        pv_t value)
01472 {
01473   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);
01474 
01475   return (m32c_is_1st_arg_reg (st, value)
01476           && !pv_area_find_reg (st->stack, st->arch, value.reg, 0)
01477           && loc.kind == srcdest_reg
01478           && (pv_is_register (*loc.reg, tdep->a0->num)
01479               || pv_is_register (*loc.reg, tdep->a1->num)));
01480 }
01481 
01482 /* Return non-zero if a 'pushm' saving the registers indicated by SRC
01483    was a register save:
01484    - all the named registers should have their original values, and
01485    - the stack pointer should be at a constant offset from the
01486      original stack pointer.  */
01487 static int
01488 m32c_pushm_is_reg_save (struct m32c_pv_state *st, int src)
01489 {
01490   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);
01491   /* The bits in SRC indicating which registers to save are:
01492      r0 r1 r2 r3 a0 a1 sb fb */
01493   return
01494     (pv_is_register (st->sp, tdep->sp->num)
01495      && (! (src & 0x01) || pv_is_register_k (st->fb, tdep->fb->num, 0))
01496      && (! (src & 0x02) || pv_is_register_k (st->sb, tdep->sb->num, 0))
01497      && (! (src & 0x04) || pv_is_register_k (st->a1, tdep->a1->num, 0))
01498      && (! (src & 0x08) || pv_is_register_k (st->a0, tdep->a0->num, 0))
01499      && (! (src & 0x10) || pv_is_register_k (st->r3, tdep->r3->num, 0))
01500      && (! (src & 0x20) || pv_is_register_k (st->r2, tdep->r2->num, 0))
01501      && (! (src & 0x40) || pv_is_register_k (st->r1, tdep->r1->num, 0))
01502      && (! (src & 0x80) || pv_is_register_k (st->r0, tdep->r0->num, 0)));
01503 }
01504 
01505 
01506 /* Function for finding saved registers in a 'struct pv_area'; we pass
01507    this to pv_area_scan.
01508 
01509    If VALUE is a saved register, ADDR says it was saved at a constant
01510    offset from the frame base, and SIZE indicates that the whole
01511    register was saved, record its offset in RESULT_UNTYPED.  */
01512 static void
01513 check_for_saved (void *prologue_untyped, pv_t addr, CORE_ADDR size, pv_t value)
01514 {
01515   struct m32c_prologue *prologue = (struct m32c_prologue *) prologue_untyped;
01516   struct gdbarch *arch = prologue->arch;
01517   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
01518 
01519   /* Is this the unchanged value of some register being saved on the
01520      stack?  */
01521   if (value.kind == pvk_register
01522       && value.k == 0
01523       && pv_is_register (addr, tdep->sp->num))
01524     {
01525       /* Some registers require special handling: they're saved as a
01526          larger value than the register itself.  */
01527       CORE_ADDR saved_size = register_size (arch, value.reg);
01528 
01529       if (value.reg == tdep->pc->num)
01530         saved_size = tdep->ret_addr_bytes;
01531       else if (register_type (arch, value.reg)
01532                == tdep->data_addr_reg_type)
01533         saved_size = tdep->push_addr_bytes;
01534 
01535       if (size == saved_size)
01536         {
01537           /* Find which end of the saved value corresponds to our
01538              register.  */
01539           if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
01540             prologue->reg_offset[value.reg]
01541               = (addr.k + saved_size - register_size (arch, value.reg));
01542           else
01543             prologue->reg_offset[value.reg] = addr.k;
01544         }
01545     }
01546 }
01547 
01548 
01549 /* Analyze the function prologue for ARCH at START, going no further
01550    than LIMIT, and place a description of what we found in
01551    PROLOGUE.  */
01552 static void
01553 m32c_analyze_prologue (struct gdbarch *arch,
01554                        CORE_ADDR start, CORE_ADDR limit,
01555                        struct m32c_prologue *prologue)
01556 {
01557   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
01558   unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
01559   CORE_ADDR after_last_frame_related_insn;
01560   struct cleanup *back_to;
01561   struct m32c_pv_state st;
01562 
01563   st.arch = arch;
01564   st.r0 = pv_register (tdep->r0->num, 0);
01565   st.r1 = pv_register (tdep->r1->num, 0);
01566   st.r2 = pv_register (tdep->r2->num, 0);
01567   st.r3 = pv_register (tdep->r3->num, 0);
01568   st.a0 = pv_register (tdep->a0->num, 0);
01569   st.a1 = pv_register (tdep->a1->num, 0);
01570   st.sb = pv_register (tdep->sb->num, 0);
01571   st.fb = pv_register (tdep->fb->num, 0);
01572   st.sp = pv_register (tdep->sp->num, 0);
01573   st.pc = pv_register (tdep->pc->num, 0);
01574   st.stack = make_pv_area (tdep->sp->num, gdbarch_addr_bit (arch));
01575   back_to = make_cleanup_free_pv_area (st.stack);
01576 
01577   /* Record that the call instruction has saved the return address on
01578      the stack.  */
01579   m32c_pv_push (&st, st.pc, tdep->ret_addr_bytes);
01580 
01581   memset (prologue, 0, sizeof (*prologue));
01582   prologue->arch = arch;
01583   {
01584     int i;
01585     for (i = 0; i < M32C_MAX_NUM_REGS; i++)
01586       prologue->reg_offset[i] = 1;
01587   }
01588 
01589   st.scan_pc = after_last_frame_related_insn = start;
01590 
01591   while (st.scan_pc < limit)
01592     {
01593       pv_t pre_insn_fb = st.fb;
01594       pv_t pre_insn_sp = st.sp;
01595 
01596       /* In theory we could get in trouble by trying to read ahead
01597          here, when we only know we're expecting one byte.  In
01598          practice I doubt anyone will care, and it makes the rest of
01599          the code easier.  */
01600       if (target_read_memory (st.scan_pc, st.insn, sizeof (st.insn)))
01601         /* If we can't fetch the instruction from memory, stop here
01602            and hope for the best.  */
01603         break;
01604       st.next_addr = st.scan_pc;
01605 
01606       /* The assembly instructions are written as they appear in the
01607          section of the processor manuals that describe the
01608          instruction encodings.
01609 
01610          When a single assembly language instruction has several
01611          different machine-language encodings, the manual
01612          distinguishes them by a number in parens, before the
01613          mnemonic.  Those numbers are included, as well.
01614 
01615          The srcdest decoding instructions have the same names as the
01616          analogous functions in the simulator.  */
01617       if (mach == bfd_mach_m16c)
01618         {
01619           /* (1) ENTER #imm8 */
01620           if (st.insn[0] == 0x7c && st.insn[1] == 0xf2)
01621             {
01622               if (m32c_pv_enter (&st, st.insn[2]))
01623                 break;
01624               st.next_addr += 3;
01625             }
01626           /* (1) PUSHM src */
01627           else if (st.insn[0] == 0xec)
01628             {
01629               int src = st.insn[1];
01630               if (m32c_pv_pushm (&st, src))
01631                 break;
01632               st.next_addr += 2;
01633 
01634               if (m32c_pushm_is_reg_save (&st, src))
01635                 after_last_frame_related_insn = st.next_addr;
01636             }
01637 
01638           /* (6) MOV.size:G src, dest */
01639           else if ((st.insn[0] & 0xfe) == 0x72)
01640             {
01641               int size = (st.insn[0] & 0x01) ? 2 : 1;
01642               struct srcdest src;
01643               struct srcdest dest;
01644               pv_t src_value;
01645               st.next_addr += 2;
01646 
01647               src
01648                 = m32c_decode_srcdest4 (&st, (st.insn[1] >> 4) & 0xf, size);
01649               dest
01650                 = m32c_decode_srcdest4 (&st, st.insn[1] & 0xf, size);
01651               src_value = m32c_srcdest_fetch (&st, src, size);
01652 
01653               if (m32c_is_arg_spill (&st, dest, src_value))
01654                 after_last_frame_related_insn = st.next_addr;
01655               else if (m32c_is_struct_return (&st, dest, src_value))
01656                 after_last_frame_related_insn = st.next_addr;
01657 
01658               if (m32c_srcdest_store (&st, dest, src_value, size))
01659                 break;
01660             }
01661 
01662           /* (1) LDC #IMM16, sp */
01663           else if (st.insn[0] == 0xeb
01664                    && st.insn[1] == 0x50)
01665             {
01666               st.next_addr += 2;
01667               st.sp = pv_constant (m32c_udisp16 (&st));
01668             }
01669 
01670           else
01671             /* We've hit some instruction we don't know how to simulate.
01672                Strictly speaking, we should set every value we're
01673                tracking to "unknown".  But we'll be optimistic, assume
01674                that we have enough information already, and stop
01675                analysis here.  */
01676             break;
01677         }
01678       else
01679         {
01680           int src_indirect = 0;
01681           int dest_indirect = 0;
01682           int i = 0;
01683 
01684           gdb_assert (mach == bfd_mach_m32c);
01685 
01686           /* Check for prefix bytes indicating indirect addressing.  */
01687           if (st.insn[0] == 0x41)
01688             {
01689               src_indirect = 1;
01690               i++;
01691             }
01692           else if (st.insn[0] == 0x09)
01693             {
01694               dest_indirect = 1;
01695               i++;
01696             }
01697           else if (st.insn[0] == 0x49)
01698             {
01699               src_indirect = dest_indirect = 1;
01700               i++;
01701             }
01702 
01703           /* (1) ENTER #imm8 */
01704           if (st.insn[i] == 0xec)
01705             {
01706               if (m32c_pv_enter (&st, st.insn[i + 1]))
01707                 break;
01708               st.next_addr += 2;
01709             }
01710 
01711           /* (1) PUSHM src */
01712           else if (st.insn[i] == 0x8f)
01713             {
01714               int src = st.insn[i + 1];
01715               if (m32c_pv_pushm (&st, src))
01716                 break;
01717               st.next_addr += 2;
01718 
01719               if (m32c_pushm_is_reg_save (&st, src))
01720                 after_last_frame_related_insn = st.next_addr;
01721             }
01722 
01723           /* (7) MOV.size:G src, dest */
01724           else if ((st.insn[i] & 0x80) == 0x80
01725                    && (st.insn[i + 1] & 0x0f) == 0x0b
01726                    && m32c_get_src23 (&st.insn[i]) < 20
01727                    && m32c_get_dest23 (&st.insn[i]) < 20)
01728             {
01729               struct srcdest src;
01730               struct srcdest dest;
01731               pv_t src_value;
01732               int bw = st.insn[i] & 0x01;
01733               int size = bw ? 2 : 1;
01734               st.next_addr += 2;
01735 
01736               src
01737                 = m32c_decode_sd23 (&st, m32c_get_src23 (&st.insn[i]),
01738                                     size, src_indirect);
01739               dest
01740                 = m32c_decode_sd23 (&st, m32c_get_dest23 (&st.insn[i]),
01741                                     size, dest_indirect);
01742               src_value = m32c_srcdest_fetch (&st, src, size);
01743 
01744               if (m32c_is_arg_spill (&st, dest, src_value))
01745                 after_last_frame_related_insn = st.next_addr;
01746 
01747               if (m32c_srcdest_store (&st, dest, src_value, size))
01748                 break;
01749             }
01750           /* (2) LDC #IMM24, sp */
01751           else if (st.insn[i] == 0xd5
01752                    && st.insn[i + 1] == 0x29)
01753             {
01754               st.next_addr += 2;
01755               st.sp = pv_constant (m32c_udisp24 (&st));
01756             }
01757           else
01758             /* We've hit some instruction we don't know how to simulate.
01759                Strictly speaking, we should set every value we're
01760                tracking to "unknown".  But we'll be optimistic, assume
01761                that we have enough information already, and stop
01762                analysis here.  */
01763             break;
01764         }
01765 
01766       /* If this instruction changed the FB or decreased the SP (i.e.,
01767          allocated more stack space), then this may be a good place to
01768          declare the prologue finished.  However, there are some
01769          exceptions:
01770 
01771          - If the instruction just changed the FB back to its original
01772            value, then that's probably a restore instruction.  The
01773            prologue should definitely end before that.
01774 
01775          - If the instruction increased the value of the SP (that is,
01776            shrunk the frame), then it's probably part of a frame
01777            teardown sequence, and the prologue should end before
01778            that.  */
01779 
01780       if (! pv_is_identical (st.fb, pre_insn_fb))
01781         {
01782           if (! pv_is_register_k (st.fb, tdep->fb->num, 0))
01783             after_last_frame_related_insn = st.next_addr;
01784         }
01785       else if (! pv_is_identical (st.sp, pre_insn_sp))
01786         {
01787           /* The comparison of the constants looks odd, there, because
01788              .k is unsigned.  All it really means is that the SP is
01789              lower than it was before the instruction.  */
01790           if (   pv_is_register (pre_insn_sp, tdep->sp->num)
01791               && pv_is_register (st.sp,       tdep->sp->num)
01792               && ((pre_insn_sp.k - st.sp.k) < (st.sp.k - pre_insn_sp.k)))
01793             after_last_frame_related_insn = st.next_addr;
01794         }
01795 
01796       st.scan_pc = st.next_addr;
01797     }
01798 
01799   /* Did we load a constant value into the stack pointer?  */
01800   if (pv_is_constant (st.sp))
01801     prologue->kind = prologue_first_frame;
01802 
01803   /* Alternatively, did we initialize the frame pointer?  Remember
01804      that the CFA is the address after the return address.  */
01805   if (pv_is_register (st.fb, tdep->sp->num))
01806     {
01807       prologue->kind = prologue_with_frame_ptr;
01808       prologue->frame_ptr_offset = st.fb.k;
01809     }
01810 
01811   /* Is the frame size a known constant?  Remember that frame_size is
01812      actually the offset from the CFA to the SP (i.e., a negative
01813      value).  */
01814   else if (pv_is_register (st.sp, tdep->sp->num))
01815     {
01816       prologue->kind = prologue_sans_frame_ptr;
01817       prologue->frame_size = st.sp.k;
01818     }
01819 
01820   /* We haven't been able to make sense of this function's frame.  Treat
01821      it as the first frame.  */
01822   else
01823     prologue->kind = prologue_first_frame;
01824 
01825   /* Record where all the registers were saved.  */
01826   pv_area_scan (st.stack, check_for_saved, (void *) prologue);
01827 
01828   prologue->prologue_end = after_last_frame_related_insn;
01829 
01830   do_cleanups (back_to);
01831 }
01832 
01833 
01834 static CORE_ADDR
01835 m32c_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR ip)
01836 {
01837   const char *name;
01838   CORE_ADDR func_addr, func_end, sal_end;
01839   struct m32c_prologue p;
01840 
01841   /* Try to find the extent of the function that contains IP.  */
01842   if (! find_pc_partial_function (ip, &name, &func_addr, &func_end))
01843     return ip;
01844 
01845   /* Find end by prologue analysis.  */
01846   m32c_analyze_prologue (gdbarch, ip, func_end, &p);
01847   /* Find end by line info.  */
01848   sal_end = skip_prologue_using_sal (gdbarch, ip);
01849   /* Return whichever is lower.  */
01850   if (sal_end != 0 && sal_end != ip && sal_end < p.prologue_end)
01851     return sal_end;
01852   else
01853     return p.prologue_end;
01854 }
01855 
01856 
01857 
01858 /* Stack unwinding.  */
01859 
01860 static struct m32c_prologue *
01861 m32c_analyze_frame_prologue (struct frame_info *this_frame,
01862                              void **this_prologue_cache)
01863 {
01864   if (! *this_prologue_cache)
01865     {
01866       CORE_ADDR func_start = get_frame_func (this_frame);
01867       CORE_ADDR stop_addr = get_frame_pc (this_frame);
01868 
01869       /* If we couldn't find any function containing the PC, then
01870          just initialize the prologue cache, but don't do anything.  */
01871       if (! func_start)
01872         stop_addr = func_start;
01873 
01874       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct m32c_prologue);
01875       m32c_analyze_prologue (get_frame_arch (this_frame),
01876                              func_start, stop_addr, *this_prologue_cache);
01877     }
01878 
01879   return *this_prologue_cache;
01880 }
01881 
01882 
01883 static CORE_ADDR
01884 m32c_frame_base (struct frame_info *this_frame,
01885                 void **this_prologue_cache)
01886 {
01887   struct m32c_prologue *p
01888     = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
01889   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
01890 
01891   /* In functions that use alloca, the distance between the stack
01892      pointer and the frame base varies dynamically, so we can't use
01893      the SP plus static information like prologue analysis to find the
01894      frame base.  However, such functions must have a frame pointer,
01895      to be able to restore the SP on exit.  So whenever we do have a
01896      frame pointer, use that to find the base.  */
01897   switch (p->kind)
01898     {
01899     case prologue_with_frame_ptr:
01900       {
01901         CORE_ADDR fb
01902           = get_frame_register_unsigned (this_frame, tdep->fb->num);
01903         return fb - p->frame_ptr_offset;
01904       }
01905 
01906     case prologue_sans_frame_ptr:
01907       {
01908         CORE_ADDR sp
01909           = get_frame_register_unsigned (this_frame, tdep->sp->num);
01910         return sp - p->frame_size;
01911       }
01912 
01913     case prologue_first_frame:
01914       return 0;
01915 
01916     default:
01917       gdb_assert_not_reached ("unexpected prologue kind");
01918     }
01919 }
01920 
01921 
01922 static void
01923 m32c_this_id (struct frame_info *this_frame,
01924               void **this_prologue_cache,
01925               struct frame_id *this_id)
01926 {
01927   CORE_ADDR base = m32c_frame_base (this_frame, this_prologue_cache);
01928 
01929   if (base)
01930     *this_id = frame_id_build (base, get_frame_func (this_frame));
01931   /* Otherwise, leave it unset, and that will terminate the backtrace.  */
01932 }
01933 
01934 
01935 static struct value *
01936 m32c_prev_register (struct frame_info *this_frame,
01937                     void **this_prologue_cache, int regnum)
01938 {
01939   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
01940   struct m32c_prologue *p
01941     = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
01942   CORE_ADDR frame_base = m32c_frame_base (this_frame, this_prologue_cache);
01943   int reg_size = register_size (get_frame_arch (this_frame), regnum);
01944 
01945   if (regnum == tdep->sp->num)
01946     return frame_unwind_got_constant (this_frame, regnum, frame_base);
01947 
01948   /* If prologue analysis says we saved this register somewhere,
01949      return a description of the stack slot holding it.  */
01950   if (p->reg_offset[regnum] != 1)
01951     return frame_unwind_got_memory (this_frame, regnum,
01952                                     frame_base + p->reg_offset[regnum]);
01953 
01954   /* Otherwise, presume we haven't changed the value of this
01955      register, and get it from the next frame.  */
01956   return frame_unwind_got_register (this_frame, regnum, regnum);
01957 }
01958 
01959 
01960 static const struct frame_unwind m32c_unwind = {
01961   NORMAL_FRAME,
01962   default_frame_unwind_stop_reason,
01963   m32c_this_id,
01964   m32c_prev_register,
01965   NULL,
01966   default_frame_sniffer
01967 };
01968 
01969 
01970 static CORE_ADDR
01971 m32c_unwind_pc (struct gdbarch *arch, struct frame_info *next_frame)
01972 {
01973   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
01974   return frame_unwind_register_unsigned (next_frame, tdep->pc->num);
01975 }
01976 
01977 
01978 static CORE_ADDR
01979 m32c_unwind_sp (struct gdbarch *arch, struct frame_info *next_frame)
01980 {
01981   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
01982   return frame_unwind_register_unsigned (next_frame, tdep->sp->num);
01983 }
01984 
01985 
01986 /* Inferior calls.  */
01987 
01988 /* The calling conventions, according to GCC:
01989 
01990    r8c, m16c
01991    ---------
01992    First arg may be passed in r1l or r1 if it (1) fits (QImode or
01993    HImode), (2) is named, and (3) is an integer or pointer type (no
01994    structs, floats, etc).  Otherwise, it's passed on the stack.
01995 
01996    Second arg may be passed in r2, same restrictions (but not QImode),
01997    even if the first arg is passed on the stack.
01998 
01999    Third and further args are passed on the stack.  No padding is
02000    used, stack "alignment" is 8 bits.
02001 
02002    m32cm, m32c
02003    -----------
02004 
02005    First arg may be passed in r0l or r0, same restrictions as above.
02006 
02007    Second and further args are passed on the stack.  Padding is used
02008    after QImode parameters (i.e. lower-addressed byte is the value,
02009    higher-addressed byte is the padding), stack "alignment" is 16
02010    bits.  */
02011 
02012 
02013 /* Return true if TYPE is a type that can be passed in registers.  (We
02014    ignore the size, and pay attention only to the type code;
02015    acceptable sizes depends on which register is being considered to
02016    hold it.)  */
02017 static int
02018 m32c_reg_arg_type (struct type *type)
02019 {
02020   enum type_code code = TYPE_CODE (type);
02021 
02022   return (code == TYPE_CODE_INT
02023           || code == TYPE_CODE_ENUM
02024           || code == TYPE_CODE_PTR
02025           || code == TYPE_CODE_REF
02026           || code == TYPE_CODE_BOOL
02027           || code == TYPE_CODE_CHAR);
02028 }
02029 
02030 
02031 static CORE_ADDR
02032 m32c_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
02033                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
02034                       struct value **args, CORE_ADDR sp, int struct_return,
02035                       CORE_ADDR struct_addr)
02036 {
02037   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02038   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02039   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
02040   CORE_ADDR cfa;
02041   int i;
02042 
02043   /* The number of arguments given in this function's prototype, or
02044      zero if it has a non-prototyped function type.  The m32c ABI
02045      passes arguments mentioned in the prototype differently from
02046      those in the ellipsis of a varargs function, or from those passed
02047      to a non-prototyped function.  */
02048   int num_prototyped_args = 0;
02049 
02050   {
02051     struct type *func_type = value_type (function);
02052 
02053     /* Dereference function pointer types.  */
02054     if (TYPE_CODE (func_type) == TYPE_CODE_PTR)
02055       func_type = TYPE_TARGET_TYPE (func_type);
02056 
02057     gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC ||
02058                 TYPE_CODE (func_type) == TYPE_CODE_METHOD);
02059 
02060 #if 0
02061     /* The ABI description in gcc/config/m32c/m32c.abi says that
02062        we need to handle prototyped and non-prototyped functions
02063        separately, but the code in GCC doesn't actually do so.  */
02064     if (TYPE_PROTOTYPED (func_type))
02065 #endif
02066       num_prototyped_args = TYPE_NFIELDS (func_type);
02067   }
02068 
02069   /* First, if the function returns an aggregate by value, push a
02070      pointer to a buffer for it.  This doesn't affect the way
02071      subsequent arguments are allocated to registers.  */
02072   if (struct_return)
02073     {
02074       int ptr_len = TYPE_LENGTH (tdep->ptr_voyd);
02075       sp -= ptr_len;
02076       write_memory_unsigned_integer (sp, ptr_len, byte_order, struct_addr);
02077     }
02078 
02079   /* Push the arguments.  */
02080   for (i = nargs - 1; i >= 0; i--)
02081     {
02082       struct value *arg = args[i];
02083       const gdb_byte *arg_bits = value_contents (arg);
02084       struct type *arg_type = value_type (arg);
02085       ULONGEST arg_size = TYPE_LENGTH (arg_type);
02086 
02087       /* Can it go in r1 or r1l (for m16c) or r0 or r0l (for m32c)?  */
02088       if (i == 0
02089           && arg_size <= 2
02090           && i < num_prototyped_args
02091           && m32c_reg_arg_type (arg_type))
02092         {
02093           /* Extract and re-store as an integer as a terse way to make
02094              sure it ends up in the least significant end of r1.  (GDB
02095              should avoid assuming endianness, even on uni-endian
02096              processors.)  */
02097           ULONGEST u = extract_unsigned_integer (arg_bits, arg_size,
02098                                                  byte_order);
02099           struct m32c_reg *reg = (mach == bfd_mach_m16c) ? tdep->r1 : tdep->r0;
02100           regcache_cooked_write_unsigned (regcache, reg->num, u);
02101         }
02102 
02103       /* Can it go in r2?  */
02104       else if (mach == bfd_mach_m16c
02105                && i == 1
02106                && arg_size == 2
02107                && i < num_prototyped_args
02108                && m32c_reg_arg_type (arg_type))
02109         regcache_cooked_write (regcache, tdep->r2->num, arg_bits);
02110 
02111       /* Everything else goes on the stack.  */
02112       else
02113         {
02114           sp -= arg_size;
02115 
02116           /* Align the stack.  */
02117           if (mach == bfd_mach_m32c)
02118             sp &= ~1;
02119 
02120           write_memory (sp, arg_bits, arg_size);
02121         }
02122     }
02123 
02124   /* This is the CFA we use to identify the dummy frame.  */
02125   cfa = sp;
02126 
02127   /* Push the return address.  */
02128   sp -= tdep->ret_addr_bytes;
02129   write_memory_unsigned_integer (sp, tdep->ret_addr_bytes, byte_order,
02130                                  bp_addr);
02131 
02132   /* Update the stack pointer.  */
02133   regcache_cooked_write_unsigned (regcache, tdep->sp->num, sp);
02134 
02135   /* We need to borrow an odd trick from the i386 target here.
02136 
02137      The value we return from this function gets used as the stack
02138      address (the CFA) for the dummy frame's ID.  The obvious thing is
02139      to return the new TOS.  However, that points at the return
02140      address, saved on the stack, which is inconsistent with the CFA's
02141      described by GCC's DWARF 2 .debug_frame information: DWARF 2
02142      .debug_frame info uses the address immediately after the saved
02143      return address.  So you end up with a dummy frame whose CFA
02144      points at the return address, but the frame for the function
02145      being called has a CFA pointing after the return address: the
02146      younger CFA is *greater than* the older CFA.  The sanity checks
02147      in frame.c don't like that.
02148 
02149      So we try to be consistent with the CFA's used by DWARF 2.
02150      Having a dummy frame and a real frame with the *same* CFA is
02151      tolerable.  */
02152   return cfa;
02153 }
02154 
02155 
02156 static struct frame_id
02157 m32c_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
02158 {
02159   /* This needs to return a frame ID whose PC is the return address
02160      passed to m32c_push_dummy_call, and whose stack_addr is the SP
02161      m32c_push_dummy_call returned.
02162 
02163      m32c_unwind_sp gives us the CFA, which is the value the SP had
02164      before the return address was pushed.  */
02165   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02166   CORE_ADDR sp = get_frame_register_unsigned (this_frame, tdep->sp->num);
02167   return frame_id_build (sp, get_frame_pc (this_frame));
02168 }
02169 
02170 
02171 
02172 /* Return values.  */
02173 
02174 /* Return value conventions, according to GCC:
02175 
02176    r8c, m16c
02177    ---------
02178 
02179    QImode in r0l
02180    HImode in r0
02181    SImode in r2r0
02182    near pointer in r0
02183    far pointer in r2r0
02184 
02185    Aggregate values (regardless of size) are returned by pushing a
02186    pointer to a temporary area on the stack after the args are pushed.
02187    The function fills in this area with the value.  Note that this
02188    pointer on the stack does not affect how register arguments, if any,
02189    are configured.
02190 
02191    m32cm, m32c
02192    -----------
02193    Same.  */
02194 
02195 /* Return non-zero if values of type TYPE are returned by storing them
02196    in a buffer whose address is passed on the stack, ahead of the
02197    other arguments.  */
02198 static int
02199 m32c_return_by_passed_buf (struct type *type)
02200 {
02201   enum type_code code = TYPE_CODE (type);
02202 
02203   return (code == TYPE_CODE_STRUCT
02204           || code == TYPE_CODE_UNION);
02205 }
02206 
02207 static enum return_value_convention
02208 m32c_return_value (struct gdbarch *gdbarch,
02209                    struct value *function,
02210                    struct type *valtype,
02211                    struct regcache *regcache,
02212                    gdb_byte *readbuf,
02213                    const gdb_byte *writebuf)
02214 {
02215   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02216   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02217   enum return_value_convention conv;
02218   ULONGEST valtype_len = TYPE_LENGTH (valtype);
02219 
02220   if (m32c_return_by_passed_buf (valtype))
02221     conv = RETURN_VALUE_STRUCT_CONVENTION;
02222   else
02223     conv = RETURN_VALUE_REGISTER_CONVENTION;
02224 
02225   if (readbuf)
02226     {
02227       /* We should never be called to find values being returned by
02228          RETURN_VALUE_STRUCT_CONVENTION.  Those can't be located,
02229          unless we made the call ourselves.  */
02230       gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);
02231 
02232       gdb_assert (valtype_len <= 8);
02233 
02234       /* Anything that fits in r0 is returned there.  */
02235       if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
02236         {
02237           ULONGEST u;
02238           regcache_cooked_read_unsigned (regcache, tdep->r0->num, &u);
02239           store_unsigned_integer (readbuf, valtype_len, byte_order, u);
02240         }
02241       else
02242         {
02243           /* Everything else is passed in mem0, using as many bytes as
02244              needed.  This is not what the Renesas tools do, but it's
02245              what GCC does at the moment.  */
02246           struct minimal_symbol *mem0
02247             = lookup_minimal_symbol ("mem0", NULL, NULL);
02248 
02249           if (! mem0)
02250             error (_("The return value is stored in memory at 'mem0', "
02251                      "but GDB cannot find\n"
02252                      "its address."));
02253           read_memory (SYMBOL_VALUE_ADDRESS (mem0), readbuf, valtype_len);
02254         }
02255     }
02256 
02257   if (writebuf)
02258     {
02259       /* We should never be called to store values to be returned
02260          using RETURN_VALUE_STRUCT_CONVENTION.  We have no way of
02261          finding the buffer, unless we made the call ourselves.  */
02262       gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);
02263 
02264       gdb_assert (valtype_len <= 8);
02265 
02266       /* Anything that fits in r0 is returned there.  */
02267       if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
02268         {
02269           ULONGEST u = extract_unsigned_integer (writebuf, valtype_len,
02270                                                  byte_order);
02271           regcache_cooked_write_unsigned (regcache, tdep->r0->num, u);
02272         }
02273       else
02274         {
02275           /* Everything else is passed in mem0, using as many bytes as
02276              needed.  This is not what the Renesas tools do, but it's
02277              what GCC does at the moment.  */
02278           struct minimal_symbol *mem0
02279             = lookup_minimal_symbol ("mem0", NULL, NULL);
02280 
02281           if (! mem0)
02282             error (_("The return value is stored in memory at 'mem0', "
02283                      "but GDB cannot find\n"
02284                      " its address."));
02285           write_memory (SYMBOL_VALUE_ADDRESS (mem0), writebuf, valtype_len);
02286         }
02287     }
02288 
02289   return conv;
02290 }
02291 
02292 
02293 
02294 /* Trampolines.  */
02295 
02296 /* The m16c and m32c use a trampoline function for indirect function
02297    calls.  An indirect call looks like this:
02298 
02299              ... push arguments ...
02300              ... push target function address ...
02301              jsr.a m32c_jsri16
02302 
02303    The code for m32c_jsri16 looks like this:
02304 
02305      m32c_jsri16:
02306 
02307              # Save return address.
02308              pop.w      m32c_jsri_ret
02309              pop.b      m32c_jsri_ret+2
02310 
02311              # Store target function address.
02312              pop.w      m32c_jsri_addr
02313 
02314              # Re-push return address.
02315              push.b     m32c_jsri_ret+2
02316              push.w     m32c_jsri_ret
02317 
02318              # Call the target function.
02319              jmpi.a     m32c_jsri_addr
02320 
02321    Without further information, GDB will treat calls to m32c_jsri16
02322    like calls to any other function.  Since m32c_jsri16 doesn't have
02323    debugging information, that normally means that GDB sets a step-
02324    resume breakpoint and lets the program continue --- which is not
02325    what the user wanted.  (Giving the trampoline debugging info
02326    doesn't help: the user expects the program to stop in the function
02327    their program is calling, not in some trampoline code they've never
02328    seen before.)
02329 
02330    The gdbarch_skip_trampoline_code method tells GDB how to step
02331    through such trampoline functions transparently to the user.  When
02332    given the address of a trampoline function's first instruction,
02333    gdbarch_skip_trampoline_code should return the address of the first
02334    instruction of the function really being called.  If GDB decides it
02335    wants to step into that function, it will set a breakpoint there
02336    and silently continue to it.
02337 
02338    We recognize the trampoline by name, and extract the target address
02339    directly from the stack.  This isn't great, but recognizing by its
02340    code sequence seems more fragile.  */
02341 
02342 static CORE_ADDR
02343 m32c_skip_trampoline_code (struct frame_info *frame, CORE_ADDR stop_pc)
02344 {
02345   struct gdbarch *gdbarch = get_frame_arch (frame);
02346   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02347   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02348 
02349   /* It would be nicer to simply look up the addresses of known
02350      trampolines once, and then compare stop_pc with them.  However,
02351      we'd need to ensure that that cached address got invalidated when
02352      someone loaded a new executable, and I'm not quite sure of the
02353      best way to do that.  find_pc_partial_function does do some
02354      caching, so we'll see how this goes.  */
02355   const char *name;
02356   CORE_ADDR start, end;
02357 
02358   if (find_pc_partial_function (stop_pc, &name, &start, &end))
02359     {
02360       /* Are we stopped at the beginning of the trampoline function?  */
02361       if (strcmp (name, "m32c_jsri16") == 0
02362           && stop_pc == start)
02363         {
02364           /* Get the stack pointer.  The return address is at the top,
02365              and the target function's address is just below that.  We
02366              know it's a two-byte address, since the trampoline is
02367              m32c_jsri*16*.  */
02368           CORE_ADDR sp = get_frame_sp (get_current_frame ());
02369           CORE_ADDR target
02370             = read_memory_unsigned_integer (sp + tdep->ret_addr_bytes,
02371                                             2, byte_order);
02372 
02373           /* What we have now is the address of a jump instruction.
02374              What we need is the destination of that jump.
02375              The opcode is 1 byte, and the destination is the next 3 bytes.  */
02376 
02377           target = read_memory_unsigned_integer (target + 1, 3, byte_order);
02378           return target;
02379         }
02380     }
02381 
02382   return 0;
02383 }
02384 
02385 
02386 /* Address/pointer conversions.  */
02387 
02388 /* On the m16c, there is a 24-bit address space, but only a very few
02389    instructions can generate addresses larger than 0xffff: jumps,
02390    jumps to subroutines, and the lde/std (load/store extended)
02391    instructions.
02392 
02393    Since GCC can only support one size of pointer, we can't have
02394    distinct 'near' and 'far' pointer types; we have to pick one size
02395    for everything.  If we wanted to use 24-bit pointers, then GCC
02396    would have to use lde and ste for all memory references, which
02397    would be terrible for performance and code size.  So the GNU
02398    toolchain uses 16-bit pointers for everything, and gives up the
02399    ability to have pointers point outside the first 64k of memory.
02400 
02401    However, as a special hack, we let the linker place functions at
02402    addresses above 0xffff, as long as it also places a trampoline in
02403    the low 64k for every function whose address is taken.  Each
02404    trampoline consists of a single jmp.a instruction that jumps to the
02405    function's real entry point.  Pointers to functions can be 16 bits
02406    long, even though the functions themselves are at higher addresses:
02407    the pointers refer to the trampolines, not the functions.
02408 
02409    This complicates things for GDB, however: given the address of a
02410    function (from debug info or linker symbols, say) which could be
02411    anywhere in the 24-bit address space, how can we find an
02412    appropriate 16-bit value to use as a pointer to it?
02413 
02414    If the linker has not generated a trampoline for the function,
02415    we're out of luck.  Well, I guess we could malloc some space and
02416    write a jmp.a instruction to it, but I'm not going to get into that
02417    at the moment.
02418 
02419    If the linker has generated a trampoline for the function, then it
02420    also emitted a symbol for the trampoline: if the function's linker
02421    symbol is named NAME, then the function's trampoline's linker
02422    symbol is named NAME.plt.
02423 
02424    So, given a code address:
02425    - We try to find a linker symbol at that address.
02426    - If we find such a symbol named NAME, we look for a linker symbol
02427      named NAME.plt.
02428    - If we find such a symbol, we assume it is a trampoline, and use
02429      its address as the pointer value.
02430 
02431    And, given a function pointer:
02432    - We try to find a linker symbol at that address named NAME.plt.
02433    - If we find such a symbol, we look for a linker symbol named NAME.
02434    - If we find that, we provide that as the function's address.
02435    - If any of the above steps fail, we return the original address
02436      unchanged; it might really be a function in the low 64k.
02437 
02438    See?  You *knew* there was a reason you wanted to be a computer
02439    programmer!  :)  */
02440 
02441 static void
02442 m32c_m16c_address_to_pointer (struct gdbarch *gdbarch,
02443                               struct type *type, gdb_byte *buf, CORE_ADDR addr)
02444 {
02445   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02446   enum type_code target_code;
02447   gdb_assert (TYPE_CODE (type) == TYPE_CODE_PTR ||
02448               TYPE_CODE (type) == TYPE_CODE_REF);
02449 
02450   target_code = TYPE_CODE (TYPE_TARGET_TYPE (type));
02451 
02452   if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
02453     {
02454       const char *func_name;
02455       char *tramp_name;
02456       struct minimal_symbol *tramp_msym;
02457 
02458       /* Try to find a linker symbol at this address.  */
02459       struct bound_minimal_symbol func_msym
02460         = lookup_minimal_symbol_by_pc (addr);
02461 
02462       if (! func_msym.minsym)
02463         error (_("Cannot convert code address %s to function pointer:\n"
02464                "couldn't find a symbol at that address, to find trampoline."),
02465                paddress (gdbarch, addr));
02466 
02467       func_name = SYMBOL_LINKAGE_NAME (func_msym.minsym);
02468       tramp_name = xmalloc (strlen (func_name) + 5);
02469       strcpy (tramp_name, func_name);
02470       strcat (tramp_name, ".plt");
02471 
02472       /* Try to find a linker symbol for the trampoline.  */
02473       tramp_msym = lookup_minimal_symbol (tramp_name, NULL, NULL);
02474 
02475       /* We've either got another copy of the name now, or don't need
02476          the name any more.  */
02477       xfree (tramp_name);
02478 
02479       if (! tramp_msym)
02480         {
02481           CORE_ADDR ptrval;
02482 
02483           /* No PLT entry found.  Mask off the upper bits of the address
02484              to make a pointer.  As noted in the warning to the user
02485              below, this value might be useful if converted back into
02486              an address by GDB, but will otherwise, almost certainly,
02487              be garbage.
02488              
02489              Using this masked result does seem to be useful
02490              in gdb.cp/cplusfuncs.exp in which ~40 FAILs turn into
02491              PASSes.  These results appear to be correct as well.
02492              
02493              We print a warning here so that the user can make a
02494              determination about whether the result is useful or not.  */
02495           ptrval = addr & 0xffff;
02496 
02497           warning (_("Cannot convert code address %s to function pointer:\n"
02498                    "couldn't find trampoline named '%s.plt'.\n"
02499                    "Returning pointer value %s instead; this may produce\n"
02500                    "a useful result if converted back into an address by GDB,\n"
02501                    "but will most likely not be useful otherwise.\n"),
02502                    paddress (gdbarch, addr), func_name,
02503                    paddress (gdbarch, ptrval));
02504 
02505           addr = ptrval;
02506 
02507         }
02508       else
02509         {
02510           /* The trampoline's address is our pointer.  */
02511           addr = SYMBOL_VALUE_ADDRESS (tramp_msym);
02512         }
02513     }
02514 
02515   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
02516 }
02517 
02518 
02519 static CORE_ADDR
02520 m32c_m16c_pointer_to_address (struct gdbarch *gdbarch,
02521                               struct type *type, const gdb_byte *buf)
02522 {
02523   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
02524   CORE_ADDR ptr;
02525   enum type_code target_code;
02526 
02527   gdb_assert (TYPE_CODE (type) == TYPE_CODE_PTR ||
02528               TYPE_CODE (type) == TYPE_CODE_REF);
02529 
02530   ptr = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
02531 
02532   target_code = TYPE_CODE (TYPE_TARGET_TYPE (type));
02533 
02534   if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
02535     {
02536       /* See if there is a minimal symbol at that address whose name is
02537          "NAME.plt".  */
02538       struct bound_minimal_symbol ptr_msym = lookup_minimal_symbol_by_pc (ptr);
02539 
02540       if (ptr_msym.minsym)
02541         {
02542           const char *ptr_msym_name = SYMBOL_LINKAGE_NAME (ptr_msym.minsym);
02543           int len = strlen (ptr_msym_name);
02544 
02545           if (len > 4
02546               && strcmp (ptr_msym_name + len - 4, ".plt") == 0)
02547             {
02548               struct minimal_symbol *func_msym;
02549               /* We have a .plt symbol; try to find the symbol for the
02550                  corresponding function.
02551 
02552                  Since the trampoline contains a jump instruction, we
02553                  could also just extract the jump's target address.  I
02554                  don't see much advantage one way or the other.  */
02555               char *func_name = xmalloc (len - 4 + 1);
02556               memcpy (func_name, ptr_msym_name, len - 4);
02557               func_name[len - 4] = '\0';
02558               func_msym
02559                 = lookup_minimal_symbol (func_name, NULL, NULL);
02560 
02561               /* If we do have such a symbol, return its value as the
02562                  function's true address.  */
02563               if (func_msym)
02564                 ptr = SYMBOL_VALUE_ADDRESS (func_msym);
02565             }
02566         }
02567       else
02568         {
02569           int aspace;
02570 
02571           for (aspace = 1; aspace <= 15; aspace++)
02572             {
02573               ptr_msym = lookup_minimal_symbol_by_pc ((aspace << 16) | ptr);
02574               
02575               if (ptr_msym.minsym)
02576                 ptr |= aspace << 16;
02577             }
02578         }
02579     }
02580 
02581   return ptr;
02582 }
02583 
02584 static void
02585 m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
02586                             int *frame_regnum,
02587                             LONGEST *frame_offset)
02588 {
02589   const char *name;
02590   CORE_ADDR func_addr, func_end;
02591   struct m32c_prologue p;
02592 
02593   struct regcache *regcache = get_current_regcache ();
02594   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
02595   
02596   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
02597     internal_error (__FILE__, __LINE__,
02598                     _("No virtual frame pointer available"));
02599 
02600   m32c_analyze_prologue (gdbarch, func_addr, pc, &p);
02601   switch (p.kind)
02602     {
02603     case prologue_with_frame_ptr:
02604       *frame_regnum = m32c_banked_register (tdep->fb, regcache)->num;
02605       *frame_offset = p.frame_ptr_offset;
02606       break;
02607     case prologue_sans_frame_ptr:
02608       *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
02609       *frame_offset = p.frame_size;
02610       break;
02611     default:
02612       *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
02613       *frame_offset = 0;
02614       break;
02615     }
02616   /* Sanity check */
02617   if (*frame_regnum > gdbarch_num_regs (gdbarch))
02618     internal_error (__FILE__, __LINE__,
02619                     _("No virtual frame pointer available"));
02620 }
02621 
02622 
02623 /* Initialization.  */
02624 
02625 static struct gdbarch *
02626 m32c_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
02627 {
02628   struct gdbarch *arch;
02629   struct gdbarch_tdep *tdep;
02630   unsigned long mach = info.bfd_arch_info->mach;
02631 
02632   /* Find a candidate among the list of architectures we've created
02633      already.  */
02634   for (arches = gdbarch_list_lookup_by_info (arches, &info);
02635        arches != NULL;
02636        arches = gdbarch_list_lookup_by_info (arches->next, &info))
02637     return arches->gdbarch;
02638 
02639   tdep = xcalloc (1, sizeof (*tdep));
02640   arch = gdbarch_alloc (&info, tdep);
02641 
02642   /* Essential types.  */
02643   make_types (arch);
02644 
02645   /* Address/pointer conversions.  */
02646   if (mach == bfd_mach_m16c)
02647     {
02648       set_gdbarch_address_to_pointer (arch, m32c_m16c_address_to_pointer);
02649       set_gdbarch_pointer_to_address (arch, m32c_m16c_pointer_to_address);
02650     }
02651 
02652   /* Register set.  */
02653   make_regs (arch);
02654 
02655   /* Disassembly.  */
02656   set_gdbarch_print_insn (arch, print_insn_m32c);
02657 
02658   /* Breakpoints.  */
02659   set_gdbarch_breakpoint_from_pc (arch, m32c_breakpoint_from_pc);
02660 
02661   /* Prologue analysis and unwinding.  */
02662   set_gdbarch_inner_than (arch, core_addr_lessthan);
02663   set_gdbarch_skip_prologue (arch, m32c_skip_prologue);
02664   set_gdbarch_unwind_pc (arch, m32c_unwind_pc);
02665   set_gdbarch_unwind_sp (arch, m32c_unwind_sp);
02666 #if 0
02667   /* I'm dropping the dwarf2 sniffer because it has a few problems.
02668      They may be in the dwarf2 cfi code in GDB, or they may be in
02669      the debug info emitted by the upstream toolchain.  I don't 
02670      know which, but I do know that the prologue analyzer works better.
02671      MVS 04/13/06  */
02672   dwarf2_append_sniffers (arch);
02673 #endif
02674   frame_unwind_append_unwinder (arch, &m32c_unwind);
02675 
02676   /* Inferior calls.  */
02677   set_gdbarch_push_dummy_call (arch, m32c_push_dummy_call);
02678   set_gdbarch_return_value (arch, m32c_return_value);
02679   set_gdbarch_dummy_id (arch, m32c_dummy_id);
02680 
02681   /* Trampolines.  */
02682   set_gdbarch_skip_trampoline_code (arch, m32c_skip_trampoline_code);
02683 
02684   set_gdbarch_virtual_frame_pointer (arch, m32c_virtual_frame_pointer);
02685 
02686   /* m32c function boundary addresses are not necessarily even.
02687      Therefore, the `vbit', which indicates a pointer to a virtual
02688      member function, is stored in the delta field, rather than as
02689      the low bit of a function pointer address.
02690 
02691      In order to verify this, see the definition of
02692      TARGET_PTRMEMFUNC_VBIT_LOCATION in gcc/defaults.h along with the
02693      definition of FUNCTION_BOUNDARY in gcc/config/m32c/m32c.h.  */
02694   set_gdbarch_vbit_in_delta (arch, 1);
02695 
02696   return arch;
02697 }
02698 
02699 /* Provide a prototype to silence -Wmissing-prototypes.  */
02700 extern initialize_file_ftype _initialize_m32c_tdep;
02701 
02702 void
02703 _initialize_m32c_tdep (void)
02704 {
02705   register_gdbarch_init (bfd_arch_m32c, m32c_gdbarch_init);
02706 
02707   m32c_dma_reggroup = reggroup_new ("dma", USER_REGGROUP);
02708 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines