xref: /netbsd/external/gpl3/gdb/dist/gdb/m32c-tdep.c (revision 1424dfb3)
166e63ce3Schristos /* Renesas M32C target-dependent code for GDB, the GNU debugger.
266e63ce3Schristos 
3*1424dfb3Schristos    Copyright (C) 2004-2020 Free Software Foundation, Inc.
466e63ce3Schristos 
566e63ce3Schristos    This file is part of GDB.
666e63ce3Schristos 
766e63ce3Schristos    This program is free software; you can redistribute it and/or modify
866e63ce3Schristos    it under the terms of the GNU General Public License as published by
966e63ce3Schristos    the Free Software Foundation; either version 3 of the License, or
1066e63ce3Schristos    (at your option) any later version.
1166e63ce3Schristos 
1266e63ce3Schristos    This program is distributed in the hope that it will be useful,
1366e63ce3Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1466e63ce3Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1566e63ce3Schristos    GNU General Public License for more details.
1666e63ce3Schristos 
1766e63ce3Schristos    You should have received a copy of the GNU General Public License
1866e63ce3Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1966e63ce3Schristos 
2066e63ce3Schristos #include "defs.h"
2166e63ce3Schristos #include "elf-bfd.h"
2266e63ce3Schristos #include "elf/m32c.h"
2366e63ce3Schristos #include "gdb/sim-m32c.h"
2466e63ce3Schristos #include "dis-asm.h"
2566e63ce3Schristos #include "gdbtypes.h"
2666e63ce3Schristos #include "regcache.h"
2766e63ce3Schristos #include "arch-utils.h"
2866e63ce3Schristos #include "frame.h"
2966e63ce3Schristos #include "frame-unwind.h"
30*1424dfb3Schristos #include "dwarf2/frame.h"
31*1424dfb3Schristos #include "dwarf2/expr.h"
3266e63ce3Schristos #include "symtab.h"
3366e63ce3Schristos #include "gdbcore.h"
3466e63ce3Schristos #include "value.h"
3566e63ce3Schristos #include "reggroups.h"
3666e63ce3Schristos #include "prologue-value.h"
3766e63ce3Schristos #include "target.h"
3826a53354Schristos #include "objfiles.h"
3966e63ce3Schristos 
4066e63ce3Schristos 
4166e63ce3Schristos /* The m32c tdep structure.  */
4266e63ce3Schristos 
4366e63ce3Schristos static struct reggroup *m32c_dma_reggroup;
4466e63ce3Schristos 
4566e63ce3Schristos struct m32c_reg;
4666e63ce3Schristos 
4766e63ce3Schristos /* The type of a function that moves the value of REG between CACHE or
4866e63ce3Schristos    BUF --- in either direction.  */
49c03b94e9Schristos typedef enum register_status (m32c_write_reg_t) (struct m32c_reg *reg,
5066e63ce3Schristos 						 struct regcache *cache,
51c03b94e9Schristos 						 const gdb_byte *buf);
52c03b94e9Schristos 
53c03b94e9Schristos typedef enum register_status (m32c_read_reg_t) (struct m32c_reg *reg,
5407163879Schristos 						readable_regcache *cache,
55c03b94e9Schristos 						gdb_byte *buf);
5666e63ce3Schristos 
5766e63ce3Schristos struct m32c_reg
5866e63ce3Schristos {
5966e63ce3Schristos   /* The name of this register.  */
6066e63ce3Schristos   const char *name;
6166e63ce3Schristos 
6266e63ce3Schristos   /* Its type.  */
6366e63ce3Schristos   struct type *type;
6466e63ce3Schristos 
6566e63ce3Schristos   /* The architecture this register belongs to.  */
6666e63ce3Schristos   struct gdbarch *arch;
6766e63ce3Schristos 
6866e63ce3Schristos   /* Its GDB register number.  */
6966e63ce3Schristos   int num;
7066e63ce3Schristos 
7166e63ce3Schristos   /* Its sim register number.  */
7266e63ce3Schristos   int sim_num;
7366e63ce3Schristos 
7466e63ce3Schristos   /* Its DWARF register number, or -1 if it doesn't have one.  */
7566e63ce3Schristos   int dwarf_num;
7666e63ce3Schristos 
7766e63ce3Schristos   /* Register group memberships.  */
7866e63ce3Schristos   unsigned int general_p : 1;
7966e63ce3Schristos   unsigned int dma_p : 1;
8066e63ce3Schristos   unsigned int system_p : 1;
8166e63ce3Schristos   unsigned int save_restore_p : 1;
8266e63ce3Schristos 
8366e63ce3Schristos   /* Functions to read its value from a regcache, and write its value
8466e63ce3Schristos      to a regcache.  */
85c03b94e9Schristos   m32c_read_reg_t *read;
86c03b94e9Schristos   m32c_write_reg_t *write;
8766e63ce3Schristos 
8866e63ce3Schristos   /* Data for READ and WRITE functions.  The exact meaning depends on
8966e63ce3Schristos      the specific functions selected; see the comments for those
9066e63ce3Schristos      functions.  */
9166e63ce3Schristos   struct m32c_reg *rx, *ry;
9266e63ce3Schristos   int n;
9366e63ce3Schristos };
9466e63ce3Schristos 
9566e63ce3Schristos 
9666e63ce3Schristos /* An overestimate of the number of raw and pseudoregisters we will
9766e63ce3Schristos    have.  The exact answer depends on the variant of the architecture
9866e63ce3Schristos    at hand, but we can use this to declare statically allocated
9966e63ce3Schristos    arrays, and bump it up when needed.  */
10066e63ce3Schristos #define M32C_MAX_NUM_REGS (75)
10166e63ce3Schristos 
10266e63ce3Schristos /* The largest assigned DWARF register number.  */
10366e63ce3Schristos #define M32C_MAX_DWARF_REGNUM (40)
10466e63ce3Schristos 
10566e63ce3Schristos 
10666e63ce3Schristos struct gdbarch_tdep
10766e63ce3Schristos {
10866e63ce3Schristos   /* All the registers for this variant, indexed by GDB register
10966e63ce3Schristos      number, and the number of registers present.  */
11066e63ce3Schristos   struct m32c_reg regs[M32C_MAX_NUM_REGS];
11166e63ce3Schristos 
11266e63ce3Schristos   /* The number of valid registers.  */
11366e63ce3Schristos   int num_regs;
11466e63ce3Schristos 
11566e63ce3Schristos   /* Interesting registers.  These are pointers into REGS.  */
11666e63ce3Schristos   struct m32c_reg *pc, *flg;
11766e63ce3Schristos   struct m32c_reg *r0, *r1, *r2, *r3, *a0, *a1;
11866e63ce3Schristos   struct m32c_reg *r2r0, *r3r2r1r0, *r3r1r2r0;
11966e63ce3Schristos   struct m32c_reg *sb, *fb, *sp;
12066e63ce3Schristos 
12166e63ce3Schristos   /* A table indexed by DWARF register numbers, pointing into
12266e63ce3Schristos      REGS.  */
12366e63ce3Schristos   struct m32c_reg *dwarf_regs[M32C_MAX_DWARF_REGNUM + 1];
12466e63ce3Schristos 
12566e63ce3Schristos   /* Types for this architecture.  We can't use the builtin_type_foo
12666e63ce3Schristos      types, because they're not initialized when building a gdbarch
12766e63ce3Schristos      structure.  */
12866e63ce3Schristos   struct type *voyd, *ptr_voyd, *func_voyd;
12966e63ce3Schristos   struct type *uint8, *uint16;
13066e63ce3Schristos   struct type *int8, *int16, *int32, *int64;
13166e63ce3Schristos 
13266e63ce3Schristos   /* The types for data address and code address registers.  */
13366e63ce3Schristos   struct type *data_addr_reg_type, *code_addr_reg_type;
13466e63ce3Schristos 
13566e63ce3Schristos   /* The number of bytes a return address pushed by a 'jsr' instruction
13666e63ce3Schristos      occupies on the stack.  */
13766e63ce3Schristos   int ret_addr_bytes;
13866e63ce3Schristos 
13966e63ce3Schristos   /* The number of bytes an address register occupies on the stack
14066e63ce3Schristos      when saved by an 'enter' or 'pushm' instruction.  */
14166e63ce3Schristos   int push_addr_bytes;
14266e63ce3Schristos };
14366e63ce3Schristos 
14466e63ce3Schristos 
14566e63ce3Schristos /* Types.  */
14666e63ce3Schristos 
14766e63ce3Schristos static void
make_types(struct gdbarch * arch)14866e63ce3Schristos make_types (struct gdbarch *arch)
14966e63ce3Schristos {
15066e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
15166e63ce3Schristos   unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
15266e63ce3Schristos   int data_addr_reg_bits, code_addr_reg_bits;
15366e63ce3Schristos   char type_name[50];
15466e63ce3Schristos 
15566e63ce3Schristos #if 0
15666e63ce3Schristos   /* This is used to clip CORE_ADDR values, so this value is
15766e63ce3Schristos      appropriate both on the m32c, where pointers are 32 bits long,
15866e63ce3Schristos      and on the m16c, where pointers are sixteen bits long, but there
15966e63ce3Schristos      may be code above the 64k boundary.  */
16066e63ce3Schristos   set_gdbarch_addr_bit (arch, 24);
16166e63ce3Schristos #else
16266e63ce3Schristos   /* GCC uses 32 bits for addrs in the dwarf info, even though
16366e63ce3Schristos      only 16/24 bits are used.  Setting addr_bit to 24 causes
16466e63ce3Schristos      errors in reading the dwarf addresses.  */
16566e63ce3Schristos   set_gdbarch_addr_bit (arch, 32);
16666e63ce3Schristos #endif
16766e63ce3Schristos 
16866e63ce3Schristos   set_gdbarch_int_bit (arch, 16);
16966e63ce3Schristos   switch (mach)
17066e63ce3Schristos     {
17166e63ce3Schristos     case bfd_mach_m16c:
17266e63ce3Schristos       data_addr_reg_bits = 16;
17366e63ce3Schristos       code_addr_reg_bits = 24;
17466e63ce3Schristos       set_gdbarch_ptr_bit (arch, 16);
17566e63ce3Schristos       tdep->ret_addr_bytes = 3;
17666e63ce3Schristos       tdep->push_addr_bytes = 2;
17766e63ce3Schristos       break;
17866e63ce3Schristos 
17966e63ce3Schristos     case bfd_mach_m32c:
18066e63ce3Schristos       data_addr_reg_bits = 24;
18166e63ce3Schristos       code_addr_reg_bits = 24;
18266e63ce3Schristos       set_gdbarch_ptr_bit (arch, 32);
18366e63ce3Schristos       tdep->ret_addr_bytes = 4;
18466e63ce3Schristos       tdep->push_addr_bytes = 4;
18566e63ce3Schristos       break;
18666e63ce3Schristos 
18766e63ce3Schristos     default:
18866e63ce3Schristos       gdb_assert_not_reached ("unexpected mach");
18966e63ce3Schristos     }
19066e63ce3Schristos 
19166e63ce3Schristos   /* The builtin_type_mumble variables are sometimes uninitialized when
19266e63ce3Schristos      this is called, so we avoid using them.  */
19307163879Schristos   tdep->voyd = arch_type (arch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
19466e63ce3Schristos   tdep->ptr_voyd
1951c468f90Schristos     = arch_pointer_type (arch, gdbarch_ptr_bit (arch), NULL, tdep->voyd);
19666e63ce3Schristos   tdep->func_voyd = lookup_function_type (tdep->voyd);
19766e63ce3Schristos 
19848596154Schristos   xsnprintf (type_name, sizeof (type_name), "%s_data_addr_t",
19966e63ce3Schristos 	     gdbarch_bfd_arch_info (arch)->printable_name);
20066e63ce3Schristos   tdep->data_addr_reg_type
2011c468f90Schristos     = arch_pointer_type (arch, data_addr_reg_bits, type_name, tdep->voyd);
20266e63ce3Schristos 
20348596154Schristos   xsnprintf (type_name, sizeof (type_name), "%s_code_addr_t",
20466e63ce3Schristos 	     gdbarch_bfd_arch_info (arch)->printable_name);
20566e63ce3Schristos   tdep->code_addr_reg_type
2061c468f90Schristos     = arch_pointer_type (arch, code_addr_reg_bits, type_name, tdep->func_voyd);
20766e63ce3Schristos 
20866e63ce3Schristos   tdep->uint8  = arch_integer_type (arch,  8, 1, "uint8_t");
20966e63ce3Schristos   tdep->uint16 = arch_integer_type (arch, 16, 1, "uint16_t");
21066e63ce3Schristos   tdep->int8   = arch_integer_type (arch,  8, 0, "int8_t");
21166e63ce3Schristos   tdep->int16  = arch_integer_type (arch, 16, 0, "int16_t");
21266e63ce3Schristos   tdep->int32  = arch_integer_type (arch, 32, 0, "int32_t");
21366e63ce3Schristos   tdep->int64  = arch_integer_type (arch, 64, 0, "int64_t");
21466e63ce3Schristos }
21566e63ce3Schristos 
21666e63ce3Schristos 
21766e63ce3Schristos 
21866e63ce3Schristos /* Register set.  */
21966e63ce3Schristos 
22066e63ce3Schristos static const char *
m32c_register_name(struct gdbarch * gdbarch,int num)22166e63ce3Schristos m32c_register_name (struct gdbarch *gdbarch, int num)
22266e63ce3Schristos {
22366e63ce3Schristos   return gdbarch_tdep (gdbarch)->regs[num].name;
22466e63ce3Schristos }
22566e63ce3Schristos 
22666e63ce3Schristos 
22766e63ce3Schristos static struct type *
m32c_register_type(struct gdbarch * arch,int reg_nr)22866e63ce3Schristos m32c_register_type (struct gdbarch *arch, int reg_nr)
22966e63ce3Schristos {
23066e63ce3Schristos   return gdbarch_tdep (arch)->regs[reg_nr].type;
23166e63ce3Schristos }
23266e63ce3Schristos 
23366e63ce3Schristos 
23466e63ce3Schristos static int
m32c_register_sim_regno(struct gdbarch * gdbarch,int reg_nr)23566e63ce3Schristos m32c_register_sim_regno (struct gdbarch *gdbarch, int reg_nr)
23666e63ce3Schristos {
23766e63ce3Schristos   return gdbarch_tdep (gdbarch)->regs[reg_nr].sim_num;
23866e63ce3Schristos }
23966e63ce3Schristos 
24066e63ce3Schristos 
24166e63ce3Schristos static int
m32c_debug_info_reg_to_regnum(struct gdbarch * gdbarch,int reg_nr)24266e63ce3Schristos m32c_debug_info_reg_to_regnum (struct gdbarch *gdbarch, int reg_nr)
24366e63ce3Schristos {
24466e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
24566e63ce3Schristos   if (0 <= reg_nr && reg_nr <= M32C_MAX_DWARF_REGNUM
24666e63ce3Schristos       && tdep->dwarf_regs[reg_nr])
24766e63ce3Schristos     return tdep->dwarf_regs[reg_nr]->num;
24866e63ce3Schristos   else
24966e63ce3Schristos     /* The DWARF CFI code expects to see -1 for invalid register
25066e63ce3Schristos        numbers.  */
25166e63ce3Schristos     return -1;
25266e63ce3Schristos }
25366e63ce3Schristos 
25466e63ce3Schristos 
25566e63ce3Schristos static int
m32c_register_reggroup_p(struct gdbarch * gdbarch,int regnum,struct reggroup * group)25666e63ce3Schristos m32c_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
25766e63ce3Schristos 			  struct reggroup *group)
25866e63ce3Schristos {
25966e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
26066e63ce3Schristos   struct m32c_reg *reg = &tdep->regs[regnum];
26166e63ce3Schristos 
26266e63ce3Schristos   /* The anonymous raw registers aren't in any groups.  */
26366e63ce3Schristos   if (! reg->name)
26466e63ce3Schristos     return 0;
26566e63ce3Schristos 
26666e63ce3Schristos   if (group == all_reggroup)
26766e63ce3Schristos     return 1;
26866e63ce3Schristos 
26966e63ce3Schristos   if (group == general_reggroup
27066e63ce3Schristos       && reg->general_p)
27166e63ce3Schristos     return 1;
27266e63ce3Schristos 
27366e63ce3Schristos   if (group == m32c_dma_reggroup
27466e63ce3Schristos       && reg->dma_p)
27566e63ce3Schristos     return 1;
27666e63ce3Schristos 
27766e63ce3Schristos   if (group == system_reggroup
27866e63ce3Schristos       && reg->system_p)
27966e63ce3Schristos     return 1;
28066e63ce3Schristos 
28166e63ce3Schristos   /* Since the m32c DWARF register numbers refer to cooked registers, not
28266e63ce3Schristos      raw registers, and frame_pop depends on the save and restore groups
28366e63ce3Schristos      containing registers the DWARF CFI will actually mention, our save
28466e63ce3Schristos      and restore groups are cooked registers, not raw registers.  (This is
28566e63ce3Schristos      why we can't use the default reggroup function.)  */
28666e63ce3Schristos   if ((group == save_reggroup
28766e63ce3Schristos        || group == restore_reggroup)
28866e63ce3Schristos       && reg->save_restore_p)
28966e63ce3Schristos     return 1;
29066e63ce3Schristos 
29166e63ce3Schristos   return 0;
29266e63ce3Schristos }
29366e63ce3Schristos 
29466e63ce3Schristos 
29566e63ce3Schristos /* Register move functions.  We declare them here using
296c03b94e9Schristos    m32c_{read,write}_reg_t to check the types.  */
297c03b94e9Schristos static m32c_read_reg_t m32c_raw_read;
298c03b94e9Schristos static m32c_read_reg_t m32c_banked_read;
299c03b94e9Schristos static m32c_read_reg_t m32c_sb_read;
300c03b94e9Schristos static m32c_read_reg_t m32c_part_read;
301c03b94e9Schristos static m32c_read_reg_t m32c_cat_read;
302c03b94e9Schristos static m32c_read_reg_t m32c_r3r2r1r0_read;
30366e63ce3Schristos 
304c03b94e9Schristos static m32c_write_reg_t m32c_raw_write;
305c03b94e9Schristos static m32c_write_reg_t m32c_banked_write;
306c03b94e9Schristos static m32c_write_reg_t m32c_sb_write;
307c03b94e9Schristos static m32c_write_reg_t m32c_part_write;
308c03b94e9Schristos static m32c_write_reg_t m32c_cat_write;
309c03b94e9Schristos static m32c_write_reg_t m32c_r3r2r1r0_write;
31066e63ce3Schristos 
31166e63ce3Schristos /* Copy the value of the raw register REG from CACHE to BUF.  */
31266e63ce3Schristos static enum register_status
m32c_raw_read(struct m32c_reg * reg,readable_regcache * cache,gdb_byte * buf)31307163879Schristos m32c_raw_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
31466e63ce3Schristos {
31507163879Schristos   return cache->raw_read (reg->num, buf);
31666e63ce3Schristos }
31766e63ce3Schristos 
31866e63ce3Schristos 
31966e63ce3Schristos /* Copy the value of the raw register REG from BUF to CACHE.  */
32066e63ce3Schristos static enum register_status
m32c_raw_write(struct m32c_reg * reg,struct regcache * cache,const gdb_byte * buf)321c03b94e9Schristos m32c_raw_write (struct m32c_reg *reg, struct regcache *cache,
322c03b94e9Schristos 		const gdb_byte *buf)
32366e63ce3Schristos {
32407163879Schristos   cache->raw_write (reg->num, buf);
32566e63ce3Schristos 
32666e63ce3Schristos   return REG_VALID;
32766e63ce3Schristos }
32866e63ce3Schristos 
32966e63ce3Schristos 
33066e63ce3Schristos /* Return the value of the 'flg' register in CACHE.  */
33166e63ce3Schristos static int
m32c_read_flg(readable_regcache * cache)33207163879Schristos m32c_read_flg (readable_regcache *cache)
33366e63ce3Schristos {
33407163879Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (cache->arch ());
33566e63ce3Schristos   ULONGEST flg;
33607163879Schristos 
33707163879Schristos   cache->raw_read (tdep->flg->num, &flg);
33866e63ce3Schristos   return flg & 0xffff;
33966e63ce3Schristos }
34066e63ce3Schristos 
34166e63ce3Schristos 
34266e63ce3Schristos /* Evaluate the real register number of a banked register.  */
34366e63ce3Schristos static struct m32c_reg *
m32c_banked_register(struct m32c_reg * reg,readable_regcache * cache)34407163879Schristos m32c_banked_register (struct m32c_reg *reg, readable_regcache *cache)
34566e63ce3Schristos {
34666e63ce3Schristos   return ((m32c_read_flg (cache) & reg->n) ? reg->ry : reg->rx);
34766e63ce3Schristos }
34866e63ce3Schristos 
34966e63ce3Schristos 
35066e63ce3Schristos /* Move the value of a banked register from CACHE to BUF.
35166e63ce3Schristos    If the value of the 'flg' register in CACHE has any of the bits
35266e63ce3Schristos    masked in REG->n set, then read REG->ry.  Otherwise, read
35366e63ce3Schristos    REG->rx.  */
35466e63ce3Schristos static enum register_status
m32c_banked_read(struct m32c_reg * reg,readable_regcache * cache,gdb_byte * buf)35507163879Schristos m32c_banked_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
35666e63ce3Schristos {
35766e63ce3Schristos   struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
35807163879Schristos   return cache->raw_read (bank_reg->num, buf);
35966e63ce3Schristos }
36066e63ce3Schristos 
36166e63ce3Schristos 
36266e63ce3Schristos /* Move the value of a banked register from BUF to CACHE.
36366e63ce3Schristos    If the value of the 'flg' register in CACHE has any of the bits
36466e63ce3Schristos    masked in REG->n set, then write REG->ry.  Otherwise, write
36566e63ce3Schristos    REG->rx.  */
36666e63ce3Schristos static enum register_status
m32c_banked_write(struct m32c_reg * reg,struct regcache * cache,const gdb_byte * buf)367c03b94e9Schristos m32c_banked_write (struct m32c_reg *reg, struct regcache *cache,
368c03b94e9Schristos 		   const gdb_byte *buf)
36966e63ce3Schristos {
37066e63ce3Schristos   struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
37107163879Schristos   cache->raw_write (bank_reg->num, buf);
37266e63ce3Schristos 
37366e63ce3Schristos   return REG_VALID;
37466e63ce3Schristos }
37566e63ce3Schristos 
37666e63ce3Schristos 
37766e63ce3Schristos /* Move the value of SB from CACHE to BUF.  On bfd_mach_m32c, SB is a
37866e63ce3Schristos    banked register; on bfd_mach_m16c, it's not.  */
37966e63ce3Schristos static enum register_status
m32c_sb_read(struct m32c_reg * reg,readable_regcache * cache,gdb_byte * buf)38007163879Schristos m32c_sb_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
38166e63ce3Schristos {
38266e63ce3Schristos   if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
38366e63ce3Schristos     return m32c_raw_read (reg->rx, cache, buf);
38466e63ce3Schristos   else
38566e63ce3Schristos     return m32c_banked_read (reg, cache, buf);
38666e63ce3Schristos }
38766e63ce3Schristos 
38866e63ce3Schristos 
38966e63ce3Schristos /* Move the value of SB from BUF to CACHE.  On bfd_mach_m32c, SB is a
39066e63ce3Schristos    banked register; on bfd_mach_m16c, it's not.  */
39166e63ce3Schristos static enum register_status
m32c_sb_write(struct m32c_reg * reg,struct regcache * cache,const gdb_byte * buf)392c03b94e9Schristos m32c_sb_write (struct m32c_reg *reg, struct regcache *cache, const gdb_byte *buf)
39366e63ce3Schristos {
39466e63ce3Schristos   if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
39566e63ce3Schristos     m32c_raw_write (reg->rx, cache, buf);
39666e63ce3Schristos   else
39766e63ce3Schristos     m32c_banked_write (reg, cache, buf);
39866e63ce3Schristos 
39966e63ce3Schristos   return REG_VALID;
40066e63ce3Schristos }
40166e63ce3Schristos 
40266e63ce3Schristos 
40366e63ce3Schristos /* Assuming REG uses m32c_part_read and m32c_part_write, set *OFFSET_P
40466e63ce3Schristos    and *LEN_P to the offset and length, in bytes, of the part REG
40566e63ce3Schristos    occupies in its underlying register.  The offset is from the
40666e63ce3Schristos    lower-addressed end, regardless of the architecture's endianness.
40766e63ce3Schristos    (The M32C family is always little-endian, but let's keep those
40866e63ce3Schristos    assumptions out of here.)  */
40966e63ce3Schristos static void
m32c_find_part(struct m32c_reg * reg,int * offset_p,int * len_p)41066e63ce3Schristos m32c_find_part (struct m32c_reg *reg, int *offset_p, int *len_p)
41166e63ce3Schristos {
41266e63ce3Schristos   /* The length of the containing register, of which REG is one part.  */
41366e63ce3Schristos   int containing_len = TYPE_LENGTH (reg->rx->type);
41466e63ce3Schristos 
41566e63ce3Schristos   /* The length of one "element" in our imaginary array.  */
41666e63ce3Schristos   int elt_len = TYPE_LENGTH (reg->type);
41766e63ce3Schristos 
41866e63ce3Schristos   /* The offset of REG's "element" from the least significant end of
41966e63ce3Schristos      the containing register.  */
42066e63ce3Schristos   int elt_offset = reg->n * elt_len;
42166e63ce3Schristos 
42266e63ce3Schristos   /* If we extend off the end, trim the length of the element.  */
42366e63ce3Schristos   if (elt_offset + elt_len > containing_len)
42466e63ce3Schristos     {
42566e63ce3Schristos       elt_len = containing_len - elt_offset;
42666e63ce3Schristos       /* We shouldn't be declaring partial registers that go off the
42766e63ce3Schristos 	 end of their containing registers.  */
42866e63ce3Schristos       gdb_assert (elt_len > 0);
42966e63ce3Schristos     }
43066e63ce3Schristos 
43166e63ce3Schristos   /* Flip the offset around if we're big-endian.  */
43266e63ce3Schristos   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
43366e63ce3Schristos     elt_offset = TYPE_LENGTH (reg->rx->type) - elt_offset - elt_len;
43466e63ce3Schristos 
43566e63ce3Schristos   *offset_p = elt_offset;
43666e63ce3Schristos   *len_p = elt_len;
43766e63ce3Schristos }
43866e63ce3Schristos 
43966e63ce3Schristos 
44066e63ce3Schristos /* Move the value of a partial register (r0h, intbl, etc.) from CACHE
44166e63ce3Schristos    to BUF.  Treating the value of the register REG->rx as an array of
44266e63ce3Schristos    REG->type values, where higher indices refer to more significant
44366e63ce3Schristos    bits, read the value of the REG->n'th element.  */
44466e63ce3Schristos static enum register_status
m32c_part_read(struct m32c_reg * reg,readable_regcache * cache,gdb_byte * buf)44507163879Schristos m32c_part_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
44666e63ce3Schristos {
44766e63ce3Schristos   int offset, len;
44866e63ce3Schristos 
44966e63ce3Schristos   memset (buf, 0, TYPE_LENGTH (reg->type));
45066e63ce3Schristos   m32c_find_part (reg, &offset, &len);
45107163879Schristos   return cache->cooked_read_part (reg->rx->num, offset, len, buf);
45266e63ce3Schristos }
45366e63ce3Schristos 
45466e63ce3Schristos 
45566e63ce3Schristos /* Move the value of a banked register from BUF to CACHE.
45666e63ce3Schristos    Treating the value of the register REG->rx as an array of REG->type
45766e63ce3Schristos    values, where higher indices refer to more significant bits, write
45866e63ce3Schristos    the value of the REG->n'th element.  */
45966e63ce3Schristos static enum register_status
m32c_part_write(struct m32c_reg * reg,struct regcache * cache,const gdb_byte * buf)460c03b94e9Schristos m32c_part_write (struct m32c_reg *reg, struct regcache *cache,
461c03b94e9Schristos 		 const gdb_byte *buf)
46266e63ce3Schristos {
46366e63ce3Schristos   int offset, len;
46466e63ce3Schristos 
46566e63ce3Schristos   m32c_find_part (reg, &offset, &len);
46607163879Schristos   cache->cooked_write_part (reg->rx->num, offset, len, buf);
46766e63ce3Schristos 
46866e63ce3Schristos   return REG_VALID;
46966e63ce3Schristos }
47066e63ce3Schristos 
47166e63ce3Schristos 
47266e63ce3Schristos /* Move the value of REG from CACHE to BUF.  REG's value is the
47366e63ce3Schristos    concatenation of the values of the registers REG->rx and REG->ry,
47466e63ce3Schristos    with REG->rx contributing the more significant bits.  */
47566e63ce3Schristos static enum register_status
m32c_cat_read(struct m32c_reg * reg,readable_regcache * cache,gdb_byte * buf)47607163879Schristos m32c_cat_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
47766e63ce3Schristos {
47866e63ce3Schristos   int high_bytes = TYPE_LENGTH (reg->rx->type);
47966e63ce3Schristos   int low_bytes  = TYPE_LENGTH (reg->ry->type);
48066e63ce3Schristos   enum register_status status;
48166e63ce3Schristos 
48266e63ce3Schristos   gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
48366e63ce3Schristos 
48466e63ce3Schristos   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
48566e63ce3Schristos     {
48607163879Schristos       status = cache->cooked_read (reg->rx->num, buf);
48766e63ce3Schristos       if (status == REG_VALID)
48807163879Schristos 	status = cache->cooked_read (reg->ry->num, buf + high_bytes);
48966e63ce3Schristos     }
49066e63ce3Schristos   else
49166e63ce3Schristos     {
49207163879Schristos       status = cache->cooked_read (reg->rx->num, buf + low_bytes);
49366e63ce3Schristos       if (status == REG_VALID)
49407163879Schristos 	status = cache->cooked_read (reg->ry->num, buf);
49566e63ce3Schristos     }
49666e63ce3Schristos   return status;
49766e63ce3Schristos }
49866e63ce3Schristos 
49966e63ce3Schristos 
50066e63ce3Schristos /* Move the value of REG from CACHE to BUF.  REG's value is the
50166e63ce3Schristos    concatenation of the values of the registers REG->rx and REG->ry,
50266e63ce3Schristos    with REG->rx contributing the more significant bits.  */
50366e63ce3Schristos static enum register_status
m32c_cat_write(struct m32c_reg * reg,struct regcache * cache,const gdb_byte * buf)504c03b94e9Schristos m32c_cat_write (struct m32c_reg *reg, struct regcache *cache,
505c03b94e9Schristos 		const gdb_byte *buf)
50666e63ce3Schristos {
50766e63ce3Schristos   int high_bytes = TYPE_LENGTH (reg->rx->type);
50866e63ce3Schristos   int low_bytes  = TYPE_LENGTH (reg->ry->type);
50966e63ce3Schristos 
51066e63ce3Schristos   gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
51166e63ce3Schristos 
51266e63ce3Schristos   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
51366e63ce3Schristos     {
51407163879Schristos       cache->cooked_write (reg->rx->num, buf);
51507163879Schristos       cache->cooked_write (reg->ry->num, buf + high_bytes);
51666e63ce3Schristos     }
51766e63ce3Schristos   else
51866e63ce3Schristos     {
51907163879Schristos       cache->cooked_write (reg->rx->num, buf + low_bytes);
52007163879Schristos       cache->cooked_write (reg->ry->num, buf);
52166e63ce3Schristos     }
52266e63ce3Schristos 
52366e63ce3Schristos   return REG_VALID;
52466e63ce3Schristos }
52566e63ce3Schristos 
52666e63ce3Schristos 
52766e63ce3Schristos /* Copy the value of the raw register REG from CACHE to BUF.  REG is
52866e63ce3Schristos    the concatenation (from most significant to least) of r3, r2, r1,
52966e63ce3Schristos    and r0.  */
53066e63ce3Schristos static enum register_status
m32c_r3r2r1r0_read(struct m32c_reg * reg,readable_regcache * cache,gdb_byte * buf)53107163879Schristos m32c_r3r2r1r0_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
53266e63ce3Schristos {
53366e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (reg->arch);
53466e63ce3Schristos   int len = TYPE_LENGTH (tdep->r0->type);
53566e63ce3Schristos   enum register_status status;
53666e63ce3Schristos 
53766e63ce3Schristos   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
53866e63ce3Schristos     {
53907163879Schristos       status = cache->cooked_read (tdep->r0->num, buf + len * 3);
54066e63ce3Schristos       if (status == REG_VALID)
54107163879Schristos 	status = cache->cooked_read (tdep->r1->num, buf + len * 2);
54266e63ce3Schristos       if (status == REG_VALID)
54307163879Schristos 	status = cache->cooked_read (tdep->r2->num, buf + len * 1);
54466e63ce3Schristos       if (status == REG_VALID)
54507163879Schristos 	status = cache->cooked_read (tdep->r3->num, buf);
54666e63ce3Schristos     }
54766e63ce3Schristos   else
54866e63ce3Schristos     {
54907163879Schristos       status = cache->cooked_read (tdep->r0->num, buf);
55066e63ce3Schristos       if (status == REG_VALID)
55107163879Schristos 	status = cache->cooked_read (tdep->r1->num, buf + len * 1);
55266e63ce3Schristos       if (status == REG_VALID)
55307163879Schristos 	status = cache->cooked_read (tdep->r2->num, buf + len * 2);
55466e63ce3Schristos       if (status == REG_VALID)
55507163879Schristos 	status = cache->cooked_read (tdep->r3->num, buf + len * 3);
55666e63ce3Schristos     }
55766e63ce3Schristos 
55866e63ce3Schristos   return status;
55966e63ce3Schristos }
56066e63ce3Schristos 
56166e63ce3Schristos 
56266e63ce3Schristos /* Copy the value of the raw register REG from BUF to CACHE.  REG is
56366e63ce3Schristos    the concatenation (from most significant to least) of r3, r2, r1,
56466e63ce3Schristos    and r0.  */
56566e63ce3Schristos static enum register_status
m32c_r3r2r1r0_write(struct m32c_reg * reg,struct regcache * cache,const gdb_byte * buf)566c03b94e9Schristos m32c_r3r2r1r0_write (struct m32c_reg *reg, struct regcache *cache,
567c03b94e9Schristos 		     const gdb_byte *buf)
56866e63ce3Schristos {
56966e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (reg->arch);
57066e63ce3Schristos   int len = TYPE_LENGTH (tdep->r0->type);
57166e63ce3Schristos 
57266e63ce3Schristos   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
57366e63ce3Schristos     {
57407163879Schristos       cache->cooked_write (tdep->r0->num, buf + len * 3);
57507163879Schristos       cache->cooked_write (tdep->r1->num, buf + len * 2);
57607163879Schristos       cache->cooked_write (tdep->r2->num, buf + len * 1);
57707163879Schristos       cache->cooked_write (tdep->r3->num, buf);
57866e63ce3Schristos     }
57966e63ce3Schristos   else
58066e63ce3Schristos     {
58107163879Schristos       cache->cooked_write (tdep->r0->num, buf);
58207163879Schristos       cache->cooked_write (tdep->r1->num, buf + len * 1);
58307163879Schristos       cache->cooked_write (tdep->r2->num, buf + len * 2);
58407163879Schristos       cache->cooked_write (tdep->r3->num, buf + len * 3);
58566e63ce3Schristos     }
58666e63ce3Schristos 
58766e63ce3Schristos   return REG_VALID;
58866e63ce3Schristos }
58966e63ce3Schristos 
59066e63ce3Schristos 
59166e63ce3Schristos static enum register_status
m32c_pseudo_register_read(struct gdbarch * arch,readable_regcache * cache,int cookednum,gdb_byte * buf)59266e63ce3Schristos m32c_pseudo_register_read (struct gdbarch *arch,
59307163879Schristos 			   readable_regcache *cache,
59466e63ce3Schristos 			   int cookednum,
59566e63ce3Schristos 			   gdb_byte *buf)
59666e63ce3Schristos {
59766e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
59866e63ce3Schristos   struct m32c_reg *reg;
59966e63ce3Schristos 
60066e63ce3Schristos   gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
60107163879Schristos   gdb_assert (arch == cache->arch ());
60266e63ce3Schristos   gdb_assert (arch == tdep->regs[cookednum].arch);
60366e63ce3Schristos   reg = &tdep->regs[cookednum];
60466e63ce3Schristos 
60566e63ce3Schristos   return reg->read (reg, cache, buf);
60666e63ce3Schristos }
60766e63ce3Schristos 
60866e63ce3Schristos 
60966e63ce3Schristos static void
m32c_pseudo_register_write(struct gdbarch * arch,struct regcache * cache,int cookednum,const gdb_byte * buf)61066e63ce3Schristos m32c_pseudo_register_write (struct gdbarch *arch,
61166e63ce3Schristos 			    struct regcache *cache,
61266e63ce3Schristos 			    int cookednum,
61366e63ce3Schristos 			    const gdb_byte *buf)
61466e63ce3Schristos {
61566e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
61666e63ce3Schristos   struct m32c_reg *reg;
61766e63ce3Schristos 
61866e63ce3Schristos   gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
61907163879Schristos   gdb_assert (arch == cache->arch ());
62066e63ce3Schristos   gdb_assert (arch == tdep->regs[cookednum].arch);
62166e63ce3Schristos   reg = &tdep->regs[cookednum];
62266e63ce3Schristos 
623c03b94e9Schristos   reg->write (reg, cache, buf);
62466e63ce3Schristos }
62566e63ce3Schristos 
62666e63ce3Schristos 
62766e63ce3Schristos /* Add a register with the given fields to the end of ARCH's table.
62866e63ce3Schristos    Return a pointer to the newly added register.  */
62966e63ce3Schristos static struct m32c_reg *
add_reg(struct gdbarch * arch,const char * name,struct type * type,int sim_num,m32c_read_reg_t * read,m32c_write_reg_t * write,struct m32c_reg * rx,struct m32c_reg * ry,int n)63066e63ce3Schristos add_reg (struct gdbarch *arch,
63166e63ce3Schristos 	 const char *name,
63266e63ce3Schristos 	 struct type *type,
63366e63ce3Schristos 	 int sim_num,
634c03b94e9Schristos 	 m32c_read_reg_t *read,
635c03b94e9Schristos 	 m32c_write_reg_t *write,
63666e63ce3Schristos 	 struct m32c_reg *rx,
63766e63ce3Schristos 	 struct m32c_reg *ry,
63866e63ce3Schristos 	 int n)
63966e63ce3Schristos {
64066e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
64166e63ce3Schristos   struct m32c_reg *r = &tdep->regs[tdep->num_regs];
64266e63ce3Schristos 
64366e63ce3Schristos   gdb_assert (tdep->num_regs < M32C_MAX_NUM_REGS);
64466e63ce3Schristos 
64566e63ce3Schristos   r->name           = name;
64666e63ce3Schristos   r->type           = type;
64766e63ce3Schristos   r->arch           = arch;
64866e63ce3Schristos   r->num            = tdep->num_regs;
64966e63ce3Schristos   r->sim_num        = sim_num;
65066e63ce3Schristos   r->dwarf_num      = -1;
65166e63ce3Schristos   r->general_p      = 0;
65266e63ce3Schristos   r->dma_p          = 0;
65366e63ce3Schristos   r->system_p       = 0;
65466e63ce3Schristos   r->save_restore_p = 0;
65566e63ce3Schristos   r->read           = read;
65666e63ce3Schristos   r->write          = write;
65766e63ce3Schristos   r->rx             = rx;
65866e63ce3Schristos   r->ry             = ry;
65966e63ce3Schristos   r->n              = n;
66066e63ce3Schristos 
66166e63ce3Schristos   tdep->num_regs++;
66266e63ce3Schristos 
66366e63ce3Schristos   return r;
66466e63ce3Schristos }
66566e63ce3Schristos 
66666e63ce3Schristos 
66766e63ce3Schristos /* Record NUM as REG's DWARF register number.  */
66866e63ce3Schristos static void
set_dwarf_regnum(struct m32c_reg * reg,int num)66966e63ce3Schristos set_dwarf_regnum (struct m32c_reg *reg, int num)
67066e63ce3Schristos {
67166e63ce3Schristos   gdb_assert (num < M32C_MAX_NUM_REGS);
67266e63ce3Schristos 
67366e63ce3Schristos   /* Update the reg->DWARF mapping.  Only count the first number
67466e63ce3Schristos      assigned to this register.  */
67566e63ce3Schristos   if (reg->dwarf_num == -1)
67666e63ce3Schristos     reg->dwarf_num = num;
67766e63ce3Schristos 
67866e63ce3Schristos   /* Update the DWARF->reg mapping.  */
67966e63ce3Schristos   gdbarch_tdep (reg->arch)->dwarf_regs[num] = reg;
68066e63ce3Schristos }
68166e63ce3Schristos 
68266e63ce3Schristos 
68366e63ce3Schristos /* Mark REG as a general-purpose register, and return it.  */
68466e63ce3Schristos static struct m32c_reg *
mark_general(struct m32c_reg * reg)68566e63ce3Schristos mark_general (struct m32c_reg *reg)
68666e63ce3Schristos {
68766e63ce3Schristos   reg->general_p = 1;
68866e63ce3Schristos   return reg;
68966e63ce3Schristos }
69066e63ce3Schristos 
69166e63ce3Schristos 
69207163879Schristos /* Mark REG as a DMA register.  */
69307163879Schristos static void
mark_dma(struct m32c_reg * reg)69466e63ce3Schristos mark_dma (struct m32c_reg *reg)
69566e63ce3Schristos {
69666e63ce3Schristos   reg->dma_p = 1;
69766e63ce3Schristos }
69866e63ce3Schristos 
69966e63ce3Schristos 
70066e63ce3Schristos /* Mark REG as a SYSTEM register, and return it.  */
70166e63ce3Schristos static struct m32c_reg *
mark_system(struct m32c_reg * reg)70266e63ce3Schristos mark_system (struct m32c_reg *reg)
70366e63ce3Schristos {
70466e63ce3Schristos   reg->system_p = 1;
70566e63ce3Schristos   return reg;
70666e63ce3Schristos }
70766e63ce3Schristos 
70866e63ce3Schristos 
70966e63ce3Schristos /* Mark REG as a save-restore register, and return it.  */
71066e63ce3Schristos static struct m32c_reg *
mark_save_restore(struct m32c_reg * reg)71166e63ce3Schristos mark_save_restore (struct m32c_reg *reg)
71266e63ce3Schristos {
71366e63ce3Schristos   reg->save_restore_p = 1;
71466e63ce3Schristos   return reg;
71566e63ce3Schristos }
71666e63ce3Schristos 
71766e63ce3Schristos 
71866e63ce3Schristos #define FLAGBIT_B	0x0010
71966e63ce3Schristos #define FLAGBIT_U	0x0080
72066e63ce3Schristos 
72166e63ce3Schristos /* Handy macros for declaring registers.  These all evaluate to
72266e63ce3Schristos    pointers to the register declared.  Macros that define two
72366e63ce3Schristos    registers evaluate to a pointer to the first.  */
72466e63ce3Schristos 
72566e63ce3Schristos /* A raw register named NAME, with type TYPE and sim number SIM_NUM.  */
72666e63ce3Schristos #define R(name, type, sim_num)					\
72766e63ce3Schristos   (add_reg (arch, (name), (type), (sim_num),			\
72866e63ce3Schristos 	    m32c_raw_read, m32c_raw_write, NULL, NULL, 0))
72966e63ce3Schristos 
73066e63ce3Schristos /* The simulator register number for a raw register named NAME.  */
73166e63ce3Schristos #define SIM(name) (m32c_sim_reg_ ## name)
73266e63ce3Schristos 
73366e63ce3Schristos /* A raw unsigned 16-bit data register named NAME.
73466e63ce3Schristos    NAME should be an identifier, not a string.  */
73566e63ce3Schristos #define R16U(name)						\
73666e63ce3Schristos   (R(#name, tdep->uint16, SIM (name)))
73766e63ce3Schristos 
73866e63ce3Schristos /* A raw data address register named NAME.
73966e63ce3Schristos    NAME should be an identifier, not a string.  */
74066e63ce3Schristos #define RA(name)						\
74166e63ce3Schristos   (R(#name, tdep->data_addr_reg_type, SIM (name)))
74266e63ce3Schristos 
74366e63ce3Schristos /* A raw code address register named NAME.  NAME should
74466e63ce3Schristos    be an identifier, not a string.  */
74566e63ce3Schristos #define RC(name)						\
74666e63ce3Schristos   (R(#name, tdep->code_addr_reg_type, SIM (name)))
74766e63ce3Schristos 
74866e63ce3Schristos /* A pair of raw registers named NAME0 and NAME1, with type TYPE.
74966e63ce3Schristos    NAME should be an identifier, not a string.  */
75066e63ce3Schristos #define RP(name, type)				\
75166e63ce3Schristos   (R(#name "0", (type), SIM (name ## 0)),	\
75266e63ce3Schristos    R(#name "1", (type), SIM (name ## 1)) - 1)
75366e63ce3Schristos 
75466e63ce3Schristos /* A raw banked general-purpose data register named NAME.
75566e63ce3Schristos    NAME should be an identifier, not a string.  */
75666e63ce3Schristos #define RBD(name)						\
75766e63ce3Schristos   (R(NULL, tdep->int16, SIM (name ## _bank0)),		\
75866e63ce3Schristos    R(NULL, tdep->int16, SIM (name ## _bank1)) - 1)
75966e63ce3Schristos 
76066e63ce3Schristos /* A raw banked data address register named NAME.
76166e63ce3Schristos    NAME should be an identifier, not a string.  */
76266e63ce3Schristos #define RBA(name)						\
76366e63ce3Schristos   (R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank0)),	\
76466e63ce3Schristos    R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank1)) - 1)
76566e63ce3Schristos 
76666e63ce3Schristos /* A cooked register named NAME referring to a raw banked register
76766e63ce3Schristos    from the bank selected by the current value of FLG.  RAW_PAIR
76866e63ce3Schristos    should be a pointer to the first register in the banked pair.
76966e63ce3Schristos    NAME must be an identifier, not a string.  */
77066e63ce3Schristos #define CB(name, raw_pair)				\
77166e63ce3Schristos   (add_reg (arch, #name, (raw_pair)->type, 0,		\
77266e63ce3Schristos 	    m32c_banked_read, m32c_banked_write,	\
77366e63ce3Schristos             (raw_pair), (raw_pair + 1), FLAGBIT_B))
77466e63ce3Schristos 
77566e63ce3Schristos /* A pair of registers named NAMEH and NAMEL, of type TYPE, that
77666e63ce3Schristos    access the top and bottom halves of the register pointed to by
77766e63ce3Schristos    NAME.  NAME should be an identifier.  */
77866e63ce3Schristos #define CHL(name, type)							\
77966e63ce3Schristos   (add_reg (arch, #name "h", (type), 0,					\
78066e63ce3Schristos 	    m32c_part_read, m32c_part_write, name, NULL, 1),		\
78166e63ce3Schristos    add_reg (arch, #name "l", (type), 0,					\
78266e63ce3Schristos 	    m32c_part_read, m32c_part_write, name, NULL, 0) - 1)
78366e63ce3Schristos 
78466e63ce3Schristos /* A register constructed by concatenating the two registers HIGH and
78566e63ce3Schristos    LOW, whose name is HIGHLOW and whose type is TYPE.  */
78666e63ce3Schristos #define CCAT(high, low, type)					\
78766e63ce3Schristos   (add_reg (arch, #high #low, (type), 0,			\
78866e63ce3Schristos 	    m32c_cat_read, m32c_cat_write, (high), (low), 0))
78966e63ce3Schristos 
79066e63ce3Schristos /* Abbreviations for marking register group membership.  */
79166e63ce3Schristos #define G(reg)   (mark_general (reg))
79266e63ce3Schristos #define S(reg)   (mark_system  (reg))
79366e63ce3Schristos #define DMA(reg) (mark_dma     (reg))
79466e63ce3Schristos 
79566e63ce3Schristos 
79666e63ce3Schristos /* Construct the register set for ARCH.  */
79766e63ce3Schristos static void
make_regs(struct gdbarch * arch)79866e63ce3Schristos make_regs (struct gdbarch *arch)
79966e63ce3Schristos {
80066e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
80166e63ce3Schristos   int mach = gdbarch_bfd_arch_info (arch)->mach;
80266e63ce3Schristos   int num_raw_regs;
80366e63ce3Schristos   int num_cooked_regs;
80466e63ce3Schristos 
80566e63ce3Schristos   struct m32c_reg *r0;
80666e63ce3Schristos   struct m32c_reg *r1;
80766e63ce3Schristos   struct m32c_reg *r2;
80866e63ce3Schristos   struct m32c_reg *r3;
80966e63ce3Schristos   struct m32c_reg *a0;
81066e63ce3Schristos   struct m32c_reg *a1;
81166e63ce3Schristos   struct m32c_reg *fb;
81266e63ce3Schristos   struct m32c_reg *sb;
81366e63ce3Schristos   struct m32c_reg *sp;
81466e63ce3Schristos   struct m32c_reg *r0hl;
81566e63ce3Schristos   struct m32c_reg *r1hl;
81666e63ce3Schristos   struct m32c_reg *r2r0;
81766e63ce3Schristos   struct m32c_reg *r3r1;
81866e63ce3Schristos   struct m32c_reg *r3r1r2r0;
81966e63ce3Schristos   struct m32c_reg *r3r2r1r0;
82066e63ce3Schristos   struct m32c_reg *a1a0;
82166e63ce3Schristos 
82266e63ce3Schristos   struct m32c_reg *raw_r0_pair = RBD (r0);
82366e63ce3Schristos   struct m32c_reg *raw_r1_pair = RBD (r1);
82466e63ce3Schristos   struct m32c_reg *raw_r2_pair = RBD (r2);
82566e63ce3Schristos   struct m32c_reg *raw_r3_pair = RBD (r3);
82666e63ce3Schristos   struct m32c_reg *raw_a0_pair = RBA (a0);
82766e63ce3Schristos   struct m32c_reg *raw_a1_pair = RBA (a1);
82866e63ce3Schristos   struct m32c_reg *raw_fb_pair = RBA (fb);
82966e63ce3Schristos 
83066e63ce3Schristos   /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
83166e63ce3Schristos      We always declare both raw registers, and deal with the distinction
83266e63ce3Schristos      in the pseudoregister.  */
83366e63ce3Schristos   struct m32c_reg *raw_sb_pair = RBA (sb);
83466e63ce3Schristos 
83566e63ce3Schristos   struct m32c_reg *usp         = S (RA (usp));
83666e63ce3Schristos   struct m32c_reg *isp         = S (RA (isp));
83766e63ce3Schristos   struct m32c_reg *intb        = S (RC (intb));
83866e63ce3Schristos   struct m32c_reg *pc          = G (RC (pc));
83966e63ce3Schristos   struct m32c_reg *flg         = G (R16U (flg));
84066e63ce3Schristos 
84166e63ce3Schristos   if (mach == bfd_mach_m32c)
84266e63ce3Schristos     {
84307163879Schristos       S (R16U (svf));
84407163879Schristos       S (RC (svp));
84507163879Schristos       S (RC (vct));
84666e63ce3Schristos 
84707163879Schristos       DMA (RP (dmd, tdep->uint8));
84807163879Schristos       DMA (RP (dct, tdep->uint16));
84907163879Schristos       DMA (RP (drc, tdep->uint16));
85007163879Schristos       DMA (RP (dma, tdep->data_addr_reg_type));
85107163879Schristos       DMA (RP (dsa, tdep->data_addr_reg_type));
85207163879Schristos       DMA (RP (dra, tdep->data_addr_reg_type));
85366e63ce3Schristos     }
85466e63ce3Schristos 
85566e63ce3Schristos   num_raw_regs = tdep->num_regs;
85666e63ce3Schristos 
85766e63ce3Schristos   r0 	      = G (CB (r0, raw_r0_pair));
85866e63ce3Schristos   r1 	      = G (CB (r1, raw_r1_pair));
85966e63ce3Schristos   r2          = G (CB (r2, raw_r2_pair));
86066e63ce3Schristos   r3          = G (CB (r3, raw_r3_pair));
86166e63ce3Schristos   a0          = G (CB (a0, raw_a0_pair));
86266e63ce3Schristos   a1          = G (CB (a1, raw_a1_pair));
86366e63ce3Schristos   fb          = G (CB (fb, raw_fb_pair));
86466e63ce3Schristos 
86566e63ce3Schristos   /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
86666e63ce3Schristos      Specify custom read/write functions that do the right thing.  */
86766e63ce3Schristos   sb          = G (add_reg (arch, "sb", raw_sb_pair->type, 0,
86866e63ce3Schristos 			    m32c_sb_read, m32c_sb_write,
86966e63ce3Schristos 			    raw_sb_pair, raw_sb_pair + 1, 0));
87066e63ce3Schristos 
87166e63ce3Schristos   /* The current sp is either usp or isp, depending on the value of
87266e63ce3Schristos      the FLG register's U bit.  */
87366e63ce3Schristos   sp          = G (add_reg (arch, "sp", usp->type, 0,
87466e63ce3Schristos 			    m32c_banked_read, m32c_banked_write,
87566e63ce3Schristos 			    isp, usp, FLAGBIT_U));
87666e63ce3Schristos 
87766e63ce3Schristos   r0hl        = CHL (r0, tdep->int8);
87866e63ce3Schristos   r1hl        = CHL (r1, tdep->int8);
879c03b94e9Schristos   CHL (r2, tdep->int8);
880c03b94e9Schristos   CHL (r3, tdep->int8);
881c03b94e9Schristos   CHL (intb, tdep->int16);
88266e63ce3Schristos 
88366e63ce3Schristos   r2r0        = CCAT (r2,   r0,   tdep->int32);
88466e63ce3Schristos   r3r1        = CCAT (r3,   r1,   tdep->int32);
88566e63ce3Schristos   r3r1r2r0    = CCAT (r3r1, r2r0, tdep->int64);
88666e63ce3Schristos 
88766e63ce3Schristos   r3r2r1r0
88866e63ce3Schristos     = add_reg (arch, "r3r2r1r0", tdep->int64, 0,
88966e63ce3Schristos 	       m32c_r3r2r1r0_read, m32c_r3r2r1r0_write, NULL, NULL, 0);
89066e63ce3Schristos 
89166e63ce3Schristos   if (mach == bfd_mach_m16c)
89266e63ce3Schristos     a1a0 = CCAT (a1, a0, tdep->int32);
89366e63ce3Schristos   else
89466e63ce3Schristos     a1a0 = NULL;
89566e63ce3Schristos 
89666e63ce3Schristos   num_cooked_regs = tdep->num_regs - num_raw_regs;
89766e63ce3Schristos 
89866e63ce3Schristos   tdep->pc   	 = pc;
89966e63ce3Schristos   tdep->flg  	 = flg;
90066e63ce3Schristos   tdep->r0   	 = r0;
90166e63ce3Schristos   tdep->r1   	 = r1;
90266e63ce3Schristos   tdep->r2   	 = r2;
90366e63ce3Schristos   tdep->r3   	 = r3;
90466e63ce3Schristos   tdep->r2r0 	 = r2r0;
90566e63ce3Schristos   tdep->r3r2r1r0 = r3r2r1r0;
90666e63ce3Schristos   tdep->r3r1r2r0 = r3r1r2r0;
90766e63ce3Schristos   tdep->a0       = a0;
90866e63ce3Schristos   tdep->a1       = a1;
90966e63ce3Schristos   tdep->sb       = sb;
91066e63ce3Schristos   tdep->fb   	 = fb;
91166e63ce3Schristos   tdep->sp   	 = sp;
91266e63ce3Schristos 
91366e63ce3Schristos   /* Set up the DWARF register table.  */
91466e63ce3Schristos   memset (tdep->dwarf_regs, 0, sizeof (tdep->dwarf_regs));
91566e63ce3Schristos   set_dwarf_regnum (r0hl + 1, 0x01);
91666e63ce3Schristos   set_dwarf_regnum (r0hl + 0, 0x02);
91766e63ce3Schristos   set_dwarf_regnum (r1hl + 1, 0x03);
91866e63ce3Schristos   set_dwarf_regnum (r1hl + 0, 0x04);
91966e63ce3Schristos   set_dwarf_regnum (r0,       0x05);
92066e63ce3Schristos   set_dwarf_regnum (r1,       0x06);
92166e63ce3Schristos   set_dwarf_regnum (r2,       0x07);
92266e63ce3Schristos   set_dwarf_regnum (r3,       0x08);
92366e63ce3Schristos   set_dwarf_regnum (a0,       0x09);
92466e63ce3Schristos   set_dwarf_regnum (a1,       0x0a);
92566e63ce3Schristos   set_dwarf_regnum (fb,       0x0b);
92666e63ce3Schristos   set_dwarf_regnum (sp,       0x0c);
92766e63ce3Schristos   set_dwarf_regnum (pc,       0x0d); /* GCC's invention */
92866e63ce3Schristos   set_dwarf_regnum (sb,       0x13);
92966e63ce3Schristos   set_dwarf_regnum (r2r0,     0x15);
93066e63ce3Schristos   set_dwarf_regnum (r3r1,     0x16);
93166e63ce3Schristos   if (a1a0)
93266e63ce3Schristos     set_dwarf_regnum (a1a0,   0x17);
93366e63ce3Schristos 
93466e63ce3Schristos   /* Enumerate the save/restore register group.
93566e63ce3Schristos 
93666e63ce3Schristos      The regcache_save and regcache_restore functions apply their read
93766e63ce3Schristos      function to each register in this group.
93866e63ce3Schristos 
93966e63ce3Schristos      Since frame_pop supplies frame_unwind_register as its read
94066e63ce3Schristos      function, the registers meaningful to the Dwarf unwinder need to
94166e63ce3Schristos      be in this group.
94266e63ce3Schristos 
94366e63ce3Schristos      On the other hand, when we make inferior calls, save_inferior_status
94466e63ce3Schristos      and restore_inferior_status use them to preserve the current register
94566e63ce3Schristos      values across the inferior call.  For this, you'd kind of like to
94666e63ce3Schristos      preserve all the raw registers, to protect the interrupted code from
94766e63ce3Schristos      any sort of bank switching the callee might have done.  But we handle
94866e63ce3Schristos      those cases so badly anyway --- for example, it matters whether we
94966e63ce3Schristos      restore FLG before or after we restore the general-purpose registers,
95066e63ce3Schristos      but there's no way to express that --- that it isn't worth worrying
95166e63ce3Schristos      about.
95266e63ce3Schristos 
95366e63ce3Schristos      We omit control registers like inthl: if you call a function that
95466e63ce3Schristos      changes those, it's probably because you wanted that change to be
95566e63ce3Schristos      visible to the interrupted code.  */
95666e63ce3Schristos   mark_save_restore (r0);
95766e63ce3Schristos   mark_save_restore (r1);
95866e63ce3Schristos   mark_save_restore (r2);
95966e63ce3Schristos   mark_save_restore (r3);
96066e63ce3Schristos   mark_save_restore (a0);
96166e63ce3Schristos   mark_save_restore (a1);
96266e63ce3Schristos   mark_save_restore (sb);
96366e63ce3Schristos   mark_save_restore (fb);
96466e63ce3Schristos   mark_save_restore (sp);
96566e63ce3Schristos   mark_save_restore (pc);
96666e63ce3Schristos   mark_save_restore (flg);
96766e63ce3Schristos 
96866e63ce3Schristos   set_gdbarch_num_regs (arch, num_raw_regs);
96966e63ce3Schristos   set_gdbarch_num_pseudo_regs (arch, num_cooked_regs);
97066e63ce3Schristos   set_gdbarch_pc_regnum (arch, pc->num);
97166e63ce3Schristos   set_gdbarch_sp_regnum (arch, sp->num);
97266e63ce3Schristos   set_gdbarch_register_name (arch, m32c_register_name);
97366e63ce3Schristos   set_gdbarch_register_type (arch, m32c_register_type);
97466e63ce3Schristos   set_gdbarch_pseudo_register_read (arch, m32c_pseudo_register_read);
97566e63ce3Schristos   set_gdbarch_pseudo_register_write (arch, m32c_pseudo_register_write);
97666e63ce3Schristos   set_gdbarch_register_sim_regno (arch, m32c_register_sim_regno);
97766e63ce3Schristos   set_gdbarch_stab_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
97866e63ce3Schristos   set_gdbarch_dwarf2_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
97966e63ce3Schristos   set_gdbarch_register_reggroup_p (arch, m32c_register_reggroup_p);
98066e63ce3Schristos 
98166e63ce3Schristos   reggroup_add (arch, general_reggroup);
98266e63ce3Schristos   reggroup_add (arch, all_reggroup);
98366e63ce3Schristos   reggroup_add (arch, save_reggroup);
98466e63ce3Schristos   reggroup_add (arch, restore_reggroup);
98566e63ce3Schristos   reggroup_add (arch, system_reggroup);
98666e63ce3Schristos   reggroup_add (arch, m32c_dma_reggroup);
98766e63ce3Schristos }
98866e63ce3Schristos 
98966e63ce3Schristos 
99066e63ce3Schristos 
99166e63ce3Schristos /* Breakpoints.  */
9921c468f90Schristos constexpr gdb_byte m32c_break_insn[] = { 0x00 };	/* brk */
99366e63ce3Schristos 
9941c468f90Schristos typedef BP_MANIPULATION (m32c_break_insn) m32c_breakpoint;
99566e63ce3Schristos 
99666e63ce3Schristos 
99766e63ce3Schristos /* Prologue analysis.  */
99866e63ce3Schristos 
999ed6a76a9Schristos enum m32c_prologue_kind
100066e63ce3Schristos {
100166e63ce3Schristos   /* This function uses a frame pointer.  */
100266e63ce3Schristos   prologue_with_frame_ptr,
100366e63ce3Schristos 
100466e63ce3Schristos   /* This function has no frame pointer.  */
100566e63ce3Schristos   prologue_sans_frame_ptr,
100666e63ce3Schristos 
100766e63ce3Schristos   /* This function sets up the stack, so its frame is the first
100866e63ce3Schristos      frame on the stack.  */
100966e63ce3Schristos   prologue_first_frame
1010ed6a76a9Schristos };
101166e63ce3Schristos 
1012ed6a76a9Schristos struct m32c_prologue
1013ed6a76a9Schristos {
1014ed6a76a9Schristos   /* For consistency with the DWARF 2 .debug_frame info generated by
1015ed6a76a9Schristos      GCC, a frame's CFA is the address immediately after the saved
1016ed6a76a9Schristos      return address.  */
1017ed6a76a9Schristos 
1018ed6a76a9Schristos   /* The architecture for which we generated this prologue info.  */
1019ed6a76a9Schristos   struct gdbarch *arch;
1020ed6a76a9Schristos 
1021ed6a76a9Schristos   enum m32c_prologue_kind kind;
102266e63ce3Schristos 
102366e63ce3Schristos   /* If KIND is prologue_with_frame_ptr, this is the offset from the
102466e63ce3Schristos      CFA to where the frame pointer points.  This is always zero or
102566e63ce3Schristos      negative.  */
102666e63ce3Schristos   LONGEST frame_ptr_offset;
102766e63ce3Schristos 
102866e63ce3Schristos   /* If KIND is prologue_sans_frame_ptr, the offset from the CFA to
102966e63ce3Schristos      the stack pointer --- always zero or negative.
103066e63ce3Schristos 
103166e63ce3Schristos      Calling this a "size" is a bit misleading, but given that the
103266e63ce3Schristos      stack grows downwards, using offsets for everything keeps one
103366e63ce3Schristos      from going completely sign-crazy: you never change anything's
103466e63ce3Schristos      sign for an ADD instruction; always change the second operand's
103566e63ce3Schristos      sign for a SUB instruction; and everything takes care of
103666e63ce3Schristos      itself.
103766e63ce3Schristos 
103866e63ce3Schristos      Functions that use alloca don't have a constant frame size.  But
103966e63ce3Schristos      they always have frame pointers, so we must use that to find the
104066e63ce3Schristos      CFA (and perhaps to unwind the stack pointer).  */
104166e63ce3Schristos   LONGEST frame_size;
104266e63ce3Schristos 
104366e63ce3Schristos   /* The address of the first instruction at which the frame has been
104466e63ce3Schristos      set up and the arguments are where the debug info says they are
104566e63ce3Schristos      --- as best as we can tell.  */
104666e63ce3Schristos   CORE_ADDR prologue_end;
104766e63ce3Schristos 
104866e63ce3Schristos   /* reg_offset[R] is the offset from the CFA at which register R is
104966e63ce3Schristos      saved, or 1 if register R has not been saved.  (Real values are
105066e63ce3Schristos      always zero or negative.)  */
105166e63ce3Schristos   LONGEST reg_offset[M32C_MAX_NUM_REGS];
105266e63ce3Schristos };
105366e63ce3Schristos 
105466e63ce3Schristos 
105566e63ce3Schristos /* The longest I've seen, anyway.  */
105666e63ce3Schristos #define M32C_MAX_INSN_LEN (9)
105766e63ce3Schristos 
105866e63ce3Schristos /* Processor state, for the prologue analyzer.  */
105966e63ce3Schristos struct m32c_pv_state
106066e63ce3Schristos {
106166e63ce3Schristos   struct gdbarch *arch;
106266e63ce3Schristos   pv_t r0, r1, r2, r3;
106366e63ce3Schristos   pv_t a0, a1;
106466e63ce3Schristos   pv_t sb, fb, sp;
106566e63ce3Schristos   pv_t pc;
106666e63ce3Schristos   struct pv_area *stack;
106766e63ce3Schristos 
106866e63ce3Schristos   /* Bytes from the current PC, the address they were read from,
106966e63ce3Schristos      and the address of the next unconsumed byte.  */
107066e63ce3Schristos   gdb_byte insn[M32C_MAX_INSN_LEN];
107166e63ce3Schristos   CORE_ADDR scan_pc, next_addr;
107266e63ce3Schristos };
107366e63ce3Schristos 
107466e63ce3Schristos 
107566e63ce3Schristos /* Push VALUE on STATE's stack, occupying SIZE bytes.  Return zero if
107666e63ce3Schristos    all went well, or non-zero if simulating the action would trash our
107766e63ce3Schristos    state.  */
107866e63ce3Schristos static int
m32c_pv_push(struct m32c_pv_state * state,pv_t value,int size)107966e63ce3Schristos m32c_pv_push (struct m32c_pv_state *state, pv_t value, int size)
108066e63ce3Schristos {
108107163879Schristos   if (state->stack->store_would_trash (state->sp))
108266e63ce3Schristos     return 1;
108366e63ce3Schristos 
108466e63ce3Schristos   state->sp = pv_add_constant (state->sp, -size);
108507163879Schristos   state->stack->store (state->sp, size, value);
108666e63ce3Schristos 
108766e63ce3Schristos   return 0;
108866e63ce3Schristos }
108966e63ce3Schristos 
109066e63ce3Schristos 
1091ed6a76a9Schristos enum srcdest_kind
1092ed6a76a9Schristos {
1093ed6a76a9Schristos   srcdest_reg,
1094ed6a76a9Schristos   srcdest_partial_reg,
1095ed6a76a9Schristos   srcdest_mem
1096ed6a76a9Schristos };
1097ed6a76a9Schristos 
109866e63ce3Schristos /* A source or destination location for an m16c or m32c
109966e63ce3Schristos    instruction.  */
110066e63ce3Schristos struct srcdest
110166e63ce3Schristos {
110266e63ce3Schristos   /* If srcdest_reg, the location is a register pointed to by REG.
110366e63ce3Schristos      If srcdest_partial_reg, the location is part of a register pointed
110466e63ce3Schristos      to by REG.  We don't try to handle this too well.
110566e63ce3Schristos      If srcdest_mem, the location is memory whose address is ADDR.  */
1106ed6a76a9Schristos   enum srcdest_kind kind;
110766e63ce3Schristos   pv_t *reg, addr;
110866e63ce3Schristos };
110966e63ce3Schristos 
111066e63ce3Schristos 
111166e63ce3Schristos /* Return the SIZE-byte value at LOC in STATE.  */
111266e63ce3Schristos static pv_t
m32c_srcdest_fetch(struct m32c_pv_state * state,struct srcdest loc,int size)111366e63ce3Schristos m32c_srcdest_fetch (struct m32c_pv_state *state, struct srcdest loc, int size)
111466e63ce3Schristos {
111566e63ce3Schristos   if (loc.kind == srcdest_mem)
111607163879Schristos     return state->stack->fetch (loc.addr, size);
111766e63ce3Schristos   else if (loc.kind == srcdest_partial_reg)
111866e63ce3Schristos     return pv_unknown ();
111966e63ce3Schristos   else
112066e63ce3Schristos     return *loc.reg;
112166e63ce3Schristos }
112266e63ce3Schristos 
112366e63ce3Schristos 
112466e63ce3Schristos /* Write VALUE, a SIZE-byte value, to LOC in STATE.  Return zero if
112566e63ce3Schristos    all went well, or non-zero if simulating the store would trash our
112666e63ce3Schristos    state.  */
112766e63ce3Schristos static int
m32c_srcdest_store(struct m32c_pv_state * state,struct srcdest loc,pv_t value,int size)112866e63ce3Schristos m32c_srcdest_store (struct m32c_pv_state *state, struct srcdest loc,
112966e63ce3Schristos 		    pv_t value, int size)
113066e63ce3Schristos {
113166e63ce3Schristos   if (loc.kind == srcdest_mem)
113266e63ce3Schristos     {
113307163879Schristos       if (state->stack->store_would_trash (loc.addr))
113466e63ce3Schristos 	return 1;
113507163879Schristos       state->stack->store (loc.addr, size, value);
113666e63ce3Schristos     }
113766e63ce3Schristos   else if (loc.kind == srcdest_partial_reg)
113866e63ce3Schristos     *loc.reg = pv_unknown ();
113966e63ce3Schristos   else
114066e63ce3Schristos     *loc.reg = value;
114166e63ce3Schristos 
114266e63ce3Schristos   return 0;
114366e63ce3Schristos }
114466e63ce3Schristos 
114566e63ce3Schristos 
114666e63ce3Schristos static int
m32c_sign_ext(int v,int bits)114766e63ce3Schristos m32c_sign_ext (int v, int bits)
114866e63ce3Schristos {
114966e63ce3Schristos   int mask = 1 << (bits - 1);
115066e63ce3Schristos   return (v ^ mask) - mask;
115166e63ce3Schristos }
115266e63ce3Schristos 
115366e63ce3Schristos static unsigned int
m32c_next_byte(struct m32c_pv_state * st)115466e63ce3Schristos m32c_next_byte (struct m32c_pv_state *st)
115566e63ce3Schristos {
115666e63ce3Schristos   gdb_assert (st->next_addr - st->scan_pc < sizeof (st->insn));
115766e63ce3Schristos   return st->insn[st->next_addr++ - st->scan_pc];
115866e63ce3Schristos }
115966e63ce3Schristos 
116066e63ce3Schristos static int
m32c_udisp8(struct m32c_pv_state * st)116166e63ce3Schristos m32c_udisp8 (struct m32c_pv_state *st)
116266e63ce3Schristos {
116366e63ce3Schristos   return m32c_next_byte (st);
116466e63ce3Schristos }
116566e63ce3Schristos 
116666e63ce3Schristos 
116766e63ce3Schristos static int
m32c_sdisp8(struct m32c_pv_state * st)116866e63ce3Schristos m32c_sdisp8 (struct m32c_pv_state *st)
116966e63ce3Schristos {
117066e63ce3Schristos   return m32c_sign_ext (m32c_next_byte (st), 8);
117166e63ce3Schristos }
117266e63ce3Schristos 
117366e63ce3Schristos 
117466e63ce3Schristos static int
m32c_udisp16(struct m32c_pv_state * st)117566e63ce3Schristos m32c_udisp16 (struct m32c_pv_state *st)
117666e63ce3Schristos {
117766e63ce3Schristos   int low  = m32c_next_byte (st);
117866e63ce3Schristos   int high = m32c_next_byte (st);
117966e63ce3Schristos 
118066e63ce3Schristos   return low + (high << 8);
118166e63ce3Schristos }
118266e63ce3Schristos 
118366e63ce3Schristos 
118466e63ce3Schristos static int
m32c_sdisp16(struct m32c_pv_state * st)118566e63ce3Schristos m32c_sdisp16 (struct m32c_pv_state *st)
118666e63ce3Schristos {
118766e63ce3Schristos   int low  = m32c_next_byte (st);
118866e63ce3Schristos   int high = m32c_next_byte (st);
118966e63ce3Schristos 
119066e63ce3Schristos   return m32c_sign_ext (low + (high << 8), 16);
119166e63ce3Schristos }
119266e63ce3Schristos 
119366e63ce3Schristos 
119466e63ce3Schristos static int
m32c_udisp24(struct m32c_pv_state * st)119566e63ce3Schristos m32c_udisp24 (struct m32c_pv_state *st)
119666e63ce3Schristos {
119766e63ce3Schristos   int low  = m32c_next_byte (st);
119866e63ce3Schristos   int mid  = m32c_next_byte (st);
119966e63ce3Schristos   int high = m32c_next_byte (st);
120066e63ce3Schristos 
120166e63ce3Schristos   return low + (mid << 8) + (high << 16);
120266e63ce3Schristos }
120366e63ce3Schristos 
120466e63ce3Schristos 
120566e63ce3Schristos /* Extract the 'source' field from an m32c MOV.size:G-format instruction.  */
120666e63ce3Schristos static int
m32c_get_src23(unsigned char * i)120766e63ce3Schristos m32c_get_src23 (unsigned char *i)
120866e63ce3Schristos {
120966e63ce3Schristos   return (((i[0] & 0x70) >> 2)
121066e63ce3Schristos 	  | ((i[1] & 0x30) >> 4));
121166e63ce3Schristos }
121266e63ce3Schristos 
121366e63ce3Schristos 
121466e63ce3Schristos /* Extract the 'dest' field from an m32c MOV.size:G-format instruction.  */
121566e63ce3Schristos static int
m32c_get_dest23(unsigned char * i)121666e63ce3Schristos m32c_get_dest23 (unsigned char *i)
121766e63ce3Schristos {
121866e63ce3Schristos   return (((i[0] & 0x0e) << 1)
121966e63ce3Schristos 	  | ((i[1] & 0xc0) >> 6));
122066e63ce3Schristos }
122166e63ce3Schristos 
122266e63ce3Schristos 
122366e63ce3Schristos static struct srcdest
m32c_decode_srcdest4(struct m32c_pv_state * st,int code,int size)122466e63ce3Schristos m32c_decode_srcdest4 (struct m32c_pv_state *st,
122566e63ce3Schristos 		      int code, int size)
122666e63ce3Schristos {
122766e63ce3Schristos   struct srcdest sd;
122866e63ce3Schristos 
122966e63ce3Schristos   if (code < 6)
123066e63ce3Schristos     sd.kind = (size == 2 ? srcdest_reg : srcdest_partial_reg);
123166e63ce3Schristos   else
123266e63ce3Schristos     sd.kind = srcdest_mem;
123366e63ce3Schristos 
123466e63ce3Schristos   sd.addr = pv_unknown ();
123566e63ce3Schristos   sd.reg = 0;
123666e63ce3Schristos 
123766e63ce3Schristos   switch (code)
123866e63ce3Schristos     {
123907163879Schristos     case 0x0: sd.reg = &st->r0; break;
124066e63ce3Schristos     case 0x1: sd.reg = (size == 1 ? &st->r0 : &st->r1); break;
124166e63ce3Schristos     case 0x2: sd.reg = (size == 1 ? &st->r1 : &st->r2); break;
124266e63ce3Schristos     case 0x3: sd.reg = (size == 1 ? &st->r1 : &st->r3); break;
124366e63ce3Schristos 
124466e63ce3Schristos     case 0x4: sd.reg = &st->a0; break;
124566e63ce3Schristos     case 0x5: sd.reg = &st->a1; break;
124666e63ce3Schristos 
124766e63ce3Schristos     case 0x6: sd.addr = st->a0; break;
124866e63ce3Schristos     case 0x7: sd.addr = st->a1; break;
124966e63ce3Schristos 
125066e63ce3Schristos     case 0x8: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
125166e63ce3Schristos     case 0x9: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
125266e63ce3Schristos     case 0xa: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
125366e63ce3Schristos     case 0xb: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
125466e63ce3Schristos 
125566e63ce3Schristos     case 0xc: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
125666e63ce3Schristos     case 0xd: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
125766e63ce3Schristos     case 0xe: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
125866e63ce3Schristos     case 0xf: sd.addr = pv_constant (m32c_udisp16 (st)); break;
125966e63ce3Schristos 
126066e63ce3Schristos     default:
126166e63ce3Schristos       gdb_assert_not_reached ("unexpected srcdest4");
126266e63ce3Schristos     }
126366e63ce3Schristos 
126466e63ce3Schristos   return sd;
126566e63ce3Schristos }
126666e63ce3Schristos 
126766e63ce3Schristos 
126866e63ce3Schristos static struct srcdest
m32c_decode_sd23(struct m32c_pv_state * st,int code,int size,int ind)126966e63ce3Schristos m32c_decode_sd23 (struct m32c_pv_state *st, int code, int size, int ind)
127066e63ce3Schristos {
127166e63ce3Schristos   struct srcdest sd;
127266e63ce3Schristos 
127366e63ce3Schristos   sd.addr = pv_unknown ();
127466e63ce3Schristos   sd.reg = 0;
127566e63ce3Schristos 
127666e63ce3Schristos   switch (code)
127766e63ce3Schristos     {
127866e63ce3Schristos     case 0x12:
127966e63ce3Schristos     case 0x13:
128066e63ce3Schristos     case 0x10:
128166e63ce3Schristos     case 0x11:
128266e63ce3Schristos       sd.kind = (size == 1) ? srcdest_partial_reg : srcdest_reg;
128366e63ce3Schristos       break;
128466e63ce3Schristos 
128566e63ce3Schristos     case 0x02:
128666e63ce3Schristos     case 0x03:
128766e63ce3Schristos       sd.kind = (size == 4) ? srcdest_reg : srcdest_partial_reg;
128866e63ce3Schristos       break;
128966e63ce3Schristos 
129066e63ce3Schristos     default:
129166e63ce3Schristos       sd.kind = srcdest_mem;
129266e63ce3Schristos       break;
129366e63ce3Schristos 
129466e63ce3Schristos     }
129566e63ce3Schristos 
129666e63ce3Schristos   switch (code)
129766e63ce3Schristos     {
129866e63ce3Schristos     case 0x12: sd.reg = &st->r0; break;
129966e63ce3Schristos     case 0x13: sd.reg = &st->r1; break;
130066e63ce3Schristos     case 0x10: sd.reg = ((size == 1) ? &st->r0 : &st->r2); break;
130166e63ce3Schristos     case 0x11: sd.reg = ((size == 1) ? &st->r1 : &st->r3); break;
130266e63ce3Schristos     case 0x02: sd.reg = &st->a0; break;
130366e63ce3Schristos     case 0x03: sd.reg = &st->a1; break;
130466e63ce3Schristos 
130566e63ce3Schristos     case 0x00: sd.addr = st->a0; break;
130666e63ce3Schristos     case 0x01: sd.addr = st->a1; break;
130766e63ce3Schristos     case 0x04: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
130866e63ce3Schristos     case 0x05: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
130966e63ce3Schristos     case 0x06: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
131066e63ce3Schristos     case 0x07: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
131166e63ce3Schristos     case 0x08: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
131266e63ce3Schristos     case 0x09: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
131366e63ce3Schristos     case 0x0a: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
131466e63ce3Schristos     case 0x0b: sd.addr = pv_add_constant (st->fb, m32c_sdisp16 (st)); break;
131566e63ce3Schristos     case 0x0c: sd.addr = pv_add_constant (st->a0, m32c_udisp24 (st)); break;
131666e63ce3Schristos     case 0x0d: sd.addr = pv_add_constant (st->a1, m32c_udisp24 (st)); break;
131766e63ce3Schristos     case 0x0f: sd.addr = pv_constant (m32c_udisp16 (st)); break;
131866e63ce3Schristos     case 0x0e: sd.addr = pv_constant (m32c_udisp24 (st)); break;
131966e63ce3Schristos     default:
132066e63ce3Schristos       gdb_assert_not_reached ("unexpected sd23");
132166e63ce3Schristos     }
132266e63ce3Schristos 
132366e63ce3Schristos   if (ind)
132466e63ce3Schristos     {
132566e63ce3Schristos       sd.addr = m32c_srcdest_fetch (st, sd, 4);
132666e63ce3Schristos       sd.kind = srcdest_mem;
132766e63ce3Schristos     }
132866e63ce3Schristos 
132966e63ce3Schristos   return sd;
133066e63ce3Schristos }
133166e63ce3Schristos 
133266e63ce3Schristos 
133366e63ce3Schristos /* The r16c and r32c machines have instructions with similar
133466e63ce3Schristos    semantics, but completely different machine language encodings.  So
133566e63ce3Schristos    we break out the semantics into their own functions, and leave
133666e63ce3Schristos    machine-specific decoding in m32c_analyze_prologue.
133766e63ce3Schristos 
133866e63ce3Schristos    The following functions all expect their arguments already decoded,
133966e63ce3Schristos    and they all return zero if analysis should continue past this
134066e63ce3Schristos    instruction, or non-zero if analysis should stop.  */
134166e63ce3Schristos 
134266e63ce3Schristos 
134366e63ce3Schristos /* Simulate an 'enter SIZE' instruction in STATE.  */
134466e63ce3Schristos static int
m32c_pv_enter(struct m32c_pv_state * state,int size)134566e63ce3Schristos m32c_pv_enter (struct m32c_pv_state *state, int size)
134666e63ce3Schristos {
134766e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
134866e63ce3Schristos 
134966e63ce3Schristos   /* If simulating this store would require us to forget
135066e63ce3Schristos      everything we know about the stack frame in the name of
135166e63ce3Schristos      accuracy, it would be better to just quit now.  */
135207163879Schristos   if (state->stack->store_would_trash (state->sp))
135366e63ce3Schristos     return 1;
135466e63ce3Schristos 
135566e63ce3Schristos   if (m32c_pv_push (state, state->fb, tdep->push_addr_bytes))
135666e63ce3Schristos     return 1;
135766e63ce3Schristos   state->fb = state->sp;
135866e63ce3Schristos   state->sp = pv_add_constant (state->sp, -size);
135966e63ce3Schristos 
136066e63ce3Schristos   return 0;
136166e63ce3Schristos }
136266e63ce3Schristos 
136366e63ce3Schristos 
136466e63ce3Schristos static int
m32c_pv_pushm_one(struct m32c_pv_state * state,pv_t reg,int bit,int src,int size)136566e63ce3Schristos m32c_pv_pushm_one (struct m32c_pv_state *state, pv_t reg,
136666e63ce3Schristos 		   int bit, int src, int size)
136766e63ce3Schristos {
136866e63ce3Schristos   if (bit & src)
136966e63ce3Schristos     {
137066e63ce3Schristos       if (m32c_pv_push (state, reg, size))
137166e63ce3Schristos 	return 1;
137266e63ce3Schristos     }
137366e63ce3Schristos 
137466e63ce3Schristos   return 0;
137566e63ce3Schristos }
137666e63ce3Schristos 
137766e63ce3Schristos 
137866e63ce3Schristos /* Simulate a 'pushm SRC' instruction in STATE.  */
137966e63ce3Schristos static int
m32c_pv_pushm(struct m32c_pv_state * state,int src)138066e63ce3Schristos m32c_pv_pushm (struct m32c_pv_state *state, int src)
138166e63ce3Schristos {
138266e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
138366e63ce3Schristos 
138466e63ce3Schristos   /* The bits in SRC indicating which registers to save are:
138566e63ce3Schristos      r0 r1 r2 r3 a0 a1 sb fb */
138666e63ce3Schristos   return
138766e63ce3Schristos     (   m32c_pv_pushm_one (state, state->fb, 0x01, src, tdep->push_addr_bytes)
138866e63ce3Schristos      || m32c_pv_pushm_one (state, state->sb, 0x02, src, tdep->push_addr_bytes)
138966e63ce3Schristos      || m32c_pv_pushm_one (state, state->a1, 0x04, src, tdep->push_addr_bytes)
139066e63ce3Schristos      || m32c_pv_pushm_one (state, state->a0, 0x08, src, tdep->push_addr_bytes)
139166e63ce3Schristos      || m32c_pv_pushm_one (state, state->r3, 0x10, src, 2)
139266e63ce3Schristos      || m32c_pv_pushm_one (state, state->r2, 0x20, src, 2)
139366e63ce3Schristos      || m32c_pv_pushm_one (state, state->r1, 0x40, src, 2)
139466e63ce3Schristos      || m32c_pv_pushm_one (state, state->r0, 0x80, src, 2));
139566e63ce3Schristos }
139666e63ce3Schristos 
139766e63ce3Schristos /* Return non-zero if VALUE is the first incoming argument register.  */
139866e63ce3Schristos 
139966e63ce3Schristos static int
m32c_is_1st_arg_reg(struct m32c_pv_state * state,pv_t value)140066e63ce3Schristos m32c_is_1st_arg_reg (struct m32c_pv_state *state, pv_t value)
140166e63ce3Schristos {
140266e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
140366e63ce3Schristos   return (value.kind == pvk_register
140466e63ce3Schristos           && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
140566e63ce3Schristos 	      ? (value.reg == tdep->r1->num)
140666e63ce3Schristos 	      : (value.reg == tdep->r0->num))
140766e63ce3Schristos           && value.k == 0);
140866e63ce3Schristos }
140966e63ce3Schristos 
141066e63ce3Schristos /* Return non-zero if VALUE is an incoming argument register.  */
141166e63ce3Schristos 
141266e63ce3Schristos static int
m32c_is_arg_reg(struct m32c_pv_state * state,pv_t value)141366e63ce3Schristos m32c_is_arg_reg (struct m32c_pv_state *state, pv_t value)
141466e63ce3Schristos {
141566e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (state->arch);
141666e63ce3Schristos   return (value.kind == pvk_register
141766e63ce3Schristos           && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
141866e63ce3Schristos 	      ? (value.reg == tdep->r1->num || value.reg == tdep->r2->num)
141966e63ce3Schristos 	      : (value.reg == tdep->r0->num))
142066e63ce3Schristos           && value.k == 0);
142166e63ce3Schristos }
142266e63ce3Schristos 
142366e63ce3Schristos /* Return non-zero if a store of VALUE to LOC is probably spilling an
142466e63ce3Schristos    argument register to its stack slot in STATE.  Such instructions
142566e63ce3Schristos    should be included in the prologue, if possible.
142666e63ce3Schristos 
142766e63ce3Schristos    The store is a spill if:
142866e63ce3Schristos    - the value being stored is the original value of an argument register;
142966e63ce3Schristos    - the value has not already been stored somewhere in STACK; and
143066e63ce3Schristos    - LOC is a stack slot (e.g., a memory location whose address is
143166e63ce3Schristos      relative to the original value of the SP).  */
143266e63ce3Schristos 
143366e63ce3Schristos static int
m32c_is_arg_spill(struct m32c_pv_state * st,struct srcdest loc,pv_t value)143466e63ce3Schristos m32c_is_arg_spill (struct m32c_pv_state *st,
143566e63ce3Schristos 		   struct srcdest loc,
143666e63ce3Schristos 		   pv_t value)
143766e63ce3Schristos {
143866e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);
143966e63ce3Schristos 
144066e63ce3Schristos   return (m32c_is_arg_reg (st, value)
144166e63ce3Schristos 	  && loc.kind == srcdest_mem
144266e63ce3Schristos           && pv_is_register (loc.addr, tdep->sp->num)
144307163879Schristos           && ! st->stack->find_reg (st->arch, value.reg, 0));
144466e63ce3Schristos }
144566e63ce3Schristos 
144666e63ce3Schristos /* Return non-zero if a store of VALUE to LOC is probably
144766e63ce3Schristos    copying the struct return address into an address register
144866e63ce3Schristos    for immediate use.  This is basically a "spill" into the
144966e63ce3Schristos    address register, instead of onto the stack.
145066e63ce3Schristos 
145166e63ce3Schristos    The prerequisites are:
145266e63ce3Schristos    - value being stored is original value of the FIRST arg register;
145366e63ce3Schristos    - value has not already been stored on stack; and
145466e63ce3Schristos    - LOC is an address register (a0 or a1).  */
145566e63ce3Schristos 
145666e63ce3Schristos static int
m32c_is_struct_return(struct m32c_pv_state * st,struct srcdest loc,pv_t value)145766e63ce3Schristos m32c_is_struct_return (struct m32c_pv_state *st,
145866e63ce3Schristos 		       struct srcdest loc,
145966e63ce3Schristos 		       pv_t value)
146066e63ce3Schristos {
146166e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);
146266e63ce3Schristos 
146366e63ce3Schristos   return (m32c_is_1st_arg_reg (st, value)
146407163879Schristos 	  && !st->stack->find_reg (st->arch, value.reg, 0)
146566e63ce3Schristos 	  && loc.kind == srcdest_reg
146666e63ce3Schristos 	  && (pv_is_register (*loc.reg, tdep->a0->num)
146766e63ce3Schristos 	      || pv_is_register (*loc.reg, tdep->a1->num)));
146866e63ce3Schristos }
146966e63ce3Schristos 
147066e63ce3Schristos /* Return non-zero if a 'pushm' saving the registers indicated by SRC
147166e63ce3Schristos    was a register save:
147266e63ce3Schristos    - all the named registers should have their original values, and
147366e63ce3Schristos    - the stack pointer should be at a constant offset from the
147466e63ce3Schristos      original stack pointer.  */
147566e63ce3Schristos static int
m32c_pushm_is_reg_save(struct m32c_pv_state * st,int src)147666e63ce3Schristos m32c_pushm_is_reg_save (struct m32c_pv_state *st, int src)
147766e63ce3Schristos {
147866e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (st->arch);
147966e63ce3Schristos   /* The bits in SRC indicating which registers to save are:
148066e63ce3Schristos      r0 r1 r2 r3 a0 a1 sb fb */
148166e63ce3Schristos   return
148266e63ce3Schristos     (pv_is_register (st->sp, tdep->sp->num)
148366e63ce3Schristos      && (! (src & 0x01) || pv_is_register_k (st->fb, tdep->fb->num, 0))
148466e63ce3Schristos      && (! (src & 0x02) || pv_is_register_k (st->sb, tdep->sb->num, 0))
148566e63ce3Schristos      && (! (src & 0x04) || pv_is_register_k (st->a1, tdep->a1->num, 0))
148666e63ce3Schristos      && (! (src & 0x08) || pv_is_register_k (st->a0, tdep->a0->num, 0))
148766e63ce3Schristos      && (! (src & 0x10) || pv_is_register_k (st->r3, tdep->r3->num, 0))
148866e63ce3Schristos      && (! (src & 0x20) || pv_is_register_k (st->r2, tdep->r2->num, 0))
148966e63ce3Schristos      && (! (src & 0x40) || pv_is_register_k (st->r1, tdep->r1->num, 0))
149066e63ce3Schristos      && (! (src & 0x80) || pv_is_register_k (st->r0, tdep->r0->num, 0)));
149166e63ce3Schristos }
149266e63ce3Schristos 
149366e63ce3Schristos 
149466e63ce3Schristos /* Function for finding saved registers in a 'struct pv_area'; we pass
149507163879Schristos    this to pv_area::scan.
149666e63ce3Schristos 
149766e63ce3Schristos    If VALUE is a saved register, ADDR says it was saved at a constant
149866e63ce3Schristos    offset from the frame base, and SIZE indicates that the whole
149966e63ce3Schristos    register was saved, record its offset in RESULT_UNTYPED.  */
150066e63ce3Schristos static void
check_for_saved(void * prologue_untyped,pv_t addr,CORE_ADDR size,pv_t value)150166e63ce3Schristos check_for_saved (void *prologue_untyped, pv_t addr, CORE_ADDR size, pv_t value)
150266e63ce3Schristos {
150366e63ce3Schristos   struct m32c_prologue *prologue = (struct m32c_prologue *) prologue_untyped;
150466e63ce3Schristos   struct gdbarch *arch = prologue->arch;
150566e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
150666e63ce3Schristos 
150766e63ce3Schristos   /* Is this the unchanged value of some register being saved on the
150866e63ce3Schristos      stack?  */
150966e63ce3Schristos   if (value.kind == pvk_register
151066e63ce3Schristos       && value.k == 0
151166e63ce3Schristos       && pv_is_register (addr, tdep->sp->num))
151266e63ce3Schristos     {
151366e63ce3Schristos       /* Some registers require special handling: they're saved as a
151466e63ce3Schristos 	 larger value than the register itself.  */
151566e63ce3Schristos       CORE_ADDR saved_size = register_size (arch, value.reg);
151666e63ce3Schristos 
151766e63ce3Schristos       if (value.reg == tdep->pc->num)
151866e63ce3Schristos 	saved_size = tdep->ret_addr_bytes;
151966e63ce3Schristos       else if (register_type (arch, value.reg)
152066e63ce3Schristos 	       == tdep->data_addr_reg_type)
152166e63ce3Schristos 	saved_size = tdep->push_addr_bytes;
152266e63ce3Schristos 
152366e63ce3Schristos       if (size == saved_size)
152466e63ce3Schristos 	{
152566e63ce3Schristos 	  /* Find which end of the saved value corresponds to our
152666e63ce3Schristos 	     register.  */
152766e63ce3Schristos 	  if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
152866e63ce3Schristos 	    prologue->reg_offset[value.reg]
152966e63ce3Schristos 	      = (addr.k + saved_size - register_size (arch, value.reg));
153066e63ce3Schristos 	  else
153166e63ce3Schristos 	    prologue->reg_offset[value.reg] = addr.k;
153266e63ce3Schristos 	}
153366e63ce3Schristos     }
153466e63ce3Schristos }
153566e63ce3Schristos 
153666e63ce3Schristos 
153766e63ce3Schristos /* Analyze the function prologue for ARCH at START, going no further
153866e63ce3Schristos    than LIMIT, and place a description of what we found in
153966e63ce3Schristos    PROLOGUE.  */
154066e63ce3Schristos static void
m32c_analyze_prologue(struct gdbarch * arch,CORE_ADDR start,CORE_ADDR limit,struct m32c_prologue * prologue)154166e63ce3Schristos m32c_analyze_prologue (struct gdbarch *arch,
154266e63ce3Schristos 		       CORE_ADDR start, CORE_ADDR limit,
154366e63ce3Schristos 		       struct m32c_prologue *prologue)
154466e63ce3Schristos {
154566e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
154666e63ce3Schristos   unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
154766e63ce3Schristos   CORE_ADDR after_last_frame_related_insn;
154866e63ce3Schristos   struct m32c_pv_state st;
154966e63ce3Schristos 
155066e63ce3Schristos   st.arch = arch;
155166e63ce3Schristos   st.r0 = pv_register (tdep->r0->num, 0);
155266e63ce3Schristos   st.r1 = pv_register (tdep->r1->num, 0);
155366e63ce3Schristos   st.r2 = pv_register (tdep->r2->num, 0);
155466e63ce3Schristos   st.r3 = pv_register (tdep->r3->num, 0);
155566e63ce3Schristos   st.a0 = pv_register (tdep->a0->num, 0);
155666e63ce3Schristos   st.a1 = pv_register (tdep->a1->num, 0);
155766e63ce3Schristos   st.sb = pv_register (tdep->sb->num, 0);
155866e63ce3Schristos   st.fb = pv_register (tdep->fb->num, 0);
155966e63ce3Schristos   st.sp = pv_register (tdep->sp->num, 0);
156066e63ce3Schristos   st.pc = pv_register (tdep->pc->num, 0);
156107163879Schristos   pv_area stack (tdep->sp->num, gdbarch_addr_bit (arch));
156207163879Schristos   st.stack = &stack;
156366e63ce3Schristos 
156466e63ce3Schristos   /* Record that the call instruction has saved the return address on
156566e63ce3Schristos      the stack.  */
156666e63ce3Schristos   m32c_pv_push (&st, st.pc, tdep->ret_addr_bytes);
156766e63ce3Schristos 
156866e63ce3Schristos   memset (prologue, 0, sizeof (*prologue));
156966e63ce3Schristos   prologue->arch = arch;
157066e63ce3Schristos   {
157166e63ce3Schristos     int i;
157266e63ce3Schristos     for (i = 0; i < M32C_MAX_NUM_REGS; i++)
157366e63ce3Schristos       prologue->reg_offset[i] = 1;
157466e63ce3Schristos   }
157566e63ce3Schristos 
157666e63ce3Schristos   st.scan_pc = after_last_frame_related_insn = start;
157766e63ce3Schristos 
157866e63ce3Schristos   while (st.scan_pc < limit)
157966e63ce3Schristos     {
158066e63ce3Schristos       pv_t pre_insn_fb = st.fb;
158166e63ce3Schristos       pv_t pre_insn_sp = st.sp;
158266e63ce3Schristos 
158366e63ce3Schristos       /* In theory we could get in trouble by trying to read ahead
158466e63ce3Schristos 	 here, when we only know we're expecting one byte.  In
158566e63ce3Schristos 	 practice I doubt anyone will care, and it makes the rest of
158666e63ce3Schristos 	 the code easier.  */
158766e63ce3Schristos       if (target_read_memory (st.scan_pc, st.insn, sizeof (st.insn)))
158866e63ce3Schristos 	/* If we can't fetch the instruction from memory, stop here
158966e63ce3Schristos 	   and hope for the best.  */
159066e63ce3Schristos 	break;
159166e63ce3Schristos       st.next_addr = st.scan_pc;
159266e63ce3Schristos 
159366e63ce3Schristos       /* The assembly instructions are written as they appear in the
159466e63ce3Schristos 	 section of the processor manuals that describe the
159566e63ce3Schristos 	 instruction encodings.
159666e63ce3Schristos 
159766e63ce3Schristos 	 When a single assembly language instruction has several
159866e63ce3Schristos 	 different machine-language encodings, the manual
159966e63ce3Schristos 	 distinguishes them by a number in parens, before the
160066e63ce3Schristos 	 mnemonic.  Those numbers are included, as well.
160166e63ce3Schristos 
160266e63ce3Schristos 	 The srcdest decoding instructions have the same names as the
160366e63ce3Schristos 	 analogous functions in the simulator.  */
160466e63ce3Schristos       if (mach == bfd_mach_m16c)
160566e63ce3Schristos 	{
160666e63ce3Schristos 	  /* (1) ENTER #imm8 */
160766e63ce3Schristos 	  if (st.insn[0] == 0x7c && st.insn[1] == 0xf2)
160866e63ce3Schristos 	    {
160966e63ce3Schristos 	      if (m32c_pv_enter (&st, st.insn[2]))
161066e63ce3Schristos 		break;
161166e63ce3Schristos 	      st.next_addr += 3;
161266e63ce3Schristos 	    }
161366e63ce3Schristos 	  /* (1) PUSHM src */
161466e63ce3Schristos 	  else if (st.insn[0] == 0xec)
161566e63ce3Schristos 	    {
161666e63ce3Schristos 	      int src = st.insn[1];
161766e63ce3Schristos 	      if (m32c_pv_pushm (&st, src))
161866e63ce3Schristos 		break;
161966e63ce3Schristos 	      st.next_addr += 2;
162066e63ce3Schristos 
162166e63ce3Schristos 	      if (m32c_pushm_is_reg_save (&st, src))
162266e63ce3Schristos 		after_last_frame_related_insn = st.next_addr;
162366e63ce3Schristos 	    }
162466e63ce3Schristos 
162566e63ce3Schristos 	  /* (6) MOV.size:G src, dest */
162666e63ce3Schristos 	  else if ((st.insn[0] & 0xfe) == 0x72)
162766e63ce3Schristos 	    {
162866e63ce3Schristos 	      int size = (st.insn[0] & 0x01) ? 2 : 1;
162966e63ce3Schristos 	      struct srcdest src;
163066e63ce3Schristos 	      struct srcdest dest;
163166e63ce3Schristos 	      pv_t src_value;
163266e63ce3Schristos 	      st.next_addr += 2;
163366e63ce3Schristos 
163466e63ce3Schristos 	      src
163566e63ce3Schristos 		= m32c_decode_srcdest4 (&st, (st.insn[1] >> 4) & 0xf, size);
163666e63ce3Schristos 	      dest
163766e63ce3Schristos 		= m32c_decode_srcdest4 (&st, st.insn[1] & 0xf, size);
163866e63ce3Schristos 	      src_value = m32c_srcdest_fetch (&st, src, size);
163966e63ce3Schristos 
164066e63ce3Schristos 	      if (m32c_is_arg_spill (&st, dest, src_value))
164166e63ce3Schristos 		after_last_frame_related_insn = st.next_addr;
164266e63ce3Schristos 	      else if (m32c_is_struct_return (&st, dest, src_value))
164366e63ce3Schristos 		after_last_frame_related_insn = st.next_addr;
164466e63ce3Schristos 
164566e63ce3Schristos 	      if (m32c_srcdest_store (&st, dest, src_value, size))
164666e63ce3Schristos 		break;
164766e63ce3Schristos 	    }
164866e63ce3Schristos 
164966e63ce3Schristos 	  /* (1) LDC #IMM16, sp */
165066e63ce3Schristos 	  else if (st.insn[0] == 0xeb
165166e63ce3Schristos 		   && st.insn[1] == 0x50)
165266e63ce3Schristos 	    {
165366e63ce3Schristos 	      st.next_addr += 2;
165466e63ce3Schristos 	      st.sp = pv_constant (m32c_udisp16 (&st));
165566e63ce3Schristos 	    }
165666e63ce3Schristos 
165766e63ce3Schristos 	  else
165866e63ce3Schristos 	    /* We've hit some instruction we don't know how to simulate.
165966e63ce3Schristos 	       Strictly speaking, we should set every value we're
166066e63ce3Schristos 	       tracking to "unknown".  But we'll be optimistic, assume
166166e63ce3Schristos 	       that we have enough information already, and stop
166266e63ce3Schristos 	       analysis here.  */
166366e63ce3Schristos 	    break;
166466e63ce3Schristos 	}
166566e63ce3Schristos       else
166666e63ce3Schristos 	{
166766e63ce3Schristos 	  int src_indirect = 0;
166866e63ce3Schristos 	  int dest_indirect = 0;
166966e63ce3Schristos 	  int i = 0;
167066e63ce3Schristos 
167166e63ce3Schristos 	  gdb_assert (mach == bfd_mach_m32c);
167266e63ce3Schristos 
167366e63ce3Schristos 	  /* Check for prefix bytes indicating indirect addressing.  */
167466e63ce3Schristos 	  if (st.insn[0] == 0x41)
167566e63ce3Schristos 	    {
167666e63ce3Schristos 	      src_indirect = 1;
167766e63ce3Schristos 	      i++;
167866e63ce3Schristos 	    }
167966e63ce3Schristos 	  else if (st.insn[0] == 0x09)
168066e63ce3Schristos 	    {
168166e63ce3Schristos 	      dest_indirect = 1;
168266e63ce3Schristos 	      i++;
168366e63ce3Schristos 	    }
168466e63ce3Schristos 	  else if (st.insn[0] == 0x49)
168566e63ce3Schristos 	    {
168666e63ce3Schristos 	      src_indirect = dest_indirect = 1;
168766e63ce3Schristos 	      i++;
168866e63ce3Schristos 	    }
168966e63ce3Schristos 
169066e63ce3Schristos 	  /* (1) ENTER #imm8 */
169166e63ce3Schristos 	  if (st.insn[i] == 0xec)
169266e63ce3Schristos 	    {
169366e63ce3Schristos 	      if (m32c_pv_enter (&st, st.insn[i + 1]))
169466e63ce3Schristos 		break;
169566e63ce3Schristos 	      st.next_addr += 2;
169666e63ce3Schristos 	    }
169766e63ce3Schristos 
169866e63ce3Schristos 	  /* (1) PUSHM src */
169966e63ce3Schristos 	  else if (st.insn[i] == 0x8f)
170066e63ce3Schristos 	    {
170166e63ce3Schristos 	      int src = st.insn[i + 1];
170266e63ce3Schristos 	      if (m32c_pv_pushm (&st, src))
170366e63ce3Schristos 		break;
170466e63ce3Schristos 	      st.next_addr += 2;
170566e63ce3Schristos 
170666e63ce3Schristos 	      if (m32c_pushm_is_reg_save (&st, src))
170766e63ce3Schristos 		after_last_frame_related_insn = st.next_addr;
170866e63ce3Schristos 	    }
170966e63ce3Schristos 
171066e63ce3Schristos 	  /* (7) MOV.size:G src, dest */
171166e63ce3Schristos 	  else if ((st.insn[i] & 0x80) == 0x80
171266e63ce3Schristos 		   && (st.insn[i + 1] & 0x0f) == 0x0b
171366e63ce3Schristos 		   && m32c_get_src23 (&st.insn[i]) < 20
171466e63ce3Schristos 		   && m32c_get_dest23 (&st.insn[i]) < 20)
171566e63ce3Schristos 	    {
171666e63ce3Schristos 	      struct srcdest src;
171766e63ce3Schristos 	      struct srcdest dest;
171866e63ce3Schristos 	      pv_t src_value;
171966e63ce3Schristos 	      int bw = st.insn[i] & 0x01;
172066e63ce3Schristos 	      int size = bw ? 2 : 1;
172166e63ce3Schristos 	      st.next_addr += 2;
172266e63ce3Schristos 
172366e63ce3Schristos 	      src
172466e63ce3Schristos 		= m32c_decode_sd23 (&st, m32c_get_src23 (&st.insn[i]),
172566e63ce3Schristos 				    size, src_indirect);
172666e63ce3Schristos 	      dest
172766e63ce3Schristos 		= m32c_decode_sd23 (&st, m32c_get_dest23 (&st.insn[i]),
172866e63ce3Schristos 				    size, dest_indirect);
172966e63ce3Schristos 	      src_value = m32c_srcdest_fetch (&st, src, size);
173066e63ce3Schristos 
173166e63ce3Schristos 	      if (m32c_is_arg_spill (&st, dest, src_value))
173266e63ce3Schristos 		after_last_frame_related_insn = st.next_addr;
173366e63ce3Schristos 
173466e63ce3Schristos 	      if (m32c_srcdest_store (&st, dest, src_value, size))
173566e63ce3Schristos 		break;
173666e63ce3Schristos 	    }
173766e63ce3Schristos 	  /* (2) LDC #IMM24, sp */
173866e63ce3Schristos 	  else if (st.insn[i] == 0xd5
173966e63ce3Schristos 		   && st.insn[i + 1] == 0x29)
174066e63ce3Schristos 	    {
174166e63ce3Schristos 	      st.next_addr += 2;
174266e63ce3Schristos 	      st.sp = pv_constant (m32c_udisp24 (&st));
174366e63ce3Schristos 	    }
174466e63ce3Schristos 	  else
174566e63ce3Schristos 	    /* We've hit some instruction we don't know how to simulate.
174666e63ce3Schristos 	       Strictly speaking, we should set every value we're
174766e63ce3Schristos 	       tracking to "unknown".  But we'll be optimistic, assume
174866e63ce3Schristos 	       that we have enough information already, and stop
174966e63ce3Schristos 	       analysis here.  */
175066e63ce3Schristos 	    break;
175166e63ce3Schristos 	}
175266e63ce3Schristos 
175366e63ce3Schristos       /* If this instruction changed the FB or decreased the SP (i.e.,
175466e63ce3Schristos          allocated more stack space), then this may be a good place to
175566e63ce3Schristos          declare the prologue finished.  However, there are some
175666e63ce3Schristos          exceptions:
175766e63ce3Schristos 
175866e63ce3Schristos          - If the instruction just changed the FB back to its original
175966e63ce3Schristos            value, then that's probably a restore instruction.  The
176066e63ce3Schristos            prologue should definitely end before that.
176166e63ce3Schristos 
176266e63ce3Schristos          - If the instruction increased the value of the SP (that is,
176366e63ce3Schristos            shrunk the frame), then it's probably part of a frame
176466e63ce3Schristos            teardown sequence, and the prologue should end before
176566e63ce3Schristos            that.  */
176666e63ce3Schristos 
176766e63ce3Schristos       if (! pv_is_identical (st.fb, pre_insn_fb))
176866e63ce3Schristos         {
176966e63ce3Schristos           if (! pv_is_register_k (st.fb, tdep->fb->num, 0))
177066e63ce3Schristos             after_last_frame_related_insn = st.next_addr;
177166e63ce3Schristos         }
177266e63ce3Schristos       else if (! pv_is_identical (st.sp, pre_insn_sp))
177366e63ce3Schristos         {
177466e63ce3Schristos           /* The comparison of the constants looks odd, there, because
177566e63ce3Schristos              .k is unsigned.  All it really means is that the SP is
177666e63ce3Schristos              lower than it was before the instruction.  */
177766e63ce3Schristos           if (   pv_is_register (pre_insn_sp, tdep->sp->num)
177866e63ce3Schristos               && pv_is_register (st.sp,       tdep->sp->num)
177966e63ce3Schristos               && ((pre_insn_sp.k - st.sp.k) < (st.sp.k - pre_insn_sp.k)))
178066e63ce3Schristos             after_last_frame_related_insn = st.next_addr;
178166e63ce3Schristos         }
178266e63ce3Schristos 
178366e63ce3Schristos       st.scan_pc = st.next_addr;
178466e63ce3Schristos     }
178566e63ce3Schristos 
178666e63ce3Schristos   /* Did we load a constant value into the stack pointer?  */
178766e63ce3Schristos   if (pv_is_constant (st.sp))
178866e63ce3Schristos     prologue->kind = prologue_first_frame;
178966e63ce3Schristos 
179066e63ce3Schristos   /* Alternatively, did we initialize the frame pointer?  Remember
179166e63ce3Schristos      that the CFA is the address after the return address.  */
179266e63ce3Schristos   if (pv_is_register (st.fb, tdep->sp->num))
179366e63ce3Schristos     {
179466e63ce3Schristos       prologue->kind = prologue_with_frame_ptr;
179566e63ce3Schristos       prologue->frame_ptr_offset = st.fb.k;
179666e63ce3Schristos     }
179766e63ce3Schristos 
179866e63ce3Schristos   /* Is the frame size a known constant?  Remember that frame_size is
179966e63ce3Schristos      actually the offset from the CFA to the SP (i.e., a negative
180066e63ce3Schristos      value).  */
180166e63ce3Schristos   else if (pv_is_register (st.sp, tdep->sp->num))
180266e63ce3Schristos     {
180366e63ce3Schristos       prologue->kind = prologue_sans_frame_ptr;
180466e63ce3Schristos       prologue->frame_size = st.sp.k;
180566e63ce3Schristos     }
180666e63ce3Schristos 
180766e63ce3Schristos   /* We haven't been able to make sense of this function's frame.  Treat
180866e63ce3Schristos      it as the first frame.  */
180966e63ce3Schristos   else
181066e63ce3Schristos     prologue->kind = prologue_first_frame;
181166e63ce3Schristos 
181266e63ce3Schristos   /* Record where all the registers were saved.  */
181307163879Schristos   st.stack->scan (check_for_saved, (void *) prologue);
181466e63ce3Schristos 
181566e63ce3Schristos   prologue->prologue_end = after_last_frame_related_insn;
181666e63ce3Schristos }
181766e63ce3Schristos 
181866e63ce3Schristos 
181966e63ce3Schristos static CORE_ADDR
m32c_skip_prologue(struct gdbarch * gdbarch,CORE_ADDR ip)182066e63ce3Schristos m32c_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR ip)
182166e63ce3Schristos {
182248596154Schristos   const char *name;
182366e63ce3Schristos   CORE_ADDR func_addr, func_end, sal_end;
182466e63ce3Schristos   struct m32c_prologue p;
182566e63ce3Schristos 
182666e63ce3Schristos   /* Try to find the extent of the function that contains IP.  */
182766e63ce3Schristos   if (! find_pc_partial_function (ip, &name, &func_addr, &func_end))
182866e63ce3Schristos     return ip;
182966e63ce3Schristos 
183066e63ce3Schristos   /* Find end by prologue analysis.  */
183166e63ce3Schristos   m32c_analyze_prologue (gdbarch, ip, func_end, &p);
183266e63ce3Schristos   /* Find end by line info.  */
183366e63ce3Schristos   sal_end = skip_prologue_using_sal (gdbarch, ip);
183466e63ce3Schristos   /* Return whichever is lower.  */
183566e63ce3Schristos   if (sal_end != 0 && sal_end != ip && sal_end < p.prologue_end)
183666e63ce3Schristos     return sal_end;
183766e63ce3Schristos   else
183866e63ce3Schristos     return p.prologue_end;
183966e63ce3Schristos }
184066e63ce3Schristos 
184166e63ce3Schristos 
184266e63ce3Schristos 
184366e63ce3Schristos /* Stack unwinding.  */
184466e63ce3Schristos 
184566e63ce3Schristos static struct m32c_prologue *
m32c_analyze_frame_prologue(struct frame_info * this_frame,void ** this_prologue_cache)184666e63ce3Schristos m32c_analyze_frame_prologue (struct frame_info *this_frame,
184766e63ce3Schristos 			     void **this_prologue_cache)
184866e63ce3Schristos {
184966e63ce3Schristos   if (! *this_prologue_cache)
185066e63ce3Schristos     {
185166e63ce3Schristos       CORE_ADDR func_start = get_frame_func (this_frame);
185266e63ce3Schristos       CORE_ADDR stop_addr = get_frame_pc (this_frame);
185366e63ce3Schristos 
185466e63ce3Schristos       /* If we couldn't find any function containing the PC, then
185566e63ce3Schristos          just initialize the prologue cache, but don't do anything.  */
185666e63ce3Schristos       if (! func_start)
185766e63ce3Schristos         stop_addr = func_start;
185866e63ce3Schristos 
185966e63ce3Schristos       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct m32c_prologue);
186066e63ce3Schristos       m32c_analyze_prologue (get_frame_arch (this_frame),
1861c03b94e9Schristos 			     func_start, stop_addr,
1862c03b94e9Schristos 			     (struct m32c_prologue *) *this_prologue_cache);
186366e63ce3Schristos     }
186466e63ce3Schristos 
1865c03b94e9Schristos   return (struct m32c_prologue *) *this_prologue_cache;
186666e63ce3Schristos }
186766e63ce3Schristos 
186866e63ce3Schristos 
186966e63ce3Schristos static CORE_ADDR
m32c_frame_base(struct frame_info * this_frame,void ** this_prologue_cache)187066e63ce3Schristos m32c_frame_base (struct frame_info *this_frame,
187166e63ce3Schristos                 void **this_prologue_cache)
187266e63ce3Schristos {
187366e63ce3Schristos   struct m32c_prologue *p
187466e63ce3Schristos     = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
187566e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
187666e63ce3Schristos 
187766e63ce3Schristos   /* In functions that use alloca, the distance between the stack
187866e63ce3Schristos      pointer and the frame base varies dynamically, so we can't use
187966e63ce3Schristos      the SP plus static information like prologue analysis to find the
188066e63ce3Schristos      frame base.  However, such functions must have a frame pointer,
188166e63ce3Schristos      to be able to restore the SP on exit.  So whenever we do have a
188266e63ce3Schristos      frame pointer, use that to find the base.  */
188366e63ce3Schristos   switch (p->kind)
188466e63ce3Schristos     {
188566e63ce3Schristos     case prologue_with_frame_ptr:
188666e63ce3Schristos       {
188766e63ce3Schristos 	CORE_ADDR fb
188866e63ce3Schristos 	  = get_frame_register_unsigned (this_frame, tdep->fb->num);
188966e63ce3Schristos 	return fb - p->frame_ptr_offset;
189066e63ce3Schristos       }
189166e63ce3Schristos 
189266e63ce3Schristos     case prologue_sans_frame_ptr:
189366e63ce3Schristos       {
189466e63ce3Schristos 	CORE_ADDR sp
189566e63ce3Schristos 	  = get_frame_register_unsigned (this_frame, tdep->sp->num);
189666e63ce3Schristos 	return sp - p->frame_size;
189766e63ce3Schristos       }
189866e63ce3Schristos 
189966e63ce3Schristos     case prologue_first_frame:
190066e63ce3Schristos       return 0;
190166e63ce3Schristos 
190266e63ce3Schristos     default:
190366e63ce3Schristos       gdb_assert_not_reached ("unexpected prologue kind");
190466e63ce3Schristos     }
190566e63ce3Schristos }
190666e63ce3Schristos 
190766e63ce3Schristos 
190866e63ce3Schristos static void
m32c_this_id(struct frame_info * this_frame,void ** this_prologue_cache,struct frame_id * this_id)190966e63ce3Schristos m32c_this_id (struct frame_info *this_frame,
191066e63ce3Schristos 	      void **this_prologue_cache,
191166e63ce3Schristos 	      struct frame_id *this_id)
191266e63ce3Schristos {
191366e63ce3Schristos   CORE_ADDR base = m32c_frame_base (this_frame, this_prologue_cache);
191466e63ce3Schristos 
191566e63ce3Schristos   if (base)
191666e63ce3Schristos     *this_id = frame_id_build (base, get_frame_func (this_frame));
191766e63ce3Schristos   /* Otherwise, leave it unset, and that will terminate the backtrace.  */
191866e63ce3Schristos }
191966e63ce3Schristos 
192066e63ce3Schristos 
192166e63ce3Schristos static struct value *
m32c_prev_register(struct frame_info * this_frame,void ** this_prologue_cache,int regnum)192266e63ce3Schristos m32c_prev_register (struct frame_info *this_frame,
192366e63ce3Schristos 		    void **this_prologue_cache, int regnum)
192466e63ce3Schristos {
192566e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
192666e63ce3Schristos   struct m32c_prologue *p
192766e63ce3Schristos     = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
192866e63ce3Schristos   CORE_ADDR frame_base = m32c_frame_base (this_frame, this_prologue_cache);
192966e63ce3Schristos 
193066e63ce3Schristos   if (regnum == tdep->sp->num)
193166e63ce3Schristos     return frame_unwind_got_constant (this_frame, regnum, frame_base);
193266e63ce3Schristos 
193366e63ce3Schristos   /* If prologue analysis says we saved this register somewhere,
193466e63ce3Schristos      return a description of the stack slot holding it.  */
193566e63ce3Schristos   if (p->reg_offset[regnum] != 1)
193666e63ce3Schristos     return frame_unwind_got_memory (this_frame, regnum,
193766e63ce3Schristos                                     frame_base + p->reg_offset[regnum]);
193866e63ce3Schristos 
193966e63ce3Schristos   /* Otherwise, presume we haven't changed the value of this
194066e63ce3Schristos      register, and get it from the next frame.  */
194166e63ce3Schristos   return frame_unwind_got_register (this_frame, regnum, regnum);
194266e63ce3Schristos }
194366e63ce3Schristos 
194466e63ce3Schristos 
194566e63ce3Schristos static const struct frame_unwind m32c_unwind = {
194666e63ce3Schristos   NORMAL_FRAME,
194766e63ce3Schristos   default_frame_unwind_stop_reason,
194866e63ce3Schristos   m32c_this_id,
194966e63ce3Schristos   m32c_prev_register,
195066e63ce3Schristos   NULL,
195166e63ce3Schristos   default_frame_sniffer
195266e63ce3Schristos };
195366e63ce3Schristos 
195466e63ce3Schristos 
195566e63ce3Schristos /* Inferior calls.  */
195666e63ce3Schristos 
195766e63ce3Schristos /* The calling conventions, according to GCC:
195866e63ce3Schristos 
195966e63ce3Schristos    r8c, m16c
196066e63ce3Schristos    ---------
196166e63ce3Schristos    First arg may be passed in r1l or r1 if it (1) fits (QImode or
196266e63ce3Schristos    HImode), (2) is named, and (3) is an integer or pointer type (no
196366e63ce3Schristos    structs, floats, etc).  Otherwise, it's passed on the stack.
196466e63ce3Schristos 
196566e63ce3Schristos    Second arg may be passed in r2, same restrictions (but not QImode),
196666e63ce3Schristos    even if the first arg is passed on the stack.
196766e63ce3Schristos 
196866e63ce3Schristos    Third and further args are passed on the stack.  No padding is
196966e63ce3Schristos    used, stack "alignment" is 8 bits.
197066e63ce3Schristos 
197166e63ce3Schristos    m32cm, m32c
197266e63ce3Schristos    -----------
197366e63ce3Schristos 
197466e63ce3Schristos    First arg may be passed in r0l or r0, same restrictions as above.
197566e63ce3Schristos 
197666e63ce3Schristos    Second and further args are passed on the stack.  Padding is used
197766e63ce3Schristos    after QImode parameters (i.e. lower-addressed byte is the value,
197866e63ce3Schristos    higher-addressed byte is the padding), stack "alignment" is 16
197966e63ce3Schristos    bits.  */
198066e63ce3Schristos 
198166e63ce3Schristos 
198266e63ce3Schristos /* Return true if TYPE is a type that can be passed in registers.  (We
198366e63ce3Schristos    ignore the size, and pay attention only to the type code;
198466e63ce3Schristos    acceptable sizes depends on which register is being considered to
198566e63ce3Schristos    hold it.)  */
198666e63ce3Schristos static int
m32c_reg_arg_type(struct type * type)198766e63ce3Schristos m32c_reg_arg_type (struct type *type)
198866e63ce3Schristos {
1989*1424dfb3Schristos   enum type_code code = type->code ();
199066e63ce3Schristos 
199166e63ce3Schristos   return (code == TYPE_CODE_INT
199266e63ce3Schristos 	  || code == TYPE_CODE_ENUM
199366e63ce3Schristos 	  || code == TYPE_CODE_PTR
19941c468f90Schristos 	  || TYPE_IS_REFERENCE (type)
199566e63ce3Schristos 	  || code == TYPE_CODE_BOOL
199666e63ce3Schristos 	  || code == TYPE_CODE_CHAR);
199766e63ce3Schristos }
199866e63ce3Schristos 
199966e63ce3Schristos 
200066e63ce3Schristos static CORE_ADDR
m32c_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,function_call_return_method return_method,CORE_ADDR struct_addr)200166e63ce3Schristos m32c_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
200266e63ce3Schristos 		      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
200307163879Schristos 		      struct value **args, CORE_ADDR sp,
200407163879Schristos 		      function_call_return_method return_method,
200566e63ce3Schristos 		      CORE_ADDR struct_addr)
200666e63ce3Schristos {
200766e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
200866e63ce3Schristos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
200966e63ce3Schristos   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
201066e63ce3Schristos   CORE_ADDR cfa;
201166e63ce3Schristos   int i;
201266e63ce3Schristos 
201366e63ce3Schristos   /* The number of arguments given in this function's prototype, or
201466e63ce3Schristos      zero if it has a non-prototyped function type.  The m32c ABI
201566e63ce3Schristos      passes arguments mentioned in the prototype differently from
201666e63ce3Schristos      those in the ellipsis of a varargs function, or from those passed
201766e63ce3Schristos      to a non-prototyped function.  */
201866e63ce3Schristos   int num_prototyped_args = 0;
201966e63ce3Schristos 
202066e63ce3Schristos   {
202166e63ce3Schristos     struct type *func_type = value_type (function);
202266e63ce3Schristos 
202366e63ce3Schristos     /* Dereference function pointer types.  */
2024*1424dfb3Schristos     if (func_type->code () == TYPE_CODE_PTR)
202566e63ce3Schristos       func_type = TYPE_TARGET_TYPE (func_type);
202666e63ce3Schristos 
2027*1424dfb3Schristos     gdb_assert (func_type->code () == TYPE_CODE_FUNC ||
2028*1424dfb3Schristos 		func_type->code () == TYPE_CODE_METHOD);
202966e63ce3Schristos 
203066e63ce3Schristos #if 0
203166e63ce3Schristos     /* The ABI description in gcc/config/m32c/m32c.abi says that
203266e63ce3Schristos        we need to handle prototyped and non-prototyped functions
203366e63ce3Schristos        separately, but the code in GCC doesn't actually do so.  */
203466e63ce3Schristos     if (TYPE_PROTOTYPED (func_type))
203566e63ce3Schristos #endif
2036*1424dfb3Schristos       num_prototyped_args = func_type->num_fields ();
203766e63ce3Schristos   }
203866e63ce3Schristos 
203966e63ce3Schristos   /* First, if the function returns an aggregate by value, push a
204066e63ce3Schristos      pointer to a buffer for it.  This doesn't affect the way
204166e63ce3Schristos      subsequent arguments are allocated to registers.  */
204207163879Schristos   if (return_method == return_method_struct)
204366e63ce3Schristos     {
204466e63ce3Schristos       int ptr_len = TYPE_LENGTH (tdep->ptr_voyd);
204566e63ce3Schristos       sp -= ptr_len;
204666e63ce3Schristos       write_memory_unsigned_integer (sp, ptr_len, byte_order, struct_addr);
204766e63ce3Schristos     }
204866e63ce3Schristos 
204966e63ce3Schristos   /* Push the arguments.  */
205066e63ce3Schristos   for (i = nargs - 1; i >= 0; i--)
205166e63ce3Schristos     {
205266e63ce3Schristos       struct value *arg = args[i];
205366e63ce3Schristos       const gdb_byte *arg_bits = value_contents (arg);
205466e63ce3Schristos       struct type *arg_type = value_type (arg);
205566e63ce3Schristos       ULONGEST arg_size = TYPE_LENGTH (arg_type);
205666e63ce3Schristos 
205766e63ce3Schristos       /* Can it go in r1 or r1l (for m16c) or r0 or r0l (for m32c)?  */
205866e63ce3Schristos       if (i == 0
205966e63ce3Schristos 	  && arg_size <= 2
206066e63ce3Schristos 	  && i < num_prototyped_args
206166e63ce3Schristos 	  && m32c_reg_arg_type (arg_type))
206266e63ce3Schristos 	{
206366e63ce3Schristos 	  /* Extract and re-store as an integer as a terse way to make
206466e63ce3Schristos 	     sure it ends up in the least significant end of r1.  (GDB
206566e63ce3Schristos 	     should avoid assuming endianness, even on uni-endian
206666e63ce3Schristos 	     processors.)  */
206766e63ce3Schristos 	  ULONGEST u = extract_unsigned_integer (arg_bits, arg_size,
206866e63ce3Schristos 						 byte_order);
206966e63ce3Schristos 	  struct m32c_reg *reg = (mach == bfd_mach_m16c) ? tdep->r1 : tdep->r0;
207066e63ce3Schristos 	  regcache_cooked_write_unsigned (regcache, reg->num, u);
207166e63ce3Schristos 	}
207266e63ce3Schristos 
207366e63ce3Schristos       /* Can it go in r2?  */
207466e63ce3Schristos       else if (mach == bfd_mach_m16c
207566e63ce3Schristos 	       && i == 1
207666e63ce3Schristos 	       && arg_size == 2
207766e63ce3Schristos 	       && i < num_prototyped_args
207866e63ce3Schristos 	       && m32c_reg_arg_type (arg_type))
207907163879Schristos 	regcache->cooked_write (tdep->r2->num, arg_bits);
208066e63ce3Schristos 
208166e63ce3Schristos       /* Everything else goes on the stack.  */
208266e63ce3Schristos       else
208366e63ce3Schristos 	{
208466e63ce3Schristos 	  sp -= arg_size;
208566e63ce3Schristos 
208666e63ce3Schristos 	  /* Align the stack.  */
208766e63ce3Schristos 	  if (mach == bfd_mach_m32c)
208866e63ce3Schristos 	    sp &= ~1;
208966e63ce3Schristos 
209066e63ce3Schristos 	  write_memory (sp, arg_bits, arg_size);
209166e63ce3Schristos 	}
209266e63ce3Schristos     }
209366e63ce3Schristos 
209466e63ce3Schristos   /* This is the CFA we use to identify the dummy frame.  */
209566e63ce3Schristos   cfa = sp;
209666e63ce3Schristos 
209766e63ce3Schristos   /* Push the return address.  */
209866e63ce3Schristos   sp -= tdep->ret_addr_bytes;
209966e63ce3Schristos   write_memory_unsigned_integer (sp, tdep->ret_addr_bytes, byte_order,
210066e63ce3Schristos 				 bp_addr);
210166e63ce3Schristos 
210266e63ce3Schristos   /* Update the stack pointer.  */
210366e63ce3Schristos   regcache_cooked_write_unsigned (regcache, tdep->sp->num, sp);
210466e63ce3Schristos 
210566e63ce3Schristos   /* We need to borrow an odd trick from the i386 target here.
210666e63ce3Schristos 
210766e63ce3Schristos      The value we return from this function gets used as the stack
210866e63ce3Schristos      address (the CFA) for the dummy frame's ID.  The obvious thing is
210966e63ce3Schristos      to return the new TOS.  However, that points at the return
211066e63ce3Schristos      address, saved on the stack, which is inconsistent with the CFA's
211166e63ce3Schristos      described by GCC's DWARF 2 .debug_frame information: DWARF 2
211266e63ce3Schristos      .debug_frame info uses the address immediately after the saved
211366e63ce3Schristos      return address.  So you end up with a dummy frame whose CFA
211466e63ce3Schristos      points at the return address, but the frame for the function
211566e63ce3Schristos      being called has a CFA pointing after the return address: the
211666e63ce3Schristos      younger CFA is *greater than* the older CFA.  The sanity checks
211766e63ce3Schristos      in frame.c don't like that.
211866e63ce3Schristos 
211966e63ce3Schristos      So we try to be consistent with the CFA's used by DWARF 2.
212066e63ce3Schristos      Having a dummy frame and a real frame with the *same* CFA is
212166e63ce3Schristos      tolerable.  */
212266e63ce3Schristos   return cfa;
212366e63ce3Schristos }
212466e63ce3Schristos 
212566e63ce3Schristos 
212666e63ce3Schristos 
212766e63ce3Schristos /* Return values.  */
212866e63ce3Schristos 
212966e63ce3Schristos /* Return value conventions, according to GCC:
213066e63ce3Schristos 
213166e63ce3Schristos    r8c, m16c
213266e63ce3Schristos    ---------
213366e63ce3Schristos 
213466e63ce3Schristos    QImode in r0l
213566e63ce3Schristos    HImode in r0
213666e63ce3Schristos    SImode in r2r0
213766e63ce3Schristos    near pointer in r0
213866e63ce3Schristos    far pointer in r2r0
213966e63ce3Schristos 
214066e63ce3Schristos    Aggregate values (regardless of size) are returned by pushing a
214166e63ce3Schristos    pointer to a temporary area on the stack after the args are pushed.
214266e63ce3Schristos    The function fills in this area with the value.  Note that this
214366e63ce3Schristos    pointer on the stack does not affect how register arguments, if any,
214466e63ce3Schristos    are configured.
214566e63ce3Schristos 
214666e63ce3Schristos    m32cm, m32c
214766e63ce3Schristos    -----------
214866e63ce3Schristos    Same.  */
214966e63ce3Schristos 
215066e63ce3Schristos /* Return non-zero if values of type TYPE are returned by storing them
215166e63ce3Schristos    in a buffer whose address is passed on the stack, ahead of the
215266e63ce3Schristos    other arguments.  */
215366e63ce3Schristos static int
m32c_return_by_passed_buf(struct type * type)215466e63ce3Schristos m32c_return_by_passed_buf (struct type *type)
215566e63ce3Schristos {
2156*1424dfb3Schristos   enum type_code code = type->code ();
215766e63ce3Schristos 
215866e63ce3Schristos   return (code == TYPE_CODE_STRUCT
215966e63ce3Schristos 	  || code == TYPE_CODE_UNION);
216066e63ce3Schristos }
216166e63ce3Schristos 
216266e63ce3Schristos static enum return_value_convention
m32c_return_value(struct gdbarch * gdbarch,struct value * function,struct type * valtype,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)216366e63ce3Schristos m32c_return_value (struct gdbarch *gdbarch,
216448596154Schristos 		   struct value *function,
216566e63ce3Schristos 		   struct type *valtype,
216666e63ce3Schristos 		   struct regcache *regcache,
216766e63ce3Schristos 		   gdb_byte *readbuf,
216866e63ce3Schristos 		   const gdb_byte *writebuf)
216966e63ce3Schristos {
217066e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
217166e63ce3Schristos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
217266e63ce3Schristos   enum return_value_convention conv;
217366e63ce3Schristos   ULONGEST valtype_len = TYPE_LENGTH (valtype);
217466e63ce3Schristos 
217566e63ce3Schristos   if (m32c_return_by_passed_buf (valtype))
217666e63ce3Schristos     conv = RETURN_VALUE_STRUCT_CONVENTION;
217766e63ce3Schristos   else
217866e63ce3Schristos     conv = RETURN_VALUE_REGISTER_CONVENTION;
217966e63ce3Schristos 
218066e63ce3Schristos   if (readbuf)
218166e63ce3Schristos     {
218266e63ce3Schristos       /* We should never be called to find values being returned by
218366e63ce3Schristos 	 RETURN_VALUE_STRUCT_CONVENTION.  Those can't be located,
218466e63ce3Schristos 	 unless we made the call ourselves.  */
218566e63ce3Schristos       gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);
218666e63ce3Schristos 
218766e63ce3Schristos       gdb_assert (valtype_len <= 8);
218866e63ce3Schristos 
218966e63ce3Schristos       /* Anything that fits in r0 is returned there.  */
219066e63ce3Schristos       if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
219166e63ce3Schristos 	{
219266e63ce3Schristos 	  ULONGEST u;
219366e63ce3Schristos 	  regcache_cooked_read_unsigned (regcache, tdep->r0->num, &u);
219466e63ce3Schristos 	  store_unsigned_integer (readbuf, valtype_len, byte_order, u);
219566e63ce3Schristos 	}
219666e63ce3Schristos       else
219766e63ce3Schristos 	{
219866e63ce3Schristos 	  /* Everything else is passed in mem0, using as many bytes as
219966e63ce3Schristos 	     needed.  This is not what the Renesas tools do, but it's
220066e63ce3Schristos 	     what GCC does at the moment.  */
220126a53354Schristos 	  struct bound_minimal_symbol mem0
220266e63ce3Schristos 	    = lookup_minimal_symbol ("mem0", NULL, NULL);
220366e63ce3Schristos 
220426a53354Schristos 	  if (! mem0.minsym)
220566e63ce3Schristos 	    error (_("The return value is stored in memory at 'mem0', "
220666e63ce3Schristos 		     "but GDB cannot find\n"
220766e63ce3Schristos 		     "its address."));
220826a53354Schristos 	  read_memory (BMSYMBOL_VALUE_ADDRESS (mem0), readbuf, valtype_len);
220966e63ce3Schristos 	}
221066e63ce3Schristos     }
221166e63ce3Schristos 
221266e63ce3Schristos   if (writebuf)
221366e63ce3Schristos     {
221466e63ce3Schristos       /* We should never be called to store values to be returned
221566e63ce3Schristos 	 using RETURN_VALUE_STRUCT_CONVENTION.  We have no way of
221666e63ce3Schristos 	 finding the buffer, unless we made the call ourselves.  */
221766e63ce3Schristos       gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);
221866e63ce3Schristos 
221966e63ce3Schristos       gdb_assert (valtype_len <= 8);
222066e63ce3Schristos 
222166e63ce3Schristos       /* Anything that fits in r0 is returned there.  */
222266e63ce3Schristos       if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
222366e63ce3Schristos 	{
222466e63ce3Schristos 	  ULONGEST u = extract_unsigned_integer (writebuf, valtype_len,
222566e63ce3Schristos 						 byte_order);
222666e63ce3Schristos 	  regcache_cooked_write_unsigned (regcache, tdep->r0->num, u);
222766e63ce3Schristos 	}
222866e63ce3Schristos       else
222966e63ce3Schristos 	{
223066e63ce3Schristos 	  /* Everything else is passed in mem0, using as many bytes as
223166e63ce3Schristos 	     needed.  This is not what the Renesas tools do, but it's
223266e63ce3Schristos 	     what GCC does at the moment.  */
223326a53354Schristos 	  struct bound_minimal_symbol mem0
223466e63ce3Schristos 	    = lookup_minimal_symbol ("mem0", NULL, NULL);
223566e63ce3Schristos 
223626a53354Schristos 	  if (! mem0.minsym)
223766e63ce3Schristos 	    error (_("The return value is stored in memory at 'mem0', "
223866e63ce3Schristos 		     "but GDB cannot find\n"
223966e63ce3Schristos 		     " its address."));
224026a53354Schristos 	  write_memory (BMSYMBOL_VALUE_ADDRESS (mem0), writebuf, valtype_len);
224166e63ce3Schristos 	}
224266e63ce3Schristos     }
224366e63ce3Schristos 
224466e63ce3Schristos   return conv;
224566e63ce3Schristos }
224666e63ce3Schristos 
224766e63ce3Schristos 
224866e63ce3Schristos 
224966e63ce3Schristos /* Trampolines.  */
225066e63ce3Schristos 
225166e63ce3Schristos /* The m16c and m32c use a trampoline function for indirect function
225266e63ce3Schristos    calls.  An indirect call looks like this:
225366e63ce3Schristos 
225466e63ce3Schristos 	     ... push arguments ...
225566e63ce3Schristos 	     ... push target function address ...
225666e63ce3Schristos 	     jsr.a m32c_jsri16
225766e63ce3Schristos 
225866e63ce3Schristos    The code for m32c_jsri16 looks like this:
225966e63ce3Schristos 
226066e63ce3Schristos      m32c_jsri16:
226166e63ce3Schristos 
226266e63ce3Schristos              # Save return address.
226366e63ce3Schristos 	     pop.w	m32c_jsri_ret
226466e63ce3Schristos 	     pop.b	m32c_jsri_ret+2
226566e63ce3Schristos 
226666e63ce3Schristos              # Store target function address.
226766e63ce3Schristos 	     pop.w	m32c_jsri_addr
226866e63ce3Schristos 
226966e63ce3Schristos 	     # Re-push return address.
227066e63ce3Schristos 	     push.b	m32c_jsri_ret+2
227166e63ce3Schristos 	     push.w	m32c_jsri_ret
227266e63ce3Schristos 
227366e63ce3Schristos 	     # Call the target function.
227466e63ce3Schristos 	     jmpi.a	m32c_jsri_addr
227566e63ce3Schristos 
227666e63ce3Schristos    Without further information, GDB will treat calls to m32c_jsri16
227766e63ce3Schristos    like calls to any other function.  Since m32c_jsri16 doesn't have
227866e63ce3Schristos    debugging information, that normally means that GDB sets a step-
227966e63ce3Schristos    resume breakpoint and lets the program continue --- which is not
228066e63ce3Schristos    what the user wanted.  (Giving the trampoline debugging info
228166e63ce3Schristos    doesn't help: the user expects the program to stop in the function
228266e63ce3Schristos    their program is calling, not in some trampoline code they've never
228366e63ce3Schristos    seen before.)
228466e63ce3Schristos 
228566e63ce3Schristos    The gdbarch_skip_trampoline_code method tells GDB how to step
228666e63ce3Schristos    through such trampoline functions transparently to the user.  When
228766e63ce3Schristos    given the address of a trampoline function's first instruction,
228866e63ce3Schristos    gdbarch_skip_trampoline_code should return the address of the first
228966e63ce3Schristos    instruction of the function really being called.  If GDB decides it
229066e63ce3Schristos    wants to step into that function, it will set a breakpoint there
229166e63ce3Schristos    and silently continue to it.
229266e63ce3Schristos 
229366e63ce3Schristos    We recognize the trampoline by name, and extract the target address
229466e63ce3Schristos    directly from the stack.  This isn't great, but recognizing by its
229566e63ce3Schristos    code sequence seems more fragile.  */
229666e63ce3Schristos 
229766e63ce3Schristos static CORE_ADDR
m32c_skip_trampoline_code(struct frame_info * frame,CORE_ADDR stop_pc)229866e63ce3Schristos m32c_skip_trampoline_code (struct frame_info *frame, CORE_ADDR stop_pc)
229966e63ce3Schristos {
230066e63ce3Schristos   struct gdbarch *gdbarch = get_frame_arch (frame);
230166e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
230266e63ce3Schristos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
230366e63ce3Schristos 
230466e63ce3Schristos   /* It would be nicer to simply look up the addresses of known
230566e63ce3Schristos      trampolines once, and then compare stop_pc with them.  However,
230666e63ce3Schristos      we'd need to ensure that that cached address got invalidated when
230766e63ce3Schristos      someone loaded a new executable, and I'm not quite sure of the
230866e63ce3Schristos      best way to do that.  find_pc_partial_function does do some
230966e63ce3Schristos      caching, so we'll see how this goes.  */
231048596154Schristos   const char *name;
231166e63ce3Schristos   CORE_ADDR start, end;
231266e63ce3Schristos 
231366e63ce3Schristos   if (find_pc_partial_function (stop_pc, &name, &start, &end))
231466e63ce3Schristos     {
231566e63ce3Schristos       /* Are we stopped at the beginning of the trampoline function?  */
231666e63ce3Schristos       if (strcmp (name, "m32c_jsri16") == 0
231766e63ce3Schristos 	  && stop_pc == start)
231866e63ce3Schristos 	{
231966e63ce3Schristos 	  /* Get the stack pointer.  The return address is at the top,
232066e63ce3Schristos 	     and the target function's address is just below that.  We
232166e63ce3Schristos 	     know it's a two-byte address, since the trampoline is
232266e63ce3Schristos 	     m32c_jsri*16*.  */
232366e63ce3Schristos 	  CORE_ADDR sp = get_frame_sp (get_current_frame ());
232466e63ce3Schristos 	  CORE_ADDR target
232566e63ce3Schristos 	    = read_memory_unsigned_integer (sp + tdep->ret_addr_bytes,
232666e63ce3Schristos 					    2, byte_order);
232766e63ce3Schristos 
232866e63ce3Schristos 	  /* What we have now is the address of a jump instruction.
232966e63ce3Schristos 	     What we need is the destination of that jump.
233066e63ce3Schristos 	     The opcode is 1 byte, and the destination is the next 3 bytes.  */
233166e63ce3Schristos 
233266e63ce3Schristos 	  target = read_memory_unsigned_integer (target + 1, 3, byte_order);
233366e63ce3Schristos 	  return target;
233466e63ce3Schristos 	}
233566e63ce3Schristos     }
233666e63ce3Schristos 
233766e63ce3Schristos   return 0;
233866e63ce3Schristos }
233966e63ce3Schristos 
234066e63ce3Schristos 
234166e63ce3Schristos /* Address/pointer conversions.  */
234266e63ce3Schristos 
234366e63ce3Schristos /* On the m16c, there is a 24-bit address space, but only a very few
234466e63ce3Schristos    instructions can generate addresses larger than 0xffff: jumps,
234566e63ce3Schristos    jumps to subroutines, and the lde/std (load/store extended)
234666e63ce3Schristos    instructions.
234766e63ce3Schristos 
234866e63ce3Schristos    Since GCC can only support one size of pointer, we can't have
234966e63ce3Schristos    distinct 'near' and 'far' pointer types; we have to pick one size
235066e63ce3Schristos    for everything.  If we wanted to use 24-bit pointers, then GCC
235166e63ce3Schristos    would have to use lde and ste for all memory references, which
235266e63ce3Schristos    would be terrible for performance and code size.  So the GNU
235366e63ce3Schristos    toolchain uses 16-bit pointers for everything, and gives up the
235466e63ce3Schristos    ability to have pointers point outside the first 64k of memory.
235566e63ce3Schristos 
235666e63ce3Schristos    However, as a special hack, we let the linker place functions at
235766e63ce3Schristos    addresses above 0xffff, as long as it also places a trampoline in
235866e63ce3Schristos    the low 64k for every function whose address is taken.  Each
235966e63ce3Schristos    trampoline consists of a single jmp.a instruction that jumps to the
236066e63ce3Schristos    function's real entry point.  Pointers to functions can be 16 bits
236166e63ce3Schristos    long, even though the functions themselves are at higher addresses:
236266e63ce3Schristos    the pointers refer to the trampolines, not the functions.
236366e63ce3Schristos 
236466e63ce3Schristos    This complicates things for GDB, however: given the address of a
236566e63ce3Schristos    function (from debug info or linker symbols, say) which could be
236666e63ce3Schristos    anywhere in the 24-bit address space, how can we find an
236766e63ce3Schristos    appropriate 16-bit value to use as a pointer to it?
236866e63ce3Schristos 
236966e63ce3Schristos    If the linker has not generated a trampoline for the function,
237066e63ce3Schristos    we're out of luck.  Well, I guess we could malloc some space and
237166e63ce3Schristos    write a jmp.a instruction to it, but I'm not going to get into that
237266e63ce3Schristos    at the moment.
237366e63ce3Schristos 
237466e63ce3Schristos    If the linker has generated a trampoline for the function, then it
237566e63ce3Schristos    also emitted a symbol for the trampoline: if the function's linker
237666e63ce3Schristos    symbol is named NAME, then the function's trampoline's linker
237766e63ce3Schristos    symbol is named NAME.plt.
237866e63ce3Schristos 
237966e63ce3Schristos    So, given a code address:
238066e63ce3Schristos    - We try to find a linker symbol at that address.
238166e63ce3Schristos    - If we find such a symbol named NAME, we look for a linker symbol
238266e63ce3Schristos      named NAME.plt.
238366e63ce3Schristos    - If we find such a symbol, we assume it is a trampoline, and use
238466e63ce3Schristos      its address as the pointer value.
238566e63ce3Schristos 
238666e63ce3Schristos    And, given a function pointer:
238766e63ce3Schristos    - We try to find a linker symbol at that address named NAME.plt.
238866e63ce3Schristos    - If we find such a symbol, we look for a linker symbol named NAME.
238966e63ce3Schristos    - If we find that, we provide that as the function's address.
239066e63ce3Schristos    - If any of the above steps fail, we return the original address
239166e63ce3Schristos      unchanged; it might really be a function in the low 64k.
239266e63ce3Schristos 
239366e63ce3Schristos    See?  You *knew* there was a reason you wanted to be a computer
239466e63ce3Schristos    programmer!  :)  */
239566e63ce3Schristos 
239666e63ce3Schristos static void
m32c_m16c_address_to_pointer(struct gdbarch * gdbarch,struct type * type,gdb_byte * buf,CORE_ADDR addr)239766e63ce3Schristos m32c_m16c_address_to_pointer (struct gdbarch *gdbarch,
239866e63ce3Schristos 			      struct type *type, gdb_byte *buf, CORE_ADDR addr)
239966e63ce3Schristos {
240066e63ce3Schristos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
240166e63ce3Schristos   enum type_code target_code;
2402*1424dfb3Schristos   gdb_assert (type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type));
240366e63ce3Schristos 
2404*1424dfb3Schristos   target_code = TYPE_TARGET_TYPE (type)->code ();
240566e63ce3Schristos 
240666e63ce3Schristos   if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
240766e63ce3Schristos     {
240848596154Schristos       const char *func_name;
240966e63ce3Schristos       char *tramp_name;
241026a53354Schristos       struct bound_minimal_symbol tramp_msym;
241166e63ce3Schristos 
241266e63ce3Schristos       /* Try to find a linker symbol at this address.  */
24137af5a897Schristos       struct bound_minimal_symbol func_msym
24147af5a897Schristos 	= lookup_minimal_symbol_by_pc (addr);
241566e63ce3Schristos 
24167af5a897Schristos       if (! func_msym.minsym)
241766e63ce3Schristos         error (_("Cannot convert code address %s to function pointer:\n"
241866e63ce3Schristos                "couldn't find a symbol at that address, to find trampoline."),
241966e63ce3Schristos                paddress (gdbarch, addr));
242066e63ce3Schristos 
2421*1424dfb3Schristos       func_name = func_msym.minsym->linkage_name ();
2422c03b94e9Schristos       tramp_name = (char *) xmalloc (strlen (func_name) + 5);
242366e63ce3Schristos       strcpy (tramp_name, func_name);
242466e63ce3Schristos       strcat (tramp_name, ".plt");
242566e63ce3Schristos 
242666e63ce3Schristos       /* Try to find a linker symbol for the trampoline.  */
242766e63ce3Schristos       tramp_msym = lookup_minimal_symbol (tramp_name, NULL, NULL);
242866e63ce3Schristos 
242966e63ce3Schristos       /* We've either got another copy of the name now, or don't need
243066e63ce3Schristos          the name any more.  */
243166e63ce3Schristos       xfree (tramp_name);
243266e63ce3Schristos 
243326a53354Schristos       if (! tramp_msym.minsym)
243466e63ce3Schristos 	{
243566e63ce3Schristos 	  CORE_ADDR ptrval;
243666e63ce3Schristos 
243766e63ce3Schristos 	  /* No PLT entry found.  Mask off the upper bits of the address
243866e63ce3Schristos 	     to make a pointer.  As noted in the warning to the user
243966e63ce3Schristos 	     below, this value might be useful if converted back into
244066e63ce3Schristos 	     an address by GDB, but will otherwise, almost certainly,
244166e63ce3Schristos 	     be garbage.
244266e63ce3Schristos 
244366e63ce3Schristos 	     Using this masked result does seem to be useful
244466e63ce3Schristos 	     in gdb.cp/cplusfuncs.exp in which ~40 FAILs turn into
244566e63ce3Schristos 	     PASSes.  These results appear to be correct as well.
244666e63ce3Schristos 
244766e63ce3Schristos 	     We print a warning here so that the user can make a
244866e63ce3Schristos 	     determination about whether the result is useful or not.  */
244966e63ce3Schristos 	  ptrval = addr & 0xffff;
245066e63ce3Schristos 
245166e63ce3Schristos 	  warning (_("Cannot convert code address %s to function pointer:\n"
245266e63ce3Schristos 		   "couldn't find trampoline named '%s.plt'.\n"
245366e63ce3Schristos 		   "Returning pointer value %s instead; this may produce\n"
245466e63ce3Schristos 		   "a useful result if converted back into an address by GDB,\n"
2455*1424dfb3Schristos 		   "but will most likely not be useful otherwise."),
245666e63ce3Schristos 		   paddress (gdbarch, addr), func_name,
245766e63ce3Schristos 		   paddress (gdbarch, ptrval));
245866e63ce3Schristos 
245966e63ce3Schristos 	  addr = ptrval;
246066e63ce3Schristos 
246166e63ce3Schristos 	}
246266e63ce3Schristos       else
246366e63ce3Schristos 	{
246466e63ce3Schristos 	  /* The trampoline's address is our pointer.  */
246526a53354Schristos 	  addr = BMSYMBOL_VALUE_ADDRESS (tramp_msym);
246666e63ce3Schristos 	}
246766e63ce3Schristos     }
246866e63ce3Schristos 
246966e63ce3Schristos   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
247066e63ce3Schristos }
247166e63ce3Schristos 
247266e63ce3Schristos 
247366e63ce3Schristos static CORE_ADDR
m32c_m16c_pointer_to_address(struct gdbarch * gdbarch,struct type * type,const gdb_byte * buf)247466e63ce3Schristos m32c_m16c_pointer_to_address (struct gdbarch *gdbarch,
247566e63ce3Schristos 			      struct type *type, const gdb_byte *buf)
247666e63ce3Schristos {
247766e63ce3Schristos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
247866e63ce3Schristos   CORE_ADDR ptr;
247966e63ce3Schristos   enum type_code target_code;
248066e63ce3Schristos 
2481*1424dfb3Schristos   gdb_assert (type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type));
248266e63ce3Schristos 
248366e63ce3Schristos   ptr = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
248466e63ce3Schristos 
2485*1424dfb3Schristos   target_code = TYPE_TARGET_TYPE (type)->code ();
248666e63ce3Schristos 
248766e63ce3Schristos   if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
248866e63ce3Schristos     {
248966e63ce3Schristos       /* See if there is a minimal symbol at that address whose name is
249066e63ce3Schristos          "NAME.plt".  */
24917af5a897Schristos       struct bound_minimal_symbol ptr_msym = lookup_minimal_symbol_by_pc (ptr);
249266e63ce3Schristos 
24937af5a897Schristos       if (ptr_msym.minsym)
249466e63ce3Schristos         {
2495*1424dfb3Schristos           const char *ptr_msym_name = ptr_msym.minsym->linkage_name ();
249666e63ce3Schristos           int len = strlen (ptr_msym_name);
249766e63ce3Schristos 
249866e63ce3Schristos           if (len > 4
249966e63ce3Schristos               && strcmp (ptr_msym_name + len - 4, ".plt") == 0)
250066e63ce3Schristos             {
250126a53354Schristos 	      struct bound_minimal_symbol func_msym;
250266e63ce3Schristos               /* We have a .plt symbol; try to find the symbol for the
250366e63ce3Schristos                  corresponding function.
250466e63ce3Schristos 
250566e63ce3Schristos                  Since the trampoline contains a jump instruction, we
250666e63ce3Schristos                  could also just extract the jump's target address.  I
250766e63ce3Schristos                  don't see much advantage one way or the other.  */
2508c03b94e9Schristos               char *func_name = (char *) xmalloc (len - 4 + 1);
250966e63ce3Schristos               memcpy (func_name, ptr_msym_name, len - 4);
251066e63ce3Schristos               func_name[len - 4] = '\0';
251166e63ce3Schristos               func_msym
251266e63ce3Schristos                 = lookup_minimal_symbol (func_name, NULL, NULL);
251366e63ce3Schristos 
251466e63ce3Schristos               /* If we do have such a symbol, return its value as the
251566e63ce3Schristos                  function's true address.  */
251626a53354Schristos               if (func_msym.minsym)
251726a53354Schristos                 ptr = BMSYMBOL_VALUE_ADDRESS (func_msym);
251866e63ce3Schristos             }
251966e63ce3Schristos         }
252066e63ce3Schristos       else
252166e63ce3Schristos 	{
252266e63ce3Schristos 	  int aspace;
252366e63ce3Schristos 
252466e63ce3Schristos 	  for (aspace = 1; aspace <= 15; aspace++)
252566e63ce3Schristos 	    {
252666e63ce3Schristos 	      ptr_msym = lookup_minimal_symbol_by_pc ((aspace << 16) | ptr);
252766e63ce3Schristos 
25287af5a897Schristos 	      if (ptr_msym.minsym)
252966e63ce3Schristos 		ptr |= aspace << 16;
253066e63ce3Schristos 	    }
253166e63ce3Schristos 	}
253266e63ce3Schristos     }
253366e63ce3Schristos 
253466e63ce3Schristos   return ptr;
253566e63ce3Schristos }
253666e63ce3Schristos 
253766e63ce3Schristos static void
m32c_virtual_frame_pointer(struct gdbarch * gdbarch,CORE_ADDR pc,int * frame_regnum,LONGEST * frame_offset)253866e63ce3Schristos m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
253966e63ce3Schristos 			    int *frame_regnum,
254066e63ce3Schristos 			    LONGEST *frame_offset)
254166e63ce3Schristos {
254248596154Schristos   const char *name;
254348596154Schristos   CORE_ADDR func_addr, func_end;
254466e63ce3Schristos   struct m32c_prologue p;
254566e63ce3Schristos 
254666e63ce3Schristos   struct regcache *regcache = get_current_regcache ();
254766e63ce3Schristos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
254866e63ce3Schristos 
254966e63ce3Schristos   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
255066e63ce3Schristos     internal_error (__FILE__, __LINE__,
255166e63ce3Schristos 		    _("No virtual frame pointer available"));
255266e63ce3Schristos 
255366e63ce3Schristos   m32c_analyze_prologue (gdbarch, func_addr, pc, &p);
255466e63ce3Schristos   switch (p.kind)
255566e63ce3Schristos     {
255666e63ce3Schristos     case prologue_with_frame_ptr:
255766e63ce3Schristos       *frame_regnum = m32c_banked_register (tdep->fb, regcache)->num;
255866e63ce3Schristos       *frame_offset = p.frame_ptr_offset;
255966e63ce3Schristos       break;
256066e63ce3Schristos     case prologue_sans_frame_ptr:
256166e63ce3Schristos       *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
256266e63ce3Schristos       *frame_offset = p.frame_size;
256366e63ce3Schristos       break;
256466e63ce3Schristos     default:
256566e63ce3Schristos       *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
256666e63ce3Schristos       *frame_offset = 0;
256766e63ce3Schristos       break;
256866e63ce3Schristos     }
256966e63ce3Schristos   /* Sanity check */
257066e63ce3Schristos   if (*frame_regnum > gdbarch_num_regs (gdbarch))
257166e63ce3Schristos     internal_error (__FILE__, __LINE__,
257266e63ce3Schristos 		    _("No virtual frame pointer available"));
257366e63ce3Schristos }
257466e63ce3Schristos 
257566e63ce3Schristos 
257666e63ce3Schristos /* Initialization.  */
257766e63ce3Schristos 
257866e63ce3Schristos static struct gdbarch *
m32c_gdbarch_init(struct gdbarch_info info,struct gdbarch_list * arches)257966e63ce3Schristos m32c_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
258066e63ce3Schristos {
25811c468f90Schristos   struct gdbarch *gdbarch;
258266e63ce3Schristos   struct gdbarch_tdep *tdep;
258366e63ce3Schristos   unsigned long mach = info.bfd_arch_info->mach;
258466e63ce3Schristos 
258566e63ce3Schristos   /* Find a candidate among the list of architectures we've created
258666e63ce3Schristos      already.  */
258766e63ce3Schristos   for (arches = gdbarch_list_lookup_by_info (arches, &info);
258866e63ce3Schristos        arches != NULL;
258966e63ce3Schristos        arches = gdbarch_list_lookup_by_info (arches->next, &info))
259066e63ce3Schristos     return arches->gdbarch;
259166e63ce3Schristos 
2592c03b94e9Schristos   tdep = XCNEW (struct gdbarch_tdep);
25931c468f90Schristos   gdbarch = gdbarch_alloc (&info, tdep);
259466e63ce3Schristos 
259566e63ce3Schristos   /* Essential types.  */
25961c468f90Schristos   make_types (gdbarch);
259766e63ce3Schristos 
259866e63ce3Schristos   /* Address/pointer conversions.  */
259966e63ce3Schristos   if (mach == bfd_mach_m16c)
260066e63ce3Schristos     {
26011c468f90Schristos       set_gdbarch_address_to_pointer (gdbarch, m32c_m16c_address_to_pointer);
26021c468f90Schristos       set_gdbarch_pointer_to_address (gdbarch, m32c_m16c_pointer_to_address);
260366e63ce3Schristos     }
260466e63ce3Schristos 
260566e63ce3Schristos   /* Register set.  */
26061c468f90Schristos   make_regs (gdbarch);
260766e63ce3Schristos 
260866e63ce3Schristos   /* Breakpoints.  */
26091c468f90Schristos   set_gdbarch_breakpoint_kind_from_pc (gdbarch, m32c_breakpoint::kind_from_pc);
26101c468f90Schristos   set_gdbarch_sw_breakpoint_from_kind (gdbarch, m32c_breakpoint::bp_from_kind);
261166e63ce3Schristos 
261266e63ce3Schristos   /* Prologue analysis and unwinding.  */
26131c468f90Schristos   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
26141c468f90Schristos   set_gdbarch_skip_prologue (gdbarch, m32c_skip_prologue);
261566e63ce3Schristos #if 0
261666e63ce3Schristos   /* I'm dropping the dwarf2 sniffer because it has a few problems.
261766e63ce3Schristos      They may be in the dwarf2 cfi code in GDB, or they may be in
261866e63ce3Schristos      the debug info emitted by the upstream toolchain.  I don't
261966e63ce3Schristos      know which, but I do know that the prologue analyzer works better.
262066e63ce3Schristos      MVS 04/13/06  */
26211c468f90Schristos   dwarf2_append_sniffers (gdbarch);
262266e63ce3Schristos #endif
26231c468f90Schristos   frame_unwind_append_unwinder (gdbarch, &m32c_unwind);
262466e63ce3Schristos 
262566e63ce3Schristos   /* Inferior calls.  */
26261c468f90Schristos   set_gdbarch_push_dummy_call (gdbarch, m32c_push_dummy_call);
26271c468f90Schristos   set_gdbarch_return_value (gdbarch, m32c_return_value);
262866e63ce3Schristos 
262966e63ce3Schristos   /* Trampolines.  */
26301c468f90Schristos   set_gdbarch_skip_trampoline_code (gdbarch, m32c_skip_trampoline_code);
263166e63ce3Schristos 
26321c468f90Schristos   set_gdbarch_virtual_frame_pointer (gdbarch, m32c_virtual_frame_pointer);
263366e63ce3Schristos 
263466e63ce3Schristos   /* m32c function boundary addresses are not necessarily even.
263566e63ce3Schristos      Therefore, the `vbit', which indicates a pointer to a virtual
263666e63ce3Schristos      member function, is stored in the delta field, rather than as
263766e63ce3Schristos      the low bit of a function pointer address.
263866e63ce3Schristos 
263966e63ce3Schristos      In order to verify this, see the definition of
264066e63ce3Schristos      TARGET_PTRMEMFUNC_VBIT_LOCATION in gcc/defaults.h along with the
264166e63ce3Schristos      definition of FUNCTION_BOUNDARY in gcc/config/m32c/m32c.h.  */
26421c468f90Schristos   set_gdbarch_vbit_in_delta (gdbarch, 1);
264366e63ce3Schristos 
26441c468f90Schristos   return gdbarch;
264566e63ce3Schristos }
264666e63ce3Schristos 
2647*1424dfb3Schristos void _initialize_m32c_tdep ();
264866e63ce3Schristos void
_initialize_m32c_tdep()2649*1424dfb3Schristos _initialize_m32c_tdep ()
265066e63ce3Schristos {
265166e63ce3Schristos   register_gdbarch_init (bfd_arch_m32c, m32c_gdbarch_init);
265266e63ce3Schristos 
265366e63ce3Schristos   m32c_dma_reggroup = reggroup_new ("dma", USER_REGGROUP);
265466e63ce3Schristos }
2655