arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / arch / arm.c
blobf1c292b44c9b08daf7473838507cd13f9535f140
1 /* Common target dependent code for GDB on ARM systems.
3 Copyright (C) 1988-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbsupport/common-regcache.h"
21 #include "arm.h"
23 #include "../features/arm/arm-core.c"
24 #include "../features/arm/arm-tls.c"
25 #include "../features/arm/arm-vfpv2.c"
26 #include "../features/arm/arm-vfpv3.c"
27 #include "../features/arm/xscale-iwmmxt.c"
28 #include "../features/arm/arm-m-profile.c"
29 #include "../features/arm/arm-m-profile-with-fpa.c"
30 #include "../features/arm/arm-m-profile-mve.c"
31 #include "../features/arm/arm-m-system.c"
33 /* See arm.h. */
35 int
36 thumb_insn_size (unsigned short inst1)
38 if ((inst1 & 0xe000) == 0xe000 && (inst1 & 0x1800) != 0)
39 return 4;
40 else
41 return 2;
44 /* See arm.h. */
46 int
47 condition_true (unsigned long cond, unsigned long status_reg)
49 if (cond == INST_AL || cond == INST_NV)
50 return 1;
52 switch (cond)
54 case INST_EQ:
55 return ((status_reg & FLAG_Z) != 0);
56 case INST_NE:
57 return ((status_reg & FLAG_Z) == 0);
58 case INST_CS:
59 return ((status_reg & FLAG_C) != 0);
60 case INST_CC:
61 return ((status_reg & FLAG_C) == 0);
62 case INST_MI:
63 return ((status_reg & FLAG_N) != 0);
64 case INST_PL:
65 return ((status_reg & FLAG_N) == 0);
66 case INST_VS:
67 return ((status_reg & FLAG_V) != 0);
68 case INST_VC:
69 return ((status_reg & FLAG_V) == 0);
70 case INST_HI:
71 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
72 case INST_LS:
73 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
74 case INST_GE:
75 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
76 case INST_LT:
77 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
78 case INST_GT:
79 return (((status_reg & FLAG_Z) == 0)
80 && (((status_reg & FLAG_N) == 0)
81 == ((status_reg & FLAG_V) == 0)));
82 case INST_LE:
83 return (((status_reg & FLAG_Z) != 0)
84 || (((status_reg & FLAG_N) == 0)
85 != ((status_reg & FLAG_V) == 0)));
87 return 1;
91 /* See arm.h. */
93 int
94 thumb_advance_itstate (unsigned int itstate)
96 /* Preserve IT[7:5], the first three bits of the condition. Shift
97 the upcoming condition flags left by one bit. */
98 itstate = (itstate & 0xe0) | ((itstate << 1) & 0x1f);
100 /* If we have finished the IT block, clear the state. */
101 if ((itstate & 0x0f) == 0)
102 itstate = 0;
104 return itstate;
107 /* See arm.h. */
110 arm_instruction_changes_pc (uint32_t this_instr)
112 if (bits (this_instr, 28, 31) == INST_NV)
113 /* Unconditional instructions. */
114 switch (bits (this_instr, 24, 27))
116 case 0xa:
117 case 0xb:
118 /* Branch with Link and change to Thumb. */
119 return 1;
120 case 0xc:
121 case 0xd:
122 case 0xe:
123 /* Coprocessor register transfer. */
124 if (bits (this_instr, 12, 15) == 15)
125 error (_("Invalid update to pc in instruction"));
126 return 0;
127 default:
128 return 0;
130 else
131 switch (bits (this_instr, 25, 27))
133 case 0x0:
134 if (bits (this_instr, 23, 24) == 2 && bit (this_instr, 20) == 0)
136 /* Multiplies and extra load/stores. */
137 if (bit (this_instr, 4) == 1 && bit (this_instr, 7) == 1)
138 /* Neither multiplies nor extension load/stores are allowed
139 to modify PC. */
140 return 0;
142 /* Otherwise, miscellaneous instructions. */
144 /* BX <reg>, BXJ <reg>, BLX <reg> */
145 if (bits (this_instr, 4, 27) == 0x12fff1
146 || bits (this_instr, 4, 27) == 0x12fff2
147 || bits (this_instr, 4, 27) == 0x12fff3)
148 return 1;
150 /* Other miscellaneous instructions are unpredictable if they
151 modify PC. */
152 return 0;
154 /* Data processing instruction. */
155 [[fallthrough]];
157 case 0x1:
158 if (bits (this_instr, 12, 15) == 15)
159 return 1;
160 else
161 return 0;
163 case 0x2:
164 case 0x3:
165 /* Media instructions and architecturally undefined instructions. */
166 if (bits (this_instr, 25, 27) == 3 && bit (this_instr, 4) == 1)
167 return 0;
169 /* Stores. */
170 if (bit (this_instr, 20) == 0)
171 return 0;
173 /* Loads. */
174 if (bits (this_instr, 12, 15) == ARM_PC_REGNUM)
175 return 1;
176 else
177 return 0;
179 case 0x4:
180 /* Load/store multiple. */
181 if (bit (this_instr, 20) == 1 && bit (this_instr, 15) == 1)
182 return 1;
183 else
184 return 0;
186 case 0x5:
187 /* Branch and branch with link. */
188 return 1;
190 case 0x6:
191 case 0x7:
192 /* Coprocessor transfers or SWIs can not affect PC. */
193 return 0;
195 default:
196 internal_error (_("bad value in switch"));
200 /* See arm.h. */
203 thumb_instruction_changes_pc (unsigned short inst)
205 if ((inst & 0xff00) == 0xbd00) /* pop {rlist, pc} */
206 return 1;
208 if ((inst & 0xf000) == 0xd000) /* conditional branch */
209 return 1;
211 if ((inst & 0xf800) == 0xe000) /* unconditional branch */
212 return 1;
214 if ((inst & 0xff00) == 0x4700) /* bx REG, blx REG */
215 return 1;
217 if ((inst & 0xff87) == 0x4687) /* mov pc, REG */
218 return 1;
220 if ((inst & 0xf500) == 0xb100) /* CBNZ or CBZ. */
221 return 1;
223 return 0;
227 /* See arm.h. */
230 thumb2_instruction_changes_pc (unsigned short inst1, unsigned short inst2)
232 if ((inst1 & 0xf800) == 0xf000 && (inst2 & 0x8000) == 0x8000)
234 /* Branches and miscellaneous control instructions. */
236 if ((inst2 & 0x1000) != 0 || (inst2 & 0xd001) == 0xc000)
238 /* B, BL, BLX. */
239 return 1;
241 else if (inst1 == 0xf3de && (inst2 & 0xff00) == 0x3f00)
243 /* SUBS PC, LR, #imm8. */
244 return 1;
246 else if ((inst2 & 0xd000) == 0x8000 && (inst1 & 0x0380) != 0x0380)
248 /* Conditional branch. */
249 return 1;
252 return 0;
255 if ((inst1 & 0xfe50) == 0xe810)
257 /* Load multiple or RFE. */
259 if (bit (inst1, 7) && !bit (inst1, 8))
261 /* LDMIA or POP */
262 if (bit (inst2, 15))
263 return 1;
265 else if (!bit (inst1, 7) && bit (inst1, 8))
267 /* LDMDB */
268 if (bit (inst2, 15))
269 return 1;
271 else if (bit (inst1, 7) && bit (inst1, 8))
273 /* RFEIA */
274 return 1;
276 else if (!bit (inst1, 7) && !bit (inst1, 8))
278 /* RFEDB */
279 return 1;
282 return 0;
285 if ((inst1 & 0xffef) == 0xea4f && (inst2 & 0xfff0) == 0x0f00)
287 /* MOV PC or MOVS PC. */
288 return 1;
291 if ((inst1 & 0xff70) == 0xf850 && (inst2 & 0xf000) == 0xf000)
293 /* LDR PC. */
294 if (bits (inst1, 0, 3) == 15)
295 return 1;
296 if (bit (inst1, 7))
297 return 1;
298 if (bit (inst2, 11))
299 return 1;
300 if ((inst2 & 0x0fc0) == 0x0000)
301 return 1;
303 return 0;
306 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf000)
308 /* TBB. */
309 return 1;
312 if ((inst1 & 0xfff0) == 0xe8d0 && (inst2 & 0xfff0) == 0xf010)
314 /* TBH. */
315 return 1;
318 return 0;
321 /* See arm.h. */
323 unsigned long
324 shifted_reg_val (reg_buffer_common *regcache, unsigned long inst,
325 int carry, unsigned long pc_val, unsigned long status_reg)
327 unsigned long res, shift;
328 int rm = bits (inst, 0, 3);
329 unsigned long shifttype = bits (inst, 5, 6);
331 if (bit (inst, 4))
333 int rs = bits (inst, 8, 11);
334 shift = (rs == 15
335 ? pc_val + 8
336 : regcache_raw_get_unsigned (regcache, rs)) & 0xFF;
338 else
339 shift = bits (inst, 7, 11);
341 res = (rm == ARM_PC_REGNUM
342 ? (pc_val + (bit (inst, 4) ? 12 : 8))
343 : regcache_raw_get_unsigned (regcache, rm));
345 switch (shifttype)
347 case 0: /* LSL */
348 res = shift >= 32 ? 0 : res << shift;
349 break;
351 case 1: /* LSR */
352 res = shift >= 32 ? 0 : res >> shift;
353 break;
355 case 2: /* ASR */
356 if (shift >= 32)
357 shift = 31;
358 res = ((res & 0x80000000L)
359 ? ~((~res) >> shift) : res >> shift);
360 break;
362 case 3: /* ROR/RRX */
363 shift &= 31;
364 if (shift == 0)
365 res = (res >> 1) | (carry ? 0x80000000L : 0);
366 else
367 res = (res >> shift) | (res << (32 - shift));
368 break;
371 return res & 0xffffffff;
374 /* See arch/arm.h. */
376 target_desc *
377 arm_create_target_description (arm_fp_type fp_type, bool tls)
379 target_desc_up tdesc = allocate_target_description ();
381 #ifndef IN_PROCESS_AGENT
382 if (fp_type == ARM_FP_TYPE_IWMMXT)
383 set_tdesc_architecture (tdesc.get (), "iwmmxt");
384 else
385 set_tdesc_architecture (tdesc.get (), "arm");
386 #endif
388 long regnum = 0;
390 regnum = create_feature_arm_arm_core (tdesc.get (), regnum);
392 switch (fp_type)
394 case ARM_FP_TYPE_NONE:
395 break;
397 case ARM_FP_TYPE_VFPV2:
398 regnum = create_feature_arm_arm_vfpv2 (tdesc.get (), regnum);
399 break;
401 case ARM_FP_TYPE_VFPV3:
402 regnum = create_feature_arm_arm_vfpv3 (tdesc.get (), regnum);
403 break;
405 case ARM_FP_TYPE_IWMMXT:
406 regnum = create_feature_arm_xscale_iwmmxt (tdesc.get (), regnum);
407 break;
409 default:
410 error (_("Invalid Arm FP type: %d"), fp_type);
413 if (tls)
414 regnum = create_feature_arm_arm_tls (tdesc.get (), regnum);
416 return tdesc.release ();
419 /* See arch/arm.h. */
421 target_desc *
422 arm_create_mprofile_target_description (arm_m_profile_type m_type)
424 target_desc *tdesc = allocate_target_description ().release ();
426 #ifndef IN_PROCESS_AGENT
427 set_tdesc_architecture (tdesc, "arm");
428 #endif
430 long regnum = 0;
432 switch (m_type)
434 case ARM_M_TYPE_M_PROFILE:
435 regnum = create_feature_arm_arm_m_profile (tdesc, regnum);
436 break;
438 case ARM_M_TYPE_VFP_D16:
439 regnum = create_feature_arm_arm_m_profile (tdesc, regnum);
440 regnum = create_feature_arm_arm_vfpv2 (tdesc, regnum);
441 break;
443 case ARM_M_TYPE_WITH_FPA:
444 regnum = create_feature_arm_arm_m_profile_with_fpa (tdesc, regnum);
445 break;
447 case ARM_M_TYPE_MVE:
448 regnum = create_feature_arm_arm_m_profile (tdesc, regnum);
449 regnum = create_feature_arm_arm_vfpv2 (tdesc, regnum);
450 regnum = create_feature_arm_arm_m_profile_mve (tdesc, regnum);
451 break;
453 case ARM_M_TYPE_SYSTEM:
454 regnum = create_feature_arm_arm_m_profile (tdesc, regnum);
455 regnum = create_feature_arm_arm_m_system (tdesc, regnum);
456 break;
458 default:
459 error (_("Invalid Arm M type: %d"), m_type);
462 return tdesc;