One more time...
[deliverable/binutils-gdb.git] / gdb / mips-pinsn.c
CommitLineData
dd3b648e 1/* Print mips instructions for GDB, the GNU debugger.
2d62e807 2 Copyright 1989, 1991, 1992 Free Software Foundation, Inc.
dd3b648e
RP
3 Contributed by Nobuyuki Hikichi(hikichi@sra.co.jp)
4
5This file is part of GDB.
6
99a7de40 7This program is free software; you can redistribute it and/or modify
dd3b648e 8it under the terms of the GNU General Public License as published by
99a7de40
JG
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
dd3b648e 11
99a7de40 12This program is distributed in the hope that it will be useful,
dd3b648e
RP
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
99a7de40
JG
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
dd3b648e 20
dd3b648e 21#include "defs.h"
dd3b648e 22#include "symtab.h"
7e258d18 23#include "opcode/mips.h"
dd3b648e
RP
24
25/* Mips instructions are never longer than this many bytes. */
26#define MAXLEN 4
27
28/* Number of elements in the opcode table. */
29#define NOPCODES (sizeof mips_opcodes / sizeof mips_opcodes[0])
30
31#define MKLONG(p) *(unsigned long*)p
32
33extern char *reg_names[];
34
35\f
36/* subroutine */
37static unsigned char *
38print_insn_arg (d, l, stream, pc)
39 char *d;
40 register unsigned long int *l;
41 FILE *stream;
42 CORE_ADDR pc;
43{
44 switch (*d)
45 {
46 case ',':
47 case '(':
48 case ')':
49 fputc (*d, stream);
50 break;
51
52 case 's':
53 fprintf (stream, "$%s", reg_names[((struct op_i_fmt *) l)->rs]);
54 break;
55
56 case 't':
57 fprintf (stream, "$%s", reg_names[((struct op_i_fmt *) l)->rt]);
58 break;
59
60 case 'i':
61 fprintf (stream, "%d", ((struct op_i_fmt *) l)->immediate);
62 break;
63
64 case 'j': /* same as i, but sign-extended */
65 fprintf (stream, "%d", ((struct op_b_fmt *) l)->delta);
66 break;
67
68 case 'a':
69 print_address ((pc & 0xF0000000) | (((struct op_j_fmt *)l)->target << 2),
70 stream);
71 break;
72
73 case 'b':
74 print_address ((((struct op_b_fmt *) l)->delta << 2) + pc + 4, stream);
75 break;
76
77 case 'd':
b8c50f09 78 fprintf (stream, "$%s", reg_names[((struct op_r_fmt *) l)->rd]);
dd3b648e
RP
79 break;
80
81 case 'h':
82 fprintf (stream, "0x%x", ((struct op_r_fmt *) l)->shamt);
83 break;
84
85 case 'S':
86 fprintf (stream, "$f%d", ((struct fop_r_fmt *) l)->fs);
87 break;
88
89 case 'T':
90 fprintf (stream, "$f%d", ((struct fop_r_fmt *) l)->ft);
91 break;
92
93 case 'D':
94 fprintf (stream, "$f%d", ((struct fop_r_fmt *) l)->fd);
95 break;
96
97 default:
98 fprintf (stream, "# internal error, undefined modifier(%c)", *d);
99 break;
100 }
101}
102\f
103/* Print the mips instruction at address MEMADDR in debugged memory,
104 on STREAM. Returns length of the instruction, in bytes, which
105 is always 4. */
106
107int
108print_insn (memaddr, stream)
109 CORE_ADDR memaddr;
110 FILE *stream;
111{
112 unsigned char buffer[MAXLEN];
113 register int i;
114 register char *d;
115 unsigned long int l;
116
117 read_memory (memaddr, buffer, MAXLEN);
2d62e807 118 SWAP_TARGET_AND_HOST (buffer, MAXLEN);
dd3b648e
RP
119
120 for (i = 0; i < NOPCODES; i++)
121 {
122 register unsigned int opcode = mips_opcodes[i].opcode;
123 register unsigned int match = mips_opcodes[i].match;
124 if ((*(unsigned int*)buffer & match) == opcode)
125 break;
126 }
127
128 l = MKLONG (buffer);
129 /* Handle undefined instructions. */
130 if (i == NOPCODES)
131 {
132 fprintf (stream, "0x%x",l);
133 return 4;
134 }
135
136 fprintf (stream, "%s", mips_opcodes[i].name);
137
138 if (!(d = mips_opcodes[i].args))
139 return 4;
140
141 fputc (' ', stream);
142
143 while (*d)
144 print_insn_arg (d++, &l, stream, memaddr);
145
146 return 4;
147}
This page took 0.058735 seconds and 4 git commands to generate.