gdb/mep-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the Toshiba MeP for GDB, the GNU debugger.

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

  3.    Contributed by Red Hat, Inc.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "frame.h"
  17. #include "frame-unwind.h"
  18. #include "frame-base.h"
  19. #include "symtab.h"
  20. #include "gdbtypes.h"
  21. #include "gdbcmd.h"
  22. #include "gdbcore.h"
  23. #include "value.h"
  24. #include "inferior.h"
  25. #include "dis-asm.h"
  26. #include "symfile.h"
  27. #include "objfiles.h"
  28. #include "language.h"
  29. #include "arch-utils.h"
  30. #include "regcache.h"
  31. #include "remote.h"
  32. #include "floatformat.h"
  33. #include "sim-regno.h"
  34. #include "disasm.h"
  35. #include "trad-frame.h"
  36. #include "reggroups.h"
  37. #include "elf-bfd.h"
  38. #include "elf/mep.h"
  39. #include "prologue-value.h"
  40. #include "cgen/bitset.h"
  41. #include "infcall.h"

  42. /* Get the user's customized MeP coprocessor register names from
  43.    libopcodes.  */
  44. #include "opcodes/mep-desc.h"
  45. #include "opcodes/mep-opc.h"


  46. /* The gdbarch_tdep structure.  */

  47. /* A quick recap for GDB hackers not familiar with the whole Toshiba
  48.    Media Processor story:

  49.    The MeP media engine is a configureable processor: users can design
  50.    their own coprocessors, implement custom instructions, adjust cache
  51.    sizes, select optional standard facilities like add-and-saturate
  52.    instructions, and so on.  Then, they can build custom versions of
  53.    the GNU toolchain to support their customized chips.  The
  54.    MeP-Integrator program (see utils/mep) takes a GNU toolchain source
  55.    tree, and a config file pointing to various files provided by the
  56.    user describing their customizations, and edits the source tree to
  57.    produce a compiler that can generate their custom instructions, an
  58.    assembler that can assemble them and recognize their custom
  59.    register names, and so on.

  60.    Furthermore, the user can actually specify several of these custom
  61.    configurations, called 'me_modules', and get a toolchain which can
  62.    produce code for any of them, given a compiler/assembler switch;
  63.    you say something like 'gcc -mconfig=mm_max' to generate code for
  64.    the me_module named 'mm_max'.

  65.    GDB, in particular, needs to:

  66.    - use the coprocessor control register names provided by the user
  67.      in their hardware description, in expressions, 'info register'
  68.      output, and disassembly,

  69.    - know the number, names, and types of the coprocessor's
  70.      general-purpose registers, adjust the 'info all-registers' output
  71.      accordingly, and print error messages if the user refers to one
  72.      that doesn't exist

  73.    - allow access to the control bus space only when the configuration
  74.      actually has a control bus, and recognize which regions of the
  75.      control bus space are actually populated,

  76.    - disassemble using the user's provided mnemonics for their custom
  77.      instructions, and

  78.    - recognize whether the $hi and $lo registers are present, and
  79.      allow access to them only when they are actually there.

  80.    There are three sources of information about what sort of me_module
  81.    we're actually dealing with:

  82.    - A MeP executable file indicates which me_module it was compiled
  83.      for, and libopcodes has tables describing each module.  So, given
  84.      an executable file, we can find out about the processor it was
  85.      compiled for.

  86.    - There are SID command-line options to select a particular
  87.      me_module, overriding the one specified in the ELF file.  SID
  88.      provides GDB with a fake read-only register, 'module', which
  89.      indicates which me_module GDB is communicating with an instance
  90.      of.

  91.    - There are SID command-line options to enable or disable certain
  92.      optional processor features, overriding the defaults for the
  93.      selected me_module.  The MeP $OPT register indicates which
  94.      options are present on the current processor.  */


  95. struct gdbarch_tdep
  96. {
  97.   /* A CGEN cpu descriptor for this BFD architecture and machine.

  98.      Note: this is *not* customized for any particular me_module; the
  99.      MeP libopcodes machinery actually puts off module-specific
  100.      customization until the last minute.  So this contains
  101.      information about all supported me_modules.  */
  102.   CGEN_CPU_DESC cpu_desc;

  103.   /* The me_module index from the ELF file we used to select this
  104.      architecture, or CONFIG_NONE if there was none.

  105.      Note that we should prefer to use the me_module number available
  106.      via the 'module' register, whenever we're actually talking to a
  107.      real target.

  108.      In the absence of live information, we'd like to get the
  109.      me_module number from the ELF file.  But which ELF file: the
  110.      executable file, the core file, ... ?  The answer is, "the last
  111.      ELF file we used to set the current architecture".  Thus, we
  112.      create a separate instance of the gdbarch structure for each
  113.      me_module value mep_gdbarch_init sees, and store the me_module
  114.      value from the ELF file here.  */
  115.   CONFIG_ATTR me_module;
  116. };



  117. /* Getting me_module information from the CGEN tables.  */


  118. /* Find an entry in the DESC's hardware table whose name begins with
  119.    PREFIX, and whose ISA mask intersects COPRO_ISA_MASK, but does not
  120.    intersect with GENERIC_ISA_MASK.  If there is no matching entry,
  121.    return zero.  */
  122. static const CGEN_HW_ENTRY *
  123. find_hw_entry_by_prefix_and_isa (CGEN_CPU_DESC desc,
  124.                                  const char *prefix,
  125.                                  CGEN_BITSET *copro_isa_mask,
  126.                                  CGEN_BITSET *generic_isa_mask)
  127. {
  128.   int prefix_len = strlen (prefix);
  129.   int i;

  130.   for (i = 0; i < desc->hw_table.num_entries; i++)
  131.     {
  132.       const CGEN_HW_ENTRY *hw = desc->hw_table.entries[i];
  133.       if (strncmp (prefix, hw->name, prefix_len) == 0)
  134.         {
  135.           CGEN_BITSET *hw_isa_mask
  136.             = ((CGEN_BITSET *)
  137.                &CGEN_ATTR_CGEN_HW_ISA_VALUE (CGEN_HW_ATTRS (hw)));

  138.           if (cgen_bitset_intersect_p (hw_isa_mask, copro_isa_mask)
  139.               && ! cgen_bitset_intersect_p (hw_isa_mask, generic_isa_mask))
  140.             return hw;
  141.         }
  142.     }

  143.   return 0;
  144. }


  145. /* Find an entry in DESC's hardware table whose type is TYPE.  Return
  146.    zero if there is none.  */
  147. static const CGEN_HW_ENTRY *
  148. find_hw_entry_by_type (CGEN_CPU_DESC desc, CGEN_HW_TYPE type)
  149. {
  150.   int i;

  151.   for (i = 0; i < desc->hw_table.num_entries; i++)
  152.     {
  153.       const CGEN_HW_ENTRY *hw = desc->hw_table.entries[i];

  154.       if (hw->type == type)
  155.         return hw;
  156.     }

  157.   return 0;
  158. }


  159. /* Return the CGEN hardware table entry for the coprocessor register
  160.    set for ME_MODULE, whose name prefix is PREFIX.  If ME_MODULE has
  161.    no such register set, return zero.  If ME_MODULE is the generic
  162.    me_module CONFIG_NONE, return the table entry for the register set
  163.    whose hardware type is GENERIC_TYPE.  */
  164. static const CGEN_HW_ENTRY *
  165. me_module_register_set (CONFIG_ATTR me_module,
  166.                         const char *prefix,
  167.                         CGEN_HW_TYPE generic_type)
  168. {
  169.   /* This is kind of tricky, because the hardware table is constructed
  170.      in a way that isn't very helpful.  Perhaps we can fix that, but
  171.      here's how it works at the moment:

  172.      The configuration map, `mep_config_map', is indexed by me_module
  173.      number, and indicates which coprocessor and core ISAs that
  174.      me_module supports.  The 'core_isa' mask includes all the core
  175.      ISAs, and the 'cop_isa' mask includes all the coprocessor ISAs.
  176.      The entry for the generic me_module, CONFIG_NONE, has an empty
  177.      'cop_isa', and its 'core_isa' selects only the standard MeP
  178.      instruction set.

  179.      The CGEN CPU descriptor's hardware table, desc->hw_table, has
  180.      entries for all the register sets, for all me_modules.  Each
  181.      entry has a mask indicating which ISAs use that register set.
  182.      So, if an me_module supports some coprocessor ISA, we can find
  183.      applicable register sets by scanning the hardware table for
  184.      register sets whose masks include (at least some of) those ISAs.

  185.      Each hardware table entry also has a name, whose prefix says
  186.      whether it's a general-purpose ("h-cr") or control ("h-ccr")
  187.      coprocessor register set.  It might be nicer to have an attribute
  188.      indicating what sort of register set it was, that we could use
  189.      instead of pattern-matching on the name.

  190.      When there is no hardware table entry whose mask includes a
  191.      particular coprocessor ISA and whose name starts with a given
  192.      prefix, then that means that that coprocessor doesn't have any
  193.      registers of that type.  In such cases, this function must return
  194.      a null pointer.

  195.      Coprocessor register sets' masks may or may not include the core
  196.      ISA for the me_module they belong to.  Those generated by a2cgen
  197.      do, but the sample me_module included in the unconfigured tree,
  198.      'ccfx', does not.

  199.      There are generic coprocessor register sets, intended only for
  200.      use with the generic me_module.  Unfortunately, their masks
  201.      include *all* ISAs --- even those for coprocessors that don't
  202.      have such register sets.  This makes detecting the case where a
  203.      coprocessor lacks a particular register set more complicated.

  204.      So, here's the approach we take:

  205.      - For CONFIG_NONE, we return the generic coprocessor register set.

  206.      - For any other me_module, we search for a register set whose
  207.        mask contains any of the me_module's coprocessor ISAs,
  208.        specifically excluding the generic coprocessor register sets.  */

  209.   CGEN_CPU_DESC desc = gdbarch_tdep (target_gdbarch ())->cpu_desc;
  210.   const CGEN_HW_ENTRY *hw;

  211.   if (me_module == CONFIG_NONE)
  212.     hw = find_hw_entry_by_type (desc, generic_type);
  213.   else
  214.     {
  215.       CGEN_BITSET *cop = &mep_config_map[me_module].cop_isa;
  216.       CGEN_BITSET *core = &mep_config_map[me_module].core_isa;
  217.       CGEN_BITSET *generic = &mep_config_map[CONFIG_NONE].core_isa;
  218.       CGEN_BITSET *cop_and_core;

  219.       /* The coprocessor ISAs include the ISA for the specific core which
  220.          has that coprocessor.  */
  221.       cop_and_core = cgen_bitset_copy (cop);
  222.       cgen_bitset_union (cop, core, cop_and_core);
  223.       hw = find_hw_entry_by_prefix_and_isa (desc, prefix, cop_and_core, generic);
  224.     }

  225.   return hw;
  226. }


  227. /* Given a hardware table entry HW representing a register set, return
  228.    a pointer to the keyword table with all the register names.  If HW
  229.    is NULL, return NULL, to propage the "no such register set" info
  230.    along.  */
  231. static CGEN_KEYWORD *
  232. register_set_keyword_table (const CGEN_HW_ENTRY *hw)
  233. {
  234.   if (! hw)
  235.     return NULL;

  236.   /* Check that HW is actually a keyword table.  */
  237.   gdb_assert (hw->asm_type == CGEN_ASM_KEYWORD);

  238.   /* The 'asm_data' field of a register set's hardware table entry
  239.      refers to a keyword table.  */
  240.   return (CGEN_KEYWORD *) hw->asm_data;
  241. }


  242. /* Given a keyword table KEYWORD and a register number REGNUM, return
  243.    the name of the register, or "" if KEYWORD contains no register
  244.    whose number is REGNUM.  */
  245. static char *
  246. register_name_from_keyword (CGEN_KEYWORD *keyword_table, int regnum)
  247. {
  248.   const CGEN_KEYWORD_ENTRY *entry
  249.     = cgen_keyword_lookup_value (keyword_table, regnum);

  250.   if (entry)
  251.     {
  252.       char *name = entry->name;

  253.       /* The CGEN keyword entries for register names include the
  254.          leading $, which appears in MeP assembly as well as in GDB.
  255.          But we don't want to return that; GDB core code adds that
  256.          itself.  */
  257.       if (name[0] == '$')
  258.         name++;

  259.       return name;
  260.     }
  261.   else
  262.     return "";
  263. }


  264. /* Masks for option bits in the OPT special-purpose register.  */
  265. enum {
  266.   MEP_OPT_DIV = 1 << 25,        /* 32-bit divide instruction option */
  267.   MEP_OPT_MUL = 1 << 24,        /* 32-bit multiply instruction option */
  268.   MEP_OPT_BIT = 1 << 23,        /* bit manipulation instruction option */
  269.   MEP_OPT_SAT = 1 << 22,        /* saturation instruction option */
  270.   MEP_OPT_CLP = 1 << 21,        /* clip instruction option */
  271.   MEP_OPT_MIN = 1 << 20,        /* min/max instruction option */
  272.   MEP_OPT_AVE = 1 << 19,        /* average instruction option */
  273.   MEP_OPT_ABS = 1 << 18,        /* absolute difference instruction option */
  274.   MEP_OPT_LDZ = 1 << 16,        /* leading zero instruction option */
  275.   MEP_OPT_VL64 = 1 << 6,        /* 64-bit VLIW operation mode option */
  276.   MEP_OPT_VL32 = 1 << 5,        /* 32-bit VLIW operation mode option */
  277.   MEP_OPT_COP = 1 << 4,         /* coprocessor option */
  278.   MEP_OPT_DSP = 1 << 2,         /* DSP option */
  279.   MEP_OPT_UCI = 1 << 1,         /* UCI option */
  280.   MEP_OPT_DBG = 1 << 0,         /* DBG function option */
  281. };


  282. /* Given the option_mask value for a particular entry in
  283.    mep_config_map, produce the value the processor's OPT register
  284.    would use to represent the same set of options.  */
  285. static unsigned int
  286. opt_from_option_mask (unsigned int option_mask)
  287. {
  288.   /* A table mapping OPT register bits onto CGEN config map option
  289.      bits.  */
  290.   struct {
  291.     unsigned int opt_bit, option_mask_bit;
  292.   } bits[] = {
  293.     { MEP_OPT_DIV, 1 << CGEN_INSN_OPTIONAL_DIV_INSN },
  294.     { MEP_OPT_MUL, 1 << CGEN_INSN_OPTIONAL_MUL_INSN },
  295.     { MEP_OPT_DIV, 1 << CGEN_INSN_OPTIONAL_DIV_INSN },
  296.     { MEP_OPT_DBG, 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN },
  297.     { MEP_OPT_LDZ, 1 << CGEN_INSN_OPTIONAL_LDZ_INSN },
  298.     { MEP_OPT_ABS, 1 << CGEN_INSN_OPTIONAL_ABS_INSN },
  299.     { MEP_OPT_AVE, 1 << CGEN_INSN_OPTIONAL_AVE_INSN },
  300.     { MEP_OPT_MIN, 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN },
  301.     { MEP_OPT_CLP, 1 << CGEN_INSN_OPTIONAL_CLIP_INSN },
  302.     { MEP_OPT_SAT, 1 << CGEN_INSN_OPTIONAL_SAT_INSN },
  303.     { MEP_OPT_UCI, 1 << CGEN_INSN_OPTIONAL_UCI_INSN },
  304.     { MEP_OPT_DSP, 1 << CGEN_INSN_OPTIONAL_DSP_INSN },
  305.     { MEP_OPT_COP, 1 << CGEN_INSN_OPTIONAL_CP_INSN },
  306.   };

  307.   int i;
  308.   unsigned int opt = 0;

  309.   for (i = 0; i < (sizeof (bits) / sizeof (bits[0])); i++)
  310.     if (option_mask & bits[i].option_mask_bit)
  311.       opt |= bits[i].opt_bit;

  312.   return opt;
  313. }


  314. /* Return the value the $OPT register would use to represent the set
  315.    of options for ME_MODULE.  */
  316. static unsigned int
  317. me_module_opt (CONFIG_ATTR me_module)
  318. {
  319.   return opt_from_option_mask (mep_config_map[me_module].option_mask);
  320. }


  321. /* Return the width of ME_MODULE's coprocessor data bus, in bits.
  322.    This is either 32 or 64.  */
  323. static int
  324. me_module_cop_data_bus_width (CONFIG_ATTR me_module)
  325. {
  326.   if (mep_config_map[me_module].option_mask
  327.       & (1 << CGEN_INSN_OPTIONAL_CP64_INSN))
  328.     return 64;
  329.   else
  330.     return 32;
  331. }


  332. /* Return true if ME_MODULE is big-endian, false otherwise.  */
  333. static int
  334. me_module_big_endian (CONFIG_ATTR me_module)
  335. {
  336.   return mep_config_map[me_module].big_endian;
  337. }


  338. /* Return the name of ME_MODULE, or NULL if it has no name.  */
  339. static const char *
  340. me_module_name (CONFIG_ATTR me_module)
  341. {
  342.   /* The default me_module has "" as its name, but it's easier for our
  343.      callers to test for NULL.  */
  344.   if (! mep_config_map[me_module].name
  345.       || mep_config_map[me_module].name[0] == '\0')
  346.     return NULL;
  347.   else
  348.     return mep_config_map[me_module].name;
  349. }

  350. /* Register set.  */


  351. /* The MeP spec defines the following registers:
  352.    16 general purpose registers (r0-r15)
  353.    32 control/special registers (csr0-csr31)
  354.    32 coprocessor general-purpose registers (c0 -- c31)
  355.    64 coprocessor control registers (ccr0 -- ccr63)

  356.    For the raw registers, we assign numbers here explicitly, instead
  357.    of letting the enum assign them for us; the numbers are a matter of
  358.    external protocol, and shouldn't shift around as things are edited.

  359.    We access the control/special registers via pseudoregisters, to
  360.    enforce read-only portions that some registers have.

  361.    We access the coprocessor general purpose and control registers via
  362.    pseudoregisters, to make sure they appear in the proper order in
  363.    the 'info all-registers' command (which uses the register number
  364.    ordering), and also to allow them to be renamed and resized
  365.    depending on the me_module in use.

  366.    The MeP allows coprocessor general-purpose registers to be either
  367.    32 or 64 bits long, depending on the configuration.  Since we don't
  368.    want the format of the 'g' packet to vary from one core to another,
  369.    the raw coprocessor GPRs are always 64 bits.  GDB doesn't allow the
  370.    types of registers to change (see the implementation of
  371.    register_type), so we have four banks of pseudoregisters for the
  372.    coprocessor gprs --- 32-bit vs. 64-bit, and integer
  373.    vs. floating-point --- and we show or hide them depending on the
  374.    configuration.  */
  375. enum
  376. {
  377.   MEP_FIRST_RAW_REGNUM = 0,

  378.   MEP_FIRST_GPR_REGNUM = 0,
  379.   MEP_R0_REGNUM = 0,
  380.   MEP_R1_REGNUM = 1,
  381.   MEP_R2_REGNUM = 2,
  382.   MEP_R3_REGNUM = 3,
  383.   MEP_R4_REGNUM = 4,
  384.   MEP_R5_REGNUM = 5,
  385.   MEP_R6_REGNUM = 6,
  386.   MEP_R7_REGNUM = 7,
  387.   MEP_R8_REGNUM = 8,
  388.   MEP_R9_REGNUM = 9,
  389.   MEP_R10_REGNUM = 10,
  390.   MEP_R11_REGNUM = 11,
  391.   MEP_R12_REGNUM = 12,
  392.   MEP_FP_REGNUM = MEP_R8_REGNUM,
  393.   MEP_R13_REGNUM = 13,
  394.   MEP_TP_REGNUM = MEP_R13_REGNUM,        /* (r13) Tiny data pointer */
  395.   MEP_R14_REGNUM = 14,
  396.   MEP_GP_REGNUM = MEP_R14_REGNUM,        /* (r14) Global pointer */
  397.   MEP_R15_REGNUM = 15,
  398.   MEP_SP_REGNUM = MEP_R15_REGNUM,        /* (r15) Stack pointer */
  399.   MEP_LAST_GPR_REGNUM = MEP_R15_REGNUM,

  400.   /* The raw control registers.  These are the values as received via
  401.      the remote protocol, directly from the target; we only let user
  402.      code touch the via the pseudoregisters, which enforce read-only
  403.      bits.  */
  404.   MEP_FIRST_RAW_CSR_REGNUM = 16,
  405.   MEP_RAW_PC_REGNUM    = 16,    /* Program counter */
  406.   MEP_RAW_LP_REGNUM    = 17,    /* Link pointer */
  407.   MEP_RAW_SAR_REGNUM   = 18,    /* Raw shift amount */
  408.   MEP_RAW_CSR3_REGNUM  = 19,    /* csr3: reserved */
  409.   MEP_RAW_RPB_REGNUM   = 20,    /* Raw repeat begin address */
  410.   MEP_RAW_RPE_REGNUM   = 21,    /* Repeat end address */
  411.   MEP_RAW_RPC_REGNUM   = 22,    /* Repeat count */
  412.   MEP_RAW_HI_REGNUM    = 23, /* Upper 32 bits of result of 64 bit mult/div */
  413.   MEP_RAW_LO_REGNUM    = 24, /* Lower 32 bits of result of 64 bit mult/div */
  414.   MEP_RAW_CSR9_REGNUM  = 25,    /* csr3: reserved */
  415.   MEP_RAW_CSR10_REGNUM = 26,    /* csr3: reserved */
  416.   MEP_RAW_CSR11_REGNUM = 27,    /* csr3: reserved */
  417.   MEP_RAW_MB0_REGNUM   = 28,    /* Raw modulo begin address 0 */
  418.   MEP_RAW_ME0_REGNUM   = 29,    /* Raw modulo end address 0 */
  419.   MEP_RAW_MB1_REGNUM   = 30,    /* Raw modulo begin address 1 */
  420.   MEP_RAW_ME1_REGNUM   = 31,    /* Raw modulo end address 1 */
  421.   MEP_RAW_PSW_REGNUM   = 32,    /* Raw program status word */
  422.   MEP_RAW_ID_REGNUM    = 33,    /* Raw processor ID/revision */
  423.   MEP_RAW_TMP_REGNUM   = 34,    /* Temporary */
  424.   MEP_RAW_EPC_REGNUM   = 35,    /* Exception program counter */
  425.   MEP_RAW_EXC_REGNUM   = 36,    /* Raw exception cause */
  426.   MEP_RAW_CFG_REGNUM   = 37,    /* Raw processor configuration*/
  427.   MEP_RAW_CSR22_REGNUM = 38,    /* csr3: reserved */
  428.   MEP_RAW_NPC_REGNUM   = 39,    /* Nonmaskable interrupt PC */
  429.   MEP_RAW_DBG_REGNUM   = 40,    /* Raw debug */
  430.   MEP_RAW_DEPC_REGNUM  = 41,    /* Debug exception PC */
  431.   MEP_RAW_OPT_REGNUM   = 42,    /* Raw options */
  432.   MEP_RAW_RCFG_REGNUM  = 43,    /* Raw local ram config */
  433.   MEP_RAW_CCFG_REGNUM  = 44,    /* Raw cache config */
  434.   MEP_RAW_CSR29_REGNUM = 45,    /* csr3: reserved */
  435.   MEP_RAW_CSR30_REGNUM = 46,    /* csr3: reserved */
  436.   MEP_RAW_CSR31_REGNUM = 47,    /* csr3: reserved */
  437.   MEP_LAST_RAW_CSR_REGNUM = MEP_RAW_CSR31_REGNUM,

  438.   /* The raw coprocessor general-purpose registers.  These are all 64
  439.      bits wide.  */
  440.   MEP_FIRST_RAW_CR_REGNUM = 48,
  441.   MEP_LAST_RAW_CR_REGNUM = MEP_FIRST_RAW_CR_REGNUM + 31,

  442.   MEP_FIRST_RAW_CCR_REGNUM = 80,
  443.   MEP_LAST_RAW_CCR_REGNUM = MEP_FIRST_RAW_CCR_REGNUM + 63,

  444.   /* The module number register.  This is the index of the me_module
  445.      of which the current target is an instance.  (This is not a real
  446.      MeP-specified register; it's provided by SID.)  */
  447.   MEP_MODULE_REGNUM,

  448.   MEP_LAST_RAW_REGNUM = MEP_MODULE_REGNUM,

  449.   MEP_NUM_RAW_REGS = MEP_LAST_RAW_REGNUM + 1,

  450.   /* Pseudoregisters.  See mep_pseudo_register_read and
  451.      mep_pseudo_register_write.  */
  452.   MEP_FIRST_PSEUDO_REGNUM = MEP_NUM_RAW_REGS,

  453.   /* We have a pseudoregister for every control/special register, to
  454.      implement registers with read-only bits.  */
  455.   MEP_FIRST_CSR_REGNUM = MEP_FIRST_PSEUDO_REGNUM,
  456.   MEP_PC_REGNUM = MEP_FIRST_CSR_REGNUM, /* Program counter */
  457.   MEP_LP_REGNUM,                /* Link pointer */
  458.   MEP_SAR_REGNUM,               /* shift amount */
  459.   MEP_CSR3_REGNUM,              /* csr3: reserved */
  460.   MEP_RPB_REGNUM,               /* repeat begin address */
  461.   MEP_RPE_REGNUM,               /* Repeat end address */
  462.   MEP_RPC_REGNUM,               /* Repeat count */
  463.   MEP_HI_REGNUM,  /* Upper 32 bits of the result of 64 bit mult/div */
  464.   MEP_LO_REGNUM,  /* Lower 32 bits of the result of 64 bit mult/div */
  465.   MEP_CSR9_REGNUM,              /* csr3: reserved */
  466.   MEP_CSR10_REGNUM,             /* csr3: reserved */
  467.   MEP_CSR11_REGNUM,             /* csr3: reserved */
  468.   MEP_MB0_REGNUM,               /* modulo begin address 0 */
  469.   MEP_ME0_REGNUM,               /* modulo end address 0 */
  470.   MEP_MB1_REGNUM,               /* modulo begin address 1 */
  471.   MEP_ME1_REGNUM,               /* modulo end address 1 */
  472.   MEP_PSW_REGNUM,               /* program status word */
  473.   MEP_ID_REGNUM,                /* processor ID/revision */
  474.   MEP_TMP_REGNUM,               /* Temporary */
  475.   MEP_EPC_REGNUM,               /* Exception program counter */
  476.   MEP_EXC_REGNUM,               /* exception cause */
  477.   MEP_CFG_REGNUM,               /* processor configuration*/
  478.   MEP_CSR22_REGNUM,             /* csr3: reserved */
  479.   MEP_NPC_REGNUM,               /* Nonmaskable interrupt PC */
  480.   MEP_DBG_REGNUM,               /* debug */
  481.   MEP_DEPC_REGNUM,              /* Debug exception PC */
  482.   MEP_OPT_REGNUM,               /* options */
  483.   MEP_RCFG_REGNUM,              /* local ram config */
  484.   MEP_CCFG_REGNUM,              /* cache config */
  485.   MEP_CSR29_REGNUM,             /* csr3: reserved */
  486.   MEP_CSR30_REGNUM,             /* csr3: reserved */
  487.   MEP_CSR31_REGNUM,             /* csr3: reserved */
  488.   MEP_LAST_CSR_REGNUM = MEP_CSR31_REGNUM,

  489.   /* The 32-bit integer view of the coprocessor GPR's.  */
  490.   MEP_FIRST_CR32_REGNUM,
  491.   MEP_LAST_CR32_REGNUM = MEP_FIRST_CR32_REGNUM + 31,

  492.   /* The 32-bit floating-point view of the coprocessor GPR's.  */
  493.   MEP_FIRST_FP_CR32_REGNUM,
  494.   MEP_LAST_FP_CR32_REGNUM = MEP_FIRST_FP_CR32_REGNUM + 31,

  495.   /* The 64-bit integer view of the coprocessor GPR's.  */
  496.   MEP_FIRST_CR64_REGNUM,
  497.   MEP_LAST_CR64_REGNUM = MEP_FIRST_CR64_REGNUM + 31,

  498.   /* The 64-bit floating-point view of the coprocessor GPR's.  */
  499.   MEP_FIRST_FP_CR64_REGNUM,
  500.   MEP_LAST_FP_CR64_REGNUM = MEP_FIRST_FP_CR64_REGNUM + 31,

  501.   MEP_FIRST_CCR_REGNUM,
  502.   MEP_LAST_CCR_REGNUM = MEP_FIRST_CCR_REGNUM + 63,

  503.   MEP_LAST_PSEUDO_REGNUM = MEP_LAST_CCR_REGNUM,

  504.   MEP_NUM_PSEUDO_REGS = (MEP_LAST_PSEUDO_REGNUM - MEP_LAST_RAW_REGNUM),

  505.   MEP_NUM_REGS = MEP_NUM_RAW_REGS + MEP_NUM_PSEUDO_REGS
  506. };


  507. #define IN_SET(set, n) \
  508.   (MEP_FIRST_ ## set ## _REGNUM <= (n) && (n) <= MEP_LAST_ ## set ## _REGNUM)

  509. #define IS_GPR_REGNUM(n)     (IN_SET (GPR,     (n)))
  510. #define IS_RAW_CSR_REGNUM(n) (IN_SET (RAW_CSR, (n)))
  511. #define IS_RAW_CR_REGNUM(n)  (IN_SET (RAW_CR,  (n)))
  512. #define IS_RAW_CCR_REGNUM(n) (IN_SET (RAW_CCR, (n)))

  513. #define IS_CSR_REGNUM(n)     (IN_SET (CSR,     (n)))
  514. #define IS_CR32_REGNUM(n)    (IN_SET (CR32,    (n)))
  515. #define IS_FP_CR32_REGNUM(n) (IN_SET (FP_CR32, (n)))
  516. #define IS_CR64_REGNUM(n)    (IN_SET (CR64,    (n)))
  517. #define IS_FP_CR64_REGNUM(n) (IN_SET (FP_CR64, (n)))
  518. #define IS_CR_REGNUM(n)      (IS_CR32_REGNUM (n) || IS_FP_CR32_REGNUM (n) \
  519.                               || IS_CR64_REGNUM (n) || IS_FP_CR64_REGNUM (n))
  520. #define IS_CCR_REGNUM(n)     (IN_SET (CCR,     (n)))

  521. #define IS_RAW_REGNUM(n)     (IN_SET (RAW,     (n)))
  522. #define IS_PSEUDO_REGNUM(n)  (IN_SET (PSEUDO,  (n)))

  523. #define NUM_REGS_IN_SET(set) \
  524.   (MEP_LAST_ ## set ## _REGNUM - MEP_FIRST_ ## set ## _REGNUM + 1)

  525. #define MEP_GPR_SIZE (4)        /* Size of a MeP general-purpose register.  */
  526. #define MEP_PSW_SIZE (4)        /* Size of the PSW register.  */
  527. #define MEP_LP_SIZE (4)         /* Size of the LP register.  */


  528. /* Many of the control/special registers contain bits that cannot be
  529.    written to; some are entirely read-only.  So we present them all as
  530.    pseudoregisters.

  531.    The following table describes the special properties of each CSR.  */
  532. struct mep_csr_register
  533. {
  534.   /* The number of this CSR's raw register.  */
  535.   int raw;

  536.   /* The number of this CSR's pseudoregister.  */
  537.   int pseudo;

  538.   /* A mask of the bits that are writeable: if a bit is set here, then
  539.      it can be modified; if the bit is clear, then it cannot.  */
  540.   LONGEST writeable_bits;
  541. };


  542. /* mep_csr_registers[i] describes the i'th CSR.
  543.    We just list the register numbers here explicitly to help catch
  544.    typos.  */
  545. #define CSR(name) MEP_RAW_ ## name ## _REGNUM, MEP_ ## name ## _REGNUM
  546. struct mep_csr_register mep_csr_registers[] = {
  547.   { CSR(PC),    0xffffffff },   /* manual says r/o, but we can write it */
  548.   { CSR(LP),    0xffffffff },
  549.   { CSR(SAR),   0x0000003f },
  550.   { CSR(CSR3),  0xffffffff },
  551.   { CSR(RPB),   0xfffffffe },
  552.   { CSR(RPE),   0xffffffff },
  553.   { CSR(RPC),   0xffffffff },
  554.   { CSR(HI),    0xffffffff },
  555.   { CSR(LO),    0xffffffff },
  556.   { CSR(CSR9),  0xffffffff },
  557.   { CSR(CSR10), 0xffffffff },
  558.   { CSR(CSR11), 0xffffffff },
  559.   { CSR(MB0),   0x0000ffff },
  560.   { CSR(ME0),   0x0000ffff },
  561.   { CSR(MB1),   0x0000ffff },
  562.   { CSR(ME1),   0x0000ffff },
  563.   { CSR(PSW),   0x000003ff },
  564.   { CSR(ID),    0x00000000 },
  565.   { CSR(TMP),   0xffffffff },
  566.   { CSR(EPC),   0xffffffff },
  567.   { CSR(EXC),   0x000030f0 },
  568.   { CSR(CFG),   0x00c0001b },
  569.   { CSR(CSR22), 0xffffffff },
  570.   { CSR(NPC),   0xffffffff },
  571.   { CSR(DBG),   0x00000580 },
  572.   { CSR(DEPC),  0xffffffff },
  573.   { CSR(OPT),   0x00000000 },
  574.   { CSR(RCFG),  0x00000000 },
  575.   { CSR(CCFG),  0x00000000 },
  576.   { CSR(CSR29), 0xffffffff },
  577.   { CSR(CSR30), 0xffffffff },
  578.   { CSR(CSR31), 0xffffffff },
  579. };


  580. /* If R is the number of a raw register, then mep_raw_to_pseudo[R] is
  581.    the number of the corresponding pseudoregister.  Otherwise,
  582.    mep_raw_to_pseudo[R] == R.  */
  583. static int mep_raw_to_pseudo[MEP_NUM_REGS];

  584. /* If R is the number of a pseudoregister, then mep_pseudo_to_raw[R]
  585.    is the number of the underlying raw register.  Otherwise
  586.    mep_pseudo_to_raw[R] == R.  */
  587. static int mep_pseudo_to_raw[MEP_NUM_REGS];

  588. static void
  589. mep_init_pseudoregister_maps (void)
  590. {
  591.   int i;

  592.   /* Verify that mep_csr_registers covers all the CSRs, in order.  */
  593.   gdb_assert (ARRAY_SIZE (mep_csr_registers) == NUM_REGS_IN_SET (CSR));
  594.   gdb_assert (ARRAY_SIZE (mep_csr_registers) == NUM_REGS_IN_SET (RAW_CSR));

  595.   /* Verify that the raw and pseudo ranges have matching sizes.  */
  596.   gdb_assert (NUM_REGS_IN_SET (RAW_CSR) == NUM_REGS_IN_SET (CSR));
  597.   gdb_assert (NUM_REGS_IN_SET (RAW_CR)  == NUM_REGS_IN_SET (CR32));
  598.   gdb_assert (NUM_REGS_IN_SET (RAW_CR)  == NUM_REGS_IN_SET (CR64));
  599.   gdb_assert (NUM_REGS_IN_SET (RAW_CCR) == NUM_REGS_IN_SET (CCR));

  600.   for (i = 0; i < ARRAY_SIZE (mep_csr_registers); i++)
  601.     {
  602.       struct mep_csr_register *r = &mep_csr_registers[i];

  603.       gdb_assert (r->pseudo == MEP_FIRST_CSR_REGNUM + i);
  604.       gdb_assert (r->raw    == MEP_FIRST_RAW_CSR_REGNUM + i);
  605.     }

  606.   /* Set up the initial  raw<->pseudo mappings.  */
  607.   for (i = 0; i < MEP_NUM_REGS; i++)
  608.     {
  609.       mep_raw_to_pseudo[i] = i;
  610.       mep_pseudo_to_raw[i] = i;
  611.     }

  612.   /* Add the CSR raw<->pseudo mappings.  */
  613.   for (i = 0; i < ARRAY_SIZE (mep_csr_registers); i++)
  614.     {
  615.       struct mep_csr_register *r = &mep_csr_registers[i];

  616.       mep_raw_to_pseudo[r->raw] = r->pseudo;
  617.       mep_pseudo_to_raw[r->pseudo] = r->raw;
  618.     }

  619.   /* Add the CR raw<->pseudo mappings.  */
  620.   for (i = 0; i < NUM_REGS_IN_SET (RAW_CR); i++)
  621.     {
  622.       int raw = MEP_FIRST_RAW_CR_REGNUM + i;
  623.       int pseudo32 = MEP_FIRST_CR32_REGNUM + i;
  624.       int pseudofp32 = MEP_FIRST_FP_CR32_REGNUM + i;
  625.       int pseudo64 = MEP_FIRST_CR64_REGNUM + i;
  626.       int pseudofp64 = MEP_FIRST_FP_CR64_REGNUM + i;

  627.       /* Truly, the raw->pseudo mapping depends on the current module.
  628.          But we use the raw->pseudo mapping when we read the debugging
  629.          info; at that point, we don't know what module we'll actually
  630.          be running yet.  So, we always supply the 64-bit register
  631.          numbers; GDB knows how to pick a smaller value out of a
  632.          larger register properly.  */
  633.       mep_raw_to_pseudo[raw] = pseudo64;
  634.       mep_pseudo_to_raw[pseudo32] = raw;
  635.       mep_pseudo_to_raw[pseudofp32] = raw;
  636.       mep_pseudo_to_raw[pseudo64] = raw;
  637.       mep_pseudo_to_raw[pseudofp64] = raw;
  638.     }

  639.   /* Add the CCR raw<->pseudo mappings.  */
  640.   for (i = 0; i < NUM_REGS_IN_SET (CCR); i++)
  641.     {
  642.       int raw = MEP_FIRST_RAW_CCR_REGNUM + i;
  643.       int pseudo = MEP_FIRST_CCR_REGNUM + i;
  644.       mep_raw_to_pseudo[raw] = pseudo;
  645.       mep_pseudo_to_raw[pseudo] = raw;
  646.     }
  647. }


  648. static int
  649. mep_debug_reg_to_regnum (struct gdbarch *gdbarch, int debug_reg)
  650. {
  651.   /* The debug info uses the raw register numbers.  */
  652.   return mep_raw_to_pseudo[debug_reg];
  653. }


  654. /* Return the size, in bits, of the coprocessor pseudoregister
  655.    numbered PSEUDO.  */
  656. static int
  657. mep_pseudo_cr_size (int pseudo)
  658. {
  659.   if (IS_CR32_REGNUM (pseudo)
  660.       || IS_FP_CR32_REGNUM (pseudo))
  661.     return 32;
  662.   else if (IS_CR64_REGNUM (pseudo)
  663.            || IS_FP_CR64_REGNUM (pseudo))
  664.     return 64;
  665.   else
  666.     gdb_assert_not_reached ("unexpected coprocessor pseudo register");
  667. }


  668. /* If the coprocessor pseudoregister numbered PSEUDO is a
  669.    floating-point register, return non-zero; if it is an integer
  670.    register, return zero.  */
  671. static int
  672. mep_pseudo_cr_is_float (int pseudo)
  673. {
  674.   return (IS_FP_CR32_REGNUM (pseudo)
  675.           || IS_FP_CR64_REGNUM (pseudo));
  676. }


  677. /* Given a coprocessor GPR pseudoregister number, return its index
  678.    within that register bank.  */
  679. static int
  680. mep_pseudo_cr_index (int pseudo)
  681. {
  682.   if (IS_CR32_REGNUM (pseudo))
  683.     return pseudo - MEP_FIRST_CR32_REGNUM;
  684.   else if (IS_FP_CR32_REGNUM (pseudo))
  685.       return pseudo - MEP_FIRST_FP_CR32_REGNUM;
  686.   else if (IS_CR64_REGNUM (pseudo))
  687.       return pseudo - MEP_FIRST_CR64_REGNUM;
  688.   else if (IS_FP_CR64_REGNUM (pseudo))
  689.       return pseudo - MEP_FIRST_FP_CR64_REGNUM;
  690.   else
  691.     gdb_assert_not_reached ("unexpected coprocessor pseudo register");
  692. }


  693. /* Return the me_module index describing the current target.

  694.    If the current target has registers (e.g., simulator, remote
  695.    target), then this uses the value of the 'module' register, raw
  696.    register MEP_MODULE_REGNUM.  Otherwise, this retrieves the value
  697.    from the ELF header's e_flags field of the current executable
  698.    file.  */
  699. static CONFIG_ATTR
  700. current_me_module (void)
  701. {
  702.   if (target_has_registers)
  703.     {
  704.       ULONGEST regval;
  705.       regcache_cooked_read_unsigned (get_current_regcache (),
  706.                                      MEP_MODULE_REGNUM, &regval);
  707.       return regval;
  708.     }
  709.   else
  710.     return gdbarch_tdep (target_gdbarch ())->me_module;
  711. }


  712. /* Return the set of options for the current target, in the form that
  713.    the OPT register would use.

  714.    If the current target has registers (e.g., simulator, remote
  715.    target), then this is the actual value of the OPT register.  If the
  716.    current target does not have registers (e.g., an executable file),
  717.    then use the 'module_opt' field we computed when we build the
  718.    gdbarch object for this module.  */
  719. static unsigned int
  720. current_options (void)
  721. {
  722.   if (target_has_registers)
  723.     {
  724.       ULONGEST regval;
  725.       regcache_cooked_read_unsigned (get_current_regcache (),
  726.                                      MEP_OPT_REGNUM, &regval);
  727.       return regval;
  728.     }
  729.   else
  730.     return me_module_opt (current_me_module ());
  731. }


  732. /* Return the width of the current me_module's coprocessor data bus,
  733.    in bits.  This is either 32 or 64.  */
  734. static int
  735. current_cop_data_bus_width (void)
  736. {
  737.   return me_module_cop_data_bus_width (current_me_module ());
  738. }


  739. /* Return the keyword table of coprocessor general-purpose register
  740.    names appropriate for the me_module we're dealing with.  */
  741. static CGEN_KEYWORD *
  742. current_cr_names (void)
  743. {
  744.   const CGEN_HW_ENTRY *hw
  745.     = me_module_register_set (current_me_module (), "h-cr-", HW_H_CR);

  746.   return register_set_keyword_table (hw);
  747. }


  748. /* Return non-zero if the coprocessor general-purpose registers are
  749.    floating-point values, zero otherwise.  */
  750. static int
  751. current_cr_is_float (void)
  752. {
  753.   const CGEN_HW_ENTRY *hw
  754.     = me_module_register_set (current_me_module (), "h-cr-", HW_H_CR);

  755.   return CGEN_ATTR_CGEN_HW_IS_FLOAT_VALUE (CGEN_HW_ATTRS (hw));
  756. }


  757. /* Return the keyword table of coprocessor control register names
  758.    appropriate for the me_module we're dealing with.  */
  759. static CGEN_KEYWORD *
  760. current_ccr_names (void)
  761. {
  762.   const CGEN_HW_ENTRY *hw
  763.     = me_module_register_set (current_me_module (), "h-ccr-", HW_H_CCR);

  764.   return register_set_keyword_table (hw);
  765. }


  766. static const char *
  767. mep_register_name (struct gdbarch *gdbarch, int regnr)
  768. {
  769.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  770.   /* General-purpose registers.  */
  771.   static const char *gpr_names[] = {
  772.     "r0",   "r1",   "r2",   "r3",   /* 0 */
  773.     "r4",   "r5",   "r6",   "r7",   /* 4 */
  774.     "fp",   "r9",   "r10""r11"/* 8 */
  775.     "r12""tp",   "gp",   "sp"    /* 12 */
  776.   };

  777.   /* Special-purpose registers.  */
  778.   static const char *csr_names[] = {
  779.     "pc",   "lp",   "sar""",     /* 0  csr3: reserved */
  780.     "rpb""rpe""rpc""hi",   /* 4 */
  781.     "lo",   "",     "",     "",     /* 8  csr9-csr11: reserved */
  782.     "mb0""me0""mb1""me1"/* 12 */

  783.     "psw""id",   "tmp""epc"/* 16 */
  784.     "exc""cfg""",     "npc"/* 20  csr22: reserved */
  785.     "dbg""depc", "opt""rcfg", /* 24 */
  786.     "ccfg", "",     "",     ""      /* 28  csr29-csr31: reserved */
  787.   };

  788.   if (IS_GPR_REGNUM (regnr))
  789.     return gpr_names[regnr - MEP_R0_REGNUM];
  790.   else if (IS_CSR_REGNUM (regnr))
  791.     {
  792.       /* The 'hi' and 'lo' registers are only present on processors
  793.          that have the 'MUL' or 'DIV' instructions enabled.  */
  794.       if ((regnr == MEP_HI_REGNUM || regnr == MEP_LO_REGNUM)
  795.           && (! (current_options () & (MEP_OPT_MUL | MEP_OPT_DIV))))
  796.         return "";

  797.       return csr_names[regnr - MEP_FIRST_CSR_REGNUM];
  798.     }
  799.   else if (IS_CR_REGNUM (regnr))
  800.     {
  801.       CGEN_KEYWORD *names;
  802.       int cr_size;
  803.       int cr_is_float;

  804.       /* Does this module have a coprocessor at all?  */
  805.       if (! (current_options () & MEP_OPT_COP))
  806.         return "";

  807.       names = current_cr_names ();
  808.       if (! names)
  809.         /* This module's coprocessor has no general-purpose registers.  */
  810.         return "";

  811.       cr_size = current_cop_data_bus_width ();
  812.       if (cr_size != mep_pseudo_cr_size (regnr))
  813.         /* This module's coprocessor's GPR's are of a different size.  */
  814.         return "";

  815.       cr_is_float = current_cr_is_float ();
  816.       /* The extra ! operators ensure we get boolean equality, not
  817.          numeric equality.  */
  818.       if (! cr_is_float != ! mep_pseudo_cr_is_float (regnr))
  819.         /* This module's coprocessor's GPR's are of a different type.  */
  820.         return "";

  821.       return register_name_from_keyword (names, mep_pseudo_cr_index (regnr));
  822.     }
  823.   else if (IS_CCR_REGNUM (regnr))
  824.     {
  825.       /* Does this module have a coprocessor at all?  */
  826.       if (! (current_options () & MEP_OPT_COP))
  827.         return "";

  828.       {
  829.         CGEN_KEYWORD *names = current_ccr_names ();

  830.         if (! names)
  831.           /* This me_module's coprocessor has no control registers.  */
  832.           return "";

  833.         return register_name_from_keyword (names, regnr-MEP_FIRST_CCR_REGNUM);
  834.       }
  835.     }

  836.   /* It might be nice to give the 'module' register a name, but that
  837.      would affect the output of 'info all-registers', which would
  838.      disturb the test suites.  So we leave it invisible.  */
  839.   else
  840.     return NULL;
  841. }


  842. /* Custom register groups for the MeP.  */
  843. static struct reggroup *mep_csr_reggroup; /* control/special */
  844. static struct reggroup *mep_cr_reggroup/* coprocessor general-purpose */
  845. static struct reggroup *mep_ccr_reggroup; /* coprocessor control */


  846. static int
  847. mep_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  848.                          struct reggroup *group)
  849. {
  850.   /* Filter reserved or unused register numbers.  */
  851.   {
  852.     const char *name = mep_register_name (gdbarch, regnum);

  853.     if (! name || name[0] == '\0')
  854.       return 0;
  855.   }

  856.   /* We could separate the GPRs and the CSRs.  Toshiba has approved of
  857.      the existing behavior, so we'd want to run that by them.  */
  858.   if (group == general_reggroup)
  859.     return (IS_GPR_REGNUM (regnum)
  860.             || IS_CSR_REGNUM (regnum));

  861.   /* Everything is in the 'all' reggroup, except for the raw CSR's.  */
  862.   else if (group == all_reggroup)
  863.     return (IS_GPR_REGNUM (regnum)
  864.             || IS_CSR_REGNUM (regnum)
  865.             || IS_CR_REGNUM (regnum)
  866.             || IS_CCR_REGNUM (regnum));

  867.   /* All registers should be saved and restored, except for the raw
  868.      CSR's.

  869.      This is probably right if the coprocessor is something like a
  870.      floating-point unit, but would be wrong if the coprocessor is
  871.      something that does I/O, where register accesses actually cause
  872.      externally-visible actions.  But I get the impression that the
  873.      coprocessor isn't supposed to do things like that --- you'd use a
  874.      hardware engine, perhaps.  */
  875.   else if (group == save_reggroup || group == restore_reggroup)
  876.     return (IS_GPR_REGNUM (regnum)
  877.             || IS_CSR_REGNUM (regnum)
  878.             || IS_CR_REGNUM (regnum)
  879.             || IS_CCR_REGNUM (regnum));

  880.   else if (group == mep_csr_reggroup)
  881.     return IS_CSR_REGNUM (regnum);
  882.   else if (group == mep_cr_reggroup)
  883.     return IS_CR_REGNUM (regnum);
  884.   else if (group == mep_ccr_reggroup)
  885.     return IS_CCR_REGNUM (regnum);
  886.   else
  887.     return 0;
  888. }


  889. static struct type *
  890. mep_register_type (struct gdbarch *gdbarch, int reg_nr)
  891. {
  892.   /* Coprocessor general-purpose registers may be either 32 or 64 bits
  893.      long.  So for them, the raw registers are always 64 bits long (to
  894.      keep the 'g' packet format fixed), and the pseudoregisters vary
  895.      in length.  */
  896.   if (IS_RAW_CR_REGNUM (reg_nr))
  897.     return builtin_type (gdbarch)->builtin_uint64;

  898.   /* Since GDB doesn't allow registers to change type, we have two
  899.      banks of pseudoregisters for the coprocessor general-purpose
  900.      registers: one that gives a 32-bit view, and one that gives a
  901.      64-bit view.  We hide or show one or the other depending on the
  902.      current module.  */
  903.   if (IS_CR_REGNUM (reg_nr))
  904.     {
  905.       int size = mep_pseudo_cr_size (reg_nr);
  906.       if (size == 32)
  907.         {
  908.           if (mep_pseudo_cr_is_float (reg_nr))
  909.             return builtin_type (gdbarch)->builtin_float;
  910.           else
  911.             return builtin_type (gdbarch)->builtin_uint32;
  912.         }
  913.       else if (size == 64)
  914.         {
  915.           if (mep_pseudo_cr_is_float (reg_nr))
  916.             return builtin_type (gdbarch)->builtin_double;
  917.           else
  918.             return builtin_type (gdbarch)->builtin_uint64;
  919.         }
  920.       else
  921.         gdb_assert_not_reached ("unexpected cr size");
  922.     }

  923.   /* All other registers are 32 bits long.  */
  924.   else
  925.     return builtin_type (gdbarch)->builtin_uint32;
  926. }


  927. static CORE_ADDR
  928. mep_read_pc (struct regcache *regcache)
  929. {
  930.   ULONGEST pc;
  931.   regcache_cooked_read_unsigned (regcache, MEP_PC_REGNUM, &pc);
  932.   return pc;
  933. }

  934. static enum register_status
  935. mep_pseudo_cr32_read (struct gdbarch *gdbarch,
  936.                       struct regcache *regcache,
  937.                       int cookednum,
  938.                       void *buf)
  939. {
  940.   enum register_status status;
  941.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  942.   /* Read the raw register into a 64-bit buffer, and then return the
  943.      appropriate end of that buffer.  */
  944.   int rawnum = mep_pseudo_to_raw[cookednum];
  945.   gdb_byte buf64[8];

  946.   gdb_assert (TYPE_LENGTH (register_type (gdbarch, rawnum)) == sizeof (buf64));
  947.   gdb_assert (TYPE_LENGTH (register_type (gdbarch, cookednum)) == 4);
  948.   status = regcache_raw_read (regcache, rawnum, buf64);
  949.   if (status == REG_VALID)
  950.     {
  951.       /* Slow, but legible.  */
  952.       store_unsigned_integer (buf, 4, byte_order,
  953.                               extract_unsigned_integer (buf64, 8, byte_order));
  954.     }
  955.   return status;
  956. }


  957. static enum register_status
  958. mep_pseudo_cr64_read (struct gdbarch *gdbarch,
  959.                       struct regcache *regcache,
  960.                       int cookednum,
  961.                       void *buf)
  962. {
  963.   return regcache_raw_read (regcache, mep_pseudo_to_raw[cookednum], buf);
  964. }


  965. static enum register_status
  966. mep_pseudo_register_read (struct gdbarch *gdbarch,
  967.                           struct regcache *regcache,
  968.                           int cookednum,
  969.                           gdb_byte *buf)
  970. {
  971.   if (IS_CSR_REGNUM (cookednum)
  972.       || IS_CCR_REGNUM (cookednum))
  973.     return regcache_raw_read (regcache, mep_pseudo_to_raw[cookednum], buf);
  974.   else if (IS_CR32_REGNUM (cookednum)
  975.            || IS_FP_CR32_REGNUM (cookednum))
  976.     return mep_pseudo_cr32_read (gdbarch, regcache, cookednum, buf);
  977.   else if (IS_CR64_REGNUM (cookednum)
  978.            || IS_FP_CR64_REGNUM (cookednum))
  979.     return mep_pseudo_cr64_read (gdbarch, regcache, cookednum, buf);
  980.   else
  981.     gdb_assert_not_reached ("unexpected pseudo register");
  982. }


  983. static void
  984. mep_pseudo_csr_write (struct gdbarch *gdbarch,
  985.                       struct regcache *regcache,
  986.                       int cookednum,
  987.                       const void *buf)
  988. {
  989.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  990.   int size = register_size (gdbarch, cookednum);
  991.   struct mep_csr_register *r
  992.     = &mep_csr_registers[cookednum - MEP_FIRST_CSR_REGNUM];

  993.   if (r->writeable_bits == 0)
  994.     /* A completely read-only register; avoid the read-modify-
  995.        write cycle, and juts ignore the entire write.  */
  996.     ;
  997.   else
  998.     {
  999.       /* A partially writeable register; do a read-modify-write cycle.  */
  1000.       ULONGEST old_bits;
  1001.       ULONGEST new_bits;
  1002.       ULONGEST mixed_bits;

  1003.       regcache_raw_read_unsigned (regcache, r->raw, &old_bits);
  1004.       new_bits = extract_unsigned_integer (buf, size, byte_order);
  1005.       mixed_bits = ((r->writeable_bits & new_bits)
  1006.                     | (~r->writeable_bits & old_bits));
  1007.       regcache_raw_write_unsigned (regcache, r->raw, mixed_bits);
  1008.     }
  1009. }


  1010. static void
  1011. mep_pseudo_cr32_write (struct gdbarch *gdbarch,
  1012.                        struct regcache *regcache,
  1013.                        int cookednum,
  1014.                        const void *buf)
  1015. {
  1016.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1017.   /* Expand the 32-bit value into a 64-bit value, and write that to
  1018.      the pseudoregister.  */
  1019.   int rawnum = mep_pseudo_to_raw[cookednum];
  1020.   gdb_byte buf64[8];

  1021.   gdb_assert (TYPE_LENGTH (register_type (gdbarch, rawnum)) == sizeof (buf64));
  1022.   gdb_assert (TYPE_LENGTH (register_type (gdbarch, cookednum)) == 4);
  1023.   /* Slow, but legible.  */
  1024.   store_unsigned_integer (buf64, 8, byte_order,
  1025.                           extract_unsigned_integer (buf, 4, byte_order));
  1026.   regcache_raw_write (regcache, rawnum, buf64);
  1027. }


  1028. static void
  1029. mep_pseudo_cr64_write (struct gdbarch *gdbarch,
  1030.                      struct regcache *regcache,
  1031.                      int cookednum,
  1032.                      const void *buf)
  1033. {
  1034.   regcache_raw_write (regcache, mep_pseudo_to_raw[cookednum], buf);
  1035. }


  1036. static void
  1037. mep_pseudo_register_write (struct gdbarch *gdbarch,
  1038.                            struct regcache *regcache,
  1039.                            int cookednum,
  1040.                            const gdb_byte *buf)
  1041. {
  1042.   if (IS_CSR_REGNUM (cookednum))
  1043.     mep_pseudo_csr_write (gdbarch, regcache, cookednum, buf);
  1044.   else if (IS_CR32_REGNUM (cookednum)
  1045.            || IS_FP_CR32_REGNUM (cookednum))
  1046.     mep_pseudo_cr32_write (gdbarch, regcache, cookednum, buf);
  1047.   else if (IS_CR64_REGNUM (cookednum)
  1048.            || IS_FP_CR64_REGNUM (cookednum))
  1049.     mep_pseudo_cr64_write (gdbarch, regcache, cookednum, buf);
  1050.   else if (IS_CCR_REGNUM (cookednum))
  1051.     regcache_raw_write (regcache, mep_pseudo_to_raw[cookednum], buf);
  1052.   else
  1053.     gdb_assert_not_reached ("unexpected pseudo register");
  1054. }



  1055. /* Disassembly.  */

  1056. /* The mep disassembler needs to know about the section in order to
  1057.    work correctly.  */
  1058. static int
  1059. mep_gdb_print_insn (bfd_vma pc, disassemble_info * info)
  1060. {
  1061.   struct obj_section * s = find_pc_section (pc);

  1062.   if (s)
  1063.     {
  1064.       /* The libopcodes disassembly code uses the section to find the
  1065.          BFD, the BFD to find the ELF header, the ELF header to find
  1066.          the me_module index, and the me_module index to select the
  1067.          right instructions to print.  */
  1068.       info->section = s->the_bfd_section;
  1069.       info->arch = bfd_arch_mep;

  1070.       return print_insn_mep (pc, info);
  1071.     }

  1072.   return 0;
  1073. }


  1074. /* Prologue analysis.  */


  1075. /* The MeP has two classes of instructions: "core" instructions, which
  1076.    are pretty normal RISC chip stuff, and "coprocessor" instructions,
  1077.    which are mostly concerned with moving data in and out of
  1078.    coprocessor registers, and branching on coprocessor condition
  1079.    codes.  There's space in the instruction set for custom coprocessor
  1080.    instructions, too.

  1081.    Instructions can be 16 or 32 bits long; the top two bits of the
  1082.    first byte indicate the length.  The coprocessor instructions are
  1083.    mixed in with the core instructions, and there's no easy way to
  1084.    distinguish them; you have to completely decode them to tell one
  1085.    from the other.

  1086.    The MeP also supports a "VLIW" operation mode, where instructions
  1087.    always occur in fixed-width bundles.  The bundles are either 32
  1088.    bits or 64 bits long, depending on a fixed configuration flag.  You
  1089.    decode the first part of the bundle as normal; if it's a core
  1090.    instruction, and there's any space left in the bundle, the
  1091.    remainder of the bundle is a coprocessor instruction, which will
  1092.    execute in parallel with the core instruction.  If the first part
  1093.    of the bundle is a coprocessor instruction, it occupies the entire
  1094.    bundle.

  1095.    So, here are all the cases:

  1096.    - 32-bit VLIW mode:
  1097.      Every bundle is four bytes long, and naturally aligned, and can hold
  1098.      one or two instructions:
  1099.      - 16-bit core instruction; 16-bit coprocessor instruction
  1100.        These execute in parallel.
  1101.      - 32-bit core instruction
  1102.      - 32-bit coprocessor instruction

  1103.    - 64-bit VLIW mode:
  1104.      Every bundle is eight bytes long, and naturally aligned, and can hold
  1105.      one or two instructions:
  1106.      - 16-bit core instruction; 48-bit (!) coprocessor instruction
  1107.        These execute in parallel.
  1108.      - 32-bit core instruction; 32-bit coprocessor instruction
  1109.        These execute in parallel.
  1110.      - 64-bit coprocessor instruction

  1111.    Now, the MeP manual doesn't define any 48- or 64-bit coprocessor
  1112.    instruction, so I don't really know what's up there; perhaps these
  1113.    are always the user-defined coprocessor instructions.  */


  1114. /* Return non-zero if PC is in a VLIW code section, zero
  1115.    otherwise.  */
  1116. static int
  1117. mep_pc_in_vliw_section (CORE_ADDR pc)
  1118. {
  1119.   struct obj_section *s = find_pc_section (pc);
  1120.   if (s)
  1121.     return (s->the_bfd_section->flags & SEC_MEP_VLIW);
  1122.   return 0;
  1123. }


  1124. /* Set *INSN to the next core instruction at PC, and return the
  1125.    address of the next instruction.

  1126.    The MeP instruction encoding is endian-dependent.  16- and 32-bit
  1127.    instructions are encoded as one or two two-byte parts, and each
  1128.    part is byte-swapped independently.  Thus:

  1129.       void
  1130.       foo (void)
  1131.       {
  1132.         asm ("movu $1, 0x123456");
  1133.         asm ("sb $1,0x5678($2)");
  1134.         asm ("clip $1, 19");
  1135.       }

  1136.    compiles to this big-endian code:

  1137.        0:        d1 56 12 34         movu $1,0x123456
  1138.        4:        c1 28 56 78         sb $1,22136($2)
  1139.        8:        f1 01 10 98         clip $1,0x13
  1140.        c:        70 02               ret

  1141.    and this little-endian code:

  1142.        0:        56 d1 34 12         movu $1,0x123456
  1143.        4:        28 c1 78 56         sb $1,22136($2)
  1144.        8:        01 f1 98 10         clip $1,0x13
  1145.        c:        02 70               ret

  1146.    Instructions are returned in *INSN in an endian-independent form: a
  1147.    given instruction always appears in *INSN the same way, regardless
  1148.    of whether the instruction stream is big-endian or little-endian.

  1149.    *INSN's most significant 16 bits are the first (i.e., at lower
  1150.    addresses) 16 bit part of the instruction.  Its least significant
  1151.    16 bits are the second (i.e., higher-addressed) 16 bit part of the
  1152.    instruction, or zero for a 16-bit instruction.  Both 16-bit parts
  1153.    are fetched using the current endianness.

  1154.    So, the *INSN values for the instruction sequence above would be
  1155.    the following, in either endianness:

  1156.        0xd1561234       movu $1,0x123456
  1157.        0xc1285678         sb $1,22136($2)
  1158.        0xf1011098         clip $1,0x13
  1159.        0x70020000              ret

  1160.    (In a sense, it would be more natural to return 16-bit instructions
  1161.    in the least significant 16 bits of *INSN, but that would be
  1162.    ambiguous.  In order to tell whether you're looking at a 16- or a
  1163.    32-bit instruction, you have to consult the major opcode field ---
  1164.    the most significant four bits of the instruction's first 16-bit
  1165.    part.  But if we put 16-bit instructions at the least significant
  1166.    end of *INSN, then you don't know where to find the major opcode
  1167.    field until you know if it's a 16- or a 32-bit instruction ---
  1168.    which is where we started.)

  1169.    If PC points to a core / coprocessor bundle in a VLIW section, set
  1170.    *INSN to the core instruction, and return the address of the next
  1171.    bundle.  This has the effect of skipping the bundled coprocessor
  1172.    instruction.  That's okay, since coprocessor instructions aren't
  1173.    significant to prologue analysis --- for the time being,
  1174.    anyway.  */

  1175. static CORE_ADDR
  1176. mep_get_insn (struct gdbarch *gdbarch, CORE_ADDR pc, unsigned long *insn)
  1177. {
  1178.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1179.   int pc_in_vliw_section;
  1180.   int vliw_mode;
  1181.   int insn_len;
  1182.   gdb_byte buf[2];

  1183.   *insn = 0;

  1184.   /* Are we in a VLIW section?  */
  1185.   pc_in_vliw_section = mep_pc_in_vliw_section (pc);
  1186.   if (pc_in_vliw_section)
  1187.     {
  1188.       /* Yes, find out which bundle size.  */
  1189.       vliw_mode = current_options () & (MEP_OPT_VL32 | MEP_OPT_VL64);

  1190.       /* If PC is in a VLIW section, but the current core doesn't say
  1191.          that it supports either VLIW mode, then we don't have enough
  1192.          information to parse the instruction stream it contains.
  1193.          Since the "undifferentiated" standard core doesn't have
  1194.          either VLIW mode bit set, this could happen.

  1195.          But it shouldn't be an error to (say) set a breakpoint in a
  1196.          VLIW section, if you know you'll never reach it.  (Perhaps
  1197.          you have a script that sets a bunch of standard breakpoints.)

  1198.          So we'll just return zero here, and hope for the best.  */
  1199.       if (! (vliw_mode & (MEP_OPT_VL32 | MEP_OPT_VL64)))
  1200.         return 0;

  1201.       /* If both VL32 and VL64 are set, that's bogus, too.  */
  1202.       if (vliw_mode == (MEP_OPT_VL32 | MEP_OPT_VL64))
  1203.         return 0;
  1204.     }
  1205.   else
  1206.     vliw_mode = 0;

  1207.   read_memory (pc, buf, sizeof (buf));
  1208.   *insn = extract_unsigned_integer (buf, 2, byte_order) << 16;

  1209.   /* The major opcode --- the top four bits of the first 16-bit
  1210.      part --- indicates whether this instruction is 16 or 32 bits
  1211.      long.  All 32-bit instructions have a major opcode whose top
  1212.      two bits are 11; all the rest are 16-bit instructions.  */
  1213.   if ((*insn & 0xc0000000) == 0xc0000000)
  1214.     {
  1215.       /* Fetch the second 16-bit part of the instruction.  */
  1216.       read_memory (pc + 2, buf, sizeof (buf));
  1217.       *insn = *insn | extract_unsigned_integer (buf, 2, byte_order);
  1218.     }

  1219.   /* If we're in VLIW code, then the VLIW width determines the address
  1220.      of the next instruction.  */
  1221.   if (vliw_mode)
  1222.     {
  1223.       /* In 32-bit VLIW code, all bundles are 32 bits long.  We ignore the
  1224.          coprocessor half of a core / copro bundle.  */
  1225.       if (vliw_mode == MEP_OPT_VL32)
  1226.         insn_len = 4;

  1227.       /* In 64-bit VLIW code, all bundles are 64 bits long.  We ignore the
  1228.          coprocessor half of a core / copro bundle.  */
  1229.       else if (vliw_mode == MEP_OPT_VL64)
  1230.         insn_len = 8;

  1231.       /* We'd better be in either core, 32-bit VLIW, or 64-bit VLIW mode.  */
  1232.       else
  1233.         gdb_assert_not_reached ("unexpected vliw mode");
  1234.     }

  1235.   /* Otherwise, the top two bits of the major opcode are (again) what
  1236.      we need to check.  */
  1237.   else if ((*insn & 0xc0000000) == 0xc0000000)
  1238.     insn_len = 4;
  1239.   else
  1240.     insn_len = 2;

  1241.   return pc + insn_len;
  1242. }


  1243. /* Sign-extend the LEN-bit value N.  */
  1244. #define SEXT(n, len) ((((int) (n)) ^ (1 << ((len) - 1))) - (1 << ((len) - 1)))

  1245. /* Return the LEN-bit field at POS from I.  */
  1246. #define FIELD(i, pos, len) (((i) >> (pos)) & ((1 << (len)) - 1))

  1247. /* Like FIELD, but sign-extend the field's value.  */
  1248. #define SFIELD(i, pos, len) (SEXT (FIELD ((i), (pos), (len)), (len)))


  1249. /* Macros for decoding instructions.

  1250.    Remember that 16-bit instructions are placed in bits 16..31 of i,
  1251.    not at the least significant end; this means that the major opcode
  1252.    field is always in the same place, regardless of the width of the
  1253.    instruction.  As a reminder of this, we show the lower 16 bits of a
  1254.    16-bit instruction as xxxx_xxxx_xxxx_xxxx.  */

  1255. /* SB Rn,(Rm)                      0000_nnnn_mmmm_1000 */
  1256. /* SH Rn,(Rm)                      0000_nnnn_mmmm_1001 */
  1257. /* SW Rn,(Rm)                      0000_nnnn_mmmm_1010 */

  1258. /* SW Rn,disp16(Rm)              1100_nnnn_mmmm_1010 dddd_dddd_dddd_dddd */
  1259. #define IS_SW(i)              (((i) & 0xf00f0000) == 0xc00a0000)
  1260. /* SB Rn,disp16(Rm)              1100_nnnn_mmmm_1000 dddd_dddd_dddd_dddd */
  1261. #define IS_SB(i)              (((i) & 0xf00f0000) == 0xc0080000)
  1262. /* SH Rn,disp16(Rm)              1100_nnnn_mmmm_1001 dddd_dddd_dddd_dddd */
  1263. #define IS_SH(i)              (((i) & 0xf00f0000) == 0xc0090000)
  1264. #define SWBH_32_BASE(i)       (FIELD (i, 20, 4))
  1265. #define SWBH_32_SOURCE(i)     (FIELD (i, 24, 4))
  1266. #define SWBH_32_OFFSET(i)     (SFIELD (i, 0, 16))

  1267. /* SW Rn,disp7.align4(SP)     0100_nnnn_0ddd_dd10 xxxx_xxxx_xxxx_xxxx */
  1268. #define IS_SW_IMMD(i)              (((i) & 0xf0830000) == 0x40020000)
  1269. #define SW_IMMD_SOURCE(i)     (FIELD (i, 24, 4))
  1270. #define SW_IMMD_OFFSET(i)     (FIELD (i, 18, 5) << 2)

  1271. /* SW Rn,(Rm)                 0000_nnnn_mmmm_1010 xxxx_xxxx_xxxx_xxxx */
  1272. #define IS_SW_REG(i)              (((i) & 0xf00f0000) == 0x000a0000)
  1273. #define SW_REG_SOURCE(i)      (FIELD (i, 24, 4))
  1274. #define SW_REG_BASE(i)        (FIELD (i, 20, 4))

  1275. /* ADD3 Rl,Rn,Rm              1001_nnnn_mmmm_llll xxxx_xxxx_xxxx_xxxx */
  1276. #define IS_ADD3_16_REG(i)     (((i) & 0xf0000000) == 0x90000000)
  1277. #define ADD3_16_REG_SRC1(i)   (FIELD (i, 20, 4))               /* n */
  1278. #define ADD3_16_REG_SRC2(i)   (FIELD (i, 24, 4))               /* m */

  1279. /* ADD3 Rn,Rm,imm16           1100_nnnn_mmmm_0000 iiii_iiii_iiii_iiii */
  1280. #define IS_ADD3_32(i)              (((i) & 0xf00f0000) == 0xc0000000)
  1281. #define ADD3_32_TARGET(i)     (FIELD (i, 24, 4))
  1282. #define ADD3_32_SOURCE(i)     (FIELD (i, 20, 4))
  1283. #define ADD3_32_OFFSET(i)     (SFIELD (i, 0, 16))

  1284. /* ADD3 Rn,SP,imm7.align4     0100_nnnn_0iii_ii00 xxxx_xxxx_xxxx_xxxx */
  1285. #define IS_ADD3_16(i)                (((i) & 0xf0830000) == 0x40000000)
  1286. #define ADD3_16_TARGET(i)     (FIELD (i, 24, 4))
  1287. #define ADD3_16_OFFSET(i)     (FIELD (i, 18, 5) << 2)

  1288. /* ADD Rn,imm6                      0110_nnnn_iiii_ii00 xxxx_xxxx_xxxx_xxxx */
  1289. #define IS_ADD(i)               (((i) & 0xf0030000) == 0x60000000)
  1290. #define ADD_TARGET(i)              (FIELD (i, 24, 4))
  1291. #define ADD_OFFSET(i)         (SFIELD (i, 18, 6))

  1292. /* LDC Rn,imm5                      0111_nnnn_iiii_101I xxxx_xxxx_xxxx_xxxx
  1293.                               imm5 = I||i[7:4] */
  1294. #define IS_LDC(i)              (((i) & 0xf00e0000) == 0x700a0000)
  1295. #define LDC_IMM(i)            ((FIELD (i, 16, 1) << 4) | FIELD (i, 20, 4))
  1296. #define LDC_TARGET(i)         (FIELD (i, 24, 4))

  1297. /* LW Rn,disp16(Rm)           1100_nnnn_mmmm_1110 dddd_dddd_dddd_dddd  */
  1298. #define IS_LW(i)              (((i) & 0xf00f0000) == 0xc00e0000)
  1299. #define LW_TARGET(i)          (FIELD (i, 24, 4))
  1300. #define LW_BASE(i)            (FIELD (i, 20, 4))
  1301. #define LW_OFFSET(i)          (SFIELD (i, 0, 16))

  1302. /* MOV Rn,Rm                      0000_nnnn_mmmm_0000 xxxx_xxxx_xxxx_xxxx */
  1303. #define IS_MOV(i)              (((i) & 0xf00f0000) == 0x00000000)
  1304. #define MOV_TARGET(i)              (FIELD (i, 24, 4))
  1305. #define MOV_SOURCE(i)              (FIELD (i, 20, 4))

  1306. /* BRA disp12.align2              1011_dddd_dddd_ddd0 xxxx_xxxx_xxxx_xxxx */
  1307. #define IS_BRA(i)              (((i) & 0xf0010000) == 0xb0000000)
  1308. #define BRA_DISP(i)           (SFIELD (i, 17, 11) << 1)


  1309. /* This structure holds the results of a prologue analysis.  */
  1310. struct mep_prologue
  1311. {
  1312.   /* The architecture for which we generated this prologue info.  */
  1313.   struct gdbarch *gdbarch;

  1314.   /* The offset from the frame base to the stack pointer --- always
  1315.      zero or negative.

  1316.      Calling this a "size" is a bit misleading, but given that the
  1317.      stack grows downwards, using offsets for everything keeps one
  1318.      from going completely sign-crazy: you never change anything's
  1319.      sign for an ADD instruction; always change the second operand's
  1320.      sign for a SUB instruction; and everything takes care of
  1321.      itself.  */
  1322.   int frame_size;

  1323.   /* Non-zero if this function has initialized the frame pointer from
  1324.      the stack pointer, zero otherwise.  */
  1325.   int has_frame_ptr;

  1326.   /* If has_frame_ptr is non-zero, this is the offset from the frame
  1327.      base to where the frame pointer points.  This is always zero or
  1328.      negative.  */
  1329.   int frame_ptr_offset;

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

  1334.   /* reg_offset[R] is the offset from the CFA at which register R is
  1335.      saved, or 1 if register R has not been saved.  (Real values are
  1336.      always zero or negative.)  */
  1337.   int reg_offset[MEP_NUM_REGS];
  1338. };

  1339. /* Return non-zero if VALUE is an incoming argument register.  */

  1340. static int
  1341. is_arg_reg (pv_t value)
  1342. {
  1343.   return (value.kind == pvk_register
  1344.           && MEP_R1_REGNUM <= value.reg && value.reg <= MEP_R4_REGNUM
  1345.           && value.k == 0);
  1346. }

  1347. /* Return non-zero if a store of REG's current value VALUE to ADDR is
  1348.    probably spilling an argument register to its stack slot in STACK.
  1349.    Such instructions should be included in the prologue, if possible.

  1350.    The store is a spill if:
  1351.    - the value being stored is REG's original value;
  1352.    - the value has not already been stored somewhere in STACK; and
  1353.    - ADDR is a stack slot's address (e.g., relative to the original
  1354.      value of the SP).  */
  1355. static int
  1356. is_arg_spill (struct gdbarch *gdbarch, pv_t value, pv_t addr,
  1357.               struct pv_area *stack)
  1358. {
  1359.   return (is_arg_reg (value)
  1360.           && pv_is_register (addr, MEP_SP_REGNUM)
  1361.           && ! pv_area_find_reg (stack, gdbarch, value.reg, 0));
  1362. }


  1363. /* Function for finding saved registers in a 'struct pv_area'; we pass
  1364.    this to pv_area_scan.

  1365.    If VALUE is a saved register, ADDR says it was saved at a constant
  1366.    offset from the frame base, and SIZE indicates that the whole
  1367.    register was saved, record its offset in RESULT_UNTYPED.  */
  1368. static void
  1369. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  1370. {
  1371.   struct mep_prologue *result = (struct mep_prologue *) result_untyped;

  1372.   if (value.kind == pvk_register
  1373.       && value.k == 0
  1374.       && pv_is_register (addr, MEP_SP_REGNUM)
  1375.       && size == register_size (result->gdbarch, value.reg))
  1376.     result->reg_offset[value.reg] = addr.k;
  1377. }


  1378. /* Analyze a prologue starting at START_PC, going no further than
  1379.    LIMIT_PC.  Fill in RESULT as appropriate.  */
  1380. static void
  1381. mep_analyze_prologue (struct gdbarch *gdbarch,
  1382.                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
  1383.                       struct mep_prologue *result)
  1384. {
  1385.   CORE_ADDR pc;
  1386.   unsigned long insn;
  1387.   int rn;
  1388.   int found_lp = 0;
  1389.   pv_t reg[MEP_NUM_REGS];
  1390.   struct pv_area *stack;
  1391.   struct cleanup *back_to;
  1392.   CORE_ADDR after_last_frame_setup_insn = start_pc;

  1393.   memset (result, 0, sizeof (*result));
  1394.   result->gdbarch = gdbarch;

  1395.   for (rn = 0; rn < MEP_NUM_REGS; rn++)
  1396.     {
  1397.       reg[rn] = pv_register (rn, 0);
  1398.       result->reg_offset[rn] = 1;
  1399.     }

  1400.   stack = make_pv_area (MEP_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  1401.   back_to = make_cleanup_free_pv_area (stack);

  1402.   pc = start_pc;
  1403.   while (pc < limit_pc)
  1404.     {
  1405.       CORE_ADDR next_pc;
  1406.       pv_t pre_insn_fp, pre_insn_sp;

  1407.       next_pc = mep_get_insn (gdbarch, pc, &insn);

  1408.       /* A zero return from mep_get_insn means that either we weren't
  1409.          able to read the instruction from memory, or that we don't
  1410.          have enough information to be able to reliably decode it.  So
  1411.          we'll store here and hope for the best.  */
  1412.       if (! next_pc)
  1413.         break;

  1414.       /* Note the current values of the SP and FP, so we can tell if
  1415.          this instruction changed them, below.  */
  1416.       pre_insn_fp = reg[MEP_FP_REGNUM];
  1417.       pre_insn_sp = reg[MEP_SP_REGNUM];

  1418.       if (IS_ADD (insn))
  1419.         {
  1420.           int rn = ADD_TARGET (insn);
  1421.           CORE_ADDR imm6 = ADD_OFFSET (insn);

  1422.           reg[rn] = pv_add_constant (reg[rn], imm6);
  1423.         }
  1424.       else if (IS_ADD3_16 (insn))
  1425.         {
  1426.           int rn = ADD3_16_TARGET (insn);
  1427.           int imm7 = ADD3_16_OFFSET (insn);

  1428.           reg[rn] = pv_add_constant (reg[MEP_SP_REGNUM], imm7);
  1429.         }
  1430.       else if (IS_ADD3_32 (insn))
  1431.         {
  1432.           int rn = ADD3_32_TARGET (insn);
  1433.           int rm = ADD3_32_SOURCE (insn);
  1434.           int imm16 = ADD3_32_OFFSET (insn);

  1435.           reg[rn] = pv_add_constant (reg[rm], imm16);
  1436.         }
  1437.       else if (IS_SW_REG (insn))
  1438.         {
  1439.           int rn = SW_REG_SOURCE (insn);
  1440.           int rm = SW_REG_BASE (insn);

  1441.           /* If simulating this store would require us to forget
  1442.              everything we know about the stack frame in the name of
  1443.              accuracy, it would be better to just quit now.  */
  1444.           if (pv_area_store_would_trash (stack, reg[rm]))
  1445.             break;

  1446.           if (is_arg_spill (gdbarch, reg[rn], reg[rm], stack))
  1447.             after_last_frame_setup_insn = next_pc;

  1448.           pv_area_store (stack, reg[rm], 4, reg[rn]);
  1449.         }
  1450.       else if (IS_SW_IMMD (insn))
  1451.         {
  1452.           int rn = SW_IMMD_SOURCE (insn);
  1453.           int offset = SW_IMMD_OFFSET (insn);
  1454.           pv_t addr = pv_add_constant (reg[MEP_SP_REGNUM], offset);

  1455.           /* If simulating this store would require us to forget
  1456.              everything we know about the stack frame in the name of
  1457.              accuracy, it would be better to just quit now.  */
  1458.           if (pv_area_store_would_trash (stack, addr))
  1459.             break;

  1460.           if (is_arg_spill (gdbarch, reg[rn], addr, stack))
  1461.             after_last_frame_setup_insn = next_pc;

  1462.           pv_area_store (stack, addr, 4, reg[rn]);
  1463.         }
  1464.       else if (IS_MOV (insn))
  1465.         {
  1466.           int rn = MOV_TARGET (insn);
  1467.           int rm = MOV_SOURCE (insn);

  1468.           reg[rn] = reg[rm];

  1469.           if (pv_is_register (reg[rm], rm) && is_arg_reg (reg[rm]))
  1470.             after_last_frame_setup_insn = next_pc;
  1471.         }
  1472.       else if (IS_SB (insn) || IS_SH (insn) || IS_SW (insn))
  1473.         {
  1474.           int rn = SWBH_32_SOURCE (insn);
  1475.           int rm = SWBH_32_BASE (insn);
  1476.           int disp = SWBH_32_OFFSET (insn);
  1477.           int size = (IS_SB (insn) ? 1
  1478.                       : IS_SH (insn) ? 2
  1479.                       : (gdb_assert (IS_SW (insn)), 4));
  1480.           pv_t addr = pv_add_constant (reg[rm], disp);

  1481.           if (pv_area_store_would_trash (stack, addr))
  1482.             break;

  1483.           if (is_arg_spill (gdbarch, reg[rn], addr, stack))
  1484.             after_last_frame_setup_insn = next_pc;

  1485.           pv_area_store (stack, addr, size, reg[rn]);
  1486.         }
  1487.       else if (IS_LDC (insn))
  1488.         {
  1489.           int rn = LDC_TARGET (insn);
  1490.           int cr = LDC_IMM (insn) + MEP_FIRST_CSR_REGNUM;

  1491.           reg[rn] = reg[cr];
  1492.         }
  1493.       else if (IS_LW (insn))
  1494.         {
  1495.           int rn = LW_TARGET (insn);
  1496.           int rm = LW_BASE (insn);
  1497.           int offset = LW_OFFSET (insn);
  1498.           pv_t addr = pv_add_constant (reg[rm], offset);

  1499.           reg[rn] = pv_area_fetch (stack, addr, 4);
  1500.         }
  1501.       else if (IS_BRA (insn) && BRA_DISP (insn) > 0)
  1502.         {
  1503.           /* When a loop appears as the first statement of a function
  1504.              body, gcc 4.x will use a BRA instruction to branch to the
  1505.              loop condition checking code.  This BRA instruction is
  1506.              marked as part of the prologue.  We therefore set next_pc
  1507.              to this branch target and also stop the prologue scan.
  1508.              The instructions at and beyond the branch target should
  1509.              no longer be associated with the prologue.

  1510.              Note that we only consider forward branches here.  We
  1511.              presume that a forward branch is being used to skip over
  1512.              a loop body.

  1513.              A backwards branch is covered by the default case below.
  1514.              If we were to encounter a backwards branch, that would
  1515.              most likely mean that we've scanned through a loop body.
  1516.              We definitely want to stop the prologue scan when this
  1517.              happens and that is precisely what is done by the default
  1518.              case below.  */
  1519.           next_pc = pc + BRA_DISP (insn);
  1520.           after_last_frame_setup_insn = next_pc;
  1521.           break;
  1522.         }
  1523.       else
  1524.         /* We've hit some instruction we don't know how to simulate.
  1525.            Strictly speaking, we should set every value we're
  1526.            tracking to "unknown".  But we'll be optimistic, assume
  1527.            that we have enough information already, and stop
  1528.            analysis here.  */
  1529.         break;

  1530.       /* If this instruction changed the FP or decreased the SP (i.e.,
  1531.          allocated more stack space), then this may be a good place to
  1532.          declare the prologue finished.  However, there are some
  1533.          exceptions:

  1534.          - If the instruction just changed the FP back to its original
  1535.            value, then that's probably a restore instruction.  The
  1536.            prologue should definitely end before that.

  1537.          - If the instruction increased the value of the SP (that is,
  1538.            shrunk the frame), then it's probably part of a frame
  1539.            teardown sequence, and the prologue should end before that.  */

  1540.       if (! pv_is_identical (reg[MEP_FP_REGNUM], pre_insn_fp))
  1541.         {
  1542.           if (! pv_is_register_k (reg[MEP_FP_REGNUM], MEP_FP_REGNUM, 0))
  1543.             after_last_frame_setup_insn = next_pc;
  1544.         }
  1545.       else if (! pv_is_identical (reg[MEP_SP_REGNUM], pre_insn_sp))
  1546.         {
  1547.           /* The comparison of constants looks odd, there, because .k
  1548.              is unsigned.  All it really means is that the new value
  1549.              is lower than it was before the instruction.  */
  1550.           if (pv_is_register (pre_insn_sp, MEP_SP_REGNUM)
  1551.               && pv_is_register (reg[MEP_SP_REGNUM], MEP_SP_REGNUM)
  1552.               && ((pre_insn_sp.k - reg[MEP_SP_REGNUM].k)
  1553.                   < (reg[MEP_SP_REGNUM].k - pre_insn_sp.k)))
  1554.             after_last_frame_setup_insn = next_pc;
  1555.         }

  1556.       pc = next_pc;
  1557.     }

  1558.   /* Is the frame size (offset, really) a known constant?  */
  1559.   if (pv_is_register (reg[MEP_SP_REGNUM], MEP_SP_REGNUM))
  1560.     result->frame_size = reg[MEP_SP_REGNUM].k;

  1561.   /* Was the frame pointer initialized?  */
  1562.   if (pv_is_register (reg[MEP_FP_REGNUM], MEP_SP_REGNUM))
  1563.     {
  1564.       result->has_frame_ptr = 1;
  1565.       result->frame_ptr_offset = reg[MEP_FP_REGNUM].k;
  1566.     }

  1567.   /* Record where all the registers were saved.  */
  1568.   pv_area_scan (stack, check_for_saved, (void *) result);

  1569.   result->prologue_end = after_last_frame_setup_insn;

  1570.   do_cleanups (back_to);
  1571. }


  1572. static CORE_ADDR
  1573. mep_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1574. {
  1575.   const char *name;
  1576.   CORE_ADDR func_addr, func_end;
  1577.   struct mep_prologue p;

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

  1581.   mep_analyze_prologue (gdbarch, pc, func_end, &p);
  1582.   return p.prologue_end;
  1583. }



  1584. /* Breakpoints.  */

  1585. static const unsigned char *
  1586. mep_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR * pcptr, int *lenptr)
  1587. {
  1588.   static unsigned char breakpoint[] = { 0x70, 0x32 };
  1589.   *lenptr = sizeof (breakpoint);
  1590.   return breakpoint;
  1591. }



  1592. /* Frames and frame unwinding.  */


  1593. static struct mep_prologue *
  1594. mep_analyze_frame_prologue (struct frame_info *this_frame,
  1595.                             void **this_prologue_cache)
  1596. {
  1597.   if (! *this_prologue_cache)
  1598.     {
  1599.       CORE_ADDR func_start, stop_addr;

  1600.       *this_prologue_cache
  1601.         = FRAME_OBSTACK_ZALLOC (struct mep_prologue);

  1602.       func_start = get_frame_func (this_frame);
  1603.       stop_addr = get_frame_pc (this_frame);

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

  1608.       mep_analyze_prologue (get_frame_arch (this_frame),
  1609.                             func_start, stop_addr, *this_prologue_cache);
  1610.     }

  1611.   return *this_prologue_cache;
  1612. }


  1613. /* Given the next frame and a prologue cache, return this frame's
  1614.    base.  */
  1615. static CORE_ADDR
  1616. mep_frame_base (struct frame_info *this_frame,
  1617.                 void **this_prologue_cache)
  1618. {
  1619.   struct mep_prologue *p
  1620.     = mep_analyze_frame_prologue (this_frame, this_prologue_cache);

  1621.   /* In functions that use alloca, the distance between the stack
  1622.      pointer and the frame base varies dynamically, so we can't use
  1623.      the SP plus static information like prologue analysis to find the
  1624.      frame base.  However, such functions must have a frame pointer,
  1625.      to be able to restore the SP on exit.  So whenever we do have a
  1626.      frame pointer, use that to find the base.  */
  1627.   if (p->has_frame_ptr)
  1628.     {
  1629.       CORE_ADDR fp
  1630.         = get_frame_register_unsigned (this_frame, MEP_FP_REGNUM);
  1631.       return fp - p->frame_ptr_offset;
  1632.     }
  1633.   else
  1634.     {
  1635.       CORE_ADDR sp
  1636.         = get_frame_register_unsigned (this_frame, MEP_SP_REGNUM);
  1637.       return sp - p->frame_size;
  1638.     }
  1639. }


  1640. static void
  1641. mep_frame_this_id (struct frame_info *this_frame,
  1642.                    void **this_prologue_cache,
  1643.                    struct frame_id *this_id)
  1644. {
  1645.   *this_id = frame_id_build (mep_frame_base (this_frame, this_prologue_cache),
  1646.                              get_frame_func (this_frame));
  1647. }


  1648. static struct value *
  1649. mep_frame_prev_register (struct frame_info *this_frame,
  1650.                          void **this_prologue_cache, int regnum)
  1651. {
  1652.   struct mep_prologue *p
  1653.     = mep_analyze_frame_prologue (this_frame, this_prologue_cache);

  1654.   /* There are a number of complications in unwinding registers on the
  1655.      MeP, having to do with core functions calling VLIW functions and
  1656.      vice versa.

  1657.      The least significant bit of the link register, LP.LTOM, is the
  1658.      VLIW mode toggle bit: it's set if a core function called a VLIW
  1659.      function, or vice versa, and clear when the caller and callee
  1660.      were both in the same mode.

  1661.      So, if we're asked to unwind the PC, then we really want to
  1662.      unwind the LP and clear the least significant bit.  (Real return
  1663.      addresses are always even.)  And if we want to unwind the program
  1664.      status word (PSW), we need to toggle PSW.OM if LP.LTOM is set.

  1665.      Tweaking the register values we return in this way means that the
  1666.      bits in BUFFERP[] are not the same as the bits you'd find at
  1667.      ADDRP in the inferior, so we make sure lvalp is not_lval when we
  1668.      do this.  */
  1669.   if (regnum == MEP_PC_REGNUM)
  1670.     {
  1671.       struct value *value;
  1672.       CORE_ADDR lp;
  1673.       value = mep_frame_prev_register (this_frame, this_prologue_cache,
  1674.                                        MEP_LP_REGNUM);
  1675.       lp = value_as_long (value);
  1676.       release_value (value);
  1677.       value_free (value);

  1678.       return frame_unwind_got_constant (this_frame, regnum, lp & ~1);
  1679.     }
  1680.   else
  1681.     {
  1682.       CORE_ADDR frame_base = mep_frame_base (this_frame, this_prologue_cache);
  1683.       struct value *value;

  1684.       /* Our caller's SP is our frame base.  */
  1685.       if (regnum == MEP_SP_REGNUM)
  1686.         return frame_unwind_got_constant (this_frame, regnum, frame_base);

  1687.       /* If prologue analysis says we saved this register somewhere,
  1688.          return a description of the stack slot holding it.  */
  1689.       if (p->reg_offset[regnum] != 1)
  1690.         value = frame_unwind_got_memory (this_frame, regnum,
  1691.                                          frame_base + p->reg_offset[regnum]);

  1692.       /* Otherwise, presume we haven't changed the value of this
  1693.          register, and get it from the next frame.  */
  1694.       else
  1695.         value = frame_unwind_got_register (this_frame, regnum, regnum);

  1696.       /* If we need to toggle the operating mode, do so.  */
  1697.       if (regnum == MEP_PSW_REGNUM)
  1698.         {
  1699.           CORE_ADDR psw, lp;

  1700.           psw = value_as_long (value);
  1701.           release_value (value);
  1702.           value_free (value);

  1703.           /* Get the LP's value, too.  */
  1704.           value = get_frame_register_value (this_frame, MEP_LP_REGNUM);
  1705.           lp = value_as_long (value);
  1706.           release_value (value);
  1707.           value_free (value);

  1708.           /* If LP.LTOM is set, then toggle PSW.OM.  */
  1709.           if (lp & 0x1)
  1710.             psw ^= 0x1000;

  1711.           return frame_unwind_got_constant (this_frame, regnum, psw);
  1712.         }

  1713.       return value;
  1714.     }
  1715. }


  1716. static const struct frame_unwind mep_frame_unwind = {
  1717.   NORMAL_FRAME,
  1718.   default_frame_unwind_stop_reason,
  1719.   mep_frame_this_id,
  1720.   mep_frame_prev_register,
  1721.   NULL,
  1722.   default_frame_sniffer
  1723. };


  1724. /* Our general unwinding function can handle unwinding the PC.  */
  1725. static CORE_ADDR
  1726. mep_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1727. {
  1728.   return frame_unwind_register_unsigned (next_frame, MEP_PC_REGNUM);
  1729. }


  1730. /* Our general unwinding function can handle unwinding the SP.  */
  1731. static CORE_ADDR
  1732. mep_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1733. {
  1734.   return frame_unwind_register_unsigned (next_frame, MEP_SP_REGNUM);
  1735. }



  1736. /* Return values.  */


  1737. static int
  1738. mep_use_struct_convention (struct type *type)
  1739. {
  1740.   return (TYPE_LENGTH (type) > MEP_GPR_SIZE);
  1741. }


  1742. static void
  1743. mep_extract_return_value (struct gdbarch *arch,
  1744.                           struct type *type,
  1745.                           struct regcache *regcache,
  1746.                           gdb_byte *valbuf)
  1747. {
  1748.   int byte_order = gdbarch_byte_order (arch);

  1749.   /* Values that don't occupy a full register appear at the less
  1750.      significant end of the value.  This is the offset to where the
  1751.      value starts.  */
  1752.   int offset;

  1753.   /* Return values > MEP_GPR_SIZE bytes are returned in memory,
  1754.      pointed to by R0.  */
  1755.   gdb_assert (TYPE_LENGTH (type) <= MEP_GPR_SIZE);

  1756.   if (byte_order == BFD_ENDIAN_BIG)
  1757.     offset = MEP_GPR_SIZE - TYPE_LENGTH (type);
  1758.   else
  1759.     offset = 0;

  1760.   /* Return values that do fit in a single register are returned in R0.  */
  1761.   regcache_cooked_read_part (regcache, MEP_R0_REGNUM,
  1762.                              offset, TYPE_LENGTH (type),
  1763.                              valbuf);
  1764. }


  1765. static void
  1766. mep_store_return_value (struct gdbarch *arch,
  1767.                         struct type *type,
  1768.                         struct regcache *regcache,
  1769.                         const gdb_byte *valbuf)
  1770. {
  1771.   int byte_order = gdbarch_byte_order (arch);

  1772.   /* Values that fit in a single register go in R0.  */
  1773.   if (TYPE_LENGTH (type) <= MEP_GPR_SIZE)
  1774.     {
  1775.       /* Values that don't occupy a full register appear at the least
  1776.          significant end of the value.  This is the offset to where the
  1777.          value starts.  */
  1778.       int offset;

  1779.       if (byte_order == BFD_ENDIAN_BIG)
  1780.         offset = MEP_GPR_SIZE - TYPE_LENGTH (type);
  1781.       else
  1782.         offset = 0;

  1783.       regcache_cooked_write_part (regcache, MEP_R0_REGNUM,
  1784.                                   offset, TYPE_LENGTH (type),
  1785.                                   valbuf);
  1786.     }

  1787.   /* Return values larger than a single register are returned in
  1788.      memory, pointed to by R0.  Unfortunately, we can't count on R0
  1789.      pointing to the return buffer, so we raise an error here.  */
  1790.   else
  1791.     error (_("\
  1792. GDB cannot set return values larger than four bytes; the Media Processor's\n\
  1793. calling conventions do not provide enough information to do this.\n\
  1794. Try using the 'return' command with no argument."));
  1795. }

  1796. static enum return_value_convention
  1797. mep_return_value (struct gdbarch *gdbarch, struct value *function,
  1798.                   struct type *type, struct regcache *regcache,
  1799.                   gdb_byte *readbuf, const gdb_byte *writebuf)
  1800. {
  1801.   if (mep_use_struct_convention (type))
  1802.     {
  1803.       if (readbuf)
  1804.         {
  1805.           ULONGEST addr;
  1806.           /* Although the address of the struct buffer gets passed in R1, it's
  1807.              returned in R0.  Fetch R0's value and then read the memory
  1808.              at that address.  */
  1809.           regcache_raw_read_unsigned (regcache, MEP_R0_REGNUM, &addr);
  1810.           read_memory (addr, readbuf, TYPE_LENGTH (type));
  1811.         }
  1812.       if (writebuf)
  1813.         {
  1814.           /* Return values larger than a single register are returned in
  1815.              memory, pointed to by R0.  Unfortunately, we can't count on R0
  1816.              pointing to the return buffer, so we raise an error here.  */
  1817.           error (_("\
  1818. GDB cannot set return values larger than four bytes; the Media Processor's\n\
  1819. calling conventions do not provide enough information to do this.\n\
  1820. Try using the 'return' command with no argument."));
  1821.         }
  1822.       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  1823.     }

  1824.   if (readbuf)
  1825.     mep_extract_return_value (gdbarch, type, regcache, readbuf);
  1826.   if (writebuf)
  1827.     mep_store_return_value (gdbarch, type, regcache, writebuf);

  1828.   return RETURN_VALUE_REGISTER_CONVENTION;
  1829. }


  1830. /* Inferior calls.  */


  1831. static CORE_ADDR
  1832. mep_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  1833. {
  1834.   /* Require word alignment.  */
  1835.   return sp & -4;
  1836. }


  1837. /* From "lang_spec2.txt":

  1838.    4.2 Calling conventions

  1839.    4.2.1 Core register conventions

  1840.    - Parameters should be evaluated from left to right, and they
  1841.      should be held in $1,$2,$3,$4 in order.  The fifth parameter or
  1842.      after should be held in the stack.  If the size is larger than 4
  1843.      bytes in the first four parameters, the pointer should be held in
  1844.      the registers instead.  If the size is larger than 4 bytes in the
  1845.      fifth parameter or after, the pointer should be held in the stack.

  1846.    - Return value of a function should be held in register $0.  If the
  1847.      size of return value is larger than 4 bytes, $1 should hold the
  1848.      pointer pointing memory that would hold the return value.  In this
  1849.      case, the first parameter should be held in $2, the second one in
  1850.      $3, and the third one in $4, and the forth parameter or after
  1851.      should be held in the stack.

  1852.    [This doesn't say so, but arguments shorter than four bytes are
  1853.    passed in the least significant end of a four-byte word when
  1854.    they're passed on the stack.]  */


  1855. /* Traverse the list of ARGC arguments ARGV; for every ARGV[i] too
  1856.    large to fit in a register, save it on the stack, and place its
  1857.    address in COPY[i].  SP is the initial stack pointer; return the
  1858.    new stack pointer.  */
  1859. static CORE_ADDR
  1860. push_large_arguments (CORE_ADDR sp, int argc, struct value **argv,
  1861.                       CORE_ADDR copy[])
  1862. {
  1863.   int i;

  1864.   for (i = 0; i < argc; i++)
  1865.     {
  1866.       unsigned arg_len = TYPE_LENGTH (value_type (argv[i]));

  1867.       if (arg_len > MEP_GPR_SIZE)
  1868.         {
  1869.           /* Reserve space for the copy, and then round the SP down, to
  1870.              make sure it's all aligned properly.  */
  1871.           sp = (sp - arg_len) & -4;
  1872.           write_memory (sp, value_contents (argv[i]), arg_len);
  1873.           copy[i] = sp;
  1874.         }
  1875.     }

  1876.   return sp;
  1877. }


  1878. static CORE_ADDR
  1879. mep_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1880.                      struct regcache *regcache, CORE_ADDR bp_addr,
  1881.                      int argc, struct value **argv, CORE_ADDR sp,
  1882.                      int struct_return,
  1883.                      CORE_ADDR struct_addr)
  1884. {
  1885.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1886.   CORE_ADDR *copy = (CORE_ADDR *) alloca (argc * sizeof (copy[0]));
  1887.   CORE_ADDR func_addr = find_function_addr (function, NULL);
  1888.   int i;

  1889.   /* The number of the next register available to hold an argument.  */
  1890.   int arg_reg;

  1891.   /* The address of the next stack slot available to hold an argument.  */
  1892.   CORE_ADDR arg_stack;

  1893.   /* The address of the end of the stack area for arguments.  This is
  1894.      just for error checking.  */
  1895.   CORE_ADDR arg_stack_end;

  1896.   sp = push_large_arguments (sp, argc, argv, copy);

  1897.   /* Reserve space for the stack arguments, if any.  */
  1898.   arg_stack_end = sp;
  1899.   if (argc + (struct_addr ? 1 : 0) > 4)
  1900.     sp -= ((argc + (struct_addr ? 1 : 0)) - 4) * MEP_GPR_SIZE;

  1901.   arg_reg = MEP_R1_REGNUM;
  1902.   arg_stack = sp;

  1903.   /* If we're returning a structure by value, push the pointer to the
  1904.      buffer as the first argument.  */
  1905.   if (struct_return)
  1906.     {
  1907.       regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
  1908.       arg_reg++;
  1909.     }

  1910.   for (i = 0; i < argc; i++)
  1911.     {
  1912.       ULONGEST value;

  1913.       /* Arguments that fit in a GPR get expanded to fill the GPR.  */
  1914.       if (TYPE_LENGTH (value_type (argv[i])) <= MEP_GPR_SIZE)
  1915.         value = extract_unsigned_integer (value_contents (argv[i]),
  1916.                                           TYPE_LENGTH (value_type (argv[i])),
  1917.                                           byte_order);

  1918.       /* Arguments too large to fit in a GPR get copied to the stack,
  1919.          and we pass a pointer to the copy.  */
  1920.       else
  1921.         value = copy[i];

  1922.       /* We use $1 -- $4 for passing arguments, then use the stack.  */
  1923.       if (arg_reg <= MEP_R4_REGNUM)
  1924.         {
  1925.           regcache_cooked_write_unsigned (regcache, arg_reg, value);
  1926.           arg_reg++;
  1927.         }
  1928.       else
  1929.         {
  1930.           gdb_byte buf[MEP_GPR_SIZE];
  1931.           store_unsigned_integer (buf, MEP_GPR_SIZE, byte_order, value);
  1932.           write_memory (arg_stack, buf, MEP_GPR_SIZE);
  1933.           arg_stack += MEP_GPR_SIZE;
  1934.         }
  1935.     }

  1936.   gdb_assert (arg_stack <= arg_stack_end);

  1937.   /* Set the return address.  */
  1938.   regcache_cooked_write_unsigned (regcache, MEP_LP_REGNUM, bp_addr);

  1939.   /* Update the stack pointer.  */
  1940.   regcache_cooked_write_unsigned (regcache, MEP_SP_REGNUM, sp);

  1941.   return sp;
  1942. }


  1943. static struct frame_id
  1944. mep_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1945. {
  1946.   CORE_ADDR sp = get_frame_register_unsigned (this_frame, MEP_SP_REGNUM);
  1947.   return frame_id_build (sp, get_frame_pc (this_frame));
  1948. }



  1949. /* Initialization.  */


  1950. static struct gdbarch *
  1951. mep_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1952. {
  1953.   struct gdbarch *gdbarch;
  1954.   struct gdbarch_tdep *tdep;

  1955.   /* Which me_module are we building a gdbarch object for?  */
  1956.   CONFIG_ATTR me_module;

  1957.   /* If we have a BFD in hand, figure out which me_module it was built
  1958.      for.  Otherwise, use the no-particular-me_module code.  */
  1959.   if (info.abfd)
  1960.     {
  1961.       /* The way to get the me_module code depends on the object file
  1962.          format.  At the moment, we only know how to handle ELF.  */
  1963.       if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1964.         me_module = elf_elfheader (info.abfd)->e_flags & EF_MEP_INDEX_MASK;
  1965.       else
  1966.         me_module = CONFIG_NONE;
  1967.     }
  1968.   else
  1969.     me_module = CONFIG_NONE;

  1970.   /* If we're setting the architecture from a file, check the
  1971.      endianness of the file against that of the me_module.  */
  1972.   if (info.abfd)
  1973.     {
  1974.       /* The negations on either side make the comparison treat all
  1975.          non-zero (true) values as equal.  */
  1976.       if (! bfd_big_endian (info.abfd) != ! me_module_big_endian (me_module))
  1977.         {
  1978.           const char *module_name = me_module_name (me_module);
  1979.           const char *module_endianness
  1980.             = me_module_big_endian (me_module) ? "big" : "little";
  1981.           const char *file_name = bfd_get_filename (info.abfd);
  1982.           const char *file_endianness
  1983.             = bfd_big_endian (info.abfd) ? "big" : "little";

  1984.           fputc_unfiltered ('\n', gdb_stderr);
  1985.           if (module_name)
  1986.             warning (_("the MeP module '%s' is %s-endian, but the executable\n"
  1987.                        "%s is %s-endian."),
  1988.                      module_name, module_endianness,
  1989.                      file_name, file_endianness);
  1990.           else
  1991.             warning (_("the selected MeP module is %s-endian, but the "
  1992.                        "executable\n"
  1993.                        "%s is %s-endian."),
  1994.                      module_endianness, file_name, file_endianness);
  1995.         }
  1996.     }

  1997.   /* Find a candidate among the list of architectures we've created
  1998.      already.  info->bfd_arch_info needs to match, but we also want
  1999.      the right me_module: the ELF header's e_flags field needs to
  2000.      match as well.  */
  2001.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  2002.        arches != NULL;
  2003.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  2004.     if (gdbarch_tdep (arches->gdbarch)->me_module == me_module)
  2005.       return arches->gdbarch;

  2006.   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
  2007.   gdbarch = gdbarch_alloc (&info, tdep);

  2008.   /* Get a CGEN CPU descriptor for this architecture.  */
  2009.   {
  2010.     const char *mach_name = info.bfd_arch_info->printable_name;
  2011.     enum cgen_endian endian = (info.byte_order == BFD_ENDIAN_BIG
  2012.                                ? CGEN_ENDIAN_BIG
  2013.                                : CGEN_ENDIAN_LITTLE);

  2014.     tdep->cpu_desc = mep_cgen_cpu_open (CGEN_CPU_OPEN_BFDMACH, mach_name,
  2015.                                         CGEN_CPU_OPEN_ENDIAN, endian,
  2016.                                         CGEN_CPU_OPEN_END);
  2017.   }

  2018.   tdep->me_module = me_module;

  2019.   /* Register set.  */
  2020.   set_gdbarch_read_pc (gdbarch, mep_read_pc);
  2021.   set_gdbarch_num_regs (gdbarch, MEP_NUM_RAW_REGS);
  2022.   set_gdbarch_pc_regnum (gdbarch, MEP_PC_REGNUM);
  2023.   set_gdbarch_sp_regnum (gdbarch, MEP_SP_REGNUM);
  2024.   set_gdbarch_register_name (gdbarch, mep_register_name);
  2025.   set_gdbarch_register_type (gdbarch, mep_register_type);
  2026.   set_gdbarch_num_pseudo_regs (gdbarch, MEP_NUM_PSEUDO_REGS);
  2027.   set_gdbarch_pseudo_register_read (gdbarch, mep_pseudo_register_read);
  2028.   set_gdbarch_pseudo_register_write (gdbarch, mep_pseudo_register_write);
  2029.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, mep_debug_reg_to_regnum);
  2030.   set_gdbarch_stab_reg_to_regnum (gdbarch, mep_debug_reg_to_regnum);

  2031.   set_gdbarch_register_reggroup_p (gdbarch, mep_register_reggroup_p);
  2032.   reggroup_add (gdbarch, all_reggroup);
  2033.   reggroup_add (gdbarch, general_reggroup);
  2034.   reggroup_add (gdbarch, save_reggroup);
  2035.   reggroup_add (gdbarch, restore_reggroup);
  2036.   reggroup_add (gdbarch, mep_csr_reggroup);
  2037.   reggroup_add (gdbarch, mep_cr_reggroup);
  2038.   reggroup_add (gdbarch, mep_ccr_reggroup);

  2039.   /* Disassembly.  */
  2040.   set_gdbarch_print_insn (gdbarch, mep_gdb_print_insn);

  2041.   /* Breakpoints.  */
  2042.   set_gdbarch_breakpoint_from_pc (gdbarch, mep_breakpoint_from_pc);
  2043.   set_gdbarch_decr_pc_after_break (gdbarch, 0);
  2044.   set_gdbarch_skip_prologue (gdbarch, mep_skip_prologue);

  2045.   /* Frames and frame unwinding.  */
  2046.   frame_unwind_append_unwinder (gdbarch, &mep_frame_unwind);
  2047.   set_gdbarch_unwind_pc (gdbarch, mep_unwind_pc);
  2048.   set_gdbarch_unwind_sp (gdbarch, mep_unwind_sp);
  2049.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  2050.   set_gdbarch_frame_args_skip (gdbarch, 0);

  2051.   /* Return values.  */
  2052.   set_gdbarch_return_value (gdbarch, mep_return_value);

  2053.   /* Inferior function calls.  */
  2054.   set_gdbarch_frame_align (gdbarch, mep_frame_align);
  2055.   set_gdbarch_push_dummy_call (gdbarch, mep_push_dummy_call);
  2056.   set_gdbarch_dummy_id (gdbarch, mep_dummy_id);

  2057.   return gdbarch;
  2058. }

  2059. /* Provide a prototype to silence -Wmissing-prototypes.  */
  2060. extern initialize_file_ftype _initialize_mep_tdep;

  2061. void
  2062. _initialize_mep_tdep (void)
  2063. {
  2064.   mep_csr_reggroup = reggroup_new ("csr", USER_REGGROUP);
  2065.   mep_cr_reggroup  = reggroup_new ("cr", USER_REGGROUP);
  2066.   mep_ccr_reggroup = reggroup_new ("ccr", USER_REGGROUP);

  2067.   register_gdbarch_init (bfd_arch_mep, mep_gdbarch_init);

  2068.   mep_init_pseudoregister_maps ();
  2069. }