gdb/frv-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Target-dependent code for the Fujitsu FR-V, for GDB, the GNU Debugger.

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "inferior.h"
  16. #include "gdbcore.h"
  17. #include "arch-utils.h"
  18. #include "regcache.h"
  19. #include "frame.h"
  20. #include "frame-unwind.h"
  21. #include "frame-base.h"
  22. #include "trad-frame.h"
  23. #include "dis-asm.h"
  24. #include "sim-regno.h"
  25. #include "gdb/sim-frv.h"
  26. #include "opcodes/frv-desc.h"        /* for the H_SPR_... enums */
  27. #include "symtab.h"
  28. #include "elf-bfd.h"
  29. #include "elf/frv.h"
  30. #include "osabi.h"
  31. #include "infcall.h"
  32. #include "solib.h"
  33. #include "frv-tdep.h"
  34. #include "objfiles.h"

  35. extern void _initialize_frv_tdep (void);

  36. struct frv_unwind_cache                /* was struct frame_extra_info */
  37.   {
  38.     /* The previous frame's inner-most stack address.  Used as this
  39.        frame ID's stack_addr.  */
  40.     CORE_ADDR prev_sp;

  41.     /* The frame's base, optionally used by the high-level debug info.  */
  42.     CORE_ADDR base;

  43.     /* Table indicating the location of each and every register.  */
  44.     struct trad_frame_saved_reg *saved_regs;
  45.   };

  46. /* A structure describing a particular variant of the FRV.
  47.    We allocate and initialize one of these structures when we create
  48.    the gdbarch object for a variant.

  49.    At the moment, all the FR variants we support differ only in which
  50.    registers are present; the portable code of GDB knows that
  51.    registers whose names are the empty string don't exist, so the
  52.    `register_names' array captures all the per-variant information we
  53.    need.

  54.    in the future, if we need to have per-variant maps for raw size,
  55.    virtual type, etc., we should replace register_names with an array
  56.    of structures, each of which gives all the necessary info for one
  57.    register.  Don't stick parallel arrays in here --- that's so
  58.    Fortran.  */
  59. struct gdbarch_tdep
  60. {
  61.   /* Which ABI is in use?  */
  62.   enum frv_abi frv_abi;

  63.   /* How many general-purpose registers does this variant have?  */
  64.   int num_gprs;

  65.   /* How many floating-point registers does this variant have?  */
  66.   int num_fprs;

  67.   /* How many hardware watchpoints can it support?  */
  68.   int num_hw_watchpoints;

  69.   /* How many hardware breakpoints can it support?  */
  70.   int num_hw_breakpoints;

  71.   /* Register names.  */
  72.   char **register_names;
  73. };

  74. /* Return the FR-V ABI associated with GDBARCH.  */
  75. enum frv_abi
  76. frv_abi (struct gdbarch *gdbarch)
  77. {
  78.   return gdbarch_tdep (gdbarch)->frv_abi;
  79. }

  80. /* Fetch the interpreter and executable loadmap addresses (for shared
  81.    library support) for the FDPIC ABI.  Return 0 if successful, -1 if
  82.    not.  (E.g, -1 will be returned if the ABI isn't the FDPIC ABI.)  */
  83. int
  84. frv_fdpic_loadmap_addresses (struct gdbarch *gdbarch, CORE_ADDR *interp_addr,
  85.                              CORE_ADDR *exec_addr)
  86. {
  87.   if (frv_abi (gdbarch) != FRV_ABI_FDPIC)
  88.     return -1;
  89.   else
  90.     {
  91.       struct regcache *regcache = get_current_regcache ();

  92.       if (interp_addr != NULL)
  93.         {
  94.           ULONGEST val;
  95.           regcache_cooked_read_unsigned (regcache,
  96.                                          fdpic_loadmap_interp_regnum, &val);
  97.           *interp_addr = val;
  98.         }
  99.       if (exec_addr != NULL)
  100.         {
  101.           ULONGEST val;
  102.           regcache_cooked_read_unsigned (regcache,
  103.                                          fdpic_loadmap_exec_regnum, &val);
  104.           *exec_addr = val;
  105.         }
  106.       return 0;
  107.     }
  108. }

  109. /* Allocate a new variant structure, and set up default values for all
  110.    the fields.  */
  111. static struct gdbarch_tdep *
  112. new_variant (void)
  113. {
  114.   struct gdbarch_tdep *var;
  115.   int r;

  116.   var = xmalloc (sizeof (*var));
  117.   memset (var, 0, sizeof (*var));

  118.   var->frv_abi = FRV_ABI_EABI;
  119.   var->num_gprs = 64;
  120.   var->num_fprs = 64;
  121.   var->num_hw_watchpoints = 0;
  122.   var->num_hw_breakpoints = 0;

  123.   /* By default, don't supply any general-purpose or floating-point
  124.      register names.  */
  125.   var->register_names
  126.     = (char **) xmalloc ((frv_num_regs + frv_num_pseudo_regs)
  127.                          * sizeof (char *));
  128.   for (r = 0; r < frv_num_regs + frv_num_pseudo_regs; r++)
  129.     var->register_names[r] = "";

  130.   /* Do, however, supply default names for the known special-purpose
  131.      registers.  */

  132.   var->register_names[pc_regnum] = "pc";
  133.   var->register_names[lr_regnum] = "lr";
  134.   var->register_names[lcr_regnum] = "lcr";

  135.   var->register_names[psr_regnum] = "psr";
  136.   var->register_names[ccr_regnum] = "ccr";
  137.   var->register_names[cccr_regnum] = "cccr";
  138.   var->register_names[tbr_regnum] = "tbr";

  139.   /* Debug registers.  */
  140.   var->register_names[brr_regnum] = "brr";
  141.   var->register_names[dbar0_regnum] = "dbar0";
  142.   var->register_names[dbar1_regnum] = "dbar1";
  143.   var->register_names[dbar2_regnum] = "dbar2";
  144.   var->register_names[dbar3_regnum] = "dbar3";

  145.   /* iacc0 (Only found on MB93405.)  */
  146.   var->register_names[iacc0h_regnum] = "iacc0h";
  147.   var->register_names[iacc0l_regnum] = "iacc0l";
  148.   var->register_names[iacc0_regnum] = "iacc0";

  149.   /* fsr0 (Found on FR555 and FR501.)  */
  150.   var->register_names[fsr0_regnum] = "fsr0";

  151.   /* acc0 - acc7.  The architecture provides for the possibility of many
  152.      more (up to 64 total), but we don't want to make that big of a hole
  153.      in the G packet.  If we need more in the future, we'll add them
  154.      elsewhere.  */
  155.   for (r = acc0_regnum; r <= acc7_regnum; r++)
  156.     {
  157.       char *buf;
  158.       buf = xstrprintf ("acc%d", r - acc0_regnum);
  159.       var->register_names[r] = buf;
  160.     }

  161.   /* accg0 - accg7: These are one byte registers.  The remote protocol
  162.      provides the raw values packed four into a slot.  accg0123 and
  163.      accg4567 correspond to accg0 - accg3 and accg4-accg7 respectively.
  164.      We don't provide names for accg0123 and accg4567 since the user will
  165.      likely not want to see these raw values.  */

  166.   for (r = accg0_regnum; r <= accg7_regnum; r++)
  167.     {
  168.       char *buf;
  169.       buf = xstrprintf ("accg%d", r - accg0_regnum);
  170.       var->register_names[r] = buf;
  171.     }

  172.   /* msr0 and msr1.  */

  173.   var->register_names[msr0_regnum] = "msr0";
  174.   var->register_names[msr1_regnum] = "msr1";

  175.   /* gner and fner registers.  */
  176.   var->register_names[gner0_regnum] = "gner0";
  177.   var->register_names[gner1_regnum] = "gner1";
  178.   var->register_names[fner0_regnum] = "fner0";
  179.   var->register_names[fner1_regnum] = "fner1";

  180.   return var;
  181. }


  182. /* Indicate that the variant VAR has NUM_GPRS general-purpose
  183.    registers, and fill in the names array appropriately.  */
  184. static void
  185. set_variant_num_gprs (struct gdbarch_tdep *var, int num_gprs)
  186. {
  187.   int r;

  188.   var->num_gprs = num_gprs;

  189.   for (r = 0; r < num_gprs; ++r)
  190.     {
  191.       char buf[20];

  192.       xsnprintf (buf, sizeof (buf), "gr%d", r);
  193.       var->register_names[first_gpr_regnum + r] = xstrdup (buf);
  194.     }
  195. }


  196. /* Indicate that the variant VAR has NUM_FPRS floating-point
  197.    registers, and fill in the names array appropriately.  */
  198. static void
  199. set_variant_num_fprs (struct gdbarch_tdep *var, int num_fprs)
  200. {
  201.   int r;

  202.   var->num_fprs = num_fprs;

  203.   for (r = 0; r < num_fprs; ++r)
  204.     {
  205.       char buf[20];

  206.       xsnprintf (buf, sizeof (buf), "fr%d", r);
  207.       var->register_names[first_fpr_regnum + r] = xstrdup (buf);
  208.     }
  209. }

  210. static void
  211. set_variant_abi_fdpic (struct gdbarch_tdep *var)
  212. {
  213.   var->frv_abi = FRV_ABI_FDPIC;
  214.   var->register_names[fdpic_loadmap_exec_regnum] = xstrdup ("loadmap_exec");
  215.   var->register_names[fdpic_loadmap_interp_regnum]
  216.     = xstrdup ("loadmap_interp");
  217. }

  218. static void
  219. set_variant_scratch_registers (struct gdbarch_tdep *var)
  220. {
  221.   var->register_names[scr0_regnum] = xstrdup ("scr0");
  222.   var->register_names[scr1_regnum] = xstrdup ("scr1");
  223.   var->register_names[scr2_regnum] = xstrdup ("scr2");
  224.   var->register_names[scr3_regnum] = xstrdup ("scr3");
  225. }

  226. static const char *
  227. frv_register_name (struct gdbarch *gdbarch, int reg)
  228. {
  229.   if (reg < 0)
  230.     return "?toosmall?";
  231.   if (reg >= frv_num_regs + frv_num_pseudo_regs)
  232.     return "?toolarge?";

  233.   return gdbarch_tdep (gdbarch)->register_names[reg];
  234. }


  235. static struct type *
  236. frv_register_type (struct gdbarch *gdbarch, int reg)
  237. {
  238.   if (reg >= first_fpr_regnum && reg <= last_fpr_regnum)
  239.     return builtin_type (gdbarch)->builtin_float;
  240.   else if (reg == iacc0_regnum)
  241.     return builtin_type (gdbarch)->builtin_int64;
  242.   else
  243.     return builtin_type (gdbarch)->builtin_int32;
  244. }

  245. static enum register_status
  246. frv_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  247.                           int reg, gdb_byte *buffer)
  248. {
  249.   enum register_status status;

  250.   if (reg == iacc0_regnum)
  251.     {
  252.       status = regcache_raw_read (regcache, iacc0h_regnum, buffer);
  253.       if (status == REG_VALID)
  254.         status = regcache_raw_read (regcache, iacc0l_regnum, (bfd_byte *) buffer + 4);
  255.     }
  256.   else if (accg0_regnum <= reg && reg <= accg7_regnum)
  257.     {
  258.       /* The accg raw registers have four values in each slot with the
  259.          lowest register number occupying the first byte.  */

  260.       int raw_regnum = accg0123_regnum + (reg - accg0_regnum) / 4;
  261.       int byte_num = (reg - accg0_regnum) % 4;
  262.       gdb_byte buf[4];

  263.       status = regcache_raw_read (regcache, raw_regnum, buf);
  264.       if (status == REG_VALID)
  265.         {
  266.           memset (buffer, 0, 4);
  267.           /* FR-V is big endian, so put the requested byte in the
  268.              first byte of the buffer allocated to hold the
  269.              pseudo-register.  */
  270.           buffer[0] = buf[byte_num];
  271.         }
  272.     }
  273.   else
  274.     gdb_assert_not_reached ("invalid pseudo register number");

  275.   return status;
  276. }

  277. static void
  278. frv_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  279.                           int reg, const gdb_byte *buffer)
  280. {
  281.   if (reg == iacc0_regnum)
  282.     {
  283.       regcache_raw_write (regcache, iacc0h_regnum, buffer);
  284.       regcache_raw_write (regcache, iacc0l_regnum, (bfd_byte *) buffer + 4);
  285.     }
  286.   else if (accg0_regnum <= reg && reg <= accg7_regnum)
  287.     {
  288.       /* The accg raw registers have four values in each slot with the
  289.          lowest register number occupying the first byte.  */

  290.       int raw_regnum = accg0123_regnum + (reg - accg0_regnum) / 4;
  291.       int byte_num = (reg - accg0_regnum) % 4;
  292.       gdb_byte buf[4];

  293.       regcache_raw_read (regcache, raw_regnum, buf);
  294.       buf[byte_num] = ((bfd_byte *) buffer)[0];
  295.       regcache_raw_write (regcache, raw_regnum, buf);
  296.     }
  297. }

  298. static int
  299. frv_register_sim_regno (struct gdbarch *gdbarch, int reg)
  300. {
  301.   static const int spr_map[] =
  302.     {
  303.       H_SPR_PSR,                /* psr_regnum */
  304.       H_SPR_CCR,                /* ccr_regnum */
  305.       H_SPR_CCCR,                /* cccr_regnum */
  306.       -1,                        /* fdpic_loadmap_exec_regnum */
  307.       -1,                        /* fdpic_loadmap_interp_regnum */
  308.       -1,                        /* 134 */
  309.       H_SPR_TBR,                /* tbr_regnum */
  310.       H_SPR_BRR,                /* brr_regnum */
  311.       H_SPR_DBAR0,                /* dbar0_regnum */
  312.       H_SPR_DBAR1,                /* dbar1_regnum */
  313.       H_SPR_DBAR2,                /* dbar2_regnum */
  314.       H_SPR_DBAR3,                /* dbar3_regnum */
  315.       H_SPR_SCR0,                /* scr0_regnum */
  316.       H_SPR_SCR1,                /* scr1_regnum */
  317.       H_SPR_SCR2,                /* scr2_regnum */
  318.       H_SPR_SCR3,                /* scr3_regnum */
  319.       H_SPR_LR,                        /* lr_regnum */
  320.       H_SPR_LCR,                /* lcr_regnum */
  321.       H_SPR_IACC0H,                /* iacc0h_regnum */
  322.       H_SPR_IACC0L,                /* iacc0l_regnum */
  323.       H_SPR_FSR0,                /* fsr0_regnum */
  324.       /* FIXME: Add infrastructure for fetching/setting ACC and ACCG regs.  */
  325.       -1,                        /* acc0_regnum */
  326.       -1,                        /* acc1_regnum */
  327.       -1,                        /* acc2_regnum */
  328.       -1,                        /* acc3_regnum */
  329.       -1,                        /* acc4_regnum */
  330.       -1,                        /* acc5_regnum */
  331.       -1,                        /* acc6_regnum */
  332.       -1,                        /* acc7_regnum */
  333.       -1,                        /* acc0123_regnum */
  334.       -1,                        /* acc4567_regnum */
  335.       H_SPR_MSR0,                /* msr0_regnum */
  336.       H_SPR_MSR1,                /* msr1_regnum */
  337.       H_SPR_GNER0,                /* gner0_regnum */
  338.       H_SPR_GNER1,                /* gner1_regnum */
  339.       H_SPR_FNER0,                /* fner0_regnum */
  340.       H_SPR_FNER1,                /* fner1_regnum */
  341.     };

  342.   gdb_assert (reg >= 0 && reg < gdbarch_num_regs (gdbarch));

  343.   if (first_gpr_regnum <= reg && reg <= last_gpr_regnum)
  344.     return reg - first_gpr_regnum + SIM_FRV_GR0_REGNUM;
  345.   else if (first_fpr_regnum <= reg && reg <= last_fpr_regnum)
  346.     return reg - first_fpr_regnum + SIM_FRV_FR0_REGNUM;
  347.   else if (pc_regnum == reg)
  348.     return SIM_FRV_PC_REGNUM;
  349.   else if (reg >= first_spr_regnum
  350.            && reg < first_spr_regnum + sizeof (spr_map) / sizeof (spr_map[0]))
  351.     {
  352.       int spr_reg_offset = spr_map[reg - first_spr_regnum];

  353.       if (spr_reg_offset < 0)
  354.         return SIM_REGNO_DOES_NOT_EXIST;
  355.       else
  356.         return SIM_FRV_SPR0_REGNUM + spr_reg_offset;
  357.     }

  358.   internal_error (__FILE__, __LINE__, _("Bad register number %d"), reg);
  359. }

  360. static const unsigned char *
  361. frv_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenp)
  362. {
  363.   static unsigned char breakpoint[] = {0xc0, 0x70, 0x00, 0x01};
  364.   *lenp = sizeof (breakpoint);
  365.   return breakpoint;
  366. }

  367. /* Define the maximum number of instructions which may be packed into a
  368.    bundle (VLIW instruction).  */
  369. static const int max_instrs_per_bundle = 8;

  370. /* Define the size (in bytes) of an FR-V instruction.  */
  371. static const int frv_instr_size = 4;

  372. /* Adjust a breakpoint's address to account for the FR-V architecture's
  373.    constraint that a break instruction must not appear as any but the
  374.    first instruction in the bundle.  */
  375. static CORE_ADDR
  376. frv_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
  377. {
  378.   int count = max_instrs_per_bundle;
  379.   CORE_ADDR addr = bpaddr - frv_instr_size;
  380.   CORE_ADDR func_start = get_pc_function_start (bpaddr);

  381.   /* Find the end of the previous packing sequence.  This will be indicated
  382.      by either attempting to access some inaccessible memory or by finding
  383.      an instruction word whose packing bit is set to one.  */
  384.   while (count-- > 0 && addr >= func_start)
  385.     {
  386.       gdb_byte instr[frv_instr_size];
  387.       int status;

  388.       status = target_read_memory (addr, instr, sizeof instr);

  389.       if (status != 0)
  390.         break;

  391.       /* This is a big endian architecture, so byte zero will have most
  392.          significant byte.  The most significant bit of this byte is the
  393.          packing bit.  */
  394.       if (instr[0] & 0x80)
  395.         break;

  396.       addr -= frv_instr_size;
  397.     }

  398.   if (count > 0)
  399.     bpaddr = addr + frv_instr_size;

  400.   return bpaddr;
  401. }


  402. /* Return true if REG is a caller-saves ("scratch") register,
  403.    false otherwise.  */
  404. static int
  405. is_caller_saves_reg (int reg)
  406. {
  407.   return ((4 <= reg && reg <= 7)
  408.           || (14 <= reg && reg <= 15)
  409.           || (32 <= reg && reg <= 47));
  410. }


  411. /* Return true if REG is a callee-saves register, false otherwise.  */
  412. static int
  413. is_callee_saves_reg (int reg)
  414. {
  415.   return ((16 <= reg && reg <= 31)
  416.           || (48 <= reg && reg <= 63));
  417. }


  418. /* Return true if REG is an argument register, false otherwise.  */
  419. static int
  420. is_argument_reg (int reg)
  421. {
  422.   return (8 <= reg && reg <= 13);
  423. }

  424. /* Scan an FR-V prologue, starting at PC, until frame->PC.
  425.    If FRAME is non-zero, fill in its saved_regs with appropriate addresses.
  426.    We assume FRAME's saved_regs array has already been allocated and cleared.
  427.    Return the first PC value after the prologue.

  428.    Note that, for unoptimized code, we almost don't need this function
  429.    at all; all arguments and locals live on the stack, so we just need
  430.    the FP to find everything.  The catch: structures passed by value
  431.    have their addresses living in registers; they're never spilled to
  432.    the stack.  So if you ever want to be able to get to these
  433.    arguments in any frame but the top, you'll need to do this serious
  434.    prologue analysis.  */
  435. static CORE_ADDR
  436. frv_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  437.                       struct frame_info *this_frame,
  438.                       struct frv_unwind_cache *info)
  439. {
  440.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  441.   /* When writing out instruction bitpatterns, we use the following
  442.      letters to label instruction fields:
  443.      P - The parallel bit.  We don't use this.
  444.      J - The register number of GRj in the instruction description.
  445.      K - The register number of GRk in the instruction description.
  446.      I - The register number of GRi.
  447.      S - a signed imediate offset.
  448.      U - an unsigned immediate offset.

  449.      The dots below the numbers indicate where hex digit boundaries
  450.      fall, to make it easier to check the numbers.  */

  451.   /* Non-zero iff we've seen the instruction that initializes the
  452.      frame pointer for this function's frame.  */
  453.   int fp_set = 0;

  454.   /* If fp_set is non_zero, then this is the distance from
  455.      the stack pointer to frame pointer: fp = sp + fp_offset.  */
  456.   int fp_offset = 0;

  457.   /* Total size of frame prior to any alloca operations.  */
  458.   int framesize = 0;

  459.   /* Flag indicating if lr has been saved on the stack.  */
  460.   int lr_saved_on_stack = 0;

  461.   /* The number of the general-purpose register we saved the return
  462.      address ("link register") in, or -1 if we haven't moved it yet.  */
  463.   int lr_save_reg = -1;

  464.   /* Offset (from sp) at which lr has been saved on the stack.  */

  465.   int lr_sp_offset = 0;

  466.   /* If gr_saved[i] is non-zero, then we've noticed that general
  467.      register i has been saved at gr_sp_offset[i] from the stack
  468.      pointer.  */
  469.   char gr_saved[64];
  470.   int gr_sp_offset[64];

  471.   /* The address of the most recently scanned prologue instruction.  */
  472.   CORE_ADDR last_prologue_pc;

  473.   /* The address of the next instruction.  */
  474.   CORE_ADDR next_pc;

  475.   /* The upper bound to of the pc values to scan.  */
  476.   CORE_ADDR lim_pc;

  477.   memset (gr_saved, 0, sizeof (gr_saved));

  478.   last_prologue_pc = pc;

  479.   /* Try to compute an upper limit (on how far to scan) based on the
  480.      line number info.  */
  481.   lim_pc = skip_prologue_using_sal (gdbarch, pc);
  482.   /* If there's no line number info, lim_pc will be 0.  In that case,
  483.      set the limit to be 100 instructions away from pc.  Hopefully, this
  484.      will be far enough away to account for the entire prologue.  Don't
  485.      worry about overshooting the end of the function.  The scan loop
  486.      below contains some checks to avoid scanning unreasonably far.  */
  487.   if (lim_pc == 0)
  488.     lim_pc = pc + 400;

  489.   /* If we have a frame, we don't want to scan past the frame's pc.  This
  490.      will catch those cases where the pc is in the prologue.  */
  491.   if (this_frame)
  492.     {
  493.       CORE_ADDR frame_pc = get_frame_pc (this_frame);
  494.       if (frame_pc < lim_pc)
  495.         lim_pc = frame_pc;
  496.     }

  497.   /* Scan the prologue.  */
  498.   while (pc < lim_pc)
  499.     {
  500.       gdb_byte buf[frv_instr_size];
  501.       LONGEST op;

  502.       if (target_read_memory (pc, buf, sizeof buf) != 0)
  503.         break;
  504.       op = extract_signed_integer (buf, sizeof buf, byte_order);

  505.       next_pc = pc + 4;

  506.       /* The tests in this chain of ifs should be in order of
  507.          decreasing selectivity, so that more particular patterns get
  508.          to fire before less particular patterns.  */

  509.       /* Some sort of control transfer instruction: stop scanning prologue.
  510.          Integer Conditional Branch:
  511.           X XXXX XX 0000110 XX XXXXXXXXXXXXXXXX
  512.          Floating-point / media Conditional Branch:
  513.           X XXXX XX 0000111 XX XXXXXXXXXXXXXXXX
  514.          LCR Conditional Branch to LR
  515.           X XXXX XX 0001110 XX XX 001 X XXXXXXXXXX
  516.          Integer conditional Branches to LR
  517.           X XXXX XX 0001110 XX XX 010 X XXXXXXXXXX
  518.           X XXXX XX 0001110 XX XX 011 X XXXXXXXXXX
  519.          Floating-point/Media Branches to LR
  520.           X XXXX XX 0001110 XX XX 110 X XXXXXXXXXX
  521.           X XXXX XX 0001110 XX XX 111 X XXXXXXXXXX
  522.          Jump and Link
  523.           X XXXXX X 0001100 XXXXXX XXXXXX XXXXXX
  524.           X XXXXX X 0001101 XXXXXX XXXXXX XXXXXX
  525.          Call
  526.           X XXXXXX 0001111 XXXXXXXXXXXXXXXXXX
  527.          Return from Trap
  528.           X XXXXX X 0000101 XXXXXX XXXXXX XXXXXX
  529.          Integer Conditional Trap
  530.           X XXXX XX 0000100 XXXXXX XXXX 00 XXXXXX
  531.           X XXXX XX 0011100 XXXXXX XXXXXXXXXXXX
  532.          Floating-point /media Conditional Trap
  533.           X XXXX XX 0000100 XXXXXX XXXX 01 XXXXXX
  534.           X XXXX XX 0011101 XXXXXX XXXXXXXXXXXX
  535.          Break
  536.           X XXXX XX 0000100 XXXXXX XXXX 11 XXXXXX
  537.          Media Trap
  538.           X XXXX XX 0000100 XXXXXX XXXX 10 XXXXXX */
  539.       if ((op & 0x01d80000) == 0x00180000 /* Conditional branches and Call */
  540.           || (op & 0x01f80000) == 0x00300000  /* Jump and Link */
  541.           || (op & 0x01f80000) == 0x00100000  /* Return from Trap, Trap */
  542.           || (op & 0x01f80000) == 0x00700000) /* Trap immediate */
  543.         {
  544.           /* Stop scanning; not in prologue any longer.  */
  545.           break;
  546.         }

  547.       /* Loading something from memory into fp probably means that
  548.          we're in the epilogue.  Stop scanning the prologue.
  549.          ld @(GRi, GRk), fp
  550.          X 000010 0000010 XXXXXX 000100 XXXXXX
  551.          ldi @(GRi, d12), fp
  552.          X 000010 0110010 XXXXXX XXXXXXXXXXXX */
  553.       else if ((op & 0x7ffc0fc0) == 0x04080100
  554.                || (op & 0x7ffc0000) == 0x04c80000)
  555.         {
  556.           break;
  557.         }

  558.       /* Setting the FP from the SP:
  559.          ori sp, 0, fp
  560.          P 000010 0100010 000001 000000000000 = 0x04881000
  561.          0 111111 1111111 111111 111111111111 = 0x7fffffff
  562.              .    .   .    .   .    .   .   .
  563.          We treat this as part of the prologue.  */
  564.       else if ((op & 0x7fffffff) == 0x04881000)
  565.         {
  566.           fp_set = 1;
  567.           fp_offset = 0;
  568.           last_prologue_pc = next_pc;
  569.         }

  570.       /* Move the link register to the scratch register grJ, before saving:
  571.          movsg lr, grJ
  572.          P 000100 0000011 010000 000111 JJJJJJ = 0x080d01c0
  573.          0 111111 1111111 111111 111111 000000 = 0x7fffffc0
  574.              .    .   .    .   .    .    .   .
  575.          We treat this as part of the prologue.  */
  576.       else if ((op & 0x7fffffc0) == 0x080d01c0)
  577.         {
  578.           int gr_j = op & 0x3f;

  579.           /* If we're moving it to a scratch register, that's fine.  */
  580.           if (is_caller_saves_reg (gr_j))
  581.             {
  582.               lr_save_reg = gr_j;
  583.               last_prologue_pc = next_pc;
  584.             }
  585.         }

  586.       /* To save multiple callee-saves registers on the stack, at
  587.          offset zero:

  588.          std grK,@(sp,gr0)
  589.          P KKKKKK 0000011 000001 000011 000000 = 0x000c10c0
  590.          0 000000 1111111 111111 111111 111111 = 0x01ffffff

  591.          stq grK,@(sp,gr0)
  592.          P KKKKKK 0000011 000001 000100 000000 = 0x000c1100
  593.          0 000000 1111111 111111 111111 111111 = 0x01ffffff
  594.              .    .   .    .   .    .    .   .
  595.          We treat this as part of the prologue, and record the register's
  596.          saved address in the frame structure.  */
  597.       else if ((op & 0x01ffffff) == 0x000c10c0
  598.             || (op & 0x01ffffff) == 0x000c1100)
  599.         {
  600.           int gr_k = ((op >> 25) & 0x3f);
  601.           int ope  = ((op >> 6)  & 0x3f);
  602.           int count;
  603.           int i;

  604.           /* Is it an std or an stq?  */
  605.           if (ope == 0x03)
  606.             count = 2;
  607.           else
  608.             count = 4;

  609.           /* Is it really a callee-saves register?  */
  610.           if (is_callee_saves_reg (gr_k))
  611.             {
  612.               for (i = 0; i < count; i++)
  613.                 {
  614.                   gr_saved[gr_k + i] = 1;
  615.                   gr_sp_offset[gr_k + i] = 4 * i;
  616.                 }
  617.               last_prologue_pc = next_pc;
  618.             }
  619.         }

  620.       /* Adjusting the stack pointer.  (The stack pointer is GR1.)
  621.          addi sp, S, sp
  622.          P 000001 0010000 000001 SSSSSSSSSSSS = 0x02401000
  623.          0 111111 1111111 111111 000000000000 = 0x7ffff000
  624.              .    .   .    .   .    .   .   .
  625.          We treat this as part of the prologue.  */
  626.       else if ((op & 0x7ffff000) == 0x02401000)
  627.         {
  628.           if (framesize == 0)
  629.             {
  630.               /* Sign-extend the twelve-bit field.
  631.                  (Isn't there a better way to do this?)  */
  632.               int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;

  633.               framesize -= s;
  634.               last_prologue_pc = pc;
  635.             }
  636.           else
  637.             {
  638.               /* If the prologue is being adjusted again, we've
  639.                  likely gone too far; i.e. we're probably in the
  640.                  epilogue.  */
  641.               break;
  642.             }
  643.         }

  644.       /* Setting the FP to a constant distance from the SP:
  645.          addi sp, S, fp
  646.          P 000010 0010000 000001 SSSSSSSSSSSS = 0x04401000
  647.          0 111111 1111111 111111 000000000000 = 0x7ffff000
  648.              .    .   .    .   .    .   .   .
  649.          We treat this as part of the prologue.  */
  650.       else if ((op & 0x7ffff000) == 0x04401000)
  651.         {
  652.           /* Sign-extend the twelve-bit field.
  653.              (Isn't there a better way to do this?)  */
  654.           int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;
  655.           fp_set = 1;
  656.           fp_offset = s;
  657.           last_prologue_pc = pc;
  658.         }

  659.       /* To spill an argument register to a scratch register:
  660.             ori GRi, 0, GRk
  661.          P KKKKKK 0100010 IIIIII 000000000000 = 0x00880000
  662.          0 000000 1111111 000000 111111111111 = 0x01fc0fff
  663.              .    .   .    .   .    .   .   .
  664.          For the time being, we treat this as a prologue instruction,
  665.          assuming that GRi is an argument register.  This one's kind
  666.          of suspicious, because it seems like it could be part of a
  667.          legitimate body instruction.  But we only come here when the
  668.          source info wasn't helpful, so we have to do the best we can.
  669.          Hopefully once GCC and GDB agree on how to emit line number
  670.          info for prologues, then this code will never come into play.  */
  671.       else if ((op & 0x01fc0fff) == 0x00880000)
  672.         {
  673.           int gr_i = ((op >> 12) & 0x3f);

  674.           /* Make sure that the source is an arg register; if it is, we'll
  675.              treat it as a prologue instruction.  */
  676.           if (is_argument_reg (gr_i))
  677.             last_prologue_pc = next_pc;
  678.         }

  679.       /* To spill 16-bit values to the stack:
  680.              sthi GRk, @(fp, s)
  681.          P KKKKKK 1010001 000010 SSSSSSSSSSSS = 0x01442000
  682.          0 000000 1111111 111111 000000000000 = 0x01fff000
  683.              .    .   .    .   .    .   .   .
  684.          And for 8-bit values, we use STB instructions.
  685.              stbi GRk, @(fp, s)
  686.          P KKKKKK 1010000 000010 SSSSSSSSSSSS = 0x01402000
  687.          0 000000 1111111 111111 000000000000 = 0x01fff000
  688.              .    .   .    .   .    .   .   .
  689.          We check that GRk is really an argument register, and treat
  690.          all such as part of the prologue.  */
  691.       else if (   (op & 0x01fff000) == 0x01442000
  692.                || (op & 0x01fff000) == 0x01402000)
  693.         {
  694.           int gr_k = ((op >> 25) & 0x3f);

  695.           /* Make sure that GRk is really an argument register; treat
  696.              it as a prologue instruction if so.  */
  697.           if (is_argument_reg (gr_k))
  698.             last_prologue_pc = next_pc;
  699.         }

  700.       /* To save multiple callee-saves register on the stack, at a
  701.          non-zero offset:

  702.          stdi GRk, @(sp, s)
  703.          P KKKKKK 1010011 000001 SSSSSSSSSSSS = 0x014c1000
  704.          0 000000 1111111 111111 000000000000 = 0x01fff000
  705.              .    .   .    .   .    .   .   .
  706.          stqi GRk, @(sp, s)
  707.          P KKKKKK 1010100 000001 SSSSSSSSSSSS = 0x01501000
  708.          0 000000 1111111 111111 000000000000 = 0x01fff000
  709.              .    .   .    .   .    .   .   .
  710.          We treat this as part of the prologue, and record the register's
  711.          saved address in the frame structure.  */
  712.       else if ((op & 0x01fff000) == 0x014c1000
  713.             || (op & 0x01fff000) == 0x01501000)
  714.         {
  715.           int gr_k = ((op >> 25) & 0x3f);
  716.           int count;
  717.           int i;

  718.           /* Is it a stdi or a stqi?  */
  719.           if ((op & 0x01fff000) == 0x014c1000)
  720.             count = 2;
  721.           else
  722.             count = 4;

  723.           /* Is it really a callee-saves register?  */
  724.           if (is_callee_saves_reg (gr_k))
  725.             {
  726.               /* Sign-extend the twelve-bit field.
  727.                  (Isn't there a better way to do this?)  */
  728.               int s = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;

  729.               for (i = 0; i < count; i++)
  730.                 {
  731.                   gr_saved[gr_k + i] = 1;
  732.                   gr_sp_offset[gr_k + i] = s + (4 * i);
  733.                 }
  734.               last_prologue_pc = next_pc;
  735.             }
  736.         }

  737.       /* Storing any kind of integer register at any constant offset
  738.          from any other register.

  739.          st GRk, @(GRi, gr0)
  740.          P KKKKKK 0000011 IIIIII 000010 000000 = 0x000c0080
  741.          0 000000 1111111 000000 111111 111111 = 0x01fc0fff
  742.              .    .   .    .   .    .    .   .
  743.          sti GRk, @(GRi, d12)
  744.          P KKKKKK 1010010 IIIIII SSSSSSSSSSSS = 0x01480000
  745.          0 000000 1111111 000000 000000000000 = 0x01fc0000
  746.              .    .   .    .   .    .   .   .
  747.          These could be almost anything, but a lot of prologue
  748.          instructions fall into this pattern, so let's decode the
  749.          instruction once, and then work at a higher level.  */
  750.       else if (((op & 0x01fc0fff) == 0x000c0080)
  751.             || ((op & 0x01fc0000) == 0x01480000))
  752.         {
  753.           int gr_k = ((op >> 25) & 0x3f);
  754.           int gr_i = ((op >> 12) & 0x3f);
  755.           int offset;

  756.           /* Are we storing with gr0 as an offset, or using an
  757.              immediate value?  */
  758.           if ((op & 0x01fc0fff) == 0x000c0080)
  759.             offset = 0;
  760.           else
  761.             offset = (((op & 0xfff) - 0x800) & 0xfff) - 0x800;

  762.           /* If the address isn't relative to the SP or FP, it's not a
  763.              prologue instruction.  */
  764.           if (gr_i != sp_regnum && gr_i != fp_regnum)
  765.             {
  766.               /* Do nothing; not a prologue instruction.  */
  767.             }

  768.           /* Saving the old FP in the new frame (relative to the SP).  */
  769.           else if (gr_k == fp_regnum && gr_i == sp_regnum)
  770.             {
  771.               gr_saved[fp_regnum] = 1;
  772.               gr_sp_offset[fp_regnum] = offset;
  773.               last_prologue_pc = next_pc;
  774.             }

  775.           /* Saving callee-saves register(s) on the stack, relative to
  776.              the SP.  */
  777.           else if (gr_i == sp_regnum
  778.                    && is_callee_saves_reg (gr_k))
  779.             {
  780.               gr_saved[gr_k] = 1;
  781.               if (gr_i == sp_regnum)
  782.                 gr_sp_offset[gr_k] = offset;
  783.               else
  784.                 gr_sp_offset[gr_k] = offset + fp_offset;
  785.               last_prologue_pc = next_pc;
  786.             }

  787.           /* Saving the scratch register holding the return address.  */
  788.           else if (lr_save_reg != -1
  789.                    && gr_k == lr_save_reg)
  790.             {
  791.               lr_saved_on_stack = 1;
  792.               if (gr_i == sp_regnum)
  793.                 lr_sp_offset = offset;
  794.               else
  795.                 lr_sp_offset = offset + fp_offset;
  796.               last_prologue_pc = next_pc;
  797.             }

  798.           /* Spilling int-sized arguments to the stack.  */
  799.           else if (is_argument_reg (gr_k))
  800.             last_prologue_pc = next_pc;
  801.         }
  802.       pc = next_pc;
  803.     }

  804.   if (this_frame && info)
  805.     {
  806.       int i;
  807.       ULONGEST this_base;

  808.       /* If we know the relationship between the stack and frame
  809.          pointers, record the addresses of the registers we noticed.
  810.          Note that we have to do this as a separate step at the end,
  811.          because instructions may save relative to the SP, but we need
  812.          their addresses relative to the FP.  */
  813.       if (fp_set)
  814.         this_base = get_frame_register_unsigned (this_frame, fp_regnum);
  815.       else
  816.         this_base = get_frame_register_unsigned (this_frame, sp_regnum);

  817.       for (i = 0; i < 64; i++)
  818.         if (gr_saved[i])
  819.           info->saved_regs[i].addr = this_base - fp_offset + gr_sp_offset[i];

  820.       info->prev_sp = this_base - fp_offset + framesize;
  821.       info->base = this_base;

  822.       /* If LR was saved on the stack, record its location.  */
  823.       if (lr_saved_on_stack)
  824.         info->saved_regs[lr_regnum].addr
  825.           = this_base - fp_offset + lr_sp_offset;

  826.       /* The call instruction moves the caller's PC in the callee's LR.
  827.          Since this is an unwind, do the reverse.  Copy the location of LR
  828.          into PC (the address / regnum) so that a request for PC will be
  829.          converted into a request for the LR.  */
  830.       info->saved_regs[pc_regnum] = info->saved_regs[lr_regnum];

  831.       /* Save the previous frame's computed SP value.  */
  832.       trad_frame_set_value (info->saved_regs, sp_regnum, info->prev_sp);
  833.     }

  834.   return last_prologue_pc;
  835. }


  836. static CORE_ADDR
  837. frv_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  838. {
  839.   CORE_ADDR func_addr, func_end, new_pc;

  840.   new_pc = pc;

  841.   /* If the line table has entry for a line *within* the function
  842.      (i.e., not in the prologue, and not past the end), then that's
  843.      our location.  */
  844.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  845.     {
  846.       struct symtab_and_line sal;

  847.       sal = find_pc_line (func_addr, 0);

  848.       if (sal.line != 0 && sal.end < func_end)
  849.         {
  850.           new_pc = sal.end;
  851.         }
  852.     }

  853.   /* The FR-V prologue is at least five instructions long (twenty bytes).
  854.      If we didn't find a real source location past that, then
  855.      do a full analysis of the prologue.  */
  856.   if (new_pc < pc + 20)
  857.     new_pc = frv_analyze_prologue (gdbarch, pc, 0, 0);

  858.   return new_pc;
  859. }


  860. /* Examine the instruction pointed to by PC.  If it corresponds to
  861.    a call to __main, return the address of the next instruction.
  862.    Otherwise, return PC.  */

  863. static CORE_ADDR
  864. frv_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  865. {
  866.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  867.   gdb_byte buf[4];
  868.   unsigned long op;
  869.   CORE_ADDR orig_pc = pc;

  870.   if (target_read_memory (pc, buf, 4))
  871.     return pc;
  872.   op = extract_unsigned_integer (buf, 4, byte_order);

  873.   /* In PIC code, GR15 may be loaded from some offset off of FP prior
  874.      to the call instruction.

  875.      Skip over this instruction if present.  It won't be present in
  876.      non-PIC code, and even in PIC code, it might not be present.
  877.      (This is due to the fact that GR15, the FDPIC register, already
  878.      contains the correct value.)

  879.      The general form of the LDI is given first, followed by the
  880.      specific instruction with the GRi and GRk filled in as FP and
  881.      GR15.

  882.      ldi @(GRi, d12), GRk
  883.      P KKKKKK 0110010 IIIIII SSSSSSSSSSSS = 0x00c80000
  884.      0 000000 1111111 000000 000000000000 = 0x01fc0000
  885.          .    .   .    .   .    .   .   .
  886.      ldi @(FP, d12), GR15
  887.      P KKKKKK 0110010 IIIIII SSSSSSSSSSSS = 0x1ec82000
  888.      0 001111 1111111 000010 000000000000 = 0x7ffff000
  889.          .    .   .    .   .    .   .   .               */

  890.   if ((op & 0x7ffff000) == 0x1ec82000)
  891.     {
  892.       pc += 4;
  893.       if (target_read_memory (pc, buf, 4))
  894.         return orig_pc;
  895.       op = extract_unsigned_integer (buf, 4, byte_order);
  896.     }

  897.   /* The format of an FRV CALL instruction is as follows:

  898.      call label24
  899.      P HHHHHH 0001111 LLLLLLLLLLLLLLLLLL = 0x003c0000
  900.      0 000000 1111111 000000000000000000 = 0x01fc0000
  901.          .    .   .    .   .   .   .   .

  902.      where label24 is constructed by concatenating the H bits with the
  903.      L bits.  The call target is PC + (4 * sign_ext(label24)).  */

  904.   if ((op & 0x01fc0000) == 0x003c0000)
  905.     {
  906.       LONGEST displ;
  907.       CORE_ADDR call_dest;
  908.       struct bound_minimal_symbol s;

  909.       displ = ((op & 0xfe000000) >> 7) | (op & 0x0003ffff);
  910.       if ((displ & 0x00800000) != 0)
  911.         displ |= ~((LONGEST) 0x00ffffff);

  912.       call_dest = pc + 4 * displ;
  913.       s = lookup_minimal_symbol_by_pc (call_dest);

  914.       if (s.minsym != NULL
  915.           && MSYMBOL_LINKAGE_NAME (s.minsym) != NULL
  916.           && strcmp (MSYMBOL_LINKAGE_NAME (s.minsym), "__main") == 0)
  917.         {
  918.           pc += 4;
  919.           return pc;
  920.         }
  921.     }
  922.   return orig_pc;
  923. }


  924. static struct frv_unwind_cache *
  925. frv_frame_unwind_cache (struct frame_info *this_frame,
  926.                          void **this_prologue_cache)
  927. {
  928.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  929.   struct frv_unwind_cache *info;

  930.   if ((*this_prologue_cache))
  931.     return (*this_prologue_cache);

  932.   info = FRAME_OBSTACK_ZALLOC (struct frv_unwind_cache);
  933.   (*this_prologue_cache) = info;
  934.   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  935.   /* Prologue analysis does the rest...  */
  936.   frv_analyze_prologue (gdbarch,
  937.                         get_frame_func (this_frame), this_frame, info);

  938.   return info;
  939. }

  940. static void
  941. frv_extract_return_value (struct type *type, struct regcache *regcache,
  942.                           gdb_byte *valbuf)
  943. {
  944.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  945.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  946.   int len = TYPE_LENGTH (type);

  947.   if (len <= 4)
  948.     {
  949.       ULONGEST gpr8_val;
  950.       regcache_cooked_read_unsigned (regcache, 8, &gpr8_val);
  951.       store_unsigned_integer (valbuf, len, byte_order, gpr8_val);
  952.     }
  953.   else if (len == 8)
  954.     {
  955.       ULONGEST regval;

  956.       regcache_cooked_read_unsigned (regcache, 8, &regval);
  957.       store_unsigned_integer (valbuf, 4, byte_order, regval);
  958.       regcache_cooked_read_unsigned (regcache, 9, &regval);
  959.       store_unsigned_integer ((bfd_byte *) valbuf + 4, 4, byte_order, regval);
  960.     }
  961.   else
  962.     internal_error (__FILE__, __LINE__,
  963.                     _("Illegal return value length: %d"), len);
  964. }

  965. static CORE_ADDR
  966. frv_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  967. {
  968.   /* Require dword alignment.  */
  969.   return align_down (sp, 8);
  970. }

  971. static CORE_ADDR
  972. find_func_descr (struct gdbarch *gdbarch, CORE_ADDR entry_point)
  973. {
  974.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  975.   CORE_ADDR descr;
  976.   gdb_byte valbuf[4];
  977.   CORE_ADDR start_addr;

  978.   /* If we can't find the function in the symbol table, then we assume
  979.      that the function address is already in descriptor form.  */
  980.   if (!find_pc_partial_function (entry_point, NULL, &start_addr, NULL)
  981.       || entry_point != start_addr)
  982.     return entry_point;

  983.   descr = frv_fdpic_find_canonical_descriptor (entry_point);

  984.   if (descr != 0)
  985.     return descr;

  986.   /* Construct a non-canonical descriptor from space allocated on
  987.      the stack.  */

  988.   descr = value_as_long (value_allocate_space_in_inferior (8));
  989.   store_unsigned_integer (valbuf, 4, byte_order, entry_point);
  990.   write_memory (descr, valbuf, 4);
  991.   store_unsigned_integer (valbuf, 4, byte_order,
  992.                           frv_fdpic_find_global_pointer (entry_point));
  993.   write_memory (descr + 4, valbuf, 4);
  994.   return descr;
  995. }

  996. static CORE_ADDR
  997. frv_convert_from_func_ptr_addr (struct gdbarch *gdbarch, CORE_ADDR addr,
  998.                                 struct target_ops *targ)
  999. {
  1000.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1001.   CORE_ADDR entry_point;
  1002.   CORE_ADDR got_address;

  1003.   entry_point = get_target_memory_unsigned (targ, addr, 4, byte_order);
  1004.   got_address = get_target_memory_unsigned (targ, addr + 4, 4, byte_order);

  1005.   if (got_address == frv_fdpic_find_global_pointer (entry_point))
  1006.     return entry_point;
  1007.   else
  1008.     return addr;
  1009. }

  1010. static CORE_ADDR
  1011. frv_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1012.                      struct regcache *regcache, CORE_ADDR bp_addr,
  1013.                      int nargs, struct value **args, CORE_ADDR sp,
  1014.                      int struct_return, CORE_ADDR struct_addr)
  1015. {
  1016.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1017.   int argreg;
  1018.   int argnum;
  1019.   const gdb_byte *val;
  1020.   gdb_byte valbuf[4];
  1021.   struct value *arg;
  1022.   struct type *arg_type;
  1023.   int len;
  1024.   enum type_code typecode;
  1025.   CORE_ADDR regval;
  1026.   int stack_space;
  1027.   int stack_offset;
  1028.   enum frv_abi abi = frv_abi (gdbarch);
  1029.   CORE_ADDR func_addr = find_function_addr (function, NULL);

  1030. #if 0
  1031.   printf("Push %d args at sp = %x, struct_return=%d (%x)\n",
  1032.          nargs, (int) sp, struct_return, struct_addr);
  1033. #endif

  1034.   stack_space = 0;
  1035.   for (argnum = 0; argnum < nargs; ++argnum)
  1036.     stack_space += align_up (TYPE_LENGTH (value_type (args[argnum])), 4);

  1037.   stack_space -= (6 * 4);
  1038.   if (stack_space > 0)
  1039.     sp -= stack_space;

  1040.   /* Make sure stack is dword aligned.  */
  1041.   sp = align_down (sp, 8);

  1042.   stack_offset = 0;

  1043.   argreg = 8;

  1044.   if (struct_return)
  1045.     regcache_cooked_write_unsigned (regcache, struct_return_regnum,
  1046.                                     struct_addr);

  1047.   for (argnum = 0; argnum < nargs; ++argnum)
  1048.     {
  1049.       arg = args[argnum];
  1050.       arg_type = check_typedef (value_type (arg));
  1051.       len = TYPE_LENGTH (arg_type);
  1052.       typecode = TYPE_CODE (arg_type);

  1053.       if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
  1054.         {
  1055.           store_unsigned_integer (valbuf, 4, byte_order,
  1056.                                   value_address (arg));
  1057.           typecode = TYPE_CODE_PTR;
  1058.           len = 4;
  1059.           val = valbuf;
  1060.         }
  1061.       else if (abi == FRV_ABI_FDPIC
  1062.                && len == 4
  1063.                && typecode == TYPE_CODE_PTR
  1064.                && TYPE_CODE (TYPE_TARGET_TYPE (arg_type)) == TYPE_CODE_FUNC)
  1065.         {
  1066.           /* The FDPIC ABI requires function descriptors to be passed instead
  1067.              of entry points.  */
  1068.           CORE_ADDR addr = extract_unsigned_integer
  1069.                              (value_contents (arg), 4, byte_order);
  1070.           addr = find_func_descr (gdbarch, addr);
  1071.           store_unsigned_integer (valbuf, 4, byte_order, addr);
  1072.           typecode = TYPE_CODE_PTR;
  1073.           len = 4;
  1074.           val = valbuf;
  1075.         }
  1076.       else
  1077.         {
  1078.           val = value_contents (arg);
  1079.         }

  1080.       while (len > 0)
  1081.         {
  1082.           int partial_len = (len < 4 ? len : 4);

  1083.           if (argreg < 14)
  1084.             {
  1085.               regval = extract_unsigned_integer (val, partial_len, byte_order);
  1086. #if 0
  1087.               printf("  Argnum %d data %x -> reg %d\n",
  1088.                      argnum, (int) regval, argreg);
  1089. #endif
  1090.               regcache_cooked_write_unsigned (regcache, argreg, regval);
  1091.               ++argreg;
  1092.             }
  1093.           else
  1094.             {
  1095. #if 0
  1096.               printf("  Argnum %d data %x -> offset %d (%x)\n",
  1097.                      argnum, *((int *)val), stack_offset,
  1098.                      (int) (sp + stack_offset));
  1099. #endif
  1100.               write_memory (sp + stack_offset, val, partial_len);
  1101.               stack_offset += align_up (partial_len, 4);
  1102.             }
  1103.           len -= partial_len;
  1104.           val += partial_len;
  1105.         }
  1106.     }

  1107.   /* Set the return address.  For the frv, the return breakpoint is
  1108.      always at BP_ADDR.  */
  1109.   regcache_cooked_write_unsigned (regcache, lr_regnum, bp_addr);

  1110.   if (abi == FRV_ABI_FDPIC)
  1111.     {
  1112.       /* Set the GOT register for the FDPIC ABI.  */
  1113.       regcache_cooked_write_unsigned
  1114.         (regcache, first_gpr_regnum + 15,
  1115.          frv_fdpic_find_global_pointer (func_addr));
  1116.     }

  1117.   /* Finally, update the SP register.  */
  1118.   regcache_cooked_write_unsigned (regcache, sp_regnum, sp);

  1119.   return sp;
  1120. }

  1121. static void
  1122. frv_store_return_value (struct type *type, struct regcache *regcache,
  1123.                         const gdb_byte *valbuf)
  1124. {
  1125.   int len = TYPE_LENGTH (type);

  1126.   if (len <= 4)
  1127.     {
  1128.       bfd_byte val[4];
  1129.       memset (val, 0, sizeof (val));
  1130.       memcpy (val + (4 - len), valbuf, len);
  1131.       regcache_cooked_write (regcache, 8, val);
  1132.     }
  1133.   else if (len == 8)
  1134.     {
  1135.       regcache_cooked_write (regcache, 8, valbuf);
  1136.       regcache_cooked_write (regcache, 9, (bfd_byte *) valbuf + 4);
  1137.     }
  1138.   else
  1139.     internal_error (__FILE__, __LINE__,
  1140.                     _("Don't know how to return a %d-byte value."), len);
  1141. }

  1142. static enum return_value_convention
  1143. frv_return_value (struct gdbarch *gdbarch, struct value *function,
  1144.                   struct type *valtype, struct regcache *regcache,
  1145.                   gdb_byte *readbuf, const gdb_byte *writebuf)
  1146. {
  1147.   int struct_return = TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  1148.                       || TYPE_CODE (valtype) == TYPE_CODE_UNION
  1149.                       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY;

  1150.   if (writebuf != NULL)
  1151.     {
  1152.       gdb_assert (!struct_return);
  1153.       frv_store_return_value (valtype, regcache, writebuf);
  1154.     }

  1155.   if (readbuf != NULL)
  1156.     {
  1157.       gdb_assert (!struct_return);
  1158.       frv_extract_return_value (valtype, regcache, readbuf);
  1159.     }

  1160.   if (struct_return)
  1161.     return RETURN_VALUE_STRUCT_CONVENTION;
  1162.   else
  1163.     return RETURN_VALUE_REGISTER_CONVENTION;
  1164. }

  1165. static CORE_ADDR
  1166. frv_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1167. {
  1168.   return frame_unwind_register_unsigned (next_frame, pc_regnum);
  1169. }

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

  1172. static void
  1173. frv_frame_this_id (struct frame_info *this_frame,
  1174.                     void **this_prologue_cache, struct frame_id *this_id)
  1175. {
  1176.   struct frv_unwind_cache *info
  1177.     = frv_frame_unwind_cache (this_frame, this_prologue_cache);
  1178.   CORE_ADDR base;
  1179.   CORE_ADDR func;
  1180.   struct bound_minimal_symbol msym_stack;
  1181.   struct frame_id id;

  1182.   /* The FUNC is easy.  */
  1183.   func = get_frame_func (this_frame);

  1184.   /* Check if the stack is empty.  */
  1185.   msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
  1186.   if (msym_stack.minsym && info->base == BMSYMBOL_VALUE_ADDRESS (msym_stack))
  1187.     return;

  1188.   /* Hopefully the prologue analysis either correctly determined the
  1189.      frame's base (which is the SP from the previous frame), or set
  1190.      that base to "NULL".  */
  1191.   base = info->prev_sp;
  1192.   if (base == 0)
  1193.     return;

  1194.   id = frame_id_build (base, func);
  1195.   (*this_id) = id;
  1196. }

  1197. static struct value *
  1198. frv_frame_prev_register (struct frame_info *this_frame,
  1199.                          void **this_prologue_cache, int regnum)
  1200. {
  1201.   struct frv_unwind_cache *info
  1202.     = frv_frame_unwind_cache (this_frame, this_prologue_cache);
  1203.   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  1204. }

  1205. static const struct frame_unwind frv_frame_unwind = {
  1206.   NORMAL_FRAME,
  1207.   default_frame_unwind_stop_reason,
  1208.   frv_frame_this_id,
  1209.   frv_frame_prev_register,
  1210.   NULL,
  1211.   default_frame_sniffer
  1212. };

  1213. static CORE_ADDR
  1214. frv_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1215. {
  1216.   struct frv_unwind_cache *info
  1217.     = frv_frame_unwind_cache (this_frame, this_cache);
  1218.   return info->base;
  1219. }

  1220. static const struct frame_base frv_frame_base = {
  1221.   &frv_frame_unwind,
  1222.   frv_frame_base_address,
  1223.   frv_frame_base_address,
  1224.   frv_frame_base_address
  1225. };

  1226. static CORE_ADDR
  1227. frv_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1228. {
  1229.   return frame_unwind_register_unsigned (next_frame, sp_regnum);
  1230. }


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

  1234. static struct frame_id
  1235. frv_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1236. {
  1237.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, sp_regnum);
  1238.   return frame_id_build (sp, get_frame_pc (this_frame));
  1239. }

  1240. static struct gdbarch *
  1241. frv_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1242. {
  1243.   struct gdbarch *gdbarch;
  1244.   struct gdbarch_tdep *var;
  1245.   int elf_flags = 0;

  1246.   /* Check to see if we've already built an appropriate architecture
  1247.      object for this executable.  */
  1248.   arches = gdbarch_list_lookup_by_info (arches, &info);
  1249.   if (arches)
  1250.     return arches->gdbarch;

  1251.   /* Select the right tdep structure for this variant.  */
  1252.   var = new_variant ();
  1253.   switch (info.bfd_arch_info->mach)
  1254.     {
  1255.     case bfd_mach_frv:
  1256.     case bfd_mach_frvsimple:
  1257.     case bfd_mach_fr500:
  1258.     case bfd_mach_frvtomcat:
  1259.     case bfd_mach_fr550:
  1260.       set_variant_num_gprs (var, 64);
  1261.       set_variant_num_fprs (var, 64);
  1262.       break;

  1263.     case bfd_mach_fr400:
  1264.     case bfd_mach_fr450:
  1265.       set_variant_num_gprs (var, 32);
  1266.       set_variant_num_fprs (var, 32);
  1267.       break;

  1268.     default:
  1269.       /* Never heard of this variant.  */
  1270.       return 0;
  1271.     }

  1272.   /* Extract the ELF flags, if available.  */
  1273.   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1274.     elf_flags = elf_elfheader (info.abfd)->e_flags;

  1275.   if (elf_flags & EF_FRV_FDPIC)
  1276.     set_variant_abi_fdpic (var);

  1277.   if (elf_flags & EF_FRV_CPU_FR450)
  1278.     set_variant_scratch_registers (var);

  1279.   gdbarch = gdbarch_alloc (&info, var);

  1280.   set_gdbarch_short_bit (gdbarch, 16);
  1281.   set_gdbarch_int_bit (gdbarch, 32);
  1282.   set_gdbarch_long_bit (gdbarch, 32);
  1283.   set_gdbarch_long_long_bit (gdbarch, 64);
  1284.   set_gdbarch_float_bit (gdbarch, 32);
  1285.   set_gdbarch_double_bit (gdbarch, 64);
  1286.   set_gdbarch_long_double_bit (gdbarch, 64);
  1287.   set_gdbarch_ptr_bit (gdbarch, 32);

  1288.   set_gdbarch_num_regs (gdbarch, frv_num_regs);
  1289.   set_gdbarch_num_pseudo_regs (gdbarch, frv_num_pseudo_regs);

  1290.   set_gdbarch_sp_regnum (gdbarch, sp_regnum);
  1291.   set_gdbarch_deprecated_fp_regnum (gdbarch, fp_regnum);
  1292.   set_gdbarch_pc_regnum (gdbarch, pc_regnum);

  1293.   set_gdbarch_register_name (gdbarch, frv_register_name);
  1294.   set_gdbarch_register_type (gdbarch, frv_register_type);
  1295.   set_gdbarch_register_sim_regno (gdbarch, frv_register_sim_regno);

  1296.   set_gdbarch_pseudo_register_read (gdbarch, frv_pseudo_register_read);
  1297.   set_gdbarch_pseudo_register_write (gdbarch, frv_pseudo_register_write);

  1298.   set_gdbarch_skip_prologue (gdbarch, frv_skip_prologue);
  1299.   set_gdbarch_skip_main_prologue (gdbarch, frv_skip_main_prologue);
  1300.   set_gdbarch_breakpoint_from_pc (gdbarch, frv_breakpoint_from_pc);
  1301.   set_gdbarch_adjust_breakpoint_address
  1302.     (gdbarch, frv_adjust_breakpoint_address);

  1303.   set_gdbarch_return_value (gdbarch, frv_return_value);

  1304.   /* Frame stuff.  */
  1305.   set_gdbarch_unwind_pc (gdbarch, frv_unwind_pc);
  1306.   set_gdbarch_unwind_sp (gdbarch, frv_unwind_sp);
  1307.   set_gdbarch_frame_align (gdbarch, frv_frame_align);
  1308.   frame_base_set_default (gdbarch, &frv_frame_base);
  1309.   /* We set the sniffer lower down after the OSABI hooks have been
  1310.      established.  */

  1311.   /* Settings for calling functions in the inferior.  */
  1312.   set_gdbarch_push_dummy_call (gdbarch, frv_push_dummy_call);
  1313.   set_gdbarch_dummy_id (gdbarch, frv_dummy_id);

  1314.   /* Settings that should be unnecessary.  */
  1315.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  1316.   /* Hardware watchpoint / breakpoint support.  */
  1317.   switch (info.bfd_arch_info->mach)
  1318.     {
  1319.     case bfd_mach_frv:
  1320.     case bfd_mach_frvsimple:
  1321.     case bfd_mach_fr500:
  1322.     case bfd_mach_frvtomcat:
  1323.       /* fr500-style hardware debugging support.  */
  1324.       var->num_hw_watchpoints = 4;
  1325.       var->num_hw_breakpoints = 4;
  1326.       break;

  1327.     case bfd_mach_fr400:
  1328.     case bfd_mach_fr450:
  1329.       /* fr400-style hardware debugging support.  */
  1330.       var->num_hw_watchpoints = 2;
  1331.       var->num_hw_breakpoints = 4;
  1332.       break;

  1333.     default:
  1334.       /* Otherwise, assume we don't have hardware debugging support.  */
  1335.       var->num_hw_watchpoints = 0;
  1336.       var->num_hw_breakpoints = 0;
  1337.       break;
  1338.     }

  1339.   set_gdbarch_print_insn (gdbarch, print_insn_frv);
  1340.   if (frv_abi (gdbarch) == FRV_ABI_FDPIC)
  1341.     set_gdbarch_convert_from_func_ptr_addr (gdbarch,
  1342.                                             frv_convert_from_func_ptr_addr);

  1343.   set_solib_ops (gdbarch, &frv_so_ops);

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

  1346.   /* Set the fallback (prologue based) frame sniffer.  */
  1347.   frame_unwind_append_unwinder (gdbarch, &frv_frame_unwind);

  1348.   /* Enable TLS support.  */
  1349.   set_gdbarch_fetch_tls_load_module_address (gdbarch,
  1350.                                              frv_fetch_objfile_link_map);

  1351.   return gdbarch;
  1352. }

  1353. void
  1354. _initialize_frv_tdep (void)
  1355. {
  1356.   register_gdbarch_init (bfd_arch_frv, frv_gdbarch_init);
  1357. }