import gdb-1999-08-09 snapshot
[external/binutils.git] / gdb / arm-tdep.c
1 /* Target-dependent code for the Acorn Risc Machine (ARM).
2    Copyright (C) 1988, 1989, 1991, 1992, 1993, 1995-1999
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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.  */
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 */
30
31 /*
32    The following macros are actually wrong.  Neither arm nor thumb can
33    or should set the lsb on addr.
34    The thumb addresses are mod 2, so (addr & 2) would be a good heuristic
35    to use when checking for thumb (see arm_pc_is_thumb() below).
36    Unfortunately, something else depends on these (incorrect) macros, so
37    fixing them actually breaks gdb.  I didn't have time to investigate. Z.R.
38  */
39 /* Thumb function addresses are odd (bit 0 is set).  Here are some
40    macros to test, set, or clear bit 0 of addresses.  */
41 #define IS_THUMB_ADDR(addr)     ((addr) & 1)
42 #define MAKE_THUMB_ADDR(addr)   ((addr) | 1)
43 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
44
45 /* Macros to round N up or down to the next A boundary; A must be
46    a power of two. */
47 #define ROUND_DOWN(n,a)         ((n) & ~((a) - 1))
48 #define ROUND_UP(n,a)           (((n) + (a) - 1) & ~((a) - 1))
49
50 static char *apcs_register_names[] =
51 { "a1", "a2", "a3", "a4", /*  0  1  2  3 */
52   "v1", "v2", "v3", "v4", /*  4  5  6  7 */
53   "v5", "v6", "sl", "fp", /*  8  9 10 11 */
54   "ip", "sp", "lr", "pc", /* 12 13 14 15 */
55   "f0", "f1", "f2", "f3", /* 16 17 18 19 */
56   "f4", "f5", "f6", "f7", /* 20 21 22 23 */
57   "fps","ps" }            /* 24 25       */;
58
59 /* These names are the ones which gcc emits, and 
60    I find them less confusing.  Toggle between them
61    using the `othernames' command. */
62 static char *additional_register_names[] =
63 { "r0", "r1", "r2", "r3", /*  0  1  2  3 */
64   "r4", "r5", "r6", "r7",    /*  4  5  6  7 */
65   "r8", "r9", "r10", "r11",  /*  8  9 10 11 */
66   "r12", "r13", "r14", "pc", /* 12 13 14 15 */
67   "f0", "f1", "f2", "f3",    /* 16 17 18 19 */
68   "f4", "f5", "f6", "f7",    /* 20 21 22 23 */
69   "fps","ps" }               /* 24 25       */;
70
71 /* By default use the APCS registers names */
72
73 char **arm_register_names = apcs_register_names;
74 /* This is the variable the is set with "set disassembly-flavor",
75  and its legitimate values. */
76 static char apcs_flavor[] = "apcs";
77 static char r_prefix_flavor[] = "r-prefix";
78 static char *valid_flavors[] = {
79   apcs_flavor,
80   r_prefix_flavor,
81   NULL
82 };
83 static char *disassembly_flavor = apcs_flavor;
84
85 /* This is used to keep the bfd arch_info in sync with the disassembly flavor.  */
86 static void set_disassembly_flavor_sfunc PARAMS ((char *, int, \
87                                                   struct cmd_list_element *));
88 static void set_disassembly_flavor ();
89
90 /* Should call_function allocate stack space for a struct return?  */
91 /* The system C compiler uses a similar structure return convention to gcc */
92 int
93 arm_use_struct_convention (gcc_p, type)
94      int gcc_p;
95      struct type *type;
96 {
97   return (TYPE_LENGTH (type) > 4);
98 }
99
100 int
101 arm_frame_chain_valid (chain, thisframe)
102      CORE_ADDR chain;
103      struct frame_info *thisframe;
104 {
105 #define LOWEST_PC 0x20          /* the first 0x20 bytes are the trap vectors. */
106   return (chain != 0 && (FRAME_SAVED_PC (thisframe) >= LOWEST_PC));
107 }
108
109 /* Set to true if the 32-bit mode is in use. */
110
111 int arm_apcs_32 = 1;
112
113 /* Flag set by arm_fix_call_dummy that tells whether the target function
114    is a Thumb function.  This flag is checked by arm_push_arguments.
115    FIXME: Change the PUSH_ARGUMENTS macro (and its use in valops.c) to
116    pass the function address as an additional parameter.  */
117
118 static int target_is_thumb;
119
120 /* Flag set by arm_fix_call_dummy that tells whether the calling function
121    is a Thumb function.  This flag is checked by arm_pc_is_thumb
122    and arm_call_dummy_breakpoint_offset.  */
123
124 static int caller_is_thumb;
125
126 /* Tell if the program counter value in MEMADDR is in a Thumb function.  */
127
128 int
129 arm_pc_is_thumb (memaddr)
130      bfd_vma memaddr;
131 {
132   struct minimal_symbol *sym;
133   CORE_ADDR sp;
134
135   /* If bit 0 of the address is set, assume this is a Thumb address. */
136   if (IS_THUMB_ADDR (memaddr))
137     return 1;
138
139   /* Thumb function have a "special" bit set in minimal symbols */
140   sym = lookup_minimal_symbol_by_pc (memaddr);
141   if (sym)
142     {
143       return (MSYMBOL_IS_SPECIAL (sym));
144     }
145   else
146     return 0;
147 }
148
149 /* Tell if the program counter value in MEMADDR is in a call dummy that
150    is being called from a Thumb function.  */
151
152 int
153 arm_pc_is_thumb_dummy (memaddr)
154      bfd_vma memaddr;
155 {
156   CORE_ADDR sp = read_sp ();
157
158   if (PC_IN_CALL_DUMMY (memaddr, sp, sp + 64))
159     return caller_is_thumb;
160   else
161     return 0;
162 }
163
164 CORE_ADDR
165 arm_addr_bits_remove (val)
166      CORE_ADDR val;
167 {
168   if (arm_pc_is_thumb (val))
169     return (val & (arm_apcs_32 ? 0xfffffffe : 0x03fffffe));
170   else
171     return (val & (arm_apcs_32 ? 0xfffffffc : 0x03fffffc));
172 }
173
174 CORE_ADDR
175 arm_saved_pc_after_call (frame)
176      struct frame_info *frame;
177 {
178   return ADDR_BITS_REMOVE (read_register (LR_REGNUM));
179 }
180
181 int
182 arm_frameless_function_invocation (fi)
183      struct frame_info *fi;
184 {
185   CORE_ADDR func_start, after_prologue;
186   int frameless;
187   
188   func_start = (get_pc_function_start ((fi)->pc) + FUNCTION_START_OFFSET);
189   after_prologue = func_start;
190   SKIP_PROLOGUE (after_prologue);
191   /* There are some frameless functions whose first two instructions
192      follow the standard APCS form, in which case after_prologue
193      will be func_start + 8. */
194   
195   frameless = (after_prologue < func_start + 12);
196   return frameless;
197 }
198
199 /* A typical Thumb prologue looks like this:
200    push    {r7, lr}
201    add     sp, sp, #-28
202    add     r7, sp, #12
203    Sometimes the latter instruction may be replaced by:
204    mov     r7, sp 
205  */
206
207 static CORE_ADDR
208 thumb_skip_prologue (pc)
209      CORE_ADDR pc;
210 {
211   CORE_ADDR current_pc;
212
213   for (current_pc = pc; current_pc < pc + 20; current_pc += 2)
214     {
215       unsigned short insn = read_memory_unsigned_integer (current_pc, 2);
216
217       if ((insn & 0xfe00) != 0xb400     /* push {..., r7, lr} */
218           && (insn & 0xff00) != 0xb000  /* add sp, #simm */
219           && (insn & 0xff00) != 0xaf00  /* add r7, sp, #imm */
220           && insn != 0x466f     /* mov r7, sp */
221           && (insn & 0xffc0) != 0x4640)         /* mov r0-r7, r8-r15 */
222         break;
223     }
224
225   return current_pc;
226 }
227
228 /* APCS (ARM procedure call standard) defines the following prologue:
229
230    mov          ip, sp
231    [stmfd       sp!, {a1,a2,a3,a4}]
232    stmfd        sp!, {...,fp,ip,lr,pc}
233    [stfe                f7, [sp, #-12]!]
234    [stfe                f6, [sp, #-12]!]
235    [stfe                f5, [sp, #-12]!]
236    [stfe                f4, [sp, #-12]!]
237    sub          fp, ip, #nn     // nn == 20 or 4 depending on second ins
238  */
239
240 CORE_ADDR
241 arm_skip_prologue (pc)
242      CORE_ADDR pc;
243 {
244   unsigned long inst;
245   CORE_ADDR skip_pc;
246   CORE_ADDR func_addr, func_end;
247   struct symtab_and_line sal;
248
249   /* See what the symbol table says.  */
250   
251   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
252     {
253       sal = find_pc_line (func_addr, 0);
254       if ((sal.line != 0) && (sal.end < func_end))
255         return sal.end;
256     }
257
258   /* Check if this is Thumb code.  */
259   if (arm_pc_is_thumb (pc))
260     return thumb_skip_prologue (pc);
261
262   /* Can't find the prologue end in the symbol table, try it the hard way
263      by disassembling the instructions. */
264   skip_pc = pc;
265   inst = read_memory_integer (skip_pc, 4);
266   if (inst != 0xe1a0c00d)       /* mov ip, sp */
267     return pc;
268
269   skip_pc += 4;
270   inst = read_memory_integer (skip_pc, 4);
271   if ((inst & 0xfffffff0) == 0xe92d0000)        /* stmfd sp!,{a1,a2,a3,a4}  */
272     {
273       skip_pc += 4;
274       inst = read_memory_integer (skip_pc, 4);
275     }
276
277   if ((inst & 0xfffff800) != 0xe92dd800)        /* stmfd sp!,{...,fp,ip,lr,pc} */
278     return pc;
279
280   skip_pc += 4;
281   inst = read_memory_integer (skip_pc, 4);
282
283   /* Any insns after this point may float into the code, if it makes
284      for better instruction scheduling, so we skip them only if
285      we find them, but still consdier the function to be frame-ful  */
286
287   /* We may have either one sfmfd instruction here, or several stfe insns,
288      depending on the version of floating point code we support.  */
289   if ((inst & 0xffbf0fff) == 0xec2d0200)        /* sfmfd fn, <cnt>, [sp]! */
290     {
291       skip_pc += 4;
292       inst = read_memory_integer (skip_pc, 4);
293     }
294   else
295     {
296       while ((inst & 0xffff8fff) == 0xed6d0103)         /* stfe fn, [sp, #-12]! */
297         {
298           skip_pc += 4;
299           inst = read_memory_integer (skip_pc, 4);
300         }
301     }
302
303   if ((inst & 0xfffff000) == 0xe24cb000)        /* sub fp, ip, #nn */
304     skip_pc += 4;
305
306   return skip_pc;
307 }
308 /* *INDENT-OFF* */
309 /* Function: thumb_scan_prologue (helper function for arm_scan_prologue)
310    This function decodes a Thumb function prologue to determine:
311      1) the size of the stack frame
312      2) which registers are saved on it
313      3) the offsets of saved regs
314      4) the offset from the stack pointer to the frame pointer
315    This information is stored in the "extra" fields of the frame_info.
316
317    A typical Thumb function prologue might look like this:
318         push {r7, lr}
319         sub  sp, #28,
320         add  r7, sp, #12
321    Which would create this stack frame (offsets relative to FP)
322      old SP ->  24  stack parameters
323                 20  LR
324                 16  R7
325      R7 ->       0  local variables (16 bytes)
326      SP ->     -12  additional stack space (12 bytes)
327    The frame size would thus be 36 bytes, and the frame offset would be
328    12 bytes.  The frame register is R7.  */
329 /* *INDENT-ON* */
330
331
332
333
334 static void
335 thumb_scan_prologue (fi)
336      struct frame_info *fi;
337 {
338   CORE_ADDR prologue_start;
339   CORE_ADDR prologue_end;
340   CORE_ADDR current_pc;
341   int saved_reg[16];            /* which register has been copied to register n? */
342   int i;
343
344   if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
345     {
346       struct symtab_and_line sal = find_pc_line (prologue_start, 0);
347
348       if (sal.line == 0)        /* no line info, use current PC */
349         prologue_end = fi->pc;
350       else if (sal.end < prologue_end)  /* next line begins after fn end */
351         prologue_end = sal.end; /* (probably means no prologue)  */
352     }
353   else
354     prologue_end = prologue_start + 40;         /* We're in the boondocks: allow for */
355   /* 16 pushes, an add, and "mv fp,sp" */
356
357   prologue_end = min (prologue_end, fi->pc);
358
359   /* Initialize the saved register map.  When register H is copied to
360      register L, we will put H in saved_reg[L].  */
361   for (i = 0; i < 16; i++)
362     saved_reg[i] = i;
363
364   /* Search the prologue looking for instructions that set up the
365      frame pointer, adjust the stack pointer, and save registers.  */
366
367   fi->framesize = 0;
368   for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 2)
369     {
370       unsigned short insn;
371       int regno;
372       int offset;
373
374       insn = read_memory_unsigned_integer (current_pc, 2);
375
376       if ((insn & 0xfe00) == 0xb400)    /* push { rlist } */
377         {
378           /* Bits 0-7 contain a mask for registers R0-R7.  Bit 8 says
379              whether to save LR (R14).  */
380           int mask = (insn & 0xff) | ((insn & 0x100) << 6);
381
382           /* Calculate offsets of saved R0-R7 and LR. */
383           for (regno = LR_REGNUM; regno >= 0; regno--)
384             if (mask & (1 << regno))
385               {
386                 fi->framesize += 4;
387                 fi->fsr.regs[saved_reg[regno]] = -(fi->framesize);
388                 saved_reg[regno] = regno;       /* reset saved register map */
389               }
390         }
391       else if ((insn & 0xff00) == 0xb000)       /* add sp, #simm */
392         {
393           offset = (insn & 0x7f) << 2;  /* get scaled offset */
394           if (insn & 0x80)      /* is it signed? */
395             offset = -offset;
396           fi->framesize -= offset;
397         }
398       else if ((insn & 0xff00) == 0xaf00)       /* add r7, sp, #imm */
399         {
400           fi->framereg = THUMB_FP_REGNUM;
401           fi->frameoffset = (insn & 0xff) << 2;         /* get scaled offset */
402         }
403       else if (insn == 0x466f)  /* mov r7, sp */
404         {
405           fi->framereg = THUMB_FP_REGNUM;
406           fi->frameoffset = 0;
407           saved_reg[THUMB_FP_REGNUM] = SP_REGNUM;
408         }
409       else if ((insn & 0xffc0) == 0x4640)       /* mov r0-r7, r8-r15 */
410         {
411           int lo_reg = insn & 7;        /* dest. register (r0-r7) */
412           int hi_reg = ((insn >> 3) & 7) + 8;   /* source register (r8-15) */
413           saved_reg[lo_reg] = hi_reg;   /* remember hi reg was saved */
414         }
415       else
416         break;                  /* anything else isn't prologue */
417     }
418 }
419
420 /* Function: check_prologue_cache
421    Check if prologue for this frame's PC has already been scanned.
422    If it has, copy the relevant information about that prologue and
423    return non-zero.  Otherwise do not copy anything and return zero.
424
425    The information saved in the cache includes:
426    * the frame register number;
427    * the size of the stack frame;
428    * the offsets of saved regs (relative to the old SP); and
429    * the offset from the stack pointer to the frame pointer
430
431    The cache contains only one entry, since this is adequate
432    for the typical sequence of prologue scan requests we get.
433    When performing a backtrace, GDB will usually ask to scan
434    the same function twice in a row (once to get the frame chain,
435    and once to fill in the extra frame information).
436  */
437
438 static struct frame_info prologue_cache;
439
440 static int
441 check_prologue_cache (fi)
442      struct frame_info *fi;
443 {
444   int i;
445
446   if (fi->pc == prologue_cache.pc)
447     {
448       fi->framereg = prologue_cache.framereg;
449       fi->framesize = prologue_cache.framesize;
450       fi->frameoffset = prologue_cache.frameoffset;
451       for (i = 0; i <= NUM_REGS; i++)
452         fi->fsr.regs[i] = prologue_cache.fsr.regs[i];
453       return 1;
454     }
455   else
456     return 0;
457 }
458
459
460 /* Function: save_prologue_cache
461    Copy the prologue information from fi to the prologue cache.
462  */
463
464 static void
465 save_prologue_cache (fi)
466      struct frame_info *fi;
467 {
468   int i;
469
470   prologue_cache.pc = fi->pc;
471   prologue_cache.framereg = fi->framereg;
472   prologue_cache.framesize = fi->framesize;
473   prologue_cache.frameoffset = fi->frameoffset;
474
475   for (i = 0; i <= NUM_REGS; i++)
476     prologue_cache.fsr.regs[i] = fi->fsr.regs[i];
477 }
478
479
480 /* Function: arm_scan_prologue
481    This function decodes an ARM function prologue to determine:
482    1) the size of the stack frame
483    2) which registers are saved on it
484    3) the offsets of saved regs
485    4) the offset from the stack pointer to the frame pointer
486    This information is stored in the "extra" fields of the frame_info.
487
488    There are two basic forms for the ARM prologue.  The fixed argument
489    function call will look like:
490    
491         mov    ip, sp
492         stmfd  sp!, {fp, ip, lr, pc}
493         sub    fp, ip, #4
494         [sub sp, sp, #4]
495
496    Which would create this stack frame (offsets relative to FP):
497      IP ->   4  (caller's stack)
498      FP ->   0  PC (points to address of stmfd instruction + 8 in callee)
499             -4  LR (return address in caller)
500             -8  IP (copy of caller's SP)
501            -12  FP (caller's FP)
502      SP -> -28  Local variables
503      
504    The frame size would thus be 32 bytes, and the frame offset would be
505    28 bytes.  The stmfd call can also save any of the vN registers it
506    plans to use, which increases the frame size accordingly.
507
508    Note: The stored PC is 8 off of the STMFD instruction that stored it
509    because the ARM Store instructions always store PC + 8 when you read
510    the PC register.
511    
512    A variable argument function call will look like:
513
514         mov    ip, sp
515         stmfd  sp!, {a1, a2, a3, a4}
516         stmfd  sp!, {fp, ip, lr, pc}
517         sub    fp, ip, #20
518    
519    Which would create this stack frame (offsets relative to FP):
520      IP ->  20  (caller's stack)
521             16  A4
522             12  A3
523              8  A2
524              4  A1
525      FP ->   0  PC (points to address of stmfd instruction + 8 in callee)
526             -4  LR (return address in caller)
527             -8  IP (copy of caller's SP)
528            -12  FP (caller's FP)
529      SP -> -28  Local variables
530
531    The frame size would thus be 48 bytes, and the frame offset would be
532    28 bytes.
533
534    There is another potential complication, which is that the optimizer
535    will try to separate the store of fp in the "stmfd" instruction from
536    the "sub fp, ip, #NN" instruction.  Almost anything can be there, so
537    we just key on the stmfd, and then scan for the "sub fp, ip, #NN"...
538
539    Also, note, the original version of the ARM toolchain claimed that there
540    should be an
541
542    instruction at the end of the prologue.  I have never seen GCC produce
543    this, and the ARM docs don't mention it.  We still test for it below in
544    case it happens...
545    
546 */
547
548 static void
549 arm_scan_prologue (fi)
550      struct frame_info *fi;
551 {
552   int regno, sp_offset, fp_offset;
553   CORE_ADDR prologue_start, prologue_end, current_pc;
554
555   /* Check if this function is already in the cache of frame information. */
556   if (check_prologue_cache (fi))
557     return;
558
559   /* Assume there is no frame until proven otherwise.  */
560   fi->framereg = SP_REGNUM;
561   fi->framesize = 0;
562   fi->frameoffset = 0;
563
564   /* Check for Thumb prologue.  */
565   if (arm_pc_is_thumb (fi->pc))
566     {
567       thumb_scan_prologue (fi);
568       save_prologue_cache (fi);
569       return;
570     }
571
572   /* Find the function prologue.  If we can't find the function in
573      the symbol table, peek in the stack frame to find the PC.  */
574   if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
575     {
576       /* Assume the prologue is everything between the first instruction
577          in the function and the first source line.  */
578       struct symtab_and_line sal = find_pc_line (prologue_start, 0);
579
580       if (sal.line == 0)        /* no line info, use current PC */
581         prologue_end = fi->pc;
582       else if (sal.end < prologue_end)  /* next line begins after fn end */
583         prologue_end = sal.end; /* (probably means no prologue)  */
584     }
585   else
586     {
587       /* Get address of the stmfd in the prologue of the callee; the saved
588          PC is the address of the stmfd + 8.  */
589       prologue_start = ADDR_BITS_REMOVE(read_memory_integer (fi->frame, 4))
590         - 8;
591       prologue_end = prologue_start + 64; /* This is all the insn's
592                                              that could be in the prologue,
593                                              plus room for 5 insn's inserted
594                                              by the scheduler.  */
595     }
596
597   /* Now search the prologue looking for instructions that set up the
598      frame pointer, adjust the stack pointer, and save registers.
599      
600      Be careful, however, and if it doesn't look like a prologue,
601      don't try to scan it.  If, for instance, a frameless function
602      begins with stmfd sp!, then we will tell ourselves there is
603      a frame, which will confuse stack traceback, as well ad"finish" 
604      and other operations that rely on a knowledge of the stack
605      traceback.
606
607      In the APCS, the prologue should start with  "mov ip, sp" so
608      if we don't see this as the first insn, we will stop.  */
609
610   sp_offset = fp_offset = 0;
611
612   if (read_memory_unsigned_integer (prologue_start, 4) 
613                                            == 0xe1a0c00d)  /* mov ip, sp */
614     {
615       for (current_pc = prologue_start +4; current_pc < prologue_end;
616            current_pc += 4)
617         {
618           unsigned int insn = read_memory_unsigned_integer (current_pc, 4);
619           
620           if ((insn & 0xffff0000) == 0xe92d0000)
621             /* stmfd sp!, {..., fp, ip, lr, pc}
622                or
623                stmfd sp!, {a1, a2, a3, a4}  */
624             {
625               int mask = insn & 0xffff;
626               
627               /* Calculate offsets of saved registers. */
628               for (regno = PC_REGNUM; regno >= 0; regno--)
629                 if (mask & (1 << regno))
630                   {
631                     sp_offset -= 4;
632                     fi->fsr.regs[regno] = sp_offset;
633                   }
634             }
635           else if ((insn & 0xfffff000) == 0xe24cb000)     /* sub fp, ip #n */
636             {
637               unsigned imm = insn & 0xff;                 /* immediate value */
638               unsigned rot = (insn & 0xf00) >> 7;         /* rotate amount */
639               imm = (imm >> rot) | (imm << (32-rot));
640               fp_offset = -imm;
641               fi->framereg = FP_REGNUM;
642             }
643           else if ((insn & 0xfffff000) == 0xe24dd000)     /* sub sp, sp #n */
644             {
645               unsigned imm = insn & 0xff;                 /* immediate value */
646               unsigned rot = (insn & 0xf00) >> 7;         /* rotate amount */
647               imm = (imm >> rot) | (imm << (32-rot));
648               sp_offset -= imm;
649             }
650           else if ((insn & 0xffff7fff) == 0xed6d0103) /* stfe f?, [sp, -#c]! */
651             {
652               sp_offset -= 12;
653               regno = F0_REGNUM + ((insn >> 12) & 0x07);
654               fi->fsr.regs[regno] = sp_offset;
655             }
656           else if ((insn & 0xffbf0fff) == 0xec2d0200) /* sfmfd f0, 4, [sp!] */
657             {
658               int n_saved_fp_regs, i;
659               unsigned int fp_start_reg, fp_bound_reg;
660               
661               if ((insn & 0x800) == 0x800) /* N0 is set */
662                 {  
663                   if ((insn & 0x40000) == 0x40000) /* N1 is set */
664                     n_saved_fp_regs = 3;
665                   else
666                     n_saved_fp_regs = 1;
667                 }
668               else
669                 {  
670                   if ((insn & 0x40000) == 0x40000) /* N1 is set */
671                     n_saved_fp_regs = 2;
672                   else
673                     n_saved_fp_regs = 4;
674                 }
675               
676               fp_start_reg = F0_REGNUM + ((insn >> 12) & 0x7);
677               fp_bound_reg = fp_start_reg + n_saved_fp_regs;
678               for (; fp_start_reg < fp_bound_reg; fp_start_reg++)
679                 {
680                   sp_offset -= 12;
681                   fi->fsr.regs[fp_start_reg++] = sp_offset;
682                 }
683             }
684           else
685             continue;  /* The optimizer might shove anything into the
686                           prologue, so we just skip what we don't recognize. */
687         }
688     }
689
690   /* The frame size is just the negative of the offset (from the original SP)
691      of the last thing thing we pushed on the stack.  The frame offset is
692      [new FP] - [new SP].  */
693   fi->framesize = -sp_offset;
694   fi->frameoffset = fp_offset - sp_offset;
695   
696   save_prologue_cache (fi);
697 }
698
699
700 /* Function: find_callers_reg
701    Find REGNUM on the stack.  Otherwise, it's in an active register.  One thing
702    we might want to do here is to check REGNUM against the clobber mask, and
703    somehow flag it as invalid if it isn't saved on the stack somewhere.  This
704    would provide a graceful failure mode when trying to get the value of
705    caller-saves registers for an inner frame.  */
706
707 static CORE_ADDR
708 arm_find_callers_reg (fi, regnum)
709      struct frame_info *fi;
710      int regnum;
711 {
712   for (; fi; fi = fi->next)
713
714 #if 0                           /* FIXME: enable this code if we convert to new call dummy scheme.  */
715     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
716       return generic_read_register_dummy (fi->pc, fi->frame, regnum);
717     else
718 #endif
719     if (fi->fsr.regs[regnum] != 0)
720       return read_memory_integer (fi->fsr.regs[regnum],
721                                   REGISTER_RAW_SIZE (regnum));
722   return read_register (regnum);
723 }
724 /* *INDENT-OFF* */
725 /* Function: frame_chain
726    Given a GDB frame, determine the address of the calling function's frame.
727    This will be used to create a new GDB frame struct, and then
728    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
729    For ARM, we save the frame size when we initialize the frame_info.
730
731    The original definition of this function was a macro in tm-arm.h:
732       { In the case of the ARM, the frame's nominal address is the FP value,
733          and 12 bytes before comes the saved previous FP value as a 4-byte word.  }
734
735       #define FRAME_CHAIN(thisframe)  \
736         ((thisframe)->pc >= LOWEST_PC ?    \
737          read_memory_integer ((thisframe)->frame - 12, 4) :\
738          0)
739 */
740 /* *INDENT-ON* */
741
742
743
744
745 CORE_ADDR
746 arm_frame_chain (fi)
747      struct frame_info *fi;
748 {
749 #if 0                           /* FIXME: enable this code if we convert to new call dummy scheme.  */
750   CORE_ADDR fn_start, callers_pc, fp;
751
752   /* is this a dummy frame? */
753   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
754     return fi->frame;           /* dummy frame same as caller's frame */
755
756   /* is caller-of-this a dummy frame? */
757   callers_pc = FRAME_SAVED_PC (fi);     /* find out who called us: */
758   fp = arm_find_callers_reg (fi, FP_REGNUM);
759   if (PC_IN_CALL_DUMMY (callers_pc, fp, fp))
760     return fp;                  /* dummy frame's frame may bear no relation to ours */
761
762   if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
763     if (fn_start == entry_point_address ())
764       return 0;                 /* in _start fn, don't chain further */
765 #endif
766   CORE_ADDR caller_pc, fn_start;
767   struct frame_info caller_fi;
768   int framereg = fi->framereg;
769
770   if (fi->pc < LOWEST_PC)
771     return 0;
772
773   /* If the caller is the startup code, we're at the end of the chain.  */
774   caller_pc = FRAME_SAVED_PC (fi);
775   if (find_pc_partial_function (caller_pc, 0, &fn_start, 0))
776     if (fn_start == entry_point_address ())
777       return 0;
778
779   /* If the caller is Thumb and the caller is ARM, or vice versa,
780      the frame register of the caller is different from ours.
781      So we must scan the prologue of the caller to determine its
782      frame register number. */
783   if (arm_pc_is_thumb (caller_pc) != arm_pc_is_thumb (fi->pc))
784     {
785       memset (&caller_fi, 0, sizeof (caller_fi));
786       caller_fi.pc = caller_pc;
787       arm_scan_prologue (&caller_fi);
788       framereg = caller_fi.framereg;
789     }
790
791   /* If the caller used a frame register, return its value.
792      Otherwise, return the caller's stack pointer.  */
793   if (framereg == FP_REGNUM || framereg == THUMB_FP_REGNUM)
794     return arm_find_callers_reg (fi, framereg);
795   else
796     return fi->frame + fi->framesize;
797 }
798
799 /* Function: init_extra_frame_info
800    This function actually figures out the frame address for a given pc and
801    sp.  This is tricky  because we sometimes don't use an explicit
802    frame pointer, and the previous stack pointer isn't necessarily recorded
803    on the stack.  The only reliable way to get this info is to
804    examine the prologue.
805    FROMLEAF is a little confusing, it means this is the next frame up
806    the chain AFTER a frameless function.  If this is true, then the
807    frame value for this frame is still in the fp register.  */
808
809 void
810 arm_init_extra_frame_info (fromleaf, fi)
811      int fromleaf;
812      struct frame_info * fi;
813 {
814   int reg;
815
816   if (fi->next)
817     fi->pc = FRAME_SAVED_PC (fi->next);
818
819   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
820
821 #if 0                           /* FIXME: enable this code if we convert to new call dummy scheme.  */
822   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
823     {
824       /* We need to setup fi->frame here because run_stack_dummy gets it wrong
825          by assuming it's always FP.  */
826       fi->frame = generic_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
827       fi->framesize = 0;
828       fi->frameoffset = 0;
829       return;
830     }
831   else
832 #endif
833     {
834       arm_scan_prologue (fi);
835
836       if (!fi->next)            /* this is the innermost frame? */
837         fi->frame = read_register (fi->framereg);
838       else                              /* not the innermost frame */
839         /* If we have an FP,  the callee saved it. */
840         if (fi->framereg == FP_REGNUM || fi->framereg == THUMB_FP_REGNUM)
841           if (fi->next->fsr.regs[fi->framereg] != 0)
842             fi->frame = read_memory_integer (fi->next->fsr.regs[fi->framereg],
843                                              4);
844           else if (fromleaf) /* If we were called by a frameless fn.
845                                  then our frame is still in the frame pointer
846                                  register on the board... */
847             fi->frame = read_fp ();
848
849       /* Calculate actual addresses of saved registers using offsets determined
850          by arm_scan_prologue.  */
851       for (reg = 0; reg < NUM_REGS; reg++)
852         if (fi->fsr.regs[reg] != 0)
853           fi->fsr.regs[reg] += fi->frame + fi->framesize - fi->frameoffset;
854     }
855 }
856
857
858 /* Function: frame_saved_pc
859    Find the caller of this frame.  We do this by seeing if LR_REGNUM is saved
860    in the stack anywhere, otherwise we get it from the registers.
861
862    The old definition of this function was a macro:
863    #define FRAME_SAVED_PC(FRAME) \
864    ADDR_BITS_REMOVE (read_memory_integer ((FRAME)->frame - 4, 4))
865  */
866
867 CORE_ADDR
868 arm_frame_saved_pc (fi)
869      struct frame_info *fi;
870 {
871 #if 0                           /* FIXME: enable this code if we convert to new call dummy scheme.  */
872   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
873     return generic_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
874   else
875 #endif
876     {
877       CORE_ADDR pc = arm_find_callers_reg (fi, LR_REGNUM);
878       return IS_THUMB_ADDR (pc) ? UNMAKE_THUMB_ADDR (pc) : pc;
879     }
880 }
881
882
883 /* Return the frame address.  On ARM, it is R11; on Thumb it is R7.
884    Examine the Program Status Register to decide which state we're in.  */
885
886 CORE_ADDR
887 arm_target_read_fp ()
888 {
889   if (read_register (PS_REGNUM) & 0x20)         /* Bit 5 is Thumb state bit */
890     return read_register (THUMB_FP_REGNUM);     /* R7 if Thumb */
891   else
892     return read_register (FP_REGNUM);   /* R11 if ARM */
893 }
894
895
896 /* Calculate the frame offsets of the saved registers (ARM version). */
897 void
898 arm_frame_find_saved_regs (fi, regaddr)
899      struct frame_info *fi;
900      struct frame_saved_regs *regaddr;
901 {
902   memcpy (regaddr, &fi->fsr, sizeof (struct frame_saved_regs));
903 }
904
905
906 void
907 arm_push_dummy_frame ()
908 {
909   CORE_ADDR old_sp = read_register (SP_REGNUM);
910   CORE_ADDR sp = old_sp;
911   CORE_ADDR fp, prologue_start;
912   int regnum;
913
914   /* Push the two dummy prologue instructions in reverse order,
915      so that they'll be in the correct low-to-high order in memory.  */
916   /* sub     fp, ip, #4 */
917   sp = push_word (sp, 0xe24cb004);
918   /*  stmdb   sp!, {r0-r10, fp, ip, lr, pc} */
919   prologue_start = sp = push_word (sp, 0xe92ddfff);
920
921   /* push a pointer to the dummy prologue + 12, because when
922      stm instruction stores the PC, it stores the address of the stm
923      instruction itself plus 12.  */
924   fp = sp = push_word (sp, prologue_start + 12);
925   sp = push_word (sp, read_register (PC_REGNUM));       /* FIXME: was PS_REGNUM */
926   sp = push_word (sp, old_sp);
927   sp = push_word (sp, read_register (FP_REGNUM));
928
929   for (regnum = 10; regnum >= 0; regnum--)
930     sp = push_word (sp, read_register (regnum));
931
932   write_register (FP_REGNUM, fp);
933   write_register (THUMB_FP_REGNUM, fp);
934   write_register (SP_REGNUM, sp);
935 }
936
937 /* Fix up the call dummy, based on whether the processor is currently
938    in Thumb or ARM mode, and whether the target function is Thumb
939    or ARM.  There are three different situations requiring three
940    different dummies:
941
942    * ARM calling ARM: uses the call dummy in tm-arm.h, which has already
943    been copied into the dummy parameter to this function.
944    * ARM calling Thumb: uses the call dummy in tm-arm.h, but with the
945    "mov pc,r4" instruction patched to be a "bx r4" instead.
946    * Thumb calling anything: uses the Thumb dummy defined below, which
947    works for calling both ARM and Thumb functions.
948
949    All three call dummies expect to receive the target function address
950    in R4, with the low bit set if it's a Thumb function.
951  */
952
953 void
954 arm_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
955      char *dummy;
956      CORE_ADDR pc;
957      CORE_ADDR fun;
958      int nargs;
959      value_ptr *args;
960      struct type *type;
961      int gcc_p;
962 {
963   static short thumb_dummy[4] =
964   {
965     0xf000, 0xf801,             /*        bl      label */
966     0xdf18,                     /*        swi     24 */
967     0x4720,                     /* label: bx      r4 */
968   };
969   static unsigned long arm_bx_r4 = 0xe12fff14;  /* bx r4 instruction */
970
971   /* Set flag indicating whether the current PC is in a Thumb function. */
972   caller_is_thumb = arm_pc_is_thumb (read_pc ());
973
974   /* If the target function is Thumb, set the low bit of the function address.
975      And if the CPU is currently in ARM mode, patch the second instruction
976      of call dummy to use a BX instruction to switch to Thumb mode.  */
977   target_is_thumb = arm_pc_is_thumb (fun);
978   if (target_is_thumb)
979     {
980       fun |= 1;
981       if (!caller_is_thumb)
982         store_unsigned_integer (dummy + 4, sizeof (arm_bx_r4), arm_bx_r4);
983     }
984
985   /* If the CPU is currently in Thumb mode, use the Thumb call dummy
986      instead of the ARM one that's already been copied.  This will
987      work for both Thumb and ARM target functions.  */
988   if (caller_is_thumb)
989     {
990       int i;
991       char *p = dummy;
992       int len = sizeof (thumb_dummy) / sizeof (thumb_dummy[0]);
993
994       for (i = 0; i < len; i++)
995         {
996           store_unsigned_integer (p, sizeof (thumb_dummy[0]), thumb_dummy[i]);
997           p += sizeof (thumb_dummy[0]);
998         }
999     }
1000
1001   /* Put the target address in r4; the call dummy will copy this to the PC. */
1002   write_register (4, fun);
1003 }
1004
1005
1006 /* Return the offset in the call dummy of the instruction that needs
1007    to have a breakpoint placed on it.  This is the offset of the 'swi 24'
1008    instruction, which is no longer actually used, but simply acts
1009    as a place-holder now.
1010
1011    This implements the CALL_DUMMY_BREAK_OFFSET macro.
1012  */
1013
1014 int
1015 arm_call_dummy_breakpoint_offset ()
1016 {
1017   if (caller_is_thumb)
1018     return 4;
1019   else
1020     return 8;
1021 }
1022
1023
1024 CORE_ADDR
1025 arm_push_arguments (nargs, args, sp, struct_return, struct_addr)
1026      int nargs;
1027      value_ptr *args;
1028      CORE_ADDR sp;
1029      int struct_return;
1030      CORE_ADDR struct_addr;
1031 {
1032   int argreg;
1033   int float_argreg;
1034   int argnum;
1035   int stack_offset;
1036   struct stack_arg
1037     {
1038       char *val;
1039       int len;
1040       int offset;
1041     };
1042   struct stack_arg *stack_args =
1043   (struct stack_arg *) alloca (nargs * sizeof (struct stack_arg));
1044   int nstack_args = 0;
1045
1046
1047   /* Initialize the integer and float register pointers.  */
1048   argreg = A1_REGNUM;
1049   float_argreg = F0_REGNUM;
1050
1051   /* the struct_return pointer occupies the first parameter-passing reg */
1052   if (struct_return)
1053     write_register (argreg++, struct_addr);
1054
1055   /* The offset onto the stack at which we will start copying parameters
1056      (after the registers are used up) begins at 16 in the old ABI.
1057      This leaves room for the "home" area for register parameters.  */
1058   stack_offset = REGISTER_SIZE * 4;
1059
1060   /* Process args from left to right.  Store as many as allowed in
1061      registers, save the rest to be pushed on the stack */
1062   for (argnum = 0; argnum < nargs; argnum++)
1063     {
1064       char *val;
1065       value_ptr arg = args[argnum];
1066       struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1067       struct type *target_type = TYPE_TARGET_TYPE (arg_type);
1068       int len = TYPE_LENGTH (arg_type);
1069       enum type_code typecode = TYPE_CODE (arg_type);
1070       CORE_ADDR regval;
1071       int newarg;
1072
1073       val = (char *) VALUE_CONTENTS (arg);
1074
1075       /* If the argument is a pointer to a function, and it's a Thumb
1076          function, set the low bit of the pointer.  */
1077       if (typecode == TYPE_CODE_PTR
1078           && target_type != NULL
1079           && TYPE_CODE (target_type) == TYPE_CODE_FUNC)
1080         {
1081           regval = extract_address (val, len);
1082           if (arm_pc_is_thumb (regval))
1083             store_address (val, len, MAKE_THUMB_ADDR (regval));
1084         }
1085
1086 #define MAPCS_FLOAT 0           /* --mapcs-float not implemented by the compiler yet */
1087 #if MAPCS_FLOAT
1088       /* Up to four floating point arguments can be passed in floating
1089          point registers on ARM (not on Thumb).  */
1090       if (typecode == TYPE_CODE_FLT
1091           && float_argreg <= ARM_LAST_FP_ARG_REGNUM
1092           && !target_is_thumb)
1093         {
1094           /* This is a floating point value that fits entirely
1095              in a single register.  */
1096           regval = extract_address (val, len);
1097           write_register (float_argreg++, regval);
1098         }
1099       else
1100 #endif
1101         {
1102           /* Copy the argument to general registers or the stack in
1103              register-sized pieces.  Large arguments are split between
1104              registers and stack.  */
1105           while (len > 0)
1106             {
1107               if (argreg <= ARM_LAST_ARG_REGNUM)
1108                 {
1109                   int partial_len = len < REGISTER_SIZE ? len : REGISTER_SIZE;
1110                   regval = extract_address (val, partial_len);
1111
1112                   /* It's a simple argument being passed in a general
1113                      register.  */
1114                   write_register (argreg, regval);
1115                   argreg++;
1116                   len -= partial_len;
1117                   val += partial_len;
1118                 }
1119               else
1120                 {
1121                   /* keep for later pushing */
1122                   stack_args[nstack_args].val = val;
1123                   stack_args[nstack_args++].len = len;
1124                   break;
1125                 }
1126             }
1127         }
1128     }
1129   /* now do the real stack pushing, process args right to left */
1130   while (nstack_args--)
1131     {
1132       sp -= stack_args[nstack_args].len;
1133       write_memory (sp, stack_args[nstack_args].val,
1134                     stack_args[nstack_args].len);
1135     }
1136
1137   /* Return adjusted stack pointer.  */
1138   return sp;
1139 }
1140
1141 void
1142 arm_pop_frame ()
1143 {
1144   struct frame_info *frame = get_current_frame ();
1145   int regnum;
1146   CORE_ADDR old_SP;
1147
1148   old_SP = read_register (frame->framereg);
1149   for (regnum = 0; regnum < NUM_REGS; regnum++)
1150     if (frame->fsr.regs[regnum] != 0)
1151       write_register (regnum,
1152                       read_memory_integer (frame->fsr.regs[regnum], 4));
1153
1154   write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
1155   write_register (SP_REGNUM, old_SP);
1156
1157   flush_cached_frames ();
1158 }
1159
1160 static void
1161 print_fpu_flags (flags)
1162      int flags;
1163 {
1164   if (flags & (1 << 0))
1165     fputs ("IVO ", stdout);
1166   if (flags & (1 << 1))
1167     fputs ("DVZ ", stdout);
1168   if (flags & (1 << 2))
1169     fputs ("OFL ", stdout);
1170   if (flags & (1 << 3))
1171     fputs ("UFL ", stdout);
1172   if (flags & (1 << 4))
1173     fputs ("INX ", stdout);
1174   putchar ('\n');
1175 }
1176
1177 void
1178 arm_float_info ()
1179 {
1180   register unsigned long status = read_register (FPS_REGNUM);
1181   int type;
1182
1183   type = (status >> 24) & 127;
1184   printf ("%s FPU type %d\n",
1185           (status & (1<<31)) ? "Hardware" : "Software",
1186           type);
1187   fputs ("mask: ", stdout);
1188   print_fpu_flags (status >> 16);
1189   fputs ("flags: ", stdout);
1190   print_fpu_flags (status);
1191 }
1192
1193 /* If the disassembly mode is APCS, we have to also switch the
1194    bfd mach_type.  This function is run in the set disassembly_flavor
1195    command, and does that.  */
1196
1197 static void
1198 set_disassembly_flavor_sfunc (args, from_tty, c)
1199      char *args;
1200      int from_tty;
1201      struct cmd_list_element *c;
1202 {
1203   set_disassembly_flavor ();
1204   
1205   if (disassembly_flavor_hook != NULL)
1206     disassembly_flavor_hook(args, from_tty);
1207 }
1208
1209 static void
1210 set_disassembly_flavor ()
1211 {
1212   if (disassembly_flavor == apcs_flavor)
1213     {
1214       if (arm_toggle_regnames () == 0)
1215         arm_toggle_regnames ();
1216       arm_register_names = apcs_register_names;
1217     }
1218   else if (disassembly_flavor == r_prefix_flavor)
1219     {
1220       if (arm_toggle_regnames () == 1)
1221         arm_toggle_regnames ();
1222       arm_register_names =  additional_register_names;
1223     }       
1224 }
1225
1226 /* arm_othernames implements the "othernames" command.  This is kind of
1227    hacky, and I prefer the set-show disassembly-flavor which is also used
1228    for the x86 gdb.  I will keep this around, however, in case anyone is
1229    actually using it. */
1230
1231 static void
1232 arm_othernames ()
1233 {
1234   if (disassembly_flavor == r_prefix_flavor)
1235     {
1236       disassembly_flavor = apcs_flavor;
1237       set_disassembly_flavor ();
1238     }
1239   else
1240     {
1241       disassembly_flavor = r_prefix_flavor;
1242       set_disassembly_flavor ();
1243     }
1244 }
1245
1246 /* FIXME:  Fill in with the 'right thing', see asm 
1247    template in arm-convert.s */
1248
1249 void
1250 convert_from_extended (ptr, dbl)
1251      void *ptr;
1252      double *dbl;
1253 {
1254   *dbl = *(double *) ptr;
1255 }
1256
1257 void
1258 convert_to_extended (dbl, ptr)
1259      void *ptr;
1260      double *dbl;
1261 {
1262   *(double *) ptr = *dbl;
1263 }
1264
1265 static int
1266 condition_true (cond, status_reg)
1267      unsigned long cond;
1268      unsigned long status_reg;
1269 {
1270   if (cond == INST_AL || cond == INST_NV)
1271     return 1;
1272
1273   switch (cond)
1274     {
1275     case INST_EQ:
1276       return ((status_reg & FLAG_Z) != 0);
1277     case INST_NE:
1278       return ((status_reg & FLAG_Z) == 0);
1279     case INST_CS:
1280       return ((status_reg & FLAG_C) != 0);
1281     case INST_CC:
1282       return ((status_reg & FLAG_C) == 0);
1283     case INST_MI:
1284       return ((status_reg & FLAG_N) != 0);
1285     case INST_PL:
1286       return ((status_reg & FLAG_N) == 0);
1287     case INST_VS:
1288       return ((status_reg & FLAG_V) != 0);
1289     case INST_VC:
1290       return ((status_reg & FLAG_V) == 0);
1291     case INST_HI:
1292       return ((status_reg & (FLAG_C | FLAG_Z)) == FLAG_C);
1293     case INST_LS:
1294       return ((status_reg & (FLAG_C | FLAG_Z)) != FLAG_C);
1295     case INST_GE:
1296       return (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0));
1297     case INST_LT:
1298       return (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0));
1299     case INST_GT:
1300       return (((status_reg & FLAG_Z) == 0) &&
1301             (((status_reg & FLAG_N) == 0) == ((status_reg & FLAG_V) == 0)));
1302     case INST_LE:
1303       return (((status_reg & FLAG_Z) != 0) ||
1304             (((status_reg & FLAG_N) == 0) != ((status_reg & FLAG_V) == 0)));
1305     }
1306   return 1;
1307 }
1308
1309 #define submask(x) ((1L << ((x) + 1)) - 1)
1310 #define bit(obj,st) (((obj) >> (st)) & 1)
1311 #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
1312 #define sbits(obj,st,fn) \
1313   ((long) (bits(obj,st,fn) | ((long) bit(obj,fn) * ~ submask (fn - st))))
1314 #define BranchDest(addr,instr) \
1315   ((CORE_ADDR) (((long) (addr)) + 8 + (sbits (instr, 0, 23) << 2)))
1316 #define ARM_PC_32 1
1317
1318 static unsigned long
1319 shifted_reg_val (inst, carry, pc_val, status_reg)
1320      unsigned long inst;
1321      int carry;
1322      unsigned long pc_val;
1323      unsigned long status_reg;
1324 {
1325   unsigned long res, shift;
1326   int rm = bits (inst, 0, 3);
1327   unsigned long shifttype = bits (inst, 5, 6);
1328
1329   if (bit (inst, 4))
1330     {
1331       int rs = bits (inst, 8, 11);
1332       shift = (rs == 15 ? pc_val + 8 : read_register (rs)) & 0xFF;
1333     }
1334   else
1335     shift = bits (inst, 7, 11);
1336
1337   res = (rm == 15
1338          ? ((pc_val | (ARM_PC_32 ? 0 : status_reg))
1339             + (bit (inst, 4) ? 12 : 8))
1340          : read_register (rm));
1341
1342   switch (shifttype)
1343     {
1344     case 0:                     /* LSL */
1345       res = shift >= 32 ? 0 : res << shift;
1346       break;
1347
1348     case 1:                     /* LSR */
1349       res = shift >= 32 ? 0 : res >> shift;
1350       break;
1351
1352     case 2:                     /* ASR */
1353       if (shift >= 32)
1354         shift = 31;
1355       res = ((res & 0x80000000L)
1356              ? ~((~res) >> shift) : res >> shift);
1357       break;
1358
1359     case 3:                     /* ROR/RRX */
1360       shift &= 31;
1361       if (shift == 0)
1362         res = (res >> 1) | (carry ? 0x80000000L : 0);
1363       else
1364         res = (res >> shift) | (res << (32 - shift));
1365       break;
1366     }
1367
1368   return res & 0xffffffff;
1369 }
1370
1371
1372 /* Return number of 1-bits in VAL.  */
1373
1374 static int
1375 bitcount (val)
1376      unsigned long val;
1377 {
1378   int nbits;
1379   for (nbits = 0; val != 0; nbits++)
1380     val &= val - 1;             /* delete rightmost 1-bit in val */
1381   return nbits;
1382 }
1383
1384
1385 static CORE_ADDR
1386 thumb_get_next_pc (pc)
1387      CORE_ADDR pc;
1388 {
1389   unsigned long pc_val = ((unsigned long) pc) + 4;      /* PC after prefetch */
1390   unsigned short inst1 = read_memory_integer (pc, 2);
1391   CORE_ADDR nextpc = pc + 2;    /* default is next instruction */
1392   unsigned long offset;
1393
1394   if ((inst1 & 0xff00) == 0xbd00)       /* pop {rlist, pc} */
1395     {
1396       CORE_ADDR sp;
1397
1398       /* Fetch the saved PC from the stack.  It's stored above
1399          all of the other registers.  */
1400       offset = bitcount (bits (inst1, 0, 7)) * REGISTER_SIZE;
1401       sp = read_register (SP_REGNUM);
1402       nextpc = (CORE_ADDR) read_memory_integer (sp + offset, 4);
1403       nextpc = ADDR_BITS_REMOVE (nextpc);
1404       if (nextpc == pc)
1405         error ("Infinite loop detected");
1406     }
1407   else if ((inst1 & 0xf000) == 0xd000)  /* conditional branch */
1408     {
1409       unsigned long status = read_register (PS_REGNUM);
1410       unsigned long cond = bits (inst1, 8, 11);
1411       if (cond != 0x0f && condition_true (cond, status))        /* 0x0f = SWI */
1412         nextpc = pc_val + (sbits (inst1, 0, 7) << 1);
1413     }
1414   else if ((inst1 & 0xf800) == 0xe000)  /* unconditional branch */
1415     {
1416       nextpc = pc_val + (sbits (inst1, 0, 10) << 1);
1417     }
1418   else if ((inst1 & 0xf800) == 0xf000)  /* long branch with link */
1419     {
1420       unsigned short inst2 = read_memory_integer (pc + 2, 2);
1421       offset = (sbits (inst1, 0, 10) << 12) + (bits (inst2, 0, 10) << 1);
1422       nextpc = pc_val + offset;
1423     }
1424
1425   return nextpc;
1426 }
1427
1428
1429 CORE_ADDR
1430 arm_get_next_pc (pc)
1431      CORE_ADDR pc;
1432 {
1433   unsigned long pc_val;
1434   unsigned long this_instr;
1435   unsigned long status;
1436   CORE_ADDR nextpc;
1437
1438   if (arm_pc_is_thumb (pc))
1439     return thumb_get_next_pc (pc);
1440
1441   pc_val = (unsigned long) pc;
1442   this_instr = read_memory_integer (pc, 4);
1443   status = read_register (PS_REGNUM);
1444   nextpc = (CORE_ADDR) (pc_val + 4);    /* Default case */
1445
1446   if (condition_true (bits (this_instr, 28, 31), status))
1447     {
1448       switch (bits (this_instr, 24, 27))
1449         {
1450         case 0x0:
1451         case 0x1:               /* data processing */
1452         case 0x2:
1453         case 0x3:
1454           {
1455             unsigned long operand1, operand2, result = 0;
1456             unsigned long rn;
1457             int c;
1458
1459             if (bits (this_instr, 12, 15) != 15)
1460               break;
1461
1462             if (bits (this_instr, 22, 25) == 0
1463                 && bits (this_instr, 4, 7) == 9)        /* multiply */
1464               error ("Illegal update to pc in instruction");
1465
1466             /* Multiply into PC */
1467             c = (status & FLAG_C) ? 1 : 0;
1468             rn = bits (this_instr, 16, 19);
1469             operand1 = (rn == 15) ? pc_val + 8 : read_register (rn);
1470
1471             if (bit (this_instr, 25))
1472               {
1473                 unsigned long immval = bits (this_instr, 0, 7);
1474                 unsigned long rotate = 2 * bits (this_instr, 8, 11);
1475                 operand2 = ((immval >> rotate) | (immval << (32 - rotate)))
1476                   & 0xffffffff;
1477               }
1478             else                /* operand 2 is a shifted register */
1479               operand2 = shifted_reg_val (this_instr, c, pc_val, status);
1480
1481             switch (bits (this_instr, 21, 24))
1482               {
1483               case 0x0: /*and */
1484                 result = operand1 & operand2;
1485                 break;
1486
1487               case 0x1: /*eor */
1488                 result = operand1 ^ operand2;
1489                 break;
1490
1491               case 0x2: /*sub */
1492                 result = operand1 - operand2;
1493                 break;
1494
1495               case 0x3: /*rsb */
1496                 result = operand2 - operand1;
1497                 break;
1498
1499               case 0x4: /*add */
1500                 result = operand1 + operand2;
1501                 break;
1502
1503               case 0x5: /*adc */
1504                 result = operand1 + operand2 + c;
1505                 break;
1506
1507               case 0x6: /*sbc */
1508                 result = operand1 - operand2 + c;
1509                 break;
1510
1511               case 0x7: /*rsc */
1512                 result = operand2 - operand1 + c;
1513                 break;
1514
1515               case 0x8:
1516               case 0x9:
1517               case 0xa:
1518               case 0xb: /* tst, teq, cmp, cmn */
1519                 result = (unsigned long) nextpc;
1520                 break;
1521
1522               case 0xc: /*orr */
1523                 result = operand1 | operand2;
1524                 break;
1525
1526               case 0xd: /*mov */
1527                 /* Always step into a function.  */
1528                 result = operand2;
1529                 break;
1530
1531               case 0xe: /*bic */
1532                 result = operand1 & ~operand2;
1533                 break;
1534
1535               case 0xf: /*mvn */
1536                 result = ~operand2;
1537                 break;
1538               }
1539             nextpc = (CORE_ADDR) ADDR_BITS_REMOVE (result);
1540
1541             if (nextpc == pc)
1542               error ("Infinite loop detected");
1543             break;
1544           }
1545
1546         case 0x4:
1547         case 0x5:               /* data transfer */
1548         case 0x6:
1549         case 0x7:
1550           if (bit (this_instr, 20))
1551             {
1552               /* load */
1553               if (bits (this_instr, 12, 15) == 15)
1554                 {
1555                   /* rd == pc */
1556                   unsigned long rn;
1557                   unsigned long base;
1558
1559                   if (bit (this_instr, 22))
1560                     error ("Illegal update to pc in instruction");
1561
1562                   /* byte write to PC */
1563                   rn = bits (this_instr, 16, 19);
1564                   base = (rn == 15) ? pc_val + 8 : read_register (rn);
1565                   if (bit (this_instr, 24))
1566                     {
1567                       /* pre-indexed */
1568                       int c = (status & FLAG_C) ? 1 : 0;
1569                       unsigned long offset =
1570                       (bit (this_instr, 25)
1571                        ? shifted_reg_val (this_instr, c, pc_val)
1572                        : bits (this_instr, 0, 11));
1573
1574                       if (bit (this_instr, 23))
1575                         base += offset;
1576                       else
1577                         base -= offset;
1578                     }
1579                   nextpc = (CORE_ADDR) read_memory_integer ((CORE_ADDR) base,
1580                                                             4);
1581
1582                   nextpc = ADDR_BITS_REMOVE (nextpc);
1583
1584                   if (nextpc == pc)
1585                     error ("Infinite loop detected");
1586                 }
1587             }
1588           break;
1589
1590         case 0x8:
1591         case 0x9:               /* block transfer */
1592           if (bit (this_instr, 20))
1593             {
1594               /* LDM */
1595               if (bit (this_instr, 15))
1596                 {
1597                   /* loading pc */
1598                   int offset = 0;
1599
1600                   if (bit (this_instr, 23))
1601                     {
1602                       /* up */
1603                       unsigned long reglist = bits (this_instr, 0, 14);
1604                       offset = bitcount (reglist) * 4;
1605                       if (bit (this_instr, 24))         /* pre */
1606                         offset += 4;
1607                     }
1608                   else if (bit (this_instr, 24))
1609                     offset = -4;
1610
1611                   {
1612                     unsigned long rn_val =
1613                     read_register (bits (this_instr, 16, 19));
1614                     nextpc =
1615                       (CORE_ADDR) read_memory_integer ((CORE_ADDR) (rn_val
1616                                                                   + offset),
1617                                                        4);
1618                   }
1619                   nextpc = ADDR_BITS_REMOVE (nextpc);
1620                   if (nextpc == pc)
1621                     error ("Infinite loop detected");
1622                 }
1623             }
1624           break;
1625
1626         case 0xb:               /* branch & link */
1627         case 0xa:               /* branch */
1628           {
1629             nextpc = BranchDest (pc, this_instr);
1630
1631             nextpc = ADDR_BITS_REMOVE (nextpc);
1632             if (nextpc == pc)
1633               error ("Infinite loop detected");
1634             break;
1635           }
1636
1637         case 0xc:
1638         case 0xd:
1639         case 0xe:               /* coproc ops */
1640         case 0xf:               /* SWI */
1641           break;
1642
1643         default:
1644           fprintf (stderr, "Bad bit-field extraction\n");
1645           return (pc);
1646         }
1647     }
1648
1649   return nextpc;
1650 }
1651
1652 #include "bfd-in2.h"
1653 #include "libcoff.h"
1654
1655 static int
1656 gdb_print_insn_arm (memaddr, info)
1657      bfd_vma memaddr;
1658      disassemble_info *info;
1659 {
1660   if (arm_pc_is_thumb (memaddr))
1661     {
1662       static asymbol *asym;
1663       static combined_entry_type ce;
1664       static struct coff_symbol_struct csym;
1665       static struct _bfd fake_bfd;
1666       static bfd_target fake_target;
1667
1668       if (csym.native == NULL)
1669         {
1670           /* Create a fake symbol vector containing a Thumb symbol.  This is
1671              solely so that the code in print_insn_little_arm() and
1672              print_insn_big_arm() in opcodes/arm-dis.c will detect the presence
1673              of a Thumb symbol and switch to decoding Thumb instructions.  */
1674
1675           fake_target.flavour = bfd_target_coff_flavour;
1676           fake_bfd.xvec = &fake_target;
1677           ce.u.syment.n_sclass = C_THUMBEXTFUNC;
1678           csym.native = &ce;
1679           csym.symbol.the_bfd = &fake_bfd;
1680           csym.symbol.name = "fake";
1681           asym = (asymbol *) & csym;
1682         }
1683
1684       memaddr = UNMAKE_THUMB_ADDR (memaddr);
1685       info->symbols = &asym;
1686     }
1687   else
1688     info->symbols = NULL;
1689
1690   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1691     return print_insn_big_arm (memaddr, info);
1692   else
1693     return print_insn_little_arm (memaddr, info);
1694 }
1695
1696 /* Sequence of bytes for breakpoint instruction.  */
1697 #define ARM_LE_BREAKPOINT {0xFE,0xDE,0xFF,0xE7}         /* Recognized illegal opcodes */
1698 #define ARM_BE_BREAKPOINT {0xE7,0xFF,0xDE,0xFE}
1699 #define THUMB_LE_BREAKPOINT {0xfe,0xdf}
1700 #define THUMB_BE_BREAKPOINT {0xdf,0xfe}
1701
1702 /* The following has been superseded by BREAKPOINT_FOR_PC, but
1703    is defined merely to keep mem-break.c happy.  */
1704 #define LITTLE_BREAKPOINT ARM_LE_BREAKPOINT
1705 #define BIG_BREAKPOINT    ARM_BE_BREAKPOINT
1706
1707 /* This function implements the BREAKPOINT_FROM_PC macro.  It uses the program
1708    counter value to determine whether a 16- or 32-bit breakpoint should be
1709    used.  It returns a pointer to a string of bytes that encode a breakpoint
1710    instruction, stores the length of the string to *lenptr, and adjusts pc
1711    (if necessary) to point to the actual memory location where the
1712    breakpoint should be inserted.  */
1713
1714 unsigned char *
1715 arm_breakpoint_from_pc (pcptr, lenptr)
1716      CORE_ADDR *pcptr;
1717      int *lenptr;
1718 {
1719   if (arm_pc_is_thumb (*pcptr) || arm_pc_is_thumb_dummy (*pcptr))
1720     {
1721       if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1722         {
1723           static char thumb_breakpoint[] = THUMB_BE_BREAKPOINT;
1724           *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
1725           *lenptr = sizeof (thumb_breakpoint);
1726           return thumb_breakpoint;
1727         }
1728       else
1729         {
1730           static char thumb_breakpoint[] = THUMB_LE_BREAKPOINT;
1731           *pcptr = UNMAKE_THUMB_ADDR (*pcptr);
1732           *lenptr = sizeof (thumb_breakpoint);
1733           return thumb_breakpoint;
1734         }
1735     }
1736   else
1737     {
1738       if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1739         {
1740           static char arm_breakpoint[] = ARM_BE_BREAKPOINT;
1741           *lenptr = sizeof (arm_breakpoint);
1742           return arm_breakpoint;
1743         }
1744       else
1745         {
1746           static char arm_breakpoint[] = ARM_LE_BREAKPOINT;
1747           *lenptr = sizeof (arm_breakpoint);
1748           return arm_breakpoint;
1749         }
1750     }
1751 }
1752 /* Return non-zero if the PC is inside a call thunk (aka stub or trampoline).
1753    This implements the IN_SOLIB_CALL_TRAMPOLINE macro.  */
1754
1755 int
1756 arm_in_call_stub (pc, name)
1757      CORE_ADDR pc;
1758      char *name;
1759 {
1760   CORE_ADDR start_addr;
1761
1762   /* Find the starting address of the function containing the PC.  If the
1763      caller didn't give us a name, look it up at the same time.  */
1764   if (find_pc_partial_function (pc, name ? NULL : &name, &start_addr, NULL) == 0)
1765     return 0;
1766
1767   return strncmp (name, "_call_via_r", 11) == 0;
1768 }
1769
1770
1771 /* If PC is in a Thumb call or return stub, return the address of the target
1772    PC, which is in a register.  The thunk functions are called _called_via_xx,
1773    where x is the register name.  The possible names are r0-r9, sl, fp, ip,
1774    sp, and lr. */
1775
1776 CORE_ADDR
1777 arm_skip_stub (pc)
1778      CORE_ADDR pc;
1779 {
1780   char *name;
1781   CORE_ADDR start_addr;
1782
1783   /* Find the starting address and name of the function containing the PC.  */
1784   if (find_pc_partial_function (pc, &name, &start_addr, NULL) == 0)
1785     return 0;
1786
1787   /* Call thunks always start with "_call_via_".  */
1788   if (strncmp (name, "_call_via_", 10) == 0)
1789     {
1790       /* Use the name suffix to determine which register contains
1791          the target PC.  */
1792       static char *table[15] =
1793       {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1794        "r8", "r9", "sl", "fp", "ip", "sp", "lr"
1795       };
1796       int regno;
1797
1798       for (regno = 0; regno <= 14; regno++)
1799         if (strcmp (&name[10], table[regno]) == 0)
1800           return read_register (regno);
1801     }
1802   return 0;                     /* not a stub */
1803 }
1804
1805
1806 void
1807 _initialize_arm_tdep ()
1808 {
1809   struct cmd_list_element *new_cmd;
1810
1811   tm_print_insn = gdb_print_insn_arm;
1812   
1813   /* Sync the opcode insn printer with our register viewer: */
1814
1815   if (arm_toggle_regnames () != 1)
1816     arm_toggle_regnames ();
1817
1818   /* Add the deprecated "othernames" command */
1819   
1820   add_com ("othernames", class_obscure, arm_othernames,
1821            "Switch to the other set of register names.");
1822
1823   /* Add the disassembly-flavor command */
1824   
1825   new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1826                                   valid_flavors,
1827                                   (char *) &disassembly_flavor,
1828                                   "Set the disassembly flavor, \
1829 the valid values are \"apcs\" and \"r-prefix\", \
1830 and the default value is \"apcs\".",
1831                                   &setlist);
1832   new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
1833   add_show_from_set(new_cmd, &showlist);
1834   
1835   /* ??? Maybe this should be a boolean.  */
1836   add_show_from_set (add_set_cmd ("apcs32", no_class,
1837                                   var_zinteger, (char *)&arm_apcs_32,
1838                                   "Set usage of ARM 32-bit mode.\n", &setlist),
1839                      & showlist);
1840
1841 }
1842
1843 /* Test whether the coff symbol specific value corresponds to a Thumb function */
1844 int
1845 coff_sym_is_thumb (int val)
1846 {
1847   return (val == C_THUMBEXT ||
1848           val == C_THUMBSTAT ||
1849           val == C_THUMBEXTFUNC ||
1850           val == C_THUMBSTATFUNC ||
1851           val == C_THUMBLABEL);
1852 }