gdb/msp430-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Target-dependent code for the Texas Instruments MSP430 for GDB, the
  2.    GNU debugger.

  3.    Copyright (C) 2012-2015 Free Software Foundation, Inc.

  4.    Contributed by Red Hat, Inc.

  5.    This file is part of GDB.

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

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

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

  16. #include "defs.h"
  17. #include "arch-utils.h"
  18. #include "prologue-value.h"
  19. #include "target.h"
  20. #include "regcache.h"
  21. #include "dis-asm.h"
  22. #include "gdbtypes.h"
  23. #include "frame.h"
  24. #include "frame-unwind.h"
  25. #include "frame-base.h"
  26. #include "value.h"
  27. #include "gdbcore.h"
  28. #include "dwarf2-frame.h"
  29. #include "reggroups.h"

  30. #include "elf/msp430.h"
  31. #include "opcode/msp430-decode.h"
  32. #include "elf-bfd.h"

  33. /* Register Numbers.  */

  34. enum
  35. {
  36.   MSP430_PC_RAW_REGNUM,
  37.   MSP430_SP_RAW_REGNUM,
  38.   MSP430_SR_RAW_REGNUM,
  39.   MSP430_CG_RAW_REGNUM,
  40.   MSP430_R4_RAW_REGNUM,
  41.   MSP430_R5_RAW_REGNUM,
  42.   MSP430_R6_RAW_REGNUM,
  43.   MSP430_R7_RAW_REGNUM,
  44.   MSP430_R8_RAW_REGNUM,
  45.   MSP430_R9_RAW_REGNUM,
  46.   MSP430_R10_RAW_REGNUM,
  47.   MSP430_R11_RAW_REGNUM,
  48.   MSP430_R12_RAW_REGNUM,
  49.   MSP430_R13_RAW_REGNUM,
  50.   MSP430_R14_RAW_REGNUM,
  51.   MSP430_R15_RAW_REGNUM,

  52.   MSP430_NUM_REGS,

  53.   MSP430_PC_REGNUM = MSP430_NUM_REGS,
  54.   MSP430_SP_REGNUM,
  55.   MSP430_SR_REGNUM,
  56.   MSP430_CG_REGNUM,
  57.   MSP430_R4_REGNUM,
  58.   MSP430_R5_REGNUM,
  59.   MSP430_R6_REGNUM,
  60.   MSP430_R7_REGNUM,
  61.   MSP430_R8_REGNUM,
  62.   MSP430_R9_REGNUM,
  63.   MSP430_R10_REGNUM,
  64.   MSP430_R11_REGNUM,
  65.   MSP430_R12_REGNUM,
  66.   MSP430_R13_REGNUM,
  67.   MSP430_R14_REGNUM,
  68.   MSP430_R15_REGNUM,

  69.   MSP430_NUM_TOTAL_REGS,
  70.   MSP430_NUM_PSEUDO_REGS = MSP430_NUM_TOTAL_REGS - MSP430_NUM_REGS
  71. };

  72. enum
  73. {
  74.   /* TI MSP430 Architecture.  */
  75.   MSP_ISA_MSP430,

  76.   /* TI MSP430X Architecture.  */
  77.   MSP_ISA_MSP430X
  78. };

  79. enum
  80. {
  81.   /* The small code model limits code addresses to 16 bits.  */
  82.   MSP_SMALL_CODE_MODEL,

  83.   /* The large code model uses 20 bit addresses for function
  84.      pointers.  These are stored in memory using four bytes (32 bits).  */
  85.   MSP_LARGE_CODE_MODEL
  86. };

  87. /* Architecture specific data.  */

  88. struct gdbarch_tdep
  89. {
  90.   /* The ELF header flags specify the multilib used.  */
  91.   int elf_flags;

  92.   /* One of MSP_ISA_MSP430 or MSP_ISA_MSP430X.  */
  93.   int isa;

  94.   /* One of MSP_SMALL_CODE_MODEL or MSP_LARGE_CODE_MODEL.  If, at
  95.      some point, we support different data models too, we'll probably
  96.      structure things so that we can combine values using logical
  97.      "or".  */
  98.   int code_model;
  99. };

  100. /* This structure holds the results of a prologue analysis.  */

  101. struct msp430_prologue
  102. {
  103.   /* The offset from the frame base to the stack pointer --- always
  104.      zero or negative.

  105.      Calling this a "size" is a bit misleading, but given that the
  106.      stack grows downwards, using offsets for everything keeps one
  107.      from going completely sign-crazy: you never change anything's
  108.      sign for an ADD instruction; always change the second operand's
  109.      sign for a SUB instruction; and everything takes care of
  110.      itself.  */
  111.   int frame_size;

  112.   /* Non-zero if this function has initialized the frame pointer from
  113.      the stack pointer, zero otherwise.  */
  114.   int has_frame_ptr;

  115.   /* If has_frame_ptr is non-zero, this is the offset from the frame
  116.      base to where the frame pointer points.  This is always zero or
  117.      negative.  */
  118.   int frame_ptr_offset;

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

  123.   /* reg_offset[R] is the offset from the CFA at which register R is
  124.      saved, or 1 if register R has not been saved.  (Real values are
  125.      always zero or negative.)  */
  126.   int reg_offset[MSP430_NUM_TOTAL_REGS];
  127. };

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

  129. static struct type *
  130. msp430_register_type (struct gdbarch *gdbarch, int reg_nr)
  131. {
  132.   if (reg_nr < MSP430_NUM_REGS)
  133.     return builtin_type (gdbarch)->builtin_uint32;
  134.   else if (reg_nr == MSP430_PC_REGNUM)
  135.     return builtin_type (gdbarch)->builtin_func_ptr;
  136.   else
  137.     return builtin_type (gdbarch)->builtin_uint16;
  138. }

  139. /* Implement another version of the "register_type" gdbarch method
  140.    for msp430x.  */

  141. static struct type *
  142. msp430x_register_type (struct gdbarch *gdbarch, int reg_nr)
  143. {
  144.   if (reg_nr < MSP430_NUM_REGS)
  145.     return builtin_type (gdbarch)->builtin_uint32;
  146.   else if (reg_nr == MSP430_PC_REGNUM)
  147.     return builtin_type (gdbarch)->builtin_func_ptr;
  148.   else
  149.     return builtin_type (gdbarch)->builtin_uint32;
  150. }

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

  152. static const char *
  153. msp430_register_name (struct gdbarch *gdbarch, int regnr)
  154. {
  155.   static const char *const reg_names[] = {
  156.     /* Raw registers.  */
  157.     "", "", "", "", "", "", "", "",
  158.     "", "", "", "", "", "", "", "",
  159.     /* Pseudo registers.  */
  160.     "pc", "sp", "sr", "cg", "r4", "r5", "r6", "r7",
  161.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  162.   };

  163.   return reg_names[regnr];
  164. }

  165. /* Implement the "register_reggroup_p" gdbarch method.  */

  166. static int
  167. msp430_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  168.                             struct reggroup *group)
  169. {
  170.   if (group == all_reggroup)
  171.     return 1;

  172.   /* All other registers are saved and restored.  */
  173.   if (group == save_reggroup || group == restore_reggroup)
  174.     return (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS);

  175.   return group == general_reggroup;
  176. }

  177. /* Implement the "pseudo_register_read" gdbarch method.  */

  178. static enum register_status
  179. msp430_pseudo_register_read (struct gdbarch *gdbarch,
  180.                              struct regcache *regcache,
  181.                              int regnum, gdb_byte *buffer)
  182. {
  183.   enum register_status status = REG_UNKNOWN;

  184.   if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
  185.     {
  186.       ULONGEST val;
  187.       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  188.       int regsize = register_size (gdbarch, regnum);
  189.       int raw_regnum = regnum - MSP430_NUM_REGS;

  190.       status = regcache_raw_read_unsigned (regcache, raw_regnum, &val);
  191.       if (status == REG_VALID)
  192.         store_unsigned_integer (buffer, regsize, byte_order, val);

  193.     }
  194.   else
  195.     gdb_assert_not_reached ("invalid pseudo register number");

  196.   return status;
  197. }

  198. /* Implement the "pseudo_register_write" gdbarch method.  */

  199. static void
  200. msp430_pseudo_register_write (struct gdbarch *gdbarch,
  201.                               struct regcache *regcache,
  202.                               int regnum, const gdb_byte *buffer)
  203. {
  204.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  205.   if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)

  206.     {
  207.       ULONGEST val;
  208.       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  209.       int regsize = register_size (gdbarch, regnum);
  210.       int raw_regnum = regnum - MSP430_NUM_REGS;

  211.       val = extract_unsigned_integer (buffer, regsize, byte_order);
  212.       regcache_raw_write_unsigned (regcache, raw_regnum, val);

  213.     }
  214.   else
  215.     gdb_assert_not_reached ("invalid pseudo register number");
  216. }

  217. /* Implement the `register_sim_regno' gdbarch method.  */

  218. static int
  219. msp430_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  220. {
  221.   gdb_assert (regnum < MSP430_NUM_REGS);

  222.   /* So long as regnum is in [0, RL78_NUM_REGS), it's valid.  We
  223.      just want to override the default here which disallows register
  224.      numbers which have no names.  */
  225.   return regnum;
  226. }

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

  228. static const gdb_byte *
  229. msp430_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  230.                            int *lenptr)
  231. {
  232.   static gdb_byte breakpoint[] = { 0x43, 0x43 };

  233.   *lenptr = sizeof breakpoint;
  234.   return breakpoint;
  235. }

  236. /* Define a "handle" struct for fetching the next opcode.  */

  237. struct msp430_get_opcode_byte_handle
  238. {
  239.   CORE_ADDR pc;
  240. };

  241. /* Fetch a byte on behalf of the opcode decoder.  HANDLE contains
  242.    the memory address of the next byte to fetch.  If successful,
  243.    the address in the handle is updated and the byte fetched is
  244.    returned as the value of the function.  If not successful, -1
  245.    is returned.  */

  246. static int
  247. msp430_get_opcode_byte (void *handle)
  248. {
  249.   struct msp430_get_opcode_byte_handle *opcdata = handle;
  250.   int status;
  251.   gdb_byte byte;

  252.   status = target_read_memory (opcdata->pc, &byte, 1);
  253.   if (status == 0)
  254.     {
  255.       opcdata->pc += 1;
  256.       return byte;
  257.     }
  258.   else
  259.     return -1;
  260. }

  261. /* Function for finding saved registers in a 'struct pv_area'; this
  262.    function is passed to pv_area_scan.

  263.    If VALUE is a saved register, ADDR says it was saved at a constant
  264.    offset from the frame base, and SIZE indicates that the whole
  265.    register was saved, record its offset.  */

  266. static void
  267. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  268. {
  269.   struct msp430_prologue *result = (struct msp430_prologue *) result_untyped;

  270.   if (value.kind == pvk_register
  271.       && value.k == 0
  272.       && pv_is_register (addr, MSP430_SP_REGNUM)
  273.       && size == register_size (target_gdbarch (), value.reg))
  274.     result->reg_offset[value.reg] = addr.k;
  275. }

  276. /* Analyze a prologue starting at START_PC, going no further than
  277.    LIMIT_PC.  Fill in RESULT as appropriate.  */

  278. static void
  279. msp430_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc,
  280.                          CORE_ADDR limit_pc, struct msp430_prologue *result)
  281. {
  282.   CORE_ADDR pc, next_pc;
  283.   int rn;
  284.   pv_t reg[MSP430_NUM_TOTAL_REGS];
  285.   struct pv_area *stack;
  286.   struct cleanup *back_to;
  287.   CORE_ADDR after_last_frame_setup_insn = start_pc;
  288.   int code_model = gdbarch_tdep (gdbarch)->code_model;
  289.   int sz;

  290.   memset (result, 0, sizeof (*result));

  291.   for (rn = 0; rn < MSP430_NUM_TOTAL_REGS; rn++)
  292.     {
  293.       reg[rn] = pv_register (rn, 0);
  294.       result->reg_offset[rn] = 1;
  295.     }

  296.   stack = make_pv_area (MSP430_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  297.   back_to = make_cleanup_free_pv_area (stack);

  298.   /* The call instruction has saved the return address on the stack.  */
  299.   sz = code_model == MSP_LARGE_CODE_MODEL ? 4 : 2;
  300.   reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM], -sz);
  301.   pv_area_store (stack, reg[MSP430_SP_REGNUM], sz, reg[MSP430_PC_REGNUM]);

  302.   pc = start_pc;
  303.   while (pc < limit_pc)
  304.     {
  305.       int bytes_read;
  306.       struct msp430_get_opcode_byte_handle opcode_handle;
  307.       MSP430_Opcode_Decoded opc;

  308.       opcode_handle.pc = pc;
  309.       bytes_read = msp430_decode_opcode (pc, &opc, msp430_get_opcode_byte,
  310.                                          &opcode_handle);
  311.       next_pc = pc + bytes_read;

  312.       if (opc.id == MSO_push && opc.op[0].type == MSP430_Operand_Register)
  313.         {
  314.           int rsrc = opc.op[0].reg;

  315.           reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM], -2);
  316.           pv_area_store (stack, reg[MSP430_SP_REGNUM], 2, reg[rsrc]);
  317.           after_last_frame_setup_insn = next_pc;
  318.         }
  319.       else if (opc.id == MSO_push        /* PUSHM  */
  320.                && opc.op[0].type == MSP430_Operand_None
  321.                && opc.op[1].type == MSP430_Operand_Register)
  322.         {
  323.           int rsrc = opc.op[1].reg;
  324.           int count = opc.repeats + 1;
  325.           int size = opc.size == 16 ? 2 : 4;

  326.           while (count > 0)
  327.             {
  328.               reg[MSP430_SP_REGNUM]
  329.                 = pv_add_constant (reg[MSP430_SP_REGNUM], -size);
  330.               pv_area_store (stack, reg[MSP430_SP_REGNUM], size, reg[rsrc]);
  331.               rsrc--;
  332.               count--;
  333.             }
  334.           after_last_frame_setup_insn = next_pc;
  335.         }
  336.       else if (opc.id == MSO_sub
  337.                && opc.op[0].type == MSP430_Operand_Register
  338.                && opc.op[0].reg == MSR_SP
  339.                && opc.op[1].type == MSP430_Operand_Immediate)
  340.         {
  341.           int addend = opc.op[1].addend;

  342.           reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM],
  343.                                                    -addend);
  344.           after_last_frame_setup_insn = next_pc;
  345.         }
  346.       else if (opc.id == MSO_mov
  347.                && opc.op[0].type == MSP430_Operand_Immediate
  348.                && 12 <= opc.op[0].reg && opc.op[0].reg <= 15)
  349.         after_last_frame_setup_insn = next_pc;
  350.       else
  351.         {
  352.           /* Terminate the prologue scan.  */
  353.           break;
  354.         }

  355.       pc = next_pc;
  356.     }

  357.   /* Is the frame size (offset, really) a known constant?  */
  358.   if (pv_is_register (reg[MSP430_SP_REGNUM], MSP430_SP_REGNUM))
  359.     result->frame_size = reg[MSP430_SP_REGNUM].k;

  360.   /* Record where all the registers were saved.  */
  361.   pv_area_scan (stack, check_for_saved, result);

  362.   result->prologue_end = after_last_frame_setup_insn;

  363.   do_cleanups (back_to);
  364. }

  365. /* Implement the "skip_prologue" gdbarch method.  */

  366. static CORE_ADDR
  367. msp430_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  368. {
  369.   const char *name;
  370.   CORE_ADDR func_addr, func_end;
  371.   struct msp430_prologue p;

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

  375.   msp430_analyze_prologue (gdbarch, pc, func_end, &p);
  376.   return p.prologue_end;
  377. }

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

  379. static CORE_ADDR
  380. msp430_unwind_pc (struct gdbarch *arch, struct frame_info *next_frame)
  381. {
  382.   return frame_unwind_register_unsigned (next_frame, MSP430_PC_REGNUM);
  383. }

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

  385. static CORE_ADDR
  386. msp430_unwind_sp (struct gdbarch *arch, struct frame_info *next_frame)
  387. {
  388.   return frame_unwind_register_unsigned (next_frame, MSP430_SP_REGNUM);
  389. }

  390. /* Given a frame described by THIS_FRAME, decode the prologue of its
  391.    associated function if there is not cache entry as specified by
  392.    THIS_PROLOGUE_CACHE.  Save the decoded prologue in the cache and
  393.    return that struct as the value of this function.  */

  394. static struct msp430_prologue *
  395. msp430_analyze_frame_prologue (struct frame_info *this_frame,
  396.                                void **this_prologue_cache)
  397. {
  398.   if (!*this_prologue_cache)
  399.     {
  400.       CORE_ADDR func_start, stop_addr;

  401.       *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct msp430_prologue);

  402.       func_start = get_frame_func (this_frame);
  403.       stop_addr = get_frame_pc (this_frame);

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

  408.       msp430_analyze_prologue (get_frame_arch (this_frame), func_start,
  409.                                stop_addr, *this_prologue_cache);
  410.     }

  411.   return *this_prologue_cache;
  412. }

  413. /* Given a frame and a prologue cache, return this frame's base.  */

  414. static CORE_ADDR
  415. msp430_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
  416. {
  417.   struct msp430_prologue *p
  418.     = msp430_analyze_frame_prologue (this_frame, this_prologue_cache);
  419.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, MSP430_SP_REGNUM);

  420.   return sp - p->frame_size;
  421. }

  422. /* Implement the "frame_this_id" method for unwinding frames.  */

  423. static void
  424. msp430_this_id (struct frame_info *this_frame,
  425.                 void **this_prologue_cache, struct frame_id *this_id)
  426. {
  427.   *this_id = frame_id_build (msp430_frame_base (this_frame,
  428.                                                 this_prologue_cache),
  429.                              get_frame_func (this_frame));
  430. }

  431. /* Implement the "frame_prev_register" method for unwinding frames.  */

  432. static struct value *
  433. msp430_prev_register (struct frame_info *this_frame,
  434.                       void **this_prologue_cache, int regnum)
  435. {
  436.   struct msp430_prologue *p
  437.     = msp430_analyze_frame_prologue (this_frame, this_prologue_cache);
  438.   CORE_ADDR frame_base = msp430_frame_base (this_frame, this_prologue_cache);

  439.   if (regnum == MSP430_SP_REGNUM)
  440.     return frame_unwind_got_constant (this_frame, regnum, frame_base);

  441.   /* If prologue analysis says we saved this register somewhere,
  442.      return a description of the stack slot holding it.  */
  443.   else if (p->reg_offset[regnum] != 1)
  444.     {
  445.       struct value *rv = frame_unwind_got_memory (this_frame, regnum,
  446.                                                   frame_base +
  447.                                                   p->reg_offset[regnum]);

  448.       if (regnum == MSP430_PC_REGNUM)
  449.         {
  450.           ULONGEST pc = value_as_long (rv);

  451.           return frame_unwind_got_constant (this_frame, regnum, pc);
  452.         }
  453.       return rv;
  454.     }

  455.   /* Otherwise, presume we haven't changed the value of this
  456.      register, and get it from the next frame.  */
  457.   else
  458.     return frame_unwind_got_register (this_frame, regnum, regnum);
  459. }

  460. static const struct frame_unwind msp430_unwind = {
  461.   NORMAL_FRAME,
  462.   default_frame_unwind_stop_reason,
  463.   msp430_this_id,
  464.   msp430_prev_register,
  465.   NULL,
  466.   default_frame_sniffer
  467. };

  468. /* Implement the "dwarf2_reg_to_regnum" gdbarch method.  */

  469. static int
  470. msp430_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  471. {
  472.   if (reg < MSP430_NUM_REGS)
  473.     return reg + MSP430_NUM_REGS;
  474.   else
  475.     {
  476.       warning (_("Unmapped DWARF Register #%d encountered."), reg);
  477.       return -1;
  478.     }
  479. }

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

  481. static enum return_value_convention
  482. msp430_return_value (struct gdbarch *gdbarch,
  483.                      struct value *function,
  484.                      struct type *valtype,
  485.                      struct regcache *regcache,
  486.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  487. {
  488.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  489.   LONGEST valtype_len = TYPE_LENGTH (valtype);
  490.   int code_model = gdbarch_tdep (gdbarch)->code_model;

  491.   if (TYPE_LENGTH (valtype) > 8
  492.       || TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  493.       || TYPE_CODE (valtype) == TYPE_CODE_UNION)
  494.     return RETURN_VALUE_STRUCT_CONVENTION;

  495.   if (readbuf)
  496.     {
  497.       ULONGEST u;
  498.       int argreg = MSP430_R12_REGNUM;
  499.       int offset = 0;

  500.       while (valtype_len > 0)
  501.         {
  502.           int size = 2;

  503.           if (code_model == MSP_LARGE_CODE_MODEL
  504.               && TYPE_CODE (valtype) == TYPE_CODE_PTR)
  505.             {
  506.               size = 4;
  507.             }

  508.           regcache_cooked_read_unsigned (regcache, argreg, &u);
  509.           store_unsigned_integer (readbuf + offset, size, byte_order, u);
  510.           valtype_len -= size;
  511.           offset += size;
  512.           argreg++;
  513.         }
  514.     }

  515.   if (writebuf)
  516.     {
  517.       ULONGEST u;
  518.       int argreg = MSP430_R12_REGNUM;
  519.       int offset = 0;

  520.       while (valtype_len > 0)
  521.         {
  522.           int size = 2;

  523.           if (code_model == MSP_LARGE_CODE_MODEL
  524.               && TYPE_CODE (valtype) == TYPE_CODE_PTR)
  525.             {
  526.               size = 4;
  527.             }

  528.           u = extract_unsigned_integer (writebuf + offset, size, byte_order);
  529.           regcache_cooked_write_unsigned (regcache, argreg, u);
  530.           valtype_len -= size;
  531.           offset += size;
  532.           argreg++;
  533.         }
  534.     }

  535.   return RETURN_VALUE_REGISTER_CONVENTION;
  536. }


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

  538. static CORE_ADDR
  539. msp430_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  540. {
  541.   return align_down (sp, 2);
  542. }


  543. /* Implement the "dummy_id" gdbarch method.  */

  544. static struct frame_id
  545. msp430_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  546. {
  547.   return
  548.     frame_id_build (get_frame_register_unsigned
  549.                     (this_frame, MSP430_SP_REGNUM),
  550.                     get_frame_pc (this_frame));
  551. }


  552. /* Implement the "push_dummy_call" gdbarch method.  */

  553. static CORE_ADDR
  554. msp430_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  555.                         struct regcache *regcache, CORE_ADDR bp_addr,
  556.                         int nargs, struct value **args, CORE_ADDR sp,
  557.                         int struct_return, CORE_ADDR struct_addr)
  558. {
  559.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  560.   int write_pass;
  561.   int sp_off = 0;
  562.   CORE_ADDR cfa;
  563.   int code_model = gdbarch_tdep (gdbarch)->code_model;

  564.   struct type *func_type = value_type (function);

  565.   /* Dereference function pointer types.  */
  566.   while (TYPE_CODE (func_type) == TYPE_CODE_PTR)
  567.     func_type = TYPE_TARGET_TYPE (func_type);

  568.   /* The end result had better be a function or a method.  */
  569.   gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC
  570.               || TYPE_CODE (func_type) == TYPE_CODE_METHOD);

  571.   /* We make two passes; the first does the stack allocation,
  572.      the second actually stores the arguments.  */
  573.   for (write_pass = 0; write_pass <= 1; write_pass++)
  574.     {
  575.       int i;
  576.       int arg_reg = MSP430_R12_REGNUM;
  577.       int args_on_stack = 0;

  578.       if (write_pass)
  579.         sp = align_down (sp - sp_off, 4);
  580.       sp_off = 0;

  581.       if (struct_return)
  582.         {
  583.           if (write_pass)
  584.             regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
  585.           arg_reg++;
  586.         }

  587.       /* Push the arguments.  */
  588.       for (i = 0; i < nargs; i++)
  589.         {
  590.           struct value *arg = args[i];
  591.           const gdb_byte *arg_bits = value_contents_all (arg);
  592.           struct type *arg_type = check_typedef (value_type (arg));
  593.           ULONGEST arg_size = TYPE_LENGTH (arg_type);
  594.           int offset;
  595.           int current_arg_on_stack;

  596.           current_arg_on_stack = 0;

  597.           if (TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
  598.               || TYPE_CODE (arg_type) == TYPE_CODE_UNION)
  599.             {
  600.               /* Aggregates of any size are passed by reference.  */
  601.               gdb_byte struct_addr[4];

  602.               store_unsigned_integer (struct_addr, 4, byte_order,
  603.                                       value_address (arg));
  604.               arg_bits = struct_addr;
  605.               arg_size = (code_model == MSP_LARGE_CODE_MODEL) ? 4 : 2;
  606.             }
  607.           else
  608.             {
  609.               /* Scalars bigger than 8 bytes such as complex doubles are passed
  610.                  on the stack.  */
  611.               if (arg_size > 8)
  612.                 current_arg_on_stack = 1;
  613.             }


  614.           for (offset = 0; offset < arg_size; offset += 2)
  615.             {
  616.               /* The condition below prevents 8 byte scalars from being split
  617.                  between registers and memory (stack).  It also prevents other
  618.                  splits once the stack has been written to.  */
  619.               if (!current_arg_on_stack
  620.                   && (arg_reg
  621.                       + ((arg_size == 8 || args_on_stack)
  622.                          ? ((arg_size - offset) / 2 - 1)
  623.                          : 0) <= MSP430_R15_REGNUM))
  624.                 {
  625.                   int size = 2;

  626.                   if (code_model == MSP_LARGE_CODE_MODEL
  627.                       && TYPE_CODE (arg_type) == TYPE_CODE_PTR)
  628.                     {
  629.                       /* Pointer arguments using large memory model are passed
  630.                          using entire register.  */
  631.                       if (offset != 0)
  632.                         continue;
  633.                       size = 4;
  634.                     }

  635.                   if (write_pass)
  636.                     regcache_cooked_write_unsigned (regcache, arg_reg,
  637.                                                     extract_unsigned_integer
  638.                                                     (arg_bits + offset, size,
  639.                                                      byte_order));

  640.                   arg_reg++;
  641.                 }
  642.               else
  643.                 {
  644.                   if (write_pass)
  645.                     write_memory (sp + sp_off, arg_bits + offset, 2);

  646.                   sp_off += 2;
  647.                   args_on_stack = 1;
  648.                   current_arg_on_stack = 1;
  649.                 }
  650.             }
  651.         }
  652.     }

  653.   /* Keep track of the stack address prior to pushing the return address.
  654.      This is the value that we'll return.  */
  655.   cfa = sp;

  656.   /* Push the return address.  */
  657.   {
  658.     int sz = (gdbarch_tdep (gdbarch)->code_model == MSP_SMALL_CODE_MODEL)
  659.       ? 2 : 4;
  660.     sp = sp - sz;
  661.     write_memory_unsigned_integer (sp, sz, byte_order, bp_addr);
  662.   }

  663.   /* Update the stack pointer.  */
  664.   regcache_cooked_write_unsigned (regcache, MSP430_SP_REGNUM, sp);

  665.   return cfa;
  666. }

  667. /* In order to keep code size small, the compiler may create epilogue
  668.    code through which more than one function epilogue is routed.  I.e.
  669.    the epilogue and return may just be a branch to some common piece of
  670.    code which is responsible for tearing down the frame and performing
  671.    the return.  These epilog (label) names will have the common prefix
  672.    defined here.  */

  673. static const char msp430_epilog_name_prefix[] = "__mspabi_func_epilog_";

  674. /* Implement the "in_return_stub" gdbarch method.  */

  675. static int
  676. msp430_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc,
  677.                        const char *name)
  678. {
  679.   return (name != NULL
  680.           && strncmp (msp430_epilog_name_prefix, name,
  681.                       strlen (msp430_epilog_name_prefix)) == 0);
  682. }

  683. /* Implement the "skip_trampoline_code" gdbarch method.  */
  684. static CORE_ADDR
  685. msp430_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  686. {
  687.   struct bound_minimal_symbol bms;
  688.   const char *stub_name;
  689.   struct gdbarch *gdbarch = get_frame_arch (frame);

  690.   bms = lookup_minimal_symbol_by_pc (pc);
  691.   if (!bms.minsym)
  692.     return pc;

  693.   stub_name = MSYMBOL_LINKAGE_NAME (bms.minsym);

  694.   if (gdbarch_tdep (gdbarch)->code_model == MSP_SMALL_CODE_MODEL
  695.       && msp430_in_return_stub (gdbarch, pc, stub_name))
  696.     {
  697.       CORE_ADDR sp = get_frame_register_unsigned (frame, MSP430_SP_REGNUM);

  698.       return read_memory_integer
  699.         (sp + 2 * (stub_name[strlen (msp430_epilog_name_prefix)] - '0'),
  700.          2, gdbarch_byte_order (gdbarch));
  701.     }

  702.   return pc;
  703. }

  704. /* Allocate and initialize a gdbarch object.  */

  705. static struct gdbarch *
  706. msp430_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  707. {
  708.   struct gdbarch *gdbarch;
  709.   struct gdbarch_tdep *tdep;
  710.   int elf_flags, isa, code_model;

  711.   /* Extract the elf_flags if available.  */
  712.   if (info.abfd != NULL
  713.       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  714.     elf_flags = elf_elfheader (info.abfd)->e_flags;
  715.   else
  716.     elf_flags = 0;

  717.   if (info.abfd != NULL)
  718.     switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  719.                                       OFBA_MSPABI_Tag_ISA))
  720.       {
  721.       case 1:
  722.         isa = MSP_ISA_MSP430;
  723.         code_model = MSP_SMALL_CODE_MODEL;
  724.         break;
  725.       case 2:
  726.         isa = MSP_ISA_MSP430X;
  727.         switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
  728.                                           OFBA_MSPABI_Tag_Code_Model))
  729.           {
  730.           case 1:
  731.             code_model = MSP_SMALL_CODE_MODEL;
  732.             break;
  733.           case 2:
  734.             code_model = MSP_LARGE_CODE_MODEL;
  735.             break;
  736.           default:
  737.             internal_error (__FILE__, __LINE__,
  738.                             _("Unknown msp430x code memory model"));
  739.             break;
  740.           }
  741.         break;
  742.       case 0:
  743.         /* This can happen when loading a previously dumped data structure.
  744.            Use the ISA and code model from the current architecture, provided
  745.            it's compatible.  */
  746.         {
  747.           struct gdbarch *ca = get_current_arch ();
  748.           if (ca && gdbarch_bfd_arch_info (ca)->arch == bfd_arch_msp430)
  749.             {
  750.               struct gdbarch_tdep *ca_tdep = gdbarch_tdep (ca);

  751.               elf_flags = ca_tdep->elf_flags;
  752.               isa = ca_tdep->isa;
  753.               code_model = ca_tdep->code_model;
  754.               break;
  755.             }
  756.           /* Otherwise, fall through...  */
  757.         }
  758.       default:
  759.         error (_("Unknown msp430 isa"));
  760.         break;
  761.       }
  762.   else
  763.     {
  764.       isa = MSP_ISA_MSP430;
  765.       code_model = MSP_SMALL_CODE_MODEL;
  766.     }


  767.   /* Try to find the architecture in the list of already defined
  768.      architectures.  */
  769.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  770.        arches != NULL;
  771.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  772.     {
  773.       struct gdbarch_tdep *candidate_tdep = gdbarch_tdep (arches->gdbarch);

  774.       if (candidate_tdep->elf_flags != elf_flags
  775.           || candidate_tdep->isa != isa
  776.           || candidate_tdep->code_model != code_model)
  777.         continue;

  778.       return arches->gdbarch;
  779.     }

  780.   /* None found, create a new architecture from the information
  781.      provided.  */
  782.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  783.   gdbarch = gdbarch_alloc (&info, tdep);
  784.   tdep->elf_flags = elf_flags;
  785.   tdep->isa = isa;
  786.   tdep->code_model = code_model;

  787.   /* Registers.  */
  788.   set_gdbarch_num_regs (gdbarch, MSP430_NUM_REGS);
  789.   set_gdbarch_num_pseudo_regs (gdbarch, MSP430_NUM_PSEUDO_REGS);
  790.   set_gdbarch_register_name (gdbarch, msp430_register_name);
  791.   if (isa == MSP_ISA_MSP430)
  792.     set_gdbarch_register_type (gdbarch, msp430_register_type);
  793.   else
  794.     set_gdbarch_register_type (gdbarch, msp430x_register_type);
  795.   set_gdbarch_pc_regnum (gdbarch, MSP430_PC_REGNUM);
  796.   set_gdbarch_sp_regnum (gdbarch, MSP430_SP_REGNUM);
  797.   set_gdbarch_register_reggroup_p (gdbarch, msp430_register_reggroup_p);
  798.   set_gdbarch_pseudo_register_read (gdbarch, msp430_pseudo_register_read);
  799.   set_gdbarch_pseudo_register_write (gdbarch, msp430_pseudo_register_write);
  800.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, msp430_dwarf2_reg_to_regnum);
  801.   set_gdbarch_register_sim_regno (gdbarch, msp430_register_sim_regno);

  802.   /* Data types.  */
  803.   set_gdbarch_char_signed (gdbarch, 0);
  804.   set_gdbarch_short_bit (gdbarch, 16);
  805.   set_gdbarch_int_bit (gdbarch, 16);
  806.   set_gdbarch_long_bit (gdbarch, 32);
  807.   set_gdbarch_long_long_bit (gdbarch, 64);
  808.   if (code_model == MSP_SMALL_CODE_MODEL)
  809.     {
  810.       set_gdbarch_ptr_bit (gdbarch, 16);
  811.       set_gdbarch_addr_bit (gdbarch, 16);
  812.     }
  813.   else                                /* MSP_LARGE_CODE_MODEL */
  814.     {
  815.       set_gdbarch_ptr_bit (gdbarch, 32);
  816.       set_gdbarch_addr_bit (gdbarch, 32);
  817.     }
  818.   set_gdbarch_dwarf2_addr_size (gdbarch, 4);
  819.   set_gdbarch_float_bit (gdbarch, 32);
  820.   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  821.   set_gdbarch_double_bit (gdbarch, 64);
  822.   set_gdbarch_long_double_bit (gdbarch, 64);
  823.   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  824.   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);

  825.   /* Breakpoints.  */
  826.   set_gdbarch_breakpoint_from_pc (gdbarch, msp430_breakpoint_from_pc);
  827.   set_gdbarch_decr_pc_after_break (gdbarch, 1);

  828.   /* Disassembly.  */
  829.   set_gdbarch_print_insn (gdbarch, print_insn_msp430);

  830.   /* Frames, prologues, etc.  */
  831.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  832.   set_gdbarch_skip_prologue (gdbarch, msp430_skip_prologue);
  833.   set_gdbarch_unwind_pc (gdbarch, msp430_unwind_pc);
  834.   set_gdbarch_unwind_sp (gdbarch, msp430_unwind_sp);
  835.   set_gdbarch_frame_align (gdbarch, msp430_frame_align);
  836.   dwarf2_append_unwinders (gdbarch);
  837.   frame_unwind_append_unwinder (gdbarch, &msp430_unwind);

  838.   /* Dummy frames, return values.  */
  839.   set_gdbarch_dummy_id (gdbarch, msp430_dummy_id);
  840.   set_gdbarch_push_dummy_call (gdbarch, msp430_push_dummy_call);
  841.   set_gdbarch_return_value (gdbarch, msp430_return_value);

  842.   /* Trampolines.  */
  843.   set_gdbarch_in_solib_return_trampoline (gdbarch, msp430_in_return_stub);
  844.   set_gdbarch_skip_trampoline_code (gdbarch, msp430_skip_trampoline_code);

  845.   /* Virtual tables.  */
  846.   set_gdbarch_vbit_in_delta (gdbarch, 0);

  847.   return gdbarch;
  848. }

  849. /* -Wmissing-prototypes */
  850. extern initialize_file_ftype _initialize_msp430_tdep;

  851. /* Register the initialization routine.  */

  852. void
  853. _initialize_msp430_tdep (void)
  854. {
  855.   register_gdbarch_init (bfd_arch_msp430, msp430_gdbarch_init);
  856. }