1 /* Print ARM instructions for GDB, the GNU debugger.
2 Copyright (C) 1986, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include "arm-opcode.h"
29 extern char *reg_names[];
31 static char *shift_names[] = {
32 "lsl", "lsr", "asr", "ror",
35 static char *cond_names[] = {
36 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
37 "hi", "ls", "ge", "lt", "gt", "le", "", "nv"
40 static char float_precision[] = "sdep";
41 static char float_rounding[] = " pmz";
42 static float float_immed[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 0.5, 10.0 };
44 static void print_ldr_str_offset();
45 static void print_ldc_stc_offset();
46 static long immediate_value();
48 /* Print the ARM instruction at address MEMADDR in debugged memory,
49 on STREAM. Returns length of the instruction, in bytes. */
52 print_insn (memaddr, stream)
57 register struct opcode *op;
62 ins = read_memory_integer(memaddr, 4);
63 for (i = 0, op = opcodes; i < N_OPCODES; i++, op++)
64 if ((ins & op->mask) == op->value) break;
65 assert(i != N_OPCODES);
67 for (p = op->assembler; *p;) {
72 s = s*10 + (*p++ - '0');
76 e = e*10 + (*p++ - '0');
79 assert(s >= 0 && s <= 31 && e >= 0 && e <= 31);
80 val = (ins >> s) & ((1 << (e + 1 - s)) - 1);
86 fprintf(stream, "%d", val);
89 fprintf(stream, "%x", val);
92 assert(val >= 0 && val <= 15);
93 fprintf(stream, "%s", reg_names[val]);
96 fprintf(stream, "%s", cond_names[ins >> 28]);
121 if (((ins >> 12) & 0xf) == 0xf)
126 int immed = immediate_value(ins & 0xfff);
127 fprintf (stream, "#%d (0x%x)", immed, immed);
129 int operand2 = ins & 0xfff;
131 bits 0-3 are the base register
132 bits 5-6 are the shift (0=lsl, 1=lsr, 2=asr, 3=ror)
133 if bit 4 is zero then bits 7-11 are an immediate shift count
134 else bit 7 must be zero and bits 8-11 are the register
135 to be used as a shift count.
136 Note: no shift at all is encoded as "reg lsl #0" */
137 fprintf (stream, "%s", reg_names[operand2 & 0xf]);
138 if (operand2 & 0xff0) {
139 /* ror #0 is really rrx (rotate right extend) */
140 if ((operand2 & 0xff0) == 0x060)
141 fprintf (stream, ", rrx");
143 fprintf (stream, ", %s ",
144 shift_names[(operand2 >> 5) & 3]);
145 if (operand2 & (1<<4)) /* register shift */
146 fprintf (stream, "%s",
147 reg_names[operand2 >> 8]);
148 else /* immediate shift */
149 fprintf (stream, "#%d",
156 fprintf (stream, "[%s", reg_names[(ins >> 16) & 0xf]);
158 fprintf (stream, ", ");
159 print_ldr_str_offset (ins, stream);
161 if (ins & (1<<21)) putc('!', stream);
162 /* If it is a pc relative load, then it is probably
163 a constant so print it */
164 if (((ins >> 16) & 0xf) == 15 &&
165 (ins & (1<<25)) == 0 &&
167 int addr = memaddr + 8 +
168 (ins & 0xfff) * ((ins & (1<<23)) ? 1 : -1);
169 fprintf (stream, " (contents=");
170 print_address (read_memory_integer(addr, 4), stream);
171 fprintf (stream, ")");
174 fprintf (stream, "]," );
175 print_ldr_str_offset (ins, stream);
179 print_address (memaddr + 8 + (((int)ins << 8) >> 6), stream);
182 fprintf (stream, "[%s", reg_names[(ins >> 16) & 0xf]);
184 fprintf (stream, ", ");
185 print_ldc_stc_offset (ins, stream);
190 fprintf (stream, "], ");
191 print_ldc_stc_offset (ins, stream);
196 int regnum, first = 1;
198 for (regnum = 0; regnum < 16; regnum++)
199 if (ins & (1<<regnum)) {
203 fprintf (stream, "%s", reg_names[regnum]);
209 val = ((ins >> 18) & 2) | ((ins >> 7) & 1);
210 putc(float_precision[val], stream);
213 val = ((ins >> 21) & 2) | ((ins >> 15) & 1);
214 putc(float_precision[val], stream);
217 val = ((ins >> 5) & 3);
218 if (val) putc(float_rounding[val], stream);
221 assert(val >= 0 && val <= 15);
223 fprintf (stream, "#%3.1f", float_immed[val - 8]);
225 fprintf (stream, "f%d", val);
237 immediate_value(operand)
240 int val = operand & 0xff;
241 int shift = 2*(operand >> 8);
242 /* immediate value is (val ror shift) */
243 return (val >> shift) | (val << (32 - shift));
247 print_ldr_str_offset(ins, stream)
251 if ((ins & (1<<25)) == 0)
252 fprintf (stream, "#%d",
253 (ins & 0xfff) * ((ins & (1<<23)) ? 1 : -1));
255 fprintf (stream, "%s%s", reg_names[ins & 0xf],
256 (ins & (1<<23)) ? "" : "-");
258 fprintf (stream, ", %s #%d",
259 shift_names[(ins >> 5) & 3],
265 print_ldc_stc_offset(ins, stream)
269 fprintf (stream, "#%d",
270 4 * (ins & 0xff) * ((ins & (1<<23)) ? 1 : -1));