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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [opcodes/] [or32-dis.c] - Blame information for rev 1780

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

Line No. Rev Author Line
1 578 markom
/* Instruction printing code for the OpenRISC 1000
2
   Copyright (C) 1990, 93, 94, 95, 1998 Free Software Foundation, Inc.
3
   Contributed by Damjan Lampret <lampret@opencores.org>.
4
   Modified from a29k port.
5
 
6
This file is part of GDB.
7
 
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
 
22
#define DEBUG 0
23
 
24
#include "dis-asm.h"
25
#include "opcode/or32.h"
26
 
27
#include <ctype.h>
28
#include <string.h>
29
 
30
#define EXTEND29(x) ((x) & 0x10000000 ? ((x) | 0xf0000000) : ((x)))
31
 
32
/* Now find the four bytes of INSN_CH and put them in *INSN.  */
33
static void
34
find_bytes_big (insn_ch, insn)
35
     unsigned char *insn_ch;
36
     unsigned long *insn;
37
{
38
  *insn = ((unsigned long)insn_ch[0] << 24) +
39
      ((unsigned long)insn_ch[1] << 16) +
40
      ((unsigned long)insn_ch[2] << 8) +
41
      (unsigned long)insn_ch[3];
42
#if DEBUG
43
      printf("find_bytes_big3: %x\n", *insn);
44
#endif
45
}
46
 
47
static void
48
find_bytes_little (insn_ch, insn)
49
     unsigned char *insn_ch;
50
     unsigned long *insn;
51
{
52
  *insn = ((unsigned long)insn_ch[3] << 24) +
53
      ((unsigned long)insn_ch[2] << 16) +
54
      ((unsigned long)insn_ch[1] << 8) +
55
      (unsigned long)insn_ch[0];
56
}
57
 
58
 
59
typedef void (*find_byte_func_type)
60
     PARAMS ((unsigned char *, unsigned long *));
61
 
62
static unsigned long
63
or32_extract(param_ch, enc_initial, insn)
64
     char param_ch;
65
     char *enc_initial;
66
     unsigned long insn;
67
{
68
  char *enc;
69
  unsigned long ret = 0;
70
        int opc_pos = 0;
71
        int param_pos = 0;
72
 
73
        for (enc = enc_initial; *enc != '\0'; enc++)
74
                if (*enc == param_ch)
75
                  if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
76
                     continue;
77
                  else
78
                     param_pos++;
79
 
80
#if DEBUG
81
  printf("or32_extract: %c %x ", param_ch, param_pos);
82
#endif
83
        opc_pos = 32;
84
        for (enc = enc_initial; *enc != '\0'; )
85
                if ((*enc == '0') && (*(enc+1) == 'x')) {
86
                  opc_pos -= 4;
87
                  if ((param_ch == '0') || (param_ch == '1')) {
88
                        unsigned long tmp = strtol(enc, NULL, 16);
89
#if DEBUG
90
                        printf(" enc=%s, tmp=%x ", enc, tmp);
91
#endif
92
                        if (param_ch == '0')
93
                          tmp = 15 - tmp;
94
                        ret |= tmp << opc_pos;
95
                   }
96
                   enc += 3;
97
                }
98
                else if ((*enc == '0') || (*enc == '1')) {
99
                  opc_pos--;
100
                  if (param_ch == *enc)
101
                        ret |= 1 << opc_pos;
102
                  enc++;
103
                }
104
                else if (*enc == param_ch) {
105
                        opc_pos--;
106
                        param_pos--;
107
#if DEBUG
108
                        printf("\n  ret=%x opc_pos=%x, param_pos=%x\n", ret, opc_pos, param_pos);
109
#endif
110
                        ret += ((insn >> opc_pos) & 0x1) << param_pos;
111
                        if (!param_pos && letter_signed(param_ch) && ret >> letter_range(param_ch) - 1) {
112
#if DEBUG
113
                           printf("\n  ret=%x opc_pos=%x, param_pos=%x\n", ret, opc_pos, param_pos);
114
#endif
115
                           ret |= 0xffffffff << letter_range(param_ch);
116
#if DEBUG
117
                           printf("\n  after conversion to signed: ret=%x\n", ret);
118
#endif
119
                        }
120
                        enc++;
121
                }
122
                else if (isalpha(*enc)) {
123
                        opc_pos--;
124
                        enc++;
125
                }
126
                else if (*enc == '-') {
127
                  opc_pos--;
128
                  enc++;
129
                }
130
                else
131
                        enc++;
132
 
133
#if DEBUG
134
  printf("ret=%x\n", ret);
135
#endif
136
  return ret;
137
}
138
 
139
static int
140
or32_opcode_match (insn, encoding)
141
     unsigned long insn;
142
     char *encoding;
143
{
144
  unsigned long ones, zeros;
145
 
146
#if DEBUG
147
  printf("or32_opcode_match: %.8lx\n", insn);
148
#endif    
149
  ones = or32_extract('1', encoding, insn);
150
  zeros = or32_extract('0', encoding, insn);
151
 
152
#if DEBUG
153
  printf("ones: %x \n", ones);
154
  printf("zeros: %x \n", zeros);
155
#endif
156
  if ((insn & ones) != ones) {
157
#if DEBUG
158
    printf("ret1\n");
159
#endif
160
    return 0;
161
  }
162
 
163
  if ((~insn & zeros) != zeros) {
164
#if DEBUG
165
    printf("ret2\n");
166
#endif
167
    return 0;
168
  }
169
 
170
#if DEBUG
171
  printf("ret3\n");
172
#endif
173
  return 1;
174
}
175
 
176
 
177
/* Print register to INFO->STREAM. Used only by print_insn. */
178
 
179
static void
180
or32_print_register (param_ch, encoding, insn, info)
181
     char param_ch;
182
     char *encoding;
183
     unsigned long insn;
184
     struct disassemble_info *info;
185
{
186
  int regnum = or32_extract(param_ch, encoding, insn);
187
 
188
#if DEBUG
189
  printf("or32_print_register: %c, %s, %x\n", param_ch, encoding, insn);
190
#endif  
191
  if (param_ch == 'A')
192
    (*info->fprintf_func) (info->stream, "r%d", regnum);
193
  else if (param_ch == 'B')
194
                (*info->fprintf_func) (info->stream, "r%d", regnum);
195
  else if (param_ch == 'D')
196
                (*info->fprintf_func) (info->stream, "r%d", regnum);
197
        else if (regnum < 16)
198
                (*info->fprintf_func) (info->stream, "r%d", regnum);
199
        else if (regnum < 32)
200
                (*info->fprintf_func) (info->stream, "r%d", regnum-16);
201
  else
202
                (*info->fprintf_func) (info->stream, "X%d", regnum);
203
}
204
 
205
/* Print immediate to INFO->STREAM. Used only by print_insn. */
206
 
207
static void
208
or32_print_immediate (param_ch, encoding, insn, info)
209
     char param_ch;
210
     char *encoding;
211
     unsigned long insn;
212
     struct disassemble_info *info;
213
{
214
  int imm = or32_extract(param_ch, encoding, insn);
215
 
216
  if (letter_signed(param_ch))
217
    (*info->fprintf_func) (info->stream, "0x%x", imm);
218
/*    (*info->fprintf_func) (info->stream, "%d", imm); */
219
  else
220
    (*info->fprintf_func) (info->stream, "0x%x", imm);
221
}
222
 
223
/* Print one instruction from MEMADDR on INFO->STREAM.
224
   Return the size of the instruction (always 4 on or32).  */
225
 
226
static int
227
print_insn (memaddr, info)
228
     bfd_vma memaddr;
229
     struct disassemble_info *info;
230
{
231
  /* The raw instruction.  */
232
  unsigned char insn_ch[4];
233
 
234
  /* Address. Will be sign extened 27-bit */
235
  int addr;
236
 
237
  /* The four bytes of the instruction.  */
238
  unsigned long insn;
239
 
240
  find_byte_func_type find_byte_func = (find_byte_func_type)info->private_data;
241
 
242
  struct or32_opcode CONST * opcode;
243
 
244
  {
245
    int status =
246
      (*info->read_memory_func) (memaddr, (bfd_byte *) &insn_ch[0], 4, info);
247
    if (status != 0)
248
      {
249
        (*info->memory_error_func) (status, memaddr, info);
250
        return -1;
251
      }
252
  }
253
 
254
  (*find_byte_func) (&insn_ch[0], &insn);
255
 
256
  for (opcode = &or32_opcodes[0];
257
       opcode < &or32_opcodes[num_opcodes];
258
       ++opcode)
259
    {
260
      if (or32_opcode_match (insn, opcode->encoding))
261
        {
262
          char *s;
263
 
264
          (*info->fprintf_func) (info->stream, "%s ", opcode->name);
265
          for (s = opcode->args; *s != '\0'; ++s)
266
            {
267
              switch (*s)
268
                {
269
                case '\0':
270
                  return 4;
271
 
272
                case 'r':
273
                  or32_print_register(*++s, opcode->encoding, insn, info);
274
                  break;
275
 
276
                case 'X':
277
                  addr = or32_extract ('X', opcode->encoding, insn) << 2;
278
 
279
                  /* Calulate the correct address.  XXX is this really correct ?? */
280
                  addr = memaddr + EXTEND29(addr);
281
 
282
                  (*info->print_address_func)
283
                    (addr, info);
284
                  break;
285
 
286
                default:
287
                  if (strchr (opcode->encoding, *s))
288
                    or32_print_immediate (*s, opcode->encoding, insn, info);
289
                  else
290
                    (*info->fprintf_func) (info->stream, "%c", *s);
291
                }
292
            }
293
 
294
          return 4;
295
        }
296
    }
297
 
298
  /* This used to be %8x for binutils.  */
299
  (*info->fprintf_func)
300
    (info->stream, ".word 0x%08x", insn);
301
  return 4;
302
}
303
 
304
/* Disassemble a big-endian or32 instruction.  */
305
int
306
print_insn_big_or32 (memaddr, info)
307
     bfd_vma memaddr;
308
     struct disassemble_info *info;
309
{
310
  info->private_data = (PTR) find_bytes_big;
311
  return print_insn (memaddr, info);
312
}
313
 
314
/* Disassemble a little-endian or32 instruction.  */
315
int
316
print_insn_little_or32 (memaddr, info)
317
     bfd_vma memaddr;
318
     struct disassemble_info *info;
319
{
320
  info->private_data = (PTR) find_bytes_little;
321
  return print_insn (memaddr, info);
322
}

powered by: WebSVN 2.1.0

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