gdb/h8300-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-machine dependent code for Renesas H8/300, for GDB.

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

  3.    This file is part of GDB.

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

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

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

  14. /*
  15.    Contributed by Steve Chamberlain
  16.    sac@cygnus.com
  17. */

  18. #include "defs.h"
  19. #include "value.h"
  20. #include "arch-utils.h"
  21. #include "regcache.h"
  22. #include "gdbcore.h"
  23. #include "objfiles.h"
  24. #include "dis-asm.h"
  25. #include "dwarf2-frame.h"
  26. #include "frame-base.h"
  27. #include "frame-unwind.h"

  28. enum gdb_regnum
  29. {
  30.   E_R0_REGNUM, E_ER0_REGNUM = E_R0_REGNUM, E_ARG0_REGNUM = E_R0_REGNUM,
  31.   E_RET0_REGNUM = E_R0_REGNUM,
  32.   E_R1_REGNUM, E_ER1_REGNUM = E_R1_REGNUM, E_RET1_REGNUM = E_R1_REGNUM,
  33.   E_R2_REGNUM, E_ER2_REGNUM = E_R2_REGNUM, E_ARGLAST_REGNUM = E_R2_REGNUM,
  34.   E_R3_REGNUM, E_ER3_REGNUM = E_R3_REGNUM,
  35.   E_R4_REGNUM, E_ER4_REGNUM = E_R4_REGNUM,
  36.   E_R5_REGNUM, E_ER5_REGNUM = E_R5_REGNUM,
  37.   E_R6_REGNUM, E_ER6_REGNUM = E_R6_REGNUM, E_FP_REGNUM = E_R6_REGNUM,
  38.   E_SP_REGNUM,
  39.   E_CCR_REGNUM,
  40.   E_PC_REGNUM,
  41.   E_CYCLES_REGNUM,
  42.   E_TICK_REGNUM, E_EXR_REGNUM = E_TICK_REGNUM,
  43.   E_INST_REGNUM, E_TICKS_REGNUM = E_INST_REGNUM,
  44.   E_INSTS_REGNUM,
  45.   E_MACH_REGNUM,
  46.   E_MACL_REGNUM,
  47.   E_SBR_REGNUM,
  48.   E_VBR_REGNUM
  49. };

  50. #define H8300_MAX_NUM_REGS 18

  51. #define E_PSEUDO_CCR_REGNUM(gdbarch) (gdbarch_num_regs (gdbarch))
  52. #define E_PSEUDO_EXR_REGNUM(gdbarch) (gdbarch_num_regs (gdbarch)+1)

  53. struct h8300_frame_cache
  54. {
  55.   /* Base address.  */
  56.   CORE_ADDR base;
  57.   CORE_ADDR sp_offset;
  58.   CORE_ADDR pc;

  59.   /* Flag showing that a frame has been created in the prologue code.  */
  60.   int uses_fp;

  61.   /* Saved registers.  */
  62.   CORE_ADDR saved_regs[H8300_MAX_NUM_REGS];
  63.   CORE_ADDR saved_sp;
  64. };

  65. enum
  66. {
  67.   h8300_reg_size = 2,
  68.   h8300h_reg_size = 4,
  69.   h8300_max_reg_size = 4,
  70. };

  71. static int is_h8300hmode (struct gdbarch *gdbarch);
  72. static int is_h8300smode (struct gdbarch *gdbarch);
  73. static int is_h8300sxmode (struct gdbarch *gdbarch);
  74. static int is_h8300_normal_mode (struct gdbarch *gdbarch);

  75. #define BINWORD(gdbarch) ((is_h8300hmode (gdbarch) \
  76.                   && !is_h8300_normal_mode (gdbarch)) \
  77.                  ? h8300h_reg_size : h8300_reg_size)

  78. static CORE_ADDR
  79. h8300_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  80. {
  81.   return frame_unwind_register_unsigned (next_frame, E_PC_REGNUM);
  82. }

  83. static CORE_ADDR
  84. h8300_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  85. {
  86.   return frame_unwind_register_unsigned (next_frame, E_SP_REGNUM);
  87. }

  88. static struct frame_id
  89. h8300_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  90. {
  91.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
  92.   return frame_id_build (sp, get_frame_pc (this_frame));
  93. }

  94. /* Normal frames.  */

  95. /* Allocate and initialize a frame cache.  */

  96. static void
  97. h8300_init_frame_cache (struct gdbarch *gdbarch,
  98.                         struct h8300_frame_cache *cache)
  99. {
  100.   int i;

  101.   /* Base address.  */
  102.   cache->base = 0;
  103.   cache->sp_offset = 0;
  104.   cache->pc = 0;

  105.   /* Frameless until proven otherwise.  */
  106.   cache->uses_fp = 0;

  107.   /* Saved registers.  We initialize these to -1 since zero is a valid
  108.      offset (that's where %fp is supposed to be stored).  */
  109.   for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  110.     cache->saved_regs[i] = -1;
  111. }

  112. #define IS_MOVB_RnRm(x)                (((x) & 0xff88) == 0x0c88)
  113. #define IS_MOVW_RnRm(x)                (((x) & 0xff88) == 0x0d00)
  114. #define IS_MOVL_RnRm(x)                (((x) & 0xff88) == 0x0f80)
  115. #define IS_MOVB_Rn16_SP(x)        (((x) & 0xfff0) == 0x6ee0)
  116. #define IS_MOVB_EXT(x)                ((x) == 0x7860)
  117. #define IS_MOVB_Rn24_SP(x)        (((x) & 0xfff0) == 0x6aa0)
  118. #define IS_MOVW_Rn16_SP(x)        (((x) & 0xfff0) == 0x6fe0)
  119. #define IS_MOVW_EXT(x)                ((x) == 0x78e0)
  120. #define IS_MOVW_Rn24_SP(x)        (((x) & 0xfff0) == 0x6ba0)
  121. /* Same instructions as mov.w, just prefixed with 0x0100.  */
  122. #define IS_MOVL_PRE(x)                ((x) == 0x0100)
  123. #define IS_MOVL_Rn16_SP(x)        (((x) & 0xfff0) == 0x6fe0)
  124. #define IS_MOVL_EXT(x)                ((x) == 0x78e0)
  125. #define IS_MOVL_Rn24_SP(x)        (((x) & 0xfff0) == 0x6ba0)

  126. #define IS_PUSHFP_MOVESPFP(x)        ((x) == 0x6df60d76)
  127. #define IS_PUSH_FP(x)                ((x) == 0x01006df6)
  128. #define IS_MOV_SP_FP(x)                ((x) == 0x0ff6)
  129. #define IS_SUB2_SP(x)                ((x) == 0x1b87)
  130. #define IS_SUB4_SP(x)                ((x) == 0x1b97)
  131. #define IS_ADD_IMM_SP(x)        ((x) == 0x7a1f)
  132. #define IS_SUB_IMM_SP(x)        ((x) == 0x7a3f)
  133. #define IS_SUBL4_SP(x)                ((x) == 0x1acf)
  134. #define IS_MOV_IMM_Rn(x)        (((x) & 0xfff0) == 0x7905)
  135. #define IS_SUB_RnSP(x)                (((x) & 0xff0f) == 0x1907)
  136. #define IS_ADD_RnSP(x)                (((x) & 0xff0f) == 0x0907)
  137. #define IS_PUSH(x)                (((x) & 0xfff0) == 0x6df0)

  138. /* If the instruction at PC is an argument register spill, return its
  139.    length.  Otherwise, return zero.

  140.    An argument register spill is an instruction that moves an argument
  141.    from the register in which it was passed to the stack slot in which
  142.    it really lives.  It is a byte, word, or longword move from an
  143.    argument register to a negative offset from the frame pointer.

  144.    CV, 2003-06-16: Or, in optimized code or when the `register' qualifier
  145.    is used, it could be a byte, word or long move to registers r3-r5.  */

  146. static int
  147. h8300_is_argument_spill (struct gdbarch *gdbarch, CORE_ADDR pc)
  148. {
  149.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  150.   int w = read_memory_unsigned_integer (pc, 2, byte_order);

  151.   if ((IS_MOVB_RnRm (w) || IS_MOVW_RnRm (w) || IS_MOVL_RnRm (w))
  152.       && (w & 0x70) <= 0x20        /* Rs is R0, R1 or R2 */
  153.       && (w & 0x7) >= 0x3 && (w & 0x7) <= 0x5)        /* Rd is R3, R4 or R5 */
  154.     return 2;

  155.   if (IS_MOVB_Rn16_SP (w)
  156.       && 8 <= (w & 0xf) && (w & 0xf) <= 10)        /* Rs is R0L, R1L, or R2L  */
  157.     {
  158.       /* ... and d:16 is negative.  */
  159.       if (read_memory_integer (pc + 2, 2, byte_order) < 0)
  160.         return 4;
  161.     }
  162.   else if (IS_MOVB_EXT (w))
  163.     {
  164.       if (IS_MOVB_Rn24_SP (read_memory_unsigned_integer (pc + 2,
  165.                                                          2, byte_order)))
  166.         {
  167.           LONGEST disp = read_memory_integer (pc + 4, 4, byte_order);

  168.           /* ... and d:24 is negative.  */
  169.           if (disp < 0 && disp > 0xffffff)
  170.             return 8;
  171.         }
  172.     }
  173.   else if (IS_MOVW_Rn16_SP (w)
  174.            && (w & 0xf) <= 2)        /* Rs is R0, R1, or R2 */
  175.     {
  176.       /* ... and d:16 is negative.  */
  177.       if (read_memory_integer (pc + 2, 2, byte_order) < 0)
  178.         return 4;
  179.     }
  180.   else if (IS_MOVW_EXT (w))
  181.     {
  182.       if (IS_MOVW_Rn24_SP (read_memory_unsigned_integer (pc + 2,
  183.                                                          2, byte_order)))
  184.         {
  185.           LONGEST disp = read_memory_integer (pc + 4, 4, byte_order);

  186.           /* ... and d:24 is negative.  */
  187.           if (disp < 0 && disp > 0xffffff)
  188.             return 8;
  189.         }
  190.     }
  191.   else if (IS_MOVL_PRE (w))
  192.     {
  193.       int w2 = read_memory_integer (pc + 2, 2, byte_order);

  194.       if (IS_MOVL_Rn16_SP (w2)
  195.           && (w2 & 0xf) <= 2)        /* Rs is ER0, ER1, or ER2 */
  196.         {
  197.           /* ... and d:16 is negative.  */
  198.           if (read_memory_integer (pc + 4, 2, byte_order) < 0)
  199.             return 6;
  200.         }
  201.       else if (IS_MOVL_EXT (w2))
  202.         {
  203.           int w3 = read_memory_integer (pc + 4, 2, byte_order);

  204.           if (IS_MOVL_Rn24_SP (read_memory_integer (pc + 4, 2, byte_order)))
  205.             {
  206.               LONGEST disp = read_memory_integer (pc + 6, 4, byte_order);

  207.               /* ... and d:24 is negative.  */
  208.               if (disp < 0 && disp > 0xffffff)
  209.                 return 10;
  210.             }
  211.         }
  212.     }

  213.   return 0;
  214. }

  215. /* Do a full analysis of the prologue at PC and update CACHE
  216.    accordingly.  Bail out early if CURRENT_PC is reached.  Return the
  217.    address where the analysis stopped.

  218.    We handle all cases that can be generated by gcc.

  219.    For allocating a stack frame:

  220.    mov.w r6,@-sp
  221.    mov.w sp,r6
  222.    mov.w #-n,rN
  223.    add.w rN,sp

  224.    mov.w r6,@-sp
  225.    mov.w sp,r6
  226.    subs  #2,sp
  227.    (repeat)

  228.    mov.l er6,@-sp
  229.    mov.l sp,er6
  230.    add.l #-n,sp

  231.    mov.w r6,@-sp
  232.    mov.w sp,r6
  233.    subs  #4,sp
  234.    (repeat)

  235.    For saving registers:

  236.    mov.w rN,@-sp
  237.    mov.l erN,@-sp
  238.    stm.l reglist,@-sp

  239.    */

  240. static CORE_ADDR
  241. h8300_analyze_prologue (struct gdbarch *gdbarch,
  242.                         CORE_ADDR pc, CORE_ADDR current_pc,
  243.                         struct h8300_frame_cache *cache)
  244. {
  245.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  246.   unsigned int op;
  247.   int regno, i, spill_size;

  248.   cache->sp_offset = 0;

  249.   if (pc >= current_pc)
  250.     return current_pc;

  251.   op = read_memory_unsigned_integer (pc, 4, byte_order);

  252.   if (IS_PUSHFP_MOVESPFP (op))
  253.     {
  254.       cache->saved_regs[E_FP_REGNUM] = 0;
  255.       cache->uses_fp = 1;
  256.       pc += 4;
  257.     }
  258.   else if (IS_PUSH_FP (op))
  259.     {
  260.       cache->saved_regs[E_FP_REGNUM] = 0;
  261.       pc += 4;
  262.       if (pc >= current_pc)
  263.         return current_pc;
  264.       op = read_memory_unsigned_integer (pc, 2, byte_order);
  265.       if (IS_MOV_SP_FP (op))
  266.         {
  267.           cache->uses_fp = 1;
  268.           pc += 2;
  269.         }
  270.     }

  271.   while (pc < current_pc)
  272.     {
  273.       op = read_memory_unsigned_integer (pc, 2, byte_order);
  274.       if (IS_SUB2_SP (op))
  275.         {
  276.           cache->sp_offset += 2;
  277.           pc += 2;
  278.         }
  279.       else if (IS_SUB4_SP (op))
  280.         {
  281.           cache->sp_offset += 4;
  282.           pc += 2;
  283.         }
  284.       else if (IS_ADD_IMM_SP (op))
  285.         {
  286.           cache->sp_offset += -read_memory_integer (pc + 2, 2, byte_order);
  287.           pc += 4;
  288.         }
  289.       else if (IS_SUB_IMM_SP (op))
  290.         {
  291.           cache->sp_offset += read_memory_integer (pc + 2, 2, byte_order);
  292.           pc += 4;
  293.         }
  294.       else if (IS_SUBL4_SP (op))
  295.         {
  296.           cache->sp_offset += 4;
  297.           pc += 2;
  298.         }
  299.       else if (IS_MOV_IMM_Rn (op))
  300.         {
  301.           int offset = read_memory_integer (pc + 2, 2, byte_order);
  302.           regno = op & 0x000f;
  303.           op = read_memory_unsigned_integer (pc + 4, 2, byte_order);
  304.           if (IS_ADD_RnSP (op) && (op & 0x00f0) == regno)
  305.             {
  306.               cache->sp_offset -= offset;
  307.               pc += 6;
  308.             }
  309.           else if (IS_SUB_RnSP (op) && (op & 0x00f0) == regno)
  310.             {
  311.               cache->sp_offset += offset;
  312.               pc += 6;
  313.             }
  314.           else
  315.             break;
  316.         }
  317.       else if (IS_PUSH (op))
  318.         {
  319.           regno = op & 0x000f;
  320.           cache->sp_offset += 2;
  321.           cache->saved_regs[regno] = cache->sp_offset;
  322.           pc += 2;
  323.         }
  324.       else if (op == 0x0100)
  325.         {
  326.           op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
  327.           if (IS_PUSH (op))
  328.             {
  329.               regno = op & 0x000f;
  330.               cache->sp_offset += 4;
  331.               cache->saved_regs[regno] = cache->sp_offset;
  332.               pc += 4;
  333.             }
  334.           else
  335.             break;
  336.         }
  337.       else if ((op & 0xffcf) == 0x0100)
  338.         {
  339.           int op1;
  340.           op1 = read_memory_unsigned_integer (pc + 2, 2, byte_order);
  341.           if (IS_PUSH (op1))
  342.             {
  343.               /* Since the prefix is 0x01x0, this is not a simple pushm but a
  344.                  stm.l reglist,@-sp */
  345.               i = ((op & 0x0030) >> 4) + 1;
  346.               regno = op1 & 0x000f;
  347.               for (; i > 0; regno++, --i)
  348.                 {
  349.                   cache->sp_offset += 4;
  350.                   cache->saved_regs[regno] = cache->sp_offset;
  351.                 }
  352.               pc += 4;
  353.             }
  354.           else
  355.             break;
  356.         }
  357.       else
  358.         break;
  359.     }

  360.   /* Check for spilling an argument register to the stack frame.
  361.      This could also be an initializing store from non-prologue code,
  362.      but I don't think there's any harm in skipping that.  */
  363.   while ((spill_size = h8300_is_argument_spill (gdbarch, pc)) > 0
  364.          && pc + spill_size <= current_pc)
  365.     pc += spill_size;

  366.   return pc;
  367. }

  368. static struct h8300_frame_cache *
  369. h8300_frame_cache (struct frame_info *this_frame, void **this_cache)
  370. {
  371.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  372.   struct h8300_frame_cache *cache;
  373.   int i;
  374.   CORE_ADDR current_pc;

  375.   if (*this_cache)
  376.     return *this_cache;

  377.   cache = FRAME_OBSTACK_ZALLOC (struct h8300_frame_cache);
  378.   h8300_init_frame_cache (gdbarch, cache);
  379.   *this_cache = cache;

  380.   /* In principle, for normal frames, %fp holds the frame pointer,
  381.      which holds the base address for the current stack frame.
  382.      However, for functions that don't need it, the frame pointer is
  383.      optional.  For these "frameless" functions the frame pointer is
  384.      actually the frame pointer of the calling frame.  */

  385.   cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
  386.   if (cache->base == 0)
  387.     return cache;

  388.   cache->saved_regs[E_PC_REGNUM] = -BINWORD (gdbarch);

  389.   cache->pc = get_frame_func (this_frame);
  390.   current_pc = get_frame_pc (this_frame);
  391.   if (cache->pc != 0)
  392.     h8300_analyze_prologue (gdbarch, cache->pc, current_pc, cache);

  393.   if (!cache->uses_fp)
  394.     {
  395.       /* We didn't find a valid frame, which means that CACHE->base
  396.          currently holds the frame pointer for our calling frame.  If
  397.          we're at the start of a function, or somewhere half-way its
  398.          prologue, the function's frame probably hasn't been fully
  399.          setup yet.  Try to reconstruct the base address for the stack
  400.          frame by looking at the stack pointer.  For truly "frameless"
  401.          functions this might work too.  */

  402.       cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM)
  403.                     + cache->sp_offset;
  404.       cache->saved_sp = cache->base + BINWORD (gdbarch);
  405.       cache->saved_regs[E_PC_REGNUM] = 0;
  406.     }
  407.   else
  408.     {
  409.       cache->saved_sp = cache->base + 2 * BINWORD (gdbarch);
  410.       cache->saved_regs[E_PC_REGNUM] = -BINWORD (gdbarch);
  411.     }

  412.   /* Adjust all the saved registers such that they contain addresses
  413.      instead of offsets.  */
  414.   for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  415.     if (cache->saved_regs[i] != -1)
  416.       cache->saved_regs[i] = cache->base - cache->saved_regs[i];

  417.   return cache;
  418. }

  419. static void
  420. h8300_frame_this_id (struct frame_info *this_frame, void **this_cache,
  421.                      struct frame_id *this_id)
  422. {
  423.   struct h8300_frame_cache *cache =
  424.     h8300_frame_cache (this_frame, this_cache);

  425.   /* This marks the outermost frame.  */
  426.   if (cache->base == 0)
  427.     return;

  428.   *this_id = frame_id_build (cache->saved_sp, cache->pc);
  429. }

  430. static struct value *
  431. h8300_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  432.                            int regnum)
  433. {
  434.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  435.   struct h8300_frame_cache *cache =
  436.     h8300_frame_cache (this_frame, this_cache);

  437.   gdb_assert (regnum >= 0);

  438.   if (regnum == E_SP_REGNUM && cache->saved_sp)
  439.     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);

  440.   if (regnum < gdbarch_num_regs (gdbarch)
  441.       && cache->saved_regs[regnum] != -1)
  442.     return frame_unwind_got_memory (this_frame, regnum,
  443.                                     cache->saved_regs[regnum]);

  444.   return frame_unwind_got_register (this_frame, regnum, regnum);
  445. }

  446. static const struct frame_unwind h8300_frame_unwind = {
  447.   NORMAL_FRAME,
  448.   default_frame_unwind_stop_reason,
  449.   h8300_frame_this_id,
  450.   h8300_frame_prev_register,
  451.   NULL,
  452.   default_frame_sniffer
  453. };

  454. static CORE_ADDR
  455. h8300_frame_base_address (struct frame_info *this_frame, void **this_cache)
  456. {
  457.   struct h8300_frame_cache *cache = h8300_frame_cache (this_frame, this_cache);
  458.   return cache->base;
  459. }

  460. static const struct frame_base h8300_frame_base = {
  461.   &h8300_frame_unwind,
  462.   h8300_frame_base_address,
  463.   h8300_frame_base_address,
  464.   h8300_frame_base_address
  465. };

  466. static CORE_ADDR
  467. h8300_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  468. {
  469.   CORE_ADDR func_addr = 0 , func_end = 0;

  470.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  471.     {
  472.       struct symtab_and_line sal;
  473.       struct h8300_frame_cache cache;

  474.       /* Found a function.  */
  475.       sal = find_pc_line (func_addr, 0);
  476.       if (sal.end && sal.end < func_end)
  477.         /* Found a line number, use it as end of prologue.  */
  478.         return sal.end;

  479.       /* No useable line symbol.  Use prologue parsing method.  */
  480.       h8300_init_frame_cache (gdbarch, &cache);
  481.       return h8300_analyze_prologue (gdbarch, func_addr, func_end, &cache);
  482.     }

  483.   /* No function symbol -- just return the PC.  */
  484.   return (CORE_ADDR) pc;
  485. }

  486. /* Function: push_dummy_call
  487.    Setup the function arguments for calling a function in the inferior.
  488.    In this discussion, a `word' is 16 bits on the H8/300s, and 32 bits
  489.    on the H8/300H.

  490.    There are actually two ABI's here: -mquickcall (the default) and
  491.    -mno-quickcall.  With -mno-quickcall, all arguments are passed on
  492.    the stack after the return address, word-aligned.  With
  493.    -mquickcall, GCC tries to use r0 -- r2 to pass registers.  Since
  494.    GCC doesn't indicate in the object file which ABI was used to
  495.    compile it, GDB only supports the default --- -mquickcall.

  496.    Here are the rules for -mquickcall, in detail:

  497.    Each argument, whether scalar or aggregate, is padded to occupy a
  498.    whole number of words.  Arguments smaller than a word are padded at
  499.    the most significant end; those larger than a word are padded at
  500.    the least significant end.

  501.    The initial arguments are passed in r0 -- r2.  Earlier arguments go in
  502.    lower-numbered registers.  Multi-word arguments are passed in
  503.    consecutive registers, with the most significant end in the
  504.    lower-numbered register.

  505.    If an argument doesn't fit entirely in the remaining registers, it
  506.    is passed entirely on the stack.  Stack arguments begin just after
  507.    the return address.  Once an argument has overflowed onto the stack
  508.    this way, all subsequent arguments are passed on the stack.

  509.    The above rule has odd consequences.  For example, on the h8/300s,
  510.    if a function takes two longs and an int as arguments:
  511.    - the first long will be passed in r0/r1,
  512.    - the second long will be passed entirely on the stack, since it
  513.      doesn't fit in r2,
  514.    - and the int will be passed on the stack, even though it could fit
  515.      in r2.

  516.    A weird exception: if an argument is larger than a word, but not a
  517.    whole number of words in length (before padding), it is passed on
  518.    the stack following the rules for stack arguments above, even if
  519.    there are sufficient registers available to hold it.  Stranger
  520.    still, the argument registers are still `used up' --- even though
  521.    there's nothing in them.

  522.    So, for example, on the h8/300s, if a function expects a three-byte
  523.    structure and an int, the structure will go on the stack, and the
  524.    int will go in r2, not r0.

  525.    If the function returns an aggregate type (struct, union, or class)
  526.    by value, the caller must allocate space to hold the return value,
  527.    and pass the callee a pointer to this space as an invisible first
  528.    argument, in R0.

  529.    For varargs functions, the last fixed argument and all the variable
  530.    arguments are always passed on the stack.  This means that calls to
  531.    varargs functions don't work properly unless there is a prototype
  532.    in scope.

  533.    Basically, this ABI is not good, for the following reasons:
  534.    - You can't call vararg functions properly unless a prototype is in scope.
  535.    - Structure passing is inconsistent, to no purpose I can see.
  536.    - It often wastes argument registers, of which there are only three
  537.      to begin with.  */

  538. static CORE_ADDR
  539. h8300_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  540.                        struct regcache *regcache, CORE_ADDR bp_addr,
  541.                        int nargs, struct value **args, CORE_ADDR sp,
  542.                        int struct_return, CORE_ADDR struct_addr)
  543. {
  544.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  545.   int stack_alloc = 0, stack_offset = 0;
  546.   int wordsize = BINWORD (gdbarch);
  547.   int reg = E_ARG0_REGNUM;
  548.   int argument;

  549.   /* First, make sure the stack is properly aligned.  */
  550.   sp = align_down (sp, wordsize);

  551.   /* Now make sure there's space on the stack for the arguments.  We
  552.      may over-allocate a little here, but that won't hurt anything.  */
  553.   for (argument = 0; argument < nargs; argument++)
  554.     stack_alloc += align_up (TYPE_LENGTH (value_type (args[argument])),
  555.                              wordsize);
  556.   sp -= stack_alloc;

  557.   /* Now load as many arguments as possible into registers, and push
  558.      the rest onto the stack.
  559.      If we're returning a structure by value, then we must pass a
  560.      pointer to the buffer for the return value as an invisible first
  561.      argument.  */
  562.   if (struct_return)
  563.     regcache_cooked_write_unsigned (regcache, reg++, struct_addr);

  564.   for (argument = 0; argument < nargs; argument++)
  565.     {
  566.       struct cleanup *back_to;
  567.       struct type *type = value_type (args[argument]);
  568.       int len = TYPE_LENGTH (type);
  569.       char *contents = (char *) value_contents (args[argument]);

  570.       /* Pad the argument appropriately.  */
  571.       int padded_len = align_up (len, wordsize);
  572.       gdb_byte *padded = xmalloc (padded_len);
  573.       back_to = make_cleanup (xfree, padded);

  574.       memset (padded, 0, padded_len);
  575.       memcpy (len < wordsize ? padded + padded_len - len : padded,
  576.               contents, len);

  577.       /* Could the argument fit in the remaining registers?  */
  578.       if (padded_len <= (E_ARGLAST_REGNUM - reg + 1) * wordsize)
  579.         {
  580.           /* Are we going to pass it on the stack anyway, for no good
  581.              reason?  */
  582.           if (len > wordsize && len % wordsize)
  583.             {
  584.               /* I feel so unclean.  */
  585.               write_memory (sp + stack_offset, padded, padded_len);
  586.               stack_offset += padded_len;

  587.               /* That's right --- even though we passed the argument
  588.                  on the stack, we consume the registers anyway!  Love
  589.                  me, love my dog.  */
  590.               reg += padded_len / wordsize;
  591.             }
  592.           else
  593.             {
  594.               /* Heavens to Betsy --- it's really going in registers!
  595.                  Note that on the h8/300s, there are gaps between the
  596.                  registers in the register file.  */
  597.               int offset;

  598.               for (offset = 0; offset < padded_len; offset += wordsize)
  599.                 {
  600.                   ULONGEST word
  601.                     = extract_unsigned_integer (padded + offset,
  602.                                                 wordsize, byte_order);
  603.                   regcache_cooked_write_unsigned (regcache, reg++, word);
  604.                 }
  605.             }
  606.         }
  607.       else
  608.         {
  609.           /* It doesn't fit in registers!  Onto the stack it goes.  */
  610.           write_memory (sp + stack_offset, padded, padded_len);
  611.           stack_offset += padded_len;

  612.           /* Once one argument has spilled onto the stack, all
  613.              subsequent arguments go on the stack.  */
  614.           reg = E_ARGLAST_REGNUM + 1;
  615.         }

  616.       do_cleanups (back_to);
  617.     }

  618.   /* Store return address.  */
  619.   sp -= wordsize;
  620.   write_memory_unsigned_integer (sp, wordsize, byte_order, bp_addr);

  621.   /* Update stack pointer.  */
  622.   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);

  623.   /* Return the new stack pointer minus the return address slot since
  624.      that's what DWARF2/GCC uses as the frame's CFA.  */
  625.   return sp + wordsize;
  626. }

  627. /* Function: extract_return_value
  628.    Figure out where in REGBUF the called function has left its return value.
  629.    Copy that into VALBUF.  Be sure to account for CPU type.   */

  630. static void
  631. h8300_extract_return_value (struct type *type, struct regcache *regcache,
  632.                             void *valbuf)
  633. {
  634.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  635.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  636.   int len = TYPE_LENGTH (type);
  637.   ULONGEST c, addr;

  638.   switch (len)
  639.     {
  640.     case 1:
  641.     case 2:
  642.       regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
  643.       store_unsigned_integer (valbuf, len, byte_order, c);
  644.       break;
  645.     case 4:                        /* Needs two registers on plain H8/300 */
  646.       regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
  647.       store_unsigned_integer (valbuf, 2, byte_order, c);
  648.       regcache_cooked_read_unsigned (regcache, E_RET1_REGNUM, &c);
  649.       store_unsigned_integer ((void *)((char *) valbuf + 2), 2, byte_order, c);
  650.       break;
  651.     case 8:                        /* long long is now 8 bytes.  */
  652.       if (TYPE_CODE (type) == TYPE_CODE_INT)
  653.         {
  654.           regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &addr);
  655.           c = read_memory_unsigned_integer ((CORE_ADDR) addr, len, byte_order);
  656.           store_unsigned_integer (valbuf, len, byte_order, c);
  657.         }
  658.       else
  659.         {
  660.           error (_("I don't know how this 8 byte value is returned."));
  661.         }
  662.       break;
  663.     }
  664. }

  665. static void
  666. h8300h_extract_return_value (struct type *type, struct regcache *regcache,
  667.                              void *valbuf)
  668. {
  669.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  670.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  671.   ULONGEST c;

  672.   switch (TYPE_LENGTH (type))
  673.     {
  674.     case 1:
  675.     case 2:
  676.     case 4:
  677.       regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
  678.       store_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order, c);
  679.       break;
  680.     case 8:                        /* long long is now 8 bytes.  */
  681.       if (TYPE_CODE (type) == TYPE_CODE_INT)
  682.         {
  683.           regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
  684.           store_unsigned_integer (valbuf, 4, byte_order, c);
  685.           regcache_cooked_read_unsigned (regcache, E_RET1_REGNUM, &c);
  686.           store_unsigned_integer ((void *) ((char *) valbuf + 4), 4,
  687.                                   byte_order, c);
  688.         }
  689.       else
  690.         {
  691.           error (_("I don't know how this 8 byte value is returned."));
  692.         }
  693.       break;
  694.     }
  695. }

  696. static int
  697. h8300_use_struct_convention (struct type *value_type)
  698. {
  699.   /* Types of 1, 2 or 4 bytes are returned in R0/R1, everything else on the
  700.      stack.  */

  701.   if (TYPE_CODE (value_type) == TYPE_CODE_STRUCT
  702.       || TYPE_CODE (value_type) == TYPE_CODE_UNION)
  703.     return 1;
  704.   return !(TYPE_LENGTH (value_type) == 1
  705.            || TYPE_LENGTH (value_type) == 2
  706.            || TYPE_LENGTH (value_type) == 4);
  707. }

  708. static int
  709. h8300h_use_struct_convention (struct type *value_type)
  710. {
  711.   /* Types of 1, 2 or 4 bytes are returned in R0, INT types of 8 bytes are
  712.      returned in R0/R1, everything else on the stack.  */
  713.   if (TYPE_CODE (value_type) == TYPE_CODE_STRUCT
  714.       || TYPE_CODE (value_type) == TYPE_CODE_UNION)
  715.     return 1;
  716.   return !(TYPE_LENGTH (value_type) == 1
  717.            || TYPE_LENGTH (value_type) == 2
  718.            || TYPE_LENGTH (value_type) == 4
  719.            || (TYPE_LENGTH (value_type) == 8
  720.                && TYPE_CODE (value_type) == TYPE_CODE_INT));
  721. }

  722. /* Function: store_return_value
  723.    Place the appropriate value in the appropriate registers.
  724.    Primarily used by the RETURN command.  */

  725. static void
  726. h8300_store_return_value (struct type *type, struct regcache *regcache,
  727.                           const void *valbuf)
  728. {
  729.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  730.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  731.   ULONGEST val;

  732.   switch (TYPE_LENGTH (type))
  733.     {
  734.     case 1:
  735.     case 2:                        /* short...  */
  736.       val = extract_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order);
  737.       regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM, val);
  738.       break;
  739.     case 4:                        /* long, float */
  740.       val = extract_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order);
  741.       regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM,
  742.                                       (val >> 16) & 0xffff);
  743.       regcache_cooked_write_unsigned (regcache, E_RET1_REGNUM, val & 0xffff);
  744.       break;
  745.     case 8:                        /* long long, double and long double
  746.                                    are all defined as 4 byte types so
  747.                                    far so this shouldn't happen.  */
  748.       error (_("I don't know how to return an 8 byte value."));
  749.       break;
  750.     }
  751. }

  752. static void
  753. h8300h_store_return_value (struct type *type, struct regcache *regcache,
  754.                            const void *valbuf)
  755. {
  756.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  757.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  758.   ULONGEST val;

  759.   switch (TYPE_LENGTH (type))
  760.     {
  761.     case 1:
  762.     case 2:
  763.     case 4:                        /* long, float */
  764.       val = extract_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order);
  765.       regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM, val);
  766.       break;
  767.     case 8:
  768.       val = extract_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order);
  769.       regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM,
  770.                                       (val >> 32) & 0xffffffff);
  771.       regcache_cooked_write_unsigned (regcache, E_RET1_REGNUM,
  772.                                       val & 0xffffffff);
  773.       break;
  774.     }
  775. }

  776. static enum return_value_convention
  777. h8300_return_value (struct gdbarch *gdbarch, struct value *function,
  778.                     struct type *type, struct regcache *regcache,
  779.                     gdb_byte *readbuf, const gdb_byte *writebuf)
  780. {
  781.   if (h8300_use_struct_convention (type))
  782.     return RETURN_VALUE_STRUCT_CONVENTION;
  783.   if (writebuf)
  784.     h8300_store_return_value (type, regcache, writebuf);
  785.   else if (readbuf)
  786.     h8300_extract_return_value (type, regcache, readbuf);
  787.   return RETURN_VALUE_REGISTER_CONVENTION;
  788. }

  789. static enum return_value_convention
  790. h8300h_return_value (struct gdbarch *gdbarch, struct value *function,
  791.                      struct type *type, struct regcache *regcache,
  792.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  793. {
  794.   if (h8300h_use_struct_convention (type))
  795.     {
  796.       if (readbuf)
  797.         {
  798.           ULONGEST addr;

  799.           regcache_raw_read_unsigned (regcache, E_R0_REGNUM, &addr);
  800.           read_memory (addr, readbuf, TYPE_LENGTH (type));
  801.         }

  802.       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  803.     }
  804.   if (writebuf)
  805.     h8300h_store_return_value (type, regcache, writebuf);
  806.   else if (readbuf)
  807.     h8300h_extract_return_value (type, regcache, readbuf);
  808.   return RETURN_VALUE_REGISTER_CONVENTION;
  809. }

  810. /* Implementation of 'register_sim_regno' gdbarch method.  */

  811. static int
  812. h8300_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  813. {
  814.   /* Only makes sense to supply raw registers.  */
  815.   gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));

  816.   /* We hide the raw ccr from the user by making it nameless.  Because
  817.      the default register_sim_regno hook returns
  818.      LEGACY_SIM_REGNO_IGNORE for unnamed registers, we need to
  819.      override it.  The sim register numbering is compatible with
  820.      gdb's.  */
  821.   return regnum;
  822. }

  823. static const char *
  824. h8300_register_name (struct gdbarch *gdbarch, int regno)
  825. {
  826.   /* The register names change depending on which h8300 processor
  827.      type is selected.  */
  828.   static char *register_names[] = {
  829.     "r0", "r1", "r2", "r3", "r4", "r5", "r6",
  830.     "sp", "", "pc", "cycles", "tick", "inst",
  831.     "ccr",                        /* pseudo register */
  832.   };
  833.   if (regno < 0
  834.       || regno >= (sizeof (register_names) / sizeof (*register_names)))
  835.     internal_error (__FILE__, __LINE__,
  836.                     _("h8300_register_name: illegal register number %d"),
  837.                     regno);
  838.   else
  839.     return register_names[regno];
  840. }

  841. static const char *
  842. h8300s_register_name (struct gdbarch *gdbarch, int regno)
  843. {
  844.   static char *register_names[] = {
  845.     "er0", "er1", "er2", "er3", "er4", "er5", "er6",
  846.     "sp", "", "pc", "cycles", "", "tick", "inst",
  847.     "mach", "macl",
  848.     "ccr", "exr"                /* pseudo registers */
  849.   };
  850.   if (regno < 0
  851.       || regno >= (sizeof (register_names) / sizeof (*register_names)))
  852.     internal_error (__FILE__, __LINE__,
  853.                     _("h8300s_register_name: illegal register number %d"),
  854.                     regno);
  855.   else
  856.     return register_names[regno];
  857. }

  858. static const char *
  859. h8300sx_register_name (struct gdbarch *gdbarch, int regno)
  860. {
  861.   static char *register_names[] = {
  862.     "er0", "er1", "er2", "er3", "er4", "er5", "er6",
  863.     "sp", "", "pc", "cycles", "", "tick", "inst",
  864.     "mach", "macl", "sbr", "vbr",
  865.     "ccr", "exr"                /* pseudo registers */
  866.   };
  867.   if (regno < 0
  868.       || regno >= (sizeof (register_names) / sizeof (*register_names)))
  869.     internal_error (__FILE__, __LINE__,
  870.                     _("h8300sx_register_name: illegal register number %d"),
  871.                     regno);
  872.   else
  873.     return register_names[regno];
  874. }

  875. static void
  876. h8300_print_register (struct gdbarch *gdbarch, struct ui_file *file,
  877.                       struct frame_info *frame, int regno)
  878. {
  879.   LONGEST rval;
  880.   const char *name = gdbarch_register_name (gdbarch, regno);

  881.   if (!name || !*name)
  882.     return;

  883.   rval = get_frame_register_signed (frame, regno);

  884.   fprintf_filtered (file, "%-14s ", name);
  885.   if ((regno == E_PSEUDO_CCR_REGNUM (gdbarch)) || \
  886.       (regno == E_PSEUDO_EXR_REGNUM (gdbarch) && is_h8300smode (gdbarch)))
  887.     {
  888.       fprintf_filtered (file, "0x%02x        ", (unsigned char) rval);
  889.       print_longest (file, 'u', 1, rval);
  890.     }
  891.   else
  892.     {
  893.       fprintf_filtered (file, "0x%s  ", phex ((ULONGEST) rval,
  894.                         BINWORD (gdbarch)));
  895.       print_longest (file, 'd', 1, rval);
  896.     }
  897.   if (regno == E_PSEUDO_CCR_REGNUM (gdbarch))
  898.     {
  899.       /* CCR register */
  900.       int C, Z, N, V;
  901.       unsigned char l = rval & 0xff;
  902.       fprintf_filtered (file, "\t");
  903.       fprintf_filtered (file, "I-%d ", (l & 0x80) != 0);
  904.       fprintf_filtered (file, "UI-%d ", (l & 0x40) != 0);
  905.       fprintf_filtered (file, "H-%d ", (l & 0x20) != 0);
  906.       fprintf_filtered (file, "U-%d ", (l & 0x10) != 0);
  907.       N = (l & 0x8) != 0;
  908.       Z = (l & 0x4) != 0;
  909.       V = (l & 0x2) != 0;
  910.       C = (l & 0x1) != 0;
  911.       fprintf_filtered (file, "N-%d ", N);
  912.       fprintf_filtered (file, "Z-%d ", Z);
  913.       fprintf_filtered (file, "V-%d ", V);
  914.       fprintf_filtered (file, "C-%d ", C);
  915.       if ((C | Z) == 0)
  916.         fprintf_filtered (file, "u> ");
  917.       if ((C | Z) == 1)
  918.         fprintf_filtered (file, "u<= ");
  919.       if ((C == 0))
  920.         fprintf_filtered (file, "u>= ");
  921.       if (C == 1)
  922.         fprintf_filtered (file, "u< ");
  923.       if (Z == 0)
  924.         fprintf_filtered (file, "!= ");
  925.       if (Z == 1)
  926.         fprintf_filtered (file, "== ");
  927.       if ((N ^ V) == 0)
  928.         fprintf_filtered (file, ">= ");
  929.       if ((N ^ V) == 1)
  930.         fprintf_filtered (file, "< ");
  931.       if ((Z | (N ^ V)) == 0)
  932.         fprintf_filtered (file, "> ");
  933.       if ((Z | (N ^ V)) == 1)
  934.         fprintf_filtered (file, "<= ");
  935.     }
  936.   else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch) && is_h8300smode (gdbarch))
  937.     {
  938.       /* EXR register */
  939.       unsigned char l = rval & 0xff;
  940.       fprintf_filtered (file, "\t");
  941.       fprintf_filtered (file, "T-%d - - - ", (l & 0x80) != 0);
  942.       fprintf_filtered (file, "I2-%d ", (l & 4) != 0);
  943.       fprintf_filtered (file, "I1-%d ", (l & 2) != 0);
  944.       fprintf_filtered (file, "I0-%d", (l & 1) != 0);
  945.     }
  946.   fprintf_filtered (file, "\n");
  947. }

  948. static void
  949. h8300_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
  950.                             struct frame_info *frame, int regno, int cpregs)
  951. {
  952.   if (regno < 0)
  953.     {
  954.       for (regno = E_R0_REGNUM; regno <= E_SP_REGNUM; ++regno)
  955.         h8300_print_register (gdbarch, file, frame, regno);
  956.       h8300_print_register (gdbarch, file, frame,
  957.                             E_PSEUDO_CCR_REGNUM (gdbarch));
  958.       h8300_print_register (gdbarch, file, frame, E_PC_REGNUM);
  959.       if (is_h8300smode (gdbarch))
  960.         {
  961.           h8300_print_register (gdbarch, file, frame,
  962.                                 E_PSEUDO_EXR_REGNUM (gdbarch));
  963.           if (is_h8300sxmode (gdbarch))
  964.             {
  965.               h8300_print_register (gdbarch, file, frame, E_SBR_REGNUM);
  966.               h8300_print_register (gdbarch, file, frame, E_VBR_REGNUM);
  967.             }
  968.           h8300_print_register (gdbarch, file, frame, E_MACH_REGNUM);
  969.           h8300_print_register (gdbarch, file, frame, E_MACL_REGNUM);
  970.           h8300_print_register (gdbarch, file, frame, E_CYCLES_REGNUM);
  971.           h8300_print_register (gdbarch, file, frame, E_TICKS_REGNUM);
  972.           h8300_print_register (gdbarch, file, frame, E_INSTS_REGNUM);
  973.         }
  974.       else
  975.         {
  976.           h8300_print_register (gdbarch, file, frame, E_CYCLES_REGNUM);
  977.           h8300_print_register (gdbarch, file, frame, E_TICK_REGNUM);
  978.           h8300_print_register (gdbarch, file, frame, E_INST_REGNUM);
  979.         }
  980.     }
  981.   else
  982.     {
  983.       if (regno == E_CCR_REGNUM)
  984.         h8300_print_register (gdbarch, file, frame,
  985.                               E_PSEUDO_CCR_REGNUM (gdbarch));
  986.       else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch)
  987.                && is_h8300smode (gdbarch))
  988.         h8300_print_register (gdbarch, file, frame,
  989.                               E_PSEUDO_EXR_REGNUM (gdbarch));
  990.       else
  991.         h8300_print_register (gdbarch, file, frame, regno);
  992.     }
  993. }

  994. static struct type *
  995. h8300_register_type (struct gdbarch *gdbarch, int regno)
  996. {
  997.   if (regno < 0 || regno >= gdbarch_num_regs (gdbarch)
  998.                             + gdbarch_num_pseudo_regs (gdbarch))
  999.     internal_error (__FILE__, __LINE__,
  1000.                     _("h8300_register_type: illegal register number %d"),
  1001.                     regno);
  1002.   else
  1003.     {
  1004.       switch (regno)
  1005.         {
  1006.         case E_PC_REGNUM:
  1007.           return builtin_type (gdbarch)->builtin_func_ptr;
  1008.         case E_SP_REGNUM:
  1009.         case E_FP_REGNUM:
  1010.           return builtin_type (gdbarch)->builtin_data_ptr;
  1011.         default:
  1012.           if (regno == E_PSEUDO_CCR_REGNUM (gdbarch))
  1013.             return builtin_type (gdbarch)->builtin_uint8;
  1014.           else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch))
  1015.             return builtin_type (gdbarch)->builtin_uint8;
  1016.           else if (is_h8300hmode (gdbarch))
  1017.             return builtin_type (gdbarch)->builtin_int32;
  1018.           else
  1019.             return builtin_type (gdbarch)->builtin_int16;
  1020.         }
  1021.     }
  1022. }

  1023. /* Helpers for h8300_pseudo_register_read.  We expose ccr/exr as
  1024.    pseudo-registers to users with smaller sizes than the corresponding
  1025.    raw registers.  These helpers extend/narrow the values.  */

  1026. static enum register_status
  1027. pseudo_from_raw_register (struct gdbarch *gdbarch, struct regcache *regcache,
  1028.                           gdb_byte *buf, int pseudo_regno, int raw_regno)
  1029. {
  1030.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1031.   enum register_status status;
  1032.   ULONGEST val;

  1033.   status = regcache_raw_read_unsigned (regcache, raw_regno, &val);
  1034.   if (status == REG_VALID)
  1035.     store_unsigned_integer (buf,
  1036.                             register_size (gdbarch, pseudo_regno),
  1037.                             byte_order, val);
  1038.   return status;
  1039. }

  1040. /* See pseudo_from_raw_register.  */

  1041. static void
  1042. raw_from_pseudo_register (struct gdbarch *gdbarch, struct regcache *regcache,
  1043.                           const gdb_byte *buf, int raw_regno, int pseudo_regno)
  1044. {
  1045.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1046.   ULONGEST val;

  1047.   val = extract_unsigned_integer (buf, register_size (gdbarch, pseudo_regno),
  1048.                                   byte_order);
  1049.   regcache_raw_write_unsigned (regcache, raw_regno, val);
  1050. }

  1051. static enum register_status
  1052. h8300_pseudo_register_read (struct gdbarch *gdbarch,
  1053.                             struct regcache *regcache, int regno,
  1054.                             gdb_byte *buf)
  1055. {
  1056.   if (regno == E_PSEUDO_CCR_REGNUM (gdbarch))
  1057.     {
  1058.       return pseudo_from_raw_register (gdbarch, regcache, buf,
  1059.                                        regno, E_CCR_REGNUM);
  1060.     }
  1061.   else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch))
  1062.     {
  1063.       return pseudo_from_raw_register (gdbarch, regcache, buf,
  1064.                                        regno, E_EXR_REGNUM);
  1065.     }
  1066.   else
  1067.     return regcache_raw_read (regcache, regno, buf);
  1068. }

  1069. static void
  1070. h8300_pseudo_register_write (struct gdbarch *gdbarch,
  1071.                              struct regcache *regcache, int regno,
  1072.                              const gdb_byte *buf)
  1073. {
  1074.   if (regno == E_PSEUDO_CCR_REGNUM (gdbarch))
  1075.     raw_from_pseudo_register (gdbarch, regcache, buf, E_CCR_REGNUM, regno);
  1076.   else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch))
  1077.     raw_from_pseudo_register (gdbarch, regcache, buf, E_EXR_REGNUM, regno);
  1078.   else
  1079.     regcache_raw_write (regcache, regno, buf);
  1080. }

  1081. static int
  1082. h8300_dbg_reg_to_regnum (struct gdbarch *gdbarch, int regno)
  1083. {
  1084.   if (regno == E_CCR_REGNUM)
  1085.     return E_PSEUDO_CCR_REGNUM (gdbarch);
  1086.   return regno;
  1087. }

  1088. static int
  1089. h8300s_dbg_reg_to_regnum (struct gdbarch *gdbarch, int regno)
  1090. {
  1091.   if (regno == E_CCR_REGNUM)
  1092.     return E_PSEUDO_CCR_REGNUM (gdbarch);
  1093.   if (regno == E_EXR_REGNUM)
  1094.     return E_PSEUDO_EXR_REGNUM (gdbarch);
  1095.   return regno;
  1096. }

  1097. static const unsigned char *
  1098. h8300_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  1099.                           int *lenptr)
  1100. {
  1101.   /*static unsigned char breakpoint[] = { 0x7A, 0xFF }; *//* ??? */
  1102.   static unsigned char breakpoint[] = { 0x01, 0x80 };        /* Sleep */

  1103.   *lenptr = sizeof (breakpoint);
  1104.   return breakpoint;
  1105. }

  1106. static struct gdbarch *
  1107. h8300_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1108. {
  1109.   struct gdbarch_tdep *tdep = NULL;
  1110.   struct gdbarch *gdbarch;

  1111.   arches = gdbarch_list_lookup_by_info (arches, &info);
  1112.   if (arches != NULL)
  1113.     return arches->gdbarch;

  1114. #if 0
  1115.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  1116. #endif

  1117.   if (info.bfd_arch_info->arch != bfd_arch_h8300)
  1118.     return NULL;

  1119.   gdbarch = gdbarch_alloc (&info, 0);

  1120.   set_gdbarch_register_sim_regno (gdbarch, h8300_register_sim_regno);

  1121.   switch (info.bfd_arch_info->mach)
  1122.     {
  1123.     case bfd_mach_h8300:
  1124.       set_gdbarch_num_regs (gdbarch, 13);
  1125.       set_gdbarch_num_pseudo_regs (gdbarch, 1);
  1126.       set_gdbarch_ecoff_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1127.       set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1128.       set_gdbarch_stab_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1129.       set_gdbarch_register_name (gdbarch, h8300_register_name);
  1130.       set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1131.       set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1132.       set_gdbarch_return_value (gdbarch, h8300_return_value);
  1133.       set_gdbarch_print_insn (gdbarch, print_insn_h8300);
  1134.       break;
  1135.     case bfd_mach_h8300h:
  1136.     case bfd_mach_h8300hn:
  1137.       set_gdbarch_num_regs (gdbarch, 13);
  1138.       set_gdbarch_num_pseudo_regs (gdbarch, 1);
  1139.       set_gdbarch_ecoff_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1140.       set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1141.       set_gdbarch_stab_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1142.       set_gdbarch_register_name (gdbarch, h8300_register_name);
  1143.       if (info.bfd_arch_info->mach != bfd_mach_h8300hn)
  1144.         {
  1145.           set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1146.           set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1147.         }
  1148.       else
  1149.         {
  1150.           set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1151.           set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1152.         }
  1153.       set_gdbarch_return_value (gdbarch, h8300h_return_value);
  1154.       set_gdbarch_print_insn (gdbarch, print_insn_h8300h);
  1155.       break;
  1156.     case bfd_mach_h8300s:
  1157.     case bfd_mach_h8300sn:
  1158.       set_gdbarch_num_regs (gdbarch, 16);
  1159.       set_gdbarch_num_pseudo_regs (gdbarch, 2);
  1160.       set_gdbarch_ecoff_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1161.       set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1162.       set_gdbarch_stab_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1163.       set_gdbarch_register_name (gdbarch, h8300s_register_name);
  1164.       if (info.bfd_arch_info->mach != bfd_mach_h8300sn)
  1165.         {
  1166.           set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1167.           set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1168.         }
  1169.       else
  1170.         {
  1171.           set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1172.           set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1173.         }
  1174.       set_gdbarch_return_value (gdbarch, h8300h_return_value);
  1175.       set_gdbarch_print_insn (gdbarch, print_insn_h8300s);
  1176.       break;
  1177.     case bfd_mach_h8300sx:
  1178.     case bfd_mach_h8300sxn:
  1179.       set_gdbarch_num_regs (gdbarch, 18);
  1180.       set_gdbarch_num_pseudo_regs (gdbarch, 2);
  1181.       set_gdbarch_ecoff_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1182.       set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1183.       set_gdbarch_stab_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1184.       set_gdbarch_register_name (gdbarch, h8300sx_register_name);
  1185.       if (info.bfd_arch_info->mach != bfd_mach_h8300sxn)
  1186.         {
  1187.           set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1188.           set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1189.         }
  1190.       else
  1191.         {
  1192.           set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1193.           set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1194.         }
  1195.       set_gdbarch_return_value (gdbarch, h8300h_return_value);
  1196.       set_gdbarch_print_insn (gdbarch, print_insn_h8300s);
  1197.       break;
  1198.     }

  1199.   set_gdbarch_pseudo_register_read (gdbarch, h8300_pseudo_register_read);
  1200.   set_gdbarch_pseudo_register_write (gdbarch, h8300_pseudo_register_write);

  1201.   /*
  1202.    * Basic register fields and methods.
  1203.    */

  1204.   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
  1205.   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
  1206.   set_gdbarch_register_type (gdbarch, h8300_register_type);
  1207.   set_gdbarch_print_registers_info (gdbarch, h8300_print_registers_info);

  1208.   /*
  1209.    * Frame Info
  1210.    */
  1211.   set_gdbarch_skip_prologue (gdbarch, h8300_skip_prologue);

  1212.   /* Frame unwinder.  */
  1213.   set_gdbarch_unwind_pc (gdbarch, h8300_unwind_pc);
  1214.   set_gdbarch_unwind_sp (gdbarch, h8300_unwind_sp);
  1215.   set_gdbarch_dummy_id (gdbarch, h8300_dummy_id);
  1216.   frame_base_set_default (gdbarch, &h8300_frame_base);

  1217.   /*
  1218.    * Miscelany
  1219.    */
  1220.   /* Stack grows up.  */
  1221.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  1222.   set_gdbarch_breakpoint_from_pc (gdbarch, h8300_breakpoint_from_pc);
  1223.   set_gdbarch_push_dummy_call (gdbarch, h8300_push_dummy_call);

  1224.   set_gdbarch_char_signed (gdbarch, 0);
  1225.   set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1226.   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1227.   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  1228.   set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1229.   set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  1230.   set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1231.   set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);

  1232.   set_gdbarch_believe_pcc_promotion (gdbarch, 1);

  1233.   /* Hook in the DWARF CFI frame unwinder.  */
  1234.   dwarf2_append_unwinders (gdbarch);
  1235.   frame_unwind_append_unwinder (gdbarch, &h8300_frame_unwind);

  1236.   return gdbarch;

  1237. }

  1238. extern initialize_file_ftype _initialize_h8300_tdep; /* -Wmissing-prototypes */

  1239. void
  1240. _initialize_h8300_tdep (void)
  1241. {
  1242.   register_gdbarch_init (bfd_arch_h8300, h8300_gdbarch_init);
  1243. }

  1244. static int
  1245. is_h8300hmode (struct gdbarch *gdbarch)
  1246. {
  1247.   return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
  1248.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
  1249.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300s
  1250.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn
  1251.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300h
  1252.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300hn;
  1253. }

  1254. static int
  1255. is_h8300smode (struct gdbarch *gdbarch)
  1256. {
  1257.   return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
  1258.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
  1259.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300s
  1260.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn;
  1261. }

  1262. static int
  1263. is_h8300sxmode (struct gdbarch *gdbarch)
  1264. {
  1265.   return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
  1266.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn;
  1267. }

  1268. static int
  1269. is_h8300_normal_mode (struct gdbarch *gdbarch)
  1270. {
  1271.   return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
  1272.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn
  1273.     || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300hn;
  1274. }