aboutsummaryrefslogtreecommitdiff
path: root/gdb/rs6000-pinsn.c
blob: 08e9125eccf8fcdc35fac978662c3dc3e87570ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* Print IBM RS/6000 instructions for GNU software.
   Copyright 1991 Free Software Foundation, Inc.
   Contributed by IBM Corporation.

This file is part of GDB and the GNU binutils.

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

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

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "defs.h"
#include "opcode/rs6k.h"

/* Print the rs6k instruction at address MEMADDR in debugged memory,
   on STREAM.  Returns length of the instruction, in bytes.  */

int
print_insn (memaddr, stream)
  CORE_ADDR memaddr;
  FILE *stream;
{
	int  pop, eop, probable_eop;	/* primary and extended opcodes */
	int  min, max;
	int  best = -1;			/* found best opcode index	*/
	int  oldbest = -1;
	unsigned int the_insn;

	read_memory (memaddr, &the_insn, sizeof (the_insn));
	pop = (unsigned)(the_insn >> 26);
	min = 0, max = NOPCODES-1;

	while (min < max) {
	  best = (min + max) / 2;

	  /* see if we are running in loops */
	  if (best == oldbest)
	    goto not_found;
	  oldbest = best;

	  if (pop < rs6k_ops [best].p_opcode)
	    max = best;

	  else if (pop > rs6k_ops [best].p_opcode)
	    min = best;

	  else {
	    /* Opcode matched, check extended opcode. */

	    if (rs6k_ops [best].e_opcode == -1) {
	      /* there is no valid extended opcode, what we've got is
		 just fine. */
	      goto insn_found;
	    }

	    /* Largest possible value of extended opcode. */
	    probable_eop = ((the_insn) >> 1) & 0x3ff;

	    eop = probable_eop & eopMask [rs6k_ops [best].format];

	    if (eop < rs6k_ops [best].e_opcode) {

	      while (pop == rs6k_ops [best].p_opcode) {
		if (eop == rs6k_ops [best].e_opcode)	/* found it! */
		  goto insn_found;
	        --best;
	        eop = probable_eop & eopMask [rs6k_ops [best].format];
	      }
	      goto not_found;
	    }

	    else if (eop > rs6k_ops [best].e_opcode) {

	      while (pop == rs6k_ops [best].p_opcode) {
		if (eop == rs6k_ops [best].e_opcode)	/* found it! */
		  goto insn_found;
	        ++best;
	        eop = probable_eop & eopMask [rs6k_ops [best].format];
	      }
	      goto not_found;
	    }

	    else /*  eop == rs6k_ops [best].e_opcode */
	      goto insn_found;
	  }
	}	

	best = min;
	if (pop == rs6k_ops [best].p_opcode &&
           (rs6k_ops [best].e_opcode == -1 || rs6k_ops [best].e_opcode == eop))
	    goto insn_found;

	else
	  goto not_found;


insn_found:
	print_operator (stream, memaddr, the_insn, best);
	return 4;

not_found:
	fprintf (stream, "0x%08x", the_insn);
	return 4;
}



/* condition code names */
static char *cond_code [] = {
  "lt", "gt", "eq", "so", "ge", "le", "ne", "ns", "nl", "ng", "z", "nz" };


print_operator (stream, memaddr, insn_word, insn_no)
FILE	*stream;
long	memaddr;
long	insn_word;
int	insn_no;
{
  char buf [20];
  char *qq = buf;
  char *pp = rs6k_ops[insn_no].opr_ext;
  int tmp;
  int nocomma = 0;			/* true if no comma needed */

  if (pp) {
    while (*pp) {

      switch ( *pp ) {
	case '.':
	  if (insn_word & 0x1)
	    *qq++ = '.';
	  break;

	case 'l':
	  if (insn_word & 0x1)
	   *qq++ = 'l';
	  break;

	case 't':
	  if ((insn_word & 0x03e00000) == 0x01800000)
	   *qq++ = 't';
	  break;

	case 'f':
	  if ((insn_word & 0x03e00000) == 0x00800000)
	   *qq++ = 'f';
	  break;

	case 'a':
	  if (insn_word & 0x2)
	   *qq++ = 'a';
	  break;

	case 'o':
	  if (insn_word & 0x4000)
	   *qq++ = 'o';
	  break;

	case '1':		/* exception #1 for bb/bc ambiguity */
	  tmp = (insn_word >> 21) & 0x1f;	/* extract BO	*/
	  if (tmp != 0xc && tmp != 0x4) {
	    /* you can't use `bb' now. switch to `bc' */
	    *(qq-1) = 'c';
	    ++insn_no;
	    pp = rs6k_ops[insn_no].opr_ext;
	    continue;
	  }
	  break;

	default:
	  abort ();
      }
      ++pp;
    }
  }
  *qq = '\0';

  fprintf (stream, "%s%s\t", rs6k_ops[insn_no].operator, buf);

  /* parse the operand now. */
  pp = rs6k_ops[insn_no].oprnd_format;

  while (*pp != 0) {
    switch (*pp) {
      case TO	:
	fprintf (stream, "%d", (insn_word >> 21) & 0x1f);
	break;

      case RT	:
      case RS	:
	fprintf (stream, "r%d", (insn_word >> 21) & 0x1f);
	break;

      case LI	:
	tmp  = (insn_word >> 16) & 0x1f;
#if 0
	/* This is wrong, wrong, wrong.  The condition code only goes
	   from 0 to 3 (for the instructions which can use extended
	   mnemonics of this type), and the XX (lt, gt, eq etc.) goes
	   into the mnemonic, not as an operand.

	   Probably the best way to get this right in both assembler
	   and disassembler is to switch to a match/lose style opcode
	   table like the sparc.  */
	if (tmp > 11) {
	  fprintf (stream, "{unknown cond code: 0x%x}", insn_word);
	  tmp = 0;
	}
	fprintf (stream, "%s", cond_code [tmp]);
#else
	/* So for just always use the "bbf/bbt" form.  This is perfectly
	   correct, just not necessarily as legible.

	   If tmp is not in the range 0-3, we can't use an XX form anyway.  */
	fprintf (stream, "%d", tmp);
#endif
	break;

      case A2	:
      case TA14	:
	tmp = (insn_word & 0xfffc);
	if (tmp & 0x8000)		/* fix sign extension	*/
	  tmp -= 0x10000;

	if ((insn_word & 0x2) == 0)	/* if AA not set	*/
	  tmp += memaddr;

        print_address (tmp, stream);
	break;

      case TA24	:
	tmp = insn_word & 0x03fffffc;
	if (tmp & 0x2000000)
	  tmp -= 0x4000000;
	
	if ((insn_word & 0x2) == 0)		/* if no AA bit set */
	  tmp += memaddr;

        print_address (tmp, stream);
	break;

      case LEV	:			/* for svc only */
	if (insn_word & 0x2) {		/* SA is set	*/
	  nocomma = 1;
	}
	else
          fprintf (stream, "%d", (insn_word >> 5) & 0x7f);
	break;

      case FL1	:			/* for svc only */
	if (insn_word & 0x2) {		/* SA is set	*/
	  nocomma = 1;
	}
	else
          fprintf (stream, "%d", (insn_word >> 12) & 0xf);
	break;

      case FL2	:			/* for svc only	*/
	nocomma = 0;
	if (insn_word & 0x2)		/* SA is set	*/
	  fprintf (stream, "%d", (insn_word >> 2) & 0x3fff);
	else
          fprintf (stream, "%d", (insn_word >> 2) & 0x7);
	break;

      case RA	:
	if (nocomma) {
	  fprintf (stream, "r%d)", (insn_word >> 16) & 0x1f);
	  nocomma = 0;
	}
	else
	  fprintf (stream, "r%d", (insn_word >> 16) & 0x1f);
	break;

      case RB	:
	fprintf (stream, "r%d", (insn_word >> 11) & 0x1f);
	break;

      case SI	:
	tmp = insn_word & 0xffff;
	if (tmp & 0x8000)
	  tmp -= 0x10000;
	fprintf (stream, "%d", tmp);
	break;

      case UI	:
	fprintf (stream, "%d", insn_word & 0xffff);
	break;

      case BF	:
	fprintf (stream, "%d", (insn_word >> 23) & 0x7);
	break;

      case BFA	:
	fprintf (stream, "%d", (insn_word >> 18) & 0x7);
	break;

      case BT	:
	fprintf (stream, "%d", (insn_word >> 21) & 0x1f);
	break;

      case BA	:
	fprintf (stream, "%d", (insn_word >> 16) & 0x1f);
	break;

      case BB	:
	fprintf (stream, "%d", (insn_word >> 11) & 0x1f);
	break;

      case BO	:
	fprintf (stream, "%d", (insn_word >> 21) & 0x1f);
	break;

      case BI	:
	fprintf (stream, "%d", (insn_word >> 16) & 0x1f);
	break;

      case SH	:
	fprintf (stream, "%d", (insn_word >> 11) & 0x1f);
	break;

      case MB	:
	fprintf (stream, "0x%x", (insn_word >> 6) & 0x1f);
	break;

      case ME	:
	fprintf (stream, "0x%x", (insn_word >> 1) & 0x1f);
	break;

      case SPR	:
	fprintf (stream, "%d", (insn_word >> 16) & 0x1f);
	break;

      case DIS	:
	nocomma = 1;
	tmp = insn_word & 0xffff;
	if (tmp & 0x8000)
	  tmp -= 0x10000;
	fprintf (stream, "%d(", tmp);
	break;

      case FXM	:
	fprintf (stream, "0x%x", (insn_word >> 12) & 0xff);
	break;

      case FRT	:
      case FRS	:
	fprintf (stream, "f%d", (insn_word >> 21) & 0x1f);
	break;

      case FRA	:
	fprintf (stream, "f%d", (insn_word >> 16) & 0x1f);
	break;

      case FRB	:
	fprintf (stream, "f%d", (insn_word >> 11) & 0x1f);
	break;

      case FRC	:
	fprintf (stream, "f%d", (insn_word >> 6) & 0x1f);
	break;

      case FLM	:
	fprintf (stream, "0x%x", (insn_word >> 17) & 0xff);
	break;

      case NB	:
	fprintf (stream, "%d", (insn_word >> 11) & 0x1f);
	break;

      case I	:
	fprintf (stream, "%d", (insn_word >> 12) & 0xf);
	break;

      default	:
	fprintf (stream,
		 "{Internal error: Unknown operand format identifier %d}",
		 *pp);
    }
    ++pp;

    if (*pp != '\0' && !nocomma)
      fputc(',', stream);
  }
}