OSDN Git Service

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