gdb/moxie-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for Moxie.

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

  3.    This file is part of GDB.

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

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

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

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

  34. #include "moxie-tdep.h"

  35. /* Local functions.  */

  36. extern void _initialize_moxie_tdep (void);

  37. /* Use an invalid address value as 'not available' marker.  */
  38. enum { REG_UNAVAIL = (CORE_ADDR) -1 };

  39. struct moxie_frame_cache
  40. {
  41.   /* Base address.  */
  42.   CORE_ADDR base;
  43.   CORE_ADDR pc;
  44.   LONGEST framesize;
  45.   CORE_ADDR saved_regs[MOXIE_NUM_REGS];
  46.   CORE_ADDR saved_sp;
  47. };

  48. /* Implement the "frame_align" gdbarch method.  */

  49. static CORE_ADDR
  50. moxie_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  51. {
  52.   /* Align to the size of an instruction (so that they can safely be
  53.      pushed onto the stack.  */
  54.   return sp & ~1;
  55. }

  56. /* Implement the "breakpoint_from_pc" gdbarch method.  */

  57. static const unsigned char *
  58. moxie_breakpoint_from_pc (struct gdbarch *gdbarch,
  59.                           CORE_ADDR *pcptr, int *lenptr)
  60. {
  61.   static unsigned char breakpoint[] = { 0x35, 0x00 };

  62.   *lenptr = sizeof (breakpoint);
  63.   return breakpoint;
  64. }

  65. /* Moxie register names.  */

  66. char *moxie_register_names[] = {
  67.   "$fp""$sp""$r0""$r1""$r2",
  68.   "$r3""$r4""$r5", "$r6", "$r7",
  69.   "$r8", "$r9", "$r10", "$r11", "$r12",
  70.   "$r13", "$pc", "$cc" };

  71. /* Implement the "register_name" gdbarch method.  */

  72. static const char *
  73. moxie_register_name (struct gdbarch *gdbarch, int reg_nr)
  74. {
  75.   if (reg_nr < 0)
  76.     return NULL;
  77.   if (reg_nr >= MOXIE_NUM_REGS)
  78.     return NULL;
  79.   return moxie_register_names[reg_nr];
  80. }

  81. /* Implement the "register_type" gdbarch method.  */

  82. static struct type *
  83. moxie_register_type (struct gdbarch *gdbarch, int reg_nr)
  84. {
  85.   if (reg_nr == MOXIE_PC_REGNUM)
  86.     return  builtin_type (gdbarch)->builtin_func_ptr;
  87.   else if (reg_nr == MOXIE_SP_REGNUM || reg_nr == MOXIE_FP_REGNUM)
  88.     return builtin_type (gdbarch)->builtin_data_ptr;
  89.   else
  90.     return builtin_type (gdbarch)->builtin_int32;
  91. }

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

  94. static void
  95. moxie_store_return_value (struct type *type, struct regcache *regcache,
  96.                          const void *valbuf)
  97. {
  98.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  99.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  100.   CORE_ADDR regval;
  101.   int len = TYPE_LENGTH (type);

  102.   /* Things always get returned in RET1_REGNUM, RET2_REGNUM.  */
  103.   regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
  104.   regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);
  105.   if (len > 4)
  106.     {
  107.       regval = extract_unsigned_integer ((gdb_byte *) valbuf + 4,
  108.                                          len - 4, byte_order);
  109.       regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
  110.     }
  111. }

  112. /* Decode the instructions within the given address range.  Decide
  113.    when we must have reached the end of the function prologue.  If a
  114.    frame_info pointer is provided, fill in its saved_regs etc.

  115.    Returns the address of the first instruction after the prologue.  */

  116. static CORE_ADDR
  117. moxie_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
  118.                         struct moxie_frame_cache *cache,
  119.                         struct gdbarch *gdbarch)
  120. {
  121.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  122.   CORE_ADDR next_addr;
  123.   ULONGEST inst, inst2;
  124.   LONGEST offset;
  125.   int regnum;

  126.   /* Record where the jsra instruction saves the PC and FP.  */
  127.   cache->saved_regs[MOXIE_PC_REGNUM] = -4;
  128.   cache->saved_regs[MOXIE_FP_REGNUM] = 0;
  129.   cache->framesize = 0;

  130.   if (start_addr >= end_addr)
  131.     return end_addr;

  132.   for (next_addr = start_addr; next_addr < end_addr; )
  133.     {
  134.       inst = read_memory_unsigned_integer (next_addr, 2, byte_order);

  135.       /* Match "push $sp $rN" where N is between 0 and 13 inclusive.  */
  136.       if (inst >= 0x0612 && inst <= 0x061f)
  137.         {
  138.           regnum = inst & 0x000f;
  139.           cache->framesize += 4;
  140.           cache->saved_regs[regnum] = cache->framesize;
  141.           next_addr += 2;
  142.         }
  143.       else
  144.         break;
  145.     }

  146.   inst = read_memory_unsigned_integer (next_addr, 2, byte_order);

  147.   /* Optional stack allocation for args and local vars <= 4
  148.      byte.  */
  149.   if (inst == 0x01e0)          /* ldi.l $r12, X */
  150.     {
  151.       offset = read_memory_integer (next_addr + 2, 4, byte_order);
  152.       inst2 = read_memory_unsigned_integer (next_addr + 6, 2, byte_order);

  153.       if (inst2 == 0x291e)     /* sub.l $sp, $r12 */
  154.         {
  155.           cache->framesize += offset;
  156.         }

  157.       return (next_addr + 8);
  158.     }
  159.   else if ((inst & 0xff00) == 0x9100)   /* dec $sp, X */
  160.     {
  161.       cache->framesize += (inst & 0x00ff);
  162.       next_addr += 2;

  163.       while (next_addr < end_addr)
  164.         {
  165.           inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
  166.           if ((inst & 0xff00) != 0x9100) /* no more dec $sp, X */
  167.             break;
  168.           cache->framesize += (inst & 0x00ff);
  169.           next_addr += 2;
  170.         }
  171.     }

  172.   return next_addr;
  173. }

  174. /* Find the end of function prologue.  */

  175. static CORE_ADDR
  176. moxie_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  177. {
  178.   CORE_ADDR func_addr = 0, func_end = 0;
  179.   const char *func_name;

  180.   /* See if we can determine the end of the prologue via the symbol table.
  181.      If so, then return either PC, or the PC after the prologue, whichever
  182.      is greater.  */
  183.   if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
  184.     {
  185.       CORE_ADDR post_prologue_pc
  186.         = skip_prologue_using_sal (gdbarch, func_addr);
  187.       if (post_prologue_pc != 0)
  188.         return max (pc, post_prologue_pc);
  189.       else
  190.         {
  191.           /* Can't determine prologue from the symbol table, need to examine
  192.              instructions.  */
  193.           struct symtab_and_line sal;
  194.           struct symbol *sym;
  195.           struct moxie_frame_cache cache;
  196.           CORE_ADDR plg_end;

  197.           memset (&cache, 0, sizeof cache);

  198.           plg_end = moxie_analyze_prologue (func_addr,
  199.                                             func_end, &cache, gdbarch);
  200.           /* Found a function.  */
  201.           sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL);
  202.           /* Don't use line number debug info for assembly source
  203.              files.  */
  204.           if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
  205.             {
  206.               sal = find_pc_line (func_addr, 0);
  207.               if (sal.end && sal.end < func_end)
  208.                 {
  209.                   /* Found a line number, use it as end of
  210.                      prologue.  */
  211.                   return sal.end;
  212.                 }
  213.             }
  214.           /* No useable line symbol.  Use result of prologue parsing
  215.              method.  */
  216.           return plg_end;
  217.         }
  218.     }

  219.   /* No function symbol -- just return the PC.  */
  220.   return (CORE_ADDR) pc;
  221. }

  222. struct moxie_unwind_cache
  223. {
  224.   /* The previous frame's inner most stack address.  Used as this
  225.      frame ID's stack_addr.  */
  226.   CORE_ADDR prev_sp;
  227.   /* The frame's base, optionally used by the high-level debug info.  */
  228.   CORE_ADDR base;
  229.   int size;
  230.   /* How far the SP and r13 (FP) have been offset from the start of
  231.      the stack frame (as defined by the previous frame's stack
  232.      pointer).  */
  233.   LONGEST sp_offset;
  234.   LONGEST r13_offset;
  235.   int uses_frame;
  236.   /* Table indicating the location of each and every register.  */
  237.   struct trad_frame_saved_reg *saved_regs;
  238. };

  239. /* Read an unsigned integer from the inferior, and adjust
  240.    endianess.  */
  241. static ULONGEST
  242. moxie_process_readu (CORE_ADDR addr, gdb_byte *buf,
  243.                      int length, enum bfd_endian byte_order)
  244. {
  245.   if (target_read_memory (addr, buf, length))
  246.     {
  247.       if (record_debug)
  248.         printf_unfiltered (_("Process record: error reading memory at "
  249.                              "addr 0x%s len = %d.\n"),
  250.                            paddress (target_gdbarch (), addr), length);
  251.       return -1;
  252.     }

  253.   return extract_unsigned_integer (buf, length, byte_order);
  254. }


  255. /* Helper macro to extract the signed 10-bit offset from a 16-bit
  256.    branch instruction.        */
  257. #define INST2OFFSET(o) ((((signed short)((o & ((1<<10)-1))<<6))>>6)<<1)

  258. /* Insert a single step breakpoint.  */

  259. static int
  260. moxie_software_single_step (struct frame_info *frame)
  261. {
  262.   struct gdbarch *gdbarch = get_frame_arch (frame);
  263.   struct address_space *aspace = get_frame_address_space (frame);
  264.   CORE_ADDR addr;
  265.   gdb_byte buf[4];
  266.   uint16_t inst;
  267.   uint32_t tmpu32;
  268.   ULONGEST fp;
  269.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  270.   struct regcache *regcache = get_current_regcache ();

  271.   addr = get_frame_pc (frame);

  272.   inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);

  273.   /* Decode instruction.  */
  274.   if (inst & (1 << 15))
  275.     {
  276.       if (inst & (1 << 14))
  277.         {
  278.           /* This is a Form 3 instruction.  */
  279.           int opcode = (inst >> 10 & 0xf);

  280.           switch (opcode)
  281.             {
  282.             case 0x00: /* beq */
  283.             case 0x01: /* bne */
  284.             case 0x02: /* blt */
  285.             case 0x03: /* bgt */
  286.             case 0x04: /* bltu */
  287.             case 0x05: /* bgtu */
  288.             case 0x06: /* bge */
  289.             case 0x07: /* ble */
  290.             case 0x08: /* bgeu */
  291.             case 0x09: /* bleu */
  292.               /* Insert breaks on both branches, because we can't currently tell
  293.                  which way things will go.  */
  294.               insert_single_step_breakpoint (gdbarch, aspace, addr + 2);
  295.               insert_single_step_breakpoint (gdbarch, aspace, addr + 2 + INST2OFFSET(inst));
  296.               break;
  297.             default:
  298.               {
  299.                 /* Do nothing.        */
  300.                 break;
  301.               }
  302.             }
  303.         }
  304.       else
  305.         {
  306.           /* This is a Form 2 instruction.  They are all 16 bits.  */
  307.           insert_single_step_breakpoint (gdbarch, aspace, addr + 2);
  308.         }
  309.     }
  310.   else
  311.     {
  312.       /* This is a Form 1 instruction.        */
  313.       int opcode = inst >> 8;

  314.       switch (opcode)
  315.         {
  316.           /* 16-bit instructions.  */
  317.         case 0x00: /* bad */
  318.         case 0x02: /* mov (register-to-register) */
  319.         case 0x05: /* add.l */
  320.         case 0x06: /* push */
  321.         case 0x07: /* pop */
  322.         case 0x0a: /* ld.l (register indirect) */
  323.         case 0x0b: /* st.l */
  324.         case 0x0e: /* cmp */
  325.         case 0x0f: /* nop */
  326.         case 0x10: /* sex.b */
  327.         case 0x11: /* sex.s */
  328.         case 0x12: /* zex.b */
  329.         case 0x13: /* zex.s */
  330.         case 0x14: /* umul.x */
  331.         case 0x15: /* mul.x */
  332.         case 0x16:
  333.         case 0x17:
  334.         case 0x18:
  335.         case 0x1c: /* ld.b (register indirect) */
  336.         case 0x1e: /* st.b */
  337.         case 0x21: /* ld.s (register indirect) */
  338.         case 0x23: /* st.s */
  339.         case 0x26: /* and */
  340.         case 0x27: /* lshr */
  341.         case 0x28: /* ashl */
  342.         case 0x29: /* sub.l */
  343.         case 0x2a: /* neg */
  344.         case 0x2b: /* or */
  345.         case 0x2c: /* not */
  346.         case 0x2d: /* ashr */
  347.         case 0x2e: /* xor */
  348.         case 0x2f: /* mul.l */
  349.         case 0x31: /* div.l */
  350.         case 0x32: /* udiv.l */
  351.         case 0x33: /* mod.l */
  352.         case 0x34: /* umod.l */
  353.           insert_single_step_breakpoint (gdbarch, aspace, addr + 2);
  354.           break;

  355.           /* 32-bit instructions.  */
  356.         case 0x0c: /* ldo.l */
  357.         case 0x0d: /* sto.l */
  358.         case 0x36: /* ldo.b */
  359.         case 0x37: /* sto.b */
  360.         case 0x38: /* ldo.s */
  361.         case 0x39: /* sto.s */
  362.           insert_single_step_breakpoint (gdbarch, aspace, addr + 4);
  363.           break;

  364.           /* 48-bit instructions.  */
  365.         case 0x01: /* ldi.l (immediate) */
  366.         case 0x08: /* lda.l */
  367.         case 0x09: /* sta.l */
  368.         case 0x1b: /* ldi.b (immediate) */
  369.         case 0x1d: /* lda.b */
  370.         case 0x1f: /* sta.b */
  371.         case 0x20: /* ldi.s (immediate) */
  372.         case 0x22: /* lda.s */
  373.         case 0x24: /* sta.s */
  374.           insert_single_step_breakpoint (gdbarch, aspace, addr + 6);
  375.           break;

  376.           /* Control flow instructions.         */
  377.         case 0x03: /* jsra */
  378.         case 0x1a: /* jmpa */
  379.           insert_single_step_breakpoint (gdbarch, aspace,
  380.                                          moxie_process_readu (addr + 2,
  381.                                                               buf, 4,
  382.                                                               byte_order));
  383.           break;

  384.         case 0x04: /* ret */
  385.           regcache_cooked_read_unsigned (regcache, MOXIE_FP_REGNUM, &fp);
  386.           insert_single_step_breakpoint (gdbarch, aspace,
  387.                                          moxie_process_readu (fp + 4,
  388.                                                               buf, 4,
  389.                                                               byte_order));
  390.           break;

  391.         case 0x19: /* jsr */
  392.         case 0x25: /* jmp */
  393.           regcache_raw_read (regcache,
  394.                              (inst >> 4) & 0xf, (gdb_byte *) & tmpu32);
  395.           insert_single_step_breakpoint (gdbarch, aspace,
  396.                                          tmpu32);
  397.           break;

  398.         case 0x30: /* swi */
  399.         case 0x35: /* brk */
  400.           /* Unsupported, for now.  */
  401.           break;
  402.         }
  403.     }

  404.   return 1;
  405. }

  406. /* Implement the "read_pc" gdbarch method.  */

  407. static CORE_ADDR
  408. moxie_read_pc (struct regcache *regcache)
  409. {
  410.   ULONGEST pc;

  411.   regcache_cooked_read_unsigned (regcache, MOXIE_PC_REGNUM, &pc);
  412.   return pc;
  413. }

  414. /* Implement the "write_pc" gdbarch method.  */

  415. static void
  416. moxie_write_pc (struct regcache *regcache, CORE_ADDR val)
  417. {
  418.   regcache_cooked_write_unsigned (regcache, MOXIE_PC_REGNUM, val);
  419. }

  420. /* Implement the "unwind_sp" gdbarch method.  */

  421. static CORE_ADDR
  422. moxie_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  423. {
  424.   return frame_unwind_register_unsigned (next_frame, MOXIE_SP_REGNUM);
  425. }

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

  428. static void
  429. moxie_extract_return_value (struct type *type, struct regcache *regcache,
  430.                            void *dst)
  431. {
  432.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  433.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  434.   bfd_byte *valbuf = dst;
  435.   int len = TYPE_LENGTH (type);
  436.   ULONGEST tmp;

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

  441.   /* Ignore return values more than 8 bytes in size because the moxie
  442.      returns anything more than 8 bytes in the stack.  */
  443.   if (len > 4)
  444.     {
  445.       regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
  446.       store_unsigned_integer (valbuf + len - 4, 4, byte_order, tmp);
  447.     }
  448. }

  449. /* Implement the "return_value" gdbarch method.  */

  450. static enum return_value_convention
  451. moxie_return_value (struct gdbarch *gdbarch, struct value *function,
  452.                    struct type *valtype, struct regcache *regcache,
  453.                    gdb_byte *readbuf, const gdb_byte *writebuf)
  454. {
  455.   if (TYPE_LENGTH (valtype) > 8)
  456.     return RETURN_VALUE_STRUCT_CONVENTION;
  457.   else
  458.     {
  459.       if (readbuf != NULL)
  460.         moxie_extract_return_value (valtype, regcache, readbuf);
  461.       if (writebuf != NULL)
  462.         moxie_store_return_value (valtype, regcache, writebuf);
  463.       return RETURN_VALUE_REGISTER_CONVENTION;
  464.     }
  465. }

  466. /* Allocate and initialize a moxie_frame_cache object.  */

  467. static struct moxie_frame_cache *
  468. moxie_alloc_frame_cache (void)
  469. {
  470.   struct moxie_frame_cache *cache;
  471.   int i;

  472.   cache = FRAME_OBSTACK_ZALLOC (struct moxie_frame_cache);

  473.   cache->base = 0;
  474.   cache->saved_sp = 0;
  475.   cache->pc = 0;
  476.   cache->framesize = 0;
  477.   for (i = 0; i < MOXIE_NUM_REGS; ++i)
  478.     cache->saved_regs[i] = REG_UNAVAIL;

  479.   return cache;
  480. }

  481. /* Populate a moxie_frame_cache object for this_frame.  */

  482. static struct moxie_frame_cache *
  483. moxie_frame_cache (struct frame_info *this_frame, void **this_cache)
  484. {
  485.   struct moxie_frame_cache *cache;
  486.   CORE_ADDR current_pc;
  487.   int i;

  488.   if (*this_cache)
  489.     return *this_cache;

  490.   cache = moxie_alloc_frame_cache ();
  491.   *this_cache = cache;

  492.   cache->base = get_frame_register_unsigned (this_frame, MOXIE_FP_REGNUM);
  493.   if (cache->base == 0)
  494.     return cache;

  495.   cache->pc = get_frame_func (this_frame);
  496.   current_pc = get_frame_pc (this_frame);
  497.   if (cache->pc)
  498.     {
  499.       struct gdbarch *gdbarch = get_frame_arch (this_frame);
  500.       moxie_analyze_prologue (cache->pc, current_pc, cache, gdbarch);
  501.     }

  502.   cache->saved_sp = cache->base - cache->framesize;

  503.   for (i = 0; i < MOXIE_NUM_REGS; ++i)
  504.     if (cache->saved_regs[i] != REG_UNAVAIL)
  505.       cache->saved_regs[i] = cache->base - cache->saved_regs[i];

  506.   return cache;
  507. }

  508. /* Implement the "unwind_pc" gdbarch method.  */

  509. static CORE_ADDR
  510. moxie_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  511. {
  512.   return frame_unwind_register_unsigned (next_frame, MOXIE_PC_REGNUM);
  513. }

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

  516. static void
  517. moxie_frame_this_id (struct frame_info *this_frame,
  518.                     void **this_prologue_cache, struct frame_id *this_id)
  519. {
  520.   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
  521.                                                    this_prologue_cache);

  522.   /* This marks the outermost frame.  */
  523.   if (cache->base == 0)
  524.     return;

  525.   *this_id = frame_id_build (cache->saved_sp, cache->pc);
  526. }

  527. /* Get the value of register regnum in the previous stack frame.  */

  528. static struct value *
  529. moxie_frame_prev_register (struct frame_info *this_frame,
  530.                           void **this_prologue_cache, int regnum)
  531. {
  532.   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
  533.                                                    this_prologue_cache);

  534.   gdb_assert (regnum >= 0);

  535.   if (regnum == MOXIE_SP_REGNUM && cache->saved_sp)
  536.     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);

  537.   if (regnum < MOXIE_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
  538.     return frame_unwind_got_memory (this_frame, regnum,
  539.                                     cache->saved_regs[regnum]);

  540.   return frame_unwind_got_register (this_frame, regnum, regnum);
  541. }

  542. static const struct frame_unwind moxie_frame_unwind = {
  543.   NORMAL_FRAME,
  544.   default_frame_unwind_stop_reason,
  545.   moxie_frame_this_id,
  546.   moxie_frame_prev_register,
  547.   NULL,
  548.   default_frame_sniffer
  549. };

  550. /* Return the base address of this_frame.  */

  551. static CORE_ADDR
  552. moxie_frame_base_address (struct frame_info *this_frame, void **this_cache)
  553. {
  554.   struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
  555.                                                        this_cache);

  556.   return cache->base;
  557. }

  558. static const struct frame_base moxie_frame_base = {
  559.   &moxie_frame_unwind,
  560.   moxie_frame_base_address,
  561.   moxie_frame_base_address,
  562.   moxie_frame_base_address
  563. };

  564. static struct frame_id
  565. moxie_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  566. {
  567.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, MOXIE_SP_REGNUM);

  568.   return frame_id_build (sp, get_frame_pc (this_frame));
  569. }

  570. /* Parse the current instruction and record the values of the registers and
  571.    memory that will be changed in current instruction to "record_arch_list".
  572.    Return -1 if something wrong.  */

  573. static int
  574. moxie_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
  575.                       CORE_ADDR addr)
  576. {
  577.   gdb_byte buf[4];
  578.   uint16_t inst;
  579.   uint32_t tmpu32;
  580.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  581.   if (record_debug > 1)
  582.     fprintf_unfiltered (gdb_stdlog, "Process record: moxie_process_record "
  583.                                     "addr = 0x%s\n",
  584.                         paddress (target_gdbarch (), addr));

  585.   inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);

  586.   /* Decode instruction.  */
  587.   if (inst & (1 << 15))
  588.     {
  589.       if (inst & (1 << 14))
  590.         {
  591.           /* This is a Form 3 instruction.  */
  592.           int opcode = (inst >> 10 & 0xf);

  593.           switch (opcode)
  594.             {
  595.             case 0x00: /* beq */
  596.             case 0x01: /* bne */
  597.             case 0x02: /* blt */
  598.             case 0x03: /* bgt */
  599.             case 0x04: /* bltu */
  600.             case 0x05: /* bgtu */
  601.             case 0x06: /* bge */
  602.             case 0x07: /* ble */
  603.             case 0x08: /* bgeu */
  604.             case 0x09: /* bleu */
  605.               /* Do nothing.  */
  606.               break;
  607.             default:
  608.               {
  609.                 /* Do nothing.  */
  610.                 break;
  611.               }
  612.             }
  613.         }
  614.       else
  615.         {
  616.           /* This is a Form 2 instruction.  */
  617.           int opcode = (inst >> 12 & 0x3);
  618.           switch (opcode)
  619.             {
  620.             case 0x00: /* inc */
  621.             case 0x01: /* dec */
  622.             case 0x02: /* gsr */
  623.               {
  624.                 int reg = (inst >> 8) & 0xf;
  625.                 if (record_full_arch_list_add_reg (regcache, reg))
  626.                   return -1;
  627.               }
  628.               break;
  629.             case 0x03: /* ssr */
  630.               {
  631.                 /* Do nothing until GDB learns about moxie's special
  632.                    registers.  */
  633.               }
  634.               break;
  635.             default:
  636.               /* Do nothing.  */
  637.               break;
  638.             }
  639.         }
  640.     }
  641.   else
  642.     {
  643.       /* This is a Form 1 instruction.  */
  644.       int opcode = inst >> 8;

  645.       switch (opcode)
  646.         {
  647.         case 0x00: /* nop */
  648.           /* Do nothing.  */
  649.           break;
  650.         case 0x01: /* ldi.l (immediate) */
  651.         case 0x02: /* mov (register-to-register) */
  652.           {
  653.             int reg = (inst >> 4) & 0xf;
  654.             if (record_full_arch_list_add_reg (regcache, reg))
  655.               return -1;
  656.           }
  657.           break;
  658.         case 0x03: /* jsra */
  659.           {
  660.             regcache_raw_read (regcache,
  661.                                MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
  662.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  663.                                                4, byte_order);
  664.             if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
  665.                 || (record_full_arch_list_add_reg (regcache,
  666.                                                    MOXIE_SP_REGNUM))
  667.                 || record_full_arch_list_add_mem (tmpu32 - 12, 12))
  668.               return -1;
  669.           }
  670.           break;
  671.         case 0x04: /* ret */
  672.           {
  673.             if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
  674.                 || (record_full_arch_list_add_reg (regcache,
  675.                                                    MOXIE_SP_REGNUM)))
  676.               return -1;
  677.           }
  678.           break;
  679.         case 0x05: /* add.l */
  680.           {
  681.             int reg = (inst >> 4) & 0xf;
  682.             if (record_full_arch_list_add_reg (regcache, reg))
  683.               return -1;
  684.           }
  685.           break;
  686.         case 0x06: /* push */
  687.           {
  688.             int reg = (inst >> 4) & 0xf;
  689.             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
  690.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  691.                                                4, byte_order);
  692.             if (record_full_arch_list_add_reg (regcache, reg)
  693.                 || record_full_arch_list_add_mem (tmpu32 - 4, 4))
  694.               return -1;
  695.           }
  696.           break;
  697.         case 0x07: /* pop */
  698.           {
  699.             int a = (inst >> 4) & 0xf;
  700.             int b = inst & 0xf;
  701.             if (record_full_arch_list_add_reg (regcache, a)
  702.                 || record_full_arch_list_add_reg (regcache, b))
  703.               return -1;
  704.           }
  705.           break;
  706.         case 0x08: /* lda.l */
  707.           {
  708.             int reg = (inst >> 4) & 0xf;
  709.             if (record_full_arch_list_add_reg (regcache, reg))
  710.               return -1;
  711.           }
  712.           break;
  713.         case 0x09: /* sta.l */
  714.           {
  715.             tmpu32 = (uint32_t) moxie_process_readu (addr+2, buf,
  716.                                                      4, byte_order);
  717.             if (record_full_arch_list_add_mem (tmpu32, 4))
  718.               return -1;
  719.           }
  720.           break;
  721.         case 0x0a: /* ld.l (register indirect) */
  722.           {
  723.             int reg = (inst >> 4) & 0xf;
  724.             if (record_full_arch_list_add_reg (regcache, reg))
  725.               return -1;
  726.           }
  727.           break;
  728.         case 0x0b: /* st.l */
  729.           {
  730.             int reg = (inst >> 4) & 0xf;
  731.             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
  732.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  733.                                                4, byte_order);
  734.             if (record_full_arch_list_add_mem (tmpu32, 4))
  735.               return -1;
  736.           }
  737.           break;
  738.         case 0x0c: /* ldo.l */
  739.           {
  740.             int reg = (inst >> 4) & 0xf;
  741.             if (record_full_arch_list_add_reg (regcache, reg))
  742.               return -1;
  743.           }
  744.           break;
  745.         case 0x0d: /* sto.l */
  746.           {
  747.             int reg = (inst >> 4) & 0xf;
  748.             uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
  749.                                                                byte_order)) << 16 ) >> 16;
  750.             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
  751.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  752.                                                4, byte_order);
  753.             tmpu32 += offset;
  754.             if (record_full_arch_list_add_mem (tmpu32, 4))
  755.               return -1;
  756.           }
  757.           break;
  758.         case 0x0e: /* cmp */
  759.           {
  760.             if (record_full_arch_list_add_reg (regcache, MOXIE_CC_REGNUM))
  761.               return -1;
  762.           }
  763.           break;
  764.         case 0x0f: /* nop */
  765.           {
  766.             /* Do nothing.  */
  767.             break;
  768.           }
  769.         case 0x10: /* sex.b */
  770.         case 0x11: /* sex.s */
  771.         case 0x12: /* zex.b */
  772.         case 0x13: /* zex.s */
  773.         case 0x14: /* umul.x */
  774.         case 0x15: /* mul.x */
  775.           {
  776.             int reg = (inst >> 4) & 0xf;
  777.             if (record_full_arch_list_add_reg (regcache, reg))
  778.               return -1;
  779.           }
  780.           break;
  781.         case 0x16:
  782.         case 0x17:
  783.         case 0x18:
  784.           {
  785.             /* Do nothing.  */
  786.             break;
  787.           }
  788.         case 0x19: /* jsr */
  789.           {
  790.             regcache_raw_read (regcache,
  791.                                MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
  792.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  793.                                                4, byte_order);
  794.             if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
  795.                 || (record_full_arch_list_add_reg (regcache,
  796.                                                    MOXIE_SP_REGNUM))
  797.                 || record_full_arch_list_add_mem (tmpu32 - 12, 12))
  798.               return -1;
  799.           }
  800.           break;
  801.         case 0x1a: /* jmpa */
  802.           {
  803.             /* Do nothing.  */
  804.           }
  805.           break;
  806.         case 0x1b: /* ldi.b (immediate) */
  807.         case 0x1c: /* ld.b (register indirect) */
  808.         case 0x1d: /* lda.b */
  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 0x1e: /* st.b */
  816.           {
  817.             int reg = (inst >> 4) & 0xf;
  818.             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
  819.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  820.                                                4, byte_order);
  821.             if (record_full_arch_list_add_mem (tmpu32, 1))
  822.               return -1;
  823.           }
  824.           break;
  825.         case 0x1f: /* sta.b */
  826.           {
  827.             tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
  828.             if (record_full_arch_list_add_mem (tmpu32, 1))
  829.               return -1;
  830.           }
  831.           break;
  832.         case 0x20: /* ldi.s (immediate) */
  833.         case 0x21: /* ld.s (register indirect) */
  834.         case 0x22: /* lda.s */
  835.           {
  836.             int reg = (inst >> 4) & 0xf;
  837.             if (record_full_arch_list_add_reg (regcache, reg))
  838.               return -1;
  839.           }
  840.           break;
  841.         case 0x23: /* st.s */
  842.           {
  843.             int reg = (inst >> 4) & 0xf;
  844.             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
  845.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  846.                                                4, byte_order);
  847.             if (record_full_arch_list_add_mem (tmpu32, 2))
  848.               return -1;
  849.           }
  850.           break;
  851.         case 0x24: /* sta.s */
  852.           {
  853.             tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
  854.             if (record_full_arch_list_add_mem (tmpu32, 2))
  855.               return -1;
  856.           }
  857.           break;
  858.         case 0x25: /* jmp */
  859.           {
  860.             /* Do nothing.  */
  861.           }
  862.           break;
  863.         case 0x26: /* and */
  864.         case 0x27: /* lshr */
  865.         case 0x28: /* ashl */
  866.         case 0x29: /* sub */
  867.         case 0x2a: /* neg */
  868.         case 0x2b: /* or */
  869.         case 0x2c: /* not */
  870.         case 0x2d: /* ashr */
  871.         case 0x2e: /* xor */
  872.         case 0x2f: /* mul */
  873.           {
  874.             int reg = (inst >> 4) & 0xf;
  875.             if (record_full_arch_list_add_reg (regcache, reg))
  876.               return -1;
  877.           }
  878.           break;
  879.         case 0x30: /* swi */
  880.           {
  881.             /* We currently implement support for libgloss'
  882.                system calls.  */

  883.             int inum = moxie_process_readu (addr+2, buf, 4, byte_order);

  884.             switch (inum)
  885.               {
  886.               case 0x1: /* SYS_exit */
  887.                 {
  888.                   /* Do nothing.  */
  889.                 }
  890.                 break;
  891.               case 0x2: /* SYS_open */
  892.                 {
  893.                   if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
  894.                     return -1;
  895.                 }
  896.                 break;
  897.               case 0x4: /* SYS_read */
  898.                 {
  899.                   uint32_t length, ptr;

  900.                   /* Read buffer pointer is in $r1.  */
  901.                   regcache_raw_read (regcache, 3, (gdb_byte *) & ptr);
  902.                   ptr = extract_unsigned_integer ((gdb_byte *) & ptr,
  903.                                                   4, byte_order);

  904.                   /* String length is at 0x12($fp).  */
  905.                   regcache_raw_read (regcache,
  906.                                      MOXIE_FP_REGNUM, (gdb_byte *) & tmpu32);
  907.                   tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  908.                                                      4, byte_order);
  909.                   length = moxie_process_readu (tmpu32+20, buf, 4, byte_order);

  910.                   if (record_full_arch_list_add_mem (ptr, length))
  911.                     return -1;
  912.                 }
  913.                 break;
  914.               case 0x5: /* SYS_write */
  915.                 {
  916.                   if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
  917.                     return -1;
  918.                 }
  919.                 break;
  920.               default:
  921.                 break;
  922.               }
  923.           }
  924.           break;
  925.         case 0x31: /* div.l */
  926.         case 0x32: /* udiv.l */
  927.         case 0x33: /* mod.l */
  928.         case 0x34: /* umod.l */
  929.           {
  930.             int reg = (inst >> 4) & 0xf;
  931.             if (record_full_arch_list_add_reg (regcache, reg))
  932.               return -1;
  933.           }
  934.           break;
  935.         case 0x35: /* brk */
  936.           /* Do nothing.  */
  937.           break;
  938.         case 0x36: /* ldo.b */
  939.           {
  940.             int reg = (inst >> 4) & 0xf;
  941.             if (record_full_arch_list_add_reg (regcache, reg))
  942.               return -1;
  943.           }
  944.           break;
  945.         case 0x37: /* sto.b */
  946.           {
  947.             int reg = (inst >> 4) & 0xf;
  948.             uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
  949.                                                                byte_order)) << 16 ) >> 16;
  950.             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
  951.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  952.                                                4, byte_order);
  953.             tmpu32 += offset;
  954.             if (record_full_arch_list_add_mem (tmpu32, 1))
  955.               return -1;
  956.           }
  957.           break;
  958.         case 0x38: /* ldo.s */
  959.           {
  960.             int reg = (inst >> 4) & 0xf;
  961.             if (record_full_arch_list_add_reg (regcache, reg))
  962.               return -1;
  963.           }
  964.           break;
  965.         case 0x39: /* sto.s */
  966.           {
  967.             int reg = (inst >> 4) & 0xf;
  968.             uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
  969.                                                                byte_order)) << 16 ) >> 16;
  970.             regcache_raw_read (regcache, reg, (gdb_byte *) & tmpu32);
  971.             tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  972.                                                4, byte_order);
  973.             tmpu32 += offset;
  974.             if (record_full_arch_list_add_mem (tmpu32, 2))
  975.               return -1;
  976.           }
  977.           break;
  978.         default:
  979.           /* Do nothing.  */
  980.           break;
  981.         }
  982.     }

  983.   if (record_full_arch_list_add_reg (regcache, MOXIE_PC_REGNUM))
  984.     return -1;
  985.   if (record_full_arch_list_add_end ())
  986.     return -1;
  987.   return 0;
  988. }

  989. /* Allocate and initialize the moxie gdbarch object.  */

  990. static struct gdbarch *
  991. moxie_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  992. {
  993.   struct gdbarch *gdbarch;
  994.   struct gdbarch_tdep *tdep;

  995.   /* If there is already a candidate, use it.  */
  996.   arches = gdbarch_list_lookup_by_info (arches, &info);
  997.   if (arches != NULL)
  998.     return arches->gdbarch;

  999.   /* Allocate space for the new architecture.  */
  1000.   tdep = XNEW (struct gdbarch_tdep);
  1001.   gdbarch = gdbarch_alloc (&info, tdep);

  1002.   set_gdbarch_read_pc (gdbarch, moxie_read_pc);
  1003.   set_gdbarch_write_pc (gdbarch, moxie_write_pc);
  1004.   set_gdbarch_unwind_sp (gdbarch, moxie_unwind_sp);

  1005.   set_gdbarch_num_regs (gdbarch, MOXIE_NUM_REGS);
  1006.   set_gdbarch_sp_regnum (gdbarch, MOXIE_SP_REGNUM);
  1007.   set_gdbarch_pc_regnum (gdbarch, MOXIE_PC_REGNUM);
  1008.   set_gdbarch_register_name (gdbarch, moxie_register_name);
  1009.   set_gdbarch_register_type (gdbarch, moxie_register_type);

  1010.   set_gdbarch_return_value (gdbarch, moxie_return_value);

  1011.   set_gdbarch_skip_prologue (gdbarch, moxie_skip_prologue);
  1012.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1013.   set_gdbarch_breakpoint_from_pc (gdbarch, moxie_breakpoint_from_pc);
  1014.   set_gdbarch_frame_align (gdbarch, moxie_frame_align);

  1015.   frame_base_set_default (gdbarch, &moxie_frame_base);

  1016.   /* Methods for saving / extracting a dummy frame's ID.  The ID's
  1017.      stack address must match the SP value returned by
  1018.      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
  1019.   set_gdbarch_dummy_id (gdbarch, moxie_dummy_id);

  1020.   set_gdbarch_unwind_pc (gdbarch, moxie_unwind_pc);

  1021.   set_gdbarch_print_insn (gdbarch, print_insn_moxie);

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

  1024.   /* Hook in the default unwinders.  */
  1025.   frame_unwind_append_unwinder (gdbarch, &moxie_frame_unwind);

  1026.   /* Single stepping.  */
  1027.   set_gdbarch_software_single_step (gdbarch, moxie_software_single_step);

  1028.   /* Support simple overlay manager.  */
  1029.   set_gdbarch_overlay_update (gdbarch, simple_overlay_update);

  1030.   /* Support reverse debugging.  */
  1031.   set_gdbarch_process_record (gdbarch, moxie_process_record);

  1032.   return gdbarch;
  1033. }

  1034. /* Register this machine's init routine.  */

  1035. void
  1036. _initialize_moxie_tdep (void)
  1037. {
  1038.   register_gdbarch_init (bfd_arch_moxie, moxie_gdbarch_init);
  1039. }