gdb/m68hc11-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for Motorola 68HC11 & 68HC12

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

  3.    Contributed by Stephane Carrez, stcarrez@nerim.fr

  4.    This file is part of GDB.

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

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

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


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

  33. #include "target.h"
  34. #include "opcode/m68hc11.h"
  35. #include "elf/m68hc11.h"
  36. #include "elf-bfd.h"

  37. /* Macros for setting and testing a bit in a minimal symbol.
  38.    For 68HC11/68HC12 we have two flags that tell which return
  39.    type the function is using.  This is used for prologue and frame
  40.    analysis to compute correct stack frame layout.

  41.    The MSB of the minimal symbol's "info" field is used for this purpose.

  42.    MSYMBOL_SET_RTC        Actually sets the "RTC" bit.
  43.    MSYMBOL_SET_RTI        Actually sets the "RTI" bit.
  44.    MSYMBOL_IS_RTC       Tests the "RTC" bit in a minimal symbol.
  45.    MSYMBOL_IS_RTI       Tests the "RTC" bit in a minimal symbol.  */

  46. #define MSYMBOL_SET_RTC(msym)                           \
  47.         MSYMBOL_TARGET_FLAG_1 (msym) = 1

  48. #define MSYMBOL_SET_RTI(msym)                           \
  49.         MSYMBOL_TARGET_FLAG_2 (msym) = 1

  50. #define MSYMBOL_IS_RTC(msym)                                \
  51.         MSYMBOL_TARGET_FLAG_1 (msym)

  52. #define MSYMBOL_IS_RTI(msym)                                \
  53.         MSYMBOL_TARGET_FLAG_2 (msym)

  54. enum insn_return_kind {
  55.   RETURN_RTS,
  56.   RETURN_RTC,
  57.   RETURN_RTI
  58. };


  59. /* Register numbers of various important registers.  */

  60. #define HARD_X_REGNUM         0
  61. #define HARD_D_REGNUM        1
  62. #define HARD_Y_REGNUM   2
  63. #define HARD_SP_REGNUM         3
  64. #define HARD_PC_REGNUM         4

  65. #define HARD_A_REGNUM   5
  66. #define HARD_B_REGNUM   6
  67. #define HARD_CCR_REGNUM 7

  68. /* 68HC12 page number register.
  69.    Note: to keep a compatibility with gcc register naming, we must
  70.    not have to rename FP and other soft registers.  The page register
  71.    is a real hard register and must therefore be counted by gdbarch_num_regs.
  72.    For this it has the same number as Z register (which is not used).  */
  73. #define HARD_PAGE_REGNUM 8
  74. #define M68HC11_LAST_HARD_REG (HARD_PAGE_REGNUM)

  75. /* Z is replaced by X or Y by gcc during machine reorg.
  76.    ??? There is no way to get it and even know whether
  77.    it's in X or Y or in ZS.  */
  78. #define SOFT_Z_REGNUM        8

  79. /* Soft registers.  These registers are special.  There are treated
  80.    like normal hard registers by gcc and gdb (ie, within dwarf2 info).
  81.    They are physically located in memory.  */
  82. #define SOFT_FP_REGNUM       9
  83. #define SOFT_TMP_REGNUM     10
  84. #define SOFT_ZS_REGNUM      11
  85. #define SOFT_XY_REGNUM      12
  86. #define SOFT_UNUSED_REGNUM  13
  87. #define SOFT_D1_REGNUM      14
  88. #define SOFT_D32_REGNUM     (SOFT_D1_REGNUM+31)
  89. #define M68HC11_MAX_SOFT_REGS 32

  90. #define M68HC11_NUM_REGS        (8)
  91. #define M68HC11_NUM_PSEUDO_REGS (M68HC11_MAX_SOFT_REGS+5)
  92. #define M68HC11_ALL_REGS        (M68HC11_NUM_REGS+M68HC11_NUM_PSEUDO_REGS)

  93. #define M68HC11_REG_SIZE    (2)

  94. #define M68HC12_NUM_REGS        (9)
  95. #define M68HC12_NUM_PSEUDO_REGS ((M68HC11_MAX_SOFT_REGS+5)+1-1)
  96. #define M68HC12_HARD_PC_REGNUM  (SOFT_D32_REGNUM+1)

  97. struct insn_sequence;
  98. struct gdbarch_tdep
  99.   {
  100.     /* Stack pointer correction value.  For 68hc11, the stack pointer points
  101.        to the next push location.  An offset of 1 must be applied to obtain
  102.        the address where the last value is saved.  For 68hc12, the stack
  103.        pointer points to the last value pushed.  No offset is necessary.  */
  104.     int stack_correction;

  105.     /* Description of instructions in the prologue.  */
  106.     struct insn_sequence *prologue;

  107.     /* True if the page memory bank register is available
  108.        and must be used.  */
  109.     int use_page_register;

  110.     /* ELF flags for ABI.  */
  111.     int elf_flags;
  112.   };

  113. #define STACK_CORRECTION(gdbarch) (gdbarch_tdep (gdbarch)->stack_correction)
  114. #define USE_PAGE_REGISTER(gdbarch) (gdbarch_tdep (gdbarch)->use_page_register)

  115. struct m68hc11_unwind_cache
  116. {
  117.   /* The previous frame's inner most stack address.  Used as this
  118.      frame ID's stack_addr.  */
  119.   CORE_ADDR prev_sp;
  120.   /* The frame's base, optionally used by the high-level debug info.  */
  121.   CORE_ADDR base;
  122.   CORE_ADDR pc;
  123.   int size;
  124.   int prologue_type;
  125.   CORE_ADDR return_pc;
  126.   CORE_ADDR sp_offset;
  127.   int frameless;
  128.   enum insn_return_kind return_kind;

  129.   /* Table indicating the location of each and every register.  */
  130.   struct trad_frame_saved_reg *saved_regs;
  131. };

  132. /* Table of registers for 68HC11.  This includes the hard registers
  133.    and the soft registers used by GCC.  */
  134. static char *
  135. m68hc11_register_names[] =
  136. {
  137.   "x",    "d",    "y",    "sp",   "pc",   "a",    "b",
  138.   "ccr""page", "frame","tmp""zs",   "xy",   0,
  139.   "d1",   "d2",   "d3",   "d4",   "d5",   "d6",   "d7",
  140.   "d8",   "d9",   "d10""d11""d12""d13""d14",
  141.   "d15""d16""d17""d18""d19""d20""d21",
  142.   "d22""d23""d24""d25""d26""d27""d28",
  143.   "d29""d30""d31""d32"
  144. };

  145. struct m68hc11_soft_reg
  146. {
  147.   const char *name;
  148.   CORE_ADDR   addr;
  149. };

  150. static struct m68hc11_soft_reg soft_regs[M68HC11_ALL_REGS];

  151. #define M68HC11_FP_ADDR soft_regs[SOFT_FP_REGNUM].addr

  152. static int soft_min_addr;
  153. static int soft_max_addr;
  154. static int soft_reg_initialized = 0;

  155. /* Look in the symbol table for the address of a pseudo register
  156.    in memory.  If we don't find it, pretend the register is not used
  157.    and not available.  */
  158. static void
  159. m68hc11_get_register_info (struct m68hc11_soft_reg *reg, const char *name)
  160. {
  161.   struct bound_minimal_symbol msymbol;

  162.   msymbol = lookup_minimal_symbol (name, NULL, NULL);
  163.   if (msymbol.minsym)
  164.     {
  165.       reg->addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
  166.       reg->name = xstrdup (name);

  167.       /* Keep track of the address range for soft registers.  */
  168.       if (reg->addr < (CORE_ADDR) soft_min_addr)
  169.         soft_min_addr = reg->addr;
  170.       if (reg->addr > (CORE_ADDR) soft_max_addr)
  171.         soft_max_addr = reg->addr;
  172.     }
  173.   else
  174.     {
  175.       reg->name = 0;
  176.       reg->addr = 0;
  177.     }
  178. }

  179. /* Initialize the table of soft register addresses according
  180.    to the symbol table.  */
  181.   static void
  182. m68hc11_initialize_register_info (void)
  183. {
  184.   int i;

  185.   if (soft_reg_initialized)
  186.     return;

  187.   soft_min_addr = INT_MAX;
  188.   soft_max_addr = 0;
  189.   for (i = 0; i < M68HC11_ALL_REGS; i++)
  190.     {
  191.       soft_regs[i].name = 0;
  192.     }

  193.   m68hc11_get_register_info (&soft_regs[SOFT_FP_REGNUM], "_.frame");
  194.   m68hc11_get_register_info (&soft_regs[SOFT_TMP_REGNUM], "_.tmp");
  195.   m68hc11_get_register_info (&soft_regs[SOFT_ZS_REGNUM], "_.z");
  196.   soft_regs[SOFT_Z_REGNUM] = soft_regs[SOFT_ZS_REGNUM];
  197.   m68hc11_get_register_info (&soft_regs[SOFT_XY_REGNUM], "_.xy");

  198.   for (i = SOFT_D1_REGNUM; i < M68HC11_MAX_SOFT_REGS; i++)
  199.     {
  200.       char buf[10];

  201.       xsnprintf (buf, sizeof (buf), "_.d%d", i - SOFT_D1_REGNUM + 1);
  202.       m68hc11_get_register_info (&soft_regs[i], buf);
  203.     }

  204.   if (soft_regs[SOFT_FP_REGNUM].name == 0)
  205.     warning (_("No frame soft register found in the symbol table.\n"
  206.                "Stack backtrace will not work."));
  207.   soft_reg_initialized = 1;
  208. }

  209. /* Given an address in memory, return the soft register number if
  210.    that address corresponds to a soft register.  Returns -1 if not.  */
  211. static int
  212. m68hc11_which_soft_register (CORE_ADDR addr)
  213. {
  214.   int i;

  215.   if (addr < soft_min_addr || addr > soft_max_addr)
  216.     return -1;

  217.   for (i = SOFT_FP_REGNUM; i < M68HC11_ALL_REGS; i++)
  218.     {
  219.       if (soft_regs[i].name && soft_regs[i].addr == addr)
  220.         return i;
  221.     }
  222.   return -1;
  223. }

  224. /* Fetch a pseudo register.  The 68hc11 soft registers are treated like
  225.    pseudo registers.  They are located in memory.  Translate the register
  226.    fetch into a memory read.  */
  227. static enum register_status
  228. m68hc11_pseudo_register_read (struct gdbarch *gdbarch,
  229.                               struct regcache *regcache,
  230.                               int regno, gdb_byte *buf)
  231. {
  232.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  233.   /* The PC is a pseudo reg only for 68HC12 with the memory bank
  234.      addressing mode.  */
  235.   if (regno == M68HC12_HARD_PC_REGNUM)
  236.     {
  237.       ULONGEST pc;
  238.       const int regsize = 4;
  239.       enum register_status status;

  240.       status = regcache_cooked_read_unsigned (regcache, HARD_PC_REGNUM, &pc);
  241.       if (status != REG_VALID)
  242.         return status;
  243.       if (pc >= 0x8000 && pc < 0xc000)
  244.         {
  245.           ULONGEST page;

  246.           regcache_cooked_read_unsigned (regcache, HARD_PAGE_REGNUM, &page);
  247.           pc -= 0x8000;
  248.           pc += (page << 14);
  249.           pc += 0x1000000;
  250.         }
  251.       store_unsigned_integer (buf, regsize, byte_order, pc);
  252.       return REG_VALID;
  253.     }

  254.   m68hc11_initialize_register_info ();

  255.   /* Fetch a soft register: translate into a memory read.  */
  256.   if (soft_regs[regno].name)
  257.     {
  258.       target_read_memory (soft_regs[regno].addr, buf, 2);
  259.     }
  260.   else
  261.     {
  262.       memset (buf, 0, 2);
  263.     }

  264.   return REG_VALID;
  265. }

  266. /* Store a pseudo register.  Translate the register store
  267.    into a memory write.  */
  268. static void
  269. m68hc11_pseudo_register_write (struct gdbarch *gdbarch,
  270.                                struct regcache *regcache,
  271.                                int regno, const gdb_byte *buf)
  272. {
  273.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  274.   /* The PC is a pseudo reg only for 68HC12 with the memory bank
  275.      addressing mode.  */
  276.   if (regno == M68HC12_HARD_PC_REGNUM)
  277.     {
  278.       const int regsize = 4;
  279.       gdb_byte *tmp = alloca (regsize);
  280.       CORE_ADDR pc;

  281.       memcpy (tmp, buf, regsize);
  282.       pc = extract_unsigned_integer (tmp, regsize, byte_order);
  283.       if (pc >= 0x1000000)
  284.         {
  285.           pc -= 0x1000000;
  286.           regcache_cooked_write_unsigned (regcache, HARD_PAGE_REGNUM,
  287.                                           (pc >> 14) & 0x0ff);
  288.           pc &= 0x03fff;
  289.           regcache_cooked_write_unsigned (regcache, HARD_PC_REGNUM,
  290.                                           pc + 0x8000);
  291.         }
  292.       else
  293.         regcache_cooked_write_unsigned (regcache, HARD_PC_REGNUM, pc);
  294.       return;
  295.     }

  296.   m68hc11_initialize_register_info ();

  297.   /* Store a soft register: translate into a memory write.  */
  298.   if (soft_regs[regno].name)
  299.     {
  300.       const int regsize = 2;
  301.       gdb_byte *tmp = alloca (regsize);
  302.       memcpy (tmp, buf, regsize);
  303.       target_write_memory (soft_regs[regno].addr, tmp, regsize);
  304.     }
  305. }

  306. static const char *
  307. m68hc11_register_name (struct gdbarch *gdbarch, int reg_nr)
  308. {
  309.   if (reg_nr == M68HC12_HARD_PC_REGNUM && USE_PAGE_REGISTER (gdbarch))
  310.     return "pc";
  311.   if (reg_nr == HARD_PC_REGNUM && USE_PAGE_REGISTER (gdbarch))
  312.     return "ppc";

  313.   if (reg_nr < 0)
  314.     return NULL;
  315.   if (reg_nr >= M68HC11_ALL_REGS)
  316.     return NULL;

  317.   m68hc11_initialize_register_info ();

  318.   /* If we don't know the address of a soft register, pretend it
  319.      does not exist.  */
  320.   if (reg_nr > M68HC11_LAST_HARD_REG && soft_regs[reg_nr].name == 0)
  321.     return NULL;
  322.   return m68hc11_register_names[reg_nr];
  323. }

  324. static const unsigned char *
  325. m68hc11_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  326.                             int *lenptr)
  327. {
  328.   static unsigned char breakpoint[] = {0x0};

  329.   *lenptr = sizeof (breakpoint);
  330.   return breakpoint;
  331. }


  332. /* 68HC11 & 68HC12 prologue analysis.  */

  333. #define MAX_CODES 12

  334. /* 68HC11 opcodes.  */
  335. #undef M6811_OP_PAGE2
  336. #define M6811_OP_PAGE2   (0x18)
  337. #define M6811_OP_LDX     (0xde)
  338. #define M6811_OP_LDX_EXT (0xfe)
  339. #define M6811_OP_PSHX    (0x3c)
  340. #define M6811_OP_STS     (0x9f)
  341. #define M6811_OP_STS_EXT (0xbf)
  342. #define M6811_OP_TSX     (0x30)
  343. #define M6811_OP_XGDX    (0x8f)
  344. #define M6811_OP_ADDD    (0xc3)
  345. #define M6811_OP_TXS     (0x35)
  346. #define M6811_OP_DES     (0x34)

  347. /* 68HC12 opcodes.  */
  348. #define M6812_OP_PAGE2   (0x18)
  349. #define M6812_OP_MOVW    (0x01)
  350. #define M6812_PB_PSHW    (0xae)
  351. #define M6812_OP_STS     (0x5f)
  352. #define M6812_OP_STS_EXT (0x7f)
  353. #define M6812_OP_LEAS    (0x1b)
  354. #define M6812_OP_PSHX    (0x34)
  355. #define M6812_OP_PSHY    (0x35)

  356. /* Operand extraction.  */
  357. #define OP_DIRECT      (0x100) /* 8-byte direct addressing.  */
  358. #define OP_IMM_LOW     (0x200) /* Low part of 16-bit constant/address.  */
  359. #define OP_IMM_HIGH    (0x300) /* High part of 16-bit constant/address.  */
  360. #define OP_PBYTE       (0x400) /* 68HC12 indexed operand.  */

  361. /* Identification of the sequence.  */
  362. enum m6811_seq_type
  363. {
  364.   P_LAST = 0,
  365.   P_SAVE_REG,  /* Save a register on the stack.  */
  366.   P_SET_FRAME, /* Setup the frame pointer.  */
  367.   P_LOCAL_1,   /* Allocate 1 byte for locals.  */
  368.   P_LOCAL_2,   /* Allocate 2 bytes for locals.  */
  369.   P_LOCAL_N    /* Allocate N bytes for locals.  */
  370. };

  371. struct insn_sequence {
  372.   enum m6811_seq_type type;
  373.   unsigned length;
  374.   unsigned short code[MAX_CODES];
  375. };

  376. /* Sequence of instructions in the 68HC11 function prologue.  */
  377. static struct insn_sequence m6811_prologue[] = {
  378.   /* Sequences to save a soft-register.  */
  379.   { P_SAVE_REG, 3, { M6811_OP_LDX, OP_DIRECT,
  380.                      M6811_OP_PSHX } },
  381.   { P_SAVE_REG, 5, { M6811_OP_PAGE2, M6811_OP_LDX, OP_DIRECT,
  382.                      M6811_OP_PAGE2, M6811_OP_PSHX } },
  383.   { P_SAVE_REG, 4, { M6811_OP_LDX_EXT, OP_IMM_HIGH, OP_IMM_LOW,
  384.                      M6811_OP_PSHX } },
  385.   { P_SAVE_REG, 6, { M6811_OP_PAGE2, M6811_OP_LDX_EXT, OP_IMM_HIGH, OP_IMM_LOW,
  386.                      M6811_OP_PAGE2, M6811_OP_PSHX } },

  387.   /* Sequences to allocate local variables.  */
  388.   { P_LOCAL_N,  7, { M6811_OP_TSX,
  389.                      M6811_OP_XGDX,
  390.                      M6811_OP_ADDD, OP_IMM_HIGH, OP_IMM_LOW,
  391.                      M6811_OP_XGDX,
  392.                      M6811_OP_TXS } },
  393.   { P_LOCAL_N, 11, { M6811_OP_PAGE2, M6811_OP_TSX,
  394.                      M6811_OP_PAGE2, M6811_OP_XGDX,
  395.                      M6811_OP_ADDD, OP_IMM_HIGH, OP_IMM_LOW,
  396.                      M6811_OP_PAGE2, M6811_OP_XGDX,
  397.                      M6811_OP_PAGE2, M6811_OP_TXS } },
  398.   { P_LOCAL_1,  1, { M6811_OP_DES } },
  399.   { P_LOCAL_2,  1, { M6811_OP_PSHX } },
  400.   { P_LOCAL_2,  2, { M6811_OP_PAGE2, M6811_OP_PSHX } },

  401.   /* Initialize the frame pointer.  */
  402.   { P_SET_FRAME, 2, { M6811_OP_STS, OP_DIRECT } },
  403.   { P_SET_FRAME, 3, { M6811_OP_STS_EXT, OP_IMM_HIGH, OP_IMM_LOW } },
  404.   { P_LAST, 0, { 0 } }
  405. };


  406. /* Sequence of instructions in the 68HC12 function prologue.  */
  407. static struct insn_sequence m6812_prologue[] = {
  408.   { P_SAVE_REG,  5, { M6812_OP_PAGE2, M6812_OP_MOVW, M6812_PB_PSHW,
  409.                       OP_IMM_HIGH, OP_IMM_LOW } },
  410.   { P_SET_FRAME, 2, { M6812_OP_STS, OP_DIRECT } },
  411.   { P_SET_FRAME, 3, { M6812_OP_STS_EXT, OP_IMM_HIGH, OP_IMM_LOW } },
  412.   { P_LOCAL_N,   2, { M6812_OP_LEAS, OP_PBYTE } },
  413.   { P_LOCAL_2,   1, { M6812_OP_PSHX } },
  414.   { P_LOCAL_2,   1, { M6812_OP_PSHY } },
  415.   { P_LAST, 0 }
  416. };


  417. /* Analyze the sequence of instructions starting at the given address.
  418.    Returns a pointer to the sequence when it is recognized and
  419.    the optional value (constant/address) associated with it.  */
  420. static struct insn_sequence *
  421. m68hc11_analyze_instruction (struct gdbarch *gdbarch,
  422.                              struct insn_sequence *seq, CORE_ADDR pc,
  423.                              CORE_ADDR *val)
  424. {
  425.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  426.   unsigned char buffer[MAX_CODES];
  427.   unsigned bufsize;
  428.   unsigned j;
  429.   CORE_ADDR cur_val;
  430.   short v = 0;

  431.   bufsize = 0;
  432.   for (; seq->type != P_LAST; seq++)
  433.     {
  434.       cur_val = 0;
  435.       for (j = 0; j < seq->length; j++)
  436.         {
  437.           if (bufsize < j + 1)
  438.             {
  439.               buffer[bufsize] = read_memory_unsigned_integer (pc + bufsize,
  440.                                                               1, byte_order);
  441.               bufsize++;
  442.             }
  443.           /* Continue while we match the opcode.  */
  444.           if (seq->code[j] == buffer[j])
  445.             continue;

  446.           if ((seq->code[j] & 0xf00) == 0)
  447.             break;

  448.           /* Extract a sequence parameter (address or constant).  */
  449.           switch (seq->code[j])
  450.             {
  451.             case OP_DIRECT:
  452.               cur_val = (CORE_ADDR) buffer[j];
  453.               break;

  454.             case OP_IMM_HIGH:
  455.               cur_val = cur_val & 0x0ff;
  456.               cur_val |= (buffer[j] << 8);
  457.               break;

  458.             case OP_IMM_LOW:
  459.               cur_val &= 0x0ff00;
  460.               cur_val |= buffer[j];
  461.               break;

  462.             case OP_PBYTE:
  463.               if ((buffer[j] & 0xE0) == 0x80)
  464.                 {
  465.                   v = buffer[j] & 0x1f;
  466.                   if (v & 0x10)
  467.                     v |= 0xfff0;
  468.                 }
  469.               else if ((buffer[j] & 0xfe) == 0xf0)
  470.                 {
  471.                   v = read_memory_unsigned_integer (pc + j + 1, 1, byte_order);
  472.                   if (buffer[j] & 1)
  473.                     v |= 0xff00;
  474.                 }
  475.               else if (buffer[j] == 0xf2)
  476.                 {
  477.                   v = read_memory_unsigned_integer (pc + j + 1, 2, byte_order);
  478.                 }
  479.               cur_val = v;
  480.               break;
  481.             }
  482.         }

  483.       /* We have a full match.  */
  484.       if (j == seq->length)
  485.         {
  486.           *val = cur_val;
  487.           return seq;
  488.         }
  489.     }
  490.   return 0;
  491. }

  492. /* Return the instruction that the function at the PC is using.  */
  493. static enum insn_return_kind
  494. m68hc11_get_return_insn (CORE_ADDR pc)
  495. {
  496.   struct bound_minimal_symbol sym;

  497.   /* A flag indicating that this is a STO_M68HC12_FAR or STO_M68HC12_INTERRUPT
  498.      function is stored by elfread.c in the high bit of the info field.
  499.      Use this to decide which instruction the function uses to return.  */
  500.   sym = lookup_minimal_symbol_by_pc (pc);
  501.   if (sym.minsym == 0)
  502.     return RETURN_RTS;

  503.   if (MSYMBOL_IS_RTC (sym.minsym))
  504.     return RETURN_RTC;
  505.   else if (MSYMBOL_IS_RTI (sym.minsym))
  506.     return RETURN_RTI;
  507.   else
  508.     return RETURN_RTS;
  509. }

  510. /* Analyze the function prologue to find some information
  511.    about the function:
  512.     - the PC of the first line (for m68hc11_skip_prologue)
  513.     - the offset of the previous frame saved address (from current frame)
  514.     - the soft registers which are pushed.  */
  515. static CORE_ADDR
  516. m68hc11_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  517.                        CORE_ADDR current_pc, struct m68hc11_unwind_cache *info)
  518. {
  519.   LONGEST save_addr;
  520.   CORE_ADDR func_end;
  521.   int size;
  522.   int found_frame_point;
  523.   int saved_reg;
  524.   int done = 0;
  525.   struct insn_sequence *seq_table;

  526.   info->size = 0;
  527.   info->sp_offset = 0;
  528.   if (pc >= current_pc)
  529.     return current_pc;

  530.   size = 0;

  531.   m68hc11_initialize_register_info ();
  532.   if (pc == 0)
  533.     {
  534.       info->size = 0;
  535.       return pc;
  536.     }

  537.   seq_table = gdbarch_tdep (gdbarch)->prologue;

  538.   /* The 68hc11 stack is as follows:


  539.      |           |
  540.      +-----------+
  541.      |           |
  542.      | args      |
  543.      |           |
  544.      +-----------+
  545.      | PC-return |
  546.      +-----------+
  547.      | Old frame |
  548.      +-----------+
  549.      |           |
  550.      | Locals    |
  551.      |           |
  552.      +-----------+ <--- current frame
  553.      |           |

  554.      With most processors (like 68K) the previous frame can be computed
  555.      easily because it is always at a fixed offset (see link/unlink).
  556.      That is, locals are accessed with negative offsets, arguments are
  557.      accessed with positive ones.  Since 68hc11 only supports offsets
  558.      in the range [0..255], the frame is defined at the bottom of
  559.      locals (see picture).

  560.      The purpose of the analysis made here is to find out the size
  561.      of locals in this function.  An alternative to this is to use
  562.      DWARF2 info.  This would be better but I don't know how to
  563.      access dwarf2 debug from this function.

  564.      Walk from the function entry point to the point where we save
  565.      the frame.  While walking instructions, compute the size of bytes
  566.      which are pushed.  This gives us the index to access the previous
  567.      frame.

  568.      We limit the search to 128 bytes so that the algorithm is bounded
  569.      in case of random and wrong code.  We also stop and abort if
  570.      we find an instruction which is not supposed to appear in the
  571.      prologue (as generated by gcc 2.95, 2.96).  */

  572.   func_end = pc + 128;
  573.   found_frame_point = 0;
  574.   info->size = 0;
  575.   save_addr = 0;
  576.   while (!done && pc + 2 < func_end)
  577.     {
  578.       struct insn_sequence *seq;
  579.       CORE_ADDR val;

  580.       seq = m68hc11_analyze_instruction (gdbarch, seq_table, pc, &val);
  581.       if (seq == 0)
  582.         break;

  583.       /* If we are within the instruction group, we can't advance the
  584.          pc nor the stack offset.  Otherwise the caller's stack computed
  585.          from the current stack can be wrong.  */
  586.       if (pc + seq->length > current_pc)
  587.         break;

  588.       pc = pc + seq->length;
  589.       if (seq->type == P_SAVE_REG)
  590.         {
  591.           if (found_frame_point)
  592.             {
  593.               saved_reg = m68hc11_which_soft_register (val);
  594.               if (saved_reg < 0)
  595.                 break;

  596.               save_addr -= 2;
  597.               if (info->saved_regs)
  598.                 info->saved_regs[saved_reg].addr = save_addr;
  599.             }
  600.           else
  601.             {
  602.               size += 2;
  603.             }
  604.         }
  605.       else if (seq->type == P_SET_FRAME)
  606.         {
  607.           found_frame_point = 1;
  608.           info->size = size;
  609.         }
  610.       else if (seq->type == P_LOCAL_1)
  611.         {
  612.           size += 1;
  613.         }
  614.       else if (seq->type == P_LOCAL_2)
  615.         {
  616.           size += 2;
  617.         }
  618.       else if (seq->type == P_LOCAL_N)
  619.         {
  620.           /* Stack pointer is decremented for the allocation.  */
  621.           if (val & 0x8000)
  622.             size -= (int) (val) | 0xffff0000;
  623.           else
  624.             size -= val;
  625.         }
  626.     }
  627.   if (found_frame_point == 0)
  628.     info->sp_offset = size;
  629.   else
  630.     info->sp_offset = -1;
  631.   return pc;
  632. }

  633. static CORE_ADDR
  634. m68hc11_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  635. {
  636.   CORE_ADDR func_addr, func_end;
  637.   struct symtab_and_line sal;
  638.   struct m68hc11_unwind_cache tmp_cache = { 0 };

  639.   /* If we have line debugging information, then the end of the
  640.      prologue should be the first assembly instruction of the
  641.      first source line.  */
  642.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  643.     {
  644.       sal = find_pc_line (func_addr, 0);
  645.       if (sal.end && sal.end < func_end)
  646.         return sal.end;
  647.     }

  648.   pc = m68hc11_scan_prologue (gdbarch, pc, (CORE_ADDR) -1, &tmp_cache);
  649.   return pc;
  650. }

  651. static CORE_ADDR
  652. m68hc11_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  653. {
  654.   ULONGEST pc;

  655.   pc = frame_unwind_register_unsigned (next_frame,
  656.                                        gdbarch_pc_regnum (gdbarch));
  657.   return pc;
  658. }

  659. /* Put here the code to store, into fi->saved_regs, the addresses of
  660.    the saved registers of frame described by FRAME_INFO.  This
  661.    includes special registers such as pc and fp saved in special ways
  662.    in the stack frame.  sp is even more special: the address we return
  663.    for it IS the sp for the next frame.  */

  664. static struct m68hc11_unwind_cache *
  665. m68hc11_frame_unwind_cache (struct frame_info *this_frame,
  666.                             void **this_prologue_cache)
  667. {
  668.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  669.   ULONGEST prev_sp;
  670.   ULONGEST this_base;
  671.   struct m68hc11_unwind_cache *info;
  672.   CORE_ADDR current_pc;
  673.   int i;

  674.   if ((*this_prologue_cache))
  675.     return (*this_prologue_cache);

  676.   info = FRAME_OBSTACK_ZALLOC (struct m68hc11_unwind_cache);
  677.   (*this_prologue_cache) = info;
  678.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  679.   info->pc = get_frame_func (this_frame);

  680.   info->size = 0;
  681.   info->return_kind = m68hc11_get_return_insn (info->pc);

  682.   /* The SP was moved to the FP.  This indicates that a new frame
  683.      was created.  Get THIS frame's FP value by unwinding it from
  684.      the next frame.  */
  685.   this_base = get_frame_register_unsigned (this_frame, SOFT_FP_REGNUM);
  686.   if (this_base == 0)
  687.     {
  688.       info->base = 0;
  689.       return info;
  690.     }

  691.   current_pc = get_frame_pc (this_frame);
  692.   if (info->pc != 0)
  693.     m68hc11_scan_prologue (gdbarch, info->pc, current_pc, info);

  694.   info->saved_regs[HARD_PC_REGNUM].addr = info->size;

  695.   if (info->sp_offset != (CORE_ADDR) -1)
  696.     {
  697.       info->saved_regs[HARD_PC_REGNUM].addr = info->sp_offset;
  698.       this_base = get_frame_register_unsigned (this_frame, HARD_SP_REGNUM);
  699.       prev_sp = this_base + info->sp_offset + 2;
  700.       this_base += STACK_CORRECTION (gdbarch);
  701.     }
  702.   else
  703.     {
  704.       /* The FP points at the last saved register.  Adjust the FP back
  705.          to before the first saved register giving the SP.  */
  706.       prev_sp = this_base + info->size + 2;

  707.       this_base += STACK_CORRECTION (gdbarch);
  708.       if (soft_regs[SOFT_FP_REGNUM].name)
  709.         info->saved_regs[SOFT_FP_REGNUM].addr = info->size - 2;
  710.    }

  711.   if (info->return_kind == RETURN_RTC)
  712.     {
  713.       prev_sp += 1;
  714.       info->saved_regs[HARD_PAGE_REGNUM].addr = info->size;
  715.       info->saved_regs[HARD_PC_REGNUM].addr = info->size + 1;
  716.     }
  717.   else if (info->return_kind == RETURN_RTI)
  718.     {
  719.       prev_sp += 7;
  720.       info->saved_regs[HARD_CCR_REGNUM].addr = info->size;
  721.       info->saved_regs[HARD_D_REGNUM].addr = info->size + 1;
  722.       info->saved_regs[HARD_X_REGNUM].addr = info->size + 3;
  723.       info->saved_regs[HARD_Y_REGNUM].addr = info->size + 5;
  724.       info->saved_regs[HARD_PC_REGNUM].addr = info->size + 7;
  725.     }

  726.   /* Add 1 here to adjust for the post-decrement nature of the push
  727.      instruction.  */
  728.   info->prev_sp = prev_sp;

  729.   info->base = this_base;

  730.   /* Adjust all the saved registers so that they contain addresses and not
  731.      offsets.  */
  732.   for (i = 0;
  733.        i < gdbarch_num_regs (gdbarch)
  734.            + gdbarch_num_pseudo_regs (gdbarch) - 1;
  735.        i++)
  736.     if (trad_frame_addr_p (info->saved_regs, i))
  737.       {
  738.         info->saved_regs[i].addr += this_base;
  739.       }

  740.   /* The previous frame's SP needed to be computed.  Save the computed
  741.      value.  */
  742.   trad_frame_set_value (info->saved_regs, HARD_SP_REGNUM, info->prev_sp);

  743.   return info;
  744. }

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

  747. static void
  748. m68hc11_frame_this_id (struct frame_info *this_frame,
  749.                        void **this_prologue_cache,
  750.                        struct frame_id *this_id)
  751. {
  752.   struct m68hc11_unwind_cache *info
  753.     = m68hc11_frame_unwind_cache (this_frame, this_prologue_cache);
  754.   CORE_ADDR base;
  755.   CORE_ADDR func;
  756.   struct frame_id id;

  757.   /* The FUNC is easy.  */
  758.   func = get_frame_func (this_frame);

  759.   /* Hopefully the prologue analysis either correctly determined the
  760.      frame's base (which is the SP from the previous frame), or set
  761.      that base to "NULL".  */
  762.   base = info->prev_sp;
  763.   if (base == 0)
  764.     return;

  765.   id = frame_id_build (base, func);
  766.   (*this_id) = id;
  767. }

  768. static struct value *
  769. m68hc11_frame_prev_register (struct frame_info *this_frame,
  770.                              void **this_prologue_cache, int regnum)
  771. {
  772.   struct value *value;
  773.   struct m68hc11_unwind_cache *info
  774.     = m68hc11_frame_unwind_cache (this_frame, this_prologue_cache);

  775.   value = trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);

  776.   /* Take into account the 68HC12 specific call (PC + page).  */
  777.   if (regnum == HARD_PC_REGNUM
  778.       && info->return_kind == RETURN_RTC
  779.       && USE_PAGE_REGISTER (get_frame_arch (this_frame)))
  780.     {
  781.       CORE_ADDR pc = value_as_long (value);
  782.       if (pc >= 0x08000 && pc < 0x0c000)
  783.         {
  784.           CORE_ADDR page;

  785.           release_value (value);
  786.           value_free (value);

  787.           value = trad_frame_get_prev_register (this_frame, info->saved_regs,
  788.                                                 HARD_PAGE_REGNUM);
  789.           page = value_as_long (value);
  790.           release_value (value);
  791.           value_free (value);

  792.           pc -= 0x08000;
  793.           pc += ((page & 0x0ff) << 14);
  794.           pc += 0x1000000;

  795.           return frame_unwind_got_constant (this_frame, regnum, pc);
  796.         }
  797.     }

  798.   return value;
  799. }

  800. static const struct frame_unwind m68hc11_frame_unwind = {
  801.   NORMAL_FRAME,
  802.   default_frame_unwind_stop_reason,
  803.   m68hc11_frame_this_id,
  804.   m68hc11_frame_prev_register,
  805.   NULL,
  806.   default_frame_sniffer
  807. };

  808. static CORE_ADDR
  809. m68hc11_frame_base_address (struct frame_info *this_frame, void **this_cache)
  810. {
  811.   struct m68hc11_unwind_cache *info
  812.     = m68hc11_frame_unwind_cache (this_frame, this_cache);

  813.   return info->base;
  814. }

  815. static CORE_ADDR
  816. m68hc11_frame_args_address (struct frame_info *this_frame, void **this_cache)
  817. {
  818.   CORE_ADDR addr;
  819.   struct m68hc11_unwind_cache *info
  820.     = m68hc11_frame_unwind_cache (this_frame, this_cache);

  821.   addr = info->base + info->size;
  822.   if (info->return_kind == RETURN_RTC)
  823.     addr += 1;
  824.   else if (info->return_kind == RETURN_RTI)
  825.     addr += 7;

  826.   return addr;
  827. }

  828. static const struct frame_base m68hc11_frame_base = {
  829.   &m68hc11_frame_unwind,
  830.   m68hc11_frame_base_address,
  831.   m68hc11_frame_base_address,
  832.   m68hc11_frame_args_address
  833. };

  834. static CORE_ADDR
  835. m68hc11_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  836. {
  837.   ULONGEST sp;
  838.   sp = frame_unwind_register_unsigned (next_frame, HARD_SP_REGNUM);
  839.   return sp;
  840. }

  841. /* Assuming THIS_FRAME is a dummy, return the frame ID of that dummy
  842.    frame.  The frame ID's base needs to match the TOS value saved by
  843.    save_dummy_frame_tos(), and the PC match the dummy frame's breakpoint.  */

  844. static struct frame_id
  845. m68hc11_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  846. {
  847.   ULONGEST tos;
  848.   CORE_ADDR pc = get_frame_pc (this_frame);

  849.   tos = get_frame_register_unsigned (this_frame, SOFT_FP_REGNUM);
  850.   tos += 2;
  851.   return frame_id_build (tos, pc);
  852. }


  853. /* Get and print the register from the given frame.  */
  854. static void
  855. m68hc11_print_register (struct gdbarch *gdbarch, struct ui_file *file,
  856.                         struct frame_info *frame, int regno)
  857. {
  858.   LONGEST rval;

  859.   if (regno == HARD_PC_REGNUM || regno == HARD_SP_REGNUM
  860.       || regno == SOFT_FP_REGNUM || regno == M68HC12_HARD_PC_REGNUM)
  861.     rval = get_frame_register_unsigned (frame, regno);
  862.   else
  863.     rval = get_frame_register_signed (frame, regno);

  864.   if (regno == HARD_A_REGNUM || regno == HARD_B_REGNUM
  865.       || regno == HARD_CCR_REGNUM || regno == HARD_PAGE_REGNUM)
  866.     {
  867.       fprintf_filtered (file, "0x%02x   ", (unsigned char) rval);
  868.       if (regno != HARD_CCR_REGNUM)
  869.         print_longest (file, 'd', 1, rval);
  870.     }
  871.   else
  872.     {
  873.       if (regno == HARD_PC_REGNUM && gdbarch_tdep (gdbarch)->use_page_register)
  874.         {
  875.           ULONGEST page;

  876.           page = get_frame_register_unsigned (frame, HARD_PAGE_REGNUM);
  877.           fprintf_filtered (file, "0x%02x:%04x ", (unsigned) page,
  878.                             (unsigned) rval);
  879.         }
  880.       else
  881.         {
  882.           fprintf_filtered (file, "0x%04x ", (unsigned) rval);
  883.           if (regno != HARD_PC_REGNUM && regno != HARD_SP_REGNUM
  884.               && regno != SOFT_FP_REGNUM && regno != M68HC12_HARD_PC_REGNUM)
  885.             print_longest (file, 'd', 1, rval);
  886.         }
  887.     }

  888.   if (regno == HARD_CCR_REGNUM)
  889.     {
  890.       /* CCR register */
  891.       int C, Z, N, V;
  892.       unsigned char l = rval & 0xff;

  893.       fprintf_filtered (file, "%c%c%c%c%c%c%c%c   ",
  894.                         l & M6811_S_BIT ? 'S' : '-',
  895.                         l & M6811_X_BIT ? 'X' : '-',
  896.                         l & M6811_H_BIT ? 'H' : '-',
  897.                         l & M6811_I_BIT ? 'I' : '-',
  898.                         l & M6811_N_BIT ? 'N' : '-',
  899.                         l & M6811_Z_BIT ? 'Z' : '-',
  900.                         l & M6811_V_BIT ? 'V' : '-',
  901.                         l & M6811_C_BIT ? 'C' : '-');
  902.       N = (l & M6811_N_BIT) != 0;
  903.       Z = (l & M6811_Z_BIT) != 0;
  904.       V = (l & M6811_V_BIT) != 0;
  905.       C = (l & M6811_C_BIT) != 0;

  906.       /* Print flags following the h8300.  */
  907.       if ((C | Z) == 0)
  908.         fprintf_filtered (file, "u> ");
  909.       else if ((C | Z) == 1)
  910.         fprintf_filtered (file, "u<= ");
  911.       else if (C == 0)
  912.         fprintf_filtered (file, "u< ");

  913.       if (Z == 0)
  914.         fprintf_filtered (file, "!= ");
  915.       else
  916.         fprintf_filtered (file, "== ");

  917.       if ((N ^ V) == 0)
  918.         fprintf_filtered (file, ">= ");
  919.       else
  920.         fprintf_filtered (file, "< ");

  921.       if ((Z | (N ^ V)) == 0)
  922.         fprintf_filtered (file, "> ");
  923.       else
  924.         fprintf_filtered (file, "<= ");
  925.     }
  926. }

  927. /* Same as 'info reg' but prints the registers in a different way.  */
  928. static void
  929. m68hc11_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
  930.                               struct frame_info *frame, int regno, int cpregs)
  931. {
  932.   if (regno >= 0)
  933.     {
  934.       const char *name = gdbarch_register_name (gdbarch, regno);

  935.       if (!name || !*name)
  936.         return;

  937.       fprintf_filtered (file, "%-10s ", name);
  938.       m68hc11_print_register (gdbarch, file, frame, regno);
  939.       fprintf_filtered (file, "\n");
  940.     }
  941.   else
  942.     {
  943.       int i, nr;

  944.       fprintf_filtered (file, "PC=");
  945.       m68hc11_print_register (gdbarch, file, frame, HARD_PC_REGNUM);

  946.       fprintf_filtered (file, " SP=");
  947.       m68hc11_print_register (gdbarch, file, frame, HARD_SP_REGNUM);

  948.       fprintf_filtered (file, " FP=");
  949.       m68hc11_print_register (gdbarch, file, frame, SOFT_FP_REGNUM);

  950.       fprintf_filtered (file, "\nCCR=");
  951.       m68hc11_print_register (gdbarch, file, frame, HARD_CCR_REGNUM);

  952.       fprintf_filtered (file, "\nD=");
  953.       m68hc11_print_register (gdbarch, file, frame, HARD_D_REGNUM);

  954.       fprintf_filtered (file, " X=");
  955.       m68hc11_print_register (gdbarch, file, frame, HARD_X_REGNUM);

  956.       fprintf_filtered (file, " Y=");
  957.       m68hc11_print_register (gdbarch, file, frame, HARD_Y_REGNUM);

  958.       if (gdbarch_tdep (gdbarch)->use_page_register)
  959.         {
  960.           fprintf_filtered (file, "\nPage=");
  961.           m68hc11_print_register (gdbarch, file, frame, HARD_PAGE_REGNUM);
  962.         }
  963.       fprintf_filtered (file, "\n");

  964.       nr = 0;
  965.       for (i = SOFT_D1_REGNUM; i < M68HC11_ALL_REGS; i++)
  966.         {
  967.           /* Skip registers which are not defined in the symbol table.  */
  968.           if (soft_regs[i].name == 0)
  969.             continue;

  970.           fprintf_filtered (file, "D%d=", i - SOFT_D1_REGNUM + 1);
  971.           m68hc11_print_register (gdbarch, file, frame, i);
  972.           nr++;
  973.           if ((nr % 8) == 7)
  974.             fprintf_filtered (file, "\n");
  975.           else
  976.             fprintf_filtered (file, " ");
  977.         }
  978.       if (nr && (nr % 8) != 7)
  979.         fprintf_filtered (file, "\n");
  980.     }
  981. }

  982. static CORE_ADDR
  983. m68hc11_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  984.                          struct regcache *regcache, CORE_ADDR bp_addr,
  985.                          int nargs, struct value **args, CORE_ADDR sp,
  986.                          int struct_return, CORE_ADDR struct_addr)
  987. {
  988.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  989.   int argnum;
  990.   int first_stack_argnum;
  991.   struct type *type;
  992.   const gdb_byte *val;
  993.   gdb_byte buf[2];

  994.   first_stack_argnum = 0;
  995.   if (struct_return)
  996.     {
  997.       regcache_cooked_write_unsigned (regcache, HARD_D_REGNUM, struct_addr);
  998.     }
  999.   else if (nargs > 0)
  1000.     {
  1001.       type = value_type (args[0]);

  1002.       /* First argument is passed in D and X registers.  */
  1003.       if (TYPE_LENGTH (type) <= 4)
  1004.         {
  1005.           ULONGEST v;

  1006.           v = extract_unsigned_integer (value_contents (args[0]),
  1007.                                         TYPE_LENGTH (type), byte_order);
  1008.           first_stack_argnum = 1;

  1009.           regcache_cooked_write_unsigned (regcache, HARD_D_REGNUM, v);
  1010.           if (TYPE_LENGTH (type) > 2)
  1011.             {
  1012.               v >>= 16;
  1013.               regcache_cooked_write_unsigned (regcache, HARD_X_REGNUM, v);
  1014.             }
  1015.         }
  1016.     }

  1017.   for (argnum = nargs - 1; argnum >= first_stack_argnum; argnum--)
  1018.     {
  1019.       type = value_type (args[argnum]);

  1020.       if (TYPE_LENGTH (type) & 1)
  1021.         {
  1022.           static gdb_byte zero = 0;

  1023.           sp--;
  1024.           write_memory (sp, &zero, 1);
  1025.         }
  1026.       val = value_contents (args[argnum]);
  1027.       sp -= TYPE_LENGTH (type);
  1028.       write_memory (sp, val, TYPE_LENGTH (type));
  1029.     }

  1030.   /* Store return address.  */
  1031.   sp -= 2;
  1032.   store_unsigned_integer (buf, 2, byte_order, bp_addr);
  1033.   write_memory (sp, buf, 2);

  1034.   /* Finally, update the stack pointer...  */
  1035.   sp -= STACK_CORRECTION (gdbarch);
  1036.   regcache_cooked_write_unsigned (regcache, HARD_SP_REGNUM, sp);

  1037.   /* ...and fake a frame pointer.  */
  1038.   regcache_cooked_write_unsigned (regcache, SOFT_FP_REGNUM, sp);

  1039.   /* DWARF2/GCC uses the stack address *before* the function call as a
  1040.      frame's CFA.  */
  1041.   return sp + 2;
  1042. }


  1043. /* Return the GDB type object for the "standard" data type
  1044.    of data in register N.  */

  1045. static struct type *
  1046. m68hc11_register_type (struct gdbarch *gdbarch, int reg_nr)
  1047. {
  1048.   switch (reg_nr)
  1049.     {
  1050.     case HARD_PAGE_REGNUM:
  1051.     case HARD_A_REGNUM:
  1052.     case HARD_B_REGNUM:
  1053.     case HARD_CCR_REGNUM:
  1054.       return builtin_type (gdbarch)->builtin_uint8;

  1055.     case M68HC12_HARD_PC_REGNUM:
  1056.       return builtin_type (gdbarch)->builtin_uint32;

  1057.     default:
  1058.       return builtin_type (gdbarch)->builtin_uint16;
  1059.     }
  1060. }

  1061. static void
  1062. m68hc11_store_return_value (struct type *type, struct regcache *regcache,
  1063.                             const gdb_byte *valbuf)
  1064. {
  1065.   int len;

  1066.   len = TYPE_LENGTH (type);

  1067.   /* First argument is passed in D and X registers.  */
  1068.   if (len <= 2)
  1069.     regcache_raw_write_part (regcache, HARD_D_REGNUM, 2 - len, len, valbuf);
  1070.   else if (len <= 4)
  1071.     {
  1072.       regcache_raw_write_part (regcache, HARD_X_REGNUM, 4 - len,
  1073.                                len - 2, valbuf);
  1074.       regcache_raw_write (regcache, HARD_D_REGNUM, valbuf + (len - 2));
  1075.     }
  1076.   else
  1077.     error (_("return of value > 4 is not supported."));
  1078. }


  1079. /* Given a return value in `regcache' with a type `type',
  1080.    extract and copy its value into `valbuf'.  */

  1081. static void
  1082. m68hc11_extract_return_value (struct type *type, struct regcache *regcache,
  1083.                               void *valbuf)
  1084. {
  1085.   gdb_byte buf[M68HC11_REG_SIZE];

  1086.   regcache_raw_read (regcache, HARD_D_REGNUM, buf);
  1087.   switch (TYPE_LENGTH (type))
  1088.     {
  1089.     case 1:
  1090.       memcpy (valbuf, buf + 1, 1);
  1091.       break;

  1092.     case 2:
  1093.       memcpy (valbuf, buf, 2);
  1094.       break;

  1095.     case 3:
  1096.       memcpy ((char*) valbuf + 1, buf, 2);
  1097.       regcache_raw_read (regcache, HARD_X_REGNUM, buf);
  1098.       memcpy (valbuf, buf + 1, 1);
  1099.       break;

  1100.     case 4:
  1101.       memcpy ((char*) valbuf + 2, buf, 2);
  1102.       regcache_raw_read (regcache, HARD_X_REGNUM, buf);
  1103.       memcpy (valbuf, buf, 2);
  1104.       break;

  1105.     default:
  1106.       error (_("bad size for return value"));
  1107.     }
  1108. }

  1109. static enum return_value_convention
  1110. m68hc11_return_value (struct gdbarch *gdbarch, struct value *function,
  1111.                       struct type *valtype, struct regcache *regcache,
  1112.                       gdb_byte *readbuf, const gdb_byte *writebuf)
  1113. {
  1114.   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  1115.       || TYPE_CODE (valtype) == TYPE_CODE_UNION
  1116.       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY
  1117.       || TYPE_LENGTH (valtype) > 4)
  1118.     return RETURN_VALUE_STRUCT_CONVENTION;
  1119.   else
  1120.     {
  1121.       if (readbuf != NULL)
  1122.         m68hc11_extract_return_value (valtype, regcache, readbuf);
  1123.       if (writebuf != NULL)
  1124.         m68hc11_store_return_value (valtype, regcache, writebuf);
  1125.       return RETURN_VALUE_REGISTER_CONVENTION;
  1126.     }
  1127. }

  1128. /* Test whether the ELF symbol corresponds to a function using rtc or
  1129.    rti to return.  */

  1130. static void
  1131. m68hc11_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
  1132. {
  1133.   unsigned char flags;

  1134.   flags = ((elf_symbol_type *)sym)->internal_elf_sym.st_other;
  1135.   if (flags & STO_M68HC12_FAR)
  1136.     MSYMBOL_SET_RTC (msym);
  1137.   if (flags & STO_M68HC12_INTERRUPT)
  1138.     MSYMBOL_SET_RTI (msym);
  1139. }

  1140. static int
  1141. gdb_print_insn_m68hc11 (bfd_vma memaddr, disassemble_info *info)
  1142. {
  1143.   if (info->arch == bfd_arch_m68hc11)
  1144.     return print_insn_m68hc11 (memaddr, info);
  1145.   else
  1146.     return print_insn_m68hc12 (memaddr, info);
  1147. }



  1148. /* 68HC11/68HC12 register groups.
  1149.    Identify real hard registers and soft registers used by gcc.  */

  1150. static struct reggroup *m68hc11_soft_reggroup;
  1151. static struct reggroup *m68hc11_hard_reggroup;

  1152. static void
  1153. m68hc11_init_reggroups (void)
  1154. {
  1155.   m68hc11_hard_reggroup = reggroup_new ("hard", USER_REGGROUP);
  1156.   m68hc11_soft_reggroup = reggroup_new ("soft", USER_REGGROUP);
  1157. }

  1158. static void
  1159. m68hc11_add_reggroups (struct gdbarch *gdbarch)
  1160. {
  1161.   reggroup_add (gdbarch, m68hc11_hard_reggroup);
  1162.   reggroup_add (gdbarch, m68hc11_soft_reggroup);
  1163.   reggroup_add (gdbarch, general_reggroup);
  1164.   reggroup_add (gdbarch, float_reggroup);
  1165.   reggroup_add (gdbarch, all_reggroup);
  1166.   reggroup_add (gdbarch, save_reggroup);
  1167.   reggroup_add (gdbarch, restore_reggroup);
  1168.   reggroup_add (gdbarch, vector_reggroup);
  1169.   reggroup_add (gdbarch, system_reggroup);
  1170. }

  1171. static int
  1172. m68hc11_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  1173.                              struct reggroup *group)
  1174. {
  1175.   /* We must save the real hard register as well as gcc
  1176.      soft registers including the frame pointer.  */
  1177.   if (group == save_reggroup || group == restore_reggroup)
  1178.     {
  1179.       return (regnum <= gdbarch_num_regs (gdbarch)
  1180.               || ((regnum == SOFT_FP_REGNUM
  1181.                    || regnum == SOFT_TMP_REGNUM
  1182.                    || regnum == SOFT_ZS_REGNUM
  1183.                    || regnum == SOFT_XY_REGNUM)
  1184.                   && m68hc11_register_name (gdbarch, regnum)));
  1185.     }

  1186.   /* Group to identify gcc soft registers (d1..dN).  */
  1187.   if (group == m68hc11_soft_reggroup)
  1188.     {
  1189.       return regnum >= SOFT_D1_REGNUM
  1190.              && m68hc11_register_name (gdbarch, regnum);
  1191.     }

  1192.   if (group == m68hc11_hard_reggroup)
  1193.     {
  1194.       return regnum == HARD_PC_REGNUM || regnum == HARD_SP_REGNUM
  1195.         || regnum == HARD_X_REGNUM || regnum == HARD_D_REGNUM
  1196.         || regnum == HARD_Y_REGNUM || regnum == HARD_CCR_REGNUM;
  1197.     }
  1198.   return default_register_reggroup_p (gdbarch, regnum, group);
  1199. }

  1200. static struct gdbarch *
  1201. m68hc11_gdbarch_init (struct gdbarch_info info,
  1202.                       struct gdbarch_list *arches)
  1203. {
  1204.   struct gdbarch *gdbarch;
  1205.   struct gdbarch_tdep *tdep;
  1206.   int elf_flags;

  1207.   soft_reg_initialized = 0;

  1208.   /* Extract the elf_flags if available.  */
  1209.   if (info.abfd != NULL
  1210.       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1211.     elf_flags = elf_elfheader (info.abfd)->e_flags;
  1212.   else
  1213.     elf_flags = 0;

  1214.   /* Try to find a pre-existing architecture.  */
  1215.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  1216.        arches != NULL;
  1217.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  1218.     {
  1219.       if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
  1220.         continue;

  1221.       return arches->gdbarch;
  1222.     }

  1223.   /* Need a new architecture.  Fill in a target specific vector.  */
  1224.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  1225.   gdbarch = gdbarch_alloc (&info, tdep);
  1226.   tdep->elf_flags = elf_flags;

  1227.   switch (info.bfd_arch_info->arch)
  1228.     {
  1229.     case bfd_arch_m68hc11:
  1230.       tdep->stack_correction = 1;
  1231.       tdep->use_page_register = 0;
  1232.       tdep->prologue = m6811_prologue;
  1233.       set_gdbarch_addr_bit (gdbarch, 16);
  1234.       set_gdbarch_num_pseudo_regs (gdbarch, M68HC11_NUM_PSEUDO_REGS);
  1235.       set_gdbarch_pc_regnum (gdbarch, HARD_PC_REGNUM);
  1236.       set_gdbarch_num_regs (gdbarch, M68HC11_NUM_REGS);
  1237.       break;

  1238.     case bfd_arch_m68hc12:
  1239.       tdep->stack_correction = 0;
  1240.       tdep->use_page_register = elf_flags & E_M68HC12_BANKS;
  1241.       tdep->prologue = m6812_prologue;
  1242.       set_gdbarch_addr_bit (gdbarch, elf_flags & E_M68HC12_BANKS ? 32 : 16);
  1243.       set_gdbarch_num_pseudo_regs (gdbarch,
  1244.                                    elf_flags & E_M68HC12_BANKS
  1245.                                    ? M68HC12_NUM_PSEUDO_REGS
  1246.                                    : M68HC11_NUM_PSEUDO_REGS);
  1247.       set_gdbarch_pc_regnum (gdbarch, elf_flags & E_M68HC12_BANKS
  1248.                              ? M68HC12_HARD_PC_REGNUM : HARD_PC_REGNUM);
  1249.       set_gdbarch_num_regs (gdbarch, elf_flags & E_M68HC12_BANKS
  1250.                             ? M68HC12_NUM_REGS : M68HC11_NUM_REGS);
  1251.       break;

  1252.     default:
  1253.       break;
  1254.     }

  1255.   /* Initially set everything according to the ABI.
  1256.      Use 16-bit integers since it will be the case for most
  1257.      programs.  The size of these types should normally be set
  1258.      according to the dwarf2 debug information.  */
  1259.   set_gdbarch_short_bit (gdbarch, 16);
  1260.   set_gdbarch_int_bit (gdbarch, elf_flags & E_M68HC11_I32 ? 32 : 16);
  1261.   set_gdbarch_float_bit (gdbarch, 32);
  1262.   if (elf_flags & E_M68HC11_F64)
  1263.     {
  1264.       set_gdbarch_double_bit (gdbarch, 64);
  1265.       set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  1266.     }
  1267.   else
  1268.     {
  1269.       set_gdbarch_double_bit (gdbarch, 32);
  1270.       set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  1271.     }
  1272.   set_gdbarch_long_double_bit (gdbarch, 64);
  1273.   set_gdbarch_long_bit (gdbarch, 32);
  1274.   set_gdbarch_ptr_bit (gdbarch, 16);
  1275.   set_gdbarch_long_long_bit (gdbarch, 64);

  1276.   /* Characters are unsigned.  */
  1277.   set_gdbarch_char_signed (gdbarch, 0);

  1278.   set_gdbarch_unwind_pc (gdbarch, m68hc11_unwind_pc);
  1279.   set_gdbarch_unwind_sp (gdbarch, m68hc11_unwind_sp);

  1280.   /* Set register info.  */
  1281.   set_gdbarch_fp0_regnum (gdbarch, -1);

  1282.   set_gdbarch_sp_regnum (gdbarch, HARD_SP_REGNUM);
  1283.   set_gdbarch_register_name (gdbarch, m68hc11_register_name);
  1284.   set_gdbarch_register_type (gdbarch, m68hc11_register_type);
  1285.   set_gdbarch_pseudo_register_read (gdbarch, m68hc11_pseudo_register_read);
  1286.   set_gdbarch_pseudo_register_write (gdbarch, m68hc11_pseudo_register_write);

  1287.   set_gdbarch_push_dummy_call (gdbarch, m68hc11_push_dummy_call);

  1288.   set_gdbarch_return_value (gdbarch, m68hc11_return_value);
  1289.   set_gdbarch_skip_prologue (gdbarch, m68hc11_skip_prologue);
  1290.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1291.   set_gdbarch_breakpoint_from_pc (gdbarch, m68hc11_breakpoint_from_pc);
  1292.   set_gdbarch_print_insn (gdbarch, gdb_print_insn_m68hc11);

  1293.   m68hc11_add_reggroups (gdbarch);
  1294.   set_gdbarch_register_reggroup_p (gdbarch, m68hc11_register_reggroup_p);
  1295.   set_gdbarch_print_registers_info (gdbarch, m68hc11_print_registers_info);

  1296.   /* Hook in the DWARF CFI frame unwinder.  */
  1297.   dwarf2_append_unwinders (gdbarch);

  1298.   frame_unwind_append_unwinder (gdbarch, &m68hc11_frame_unwind);
  1299.   frame_base_set_default (gdbarch, &m68hc11_frame_base);

  1300.   /* Methods for saving / extracting a dummy frame's ID.  The ID's
  1301.      stack address must match the SP value returned by
  1302.      PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos.  */
  1303.   set_gdbarch_dummy_id (gdbarch, m68hc11_dummy_id);

  1304.   /* Return the unwound PC value.  */
  1305.   set_gdbarch_unwind_pc (gdbarch, m68hc11_unwind_pc);

  1306.   /* Minsymbol frobbing.  */
  1307.   set_gdbarch_elf_make_msymbol_special (gdbarch,
  1308.                                         m68hc11_elf_make_msymbol_special);

  1309.   set_gdbarch_believe_pcc_promotion (gdbarch, 1);

  1310.   return gdbarch;
  1311. }

  1312. /* -Wmissing-prototypes */
  1313. extern initialize_file_ftype _initialize_m68hc11_tdep;

  1314. void
  1315. _initialize_m68hc11_tdep (void)
  1316. {
  1317.   register_gdbarch_init (bfd_arch_m68hc11, m68hc11_gdbarch_init);
  1318.   register_gdbarch_init (bfd_arch_m68hc12, m68hc11_gdbarch_init);
  1319.   m68hc11_init_reggroups ();
  1320. }