Home | History | Annotate | Line # | Download | only in gdb
moxie-tdep.c revision 1.1.1.4
      1 /* Target-dependent code for Moxie.
      2 
      3    Copyright (C) 2009-2017 Free Software Foundation, Inc.
      4 
      5    This file is part of GDB.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19 
     20 #include "defs.h"
     21 #include "frame.h"
     22 #include "frame-unwind.h"
     23 #include "frame-base.h"
     24 #include "symtab.h"
     25 #include "gdbtypes.h"
     26 #include "gdbcmd.h"
     27 #include "gdbcore.h"
     28 #include "value.h"
     29 #include "inferior.h"
     30 #include "symfile.h"
     31 #include "objfiles.h"
     32 #include "osabi.h"
     33 #include "language.h"
     34 #include "arch-utils.h"
     35 #include "regcache.h"
     36 #include "trad-frame.h"
     37 #include "dis-asm.h"
     38 #include "record.h"
     39 #include "record-full.h"
     40 
     41 #include "moxie-tdep.h"
     42 #include <algorithm>
     43 
     44 /* Local functions.  */
     45 
     46 extern void _initialize_moxie_tdep (void);
     47 
     48 /* Use an invalid address value as 'not available' marker.  */
     49 enum { REG_UNAVAIL = (CORE_ADDR) -1 };
     50 
     51 struct moxie_frame_cache
     52 {
     53   /* Base address.  */
     54   CORE_ADDR base;
     55   CORE_ADDR pc;
     56   LONGEST framesize;
     57   CORE_ADDR saved_regs[MOXIE_NUM_REGS];
     58   CORE_ADDR saved_sp;
     59 };
     60 
     61 /* Implement the "frame_align" gdbarch method.  */
     62 
     63 static CORE_ADDR
     64 moxie_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
     65 {
     66   /* Align to the size of an instruction (so that they can safely be
     67      pushed onto the stack.  */
     68   return sp & ~1;
     69 }
     70 
     71 constexpr gdb_byte moxie_break_insn[] = { 0x35, 0x00 };
     72 
     73 typedef BP_MANIPULATION (moxie_break_insn) moxie_breakpoint;
     74 
     75 /* Moxie register names.  */
     76 
     77 static const char *moxie_register_names[] = {
     78   "$fp",  "$sp",  "$r0",  "$r1",  "$r2",
     79   "$r3",  "$r4",  "$r5", "$r6", "$r7",
     80   "$r8", "$r9", "$r10", "$r11", "$r12",
     81   "$r13", "$pc", "$cc" };
     82 
     83 /* Implement the "register_name" gdbarch method.  */
     84 
     85 static const char *
     86 moxie_register_name (struct gdbarch *gdbarch, int reg_nr)
     87 {
     88   if (reg_nr < 0)
     89     return NULL;
     90   if (reg_nr >= MOXIE_NUM_REGS)
     91     return NULL;
     92   return moxie_register_names[reg_nr];
     93 }
     94 
     95 /* Implement the "register_type" gdbarch method.  */
     96 
     97 static struct type *
     98 moxie_register_type (struct gdbarch *gdbarch, int reg_nr)
     99 {
    100   if (reg_nr == MOXIE_PC_REGNUM)
    101     return  builtin_type (gdbarch)->builtin_func_ptr;
    102   else if (reg_nr == MOXIE_SP_REGNUM || reg_nr == MOXIE_FP_REGNUM)
    103     return builtin_type (gdbarch)->builtin_data_ptr;
    104   else
    105     return builtin_type (gdbarch)->builtin_int32;
    106 }
    107 
    108 /* Write into appropriate registers a function return value
    109    of type TYPE, given in virtual format.  */
    110 
    111 static void
    112 moxie_store_return_value (struct type *type, struct regcache *regcache,
    113 			 const gdb_byte *valbuf)
    114 {
    115   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    116   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    117   CORE_ADDR regval;
    118   int len = TYPE_LENGTH (type);
    119 
    120   /* Things always get returned in RET1_REGNUM, RET2_REGNUM.  */
    121   regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
    122   regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);
    123   if (len > 4)
    124     {
    125       regval = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
    126       regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
    127     }
    128 }
    129 
    130 /* Decode the instructions within the given address range.  Decide
    131    when we must have reached the end of the function prologue.  If a
    132    frame_info pointer is provided, fill in its saved_regs etc.
    133 
    134    Returns the address of the first instruction after the prologue.  */
    135 
    136 static CORE_ADDR
    137 moxie_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
    138 			struct moxie_frame_cache *cache,
    139 			struct gdbarch *gdbarch)
    140 {
    141   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    142   CORE_ADDR next_addr;
    143   ULONGEST inst, inst2;
    144   LONGEST offset;
    145   int regnum;
    146 
    147   /* Record where the jsra instruction saves the PC and FP.  */
    148   cache->saved_regs[MOXIE_PC_REGNUM] = -4;
    149   cache->saved_regs[MOXIE_FP_REGNUM] = 0;
    150   cache->framesize = 0;
    151 
    152   if (start_addr >= end_addr)
    153     return end_addr;
    154 
    155   for (next_addr = start_addr; next_addr < end_addr; )
    156     {
    157       inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
    158 
    159       /* Match "push $sp $rN" where N is between 0 and 13 inclusive.  */
    160       if (inst >= 0x0612 && inst <= 0x061f)
    161 	{
    162 	  regnum = inst & 0x000f;
    163 	  cache->framesize += 4;
    164 	  cache->saved_regs[regnum] = cache->framesize;
    165 	  next_addr += 2;
    166 	}
    167       else
    168 	break;
    169     }
    170 
    171   inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
    172 
    173   /* Optional stack allocation for args and local vars <= 4
    174      byte.  */
    175   if (inst == 0x01e0)          /* ldi.l $r12, X */
    176     {
    177       offset = read_memory_integer (next_addr + 2, 4, byte_order);
    178       inst2 = read_memory_unsigned_integer (next_addr + 6, 2, byte_order);
    179 
    180       if (inst2 == 0x291e)     /* sub.l $sp, $r12 */
    181 	{
    182 	  cache->framesize += offset;
    183 	}
    184 
    185       return (next_addr + 8);
    186     }
    187   else if ((inst & 0xff00) == 0x9100)   /* dec $sp, X */
    188     {
    189       cache->framesize += (inst & 0x00ff);
    190       next_addr += 2;
    191 
    192       while (next_addr < end_addr)
    193 	{
    194 	  inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
    195 	  if ((inst & 0xff00) != 0x9100) /* no more dec $sp, X */
    196 	    break;
    197 	  cache->framesize += (inst & 0x00ff);
    198 	  next_addr += 2;
    199 	}
    200     }
    201 
    202   return next_addr;
    203 }
    204 
    205 /* Find the end of function prologue.  */
    206 
    207 static CORE_ADDR
    208 moxie_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
    209 {
    210   CORE_ADDR func_addr = 0, func_end = 0;
    211   const char *func_name;
    212 
    213   /* See if we can determine the end of the prologue via the symbol table.
    214      If so, then return either PC, or the PC after the prologue, whichever
    215      is greater.  */
    216   if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
    217     {
    218       CORE_ADDR post_prologue_pc
    219 	= skip_prologue_using_sal (gdbarch, func_addr);
    220       if (post_prologue_pc != 0)
    221 	return std::max (pc, post_prologue_pc);
    222       else
    223 	{
    224 	  /* Can't determine prologue from the symbol table, need to examine
    225 	     instructions.  */
    226 	  struct symtab_and_line sal;
    227 	  struct symbol *sym;
    228 	  struct moxie_frame_cache cache;
    229 	  CORE_ADDR plg_end;
    230 
    231 	  memset (&cache, 0, sizeof cache);
    232 
    233 	  plg_end = moxie_analyze_prologue (func_addr,
    234 					    func_end, &cache, gdbarch);
    235 	  /* Found a function.  */
    236 	  sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol;
    237 	  /* Don't use line number debug info for assembly source
    238 	     files.  */
    239 	  if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
    240 	    {
    241 	      sal = find_pc_line (func_addr, 0);
    242 	      if (sal.end && sal.end < func_end)
    243 		{
    244 		  /* Found a line number, use it as end of
    245 		     prologue.  */
    246 		  return sal.end;
    247 		}
    248 	    }
    249 	  /* No useable line symbol.  Use result of prologue parsing
    250 	     method.  */
    251 	  return plg_end;
    252 	}
    253     }
    254 
    255   /* No function symbol -- just return the PC.  */
    256   return (CORE_ADDR) pc;
    257 }
    258 
    259 struct moxie_unwind_cache
    260 {
    261   /* The previous frame's inner most stack address.  Used as this
    262      frame ID's stack_addr.  */
    263   CORE_ADDR prev_sp;
    264   /* The frame's base, optionally used by the high-level debug info.  */
    265   CORE_ADDR base;
    266   int size;
    267   /* How far the SP and r13 (FP) have been offset from the start of
    268      the stack frame (as defined by the previous frame's stack
    269      pointer).  */
    270   LONGEST sp_offset;
    271   LONGEST r13_offset;
    272   int uses_frame;
    273   /* Table indicating the location of each and every register.  */
    274   struct trad_frame_saved_reg *saved_regs;
    275 };
    276 
    277 /* Read an unsigned integer from the inferior, and adjust
    278    endianess.  */
    279 static ULONGEST
    280 moxie_process_readu (CORE_ADDR addr, gdb_byte *buf,
    281 		     int length, enum bfd_endian byte_order)
    282 {
    283   if (target_read_memory (addr, buf, length))
    284     {
    285       if (record_debug)
    286 	printf_unfiltered (_("Process record: error reading memory at "
    287 			     "addr 0x%s len = %d.\n"),
    288 			   paddress (target_gdbarch (), addr), length);
    289       return -1;
    290     }
    291 
    292   return extract_unsigned_integer (buf, length, byte_order);
    293 }
    294 
    295 
    296 /* Helper macro to extract the signed 10-bit offset from a 16-bit
    297    branch instruction.	*/
    298 #define INST2OFFSET(o) ((((signed short)((o & ((1<<10)-1))<<6))>>6)<<1)
    299 
    300 /* Insert a single step breakpoint.  */
    301 
    302 static VEC (CORE_ADDR) *
    303 moxie_software_single_step (struct regcache *regcache)
    304 {
    305   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    306   CORE_ADDR addr;
    307   gdb_byte buf[4];
    308   uint16_t inst;
    309   uint32_t tmpu32;
    310   ULONGEST fp;
    311   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    312   VEC (CORE_ADDR) *next_pcs = NULL;
    313 
    314   addr = regcache_read_pc (regcache);
    315 
    316   inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);
    317 
    318   /* Decode instruction.  */
    319   if (inst & (1 << 15))
    320     {
    321       if (inst & (1 << 14))
    322 	{
    323 	  /* This is a Form 3 instruction.  */
    324 	  int opcode = (inst >> 10 & 0xf);
    325 
    326 	  switch (opcode)
    327 	    {
    328 	    case 0x00: /* beq */
    329 	    case 0x01: /* bne */
    330 	    case 0x02: /* blt */
    331 	    case 0x03: /* bgt */
    332 	    case 0x04: /* bltu */
    333 	    case 0x05: /* bgtu */
    334 	    case 0x06: /* bge */
    335 	    case 0x07: /* ble */
    336 	    case 0x08: /* bgeu */
    337 	    case 0x09: /* bleu */
    338 	      /* Insert breaks on both branches, because we can't currently tell
    339 		 which way things will go.  */
    340 	      VEC_safe_push (CORE_ADDR, next_pcs, addr + 2);
    341 	      VEC_safe_push (CORE_ADDR, next_pcs,
    342 			     addr + 2 + INST2OFFSET(inst));
    343 	      break;
    344 	    default:
    345 	      {
    346 		/* Do nothing.	*/
    347 		break;
    348 	      }
    349 	    }
    350 	}
    351       else
    352 	{
    353 	  /* This is a Form 2 instruction.  They are all 16 bits.  */
    354 	  VEC_safe_push (CORE_ADDR, next_pcs, addr + 2);
    355 	}
    356     }
    357   else
    358     {
    359       /* This is a Form 1 instruction.	*/
    360       int opcode = inst >> 8;
    361 
    362       switch (opcode)
    363 	{
    364 	  /* 16-bit instructions.  */
    365 	case 0x00: /* bad */
    366 	case 0x02: /* mov (register-to-register) */
    367 	case 0x05: /* add.l */
    368 	case 0x06: /* push */
    369 	case 0x07: /* pop */
    370 	case 0x0a: /* ld.l (register indirect) */
    371 	case 0x0b: /* st.l */
    372 	case 0x0e: /* cmp */
    373 	case 0x0f: /* nop */
    374 	case 0x10: /* sex.b */
    375 	case 0x11: /* sex.s */
    376 	case 0x12: /* zex.b */
    377 	case 0x13: /* zex.s */
    378 	case 0x14: /* umul.x */
    379 	case 0x15: /* mul.x */
    380 	case 0x16:
    381 	case 0x17:
    382 	case 0x18:
    383 	case 0x1c: /* ld.b (register indirect) */
    384 	case 0x1e: /* st.b */
    385 	case 0x21: /* ld.s (register indirect) */
    386 	case 0x23: /* st.s */
    387 	case 0x26: /* and */
    388 	case 0x27: /* lshr */
    389 	case 0x28: /* ashl */
    390 	case 0x29: /* sub.l */
    391 	case 0x2a: /* neg */
    392 	case 0x2b: /* or */
    393 	case 0x2c: /* not */
    394 	case 0x2d: /* ashr */
    395 	case 0x2e: /* xor */
    396 	case 0x2f: /* mul.l */
    397 	case 0x31: /* div.l */
    398 	case 0x32: /* udiv.l */
    399 	case 0x33: /* mod.l */
    400 	case 0x34: /* umod.l */
    401 	  VEC_safe_push (CORE_ADDR, next_pcs, addr + 2);
    402 	  break;
    403 
    404 	  /* 32-bit instructions.  */
    405 	case 0x0c: /* ldo.l */
    406 	case 0x0d: /* sto.l */
    407 	case 0x36: /* ldo.b */
    408 	case 0x37: /* sto.b */
    409 	case 0x38: /* ldo.s */
    410 	case 0x39: /* sto.s */
    411 	  VEC_safe_push (CORE_ADDR, next_pcs, addr + 4);
    412 	  break;
    413 
    414 	  /* 48-bit instructions.  */
    415 	case 0x01: /* ldi.l (immediate) */
    416 	case 0x08: /* lda.l */
    417 	case 0x09: /* sta.l */
    418 	case 0x1b: /* ldi.b (immediate) */
    419 	case 0x1d: /* lda.b */
    420 	case 0x1f: /* sta.b */
    421 	case 0x20: /* ldi.s (immediate) */
    422 	case 0x22: /* lda.s */
    423 	case 0x24: /* sta.s */
    424 	  VEC_safe_push (CORE_ADDR, next_pcs, addr + 6);
    425 	  break;
    426 
    427 	  /* Control flow instructions.	 */
    428 	case 0x03: /* jsra */
    429 	case 0x1a: /* jmpa */
    430 	  VEC_safe_push (CORE_ADDR, next_pcs,
    431 			 moxie_process_readu (addr + 2, buf, 4, byte_order));
    432 	  break;
    433 
    434 	case 0x04: /* ret */
    435 	  regcache_cooked_read_unsigned (regcache, MOXIE_FP_REGNUM, &fp);
    436 	  VEC_safe_push (CORE_ADDR, next_pcs,
    437 			 moxie_process_readu (fp + 4, buf, 4, byte_order));
    438 	  break;
    439 
    440 	case 0x19: /* jsr */
    441 	case 0x25: /* jmp */
    442 	  regcache_raw_read (regcache,
    443 			     (inst >> 4) & 0xf, (gdb_byte *) & tmpu32);
    444 	  VEC_safe_push (CORE_ADDR, next_pcs, tmpu32);
    445 	  break;
    446 
    447 	case 0x30: /* swi */
    448 	case 0x35: /* brk */
    449 	  /* Unsupported, for now.  */
    450 	  break;
    451 	}
    452     }
    453 
    454   return next_pcs;
    455 }
    456 
    457 /* Implement the "read_pc" gdbarch method.  */
    458 
    459 static CORE_ADDR
    460 moxie_read_pc (struct regcache *regcache)
    461 {
    462   ULONGEST pc;
    463 
    464   regcache_cooked_read_unsigned (regcache, MOXIE_PC_REGNUM, &pc);
    465   return pc;
    466 }
    467 
    468 /* Implement the "write_pc" gdbarch method.  */
    469 
    470 static void
    471 moxie_write_pc (struct regcache *regcache, CORE_ADDR val)
    472 {
    473   regcache_cooked_write_unsigned (regcache, MOXIE_PC_REGNUM, val);
    474 }
    475 
    476 /* Implement the "unwind_sp" gdbarch method.  */
    477 
    478 static CORE_ADDR
    479 moxie_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
    480 {
    481   return frame_unwind_register_unsigned (next_frame, MOXIE_SP_REGNUM);
    482 }
    483 
    484 /* Given a return value in `regbuf' with a type `valtype',
    485    extract and copy its value into `valbuf'.  */
    486 
    487 static void
    488 moxie_extract_return_value (struct type *type, struct regcache *regcache,
    489 			    gdb_byte *dst)
    490 {
    491   struct gdbarch *gdbarch = get_regcache_arch (regcache);
    492   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    493   int len = TYPE_LENGTH (type);
    494   ULONGEST tmp;
    495 
    496   /* By using store_unsigned_integer we avoid having to do
    497      anything special for small big-endian values.  */
    498   regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &tmp);
    499   store_unsigned_integer (dst, (len > 4 ? len - 4 : len), byte_order, tmp);
    500 
    501   /* Ignore return values more than 8 bytes in size because the moxie
    502      returns anything more than 8 bytes in the stack.  */
    503   if (len > 4)
    504     {
    505       regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
    506       store_unsigned_integer (dst + len - 4, 4, byte_order, tmp);
    507     }
    508 }
    509 
    510 /* Implement the "return_value" gdbarch method.  */
    511 
    512 static enum return_value_convention
    513 moxie_return_value (struct gdbarch *gdbarch, struct value *function,
    514 		   struct type *valtype, struct regcache *regcache,
    515 		   gdb_byte *readbuf, const gdb_byte *writebuf)
    516 {
    517   if (TYPE_LENGTH (valtype) > 8)
    518     return RETURN_VALUE_STRUCT_CONVENTION;
    519   else
    520     {
    521       if (readbuf != NULL)
    522 	moxie_extract_return_value (valtype, regcache, readbuf);
    523       if (writebuf != NULL)
    524 	moxie_store_return_value (valtype, regcache, writebuf);
    525       return RETURN_VALUE_REGISTER_CONVENTION;
    526     }
    527 }
    528 
    529 /* Allocate and initialize a moxie_frame_cache object.  */
    530 
    531 static struct moxie_frame_cache *
    532 moxie_alloc_frame_cache (void)
    533 {
    534   struct moxie_frame_cache *cache;
    535   int i;
    536 
    537   cache = FRAME_OBSTACK_ZALLOC (struct moxie_frame_cache);
    538 
    539   cache->base = 0;
    540   cache->saved_sp = 0;
    541   cache->pc = 0;
    542   cache->framesize = 0;
    543   for (i = 0; i < MOXIE_NUM_REGS; ++i)
    544     cache->saved_regs[i] = REG_UNAVAIL;
    545 
    546   return cache;
    547 }
    548 
    549 /* Populate a moxie_frame_cache object for this_frame.  */
    550 
    551 static struct moxie_frame_cache *
    552 moxie_frame_cache (struct frame_info *this_frame, void **this_cache)
    553 {
    554   struct moxie_frame_cache *cache;
    555   CORE_ADDR current_pc;
    556   int i;
    557 
    558   if (*this_cache)
    559     return (struct moxie_frame_cache *) *this_cache;
    560 
    561   cache = moxie_alloc_frame_cache ();
    562   *this_cache = cache;
    563 
    564   cache->base = get_frame_register_unsigned (this_frame, MOXIE_FP_REGNUM);
    565   if (cache->base == 0)
    566     return cache;
    567 
    568   cache->pc = get_frame_func (this_frame);
    569   current_pc = get_frame_pc (this_frame);
    570   if (cache->pc)
    571     {
    572       struct gdbarch *gdbarch = get_frame_arch (this_frame);
    573       moxie_analyze_prologue (cache->pc, current_pc, cache, gdbarch);
    574     }
    575 
    576   cache->saved_sp = cache->base - cache->framesize;
    577 
    578   for (i = 0; i < MOXIE_NUM_REGS; ++i)
    579     if (cache->saved_regs[i] != REG_UNAVAIL)
    580       cache->saved_regs[i] = cache->base - cache->saved_regs[i];
    581 
    582   return cache;
    583 }
    584 
    585 /* Implement the "unwind_pc" gdbarch method.  */
    586 
    587 static CORE_ADDR
    588 moxie_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
    589 {
    590   return frame_unwind_register_unsigned (next_frame, MOXIE_PC_REGNUM);
    591 }
    592 
    593 /* Given a GDB frame, determine the address of the calling function's
    594    frame.  This will be used to create a new GDB frame struct.  */
    595 
    596 static void
    597 moxie_frame_this_id (struct frame_info *this_frame,
    598 		    void **this_prologue_cache, struct frame_id *this_id)
    599 {
    600   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
    601 						   this_prologue_cache);
    602 
    603   /* This marks the outermost frame.  */
    604   if (cache->base == 0)
    605     return;
    606 
    607   *this_id = frame_id_build (cache->saved_sp, cache->pc);
    608 }
    609 
    610 /* Get the value of register regnum in the previous stack frame.  */
    611 
    612 static struct value *
    613 moxie_frame_prev_register (struct frame_info *this_frame,
    614 			  void **this_prologue_cache, int regnum)
    615 {
    616   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
    617 						   this_prologue_cache);
    618 
    619   gdb_assert (regnum >= 0);
    620 
    621   if (regnum == MOXIE_SP_REGNUM && cache->saved_sp)
    622     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
    623 
    624   if (regnum < MOXIE_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
    625     return frame_unwind_got_memory (this_frame, regnum,
    626 				    cache->saved_regs[regnum]);
    627 
    628   return frame_unwind_got_register (this_frame, regnum, regnum);
    629 }
    630 
    631 static const struct frame_unwind moxie_frame_unwind = {
    632   NORMAL_FRAME,
    633   default_frame_unwind_stop_reason,
    634   moxie_frame_this_id,
    635   moxie_frame_prev_register,
    636   NULL,
    637   default_frame_sniffer
    638 };
    639 
    640 /* Return the base address of this_frame.  */
    641 
    642 static CORE_ADDR
    643 moxie_frame_base_address (struct frame_info *this_frame, void **this_cache)
    644 {
    645   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
    646 						       this_cache);
    647 
    648   return cache->base;
    649 }
    650 
    651 static const struct frame_base moxie_frame_base = {
    652   &moxie_frame_unwind,
    653   moxie_frame_base_address,
    654   moxie_frame_base_address,
    655   moxie_frame_base_address
    656 };
    657 
    658 static struct frame_id
    659 moxie_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
    660 {
    661   CORE_ADDR sp = get_frame_register_unsigned (this_frame, MOXIE_SP_REGNUM);
    662 
    663   return frame_id_build (sp, get_frame_pc (this_frame));
    664 }
    665 
    666 /* Parse the current instruction and record the values of the registers and
    667    memory that will be changed in current instruction to "record_arch_list".
    668    Return -1 if something wrong.  */
    669 
    670 static int
    671 moxie_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
    672 		      CORE_ADDR addr)
    673 {
    674   gdb_byte buf[4];
    675   uint16_t inst;
    676   uint32_t tmpu32;
    677   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    678 
    679   if (record_debug > 1)
    680     fprintf_unfiltered (gdb_stdlog, "Process record: moxie_process_record "
    681 			            "addr = 0x%s\n",
    682 			paddress (target_gdbarch (), addr));
    683 
    684   inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);
    685 
    686   /* Decode instruction.  */
    687   if (inst & (1 << 15))
    688     {
    689       if (inst & (1 << 14))
    690 	{
    691 	  /* This is a Form 3 instruction.  */
    692 	  int opcode = (inst >> 10 & 0xf);
    693 
    694 	  switch (opcode)
    695 	    {
    696 	    case 0x00: /* beq */
    697 	    case 0x01: /* bne */
    698 	    case 0x02: /* blt */
    699 	    case 0x03: /* bgt */
    700 	    case 0x04: /* bltu */
    701 	    case 0x05: /* bgtu */
    702 	    case 0x06: /* bge */
    703 	    case 0x07: /* ble */
    704 	    case 0x08: /* bgeu */
    705 	    case 0x09: /* bleu */
    706 	      /* Do nothing.  */
    707 	      break;
    708 	    default:
    709 	      {
    710 		/* Do nothing.  */
    711 		break;
    712 	      }
    713 	    }
    714 	}
    715       else
    716 	{
    717 	  /* This is a Form 2 instruction.  */
    718 	  int opcode = (inst >> 12 & 0x3);
    719 	  switch (opcode)
    720 	    {
    721 	    case 0x00: /* inc */
    722 	    case 0x01: /* dec */
    723 	    case 0x02: /* gsr */
    724 	      {
    725 		int reg = (inst >> 8) & 0xf;
    726 		if (record_full_arch_list_add_reg (regcache, reg))
    727 		  return -1;
    728 	      }
    729 	      break;
    730 	    case 0x03: /* ssr */
    731 	      {
    732 		/* Do nothing until GDB learns about moxie's special
    733 		   registers.  */
    734 	      }
    735 	      break;
    736 	    default:
    737 	      /* Do nothing.  */
    738 	      break;
    739 	    }
    740 	}
    741     }
    742   else
    743     {
    744       /* This is a Form 1 instruction.  */
    745       int opcode = inst >> 8;
    746 
    747       switch (opcode)
    748 	{
    749 	case 0x00: /* nop */
    750 	  /* Do nothing.  */
    751 	  break;
    752 	case 0x01: /* ldi.l (immediate) */
    753 	case 0x02: /* mov (register-to-register) */
    754 	  {
    755 	    int reg = (inst >> 4) & 0xf;
    756 	    if (record_full_arch_list_add_reg (regcache, reg))
    757 	      return -1;
    758 	  }
    759 	  break;
    760 	case 0x03: /* jsra */
    761 	  {
    762 	    regcache_raw_read (regcache,
    763 			       MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
    764 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
    765 					       4, byte_order);
    766 	    if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
    767 		|| (record_full_arch_list_add_reg (regcache,
    768 						   MOXIE_SP_REGNUM))
    769 		|| record_full_arch_list_add_mem (tmpu32 - 12, 12))
    770 	      return -1;
    771 	  }
    772 	  break;
    773 	case 0x04: /* ret */
    774 	  {
    775 	    if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
    776 		|| (record_full_arch_list_add_reg (regcache,
    777 						   MOXIE_SP_REGNUM)))
    778 	      return -1;
    779 	  }
    780 	  break;
    781 	case 0x05: /* add.l */
    782 	  {
    783 	    int reg = (inst >> 4) & 0xf;
    784 	    if (record_full_arch_list_add_reg (regcache, reg))
    785 	      return -1;
    786 	  }
    787 	  break;
    788 	case 0x06: /* push */
    789 	  {
    790 	    int reg = (inst >> 4) & 0xf;
    791 	    regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
    792 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
    793 					       4, byte_order);
    794 	    if (record_full_arch_list_add_reg (regcache, reg)
    795 		|| record_full_arch_list_add_mem (tmpu32 - 4, 4))
    796 	      return -1;
    797 	  }
    798 	  break;
    799 	case 0x07: /* pop */
    800 	  {
    801 	    int a = (inst >> 4) & 0xf;
    802 	    int b = inst & 0xf;
    803 	    if (record_full_arch_list_add_reg (regcache, a)
    804 		|| record_full_arch_list_add_reg (regcache, b))
    805 	      return -1;
    806 	  }
    807 	  break;
    808 	case 0x08: /* lda.l */
    809 	  {
    810 	    int reg = (inst >> 4) & 0xf;
    811 	    if (record_full_arch_list_add_reg (regcache, reg))
    812 	      return -1;
    813 	  }
    814 	  break;
    815 	case 0x09: /* sta.l */
    816 	  {
    817 	    tmpu32 = (uint32_t) moxie_process_readu (addr+2, buf,
    818 						     4, byte_order);
    819 	    if (record_full_arch_list_add_mem (tmpu32, 4))
    820 	      return -1;
    821 	  }
    822 	  break;
    823 	case 0x0a: /* ld.l (register indirect) */
    824 	  {
    825 	    int reg = (inst >> 4) & 0xf;
    826 	    if (record_full_arch_list_add_reg (regcache, reg))
    827 	      return -1;
    828 	  }
    829 	  break;
    830 	case 0x0b: /* st.l */
    831 	  {
    832 	    int reg = (inst >> 4) & 0xf;
    833 	    regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
    834 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
    835 					       4, byte_order);
    836 	    if (record_full_arch_list_add_mem (tmpu32, 4))
    837 	      return -1;
    838 	  }
    839 	  break;
    840 	case 0x0c: /* ldo.l */
    841 	  {
    842 	    int reg = (inst >> 4) & 0xf;
    843 	    if (record_full_arch_list_add_reg (regcache, reg))
    844 	      return -1;
    845 	  }
    846 	  break;
    847 	case 0x0d: /* sto.l */
    848 	  {
    849 	    int reg = (inst >> 4) & 0xf;
    850 	    uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
    851 							       byte_order)) << 16 ) >> 16;
    852 	    regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
    853 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
    854 					       4, byte_order);
    855 	    tmpu32 += offset;
    856 	    if (record_full_arch_list_add_mem (tmpu32, 4))
    857 	      return -1;
    858 	  }
    859 	  break;
    860 	case 0x0e: /* cmp */
    861 	  {
    862 	    if (record_full_arch_list_add_reg (regcache, MOXIE_CC_REGNUM))
    863 	      return -1;
    864 	  }
    865 	  break;
    866 	case 0x0f: /* nop */
    867 	  {
    868 	    /* Do nothing.  */
    869 	    break;
    870 	  }
    871 	case 0x10: /* sex.b */
    872 	case 0x11: /* sex.s */
    873 	case 0x12: /* zex.b */
    874 	case 0x13: /* zex.s */
    875 	case 0x14: /* umul.x */
    876 	case 0x15: /* mul.x */
    877 	  {
    878 	    int reg = (inst >> 4) & 0xf;
    879 	    if (record_full_arch_list_add_reg (regcache, reg))
    880 	      return -1;
    881 	  }
    882 	  break;
    883 	case 0x16:
    884 	case 0x17:
    885 	case 0x18:
    886 	  {
    887 	    /* Do nothing.  */
    888 	    break;
    889 	  }
    890 	case 0x19: /* jsr */
    891 	  {
    892 	    regcache_raw_read (regcache,
    893 			       MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
    894 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
    895 					       4, byte_order);
    896 	    if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
    897 		|| (record_full_arch_list_add_reg (regcache,
    898 						   MOXIE_SP_REGNUM))
    899 		|| record_full_arch_list_add_mem (tmpu32 - 12, 12))
    900 	      return -1;
    901 	  }
    902 	  break;
    903 	case 0x1a: /* jmpa */
    904 	  {
    905 	    /* Do nothing.  */
    906 	  }
    907 	  break;
    908 	case 0x1b: /* ldi.b (immediate) */
    909 	case 0x1c: /* ld.b (register indirect) */
    910 	case 0x1d: /* lda.b */
    911 	  {
    912 	    int reg = (inst >> 4) & 0xf;
    913 	    if (record_full_arch_list_add_reg (regcache, reg))
    914 	      return -1;
    915 	  }
    916 	  break;
    917 	case 0x1e: /* st.b */
    918 	  {
    919 	    int reg = (inst >> 4) & 0xf;
    920 	    regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
    921 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
    922 					       4, byte_order);
    923 	    if (record_full_arch_list_add_mem (tmpu32, 1))
    924 	      return -1;
    925 	  }
    926 	  break;
    927 	case 0x1f: /* sta.b */
    928 	  {
    929 	    tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
    930 	    if (record_full_arch_list_add_mem (tmpu32, 1))
    931 	      return -1;
    932 	  }
    933 	  break;
    934 	case 0x20: /* ldi.s (immediate) */
    935 	case 0x21: /* ld.s (register indirect) */
    936 	case 0x22: /* lda.s */
    937 	  {
    938 	    int reg = (inst >> 4) & 0xf;
    939 	    if (record_full_arch_list_add_reg (regcache, reg))
    940 	      return -1;
    941 	  }
    942 	  break;
    943 	case 0x23: /* st.s */
    944 	  {
    945 	    int reg = (inst >> 4) & 0xf;
    946 	    regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
    947 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
    948 					       4, byte_order);
    949 	    if (record_full_arch_list_add_mem (tmpu32, 2))
    950 	      return -1;
    951 	  }
    952 	  break;
    953 	case 0x24: /* sta.s */
    954 	  {
    955 	    tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
    956 	    if (record_full_arch_list_add_mem (tmpu32, 2))
    957 	      return -1;
    958 	  }
    959 	  break;
    960 	case 0x25: /* jmp */
    961 	  {
    962 	    /* Do nothing.  */
    963 	  }
    964 	  break;
    965 	case 0x26: /* and */
    966 	case 0x27: /* lshr */
    967 	case 0x28: /* ashl */
    968 	case 0x29: /* sub */
    969 	case 0x2a: /* neg */
    970 	case 0x2b: /* or */
    971 	case 0x2c: /* not */
    972 	case 0x2d: /* ashr */
    973 	case 0x2e: /* xor */
    974 	case 0x2f: /* mul */
    975 	  {
    976 	    int reg = (inst >> 4) & 0xf;
    977 	    if (record_full_arch_list_add_reg (regcache, reg))
    978 	      return -1;
    979 	  }
    980 	  break;
    981 	case 0x30: /* swi */
    982 	  {
    983 	    /* We currently implement support for libgloss'
    984 	       system calls.  */
    985 
    986 	    int inum = moxie_process_readu (addr+2, buf, 4, byte_order);
    987 
    988 	    switch (inum)
    989 	      {
    990 	      case 0x1: /* SYS_exit */
    991 		{
    992 		  /* Do nothing.  */
    993 		}
    994 		break;
    995 	      case 0x2: /* SYS_open */
    996 		{
    997 		  if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
    998 		    return -1;
    999 		}
   1000 		break;
   1001 	      case 0x4: /* SYS_read */
   1002 		{
   1003 		  uint32_t length, ptr;
   1004 
   1005 		  /* Read buffer pointer is in $r1.  */
   1006 		  regcache_raw_read (regcache, 3, (gdb_byte *) & ptr);
   1007 		  ptr = extract_unsigned_integer ((gdb_byte *) & ptr,
   1008 						  4, byte_order);
   1009 
   1010 		  /* String length is at 0x12($fp).  */
   1011 		  regcache_raw_read (regcache,
   1012 				     MOXIE_FP_REGNUM, (gdb_byte *) & tmpu32);
   1013 		  tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
   1014 						     4, byte_order);
   1015 		  length = moxie_process_readu (tmpu32+20, buf, 4, byte_order);
   1016 
   1017 		  if (record_full_arch_list_add_mem (ptr, length))
   1018 		    return -1;
   1019 		}
   1020 		break;
   1021 	      case 0x5: /* SYS_write */
   1022 		{
   1023 		  if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
   1024 		    return -1;
   1025 		}
   1026 		break;
   1027 	      default:
   1028 		break;
   1029 	      }
   1030 	  }
   1031 	  break;
   1032 	case 0x31: /* div.l */
   1033 	case 0x32: /* udiv.l */
   1034 	case 0x33: /* mod.l */
   1035 	case 0x34: /* umod.l */
   1036 	  {
   1037 	    int reg = (inst >> 4) & 0xf;
   1038 	    if (record_full_arch_list_add_reg (regcache, reg))
   1039 	      return -1;
   1040 	  }
   1041 	  break;
   1042 	case 0x35: /* brk */
   1043 	  /* Do nothing.  */
   1044 	  break;
   1045 	case 0x36: /* ldo.b */
   1046 	  {
   1047 	    int reg = (inst >> 4) & 0xf;
   1048 	    if (record_full_arch_list_add_reg (regcache, reg))
   1049 	      return -1;
   1050 	  }
   1051 	  break;
   1052 	case 0x37: /* sto.b */
   1053 	  {
   1054 	    int reg = (inst >> 4) & 0xf;
   1055 	    uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
   1056 							       byte_order)) << 16 ) >> 16;
   1057 	    regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
   1058 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
   1059 					       4, byte_order);
   1060 	    tmpu32 += offset;
   1061 	    if (record_full_arch_list_add_mem (tmpu32, 1))
   1062 	      return -1;
   1063 	  }
   1064 	  break;
   1065 	case 0x38: /* ldo.s */
   1066 	  {
   1067 	    int reg = (inst >> 4) & 0xf;
   1068 	    if (record_full_arch_list_add_reg (regcache, reg))
   1069 	      return -1;
   1070 	  }
   1071 	  break;
   1072 	case 0x39: /* sto.s */
   1073 	  {
   1074 	    int reg = (inst >> 4) & 0xf;
   1075 	    uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
   1076 							       byte_order)) << 16 ) >> 16;
   1077 	    regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
   1078 	    tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
   1079 					       4, byte_order);
   1080 	    tmpu32 += offset;
   1081 	    if (record_full_arch_list_add_mem (tmpu32, 2))
   1082 	      return -1;
   1083 	  }
   1084 	  break;
   1085 	default:
   1086 	  /* Do nothing.  */
   1087 	  break;
   1088 	}
   1089     }
   1090 
   1091   if (record_full_arch_list_add_reg (regcache, MOXIE_PC_REGNUM))
   1092     return -1;
   1093   if (record_full_arch_list_add_end ())
   1094     return -1;
   1095   return 0;
   1096 }
   1097 
   1098 /* Allocate and initialize the moxie gdbarch object.  */
   1099 
   1100 static struct gdbarch *
   1101 moxie_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   1102 {
   1103   struct gdbarch *gdbarch;
   1104   struct gdbarch_tdep *tdep;
   1105 
   1106   /* If there is already a candidate, use it.  */
   1107   arches = gdbarch_list_lookup_by_info (arches, &info);
   1108   if (arches != NULL)
   1109     return arches->gdbarch;
   1110 
   1111   /* Allocate space for the new architecture.  */
   1112   tdep = XNEW (struct gdbarch_tdep);
   1113   gdbarch = gdbarch_alloc (&info, tdep);
   1114 
   1115   set_gdbarch_wchar_bit (gdbarch, 32);
   1116   set_gdbarch_wchar_signed (gdbarch, 0);
   1117 
   1118   set_gdbarch_read_pc (gdbarch, moxie_read_pc);
   1119   set_gdbarch_write_pc (gdbarch, moxie_write_pc);
   1120   set_gdbarch_unwind_sp (gdbarch, moxie_unwind_sp);
   1121 
   1122   set_gdbarch_num_regs (gdbarch, MOXIE_NUM_REGS);
   1123   set_gdbarch_sp_regnum (gdbarch, MOXIE_SP_REGNUM);
   1124   set_gdbarch_pc_regnum (gdbarch, MOXIE_PC_REGNUM);
   1125   set_gdbarch_register_name (gdbarch, moxie_register_name);
   1126   set_gdbarch_register_type (gdbarch, moxie_register_type);
   1127 
   1128   set_gdbarch_return_value (gdbarch, moxie_return_value);
   1129 
   1130   set_gdbarch_skip_prologue (gdbarch, moxie_skip_prologue);
   1131   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
   1132   set_gdbarch_breakpoint_kind_from_pc (gdbarch,
   1133 				       moxie_breakpoint::kind_from_pc);
   1134   set_gdbarch_sw_breakpoint_from_kind (gdbarch,
   1135 				       moxie_breakpoint::bp_from_kind);
   1136   set_gdbarch_frame_align (gdbarch, moxie_frame_align);
   1137 
   1138   frame_base_set_default (gdbarch, &moxie_frame_base);
   1139 
   1140   /* Methods for saving / extracting a dummy frame's ID.  The ID's
   1141      stack address must match the SP value returned by
   1142      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
   1143   set_gdbarch_dummy_id (gdbarch, moxie_dummy_id);
   1144 
   1145   set_gdbarch_unwind_pc (gdbarch, moxie_unwind_pc);
   1146 
   1147   set_gdbarch_print_insn (gdbarch, print_insn_moxie);
   1148 
   1149   /* Hook in ABI-specific overrides, if they have been registered.  */
   1150   gdbarch_init_osabi (info, gdbarch);
   1151 
   1152   /* Hook in the default unwinders.  */
   1153   frame_unwind_append_unwinder (gdbarch, &moxie_frame_unwind);
   1154 
   1155   /* Single stepping.  */
   1156   set_gdbarch_software_single_step (gdbarch, moxie_software_single_step);
   1157 
   1158   /* Support simple overlay manager.  */
   1159   set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
   1160 
   1161   /* Support reverse debugging.  */
   1162   set_gdbarch_process_record (gdbarch, moxie_process_record);
   1163 
   1164   return gdbarch;
   1165 }
   1166 
   1167 /* Register this machine's init routine.  */
   1168 
   1169 void
   1170 _initialize_moxie_tdep (void)
   1171 {
   1172   register_gdbarch_init (bfd_arch_moxie, moxie_gdbarch_init);
   1173 }
   1174