]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/arm-tdep.c
Fix whitespace problem in my most recent entry.
[thirdparty/binutils-gdb.git] / gdb / arm-tdep.c
CommitLineData
ed9a39eb 1/* Common target dependent code for GDB on ARM systems.
4e052eda
AC
2 Copyright 1988, 1989, 1991, 1992, 1993, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "frame.h"
24#include "inferior.h"
25#include "gdbcmd.h"
26#include "gdbcore.h"
27#include "symfile.h"
28#include "gdb_string.h"
29#include "coff/internal.h" /* Internal format of COFF symbols in BFD */
e8b09175 30#include "dis-asm.h" /* For register flavors. */
30f6df08 31#include <ctype.h> /* for isupper () */
4e052eda 32#include "regcache.h"
c906108c 33
2a451106
KB
34/* Each OS has a different mechanism for accessing the various
35 registers stored in the sigcontext structure.
36
37 SIGCONTEXT_REGISTER_ADDRESS should be defined to the name (or
38 function pointer) which may be used to determine the addresses
39 of the various saved registers in the sigcontext structure.
40
41 For the ARM target, there are three parameters to this function.
42 The first is the pc value of the frame under consideration, the
43 second the stack pointer of this frame, and the last is the
44 register number to fetch.
45
46 If the tm.h file does not define this macro, then it's assumed that
47 no mechanism is needed and we define SIGCONTEXT_REGISTER_ADDRESS to
48 be 0.
49
50 When it comes time to multi-arching this code, see the identically
51 named machinery in ia64-tdep.c for an example of how it could be
52 done. It should not be necessary to modify the code below where
53 this macro is used. */
54
3bb04bdd
AC
55#ifdef SIGCONTEXT_REGISTER_ADDRESS
56#ifndef SIGCONTEXT_REGISTER_ADDRESS_P
57#define SIGCONTEXT_REGISTER_ADDRESS_P() 1
58#endif
59#else
60#define SIGCONTEXT_REGISTER_ADDRESS(SP,PC,REG) 0
61#define SIGCONTEXT_REGISTER_ADDRESS_P() 0
2a451106
KB
62#endif
63
ed9a39eb
JM
64extern void _initialize_arm_tdep (void);
65
bc90b915
FN
66/* Number of different reg name sets (options). */
67static int num_flavor_options;
68
69/* We have more registers than the disassembler as gdb can print the value
70 of special registers as well.
71 The general register names are overwritten by whatever is being used by
72 the disassembler at the moment. We also adjust the case of cpsr and fps. */
73
74/* Initial value: Register names used in ARM's ISA documentation. */
75static char * arm_register_name_strings[] =
da59e081
JM
76{"r0", "r1", "r2", "r3", /* 0 1 2 3 */
77 "r4", "r5", "r6", "r7", /* 4 5 6 7 */
78 "r8", "r9", "r10", "r11", /* 8 9 10 11 */
79 "r12", "sp", "lr", "pc", /* 12 13 14 15 */
80 "f0", "f1", "f2", "f3", /* 16 17 18 19 */
81 "f4", "f5", "f6", "f7", /* 20 21 22 23 */
bc90b915
FN
82 "fps", "cpsr" }; /* 24 25 */
83char **arm_register_names = arm_register_name_strings;
ed9a39eb 84
bc90b915 85/* Valid register name flavors. */
53904c9e 86static const char **valid_flavors;
ed9a39eb 87
bc90b915 88/* Disassembly flavor to use. Default to "std" register names. */
53904c9e 89static const char *disassembly_flavor;
bc90b915 90static int current_option; /* Index to that option in the opcodes table. */
96baa820 91
ed9a39eb
JM
92/* This is used to keep the bfd arch_info in sync with the disassembly
93 flavor. */
94static void set_disassembly_flavor_sfunc(char *, int,
95 struct cmd_list_element *);
96static void set_disassembly_flavor (void);
97
98static void convert_from_extended (void *ptr, void *dbl);
99
100/* Define other aspects of the stack frame. We keep the offsets of
101 all saved registers, 'cause we need 'em a lot! We also keep the
102 current size of the stack frame, and the offset of the frame
103 pointer from the stack pointer (for frameless functions, and when
104 we're still in the prologue of a function with a frame) */
105
106struct frame_extra_info
107 {
108 struct frame_saved_regs fsr;
109 int framesize;
110 int frameoffset;
111 int framereg;
112 };
113
bc90b915
FN
114/* Addresses for calling Thumb functions have the bit 0 set.
115 Here are some macros to test, set, or clear bit 0 of addresses. */
116#define IS_THUMB_ADDR(addr) ((addr) & 1)
117#define MAKE_THUMB_ADDR(addr) ((addr) | 1)
118#define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
119
8b93c638
JM
120#define SWAP_TARGET_AND_HOST(buffer,len) \
121 do \
122 { \
123 if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \
124 { \
125 char tmp; \
126 char *p = (char *)(buffer); \
127 char *q = ((char *)(buffer)) + len - 1; \
128 for (; p < q; p++, q--) \
129 { \
130 tmp = *q; \
131 *q = *p; \
132 *p = tmp; \
133 } \
134 } \
135 } \
136 while (0)
137
ed9a39eb
JM
138/* Will a function return an aggregate type in memory or in a
139 register? Return 0 if an aggregate type can be returned in a
140 register, 1 if it must be returned in memory. */
085dd6e6 141
c906108c 142int
ed9a39eb 143arm_use_struct_convention (int gcc_p, struct type *type)
c906108c 144{
ed9a39eb
JM
145 int nRc;
146 register enum type_code code;
147
148 /* In the ARM ABI, "integer" like aggregate types are returned in
149 registers. For an aggregate type to be integer like, its size
150 must be less than or equal to REGISTER_SIZE and the offset of
151 each addressable subfield must be zero. Note that bit fields are
152 not addressable, and all addressable subfields of unions always
153 start at offset zero.
154
155 This function is based on the behaviour of GCC 2.95.1.
156 See: gcc/arm.c: arm_return_in_memory() for details.
157
158 Note: All versions of GCC before GCC 2.95.2 do not set up the
159 parameters correctly for a function returning the following
160 structure: struct { float f;}; This should be returned in memory,
161 not a register. Richard Earnshaw sent me a patch, but I do not
162 know of any way to detect if a function like the above has been
163 compiled with the correct calling convention. */
164
165 /* All aggregate types that won't fit in a register must be returned
166 in memory. */
167 if (TYPE_LENGTH (type) > REGISTER_SIZE)
168 {
169 return 1;
170 }
171
172 /* The only aggregate types that can be returned in a register are
173 structs and unions. Arrays must be returned in memory. */
174 code = TYPE_CODE (type);
175 if ((TYPE_CODE_STRUCT != code) && (TYPE_CODE_UNION != code))
176 {
177 return 1;
178 }
179
180 /* Assume all other aggregate types can be returned in a register.
181 Run a check for structures, unions and arrays. */
182 nRc = 0;
183
184 if ((TYPE_CODE_STRUCT == code) || (TYPE_CODE_UNION == code))
185 {
186 int i;
187 /* Need to check if this struct/union is "integer" like. For
188 this to be true, its size must be less than or equal to
189 REGISTER_SIZE and the offset of each addressable subfield
190 must be zero. Note that bit fields are not addressable, and
191 unions always start at offset zero. If any of the subfields
192 is a floating point type, the struct/union cannot be an
193 integer type. */
194
195 /* For each field in the object, check:
196 1) Is it FP? --> yes, nRc = 1;
197 2) Is it addressable (bitpos != 0) and
198 not packed (bitsize == 0)?
199 --> yes, nRc = 1
200 */
201
202 for (i = 0; i < TYPE_NFIELDS (type); i++)
203 {
204 enum type_code field_type_code;
205 field_type_code = TYPE_CODE (TYPE_FIELD_TYPE (type, i));
206
207 /* Is it a floating point type field? */
208 if (field_type_code == TYPE_CODE_FLT)
209 {
210 nRc = 1;
211 break;
212 }
213
214 /* If bitpos != 0, then we have to care about it. */
215 if (TYPE_FIELD_BITPOS (type, i) != 0)
216 {
217 /* Bitfields are not addressable. If the field bitsize is
218 zero, then the field is not packed. Hence it cannot be
219 a bitfield or any other packed type. */
220 if (TYPE_FIELD_BITSIZE (type, i) == 0)
221 {
222 nRc = 1;
223 break;
224 }
225 }
226 }
227 }
228
229 return nRc;
c906108c
SS
230}
231
232int
ed9a39eb 233arm_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
c906108c 234{
c906108c
SS
235 return (chain != 0 && (FRAME_SAVED_PC (thisframe) >= LOWEST_PC));
236}
237
238/* Set to true if the 32-bit mode is in use. */
239
240int arm_apcs_32 = 1;
241
ed9a39eb
JM
242/* Flag set by arm_fix_call_dummy that tells whether the target
243 function is a Thumb function. This flag is checked by
244 arm_push_arguments. FIXME: Change the PUSH_ARGUMENTS macro (and
245 its use in valops.c) to pass the function address as an additional
246 parameter. */
c906108c
SS
247
248static int target_is_thumb;
249
ed9a39eb
JM
250/* Flag set by arm_fix_call_dummy that tells whether the calling
251 function is a Thumb function. This flag is checked by
252 arm_pc_is_thumb and arm_call_dummy_breakpoint_offset. */
c906108c
SS
253
254static int caller_is_thumb;
255
ed9a39eb
JM
256/* Determine if the program counter specified in MEMADDR is in a Thumb
257 function. */
c906108c
SS
258
259int
2a451106 260arm_pc_is_thumb (CORE_ADDR memaddr)
c906108c 261{
c5aa993b 262 struct minimal_symbol *sym;
c906108c 263
ed9a39eb 264 /* If bit 0 of the address is set, assume this is a Thumb address. */
c906108c
SS
265 if (IS_THUMB_ADDR (memaddr))
266 return 1;
267
ed9a39eb 268 /* Thumb functions have a "special" bit set in minimal symbols. */
c906108c
SS
269 sym = lookup_minimal_symbol_by_pc (memaddr);
270 if (sym)
271 {
c5aa993b 272 return (MSYMBOL_IS_SPECIAL (sym));
c906108c
SS
273 }
274 else
ed9a39eb
JM
275 {
276 return 0;
277 }
c906108c
SS
278}
279
ed9a39eb
JM
280/* Determine if the program counter specified in MEMADDR is in a call
281 dummy being called from a Thumb function. */
c906108c
SS
282
283int
2a451106 284arm_pc_is_thumb_dummy (CORE_ADDR memaddr)
c906108c 285{
c5aa993b 286 CORE_ADDR sp = read_sp ();
c906108c 287
dfcd3bfb
JM
288 /* FIXME: Until we switch for the new call dummy macros, this heuristic
289 is the best we can do. We are trying to determine if the pc is on
290 the stack, which (hopefully) will only happen in a call dummy.
291 We hope the current stack pointer is not so far alway from the dummy
292 frame location (true if we have not pushed large data structures or
293 gone too many levels deep) and that our 1024 is not enough to consider
294 code regions as part of the stack (true for most practical purposes) */
295 if (PC_IN_CALL_DUMMY (memaddr, sp, sp + 1024))
c906108c
SS
296 return caller_is_thumb;
297 else
298 return 0;
299}
300
301CORE_ADDR
ed9a39eb 302arm_addr_bits_remove (CORE_ADDR val)
c906108c
SS
303{
304 if (arm_pc_is_thumb (val))
305 return (val & (arm_apcs_32 ? 0xfffffffe : 0x03fffffe));
306 else
307 return (val & (arm_apcs_32 ? 0xfffffffc : 0x03fffffc));
308}
309
310CORE_ADDR
ed9a39eb 311arm_saved_pc_after_call (struct frame_info *frame)
c906108c
SS
312{
313 return ADDR_BITS_REMOVE (read_register (LR_REGNUM));
314}
315
392a587b 316int
ed9a39eb 317arm_frameless_function_invocation (struct frame_info *fi)
392a587b 318{
392a587b 319 CORE_ADDR func_start, after_prologue;
96baa820 320 int frameless;
ed9a39eb 321
392a587b 322 func_start = (get_pc_function_start ((fi)->pc) + FUNCTION_START_OFFSET);
7be570e7 323 after_prologue = SKIP_PROLOGUE (func_start);
ed9a39eb 324
96baa820 325 /* There are some frameless functions whose first two instructions
ed9a39eb
JM
326 follow the standard APCS form, in which case after_prologue will
327 be func_start + 8. */
328
96baa820 329 frameless = (after_prologue < func_start + 12);
392a587b
JM
330 return frameless;
331}
332
c906108c 333/* A typical Thumb prologue looks like this:
c5aa993b
JM
334 push {r7, lr}
335 add sp, sp, #-28
336 add r7, sp, #12
c906108c 337 Sometimes the latter instruction may be replaced by:
da59e081
JM
338 mov r7, sp
339
340 or like this:
341 push {r7, lr}
342 mov r7, sp
343 sub sp, #12
344
345 or, on tpcs, like this:
346 sub sp,#16
347 push {r7, lr}
348 (many instructions)
349 mov r7, sp
350 sub sp, #12
351
352 There is always one instruction of three classes:
353 1 - push
354 2 - setting of r7
355 3 - adjusting of sp
356
357 When we have found at least one of each class we are done with the prolog.
358 Note that the "sub sp, #NN" before the push does not count.
ed9a39eb 359 */
c906108c
SS
360
361static CORE_ADDR
c7885828 362thumb_skip_prologue (CORE_ADDR pc, CORE_ADDR func_end)
c906108c
SS
363{
364 CORE_ADDR current_pc;
da59e081
JM
365 int findmask = 0; /* findmask:
366 bit 0 - push { rlist }
367 bit 1 - mov r7, sp OR add r7, sp, #imm (setting of r7)
368 bit 2 - sub sp, #simm OR add sp, #simm (adjusting of sp)
369 */
c906108c 370
c7885828 371 for (current_pc = pc; current_pc + 2 < func_end && current_pc < pc + 40; current_pc += 2)
c906108c
SS
372 {
373 unsigned short insn = read_memory_unsigned_integer (current_pc, 2);
374
da59e081
JM
375 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
376 {
377 findmask |= 1; /* push found */
378 }
379 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR sub sp, #simm */
380 {
381 if ((findmask & 1) == 0) /* before push ? */
382 continue;
383 else
384 findmask |= 4; /* add/sub sp found */
385 }
386 else if ((insn & 0xff00) == 0xaf00) /* add r7, sp, #imm */
387 {
388 findmask |= 2; /* setting of r7 found */
389 }
390 else if (insn == 0x466f) /* mov r7, sp */
391 {
392 findmask |= 2; /* setting of r7 found */
393 }
394 else
395 continue; /* something in the prolog that we don't care about or some
396 instruction from outside the prolog scheduled here for optimization */
c906108c
SS
397 }
398
399 return current_pc;
400}
401
ed9a39eb
JM
402/* The APCS (ARM Procedure Call Standard) defines the following
403 prologue:
c906108c 404
c5aa993b
JM
405 mov ip, sp
406 [stmfd sp!, {a1,a2,a3,a4}]
407 stmfd sp!, {...,fp,ip,lr,pc}
ed9a39eb
JM
408 [stfe f7, [sp, #-12]!]
409 [stfe f6, [sp, #-12]!]
410 [stfe f5, [sp, #-12]!]
411 [stfe f4, [sp, #-12]!]
412 sub fp, ip, #nn @@ nn == 20 or 4 depending on second insn */
c906108c
SS
413
414CORE_ADDR
ed9a39eb 415arm_skip_prologue (CORE_ADDR pc)
c906108c
SS
416{
417 unsigned long inst;
418 CORE_ADDR skip_pc;
419 CORE_ADDR func_addr, func_end;
420 struct symtab_and_line sal;
421
96baa820 422 /* See what the symbol table says. */
ed9a39eb 423
c5aa993b 424 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
c906108c
SS
425 {
426 sal = find_pc_line (func_addr, 0);
96baa820 427 if ((sal.line != 0) && (sal.end < func_end))
c906108c
SS
428 return sal.end;
429 }
430
431 /* Check if this is Thumb code. */
432 if (arm_pc_is_thumb (pc))
c7885828 433 return thumb_skip_prologue (pc, func_end);
c906108c
SS
434
435 /* Can't find the prologue end in the symbol table, try it the hard way
436 by disassembling the instructions. */
437 skip_pc = pc;
438 inst = read_memory_integer (skip_pc, 4);
c5aa993b 439 if (inst != 0xe1a0c00d) /* mov ip, sp */
c906108c
SS
440 return pc;
441
442 skip_pc += 4;
443 inst = read_memory_integer (skip_pc, 4);
c5aa993b 444 if ((inst & 0xfffffff0) == 0xe92d0000) /* stmfd sp!,{a1,a2,a3,a4} */
c906108c
SS
445 {
446 skip_pc += 4;
447 inst = read_memory_integer (skip_pc, 4);
448 }
449
c5aa993b 450 if ((inst & 0xfffff800) != 0xe92dd800) /* stmfd sp!,{...,fp,ip,lr,pc} */
c906108c
SS
451 return pc;
452
453 skip_pc += 4;
454 inst = read_memory_integer (skip_pc, 4);
455
456 /* Any insns after this point may float into the code, if it makes
ed9a39eb
JM
457 for better instruction scheduling, so we skip them only if we
458 find them, but still consdier the function to be frame-ful. */
c906108c 459
ed9a39eb
JM
460 /* We may have either one sfmfd instruction here, or several stfe
461 insns, depending on the version of floating point code we
462 support. */
c5aa993b 463 if ((inst & 0xffbf0fff) == 0xec2d0200) /* sfmfd fn, <cnt>, [sp]! */
c906108c
SS
464 {
465 skip_pc += 4;
466 inst = read_memory_integer (skip_pc, 4);
467 }
468 else
469 {
c5aa993b
JM
470 while ((inst & 0xffff8fff) == 0xed6d0103) /* stfe fn, [sp, #-12]! */
471 {
472 skip_pc += 4;
473 inst = read_memory_integer (skip_pc, 4);
474 }
c906108c
SS
475 }
476
c5aa993b 477 if ((inst & 0xfffff000) == 0xe24cb000) /* sub fp, ip, #nn */
c906108c
SS
478 skip_pc += 4;
479
480 return skip_pc;
481}
c5aa993b 482/* *INDENT-OFF* */
c906108c
SS
483/* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
484 This function decodes a Thumb function prologue to determine:
485 1) the size of the stack frame
486 2) which registers are saved on it
487 3) the offsets of saved regs
488 4) the offset from the stack pointer to the frame pointer
489 This information is stored in the "extra" fields of the frame_info.
490
da59e081
JM
491 A typical Thumb function prologue would create this stack frame
492 (offsets relative to FP)
c906108c
SS
493 old SP -> 24 stack parameters
494 20 LR
495 16 R7
496 R7 -> 0 local variables (16 bytes)
497 SP -> -12 additional stack space (12 bytes)
498 The frame size would thus be 36 bytes, and the frame offset would be
da59e081
JM
499 12 bytes. The frame register is R7.
500
501 The comments for thumb_skip_prolog() describe the algorithm we use to detect
502 the end of the prolog */
c5aa993b
JM
503/* *INDENT-ON* */
504
c906108c 505static void
ed9a39eb 506thumb_scan_prologue (struct frame_info *fi)
c906108c
SS
507{
508 CORE_ADDR prologue_start;
509 CORE_ADDR prologue_end;
510 CORE_ADDR current_pc;
c5aa993b 511 int saved_reg[16]; /* which register has been copied to register n? */
da59e081
JM
512 int findmask = 0; /* findmask:
513 bit 0 - push { rlist }
514 bit 1 - mov r7, sp OR add r7, sp, #imm (setting of r7)
515 bit 2 - sub sp, #simm OR add sp, #simm (adjusting of sp)
516 */
c5aa993b 517 int i;
c906108c 518
c5aa993b 519 if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
c906108c
SS
520 {
521 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
522
c5aa993b 523 if (sal.line == 0) /* no line info, use current PC */
c906108c
SS
524 prologue_end = fi->pc;
525 else if (sal.end < prologue_end) /* next line begins after fn end */
c5aa993b 526 prologue_end = sal.end; /* (probably means no prologue) */
c906108c
SS
527 }
528 else
c5aa993b
JM
529 prologue_end = prologue_start + 40; /* We're in the boondocks: allow for */
530 /* 16 pushes, an add, and "mv fp,sp" */
c906108c
SS
531
532 prologue_end = min (prologue_end, fi->pc);
533
534 /* Initialize the saved register map. When register H is copied to
535 register L, we will put H in saved_reg[L]. */
536 for (i = 0; i < 16; i++)
537 saved_reg[i] = i;
538
539 /* Search the prologue looking for instructions that set up the
da59e081
JM
540 frame pointer, adjust the stack pointer, and save registers.
541 Do this until all basic prolog instructions are found. */
c906108c
SS
542
543 fi->framesize = 0;
da59e081
JM
544 for (current_pc = prologue_start;
545 (current_pc < prologue_end) && ((findmask & 7) != 7);
546 current_pc += 2)
c906108c
SS
547 {
548 unsigned short insn;
549 int regno;
550 int offset;
551
552 insn = read_memory_unsigned_integer (current_pc, 2);
553
c5aa993b 554 if ((insn & 0xfe00) == 0xb400) /* push { rlist } */
c906108c 555 {
da59e081
JM
556 int mask;
557 findmask |= 1; /* push found */
c906108c
SS
558 /* Bits 0-7 contain a mask for registers R0-R7. Bit 8 says
559 whether to save LR (R14). */
da59e081 560 mask = (insn & 0xff) | ((insn & 0x100) << 6);
c906108c
SS
561
562 /* Calculate offsets of saved R0-R7 and LR. */
563 for (regno = LR_REGNUM; regno >= 0; regno--)
564 if (mask & (1 << regno))
c5aa993b 565 {
c906108c
SS
566 fi->framesize += 4;
567 fi->fsr.regs[saved_reg[regno]] = -(fi->framesize);
568 saved_reg[regno] = regno; /* reset saved register map */
569 }
570 }
da59e081 571 else if ((insn & 0xff00) == 0xb000) /* add sp, #simm OR sub sp, #simm */
c906108c 572 {
da59e081
JM
573 if ((findmask & 1) == 0) /* before push ? */
574 continue;
575 else
576 findmask |= 4; /* add/sub sp found */
577
c5aa993b 578 offset = (insn & 0x7f) << 2; /* get scaled offset */
da59e081
JM
579 if (insn & 0x80) /* is it signed? (==subtracting) */
580 {
581 fi->frameoffset += offset;
582 offset = -offset;
583 }
c906108c
SS
584 fi->framesize -= offset;
585 }
586 else if ((insn & 0xff00) == 0xaf00) /* add r7, sp, #imm */
587 {
da59e081 588 findmask |= 2; /* setting of r7 found */
c906108c 589 fi->framereg = THUMB_FP_REGNUM;
c5aa993b 590 fi->frameoffset = (insn & 0xff) << 2; /* get scaled offset */
c906108c 591 }
da59e081 592 else if (insn == 0x466f) /* mov r7, sp */
c906108c 593 {
da59e081 594 findmask |= 2; /* setting of r7 found */
c906108c
SS
595 fi->framereg = THUMB_FP_REGNUM;
596 fi->frameoffset = 0;
597 saved_reg[THUMB_FP_REGNUM] = SP_REGNUM;
598 }
599 else if ((insn & 0xffc0) == 0x4640) /* mov r0-r7, r8-r15 */
600 {
c5aa993b 601 int lo_reg = insn & 7; /* dest. register (r0-r7) */
c906108c 602 int hi_reg = ((insn >> 3) & 7) + 8; /* source register (r8-15) */
c5aa993b 603 saved_reg[lo_reg] = hi_reg; /* remember hi reg was saved */
c906108c
SS
604 }
605 else
da59e081
JM
606 continue; /* something in the prolog that we don't care about or some
607 instruction from outside the prolog scheduled here for optimization */
c906108c
SS
608 }
609}
610
ed9a39eb
JM
611/* Check if prologue for this frame's PC has already been scanned. If
612 it has, copy the relevant information about that prologue and
c906108c
SS
613 return non-zero. Otherwise do not copy anything and return zero.
614
615 The information saved in the cache includes:
c5aa993b
JM
616 * the frame register number;
617 * the size of the stack frame;
618 * the offsets of saved regs (relative to the old SP); and
619 * the offset from the stack pointer to the frame pointer
c906108c 620
ed9a39eb
JM
621 The cache contains only one entry, since this is adequate for the
622 typical sequence of prologue scan requests we get. When performing
623 a backtrace, GDB will usually ask to scan the same function twice
624 in a row (once to get the frame chain, and once to fill in the
625 extra frame information). */
c906108c
SS
626
627static struct frame_info prologue_cache;
628
629static int
ed9a39eb 630check_prologue_cache (struct frame_info *fi)
c906108c
SS
631{
632 int i;
633
634 if (fi->pc == prologue_cache.pc)
635 {
636 fi->framereg = prologue_cache.framereg;
637 fi->framesize = prologue_cache.framesize;
638 fi->frameoffset = prologue_cache.frameoffset;
911413e6 639 for (i = 0; i < NUM_REGS; i++)
c906108c
SS
640 fi->fsr.regs[i] = prologue_cache.fsr.regs[i];
641 return 1;
642 }
643 else
644 return 0;
645}
646
647
ed9a39eb 648/* Copy the prologue information from fi to the prologue cache. */
c906108c
SS
649
650static void
ed9a39eb 651save_prologue_cache (struct frame_info *fi)
c906108c
SS
652{
653 int i;
654
c5aa993b
JM
655 prologue_cache.pc = fi->pc;
656 prologue_cache.framereg = fi->framereg;
657 prologue_cache.framesize = fi->framesize;
c906108c 658 prologue_cache.frameoffset = fi->frameoffset;
c5aa993b 659
911413e6 660 for (i = 0; i < NUM_REGS; i++)
c906108c
SS
661 prologue_cache.fsr.regs[i] = fi->fsr.regs[i];
662}
663
664
ed9a39eb 665/* This function decodes an ARM function prologue to determine:
c5aa993b
JM
666 1) the size of the stack frame
667 2) which registers are saved on it
668 3) the offsets of saved regs
669 4) the offset from the stack pointer to the frame pointer
c906108c
SS
670 This information is stored in the "extra" fields of the frame_info.
671
96baa820
JM
672 There are two basic forms for the ARM prologue. The fixed argument
673 function call will look like:
ed9a39eb
JM
674
675 mov ip, sp
676 stmfd sp!, {fp, ip, lr, pc}
677 sub fp, ip, #4
678 [sub sp, sp, #4]
96baa820 679
c906108c 680 Which would create this stack frame (offsets relative to FP):
ed9a39eb
JM
681 IP -> 4 (caller's stack)
682 FP -> 0 PC (points to address of stmfd instruction + 8 in callee)
683 -4 LR (return address in caller)
684 -8 IP (copy of caller's SP)
685 -12 FP (caller's FP)
686 SP -> -28 Local variables
687
c906108c 688 The frame size would thus be 32 bytes, and the frame offset would be
96baa820
JM
689 28 bytes. The stmfd call can also save any of the vN registers it
690 plans to use, which increases the frame size accordingly.
691
692 Note: The stored PC is 8 off of the STMFD instruction that stored it
693 because the ARM Store instructions always store PC + 8 when you read
694 the PC register.
ed9a39eb 695
96baa820
JM
696 A variable argument function call will look like:
697
ed9a39eb
JM
698 mov ip, sp
699 stmfd sp!, {a1, a2, a3, a4}
700 stmfd sp!, {fp, ip, lr, pc}
701 sub fp, ip, #20
702
96baa820 703 Which would create this stack frame (offsets relative to FP):
ed9a39eb
JM
704 IP -> 20 (caller's stack)
705 16 A4
706 12 A3
707 8 A2
708 4 A1
709 FP -> 0 PC (points to address of stmfd instruction + 8 in callee)
710 -4 LR (return address in caller)
711 -8 IP (copy of caller's SP)
712 -12 FP (caller's FP)
713 SP -> -28 Local variables
96baa820
JM
714
715 The frame size would thus be 48 bytes, and the frame offset would be
716 28 bytes.
717
718 There is another potential complication, which is that the optimizer
719 will try to separate the store of fp in the "stmfd" instruction from
720 the "sub fp, ip, #NN" instruction. Almost anything can be there, so
721 we just key on the stmfd, and then scan for the "sub fp, ip, #NN"...
722
723 Also, note, the original version of the ARM toolchain claimed that there
724 should be an
725
726 instruction at the end of the prologue. I have never seen GCC produce
727 this, and the ARM docs don't mention it. We still test for it below in
728 case it happens...
ed9a39eb
JM
729
730 */
c906108c
SS
731
732static void
ed9a39eb 733arm_scan_prologue (struct frame_info *fi)
c906108c
SS
734{
735 int regno, sp_offset, fp_offset;
736 CORE_ADDR prologue_start, prologue_end, current_pc;
737
738 /* Check if this function is already in the cache of frame information. */
739 if (check_prologue_cache (fi))
740 return;
741
742 /* Assume there is no frame until proven otherwise. */
c5aa993b
JM
743 fi->framereg = SP_REGNUM;
744 fi->framesize = 0;
c906108c
SS
745 fi->frameoffset = 0;
746
747 /* Check for Thumb prologue. */
748 if (arm_pc_is_thumb (fi->pc))
749 {
750 thumb_scan_prologue (fi);
751 save_prologue_cache (fi);
752 return;
753 }
754
755 /* Find the function prologue. If we can't find the function in
756 the symbol table, peek in the stack frame to find the PC. */
757 if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
758 {
2a451106
KB
759 /* One way to find the end of the prologue (which works well
760 for unoptimized code) is to do the following:
761
762 struct symtab_and_line sal = find_pc_line (prologue_start, 0);
763
764 if (sal.line == 0)
765 prologue_end = fi->pc;
766 else if (sal.end < prologue_end)
767 prologue_end = sal.end;
768
769 This mechanism is very accurate so long as the optimizer
770 doesn't move any instructions from the function body into the
771 prologue. If this happens, sal.end will be the last
772 instruction in the first hunk of prologue code just before
773 the first instruction that the scheduler has moved from
774 the body to the prologue.
775
776 In order to make sure that we scan all of the prologue
777 instructions, we use a slightly less accurate mechanism which
778 may scan more than necessary. To help compensate for this
779 lack of accuracy, the prologue scanning loop below contains
780 several clauses which'll cause the loop to terminate early if
781 an implausible prologue instruction is encountered.
782
783 The expression
784
785 prologue_start + 64
786
787 is a suitable endpoint since it accounts for the largest
788 possible prologue plus up to five instructions inserted by
789 the scheduler. */
790
791 if (prologue_end > prologue_start + 64)
792 {
793 prologue_end = prologue_start + 64; /* See above. */
794 }
c906108c
SS
795 }
796 else
797 {
798 /* Get address of the stmfd in the prologue of the callee; the saved
96baa820 799 PC is the address of the stmfd + 8. */
ed9a39eb 800 prologue_start = ADDR_BITS_REMOVE (read_memory_integer (fi->frame, 4))
96baa820 801 - 8;
2a451106 802 prologue_end = prologue_start + 64; /* See above. */
c906108c
SS
803 }
804
805 /* Now search the prologue looking for instructions that set up the
96baa820 806 frame pointer, adjust the stack pointer, and save registers.
ed9a39eb 807
96baa820
JM
808 Be careful, however, and if it doesn't look like a prologue,
809 don't try to scan it. If, for instance, a frameless function
810 begins with stmfd sp!, then we will tell ourselves there is
811 a frame, which will confuse stack traceback, as well ad"finish"
812 and other operations that rely on a knowledge of the stack
813 traceback.
814
815 In the APCS, the prologue should start with "mov ip, sp" so
816 if we don't see this as the first insn, we will stop. */
c906108c
SS
817
818 sp_offset = fp_offset = 0;
c906108c 819
ed9a39eb
JM
820 if (read_memory_unsigned_integer (prologue_start, 4)
821 == 0xe1a0c00d) /* mov ip, sp */
96baa820 822 {
ed9a39eb 823 for (current_pc = prologue_start + 4; current_pc < prologue_end;
96baa820 824 current_pc += 4)
c906108c 825 {
96baa820 826 unsigned int insn = read_memory_unsigned_integer (current_pc, 4);
ed9a39eb 827
96baa820
JM
828 if ((insn & 0xffff0000) == 0xe92d0000)
829 /* stmfd sp!, {..., fp, ip, lr, pc}
830 or
831 stmfd sp!, {a1, a2, a3, a4} */
832 {
833 int mask = insn & 0xffff;
ed9a39eb 834
96baa820
JM
835 /* Calculate offsets of saved registers. */
836 for (regno = PC_REGNUM; regno >= 0; regno--)
837 if (mask & (1 << regno))
838 {
839 sp_offset -= 4;
840 fi->fsr.regs[regno] = sp_offset;
841 }
842 }
ed9a39eb 843 else if ((insn & 0xfffff000) == 0xe24cb000) /* sub fp, ip #n */
96baa820 844 {
ed9a39eb
JM
845 unsigned imm = insn & 0xff; /* immediate value */
846 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
847 imm = (imm >> rot) | (imm << (32 - rot));
96baa820
JM
848 fp_offset = -imm;
849 fi->framereg = FP_REGNUM;
850 }
ed9a39eb 851 else if ((insn & 0xfffff000) == 0xe24dd000) /* sub sp, sp #n */
96baa820 852 {
ed9a39eb
JM
853 unsigned imm = insn & 0xff; /* immediate value */
854 unsigned rot = (insn & 0xf00) >> 7; /* rotate amount */
855 imm = (imm >> rot) | (imm << (32 - rot));
96baa820
JM
856 sp_offset -= imm;
857 }
ed9a39eb 858 else if ((insn & 0xffff7fff) == 0xed6d0103) /* stfe f?, [sp, -#c]! */
96baa820
JM
859 {
860 sp_offset -= 12;
861 regno = F0_REGNUM + ((insn >> 12) & 0x07);
862 fi->fsr.regs[regno] = sp_offset;
863 }
ed9a39eb 864 else if ((insn & 0xffbf0fff) == 0xec2d0200) /* sfmfd f0, 4, [sp!] */
96baa820 865 {
ed9a39eb 866 int n_saved_fp_regs;
96baa820 867 unsigned int fp_start_reg, fp_bound_reg;
ed9a39eb
JM
868
869 if ((insn & 0x800) == 0x800) /* N0 is set */
870 {
871 if ((insn & 0x40000) == 0x40000) /* N1 is set */
96baa820
JM
872 n_saved_fp_regs = 3;
873 else
874 n_saved_fp_regs = 1;
875 }
876 else
ed9a39eb
JM
877 {
878 if ((insn & 0x40000) == 0x40000) /* N1 is set */
96baa820
JM
879 n_saved_fp_regs = 2;
880 else
881 n_saved_fp_regs = 4;
882 }
ed9a39eb 883
96baa820
JM
884 fp_start_reg = F0_REGNUM + ((insn >> 12) & 0x7);
885 fp_bound_reg = fp_start_reg + n_saved_fp_regs;
886 for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
887 {
888 sp_offset -= 12;
889 fi->fsr.regs[fp_start_reg++] = sp_offset;
890 }
891 }
2a451106
KB
892 else if ((insn & 0xf0000000) != 0xe0000000)
893 break; /* Condition not true, exit early */
894 else if ((insn & 0xfe200000) == 0xe8200000) /* ldm? */
895 break; /* Don't scan past a block load */
96baa820 896 else
ed9a39eb
JM
897 /* The optimizer might shove anything into the prologue,
898 so we just skip what we don't recognize. */
899 continue;
c906108c 900 }
c906108c
SS
901 }
902
903 /* The frame size is just the negative of the offset (from the original SP)
904 of the last thing thing we pushed on the stack. The frame offset is
905 [new FP] - [new SP]. */
906 fi->framesize = -sp_offset;
907 fi->frameoffset = fp_offset - sp_offset;
ed9a39eb 908
c906108c
SS
909 save_prologue_cache (fi);
910}
911
ed9a39eb
JM
912/* Find REGNUM on the stack. Otherwise, it's in an active register.
913 One thing we might want to do here is to check REGNUM against the
914 clobber mask, and somehow flag it as invalid if it isn't saved on
915 the stack somewhere. This would provide a graceful failure mode
916 when trying to get the value of caller-saves registers for an inner
917 frame. */
c906108c
SS
918
919static CORE_ADDR
ed9a39eb 920arm_find_callers_reg (struct frame_info *fi, int regnum)
c906108c
SS
921{
922 for (; fi; fi = fi->next)
c5aa993b
JM
923
924#if 0 /* FIXME: enable this code if we convert to new call dummy scheme. */
c906108c
SS
925 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
926 return generic_read_register_dummy (fi->pc, fi->frame, regnum);
927 else
928#endif
c5aa993b
JM
929 if (fi->fsr.regs[regnum] != 0)
930 return read_memory_integer (fi->fsr.regs[regnum],
931 REGISTER_RAW_SIZE (regnum));
c906108c
SS
932 return read_register (regnum);
933}
c5aa993b 934/* *INDENT-OFF* */
c906108c
SS
935/* Function: frame_chain
936 Given a GDB frame, determine the address of the calling function's frame.
937 This will be used to create a new GDB frame struct, and then
938 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
939 For ARM, we save the frame size when we initialize the frame_info.
940
941 The original definition of this function was a macro in tm-arm.h:
942 { In the case of the ARM, the frame's nominal address is the FP value,
943 and 12 bytes before comes the saved previous FP value as a 4-byte word. }
944
945 #define FRAME_CHAIN(thisframe) \
946 ((thisframe)->pc >= LOWEST_PC ? \
947 read_memory_integer ((thisframe)->frame - 12, 4) :\
948 0)
949*/
c5aa993b
JM
950/* *INDENT-ON* */
951
c906108c 952CORE_ADDR
ed9a39eb 953arm_frame_chain (struct frame_info *fi)
c906108c 954{
c5aa993b 955#if 0 /* FIXME: enable this code if we convert to new call dummy scheme. */
c906108c
SS
956 CORE_ADDR fn_start, callers_pc, fp;
957
958 /* is this a dummy frame? */
959 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
c5aa993b 960 return fi->frame; /* dummy frame same as caller's frame */
c906108c
SS
961
962 /* is caller-of-this a dummy frame? */
c5aa993b 963 callers_pc = FRAME_SAVED_PC (fi); /* find out who called us: */
c906108c 964 fp = arm_find_callers_reg (fi, FP_REGNUM);
c5aa993b
JM
965 if (PC_IN_CALL_DUMMY (callers_pc, fp, fp))
966 return fp; /* dummy frame's frame may bear no relation to ours */
c906108c
SS
967
968 if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
969 if (fn_start == entry_point_address ())
c5aa993b 970 return 0; /* in _start fn, don't chain further */
c906108c
SS
971#endif
972 CORE_ADDR caller_pc, fn_start;
973 struct frame_info caller_fi;
974 int framereg = fi->framereg;
975
976 if (fi->pc < LOWEST_PC)
977 return 0;
978
979 /* If the caller is the startup code, we're at the end of the chain. */
980 caller_pc = FRAME_SAVED_PC (fi);
981 if (find_pc_partial_function (caller_pc, 0, &fn_start, 0))
982 if (fn_start == entry_point_address ())
983 return 0;
984
985 /* If the caller is Thumb and the caller is ARM, or vice versa,
986 the frame register of the caller is different from ours.
987 So we must scan the prologue of the caller to determine its
988 frame register number. */
989 if (arm_pc_is_thumb (caller_pc) != arm_pc_is_thumb (fi->pc))
990 {
c5aa993b 991 memset (&caller_fi, 0, sizeof (caller_fi));
c906108c 992 caller_fi.pc = caller_pc;
c5aa993b 993 arm_scan_prologue (&caller_fi);
c906108c
SS
994 framereg = caller_fi.framereg;
995 }
996
997 /* If the caller used a frame register, return its value.
998 Otherwise, return the caller's stack pointer. */
999 if (framereg == FP_REGNUM || framereg == THUMB_FP_REGNUM)
1000 return arm_find_callers_reg (fi, framereg);
1001 else
1002 return fi->frame + fi->framesize;
1003}
1004
ed9a39eb
JM
1005/* This function actually figures out the frame address for a given pc
1006 and sp. This is tricky because we sometimes don't use an explicit
1007 frame pointer, and the previous stack pointer isn't necessarily
1008 recorded on the stack. The only reliable way to get this info is
1009 to examine the prologue. FROMLEAF is a little confusing, it means
1010 this is the next frame up the chain AFTER a frameless function. If
1011 this is true, then the frame value for this frame is still in the
1012 fp register. */
c906108c
SS
1013
1014void
ed9a39eb 1015arm_init_extra_frame_info (int fromleaf, struct frame_info *fi)
c906108c
SS
1016{
1017 int reg;
1018
1019 if (fi->next)
1020 fi->pc = FRAME_SAVED_PC (fi->next);
1021
1022 memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
1023
c5aa993b 1024#if 0 /* FIXME: enable this code if we convert to new call dummy scheme. */
c906108c
SS
1025 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1026 {
1027 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
c5aa993b
JM
1028 by assuming it's always FP. */
1029 fi->frame = generic_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
1030 fi->framesize = 0;
c906108c
SS
1031 fi->frameoffset = 0;
1032 return;
1033 }
c5aa993b 1034 else
c906108c 1035#endif
2a451106
KB
1036
1037 /* Determine whether or not we're in a sigtramp frame.
1038 Unfortunately, it isn't sufficient to test
1039 fi->signal_handler_caller because this value is sometimes set
1040 after invoking INIT_EXTRA_FRAME_INFO. So we test *both*
1041 fi->signal_handler_caller and IN_SIGTRAMP to determine if we need
1042 to use the sigcontext addresses for the saved registers.
1043
1044 Note: If an ARM IN_SIGTRAMP method ever needs to compare against
1045 the name of the function, the code below will have to be changed
1046 to first fetch the name of the function and then pass this name
1047 to IN_SIGTRAMP. */
1048
3bb04bdd 1049 if (SIGCONTEXT_REGISTER_ADDRESS_P ()
2a451106
KB
1050 && (fi->signal_handler_caller || IN_SIGTRAMP (fi->pc, 0)))
1051 {
1052 CORE_ADDR sp;
1053
1054 if (!fi->next)
1055 sp = read_sp();
1056 else
1057 sp = fi->next->frame - fi->next->frameoffset + fi->next->framesize;
1058
1059 for (reg = 0; reg < NUM_REGS; reg++)
1060 fi->fsr.regs[reg] = SIGCONTEXT_REGISTER_ADDRESS (sp, fi->pc, reg);
1061
1062 /* FIXME: What about thumb mode? */
1063 fi->framereg = SP_REGNUM;
1064 fi->frame = read_memory_integer (fi->fsr.regs[fi->framereg], 4);
1065 fi->framesize = 0;
1066 fi->frameoffset = 0;
1067
1068 }
1069 else
c906108c
SS
1070 {
1071 arm_scan_prologue (fi);
1072
104c1213
JM
1073 if (!fi->next)
1074 /* this is the innermost frame? */
c906108c 1075 fi->frame = read_register (fi->framereg);
ed9a39eb
JM
1076 else if (fi->framereg == FP_REGNUM || fi->framereg == THUMB_FP_REGNUM)
1077 {
1078 /* not the innermost frame */
1079 /* If we have an FP, the callee saved it. */
1080 if (fi->next->fsr.regs[fi->framereg] != 0)
1081 fi->frame =
1082 read_memory_integer (fi->next->fsr.regs[fi->framereg], 4);
1083 else if (fromleaf)
1084 /* If we were called by a frameless fn. then our frame is
1085 still in the frame pointer register on the board... */
1086 fi->frame = read_fp ();
1087 }
c906108c 1088
ed9a39eb
JM
1089 /* Calculate actual addresses of saved registers using offsets
1090 determined by arm_scan_prologue. */
c906108c
SS
1091 for (reg = 0; reg < NUM_REGS; reg++)
1092 if (fi->fsr.regs[reg] != 0)
1093 fi->fsr.regs[reg] += fi->frame + fi->framesize - fi->frameoffset;
1094 }
1095}
1096
1097
ed9a39eb
JM
1098/* Find the caller of this frame. We do this by seeing if LR_REGNUM
1099 is saved in the stack anywhere, otherwise we get it from the
1100 registers.
c906108c
SS
1101
1102 The old definition of this function was a macro:
c5aa993b 1103 #define FRAME_SAVED_PC(FRAME) \
ed9a39eb 1104 ADDR_BITS_REMOVE (read_memory_integer ((FRAME)->frame - 4, 4)) */
c906108c
SS
1105
1106CORE_ADDR
ed9a39eb 1107arm_frame_saved_pc (struct frame_info *fi)
c906108c 1108{
c5aa993b 1109#if 0 /* FIXME: enable this code if we convert to new call dummy scheme. */
c906108c
SS
1110 if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1111 return generic_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
1112 else
1113#endif
1114 {
1115 CORE_ADDR pc = arm_find_callers_reg (fi, LR_REGNUM);
1116 return IS_THUMB_ADDR (pc) ? UNMAKE_THUMB_ADDR (pc) : pc;
1117 }
1118}
1119
c906108c
SS
1120/* Return the frame address. On ARM, it is R11; on Thumb it is R7.
1121 Examine the Program Status Register to decide which state we're in. */
1122
1123CORE_ADDR
ed9a39eb 1124arm_target_read_fp (void)
c906108c
SS
1125{
1126 if (read_register (PS_REGNUM) & 0x20) /* Bit 5 is Thumb state bit */
1127 return read_register (THUMB_FP_REGNUM); /* R7 if Thumb */
1128 else
c5aa993b 1129 return read_register (FP_REGNUM); /* R11 if ARM */
c906108c
SS
1130}
1131
ed9a39eb 1132/* Calculate the frame offsets of the saved registers (ARM version). */
c906108c 1133
c906108c 1134void
ed9a39eb
JM
1135arm_frame_find_saved_regs (struct frame_info *fi,
1136 struct frame_saved_regs *regaddr)
c906108c
SS
1137{
1138 memcpy (regaddr, &fi->fsr, sizeof (struct frame_saved_regs));
1139}
1140
c906108c 1141void
ed9a39eb 1142arm_push_dummy_frame (void)
c906108c
SS
1143{
1144 CORE_ADDR old_sp = read_register (SP_REGNUM);
1145 CORE_ADDR sp = old_sp;
1146 CORE_ADDR fp, prologue_start;
1147 int regnum;
1148
1149 /* Push the two dummy prologue instructions in reverse order,
1150 so that they'll be in the correct low-to-high order in memory. */
1151 /* sub fp, ip, #4 */
1152 sp = push_word (sp, 0xe24cb004);
1153 /* stmdb sp!, {r0-r10, fp, ip, lr, pc} */
1154 prologue_start = sp = push_word (sp, 0xe92ddfff);
1155
ed9a39eb
JM
1156 /* Push a pointer to the dummy prologue + 12, because when stm
1157 instruction stores the PC, it stores the address of the stm
c906108c
SS
1158 instruction itself plus 12. */
1159 fp = sp = push_word (sp, prologue_start + 12);
c5aa993b 1160 sp = push_word (sp, read_register (PC_REGNUM)); /* FIXME: was PS_REGNUM */
c906108c
SS
1161 sp = push_word (sp, old_sp);
1162 sp = push_word (sp, read_register (FP_REGNUM));
c5aa993b
JM
1163
1164 for (regnum = 10; regnum >= 0; regnum--)
c906108c 1165 sp = push_word (sp, read_register (regnum));
c5aa993b 1166
c906108c
SS
1167 write_register (FP_REGNUM, fp);
1168 write_register (THUMB_FP_REGNUM, fp);
1169 write_register (SP_REGNUM, sp);
1170}
1171
1172/* Fix up the call dummy, based on whether the processor is currently
ed9a39eb
JM
1173 in Thumb or ARM mode, and whether the target function is Thumb or
1174 ARM. There are three different situations requiring three
c906108c
SS
1175 different dummies:
1176
1177 * ARM calling ARM: uses the call dummy in tm-arm.h, which has already
c5aa993b 1178 been copied into the dummy parameter to this function.
c906108c 1179 * ARM calling Thumb: uses the call dummy in tm-arm.h, but with the
c5aa993b 1180 "mov pc,r4" instruction patched to be a "bx r4" instead.
c906108c 1181 * Thumb calling anything: uses the Thumb dummy defined below, which
c5aa993b 1182 works for calling both ARM and Thumb functions.
c906108c 1183
ed9a39eb
JM
1184 All three call dummies expect to receive the target function
1185 address in R4, with the low bit set if it's a Thumb function. */
c906108c
SS
1186
1187void
ed9a39eb
JM
1188arm_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
1189 value_ptr *args, struct type *type, int gcc_p)
c906108c
SS
1190{
1191 static short thumb_dummy[4] =
1192 {
c5aa993b
JM
1193 0xf000, 0xf801, /* bl label */
1194 0xdf18, /* swi 24 */
1195 0x4720, /* label: bx r4 */
c906108c
SS
1196 };
1197 static unsigned long arm_bx_r4 = 0xe12fff14; /* bx r4 instruction */
1198
1199 /* Set flag indicating whether the current PC is in a Thumb function. */
c5aa993b 1200 caller_is_thumb = arm_pc_is_thumb (read_pc ());
c906108c 1201
ed9a39eb
JM
1202 /* If the target function is Thumb, set the low bit of the function
1203 address. And if the CPU is currently in ARM mode, patch the
1204 second instruction of call dummy to use a BX instruction to
1205 switch to Thumb mode. */
c906108c
SS
1206 target_is_thumb = arm_pc_is_thumb (fun);
1207 if (target_is_thumb)
1208 {
1209 fun |= 1;
1210 if (!caller_is_thumb)
1211 store_unsigned_integer (dummy + 4, sizeof (arm_bx_r4), arm_bx_r4);
1212 }
1213
1214 /* If the CPU is currently in Thumb mode, use the Thumb call dummy
1215 instead of the ARM one that's already been copied. This will
1216 work for both Thumb and ARM target functions. */
1217 if (caller_is_thumb)
1218 {
1219 int i;
1220 char *p = dummy;
1221 int len = sizeof (thumb_dummy) / sizeof (thumb_dummy[0]);
1222
1223 for (i = 0; i < len; i++)
1224 {
1225 store_unsigned_integer (p, sizeof (thumb_dummy[0]), thumb_dummy[i]);
1226 p += sizeof (thumb_dummy[0]);
1227 }
1228 }
1229
ed9a39eb
JM
1230 /* Put the target address in r4; the call dummy will copy this to
1231 the PC. */
c906108c
SS
1232 write_register (4, fun);
1233}
1234
c906108c 1235/* Return the offset in the call dummy of the instruction that needs
ed9a39eb
JM
1236 to have a breakpoint placed on it. This is the offset of the 'swi
1237 24' instruction, which is no longer actually used, but simply acts
c906108c
SS
1238 as a place-holder now.
1239
ed9a39eb 1240 This implements the CALL_DUMMY_BREAK_OFFSET macro. */
c906108c
SS
1241
1242int
ed9a39eb 1243arm_call_dummy_breakpoint_offset (void)
c906108c
SS
1244{
1245 if (caller_is_thumb)
1246 return 4;
1247 else
1248 return 8;
1249}
1250
ed9a39eb
JM
1251/* Note: ScottB
1252
1253 This function does not support passing parameters using the FPA
1254 variant of the APCS. It passes any floating point arguments in the
1255 general registers and/or on the stack. */
c906108c
SS
1256
1257CORE_ADDR
ed9a39eb
JM
1258arm_push_arguments (int nargs, value_ptr * args, CORE_ADDR sp,
1259 int struct_return, CORE_ADDR struct_addr)
c906108c 1260{
ed9a39eb
JM
1261 char *fp;
1262 int argnum, argreg, nstack_size;
1263
1264 /* Walk through the list of args and determine how large a temporary
1265 stack is required. Need to take care here as structs may be
1266 passed on the stack, and we have to to push them. */
1267 nstack_size = -4 * REGISTER_SIZE; /* Some arguments go into A1-A4. */
1268 if (struct_return) /* The struct address goes in A1. */
1269 nstack_size += REGISTER_SIZE;
1270
1271 /* Walk through the arguments and add their size to nstack_size. */
1272 for (argnum = 0; argnum < nargs; argnum++)
c5aa993b 1273 {
c906108c 1274 int len;
ed9a39eb
JM
1275 struct type *arg_type;
1276
1277 arg_type = check_typedef (VALUE_TYPE (args[argnum]));
1278 len = TYPE_LENGTH (arg_type);
c906108c 1279
ed9a39eb
JM
1280 /* ANSI C code passes float arguments as integers, K&R code
1281 passes float arguments as doubles. Correct for this here. */
1282 if (TYPE_CODE_FLT == TYPE_CODE (arg_type) && REGISTER_SIZE == len)
1283 nstack_size += FP_REGISTER_VIRTUAL_SIZE;
1284 else
1285 nstack_size += len;
1286 }
c906108c 1287
ed9a39eb
JM
1288 /* Allocate room on the stack, and initialize our stack frame
1289 pointer. */
1290 fp = NULL;
1291 if (nstack_size > 0)
1292 {
1293 sp -= nstack_size;
1294 fp = (char *) sp;
1295 }
1296
1297 /* Initialize the integer argument register pointer. */
c906108c 1298 argreg = A1_REGNUM;
c906108c 1299
ed9a39eb
JM
1300 /* The struct_return pointer occupies the first parameter passing
1301 register. */
c906108c 1302 if (struct_return)
c5aa993b 1303 write_register (argreg++, struct_addr);
c906108c 1304
ed9a39eb
JM
1305 /* Process arguments from left to right. Store as many as allowed
1306 in the parameter passing registers (A1-A4), and save the rest on
1307 the temporary stack. */
c5aa993b 1308 for (argnum = 0; argnum < nargs; argnum++)
c906108c 1309 {
ed9a39eb 1310 int len;
c5aa993b 1311 char *val;
ed9a39eb 1312 double dbl_arg;
c5aa993b 1313 CORE_ADDR regval;
ed9a39eb
JM
1314 enum type_code typecode;
1315 struct type *arg_type, *target_type;
1316
1317 arg_type = check_typedef (VALUE_TYPE (args[argnum]));
1318 target_type = TYPE_TARGET_TYPE (arg_type);
1319 len = TYPE_LENGTH (arg_type);
1320 typecode = TYPE_CODE (arg_type);
1321 val = (char *) VALUE_CONTENTS (args[argnum]);
1322
1323 /* ANSI C code passes float arguments as integers, K&R code
1324 passes float arguments as doubles. The .stabs record for
1325 for ANSI prototype floating point arguments records the
1326 type as FP_INTEGER, while a K&R style (no prototype)
1327 .stabs records the type as FP_FLOAT. In this latter case
1328 the compiler converts the float arguments to double before
1329 calling the function. */
1330 if (TYPE_CODE_FLT == typecode && REGISTER_SIZE == len)
1331 {
8b93c638
JM
1332 float f;
1333 double d;
1334 char * bufo = (char *) &d;
1335 char * bufd = (char *) &dbl_arg;
1336
ed9a39eb 1337 len = sizeof (double);
8b93c638
JM
1338 f = *(float *) val;
1339 SWAP_TARGET_AND_HOST (&f, sizeof (float)); /* adjust endianess */
1340 d = f;
1341 /* We must revert the longwords so they get loaded into the
1342 the right registers. */
1343 memcpy (bufd, bufo + len / 2, len / 2);
1344 SWAP_TARGET_AND_HOST (bufd, len / 2); /* adjust endianess */
1345 memcpy (bufd + len / 2, bufo, len / 2);
1346 SWAP_TARGET_AND_HOST (bufd + len / 2, len / 2); /* adjust endianess */
1347 val = (char *) &dbl_arg;
ed9a39eb 1348 }
da59e081
JM
1349#if 1
1350 /* I don't know why this code was disable. The only logical use
1351 for a function pointer is to call that function, so setting
1352 the mode bit is perfectly fine. FN */
ed9a39eb 1353 /* If the argument is a pointer to a function, and it is a Thumb
c906108c 1354 function, set the low bit of the pointer. */
ed9a39eb
JM
1355 if (TYPE_CODE_PTR == typecode
1356 && NULL != target_type
1357 && TYPE_CODE_FUNC == TYPE_CODE (target_type))
c906108c 1358 {
ed9a39eb 1359 CORE_ADDR regval = extract_address (val, len);
c906108c
SS
1360 if (arm_pc_is_thumb (regval))
1361 store_address (val, len, MAKE_THUMB_ADDR (regval));
1362 }
c906108c 1363#endif
ed9a39eb
JM
1364 /* Copy the argument to general registers or the stack in
1365 register-sized pieces. Large arguments are split between
1366 registers and stack. */
1367 while (len > 0)
c906108c 1368 {
ed9a39eb
JM
1369 int partial_len = len < REGISTER_SIZE ? len : REGISTER_SIZE;
1370
1371 if (argreg <= ARM_LAST_ARG_REGNUM)
c906108c 1372 {
ed9a39eb
JM
1373 /* It's an argument being passed in a general register. */
1374 regval = extract_address (val, partial_len);
1375 write_register (argreg++, regval);
c906108c 1376 }
ed9a39eb
JM
1377 else
1378 {
1379 /* Push the arguments onto the stack. */
1380 write_memory ((CORE_ADDR) fp, val, REGISTER_SIZE);
1381 fp += REGISTER_SIZE;
1382 }
1383
1384 len -= partial_len;
1385 val += partial_len;
c906108c
SS
1386 }
1387 }
c906108c
SS
1388
1389 /* Return adjusted stack pointer. */
1390 return sp;
1391}
1392
1393void
ed9a39eb 1394arm_pop_frame (void)
c906108c 1395{
c906108c 1396 int regnum;
8b93c638 1397 struct frame_info *frame = get_current_frame ();
c906108c 1398
8b93c638
JM
1399 if (!PC_IN_CALL_DUMMY(frame->pc, frame->frame, read_fp()))
1400 {
1401 CORE_ADDR old_SP;
1402
1403 old_SP = read_register (frame->framereg);
1404 for (regnum = 0; regnum < NUM_REGS; regnum++)
1405 if (frame->fsr.regs[regnum] != 0)
1406 write_register (regnum,
c906108c
SS
1407 read_memory_integer (frame->fsr.regs[regnum], 4));
1408
8b93c638
JM
1409 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
1410 write_register (SP_REGNUM, old_SP);
1411 }
1412 else
1413 {
1414 CORE_ADDR sp;
1415
1416 sp = read_register (FP_REGNUM);
1417 sp -= sizeof(CORE_ADDR); /* we don't care about this first word */
1418
1419 write_register (PC_REGNUM, read_memory_integer (sp, 4));
1420 sp -= sizeof(CORE_ADDR);
1421 write_register (SP_REGNUM, read_memory_integer (sp, 4));
1422 sp -= sizeof(CORE_ADDR);
1423 write_register (FP_REGNUM, read_memory_integer (sp, 4));
1424 sp -= sizeof(CORE_ADDR);
1425
1426 for (regnum = 10; regnum >= 0; regnum--)
1427 {
1428 write_register (regnum, read_memory_integer (sp, 4));
1429 sp -= sizeof(CORE_ADDR);
1430 }
1431 }
c906108c
SS
1432
1433 flush_cached_frames ();
1434}
1435
1436static void
ed9a39eb 1437print_fpu_flags (int flags)
c906108c 1438{
c5aa993b
JM
1439 if (flags & (1 << 0))
1440 fputs ("IVO ", stdout);
1441 if (flags & (1 << 1))
1442 fputs ("DVZ ", stdout);
1443 if (flags & (1 << 2))
1444 fputs ("OFL ", stdout);
1445 if (flags & (1 << 3))
1446 fputs ("UFL ", stdout);
1447 if (flags & (1 << 4))
1448 fputs ("INX ", stdout);
1449 putchar ('\n');
c906108c
SS
1450}
1451
1452void
ed9a39eb 1453arm_float_info (void)
c906108c 1454{
c5aa993b
JM
1455 register unsigned long status = read_register (FPS_REGNUM);
1456 int type;
1457
1458 type = (status >> 24) & 127;
1459 printf ("%s FPU type %d\n",
ed9a39eb 1460 (status & (1 << 31)) ? "Hardware" : "Software",
c5aa993b
JM
1461 type);
1462 fputs ("mask: ", stdout);
1463 print_fpu_flags (status >> 16);
1464 fputs ("flags: ", stdout);
1465 print_fpu_flags (status);
c906108c
SS
1466}
1467
ed9a39eb
JM
1468#if 0
1469/* FIXME: The generated assembler works but sucks. Instead of using
1470 r0, r1 it pushes them on the stack, then loads them into r3, r4 and
1471 uses those registers. I must be missing something. ScottB */
c906108c 1472
c5aa993b 1473void
ed9a39eb
JM
1474convert_from_extended (void *ptr, void *dbl)
1475{
1476 __asm__ ("
1477 ldfe f0,[%0]
1478 stfd f0,[%1] "
1479: /* no output */
1480: "r" (ptr), "r" (dbl));
1481}
1482
1483void
1484convert_to_extended (void *dbl, void *ptr)
1485{
1486 __asm__ ("
1487 ldfd f0,[%0]
1488 stfe f0,[%1] "
1489: /* no output */
1490: "r" (dbl), "r" (ptr));
1491}
1492#else
1493static void
1494convert_from_extended (void *ptr, void *dbl)
c906108c 1495{
104c1213 1496 *(double *) dbl = *(double *) ptr;
c906108c
SS
1497}
1498
c5aa993b 1499void
ed9a39eb 1500convert_to_extended (void *dbl, void *ptr)
c906108c 1501{
104c1213 1502 *(double *) ptr = *(double *) dbl;
c906108c 1503}
ed9a39eb
JM
1504#endif
1505
1506/* Nonzero if register N requires conversion from raw format to
1507 virtual format. */
1508
1509int
1510arm_register_convertible (unsigned int regnum)
1511{
1512 return ((regnum - F0_REGNUM) < 8);
1513}
1514
1515/* Convert data from raw format for register REGNUM in buffer FROM to
1516 virtual format with type TYPE in buffer TO. */
1517
1518void
1519arm_register_convert_to_virtual (unsigned int regnum, struct type *type,
1520 void *from, void *to)
1521{
1522 double val;
1523
1524 convert_from_extended (from, &val);
1525 store_floating (to, TYPE_LENGTH (type), val);
1526}
1527
1528/* Convert data from virtual format with type TYPE in buffer FROM to
1529 raw format for register REGNUM in buffer TO. */
1530
1531void
1532arm_register_convert_to_raw (unsigned int regnum, struct type *type,
1533 void *from, void *to)
1534{
1535 double val = extract_floating (from, TYPE_LENGTH (type));
1536
1537 convert_to_extended (&val, to);
1538}
c906108c
SS
1539
1540static int
ed9a39eb 1541condition_true (unsigned long cond, unsigned long status_reg)
c906108c
SS
1542{
1543 if (cond == INST_AL || cond == INST_NV)
1544 return 1;
1545
1546 switch (cond)
1547 {
1548 case INST_EQ:
1549 return ((status_reg & FLAG_Z) != 0);
1550 case INST_NE:
1551 return ((status_reg & FLAG_Z) == 0);
1552 case INST_CS:
1553 return ((status_reg & FLAG_C) != 0);
1554 case INST_CC:
1555 return ((status_reg & FLAG_C) == 0);
1556 case INST_MI:
1557 return ((status_reg & FLAG_N) != 0);
1558 case INST_PL:
1559 return ((status_reg & FLAG_N) == 0);
1560 case INST_VS:
1561 return ((status_reg & FLAG_V) != 0);
1562 case INST_VC:
1563 return ((status_reg & FLAG_V) == 0);
1564 case INST_HI:
1565 return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
1566 case INST_LS:
1567 return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
1568 case INST_GE:
1569 return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
1570 case INST_LT:
1571 return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
1572 case INST_GT:
1573 return (((status_reg & FLAG_Z) == 0) &&
ed9a39eb 1574 (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0)));
c906108c
SS
1575 case INST_LE:
1576 return (((status_reg & FLAG_Z) != 0) ||
ed9a39eb 1577 (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0)));
c906108c
SS
1578 }
1579 return 1;
1580}
1581
1582#define submask(x) ((1L << ((x) + 1)) - 1)
1583#define bit(obj,st) (((obj) >> (st)) & 1)
1584#define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
1585#define sbits(obj,st,fn) \
1586 ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
1587#define BranchDest(addr,instr) \
1588 ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
1589#define ARM_PC_32 1
1590
1591static unsigned long
ed9a39eb
JM
1592shifted_reg_val (unsigned long inst, int carry, unsigned long pc_val,
1593 unsigned long status_reg)
c906108c
SS
1594{
1595 unsigned long res, shift;
1596 int rm = bits (inst, 0, 3);
1597 unsigned long shifttype = bits (inst, 5, 6);
c5aa993b
JM
1598
1599 if (bit (inst, 4))
c906108c
SS
1600 {
1601 int rs = bits (inst, 8, 11);
1602 shift = (rs == 15 ? pc_val + 8 : read_register (rs)) & 0xFF;
1603 }
1604 else
1605 shift = bits (inst, 7, 11);
c5aa993b
JM
1606
1607 res = (rm == 15
c906108c 1608 ? ((pc_val | (ARM_PC_32 ? 0 : status_reg))
c5aa993b 1609 + (bit (inst, 4) ? 12 : 8))
c906108c
SS
1610 : read_register (rm));
1611
1612 switch (shifttype)
1613 {
c5aa993b 1614 case 0: /* LSL */
c906108c
SS
1615 res = shift >= 32 ? 0 : res << shift;
1616 break;
c5aa993b
JM
1617
1618 case 1: /* LSR */
c906108c
SS
1619 res = shift >= 32 ? 0 : res >> shift;
1620 break;
1621
c5aa993b
JM
1622 case 2: /* ASR */
1623 if (shift >= 32)
1624 shift = 31;
c906108c
SS
1625 res = ((res & 0x80000000L)
1626 ? ~((~res) >> shift) : res >> shift);
1627 break;
1628
c5aa993b 1629 case 3: /* ROR/RRX */
c906108c
SS
1630 shift &= 31;
1631 if (shift == 0)
1632 res = (res >> 1) | (carry ? 0x80000000L : 0);
1633 else
c5aa993b 1634 res = (res >> shift) | (res << (32 - shift));
c906108c
SS
1635 break;
1636 }
1637
1638 return res & 0xffffffff;
1639}
1640
c906108c
SS
1641/* Return number of 1-bits in VAL. */
1642
1643static int
ed9a39eb 1644bitcount (unsigned long val)
c906108c
SS
1645{
1646 int nbits;
1647 for (nbits = 0; val != 0; nbits++)
c5aa993b 1648 val &= val - 1; /* delete rightmost 1-bit in val */
c906108c
SS
1649 return nbits;
1650}
1651
c906108c 1652static CORE_ADDR
ed9a39eb 1653thumb_get_next_pc (CORE_ADDR pc)
c906108c 1654{
c5aa993b 1655 unsigned long pc_val = ((unsigned long) pc) + 4; /* PC after prefetch */
c906108c 1656 unsigned short inst1 = read_memory_integer (pc, 2);
c5aa993b 1657 CORE_ADDR nextpc = pc + 2; /* default is next instruction */
c906108c
SS
1658 unsigned long offset;
1659
1660 if ((inst1 & 0xff00) == 0xbd00) /* pop {rlist, pc} */
1661 {
1662 CORE_ADDR sp;
1663
1664 /* Fetch the saved PC from the stack. It's stored above
1665 all of the other registers. */
1666 offset = bitcount (bits (inst1, 0, 7)) * REGISTER_SIZE;
1667 sp = read_register (SP_REGNUM);
1668 nextpc = (CORE_ADDR) read_memory_integer (sp + offset, 4);
1669 nextpc = ADDR_BITS_REMOVE (nextpc);
1670 if (nextpc == pc)
1671 error ("Infinite loop detected");
1672 }
1673 else if ((inst1 & 0xf000) == 0xd000) /* conditional branch */
1674 {
1675 unsigned long status = read_register (PS_REGNUM);
c5aa993b 1676 unsigned long cond = bits (inst1, 8, 11);
c906108c
SS
1677 if (cond != 0x0f && condition_true (cond, status)) /* 0x0f = SWI */
1678 nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
1679 }
1680 else if ((inst1 & 0xf800) == 0xe000) /* unconditional branch */
1681 {
1682 nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
1683 }
1684 else if ((inst1 & 0xf800) == 0xf000) /* long branch with link */
1685 {
1686 unsigned short inst2 = read_memory_integer (pc + 2, 2);
c5aa993b 1687 offset = (sbits (inst1, 0, 10) << 12) + (bits (inst2, 0, 10) << 1);
c906108c
SS
1688 nextpc = pc_val + offset;
1689 }
1690
1691 return nextpc;
1692}
1693
c906108c 1694CORE_ADDR
ed9a39eb 1695arm_get_next_pc (CORE_ADDR pc)
c906108c
SS
1696{
1697 unsigned long pc_val;
1698 unsigned long this_instr;
1699 unsigned long status;
1700 CORE_ADDR nextpc;
1701
1702 if (arm_pc_is_thumb (pc))
1703 return thumb_get_next_pc (pc);
1704
1705 pc_val = (unsigned long) pc;
1706 this_instr = read_memory_integer (pc, 4);
1707 status = read_register (PS_REGNUM);
c5aa993b 1708 nextpc = (CORE_ADDR) (pc_val + 4); /* Default case */
c906108c
SS
1709
1710 if (condition_true (bits (this_instr, 28, 31), status))
1711 {
1712 switch (bits (this_instr, 24, 27))
1713 {
c5aa993b
JM
1714 case 0x0:
1715 case 0x1: /* data processing */
1716 case 0x2:
1717 case 0x3:
c906108c
SS
1718 {
1719 unsigned long operand1, operand2, result = 0;
1720 unsigned long rn;
1721 int c;
c5aa993b 1722
c906108c
SS
1723 if (bits (this_instr, 12, 15) != 15)
1724 break;
1725
1726 if (bits (this_instr, 22, 25) == 0
c5aa993b 1727 && bits (this_instr, 4, 7) == 9) /* multiply */
c906108c
SS
1728 error ("Illegal update to pc in instruction");
1729
1730 /* Multiply into PC */
1731 c = (status & FLAG_C) ? 1 : 0;
1732 rn = bits (this_instr, 16, 19);
1733 operand1 = (rn == 15) ? pc_val + 8 : read_register (rn);
c5aa993b 1734
c906108c
SS
1735 if (bit (this_instr, 25))
1736 {
1737 unsigned long immval = bits (this_instr, 0, 7);
1738 unsigned long rotate = 2 * bits (this_instr, 8, 11);
c5aa993b
JM
1739 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
1740 & 0xffffffff;
c906108c 1741 }
c5aa993b 1742 else /* operand 2 is a shifted register */
c906108c 1743 operand2 = shifted_reg_val (this_instr, c, pc_val, status);
c5aa993b 1744
c906108c
SS
1745 switch (bits (this_instr, 21, 24))
1746 {
c5aa993b 1747 case 0x0: /*and */
c906108c
SS
1748 result = operand1 & operand2;
1749 break;
1750
c5aa993b 1751 case 0x1: /*eor */
c906108c
SS
1752 result = operand1 ^ operand2;
1753 break;
1754
c5aa993b 1755 case 0x2: /*sub */
c906108c
SS
1756 result = operand1 - operand2;
1757 break;
1758
c5aa993b 1759 case 0x3: /*rsb */
c906108c
SS
1760 result = operand2 - operand1;
1761 break;
1762
c5aa993b 1763 case 0x4: /*add */
c906108c
SS
1764 result = operand1 + operand2;
1765 break;
1766
c5aa993b 1767 case 0x5: /*adc */
c906108c
SS
1768 result = operand1 + operand2 + c;
1769 break;
1770
c5aa993b 1771 case 0x6: /*sbc */
c906108c
SS
1772 result = operand1 - operand2 + c;
1773 break;
1774
c5aa993b 1775 case 0x7: /*rsc */
c906108c
SS
1776 result = operand2 - operand1 + c;
1777 break;
1778
c5aa993b
JM
1779 case 0x8:
1780 case 0x9:
1781 case 0xa:
1782 case 0xb: /* tst, teq, cmp, cmn */
c906108c
SS
1783 result = (unsigned long) nextpc;
1784 break;
1785
c5aa993b 1786 case 0xc: /*orr */
c906108c
SS
1787 result = operand1 | operand2;
1788 break;
1789
c5aa993b 1790 case 0xd: /*mov */
c906108c
SS
1791 /* Always step into a function. */
1792 result = operand2;
c5aa993b 1793 break;
c906108c 1794
c5aa993b 1795 case 0xe: /*bic */
c906108c
SS
1796 result = operand1 & ~operand2;
1797 break;
1798
c5aa993b 1799 case 0xf: /*mvn */
c906108c
SS
1800 result = ~operand2;
1801 break;
1802 }
1803 nextpc = (CORE_ADDR) ADDR_BITS_REMOVE (result);
1804
1805 if (nextpc == pc)
1806 error ("Infinite loop detected");
1807 break;
1808 }
c5aa993b
JM
1809
1810 case 0x4:
1811 case 0x5: /* data transfer */
1812 case 0x6:
1813 case 0x7:
c906108c
SS
1814 if (bit (this_instr, 20))
1815 {
1816 /* load */
1817 if (bits (this_instr, 12, 15) == 15)
1818 {
1819 /* rd == pc */
c5aa993b 1820 unsigned long rn;
c906108c 1821 unsigned long base;
c5aa993b 1822
c906108c
SS
1823 if (bit (this_instr, 22))
1824 error ("Illegal update to pc in instruction");
1825
1826 /* byte write to PC */
1827 rn = bits (this_instr, 16, 19);
1828 base = (rn == 15) ? pc_val + 8 : read_register (rn);
1829 if (bit (this_instr, 24))
1830 {
1831 /* pre-indexed */
1832 int c = (status & FLAG_C) ? 1 : 0;
1833 unsigned long offset =
c5aa993b 1834 (bit (this_instr, 25)
ed9a39eb 1835 ? shifted_reg_val (this_instr, c, pc_val, status)
c5aa993b 1836 : bits (this_instr, 0, 11));
c906108c
SS
1837
1838 if (bit (this_instr, 23))
1839 base += offset;
1840 else
1841 base -= offset;
1842 }
c5aa993b 1843 nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) base,
c906108c 1844 4);
c5aa993b 1845
c906108c
SS
1846 nextpc = ADDR_BITS_REMOVE (nextpc);
1847
1848 if (nextpc == pc)
1849 error ("Infinite loop detected");
1850 }
1851 }
1852 break;
c5aa993b
JM
1853
1854 case 0x8:
1855 case 0x9: /* block transfer */
c906108c
SS
1856 if (bit (this_instr, 20))
1857 {
1858 /* LDM */
1859 if (bit (this_instr, 15))
1860 {
1861 /* loading pc */
1862 int offset = 0;
1863
1864 if (bit (this_instr, 23))
1865 {
1866 /* up */
1867 unsigned long reglist = bits (this_instr, 0, 14);
1868 offset = bitcount (reglist) * 4;
c5aa993b 1869 if (bit (this_instr, 24)) /* pre */
c906108c
SS
1870 offset += 4;
1871 }
1872 else if (bit (this_instr, 24))
1873 offset = -4;
c5aa993b 1874
c906108c 1875 {
c5aa993b
JM
1876 unsigned long rn_val =
1877 read_register (bits (this_instr, 16, 19));
c906108c
SS
1878 nextpc =
1879 (CORE_ADDR) read_memory_integer ((CORE_ADDR) (rn_val
c5aa993b 1880 + offset),
c906108c
SS
1881 4);
1882 }
1883 nextpc = ADDR_BITS_REMOVE (nextpc);
1884 if (nextpc == pc)
1885 error ("Infinite loop detected");
1886 }
1887 }
1888 break;
c5aa993b
JM
1889
1890 case 0xb: /* branch & link */
1891 case 0xa: /* branch */
c906108c
SS
1892 {
1893 nextpc = BranchDest (pc, this_instr);
1894
1895 nextpc = ADDR_BITS_REMOVE (nextpc);
1896 if (nextpc == pc)
1897 error ("Infinite loop detected");
1898 break;
1899 }
c5aa993b
JM
1900
1901 case 0xc:
1902 case 0xd:
1903 case 0xe: /* coproc ops */
1904 case 0xf: /* SWI */
c906108c
SS
1905 break;
1906
1907 default:
1908 fprintf (stderr, "Bad bit-field extraction\n");
1909 return (pc);
1910 }
1911 }
1912
1913 return nextpc;
1914}
1915
1916#include "bfd-in2.h"
1917#include "libcoff.h"
1918
1919static int
ed9a39eb 1920gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
c906108c
SS
1921{
1922 if (arm_pc_is_thumb (memaddr))
1923 {
c5aa993b
JM
1924 static asymbol *asym;
1925 static combined_entry_type ce;
1926 static struct coff_symbol_struct csym;
1927 static struct _bfd fake_bfd;
1928 static bfd_target fake_target;
c906108c
SS
1929
1930 if (csym.native == NULL)
1931 {
1932 /* Create a fake symbol vector containing a Thumb symbol. This is
1933 solely so that the code in print_insn_little_arm() and
1934 print_insn_big_arm() in opcodes/arm-dis.c will detect the presence
1935 of a Thumb symbol and switch to decoding Thumb instructions. */
c5aa993b
JM
1936
1937 fake_target.flavour = bfd_target_coff_flavour;
1938 fake_bfd.xvec = &fake_target;
c906108c 1939 ce.u.syment.n_sclass = C_THUMBEXTFUNC;
c5aa993b
JM
1940 csym.native = &ce;
1941 csym.symbol.the_bfd = &fake_bfd;
1942 csym.symbol.name = "fake";
1943 asym = (asymbol *) & csym;
c906108c 1944 }
c5aa993b 1945
c906108c 1946 memaddr = UNMAKE_THUMB_ADDR (memaddr);
c5aa993b 1947 info->symbols = &asym;
c906108c
SS
1948 }
1949 else
1950 info->symbols = NULL;
c5aa993b 1951
c906108c
SS
1952 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1953 return print_insn_big_arm (memaddr, info);
1954 else
1955 return print_insn_little_arm (memaddr, info);
1956}
1957
ed9a39eb
JM
1958/* This function implements the BREAKPOINT_FROM_PC macro. It uses the
1959 program counter value to determine whether a 16-bit or 32-bit
1960 breakpoint should be used. It returns a pointer to a string of
1961 bytes that encode a breakpoint instruction, stores the length of
1962 the string to *lenptr, and adjusts the program counter (if
1963 necessary) to point to the actual memory location where the
c906108c
SS
1964 breakpoint should be inserted. */
1965
1966unsigned char *
ed9a39eb 1967arm_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
c906108c
SS
1968{
1969 if (arm_pc_is_thumb (*pcptr) || arm_pc_is_thumb_dummy (*pcptr))
1970 {
1971 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
c5aa993b
JM
1972 {
1973 static char thumb_breakpoint[] = THUMB_BE_BREAKPOINT;
1974 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
1975 *lenptr = sizeof (thumb_breakpoint);
1976 return thumb_breakpoint;
1977 }
c906108c 1978 else
c5aa993b
JM
1979 {
1980 static char thumb_breakpoint[] = THUMB_LE_BREAKPOINT;
1981 *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
1982 *lenptr = sizeof (thumb_breakpoint);
1983 return thumb_breakpoint;
1984 }
c906108c
SS
1985 }
1986 else
1987 {
1988 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
c5aa993b
JM
1989 {
1990 static char arm_breakpoint[] = ARM_BE_BREAKPOINT;
1991 *lenptr = sizeof (arm_breakpoint);
1992 return arm_breakpoint;
1993 }
c906108c 1994 else
c5aa993b
JM
1995 {
1996 static char arm_breakpoint[] = ARM_LE_BREAKPOINT;
1997 *lenptr = sizeof (arm_breakpoint);
1998 return arm_breakpoint;
1999 }
c906108c
SS
2000 }
2001}
ed9a39eb
JM
2002
2003/* Extract from an array REGBUF containing the (raw) register state a
2004 function return value of type TYPE, and copy that, in virtual
2005 format, into VALBUF. */
2006
2007void
2008arm_extract_return_value (struct type *type,
2009 char regbuf[REGISTER_BYTES],
2010 char *valbuf)
2011{
2012 if (TYPE_CODE_FLT == TYPE_CODE (type))
2013 convert_from_extended (&regbuf[REGISTER_BYTE (F0_REGNUM)], valbuf);
2014 else
2015 memcpy (valbuf, &regbuf[REGISTER_BYTE (A1_REGNUM)], TYPE_LENGTH (type));
2016}
2017
2018/* Return non-zero if the PC is inside a thumb call thunk. */
c906108c
SS
2019
2020int
ed9a39eb 2021arm_in_call_stub (CORE_ADDR pc, char *name)
c906108c
SS
2022{
2023 CORE_ADDR start_addr;
2024
ed9a39eb
JM
2025 /* Find the starting address of the function containing the PC. If
2026 the caller didn't give us a name, look it up at the same time. */
c906108c
SS
2027 if (find_pc_partial_function (pc, name ? NULL : &name, &start_addr, NULL) == 0)
2028 return 0;
2029
2030 return strncmp (name, "_call_via_r", 11) == 0;
2031}
2032
ed9a39eb
JM
2033/* If PC is in a Thumb call or return stub, return the address of the
2034 target PC, which is in a register. The thunk functions are called
2035 _called_via_xx, where x is the register name. The possible names
2036 are r0-r9, sl, fp, ip, sp, and lr. */
c906108c
SS
2037
2038CORE_ADDR
ed9a39eb 2039arm_skip_stub (CORE_ADDR pc)
c906108c 2040{
c5aa993b 2041 char *name;
c906108c
SS
2042 CORE_ADDR start_addr;
2043
2044 /* Find the starting address and name of the function containing the PC. */
2045 if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
2046 return 0;
2047
2048 /* Call thunks always start with "_call_via_". */
2049 if (strncmp (name, "_call_via_", 10) == 0)
2050 {
ed9a39eb
JM
2051 /* Use the name suffix to determine which register contains the
2052 target PC. */
c5aa993b
JM
2053 static char *table[15] =
2054 {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
2055 "r8", "r9", "sl", "fp", "ip", "sp", "lr"
2056 };
c906108c
SS
2057 int regno;
2058
2059 for (regno = 0; regno <= 14; regno++)
2060 if (strcmp (&name[10], table[regno]) == 0)
2061 return read_register (regno);
2062 }
ed9a39eb 2063
c5aa993b 2064 return 0; /* not a stub */
c906108c
SS
2065}
2066
bc90b915
FN
2067/* If the user changes the register disassembly flavor used for info register
2068 and other commands, we have to also switch the flavor used in opcodes
2069 for disassembly output.
2070 This function is run in the set disassembly_flavor command, and does that. */
2071
2072static void
2073set_disassembly_flavor_sfunc (char *args, int from_tty,
2074 struct cmd_list_element *c)
2075{
2076 set_disassembly_flavor ();
2077}
2078\f
2079static void
2080set_disassembly_flavor (void)
2081{
2082 const char *setname, *setdesc, **regnames;
2083 int numregs, j;
2084
2085 /* Find the flavor that the user wants in the opcodes table. */
2086 int current = 0;
2087 numregs = get_arm_regnames (current, &setname, &setdesc, &regnames);
2088 while ((disassembly_flavor != setname)
2089 && (current < num_flavor_options))
2090 get_arm_regnames (++current, &setname, &setdesc, &regnames);
2091 current_option = current;
2092
2093 /* Fill our copy. */
2094 for (j = 0; j < numregs; j++)
2095 arm_register_names[j] = (char *) regnames[j];
2096
2097 /* Adjust case. */
2098 if (isupper (*regnames[PC_REGNUM]))
2099 {
2100 arm_register_names[FPS_REGNUM] = "FPS";
2101 arm_register_names[PS_REGNUM] = "CPSR";
2102 }
2103 else
2104 {
2105 arm_register_names[FPS_REGNUM] = "fps";
2106 arm_register_names[PS_REGNUM] = "cpsr";
2107 }
2108
2109 /* Synchronize the disassembler. */
2110 set_arm_regname_option (current);
2111}
2112
2113/* arm_othernames implements the "othernames" command. This is kind
2114 of hacky, and I prefer the set-show disassembly-flavor which is
2115 also used for the x86 gdb. I will keep this around, however, in
2116 case anyone is actually using it. */
2117
2118static void
2119arm_othernames (char *names, int n)
2120{
2121 /* Circle through the various flavors. */
2122 current_option = (current_option + 1) % num_flavor_options;
2123
2124 disassembly_flavor = valid_flavors[current_option];
2125 set_disassembly_flavor ();
2126}
2127
c906108c 2128void
ed9a39eb 2129_initialize_arm_tdep (void)
c906108c 2130{
bc90b915
FN
2131 struct ui_file *stb;
2132 long length;
96baa820 2133 struct cmd_list_element *new_cmd;
53904c9e
AC
2134 const char *setname;
2135 const char *setdesc;
2136 const char **regnames;
bc90b915
FN
2137 int numregs, i, j;
2138 static char *helptext;
085dd6e6 2139
c906108c 2140 tm_print_insn = gdb_print_insn_arm;
ed9a39eb 2141
bc90b915
FN
2142 /* Get the number of possible sets of register names defined in opcodes. */
2143 num_flavor_options = get_arm_regname_num_options ();
2144
085dd6e6 2145 /* Sync the opcode insn printer with our register viewer: */
bc90b915 2146 parse_arm_disassembler_option ("reg-names-std");
c5aa993b 2147
bc90b915
FN
2148 /* Begin creating the help text. */
2149 stb = mem_fileopen ();
2150 fprintf_unfiltered (stb, "Set the disassembly flavor.\n\
2151The valid values are:\n");
ed9a39eb 2152
bc90b915
FN
2153 /* Initialize the array that will be passed to add_set_enum_cmd(). */
2154 valid_flavors = xmalloc ((num_flavor_options + 1) * sizeof (char *));
2155 for (i = 0; i < num_flavor_options; i++)
2156 {
2157 numregs = get_arm_regnames (i, &setname, &setdesc, &regnames);
53904c9e 2158 valid_flavors[i] = setname;
bc90b915
FN
2159 fprintf_unfiltered (stb, "%s - %s\n", setname,
2160 setdesc);
2161 /* Copy the default names (if found) and synchronize disassembler. */
2162 if (!strcmp (setname, "std"))
2163 {
53904c9e 2164 disassembly_flavor = setname;
bc90b915
FN
2165 current_option = i;
2166 for (j = 0; j < numregs; j++)
2167 arm_register_names[j] = (char *) regnames[j];
2168 set_arm_regname_option (i);
2169 }
2170 }
2171 /* Mark the end of valid options. */
2172 valid_flavors[num_flavor_options] = NULL;
c906108c 2173
bc90b915
FN
2174 /* Finish the creation of the help text. */
2175 fprintf_unfiltered (stb, "The default is \"std\".");
2176 helptext = ui_file_xstrdup (stb, &length);
2177 ui_file_delete (stb);
ed9a39eb 2178
bc90b915 2179 /* Add the disassembly-flavor command */
96baa820 2180 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
ed9a39eb 2181 valid_flavors,
1ed2a135 2182 &disassembly_flavor,
bc90b915 2183 helptext,
ed9a39eb 2184 &setlist);
96baa820 2185 new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
ed9a39eb
JM
2186 add_show_from_set (new_cmd, &showlist);
2187
c906108c
SS
2188 /* ??? Maybe this should be a boolean. */
2189 add_show_from_set (add_set_cmd ("apcs32", no_class,
ed9a39eb 2190 var_zinteger, (char *) &arm_apcs_32,
96baa820 2191 "Set usage of ARM 32-bit mode.\n", &setlist),
ed9a39eb 2192 &showlist);
c906108c 2193
bc90b915
FN
2194 /* Add the deprecated "othernames" command */
2195
2196 add_com ("othernames", class_obscure, arm_othernames,
2197 "Switch to the next set of register names.");
c906108c
SS
2198}
2199
ed9a39eb
JM
2200/* Test whether the coff symbol specific value corresponds to a Thumb
2201 function. */
2202
c906108c 2203int
c5aa993b 2204coff_sym_is_thumb (int val)
c906108c 2205{
c5aa993b
JM
2206 return (val == C_THUMBEXT ||
2207 val == C_THUMBSTAT ||
2208 val == C_THUMBEXTFUNC ||
2209 val == C_THUMBSTATFUNC ||
2210 val == C_THUMBLABEL);
c906108c 2211}