OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gdb-7.2/] [gdb/] [or32-tdep.c] - Blame information for rev 835

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* Target-dependent code for the 32-bit OpenRISC 1000, for the GNU Debugger.
2
 
3
   Copyright 1988-2008, Free Software Foundation, Inc.
4
   Copyright (C) 2008, 2010 Embecosm Limited
5
 
6
   Contributed by Alessandro Forin(af@cs.cmu.edu at CMU
7
   and by Per Bothner(bothner@cs.wisc.edu) at U.Wisconsin.
8
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
9
 
10
   This file is part of GDB.
11
 
12
   This program is free software; you can redistribute it and/or modify it
13
   under the terms of the GNU General Public License as published by the Free
14
   Software Foundation; either version 3 of the License, or (at your option)
15
   any later version.
16
 
17
   This program is distributed in the hope that it will be useful, but WITHOUT
18
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
20
   more details.
21
 
22
   You should have received a copy of the GNU General Public License along
23
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
24
 
25
/*-----------------------------------------------------------------------------
26
   This version for the OpenRISC 1000 architecture is a rewrite by Jeremy
27 522 jeremybenn
   Bennett of the old GDB 5.3 interface to make use of gdbarch for GDB 6.8. It
28
   has since been updated for GDB 7.2.
29 330 jeremybenn
 
30
   The code tries to follow the GDB coding style.
31
 
32
   Commenting is Doxygen compatible.
33
 
34 522 jeremybenn
   Notes on the GDB 7.2 version
35
   ============================
36
 
37
   The primary change is to support the new GCC 4.5.1 compiler, which no
38
   longer adds preceding underscores to global values and uses DWARF2 as its
39
   default debug format.
40
 
41
   This version now supports Or1ksim integrated as a simulator library, so
42
   "target sim" will work. It does require Or1ksim to be available as a
43
   library at configuration time, with the Or1ksim installation directory
44
   specified by the argument --with-or1ksim.
45
 
46
   The ad-hoc prologue analysis, which was always a weak point has been
47
   stripped out and replaced with code based on the generic approach in
48
   prologue-value.c and prologue-value.h.
49
 
50
   The objective with this version is to get reasonable results on regression
51
   testing. Something the older versions never achieved.
52
 
53
   Notes on the GDB 6.8 version
54
   ============================
55
 
56 330 jeremybenn
   Much has been stripped out in the interests of getting a basic working
57
   system. This is described as the OpenRISC 1000 target architecture, so
58 522 jeremybenn
   should work with 32 and 64 bit versions of that architecture and should
59
   work whether or not they have floating point and/or vector registers,
60
   although to date it has only been tested with the 32-bit integer
61
   archtiecture.
62 330 jeremybenn
 
63
   The info trace command has been removed. The meaning of this is not clear -
64
   it relies on a value in register 255 of the debug group, which is
65
   undocumented.
66
 
67
   All the hardware trace has been removed for the time being. The new debug
68
   interface does not support hardware trace, so there is no plan to reinstate
69
   this functionality.
70
 
71
   Support for multiple contexts (which was rudimentary, and not working) has
72 522 jeremybenn
   been removed.                                                             */
73 330 jeremybenn
/*---------------------------------------------------------------------------*/
74
 
75
#include "demangle.h"
76
#include "defs.h"
77
#include "gdb_string.h"
78
#include "frame.h"
79
#include "inferior.h"
80
#include "symtab.h"
81
#include "value.h"
82
#include "gdbcmd.h"
83
#include "language.h"
84
#include "gdbcore.h"
85
#include "symfile.h"
86
#include "objfiles.h"
87
#include "gdbtypes.h"
88
#include "target.h"
89
#include "regcache.h"
90
 
91
#include "opcode/or32.h"
92
#include "or32-tdep.h"
93
 
94
#include "safe-ctype.h"
95
#include "block.h"
96
#include "reggroups.h"
97
#include "arch-utils.h"
98
#include "frame.h"
99
#include "frame-unwind.h"
100
#include "frame-base.h"
101
#include "dwarf2-frame.h"
102
#include "trad-frame.h"
103 565 jeremybenn
#include "regset.h"
104 330 jeremybenn
 
105
#include <inttypes.h>
106
 
107
 
108
 
109
 
110
/* Support functions for the architecture definition */
111
 
112
 
113
/*----------------------------------------------------------------------------*/
114
/*!Get an instruction
115
 
116
   This reads from memory. The old version relied on the frame, this relies
117
   just on the architecture. The old version also guaranteed not to get a
118
   software breakpoint if one had been set. However that seems to happen just
119
   before execution and is removed immediately after, so we believe should not
120
   happen. The old function from GDB 6.8 to do this has been deleted.
121
 
122
   @param[in] gdbarch  Architecture for which we are getting the instruction.
123
   @param[in] addr     Address from which to get the instruction
124
 
125
   @return  The instruction */
126
/*---------------------------------------------------------------------------*/
127
 
128
static ULONGEST
129
or32_fetch_instruction (struct gdbarch *gdbarch,
130
                        CORE_ADDR       addr)
131
{
132
  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
133
  char            buf[OR32_INSTLEN];
134
  int             status;
135
 
136
  status = target_read_memory (addr, buf, OR32_INSTLEN);
137
 
138
  if (status)
139
    {
140
      memory_error (status, addr);
141
    }
142
 
143
  return  extract_unsigned_integer (buf, OR32_INSTLEN, byte_order);
144
 
145
}       /* or32_fetch_instruction() */
146
 
147
 
148
/*---------------------------------------------------------------------------*/
149
/*!Generic function to read bits from an instruction
150
 
151
   printf style. Basic syntax
152
 
153
      or32_analyse_inst (inst, format, &arg1, &arg2 ...)
154
 
155
   Format string can contain the following characters:
156
 
157
   - SPACE:  Ignored, just for layout
158
   - 0:      Match with a zero bit
159
   - 1:      Match with a one bit
160
   - %<n>b:  Match <n> bits to the next argument (n decimal)
161
 
162
   If the arg corresponding to a bit field is non-null, the value will be
163
   assigned to that argument (using NULL allows fields to be skipped).
164
 
165
   Any bad string will cause a fatal error. These are constant strings, so
166
   should be correct.
167
 
168
   The bit field must be 32 bits long. A failure here will cause a fatal error
169
   for the same reason.
170
 
171
   @note The format string is presented MS field to LS field, left to
172
         right. This means that it is read lowest numbered char first.
173
 
174
   @note Some of the arg fields may be populated, even if recognition
175
         ultimately fails.
176
 
177
   @param[in]  inst    The instruction to analyse
178
   @param[in]  format  The format string
179
   @param[out] ...     Argument fields
180
 
181
   @return  1 (TRUE) if the instruction matches, 0 (FALSE) otherwise.        */
182
/*---------------------------------------------------------------------------*/
183
static int
184
or32_analyse_inst (uint32_t    inst,
185
                   const char *format,
186
                   ...)
187
{
188
  /* Break out each field in turn, validating as we go. */
189
  va_list     ap;
190
 
191
  int         i;
192
  int         iptr = 0;                  /* Instruction pointer */
193
 
194
  va_start (ap, format);
195
 
196
  for (i = 0; 0 != format[i];)
197
    {
198
      const char *start_ptr;
199
      char       *end_ptr;
200
 
201
      uint32_t    bits;                 /* Bit substring of interest */
202
      uint32_t    width;                /* Substring width */
203
      uint32_t   *arg_ptr;
204
 
205
      switch (format[i])
206
        {
207
        case ' ': i++; break;   /* Formatting: ignored */
208
 
209
        case '0': case '1':     /* Constant bit field */
210
          bits = (inst >> (OR32_INSTBITLEN - iptr - 1)) & 0x1;
211
 
212
          if ((format[i] - '0') != bits)
213
            {
214
              return  0;
215
            }
216
 
217
          iptr++;
218
          i++;
219
          break;
220
 
221
        case '%':               /* Bit field */
222
          i++;
223
          start_ptr = &(format[i]);
224
          width     = strtoul (start_ptr, &end_ptr, 10);
225
 
226
          /* Check we got something, and if so skip on */
227
          if (start_ptr == end_ptr)
228
            {
229
              fatal ("bitstring \"%s\" at offset %d has no length field.\n",
230
                     format, i);
231
            }
232
 
233
          i += end_ptr - start_ptr;
234
 
235
          /* Look for and skip the terminating 'b'. If it's not there, we
236
             still give a fatal error, because these are fixed strings that
237
             just should not be wrong. */
238
          if ('b' != format[i++])
239
            {
240
              fatal ("bitstring \"%s\" at offset %d has no terminating 'b'.\n",
241
                     format, i);
242
            }
243
 
244
          /* Break out the field. There is a special case with a bit width of
245
             32. */
246
          if (32 == width)
247
            {
248
              bits = inst;
249
            }
250
          else
251
            {
252
              bits = (inst >> (OR32_INSTBITLEN - iptr - width)) & ((1 << width) - 1);
253
            }
254
 
255
          arg_ptr   = va_arg (ap, uint32_t *);
256
          *arg_ptr  = bits;
257
          iptr     += width;
258
          break;
259
 
260
        default:
261
          fatal ("invalid character in bitstring \"%s\" at offset %d.\n",
262
                 format, i);
263
          break;
264
        }
265
    }
266
 
267
  /* Is the length OK? */
268
  gdb_assert (OR32_INSTBITLEN == iptr);
269
 
270
  return  1;                            /* We succeeded */
271
 
272
}       /* or32_analyse_inst () */
273
 
274
 
275
/*---------------------------------------------------------------------------*/
276
/*!Analyse a l.addi instruction
277
 
278
   General form is:
279
 
280
     l.addi  rD,rA,I
281
 
282
   Makes use of the generic analysis function (@see or32_analyse_inst ()).
283
 
284
   @param[in]  inst      The instruction to analyse.
285
   @param[out] rd_ptr    Pointer to the rD value.
286
   @param[out] ra_ptr    Pointer to the rA value.
287
   @param[out] simm_ptr  Pointer to the signed immediate value.
288
 
289
   @return  1 (TRUE) if the instruction matches, 0 (FALSE) otherwise.        */
290
/*---------------------------------------------------------------------------*/
291
static int
292
or32_analyse_l_addi (uint32_t      inst,
293
                     unsigned int *rd_ptr,
294
                     unsigned int *ra_ptr,
295
                     int          *simm_ptr)
296
{
297
  /* Instruction fields */
298
  uint32_t  rd, ra, i;
299
 
300
  if (or32_analyse_inst (inst, "10 0111 %5b %5b %16b", &rd, &ra, &i))
301
    {
302
      /* Found it. Construct the result fields */
303
      *rd_ptr   = (unsigned int) rd;
304
      *ra_ptr   = (unsigned int) ra;
305
      *simm_ptr = (int) (((i & 0x8000) == 0x8000) ? 0xffff0000 | i : i);
306
 
307
      return  1;                /* Success */
308
    }
309
  else
310
    {
311
      return  0;         /* Failure */
312
    }
313
}       /* or32_analyse_l_addi () */
314
 
315
 
316
/*---------------------------------------------------------------------------*/
317
/*!Analyse a l.sw instruction
318
 
319
   General form is:
320
 
321
     l.sw  I(rA),rB
322
 
323
   Makes use of the generic analysis function (@see or32_analyse_inst ()).
324
 
325
   @param[in]  inst      The instruction to analyse.
326
   @param[out] simm_ptr  Pointer to the signed immediate value.
327
   @param[out] ra_ptr    Pointer to the rA value.
328
   @param[out] rb_ptr    Pointer to the rB value.
329
 
330
   @return  1 (TRUE) if the instruction matches, 0 (FALSE) otherwise.        */
331
/*---------------------------------------------------------------------------*/
332
static int
333
or32_analyse_l_sw (uint32_t      inst,
334
                   int          *simm_ptr,
335
                   unsigned int *ra_ptr,
336
                   unsigned int *rb_ptr)
337
{
338
  /* Instruction fields */
339
  uint32_t  ihi, ilo, ra, rb;
340
 
341
  if (or32_analyse_inst (inst, "11 0101 %5b %5b %5b %11b", &ihi, &ra, &rb,
342
                         &ilo))
343
 
344
    {
345
      /* Found it. Construct the result fields */
346
      *simm_ptr  = (int) ((ihi << 11) | ilo);
347
      *simm_ptr |= ((ihi & 0x10) == 0x10) ? 0xffff0000 : 0;
348
 
349
      *ra_ptr    = (unsigned int) ra;
350
      *rb_ptr    = (unsigned int) rb;
351
 
352
      return  1;                /* Success */
353
    }
354
  else
355
    {
356
      return  0;         /* Failure */
357
    }
358
}       /* or32_analyse_l_sw () */
359
 
360
 
361
 
362
 
363
/* Functions defining the architecture */
364
 
365
 
366
/*----------------------------------------------------------------------------*/
367
/*!Determine the return convention used for a given type
368
 
369
   Optionally, fetch or set the return value via "readbuf" or "writebuf"
370
   respectively using "regcache" for the register values.
371
 
372
   The OpenRISC 1000 returns scalar values via R11 and (for 64 bit values on
373
   32 bit architectures) R12. Structs and unions are returned by reference,
374
   with the address in R11
375
 
376
   The result returned is independent of the function type, so we ignore that.
377
 
378
   Throughout use read_memory(), not target_read_memory(), since the address
379
   may be invalid and we want an error reported (read_memory() is
380
   target_read_memory() with error reporting).
381
 
382
   @todo This implementation is labelled OR32, but in fact is just for the 32
383
         bit version, OR32. This should be made explicit
384
 
385
   @param[in]  gdbarch   The GDB architecture being used
386
   @param[in]  functype  The type of the function to be called (may be NULL)
387
   @param[in]  valtype   The type of the entity to be returned
388
   @param[in]  regcache  The register cache
389
   @param[in]  readbuf   Buffer into which the return value should be written
390
   @param[out] writebuf  Buffer from which the return value should be written
391
 
392
   @return  The type of return value */
393
/*---------------------------------------------------------------------------*/
394
 
395
static enum return_value_convention
396
or32_return_value (struct gdbarch  *gdbarch,
397
                   struct type     *functype,
398
                   struct type     *valtype,
399
                   struct regcache *regcache,
400
                   gdb_byte        *readbuf,
401
                   const gdb_byte  *writebuf)
402
{
403
  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
404
  enum type_code  rv_type    = TYPE_CODE (valtype);
405
  unsigned int    rv_size    = TYPE_LENGTH (valtype);
406 531 jeremybenn
  unsigned int    bpw        = (gdbarch_tdep (gdbarch))->bytes_per_word;
407 330 jeremybenn
 
408 531 jeremybenn
  /* Deal with struct/union first. If this won't fit in a single register it
409
     is returned in memory. Large (2 word) scalars are returned in r11 and r12
410
     (this is a change from GCC 4.2.2, when they were apparently returned in
411
     memory). */
412
  if (((TYPE_CODE_STRUCT == rv_type) || (TYPE_CODE_UNION  == rv_type)) &&
413
      (rv_size >  bpw))
414 330 jeremybenn
    {
415
      if (readbuf)
416
        {
417 531 jeremybenn
          ULONGEST        tmp;
418
 
419 330 jeremybenn
          regcache_cooked_read_unsigned (regcache, OR32_RV_REGNUM, &tmp);
420
          read_memory (tmp, readbuf, rv_size);
421
        }
422
      if (writebuf)
423
        {
424 531 jeremybenn
          ULONGEST        tmp;
425
 
426 330 jeremybenn
          regcache_cooked_read_unsigned (regcache, OR32_RV_REGNUM, &tmp);
427
          write_memory (tmp, writebuf, rv_size);
428
        }
429
 
430
      return RETURN_VALUE_ABI_RETURNS_ADDRESS;
431
    }
432
 
433 531 jeremybenn
  if (rv_size <= bpw)
434
    {
435
      /* up to one word scalars are returned in R11 */
436
      if (readbuf)
437
        {
438
          ULONGEST        tmp;
439 330 jeremybenn
 
440 531 jeremybenn
          regcache_cooked_read_unsigned (regcache, OR32_RV_REGNUM, &tmp);
441
          store_unsigned_integer (readbuf, rv_size, byte_order, tmp);
442
 
443
        }
444
      if (writebuf)
445
        {
446
          gdb_byte buf[4];                      /* TODO - fixed const! */
447
          memset (buf, 0, sizeof (buf)); /* Zero pad if < bpw bytes */
448
 
449
          if (BFD_ENDIAN_BIG == byte_order)
450
            {
451
              memcpy (buf + sizeof (buf) - rv_size, writebuf, rv_size);
452
            }
453
          else
454
            {
455
              memcpy (buf,                          writebuf, rv_size);
456
            }
457
 
458
          regcache_cooked_write (regcache, OR32_RV_REGNUM, buf);
459
        }
460 330 jeremybenn
    }
461 531 jeremybenn
  else
462 330 jeremybenn
    {
463 531 jeremybenn
      /* 2 word scalars are returned in r11/r12 (with the MS word in r11). */
464
      if (readbuf)
465
        {
466
          ULONGEST        tmp_lo;
467
          ULONGEST        tmp_hi;
468
          ULONGEST        tmp;
469 330 jeremybenn
 
470 531 jeremybenn
          regcache_cooked_read_unsigned (regcache, OR32_RV_REGNUM    , &tmp_hi);
471
          regcache_cooked_read_unsigned (regcache, OR32_RV_REGNUM + 1, &tmp_lo);
472
          tmp = (tmp_hi << (bpw * 8)) | tmp_lo;
473
 
474
          store_unsigned_integer (readbuf, rv_size, byte_order, tmp);
475 330 jeremybenn
        }
476 531 jeremybenn
      if (writebuf)
477 330 jeremybenn
        {
478 531 jeremybenn
          gdb_byte buf_lo[4];                   /* TODO - fixed const! */
479
          gdb_byte buf_hi[4];                   /* TODO - fixed const! */
480
 
481
          memset (buf_lo, 0, sizeof (buf_lo));   /* Zero pad if < bpw bytes */
482
          memset (buf_hi, 0, sizeof (buf_hi));   /* Zero pad if < bpw bytes */
483
 
484
          /* This is cheating. We assume that we fit in 2 words exactly, which
485
             wouldn't work if we had (say) a 6-byte scalar type on a big
486
             endian architecture (with the OpenRISC 1000 usually is). */
487
          memcpy (buf_hi, writebuf,       rv_size - bpw);
488
          memcpy (buf_lo, writebuf + bpw, bpw);
489
 
490
          regcache_cooked_write (regcache, OR32_RV_REGNUM,     buf_hi);
491
          regcache_cooked_write (regcache, OR32_RV_REGNUM + 1, buf_lo);
492 330 jeremybenn
        }
493
    }
494
 
495
  return RETURN_VALUE_REGISTER_CONVENTION;
496
 
497
}       /* or32_return_value() */
498
 
499
 
500
/*----------------------------------------------------------------------------*/
501
/*!Determine the instruction to use for a breakpoint.
502
 
503
   Given the address at which to insert a breakpoint (bp_addr), what will
504
   that breakpoint be?
505
 
506
   For or32, we have a breakpoint instruction. Since all or32 instructions
507
   are 32 bits, this is all we need, regardless of address.
508
 
509
   @param[in]  gdbarch  The GDB architecture being used
510
   @param[in]  bp_addr  The breakpoint address in question
511
   @param[out] bp_size  The size of instruction selected
512
 
513
   @return  The chosen breakpoint instruction */
514
/*---------------------------------------------------------------------------*/
515
 
516
static const gdb_byte *
517
or32_breakpoint_from_pc (struct gdbarch *gdbarch,
518
                         CORE_ADDR      *bp_addr,
519
                         int            *bp_size)
520
{
521
  static const gdb_byte breakpoint[] = OR32_BRK_INSTR_STRUCT;
522
 
523
  *bp_size = OR32_INSTLEN;
524
  return breakpoint;
525
 
526
}       /* or32_breakpoint_from_pc() */
527
 
528
 
529
/*----------------------------------------------------------------------------*/
530
/*!Determine if we are executing a delay slot
531
 
532
   Looks at the instruction at the previous instruction to see if it was one
533
   with a delay slot. But it also has to be the address prior to NPC, because
534
   we may have just taken an exception.
535
 
536
   @param[in] gdbarch     The GDB architecture being used
537
   @param[in] this_frame  Information about THIS frame
538
 
539
   @return  1 (true) if this instruction is executing a delay slot, 0 (false)
540
   otherwise. */
541
/*--------------------------------------------------------------------------*/
542
 
543
static int
544
or32_single_step_through_delay( struct gdbarch    *gdbarch,
545
                                struct frame_info *this_frame )
546
{
547
  struct regcache   *regcache = get_current_regcache ();
548
  ULONGEST           val;
549
  CORE_ADDR          ppc;
550
  CORE_ADDR          npc;
551
  int                index;
552
 
553
  /* Get and the previous and current instruction addresses. If they are not
554
     adjacent, we cannot be in a delay slot. */
555
  regcache_cooked_read_unsigned (regcache, OR32_PPC_REGNUM, &val);
556
  ppc        = (CORE_ADDR) val;
557
  regcache_cooked_read_unsigned (regcache, OR32_NPC_REGNUM, &val);
558
  npc        = (CORE_ADDR) val;
559
 
560
  if (0x4 != (npc - ppc))
561
    {
562
      return  0;
563
    }
564
 
565
  /* Decode the previous instruction to see if it was a branch or a jump, and
566
     hence we are in a delay slot. */
567
  index = insn_decode (or32_fetch_instruction (gdbarch, ppc));
568
  return  or32_opcodes[index].flags & OR32_IF_DELAY;
569
 
570
}       /* or32_single_step_through_delay() */
571
 
572
 
573
/*----------------------------------------------------------------------------*/
574
/*!Read a pseudo register
575
 
576
   Since we have no pseudo registers this is a null function for now.
577
 
578
   @todo The floating point and vector registers ought to be done as
579
         pseudo-registers.
580
 
581
   @param[in]  gdbarch   The GDB architecture to consider
582
   @param[in]  regcache  The cached register values as an array
583
   @param[in]  regnum    The register to read
584
   @param[out] buf       A buffer to put the result in */
585
/*---------------------------------------------------------------------------*/
586
 
587
static void
588
or32_pseudo_register_read (struct gdbarch  *gdbarch,
589
                           struct regcache *regcache,
590
                           int              regnum,
591
                           gdb_byte        *buf)
592
{
593
  return;
594
 
595
}       /* or32_pseudo_register_read() */
596
 
597
 
598
/*----------------------------------------------------------------------------*/
599
/*!Write a pseudo register
600
 
601
   Since we have no pseudo registers this is a null function for now.
602
 
603
   @todo The floating point and vector registers ought to be done as
604
         pseudo-registers.
605
 
606
   @param[in] gdbarch   The GDB architecture to consider
607
   @param[in] regcache  The cached register values as an array
608
   @param[in] regnum    The register to read
609
   @param[in] buf       A buffer with the value to write */
610
/*---------------------------------------------------------------------------*/
611
 
612
static void
613
or32_pseudo_register_write (struct gdbarch  *gdbarch,
614
                            struct regcache *regcache,
615
                            int              regnum,
616
                            const gdb_byte  *buf)
617
{
618
  return;
619
 
620
}       /* or32_pseudo_register_write() */
621
 
622
 
623
/*----------------------------------------------------------------------------*/
624
/*!Return the register name for the OpenRISC 1000 architecture
625
 
626
   This version converted to ANSI C, made static and incorporates the static
627
   table of register names (this is the only place it is referenced).
628
 
629
   @todo The floating point and vector registers ought to be done as
630
         pseudo-registers.
631
 
632
   @param[in] gdbarch  The GDB architecture being used
633
   @param[in] regnum    The register number
634
 
635
   @return  The textual name of the register */
636
/*---------------------------------------------------------------------------*/
637
 
638
static const char *
639
or32_register_name (struct gdbarch *gdbarch,
640
                    int             regnum)
641
{
642
  static char *or32_gdb_reg_names[OR32_TOTAL_NUM_REGS] =
643
    {
644
      /* general purpose registers */
645 524 jeremybenn
      "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
646
      "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
647 330 jeremybenn
      "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
648
      "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
649
 
650
      /* previous program counter, next program counter and status register */
651 524 jeremybenn
      "ppc",   "npc",   "sr"
652 330 jeremybenn
 
653
      /* Floating point and vector registers may appear as pseudo registers in
654
         the future. */
655
    };
656
 
657
  return or32_gdb_reg_names[regnum];
658
 
659
}       /* or32_register_name() */
660
 
661
 
662
/*----------------------------------------------------------------------------*/
663
/*!Identify the type of a register
664
 
665
   @todo I don't fully understand exactly what this does, but I think this
666
         makes sense!
667
 
668
   @param[in] arch     The GDB architecture to consider
669
   @param[in] regnum   The register to identify
670
 
671
   @return  The type of the register */
672
/*---------------------------------------------------------------------------*/
673
 
674
static struct type *
675
or32_register_type (struct gdbarch *arch,
676
                    int             regnum)
677
{
678
  static struct type *void_func_ptr = NULL;
679
  static struct type *void_ptr      = NULL;
680
 
681
  /* Set up the static pointers once, the first time*/
682
  if (NULL == void_func_ptr)
683
    {
684
      struct type *void_type = builtin_type (arch)->builtin_void;
685
 
686
      void_ptr      = lookup_pointer_type (void_type);
687
      void_func_ptr = lookup_pointer_type (lookup_function_type (void_type));
688
    }
689
 
690
  if((regnum >= 0) && (regnum < OR32_TOTAL_NUM_REGS))
691
    {
692
      switch (regnum)
693
        {
694
        case OR32_PPC_REGNUM:
695
        case OR32_NPC_REGNUM:
696
          return void_func_ptr;                 /* Pointer to code */
697
 
698
        case OR32_SP_REGNUM:
699
        case OR32_FP_REGNUM:
700
          return void_ptr;                              /* Pointer to data */
701
 
702
        default:
703
          return builtin_type (arch)->builtin_uint32;   /* Data */
704
        }
705
    }
706
 
707
  internal_error (__FILE__, __LINE__,
708
                  _("or32_register_type: illegal register number %d"), regnum);
709
 
710
}       /* or32_register_type() */
711
 
712
 
713
/*----------------------------------------------------------------------------*/
714
/*!Handle the "info register" command
715
 
716
   Print the identified register, unless it is -1, in which case print all
717
   the registers. If all is 1 means all registers, otherwise only the core
718
   GPRs.
719
 
720
   @todo At present all registers are printed with the default method. Should
721
         there be something special for FP registers?
722
 
723
   @param[in] gdbarch  The GDB architecture being used
724
   @param[in] file     File handle for use with any custom I/O
725
   @param[in] frame    Frame info for use with custom output
726
   @param[in] regnum   Register of interest, or -1 if all registers
727
   @param[in] all      1 if all means all, 0 if all means just GPRs
728
 
729
   @return  The aligned stack frame address */
730
/*---------------------------------------------------------------------------*/
731
 
732
static void
733
or32_registers_info (struct gdbarch    *gdbarch,
734
                     struct ui_file    *file,
735
                     struct frame_info *frame,
736
                     int                regnum,
737
                     int                all)
738
{
739
  if (-1 == regnum)
740
    {
741
      /* Do all (valid) registers */
742
      unsigned int  lim = all ? OR32_NUM_REGS : OR32_MAX_GPR_REGS;
743
 
744
      for (regnum = 0; regnum < lim; regnum++) {
745
        if ('\0' != *(or32_register_name (gdbarch, regnum)))
746
          {
747
            or32_registers_info (gdbarch, file, frame, regnum, all);
748
          }
749
      }
750
    }
751
  else
752
    {
753
      /* Do one specified register - if it is part of this architecture */
754
      if ('\0' == *(or32_register_name (gdbarch, regnum)))
755
        {
756
          error ("Not a valid register for the current processor type");
757
        }
758
      else
759
        {
760
          default_print_registers_info (gdbarch, file, frame, regnum, all);
761
        }
762
    }
763
}       /* or32_registers_info() */
764
 
765
 
766
/*----------------------------------------------------------------------------*/
767
/*!Identify if a register belongs to a specified group
768
 
769
   Return true if the specified register is a member of the specified
770
   register group.
771
 
772
   These are the groups of registers that can be displayed via "info reg".
773
 
774
   @todo The Vector and Floating Point registers ought to be displayed as
775
         pseudo-registers.
776
 
777
   @param[in] gdbarch  The GDB architecture to consider
778
   @param[in] regnum   The register to consider
779
   @param[in] group    The group to consider
780
 
781
   @return  True (1) if regnum is a member of group */
782
/*---------------------------------------------------------------------------*/
783
 
784
static int
785
or32_register_reggroup_p (struct gdbarch  *gdbarch,
786
                          int              regnum,
787
                          struct reggroup *group)
788
{
789
  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
790
 
791
  /* All register group */
792
  if (group == all_reggroup)
793
    {
794
      return ((regnum >= 0) &&
795
              (regnum < OR32_TOTAL_NUM_REGS) &&
796
              (or32_register_name (gdbarch, regnum)[0] != '\0'));
797
    }
798
 
799
  /* For now everything except the PC */
800
  if (group == general_reggroup)
801
    {
802
      return ((regnum >= OR32_ZERO_REGNUM) &&
803
              (regnum <  tdep->num_gpr_regs) &&
804
              (regnum != OR32_PPC_REGNUM) &&
805
              (regnum != OR32_NPC_REGNUM));
806
    }
807
 
808
  if (group == float_reggroup)
809
    {
810
      return 0;                  /* No float regs.  */
811
    }
812
 
813
  if (group == vector_reggroup)
814
    {
815
      return 0;                  /* No vector regs.  */
816
    }
817
 
818
  /* For any that are not handled above.  */
819
  return default_register_reggroup_p (gdbarch, regnum, group);
820
 
821
}       /* or32_register_reggroup_p() */
822
 
823
 
824
/*----------------------------------------------------------------------------*/
825
/*!Is this one of the registers used for passing arguments?
826
 
827
   These are r3-r8 in the API.
828
 
829
   @param[in] regnum  The register to consider
830
 
831
   @return  Non-zero (TRUE) if it is an argument register, zero (FALSE)
832
            otherwise.                                                        */
833
/*----------------------------------------------------------------------------*/
834
static int
835
or32_is_arg_reg (unsigned int  regnum)
836
{
837
  return (OR32_FIRST_ARG_REGNUM <= regnum) && (regnum <= OR32_LAST_ARG_REGNUM);
838
 
839
}       /* or32_is_arg_reg () */
840
 
841
 
842
/*----------------------------------------------------------------------------*/
843
/*!Is this a callee saved register?
844
 
845
   These are r10, r12, r14, r16, r18, r20, r22, r24, r26, r28 and r30 in the
846
   API.
847
 
848
   @param[in] regnum  The register to consider
849
 
850
   @return  Non-zero (TRUE) if it is a callee saved register, zero (FALSE)
851
            otherwise.                                                        */
852
/*----------------------------------------------------------------------------*/
853
static int
854
or32_is_callee_saved_reg (unsigned int  regnum)
855
{
856
  return (OR32_FIRST_SAVED_REGNUM <= regnum) && (0 == regnum % 2);
857
 
858
}       /* or32_is_callee_saved_reg () */
859
 
860
 
861
/*----------------------------------------------------------------------------*/
862
/*!Skip a function prolog
863
 
864
   If the input address, PC, is in a function prologue, return the address of
865
   the end of the prologue, otherwise return the input  address.
866
 
867
   @see For details of the stack frame, see the function
868
        or32_frame_cache().
869
 
870
   @note The old version of this function used to use skip_prologue_using_sal
871
         to skip the prologue without checking if it had actually worked. It
872
         doesn't for STABS, so we had better check for a valid result.
873
 
874
   This function reuses the helper functions from or32_frame_cache() to
875
   locate the various parts of the prolog, any or all of which may be missing.
876
 
877
   @param[in] gdbarch  The GDB architecture being used
878
   @param[in] pc       Current program counter
879
 
880
   @return  The address of the end of the prolog if the PC is in a function
881
            prologue, otherwise the input  address.                           */
882
/*----------------------------------------------------------------------------*/
883
static CORE_ADDR
884
or32_skip_prologue (struct gdbarch *gdbarch,
885
                    CORE_ADDR       pc)
886
{
887
  CORE_ADDR     start_pc;
888
  CORE_ADDR     addr;
889
  uint32_t      inst;
890
 
891
  unsigned int  ra, rb, rd;             /* For instruction analysis */
892
  int           simm;
893
 
894
  int           frame_size = 0;
895
 
896
  /* Try using SAL first if we have symbolic information available. This only
897
     works for DWARF 2, not STABS. */
898
  if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
899
    {
900
      CORE_ADDR  prologue_end = skip_prologue_using_sal( gdbarch, pc );
901
 
902
      if (0 != prologue_end)
903
        {
904
          struct symtab_and_line  prologue_sal = find_pc_line (start_pc, 0);
905
          char *debug_format = prologue_sal.symtab->debugformat;
906
 
907
          if ((strlen ("dwarf") <= strlen (debug_format))
908
              && (0 == strncasecmp ("dwarf", debug_format, strlen ("dwarf"))))
909
            {
910
              return  (prologue_end > pc) ? prologue_end : pc;
911
            }
912
        }
913
    }
914
 
915
  /* Look to see if we can find any of the standard prologue sequence. All
916
     quite difficult, since any or all of it may be missing. So this is just a
917
     best guess! */
918
  addr = pc;                            /* Where we have got to */
919
  inst = or32_fetch_instruction (gdbarch, addr);
920
 
921
  /* Look for the new stack pointer being set up. */
922
  if (or32_analyse_l_addi (inst, &rd, &ra, &simm) &&
923
      (OR32_SP_REGNUM == rd) && (OR32_SP_REGNUM == ra) &&
924
      (simm < 0) && (0 == (simm % 4)))
925
    {
926
      frame_size  = -simm;
927
      addr       += OR32_INSTLEN;
928
      inst        = or32_fetch_instruction (gdbarch, addr);
929
    }
930
 
931
  /* Look for the frame pointer being manipulated. */
932
  if (or32_analyse_l_sw (inst, &simm, &ra, &rb) &&
933
      (OR32_SP_REGNUM == ra) && (OR32_FP_REGNUM == rb) &&
934
      (simm >= 0) && (0 == (simm % 4)))
935
    {
936
      addr += OR32_INSTLEN;
937
      inst  = or32_fetch_instruction (gdbarch, addr);
938
 
939
      gdb_assert (or32_analyse_l_addi (inst, &rd, &ra, &simm) &&
940
                  (OR32_FP_REGNUM == rd) && (OR32_SP_REGNUM == ra) &&
941
                  (simm == frame_size));
942
 
943
      addr += OR32_INSTLEN;
944
      inst  = or32_fetch_instruction (gdbarch, addr);
945
    }
946
 
947
  /* Look for the link register being saved */
948
  if (or32_analyse_l_sw (inst, &simm, &ra, &rb) &&
949
      (OR32_SP_REGNUM == ra) && (OR32_LR_REGNUM == rb) &&
950
      (simm >= 0) && (0 == (simm % 4)))
951
    {
952
      addr += OR32_INSTLEN;
953
      inst  = or32_fetch_instruction (gdbarch, addr);
954
    }
955
 
956
  /* Look for arguments or callee-saved register being saved. The register
957
     must be one of the arguments (r3-r8) or the 10 callee saved registers
958
     (r10, r12, r14, r16, r18, r20, r22, r24, r26, r28, r30). The base
959
     register must be the FP (for the args) or the SP (for the callee_saved
960
     registers). */
961
  while (1)
962
    {
963
      if (or32_analyse_l_sw (inst, &simm, &ra, &rb) &&
964
          (((OR32_FP_REGNUM == ra) && or32_is_arg_reg (rb)) ||
965
           ((OR32_SP_REGNUM == ra) && or32_is_callee_saved_reg (rb))) &&
966
          (0 == (simm % 4)))
967
        {
968
          addr += OR32_INSTLEN;
969
          inst  = or32_fetch_instruction (gdbarch, addr);
970
        }
971
      else
972
        {
973
          /* Nothing else to look for. We have found the end of the prologue. */
974
          return  addr;
975
        }
976
    }
977
}       /* or32_skip_prologue() */
978
 
979
 
980
/*----------------------------------------------------------------------------*/
981
/*!Align the stack frame
982
 
983
   OpenRISC 1000 uses a falling stack frame, so this aligns down to the
984
   nearest 8 bytes. Useful when we'be building a dummy frame.
985
 
986
   @param[in] gdbarch  The GDB architecture being used
987
   @param[in] sp       Current stack pointer
988
 
989
   @return  The aligned stack frame address */
990
/*---------------------------------------------------------------------------*/
991
 
992
static CORE_ADDR
993
or32_frame_align (struct gdbarch *gdbarch,
994
                  CORE_ADDR       sp)
995
{
996
  return align_down (sp, OR32_STACK_ALIGN);
997
 
998
}       /* or32_frame_align() */
999
 
1000
 
1001
/*----------------------------------------------------------------------------*/
1002
/*!Unwind the program counter from a stack frame
1003
 
1004
   This just uses the built in frame unwinder
1005
 
1006
   @param[in] gdbarch     The GDB architecture being used
1007
   @param[in] next_frame  Frame info for the NEXT frame
1008
 
1009
   @return  The program counter for THIS frame */
1010
/*---------------------------------------------------------------------------*/
1011
 
1012
static CORE_ADDR
1013
or32_unwind_pc (struct gdbarch    *gdbarch,
1014
                struct frame_info *next_frame)
1015
{
1016 531 jeremybenn
  CORE_ADDR pc;
1017
 
1018 524 jeremybenn
  if (frame_debug)
1019
    {
1020
      fprintf_unfiltered (gdb_stdlog, "or32_unwind_pc, next_frame=%d\n",
1021
                          frame_relative_level (next_frame));
1022
    }
1023
 
1024 531 jeremybenn
  pc = frame_unwind_register_unsigned (next_frame, OR32_NPC_REGNUM);
1025 330 jeremybenn
 
1026 522 jeremybenn
  if (frame_debug)
1027
    {
1028 524 jeremybenn
      fprintf_unfiltered (gdb_stdlog, "or32_unwind_pc, pc=0x%p\n", (void *) pc);
1029 522 jeremybenn
    }
1030
 
1031 330 jeremybenn
  return pc;
1032
 
1033
}       /* or32_unwind_pc() */
1034
 
1035
 
1036
/*----------------------------------------------------------------------------*/
1037
/*!Unwind the stack pointer from a stack frame
1038
 
1039
   This just uses the built in frame unwinder
1040
 
1041
   @param[in] gdbarch     The GDB architecture being used
1042
   @param[in] next_frame  Frame info for the NEXT frame
1043
 
1044
   @return  The stack pointer for THIS frame */
1045
/*---------------------------------------------------------------------------*/
1046
 
1047
static CORE_ADDR
1048
or32_unwind_sp (struct gdbarch    *gdbarch,
1049
                struct frame_info *next_frame)
1050
{
1051 531 jeremybenn
  CORE_ADDR sp;
1052
 
1053 524 jeremybenn
  if (frame_debug)
1054
    {
1055
      fprintf_unfiltered (gdb_stdlog, "or32_unwind_sp, next_frame=%d\n",
1056
                          frame_relative_level (next_frame));
1057
    }
1058
 
1059 531 jeremybenn
  sp = frame_unwind_register_unsigned (next_frame, OR32_SP_REGNUM);
1060 330 jeremybenn
 
1061 522 jeremybenn
  if (frame_debug)
1062
    {
1063 524 jeremybenn
      fprintf_unfiltered (gdb_stdlog, "or32_unwind_sp, sp=0x%p\n", (void *) sp);
1064 522 jeremybenn
    }
1065
 
1066 330 jeremybenn
  return sp;
1067
 
1068
}       /* or32_unwind_sp() */
1069
 
1070
 
1071
/*----------------------------------------------------------------------------*/
1072
/*!Create a dummy stack frame
1073
 
1074
   The arguments are placed in registers and/or pushed on the stack as per the
1075
   OR32 ABI.
1076
 
1077
   @param[in] gdbarch        The architecture to use
1078
   @param[in] function       Pointer to the function that will be called
1079
   @param[in] regcache       The register cache to use
1080
   @param[in] bp_addr        Breakpoint address
1081
   @param[in] nargs          Number of ags to push
1082
   @param[in] args           The arguments
1083
   @param[in] sp             The stack pointer
1084
   @param[in] struct_return  True (1) if this returns a structure
1085
   @param[in] struct_addr    Address for returning structures
1086
 
1087
   @return  The updated stack pointer */
1088
/*---------------------------------------------------------------------------*/
1089
 
1090
static CORE_ADDR
1091
or32_push_dummy_call (struct gdbarch  *gdbarch,
1092
                      struct value    *function,
1093
                      struct regcache *regcache,
1094
                      CORE_ADDR        bp_addr,
1095
                      int              nargs,
1096
                      struct value   **args,
1097
                      CORE_ADDR        sp,
1098
                      int              struct_return,
1099
                      CORE_ADDR        struct_addr)
1100
{
1101
  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1102
 
1103
  int             argreg;
1104
  int             argnum;
1105
  int             first_stack_arg;
1106
  int             stack_offset = 0;
1107
 
1108
  unsigned int    bpa = (gdbarch_tdep (gdbarch))->bytes_per_address;
1109
  unsigned int    bpw = (gdbarch_tdep (gdbarch))->bytes_per_word;
1110
 
1111
  /* Return address */
1112
  regcache_cooked_write_unsigned (regcache, OR32_LR_REGNUM, bp_addr);
1113
 
1114
  /* Register for the next argument */
1115
  argreg = OR32_FIRST_ARG_REGNUM;
1116
 
1117
  /* Location for a returned structure. This is passed as a silent first
1118
     argument. */
1119
 
1120
  if (struct_return)
1121
    {
1122
      regcache_cooked_write_unsigned (regcache, OR32_FIRST_ARG_REGNUM,
1123
                                      struct_addr);
1124
      argreg++;
1125
    }
1126
 
1127
  /* Put as many args as possible in registers */
1128
  for (argnum = 0; argnum < nargs; argnum++)
1129
    {
1130
      char           *val;
1131
      char            valbuf[sizeof (ULONGEST) ];
1132
 
1133
      struct value   *arg      = args[argnum];
1134
      struct type    *arg_type = check_typedef (value_type (arg));
1135
      int             len      = arg_type->length;
1136
      enum type_code  typecode = arg_type->main_type->code;
1137
 
1138 531 jeremybenn
      /* Handle the different argument types. */
1139
      if((TYPE_CODE_STRUCT == typecode) || (TYPE_CODE_UNION == typecode))
1140 330 jeremybenn
        {
1141 531 jeremybenn
          /* The ABI passes all structures by reference, so get its address. */
1142
          store_unsigned_integer (valbuf, bpa, byte_order, value_address (arg));
1143 330 jeremybenn
          len      = bpa;
1144
          val      = valbuf;
1145
        }
1146
      else
1147
        {
1148 531 jeremybenn
          /* Everything else, we just get the value. */
1149
          val = (char *) value_contents (arg);
1150
        }
1151 330 jeremybenn
 
1152 531 jeremybenn
      /* Stick the value in a register */
1153
      if(len > bpw)
1154 330 jeremybenn
        {
1155
          /* Big scalars use two registers, but need NOT be pair aligned. This
1156
             code breaks if we can have quad-word scalars (e.g. long
1157
             double). */
1158
 
1159 531 jeremybenn
          if (argreg <= (OR32_LAST_ARG_REGNUM - 1))
1160
            {
1161
              ULONGEST regval = extract_unsigned_integer (val, len, byte_order);
1162 330 jeremybenn
 
1163 531 jeremybenn
              unsigned int  bits_per_word = bpw * 8;
1164
              ULONGEST      mask = (((ULONGEST) 1) << bits_per_word) - 1;
1165
              ULONGEST      lo   = regval & mask;
1166
              ULONGEST      hi   = regval >> bits_per_word;
1167
 
1168
              gdb_assert (len <= (bpw * 2));
1169
 
1170
              regcache_cooked_write_unsigned (regcache, argreg,     hi);
1171
              regcache_cooked_write_unsigned (regcache, argreg + 1, lo);
1172
              argreg += 2;
1173
            }
1174
          else
1175
            {
1176
              /* Run out of regs */
1177
              break;
1178
            }
1179 330 jeremybenn
        }
1180
      else if (argreg <= OR32_LAST_ARG_REGNUM)
1181
        {
1182 531 jeremybenn
          /* Smaller scalars fit in a single register */
1183 330 jeremybenn
          regcache_cooked_write_unsigned (regcache, argreg,
1184
                                          extract_unsigned_integer (val, len,
1185
                                                                   byte_order));
1186
          argreg++;
1187
        }
1188
      else
1189
        {
1190
          /* Run out of regs */
1191
          break;
1192
        }
1193
    }
1194
 
1195
  first_stack_arg = argnum;
1196
 
1197
  /* If we get here with argnum < nargs, then arguments remain to be placed on
1198
     the stack. This is tricky, since they must be pushed in reverse order and
1199
     the stack in the end must be aligned. The only solution is to do it in
1200
     two stages, the first to compute the stack size, the second to save the
1201
     args. */
1202
 
1203
  for (argnum = first_stack_arg; argnum < nargs; argnum++)
1204
    {
1205
      struct value   *arg      = args[argnum];
1206
      struct type    *arg_type = check_typedef (value_type (arg));
1207
      int             len      = arg_type->length;
1208
      enum type_code  typecode = arg_type->main_type->code;
1209
 
1210 531 jeremybenn
      if((TYPE_CODE_STRUCT == typecode) || (TYPE_CODE_UNION == typecode))
1211 330 jeremybenn
        {
1212 531 jeremybenn
          /* Structures are passed as addresses */
1213 330 jeremybenn
          sp -= bpa;
1214
        }
1215
      else
1216
        {
1217
        /* Big scalars use more than one word. Code here allows for future
1218
         quad-word entities (e.g. long double) */
1219
          sp -= ((len + bpw - 1) / bpw) * bpw;
1220
        }
1221
    }
1222
 
1223
  sp           = gdbarch_frame_align (gdbarch, sp);
1224
  stack_offset = 0;
1225
 
1226
  /* Push the remaining args on the stack */
1227
  for (argnum = first_stack_arg; argnum < nargs; argnum++)
1228
    {
1229
      char           *val;
1230
      char            valbuf[sizeof (ULONGEST) ];
1231
 
1232
      struct value   *arg      = args[argnum];
1233
      struct type    *arg_type = check_typedef (value_type (arg));
1234
      int             len      = arg_type->length;
1235
      enum type_code  typecode = arg_type->main_type->code;
1236
 
1237
      /* The EABI passes structures that do not fit in a register by
1238
         reference. In all other cases, pass the structure by value.  */
1239 531 jeremybenn
      if((TYPE_CODE_STRUCT == typecode) || (TYPE_CODE_UNION == typecode))
1240 330 jeremybenn
        {
1241 531 jeremybenn
          store_unsigned_integer (valbuf, bpa, byte_order, value_address (arg));
1242 330 jeremybenn
          len      = bpa;
1243
          val      = valbuf;
1244
        }
1245
      else
1246
        {
1247 531 jeremybenn
          val = (char *) value_contents (arg);
1248 330 jeremybenn
        }
1249
 
1250
      gdb_assert (len <= (bpw * 2));
1251
 
1252
      write_memory (sp + stack_offset, val, len);
1253
      stack_offset += ((len + bpw - 1) / bpw) * bpw;
1254
    }
1255
 
1256
  /* Save the updated stack pointer */
1257
  regcache_cooked_write_unsigned (regcache, OR32_SP_REGNUM, sp);
1258
 
1259
  return sp;
1260
 
1261
}       /* or32_push_dummy_call() */
1262
 
1263
 
1264
/*----------------------------------------------------------------------------*/
1265
/*!Return the frame ID for a dummy stack frame
1266
 
1267
   Tear down a dummy frame created by or32_push_dummy_call(). This data has to
1268
   be constructed manually from the data in our hand.
1269
 
1270
   The stack pointer and program counter can be obtained from the frame info.
1271
 
1272
   @param[in] gdbarch     The architecture to use
1273
   @param[in] this_frame  Information about this frame
1274
 
1275
   @return  Frame ID of this frame */
1276
/*---------------------------------------------------------------------------*/
1277
 
1278
static struct frame_id
1279
or32_dummy_id (struct gdbarch    *gdbarch,
1280
               struct frame_info *this_frame)
1281
{
1282
  return  frame_id_build (get_frame_sp (this_frame), get_frame_pc (this_frame));
1283
 
1284
}       /* or32_dummy_id() */
1285
 
1286
 
1287
 
1288
 
1289
/* Support functions for frame handling */
1290
 
1291
/* -------------------------------------------------------------------------- */
1292
/*!Initialize a prologue cache
1293
 
1294
   This function is changed from its GDB 6.8 version (named
1295
   or32_frame_unwind_cache), in that it is based on THIS frame, not the NEXT
1296
   frame.
1297
 
1298
   We build a cache, saying where registers of the PREV frame can be found
1299
   from the data so far set up in this THIS.
1300
 
1301
   We also compute a unique ID for this frame, based on the function start
1302
   address and the stack pointer (as it will be, even if it has yet to be
1303
   computed.
1304
 
1305
   STACK FORMAT
1306
   ============
1307
 
1308
   The OR32 has a falling stack frame and a simple prolog. The Stack pointer
1309
   is R1 and the frame pointer R2. The frame base is therefore the address
1310
   held in R2 and the stack pointer (R1) is the frame base of the NEXT frame.
1311
 
1312
   @verbatim
1313
   l.addi  r1,r1,-frame_size    # SP now points to end of new stack frame
1314
   @endverbatim
1315
 
1316
   The stack pointer may not be set up in a frameless function (e.g. a simple
1317
   leaf function).
1318
 
1319
   @verbatim
1320
   l.sw    fp_loc(r1),r2        # old FP saved in new stack frame
1321
   l.addi  r2,r1,frame_size     # FP now points to base of new stack frame
1322
   @endverbatim
1323
 
1324
   The frame pointer is not necessarily saved right at the end of the stack
1325
   frame - OR32 saves enough space for any args to called functions right at
1326
   the end (this is a difference from the Architecture Manual).
1327
 
1328
   @verbatim
1329
   l.sw    lr_loc(r1),r9        # Link (return) address
1330
   @endverbatim
1331
 
1332
   The link register is usally saved at fp_loc - 4. It may not be saved at all
1333
   in a leaf function.
1334
 
1335
   @verbatim
1336
   l.sw    reg_loc(r1),ry       # Save any callee saved regs
1337
   @endverbatim
1338
 
1339
   The offsets x for the callee saved registers generally (always?) rise in
1340
   increments of 4, starting at fp_loc + 4. If the frame pointer is omitted
1341
   (an option to GCC), then it may not be saved at all. There may be no callee
1342
   saved registers.
1343
 
1344
   So in summary none of this may be present. However what is present seems
1345
   always to follow this fixed order, and occur before any substantive code
1346
   (it is possible for GCC to have more flexible scheduling of the prologue,
1347
   but this does not seem to occur for OR32).
1348
 
1349
   ANALYSIS
1350
   ========
1351
 
1352
   This prolog is used, even for -O3 with GCC.
1353
 
1354
   All this analysis must allow for the possibility that the PC is in the
1355
   middle of the prologue. Data in the cache should only be set up insofar as
1356
   it has been computed.
1357
 
1358
   HOWEVER. The frame_id must be created with the SP *as it will be* at the
1359
   end of the Prologue. Otherwise a recursive call, checking the frame with
1360
   the PC at the start address will end up with the same frame_id as the
1361
   caller.
1362
 
1363
   A suite of "helper" routines are used, allowing reuse for
1364
   or32_skip_prologue().
1365
 
1366
   Reportedly, this is only valid for frames less than 0x7fff in size.
1367
 
1368
   @param[in]     this_frame      Our stack frame.
1369
   @param[in,out] prologue_cache  The prologue cache. If not supplied, we
1370
                                  build it.
1371
 
1372
   @return  The prolog cache (duplicates the return through the argument) */
1373
/* ---------------------------------------------------------------------------*/
1374
static struct trad_frame_cache *
1375
or32_frame_cache (struct frame_info  *this_frame,
1376
                  void              **prologue_cache)
1377
{
1378
  struct gdbarch          *gdbarch;
1379
  struct trad_frame_cache *info;
1380
 
1381
  CORE_ADDR                this_pc;
1382
  CORE_ADDR                this_sp;
1383
  CORE_ADDR                this_sp_for_id;
1384
  int                      frame_size = 0;
1385
 
1386
  CORE_ADDR                start_addr;
1387
  CORE_ADDR                end_addr;
1388
 
1389 522 jeremybenn
  if (frame_debug)
1390
    {
1391
      fprintf_unfiltered (gdb_stdlog,
1392
                          "or32_frame_cache, prologue_cache = 0x%p\n",
1393
                          *prologue_cache);
1394
    }
1395
 
1396 330 jeremybenn
  /* Nothing to do if we already have this info */
1397
  if (NULL != *prologue_cache)
1398
    {
1399
      return *prologue_cache;
1400
    }
1401
 
1402
  /* Get a new prologue cache and populate it with default values */
1403 531 jeremybenn
  info            = trad_frame_cache_zalloc (this_frame);
1404 330 jeremybenn
  *prologue_cache = info;
1405
 
1406
  /* Find the start address of THIS function (which is a NORMAL frame, even if
1407
     the NEXT frame is the sentinel frame) and the end of its prologue.  */
1408
  this_pc = get_frame_pc (this_frame);
1409
  find_pc_partial_function (this_pc, NULL, &start_addr, NULL);
1410
 
1411 531 jeremybenn
  /* Get the stack pointer if we have one (if there's no process executing yet
1412
     we won't have a frame. */
1413
  this_sp = (NULL == this_frame) ? 0 :
1414
                                   get_frame_register_unsigned (this_frame,
1415
                                                                OR32_SP_REGNUM);
1416
 
1417 330 jeremybenn
  /* Return early if GDB couldn't find the function.  */
1418
  if (start_addr == 0)
1419
    {
1420 522 jeremybenn
      if (frame_debug)
1421
        {
1422
          fprintf_unfiltered (gdb_stdlog, "  couldn't find function\n");
1423
        }
1424
 
1425 531 jeremybenn
      /* JPB: 28-Apr-11. This is a temporary patch, to get round GDB crashing
1426
         right at the beginning. Build the frame ID as best we can. */
1427
      trad_frame_set_id (info, frame_id_build (this_sp, this_pc));
1428
 
1429 330 jeremybenn
      return  info;
1430
    }
1431
 
1432
 
1433
  /* The default frame base of THIS frame (for ID purposes only - frame base
1434
     is an overloaded term) is its stack pointer. For now we use the value of
1435
     the SP register in THIS frame. However if the PC is in the prologue of
1436
     THIS frame, before the SP has been set up, then the value will actually
1437
     be that of the PREV frame, and we'll need to adjust it later. */
1438
  trad_frame_set_this_base (info, this_sp);
1439
  this_sp_for_id = this_sp;
1440
 
1441
  /* The default is to find the PC of the PREVIOUS frame in the link register
1442
     of this frame. This may be changed if we find the link register was saved
1443
     on the stack. */
1444
  trad_frame_set_reg_realreg (info, OR32_NPC_REGNUM, OR32_LR_REGNUM);
1445
 
1446
  /* We should only examine code that is in the prologue. This is all code up
1447
     to (but not including) end_addr. We should only populate the cache while
1448
     the address is up to (but not including) the PC or end_addr, whichever is
1449
     first. */
1450
  gdbarch = get_frame_arch (this_frame);
1451
  end_addr = or32_skip_prologue (gdbarch, start_addr);
1452
 
1453
  /* All the following analysis only occurs if we are in the prologue and have
1454
     executed the code. Check we have a sane prologue size, and if zero we
1455
     are frameless and can give up here. */
1456
  if (end_addr < start_addr)
1457
    {
1458
      fatal ("end addr 0x%08x is less than start addr 0x%08x\n",
1459
             (unsigned int) end_addr, (unsigned int) start_addr);
1460
    }
1461
 
1462
  if (end_addr == start_addr)
1463
    {
1464
      frame_size = 0;
1465
    }
1466
  else
1467
    {
1468
      /* have a frame. Look for the various components */
1469
      CORE_ADDR  addr = start_addr;     /* Where we have got to */
1470
      uint32_t   inst = or32_fetch_instruction (gdbarch, addr);
1471
 
1472
      unsigned int  ra, rb, rd;         /* For instruction analysis */
1473
      int           simm;
1474
 
1475
      /* Look for the new stack pointer being set up. */
1476
      if (or32_analyse_l_addi (inst, &rd, &ra, &simm) &&
1477
          (OR32_SP_REGNUM == rd) && (OR32_SP_REGNUM == ra) &&
1478
          (simm < 0) && (0 == (simm % 4)))
1479
        {
1480
          frame_size  = -simm;
1481
          addr       += OR32_INSTLEN;
1482
          inst        = or32_fetch_instruction (gdbarch, addr);
1483
 
1484
          /* If the PC has not actually got to this point, then the frame base
1485
             will be wrong, and we adjust it.
1486
 
1487
             If we are past this point, then we need to populate the stack
1488
             accoringly. */
1489
          if (this_pc <= addr)
1490
            {
1491
              /* Only do if executing */
1492
              if (0 != this_sp)
1493
                {
1494
                  this_sp_for_id = this_sp + frame_size;
1495
                  trad_frame_set_this_base (info, this_sp_for_id);
1496
                }
1497
            }
1498
          else
1499
            {
1500
              /* We are past this point, so the stack pointer of the PREV
1501
                 frame is frame_size greater than the stack pointer of THIS
1502
                 frame. */
1503
              trad_frame_set_reg_value (info, OR32_SP_REGNUM,
1504
                                        this_sp + frame_size);
1505
            }
1506
        }
1507
 
1508
      /* From now on we are only populating the cache, so we stop once we get
1509
         to either the end OR the current PC. */
1510
      end_addr = (this_pc < end_addr) ? this_pc : end_addr;
1511
 
1512
      /* Look for the frame pointer being manipulated. */
1513
      if ((addr < end_addr) &&
1514
          or32_analyse_l_sw (inst, &simm, &ra, &rb) &&
1515
          (OR32_SP_REGNUM == ra) && (OR32_FP_REGNUM == rb) &&
1516
          (simm >= 0) && (0 == (simm % 4)))
1517
        {
1518
          addr += OR32_INSTLEN;
1519
          inst  = or32_fetch_instruction (gdbarch, addr);
1520
 
1521
          /* At this stage, we can find the frame pointer of the PREVIOUS
1522
             frame on the stack of the current frame. */
1523
          trad_frame_set_reg_addr (info, OR32_FP_REGNUM, this_sp + simm);
1524
 
1525
          /* Look for the new frame pointer being set up */
1526
          if (addr < end_addr)
1527
            {
1528
              gdb_assert (or32_analyse_l_addi (inst, &rd, &ra, &simm) &&
1529
                          (OR32_FP_REGNUM == rd) && (OR32_SP_REGNUM == ra) &&
1530
                          (simm == frame_size));
1531
 
1532
              addr += OR32_INSTLEN;
1533
              inst  = or32_fetch_instruction (gdbarch, addr);
1534
 
1535
              /* If we have got this far, the stack pointer of the PREVIOUS
1536
                 frame is the frame pointer of THIS frame. */
1537
              trad_frame_set_reg_realreg (info, OR32_SP_REGNUM, OR32_FP_REGNUM);
1538
            }
1539
        }
1540
 
1541
      /* Look for the link register being saved */
1542
      if ((addr < end_addr) &&
1543
          or32_analyse_l_sw (inst, &simm, &ra, &rb) &&
1544
          (OR32_SP_REGNUM == ra) && (OR32_LR_REGNUM == rb) &&
1545
          (simm >= 0) && (0 == (simm % 4)))
1546
        {
1547
          addr += OR32_INSTLEN;
1548
          inst  = or32_fetch_instruction (gdbarch, addr);
1549
 
1550
          /* If the link register is saved in the THIS frame, it holds the
1551
             value of the PC in the PREVIOUS frame. This overwrites the
1552
             previous information about finding the PC in the link
1553
             register. */
1554
          trad_frame_set_reg_addr (info, OR32_NPC_REGNUM, this_sp + simm);
1555
        }
1556
 
1557
      /* Look for arguments or callee-saved register being saved. The register
1558
         must be one of the arguments (r3-r8) or the 10 callee saved registers
1559
         (r10, r12, r14, r16, r18, r20, r22, r24, r26, r28, r30). The base
1560
         register must be the FP (for the args) or the SP (for the
1561
         callee_saved registers). */
1562
      while (addr < end_addr)
1563
        {
1564
          if (or32_analyse_l_sw (inst, &simm, &ra, &rb) &&
1565
              (((OR32_FP_REGNUM == ra) && or32_is_arg_reg (rb)) ||
1566
               ((OR32_SP_REGNUM == ra) && or32_is_callee_saved_reg (rb))) &&
1567
              (0 == (simm % 4)))
1568
            {
1569
              addr += OR32_INSTLEN;
1570
              inst  = or32_fetch_instruction (gdbarch, addr);
1571
 
1572
              /* The register in the PREVIOUS frame can be found at this
1573
                 location in THIS frame */
1574
              trad_frame_set_reg_addr (info, rb, this_sp + simm);
1575
            }
1576
          else
1577
            {
1578
              break;                    /* Not a register save instruction */
1579
            }
1580
        }
1581
    }
1582
 
1583
  /* Build the frame ID */
1584
  trad_frame_set_id (info, frame_id_build (this_sp_for_id, start_addr));
1585
 
1586 522 jeremybenn
  if (frame_debug)
1587
    {
1588
      fprintf_unfiltered (gdb_stdlog, "  this_sp_for_id = 0x%p\n",
1589
                          (void *) this_sp_for_id);
1590
      fprintf_unfiltered (gdb_stdlog, "  start_addr     = 0x%p\n",
1591
                          (void *) start_addr);
1592
    }
1593
 
1594 330 jeremybenn
  return info;
1595
 
1596
}       /* or32_frame_cache() */
1597
 
1598
 
1599
/* -------------------------------------------------------------------------- */
1600
/*!Find the frame ID of this frame
1601
 
1602
   This function has changed since GDB 6.8 to use THIS frame, rather than the
1603
   NEXT frame.
1604
 
1605
   Given a GDB frame, return its frame_id.
1606
 
1607
   @param[in]  this_frame      Our frame, for which the ID is wanted.
1608
   @param[in]  prologue_cache  Any cached prologue for THIS function.
1609
   @param[out] this_id         Frame ID of our own frame.
1610
 
1611
   @return  Frame ID for THIS frame */
1612
/* ------------------------------------------------------------------------- */
1613
static void
1614
or32_frame_this_id (struct frame_info  *this_frame,
1615
                    void              **prologue_cache,
1616
                    struct frame_id    *this_id)
1617
{
1618
  struct trad_frame_cache *info =
1619
    or32_frame_cache (this_frame, prologue_cache);
1620
 
1621
  trad_frame_get_id (info, this_id);
1622
 
1623
}       /* or32_frame_this_id() */
1624
 
1625
 
1626
/*----------------------------------------------------------------------------*/
1627
/*!Get a register from the PREVIOUS frame
1628
 
1629
   This function has changed from GDB 6.8. It now takes a reference to THIS
1630
   frame, not the NEXT frame. It returns it results via a structure, not its
1631
   argument list.
1632
 
1633
   Given a pointer to the THIS frame, return the details of a register in the
1634
   PREVIOUS frame.
1635
 
1636
   @param[in] this_frame      The stack frame under consideration
1637
   @param[in] prologue_cache  Any cached prologue associated with THIS frame,
1638
                              which may therefore tell us about registers in
1639
                              the PREVIOUS frame.
1640
   @param[in] regnum          The register of interest in the PREVIOUS frame
1641
 
1642
   @return  A value structure representing the register.                      */
1643
/* -------------------------------------------------------------------------- */
1644
static struct value *
1645
or32_frame_prev_register (struct frame_info  *this_frame,
1646
                          void              **prologue_cache,
1647
                          int                 regnum)
1648
{
1649
  struct trad_frame_cache *info = or32_frame_cache (this_frame,
1650
                                                    prologue_cache);
1651
 
1652
  return  trad_frame_get_register (info, this_frame, regnum);
1653
 
1654
}       /* or32_frame_prev_register() */
1655
 
1656
 
1657
/* -------------------------------------------------------------------------- */
1658
/*!Structure defining the OR32 frame unwind functions
1659
 
1660
   Must be global (to this file), since referred to by multiple functions.
1661
 
1662
   Since we are the fallback unwinder, we use the default frame sniffer, which
1663
   always accepts the frame
1664
 
1665
   This applies to NORMAL frames only. We provide the following functions.
1666
   - to give the ID of THIS frame
1667
   - to give the details of a register in PREVIOUS frame
1668
   - a frame sniffer.                                                         */
1669
/* -------------------------------------------------------------------------- */
1670
static const struct frame_unwind or32_frame_unwind = {
1671
  .type          = NORMAL_FRAME,
1672
  .this_id       = or32_frame_this_id,
1673
  .prev_register = or32_frame_prev_register,
1674
  .unwind_data   = NULL,
1675
  .sniffer       = default_frame_sniffer,
1676
  .dealloc_cache = NULL,
1677
  .prev_arch     = NULL
1678
};
1679
 
1680
 
1681 522 jeremybenn
#if 0
1682 330 jeremybenn
/*----------------------------------------------------------------------------*/
1683
/*!Return the base address of the frame
1684
 
1685
   The implementations has changed since GDB 6.8, since we are now provided
1686
   with the address of THIS frame, rather than the NEXT frame.
1687
 
1688
   For the OR32, the base address is the frame pointer
1689
 
1690
   @param[in] this_frame      The current stack frame.
1691
   @param[in] prologue_cache  Any cached prologue for THIS function.
1692
 
1693
   @return  The frame base address */
1694
/*---------------------------------------------------------------------------*/
1695
 
1696
static CORE_ADDR
1697
or32_frame_base_address (struct frame_info  *this_frame,
1698
                         void              **prologue_cache)
1699
{
1700
  return  (CORE_ADDR) get_frame_register_unsigned (this_frame, OR32_FP_REGNUM);
1701
 
1702
}       /* or32_frame_base_address() */
1703
 
1704
 
1705
/* -------------------------------------------------------------------------- */
1706
/*!Identify our frame base sniffer functions
1707
 
1708
   This function just identifies our family of frame sniffing functions.
1709
 
1710
   @param[in] this_frame  The frame of THIS function. Not used here.
1711
 
1712
   @return  A pointer to a struct identifying the frame base sniffing
1713
            functions.                                                        */
1714
/* -------------------------------------------------------------------------- */
1715
static const struct frame_base *
1716
or32_frame_base_sniffer (struct frame_info *this_frame)
1717
{
1718
  /* Structure defining how the frame base is to be identified. */
1719
  static const struct frame_base  or32_frame_base =
1720
    {
1721
      .unwind      = &or32_frame_unwind,
1722
      .this_base   = or32_frame_base_address,
1723
      .this_locals = or32_frame_base_address,
1724
      .this_args   = or32_frame_base_address
1725
    };
1726
 
1727
  return &or32_frame_base;
1728
 
1729
}       /* or32_frame_base_sniffer () */
1730 522 jeremybenn
#endif
1731 330 jeremybenn
 
1732
 
1733
/* -------------------------------------------------------------------------- */
1734 565 jeremybenn
/*!Return information needed to handle a core file.
1735
 
1736
   We put together a regset structure that tells the system how to transfer
1737
   registers to and from a core file image.
1738
 
1739
   @param[in] gdbarch    The GDB architecture we are using.
1740
   @param[in] sect_name  The name of the section being considered.
1741
   @param[in] sect_size  The size of the section being considered.
1742
 
1743
   @return  A regset structure for the section, or NULL if none is available. */
1744
/* -------------------------------------------------------------------------- */
1745
static const struct regset *
1746
or32_regset_from_core_section (struct gdbarch *gdbarch,
1747
                               const char     *sect_name,
1748
                               size_t          sect_size)
1749
{
1750
  printf ("sect_name \"%s\", sect_size %d\n", sect_name, sect_size);
1751
  return NULL;
1752
 
1753
}       /* or32_regset_from_core_section () */
1754
 
1755
 
1756
/* -------------------------------------------------------------------------- */
1757 330 jeremybenn
/*!Architecture initialization for OpenRISC 1000
1758
 
1759
   Looks for a candidate architecture in the list of architectures supplied
1760
   using the info supplied. If none match, create a new architecture.
1761
 
1762
   @param[in] info    Information about the target architecture
1763
   @param[in] arches  The list of currently know architectures
1764
 
1765
   @return  A structure describing the target architecture                    */
1766
/* -------------------------------------------------------------------------- */
1767
static struct gdbarch *
1768
or32_gdbarch_init (struct gdbarch_info  info,
1769
                   struct gdbarch_list *arches)
1770
{
1771
  static struct frame_base     or32_frame_base;
1772
  struct        gdbarch       *gdbarch;
1773
  struct        gdbarch_tdep  *tdep;
1774
  const struct  bfd_arch_info *binfo;
1775
 
1776
  /* Find a candidate among the list of pre-declared architectures.  */
1777
  arches = gdbarch_list_lookup_by_info (arches, &info);
1778
  if (NULL != arches)
1779
    {
1780
      return arches->gdbarch;
1781
    }
1782
 
1783
  /* None found, create a new architecture from the information
1784
     provided. Can't initialize all the target dependencies until we actually
1785
     know which target we are talking to, but put in some defaults for now. */
1786
 
1787
  binfo                   = info.bfd_arch_info;
1788
  tdep                    = xmalloc (sizeof *tdep);
1789
  tdep->num_matchpoints   = OR32_MAX_MATCHPOINTS;
1790
  tdep->num_gpr_regs      = OR32_MAX_GPR_REGS;
1791
  tdep->bytes_per_word    = binfo->bits_per_word    / binfo->bits_per_byte;
1792
  tdep->bytes_per_address = binfo->bits_per_address / binfo->bits_per_byte;
1793
  gdbarch                 = gdbarch_alloc (&info, tdep);
1794
 
1795
  /* Target data types.  */
1796
  set_gdbarch_short_bit             (gdbarch, 16);
1797
  set_gdbarch_int_bit               (gdbarch, 32);
1798
  set_gdbarch_long_bit              (gdbarch, 32);
1799
  set_gdbarch_long_long_bit         (gdbarch, 64);
1800
  set_gdbarch_float_bit             (gdbarch, 32);
1801
  set_gdbarch_float_format          (gdbarch, floatformats_ieee_single);
1802
  set_gdbarch_double_bit            (gdbarch, 64);
1803
  set_gdbarch_double_format         (gdbarch, floatformats_ieee_double);
1804
  set_gdbarch_long_double_bit       (gdbarch, 64);
1805
  set_gdbarch_long_double_format    (gdbarch, floatformats_ieee_double);
1806
  set_gdbarch_ptr_bit               (gdbarch, binfo->bits_per_address);
1807
  set_gdbarch_addr_bit              (gdbarch, binfo->bits_per_address);
1808
  set_gdbarch_char_signed           (gdbarch, 1);
1809
 
1810
  /* Information about the target architecture */
1811
  set_gdbarch_return_value          (gdbarch, or32_return_value);
1812
  set_gdbarch_breakpoint_from_pc    (gdbarch, or32_breakpoint_from_pc);
1813
  set_gdbarch_single_step_through_delay
1814
                                    (gdbarch, or32_single_step_through_delay);
1815
  set_gdbarch_have_nonsteppable_watchpoint
1816
                                    (gdbarch, 1);
1817
  switch (gdbarch_byte_order (gdbarch))
1818
    {
1819
    case BFD_ENDIAN_BIG:
1820
      set_gdbarch_print_insn        (gdbarch, print_insn_big_or32);
1821
      break;
1822
 
1823
    case BFD_ENDIAN_LITTLE:
1824
      set_gdbarch_print_insn        (gdbarch, print_insn_little_or32);
1825
      break;
1826
 
1827
    case BFD_ENDIAN_UNKNOWN:
1828
      error ("or32_gdbarch_init: Unknown endianness");
1829
      break;
1830
    }
1831
 
1832
  /* Register architecture */
1833
  set_gdbarch_pseudo_register_read  (gdbarch, or32_pseudo_register_read);
1834
  set_gdbarch_pseudo_register_write (gdbarch, or32_pseudo_register_write);
1835
  set_gdbarch_num_regs              (gdbarch, OR32_NUM_REGS);
1836
  set_gdbarch_num_pseudo_regs       (gdbarch, OR32_NUM_PSEUDO_REGS);
1837
  set_gdbarch_sp_regnum             (gdbarch, OR32_SP_REGNUM);
1838
  set_gdbarch_pc_regnum             (gdbarch, OR32_NPC_REGNUM);
1839
  set_gdbarch_ps_regnum             (gdbarch, OR32_SR_REGNUM);
1840
  set_gdbarch_deprecated_fp_regnum  (gdbarch, OR32_FP_REGNUM);
1841
 
1842
  /* Functions to supply register information */
1843
  set_gdbarch_register_name         (gdbarch, or32_register_name);
1844
  set_gdbarch_register_type         (gdbarch, or32_register_type);
1845
  set_gdbarch_print_registers_info  (gdbarch, or32_registers_info);
1846
  set_gdbarch_register_reggroup_p   (gdbarch, or32_register_reggroup_p);
1847
 
1848
  /* Functions to analyse frames */
1849
  set_gdbarch_skip_prologue         (gdbarch, or32_skip_prologue);
1850
  set_gdbarch_inner_than            (gdbarch, core_addr_lessthan);
1851
  set_gdbarch_frame_align           (gdbarch, or32_frame_align);
1852
  set_gdbarch_frame_red_zone_size   (gdbarch, OR32_FRAME_RED_ZONE_SIZE);
1853
 
1854
  /* Functions to access frame data */
1855
  set_gdbarch_unwind_pc             (gdbarch, or32_unwind_pc);
1856
  set_gdbarch_unwind_sp             (gdbarch, or32_unwind_sp);
1857
 
1858
  /* Functions handling dummy frames */
1859
  set_gdbarch_push_dummy_call       (gdbarch, or32_push_dummy_call);
1860
  set_gdbarch_dummy_id              (gdbarch, or32_dummy_id);
1861
 
1862 522 jeremybenn
#if 0
1863 330 jeremybenn
  /* Set up sniffers for the frame base. Use DWARF debug info if available,
1864
     otherwise use our own sniffer. */
1865
  frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
1866
  frame_base_append_sniffer (gdbarch, or32_frame_base_sniffer);
1867 522 jeremybenn
#endif
1868 330 jeremybenn
 
1869 565 jeremybenn
  /* Handle core files */
1870
  set_gdbarch_regset_from_core_section (gdbarch, or32_regset_from_core_section);
1871
 
1872 330 jeremybenn
  /* Frame unwinders. Use DWARF debug info if available, otherwise use our
1873
     own unwinder. */
1874
  dwarf2_append_unwinders (gdbarch);
1875
  frame_unwind_append_unwinder (gdbarch, &or32_frame_unwind);
1876
 
1877
  return gdbarch;
1878
 
1879
}       /* or32_gdbarch_init() */
1880
 
1881
 
1882
/*----------------------------------------------------------------------------*/
1883
/*!Dump the target specific data for this architecture
1884
 
1885
   @param[in] gdbarch  The architecture of interest
1886
   @param[in] file     Where to dump the data */
1887
/*---------------------------------------------------------------------------*/
1888
 
1889
static void
1890
or32_dump_tdep (struct gdbarch *gdbarch,
1891
                struct ui_file *file)
1892
{
1893
  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1894
 
1895
  if (NULL == tdep)
1896
    {
1897
      return;                   /* Nothing to report */
1898
    }
1899
 
1900
  fprintf_unfiltered (file, "or32_dump_tdep: %d matchpoints available\n",
1901
                      tdep->num_matchpoints);
1902
  fprintf_unfiltered (file, "or32_dump_tdep: %d general purpose registers\n",
1903
                      tdep->num_gpr_regs);
1904
  fprintf_unfiltered (file, "or32_dump_tdep: %d bytes per word\n",
1905
                      tdep->bytes_per_word);
1906
  fprintf_unfiltered (file, "or32_dump_tdep: %d bytes per address\n",
1907
                      tdep->bytes_per_address);
1908
 
1909
}       /* or32_dump_tdep() */
1910
 
1911
 
1912
 
1913
/* Functions to add extra commands to GDB */
1914
 
1915
 
1916
/*----------------------------------------------------------------------------*/
1917
/*!Returns a special purpose register group name
1918
 
1919
   @param[in]  group  The SPR group number
1920
 
1921
   @return  The SPR name (pointer to the name argument) */
1922
/*---------------------------------------------------------------------------*/
1923
 
1924
static const char *
1925
or32_spr_group_name (int  group)
1926
{
1927
  static const char *or32_group_names[OR32_NUM_SPGS] =
1928
    {
1929
      "SYS",
1930
      "DMMU",
1931
      "IMMU",
1932
      "DCACHE",
1933
      "ICACHE",
1934
      "MAC",
1935
      "DEBUG",
1936
      "PERF",
1937
      "POWER",
1938
      "PIC",
1939
      "TIMER",
1940
      "FPU"
1941
    };
1942
 
1943
  if ((0 <= group) && (group < OR32_NUM_SPGS))
1944
    {
1945
      return or32_group_names[group];
1946
    }
1947
  else
1948
    {
1949
      return "";
1950
    }
1951
}       /* or32_spr_group_name() */
1952
 
1953
 
1954
/*----------------------------------------------------------------------------*/
1955
/*!Returns a special purpose register name
1956
 
1957
   @param[in]  group  The SPR group
1958
   @param[in]  index  The index within the SPR group
1959
   @param[out] name   Array to put the name in
1960
 
1961
   @return  The SPR name (pointer to the name argument) */
1962
/*---------------------------------------------------------------------------*/
1963
 
1964
static char *
1965
or32_spr_register_name (int   group,
1966
                        int   index,
1967
                        char *name)
1968
{
1969
  char di;
1970
 
1971
  switch (group)
1972
    {
1973
 
1974
    case OR32_SPG_SYS:
1975
      /* 1:1 names */
1976
      switch (index)
1977
        {
1978
        case OR32_SPG_SYS_VR:       sprintf (name, "VR"      ); return  name;
1979
        case OR32_SPG_SYS_UPR:      sprintf (name, "UPR"     ); return  name;
1980
        case OR32_SPG_SYS_CPUCFGR:  sprintf (name, "CPUCFGR" ); return  name;
1981
        case OR32_SPG_SYS_DMMUCFGR: sprintf (name, "DMMUCFGR"); return  name;
1982
        case OR32_SPG_SYS_IMMUCFGR: sprintf (name, "IMMUCFGR"); return  name;
1983
        case OR32_SPG_SYS_DCCFGR:   sprintf (name, "DCCFGR"  ); return  name;
1984
        case OR32_SPG_SYS_ICCFGR:   sprintf (name, "ICCFGR"  ); return  name;
1985
        case OR32_SPG_SYS_DCFGR:    sprintf (name, "DCFGR"   ); return  name;
1986
        case OR32_SPG_SYS_PCCFGR:   sprintf (name, "PCCFGR"  ); return  name;
1987
        case OR32_SPG_SYS_NPC:      sprintf (name, "NPC"     ); return  name;
1988
        case OR32_SPG_SYS_SR:       sprintf (name, "SR"      ); return  name;
1989
        case OR32_SPG_SYS_PPC:      sprintf (name, "PPC"     ); return  name;
1990
        case OR32_SPG_SYS_FPCSR:    sprintf (name, "FPCSR"   ); return  name;
1991
        }
1992
 
1993
      /* Exception PC regs */
1994
      if((OR32_SPG_SYS_EPCR <= index) &&
1995
         (index             <= OR32_SPG_SYS_EPCR_END))
1996
        {
1997
          sprintf (name, "EPCR%d", index - OR32_SPG_SYS_EPCR);
1998
          return  name;
1999
        }
2000
 
2001
      /* Exception EA regs */
2002
      if((OR32_SPG_SYS_EEAR <= index) &&
2003
         (index             <= OR32_SPG_SYS_EEAR_END))
2004
        {
2005
          sprintf (name, "EEAR%d", index - OR32_SPG_SYS_EEAR);
2006
          return  name;
2007
        }
2008
 
2009
      /* Exception SR regs */
2010
      if((OR32_SPG_SYS_ESR <= index) &&
2011
         (index            <= OR32_SPG_SYS_ESR_END))
2012
        {
2013
          sprintf (name, "ESR%d", index - OR32_SPG_SYS_ESR);
2014
          return  name;
2015
        }
2016
 
2017
      /* GPRs */
2018
      if((OR32_SPG_SYS_GPR <= index) &&
2019
         (index            <= OR32_SPG_SYS_GPR_END))
2020
        {
2021
          sprintf (name, "GPR%d", index - OR32_SPG_SYS_GPR);
2022
          return  name;
2023
        }
2024
 
2025
      break;
2026
 
2027
    case OR32_SPG_DMMU:
2028
    case OR32_SPG_IMMU:
2029
      /* MMU registers. Use DMMU constants throughout, but these are identical
2030
         to the corresponding IMMU constants */
2031
      di = OR32_SPG_DMMU == group ? 'D' : 'I';
2032
 
2033
      /* 1:1 names */
2034
      switch (index)
2035
        {
2036
        case OR32_SPG_DMMU_DMMUCR:
2037
          sprintf (name, "%cMMUCR",  di); return  name;
2038
        case OR32_SPG_DMMU_DMMUPR:
2039
          sprintf (name, "%cMMUPR",  di); return  name;
2040
        case OR32_SPG_DMMU_DTLBEIR:
2041
          sprintf (name, "%cTLBEIR", di); return  name;
2042
        }
2043
 
2044
      /* ATB Match registers */
2045
      if((OR32_SPG_DMMU_DATBMR <= index) &&
2046
         (index                <= OR32_SPG_DMMU_DATBMR_END))
2047
        {
2048
          sprintf (name, "%cATBMR%d", di, index - OR32_SPG_DMMU_DATBMR);
2049
          return  name;
2050
        }
2051
 
2052
      /* ATB Translate registers */
2053
      if((OR32_SPG_DMMU_DATBTR <= index) &&
2054
         (index                <= OR32_SPG_DMMU_DATBTR_END))
2055
        {
2056
          sprintf (name, "%cATBTR%d", di, index - OR32_SPG_DMMU_DATBTR);
2057
          return  name;
2058
        }
2059
 
2060
      /* TLB Way 1 Match registers */
2061
      if((OR32_SPG_DMMU_DTLBW1MR <= index) &&
2062
         (index                <= OR32_SPG_DMMU_DTLBW1MR_END))
2063
        {
2064
          sprintf (name, "%cTLBW1MR%d", di, index - OR32_SPG_DMMU_DTLBW1MR);
2065
          return  name;
2066
        }
2067
 
2068
      /* TLB Way 1 Translate registers */
2069
      if((OR32_SPG_DMMU_DTLBW1TR <= index) &&
2070
         (index                <= OR32_SPG_DMMU_DTLBW1TR_END))
2071
        {
2072
          sprintf (name, "%cTLBW1TR%d", di, index - OR32_SPG_DMMU_DTLBW1TR);
2073
          return  name;
2074
        }
2075
 
2076
      /* TLB Way 2 Match registers */
2077
      if((OR32_SPG_DMMU_DTLBW2MR <= index) &&
2078
         (index                <= OR32_SPG_DMMU_DTLBW2MR_END))
2079
        {
2080
          sprintf (name, "%cTLBW2MR%d", di, index - OR32_SPG_DMMU_DTLBW2MR);
2081
          return  name;
2082
        }
2083
 
2084
      /* TLB Way 2 Translate registers */
2085
      if((OR32_SPG_DMMU_DTLBW2TR <= index) &&
2086
         (index                <= OR32_SPG_DMMU_DTLBW2TR_END))
2087
        {
2088
          sprintf (name, "%cTLBW2TR%d", di, index - OR32_SPG_DMMU_DTLBW2TR);
2089
          return  name;
2090
        }
2091
 
2092
      /* TLB Way 3 Match registers */
2093
      if((OR32_SPG_DMMU_DTLBW3MR <= index) &&
2094
         (index                <= OR32_SPG_DMMU_DTLBW3MR_END))
2095
        {
2096
          sprintf (name, "%cTLBW3MR%d", di, index - OR32_SPG_DMMU_DTLBW3MR);
2097
          return  name;
2098
        }
2099
 
2100
      /* TLB Way 3 Translate registers */
2101
      if((OR32_SPG_DMMU_DTLBW3TR <= index) &&
2102
         (index                <= OR32_SPG_DMMU_DTLBW3TR_END))
2103
        {
2104
          sprintf (name, "%cTLBW3TR%d", di, index - OR32_SPG_DMMU_DTLBW3TR);
2105
          return  name;
2106
        }
2107
 
2108
      break;
2109
 
2110
    case OR32_SPG_DC:
2111
      /* Data cache registers. These do not have an exact correspondence with
2112
         their instruction cache counterparts, so must be done separately. */
2113
 
2114
      /* 1:1 names */
2115
      switch (index)
2116
        {
2117
        case OR32_SPG_DC_DCCR:  sprintf (name, "DCCR" ); return  name;
2118
        case OR32_SPG_DC_DCBPR: sprintf (name, "DCBPR"); return  name;
2119
        case OR32_SPG_DC_DCBFR: sprintf (name, "DCBFR"); return  name;
2120
        case OR32_SPG_DC_DCBIR: sprintf (name, "DCBIR"); return  name;
2121
        case OR32_SPG_DC_DCBWR: sprintf (name, "DCBWR"); return  name;
2122
        case OR32_SPG_DC_DCBLR: sprintf (name, "DCBLR"); return  name;
2123
        }
2124
 
2125
      break;
2126
 
2127
    case OR32_SPG_IC:
2128
      /* Instruction cache registers */
2129
 
2130
      /* 1:1 names */
2131
      switch (index)
2132
        {
2133
        case OR32_SPG_IC_ICCR:  sprintf (name, "ICCR" ); return  name;
2134
        case OR32_SPG_IC_ICBPR: sprintf (name, "ICBPR"); return  name;
2135
        case OR32_SPG_IC_ICBIR: sprintf (name, "ICBIR"); return  name;
2136
        case OR32_SPG_IC_ICBLR: sprintf (name, "ICBLR"); return  name;
2137
        }
2138
 
2139
      break;
2140
 
2141
    case OR32_SPG_MAC:
2142
      /* MAC registers */
2143
 
2144
      /* 1:1 names */
2145
      switch (index)
2146
        {
2147
        case OR32_SPG_MAC_MACLO: sprintf (name, "MACLO"); return  name;
2148
        case OR32_SPG_MAC_MACHI: sprintf (name, "MACHI"); return  name;
2149
        }
2150
 
2151
      break;
2152
 
2153
    case OR32_SPG_DEBUG:
2154
      /* Debug registers */
2155
 
2156
      /* Debug Value registers */
2157
      if((OR32_SPG_DEBUG_DVR <= index) &&
2158
         (index                <= OR32_SPG_DEBUG_DVR_END))
2159
        {
2160
          sprintf (name, "DVR%d", index - OR32_SPG_DEBUG_DVR);
2161
          return  name;
2162
        }
2163
 
2164
      /* Debug Control registers */
2165
      if((OR32_SPG_DEBUG_DCR <= index) &&
2166
         (index                <= OR32_SPG_DEBUG_DCR_END))
2167
        {
2168
          sprintf (name, "DCR%d", index - OR32_SPG_DEBUG_DCR);
2169
          return  name;
2170
        }
2171
 
2172
      /* 1:1 names */
2173
      switch (index)
2174
        {
2175
        case OR32_SPG_DEBUG_DMR1:  sprintf (name, "DMR1" ); return  name;
2176
        case OR32_SPG_DEBUG_DMR2:  sprintf (name, "DMR2" ); return  name;
2177
        case OR32_SPG_DEBUG_DCWR0: sprintf (name, "DCWR0"); return  name;
2178
        case OR32_SPG_DEBUG_DCWR1: sprintf (name, "DCWR1"); return  name;
2179
        case OR32_SPG_DEBUG_DSR:   sprintf (name, "DSR"  ); return  name;
2180
        case OR32_SPG_DEBUG_DRR:   sprintf (name, "DRR"  ); return  name;
2181
        }
2182
 
2183
      break;
2184
 
2185
    case OR32_SPG_PC:
2186
      /* Performance Counter registers */
2187
 
2188
      /* Performance Counters Count registers */
2189
      if((OR32_SPG_PC_PCCR <= index) &&
2190
         (index                <= OR32_SPG_PC_PCCR_END))
2191
        {
2192
          sprintf (name, "PCCR%d", index - OR32_SPG_PC_PCCR);
2193
          return  name;
2194
        }
2195
 
2196
      /* Performance Counters Mode registers */
2197
      if((OR32_SPG_PC_PCMR <= index) &&
2198
         (index                <= OR32_SPG_PC_PCMR_END))
2199
        {
2200
          sprintf (name, "PCMR%d", index - OR32_SPG_PC_PCMR);
2201
          return  name;
2202
        }
2203
 
2204
      break;
2205
 
2206
    case OR32_SPG_PM:
2207
      /* Power Management registers */
2208
 
2209
      /* 1:1 names */
2210
      switch (index)
2211
        {
2212
        case OR32_SPG_PM_PMR:  sprintf (name, "PMR"); return  name;
2213
        }
2214
 
2215
      break;
2216
 
2217
    case OR32_SPG_PIC:
2218
      /* Programmable Interrupt Controller registers */
2219
 
2220
      /* 1:1 names */
2221
      switch (index)
2222
        {
2223
        case OR32_SPG_PIC_PICMR:  sprintf (name, "PICMR"); return  name;
2224
        case OR32_SPG_PIC_PICSR:  sprintf (name, "PICSR"); return  name;
2225
        }
2226
 
2227
      break;
2228
 
2229
    case OR32_SPG_TT:
2230
      /* Tick Timer registers */
2231
 
2232
      /* 1:1 names */
2233
      switch (index)
2234
        {
2235
        case OR32_SPG_TT_TTMR:  sprintf (name, "TTMR"); return  name;
2236
        case OR32_SPG_TT_TTCR:  sprintf (name, "TTCR"); return  name;
2237
        }
2238
 
2239
      break;
2240
 
2241
    case OR32_SPG_FPU:
2242
 
2243
      break;
2244
    }
2245
 
2246
  /* Not a recognized register */
2247
  strcpy (name, "");
2248
  return  name;
2249
 
2250
}       /* or32_spr_register_name() */
2251
 
2252
 
2253
/*----------------------------------------------------------------------------*/
2254
/*!Get SPR group number from a name
2255
 
2256
   @param[in] group_name  SPR register group
2257
 
2258
   @return  The index, or negative if no match. */
2259
/*----------------------------------------------------------------------------*/
2260
 
2261
static int
2262
or32_groupnum_from_name (char *group_name)
2263
{
2264
  int  group;
2265
 
2266
  for (group = 0; group < OR32_NUM_SPGS; group++)
2267
    {
2268
      if (0 == strcasecmp (group_name, or32_spr_group_name (group)))
2269
        {
2270
          return group;
2271
        }
2272
    }
2273
 
2274
  return -1;
2275
 
2276
}       /* or32_groupnum_from_name() */
2277
 
2278
 
2279
/*----------------------------------------------------------------------------*/
2280
/*!Get register index in special purpose register group from name
2281
 
2282
   The name may either be SPR<group_num>_<index> or a known unique name. In
2283
   either case the group number must match the supplied group number.
2284
 
2285
   @param[in] group  SPR register group
2286
   @param[in] name   Register name
2287
 
2288
   @return  The index, or negative if no match. */
2289
/*----------------------------------------------------------------------------*/
2290
 
2291
static int
2292
or32_regnum_from_name (int   group,
2293
                       char *name)
2294
{
2295
  /* Last valid register in each group. */
2296
  static const int  or32_spr_group_last[OR32_NUM_SPGS] =
2297
    {
2298
      OR32_SPG_SYS_LAST,
2299
      OR32_SPG_DMMU_LAST,
2300
      OR32_SPG_IMMU_LAST,
2301
      OR32_SPG_DC_LAST,
2302
      OR32_SPG_IC_LAST,
2303
      OR32_SPG_MAC_LAST,
2304
      OR32_SPG_DEBUG_LAST,
2305
      OR32_SPG_PC_LAST,
2306
      OR32_SPG_PM_LAST,
2307
      OR32_SPG_PIC_LAST,
2308
      OR32_SPG_TT_LAST,
2309
      OR32_SPG_FPU_LAST
2310
    };
2311
 
2312
  int  i;
2313
  char  spr_name[32];
2314
 
2315
  if (0 == strcasecmp (name, "SPR"))
2316
    {
2317
      char *ptr_c;
2318
 
2319
      /* Skip SPR */
2320
      name += 3;
2321
 
2322
      /* Get group number */
2323
      i = (int) strtoul (name, &ptr_c, 10);
2324
      if (*ptr_c != '_' || i != group)
2325
        {
2326
          return -1;
2327
        }
2328
 
2329
      /* Get index */
2330
      ptr_c++;
2331
      i = (int) strtoul (name, &ptr_c, 10);
2332
      if (*ptr_c)
2333
        {
2334
          return -1;
2335
        }
2336
      else
2337
        {
2338
          return  i;
2339
        }
2340
    }
2341
 
2342
  /* Look for a "known" name in this group */
2343
  for (i = 0; i <= or32_spr_group_last[group]; i++)
2344
    {
2345
      char *s = or32_spr_register_name (group, i, spr_name);
2346
 
2347
      if (0 == strcasecmp (name, s))
2348
        {
2349
          return i;
2350
        }
2351
    }
2352
 
2353
  /* Failure */
2354
  return -1;
2355
 
2356
}       /* or32_regnum_from_name() */
2357
 
2358
 
2359
/*----------------------------------------------------------------------------*/
2360
/*!Get the next token from a string
2361
 
2362
   I can't believe there isn't a library argument for this, but strtok is
2363
   deprecated.
2364
 
2365
   Take a string and find the start of the next token and its length. A token
2366
   is anything containing non-blank characters.
2367
 
2368
   @param[in]  str  The string to look at (may be NULL).
2369
   @param[out] tok  Pointer to the start of the token within str. May be NULL
2370
                    if this result is not wanted (e.g. just the length is
2371
                    wanted. If no token is found will be the NULL char at the
2372
                    end of the string, if the original str was NULL, this will
2373
                    be NULL.
2374
 
2375
                    @return  The length of the token found */
2376
/*----------------------------------------------------------------------------*/
2377
 
2378
static int
2379
or32_tokenize (char  *str,
2380
               char **tok)
2381
{
2382
  char *ptr;
2383
  int   len;
2384
 
2385
  /* Deal with NULL argument */
2386
  if (NULL == str)
2387
    {
2388
      if (NULL != tok)
2389
        {
2390
          *tok = NULL;
2391
        }
2392
      return 0;
2393
    }
2394
 
2395
  /* Find the start */
2396
  for (ptr = str; ISBLANK (*ptr) ; ptr++)
2397
    {
2398
      continue;
2399
    }
2400
 
2401
  /* Return the start pointer if requested */
2402
  if (NULL != tok)
2403
    {
2404
      *tok = ptr;
2405
    }
2406
 
2407
  /* Find the end and put in EOS */
2408
  for (len = 0;  ('\0' != ptr[len]) && (!ISBLANK (ptr[len])); len++)
2409
    {
2410
      continue;
2411
    }
2412
 
2413
  return len;
2414
 
2415
}       /* or32_tokenize() */
2416
 
2417
 
2418
/*----------------------------------------------------------------------------*/
2419
/*!Parses args for spr commands
2420
 
2421
   Determines the special purpose register (SPR) name and puts result into
2422
   group and index
2423
 
2424
   Syntax is:
2425
 
2426
   @verbatim
2427
   <spr_args>    -> <group_ref> | <reg_name>
2428
   <group_ref>   -> <group_id> <index>
2429
   <group_id>    -> <group_num> | <group_name>
2430
   @endverbatim
2431
 
2432
   Where the indices/names have to be valid.
2433
 
2434
   So to parse, we look for 1 or 2 args. If 1 it must be a unique register
2435
   name. If 2, the first must be a group number or name and the second an
2436
   index within that group.
2437
 
2438
   Also responsible for providing diagnostics if the arguments do not match.
2439
 
2440
   Rewritten for GDB 6.8 to use the new UI calls and remove assorted
2441
   bugs. Syntax also slightly restricted to be more comprehensible.
2442
 
2443
   @param[in]  arg_str  The argument string
2444
   @param[out] group    The group this SPR belongs in, or -1 to indicate
2445
                        failure
2446
   @param[out] index    Index of the register within the group, or -1 to
2447
                        indicate the whole group
2448
   @param[in]  is_set   1 (true) if we are called from the "spr" command (so
2449
                        there is an extra arg) rather than the "info spr"
2450
                        command. Needed to distinguish between the case where
2451
                        info is sought from a register specified as group and
2452
                        index and setting a uniquely identified register to a
2453
                        value.
2454
 
2455
                        @return  A pointer to any remaining args */
2456
/*---------------------------------------------------------------------------*/
2457
 
2458
static char *
2459
or32_parse_spr_params (char *arg_str,
2460
                       int  *group,
2461
                       int  *index,
2462
                       int   is_set)
2463
{
2464
  struct {
2465
    char              *str;
2466
    int                len;
2467
    unsigned long int  val;
2468
    int                is_num;
2469
  } arg[3] = {
2470
    {
2471
      .str    = NULL,
2472
      .len    = 0,
2473
      .val    = 0,
2474
      .is_num = 0,
2475
    },
2476
   {
2477
      .str    = NULL,
2478
      .len    = 0,
2479
      .val    = 0,
2480
      .is_num = 0,
2481
    },
2482
   {
2483
      .str    = NULL,
2484
      .len    = 0,
2485
      .val    = 0,
2486
      .is_num = 0,
2487
    }
2488
  };
2489
 
2490
  int   num_args;
2491
  char *trailer  = arg_str;
2492
  char *tmp_str;
2493
  int   i;
2494
 
2495
  char  spr_name[32];
2496
 
2497
  /* Break out the arguments. Note that the strings are NOT null terminated
2498
     (we don't want to change arg_str), so we must rely on len. The stroul
2499
     call will still work, since there is always a non-digit char (possibly EOS)
2500
     after the last digit. */
2501
  if (NULL == arg_str)
2502
    {
2503
      num_args = 0;
2504
    }
2505
  else
2506
    {
2507
      for (num_args = 0; num_args < 3; num_args++)
2508
        {
2509
          arg[num_args].len = or32_tokenize (trailer, &(arg[num_args].str));
2510
          trailer           = arg[num_args].str + arg[num_args].len;
2511
 
2512
          if (0 == arg[num_args].len)
2513
            {
2514
              break;
2515
            }
2516
        }
2517
    }
2518
 
2519
  /* Patch nulls into the arg strings and see about values. Couldn't do this
2520
     earlier, since we needed the next char clean to check later args. This
2521
     means advancing trailer, UNLESS it was already at EOS */
2522
 
2523
  if((NULL != arg_str) && ('\0' != *trailer))
2524
    {
2525
      trailer++;
2526
    }
2527
 
2528
  for (i = 0; i < num_args; i++)
2529
    {
2530
      (arg[i].str)[arg[i].len] = '\0';
2531
      errno                    = 0;
2532
      arg[i].val               = strtoul (arg[i].str, &tmp_str, 0);
2533
      arg[i].is_num            = (0 == errno) && ('\0' == *tmp_str);
2534
    }
2535
 
2536
  /* Deal with the case where we are setting a register, so the final argument
2537
     should be disregarded (it is the trailer). Do this anyway if we get a
2538
     third argument */
2539
  if ((is_set & (num_args > 0)) || (num_args > 2))
2540
    {
2541
      trailer = arg[num_args - 1].str;
2542
      num_args--;
2543
    }
2544
 
2545
  /* Deal with different numbers of args */
2546
 
2547
  switch (num_args)
2548
    {
2549
 
2550
    case 0:
2551
      ui_out_message (uiout, 0,
2552
                      "Usage: <command> <register>      |\n"
2553
                      "       <command> <group>         |\n"
2554
                      "       <command> <group> <index>\n"
2555
                      "Valid groups are:\n");
2556
      for (i = 0; i < OR32_NUM_SPGS; i++)
2557
        {
2558
          ui_out_field_string (uiout, NULL, or32_spr_group_name  (i));
2559
          ui_out_spaces (uiout, 1);
2560
          ui_out_wrap_hint (uiout, NULL);
2561
        }
2562
      ui_out_field_string (uiout, NULL, "\n");
2563
 
2564
      *index = -1;
2565
      return  trailer;
2566
 
2567
    case 1:
2568
      /* See if it is a numeric group */
2569
      if (arg[0].is_num)
2570
        {
2571
          if (arg[0].val < OR32_NUM_SPGS)
2572
            {
2573
              *group = arg[0].val;
2574
              *index = -1;
2575
              return trailer;
2576
            }
2577
          else
2578
            {
2579
              ui_out_message (uiout, 0,
2580
                              "Group index should be in the range 0 - %d\n",
2581
                              OR32_NUM_SPGS);
2582
              *group = -1;
2583
              *index = -1;
2584
              return trailer;
2585
            }
2586
        }
2587
 
2588
      /* Is is it a group name? */
2589
      *group = or32_groupnum_from_name (arg[0].str);
2590
      if (*group >= 0)
2591
        {
2592
          *index = -1;
2593
          return trailer;
2594
        }
2595
 
2596
      /* See if it is a valid register name in any group */
2597
      for (*group = 0; *group < OR32_NUM_SPGS; (*group)++)
2598
        {
2599
          *index = or32_regnum_from_name (*group, arg[0].str);
2600
 
2601
          if (*index >= 0)
2602
            {
2603
              return  trailer;
2604
            }
2605
        }
2606
 
2607
      /* Couldn't find it - print out a rude message */
2608
      ui_out_message (uiout, 0,
2609
                      "Group or register name not recognized.\n"
2610
                      "Valid groups are:\n");
2611
      for (i = 0; i < OR32_NUM_SPGS; i++)
2612
        {
2613
          ui_out_field_string (uiout, NULL, or32_spr_group_name (i));
2614
          ui_out_spaces (uiout, 1);
2615
          ui_out_wrap_hint (uiout, NULL);
2616
        }
2617
      ui_out_field_string (uiout, NULL, "\n");
2618
 
2619
      *group = -1;
2620
      *index = -1;
2621
      return  trailer;
2622
 
2623
    case 2:
2624
      /* See if first arg is a numeric group */
2625
      if (arg[0].is_num)
2626
        {
2627
          if (arg[0].val < OR32_NUM_SPGS)
2628
            {
2629
              *group = arg[0].val;
2630
              *index = -1;
2631
            }
2632
          else
2633
            {
2634
              ui_out_message (uiout, 0,
2635
                              "Group index should be in the range 0 - %d\n",
2636
                              OR32_NUM_SPGS - 1);
2637
              *group = -1;
2638
              *index = -1;
2639
              return trailer;
2640
            }
2641
        }
2642
      else
2643
        {
2644
          /* Is is it a group name? */
2645
          *group = or32_groupnum_from_name (arg[0].str);
2646
          if (*group >= 0)
2647
            {
2648
              *index = -1;
2649
            }
2650
          else
2651
            {
2652
              ui_out_message (uiout, 0,
2653
                              "Group name not recognized.\n"
2654
                              "Valid groups are:\n");
2655
              for (i = 0; i < OR32_NUM_SPGS; i++)
2656
                {
2657
                  ui_out_field_string (uiout, NULL, or32_spr_group_name (i));
2658
                  ui_out_spaces (uiout, 1);
2659
                  ui_out_wrap_hint (uiout, NULL);
2660
                }
2661
              ui_out_field_string (uiout, NULL, "\n");
2662
 
2663
              *group = -1;
2664
              *index = -1;
2665
              return  trailer;
2666
            }
2667
        }
2668
 
2669
      /* Is second arg an index or name? */
2670
      if (arg[1].is_num)
2671
        {
2672
          if (arg[1].val < OR32_SPG_SIZE)
2673
            {
2674
              /* Check this really is a register */
2675
              if (0 != strlen (or32_spr_register_name (*group, arg[1].val,
2676
                                                       spr_name)))
2677
                {
2678
                  *index = arg[1].val;
2679
                  return trailer;
2680
                }
2681
              else
2682
                {
2683
                  ui_out_message (uiout, 0,
2684
                                  "No valid register at that index in group\n");
2685
                  *group = -1;
2686
                  *index = -1;
2687
                  return  trailer;
2688
                }
2689
            }
2690
          else
2691
            {
2692
              ui_out_message (uiout, 0,
2693
                              "Register index should be in the range 0 - %d\n",
2694
                              OR32_SPG_SIZE - 1);
2695
              *group = -1;
2696
              *index = -1;
2697
              return  trailer;
2698
            }
2699
        }
2700
 
2701
      /* Must be a name */
2702
      *index = or32_regnum_from_name (*group, arg[1].str);
2703
 
2704
      if (*index >= 0)
2705
        {
2706
          return trailer;
2707
        }
2708
 
2709
      /* Couldn't find it - print out a rude message */
2710
      ui_out_message (uiout, 0, "Register name not recognized in group.\n");
2711
      *group = -1;
2712
      *index = -1;
2713
      return  trailer;
2714
 
2715
    default:
2716
      /* Anything else is an error */
2717
      ui_out_message (uiout, 0, "Unable to parse arguments\n");
2718
      *group = -1;
2719
      *index = -1;
2720
      return  trailer;
2721
    }
2722
}       /* or32_parse_spr_params() */
2723
 
2724
 
2725
/*---------------------------------------------------------------------------*/
2726
/*!Read a special purpose register from the target
2727
 
2728
   This has to be done using the target remote command "readspr"
2729
 
2730
   @param[in] regnum  The register to read
2731
 
2732
   @return  The value read */
2733
/*---------------------------------------------------------------------------*/
2734
 
2735
static ULONGEST
2736
or32_read_spr (unsigned int  regnum)
2737
{
2738
  struct ui_file    *uibuf = mem_fileopen ();
2739
  char               cmd[sizeof ("readspr ffff")];
2740
  unsigned long int  data;
2741
  char              *res;
2742
  long int           len;
2743
 
2744
  /* Create the command string and pass it to target remote command function */
2745
  sprintf (cmd, "readspr %4x", regnum);
2746
  target_rcmd (cmd, uibuf);
2747
 
2748
  /* Get the output for the UI file as a string */
2749
  res = ui_file_xstrdup (uibuf, &len);
2750
  sscanf (res, "%lx", &data);
2751
 
2752
  /* Tidy up */
2753
  xfree (res);
2754
  ui_file_delete (uibuf);
2755
 
2756
  return  (ULONGEST)data;
2757
 
2758
}       /* or32_read_spr() */
2759
 
2760
 
2761
/*---------------------------------------------------------------------------*/
2762
/*!Write a special purpose register on the target
2763
 
2764
   This has to be done using the target remote command "writespr"
2765
 
2766
   Since the SPRs may map to GPR's or the other GDB register (PPC, NPC, SR),
2767
   any register cache is flushed.
2768
 
2769
   @param[in] regnum  The register to write
2770
   @param[in] data  The value to write */
2771
/*---------------------------------------------------------------------------*/
2772
 
2773
static void
2774
or32_write_spr (unsigned int  regnum,
2775
                ULONGEST      data)
2776
{
2777
  struct ui_file    *uibuf = mem_fileopen ();
2778
  char               cmd[sizeof ("writespr ffff ffffffff")];
2779
  char              *res;
2780
  long int           len;
2781
 
2782
  /* Create the command string and pass it to target remote command function */
2783
  sprintf (cmd, "writespr %4x %8llx", regnum, (long long unsigned int)data);
2784
  target_rcmd (cmd, uibuf);
2785
 
2786
  /* Flush the register cache */
2787
  registers_changed ();
2788
 
2789
  /* We ignore the result - Rcmd can put out its own error messages. Just
2790
     tidy up */
2791
  ui_file_delete (uibuf);
2792
 
2793
}       /* or32_write_spr() */
2794
 
2795
 
2796
/*----------------------------------------------------------------------------*/
2797
/*!Show the value of a special purpose register or group
2798
 
2799
   This is a custom extension to the GDB info command.
2800
 
2801
   @param[in] args
2802
   @param[in] from_tty  True (1) if GDB is running from a TTY, false (0)
2803
   otherwise. */
2804
/*---------------------------------------------------------------------------*/
2805
 
2806
static void
2807
or32_info_spr_command (char *args,
2808
                       int   from_tty)
2809
{
2810
  int  group;
2811
  int  index;
2812
 
2813
  char  spr_name[32];
2814
 
2815
  or32_parse_spr_params (args, &group, &index, 0);
2816
 
2817
  if (group < 0)
2818
    {
2819
      return;                   /* Couldn't parse the args */
2820
    }
2821
 
2822
  if (index >= 0)
2823
    {
2824
      ULONGEST  value = or32_read_spr (OR32_SPR (group, index));
2825
 
2826
      ui_out_field_fmt (uiout, NULL, "%s.%s = SPR%i_%i = %llu (0x%llx)\n",
2827
                        or32_spr_group_name (group),
2828
                        or32_spr_register_name (group, index, spr_name), group,
2829
                        index, (long long unsigned int)value, (long long unsigned int)value);
2830
    }
2831
  else
2832
    {
2833
      /* Print all valid registers in the group */
2834
      for (index = 0; index < OR32_SPG_SIZE; index++)
2835
        {
2836
          if (0 != strlen (or32_spr_register_name (group, index, spr_name)))
2837
            {
2838
              ULONGEST  value = or32_read_spr (OR32_SPR (group, index));
2839
 
2840
              ui_out_field_fmt (uiout, NULL,
2841
                                "%s.%s = SPR%i_%i = %llu (0x%llx)\n",
2842
                                or32_spr_group_name (group),
2843
                                or32_spr_register_name (group, index, spr_name),
2844
                                group, index, (long long unsigned int)value, (long long unsigned int)value);
2845
            }
2846
        }
2847
    }
2848
}       /* or32_info_spr_command() */
2849
 
2850
 
2851
/*----------------------------------------------------------------------------*/
2852
/*!Set a special purpose register
2853
 
2854
   This is a custom command added to GDB.
2855
 
2856
   @param[in] args
2857
   @param[in] from_tty  True (1) if GDB is running from a TTY, false (0)
2858
   otherwise. */
2859
/*---------------------------------------------------------------------------*/
2860
 
2861
static void
2862
or32_spr_command (char *args,
2863
                  int   from_tty)
2864
{
2865
  int   group;
2866
  int   index;
2867
  char *tmp_str;
2868
  char *nargs = or32_parse_spr_params (args, &group, &index, 1);
2869
 
2870
  ULONGEST  old_val;
2871
  ULONGEST  new_val;
2872
 
2873
  char  spr_name[32];
2874
 
2875
  /* Do we have a valid register spec? */
2876
  if (index < 0)
2877
    {
2878
      return;           /* Parser will have printed the error message */
2879
    }
2880
 
2881
  /* Do we have a value to set? */
2882
 
2883
  errno = 0;
2884
  new_val = (ULONGEST)strtoul (nargs, &tmp_str, 0);
2885
 
2886
  if((0 != errno) || ('\0' != *tmp_str))
2887
    {
2888
      ui_out_message (uiout, 0, "Invalid value - register not changed\n");
2889
      return;
2890
    }
2891
 
2892
  old_val = or32_read_spr (OR32_SPR (group, index));
2893
 
2894
  or32_write_spr (OR32_SPR (group, index) , new_val);
2895
 
2896
  ui_out_field_fmt (uiout, NULL,
2897
                    "%s.%s (SPR%i_%i) set to %llu (0x%llx), "
2898
                    "was: %llu (0x%llx)\n",
2899
                    or32_spr_group_name (group),
2900
                    or32_spr_register_name (group, index, spr_name) , group,
2901
                    index, (long long unsigned int)new_val, (long long unsigned int)new_val, (long long unsigned int)old_val, (long long unsigned int)old_val);
2902
 
2903
}       /* or32_spr_command() */
2904
 
2905
 
2906
/*----------------------------------------------------------------------------*/
2907
/*!Main entry point for target architecture initialization
2908
 
2909
   In this version initializes the architecture via
2910
   registers_gdbarch_init(). Add a command to set and show special purpose
2911
   registers. */
2912
/*---------------------------------------------------------------------------*/
2913
 
2914
void
2915
_initialize_or32_tdep (void)
2916
{
2917
  /* Register this architecture. We should do this for or16 and or64 when
2918
     they have their BFD defined. */
2919
  gdbarch_register (bfd_arch_or32, or32_gdbarch_init, or32_dump_tdep);
2920
 
2921
  /* Initialize the automata for the assembler */
2922
  build_automata();
2923
 
2924
  /* Commands to show and set special purpose registers */
2925
  add_info ("spr", or32_info_spr_command,
2926
            "Show the value of a special purpose register");
2927
  add_com ("spr", class_support, or32_spr_command,
2928
           "Set a special purpose register");
2929
 
2930
}       /* _initialize_or32_tdep() */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.