gdb/mn10300-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.

  2.    Copyright (C) 1996-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 "arch-utils.h"
  16. #include "dis-asm.h"
  17. #include "gdbtypes.h"
  18. #include "regcache.h"
  19. #include "gdbcore.h"        /* For write_memory_unsigned_integer.  */
  20. #include "value.h"
  21. #include "frame.h"
  22. #include "frame-unwind.h"
  23. #include "frame-base.h"
  24. #include "symtab.h"
  25. #include "dwarf2-frame.h"
  26. #include "osabi.h"
  27. #include "infcall.h"
  28. #include "prologue-value.h"
  29. #include "target.h"

  30. #include "mn10300-tdep.h"


  31. /* The am33-2 has 64 registers.  */
  32. #define MN10300_MAX_NUM_REGS 64

  33. /* This structure holds the results of a prologue analysis.  */
  34. struct mn10300_prologue
  35. {
  36.   /* The architecture for which we generated this prologue info.  */
  37.   struct gdbarch *gdbarch;

  38.   /* The offset from the frame base to the stack pointer --- always
  39.      zero or negative.

  40.      Calling this a "size" is a bit misleading, but given that the
  41.      stack grows downwards, using offsets for everything keeps one
  42.      from going completely sign-crazy: you never change anything's
  43.      sign for an ADD instruction; always change the second operand's
  44.      sign for a SUB instruction; and everything takes care of
  45.      itself.  */
  46.   int frame_size;

  47.   /* Non-zero if this function has initialized the frame pointer from
  48.      the stack pointer, zero otherwise.  */
  49.   int has_frame_ptr;

  50.   /* If has_frame_ptr is non-zero, this is the offset from the frame
  51.      base to where the frame pointer points.  This is always zero or
  52.      negative.  */
  53.   int frame_ptr_offset;

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

  58.   /* reg_offset[R] is the offset from the CFA at which register R is
  59.      saved, or 1 if register R has not been saved.  (Real values are
  60.      always zero or negative.)  */
  61.   int reg_offset[MN10300_MAX_NUM_REGS];
  62. };


  63. /* Compute the alignment required by a type.  */

  64. static int
  65. mn10300_type_align (struct type *type)
  66. {
  67.   int i, align = 1;

  68.   switch (TYPE_CODE (type))
  69.     {
  70.     case TYPE_CODE_INT:
  71.     case TYPE_CODE_ENUM:
  72.     case TYPE_CODE_SET:
  73.     case TYPE_CODE_RANGE:
  74.     case TYPE_CODE_CHAR:
  75.     case TYPE_CODE_BOOL:
  76.     case TYPE_CODE_FLT:
  77.     case TYPE_CODE_PTR:
  78.     case TYPE_CODE_REF:
  79.       return TYPE_LENGTH (type);

  80.     case TYPE_CODE_COMPLEX:
  81.       return TYPE_LENGTH (type) / 2;

  82.     case TYPE_CODE_STRUCT:
  83.     case TYPE_CODE_UNION:
  84.       for (i = 0; i < TYPE_NFIELDS (type); i++)
  85.         {
  86.           int falign = mn10300_type_align (TYPE_FIELD_TYPE (type, i));
  87.           while (align < falign)
  88.             align <<= 1;
  89.         }
  90.       return align;

  91.     case TYPE_CODE_ARRAY:
  92.       /* HACK!  Structures containing arrays, even small ones, are not
  93.          elligible for returning in registers.  */
  94.       return 256;

  95.     case TYPE_CODE_TYPEDEF:
  96.       return mn10300_type_align (check_typedef (type));

  97.     default:
  98.       internal_error (__FILE__, __LINE__, _("bad switch"));
  99.     }
  100. }

  101. /* Should call_function allocate stack space for a struct return?  */
  102. static int
  103. mn10300_use_struct_convention (struct type *type)
  104. {
  105.   /* Structures bigger than a pair of words can't be returned in
  106.      registers.  */
  107.   if (TYPE_LENGTH (type) > 8)
  108.     return 1;

  109.   switch (TYPE_CODE (type))
  110.     {
  111.     case TYPE_CODE_STRUCT:
  112.     case TYPE_CODE_UNION:
  113.       /* Structures with a single field are handled as the field
  114.          itself.  */
  115.       if (TYPE_NFIELDS (type) == 1)
  116.         return mn10300_use_struct_convention (TYPE_FIELD_TYPE (type, 0));

  117.       /* Structures with word or double-word size are passed in memory, as
  118.          long as they require at least word alignment.  */
  119.       if (mn10300_type_align (type) >= 4)
  120.         return 0;

  121.       return 1;

  122.       /* Arrays are addressable, so they're never returned in
  123.          registers.  This condition can only hold when the array is
  124.          the only field of a struct or union.  */
  125.     case TYPE_CODE_ARRAY:
  126.       return 1;

  127.     case TYPE_CODE_TYPEDEF:
  128.       return mn10300_use_struct_convention (check_typedef (type));

  129.     default:
  130.       return 0;
  131.     }
  132. }

  133. static void
  134. mn10300_store_return_value (struct gdbarch *gdbarch, struct type *type,
  135.                             struct regcache *regcache, const gdb_byte *valbuf)
  136. {
  137.   int len = TYPE_LENGTH (type);
  138.   int reg, regsz;

  139.   if (TYPE_CODE (type) == TYPE_CODE_PTR)
  140.     reg = 4;
  141.   else
  142.     reg = 0;

  143.   regsz = register_size (gdbarch, reg);

  144.   if (len <= regsz)
  145.     regcache_raw_write_part (regcache, reg, 0, len, valbuf);
  146.   else if (len <= 2 * regsz)
  147.     {
  148.       regcache_raw_write (regcache, reg, valbuf);
  149.       gdb_assert (regsz == register_size (gdbarch, reg + 1));
  150.       regcache_raw_write_part (regcache, reg+1, 0,
  151.                                len - regsz, valbuf + regsz);
  152.     }
  153.   else
  154.     internal_error (__FILE__, __LINE__,
  155.                     _("Cannot store return value %d bytes long."), len);
  156. }

  157. static void
  158. mn10300_extract_return_value (struct gdbarch *gdbarch, struct type *type,
  159.                               struct regcache *regcache, void *valbuf)
  160. {
  161.   gdb_byte buf[MAX_REGISTER_SIZE];
  162.   int len = TYPE_LENGTH (type);
  163.   int reg, regsz;

  164.   if (TYPE_CODE (type) == TYPE_CODE_PTR)
  165.     reg = 4;
  166.   else
  167.     reg = 0;

  168.   regsz = register_size (gdbarch, reg);
  169.   if (len <= regsz)
  170.     {
  171.       regcache_raw_read (regcache, reg, buf);
  172.       memcpy (valbuf, buf, len);
  173.     }
  174.   else if (len <= 2 * regsz)
  175.     {
  176.       regcache_raw_read (regcache, reg, buf);
  177.       memcpy (valbuf, buf, regsz);
  178.       gdb_assert (regsz == register_size (gdbarch, reg + 1));
  179.       regcache_raw_read (regcache, reg + 1, buf);
  180.       memcpy ((char *) valbuf + regsz, buf, len - regsz);
  181.     }
  182.   else
  183.     internal_error (__FILE__, __LINE__,
  184.                     _("Cannot extract return value %d bytes long."), len);
  185. }

  186. /* Determine, for architecture GDBARCH, how a return value of TYPE
  187.    should be returned.  If it is supposed to be returned in registers,
  188.    and READBUF is non-zero, read the appropriate value from REGCACHE,
  189.    and copy it into READBUF.  If WRITEBUF is non-zero, write the value
  190.    from WRITEBUF into REGCACHE.  */

  191. static enum return_value_convention
  192. mn10300_return_value (struct gdbarch *gdbarch, struct value *function,
  193.                       struct type *type, struct regcache *regcache,
  194.                       gdb_byte *readbuf, const gdb_byte *writebuf)
  195. {
  196.   if (mn10300_use_struct_convention (type))
  197.     return RETURN_VALUE_STRUCT_CONVENTION;

  198.   if (readbuf)
  199.     mn10300_extract_return_value (gdbarch, type, regcache, readbuf);
  200.   if (writebuf)
  201.     mn10300_store_return_value (gdbarch, type, regcache, writebuf);

  202.   return RETURN_VALUE_REGISTER_CONVENTION;
  203. }

  204. static char *
  205. register_name (int reg, char **regs, long sizeof_regs)
  206. {
  207.   if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0]))
  208.     return NULL;
  209.   else
  210.     return regs[reg];
  211. }

  212. static const char *
  213. mn10300_generic_register_name (struct gdbarch *gdbarch, int reg)
  214. {
  215.   static char *regs[] =
  216.   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
  217.     "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
  218.     "", "", "", "", "", "", "", "",
  219.     "", "", "", "", "", "", "", "fp"
  220.   };
  221.   return register_name (reg, regs, sizeof regs);
  222. }


  223. static const char *
  224. am33_register_name (struct gdbarch *gdbarch, int reg)
  225. {
  226.   static char *regs[] =
  227.   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
  228.     "sp", "pc", "mdr", "psw", "lir", "lar", "",
  229.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  230.     "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""
  231.   };
  232.   return register_name (reg, regs, sizeof regs);
  233. }

  234. static const char *
  235. am33_2_register_name (struct gdbarch *gdbarch, int reg)
  236. {
  237.   static char *regs[] =
  238.   {
  239.     "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
  240.     "sp", "pc", "mdr", "psw", "lir", "lar", "mdrq", "r0",
  241.     "r1", "r2", "r3", "r4", "r5", "r6", "r7", "ssp",
  242.     "msp", "usp", "mcrh", "mcrl", "mcvf", "fpcr", "", "",
  243.     "fs0", "fs1", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7",
  244.     "fs8", "fs9", "fs10", "fs11", "fs12", "fs13", "fs14", "fs15",
  245.     "fs16", "fs17", "fs18", "fs19", "fs20", "fs21", "fs22", "fs23",
  246.     "fs24", "fs25", "fs26", "fs27", "fs28", "fs29", "fs30", "fs31"
  247.   };
  248.   return register_name (reg, regs, sizeof regs);
  249. }

  250. static struct type *
  251. mn10300_register_type (struct gdbarch *gdbarch, int reg)
  252. {
  253.   return builtin_type (gdbarch)->builtin_int;
  254. }

  255. static CORE_ADDR
  256. mn10300_read_pc (struct regcache *regcache)
  257. {
  258.   ULONGEST val;
  259.   regcache_cooked_read_unsigned (regcache, E_PC_REGNUM, &val);
  260.   return val;
  261. }

  262. static void
  263. mn10300_write_pc (struct regcache *regcache, CORE_ADDR val)
  264. {
  265.   regcache_cooked_write_unsigned (regcache, E_PC_REGNUM, val);
  266. }

  267. /* The breakpoint instruction must be the same size as the smallest
  268.    instruction in the instruction set.

  269.    The Matsushita mn10x00 processors have single byte instructions
  270.    so we need a single byte breakpoint.  Matsushita hasn't defined
  271.    one, so we defined it ourselves.  */

  272. static const unsigned char *
  273. mn10300_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *bp_addr,
  274.                             int *bp_size)
  275. {
  276.   static gdb_byte breakpoint[] = {0xff};
  277.   *bp_size = 1;
  278.   return breakpoint;
  279. }

  280. /* Model the semantics of pushing a register onto the stack.  This
  281.    is a helper function for mn10300_analyze_prologue, below.  */
  282. static void
  283. push_reg (pv_t *regs, struct pv_area *stack, int regnum)
  284. {
  285.   regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], -4);
  286.   pv_area_store (stack, regs[E_SP_REGNUM], 4, regs[regnum]);
  287. }

  288. /* Translate an "r" register number extracted from an instruction encoding
  289.    into a GDB register number.  Adapted from a simulator function
  290.    of the same name; see am33.igen.  */
  291. static int
  292. translate_rreg (int rreg)
  293. {
  294. /* The higher register numbers actually correspond to the
  295.      basic machine's address and data registers.  */
  296.   if (rreg > 7 && rreg < 12)
  297.     return E_A0_REGNUM + rreg - 8;
  298.   else if (rreg > 11 && rreg < 16)
  299.     return E_D0_REGNUM + rreg - 12;
  300.   else
  301.     return E_E0_REGNUM + rreg;
  302. }

  303. /* Find saved registers in a 'struct pv_area'; we pass this to pv_area_scan.

  304.    If VALUE is a saved register, ADDR says it was saved at a constant
  305.    offset from the frame base, and SIZE indicates that the whole
  306.    register was saved, record its offset in RESULT_UNTYPED.  */
  307. static void
  308. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  309. {
  310.   struct mn10300_prologue *result = (struct mn10300_prologue *) result_untyped;

  311.   if (value.kind == pvk_register
  312.       && value.k == 0
  313.       && pv_is_register (addr, E_SP_REGNUM)
  314.       && size == register_size (result->gdbarch, value.reg))
  315.     result->reg_offset[value.reg] = addr.k;
  316. }

  317. /* Analyze the prologue to determine where registers are saved,
  318.    the end of the prologue, etc.  The result of this analysis is
  319.    returned in RESULT.  See struct mn10300_prologue above for more
  320.    information.  */
  321. static void
  322. mn10300_analyze_prologue (struct gdbarch *gdbarch,
  323.                           CORE_ADDR start_pc, CORE_ADDR limit_pc,
  324.                           struct mn10300_prologue *result)
  325. {
  326.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  327.   CORE_ADDR pc;
  328.   int rn;
  329.   pv_t regs[MN10300_MAX_NUM_REGS];
  330.   struct pv_area *stack;
  331.   struct cleanup *back_to;
  332.   CORE_ADDR after_last_frame_setup_insn = start_pc;
  333.   int am33_mode = AM33_MODE (gdbarch);

  334.   memset (result, 0, sizeof (*result));
  335.   result->gdbarch = gdbarch;

  336.   for (rn = 0; rn < MN10300_MAX_NUM_REGS; rn++)
  337.     {
  338.       regs[rn] = pv_register (rn, 0);
  339.       result->reg_offset[rn] = 1;
  340.     }
  341.   stack = make_pv_area (E_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  342.   back_to = make_cleanup_free_pv_area (stack);

  343. /* The typical call instruction will have saved the return address on the
  344.     stack.  Space for the return address has already been preallocated in
  345.     the caller's frame.  It's possible, such as when using -mrelax with gcc
  346.     that other registers were saved as well.  If this happens, we really
  347.     have no chance of deciphering the frame.  DWARF info can save the day
  348.     when this happens.  */
  349.   pv_area_store (stack, regs[E_SP_REGNUM], 4, regs[E_PC_REGNUM]);

  350.   pc = start_pc;
  351.   while (pc < limit_pc)
  352.     {
  353.       int status;
  354.       gdb_byte instr[2];

  355.       /* Instructions can be as small as one byte; however, we usually
  356.          need at least two bytes to do the decoding, so fetch that many
  357.          to begin with.  */
  358.       status = target_read_memory (pc, instr, 2);
  359.       if (status != 0)
  360.         break;

  361.       /* movm [regs], sp  */
  362.       if (instr[0] == 0xcf)
  363.         {
  364.           gdb_byte save_mask;

  365.           save_mask = instr[1];

  366.           if ((save_mask & movm_exreg0_bit) && am33_mode)
  367.             {
  368.               push_reg (regs, stack, E_E2_REGNUM);
  369.               push_reg (regs, stack, E_E3_REGNUM);
  370.             }
  371.           if ((save_mask & movm_exreg1_bit) && am33_mode)
  372.             {
  373.               push_reg (regs, stack, E_E4_REGNUM);
  374.               push_reg (regs, stack, E_E5_REGNUM);
  375.               push_reg (regs, stack, E_E6_REGNUM);
  376.               push_reg (regs, stack, E_E7_REGNUM);
  377.             }
  378.           if ((save_mask & movm_exother_bit) && am33_mode)
  379.             {
  380.               push_reg (regs, stack, E_E0_REGNUM);
  381.               push_reg (regs, stack, E_E1_REGNUM);
  382.               push_reg (regs, stack, E_MDRQ_REGNUM);
  383.               push_reg (regs, stack, E_MCRH_REGNUM);
  384.               push_reg (regs, stack, E_MCRL_REGNUM);
  385.               push_reg (regs, stack, E_MCVF_REGNUM);
  386.             }
  387.           if (save_mask & movm_d2_bit)
  388.             push_reg (regs, stack, E_D2_REGNUM);
  389.           if (save_mask & movm_d3_bit)
  390.             push_reg (regs, stack, E_D3_REGNUM);
  391.           if (save_mask & movm_a2_bit)
  392.             push_reg (regs, stack, E_A2_REGNUM);
  393.           if (save_mask & movm_a3_bit)
  394.             push_reg (regs, stack, E_A3_REGNUM);
  395.           if (save_mask & movm_other_bit)
  396.             {
  397.               push_reg (regs, stack, E_D0_REGNUM);
  398.               push_reg (regs, stack, E_D1_REGNUM);
  399.               push_reg (regs, stack, E_A0_REGNUM);
  400.               push_reg (regs, stack, E_A1_REGNUM);
  401.               push_reg (regs, stack, E_MDR_REGNUM);
  402.               push_reg (regs, stack, E_LIR_REGNUM);
  403.               push_reg (regs, stack, E_LAR_REGNUM);
  404.               /* The `other' bit leaves a blank area of four bytes at
  405.                  the beginning of its block of saved registers, making
  406.                  it 32 bytes long in total.  */
  407.               regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], -4);
  408.             }

  409.           pc += 2;
  410.           after_last_frame_setup_insn = pc;
  411.         }
  412.       /* mov sp, aN */
  413.       else if ((instr[0] & 0xfc) == 0x3c)
  414.         {
  415.           int aN = instr[0] & 0x03;

  416.           regs[E_A0_REGNUM + aN] = regs[E_SP_REGNUM];

  417.           pc += 1;
  418.           if (aN == 3)
  419.             after_last_frame_setup_insn = pc;
  420.         }
  421.       /* mov aM, aN */
  422.       else if ((instr[0] & 0xf0) == 0x90
  423.                && (instr[0] & 0x03) != ((instr[0] & 0x0c) >> 2))
  424.         {
  425.           int aN = instr[0] & 0x03;
  426.           int aM = (instr[0] & 0x0c) >> 2;

  427.           regs[E_A0_REGNUM + aN] = regs[E_A0_REGNUM + aM];

  428.           pc += 1;
  429.         }
  430.       /* mov dM, dN */
  431.       else if ((instr[0] & 0xf0) == 0x80
  432.                && (instr[0] & 0x03) != ((instr[0] & 0x0c) >> 2))
  433.         {
  434.           int dN = instr[0] & 0x03;
  435.           int dM = (instr[0] & 0x0c) >> 2;

  436.           regs[E_D0_REGNUM + dN] = regs[E_D0_REGNUM + dM];

  437.           pc += 1;
  438.         }
  439.       /* mov aM, dN */
  440.       else if (instr[0] == 0xf1 && (instr[1] & 0xf0) == 0xd0)
  441.         {
  442.           int dN = instr[1] & 0x03;
  443.           int aM = (instr[1] & 0x0c) >> 2;

  444.           regs[E_D0_REGNUM + dN] = regs[E_A0_REGNUM + aM];

  445.           pc += 2;
  446.         }
  447.       /* mov dM, aN */
  448.       else if (instr[0] == 0xf1 && (instr[1] & 0xf0) == 0xe0)
  449.         {
  450.           int aN = instr[1] & 0x03;
  451.           int dM = (instr[1] & 0x0c) >> 2;

  452.           regs[E_A0_REGNUM + aN] = regs[E_D0_REGNUM + dM];

  453.           pc += 2;
  454.         }
  455.       /* add imm8, SP */
  456.       else if (instr[0] == 0xf8 && instr[1] == 0xfe)
  457.         {
  458.           gdb_byte buf[1];
  459.           LONGEST imm8;


  460.           status = target_read_memory (pc + 2, buf, 1);
  461.           if (status != 0)
  462.             break;

  463.           imm8 = extract_signed_integer (buf, 1, byte_order);
  464.           regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], imm8);

  465.           pc += 3;
  466.           /* Stack pointer adjustments are frame related.  */
  467.           after_last_frame_setup_insn = pc;
  468.         }
  469.       /* add imm16, SP */
  470.       else if (instr[0] == 0xfa && instr[1] == 0xfe)
  471.         {
  472.           gdb_byte buf[2];
  473.           LONGEST imm16;

  474.           status = target_read_memory (pc + 2, buf, 2);
  475.           if (status != 0)
  476.             break;

  477.           imm16 = extract_signed_integer (buf, 2, byte_order);
  478.           regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], imm16);

  479.           pc += 4;
  480.           /* Stack pointer adjustments are frame related.  */
  481.           after_last_frame_setup_insn = pc;
  482.         }
  483.       /* add imm32, SP */
  484.       else if (instr[0] == 0xfc && instr[1] == 0xfe)
  485.         {
  486.           gdb_byte buf[4];
  487.           LONGEST imm32;

  488.           status = target_read_memory (pc + 2, buf, 4);
  489.           if (status != 0)
  490.             break;


  491.           imm32 = extract_signed_integer (buf, 4, byte_order);
  492.           regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], imm32);

  493.           pc += 6;
  494.           /* Stack pointer adjustments are frame related.  */
  495.           after_last_frame_setup_insn = pc;
  496.         }
  497.       /* add imm8, aN  */
  498.       else if ((instr[0] & 0xfc) == 0x20)
  499.         {
  500.           int aN;
  501.           LONGEST imm8;

  502.           aN = instr[0] & 0x03;
  503.           imm8 = extract_signed_integer (&instr[1], 1, byte_order);

  504.           regs[E_A0_REGNUM + aN] = pv_add_constant (regs[E_A0_REGNUM + aN],
  505.                                                     imm8);

  506.           pc += 2;
  507.         }
  508.       /* add imm16, aN  */
  509.       else if (instr[0] == 0xfa && (instr[1] & 0xfc) == 0xd0)
  510.         {
  511.           int aN;
  512.           LONGEST imm16;
  513.           gdb_byte buf[2];

  514.           aN = instr[1] & 0x03;

  515.           status = target_read_memory (pc + 2, buf, 2);
  516.           if (status != 0)
  517.             break;


  518.           imm16 = extract_signed_integer (buf, 2, byte_order);

  519.           regs[E_A0_REGNUM + aN] = pv_add_constant (regs[E_A0_REGNUM + aN],
  520.                                                     imm16);

  521.           pc += 4;
  522.         }
  523.       /* add imm32, aN  */
  524.       else if (instr[0] == 0xfc && (instr[1] & 0xfc) == 0xd0)
  525.         {
  526.           int aN;
  527.           LONGEST imm32;
  528.           gdb_byte buf[4];

  529.           aN = instr[1] & 0x03;

  530.           status = target_read_memory (pc + 2, buf, 4);
  531.           if (status != 0)
  532.             break;

  533.           imm32 = extract_signed_integer (buf, 2, byte_order);

  534.           regs[E_A0_REGNUM + aN] = pv_add_constant (regs[E_A0_REGNUM + aN],
  535.                                                     imm32);
  536.           pc += 6;
  537.         }
  538.       /* fmov fsM, (rN) */
  539.       else if (instr[0] == 0xf9 && (instr[1] & 0xfd) == 0x30)
  540.         {
  541.           int fsM, sM, Y, rN;
  542.           gdb_byte buf[1];

  543.           Y = (instr[1] & 0x02) >> 1;

  544.           status = target_read_memory (pc + 2, buf, 1);
  545.           if (status != 0)
  546.             break;

  547.           sM = (buf[0] & 0xf0) >> 4;
  548.           rN = buf[0] & 0x0f;
  549.           fsM = (Y << 4) | sM;

  550.           pv_area_store (stack, regs[translate_rreg (rN)], 4,
  551.                          regs[E_FS0_REGNUM + fsM]);

  552.           pc += 3;
  553.         }
  554.       /* fmov fsM, (sp) */
  555.       else if (instr[0] == 0xf9 && (instr[1] & 0xfd) == 0x34)
  556.         {
  557.           int fsM, sM, Y;
  558.           gdb_byte buf[1];

  559.           Y = (instr[1] & 0x02) >> 1;

  560.           status = target_read_memory (pc + 2, buf, 1);
  561.           if (status != 0)
  562.             break;

  563.           sM = (buf[0] & 0xf0) >> 4;
  564.           fsM = (Y << 4) | sM;

  565.           pv_area_store (stack, regs[E_SP_REGNUM], 4,
  566.                          regs[E_FS0_REGNUM + fsM]);

  567.           pc += 3;
  568.         }
  569.       /* fmov fsM, (rN, rI) */
  570.       else if (instr[0] == 0xfb && instr[1] == 0x37)
  571.         {
  572.           int fsM, sM, Z, rN, rI;
  573.           gdb_byte buf[2];


  574.           status = target_read_memory (pc + 2, buf, 2);
  575.           if (status != 0)
  576.             break;

  577.           rI = (buf[0] & 0xf0) >> 4;
  578.           rN = buf[0] & 0x0f;
  579.           sM = (buf[1] & 0xf0) >> 4;
  580.           Z = (buf[1] & 0x02) >> 1;
  581.           fsM = (Z << 4) | sM;

  582.           pv_area_store (stack,
  583.                          pv_add (regs[translate_rreg (rN)],
  584.                                  regs[translate_rreg (rI)]),
  585.                          4, regs[E_FS0_REGNUM + fsM]);

  586.           pc += 4;
  587.         }
  588.       /* fmov fsM, (d8, rN) */
  589.       else if (instr[0] == 0xfb && (instr[1] & 0xfd) == 0x30)
  590.         {
  591.           int fsM, sM, Y, rN;
  592.           LONGEST d8;
  593.           gdb_byte buf[2];

  594.           Y = (instr[1] & 0x02) >> 1;

  595.           status = target_read_memory (pc + 2, buf, 2);
  596.           if (status != 0)
  597.             break;

  598.           sM = (buf[0] & 0xf0) >> 4;
  599.           rN = buf[0] & 0x0f;
  600.           fsM = (Y << 4) | sM;
  601.           d8 = extract_signed_integer (&buf[1], 1, byte_order);

  602.           pv_area_store (stack,
  603.                          pv_add_constant (regs[translate_rreg (rN)], d8),
  604.                          4, regs[E_FS0_REGNUM + fsM]);

  605.           pc += 4;
  606.         }
  607.       /* fmov fsM, (d24, rN) */
  608.       else if (instr[0] == 0xfd && (instr[1] & 0xfd) == 0x30)
  609.         {
  610.           int fsM, sM, Y, rN;
  611.           LONGEST d24;
  612.           gdb_byte buf[4];

  613.           Y = (instr[1] & 0x02) >> 1;

  614.           status = target_read_memory (pc + 2, buf, 4);
  615.           if (status != 0)
  616.             break;

  617.           sM = (buf[0] & 0xf0) >> 4;
  618.           rN = buf[0] & 0x0f;
  619.           fsM = (Y << 4) | sM;
  620.           d24 = extract_signed_integer (&buf[1], 3, byte_order);

  621.           pv_area_store (stack,
  622.                          pv_add_constant (regs[translate_rreg (rN)], d24),
  623.                          4, regs[E_FS0_REGNUM + fsM]);

  624.           pc += 6;
  625.         }
  626.       /* fmov fsM, (d32, rN) */
  627.       else if (instr[0] == 0xfe && (instr[1] & 0xfd) == 0x30)
  628.         {
  629.           int fsM, sM, Y, rN;
  630.           LONGEST d32;
  631.           gdb_byte buf[5];

  632.           Y = (instr[1] & 0x02) >> 1;

  633.           status = target_read_memory (pc + 2, buf, 5);
  634.           if (status != 0)
  635.             break;

  636.           sM = (buf[0] & 0xf0) >> 4;
  637.           rN = buf[0] & 0x0f;
  638.           fsM = (Y << 4) | sM;
  639.           d32 = extract_signed_integer (&buf[1], 4, byte_order);

  640.           pv_area_store (stack,
  641.                          pv_add_constant (regs[translate_rreg (rN)], d32),
  642.                          4, regs[E_FS0_REGNUM + fsM]);

  643.           pc += 7;
  644.         }
  645.       /* fmov fsM, (d8, SP) */
  646.       else if (instr[0] == 0xfb && (instr[1] & 0xfd) == 0x34)
  647.         {
  648.           int fsM, sM, Y;
  649.           LONGEST d8;
  650.           gdb_byte buf[2];

  651.           Y = (instr[1] & 0x02) >> 1;

  652.           status = target_read_memory (pc + 2, buf, 2);
  653.           if (status != 0)
  654.             break;

  655.           sM = (buf[0] & 0xf0) >> 4;
  656.           fsM = (Y << 4) | sM;
  657.           d8 = extract_signed_integer (&buf[1], 1, byte_order);

  658.           pv_area_store (stack,
  659.                          pv_add_constant (regs[E_SP_REGNUM], d8),
  660.                          4, regs[E_FS0_REGNUM + fsM]);

  661.           pc += 4;
  662.         }
  663.       /* fmov fsM, (d24, SP) */
  664.       else if (instr[0] == 0xfd && (instr[1] & 0xfd) == 0x34)
  665.         {
  666.           int fsM, sM, Y;
  667.           LONGEST d24;
  668.           gdb_byte buf[4];

  669.           Y = (instr[1] & 0x02) >> 1;

  670.           status = target_read_memory (pc + 2, buf, 4);
  671.           if (status != 0)
  672.             break;

  673.           sM = (buf[0] & 0xf0) >> 4;
  674.           fsM = (Y << 4) | sM;
  675.           d24 = extract_signed_integer (&buf[1], 3, byte_order);

  676.           pv_area_store (stack,
  677.                          pv_add_constant (regs[E_SP_REGNUM], d24),
  678.                          4, regs[E_FS0_REGNUM + fsM]);

  679.           pc += 6;
  680.         }
  681.       /* fmov fsM, (d32, SP) */
  682.       else if (instr[0] == 0xfe && (instr[1] & 0xfd) == 0x34)
  683.         {
  684.           int fsM, sM, Y;
  685.           LONGEST d32;
  686.           gdb_byte buf[5];

  687.           Y = (instr[1] & 0x02) >> 1;

  688.           status = target_read_memory (pc + 2, buf, 5);
  689.           if (status != 0)
  690.             break;

  691.           sM = (buf[0] & 0xf0) >> 4;
  692.           fsM = (Y << 4) | sM;
  693.           d32 = extract_signed_integer (&buf[1], 4, byte_order);

  694.           pv_area_store (stack,
  695.                          pv_add_constant (regs[E_SP_REGNUM], d32),
  696.                          4, regs[E_FS0_REGNUM + fsM]);

  697.           pc += 7;
  698.         }
  699.       /* fmov fsM, (rN+) */
  700.       else if (instr[0] == 0xf9 && (instr[1] & 0xfd) == 0x31)
  701.         {
  702.           int fsM, sM, Y, rN, rN_regnum;
  703.           gdb_byte buf[1];

  704.           Y = (instr[1] & 0x02) >> 1;

  705.           status = target_read_memory (pc + 2, buf, 1);
  706.           if (status != 0)
  707.             break;

  708.           sM = (buf[0] & 0xf0) >> 4;
  709.           rN = buf[0] & 0x0f;
  710.           fsM = (Y << 4) | sM;

  711.           rN_regnum = translate_rreg (rN);

  712.           pv_area_store (stack, regs[rN_regnum], 4,
  713.                          regs[E_FS0_REGNUM + fsM]);
  714.           regs[rN_regnum] = pv_add_constant (regs[rN_regnum], 4);

  715.           pc += 3;
  716.         }
  717.       /* fmov fsM, (rN+, imm8) */
  718.       else if (instr[0] == 0xfb && (instr[1] & 0xfd) == 0x31)
  719.         {
  720.           int fsM, sM, Y, rN, rN_regnum;
  721.           LONGEST imm8;
  722.           gdb_byte buf[2];

  723.           Y = (instr[1] & 0x02) >> 1;

  724.           status = target_read_memory (pc + 2, buf, 2);
  725.           if (status != 0)
  726.             break;

  727.           sM = (buf[0] & 0xf0) >> 4;
  728.           rN = buf[0] & 0x0f;
  729.           fsM = (Y << 4) | sM;
  730.           imm8 = extract_signed_integer (&buf[1], 1, byte_order);

  731.           rN_regnum = translate_rreg (rN);

  732.           pv_area_store (stack, regs[rN_regnum], 4, regs[E_FS0_REGNUM + fsM]);
  733.           regs[rN_regnum] = pv_add_constant (regs[rN_regnum], imm8);

  734.           pc += 4;
  735.         }
  736.       /* fmov fsM, (rN+, imm24) */
  737.       else if (instr[0] == 0xfd && (instr[1] & 0xfd) == 0x31)
  738.         {
  739.           int fsM, sM, Y, rN, rN_regnum;
  740.           LONGEST imm24;
  741.           gdb_byte buf[4];

  742.           Y = (instr[1] & 0x02) >> 1;

  743.           status = target_read_memory (pc + 2, buf, 4);
  744.           if (status != 0)
  745.             break;

  746.           sM = (buf[0] & 0xf0) >> 4;
  747.           rN = buf[0] & 0x0f;
  748.           fsM = (Y << 4) | sM;
  749.           imm24 = extract_signed_integer (&buf[1], 3, byte_order);

  750.           rN_regnum = translate_rreg (rN);

  751.           pv_area_store (stack, regs[rN_regnum], 4, regs[E_FS0_REGNUM + fsM]);
  752.           regs[rN_regnum] = pv_add_constant (regs[rN_regnum], imm24);

  753.           pc += 6;
  754.         }
  755.       /* fmov fsM, (rN+, imm32) */
  756.       else if (instr[0] == 0xfe && (instr[1] & 0xfd) == 0x31)
  757.         {
  758.           int fsM, sM, Y, rN, rN_regnum;
  759.           LONGEST imm32;
  760.           gdb_byte buf[5];

  761.           Y = (instr[1] & 0x02) >> 1;

  762.           status = target_read_memory (pc + 2, buf, 5);
  763.           if (status != 0)
  764.             break;

  765.           sM = (buf[0] & 0xf0) >> 4;
  766.           rN = buf[0] & 0x0f;
  767.           fsM = (Y << 4) | sM;
  768.           imm32 = extract_signed_integer (&buf[1], 4, byte_order);

  769.           rN_regnum = translate_rreg (rN);

  770.           pv_area_store (stack, regs[rN_regnum], 4, regs[E_FS0_REGNUM + fsM]);
  771.           regs[rN_regnum] = pv_add_constant (regs[rN_regnum], imm32);

  772.           pc += 7;
  773.         }
  774.       /* mov imm8, aN */
  775.       else if ((instr[0] & 0xf0) == 0x90)
  776.         {
  777.           int aN = instr[0] & 0x03;
  778.           LONGEST imm8;

  779.           imm8 = extract_signed_integer (&instr[1], 1, byte_order);

  780.           regs[E_A0_REGNUM + aN] = pv_constant (imm8);
  781.           pc += 2;
  782.         }
  783.       /* mov imm16, aN */
  784.       else if ((instr[0] & 0xfc) == 0x24)
  785.         {
  786.           int aN = instr[0] & 0x03;
  787.           gdb_byte buf[2];
  788.           LONGEST imm16;

  789.           status = target_read_memory (pc + 1, buf, 2);
  790.           if (status != 0)
  791.             break;

  792.           imm16 = extract_signed_integer (buf, 2, byte_order);
  793.           regs[E_A0_REGNUM + aN] = pv_constant (imm16);
  794.           pc += 3;
  795.         }
  796.       /* mov imm32, aN */
  797.       else if (instr[0] == 0xfc && ((instr[1] & 0xfc) == 0xdc))
  798.         {
  799.           int aN = instr[1] & 0x03;
  800.           gdb_byte buf[4];
  801.           LONGEST imm32;

  802.           status = target_read_memory (pc + 2, buf, 4);
  803.           if (status != 0)
  804.             break;

  805.           imm32 = extract_signed_integer (buf, 4, byte_order);
  806.           regs[E_A0_REGNUM + aN] = pv_constant (imm32);
  807.           pc += 6;
  808.         }
  809.       /* mov imm8, dN */
  810.       else if ((instr[0] & 0xf0) == 0x80)
  811.         {
  812.           int dN = instr[0] & 0x03;
  813.           LONGEST imm8;

  814.           imm8 = extract_signed_integer (&instr[1], 1, byte_order);

  815.           regs[E_D0_REGNUM + dN] = pv_constant (imm8);
  816.           pc += 2;
  817.         }
  818.       /* mov imm16, dN */
  819.       else if ((instr[0] & 0xfc) == 0x2c)
  820.         {
  821.           int dN = instr[0] & 0x03;
  822.           gdb_byte buf[2];
  823.           LONGEST imm16;

  824.           status = target_read_memory (pc + 1, buf, 2);
  825.           if (status != 0)
  826.             break;

  827.           imm16 = extract_signed_integer (buf, 2, byte_order);
  828.           regs[E_D0_REGNUM + dN] = pv_constant (imm16);
  829.           pc += 3;
  830.         }
  831.       /* mov imm32, dN */
  832.       else if (instr[0] == 0xfc && ((instr[1] & 0xfc) == 0xcc))
  833.         {
  834.           int dN = instr[1] & 0x03;
  835.           gdb_byte buf[4];
  836.           LONGEST imm32;

  837.           status = target_read_memory (pc + 2, buf, 4);
  838.           if (status != 0)
  839.             break;

  840.           imm32 = extract_signed_integer (buf, 4, byte_order);
  841.           regs[E_D0_REGNUM + dN] = pv_constant (imm32);
  842.           pc += 6;
  843.         }
  844.       else
  845.         {
  846.           /* We've hit some instruction that we don't recognize.  Hopefully,
  847.              we have enough to do prologue analysis.  */
  848.           break;
  849.         }
  850.     }

  851.   /* Is the frame size (offset, really) a known constant?  */
  852.   if (pv_is_register (regs[E_SP_REGNUM], E_SP_REGNUM))
  853.     result->frame_size = regs[E_SP_REGNUM].k;

  854.   /* Was the frame pointer initialized?  */
  855.   if (pv_is_register (regs[E_A3_REGNUM], E_SP_REGNUM))
  856.     {
  857.       result->has_frame_ptr = 1;
  858.       result->frame_ptr_offset = regs[E_A3_REGNUM].k;
  859.     }

  860.   /* Record where all the registers were saved.  */
  861.   pv_area_scan (stack, check_for_saved, (void *) result);

  862.   result->prologue_end = after_last_frame_setup_insn;

  863.   do_cleanups (back_to);
  864. }

  865. /* Function: skip_prologue
  866.    Return the address of the first inst past the prologue of the function.  */

  867. static CORE_ADDR
  868. mn10300_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  869. {
  870.   const char *name;
  871.   CORE_ADDR func_addr, func_end;
  872.   struct mn10300_prologue p;

  873.   /* Try to find the extent of the function that contains PC.  */
  874.   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  875.     return pc;

  876.   mn10300_analyze_prologue (gdbarch, pc, func_end, &p);
  877.   return p.prologue_end;
  878. }

  879. /* Wrapper for mn10300_analyze_prologue: find the function start;
  880.    use the current frame PC as the limit, then
  881.    invoke mn10300_analyze_prologue and return its result.  */
  882. static struct mn10300_prologue *
  883. mn10300_analyze_frame_prologue (struct frame_info *this_frame,
  884.                            void **this_prologue_cache)
  885. {
  886.   if (!*this_prologue_cache)
  887.     {
  888.       CORE_ADDR func_start, stop_addr;

  889.       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct mn10300_prologue);

  890.       func_start = get_frame_func (this_frame);
  891.       stop_addr = get_frame_pc (this_frame);

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

  896.       mn10300_analyze_prologue (get_frame_arch (this_frame),
  897.                                 func_start, stop_addr, *this_prologue_cache);
  898.     }

  899.   return *this_prologue_cache;
  900. }

  901. /* Given the next frame and a prologue cache, return this frame's
  902.    base.  */
  903. static CORE_ADDR
  904. mn10300_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
  905. {
  906.   struct mn10300_prologue *p
  907.     = mn10300_analyze_frame_prologue (this_frame, this_prologue_cache);

  908.   /* In functions that use alloca, the distance between the stack
  909.      pointer and the frame base varies dynamically, so we can't use
  910.      the SP plus static information like prologue analysis to find the
  911.      frame base.  However, such functions must have a frame pointer,
  912.      to be able to restore the SP on exit.  So whenever we do have a
  913.      frame pointer, use that to find the base.  */
  914.   if (p->has_frame_ptr)
  915.     {
  916.       CORE_ADDR fp = get_frame_register_unsigned (this_frame, E_A3_REGNUM);
  917.       return fp - p->frame_ptr_offset;
  918.     }
  919.   else
  920.     {
  921.       CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
  922.       return sp - p->frame_size;
  923.     }
  924. }

  925. /* Here is a dummy implementation.  */
  926. static struct frame_id
  927. mn10300_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  928. {
  929.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
  930.   CORE_ADDR pc = get_frame_register_unsigned (this_frame, E_PC_REGNUM);
  931.   return frame_id_build (sp, pc);
  932. }

  933. static void
  934. mn10300_frame_this_id (struct frame_info *this_frame,
  935.                        void **this_prologue_cache,
  936.                        struct frame_id *this_id)
  937. {
  938.   *this_id = frame_id_build (mn10300_frame_base (this_frame,
  939.                                                  this_prologue_cache),
  940.                              get_frame_func (this_frame));

  941. }

  942. static struct value *
  943. mn10300_frame_prev_register (struct frame_info *this_frame,
  944.                              void **this_prologue_cache, int regnum)
  945. {
  946.   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
  947.   struct mn10300_prologue *p
  948.     = mn10300_analyze_frame_prologue (this_frame, this_prologue_cache);
  949.   CORE_ADDR frame_base = mn10300_frame_base (this_frame, this_prologue_cache);
  950.   int reg_size = register_size (get_frame_arch (this_frame), regnum);

  951.   if (regnum == E_SP_REGNUM)
  952.     return frame_unwind_got_constant (this_frame, regnum, frame_base);

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

  958.   /* Otherwise, presume we haven't changed the value of this
  959.      register, and get it from the next frame.  */
  960.   return frame_unwind_got_register (this_frame, regnum, regnum);
  961. }

  962. static const struct frame_unwind mn10300_frame_unwind = {
  963.   NORMAL_FRAME,
  964.   default_frame_unwind_stop_reason,
  965.   mn10300_frame_this_id,
  966.   mn10300_frame_prev_register,
  967.   NULL,
  968.   default_frame_sniffer
  969. };

  970. static CORE_ADDR
  971. mn10300_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
  972. {
  973.   ULONGEST pc;

  974.   pc = frame_unwind_register_unsigned (this_frame, E_PC_REGNUM);
  975.   return pc;
  976. }

  977. static CORE_ADDR
  978. mn10300_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
  979. {
  980.   ULONGEST sp;

  981.   sp = frame_unwind_register_unsigned (this_frame, E_SP_REGNUM);
  982.   return sp;
  983. }

  984. static void
  985. mn10300_frame_unwind_init (struct gdbarch *gdbarch)
  986. {
  987.   dwarf2_append_unwinders (gdbarch);
  988.   frame_unwind_append_unwinder (gdbarch, &mn10300_frame_unwind);
  989.   set_gdbarch_dummy_id (gdbarch, mn10300_dummy_id);
  990.   set_gdbarch_unwind_pc (gdbarch, mn10300_unwind_pc);
  991.   set_gdbarch_unwind_sp (gdbarch, mn10300_unwind_sp);
  992. }

  993. /* Function: push_dummy_call
  994. *
  995. * Set up machine state for a target call, including
  996. * function arguments, stack, return address, etc.
  997. *
  998. */

  999. static CORE_ADDR
  1000. mn10300_push_dummy_call (struct gdbarch *gdbarch,
  1001.                          struct value *target_func,
  1002.                          struct regcache *regcache,
  1003.                          CORE_ADDR bp_addr,
  1004.                          int nargs, struct value **args,
  1005.                          CORE_ADDR sp,
  1006.                          int struct_return,
  1007.                          CORE_ADDR struct_addr)
  1008. {
  1009.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1010.   const int push_size = register_size (gdbarch, E_PC_REGNUM);
  1011.   int regs_used;
  1012.   int len, arg_len;
  1013.   int stack_offset = 0;
  1014.   int argnum;
  1015.   const gdb_byte *val;
  1016.   gdb_byte valbuf[MAX_REGISTER_SIZE];

  1017.   /* This should be a nop, but align the stack just in case something
  1018.      went wrong.  Stacks are four byte aligned on the mn10300.  */
  1019.   sp &= ~3;

  1020.   /* Now make space on the stack for the args.

  1021.      XXX This doesn't appear to handle pass-by-invisible reference
  1022.      arguments.  */
  1023.   regs_used = struct_return ? 1 : 0;
  1024.   for (len = 0, argnum = 0; argnum < nargs; argnum++)
  1025.     {
  1026.       arg_len = (TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3;
  1027.       while (regs_used < 2 && arg_len > 0)
  1028.         {
  1029.           regs_used++;
  1030.           arg_len -= push_size;
  1031.         }
  1032.       len += arg_len;
  1033.     }

  1034.   /* Allocate stack space.  */
  1035.   sp -= len;

  1036.   if (struct_return)
  1037.     {
  1038.       regs_used = 1;
  1039.       regcache_cooked_write_unsigned (regcache, E_D0_REGNUM, struct_addr);
  1040.     }
  1041.   else
  1042.     regs_used = 0;

  1043.   /* Push all arguments onto the stack.  */
  1044.   for (argnum = 0; argnum < nargs; argnum++)
  1045.     {
  1046.       /* FIXME what about structs?  Unions?  */
  1047.       if (TYPE_CODE (value_type (*args)) == TYPE_CODE_STRUCT
  1048.           && TYPE_LENGTH (value_type (*args)) > 8)
  1049.         {
  1050.           /* Change to pointer-to-type.  */
  1051.           arg_len = push_size;
  1052.           store_unsigned_integer (valbuf, push_size, byte_order,
  1053.                                   value_address (*args));
  1054.           val = &valbuf[0];
  1055.         }
  1056.       else
  1057.         {
  1058.           arg_len = TYPE_LENGTH (value_type (*args));
  1059.           val = value_contents (*args);
  1060.         }

  1061.       while (regs_used < 2 && arg_len > 0)
  1062.         {
  1063.           regcache_cooked_write_unsigned (regcache, regs_used,
  1064.                   extract_unsigned_integer (val, push_size, byte_order));
  1065.           val += push_size;
  1066.           arg_len -= push_size;
  1067.           regs_used++;
  1068.         }

  1069.       while (arg_len > 0)
  1070.         {
  1071.           write_memory (sp + stack_offset, val, push_size);
  1072.           arg_len -= push_size;
  1073.           val += push_size;
  1074.           stack_offset += push_size;
  1075.         }

  1076.       args++;
  1077.     }

  1078.   /* Make space for the flushback area.  */
  1079.   sp -= 8;

  1080.   /* Push the return address that contains the magic breakpoint.  */
  1081.   sp -= 4;
  1082.   write_memory_unsigned_integer (sp, push_size, byte_order, bp_addr);

  1083.   /* The CPU also writes the return address always into the
  1084.      MDR register on "call".  */
  1085.   regcache_cooked_write_unsigned (regcache, E_MDR_REGNUM, bp_addr);

  1086.   /* Update $sp.  */
  1087.   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);

  1088.   /* On the mn10300, it's possible to move some of the stack adjustment
  1089.      and saving of the caller-save registers out of the prologue and
  1090.      into the call sites.  (When using gcc, this optimization can
  1091.      occur when using the -mrelax switch.) If this occurs, the dwarf2
  1092.      info will reflect this fact.  We can test to see if this is the
  1093.      case by creating a new frame using the current stack pointer and
  1094.      the address of the function that we're about to call.  We then
  1095.      unwind SP and see if it's different than the SP of our newly
  1096.      created frame.  If the SP values are the same, the caller is not
  1097.      expected to allocate any additional stack.  On the other hand, if
  1098.      the SP values are different, the difference determines the
  1099.      additional stack that must be allocated.

  1100.      Note that we don't update the return value though because that's
  1101.      the value of the stack just after pushing the arguments, but prior
  1102.      to performing the call.  This value is needed in order to
  1103.      construct the frame ID of the dummy call.  */
  1104.   {
  1105.     CORE_ADDR func_addr = find_function_addr (target_func, NULL);
  1106.     CORE_ADDR unwound_sp
  1107.       = mn10300_unwind_sp (gdbarch, create_new_frame (sp, func_addr));
  1108.     if (sp != unwound_sp)
  1109.       regcache_cooked_write_unsigned (regcache, E_SP_REGNUM,
  1110.                                       sp - (unwound_sp - sp));
  1111.   }

  1112.   return sp;
  1113. }

  1114. /* If DWARF2 is a register number appearing in Dwarf2 debug info, then
  1115.    mn10300_dwarf2_reg_to_regnum (DWARF2) is the corresponding GDB
  1116.    register number.  Why don't Dwarf2 and GDB use the same numbering?
  1117.    Who knows?  But since people have object files lying around with
  1118.    the existing Dwarf2 numbering, and other people have written stubs
  1119.    to work with the existing GDB, neither of them can change.  So we
  1120.    just have to cope.  */
  1121. static int
  1122. mn10300_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int dwarf2)
  1123. {
  1124.   /* This table is supposed to be shaped like the gdbarch_register_name
  1125.      initializer in gcc/config/mn10300/mn10300.h.  Registers which
  1126.      appear in GCC's numbering, but have no counterpart in GDB's
  1127.      world, are marked with a -1.  */
  1128.   static int dwarf2_to_gdb[] = {
  1129.     E_D0_REGNUM, E_D1_REGNUM, E_D2_REGNUM, E_D3_REGNUM,
  1130.     E_A0_REGNUM, E_A1_REGNUM, E_A2_REGNUM, E_A3_REGNUM,
  1131.     -1, E_SP_REGNUM,

  1132.     E_E0_REGNUM, E_E1_REGNUM, E_E2_REGNUM, E_E3_REGNUM,
  1133.     E_E4_REGNUM, E_E5_REGNUM, E_E6_REGNUM, E_E7_REGNUM,

  1134.     E_FS0_REGNUM + 0, E_FS0_REGNUM + 1, E_FS0_REGNUM + 2, E_FS0_REGNUM + 3,
  1135.     E_FS0_REGNUM + 4, E_FS0_REGNUM + 5, E_FS0_REGNUM + 6, E_FS0_REGNUM + 7,

  1136.     E_FS0_REGNUM + 8, E_FS0_REGNUM + 9, E_FS0_REGNUM + 10, E_FS0_REGNUM + 11,
  1137.     E_FS0_REGNUM + 12, E_FS0_REGNUM + 13, E_FS0_REGNUM + 14, E_FS0_REGNUM + 15,

  1138.     E_FS0_REGNUM + 16, E_FS0_REGNUM + 17, E_FS0_REGNUM + 18, E_FS0_REGNUM + 19,
  1139.     E_FS0_REGNUM + 20, E_FS0_REGNUM + 21, E_FS0_REGNUM + 22, E_FS0_REGNUM + 23,

  1140.     E_FS0_REGNUM + 24, E_FS0_REGNUM + 25, E_FS0_REGNUM + 26, E_FS0_REGNUM + 27,
  1141.     E_FS0_REGNUM + 28, E_FS0_REGNUM + 29, E_FS0_REGNUM + 30, E_FS0_REGNUM + 31,

  1142.     E_MDR_REGNUM, E_PSW_REGNUM, E_PC_REGNUM
  1143.   };

  1144.   if (dwarf2 < 0
  1145.       || dwarf2 >= ARRAY_SIZE (dwarf2_to_gdb))
  1146.     {
  1147.       warning (_("Bogus register number in debug info: %d"), dwarf2);
  1148.       return -1;
  1149.     }

  1150.   return dwarf2_to_gdb[dwarf2];
  1151. }

  1152. static struct gdbarch *
  1153. mn10300_gdbarch_init (struct gdbarch_info info,
  1154.                       struct gdbarch_list *arches)
  1155. {
  1156.   struct gdbarch *gdbarch;
  1157.   struct gdbarch_tdep *tdep;
  1158.   int num_regs;

  1159.   arches = gdbarch_list_lookup_by_info (arches, &info);
  1160.   if (arches != NULL)
  1161.     return arches->gdbarch;

  1162.   tdep = xmalloc (sizeof (struct gdbarch_tdep));
  1163.   gdbarch = gdbarch_alloc (&info, tdep);

  1164.   switch (info.bfd_arch_info->mach)
  1165.     {
  1166.     case 0:
  1167.     case bfd_mach_mn10300:
  1168.       set_gdbarch_register_name (gdbarch, mn10300_generic_register_name);
  1169.       tdep->am33_mode = 0;
  1170.       num_regs = 32;
  1171.       break;
  1172.     case bfd_mach_am33:
  1173.       set_gdbarch_register_name (gdbarch, am33_register_name);
  1174.       tdep->am33_mode = 1;
  1175.       num_regs = 32;
  1176.       break;
  1177.     case bfd_mach_am33_2:
  1178.       set_gdbarch_register_name (gdbarch, am33_2_register_name);
  1179.       tdep->am33_mode = 2;
  1180.       num_regs = 64;
  1181.       set_gdbarch_fp0_regnum (gdbarch, 32);
  1182.       break;
  1183.     default:
  1184.       internal_error (__FILE__, __LINE__,
  1185.                       _("mn10300_gdbarch_init: Unknown mn10300 variant"));
  1186.       break;
  1187.     }

  1188.   /* By default, chars are unsigned.  */
  1189.   set_gdbarch_char_signed (gdbarch, 0);

  1190.   /* Registers.  */
  1191.   set_gdbarch_num_regs (gdbarch, num_regs);
  1192.   set_gdbarch_register_type (gdbarch, mn10300_register_type);
  1193.   set_gdbarch_skip_prologue (gdbarch, mn10300_skip_prologue);
  1194.   set_gdbarch_read_pc (gdbarch, mn10300_read_pc);
  1195.   set_gdbarch_write_pc (gdbarch, mn10300_write_pc);
  1196.   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
  1197.   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
  1198.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, mn10300_dwarf2_reg_to_regnum);

  1199.   /* Stack unwinding.  */
  1200.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1201.   /* Breakpoints.  */
  1202.   set_gdbarch_breakpoint_from_pc (gdbarch, mn10300_breakpoint_from_pc);
  1203.   /* decr_pc_after_break?  */
  1204.   /* Disassembly.  */
  1205.   set_gdbarch_print_insn (gdbarch, print_insn_mn10300);

  1206.   /* Stage 2 */
  1207.   set_gdbarch_return_value (gdbarch, mn10300_return_value);

  1208.   /* Stage 3 -- get target calls working.  */
  1209.   set_gdbarch_push_dummy_call (gdbarch, mn10300_push_dummy_call);
  1210.   /* set_gdbarch_return_value (store, extract) */


  1211.   mn10300_frame_unwind_init (gdbarch);

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

  1214.   return gdbarch;
  1215. }

  1216. /* Dump out the mn10300 specific architecture information.  */

  1217. static void
  1218. mn10300_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  1219. {
  1220.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1221.   fprintf_unfiltered (file, "mn10300_dump_tdep: am33_mode = %d\n",
  1222.                       tdep->am33_mode);
  1223. }

  1224. /* Provide a prototype to silence -Wmissing-prototypes.  */
  1225. extern initialize_file_ftype _initialize_mn10300_tdep;

  1226. void
  1227. _initialize_mn10300_tdep (void)
  1228. {
  1229.   gdbarch_register (bfd_arch_mn10300, mn10300_gdbarch_init, mn10300_dump_tdep);
  1230. }