gdb/iq2000-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the IQ2000 architecture, for GDB, the GNU
  2.    Debugger.

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

  4.    Contributed by Red Hat.

  5.    This file is part of GDB.

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

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

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

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

  28. enum gdb_regnum
  29. {
  30.   E_R0_REGNUM,  E_R1_REGNUM,  E_R2_REGNUM,  E_R3_REGNUM,
  31.   E_R4_REGNUM,  E_R5_REGNUM,  E_R6_REGNUM,  E_R7_REGNUM,
  32.   E_R8_REGNUM,  E_R9_REGNUM,  E_R10_REGNUM, E_R11_REGNUM,
  33.   E_R12_REGNUM, E_R13_REGNUM, E_R14_REGNUM, E_R15_REGNUM,
  34.   E_R16_REGNUM, E_R17_REGNUM, E_R18_REGNUM, E_R19_REGNUM,
  35.   E_R20_REGNUM, E_R21_REGNUM, E_R22_REGNUM, E_R23_REGNUM,
  36.   E_R24_REGNUM, E_R25_REGNUM, E_R26_REGNUM, E_R27_REGNUM,
  37.   E_R28_REGNUM, E_R29_REGNUM, E_R30_REGNUM, E_R31_REGNUM,
  38.   E_PC_REGNUM,
  39.   E_LR_REGNUM        = E_R31_REGNUM, /* Link register.  */
  40.   E_SP_REGNUM        = E_R29_REGNUM, /* Stack pointer.  */
  41.   E_FP_REGNUM        = E_R27_REGNUM, /* Frame pointer.  */
  42.   E_FN_RETURN_REGNUM = E_R2_REGNUM,  /* Function return value register.  */
  43.   E_1ST_ARGREG       = E_R4_REGNUM,  /* 1st  function arg register.  */
  44.   E_LAST_ARGREG      = E_R11_REGNUM, /* Last function arg register.  */
  45.   E_NUM_REGS         = E_PC_REGNUM + 1
  46. };

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

  49. struct iq2000_frame_cache
  50. {
  51.   /* Base address.  */
  52.   CORE_ADDR  base;
  53.   CORE_ADDR  pc;
  54.   LONGEST    framesize;
  55.   int        using_fp;
  56.   CORE_ADDR  saved_sp;
  57.   CORE_ADDR  saved_regs [E_NUM_REGS];
  58. };

  59. /* Harvard methods: */

  60. static CORE_ADDR
  61. insn_ptr_from_addr (CORE_ADDR addr)        /* CORE_ADDR to target pointer.  */
  62. {
  63.   return addr & 0x7fffffffL;
  64. }

  65. static CORE_ADDR
  66. insn_addr_from_ptr (CORE_ADDR ptr)        /* target_pointer to CORE_ADDR.  */
  67. {
  68.   return (ptr & 0x7fffffffL) | 0x80000000L;
  69. }

  70. /* Function: pointer_to_address
  71.    Convert a target pointer to an address in host (CORE_ADDR) format.  */

  72. static CORE_ADDR
  73. iq2000_pointer_to_address (struct gdbarch *gdbarch,
  74.                            struct type * type, const gdb_byte * buf)
  75. {
  76.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  77.   enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
  78.   CORE_ADDR addr
  79.     = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);

  80.   if (target == TYPE_CODE_FUNC
  81.       || target == TYPE_CODE_METHOD
  82.       || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
  83.     addr = insn_addr_from_ptr (addr);

  84.   return addr;
  85. }

  86. /* Function: address_to_pointer
  87.    Convert a host-format address (CORE_ADDR) into a target pointer.  */

  88. static void
  89. iq2000_address_to_pointer (struct gdbarch *gdbarch,
  90.                            struct type *type, gdb_byte *buf, CORE_ADDR addr)
  91. {
  92.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  93.   enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));

  94.   if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
  95.     addr = insn_ptr_from_addr (addr);
  96.   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  97. }

  98. /* Real register methods: */

  99. /* Function: register_name
  100.    Returns the name of the iq2000 register number N.  */

  101. static const char *
  102. iq2000_register_name (struct gdbarch *gdbarch, int regnum)
  103. {
  104.   static const char * names[E_NUM_REGS] =
  105.     {
  106.       "r0""r1""r2""r3""r4",
  107.       "r5""r6""r7""r8""r9",
  108.       "r10", "r11", "r12", "r13", "r14",
  109.       "r15", "r16", "r17", "r18", "r19",
  110.       "r20", "r21", "r22", "r23", "r24",
  111.       "r25", "r26", "r27", "r28", "r29",
  112.       "r30", "r31",
  113.       "pc"
  114.     };
  115.   if (regnum < 0 || regnum >= E_NUM_REGS)
  116.     return NULL;
  117.   return names[regnum];
  118. }

  119. /* Prologue analysis methods:  */

  120. /* ADDIU insn (001001 rs(5) rt(5) imm(16)).  */
  121. #define INSN_IS_ADDIU(X)        (((X) & 0xfc000000) == 0x24000000)
  122. #define ADDIU_REG_SRC(X)        (((X) & 0x03e00000) >> 21)
  123. #define ADDIU_REG_TGT(X)        (((X) & 0x001f0000) >> 16)
  124. #define ADDIU_IMMEDIATE(X)        ((signed short) ((X) & 0x0000ffff))

  125. /* "MOVE" (OR) insn (000000 rs(5) rt(5) rd(5) 00000 100101).  */
  126. #define INSN_IS_MOVE(X)                (((X) & 0xffe007ff) == 0x00000025)
  127. #define MOVE_REG_SRC(X)                (((X) & 0x001f0000) >> 16)
  128. #define MOVE_REG_TGT(X)                (((X) & 0x0000f800) >> 11)

  129. /* STORE WORD insn (101011 rs(5) rt(5) offset(16)).  */
  130. #define INSN_IS_STORE_WORD(X)        (((X) & 0xfc000000) == 0xac000000)
  131. #define SW_REG_INDEX(X)                (((X) & 0x03e00000) >> 21)
  132. #define SW_REG_SRC(X)                (((X) & 0x001f0000) >> 16)
  133. #define SW_OFFSET(X)                ((signed short) ((X) & 0x0000ffff))

  134. /* Function: find_last_line_symbol

  135.    Given an address range, first find a line symbol corresponding to
  136.    the starting address.  Then find the last line symbol within the
  137.    range that has a line number less than or equal to the first line.

  138.    For optimized code with code motion, this finds the last address
  139.    for the lowest-numbered line within the address range.  */

  140. static struct symtab_and_line
  141. find_last_line_symbol (CORE_ADDR start, CORE_ADDR end, int notcurrent)
  142. {
  143.   struct symtab_and_line sal = find_pc_line (start, notcurrent);
  144.   struct symtab_and_line best_sal = sal;

  145.   if (sal.pc == 0 || sal.line == 0 || sal.end == 0)
  146.     return sal;

  147.   do
  148.     {
  149.       if (sal.line && sal.line <= best_sal.line)
  150.         best_sal = sal;
  151.       sal = find_pc_line (sal.end, notcurrent);
  152.     }
  153.   while (sal.pc && sal.pc < end);

  154.   return best_sal;
  155. }

  156. /* Function: scan_prologue
  157.    Decode the instructions within the given address range.
  158.    Decide when we must have reached the end of the function prologue.
  159.    If a frame_info pointer is provided, fill in its prologue information.

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

  161. static CORE_ADDR
  162. iq2000_scan_prologue (struct gdbarch *gdbarch,
  163.                       CORE_ADDR scan_start,
  164.                       CORE_ADDR scan_end,
  165.                       struct frame_info *fi,
  166.                       struct iq2000_frame_cache *cache)
  167. {
  168.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  169.   struct symtab_and_line sal;
  170.   CORE_ADDR pc;
  171.   CORE_ADDR loop_end;
  172.   int found_store_lr = 0;
  173.   int found_decr_sp = 0;
  174.   int srcreg;
  175.   int tgtreg;
  176.   signed short offset;

  177.   if (scan_end == (CORE_ADDR) 0)
  178.     {
  179.       loop_end = scan_start + 100;
  180.       sal.end = sal.pc = 0;
  181.     }
  182.   else
  183.     {
  184.       loop_end = scan_end;
  185.       if (fi)
  186.         sal = find_last_line_symbol (scan_start, scan_end, 0);
  187.       else
  188.         sal.end = 0;        /* Avoid GCC false warning.  */
  189.     }

  190.   /* Saved registers:
  191.      We first have to save the saved register's offset, and
  192.      only later do we compute its actual address.  Since the
  193.      offset can be zero, we must first initialize all the
  194.      saved regs to minus one (so we can later distinguish
  195.      between one that's not saved, and one that's saved at zero).  */
  196.   for (srcreg = 0; srcreg < E_NUM_REGS; srcreg ++)
  197.     cache->saved_regs[srcreg] = -1;
  198.   cache->using_fp = 0;
  199.   cache->framesize = 0;

  200.   for (pc = scan_start; pc < loop_end; pc += 4)
  201.     {
  202.       LONGEST insn = read_memory_unsigned_integer (pc, 4, byte_order);
  203.       /* Skip any instructions writing to (sp) or decrementing the
  204.          SP.  */
  205.       if ((insn & 0xffe00000) == 0xac200000)
  206.         {
  207.           /* sw using SP/%1 as base.  */
  208.           /* LEGACY -- from assembly-only port.  */
  209.           tgtreg = ((insn >> 16) & 0x1f);
  210.           if (tgtreg >= 0 && tgtreg < E_NUM_REGS)
  211.             cache->saved_regs[tgtreg] = -((signed short) (insn & 0xffff));

  212.           if (tgtreg == E_LR_REGNUM)
  213.             found_store_lr = 1;
  214.           continue;
  215.         }

  216.       if ((insn & 0xffff8000) == 0x20218000)
  217.         {
  218.           /* addi %1, %1, -N == addi %sp, %sp, -N */
  219.           /* LEGACY -- from assembly-only port.  */
  220.           found_decr_sp = 1;
  221.           cache->framesize = -((signed short) (insn & 0xffff));
  222.           continue;
  223.         }

  224.       if (INSN_IS_ADDIU (insn))
  225.         {
  226.           srcreg = ADDIU_REG_SRC (insn);
  227.           tgtreg = ADDIU_REG_TGT (insn);
  228.           offset = ADDIU_IMMEDIATE (insn);
  229.           if (srcreg == E_SP_REGNUM && tgtreg == E_SP_REGNUM)
  230.             cache->framesize = -offset;
  231.           continue;
  232.         }

  233.       if (INSN_IS_STORE_WORD (insn))
  234.         {
  235.           srcreg = SW_REG_SRC (insn);
  236.           tgtreg = SW_REG_INDEX (insn);
  237.           offset = SW_OFFSET (insn);

  238.           if (tgtreg == E_SP_REGNUM || tgtreg == E_FP_REGNUM)
  239.             {
  240.               /* "push" to stack (via SP or FP reg).  */
  241.               if (cache->saved_regs[srcreg] == -1) /* Don't save twice.  */
  242.                 cache->saved_regs[srcreg] = offset;
  243.               continue;
  244.             }
  245.         }

  246.       if (INSN_IS_MOVE (insn))
  247.         {
  248.           srcreg = MOVE_REG_SRC (insn);
  249.           tgtreg = MOVE_REG_TGT (insn);

  250.           if (srcreg == E_SP_REGNUM && tgtreg == E_FP_REGNUM)
  251.             {
  252.               /* Copy sp to fp.  */
  253.               cache->using_fp = 1;
  254.               continue;
  255.             }
  256.         }

  257.       /* Unknown instruction encountered in frame.  Bail out?
  258.          1) If we have a subsequent line symbol, we can keep going.
  259.          2) If not, we need to bail out and quit scanning instructions.  */

  260.       if (fi && sal.end && (pc < sal.end)) /* Keep scanning.  */
  261.         continue;
  262.       else /* bail */
  263.         break;
  264.     }

  265.   return pc;
  266. }

  267. static void
  268. iq2000_init_frame_cache (struct iq2000_frame_cache *cache)
  269. {
  270.   int i;

  271.   cache->base = 0;
  272.   cache->framesize = 0;
  273.   cache->using_fp = 0;
  274.   cache->saved_sp = 0;
  275.   for (i = 0; i < E_NUM_REGS; i++)
  276.     cache->saved_regs[i] = -1;
  277. }

  278. /* Function: iq2000_skip_prologue
  279.    If the input address is in a function prologue,
  280.    returns the address of the end of the prologue;
  281.    else returns the input address.

  282.    Note: the input address is likely to be the function start,
  283.    since this function is mainly used for advancing a breakpoint
  284.    to the first line, or stepping to the first line when we have
  285.    stepped into a function call.  */

  286. static CORE_ADDR
  287. iq2000_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  288. {
  289.   CORE_ADDR func_addr = 0 , func_end = 0;

  290.   if (find_pc_partial_function (pc, NULL, & func_addr, & func_end))
  291.     {
  292.       struct symtab_and_line sal;
  293.       struct iq2000_frame_cache cache;

  294.       /* Found a function.  */
  295.       sal = find_pc_line (func_addr, 0);
  296.       if (sal.end && sal.end < func_end)
  297.         /* Found a line number, use it as end of prologue.  */
  298.         return sal.end;

  299.       /* No useable line symbol.  Use prologue parsing method.  */
  300.       iq2000_init_frame_cache (&cache);
  301.       return iq2000_scan_prologue (gdbarch, func_addr, func_end, NULL, &cache);
  302.     }

  303.   /* No function symbol -- just return the PC.  */
  304.   return (CORE_ADDR) pc;
  305. }

  306. static struct iq2000_frame_cache *
  307. iq2000_frame_cache (struct frame_info *this_frame, void **this_cache)
  308. {
  309.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  310.   struct iq2000_frame_cache *cache;
  311.   CORE_ADDR current_pc;
  312.   int i;

  313.   if (*this_cache)
  314.     return *this_cache;

  315.   cache = FRAME_OBSTACK_ZALLOC (struct iq2000_frame_cache);
  316.   iq2000_init_frame_cache (cache);
  317.   *this_cache = cache;

  318.   cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
  319.   //if (cache->base == 0)
  320.     //return cache;

  321.   current_pc = get_frame_pc (this_frame);
  322.   find_pc_partial_function (current_pc, NULL, &cache->pc, NULL);
  323.   if (cache->pc != 0)
  324.     iq2000_scan_prologue (gdbarch, cache->pc, current_pc, this_frame, cache);
  325.   if (!cache->using_fp)
  326.     cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM);

  327.   cache->saved_sp = cache->base + cache->framesize;

  328.   for (i = 0; i < E_NUM_REGS; i++)
  329.     if (cache->saved_regs[i] != -1)
  330.       cache->saved_regs[i] += cache->base;

  331.   return cache;
  332. }

  333. static struct value *
  334. iq2000_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  335.                             int regnum)
  336. {
  337.   struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame,
  338.                                                          this_cache);

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

  341.   if (regnum == E_PC_REGNUM)
  342.     regnum = E_LR_REGNUM;

  343.   if (regnum < E_NUM_REGS && cache->saved_regs[regnum] != -1)
  344.     return frame_unwind_got_memory (this_frame, regnum,
  345.                                     cache->saved_regs[regnum]);

  346.   return frame_unwind_got_register (this_frame, regnum, regnum);
  347. }

  348. static void
  349. iq2000_frame_this_id (struct frame_info *this_frame, void **this_cache,
  350.                       struct frame_id *this_id)
  351. {
  352.   struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame,
  353.                                                          this_cache);

  354.   /* This marks the outermost frame.  */
  355.   if (cache->base == 0)
  356.     return;

  357.   *this_id = frame_id_build (cache->saved_sp, cache->pc);
  358. }

  359. static const struct frame_unwind iq2000_frame_unwind = {
  360.   NORMAL_FRAME,
  361.   default_frame_unwind_stop_reason,
  362.   iq2000_frame_this_id,
  363.   iq2000_frame_prev_register,
  364.   NULL,
  365.   default_frame_sniffer
  366. };

  367. static CORE_ADDR
  368. iq2000_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  369. {
  370.   return frame_unwind_register_unsigned (next_frame, E_SP_REGNUM);
  371. }

  372. static CORE_ADDR
  373. iq2000_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  374. {
  375.   return frame_unwind_register_unsigned (next_frame, E_PC_REGNUM);
  376. }

  377. static struct frame_id
  378. iq2000_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  379. {
  380.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
  381.   return frame_id_build (sp, get_frame_pc (this_frame));
  382. }

  383. static CORE_ADDR
  384. iq2000_frame_base_address (struct frame_info *this_frame, void **this_cache)
  385. {
  386.   struct iq2000_frame_cache *cache = iq2000_frame_cache (this_frame,
  387.                                                          this_cache);

  388.   return cache->base;
  389. }

  390. static const struct frame_base iq2000_frame_base = {
  391.   &iq2000_frame_unwind,
  392.   iq2000_frame_base_address,
  393.   iq2000_frame_base_address,
  394.   iq2000_frame_base_address
  395. };

  396. static const unsigned char *
  397. iq2000_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  398.                            int *lenptr)
  399. {
  400.   static const unsigned char big_breakpoint[] = { 0x00, 0x00, 0x00, 0x0d };
  401.   static const unsigned char little_breakpoint[] = { 0x0d, 0x00, 0x00, 0x00 };

  402.   if ((*pcptr & 3) != 0)
  403.     error (_("breakpoint_from_pc: invalid breakpoint address 0x%lx"),
  404.            (long) *pcptr);

  405.   *lenptr = 4;
  406.   return (gdbarch_byte_order (gdbarch)
  407.           == BFD_ENDIAN_BIG) ? big_breakpoint : little_breakpoint;
  408. }

  409. /* Target function return value methods: */

  410. /* Function: store_return_value
  411.    Copy the function return value from VALBUF into the
  412.    proper location for a function return.  */

  413. static void
  414. iq2000_store_return_value (struct type *type, struct regcache *regcache,
  415.                            const void *valbuf)
  416. {
  417.   int len = TYPE_LENGTH (type);
  418.   int regno = E_FN_RETURN_REGNUM;

  419.   while (len > 0)
  420.     {
  421.       gdb_byte buf[4];
  422.       int size = len % 4 ?: 4;

  423.       memset (buf, 0, 4);
  424.       memcpy (buf + 4 - size, valbuf, size);
  425.       regcache_raw_write (regcache, regno++, buf);
  426.       len -= size;
  427.       valbuf = ((char *) valbuf) + size;
  428.     }
  429. }

  430. /* Function: use_struct_convention
  431.    Returns non-zero if the given struct type will be returned using
  432.    a special convention, rather than the normal function return method.  */

  433. static int
  434. iq2000_use_struct_convention (struct type *type)
  435. {
  436.   return ((TYPE_CODE (type) == TYPE_CODE_STRUCT)
  437.           || (TYPE_CODE (type) == TYPE_CODE_UNION))
  438.          && TYPE_LENGTH (type) > 8;
  439. }

  440. /* Function: extract_return_value
  441.    Copy the function's return value into VALBUF.
  442.    This function is called only in the context of "target function calls",
  443.    ie. when the debugger forces a function to be called in the child, and
  444.    when the debugger forces a function to return prematurely via the
  445.    "return" command.  */

  446. static void
  447. iq2000_extract_return_value (struct type *type, struct regcache *regcache,
  448.                              void *valbuf)
  449. {
  450.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  451.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  452.   /* If the function's return value is 8 bytes or less, it is
  453.      returned in a register, and if larger than 8 bytes, it is
  454.      returned in a stack location which is pointed to by the same
  455.      register.  */
  456.   int len = TYPE_LENGTH (type);

  457.   if (len <= (2 * 4))
  458.     {
  459.       int regno = E_FN_RETURN_REGNUM;

  460.       /* Return values of <= 8 bytes are returned in
  461.          FN_RETURN_REGNUM.  */
  462.       while (len > 0)
  463.         {
  464.           ULONGEST tmp;
  465.           int size = len % 4 ?: 4;

  466.           /* By using store_unsigned_integer we avoid having to
  467.              do anything special for small big-endian values.  */
  468.           regcache_cooked_read_unsigned (regcache, regno++, &tmp);
  469.           store_unsigned_integer (valbuf, size, byte_order, tmp);
  470.           len -= size;
  471.           valbuf = ((char *) valbuf) + size;
  472.         }
  473.     }
  474.   else
  475.     {
  476.       /* Return values > 8 bytes are returned in memory,
  477.          pointed to by FN_RETURN_REGNUM.  */
  478.       ULONGEST return_buffer;
  479.       regcache_cooked_read_unsigned (regcache, E_FN_RETURN_REGNUM,
  480.                                      &return_buffer);
  481.       read_memory (return_buffer, valbuf, TYPE_LENGTH (type));
  482.     }
  483. }

  484. static enum return_value_convention
  485. iq2000_return_value (struct gdbarch *gdbarch, struct value *function,
  486.                      struct type *type, struct regcache *regcache,
  487.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  488. {
  489.   if (iq2000_use_struct_convention (type))
  490.     return RETURN_VALUE_STRUCT_CONVENTION;
  491.   if (writebuf)
  492.     iq2000_store_return_value (type, regcache, writebuf);
  493.   else if (readbuf)
  494.     iq2000_extract_return_value (type, regcache, readbuf);
  495.   return RETURN_VALUE_REGISTER_CONVENTION;
  496. }

  497. /* Function: register_virtual_type
  498.    Returns the default type for register N.  */

  499. static struct type *
  500. iq2000_register_type (struct gdbarch *gdbarch, int regnum)
  501. {
  502.   return builtin_type (gdbarch)->builtin_int32;
  503. }

  504. static CORE_ADDR
  505. iq2000_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
  506. {
  507.   /* This is the same frame alignment used by gcc.  */
  508.   return ((sp + 7) & ~7);
  509. }

  510. /* Convenience function to check 8-byte types for being a scalar type
  511.    or a struct with only one long long or double member.  */
  512. static int
  513. iq2000_pass_8bytetype_by_address (struct type *type)
  514. {
  515.   struct type *ftype;

  516.   /* Skip typedefs.  */
  517.   while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
  518.     type = TYPE_TARGET_TYPE (type);
  519.   /* Non-struct and non-union types are always passed by value.  */
  520.   if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  521.       && TYPE_CODE (type) != TYPE_CODE_UNION)
  522.     return 0;
  523.   /* Structs with more than 1 field are always passed by address.  */
  524.   if (TYPE_NFIELDS (type) != 1)
  525.     return 1;
  526.   /* Get field type.  */
  527.   ftype = (TYPE_FIELDS (type))[0].type;
  528.   /* The field type must have size 8, otherwise pass by address.  */
  529.   if (TYPE_LENGTH (ftype) != 8)
  530.     return 1;
  531.   /* Skip typedefs of field type.  */
  532.   while (TYPE_CODE (ftype) == TYPE_CODE_TYPEDEF)
  533.     ftype = TYPE_TARGET_TYPE (ftype);
  534.   /* If field is int or float, pass by value.  */
  535.   if (TYPE_CODE (ftype) == TYPE_CODE_FLT
  536.       || TYPE_CODE (ftype) == TYPE_CODE_INT)
  537.     return 0;
  538.   /* Everything else, pass by address.  */
  539.   return 1;
  540. }

  541. static CORE_ADDR
  542. iq2000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  543.                         struct regcache *regcache, CORE_ADDR bp_addr,
  544.                         int nargs, struct value **args, CORE_ADDR sp,
  545.                         int struct_return, CORE_ADDR struct_addr)
  546. {
  547.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  548.   const bfd_byte *val;
  549.   bfd_byte buf[4];
  550.   struct type *type;
  551.   int i, argreg, typelen, slacklen;
  552.   int stackspace = 0;
  553.   /* Used to copy struct arguments into the stack.  */
  554.   CORE_ADDR struct_ptr;

  555.   /* First determine how much stack space we will need.  */
  556.   for (i = 0, argreg = E_1ST_ARGREG + (struct_return != 0); i < nargs; i++)
  557.     {
  558.       type = value_type (args[i]);
  559.       typelen = TYPE_LENGTH (type);
  560.       if (typelen <= 4)
  561.         {
  562.           /* Scalars of up to 4 bytes,
  563.              structs of up to 4 bytes, and
  564.              pointers.  */
  565.           if (argreg <= E_LAST_ARGREG)
  566.             argreg++;
  567.           else
  568.             stackspace += 4;
  569.         }
  570.       else if (typelen == 8 && !iq2000_pass_8bytetype_by_address (type))
  571.         {
  572.           /* long long,
  573.              double, and possibly
  574.              structs with a single field of long long or double.  */
  575.           if (argreg <= E_LAST_ARGREG - 1)
  576.             {
  577.               /* 8-byte arg goes into a register pair
  578.                  (must start with an even-numbered reg).  */
  579.               if (((argreg - E_1ST_ARGREG) % 2) != 0)
  580.                 argreg ++;
  581.               argreg += 2;
  582.             }
  583.           else
  584.             {
  585.               argreg = E_LAST_ARGREG + 1;       /* no more argregs.  */
  586.               /* 8-byte arg goes on stack, must be 8-byte aligned.  */
  587.               stackspace = ((stackspace + 7) & ~7);
  588.               stackspace += 8;
  589.             }
  590.         }
  591.       else
  592.         {
  593.           /* Structs are passed as pointer to a copy of the struct.
  594.              So we need room on the stack for a copy of the struct
  595.              plus for the argument pointer.  */
  596.           if (argreg <= E_LAST_ARGREG)
  597.             argreg++;
  598.           else
  599.             stackspace += 4;
  600.           /* Care for 8-byte alignment of structs saved on stack.  */
  601.           stackspace += ((typelen + 7) & ~7);
  602.         }
  603.     }

  604.   /* Now copy params, in ascending order, into their assigned location
  605.      (either in a register or on the stack).  */

  606.   sp -= (sp % 8);       /* align */
  607.   struct_ptr = sp;
  608.   sp -= stackspace;
  609.   sp -= (sp % 8);       /* align again */
  610.   stackspace = 0;

  611.   argreg = E_1ST_ARGREG;
  612.   if (struct_return)
  613.     {
  614.       /* A function that returns a struct will consume one argreg to do so.
  615.        */
  616.       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
  617.     }

  618.   for (i = 0; i < nargs; i++)
  619.     {
  620.       type = value_type (args[i]);
  621.       typelen = TYPE_LENGTH (type);
  622.       val = value_contents (args[i]);
  623.       if (typelen <= 4)
  624.         {
  625.           /* Char, short, int, float, pointer, and structs <= four bytes.  */
  626.           slacklen = (4 - (typelen % 4)) % 4;
  627.           memset (buf, 0, sizeof (buf));
  628.           memcpy (buf + slacklen, val, typelen);
  629.           if (argreg <= E_LAST_ARGREG)
  630.             {
  631.               /* Passed in a register.  */
  632.               regcache_raw_write (regcache, argreg++, buf);
  633.             }
  634.           else
  635.             {
  636.               /* Passed on the stack.  */
  637.               write_memory (sp + stackspace, buf, 4);
  638.               stackspace += 4;
  639.             }
  640.         }
  641.       else if (typelen == 8 && !iq2000_pass_8bytetype_by_address (type))
  642.         {
  643.           /* (long long), (double), or struct consisting of
  644.              a single (long long) or (double).  */
  645.           if (argreg <= E_LAST_ARGREG - 1)
  646.             {
  647.               /* 8-byte arg goes into a register pair
  648.                  (must start with an even-numbered reg).  */
  649.               if (((argreg - E_1ST_ARGREG) % 2) != 0)
  650.                 argreg++;
  651.               regcache_raw_write (regcache, argreg++, val);
  652.               regcache_raw_write (regcache, argreg++, val + 4);
  653.             }
  654.           else
  655.             {
  656.               /* 8-byte arg goes on stack, must be 8-byte aligned.  */
  657.               argreg = E_LAST_ARGREG + 1;       /* no more argregs.  */
  658.               stackspace = ((stackspace + 7) & ~7);
  659.               write_memory (sp + stackspace, val, typelen);
  660.               stackspace += 8;
  661.             }
  662.         }
  663.       else
  664.         {
  665.           /* Store struct beginning at the upper end of the previously
  666.              computed stack space.  Then store the address of the struct
  667.              using the usual rules for a 4 byte value.  */
  668.           struct_ptr -= ((typelen + 7) & ~7);
  669.           write_memory (struct_ptr, val, typelen);
  670.           if (argreg <= E_LAST_ARGREG)
  671.             regcache_cooked_write_unsigned (regcache, argreg++, struct_ptr);
  672.           else
  673.             {
  674.               store_unsigned_integer (buf, 4, byte_order, struct_ptr);
  675.               write_memory (sp + stackspace, buf, 4);
  676.               stackspace += 4;
  677.             }
  678.         }
  679.     }

  680.   /* Store return address.  */
  681.   regcache_cooked_write_unsigned (regcache, E_LR_REGNUM, bp_addr);

  682.   /* Update stack pointer.  */
  683.   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);

  684.   /* And that should do it.  Return the new stack pointer.  */
  685.   return sp;
  686. }

  687. /* Function: gdbarch_init
  688.    Initializer function for the iq2000 gdbarch vector.
  689.    Called by gdbarch.  Sets up the gdbarch vector(s) for this target.  */

  690. static struct gdbarch *
  691. iq2000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  692. {
  693.   struct gdbarch *gdbarch;

  694.   /* Look up list for candidates - only one.  */
  695.   arches = gdbarch_list_lookup_by_info (arches, &info);
  696.   if (arches != NULL)
  697.     return arches->gdbarch;

  698.   gdbarch = gdbarch_alloc (&info, NULL);

  699.   set_gdbarch_num_regs             (gdbarch, E_NUM_REGS);
  700.   set_gdbarch_num_pseudo_regs      (gdbarch, 0);
  701.   set_gdbarch_sp_regnum            (gdbarch, E_SP_REGNUM);
  702.   set_gdbarch_pc_regnum            (gdbarch, E_PC_REGNUM);
  703.   set_gdbarch_register_name        (gdbarch, iq2000_register_name);
  704.   set_gdbarch_address_to_pointer   (gdbarch, iq2000_address_to_pointer);
  705.   set_gdbarch_pointer_to_address   (gdbarch, iq2000_pointer_to_address);
  706.   set_gdbarch_ptr_bit              (gdbarch, 4 * TARGET_CHAR_BIT);
  707.   set_gdbarch_short_bit            (gdbarch, 2 * TARGET_CHAR_BIT);
  708.   set_gdbarch_int_bit              (gdbarch, 4 * TARGET_CHAR_BIT);
  709.   set_gdbarch_long_bit             (gdbarch, 4 * TARGET_CHAR_BIT);
  710.   set_gdbarch_long_long_bit        (gdbarch, 8 * TARGET_CHAR_BIT);
  711.   set_gdbarch_float_bit            (gdbarch, 4 * TARGET_CHAR_BIT);
  712.   set_gdbarch_double_bit           (gdbarch, 8 * TARGET_CHAR_BIT);
  713.   set_gdbarch_long_double_bit      (gdbarch, 8 * TARGET_CHAR_BIT);
  714.   set_gdbarch_float_format         (gdbarch, floatformats_ieee_single);
  715.   set_gdbarch_double_format        (gdbarch, floatformats_ieee_double);
  716.   set_gdbarch_long_double_format   (gdbarch, floatformats_ieee_double);
  717.   set_gdbarch_return_value           (gdbarch, iq2000_return_value);
  718.   set_gdbarch_breakpoint_from_pc   (gdbarch, iq2000_breakpoint_from_pc);
  719.   set_gdbarch_frame_args_skip      (gdbarch, 0);
  720.   set_gdbarch_skip_prologue        (gdbarch, iq2000_skip_prologue);
  721.   set_gdbarch_inner_than           (gdbarch, core_addr_lessthan);
  722.   set_gdbarch_print_insn           (gdbarch, print_insn_iq2000);
  723.   set_gdbarch_register_type (gdbarch, iq2000_register_type);
  724.   set_gdbarch_frame_align (gdbarch, iq2000_frame_align);
  725.   set_gdbarch_unwind_sp (gdbarch, iq2000_unwind_sp);
  726.   set_gdbarch_unwind_pc (gdbarch, iq2000_unwind_pc);
  727.   set_gdbarch_dummy_id (gdbarch, iq2000_dummy_id);
  728.   frame_base_set_default (gdbarch, &iq2000_frame_base);
  729.   set_gdbarch_push_dummy_call (gdbarch, iq2000_push_dummy_call);

  730.   gdbarch_init_osabi (info, gdbarch);

  731.   dwarf2_append_unwinders (gdbarch);
  732.   frame_unwind_append_unwinder (gdbarch, &iq2000_frame_unwind);

  733.   return gdbarch;
  734. }

  735. /* Function: _initialize_iq2000_tdep
  736.    Initializer function for the iq2000 module.
  737.    Called by gdb at start-up.  */

  738. /* Provide a prototype to silence -Wmissing-prototypes.  */
  739. extern initialize_file_ftype _initialize_iq2000_tdep;

  740. void
  741. _initialize_iq2000_tdep (void)
  742. {
  743.   register_gdbarch_init (bfd_arch_iq2000, iq2000_gdbarch_init);
  744. }