]> Git Repo - binutils.git/blob - gdb/mep-tdep.c
gdb: remove COMPUNIT_BLOCKVECTOR macro, add getter/setter
[binutils.git] / gdb / mep-tdep.c
1 /* Target-dependent code for the Toshiba MeP for GDB, the GNU debugger.
2
3    Copyright (C) 2001-2022 Free Software Foundation, Inc.
4
5    Contributed by Red Hat, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "frame-unwind.h"
25 #include "frame-base.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "gdbcmd.h"
29 #include "gdbcore.h"
30 #include "value.h"
31 #include "inferior.h"
32 #include "dis-asm.h"
33 #include "symfile.h"
34 #include "objfiles.h"
35 #include "language.h"
36 #include "arch-utils.h"
37 #include "regcache.h"
38 #include "remote.h"
39 #include "sim-regno.h"
40 #include "disasm.h"
41 #include "trad-frame.h"
42 #include "reggroups.h"
43 #include "elf-bfd.h"
44 #include "elf/mep.h"
45 #include "prologue-value.h"
46 #include "cgen/bitset.h"
47 #include "infcall.h"
48 #include "gdbarch.h"
49
50 /* Get the user's customized MeP coprocessor register names from
51    libopcodes.  */
52 #include "opcodes/mep-desc.h"
53 #include "opcodes/mep-opc.h"
54
55 \f
56 /* The gdbarch_tdep structure.  */
57
58 /* A quick recap for GDB hackers not familiar with the whole Toshiba
59    Media Processor story:
60
61    The MeP media engine is a configureable processor: users can design
62    their own coprocessors, implement custom instructions, adjust cache
63    sizes, select optional standard facilities like add-and-saturate
64    instructions, and so on.  Then, they can build custom versions of
65    the GNU toolchain to support their customized chips.  The
66    MeP-Integrator program (see utils/mep) takes a GNU toolchain source
67    tree, and a config file pointing to various files provided by the
68    user describing their customizations, and edits the source tree to
69    produce a compiler that can generate their custom instructions, an
70    assembler that can assemble them and recognize their custom
71    register names, and so on.
72
73    Furthermore, the user can actually specify several of these custom
74    configurations, called 'me_modules', and get a toolchain which can
75    produce code for any of them, given a compiler/assembler switch;
76    you say something like 'gcc -mconfig=mm_max' to generate code for
77    the me_module named 'mm_max'.
78
79    GDB, in particular, needs to:
80
81    - use the coprocessor control register names provided by the user
82      in their hardware description, in expressions, 'info register'
83      output, and disassembly,
84
85    - know the number, names, and types of the coprocessor's
86      general-purpose registers, adjust the 'info all-registers' output
87      accordingly, and print error messages if the user refers to one
88      that doesn't exist
89
90    - allow access to the control bus space only when the configuration
91      actually has a control bus, and recognize which regions of the
92      control bus space are actually populated,
93
94    - disassemble using the user's provided mnemonics for their custom
95      instructions, and
96
97    - recognize whether the $hi and $lo registers are present, and
98      allow access to them only when they are actually there.
99
100    There are three sources of information about what sort of me_module
101    we're actually dealing with:
102
103    - A MeP executable file indicates which me_module it was compiled
104      for, and libopcodes has tables describing each module.  So, given
105      an executable file, we can find out about the processor it was
106      compiled for.
107
108    - There are SID command-line options to select a particular
109      me_module, overriding the one specified in the ELF file.  SID
110      provides GDB with a fake read-only register, 'module', which
111      indicates which me_module GDB is communicating with an instance
112      of.
113
114    - There are SID command-line options to enable or disable certain
115      optional processor features, overriding the defaults for the
116      selected me_module.  The MeP $OPT register indicates which
117      options are present on the current processor.  */
118
119
120 struct mep_gdbarch_tdep : gdbarch_tdep
121 {
122   /* A CGEN cpu descriptor for this BFD architecture and machine.
123
124      Note: this is *not* customized for any particular me_module; the
125      MeP libopcodes machinery actually puts off module-specific
126      customization until the last minute.  So this contains
127      information about all supported me_modules.  */
128   CGEN_CPU_DESC cpu_desc = nullptr;
129
130   /* The me_module index from the ELF file we used to select this
131      architecture, or CONFIG_NONE if there was none.
132
133      Note that we should prefer to use the me_module number available
134      via the 'module' register, whenever we're actually talking to a
135      real target.
136
137      In the absence of live information, we'd like to get the
138      me_module number from the ELF file.  But which ELF file: the
139      executable file, the core file, ... ?  The answer is, "the last
140      ELF file we used to set the current architecture".  Thus, we
141      create a separate instance of the gdbarch structure for each
142      me_module value mep_gdbarch_init sees, and store the me_module
143      value from the ELF file here.  */
144   CONFIG_ATTR me_module {};
145 };
146
147
148 \f
149 /* Getting me_module information from the CGEN tables.  */
150
151
152 /* Find an entry in the DESC's hardware table whose name begins with
153    PREFIX, and whose ISA mask intersects COPRO_ISA_MASK, but does not
154    intersect with GENERIC_ISA_MASK.  If there is no matching entry,
155    return zero.  */
156 static const CGEN_HW_ENTRY *
157 find_hw_entry_by_prefix_and_isa (CGEN_CPU_DESC desc,
158                                  const char *prefix,
159                                  CGEN_BITSET *copro_isa_mask,
160                                  CGEN_BITSET *generic_isa_mask)
161 {
162   int prefix_len = strlen (prefix);
163   int i;
164
165   for (i = 0; i < desc->hw_table.num_entries; i++)
166     {
167       const CGEN_HW_ENTRY *hw = desc->hw_table.entries[i];
168       if (strncmp (prefix, hw->name, prefix_len) == 0)
169         {
170           CGEN_BITSET *hw_isa_mask
171             = ((CGEN_BITSET *)
172                &CGEN_ATTR_CGEN_HW_ISA_VALUE (CGEN_HW_ATTRS (hw)));
173
174           if (cgen_bitset_intersect_p (hw_isa_mask, copro_isa_mask)
175               && ! cgen_bitset_intersect_p (hw_isa_mask, generic_isa_mask))
176             return hw;
177         }
178     }
179
180   return 0;
181 }
182
183
184 /* Find an entry in DESC's hardware table whose type is TYPE.  Return
185    zero if there is none.  */
186 static const CGEN_HW_ENTRY *
187 find_hw_entry_by_type (CGEN_CPU_DESC desc, CGEN_HW_TYPE type)
188 {
189   int i;
190
191   for (i = 0; i < desc->hw_table.num_entries; i++)
192     {
193       const CGEN_HW_ENTRY *hw = desc->hw_table.entries[i];
194
195       if (hw->type == type)
196         return hw;
197     }
198
199   return 0;
200 }
201
202
203 /* Return the CGEN hardware table entry for the coprocessor register
204    set for ME_MODULE, whose name prefix is PREFIX.  If ME_MODULE has
205    no such register set, return zero.  If ME_MODULE is the generic
206    me_module CONFIG_NONE, return the table entry for the register set
207    whose hardware type is GENERIC_TYPE.  */
208 static const CGEN_HW_ENTRY *
209 me_module_register_set (CONFIG_ATTR me_module,
210                         const char *prefix,
211                         CGEN_HW_TYPE generic_type)
212 {
213   /* This is kind of tricky, because the hardware table is constructed
214      in a way that isn't very helpful.  Perhaps we can fix that, but
215      here's how it works at the moment:
216
217      The configuration map, `mep_config_map', is indexed by me_module
218      number, and indicates which coprocessor and core ISAs that
219      me_module supports.  The 'core_isa' mask includes all the core
220      ISAs, and the 'cop_isa' mask includes all the coprocessor ISAs.
221      The entry for the generic me_module, CONFIG_NONE, has an empty
222      'cop_isa', and its 'core_isa' selects only the standard MeP
223      instruction set.
224
225      The CGEN CPU descriptor's hardware table, desc->hw_table, has
226      entries for all the register sets, for all me_modules.  Each
227      entry has a mask indicating which ISAs use that register set.
228      So, if an me_module supports some coprocessor ISA, we can find
229      applicable register sets by scanning the hardware table for
230      register sets whose masks include (at least some of) those ISAs.
231
232      Each hardware table entry also has a name, whose prefix says
233      whether it's a general-purpose ("h-cr") or control ("h-ccr")
234      coprocessor register set.  It might be nicer to have an attribute
235      indicating what sort of register set it was, that we could use
236      instead of pattern-matching on the name.
237
238      When there is no hardware table entry whose mask includes a
239      particular coprocessor ISA and whose name starts with a given
240      prefix, then that means that that coprocessor doesn't have any
241      registers of that type.  In such cases, this function must return
242      a null pointer.
243
244      Coprocessor register sets' masks may or may not include the core
245      ISA for the me_module they belong to.  Those generated by a2cgen
246      do, but the sample me_module included in the unconfigured tree,
247      'ccfx', does not.
248
249      There are generic coprocessor register sets, intended only for
250      use with the generic me_module.  Unfortunately, their masks
251      include *all* ISAs --- even those for coprocessors that don't
252      have such register sets.  This makes detecting the case where a
253      coprocessor lacks a particular register set more complicated.
254
255      So, here's the approach we take:
256
257      - For CONFIG_NONE, we return the generic coprocessor register set.
258
259      - For any other me_module, we search for a register set whose
260        mask contains any of the me_module's coprocessor ISAs,
261        specifically excluding the generic coprocessor register sets.  */
262
263   mep_gdbarch_tdep *tdep
264     = (mep_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
265   CGEN_CPU_DESC desc = tdep->cpu_desc;
266   const CGEN_HW_ENTRY *hw;
267
268   if (me_module == CONFIG_NONE)
269     hw = find_hw_entry_by_type (desc, generic_type);
270   else
271     {
272       CGEN_BITSET *cop = &mep_config_map[me_module].cop_isa;
273       CGEN_BITSET *core = &mep_config_map[me_module].core_isa;
274       CGEN_BITSET *generic = &mep_config_map[CONFIG_NONE].core_isa;
275       CGEN_BITSET *cop_and_core;
276
277       /* The coprocessor ISAs include the ISA for the specific core which
278          has that coprocessor.  */
279       cop_and_core = cgen_bitset_copy (cop);
280       cgen_bitset_union (cop, core, cop_and_core);
281       hw = find_hw_entry_by_prefix_and_isa (desc, prefix, cop_and_core, generic);
282     }
283
284   return hw;
285 }
286
287
288 /* Given a hardware table entry HW representing a register set, return
289    a pointer to the keyword table with all the register names.  If HW
290    is NULL, return NULL, to propagate the "no such register set" info
291    along.  */
292 static CGEN_KEYWORD *
293 register_set_keyword_table (const CGEN_HW_ENTRY *hw)
294 {
295   if (! hw)
296     return NULL;
297
298   /* Check that HW is actually a keyword table.  */
299   gdb_assert (hw->asm_type == CGEN_ASM_KEYWORD);
300
301   /* The 'asm_data' field of a register set's hardware table entry
302      refers to a keyword table.  */
303   return (CGEN_KEYWORD *) hw->asm_data;
304 }
305
306
307 /* Given a keyword table KEYWORD and a register number REGNUM, return
308    the name of the register, or "" if KEYWORD contains no register
309    whose number is REGNUM.  */
310 static const char *
311 register_name_from_keyword (CGEN_KEYWORD *keyword_table, int regnum)
312 {
313   const CGEN_KEYWORD_ENTRY *entry
314     = cgen_keyword_lookup_value (keyword_table, regnum);
315
316   if (entry)
317     {
318       char *name = entry->name;
319
320       /* The CGEN keyword entries for register names include the
321          leading $, which appears in MeP assembly as well as in GDB.
322          But we don't want to return that; GDB core code adds that
323          itself.  */
324       if (name[0] == '$')
325         name++;
326
327       return name;
328     }
329   else
330     return "";
331 }
332
333   
334 /* Masks for option bits in the OPT special-purpose register.  */
335 enum {
336   MEP_OPT_DIV = 1 << 25,        /* 32-bit divide instruction option */
337   MEP_OPT_MUL = 1 << 24,        /* 32-bit multiply instruction option */
338   MEP_OPT_BIT = 1 << 23,        /* bit manipulation instruction option */
339   MEP_OPT_SAT = 1 << 22,        /* saturation instruction option */
340   MEP_OPT_CLP = 1 << 21,        /* clip instruction option */
341   MEP_OPT_MIN = 1 << 20,        /* min/max instruction option */
342   MEP_OPT_AVE = 1 << 19,        /* average instruction option */
343   MEP_OPT_ABS = 1 << 18,        /* absolute difference instruction option */
344   MEP_OPT_LDZ = 1 << 16,        /* leading zero instruction option */
345   MEP_OPT_VL64 = 1 << 6,        /* 64-bit VLIW operation mode option */
346   MEP_OPT_VL32 = 1 << 5,        /* 32-bit VLIW operation mode option */
347   MEP_OPT_COP = 1 << 4,         /* coprocessor option */
348   MEP_OPT_DSP = 1 << 2,         /* DSP option */
349   MEP_OPT_UCI = 1 << 1,         /* UCI option */
350   MEP_OPT_DBG = 1 << 0,         /* DBG function option */
351 };
352
353
354 /* Given the option_mask value for a particular entry in
355    mep_config_map, produce the value the processor's OPT register
356    would use to represent the same set of options.  */
357 static unsigned int
358 opt_from_option_mask (unsigned int option_mask)
359 {
360   /* A table mapping OPT register bits onto CGEN config map option
361      bits.  */
362   struct {
363     unsigned int opt_bit, option_mask_bit;
364   } bits[] = {
365     { MEP_OPT_DIV, 1 << CGEN_INSN_OPTIONAL_DIV_INSN },
366     { MEP_OPT_MUL, 1 << CGEN_INSN_OPTIONAL_MUL_INSN },
367     { MEP_OPT_DIV, 1 << CGEN_INSN_OPTIONAL_DIV_INSN },
368     { MEP_OPT_DBG, 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN },
369     { MEP_OPT_LDZ, 1 << CGEN_INSN_OPTIONAL_LDZ_INSN },
370     { MEP_OPT_ABS, 1 << CGEN_INSN_OPTIONAL_ABS_INSN },
371     { MEP_OPT_AVE, 1 << CGEN_INSN_OPTIONAL_AVE_INSN },
372     { MEP_OPT_MIN, 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN },
373     { MEP_OPT_CLP, 1 << CGEN_INSN_OPTIONAL_CLIP_INSN },
374     { MEP_OPT_SAT, 1 << CGEN_INSN_OPTIONAL_SAT_INSN },
375     { MEP_OPT_UCI, 1 << CGEN_INSN_OPTIONAL_UCI_INSN },
376     { MEP_OPT_DSP, 1 << CGEN_INSN_OPTIONAL_DSP_INSN },
377     { MEP_OPT_COP, 1 << CGEN_INSN_OPTIONAL_CP_INSN },
378   };
379
380   int i;
381   unsigned int opt = 0;
382
383   for (i = 0; i < (sizeof (bits) / sizeof (bits[0])); i++)
384     if (option_mask & bits[i].option_mask_bit)
385       opt |= bits[i].opt_bit;
386
387   return opt;
388 }
389
390
391 /* Return the value the $OPT register would use to represent the set
392    of options for ME_MODULE.  */
393 static unsigned int
394 me_module_opt (CONFIG_ATTR me_module)
395 {
396   return opt_from_option_mask (mep_config_map[me_module].option_mask);
397 }
398
399
400 /* Return the width of ME_MODULE's coprocessor data bus, in bits.
401    This is either 32 or 64.  */
402 static int
403 me_module_cop_data_bus_width (CONFIG_ATTR me_module)
404 {
405   if (mep_config_map[me_module].option_mask
406       & (1 << CGEN_INSN_OPTIONAL_CP64_INSN))
407     return 64;
408   else
409     return 32;
410 }
411
412
413 /* Return true if ME_MODULE is big-endian, false otherwise.  */
414 static int
415 me_module_big_endian (CONFIG_ATTR me_module)
416 {
417   return mep_config_map[me_module].big_endian;
418 }
419
420
421 /* Return the name of ME_MODULE, or NULL if it has no name.  */
422 static const char *
423 me_module_name (CONFIG_ATTR me_module)
424 {
425   /* The default me_module has "" as its name, but it's easier for our
426      callers to test for NULL.  */
427   if (! mep_config_map[me_module].name
428       || mep_config_map[me_module].name[0] == '\0')
429     return NULL;
430   else
431     return mep_config_map[me_module].name;
432 }
433 \f
434 /* Register set.  */
435
436
437 /* The MeP spec defines the following registers:
438    16 general purpose registers (r0-r15) 
439    32 control/special registers (csr0-csr31)
440    32 coprocessor general-purpose registers (c0 -- c31)
441    64 coprocessor control registers (ccr0 -- ccr63)
442
443    For the raw registers, we assign numbers here explicitly, instead
444    of letting the enum assign them for us; the numbers are a matter of
445    external protocol, and shouldn't shift around as things are edited.
446
447    We access the control/special registers via pseudoregisters, to
448    enforce read-only portions that some registers have.
449
450    We access the coprocessor general purpose and control registers via
451    pseudoregisters, to make sure they appear in the proper order in
452    the 'info all-registers' command (which uses the register number
453    ordering), and also to allow them to be renamed and resized
454    depending on the me_module in use.
455
456    The MeP allows coprocessor general-purpose registers to be either
457    32 or 64 bits long, depending on the configuration.  Since we don't
458    want the format of the 'g' packet to vary from one core to another,
459    the raw coprocessor GPRs are always 64 bits.  GDB doesn't allow the
460    types of registers to change (see the implementation of
461    register_type), so we have four banks of pseudoregisters for the
462    coprocessor gprs --- 32-bit vs. 64-bit, and integer
463    vs. floating-point --- and we show or hide them depending on the
464    configuration.  */
465 enum
466 {
467   MEP_FIRST_RAW_REGNUM = 0,
468
469   MEP_FIRST_GPR_REGNUM = 0,
470   MEP_R0_REGNUM = 0,
471   MEP_R1_REGNUM = 1,
472   MEP_R2_REGNUM = 2,
473   MEP_R3_REGNUM = 3,
474   MEP_R4_REGNUM = 4,
475   MEP_R5_REGNUM = 5,
476   MEP_R6_REGNUM = 6,
477   MEP_R7_REGNUM = 7,
478   MEP_R8_REGNUM = 8,
479   MEP_R9_REGNUM = 9,
480   MEP_R10_REGNUM = 10,
481   MEP_R11_REGNUM = 11,
482   MEP_R12_REGNUM = 12,
483   MEP_FP_REGNUM = MEP_R8_REGNUM,
484   MEP_R13_REGNUM = 13,
485   MEP_TP_REGNUM = MEP_R13_REGNUM,       /* (r13) Tiny data pointer */
486   MEP_R14_REGNUM = 14,
487   MEP_GP_REGNUM = MEP_R14_REGNUM,       /* (r14) Global pointer */
488   MEP_R15_REGNUM = 15,
489   MEP_SP_REGNUM = MEP_R15_REGNUM,       /* (r15) Stack pointer */
490   MEP_LAST_GPR_REGNUM = MEP_R15_REGNUM,
491
492   /* The raw control registers.  These are the values as received via
493      the remote protocol, directly from the target; we only let user
494      code touch the via the pseudoregisters, which enforce read-only
495      bits.  */
496   MEP_FIRST_RAW_CSR_REGNUM = 16,
497   MEP_RAW_PC_REGNUM    = 16,    /* Program counter */
498   MEP_RAW_LP_REGNUM    = 17,    /* Link pointer */
499   MEP_RAW_SAR_REGNUM   = 18,    /* Raw shift amount */
500   MEP_RAW_CSR3_REGNUM  = 19,    /* csr3: reserved */
501   MEP_RAW_RPB_REGNUM   = 20,    /* Raw repeat begin address */
502   MEP_RAW_RPE_REGNUM   = 21,    /* Repeat end address */
503   MEP_RAW_RPC_REGNUM   = 22,    /* Repeat count */
504   MEP_RAW_HI_REGNUM    = 23, /* Upper 32 bits of result of 64 bit mult/div */
505   MEP_RAW_LO_REGNUM    = 24, /* Lower 32 bits of result of 64 bit mult/div */
506   MEP_RAW_CSR9_REGNUM  = 25,    /* csr3: reserved */
507   MEP_RAW_CSR10_REGNUM = 26,    /* csr3: reserved */
508   MEP_RAW_CSR11_REGNUM = 27,    /* csr3: reserved */
509   MEP_RAW_MB0_REGNUM   = 28,    /* Raw modulo begin address 0 */
510   MEP_RAW_ME0_REGNUM   = 29,    /* Raw modulo end address 0 */
511   MEP_RAW_MB1_REGNUM   = 30,    /* Raw modulo begin address 1 */
512   MEP_RAW_ME1_REGNUM   = 31,    /* Raw modulo end address 1 */
513   MEP_RAW_PSW_REGNUM   = 32,    /* Raw program status word */
514   MEP_RAW_ID_REGNUM    = 33,    /* Raw processor ID/revision */
515   MEP_RAW_TMP_REGNUM   = 34,    /* Temporary */
516   MEP_RAW_EPC_REGNUM   = 35,    /* Exception program counter */
517   MEP_RAW_EXC_REGNUM   = 36,    /* Raw exception cause */
518   MEP_RAW_CFG_REGNUM   = 37,    /* Raw processor configuration*/
519   MEP_RAW_CSR22_REGNUM = 38,    /* csr3: reserved */
520   MEP_RAW_NPC_REGNUM   = 39,    /* Nonmaskable interrupt PC */
521   MEP_RAW_DBG_REGNUM   = 40,    /* Raw debug */
522   MEP_RAW_DEPC_REGNUM  = 41,    /* Debug exception PC */
523   MEP_RAW_OPT_REGNUM   = 42,    /* Raw options */
524   MEP_RAW_RCFG_REGNUM  = 43,    /* Raw local ram config */
525   MEP_RAW_CCFG_REGNUM  = 44,    /* Raw cache config */
526   MEP_RAW_CSR29_REGNUM = 45,    /* csr3: reserved */
527   MEP_RAW_CSR30_REGNUM = 46,    /* csr3: reserved */
528   MEP_RAW_CSR31_REGNUM = 47,    /* csr3: reserved */
529   MEP_LAST_RAW_CSR_REGNUM = MEP_RAW_CSR31_REGNUM,
530
531   /* The raw coprocessor general-purpose registers.  These are all 64
532      bits wide.  */
533   MEP_FIRST_RAW_CR_REGNUM = 48,
534   MEP_LAST_RAW_CR_REGNUM = MEP_FIRST_RAW_CR_REGNUM + 31,
535
536   MEP_FIRST_RAW_CCR_REGNUM = 80,
537   MEP_LAST_RAW_CCR_REGNUM = MEP_FIRST_RAW_CCR_REGNUM + 63,
538
539   /* The module number register.  This is the index of the me_module
540      of which the current target is an instance.  (This is not a real
541      MeP-specified register; it's provided by SID.)  */
542   MEP_MODULE_REGNUM,
543
544   MEP_LAST_RAW_REGNUM = MEP_MODULE_REGNUM,
545
546   MEP_NUM_RAW_REGS = MEP_LAST_RAW_REGNUM + 1,
547
548   /* Pseudoregisters.  See mep_pseudo_register_read and
549      mep_pseudo_register_write.  */
550   MEP_FIRST_PSEUDO_REGNUM = MEP_NUM_RAW_REGS,
551
552   /* We have a pseudoregister for every control/special register, to
553      implement registers with read-only bits.  */
554   MEP_FIRST_CSR_REGNUM = MEP_FIRST_PSEUDO_REGNUM,
555   MEP_PC_REGNUM = MEP_FIRST_CSR_REGNUM, /* Program counter */
556   MEP_LP_REGNUM,                /* Link pointer */
557   MEP_SAR_REGNUM,               /* shift amount */
558   MEP_CSR3_REGNUM,              /* csr3: reserved */
559   MEP_RPB_REGNUM,               /* repeat begin address */
560   MEP_RPE_REGNUM,               /* Repeat end address */
561   MEP_RPC_REGNUM,               /* Repeat count */
562   MEP_HI_REGNUM,  /* Upper 32 bits of the result of 64 bit mult/div */
563   MEP_LO_REGNUM,  /* Lower 32 bits of the result of 64 bit mult/div */
564   MEP_CSR9_REGNUM,              /* csr3: reserved */
565   MEP_CSR10_REGNUM,             /* csr3: reserved */
566   MEP_CSR11_REGNUM,             /* csr3: reserved */
567   MEP_MB0_REGNUM,               /* modulo begin address 0 */
568   MEP_ME0_REGNUM,               /* modulo end address 0 */
569   MEP_MB1_REGNUM,               /* modulo begin address 1 */
570   MEP_ME1_REGNUM,               /* modulo end address 1 */
571   MEP_PSW_REGNUM,               /* program status word */
572   MEP_ID_REGNUM,                /* processor ID/revision */
573   MEP_TMP_REGNUM,               /* Temporary */
574   MEP_EPC_REGNUM,               /* Exception program counter */
575   MEP_EXC_REGNUM,               /* exception cause */
576   MEP_CFG_REGNUM,               /* processor configuration*/
577   MEP_CSR22_REGNUM,             /* csr3: reserved */
578   MEP_NPC_REGNUM,               /* Nonmaskable interrupt PC */
579   MEP_DBG_REGNUM,               /* debug */
580   MEP_DEPC_REGNUM,              /* Debug exception PC */
581   MEP_OPT_REGNUM,               /* options */
582   MEP_RCFG_REGNUM,              /* local ram config */
583   MEP_CCFG_REGNUM,              /* cache config */
584   MEP_CSR29_REGNUM,             /* csr3: reserved */
585   MEP_CSR30_REGNUM,             /* csr3: reserved */
586   MEP_CSR31_REGNUM,             /* csr3: reserved */
587   MEP_LAST_CSR_REGNUM = MEP_CSR31_REGNUM,
588
589   /* The 32-bit integer view of the coprocessor GPR's.  */
590   MEP_FIRST_CR32_REGNUM,
591   MEP_LAST_CR32_REGNUM = MEP_FIRST_CR32_REGNUM + 31,
592
593   /* The 32-bit floating-point view of the coprocessor GPR's.  */
594   MEP_FIRST_FP_CR32_REGNUM,
595   MEP_LAST_FP_CR32_REGNUM = MEP_FIRST_FP_CR32_REGNUM + 31,
596
597   /* The 64-bit integer view of the coprocessor GPR's.  */
598   MEP_FIRST_CR64_REGNUM,
599   MEP_LAST_CR64_REGNUM = MEP_FIRST_CR64_REGNUM + 31,
600
601   /* The 64-bit floating-point view of the coprocessor GPR's.  */
602   MEP_FIRST_FP_CR64_REGNUM,
603   MEP_LAST_FP_CR64_REGNUM = MEP_FIRST_FP_CR64_REGNUM + 31,
604
605   MEP_FIRST_CCR_REGNUM,
606   MEP_LAST_CCR_REGNUM = MEP_FIRST_CCR_REGNUM + 63,
607
608   MEP_LAST_PSEUDO_REGNUM = MEP_LAST_CCR_REGNUM,
609
610   MEP_NUM_PSEUDO_REGS = (MEP_LAST_PSEUDO_REGNUM - MEP_LAST_RAW_REGNUM),
611
612   MEP_NUM_REGS = MEP_NUM_RAW_REGS + MEP_NUM_PSEUDO_REGS
613 };
614
615
616 #define IN_SET(set, n) \
617   (MEP_FIRST_ ## set ## _REGNUM <= (n) && (n) <= MEP_LAST_ ## set ## _REGNUM)
618
619 #define IS_GPR_REGNUM(n)     (IN_SET (GPR,     (n)))
620 #define IS_RAW_CSR_REGNUM(n) (IN_SET (RAW_CSR, (n)))
621 #define IS_RAW_CR_REGNUM(n)  (IN_SET (RAW_CR,  (n)))
622 #define IS_RAW_CCR_REGNUM(n) (IN_SET (RAW_CCR, (n)))
623
624 #define IS_CSR_REGNUM(n)     (IN_SET (CSR,     (n)))
625 #define IS_CR32_REGNUM(n)    (IN_SET (CR32,    (n)))
626 #define IS_FP_CR32_REGNUM(n) (IN_SET (FP_CR32, (n)))
627 #define IS_CR64_REGNUM(n)    (IN_SET (CR64,    (n)))
628 #define IS_FP_CR64_REGNUM(n) (IN_SET (FP_CR64, (n)))
629 #define IS_CR_REGNUM(n)      (IS_CR32_REGNUM (n) || IS_FP_CR32_REGNUM (n) \
630                               || IS_CR64_REGNUM (n) || IS_FP_CR64_REGNUM (n))
631 #define IS_CCR_REGNUM(n)     (IN_SET (CCR,     (n)))
632
633 #define IS_RAW_REGNUM(n)     (IN_SET (RAW,     (n)))
634 #define IS_PSEUDO_REGNUM(n)  (IN_SET (PSEUDO,  (n)))
635
636 #define NUM_REGS_IN_SET(set) \
637   (MEP_LAST_ ## set ## _REGNUM - MEP_FIRST_ ## set ## _REGNUM + 1)
638
639 #define MEP_GPR_SIZE (4)        /* Size of a MeP general-purpose register.  */
640 #define MEP_PSW_SIZE (4)        /* Size of the PSW register.  */
641 #define MEP_LP_SIZE (4)         /* Size of the LP register.  */
642
643
644 /* Many of the control/special registers contain bits that cannot be
645    written to; some are entirely read-only.  So we present them all as
646    pseudoregisters.
647
648    The following table describes the special properties of each CSR.  */
649 struct mep_csr_register
650 {
651   /* The number of this CSR's raw register.  */
652   int raw;
653
654   /* The number of this CSR's pseudoregister.  */
655   int pseudo;
656
657   /* A mask of the bits that are writeable: if a bit is set here, then
658      it can be modified; if the bit is clear, then it cannot.  */
659   LONGEST writeable_bits;
660 };
661
662
663 /* mep_csr_registers[i] describes the i'th CSR.
664    We just list the register numbers here explicitly to help catch
665    typos.  */
666 #define CSR(name) MEP_RAW_ ## name ## _REGNUM, MEP_ ## name ## _REGNUM
667 static mep_csr_register mep_csr_registers[] = {
668   { CSR(PC),    0xffffffff },   /* manual says r/o, but we can write it */
669   { CSR(LP),    0xffffffff },
670   { CSR(SAR),   0x0000003f },
671   { CSR(CSR3),  0xffffffff },
672   { CSR(RPB),   0xfffffffe },
673   { CSR(RPE),   0xffffffff },
674   { CSR(RPC),   0xffffffff },
675   { CSR(HI),    0xffffffff },
676   { CSR(LO),    0xffffffff },
677   { CSR(CSR9),  0xffffffff },
678   { CSR(CSR10), 0xffffffff },
679   { CSR(CSR11), 0xffffffff },
680   { CSR(MB0),   0x0000ffff },
681   { CSR(ME0),   0x0000ffff },
682   { CSR(MB1),   0x0000ffff },
683   { CSR(ME1),   0x0000ffff },
684   { CSR(PSW),   0x000003ff },
685   { CSR(ID),    0x00000000 },
686   { CSR(TMP),   0xffffffff },
687   { CSR(EPC),   0xffffffff },
688   { CSR(EXC),   0x000030f0 },
689   { CSR(CFG),   0x00c0001b },
690   { CSR(CSR22), 0xffffffff },
691   { CSR(NPC),   0xffffffff },
692   { CSR(DBG),   0x00000580 },
693   { CSR(DEPC),  0xffffffff },
694   { CSR(OPT),   0x00000000 },
695   { CSR(RCFG),  0x00000000 },
696   { CSR(CCFG),  0x00000000 },
697   { CSR(CSR29), 0xffffffff },
698   { CSR(CSR30), 0xffffffff },
699   { CSR(CSR31), 0xffffffff },
700 };
701
702
703 /* If R is the number of a raw register, then mep_raw_to_pseudo[R] is
704    the number of the corresponding pseudoregister.  Otherwise,
705    mep_raw_to_pseudo[R] == R.  */
706 static int mep_raw_to_pseudo[MEP_NUM_REGS];
707
708 /* If R is the number of a pseudoregister, then mep_pseudo_to_raw[R]
709    is the number of the underlying raw register.  Otherwise
710    mep_pseudo_to_raw[R] == R.  */
711 static int mep_pseudo_to_raw[MEP_NUM_REGS];
712
713 static void
714 mep_init_pseudoregister_maps (void)
715 {
716   int i;
717
718   /* Verify that mep_csr_registers covers all the CSRs, in order.  */
719   gdb_assert (ARRAY_SIZE (mep_csr_registers) == NUM_REGS_IN_SET (CSR));
720   gdb_assert (ARRAY_SIZE (mep_csr_registers) == NUM_REGS_IN_SET (RAW_CSR));
721
722   /* Verify that the raw and pseudo ranges have matching sizes.  */
723   gdb_assert (NUM_REGS_IN_SET (RAW_CSR) == NUM_REGS_IN_SET (CSR));
724   gdb_assert (NUM_REGS_IN_SET (RAW_CR)  == NUM_REGS_IN_SET (CR32));
725   gdb_assert (NUM_REGS_IN_SET (RAW_CR)  == NUM_REGS_IN_SET (CR64));
726   gdb_assert (NUM_REGS_IN_SET (RAW_CCR) == NUM_REGS_IN_SET (CCR));
727
728   for (i = 0; i < ARRAY_SIZE (mep_csr_registers); i++)
729     {
730       struct mep_csr_register *r = &mep_csr_registers[i];
731
732       gdb_assert (r->pseudo == MEP_FIRST_CSR_REGNUM + i);
733       gdb_assert (r->raw    == MEP_FIRST_RAW_CSR_REGNUM + i);
734     }
735
736   /* Set up the initial  raw<->pseudo mappings.  */
737   for (i = 0; i < MEP_NUM_REGS; i++)
738     {
739       mep_raw_to_pseudo[i] = i;
740       mep_pseudo_to_raw[i] = i;
741     }
742
743   /* Add the CSR raw<->pseudo mappings.  */
744   for (i = 0; i < ARRAY_SIZE (mep_csr_registers); i++)
745     {
746       struct mep_csr_register *r = &mep_csr_registers[i];
747
748       mep_raw_to_pseudo[r->raw] = r->pseudo;
749       mep_pseudo_to_raw[r->pseudo] = r->raw;
750     }
751
752   /* Add the CR raw<->pseudo mappings.  */
753   for (i = 0; i < NUM_REGS_IN_SET (RAW_CR); i++)
754     {
755       int raw = MEP_FIRST_RAW_CR_REGNUM + i;
756       int pseudo32 = MEP_FIRST_CR32_REGNUM + i;
757       int pseudofp32 = MEP_FIRST_FP_CR32_REGNUM + i;
758       int pseudo64 = MEP_FIRST_CR64_REGNUM + i;
759       int pseudofp64 = MEP_FIRST_FP_CR64_REGNUM + i;
760
761       /* Truly, the raw->pseudo mapping depends on the current module.
762          But we use the raw->pseudo mapping when we read the debugging
763          info; at that point, we don't know what module we'll actually
764          be running yet.  So, we always supply the 64-bit register
765          numbers; GDB knows how to pick a smaller value out of a
766          larger register properly.  */
767       mep_raw_to_pseudo[raw] = pseudo64;
768       mep_pseudo_to_raw[pseudo32] = raw;
769       mep_pseudo_to_raw[pseudofp32] = raw;
770       mep_pseudo_to_raw[pseudo64] = raw;
771       mep_pseudo_to_raw[pseudofp64] = raw;
772     }
773
774   /* Add the CCR raw<->pseudo mappings.  */
775   for (i = 0; i < NUM_REGS_IN_SET (CCR); i++)
776     {
777       int raw = MEP_FIRST_RAW_CCR_REGNUM + i;
778       int pseudo = MEP_FIRST_CCR_REGNUM + i;
779       mep_raw_to_pseudo[raw] = pseudo;
780       mep_pseudo_to_raw[pseudo] = raw;
781     }
782 }
783
784
785 static int
786 mep_debug_reg_to_regnum (struct gdbarch *gdbarch, int debug_reg)
787 {
788   /* The debug info uses the raw register numbers.  */
789   if (debug_reg >= 0 && debug_reg < ARRAY_SIZE (mep_raw_to_pseudo))
790     return mep_raw_to_pseudo[debug_reg];
791   return -1;
792 }
793
794
795 /* Return the size, in bits, of the coprocessor pseudoregister
796    numbered PSEUDO.  */
797 static int
798 mep_pseudo_cr_size (int pseudo)
799 {
800   if (IS_CR32_REGNUM (pseudo)
801       || IS_FP_CR32_REGNUM (pseudo))
802     return 32;
803   else if (IS_CR64_REGNUM (pseudo)
804            || IS_FP_CR64_REGNUM (pseudo))
805     return 64;
806   else
807     gdb_assert_not_reached ("unexpected coprocessor pseudo register");
808 }
809
810
811 /* If the coprocessor pseudoregister numbered PSEUDO is a
812    floating-point register, return non-zero; if it is an integer
813    register, return zero.  */
814 static int
815 mep_pseudo_cr_is_float (int pseudo)
816 {
817   return (IS_FP_CR32_REGNUM (pseudo)
818           || IS_FP_CR64_REGNUM (pseudo));
819 }
820
821
822 /* Given a coprocessor GPR pseudoregister number, return its index
823    within that register bank.  */
824 static int
825 mep_pseudo_cr_index (int pseudo)
826 {
827   if (IS_CR32_REGNUM (pseudo))
828     return pseudo - MEP_FIRST_CR32_REGNUM;
829   else if (IS_FP_CR32_REGNUM (pseudo))
830       return pseudo - MEP_FIRST_FP_CR32_REGNUM;
831   else if (IS_CR64_REGNUM (pseudo))
832       return pseudo - MEP_FIRST_CR64_REGNUM;
833   else if (IS_FP_CR64_REGNUM (pseudo))
834       return pseudo - MEP_FIRST_FP_CR64_REGNUM;
835   else
836     gdb_assert_not_reached ("unexpected coprocessor pseudo register");
837 }
838
839
840 /* Return the me_module index describing the current target.
841
842    If the current target has registers (e.g., simulator, remote
843    target), then this uses the value of the 'module' register, raw
844    register MEP_MODULE_REGNUM.  Otherwise, this retrieves the value
845    from the ELF header's e_flags field of the current executable
846    file.  */
847 static CONFIG_ATTR
848 current_me_module (void)
849 {
850   if (target_has_registers ())
851     {
852       ULONGEST regval;
853       regcache_cooked_read_unsigned (get_current_regcache (),
854                                      MEP_MODULE_REGNUM, &regval);
855       return (CONFIG_ATTR) regval;
856     }
857   else
858     {
859       mep_gdbarch_tdep *tdep
860         = (mep_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
861       return tdep->me_module;
862     }
863 }
864
865
866 /* Return the set of options for the current target, in the form that
867    the OPT register would use.
868
869    If the current target has registers (e.g., simulator, remote
870    target), then this is the actual value of the OPT register.  If the
871    current target does not have registers (e.g., an executable file),
872    then use the 'module_opt' field we computed when we build the
873    gdbarch object for this module.  */
874 static unsigned int
875 current_options (void)
876 {
877   if (target_has_registers ())
878     {
879       ULONGEST regval;
880       regcache_cooked_read_unsigned (get_current_regcache (),
881                                      MEP_OPT_REGNUM, &regval);
882       return regval;
883     }
884   else
885     return me_module_opt (current_me_module ());
886 }
887
888
889 /* Return the width of the current me_module's coprocessor data bus,
890    in bits.  This is either 32 or 64.  */
891 static int
892 current_cop_data_bus_width (void)
893 {
894   return me_module_cop_data_bus_width (current_me_module ());
895 }
896
897
898 /* Return the keyword table of coprocessor general-purpose register
899    names appropriate for the me_module we're dealing with.  */
900 static CGEN_KEYWORD *
901 current_cr_names (void)
902 {
903   const CGEN_HW_ENTRY *hw
904     = me_module_register_set (current_me_module (), "h-cr-", HW_H_CR);
905
906   return register_set_keyword_table (hw);
907 }
908
909
910 /* Return non-zero if the coprocessor general-purpose registers are
911    floating-point values, zero otherwise.  */
912 static int
913 current_cr_is_float (void)
914 {
915   const CGEN_HW_ENTRY *hw
916     = me_module_register_set (current_me_module (), "h-cr-", HW_H_CR);
917
918   return CGEN_ATTR_CGEN_HW_IS_FLOAT_VALUE (CGEN_HW_ATTRS (hw));
919 }
920
921
922 /* Return the keyword table of coprocessor control register names
923    appropriate for the me_module we're dealing with.  */
924 static CGEN_KEYWORD *
925 current_ccr_names (void)
926 {
927   const CGEN_HW_ENTRY *hw
928     = me_module_register_set (current_me_module (), "h-ccr-", HW_H_CCR);
929
930   return register_set_keyword_table (hw);
931 }
932
933
934 static const char *
935 mep_register_name (struct gdbarch *gdbarch, int regnr)
936 {
937   /* General-purpose registers.  */
938   static const char *gpr_names[] = {
939     "r0",   "r1",   "r2",   "r3",   /* 0 */
940     "r4",   "r5",   "r6",   "r7",   /* 4 */
941     "fp",   "r9",   "r10",  "r11",  /* 8 */
942     "r12",  "tp",   "gp",   "sp"    /* 12 */
943   };
944
945   /* Special-purpose registers.  */
946   static const char *csr_names[] = {
947     "pc",   "lp",   "sar",  "",     /* 0  csr3: reserved */ 
948     "rpb",  "rpe",  "rpc",  "hi",   /* 4 */
949     "lo",   "",     "",     "",     /* 8  csr9-csr11: reserved */
950     "mb0",  "me0",  "mb1",  "me1",  /* 12 */
951
952     "psw",  "id",   "tmp",  "epc",  /* 16 */
953     "exc",  "cfg",  "",     "npc",  /* 20  csr22: reserved */
954     "dbg",  "depc", "opt",  "rcfg", /* 24 */
955     "ccfg", "",     "",     ""      /* 28  csr29-csr31: reserved */
956   };
957
958   if (IS_GPR_REGNUM (regnr))
959     return gpr_names[regnr - MEP_R0_REGNUM];
960   else if (IS_CSR_REGNUM (regnr))
961     {
962       /* The 'hi' and 'lo' registers are only present on processors
963          that have the 'MUL' or 'DIV' instructions enabled.  */
964       if ((regnr == MEP_HI_REGNUM || regnr == MEP_LO_REGNUM)
965           && (! (current_options () & (MEP_OPT_MUL | MEP_OPT_DIV))))
966         return "";
967
968       return csr_names[regnr - MEP_FIRST_CSR_REGNUM];
969     }
970   else if (IS_CR_REGNUM (regnr))
971     {
972       CGEN_KEYWORD *names;
973       int cr_size;
974       int cr_is_float;
975
976       /* Does this module have a coprocessor at all?  */
977       if (! (current_options () & MEP_OPT_COP))
978         return "";
979
980       names = current_cr_names ();
981       if (! names)
982         /* This module's coprocessor has no general-purpose registers.  */
983         return "";
984
985       cr_size = current_cop_data_bus_width ();
986       if (cr_size != mep_pseudo_cr_size (regnr))
987         /* This module's coprocessor's GPR's are of a different size.  */
988         return "";
989
990       cr_is_float = current_cr_is_float ();
991       /* The extra ! operators ensure we get boolean equality, not
992          numeric equality.  */
993       if (! cr_is_float != ! mep_pseudo_cr_is_float (regnr))
994         /* This module's coprocessor's GPR's are of a different type.  */
995         return "";
996
997       return register_name_from_keyword (names, mep_pseudo_cr_index (regnr));
998     }
999   else if (IS_CCR_REGNUM (regnr))
1000     {
1001       /* Does this module have a coprocessor at all?  */
1002       if (! (current_options () & MEP_OPT_COP))
1003         return "";
1004
1005       {
1006         CGEN_KEYWORD *names = current_ccr_names ();
1007
1008         if (! names)
1009           /* This me_module's coprocessor has no control registers.  */
1010           return "";
1011
1012         return register_name_from_keyword (names, regnr-MEP_FIRST_CCR_REGNUM);
1013       }
1014     }
1015
1016   /* It might be nice to give the 'module' register a name, but that
1017      would affect the output of 'info all-registers', which would
1018      disturb the test suites.  So we leave it invisible.  */
1019   else
1020     return NULL;
1021 }
1022
1023
1024 /* Custom register groups for the MeP.  */
1025 static struct reggroup *mep_csr_reggroup; /* control/special */
1026 static struct reggroup *mep_cr_reggroup;  /* coprocessor general-purpose */
1027 static struct reggroup *mep_ccr_reggroup; /* coprocessor control */
1028
1029
1030 static int
1031 mep_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1032                          struct reggroup *group)
1033 {
1034   /* Filter reserved or unused register numbers.  */
1035   {
1036     const char *name = mep_register_name (gdbarch, regnum);
1037
1038     if (! name || name[0] == '\0')
1039       return 0;
1040   }
1041
1042   /* We could separate the GPRs and the CSRs.  Toshiba has approved of
1043      the existing behavior, so we'd want to run that by them.  */
1044   if (group == general_reggroup)
1045     return (IS_GPR_REGNUM (regnum)
1046             || IS_CSR_REGNUM (regnum));
1047
1048   /* Everything is in the 'all' reggroup, except for the raw CSR's.  */
1049   else if (group == all_reggroup)
1050     return (IS_GPR_REGNUM (regnum)
1051             || IS_CSR_REGNUM (regnum)
1052             || IS_CR_REGNUM (regnum)
1053             || IS_CCR_REGNUM (regnum));
1054
1055   /* All registers should be saved and restored, except for the raw
1056      CSR's.
1057
1058      This is probably right if the coprocessor is something like a
1059      floating-point unit, but would be wrong if the coprocessor is
1060      something that does I/O, where register accesses actually cause
1061      externally-visible actions.  But I get the impression that the
1062      coprocessor isn't supposed to do things like that --- you'd use a
1063      hardware engine, perhaps.  */
1064   else if (group == save_reggroup || group == restore_reggroup)
1065     return (IS_GPR_REGNUM (regnum)
1066             || IS_CSR_REGNUM (regnum)
1067             || IS_CR_REGNUM (regnum)
1068             || IS_CCR_REGNUM (regnum));
1069
1070   else if (group == mep_csr_reggroup)
1071     return IS_CSR_REGNUM (regnum);
1072   else if (group == mep_cr_reggroup)
1073     return IS_CR_REGNUM (regnum);
1074   else if (group == mep_ccr_reggroup)
1075     return IS_CCR_REGNUM (regnum);
1076   else
1077     return 0;
1078 }
1079
1080
1081 static struct type *
1082 mep_register_type (struct gdbarch *gdbarch, int reg_nr)
1083 {
1084   /* Coprocessor general-purpose registers may be either 32 or 64 bits
1085      long.  So for them, the raw registers are always 64 bits long (to
1086      keep the 'g' packet format fixed), and the pseudoregisters vary
1087      in length.  */
1088   if (IS_RAW_CR_REGNUM (reg_nr))
1089     return builtin_type (gdbarch)->builtin_uint64;
1090
1091   /* Since GDB doesn't allow registers to change type, we have two
1092      banks of pseudoregisters for the coprocessor general-purpose
1093      registers: one that gives a 32-bit view, and one that gives a
1094      64-bit view.  We hide or show one or the other depending on the
1095      current module.  */
1096   if (IS_CR_REGNUM (reg_nr))
1097     {
1098       int size = mep_pseudo_cr_size (reg_nr);
1099       if (size == 32)
1100         {
1101           if (mep_pseudo_cr_is_float (reg_nr))
1102             return builtin_type (gdbarch)->builtin_float;
1103           else
1104             return builtin_type (gdbarch)->builtin_uint32;
1105         }
1106       else if (size == 64)
1107         {
1108           if (mep_pseudo_cr_is_float (reg_nr))
1109             return builtin_type (gdbarch)->builtin_double;
1110           else
1111             return builtin_type (gdbarch)->builtin_uint64;
1112         }
1113       else
1114         gdb_assert_not_reached ("unexpected cr size");
1115     }
1116
1117   /* All other registers are 32 bits long.  */
1118   else
1119     return builtin_type (gdbarch)->builtin_uint32;
1120 }
1121
1122 static enum register_status
1123 mep_pseudo_cr32_read (struct gdbarch *gdbarch,
1124                       readable_regcache *regcache,
1125                       int cookednum,
1126                       gdb_byte *buf)
1127 {
1128   enum register_status status;
1129   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1130   /* Read the raw register into a 64-bit buffer, and then return the
1131      appropriate end of that buffer.  */
1132   int rawnum = mep_pseudo_to_raw[cookednum];
1133   gdb_byte buf64[8];
1134
1135   gdb_assert (TYPE_LENGTH (register_type (gdbarch, rawnum)) == sizeof (buf64));
1136   gdb_assert (TYPE_LENGTH (register_type (gdbarch, cookednum)) == 4);
1137   status = regcache->raw_read (rawnum, buf64);
1138   if (status == REG_VALID)
1139     {
1140       /* Slow, but legible.  */
1141       store_unsigned_integer (buf, 4, byte_order,
1142                               extract_unsigned_integer (buf64, 8, byte_order));
1143     }
1144   return status;
1145 }
1146
1147
1148 static enum register_status
1149 mep_pseudo_cr64_read (struct gdbarch *gdbarch,
1150                       readable_regcache *regcache,
1151                       int cookednum,
1152                       gdb_byte *buf)
1153 {
1154   return regcache->raw_read (mep_pseudo_to_raw[cookednum], buf);
1155 }
1156
1157
1158 static enum register_status
1159 mep_pseudo_register_read (struct gdbarch *gdbarch,
1160                           readable_regcache *regcache,
1161                           int cookednum,
1162                           gdb_byte *buf)
1163 {
1164   if (IS_CSR_REGNUM (cookednum)
1165       || IS_CCR_REGNUM (cookednum))
1166     return regcache->raw_read (mep_pseudo_to_raw[cookednum], buf);
1167   else if (IS_CR32_REGNUM (cookednum)
1168            || IS_FP_CR32_REGNUM (cookednum))
1169     return mep_pseudo_cr32_read (gdbarch, regcache, cookednum, buf);
1170   else if (IS_CR64_REGNUM (cookednum)
1171            || IS_FP_CR64_REGNUM (cookednum))
1172     return mep_pseudo_cr64_read (gdbarch, regcache, cookednum, buf);
1173   else
1174     gdb_assert_not_reached ("unexpected pseudo register");
1175 }
1176
1177
1178 static void
1179 mep_pseudo_csr_write (struct gdbarch *gdbarch,
1180                       struct regcache *regcache,
1181                       int cookednum,
1182                       const gdb_byte *buf)
1183 {
1184   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1185   int size = register_size (gdbarch, cookednum);
1186   struct mep_csr_register *r
1187     = &mep_csr_registers[cookednum - MEP_FIRST_CSR_REGNUM];
1188
1189   if (r->writeable_bits == 0)
1190     /* A completely read-only register; avoid the read-modify-
1191        write cycle, and juts ignore the entire write.  */
1192     ;
1193   else
1194     {
1195       /* A partially writeable register; do a read-modify-write cycle.  */
1196       ULONGEST old_bits;
1197       ULONGEST new_bits;
1198       ULONGEST mixed_bits;
1199           
1200       regcache_raw_read_unsigned (regcache, r->raw, &old_bits);
1201       new_bits = extract_unsigned_integer (buf, size, byte_order);
1202       mixed_bits = ((r->writeable_bits & new_bits)
1203                     | (~r->writeable_bits & old_bits));
1204       regcache_raw_write_unsigned (regcache, r->raw, mixed_bits);
1205     }
1206 }
1207                       
1208
1209 static void
1210 mep_pseudo_cr32_write (struct gdbarch *gdbarch,
1211                        struct regcache *regcache,
1212                        int cookednum,
1213                        const gdb_byte *buf)
1214 {
1215   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1216   /* Expand the 32-bit value into a 64-bit value, and write that to
1217      the pseudoregister.  */
1218   int rawnum = mep_pseudo_to_raw[cookednum];
1219   gdb_byte buf64[8];
1220   
1221   gdb_assert (TYPE_LENGTH (register_type (gdbarch, rawnum)) == sizeof (buf64));
1222   gdb_assert (TYPE_LENGTH (register_type (gdbarch, cookednum)) == 4);
1223   /* Slow, but legible.  */
1224   store_unsigned_integer (buf64, 8, byte_order,
1225                           extract_unsigned_integer (buf, 4, byte_order));
1226   regcache->raw_write (rawnum, buf64);
1227 }
1228
1229
1230 static void
1231 mep_pseudo_cr64_write (struct gdbarch *gdbarch,
1232                      struct regcache *regcache,
1233                      int cookednum,
1234                      const gdb_byte *buf)
1235 {
1236   regcache->raw_write (mep_pseudo_to_raw[cookednum], buf);
1237 }
1238
1239
1240 static void
1241 mep_pseudo_register_write (struct gdbarch *gdbarch,
1242                            struct regcache *regcache,
1243                            int cookednum,
1244                            const gdb_byte *buf)
1245 {
1246   if (IS_CSR_REGNUM (cookednum))
1247     mep_pseudo_csr_write (gdbarch, regcache, cookednum, buf);
1248   else if (IS_CR32_REGNUM (cookednum)
1249            || IS_FP_CR32_REGNUM (cookednum))
1250     mep_pseudo_cr32_write (gdbarch, regcache, cookednum, buf);
1251   else if (IS_CR64_REGNUM (cookednum)
1252            || IS_FP_CR64_REGNUM (cookednum))
1253     mep_pseudo_cr64_write (gdbarch, regcache, cookednum, buf);
1254   else if (IS_CCR_REGNUM (cookednum))
1255     regcache->raw_write (mep_pseudo_to_raw[cookednum], buf);
1256   else
1257     gdb_assert_not_reached ("unexpected pseudo register");
1258 }
1259
1260
1261 \f
1262 /* Disassembly.  */
1263
1264 static int
1265 mep_gdb_print_insn (bfd_vma pc, disassemble_info * info)
1266 {
1267   struct obj_section * s = find_pc_section (pc);
1268
1269   info->arch = bfd_arch_mep;
1270   if (s)
1271     {
1272       /* The libopcodes disassembly code uses the section to find the
1273          BFD, the BFD to find the ELF header, the ELF header to find
1274          the me_module index, and the me_module index to select the
1275          right instructions to print.  */
1276       info->section = s->the_bfd_section;
1277     }
1278
1279   return print_insn_mep (pc, info);
1280 }
1281
1282 \f
1283 /* Prologue analysis.  */
1284
1285
1286 /* The MeP has two classes of instructions: "core" instructions, which
1287    are pretty normal RISC chip stuff, and "coprocessor" instructions,
1288    which are mostly concerned with moving data in and out of
1289    coprocessor registers, and branching on coprocessor condition
1290    codes.  There's space in the instruction set for custom coprocessor
1291    instructions, too.
1292
1293    Instructions can be 16 or 32 bits long; the top two bits of the
1294    first byte indicate the length.  The coprocessor instructions are
1295    mixed in with the core instructions, and there's no easy way to
1296    distinguish them; you have to completely decode them to tell one
1297    from the other.
1298
1299    The MeP also supports a "VLIW" operation mode, where instructions
1300    always occur in fixed-width bundles.  The bundles are either 32
1301    bits or 64 bits long, depending on a fixed configuration flag.  You
1302    decode the first part of the bundle as normal; if it's a core
1303    instruction, and there's any space left in the bundle, the
1304    remainder of the bundle is a coprocessor instruction, which will
1305    execute in parallel with the core instruction.  If the first part
1306    of the bundle is a coprocessor instruction, it occupies the entire
1307    bundle.
1308
1309    So, here are all the cases:
1310
1311    - 32-bit VLIW mode:
1312      Every bundle is four bytes long, and naturally aligned, and can hold
1313      one or two instructions:
1314      - 16-bit core instruction; 16-bit coprocessor instruction
1315        These execute in parallel.
1316      - 32-bit core instruction
1317      - 32-bit coprocessor instruction
1318
1319    - 64-bit VLIW mode:
1320      Every bundle is eight bytes long, and naturally aligned, and can hold
1321      one or two instructions:
1322      - 16-bit core instruction; 48-bit (!) coprocessor instruction
1323        These execute in parallel.
1324      - 32-bit core instruction; 32-bit coprocessor instruction
1325        These execute in parallel.
1326      - 64-bit coprocessor instruction
1327
1328    Now, the MeP manual doesn't define any 48- or 64-bit coprocessor
1329    instruction, so I don't really know what's up there; perhaps these
1330    are always the user-defined coprocessor instructions.  */
1331
1332
1333 /* Return non-zero if PC is in a VLIW code section, zero
1334    otherwise.  */
1335 static int
1336 mep_pc_in_vliw_section (CORE_ADDR pc)
1337 {
1338   struct obj_section *s = find_pc_section (pc);
1339   if (s)
1340     return (s->the_bfd_section->flags & SEC_MEP_VLIW);
1341   return 0;
1342 }
1343
1344
1345 /* Set *INSN to the next core instruction at PC, and return the
1346    address of the next instruction.
1347
1348    The MeP instruction encoding is endian-dependent.  16- and 32-bit
1349    instructions are encoded as one or two two-byte parts, and each
1350    part is byte-swapped independently.  Thus:
1351
1352       void
1353       foo (void)
1354       {
1355         asm ("movu $1, 0x123456");
1356         asm ("sb $1,0x5678($2)");
1357         asm ("clip $1, 19");
1358       }
1359
1360    compiles to this big-endian code:
1361
1362        0:       d1 56 12 34     movu $1,0x123456
1363        4:       c1 28 56 78     sb $1,22136($2)
1364        8:       f1 01 10 98     clip $1,0x13
1365        c:       70 02           ret
1366
1367    and this little-endian code:
1368
1369        0:       56 d1 34 12     movu $1,0x123456
1370        4:       28 c1 78 56     sb $1,22136($2)
1371        8:       01 f1 98 10     clip $1,0x13
1372        c:       02 70           ret
1373
1374    Instructions are returned in *INSN in an endian-independent form: a
1375    given instruction always appears in *INSN the same way, regardless
1376    of whether the instruction stream is big-endian or little-endian.
1377
1378    *INSN's most significant 16 bits are the first (i.e., at lower
1379    addresses) 16 bit part of the instruction.  Its least significant
1380    16 bits are the second (i.e., higher-addressed) 16 bit part of the
1381    instruction, or zero for a 16-bit instruction.  Both 16-bit parts
1382    are fetched using the current endianness.
1383
1384    So, the *INSN values for the instruction sequence above would be
1385    the following, in either endianness:
1386
1387        0xd1561234       movu $1,0x123456     
1388        0xc1285678       sb $1,22136($2)
1389        0xf1011098       clip $1,0x13
1390        0x70020000       ret
1391
1392    (In a sense, it would be more natural to return 16-bit instructions
1393    in the least significant 16 bits of *INSN, but that would be
1394    ambiguous.  In order to tell whether you're looking at a 16- or a
1395    32-bit instruction, you have to consult the major opcode field ---
1396    the most significant four bits of the instruction's first 16-bit
1397    part.  But if we put 16-bit instructions at the least significant
1398    end of *INSN, then you don't know where to find the major opcode
1399    field until you know if it's a 16- or a 32-bit instruction ---
1400    which is where we started.)
1401
1402    If PC points to a core / coprocessor bundle in a VLIW section, set
1403    *INSN to the core instruction, and return the address of the next
1404    bundle.  This has the effect of skipping the bundled coprocessor
1405    instruction.  That's okay, since coprocessor instructions aren't
1406    significant to prologue analysis --- for the time being,
1407    anyway.  */
1408
1409 static CORE_ADDR 
1410 mep_get_insn (struct gdbarch *gdbarch, CORE_ADDR pc, unsigned long *insn)
1411 {
1412   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1413   int pc_in_vliw_section;
1414   int vliw_mode;
1415   int insn_len;
1416   gdb_byte buf[2];
1417
1418   *insn = 0;
1419
1420   /* Are we in a VLIW section?  */
1421   pc_in_vliw_section = mep_pc_in_vliw_section (pc);
1422   if (pc_in_vliw_section)
1423     {
1424       /* Yes, find out which bundle size.  */
1425       vliw_mode = current_options () & (MEP_OPT_VL32 | MEP_OPT_VL64);
1426
1427       /* If PC is in a VLIW section, but the current core doesn't say
1428          that it supports either VLIW mode, then we don't have enough
1429          information to parse the instruction stream it contains.
1430          Since the "undifferentiated" standard core doesn't have
1431          either VLIW mode bit set, this could happen.
1432
1433          But it shouldn't be an error to (say) set a breakpoint in a
1434          VLIW section, if you know you'll never reach it.  (Perhaps
1435          you have a script that sets a bunch of standard breakpoints.)
1436
1437          So we'll just return zero here, and hope for the best.  */
1438       if (! (vliw_mode & (MEP_OPT_VL32 | MEP_OPT_VL64)))
1439         return 0;
1440
1441       /* If both VL32 and VL64 are set, that's bogus, too.  */
1442       if (vliw_mode == (MEP_OPT_VL32 | MEP_OPT_VL64))
1443         return 0;
1444     }
1445   else
1446     vliw_mode = 0;
1447
1448   read_memory (pc, buf, sizeof (buf));
1449   *insn = extract_unsigned_integer (buf, 2, byte_order) << 16;
1450
1451   /* The major opcode --- the top four bits of the first 16-bit
1452      part --- indicates whether this instruction is 16 or 32 bits
1453      long.  All 32-bit instructions have a major opcode whose top
1454      two bits are 11; all the rest are 16-bit instructions.  */
1455   if ((*insn & 0xc0000000) == 0xc0000000)
1456     {
1457       /* Fetch the second 16-bit part of the instruction.  */
1458       read_memory (pc + 2, buf, sizeof (buf));
1459       *insn = *insn | extract_unsigned_integer (buf, 2, byte_order);
1460     }
1461
1462   /* If we're in VLIW code, then the VLIW width determines the address
1463      of the next instruction.  */
1464   if (vliw_mode)
1465     {
1466       /* In 32-bit VLIW code, all bundles are 32 bits long.  We ignore the
1467          coprocessor half of a core / copro bundle.  */
1468       if (vliw_mode == MEP_OPT_VL32)
1469         insn_len = 4;
1470
1471       /* In 64-bit VLIW code, all bundles are 64 bits long.  We ignore the
1472          coprocessor half of a core / copro bundle.  */
1473       else if (vliw_mode == MEP_OPT_VL64)
1474         insn_len = 8;
1475
1476       /* We'd better be in either core, 32-bit VLIW, or 64-bit VLIW mode.  */
1477       else
1478         gdb_assert_not_reached ("unexpected vliw mode");
1479     }
1480   
1481   /* Otherwise, the top two bits of the major opcode are (again) what
1482      we need to check.  */
1483   else if ((*insn & 0xc0000000) == 0xc0000000)
1484     insn_len = 4;
1485   else
1486     insn_len = 2;
1487
1488   return pc + insn_len;
1489 }
1490
1491
1492 /* Sign-extend the LEN-bit value N.  */
1493 #define SEXT(n, len) ((((int) (n)) ^ (1 << ((len) - 1))) - (1 << ((len) - 1)))
1494
1495 /* Return the LEN-bit field at POS from I.  */
1496 #define FIELD(i, pos, len) (((i) >> (pos)) & ((1 << (len)) - 1))
1497
1498 /* Like FIELD, but sign-extend the field's value.  */
1499 #define SFIELD(i, pos, len) (SEXT (FIELD ((i), (pos), (len)), (len)))
1500
1501
1502 /* Macros for decoding instructions.
1503
1504    Remember that 16-bit instructions are placed in bits 16..31 of i,
1505    not at the least significant end; this means that the major opcode
1506    field is always in the same place, regardless of the width of the
1507    instruction.  As a reminder of this, we show the lower 16 bits of a
1508    16-bit instruction as xxxx_xxxx_xxxx_xxxx.  */
1509
1510 /* SB Rn,(Rm)                 0000_nnnn_mmmm_1000 */
1511 /* SH Rn,(Rm)                 0000_nnnn_mmmm_1001 */
1512 /* SW Rn,(Rm)                 0000_nnnn_mmmm_1010 */
1513
1514 /* SW Rn,disp16(Rm)           1100_nnnn_mmmm_1010 dddd_dddd_dddd_dddd */
1515 #define IS_SW(i)              (((i) & 0xf00f0000) == 0xc00a0000)
1516 /* SB Rn,disp16(Rm)           1100_nnnn_mmmm_1000 dddd_dddd_dddd_dddd */
1517 #define IS_SB(i)              (((i) & 0xf00f0000) == 0xc0080000)
1518 /* SH Rn,disp16(Rm)           1100_nnnn_mmmm_1001 dddd_dddd_dddd_dddd */
1519 #define IS_SH(i)              (((i) & 0xf00f0000) == 0xc0090000)
1520 #define SWBH_32_BASE(i)       (FIELD (i, 20, 4))
1521 #define SWBH_32_SOURCE(i)     (FIELD (i, 24, 4))
1522 #define SWBH_32_OFFSET(i)     (SFIELD (i, 0, 16))
1523
1524 /* SW Rn,disp7.align4(SP)     0100_nnnn_0ddd_dd10 xxxx_xxxx_xxxx_xxxx */
1525 #define IS_SW_IMMD(i)         (((i) & 0xf0830000) == 0x40020000)
1526 #define SW_IMMD_SOURCE(i)     (FIELD (i, 24, 4))
1527 #define SW_IMMD_OFFSET(i)     (FIELD (i, 18, 5) << 2)
1528
1529 /* SW Rn,(Rm)                 0000_nnnn_mmmm_1010 xxxx_xxxx_xxxx_xxxx */
1530 #define IS_SW_REG(i)          (((i) & 0xf00f0000) == 0x000a0000)
1531 #define SW_REG_SOURCE(i)      (FIELD (i, 24, 4))
1532 #define SW_REG_BASE(i)        (FIELD (i, 20, 4))
1533
1534 /* ADD3 Rl,Rn,Rm              1001_nnnn_mmmm_llll xxxx_xxxx_xxxx_xxxx */
1535 #define IS_ADD3_16_REG(i)     (((i) & 0xf0000000) == 0x90000000)
1536 #define ADD3_16_REG_SRC1(i)   (FIELD (i, 20, 4))               /* n */
1537 #define ADD3_16_REG_SRC2(i)   (FIELD (i, 24, 4))               /* m */
1538
1539 /* ADD3 Rn,Rm,imm16           1100_nnnn_mmmm_0000 iiii_iiii_iiii_iiii */
1540 #define IS_ADD3_32(i)         (((i) & 0xf00f0000) == 0xc0000000)
1541 #define ADD3_32_TARGET(i)     (FIELD (i, 24, 4))
1542 #define ADD3_32_SOURCE(i)     (FIELD (i, 20, 4))
1543 #define ADD3_32_OFFSET(i)     (SFIELD (i, 0, 16))
1544
1545 /* ADD3 Rn,SP,imm7.align4     0100_nnnn_0iii_ii00 xxxx_xxxx_xxxx_xxxx */
1546 #define IS_ADD3_16(i)         (((i) & 0xf0830000) == 0x40000000)
1547 #define ADD3_16_TARGET(i)     (FIELD (i, 24, 4))
1548 #define ADD3_16_OFFSET(i)     (FIELD (i, 18, 5) << 2)
1549
1550 /* ADD Rn,imm6                0110_nnnn_iiii_ii00 xxxx_xxxx_xxxx_xxxx */
1551 #define IS_ADD(i)             (((i) & 0xf0030000) == 0x60000000)
1552 #define ADD_TARGET(i)         (FIELD (i, 24, 4))
1553 #define ADD_OFFSET(i)         (SFIELD (i, 18, 6))
1554
1555 /* LDC Rn,imm5                0111_nnnn_iiii_101I xxxx_xxxx_xxxx_xxxx
1556                               imm5 = I||i[7:4] */
1557 #define IS_LDC(i)             (((i) & 0xf00e0000) == 0x700a0000)
1558 #define LDC_IMM(i)            ((FIELD (i, 16, 1) << 4) | FIELD (i, 20, 4))
1559 #define LDC_TARGET(i)         (FIELD (i, 24, 4))
1560
1561 /* LW Rn,disp16(Rm)           1100_nnnn_mmmm_1110 dddd_dddd_dddd_dddd  */
1562 #define IS_LW(i)              (((i) & 0xf00f0000) == 0xc00e0000)
1563 #define LW_TARGET(i)          (FIELD (i, 24, 4))
1564 #define LW_BASE(i)            (FIELD (i, 20, 4))
1565 #define LW_OFFSET(i)          (SFIELD (i, 0, 16))
1566
1567 /* MOV Rn,Rm                  0000_nnnn_mmmm_0000 xxxx_xxxx_xxxx_xxxx */
1568 #define IS_MOV(i)             (((i) & 0xf00f0000) == 0x00000000)
1569 #define MOV_TARGET(i)         (FIELD (i, 24, 4))
1570 #define MOV_SOURCE(i)         (FIELD (i, 20, 4))
1571
1572 /* BRA disp12.align2          1011_dddd_dddd_ddd0 xxxx_xxxx_xxxx_xxxx */
1573 #define IS_BRA(i)             (((i) & 0xf0010000) == 0xb0000000)
1574 #define BRA_DISP(i)           (SFIELD (i, 17, 11) << 1)
1575
1576
1577 /* This structure holds the results of a prologue analysis.  */
1578 struct mep_prologue
1579 {
1580   /* The architecture for which we generated this prologue info.  */
1581   struct gdbarch *gdbarch;
1582
1583   /* The offset from the frame base to the stack pointer --- always
1584      zero or negative.
1585
1586      Calling this a "size" is a bit misleading, but given that the
1587      stack grows downwards, using offsets for everything keeps one
1588      from going completely sign-crazy: you never change anything's
1589      sign for an ADD instruction; always change the second operand's
1590      sign for a SUB instruction; and everything takes care of
1591      itself.  */
1592   int frame_size;
1593
1594   /* Non-zero if this function has initialized the frame pointer from
1595      the stack pointer, zero otherwise.  */
1596   int has_frame_ptr;
1597
1598   /* If has_frame_ptr is non-zero, this is the offset from the frame
1599      base to where the frame pointer points.  This is always zero or
1600      negative.  */
1601   int frame_ptr_offset;
1602
1603   /* The address of the first instruction at which the frame has been
1604      set up and the arguments are where the debug info says they are
1605      --- as best as we can tell.  */
1606   CORE_ADDR prologue_end;
1607
1608   /* reg_offset[R] is the offset from the CFA at which register R is
1609      saved, or 1 if register R has not been saved.  (Real values are
1610      always zero or negative.)  */
1611   int reg_offset[MEP_NUM_REGS];
1612 };
1613
1614 /* Return non-zero if VALUE is an incoming argument register.  */
1615
1616 static int
1617 is_arg_reg (pv_t value)
1618 {
1619   return (value.kind == pvk_register
1620           && MEP_R1_REGNUM <= value.reg && value.reg <= MEP_R4_REGNUM
1621           && value.k == 0);
1622 }
1623
1624 /* Return non-zero if a store of REG's current value VALUE to ADDR is
1625    probably spilling an argument register to its stack slot in STACK.
1626    Such instructions should be included in the prologue, if possible.
1627
1628    The store is a spill if:
1629    - the value being stored is REG's original value;
1630    - the value has not already been stored somewhere in STACK; and
1631    - ADDR is a stack slot's address (e.g., relative to the original
1632      value of the SP).  */
1633 static int
1634 is_arg_spill (struct gdbarch *gdbarch, pv_t value, pv_t addr,
1635               struct pv_area *stack)
1636 {
1637   return (is_arg_reg (value)
1638           && pv_is_register (addr, MEP_SP_REGNUM)
1639           && ! stack->find_reg (gdbarch, value.reg, 0));
1640 }
1641
1642
1643 /* Function for finding saved registers in a 'struct pv_area'; we pass
1644    this to pv_area::scan.
1645
1646    If VALUE is a saved register, ADDR says it was saved at a constant
1647    offset from the frame base, and SIZE indicates that the whole
1648    register was saved, record its offset in RESULT_UNTYPED.  */
1649 static void
1650 check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
1651 {
1652   struct mep_prologue *result = (struct mep_prologue *) result_untyped;
1653
1654   if (value.kind == pvk_register
1655       && value.k == 0
1656       && pv_is_register (addr, MEP_SP_REGNUM)
1657       && size == register_size (result->gdbarch, value.reg))
1658     result->reg_offset[value.reg] = addr.k;
1659 }
1660
1661
1662 /* Analyze a prologue starting at START_PC, going no further than
1663    LIMIT_PC.  Fill in RESULT as appropriate.  */
1664 static void
1665 mep_analyze_prologue (struct gdbarch *gdbarch,
1666                       CORE_ADDR start_pc, CORE_ADDR limit_pc,
1667                       struct mep_prologue *result)
1668 {
1669   CORE_ADDR pc;
1670   unsigned long insn;
1671   pv_t reg[MEP_NUM_REGS];
1672   CORE_ADDR after_last_frame_setup_insn = start_pc;
1673
1674   memset (result, 0, sizeof (*result));
1675   result->gdbarch = gdbarch;
1676
1677   for (int rn = 0; rn < MEP_NUM_REGS; rn++)
1678     {
1679       reg[rn] = pv_register (rn, 0);
1680       result->reg_offset[rn] = 1;
1681     }
1682
1683   pv_area stack (MEP_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1684
1685   pc = start_pc;
1686   while (pc < limit_pc)
1687     {
1688       CORE_ADDR next_pc;
1689       pv_t pre_insn_fp, pre_insn_sp;
1690
1691       next_pc = mep_get_insn (gdbarch, pc, &insn);
1692
1693       /* A zero return from mep_get_insn means that either we weren't
1694          able to read the instruction from memory, or that we don't
1695          have enough information to be able to reliably decode it.  So
1696          we'll store here and hope for the best.  */
1697       if (! next_pc)
1698         break;
1699
1700       /* Note the current values of the SP and FP, so we can tell if
1701          this instruction changed them, below.  */
1702       pre_insn_fp = reg[MEP_FP_REGNUM];
1703       pre_insn_sp = reg[MEP_SP_REGNUM];
1704
1705       if (IS_ADD (insn))
1706         {
1707           int rn = ADD_TARGET (insn);
1708           CORE_ADDR imm6 = ADD_OFFSET (insn);
1709
1710           reg[rn] = pv_add_constant (reg[rn], imm6);
1711         }
1712       else if (IS_ADD3_16 (insn))
1713         {
1714           int rn = ADD3_16_TARGET (insn);
1715           int imm7 = ADD3_16_OFFSET (insn);
1716
1717           reg[rn] = pv_add_constant (reg[MEP_SP_REGNUM], imm7);
1718         }
1719       else if (IS_ADD3_32 (insn))
1720         {
1721           int rn = ADD3_32_TARGET (insn);
1722           int rm = ADD3_32_SOURCE (insn);
1723           int imm16 = ADD3_32_OFFSET (insn);
1724
1725           reg[rn] = pv_add_constant (reg[rm], imm16);
1726         }
1727       else if (IS_SW_REG (insn))
1728         {
1729           int rn = SW_REG_SOURCE (insn);
1730           int rm = SW_REG_BASE (insn);
1731
1732           /* If simulating this store would require us to forget
1733              everything we know about the stack frame in the name of
1734              accuracy, it would be better to just quit now.  */
1735           if (stack.store_would_trash (reg[rm]))
1736             break;
1737           
1738           if (is_arg_spill (gdbarch, reg[rn], reg[rm], &stack))
1739             after_last_frame_setup_insn = next_pc;
1740
1741           stack.store (reg[rm], 4, reg[rn]);
1742         }
1743       else if (IS_SW_IMMD (insn))
1744         {
1745           int rn = SW_IMMD_SOURCE (insn);
1746           int offset = SW_IMMD_OFFSET (insn);
1747           pv_t addr = pv_add_constant (reg[MEP_SP_REGNUM], offset);
1748
1749           /* If simulating this store would require us to forget
1750              everything we know about the stack frame in the name of
1751              accuracy, it would be better to just quit now.  */
1752           if (stack.store_would_trash (addr))
1753             break;
1754
1755           if (is_arg_spill (gdbarch, reg[rn], addr, &stack))
1756             after_last_frame_setup_insn = next_pc;
1757
1758           stack.store (addr, 4, reg[rn]);
1759         }
1760       else if (IS_MOV (insn))
1761         {
1762           int rn = MOV_TARGET (insn);
1763           int rm = MOV_SOURCE (insn);
1764
1765           reg[rn] = reg[rm];
1766
1767           if (pv_is_register (reg[rm], rm) && is_arg_reg (reg[rm]))
1768             after_last_frame_setup_insn = next_pc;
1769         }
1770       else if (IS_SB (insn) || IS_SH (insn) || IS_SW (insn))
1771         {
1772           int rn = SWBH_32_SOURCE (insn);
1773           int rm = SWBH_32_BASE (insn);
1774           int disp = SWBH_32_OFFSET (insn);
1775           int size = (IS_SB (insn) ? 1
1776                       : IS_SH (insn) ? 2
1777                       : (gdb_assert (IS_SW (insn)), 4));
1778           pv_t addr = pv_add_constant (reg[rm], disp);
1779
1780           if (stack.store_would_trash (addr))
1781             break;
1782
1783           if (is_arg_spill (gdbarch, reg[rn], addr, &stack))
1784             after_last_frame_setup_insn = next_pc;
1785
1786           stack.store (addr, size, reg[rn]);
1787         }
1788       else if (IS_LDC (insn))
1789         {
1790           int rn = LDC_TARGET (insn);
1791           int cr = LDC_IMM (insn) + MEP_FIRST_CSR_REGNUM;
1792
1793           reg[rn] = reg[cr];
1794         }
1795       else if (IS_LW (insn))
1796         {
1797           int rn = LW_TARGET (insn);
1798           int rm = LW_BASE (insn);
1799           int offset = LW_OFFSET (insn);
1800           pv_t addr = pv_add_constant (reg[rm], offset);
1801
1802           reg[rn] = stack.fetch (addr, 4);
1803         }
1804       else if (IS_BRA (insn) && BRA_DISP (insn) > 0)
1805         {
1806           /* When a loop appears as the first statement of a function
1807              body, gcc 4.x will use a BRA instruction to branch to the
1808              loop condition checking code.  This BRA instruction is
1809              marked as part of the prologue.  We therefore set next_pc
1810              to this branch target and also stop the prologue scan.
1811              The instructions at and beyond the branch target should
1812              no longer be associated with the prologue.
1813              
1814              Note that we only consider forward branches here.  We
1815              presume that a forward branch is being used to skip over
1816              a loop body.
1817              
1818              A backwards branch is covered by the default case below.
1819              If we were to encounter a backwards branch, that would
1820              most likely mean that we've scanned through a loop body.
1821              We definitely want to stop the prologue scan when this
1822              happens and that is precisely what is done by the default
1823              case below.  */
1824           next_pc = pc + BRA_DISP (insn);
1825           after_last_frame_setup_insn = next_pc;
1826           break;
1827         }
1828       else
1829         /* We've hit some instruction we don't know how to simulate.
1830            Strictly speaking, we should set every value we're
1831            tracking to "unknown".  But we'll be optimistic, assume
1832            that we have enough information already, and stop
1833            analysis here.  */
1834         break;
1835
1836       /* If this instruction changed the FP or decreased the SP (i.e.,
1837          allocated more stack space), then this may be a good place to
1838          declare the prologue finished.  However, there are some
1839          exceptions:
1840
1841          - If the instruction just changed the FP back to its original
1842            value, then that's probably a restore instruction.  The
1843            prologue should definitely end before that.  
1844
1845          - If the instruction increased the value of the SP (that is,
1846            shrunk the frame), then it's probably part of a frame
1847            teardown sequence, and the prologue should end before that.  */
1848
1849       if (! pv_is_identical (reg[MEP_FP_REGNUM], pre_insn_fp))
1850         {
1851           if (! pv_is_register_k (reg[MEP_FP_REGNUM], MEP_FP_REGNUM, 0))
1852             after_last_frame_setup_insn = next_pc;
1853         }
1854       else if (! pv_is_identical (reg[MEP_SP_REGNUM], pre_insn_sp))
1855         {
1856           /* The comparison of constants looks odd, there, because .k
1857              is unsigned.  All it really means is that the new value
1858              is lower than it was before the instruction.  */
1859           if (pv_is_register (pre_insn_sp, MEP_SP_REGNUM)
1860               && pv_is_register (reg[MEP_SP_REGNUM], MEP_SP_REGNUM)
1861               && ((pre_insn_sp.k - reg[MEP_SP_REGNUM].k)
1862                   < (reg[MEP_SP_REGNUM].k - pre_insn_sp.k)))
1863             after_last_frame_setup_insn = next_pc;
1864         }
1865
1866       pc = next_pc;
1867     }
1868
1869   /* Is the frame size (offset, really) a known constant?  */
1870   if (pv_is_register (reg[MEP_SP_REGNUM], MEP_SP_REGNUM))
1871     result->frame_size = reg[MEP_SP_REGNUM].k;
1872
1873   /* Was the frame pointer initialized?  */
1874   if (pv_is_register (reg[MEP_FP_REGNUM], MEP_SP_REGNUM))
1875     {
1876       result->has_frame_ptr = 1;
1877       result->frame_ptr_offset = reg[MEP_FP_REGNUM].k;
1878     }
1879
1880   /* Record where all the registers were saved.  */
1881   stack.scan (check_for_saved, (void *) result);
1882
1883   result->prologue_end = after_last_frame_setup_insn;
1884 }
1885
1886
1887 static CORE_ADDR
1888 mep_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1889 {
1890   const char *name;
1891   CORE_ADDR func_addr, func_end;
1892   struct mep_prologue p;
1893
1894   /* Try to find the extent of the function that contains PC.  */
1895   if (! find_pc_partial_function (pc, &name, &func_addr, &func_end))
1896     return pc;
1897
1898   mep_analyze_prologue (gdbarch, pc, func_end, &p);
1899   return p.prologue_end;
1900 }
1901
1902
1903 \f
1904 /* Breakpoints.  */
1905 constexpr gdb_byte mep_break_insn[] = { 0x70, 0x32 };
1906
1907 typedef BP_MANIPULATION (mep_break_insn) mep_breakpoint;
1908
1909 \f
1910 /* Frames and frame unwinding.  */
1911
1912
1913 static struct mep_prologue *
1914 mep_analyze_frame_prologue (struct frame_info *this_frame,
1915                             void **this_prologue_cache)
1916 {
1917   if (! *this_prologue_cache)
1918     {
1919       CORE_ADDR func_start, stop_addr;
1920
1921       *this_prologue_cache 
1922         = FRAME_OBSTACK_ZALLOC (struct mep_prologue);
1923
1924       func_start = get_frame_func (this_frame);
1925       stop_addr = get_frame_pc (this_frame);
1926
1927       /* If we couldn't find any function containing the PC, then
1928          just initialize the prologue cache, but don't do anything.  */
1929       if (! func_start)
1930         stop_addr = func_start;
1931
1932       mep_analyze_prologue (get_frame_arch (this_frame),
1933                             func_start, stop_addr,
1934                             (struct mep_prologue *) *this_prologue_cache);
1935     }
1936
1937   return (struct mep_prologue *) *this_prologue_cache;
1938 }
1939
1940
1941 /* Given the next frame and a prologue cache, return this frame's
1942    base.  */
1943 static CORE_ADDR
1944 mep_frame_base (struct frame_info *this_frame,
1945                 void **this_prologue_cache)
1946 {
1947   struct mep_prologue *p
1948     = mep_analyze_frame_prologue (this_frame, this_prologue_cache);
1949
1950   /* In functions that use alloca, the distance between the stack
1951      pointer and the frame base varies dynamically, so we can't use
1952      the SP plus static information like prologue analysis to find the
1953      frame base.  However, such functions must have a frame pointer,
1954      to be able to restore the SP on exit.  So whenever we do have a
1955      frame pointer, use that to find the base.  */
1956   if (p->has_frame_ptr)
1957     {
1958       CORE_ADDR fp
1959         = get_frame_register_unsigned (this_frame, MEP_FP_REGNUM);
1960       return fp - p->frame_ptr_offset;
1961     }
1962   else
1963     {
1964       CORE_ADDR sp
1965         = get_frame_register_unsigned (this_frame, MEP_SP_REGNUM);
1966       return sp - p->frame_size;
1967     }
1968 }
1969
1970
1971 static void
1972 mep_frame_this_id (struct frame_info *this_frame,
1973                    void **this_prologue_cache,
1974                    struct frame_id *this_id)
1975 {
1976   *this_id = frame_id_build (mep_frame_base (this_frame, this_prologue_cache),
1977                              get_frame_func (this_frame));
1978 }
1979
1980
1981 static struct value *
1982 mep_frame_prev_register (struct frame_info *this_frame,
1983                          void **this_prologue_cache, int regnum)
1984 {
1985   struct mep_prologue *p
1986     = mep_analyze_frame_prologue (this_frame, this_prologue_cache);
1987
1988   /* There are a number of complications in unwinding registers on the
1989      MeP, having to do with core functions calling VLIW functions and
1990      vice versa.
1991
1992      The least significant bit of the link register, LP.LTOM, is the
1993      VLIW mode toggle bit: it's set if a core function called a VLIW
1994      function, or vice versa, and clear when the caller and callee
1995      were both in the same mode.
1996
1997      So, if we're asked to unwind the PC, then we really want to
1998      unwind the LP and clear the least significant bit.  (Real return
1999      addresses are always even.)  And if we want to unwind the program
2000      status word (PSW), we need to toggle PSW.OM if LP.LTOM is set.
2001
2002      Tweaking the register values we return in this way means that the
2003      bits in BUFFERP[] are not the same as the bits you'd find at
2004      ADDRP in the inferior, so we make sure lvalp is not_lval when we
2005      do this.  */
2006   if (regnum == MEP_PC_REGNUM)
2007     {
2008       struct value *value;
2009       CORE_ADDR lp;
2010       value = mep_frame_prev_register (this_frame, this_prologue_cache,
2011                                        MEP_LP_REGNUM);
2012       lp = value_as_long (value);
2013       release_value (value);
2014
2015       return frame_unwind_got_constant (this_frame, regnum, lp & ~1);
2016     }
2017   else
2018     {
2019       CORE_ADDR frame_base = mep_frame_base (this_frame, this_prologue_cache);
2020       struct value *value;
2021
2022       /* Our caller's SP is our frame base.  */
2023       if (regnum == MEP_SP_REGNUM)
2024         return frame_unwind_got_constant (this_frame, regnum, frame_base);
2025
2026       /* If prologue analysis says we saved this register somewhere,
2027          return a description of the stack slot holding it.  */
2028       if (p->reg_offset[regnum] != 1)
2029         value = frame_unwind_got_memory (this_frame, regnum,
2030                                          frame_base + p->reg_offset[regnum]);
2031
2032       /* Otherwise, presume we haven't changed the value of this
2033          register, and get it from the next frame.  */
2034       else
2035         value = frame_unwind_got_register (this_frame, regnum, regnum);
2036
2037       /* If we need to toggle the operating mode, do so.  */
2038       if (regnum == MEP_PSW_REGNUM)
2039         {
2040           CORE_ADDR psw, lp;
2041
2042           psw = value_as_long (value);
2043           release_value (value);
2044
2045           /* Get the LP's value, too.  */
2046           value = get_frame_register_value (this_frame, MEP_LP_REGNUM);
2047           lp = value_as_long (value);
2048           release_value (value);
2049
2050           /* If LP.LTOM is set, then toggle PSW.OM.  */
2051           if (lp & 0x1)
2052             psw ^= 0x1000;
2053
2054           return frame_unwind_got_constant (this_frame, regnum, psw);
2055         }
2056
2057       return value;
2058     }
2059 }
2060
2061
2062 static const struct frame_unwind mep_frame_unwind = {
2063   "mep prologue",
2064   NORMAL_FRAME,
2065   default_frame_unwind_stop_reason,
2066   mep_frame_this_id,
2067   mep_frame_prev_register,
2068   NULL,
2069   default_frame_sniffer
2070 };
2071
2072 \f
2073 /* Return values.  */
2074
2075
2076 static int
2077 mep_use_struct_convention (struct type *type)
2078 {
2079   return (TYPE_LENGTH (type) > MEP_GPR_SIZE);
2080 }
2081
2082
2083 static void
2084 mep_extract_return_value (struct gdbarch *arch,
2085                           struct type *type,
2086                           struct regcache *regcache,
2087                           gdb_byte *valbuf)
2088 {
2089   int byte_order = gdbarch_byte_order (arch);
2090
2091   /* Values that don't occupy a full register appear at the less
2092      significant end of the value.  This is the offset to where the
2093      value starts.  */
2094   int offset;
2095
2096   /* Return values > MEP_GPR_SIZE bytes are returned in memory,
2097      pointed to by R0.  */
2098   gdb_assert (TYPE_LENGTH (type) <= MEP_GPR_SIZE);
2099
2100   if (byte_order == BFD_ENDIAN_BIG)
2101     offset = MEP_GPR_SIZE - TYPE_LENGTH (type);
2102   else
2103     offset = 0;
2104
2105   /* Return values that do fit in a single register are returned in R0.  */
2106   regcache->cooked_read_part (MEP_R0_REGNUM, offset, TYPE_LENGTH (type),
2107                               valbuf);
2108 }
2109
2110
2111 static void
2112 mep_store_return_value (struct gdbarch *arch,
2113                         struct type *type,
2114                         struct regcache *regcache,
2115                         const gdb_byte *valbuf)
2116 {
2117   int byte_order = gdbarch_byte_order (arch);
2118
2119   /* Values that fit in a single register go in R0.  */
2120   if (TYPE_LENGTH (type) <= MEP_GPR_SIZE)
2121     {
2122       /* Values that don't occupy a full register appear at the least
2123          significant end of the value.  This is the offset to where the
2124          value starts.  */
2125       int offset;
2126
2127       if (byte_order == BFD_ENDIAN_BIG)
2128         offset = MEP_GPR_SIZE - TYPE_LENGTH (type);
2129       else
2130         offset = 0;
2131
2132       regcache->cooked_write_part (MEP_R0_REGNUM, offset, TYPE_LENGTH (type),
2133                                    valbuf);
2134     }
2135
2136   /* Return values larger than a single register are returned in
2137      memory, pointed to by R0.  Unfortunately, we can't count on R0
2138      pointing to the return buffer, so we raise an error here.  */
2139   else
2140     error (_("\
2141 GDB cannot set return values larger than four bytes; the Media Processor's\n\
2142 calling conventions do not provide enough information to do this.\n\
2143 Try using the 'return' command with no argument."));
2144 }
2145
2146 static enum return_value_convention
2147 mep_return_value (struct gdbarch *gdbarch, struct value *function,
2148                   struct type *type, struct regcache *regcache,
2149                   gdb_byte *readbuf, const gdb_byte *writebuf)
2150 {
2151   if (mep_use_struct_convention (type))
2152     {
2153       if (readbuf)
2154         {
2155           ULONGEST addr;
2156           /* Although the address of the struct buffer gets passed in R1, it's
2157              returned in R0.  Fetch R0's value and then read the memory
2158              at that address.  */
2159           regcache_raw_read_unsigned (regcache, MEP_R0_REGNUM, &addr);
2160           read_memory (addr, readbuf, TYPE_LENGTH (type));
2161         }
2162       if (writebuf)
2163         {
2164           /* Return values larger than a single register are returned in
2165              memory, pointed to by R0.  Unfortunately, we can't count on R0
2166              pointing to the return buffer, so we raise an error here.  */
2167           error (_("\
2168 GDB cannot set return values larger than four bytes; the Media Processor's\n\
2169 calling conventions do not provide enough information to do this.\n\
2170 Try using the 'return' command with no argument."));
2171         }
2172       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
2173     }
2174
2175   if (readbuf)
2176     mep_extract_return_value (gdbarch, type, regcache, readbuf);
2177   if (writebuf)
2178     mep_store_return_value (gdbarch, type, regcache, writebuf);
2179
2180   return RETURN_VALUE_REGISTER_CONVENTION;
2181 }
2182
2183 \f
2184 /* Inferior calls.  */
2185
2186
2187 static CORE_ADDR
2188 mep_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2189 {
2190   /* Require word alignment.  */
2191   return sp & -4;
2192 }
2193
2194
2195 /* From "lang_spec2.txt":
2196
2197    4.2 Calling conventions
2198
2199    4.2.1 Core register conventions
2200
2201    - Parameters should be evaluated from left to right, and they
2202      should be held in $1,$2,$3,$4 in order.  The fifth parameter or
2203      after should be held in the stack.  If the size is larger than 4
2204      bytes in the first four parameters, the pointer should be held in
2205      the registers instead.  If the size is larger than 4 bytes in the
2206      fifth parameter or after, the pointer should be held in the stack.
2207
2208    - Return value of a function should be held in register $0.  If the
2209      size of return value is larger than 4 bytes, $1 should hold the
2210      pointer pointing memory that would hold the return value.  In this
2211      case, the first parameter should be held in $2, the second one in
2212      $3, and the third one in $4, and the forth parameter or after
2213      should be held in the stack.
2214
2215    [This doesn't say so, but arguments shorter than four bytes are
2216    passed in the least significant end of a four-byte word when
2217    they're passed on the stack.]  */
2218
2219
2220 /* Traverse the list of ARGC arguments ARGV; for every ARGV[i] too
2221    large to fit in a register, save it on the stack, and place its
2222    address in COPY[i].  SP is the initial stack pointer; return the
2223    new stack pointer.  */
2224 static CORE_ADDR
2225 push_large_arguments (CORE_ADDR sp, int argc, struct value **argv,
2226                       CORE_ADDR copy[])
2227 {
2228   int i;
2229
2230   for (i = 0; i < argc; i++)
2231     {
2232       unsigned arg_len = TYPE_LENGTH (value_type (argv[i]));
2233
2234       if (arg_len > MEP_GPR_SIZE)
2235         {
2236           /* Reserve space for the copy, and then round the SP down, to
2237              make sure it's all aligned properly.  */
2238           sp = (sp - arg_len) & -4;
2239           write_memory (sp, value_contents (argv[i]).data (), arg_len);
2240           copy[i] = sp;
2241         }
2242     }
2243
2244   return sp;
2245 }
2246
2247
2248 static CORE_ADDR
2249 mep_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
2250                      struct regcache *regcache, CORE_ADDR bp_addr,
2251                      int argc, struct value **argv, CORE_ADDR sp,
2252                      function_call_return_method return_method,
2253                      CORE_ADDR struct_addr)
2254 {
2255   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2256   CORE_ADDR *copy = (CORE_ADDR *) alloca (argc * sizeof (copy[0]));
2257   int i;
2258
2259   /* The number of the next register available to hold an argument.  */
2260   int arg_reg;
2261
2262   /* The address of the next stack slot available to hold an argument.  */
2263   CORE_ADDR arg_stack;
2264
2265   /* The address of the end of the stack area for arguments.  This is
2266      just for error checking.  */
2267   CORE_ADDR arg_stack_end;
2268   
2269   sp = push_large_arguments (sp, argc, argv, copy);
2270
2271   /* Reserve space for the stack arguments, if any.  */
2272   arg_stack_end = sp;
2273   if (argc + (struct_addr ? 1 : 0) > 4)
2274     sp -= ((argc + (struct_addr ? 1 : 0)) - 4) * MEP_GPR_SIZE;
2275
2276   arg_reg = MEP_R1_REGNUM;
2277   arg_stack = sp;
2278
2279   /* If we're returning a structure by value, push the pointer to the
2280      buffer as the first argument.  */
2281   if (return_method == return_method_struct)
2282     {
2283       regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
2284       arg_reg++;
2285     }
2286
2287   for (i = 0; i < argc; i++)
2288     {
2289       ULONGEST value;
2290
2291       /* Arguments that fit in a GPR get expanded to fill the GPR.  */
2292       if (TYPE_LENGTH (value_type (argv[i])) <= MEP_GPR_SIZE)
2293         value = extract_unsigned_integer (value_contents (argv[i]).data (),
2294                                           TYPE_LENGTH (value_type (argv[i])),
2295                                           byte_order);
2296
2297       /* Arguments too large to fit in a GPR get copied to the stack,
2298          and we pass a pointer to the copy.  */
2299       else
2300         value = copy[i];
2301
2302       /* We use $1 -- $4 for passing arguments, then use the stack.  */
2303       if (arg_reg <= MEP_R4_REGNUM)
2304         {
2305           regcache_cooked_write_unsigned (regcache, arg_reg, value);
2306           arg_reg++;
2307         }
2308       else
2309         {
2310           gdb_byte buf[MEP_GPR_SIZE];
2311           store_unsigned_integer (buf, MEP_GPR_SIZE, byte_order, value);
2312           write_memory (arg_stack, buf, MEP_GPR_SIZE);
2313           arg_stack += MEP_GPR_SIZE;
2314         }
2315     }
2316
2317   gdb_assert (arg_stack <= arg_stack_end);
2318
2319   /* Set the return address.  */
2320   regcache_cooked_write_unsigned (regcache, MEP_LP_REGNUM, bp_addr);
2321
2322   /* Update the stack pointer.  */
2323   regcache_cooked_write_unsigned (regcache, MEP_SP_REGNUM, sp);
2324   
2325   return sp;
2326 }
2327
2328 \f
2329 /* Initialization.  */
2330
2331
2332 static struct gdbarch *
2333 mep_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2334 {
2335   struct gdbarch *gdbarch;
2336
2337   /* Which me_module are we building a gdbarch object for?  */
2338   CONFIG_ATTR me_module;
2339
2340   /* If we have a BFD in hand, figure out which me_module it was built
2341      for.  Otherwise, use the no-particular-me_module code.  */
2342   if (info.abfd)
2343     {
2344       /* The way to get the me_module code depends on the object file
2345          format.  At the moment, we only know how to handle ELF.  */
2346       if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
2347         {
2348           int flag = elf_elfheader (info.abfd)->e_flags & EF_MEP_INDEX_MASK;
2349           me_module = (CONFIG_ATTR) flag;
2350         }
2351       else
2352         me_module = CONFIG_NONE;
2353     }
2354   else
2355     me_module = CONFIG_NONE;
2356
2357   /* If we're setting the architecture from a file, check the
2358      endianness of the file against that of the me_module.  */
2359   if (info.abfd)
2360     {
2361       /* The negations on either side make the comparison treat all
2362          non-zero (true) values as equal.  */
2363       if (! bfd_big_endian (info.abfd) != ! me_module_big_endian (me_module))
2364         {
2365           const char *module_name = me_module_name (me_module);
2366           const char *module_endianness
2367             = me_module_big_endian (me_module) ? "big" : "little";
2368           const char *file_name = bfd_get_filename (info.abfd);
2369           const char *file_endianness
2370             = bfd_big_endian (info.abfd) ? "big" : "little";
2371           
2372           fputc_unfiltered ('\n', gdb_stderr);
2373           if (module_name)
2374             warning (_("the MeP module '%s' is %s-endian, but the executable\n"
2375                        "%s is %s-endian."),
2376                      module_name, module_endianness,
2377                      file_name, file_endianness);
2378           else
2379             warning (_("the selected MeP module is %s-endian, but the "
2380                        "executable\n"
2381                        "%s is %s-endian."),
2382                      module_endianness, file_name, file_endianness);
2383         }
2384     }
2385
2386   /* Find a candidate among the list of architectures we've created
2387      already.  info->bfd_arch_info needs to match, but we also want
2388      the right me_module: the ELF header's e_flags field needs to
2389      match as well.  */
2390   for (arches = gdbarch_list_lookup_by_info (arches, &info); 
2391        arches != NULL;
2392        arches = gdbarch_list_lookup_by_info (arches->next, &info))
2393     {
2394       mep_gdbarch_tdep *tdep
2395         = (mep_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
2396
2397       if (tdep->me_module == me_module)
2398         return arches->gdbarch;
2399     }
2400
2401   mep_gdbarch_tdep *tdep = new mep_gdbarch_tdep;
2402   gdbarch = gdbarch_alloc (&info, tdep);
2403
2404   /* Get a CGEN CPU descriptor for this architecture.  */
2405   {
2406     const char *mach_name = info.bfd_arch_info->printable_name;
2407     enum cgen_endian endian = (info.byte_order == BFD_ENDIAN_BIG
2408                                ? CGEN_ENDIAN_BIG
2409                                : CGEN_ENDIAN_LITTLE);
2410
2411     tdep->cpu_desc = mep_cgen_cpu_open (CGEN_CPU_OPEN_BFDMACH, mach_name,
2412                                         CGEN_CPU_OPEN_ENDIAN, endian,
2413                                         CGEN_CPU_OPEN_END);
2414   }
2415
2416   tdep->me_module = me_module;
2417
2418   /* Register set.  */
2419   set_gdbarch_num_regs (gdbarch, MEP_NUM_RAW_REGS);
2420   set_gdbarch_pc_regnum (gdbarch, MEP_PC_REGNUM);
2421   set_gdbarch_sp_regnum (gdbarch, MEP_SP_REGNUM);
2422   set_gdbarch_register_name (gdbarch, mep_register_name);
2423   set_gdbarch_register_type (gdbarch, mep_register_type);
2424   set_gdbarch_num_pseudo_regs (gdbarch, MEP_NUM_PSEUDO_REGS);
2425   set_gdbarch_pseudo_register_read (gdbarch, mep_pseudo_register_read);
2426   set_gdbarch_pseudo_register_write (gdbarch, mep_pseudo_register_write);
2427   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, mep_debug_reg_to_regnum);
2428   set_gdbarch_stab_reg_to_regnum (gdbarch, mep_debug_reg_to_regnum);
2429
2430   set_gdbarch_register_reggroup_p (gdbarch, mep_register_reggroup_p);
2431   reggroup_add (gdbarch, all_reggroup);
2432   reggroup_add (gdbarch, general_reggroup);
2433   reggroup_add (gdbarch, save_reggroup);
2434   reggroup_add (gdbarch, restore_reggroup);
2435   reggroup_add (gdbarch, mep_csr_reggroup);
2436   reggroup_add (gdbarch, mep_cr_reggroup);
2437   reggroup_add (gdbarch, mep_ccr_reggroup);
2438
2439   /* Disassembly.  */
2440   set_gdbarch_print_insn (gdbarch, mep_gdb_print_insn); 
2441
2442   /* Breakpoints.  */
2443   set_gdbarch_breakpoint_kind_from_pc (gdbarch, mep_breakpoint::kind_from_pc);
2444   set_gdbarch_sw_breakpoint_from_kind (gdbarch, mep_breakpoint::bp_from_kind);
2445   set_gdbarch_decr_pc_after_break (gdbarch, 0);
2446   set_gdbarch_skip_prologue (gdbarch, mep_skip_prologue);
2447
2448   /* Frames and frame unwinding.  */
2449   frame_unwind_append_unwinder (gdbarch, &mep_frame_unwind);
2450   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2451   set_gdbarch_frame_args_skip (gdbarch, 0);
2452
2453   /* Return values.  */
2454   set_gdbarch_return_value (gdbarch, mep_return_value);
2455   
2456   /* Inferior function calls.  */
2457   set_gdbarch_frame_align (gdbarch, mep_frame_align);
2458   set_gdbarch_push_dummy_call (gdbarch, mep_push_dummy_call);
2459
2460   return gdbarch;
2461 }
2462
2463 void _initialize_mep_tdep ();
2464 void
2465 _initialize_mep_tdep ()
2466 {
2467   mep_csr_reggroup = reggroup_new ("csr", USER_REGGROUP);
2468   mep_cr_reggroup  = reggroup_new ("cr", USER_REGGROUP); 
2469   mep_ccr_reggroup = reggroup_new ("ccr", USER_REGGROUP);
2470
2471   register_gdbarch_init (bfd_arch_mep, mep_gdbarch_init);
2472
2473   mep_init_pseudoregister_maps ();
2474 }
This page took 0.163595 seconds and 4 git commands to generate.