6f2e38e0befa6bd2ef94749998b971439dfe165c
[external/binutils.git] / gdb / aarch64-tdep.c
1 /* Common target dependent code for GDB on AArch64 systems.
2
3    Copyright (C) 2009-2016 Free Software Foundation, Inc.
4    Contributed by ARM Ltd.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22
23 #include "frame.h"
24 #include "inferior.h"
25 #include "gdbcmd.h"
26 #include "gdbcore.h"
27 #include "dis-asm.h"
28 #include "regcache.h"
29 #include "reggroups.h"
30 #include "doublest.h"
31 #include "value.h"
32 #include "arch-utils.h"
33 #include "osabi.h"
34 #include "frame-unwind.h"
35 #include "frame-base.h"
36 #include "trad-frame.h"
37 #include "objfiles.h"
38 #include "dwarf2-frame.h"
39 #include "gdbtypes.h"
40 #include "prologue-value.h"
41 #include "target-descriptions.h"
42 #include "user-regs.h"
43 #include "language.h"
44 #include "infcall.h"
45 #include "ax.h"
46 #include "ax-gdb.h"
47
48 #include "aarch64-tdep.h"
49
50 #include "elf-bfd.h"
51 #include "elf/aarch64.h"
52
53 #include "vec.h"
54
55 #include "record.h"
56 #include "record-full.h"
57
58 #include "features/aarch64.c"
59
60 #include "arch/aarch64-insn.h"
61
62 #include "opcode/aarch64.h"
63
64 #define submask(x) ((1L << ((x) + 1)) - 1)
65 #define bit(obj,st) (((obj) >> (st)) & 1)
66 #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st)))
67
68 /* Pseudo register base numbers.  */
69 #define AARCH64_Q0_REGNUM 0
70 #define AARCH64_D0_REGNUM (AARCH64_Q0_REGNUM + 32)
71 #define AARCH64_S0_REGNUM (AARCH64_D0_REGNUM + 32)
72 #define AARCH64_H0_REGNUM (AARCH64_S0_REGNUM + 32)
73 #define AARCH64_B0_REGNUM (AARCH64_H0_REGNUM + 32)
74
75 /* The standard register names, and all the valid aliases for them.  */
76 static const struct
77 {
78   const char *const name;
79   int regnum;
80 } aarch64_register_aliases[] =
81 {
82   /* 64-bit register names.  */
83   {"fp", AARCH64_FP_REGNUM},
84   {"lr", AARCH64_LR_REGNUM},
85   {"sp", AARCH64_SP_REGNUM},
86
87   /* 32-bit register names.  */
88   {"w0", AARCH64_X0_REGNUM + 0},
89   {"w1", AARCH64_X0_REGNUM + 1},
90   {"w2", AARCH64_X0_REGNUM + 2},
91   {"w3", AARCH64_X0_REGNUM + 3},
92   {"w4", AARCH64_X0_REGNUM + 4},
93   {"w5", AARCH64_X0_REGNUM + 5},
94   {"w6", AARCH64_X0_REGNUM + 6},
95   {"w7", AARCH64_X0_REGNUM + 7},
96   {"w8", AARCH64_X0_REGNUM + 8},
97   {"w9", AARCH64_X0_REGNUM + 9},
98   {"w10", AARCH64_X0_REGNUM + 10},
99   {"w11", AARCH64_X0_REGNUM + 11},
100   {"w12", AARCH64_X0_REGNUM + 12},
101   {"w13", AARCH64_X0_REGNUM + 13},
102   {"w14", AARCH64_X0_REGNUM + 14},
103   {"w15", AARCH64_X0_REGNUM + 15},
104   {"w16", AARCH64_X0_REGNUM + 16},
105   {"w17", AARCH64_X0_REGNUM + 17},
106   {"w18", AARCH64_X0_REGNUM + 18},
107   {"w19", AARCH64_X0_REGNUM + 19},
108   {"w20", AARCH64_X0_REGNUM + 20},
109   {"w21", AARCH64_X0_REGNUM + 21},
110   {"w22", AARCH64_X0_REGNUM + 22},
111   {"w23", AARCH64_X0_REGNUM + 23},
112   {"w24", AARCH64_X0_REGNUM + 24},
113   {"w25", AARCH64_X0_REGNUM + 25},
114   {"w26", AARCH64_X0_REGNUM + 26},
115   {"w27", AARCH64_X0_REGNUM + 27},
116   {"w28", AARCH64_X0_REGNUM + 28},
117   {"w29", AARCH64_X0_REGNUM + 29},
118   {"w30", AARCH64_X0_REGNUM + 30},
119
120   /*  specials */
121   {"ip0", AARCH64_X0_REGNUM + 16},
122   {"ip1", AARCH64_X0_REGNUM + 17}
123 };
124
125 /* The required core 'R' registers.  */
126 static const char *const aarch64_r_register_names[] =
127 {
128   /* These registers must appear in consecutive RAW register number
129      order and they must begin with AARCH64_X0_REGNUM! */
130   "x0", "x1", "x2", "x3",
131   "x4", "x5", "x6", "x7",
132   "x8", "x9", "x10", "x11",
133   "x12", "x13", "x14", "x15",
134   "x16", "x17", "x18", "x19",
135   "x20", "x21", "x22", "x23",
136   "x24", "x25", "x26", "x27",
137   "x28", "x29", "x30", "sp",
138   "pc", "cpsr"
139 };
140
141 /* The FP/SIMD 'V' registers.  */
142 static const char *const aarch64_v_register_names[] =
143 {
144   /* These registers must appear in consecutive RAW register number
145      order and they must begin with AARCH64_V0_REGNUM! */
146   "v0", "v1", "v2", "v3",
147   "v4", "v5", "v6", "v7",
148   "v8", "v9", "v10", "v11",
149   "v12", "v13", "v14", "v15",
150   "v16", "v17", "v18", "v19",
151   "v20", "v21", "v22", "v23",
152   "v24", "v25", "v26", "v27",
153   "v28", "v29", "v30", "v31",
154   "fpsr",
155   "fpcr"
156 };
157
158 /* AArch64 prologue cache structure.  */
159 struct aarch64_prologue_cache
160 {
161   /* The program counter at the start of the function.  It is used to
162      identify this frame as a prologue frame.  */
163   CORE_ADDR func;
164
165   /* The program counter at the time this frame was created; i.e. where
166      this function was called from.  It is used to identify this frame as a
167      stub frame.  */
168   CORE_ADDR prev_pc;
169
170   /* The stack pointer at the time this frame was created; i.e. the
171      caller's stack pointer when this function was called.  It is used
172      to identify this frame.  */
173   CORE_ADDR prev_sp;
174
175   /* Is the target available to read from?  */
176   int available_p;
177
178   /* The frame base for this frame is just prev_sp - frame size.
179      FRAMESIZE is the distance from the frame pointer to the
180      initial stack pointer.  */
181   int framesize;
182
183   /* The register used to hold the frame pointer for this frame.  */
184   int framereg;
185
186   /* Saved register offsets.  */
187   struct trad_frame_saved_reg *saved_regs;
188 };
189
190 static void
191 show_aarch64_debug (struct ui_file *file, int from_tty,
192                     struct cmd_list_element *c, const char *value)
193 {
194   fprintf_filtered (file, _("AArch64 debugging is %s.\n"), value);
195 }
196
197 /* Analyze a prologue, looking for a recognizable stack frame
198    and frame pointer.  Scan until we encounter a store that could
199    clobber the stack frame unexpectedly, or an unknown instruction.  */
200
201 static CORE_ADDR
202 aarch64_analyze_prologue (struct gdbarch *gdbarch,
203                           CORE_ADDR start, CORE_ADDR limit,
204                           struct aarch64_prologue_cache *cache)
205 {
206   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
207   int i;
208   pv_t regs[AARCH64_X_REGISTER_COUNT];
209   struct pv_area *stack;
210   struct cleanup *back_to;
211
212   for (i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
213     regs[i] = pv_register (i, 0);
214   stack = make_pv_area (AARCH64_SP_REGNUM, gdbarch_addr_bit (gdbarch));
215   back_to = make_cleanup_free_pv_area (stack);
216
217   for (; start < limit; start += 4)
218     {
219       uint32_t insn;
220       aarch64_inst inst;
221
222       insn = read_memory_unsigned_integer (start, 4, byte_order_for_code);
223
224       if (aarch64_decode_insn (insn, &inst, 1) != 0)
225         break;
226
227       if (inst.opcode->iclass == addsub_imm
228           && (inst.opcode->op == OP_ADD
229               || strcmp ("sub", inst.opcode->name) == 0))
230         {
231           unsigned rd = inst.operands[0].reg.regno;
232           unsigned rn = inst.operands[1].reg.regno;
233
234           gdb_assert (aarch64_num_of_operands (inst.opcode) == 3);
235           gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd_SP);
236           gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn_SP);
237           gdb_assert (inst.operands[2].type == AARCH64_OPND_AIMM);
238
239           if (inst.opcode->op == OP_ADD)
240             {
241               regs[rd] = pv_add_constant (regs[rn],
242                                           inst.operands[2].imm.value);
243             }
244           else
245             {
246               regs[rd] = pv_add_constant (regs[rn],
247                                           -inst.operands[2].imm.value);
248             }
249         }
250       else if (inst.opcode->iclass == pcreladdr
251                && inst.operands[1].type == AARCH64_OPND_ADDR_ADRP)
252         {
253           gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
254           gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
255
256           regs[inst.operands[0].reg.regno] = pv_unknown ();
257         }
258       else if (inst.opcode->iclass == branch_imm)
259         {
260           /* Stop analysis on branch.  */
261           break;
262         }
263       else if (inst.opcode->iclass == condbranch)
264         {
265           /* Stop analysis on branch.  */
266           break;
267         }
268       else if (inst.opcode->iclass == branch_reg)
269         {
270           /* Stop analysis on branch.  */
271           break;
272         }
273       else if (inst.opcode->iclass == compbranch)
274         {
275           /* Stop analysis on branch.  */
276           break;
277         }
278       else if (inst.opcode->op == OP_MOVZ)
279         {
280           gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
281           regs[inst.operands[0].reg.regno] = pv_unknown ();
282         }
283       else if (inst.opcode->iclass == log_shift
284                && strcmp (inst.opcode->name, "orr") == 0)
285         {
286           unsigned rd = inst.operands[0].reg.regno;
287           unsigned rn = inst.operands[1].reg.regno;
288           unsigned rm = inst.operands[2].reg.regno;
289
290           gdb_assert (inst.operands[0].type == AARCH64_OPND_Rd);
291           gdb_assert (inst.operands[1].type == AARCH64_OPND_Rn);
292           gdb_assert (inst.operands[2].type == AARCH64_OPND_Rm_SFT);
293
294           if (inst.operands[2].shifter.amount == 0
295               && rn == AARCH64_SP_REGNUM)
296             regs[rd] = regs[rm];
297           else
298             {
299               if (aarch64_debug)
300                 {
301                   debug_printf ("aarch64: prologue analysis gave up "
302                                 "addr=%s opcode=0x%x (orr x register)\n",
303                                 core_addr_to_string_nz (start), insn);
304                 }
305               break;
306             }
307         }
308       else if (inst.opcode->op == OP_STUR)
309         {
310           unsigned rt = inst.operands[0].reg.regno;
311           unsigned rn = inst.operands[1].addr.base_regno;
312           int is64
313             = (aarch64_get_qualifier_esize (inst.operands[0].qualifier) == 8);
314
315           gdb_assert (aarch64_num_of_operands (inst.opcode) == 2);
316           gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt);
317           gdb_assert (inst.operands[1].type == AARCH64_OPND_ADDR_SIMM9);
318           gdb_assert (!inst.operands[1].addr.offset.is_reg);
319
320           pv_area_store (stack, pv_add_constant (regs[rn],
321                                                  inst.operands[1].addr.offset.imm),
322                          is64 ? 8 : 4, regs[rt]);
323         }
324       else if ((inst.opcode->iclass == ldstpair_off
325                 || inst.opcode->iclass == ldstpair_indexed)
326                && inst.operands[2].addr.preind
327                && strcmp ("stp", inst.opcode->name) == 0)
328         {
329           unsigned rt1 = inst.operands[0].reg.regno;
330           unsigned rt2 = inst.operands[1].reg.regno;
331           unsigned rn = inst.operands[2].addr.base_regno;
332           int32_t imm = inst.operands[2].addr.offset.imm;
333
334           gdb_assert (inst.operands[0].type == AARCH64_OPND_Rt);
335           gdb_assert (inst.operands[1].type == AARCH64_OPND_Rt2);
336           gdb_assert (inst.operands[2].type == AARCH64_OPND_ADDR_SIMM7);
337           gdb_assert (!inst.operands[2].addr.offset.is_reg);
338
339           /* If recording this store would invalidate the store area
340              (perhaps because rn is not known) then we should abandon
341              further prologue analysis.  */
342           if (pv_area_store_would_trash (stack,
343                                          pv_add_constant (regs[rn], imm)))
344             break;
345
346           if (pv_area_store_would_trash (stack,
347                                          pv_add_constant (regs[rn], imm + 8)))
348             break;
349
350           pv_area_store (stack, pv_add_constant (regs[rn], imm), 8,
351                          regs[rt1]);
352           pv_area_store (stack, pv_add_constant (regs[rn], imm + 8), 8,
353                          regs[rt2]);
354
355           if (inst.operands[2].addr.writeback)
356             regs[rn] = pv_add_constant (regs[rn], imm);
357
358         }
359       else if (inst.opcode->iclass == testbranch)
360         {
361           /* Stop analysis on branch.  */
362           break;
363         }
364       else
365         {
366           if (aarch64_debug)
367             {
368               debug_printf ("aarch64: prologue analysis gave up addr=%s"
369                             " opcode=0x%x\n",
370                             core_addr_to_string_nz (start), insn);
371             }
372           break;
373         }
374     }
375
376   if (cache == NULL)
377     {
378       do_cleanups (back_to);
379       return start;
380     }
381
382   if (pv_is_register (regs[AARCH64_FP_REGNUM], AARCH64_SP_REGNUM))
383     {
384       /* Frame pointer is fp.  Frame size is constant.  */
385       cache->framereg = AARCH64_FP_REGNUM;
386       cache->framesize = -regs[AARCH64_FP_REGNUM].k;
387     }
388   else if (pv_is_register (regs[AARCH64_SP_REGNUM], AARCH64_SP_REGNUM))
389     {
390       /* Try the stack pointer.  */
391       cache->framesize = -regs[AARCH64_SP_REGNUM].k;
392       cache->framereg = AARCH64_SP_REGNUM;
393     }
394   else
395     {
396       /* We're just out of luck.  We don't know where the frame is.  */
397       cache->framereg = -1;
398       cache->framesize = 0;
399     }
400
401   for (i = 0; i < AARCH64_X_REGISTER_COUNT; i++)
402     {
403       CORE_ADDR offset;
404
405       if (pv_area_find_reg (stack, gdbarch, i, &offset))
406         cache->saved_regs[i].addr = offset;
407     }
408
409   do_cleanups (back_to);
410   return start;
411 }
412
413 /* Implement the "skip_prologue" gdbarch method.  */
414
415 static CORE_ADDR
416 aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
417 {
418   unsigned long inst;
419   CORE_ADDR skip_pc;
420   CORE_ADDR func_addr, limit_pc;
421   struct symtab_and_line sal;
422
423   /* See if we can determine the end of the prologue via the symbol
424      table.  If so, then return either PC, or the PC after the
425      prologue, whichever is greater.  */
426   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
427     {
428       CORE_ADDR post_prologue_pc
429         = skip_prologue_using_sal (gdbarch, func_addr);
430
431       if (post_prologue_pc != 0)
432         return max (pc, post_prologue_pc);
433     }
434
435   /* Can't determine prologue from the symbol table, need to examine
436      instructions.  */
437
438   /* Find an upper limit on the function prologue using the debug
439      information.  If the debug information could not be used to
440      provide that bound, then use an arbitrary large number as the
441      upper bound.  */
442   limit_pc = skip_prologue_using_sal (gdbarch, pc);
443   if (limit_pc == 0)
444     limit_pc = pc + 128;        /* Magic.  */
445
446   /* Try disassembling prologue.  */
447   return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
448 }
449
450 /* Scan the function prologue for THIS_FRAME and populate the prologue
451    cache CACHE.  */
452
453 static void
454 aarch64_scan_prologue (struct frame_info *this_frame,
455                        struct aarch64_prologue_cache *cache)
456 {
457   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
458   CORE_ADDR prologue_start;
459   CORE_ADDR prologue_end;
460   CORE_ADDR prev_pc = get_frame_pc (this_frame);
461   struct gdbarch *gdbarch = get_frame_arch (this_frame);
462
463   cache->prev_pc = prev_pc;
464
465   /* Assume we do not find a frame.  */
466   cache->framereg = -1;
467   cache->framesize = 0;
468
469   if (find_pc_partial_function (block_addr, NULL, &prologue_start,
470                                 &prologue_end))
471     {
472       struct symtab_and_line sal = find_pc_line (prologue_start, 0);
473
474       if (sal.line == 0)
475         {
476           /* No line info so use the current PC.  */
477           prologue_end = prev_pc;
478         }
479       else if (sal.end < prologue_end)
480         {
481           /* The next line begins after the function end.  */
482           prologue_end = sal.end;
483         }
484
485       prologue_end = min (prologue_end, prev_pc);
486       aarch64_analyze_prologue (gdbarch, prologue_start, prologue_end, cache);
487     }
488   else
489     {
490       CORE_ADDR frame_loc;
491       LONGEST saved_fp;
492       LONGEST saved_lr;
493       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
494
495       frame_loc = get_frame_register_unsigned (this_frame, AARCH64_FP_REGNUM);
496       if (frame_loc == 0)
497         return;
498
499       cache->framereg = AARCH64_FP_REGNUM;
500       cache->framesize = 16;
501       cache->saved_regs[29].addr = 0;
502       cache->saved_regs[30].addr = 8;
503     }
504 }
505
506 /* Fill in *CACHE with information about the prologue of *THIS_FRAME.  This
507    function may throw an exception if the inferior's registers or memory is
508    not available.  */
509
510 static void
511 aarch64_make_prologue_cache_1 (struct frame_info *this_frame,
512                                struct aarch64_prologue_cache *cache)
513 {
514   CORE_ADDR unwound_fp;
515   int reg;
516
517   aarch64_scan_prologue (this_frame, cache);
518
519   if (cache->framereg == -1)
520     return;
521
522   unwound_fp = get_frame_register_unsigned (this_frame, cache->framereg);
523   if (unwound_fp == 0)
524     return;
525
526   cache->prev_sp = unwound_fp + cache->framesize;
527
528   /* Calculate actual addresses of saved registers using offsets
529      determined by aarch64_analyze_prologue.  */
530   for (reg = 0; reg < gdbarch_num_regs (get_frame_arch (this_frame)); reg++)
531     if (trad_frame_addr_p (cache->saved_regs, reg))
532       cache->saved_regs[reg].addr += cache->prev_sp;
533
534   cache->func = get_frame_func (this_frame);
535
536   cache->available_p = 1;
537 }
538
539 /* Allocate and fill in *THIS_CACHE with information about the prologue of
540    *THIS_FRAME.  Do not do this is if *THIS_CACHE was already allocated.
541    Return a pointer to the current aarch64_prologue_cache in
542    *THIS_CACHE.  */
543
544 static struct aarch64_prologue_cache *
545 aarch64_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
546 {
547   struct aarch64_prologue_cache *cache;
548
549   if (*this_cache != NULL)
550     return (struct aarch64_prologue_cache *) *this_cache;
551
552   cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
553   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
554   *this_cache = cache;
555
556   TRY
557     {
558       aarch64_make_prologue_cache_1 (this_frame, cache);
559     }
560   CATCH (ex, RETURN_MASK_ERROR)
561     {
562       if (ex.error != NOT_AVAILABLE_ERROR)
563         throw_exception (ex);
564     }
565   END_CATCH
566
567   return cache;
568 }
569
570 /* Implement the "stop_reason" frame_unwind method.  */
571
572 static enum unwind_stop_reason
573 aarch64_prologue_frame_unwind_stop_reason (struct frame_info *this_frame,
574                                            void **this_cache)
575 {
576   struct aarch64_prologue_cache *cache
577     = aarch64_make_prologue_cache (this_frame, this_cache);
578
579   if (!cache->available_p)
580     return UNWIND_UNAVAILABLE;
581
582   /* Halt the backtrace at "_start".  */
583   if (cache->prev_pc <= gdbarch_tdep (get_frame_arch (this_frame))->lowest_pc)
584     return UNWIND_OUTERMOST;
585
586   /* We've hit a wall, stop.  */
587   if (cache->prev_sp == 0)
588     return UNWIND_OUTERMOST;
589
590   return UNWIND_NO_REASON;
591 }
592
593 /* Our frame ID for a normal frame is the current function's starting
594    PC and the caller's SP when we were called.  */
595
596 static void
597 aarch64_prologue_this_id (struct frame_info *this_frame,
598                           void **this_cache, struct frame_id *this_id)
599 {
600   struct aarch64_prologue_cache *cache
601     = aarch64_make_prologue_cache (this_frame, this_cache);
602
603   if (!cache->available_p)
604     *this_id = frame_id_build_unavailable_stack (cache->func);
605   else
606     *this_id = frame_id_build (cache->prev_sp, cache->func);
607 }
608
609 /* Implement the "prev_register" frame_unwind method.  */
610
611 static struct value *
612 aarch64_prologue_prev_register (struct frame_info *this_frame,
613                                 void **this_cache, int prev_regnum)
614 {
615   struct gdbarch *gdbarch = get_frame_arch (this_frame);
616   struct aarch64_prologue_cache *cache
617     = aarch64_make_prologue_cache (this_frame, this_cache);
618
619   /* If we are asked to unwind the PC, then we need to return the LR
620      instead.  The prologue may save PC, but it will point into this
621      frame's prologue, not the next frame's resume location.  */
622   if (prev_regnum == AARCH64_PC_REGNUM)
623     {
624       CORE_ADDR lr;
625
626       lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
627       return frame_unwind_got_constant (this_frame, prev_regnum, lr);
628     }
629
630   /* SP is generally not saved to the stack, but this frame is
631      identified by the next frame's stack pointer at the time of the
632      call.  The value was already reconstructed into PREV_SP.  */
633   /*
634          +----------+  ^
635          | saved lr |  |
636       +->| saved fp |--+
637       |  |          |
638       |  |          |     <- Previous SP
639       |  +----------+
640       |  | saved lr |
641       +--| saved fp |<- FP
642          |          |
643          |          |<- SP
644          +----------+  */
645   if (prev_regnum == AARCH64_SP_REGNUM)
646     return frame_unwind_got_constant (this_frame, prev_regnum,
647                                       cache->prev_sp);
648
649   return trad_frame_get_prev_register (this_frame, cache->saved_regs,
650                                        prev_regnum);
651 }
652
653 /* AArch64 prologue unwinder.  */
654 struct frame_unwind aarch64_prologue_unwind =
655 {
656   NORMAL_FRAME,
657   aarch64_prologue_frame_unwind_stop_reason,
658   aarch64_prologue_this_id,
659   aarch64_prologue_prev_register,
660   NULL,
661   default_frame_sniffer
662 };
663
664 /* Allocate and fill in *THIS_CACHE with information about the prologue of
665    *THIS_FRAME.  Do not do this is if *THIS_CACHE was already allocated.
666    Return a pointer to the current aarch64_prologue_cache in
667    *THIS_CACHE.  */
668
669 static struct aarch64_prologue_cache *
670 aarch64_make_stub_cache (struct frame_info *this_frame, void **this_cache)
671 {
672   struct aarch64_prologue_cache *cache;
673
674   if (*this_cache != NULL)
675     return (struct aarch64_prologue_cache *) *this_cache;
676
677   cache = FRAME_OBSTACK_ZALLOC (struct aarch64_prologue_cache);
678   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
679   *this_cache = cache;
680
681   TRY
682     {
683       cache->prev_sp = get_frame_register_unsigned (this_frame,
684                                                     AARCH64_SP_REGNUM);
685       cache->prev_pc = get_frame_pc (this_frame);
686       cache->available_p = 1;
687     }
688   CATCH (ex, RETURN_MASK_ERROR)
689     {
690       if (ex.error != NOT_AVAILABLE_ERROR)
691         throw_exception (ex);
692     }
693   END_CATCH
694
695   return cache;
696 }
697
698 /* Implement the "stop_reason" frame_unwind method.  */
699
700 static enum unwind_stop_reason
701 aarch64_stub_frame_unwind_stop_reason (struct frame_info *this_frame,
702                                        void **this_cache)
703 {
704   struct aarch64_prologue_cache *cache
705     = aarch64_make_stub_cache (this_frame, this_cache);
706
707   if (!cache->available_p)
708     return UNWIND_UNAVAILABLE;
709
710   return UNWIND_NO_REASON;
711 }
712
713 /* Our frame ID for a stub frame is the current SP and LR.  */
714
715 static void
716 aarch64_stub_this_id (struct frame_info *this_frame,
717                       void **this_cache, struct frame_id *this_id)
718 {
719   struct aarch64_prologue_cache *cache
720     = aarch64_make_stub_cache (this_frame, this_cache);
721
722   if (cache->available_p)
723     *this_id = frame_id_build (cache->prev_sp, cache->prev_pc);
724   else
725     *this_id = frame_id_build_unavailable_stack (cache->prev_pc);
726 }
727
728 /* Implement the "sniffer" frame_unwind method.  */
729
730 static int
731 aarch64_stub_unwind_sniffer (const struct frame_unwind *self,
732                              struct frame_info *this_frame,
733                              void **this_prologue_cache)
734 {
735   CORE_ADDR addr_in_block;
736   gdb_byte dummy[4];
737
738   addr_in_block = get_frame_address_in_block (this_frame);
739   if (in_plt_section (addr_in_block)
740       /* We also use the stub winder if the target memory is unreadable
741          to avoid having the prologue unwinder trying to read it.  */
742       || target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
743     return 1;
744
745   return 0;
746 }
747
748 /* AArch64 stub unwinder.  */
749 struct frame_unwind aarch64_stub_unwind =
750 {
751   NORMAL_FRAME,
752   aarch64_stub_frame_unwind_stop_reason,
753   aarch64_stub_this_id,
754   aarch64_prologue_prev_register,
755   NULL,
756   aarch64_stub_unwind_sniffer
757 };
758
759 /* Return the frame base address of *THIS_FRAME.  */
760
761 static CORE_ADDR
762 aarch64_normal_frame_base (struct frame_info *this_frame, void **this_cache)
763 {
764   struct aarch64_prologue_cache *cache
765     = aarch64_make_prologue_cache (this_frame, this_cache);
766
767   return cache->prev_sp - cache->framesize;
768 }
769
770 /* AArch64 default frame base information.  */
771 struct frame_base aarch64_normal_base =
772 {
773   &aarch64_prologue_unwind,
774   aarch64_normal_frame_base,
775   aarch64_normal_frame_base,
776   aarch64_normal_frame_base
777 };
778
779 /* Assuming THIS_FRAME is a dummy, return the frame ID of that
780    dummy frame.  The frame ID's base needs to match the TOS value
781    saved by save_dummy_frame_tos () and returned from
782    aarch64_push_dummy_call, and the PC needs to match the dummy
783    frame's breakpoint.  */
784
785 static struct frame_id
786 aarch64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
787 {
788   return frame_id_build (get_frame_register_unsigned (this_frame,
789                                                       AARCH64_SP_REGNUM),
790                          get_frame_pc (this_frame));
791 }
792
793 /* Implement the "unwind_pc" gdbarch method.  */
794
795 static CORE_ADDR
796 aarch64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
797 {
798   CORE_ADDR pc
799     = frame_unwind_register_unsigned (this_frame, AARCH64_PC_REGNUM);
800
801   return pc;
802 }
803
804 /* Implement the "unwind_sp" gdbarch method.  */
805
806 static CORE_ADDR
807 aarch64_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
808 {
809   return frame_unwind_register_unsigned (this_frame, AARCH64_SP_REGNUM);
810 }
811
812 /* Return the value of the REGNUM register in the previous frame of
813    *THIS_FRAME.  */
814
815 static struct value *
816 aarch64_dwarf2_prev_register (struct frame_info *this_frame,
817                               void **this_cache, int regnum)
818 {
819   struct gdbarch *gdbarch = get_frame_arch (this_frame);
820   CORE_ADDR lr;
821
822   switch (regnum)
823     {
824     case AARCH64_PC_REGNUM:
825       lr = frame_unwind_register_unsigned (this_frame, AARCH64_LR_REGNUM);
826       return frame_unwind_got_constant (this_frame, regnum, lr);
827
828     default:
829       internal_error (__FILE__, __LINE__,
830                       _("Unexpected register %d"), regnum);
831     }
832 }
833
834 /* Implement the "init_reg" dwarf2_frame_ops method.  */
835
836 static void
837 aarch64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
838                                struct dwarf2_frame_state_reg *reg,
839                                struct frame_info *this_frame)
840 {
841   switch (regnum)
842     {
843     case AARCH64_PC_REGNUM:
844       reg->how = DWARF2_FRAME_REG_FN;
845       reg->loc.fn = aarch64_dwarf2_prev_register;
846       break;
847     case AARCH64_SP_REGNUM:
848       reg->how = DWARF2_FRAME_REG_CFA;
849       break;
850     }
851 }
852
853 /* When arguments must be pushed onto the stack, they go on in reverse
854    order.  The code below implements a FILO (stack) to do this.  */
855
856 typedef struct
857 {
858   /* Value to pass on stack.  It can be NULL if this item is for stack
859      padding.  */
860   const gdb_byte *data;
861
862   /* Size in bytes of value to pass on stack.  */
863   int len;
864 } stack_item_t;
865
866 DEF_VEC_O (stack_item_t);
867
868 /* Return the alignment (in bytes) of the given type.  */
869
870 static int
871 aarch64_type_align (struct type *t)
872 {
873   int n;
874   int align;
875   int falign;
876
877   t = check_typedef (t);
878   switch (TYPE_CODE (t))
879     {
880     default:
881       /* Should never happen.  */
882       internal_error (__FILE__, __LINE__, _("unknown type alignment"));
883       return 4;
884
885     case TYPE_CODE_PTR:
886     case TYPE_CODE_ENUM:
887     case TYPE_CODE_INT:
888     case TYPE_CODE_FLT:
889     case TYPE_CODE_SET:
890     case TYPE_CODE_RANGE:
891     case TYPE_CODE_BITSTRING:
892     case TYPE_CODE_REF:
893     case TYPE_CODE_CHAR:
894     case TYPE_CODE_BOOL:
895       return TYPE_LENGTH (t);
896
897     case TYPE_CODE_ARRAY:
898       if (TYPE_VECTOR (t))
899         {
900           /* Use the natural alignment for vector types (the same for
901              scalar type), but the maximum alignment is 128-bit.  */
902           if (TYPE_LENGTH (t) > 16)
903             return 16;
904           else
905             return TYPE_LENGTH (t);
906         }
907       else
908         return aarch64_type_align (TYPE_TARGET_TYPE (t));
909     case TYPE_CODE_COMPLEX:
910       return aarch64_type_align (TYPE_TARGET_TYPE (t));
911
912     case TYPE_CODE_STRUCT:
913     case TYPE_CODE_UNION:
914       align = 1;
915       for (n = 0; n < TYPE_NFIELDS (t); n++)
916         {
917           falign = aarch64_type_align (TYPE_FIELD_TYPE (t, n));
918           if (falign > align)
919             align = falign;
920         }
921       return align;
922     }
923 }
924
925 /* Return 1 if *TY is a homogeneous floating-point aggregate or
926    homogeneous short-vector aggregate as defined in the AAPCS64 ABI
927    document; otherwise return 0.  */
928
929 static int
930 is_hfa_or_hva (struct type *ty)
931 {
932   switch (TYPE_CODE (ty))
933     {
934     case TYPE_CODE_ARRAY:
935       {
936         struct type *target_ty = TYPE_TARGET_TYPE (ty);
937
938         if (TYPE_VECTOR (ty))
939           return 0;
940
941         if (TYPE_LENGTH (ty) <= 4 /* HFA or HVA has at most 4 members.  */
942             && (TYPE_CODE (target_ty) == TYPE_CODE_FLT /* HFA */
943                 || (TYPE_CODE (target_ty) == TYPE_CODE_ARRAY /* HVA */
944                     && TYPE_VECTOR (target_ty))))
945           return 1;
946         break;
947       }
948
949     case TYPE_CODE_UNION:
950     case TYPE_CODE_STRUCT:
951       {
952         /* HFA or HVA has at most four members.  */
953         if (TYPE_NFIELDS (ty) > 0 && TYPE_NFIELDS (ty) <= 4)
954           {
955             struct type *member0_type;
956
957             member0_type = check_typedef (TYPE_FIELD_TYPE (ty, 0));
958             if (TYPE_CODE (member0_type) == TYPE_CODE_FLT
959                 || (TYPE_CODE (member0_type) == TYPE_CODE_ARRAY
960                     && TYPE_VECTOR (member0_type)))
961               {
962                 int i;
963
964                 for (i = 0; i < TYPE_NFIELDS (ty); i++)
965                   {
966                     struct type *member1_type;
967
968                     member1_type = check_typedef (TYPE_FIELD_TYPE (ty, i));
969                     if (TYPE_CODE (member0_type) != TYPE_CODE (member1_type)
970                         || (TYPE_LENGTH (member0_type)
971                             != TYPE_LENGTH (member1_type)))
972                       return 0;
973                   }
974                 return 1;
975               }
976           }
977         return 0;
978       }
979
980     default:
981       break;
982     }
983
984   return 0;
985 }
986
987 /* AArch64 function call information structure.  */
988 struct aarch64_call_info
989 {
990   /* the current argument number.  */
991   unsigned argnum;
992
993   /* The next general purpose register number, equivalent to NGRN as
994      described in the AArch64 Procedure Call Standard.  */
995   unsigned ngrn;
996
997   /* The next SIMD and floating point register number, equivalent to
998      NSRN as described in the AArch64 Procedure Call Standard.  */
999   unsigned nsrn;
1000
1001   /* The next stacked argument address, equivalent to NSAA as
1002      described in the AArch64 Procedure Call Standard.  */
1003   unsigned nsaa;
1004
1005   /* Stack item vector.  */
1006   VEC(stack_item_t) *si;
1007 };
1008
1009 /* Pass a value in a sequence of consecutive X registers.  The caller
1010    is responsbile for ensuring sufficient registers are available.  */
1011
1012 static void
1013 pass_in_x (struct gdbarch *gdbarch, struct regcache *regcache,
1014            struct aarch64_call_info *info, struct type *type,
1015            struct value *arg)
1016 {
1017   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1018   int len = TYPE_LENGTH (type);
1019   enum type_code typecode = TYPE_CODE (type);
1020   int regnum = AARCH64_X0_REGNUM + info->ngrn;
1021   const bfd_byte *buf = value_contents (arg);
1022
1023   info->argnum++;
1024
1025   while (len > 0)
1026     {
1027       int partial_len = len < X_REGISTER_SIZE ? len : X_REGISTER_SIZE;
1028       CORE_ADDR regval = extract_unsigned_integer (buf, partial_len,
1029                                                    byte_order);
1030
1031
1032       /* Adjust sub-word struct/union args when big-endian.  */
1033       if (byte_order == BFD_ENDIAN_BIG
1034           && partial_len < X_REGISTER_SIZE
1035           && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
1036         regval <<= ((X_REGISTER_SIZE - partial_len) * TARGET_CHAR_BIT);
1037
1038       if (aarch64_debug)
1039         {
1040           debug_printf ("arg %d in %s = 0x%s\n", info->argnum,
1041                         gdbarch_register_name (gdbarch, regnum),
1042                         phex (regval, X_REGISTER_SIZE));
1043         }
1044       regcache_cooked_write_unsigned (regcache, regnum, regval);
1045       len -= partial_len;
1046       buf += partial_len;
1047       regnum++;
1048     }
1049 }
1050
1051 /* Attempt to marshall a value in a V register.  Return 1 if
1052    successful, or 0 if insufficient registers are available.  This
1053    function, unlike the equivalent pass_in_x() function does not
1054    handle arguments spread across multiple registers.  */
1055
1056 static int
1057 pass_in_v (struct gdbarch *gdbarch,
1058            struct regcache *regcache,
1059            struct aarch64_call_info *info,
1060            int len, const bfd_byte *buf)
1061 {
1062   if (info->nsrn < 8)
1063     {
1064       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1065       int regnum = AARCH64_V0_REGNUM + info->nsrn;
1066       gdb_byte reg[V_REGISTER_SIZE];
1067
1068       info->argnum++;
1069       info->nsrn++;
1070
1071       memset (reg, 0, sizeof (reg));
1072       /* PCS C.1, the argument is allocated to the least significant
1073          bits of V register.  */
1074       memcpy (reg, buf, len);
1075       regcache_cooked_write (regcache, regnum, reg);
1076
1077       if (aarch64_debug)
1078         {
1079           debug_printf ("arg %d in %s\n", info->argnum,
1080                         gdbarch_register_name (gdbarch, regnum));
1081         }
1082       return 1;
1083     }
1084   info->nsrn = 8;
1085   return 0;
1086 }
1087
1088 /* Marshall an argument onto the stack.  */
1089
1090 static void
1091 pass_on_stack (struct aarch64_call_info *info, struct type *type,
1092                struct value *arg)
1093 {
1094   const bfd_byte *buf = value_contents (arg);
1095   int len = TYPE_LENGTH (type);
1096   int align;
1097   stack_item_t item;
1098
1099   info->argnum++;
1100
1101   align = aarch64_type_align (type);
1102
1103   /* PCS C.17 Stack should be aligned to the larger of 8 bytes or the
1104      Natural alignment of the argument's type.  */
1105   align = align_up (align, 8);
1106
1107   /* The AArch64 PCS requires at most doubleword alignment.  */
1108   if (align > 16)
1109     align = 16;
1110
1111   if (aarch64_debug)
1112     {
1113       debug_printf ("arg %d len=%d @ sp + %d\n", info->argnum, len,
1114                     info->nsaa);
1115     }
1116
1117   item.len = len;
1118   item.data = buf;
1119   VEC_safe_push (stack_item_t, info->si, &item);
1120
1121   info->nsaa += len;
1122   if (info->nsaa & (align - 1))
1123     {
1124       /* Push stack alignment padding.  */
1125       int pad = align - (info->nsaa & (align - 1));
1126
1127       item.len = pad;
1128       item.data = NULL;
1129
1130       VEC_safe_push (stack_item_t, info->si, &item);
1131       info->nsaa += pad;
1132     }
1133 }
1134
1135 /* Marshall an argument into a sequence of one or more consecutive X
1136    registers or, if insufficient X registers are available then onto
1137    the stack.  */
1138
1139 static void
1140 pass_in_x_or_stack (struct gdbarch *gdbarch, struct regcache *regcache,
1141                     struct aarch64_call_info *info, struct type *type,
1142                     struct value *arg)
1143 {
1144   int len = TYPE_LENGTH (type);
1145   int nregs = (len + X_REGISTER_SIZE - 1) / X_REGISTER_SIZE;
1146
1147   /* PCS C.13 - Pass in registers if we have enough spare */
1148   if (info->ngrn + nregs <= 8)
1149     {
1150       pass_in_x (gdbarch, regcache, info, type, arg);
1151       info->ngrn += nregs;
1152     }
1153   else
1154     {
1155       info->ngrn = 8;
1156       pass_on_stack (info, type, arg);
1157     }
1158 }
1159
1160 /* Pass a value in a V register, or on the stack if insufficient are
1161    available.  */
1162
1163 static void
1164 pass_in_v_or_stack (struct gdbarch *gdbarch,
1165                     struct regcache *regcache,
1166                     struct aarch64_call_info *info,
1167                     struct type *type,
1168                     struct value *arg)
1169 {
1170   if (!pass_in_v (gdbarch, regcache, info, TYPE_LENGTH (type),
1171                   value_contents (arg)))
1172     pass_on_stack (info, type, arg);
1173 }
1174
1175 /* Implement the "push_dummy_call" gdbarch method.  */
1176
1177 static CORE_ADDR
1178 aarch64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1179                          struct regcache *regcache, CORE_ADDR bp_addr,
1180                          int nargs,
1181                          struct value **args, CORE_ADDR sp, int struct_return,
1182                          CORE_ADDR struct_addr)
1183 {
1184   int nstack = 0;
1185   int argnum;
1186   int x_argreg;
1187   int v_argreg;
1188   struct aarch64_call_info info;
1189   struct type *func_type;
1190   struct type *return_type;
1191   int lang_struct_return;
1192
1193   memset (&info, 0, sizeof (info));
1194
1195   /* We need to know what the type of the called function is in order
1196      to determine the number of named/anonymous arguments for the
1197      actual argument placement, and the return type in order to handle
1198      return value correctly.
1199
1200      The generic code above us views the decision of return in memory
1201      or return in registers as a two stage processes.  The language
1202      handler is consulted first and may decide to return in memory (eg
1203      class with copy constructor returned by value), this will cause
1204      the generic code to allocate space AND insert an initial leading
1205      argument.
1206
1207      If the language code does not decide to pass in memory then the
1208      target code is consulted.
1209
1210      If the language code decides to pass in memory we want to move
1211      the pointer inserted as the initial argument from the argument
1212      list and into X8, the conventional AArch64 struct return pointer
1213      register.
1214
1215      This is slightly awkward, ideally the flag "lang_struct_return"
1216      would be passed to the targets implementation of push_dummy_call.
1217      Rather that change the target interface we call the language code
1218      directly ourselves.  */
1219
1220   func_type = check_typedef (value_type (function));
1221
1222   /* Dereference function pointer types.  */
1223   if (TYPE_CODE (func_type) == TYPE_CODE_PTR)
1224     func_type = TYPE_TARGET_TYPE (func_type);
1225
1226   gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC
1227               || TYPE_CODE (func_type) == TYPE_CODE_METHOD);
1228
1229   /* If language_pass_by_reference () returned true we will have been
1230      given an additional initial argument, a hidden pointer to the
1231      return slot in memory.  */
1232   return_type = TYPE_TARGET_TYPE (func_type);
1233   lang_struct_return = language_pass_by_reference (return_type);
1234
1235   /* Set the return address.  For the AArch64, the return breakpoint
1236      is always at BP_ADDR.  */
1237   regcache_cooked_write_unsigned (regcache, AARCH64_LR_REGNUM, bp_addr);
1238
1239   /* If we were given an initial argument for the return slot because
1240      lang_struct_return was true, lose it.  */
1241   if (lang_struct_return)
1242     {
1243       args++;
1244       nargs--;
1245     }
1246
1247   /* The struct_return pointer occupies X8.  */
1248   if (struct_return || lang_struct_return)
1249     {
1250       if (aarch64_debug)
1251         {
1252           debug_printf ("struct return in %s = 0x%s\n",
1253                         gdbarch_register_name (gdbarch,
1254                                                AARCH64_STRUCT_RETURN_REGNUM),
1255                         paddress (gdbarch, struct_addr));
1256         }
1257       regcache_cooked_write_unsigned (regcache, AARCH64_STRUCT_RETURN_REGNUM,
1258                                       struct_addr);
1259     }
1260
1261   for (argnum = 0; argnum < nargs; argnum++)
1262     {
1263       struct value *arg = args[argnum];
1264       struct type *arg_type;
1265       int len;
1266
1267       arg_type = check_typedef (value_type (arg));
1268       len = TYPE_LENGTH (arg_type);
1269
1270       switch (TYPE_CODE (arg_type))
1271         {
1272         case TYPE_CODE_INT:
1273         case TYPE_CODE_BOOL:
1274         case TYPE_CODE_CHAR:
1275         case TYPE_CODE_RANGE:
1276         case TYPE_CODE_ENUM:
1277           if (len < 4)
1278             {
1279               /* Promote to 32 bit integer.  */
1280               if (TYPE_UNSIGNED (arg_type))
1281                 arg_type = builtin_type (gdbarch)->builtin_uint32;
1282               else
1283                 arg_type = builtin_type (gdbarch)->builtin_int32;
1284               arg = value_cast (arg_type, arg);
1285             }
1286           pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1287           break;
1288
1289         case TYPE_CODE_COMPLEX:
1290           if (info.nsrn <= 6)
1291             {
1292               const bfd_byte *buf = value_contents (arg);
1293               struct type *target_type =
1294                 check_typedef (TYPE_TARGET_TYPE (arg_type));
1295
1296               pass_in_v (gdbarch, regcache, &info,
1297                          TYPE_LENGTH (target_type), buf);
1298               pass_in_v (gdbarch, regcache, &info,
1299                          TYPE_LENGTH (target_type),
1300                          buf + TYPE_LENGTH (target_type));
1301             }
1302           else
1303             {
1304               info.nsrn = 8;
1305               pass_on_stack (&info, arg_type, arg);
1306             }
1307           break;
1308         case TYPE_CODE_FLT:
1309           pass_in_v_or_stack (gdbarch, regcache, &info, arg_type, arg);
1310           break;
1311
1312         case TYPE_CODE_STRUCT:
1313         case TYPE_CODE_ARRAY:
1314         case TYPE_CODE_UNION:
1315           if (is_hfa_or_hva (arg_type))
1316             {
1317               int elements = TYPE_NFIELDS (arg_type);
1318
1319               /* Homogeneous Aggregates */
1320               if (info.nsrn + elements < 8)
1321                 {
1322                   int i;
1323
1324                   for (i = 0; i < elements; i++)
1325                     {
1326                       /* We know that we have sufficient registers
1327                          available therefore this will never fallback
1328                          to the stack.  */
1329                       struct value *field =
1330                         value_primitive_field (arg, 0, i, arg_type);
1331                       struct type *field_type =
1332                         check_typedef (value_type (field));
1333
1334                       pass_in_v_or_stack (gdbarch, regcache, &info,
1335                                           field_type, field);
1336                     }
1337                 }
1338               else
1339                 {
1340                   info.nsrn = 8;
1341                   pass_on_stack (&info, arg_type, arg);
1342                 }
1343             }
1344           else if (TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
1345                    && TYPE_VECTOR (arg_type) && (len == 16 || len == 8))
1346             {
1347               /* Short vector types are passed in V registers.  */
1348               pass_in_v_or_stack (gdbarch, regcache, &info, arg_type, arg);
1349             }
1350           else if (len > 16)
1351             {
1352               /* PCS B.7 Aggregates larger than 16 bytes are passed by
1353                  invisible reference.  */
1354
1355               /* Allocate aligned storage.  */
1356               sp = align_down (sp - len, 16);
1357
1358               /* Write the real data into the stack.  */
1359               write_memory (sp, value_contents (arg), len);
1360
1361               /* Construct the indirection.  */
1362               arg_type = lookup_pointer_type (arg_type);
1363               arg = value_from_pointer (arg_type, sp);
1364               pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1365             }
1366           else
1367             /* PCS C.15 / C.18 multiple values pass.  */
1368             pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1369           break;
1370
1371         default:
1372           pass_in_x_or_stack (gdbarch, regcache, &info, arg_type, arg);
1373           break;
1374         }
1375     }
1376
1377   /* Make sure stack retains 16 byte alignment.  */
1378   if (info.nsaa & 15)
1379     sp -= 16 - (info.nsaa & 15);
1380
1381   while (!VEC_empty (stack_item_t, info.si))
1382     {
1383       stack_item_t *si = VEC_last (stack_item_t, info.si);
1384
1385       sp -= si->len;
1386       if (si->data != NULL)
1387         write_memory (sp, si->data, si->len);
1388       VEC_pop (stack_item_t, info.si);
1389     }
1390
1391   VEC_free (stack_item_t, info.si);
1392
1393   /* Finally, update the SP register.  */
1394   regcache_cooked_write_unsigned (regcache, AARCH64_SP_REGNUM, sp);
1395
1396   return sp;
1397 }
1398
1399 /* Implement the "frame_align" gdbarch method.  */
1400
1401 static CORE_ADDR
1402 aarch64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1403 {
1404   /* Align the stack to sixteen bytes.  */
1405   return sp & ~(CORE_ADDR) 15;
1406 }
1407
1408 /* Return the type for an AdvSISD Q register.  */
1409
1410 static struct type *
1411 aarch64_vnq_type (struct gdbarch *gdbarch)
1412 {
1413   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1414
1415   if (tdep->vnq_type == NULL)
1416     {
1417       struct type *t;
1418       struct type *elem;
1419
1420       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnq",
1421                                TYPE_CODE_UNION);
1422
1423       elem = builtin_type (gdbarch)->builtin_uint128;
1424       append_composite_type_field (t, "u", elem);
1425
1426       elem = builtin_type (gdbarch)->builtin_int128;
1427       append_composite_type_field (t, "s", elem);
1428
1429       tdep->vnq_type = t;
1430     }
1431
1432   return tdep->vnq_type;
1433 }
1434
1435 /* Return the type for an AdvSISD D register.  */
1436
1437 static struct type *
1438 aarch64_vnd_type (struct gdbarch *gdbarch)
1439 {
1440   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1441
1442   if (tdep->vnd_type == NULL)
1443     {
1444       struct type *t;
1445       struct type *elem;
1446
1447       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnd",
1448                                TYPE_CODE_UNION);
1449
1450       elem = builtin_type (gdbarch)->builtin_double;
1451       append_composite_type_field (t, "f", elem);
1452
1453       elem = builtin_type (gdbarch)->builtin_uint64;
1454       append_composite_type_field (t, "u", elem);
1455
1456       elem = builtin_type (gdbarch)->builtin_int64;
1457       append_composite_type_field (t, "s", elem);
1458
1459       tdep->vnd_type = t;
1460     }
1461
1462   return tdep->vnd_type;
1463 }
1464
1465 /* Return the type for an AdvSISD S register.  */
1466
1467 static struct type *
1468 aarch64_vns_type (struct gdbarch *gdbarch)
1469 {
1470   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1471
1472   if (tdep->vns_type == NULL)
1473     {
1474       struct type *t;
1475       struct type *elem;
1476
1477       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vns",
1478                                TYPE_CODE_UNION);
1479
1480       elem = builtin_type (gdbarch)->builtin_float;
1481       append_composite_type_field (t, "f", elem);
1482
1483       elem = builtin_type (gdbarch)->builtin_uint32;
1484       append_composite_type_field (t, "u", elem);
1485
1486       elem = builtin_type (gdbarch)->builtin_int32;
1487       append_composite_type_field (t, "s", elem);
1488
1489       tdep->vns_type = t;
1490     }
1491
1492   return tdep->vns_type;
1493 }
1494
1495 /* Return the type for an AdvSISD H register.  */
1496
1497 static struct type *
1498 aarch64_vnh_type (struct gdbarch *gdbarch)
1499 {
1500   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1501
1502   if (tdep->vnh_type == NULL)
1503     {
1504       struct type *t;
1505       struct type *elem;
1506
1507       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnh",
1508                                TYPE_CODE_UNION);
1509
1510       elem = builtin_type (gdbarch)->builtin_uint16;
1511       append_composite_type_field (t, "u", elem);
1512
1513       elem = builtin_type (gdbarch)->builtin_int16;
1514       append_composite_type_field (t, "s", elem);
1515
1516       tdep->vnh_type = t;
1517     }
1518
1519   return tdep->vnh_type;
1520 }
1521
1522 /* Return the type for an AdvSISD B register.  */
1523
1524 static struct type *
1525 aarch64_vnb_type (struct gdbarch *gdbarch)
1526 {
1527   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1528
1529   if (tdep->vnb_type == NULL)
1530     {
1531       struct type *t;
1532       struct type *elem;
1533
1534       t = arch_composite_type (gdbarch, "__gdb_builtin_type_vnb",
1535                                TYPE_CODE_UNION);
1536
1537       elem = builtin_type (gdbarch)->builtin_uint8;
1538       append_composite_type_field (t, "u", elem);
1539
1540       elem = builtin_type (gdbarch)->builtin_int8;
1541       append_composite_type_field (t, "s", elem);
1542
1543       tdep->vnb_type = t;
1544     }
1545
1546   return tdep->vnb_type;
1547 }
1548
1549 /* Implement the "dwarf2_reg_to_regnum" gdbarch method.  */
1550
1551 static int
1552 aarch64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
1553 {
1554   if (reg >= AARCH64_DWARF_X0 && reg <= AARCH64_DWARF_X0 + 30)
1555     return AARCH64_X0_REGNUM + reg - AARCH64_DWARF_X0;
1556
1557   if (reg == AARCH64_DWARF_SP)
1558     return AARCH64_SP_REGNUM;
1559
1560   if (reg >= AARCH64_DWARF_V0 && reg <= AARCH64_DWARF_V0 + 31)
1561     return AARCH64_V0_REGNUM + reg - AARCH64_DWARF_V0;
1562
1563   return -1;
1564 }
1565 \f
1566
1567 /* Implement the "print_insn" gdbarch method.  */
1568
1569 static int
1570 aarch64_gdb_print_insn (bfd_vma memaddr, disassemble_info *info)
1571 {
1572   info->symbols = NULL;
1573   return print_insn_aarch64 (memaddr, info);
1574 }
1575
1576 /* AArch64 BRK software debug mode instruction.
1577    Note that AArch64 code is always little-endian.
1578    1101.0100.0010.0000.0000.0000.0000.0000 = 0xd4200000.  */
1579 static const gdb_byte aarch64_default_breakpoint[] = {0x00, 0x00, 0x20, 0xd4};
1580
1581 /* Implement the "breakpoint_from_pc" gdbarch method.  */
1582
1583 static const gdb_byte *
1584 aarch64_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
1585                             int *lenptr)
1586 {
1587   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1588
1589   *lenptr = sizeof (aarch64_default_breakpoint);
1590   return aarch64_default_breakpoint;
1591 }
1592
1593 /* Extract from an array REGS containing the (raw) register state a
1594    function return value of type TYPE, and copy that, in virtual
1595    format, into VALBUF.  */
1596
1597 static void
1598 aarch64_extract_return_value (struct type *type, struct regcache *regs,
1599                               gdb_byte *valbuf)
1600 {
1601   struct gdbarch *gdbarch = get_regcache_arch (regs);
1602   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1603
1604   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1605     {
1606       bfd_byte buf[V_REGISTER_SIZE];
1607       int len = TYPE_LENGTH (type);
1608
1609       regcache_cooked_read (regs, AARCH64_V0_REGNUM, buf);
1610       memcpy (valbuf, buf, len);
1611     }
1612   else if (TYPE_CODE (type) == TYPE_CODE_INT
1613            || TYPE_CODE (type) == TYPE_CODE_CHAR
1614            || TYPE_CODE (type) == TYPE_CODE_BOOL
1615            || TYPE_CODE (type) == TYPE_CODE_PTR
1616            || TYPE_CODE (type) == TYPE_CODE_REF
1617            || TYPE_CODE (type) == TYPE_CODE_ENUM)
1618     {
1619       /* If the the type is a plain integer, then the access is
1620          straight-forward.  Otherwise we have to play around a bit
1621          more.  */
1622       int len = TYPE_LENGTH (type);
1623       int regno = AARCH64_X0_REGNUM;
1624       ULONGEST tmp;
1625
1626       while (len > 0)
1627         {
1628           /* By using store_unsigned_integer we avoid having to do
1629              anything special for small big-endian values.  */
1630           regcache_cooked_read_unsigned (regs, regno++, &tmp);
1631           store_unsigned_integer (valbuf,
1632                                   (len > X_REGISTER_SIZE
1633                                    ? X_REGISTER_SIZE : len), byte_order, tmp);
1634           len -= X_REGISTER_SIZE;
1635           valbuf += X_REGISTER_SIZE;
1636         }
1637     }
1638   else if (TYPE_CODE (type) == TYPE_CODE_COMPLEX)
1639     {
1640       int regno = AARCH64_V0_REGNUM;
1641       bfd_byte buf[V_REGISTER_SIZE];
1642       struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1643       int len = TYPE_LENGTH (target_type);
1644
1645       regcache_cooked_read (regs, regno, buf);
1646       memcpy (valbuf, buf, len);
1647       valbuf += len;
1648       regcache_cooked_read (regs, regno + 1, buf);
1649       memcpy (valbuf, buf, len);
1650       valbuf += len;
1651     }
1652   else if (is_hfa_or_hva (type))
1653     {
1654       int elements = TYPE_NFIELDS (type);
1655       struct type *member_type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1656       int len = TYPE_LENGTH (member_type);
1657       int i;
1658
1659       for (i = 0; i < elements; i++)
1660         {
1661           int regno = AARCH64_V0_REGNUM + i;
1662           bfd_byte buf[V_REGISTER_SIZE];
1663
1664           if (aarch64_debug)
1665             {
1666               debug_printf ("read HFA or HVA return value element %d from %s\n",
1667                             i + 1,
1668                             gdbarch_register_name (gdbarch, regno));
1669             }
1670           regcache_cooked_read (regs, regno, buf);
1671
1672           memcpy (valbuf, buf, len);
1673           valbuf += len;
1674         }
1675     }
1676   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1677            && (TYPE_LENGTH (type) == 16 || TYPE_LENGTH (type) == 8))
1678     {
1679       /* Short vector is returned in V register.  */
1680       gdb_byte buf[V_REGISTER_SIZE];
1681
1682       regcache_cooked_read (regs, AARCH64_V0_REGNUM, buf);
1683       memcpy (valbuf, buf, TYPE_LENGTH (type));
1684     }
1685   else
1686     {
1687       /* For a structure or union the behaviour is as if the value had
1688          been stored to word-aligned memory and then loaded into
1689          registers with 64-bit load instruction(s).  */
1690       int len = TYPE_LENGTH (type);
1691       int regno = AARCH64_X0_REGNUM;
1692       bfd_byte buf[X_REGISTER_SIZE];
1693
1694       while (len > 0)
1695         {
1696           regcache_cooked_read (regs, regno++, buf);
1697           memcpy (valbuf, buf, len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
1698           len -= X_REGISTER_SIZE;
1699           valbuf += X_REGISTER_SIZE;
1700         }
1701     }
1702 }
1703
1704
1705 /* Will a function return an aggregate type in memory or in a
1706    register?  Return 0 if an aggregate type can be returned in a
1707    register, 1 if it must be returned in memory.  */
1708
1709 static int
1710 aarch64_return_in_memory (struct gdbarch *gdbarch, struct type *type)
1711 {
1712   int nRc;
1713   enum type_code code;
1714
1715   type = check_typedef (type);
1716
1717   if (is_hfa_or_hva (type))
1718     {
1719       /* v0-v7 are used to return values and one register is allocated
1720          for one member.  However, HFA or HVA has at most four members.  */
1721       return 0;
1722     }
1723
1724   if (TYPE_LENGTH (type) > 16)
1725     {
1726       /* PCS B.6 Aggregates larger than 16 bytes are passed by
1727          invisible reference.  */
1728
1729       return 1;
1730     }
1731
1732   return 0;
1733 }
1734
1735 /* Write into appropriate registers a function return value of type
1736    TYPE, given in virtual format.  */
1737
1738 static void
1739 aarch64_store_return_value (struct type *type, struct regcache *regs,
1740                             const gdb_byte *valbuf)
1741 {
1742   struct gdbarch *gdbarch = get_regcache_arch (regs);
1743   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1744
1745   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1746     {
1747       bfd_byte buf[V_REGISTER_SIZE];
1748       int len = TYPE_LENGTH (type);
1749
1750       memcpy (buf, valbuf, len > V_REGISTER_SIZE ? V_REGISTER_SIZE : len);
1751       regcache_cooked_write (regs, AARCH64_V0_REGNUM, buf);
1752     }
1753   else if (TYPE_CODE (type) == TYPE_CODE_INT
1754            || TYPE_CODE (type) == TYPE_CODE_CHAR
1755            || TYPE_CODE (type) == TYPE_CODE_BOOL
1756            || TYPE_CODE (type) == TYPE_CODE_PTR
1757            || TYPE_CODE (type) == TYPE_CODE_REF
1758            || TYPE_CODE (type) == TYPE_CODE_ENUM)
1759     {
1760       if (TYPE_LENGTH (type) <= X_REGISTER_SIZE)
1761         {
1762           /* Values of one word or less are zero/sign-extended and
1763              returned in r0.  */
1764           bfd_byte tmpbuf[X_REGISTER_SIZE];
1765           LONGEST val = unpack_long (type, valbuf);
1766
1767           store_signed_integer (tmpbuf, X_REGISTER_SIZE, byte_order, val);
1768           regcache_cooked_write (regs, AARCH64_X0_REGNUM, tmpbuf);
1769         }
1770       else
1771         {
1772           /* Integral values greater than one word are stored in
1773              consecutive registers starting with r0.  This will always
1774              be a multiple of the regiser size.  */
1775           int len = TYPE_LENGTH (type);
1776           int regno = AARCH64_X0_REGNUM;
1777
1778           while (len > 0)
1779             {
1780               regcache_cooked_write (regs, regno++, valbuf);
1781               len -= X_REGISTER_SIZE;
1782               valbuf += X_REGISTER_SIZE;
1783             }
1784         }
1785     }
1786   else if (is_hfa_or_hva (type))
1787     {
1788       int elements = TYPE_NFIELDS (type);
1789       struct type *member_type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1790       int len = TYPE_LENGTH (member_type);
1791       int i;
1792
1793       for (i = 0; i < elements; i++)
1794         {
1795           int regno = AARCH64_V0_REGNUM + i;
1796           bfd_byte tmpbuf[MAX_REGISTER_SIZE];
1797
1798           if (aarch64_debug)
1799             {
1800               debug_printf ("write HFA or HVA return value element %d to %s\n",
1801                             i + 1,
1802                             gdbarch_register_name (gdbarch, regno));
1803             }
1804
1805           memcpy (tmpbuf, valbuf, len);
1806           regcache_cooked_write (regs, regno, tmpbuf);
1807           valbuf += len;
1808         }
1809     }
1810   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1811            && (TYPE_LENGTH (type) == 8 || TYPE_LENGTH (type) == 16))
1812     {
1813       /* Short vector.  */
1814       gdb_byte buf[V_REGISTER_SIZE];
1815
1816       memcpy (buf, valbuf, TYPE_LENGTH (type));
1817       regcache_cooked_write (regs, AARCH64_V0_REGNUM, buf);
1818     }
1819   else
1820     {
1821       /* For a structure or union the behaviour is as if the value had
1822          been stored to word-aligned memory and then loaded into
1823          registers with 64-bit load instruction(s).  */
1824       int len = TYPE_LENGTH (type);
1825       int regno = AARCH64_X0_REGNUM;
1826       bfd_byte tmpbuf[X_REGISTER_SIZE];
1827
1828       while (len > 0)
1829         {
1830           memcpy (tmpbuf, valbuf,
1831                   len > X_REGISTER_SIZE ? X_REGISTER_SIZE : len);
1832           regcache_cooked_write (regs, regno++, tmpbuf);
1833           len -= X_REGISTER_SIZE;
1834           valbuf += X_REGISTER_SIZE;
1835         }
1836     }
1837 }
1838
1839 /* Implement the "return_value" gdbarch method.  */
1840
1841 static enum return_value_convention
1842 aarch64_return_value (struct gdbarch *gdbarch, struct value *func_value,
1843                       struct type *valtype, struct regcache *regcache,
1844                       gdb_byte *readbuf, const gdb_byte *writebuf)
1845 {
1846   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1847
1848   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1849       || TYPE_CODE (valtype) == TYPE_CODE_UNION
1850       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1851     {
1852       if (aarch64_return_in_memory (gdbarch, valtype))
1853         {
1854           if (aarch64_debug)
1855             debug_printf ("return value in memory\n");
1856           return RETURN_VALUE_STRUCT_CONVENTION;
1857         }
1858     }
1859
1860   if (writebuf)
1861     aarch64_store_return_value (valtype, regcache, writebuf);
1862
1863   if (readbuf)
1864     aarch64_extract_return_value (valtype, regcache, readbuf);
1865
1866   if (aarch64_debug)
1867     debug_printf ("return value in registers\n");
1868
1869   return RETURN_VALUE_REGISTER_CONVENTION;
1870 }
1871
1872 /* Implement the "get_longjmp_target" gdbarch method.  */
1873
1874 static int
1875 aarch64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
1876 {
1877   CORE_ADDR jb_addr;
1878   gdb_byte buf[X_REGISTER_SIZE];
1879   struct gdbarch *gdbarch = get_frame_arch (frame);
1880   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1881   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1882
1883   jb_addr = get_frame_register_unsigned (frame, AARCH64_X0_REGNUM);
1884
1885   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
1886                           X_REGISTER_SIZE))
1887     return 0;
1888
1889   *pc = extract_unsigned_integer (buf, X_REGISTER_SIZE, byte_order);
1890   return 1;
1891 }
1892
1893 /* Implement the "gen_return_address" gdbarch method.  */
1894
1895 static void
1896 aarch64_gen_return_address (struct gdbarch *gdbarch,
1897                             struct agent_expr *ax, struct axs_value *value,
1898                             CORE_ADDR scope)
1899 {
1900   value->type = register_type (gdbarch, AARCH64_LR_REGNUM);
1901   value->kind = axs_lvalue_register;
1902   value->u.reg = AARCH64_LR_REGNUM;
1903 }
1904 \f
1905
1906 /* Return the pseudo register name corresponding to register regnum.  */
1907
1908 static const char *
1909 aarch64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
1910 {
1911   static const char *const q_name[] =
1912     {
1913       "q0", "q1", "q2", "q3",
1914       "q4", "q5", "q6", "q7",
1915       "q8", "q9", "q10", "q11",
1916       "q12", "q13", "q14", "q15",
1917       "q16", "q17", "q18", "q19",
1918       "q20", "q21", "q22", "q23",
1919       "q24", "q25", "q26", "q27",
1920       "q28", "q29", "q30", "q31",
1921     };
1922
1923   static const char *const d_name[] =
1924     {
1925       "d0", "d1", "d2", "d3",
1926       "d4", "d5", "d6", "d7",
1927       "d8", "d9", "d10", "d11",
1928       "d12", "d13", "d14", "d15",
1929       "d16", "d17", "d18", "d19",
1930       "d20", "d21", "d22", "d23",
1931       "d24", "d25", "d26", "d27",
1932       "d28", "d29", "d30", "d31",
1933     };
1934
1935   static const char *const s_name[] =
1936     {
1937       "s0", "s1", "s2", "s3",
1938       "s4", "s5", "s6", "s7",
1939       "s8", "s9", "s10", "s11",
1940       "s12", "s13", "s14", "s15",
1941       "s16", "s17", "s18", "s19",
1942       "s20", "s21", "s22", "s23",
1943       "s24", "s25", "s26", "s27",
1944       "s28", "s29", "s30", "s31",
1945     };
1946
1947   static const char *const h_name[] =
1948     {
1949       "h0", "h1", "h2", "h3",
1950       "h4", "h5", "h6", "h7",
1951       "h8", "h9", "h10", "h11",
1952       "h12", "h13", "h14", "h15",
1953       "h16", "h17", "h18", "h19",
1954       "h20", "h21", "h22", "h23",
1955       "h24", "h25", "h26", "h27",
1956       "h28", "h29", "h30", "h31",
1957     };
1958
1959   static const char *const b_name[] =
1960     {
1961       "b0", "b1", "b2", "b3",
1962       "b4", "b5", "b6", "b7",
1963       "b8", "b9", "b10", "b11",
1964       "b12", "b13", "b14", "b15",
1965       "b16", "b17", "b18", "b19",
1966       "b20", "b21", "b22", "b23",
1967       "b24", "b25", "b26", "b27",
1968       "b28", "b29", "b30", "b31",
1969     };
1970
1971   regnum -= gdbarch_num_regs (gdbarch);
1972
1973   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
1974     return q_name[regnum - AARCH64_Q0_REGNUM];
1975
1976   if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
1977     return d_name[regnum - AARCH64_D0_REGNUM];
1978
1979   if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
1980     return s_name[regnum - AARCH64_S0_REGNUM];
1981
1982   if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
1983     return h_name[regnum - AARCH64_H0_REGNUM];
1984
1985   if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
1986     return b_name[regnum - AARCH64_B0_REGNUM];
1987
1988   internal_error (__FILE__, __LINE__,
1989                   _("aarch64_pseudo_register_name: bad register number %d"),
1990                   regnum);
1991 }
1992
1993 /* Implement the "pseudo_register_type" tdesc_arch_data method.  */
1994
1995 static struct type *
1996 aarch64_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
1997 {
1998   regnum -= gdbarch_num_regs (gdbarch);
1999
2000   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
2001     return aarch64_vnq_type (gdbarch);
2002
2003   if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
2004     return aarch64_vnd_type (gdbarch);
2005
2006   if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
2007     return aarch64_vns_type (gdbarch);
2008
2009   if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
2010     return aarch64_vnh_type (gdbarch);
2011
2012   if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
2013     return aarch64_vnb_type (gdbarch);
2014
2015   internal_error (__FILE__, __LINE__,
2016                   _("aarch64_pseudo_register_type: bad register number %d"),
2017                   regnum);
2018 }
2019
2020 /* Implement the "pseudo_register_reggroup_p" tdesc_arch_data method.  */
2021
2022 static int
2023 aarch64_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2024                                     struct reggroup *group)
2025 {
2026   regnum -= gdbarch_num_regs (gdbarch);
2027
2028   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
2029     return group == all_reggroup || group == vector_reggroup;
2030   else if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
2031     return (group == all_reggroup || group == vector_reggroup
2032             || group == float_reggroup);
2033   else if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
2034     return (group == all_reggroup || group == vector_reggroup
2035             || group == float_reggroup);
2036   else if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
2037     return group == all_reggroup || group == vector_reggroup;
2038   else if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
2039     return group == all_reggroup || group == vector_reggroup;
2040
2041   return group == all_reggroup;
2042 }
2043
2044 /* Implement the "pseudo_register_read_value" gdbarch method.  */
2045
2046 static struct value *
2047 aarch64_pseudo_read_value (struct gdbarch *gdbarch,
2048                            struct regcache *regcache,
2049                            int regnum)
2050 {
2051   gdb_byte reg_buf[MAX_REGISTER_SIZE];
2052   struct value *result_value;
2053   gdb_byte *buf;
2054
2055   result_value = allocate_value (register_type (gdbarch, regnum));
2056   VALUE_LVAL (result_value) = lval_register;
2057   VALUE_REGNUM (result_value) = regnum;
2058   buf = value_contents_raw (result_value);
2059
2060   regnum -= gdbarch_num_regs (gdbarch);
2061
2062   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
2063     {
2064       enum register_status status;
2065       unsigned v_regnum;
2066
2067       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_Q0_REGNUM;
2068       status = regcache_raw_read (regcache, v_regnum, reg_buf);
2069       if (status != REG_VALID)
2070         mark_value_bytes_unavailable (result_value, 0,
2071                                       TYPE_LENGTH (value_type (result_value)));
2072       else
2073         memcpy (buf, reg_buf, Q_REGISTER_SIZE);
2074       return result_value;
2075     }
2076
2077   if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
2078     {
2079       enum register_status status;
2080       unsigned v_regnum;
2081
2082       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_D0_REGNUM;
2083       status = regcache_raw_read (regcache, v_regnum, reg_buf);
2084       if (status != REG_VALID)
2085         mark_value_bytes_unavailable (result_value, 0,
2086                                       TYPE_LENGTH (value_type (result_value)));
2087       else
2088         memcpy (buf, reg_buf, D_REGISTER_SIZE);
2089       return result_value;
2090     }
2091
2092   if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
2093     {
2094       enum register_status status;
2095       unsigned v_regnum;
2096
2097       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_S0_REGNUM;
2098       status = regcache_raw_read (regcache, v_regnum, reg_buf);
2099       if (status != REG_VALID)
2100         mark_value_bytes_unavailable (result_value, 0,
2101                                       TYPE_LENGTH (value_type (result_value)));
2102       else
2103         memcpy (buf, reg_buf, S_REGISTER_SIZE);
2104       return result_value;
2105     }
2106
2107   if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
2108     {
2109       enum register_status status;
2110       unsigned v_regnum;
2111
2112       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_H0_REGNUM;
2113       status = regcache_raw_read (regcache, v_regnum, reg_buf);
2114       if (status != REG_VALID)
2115         mark_value_bytes_unavailable (result_value, 0,
2116                                       TYPE_LENGTH (value_type (result_value)));
2117       else
2118         memcpy (buf, reg_buf, H_REGISTER_SIZE);
2119       return result_value;
2120     }
2121
2122   if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
2123     {
2124       enum register_status status;
2125       unsigned v_regnum;
2126
2127       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_B0_REGNUM;
2128       status = regcache_raw_read (regcache, v_regnum, reg_buf);
2129       if (status != REG_VALID)
2130         mark_value_bytes_unavailable (result_value, 0,
2131                                       TYPE_LENGTH (value_type (result_value)));
2132       else
2133         memcpy (buf, reg_buf, B_REGISTER_SIZE);
2134       return result_value;
2135     }
2136
2137   gdb_assert_not_reached ("regnum out of bound");
2138 }
2139
2140 /* Implement the "pseudo_register_write" gdbarch method.  */
2141
2142 static void
2143 aarch64_pseudo_write (struct gdbarch *gdbarch, struct regcache *regcache,
2144                       int regnum, const gdb_byte *buf)
2145 {
2146   gdb_byte reg_buf[MAX_REGISTER_SIZE];
2147
2148   /* Ensure the register buffer is zero, we want gdb writes of the
2149      various 'scalar' pseudo registers to behavior like architectural
2150      writes, register width bytes are written the remainder are set to
2151      zero.  */
2152   memset (reg_buf, 0, sizeof (reg_buf));
2153
2154   regnum -= gdbarch_num_regs (gdbarch);
2155
2156   if (regnum >= AARCH64_Q0_REGNUM && regnum < AARCH64_Q0_REGNUM + 32)
2157     {
2158       /* pseudo Q registers */
2159       unsigned v_regnum;
2160
2161       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_Q0_REGNUM;
2162       memcpy (reg_buf, buf, Q_REGISTER_SIZE);
2163       regcache_raw_write (regcache, v_regnum, reg_buf);
2164       return;
2165     }
2166
2167   if (regnum >= AARCH64_D0_REGNUM && regnum < AARCH64_D0_REGNUM + 32)
2168     {
2169       /* pseudo D registers */
2170       unsigned v_regnum;
2171
2172       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_D0_REGNUM;
2173       memcpy (reg_buf, buf, D_REGISTER_SIZE);
2174       regcache_raw_write (regcache, v_regnum, reg_buf);
2175       return;
2176     }
2177
2178   if (regnum >= AARCH64_S0_REGNUM && regnum < AARCH64_S0_REGNUM + 32)
2179     {
2180       unsigned v_regnum;
2181
2182       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_S0_REGNUM;
2183       memcpy (reg_buf, buf, S_REGISTER_SIZE);
2184       regcache_raw_write (regcache, v_regnum, reg_buf);
2185       return;
2186     }
2187
2188   if (regnum >= AARCH64_H0_REGNUM && regnum < AARCH64_H0_REGNUM + 32)
2189     {
2190       /* pseudo H registers */
2191       unsigned v_regnum;
2192
2193       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_H0_REGNUM;
2194       memcpy (reg_buf, buf, H_REGISTER_SIZE);
2195       regcache_raw_write (regcache, v_regnum, reg_buf);
2196       return;
2197     }
2198
2199   if (regnum >= AARCH64_B0_REGNUM && regnum < AARCH64_B0_REGNUM + 32)
2200     {
2201       /* pseudo B registers */
2202       unsigned v_regnum;
2203
2204       v_regnum = AARCH64_V0_REGNUM + regnum - AARCH64_B0_REGNUM;
2205       memcpy (reg_buf, buf, B_REGISTER_SIZE);
2206       regcache_raw_write (regcache, v_regnum, reg_buf);
2207       return;
2208     }
2209
2210   gdb_assert_not_reached ("regnum out of bound");
2211 }
2212
2213 /* Callback function for user_reg_add.  */
2214
2215 static struct value *
2216 value_of_aarch64_user_reg (struct frame_info *frame, const void *baton)
2217 {
2218   const int *reg_p = (const int *) baton;
2219
2220   return value_of_register (*reg_p, frame);
2221 }
2222 \f
2223
2224 /* Implement the "software_single_step" gdbarch method, needed to
2225    single step through atomic sequences on AArch64.  */
2226
2227 static int
2228 aarch64_software_single_step (struct frame_info *frame)
2229 {
2230   struct gdbarch *gdbarch = get_frame_arch (frame);
2231   struct address_space *aspace = get_frame_address_space (frame);
2232   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2233   const int insn_size = 4;
2234   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
2235   CORE_ADDR pc = get_frame_pc (frame);
2236   CORE_ADDR breaks[2] = { -1, -1 };
2237   CORE_ADDR loc = pc;
2238   CORE_ADDR closing_insn = 0;
2239   uint32_t insn = read_memory_unsigned_integer (loc, insn_size,
2240                                                 byte_order_for_code);
2241   int index;
2242   int insn_count;
2243   int bc_insn_count = 0; /* Conditional branch instruction count.  */
2244   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */
2245   aarch64_inst inst;
2246
2247   if (aarch64_decode_insn (insn, &inst, 1) != 0)
2248     return 0;
2249
2250   /* Look for a Load Exclusive instruction which begins the sequence.  */
2251   if (inst.opcode->iclass != ldstexcl || bit (insn, 22) == 0)
2252     return 0;
2253
2254   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
2255     {
2256       loc += insn_size;
2257       insn = read_memory_unsigned_integer (loc, insn_size,
2258                                            byte_order_for_code);
2259
2260       if (aarch64_decode_insn (insn, &inst, 1) != 0)
2261         return 0;
2262       /* Check if the instruction is a conditional branch.  */
2263       if (inst.opcode->iclass == condbranch)
2264         {
2265           gdb_assert (inst.operands[0].type == AARCH64_OPND_ADDR_PCREL19);
2266
2267           if (bc_insn_count >= 1)
2268             return 0;
2269
2270           /* It is, so we'll try to set a breakpoint at the destination.  */
2271           breaks[1] = loc + inst.operands[0].imm.value;
2272
2273           bc_insn_count++;
2274           last_breakpoint++;
2275         }
2276
2277       /* Look for the Store Exclusive which closes the atomic sequence.  */
2278       if (inst.opcode->iclass == ldstexcl && bit (insn, 22) == 0)
2279         {
2280           closing_insn = loc;
2281           break;
2282         }
2283     }
2284
2285   /* We didn't find a closing Store Exclusive instruction, fall back.  */
2286   if (!closing_insn)
2287     return 0;
2288
2289   /* Insert breakpoint after the end of the atomic sequence.  */
2290   breaks[0] = loc + insn_size;
2291
2292   /* Check for duplicated breakpoints, and also check that the second
2293      breakpoint is not within the atomic sequence.  */
2294   if (last_breakpoint
2295       && (breaks[1] == breaks[0]
2296           || (breaks[1] >= pc && breaks[1] <= closing_insn)))
2297     last_breakpoint = 0;
2298
2299   /* Insert the breakpoint at the end of the sequence, and one at the
2300      destination of the conditional branch, if it exists.  */
2301   for (index = 0; index <= last_breakpoint; index++)
2302     insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
2303
2304   return 1;
2305 }
2306
2307 struct displaced_step_closure
2308 {
2309   /* It is true when condition instruction, such as B.CON, TBZ, etc,
2310      is being displaced stepping.  */
2311   int cond;
2312
2313   /* PC adjustment offset after displaced stepping.  */
2314   int32_t pc_adjust;
2315 };
2316
2317 /* Data when visiting instructions for displaced stepping.  */
2318
2319 struct aarch64_displaced_step_data
2320 {
2321   struct aarch64_insn_data base;
2322
2323   /* The address where the instruction will be executed at.  */
2324   CORE_ADDR new_addr;
2325   /* Buffer of instructions to be copied to NEW_ADDR to execute.  */
2326   uint32_t insn_buf[DISPLACED_MODIFIED_INSNS];
2327   /* Number of instructions in INSN_BUF.  */
2328   unsigned insn_count;
2329   /* Registers when doing displaced stepping.  */
2330   struct regcache *regs;
2331
2332   struct displaced_step_closure *dsc;
2333 };
2334
2335 /* Implementation of aarch64_insn_visitor method "b".  */
2336
2337 static void
2338 aarch64_displaced_step_b (const int is_bl, const int32_t offset,
2339                           struct aarch64_insn_data *data)
2340 {
2341   struct aarch64_displaced_step_data *dsd
2342     = (struct aarch64_displaced_step_data *) data;
2343   int32_t new_offset = data->insn_addr - dsd->new_addr + offset;
2344
2345   if (can_encode_int32 (new_offset, 28))
2346     {
2347       /* Emit B rather than BL, because executing BL on a new address
2348          will get the wrong address into LR.  In order to avoid this,
2349          we emit B, and update LR if the instruction is BL.  */
2350       emit_b (dsd->insn_buf, 0, new_offset);
2351       dsd->insn_count++;
2352     }
2353   else
2354     {
2355       /* Write NOP.  */
2356       emit_nop (dsd->insn_buf);
2357       dsd->insn_count++;
2358       dsd->dsc->pc_adjust = offset;
2359     }
2360
2361   if (is_bl)
2362     {
2363       /* Update LR.  */
2364       regcache_cooked_write_unsigned (dsd->regs, AARCH64_LR_REGNUM,
2365                                       data->insn_addr + 4);
2366     }
2367 }
2368
2369 /* Implementation of aarch64_insn_visitor method "b_cond".  */
2370
2371 static void
2372 aarch64_displaced_step_b_cond (const unsigned cond, const int32_t offset,
2373                                struct aarch64_insn_data *data)
2374 {
2375   struct aarch64_displaced_step_data *dsd
2376     = (struct aarch64_displaced_step_data *) data;
2377   int32_t new_offset = data->insn_addr - dsd->new_addr + offset;
2378
2379   /* GDB has to fix up PC after displaced step this instruction
2380      differently according to the condition is true or false.  Instead
2381      of checking COND against conditional flags, we can use
2382      the following instructions, and GDB can tell how to fix up PC
2383      according to the PC value.
2384
2385      B.COND TAKEN    ; If cond is true, then jump to TAKEN.
2386      INSN1     ;
2387      TAKEN:
2388      INSN2
2389   */
2390
2391   emit_bcond (dsd->insn_buf, cond, 8);
2392   dsd->dsc->cond = 1;
2393   dsd->dsc->pc_adjust = offset;
2394   dsd->insn_count = 1;
2395 }
2396
2397 /* Dynamically allocate a new register.  If we know the register
2398    statically, we should make it a global as above instead of using this
2399    helper function.  */
2400
2401 static struct aarch64_register
2402 aarch64_register (unsigned num, int is64)
2403 {
2404   return (struct aarch64_register) { num, is64 };
2405 }
2406
2407 /* Implementation of aarch64_insn_visitor method "cb".  */
2408
2409 static void
2410 aarch64_displaced_step_cb (const int32_t offset, const int is_cbnz,
2411                            const unsigned rn, int is64,
2412                            struct aarch64_insn_data *data)
2413 {
2414   struct aarch64_displaced_step_data *dsd
2415     = (struct aarch64_displaced_step_data *) data;
2416   int32_t new_offset = data->insn_addr - dsd->new_addr + offset;
2417
2418   /* The offset is out of range for a compare and branch
2419      instruction.  We can use the following instructions instead:
2420
2421          CBZ xn, TAKEN   ; xn == 0, then jump to TAKEN.
2422          INSN1     ;
2423          TAKEN:
2424          INSN2
2425   */
2426   emit_cb (dsd->insn_buf, is_cbnz, aarch64_register (rn, is64), 8);
2427   dsd->insn_count = 1;
2428   dsd->dsc->cond = 1;
2429   dsd->dsc->pc_adjust = offset;
2430 }
2431
2432 /* Implementation of aarch64_insn_visitor method "tb".  */
2433
2434 static void
2435 aarch64_displaced_step_tb (const int32_t offset, int is_tbnz,
2436                            const unsigned rt, unsigned bit,
2437                            struct aarch64_insn_data *data)
2438 {
2439   struct aarch64_displaced_step_data *dsd
2440     = (struct aarch64_displaced_step_data *) data;
2441   int32_t new_offset = data->insn_addr - dsd->new_addr + offset;
2442
2443   /* The offset is out of range for a test bit and branch
2444      instruction We can use the following instructions instead:
2445
2446      TBZ xn, #bit, TAKEN ; xn[bit] == 0, then jump to TAKEN.
2447      INSN1         ;
2448      TAKEN:
2449      INSN2
2450
2451   */
2452   emit_tb (dsd->insn_buf, is_tbnz, bit, aarch64_register (rt, 1), 8);
2453   dsd->insn_count = 1;
2454   dsd->dsc->cond = 1;
2455   dsd->dsc->pc_adjust = offset;
2456 }
2457
2458 /* Implementation of aarch64_insn_visitor method "adr".  */
2459
2460 static void
2461 aarch64_displaced_step_adr (const int32_t offset, const unsigned rd,
2462                             const int is_adrp, struct aarch64_insn_data *data)
2463 {
2464   struct aarch64_displaced_step_data *dsd
2465     = (struct aarch64_displaced_step_data *) data;
2466   /* We know exactly the address the ADR{P,} instruction will compute.
2467      We can just write it to the destination register.  */
2468   CORE_ADDR address = data->insn_addr + offset;
2469
2470   if (is_adrp)
2471     {
2472       /* Clear the lower 12 bits of the offset to get the 4K page.  */
2473       regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rd,
2474                                       address & ~0xfff);
2475     }
2476   else
2477       regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rd,
2478                                       address);
2479
2480   dsd->dsc->pc_adjust = 4;
2481   emit_nop (dsd->insn_buf);
2482   dsd->insn_count = 1;
2483 }
2484
2485 /* Implementation of aarch64_insn_visitor method "ldr_literal".  */
2486
2487 static void
2488 aarch64_displaced_step_ldr_literal (const int32_t offset, const int is_sw,
2489                                     const unsigned rt, const int is64,
2490                                     struct aarch64_insn_data *data)
2491 {
2492   struct aarch64_displaced_step_data *dsd
2493     = (struct aarch64_displaced_step_data *) data;
2494   CORE_ADDR address = data->insn_addr + offset;
2495   struct aarch64_memory_operand zero = { MEMORY_OPERAND_OFFSET, 0 };
2496
2497   regcache_cooked_write_unsigned (dsd->regs, AARCH64_X0_REGNUM + rt,
2498                                   address);
2499
2500   if (is_sw)
2501     dsd->insn_count = emit_ldrsw (dsd->insn_buf, aarch64_register (rt, 1),
2502                                   aarch64_register (rt, 1), zero);
2503   else
2504     dsd->insn_count = emit_ldr (dsd->insn_buf, aarch64_register (rt, is64),
2505                                 aarch64_register (rt, 1), zero);
2506
2507   dsd->dsc->pc_adjust = 4;
2508 }
2509
2510 /* Implementation of aarch64_insn_visitor method "others".  */
2511
2512 static void
2513 aarch64_displaced_step_others (const uint32_t insn,
2514                                struct aarch64_insn_data *data)
2515 {
2516   struct aarch64_displaced_step_data *dsd
2517     = (struct aarch64_displaced_step_data *) data;
2518
2519   aarch64_emit_insn (dsd->insn_buf, insn);
2520   dsd->insn_count = 1;
2521
2522   if ((insn & 0xfffffc1f) == 0xd65f0000)
2523     {
2524       /* RET */
2525       dsd->dsc->pc_adjust = 0;
2526     }
2527   else
2528     dsd->dsc->pc_adjust = 4;
2529 }
2530
2531 static const struct aarch64_insn_visitor visitor =
2532 {
2533   aarch64_displaced_step_b,
2534   aarch64_displaced_step_b_cond,
2535   aarch64_displaced_step_cb,
2536   aarch64_displaced_step_tb,
2537   aarch64_displaced_step_adr,
2538   aarch64_displaced_step_ldr_literal,
2539   aarch64_displaced_step_others,
2540 };
2541
2542 /* Implement the "displaced_step_copy_insn" gdbarch method.  */
2543
2544 struct displaced_step_closure *
2545 aarch64_displaced_step_copy_insn (struct gdbarch *gdbarch,
2546                                   CORE_ADDR from, CORE_ADDR to,
2547                                   struct regcache *regs)
2548 {
2549   struct displaced_step_closure *dsc = NULL;
2550   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
2551   uint32_t insn = read_memory_unsigned_integer (from, 4, byte_order_for_code);
2552   struct aarch64_displaced_step_data dsd;
2553   aarch64_inst inst;
2554
2555   if (aarch64_decode_insn (insn, &inst, 1) != 0)
2556     return NULL;
2557
2558   /* Look for a Load Exclusive instruction which begins the sequence.  */
2559   if (inst.opcode->iclass == ldstexcl && bit (insn, 22))
2560     {
2561       /* We can't displaced step atomic sequences.  */
2562       return NULL;
2563     }
2564
2565   dsc = XCNEW (struct displaced_step_closure);
2566   dsd.base.insn_addr = from;
2567   dsd.new_addr = to;
2568   dsd.regs = regs;
2569   dsd.dsc = dsc;
2570   dsd.insn_count = 0;
2571   aarch64_relocate_instruction (insn, &visitor,
2572                                 (struct aarch64_insn_data *) &dsd);
2573   gdb_assert (dsd.insn_count <= DISPLACED_MODIFIED_INSNS);
2574
2575   if (dsd.insn_count != 0)
2576     {
2577       int i;
2578
2579       /* Instruction can be relocated to scratch pad.  Copy
2580          relocated instruction(s) there.  */
2581       for (i = 0; i < dsd.insn_count; i++)
2582         {
2583           if (debug_displaced)
2584             {
2585               debug_printf ("displaced: writing insn ");
2586               debug_printf ("%.8x", dsd.insn_buf[i]);
2587               debug_printf (" at %s\n", paddress (gdbarch, to + i * 4));
2588             }
2589           write_memory_unsigned_integer (to + i * 4, 4, byte_order_for_code,
2590                                          (ULONGEST) dsd.insn_buf[i]);
2591         }
2592     }
2593   else
2594     {
2595       xfree (dsc);
2596       dsc = NULL;
2597     }
2598
2599   return dsc;
2600 }
2601
2602 /* Implement the "displaced_step_fixup" gdbarch method.  */
2603
2604 void
2605 aarch64_displaced_step_fixup (struct gdbarch *gdbarch,
2606                               struct displaced_step_closure *dsc,
2607                               CORE_ADDR from, CORE_ADDR to,
2608                               struct regcache *regs)
2609 {
2610   if (dsc->cond)
2611     {
2612       ULONGEST pc;
2613
2614       regcache_cooked_read_unsigned (regs, AARCH64_PC_REGNUM, &pc);
2615       if (pc - to == 8)
2616         {
2617           /* Condition is true.  */
2618         }
2619       else if (pc - to == 4)
2620         {
2621           /* Condition is false.  */
2622           dsc->pc_adjust = 4;
2623         }
2624       else
2625         gdb_assert_not_reached ("Unexpected PC value after displaced stepping");
2626     }
2627
2628   if (dsc->pc_adjust != 0)
2629     {
2630       if (debug_displaced)
2631         {
2632           debug_printf ("displaced: fixup: set PC to %s:%d\n",
2633                         paddress (gdbarch, from), dsc->pc_adjust);
2634         }
2635       regcache_cooked_write_unsigned (regs, AARCH64_PC_REGNUM,
2636                                       from + dsc->pc_adjust);
2637     }
2638 }
2639
2640 /* Implement the "displaced_step_hw_singlestep" gdbarch method.  */
2641
2642 int
2643 aarch64_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
2644                                       struct displaced_step_closure *closure)
2645 {
2646   return 1;
2647 }
2648
2649 /* Initialize the current architecture based on INFO.  If possible,
2650    re-use an architecture from ARCHES, which is a list of
2651    architectures already created during this debugging session.
2652
2653    Called e.g. at program startup, when reading a core file, and when
2654    reading a binary file.  */
2655
2656 static struct gdbarch *
2657 aarch64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2658 {
2659   struct gdbarch_tdep *tdep;
2660   struct gdbarch *gdbarch;
2661   struct gdbarch_list *best_arch;
2662   struct tdesc_arch_data *tdesc_data = NULL;
2663   const struct target_desc *tdesc = info.target_desc;
2664   int i;
2665   int have_fpa_registers = 1;
2666   int valid_p = 1;
2667   const struct tdesc_feature *feature;
2668   int num_regs = 0;
2669   int num_pseudo_regs = 0;
2670
2671   /* Ensure we always have a target descriptor.  */
2672   if (!tdesc_has_registers (tdesc))
2673     tdesc = tdesc_aarch64;
2674
2675   gdb_assert (tdesc);
2676
2677   feature = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.core");
2678
2679   if (feature == NULL)
2680     return NULL;
2681
2682   tdesc_data = tdesc_data_alloc ();
2683
2684   /* Validate the descriptor provides the mandatory core R registers
2685      and allocate their numbers.  */
2686   for (i = 0; i < ARRAY_SIZE (aarch64_r_register_names); i++)
2687     valid_p &=
2688       tdesc_numbered_register (feature, tdesc_data, AARCH64_X0_REGNUM + i,
2689                                aarch64_r_register_names[i]);
2690
2691   num_regs = AARCH64_X0_REGNUM + i;
2692
2693   /* Look for the V registers.  */
2694   feature = tdesc_find_feature (tdesc, "org.gnu.gdb.aarch64.fpu");
2695   if (feature)
2696     {
2697       /* Validate the descriptor provides the mandatory V registers
2698          and allocate their numbers.  */
2699       for (i = 0; i < ARRAY_SIZE (aarch64_v_register_names); i++)
2700         valid_p &=
2701           tdesc_numbered_register (feature, tdesc_data, AARCH64_V0_REGNUM + i,
2702                                    aarch64_v_register_names[i]);
2703
2704       num_regs = AARCH64_V0_REGNUM + i;
2705
2706       num_pseudo_regs += 32;    /* add the Qn scalar register pseudos */
2707       num_pseudo_regs += 32;    /* add the Dn scalar register pseudos */
2708       num_pseudo_regs += 32;    /* add the Sn scalar register pseudos */
2709       num_pseudo_regs += 32;    /* add the Hn scalar register pseudos */
2710       num_pseudo_regs += 32;    /* add the Bn scalar register pseudos */
2711     }
2712
2713   if (!valid_p)
2714     {
2715       tdesc_data_cleanup (tdesc_data);
2716       return NULL;
2717     }
2718
2719   /* AArch64 code is always little-endian.  */
2720   info.byte_order_for_code = BFD_ENDIAN_LITTLE;
2721
2722   /* If there is already a candidate, use it.  */
2723   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
2724        best_arch != NULL;
2725        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
2726     {
2727       /* Found a match.  */
2728       break;
2729     }
2730
2731   if (best_arch != NULL)
2732     {
2733       if (tdesc_data != NULL)
2734         tdesc_data_cleanup (tdesc_data);
2735       return best_arch->gdbarch;
2736     }
2737
2738   tdep = XCNEW (struct gdbarch_tdep);
2739   gdbarch = gdbarch_alloc (&info, tdep);
2740
2741   /* This should be low enough for everything.  */
2742   tdep->lowest_pc = 0x20;
2743   tdep->jb_pc = -1;             /* Longjump support not enabled by default.  */
2744   tdep->jb_elt_size = 8;
2745
2746   set_gdbarch_push_dummy_call (gdbarch, aarch64_push_dummy_call);
2747   set_gdbarch_frame_align (gdbarch, aarch64_frame_align);
2748
2749   /* Frame handling.  */
2750   set_gdbarch_dummy_id (gdbarch, aarch64_dummy_id);
2751   set_gdbarch_unwind_pc (gdbarch, aarch64_unwind_pc);
2752   set_gdbarch_unwind_sp (gdbarch, aarch64_unwind_sp);
2753
2754   /* Advance PC across function entry code.  */
2755   set_gdbarch_skip_prologue (gdbarch, aarch64_skip_prologue);
2756
2757   /* The stack grows downward.  */
2758   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2759
2760   /* Breakpoint manipulation.  */
2761   set_gdbarch_breakpoint_from_pc (gdbarch, aarch64_breakpoint_from_pc);
2762   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
2763   set_gdbarch_software_single_step (gdbarch, aarch64_software_single_step);
2764
2765   /* Information about registers, etc.  */
2766   set_gdbarch_sp_regnum (gdbarch, AARCH64_SP_REGNUM);
2767   set_gdbarch_pc_regnum (gdbarch, AARCH64_PC_REGNUM);
2768   set_gdbarch_num_regs (gdbarch, num_regs);
2769
2770   set_gdbarch_num_pseudo_regs (gdbarch, num_pseudo_regs);
2771   set_gdbarch_pseudo_register_read_value (gdbarch, aarch64_pseudo_read_value);
2772   set_gdbarch_pseudo_register_write (gdbarch, aarch64_pseudo_write);
2773   set_tdesc_pseudo_register_name (gdbarch, aarch64_pseudo_register_name);
2774   set_tdesc_pseudo_register_type (gdbarch, aarch64_pseudo_register_type);
2775   set_tdesc_pseudo_register_reggroup_p (gdbarch,
2776                                         aarch64_pseudo_register_reggroup_p);
2777
2778   /* ABI */
2779   set_gdbarch_short_bit (gdbarch, 16);
2780   set_gdbarch_int_bit (gdbarch, 32);
2781   set_gdbarch_float_bit (gdbarch, 32);
2782   set_gdbarch_double_bit (gdbarch, 64);
2783   set_gdbarch_long_double_bit (gdbarch, 128);
2784   set_gdbarch_long_bit (gdbarch, 64);
2785   set_gdbarch_long_long_bit (gdbarch, 64);
2786   set_gdbarch_ptr_bit (gdbarch, 64);
2787   set_gdbarch_char_signed (gdbarch, 0);
2788   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
2789   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
2790   set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
2791
2792   /* Internal <-> external register number maps.  */
2793   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, aarch64_dwarf_reg_to_regnum);
2794
2795   /* Returning results.  */
2796   set_gdbarch_return_value (gdbarch, aarch64_return_value);
2797
2798   /* Disassembly.  */
2799   set_gdbarch_print_insn (gdbarch, aarch64_gdb_print_insn);
2800
2801   /* Virtual tables.  */
2802   set_gdbarch_vbit_in_delta (gdbarch, 1);
2803
2804   /* Hook in the ABI-specific overrides, if they have been registered.  */
2805   info.target_desc = tdesc;
2806   info.tdep_info = (void *) tdesc_data;
2807   gdbarch_init_osabi (info, gdbarch);
2808
2809   dwarf2_frame_set_init_reg (gdbarch, aarch64_dwarf2_frame_init_reg);
2810
2811   /* Add some default predicates.  */
2812   frame_unwind_append_unwinder (gdbarch, &aarch64_stub_unwind);
2813   dwarf2_append_unwinders (gdbarch);
2814   frame_unwind_append_unwinder (gdbarch, &aarch64_prologue_unwind);
2815
2816   frame_base_set_default (gdbarch, &aarch64_normal_base);
2817
2818   /* Now we have tuned the configuration, set a few final things,
2819      based on what the OS ABI has told us.  */
2820
2821   if (tdep->jb_pc >= 0)
2822     set_gdbarch_get_longjmp_target (gdbarch, aarch64_get_longjmp_target);
2823
2824   set_gdbarch_gen_return_address (gdbarch, aarch64_gen_return_address);
2825
2826   tdesc_use_registers (gdbarch, tdesc, tdesc_data);
2827
2828   /* Add standard register aliases.  */
2829   for (i = 0; i < ARRAY_SIZE (aarch64_register_aliases); i++)
2830     user_reg_add (gdbarch, aarch64_register_aliases[i].name,
2831                   value_of_aarch64_user_reg,
2832                   &aarch64_register_aliases[i].regnum);
2833
2834   return gdbarch;
2835 }
2836
2837 static void
2838 aarch64_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
2839 {
2840   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2841
2842   if (tdep == NULL)
2843     return;
2844
2845   fprintf_unfiltered (file, _("aarch64_dump_tdep: Lowest pc = 0x%s"),
2846                       paddress (gdbarch, tdep->lowest_pc));
2847 }
2848
2849 /* Suppress warning from -Wmissing-prototypes.  */
2850 extern initialize_file_ftype _initialize_aarch64_tdep;
2851
2852 void
2853 _initialize_aarch64_tdep (void)
2854 {
2855   gdbarch_register (bfd_arch_aarch64, aarch64_gdbarch_init,
2856                     aarch64_dump_tdep);
2857
2858   initialize_tdesc_aarch64 ();
2859
2860   /* Debug this file's internals.  */
2861   add_setshow_boolean_cmd ("aarch64", class_maintenance, &aarch64_debug, _("\
2862 Set AArch64 debugging."), _("\
2863 Show AArch64 debugging."), _("\
2864 When on, AArch64 specific debugging is enabled."),
2865                             NULL,
2866                             show_aarch64_debug,
2867                             &setdebuglist, &showdebuglist);
2868 }
2869
2870 /* AArch64 process record-replay related structures, defines etc.  */
2871
2872 #define REG_ALLOC(REGS, LENGTH, RECORD_BUF) \
2873         do  \
2874           { \
2875             unsigned int reg_len = LENGTH; \
2876             if (reg_len) \
2877               { \
2878                 REGS = XNEWVEC (uint32_t, reg_len); \
2879                 memcpy(&REGS[0], &RECORD_BUF[0], sizeof(uint32_t)*LENGTH); \
2880               } \
2881           } \
2882         while (0)
2883
2884 #define MEM_ALLOC(MEMS, LENGTH, RECORD_BUF) \
2885         do  \
2886           { \
2887             unsigned int mem_len = LENGTH; \
2888             if (mem_len) \
2889             { \
2890               MEMS =  XNEWVEC (struct aarch64_mem_r, mem_len);  \
2891               memcpy(&MEMS->len, &RECORD_BUF[0], \
2892                      sizeof(struct aarch64_mem_r) * LENGTH); \
2893             } \
2894           } \
2895           while (0)
2896
2897 /* AArch64 record/replay structures and enumerations.  */
2898
2899 struct aarch64_mem_r
2900 {
2901   uint64_t len;    /* Record length.  */
2902   uint64_t addr;   /* Memory address.  */
2903 };
2904
2905 enum aarch64_record_result
2906 {
2907   AARCH64_RECORD_SUCCESS,
2908   AARCH64_RECORD_FAILURE,
2909   AARCH64_RECORD_UNSUPPORTED,
2910   AARCH64_RECORD_UNKNOWN
2911 };
2912
2913 typedef struct insn_decode_record_t
2914 {
2915   struct gdbarch *gdbarch;
2916   struct regcache *regcache;
2917   CORE_ADDR this_addr;                 /* Address of insn to be recorded.  */
2918   uint32_t aarch64_insn;               /* Insn to be recorded.  */
2919   uint32_t mem_rec_count;              /* Count of memory records.  */
2920   uint32_t reg_rec_count;              /* Count of register records.  */
2921   uint32_t *aarch64_regs;              /* Registers to be recorded.  */
2922   struct aarch64_mem_r *aarch64_mems;  /* Memory locations to be recorded.  */
2923 } insn_decode_record;
2924
2925 /* Record handler for data processing - register instructions.  */
2926
2927 static unsigned int
2928 aarch64_record_data_proc_reg (insn_decode_record *aarch64_insn_r)
2929 {
2930   uint8_t reg_rd, insn_bits24_27, insn_bits21_23;
2931   uint32_t record_buf[4];
2932
2933   reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
2934   insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
2935   insn_bits21_23 = bits (aarch64_insn_r->aarch64_insn, 21, 23);
2936
2937   if (!bit (aarch64_insn_r->aarch64_insn, 28))
2938     {
2939       uint8_t setflags;
2940
2941       /* Logical (shifted register).  */
2942       if (insn_bits24_27 == 0x0a)
2943         setflags = (bits (aarch64_insn_r->aarch64_insn, 29, 30) == 0x03);
2944       /* Add/subtract.  */
2945       else if (insn_bits24_27 == 0x0b)
2946         setflags = bit (aarch64_insn_r->aarch64_insn, 29);
2947       else
2948         return AARCH64_RECORD_UNKNOWN;
2949
2950       record_buf[0] = reg_rd;
2951       aarch64_insn_r->reg_rec_count = 1;
2952       if (setflags)
2953         record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
2954     }
2955   else
2956     {
2957       if (insn_bits24_27 == 0x0b)
2958         {
2959           /* Data-processing (3 source).  */
2960           record_buf[0] = reg_rd;
2961           aarch64_insn_r->reg_rec_count = 1;
2962         }
2963       else if (insn_bits24_27 == 0x0a)
2964         {
2965           if (insn_bits21_23 == 0x00)
2966             {
2967               /* Add/subtract (with carry).  */
2968               record_buf[0] = reg_rd;
2969               aarch64_insn_r->reg_rec_count = 1;
2970               if (bit (aarch64_insn_r->aarch64_insn, 29))
2971                 {
2972                   record_buf[1] = AARCH64_CPSR_REGNUM;
2973                   aarch64_insn_r->reg_rec_count = 2;
2974                 }
2975             }
2976           else if (insn_bits21_23 == 0x02)
2977             {
2978               /* Conditional compare (register) and conditional compare
2979                  (immediate) instructions.  */
2980               record_buf[0] = AARCH64_CPSR_REGNUM;
2981               aarch64_insn_r->reg_rec_count = 1;
2982             }
2983           else if (insn_bits21_23 == 0x04 || insn_bits21_23 == 0x06)
2984             {
2985               /* CConditional select.  */
2986               /* Data-processing (2 source).  */
2987               /* Data-processing (1 source).  */
2988               record_buf[0] = reg_rd;
2989               aarch64_insn_r->reg_rec_count = 1;
2990             }
2991           else
2992             return AARCH64_RECORD_UNKNOWN;
2993         }
2994     }
2995
2996   REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
2997              record_buf);
2998   return AARCH64_RECORD_SUCCESS;
2999 }
3000
3001 /* Record handler for data processing - immediate instructions.  */
3002
3003 static unsigned int
3004 aarch64_record_data_proc_imm (insn_decode_record *aarch64_insn_r)
3005 {
3006   uint8_t reg_rd, insn_bit28, insn_bit23, insn_bits24_27, setflags;
3007   uint32_t record_buf[4];
3008
3009   reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3010   insn_bit28 = bit (aarch64_insn_r->aarch64_insn, 28);
3011   insn_bit23 = bit (aarch64_insn_r->aarch64_insn, 23);
3012   insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3013
3014   if (insn_bits24_27 == 0x00                     /* PC rel addressing.  */
3015      || insn_bits24_27 == 0x03                   /* Bitfield and Extract.  */
3016      || (insn_bits24_27 == 0x02 && insn_bit23))  /* Move wide (immediate).  */
3017     {
3018       record_buf[0] = reg_rd;
3019       aarch64_insn_r->reg_rec_count = 1;
3020     }
3021   else if (insn_bits24_27 == 0x01)
3022     {
3023       /* Add/Subtract (immediate).  */
3024       setflags = bit (aarch64_insn_r->aarch64_insn, 29);
3025       record_buf[0] = reg_rd;
3026       aarch64_insn_r->reg_rec_count = 1;
3027       if (setflags)
3028         record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
3029     }
3030   else if (insn_bits24_27 == 0x02 && !insn_bit23)
3031     {
3032       /* Logical (immediate).  */
3033       setflags = bits (aarch64_insn_r->aarch64_insn, 29, 30) == 0x03;
3034       record_buf[0] = reg_rd;
3035       aarch64_insn_r->reg_rec_count = 1;
3036       if (setflags)
3037         record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_CPSR_REGNUM;
3038     }
3039   else
3040     return AARCH64_RECORD_UNKNOWN;
3041
3042   REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3043              record_buf);
3044   return AARCH64_RECORD_SUCCESS;
3045 }
3046
3047 /* Record handler for branch, exception generation and system instructions.  */
3048
3049 static unsigned int
3050 aarch64_record_branch_except_sys (insn_decode_record *aarch64_insn_r)
3051 {
3052   struct gdbarch_tdep *tdep = gdbarch_tdep (aarch64_insn_r->gdbarch);
3053   uint8_t insn_bits24_27, insn_bits28_31, insn_bits22_23;
3054   uint32_t record_buf[4];
3055
3056   insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3057   insn_bits28_31 = bits (aarch64_insn_r->aarch64_insn, 28, 31);
3058   insn_bits22_23 = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3059
3060   if (insn_bits28_31 == 0x0d)
3061     {
3062       /* Exception generation instructions. */
3063       if (insn_bits24_27 == 0x04)
3064         {
3065           if (!bits (aarch64_insn_r->aarch64_insn, 2, 4)
3066               && !bits (aarch64_insn_r->aarch64_insn, 21, 23)
3067               && bits (aarch64_insn_r->aarch64_insn, 0, 1) == 0x01)
3068             {
3069               ULONGEST svc_number;
3070
3071               regcache_raw_read_unsigned (aarch64_insn_r->regcache, 8,
3072                                           &svc_number);
3073               return tdep->aarch64_syscall_record (aarch64_insn_r->regcache,
3074                                                    svc_number);
3075             }
3076           else
3077             return AARCH64_RECORD_UNSUPPORTED;
3078         }
3079       /* System instructions. */
3080       else if (insn_bits24_27 == 0x05 && insn_bits22_23 == 0x00)
3081         {
3082           uint32_t reg_rt, reg_crn;
3083
3084           reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3085           reg_crn = bits (aarch64_insn_r->aarch64_insn, 12, 15);
3086
3087           /* Record rt in case of sysl and mrs instructions.  */
3088           if (bit (aarch64_insn_r->aarch64_insn, 21))
3089             {
3090               record_buf[0] = reg_rt;
3091               aarch64_insn_r->reg_rec_count = 1;
3092             }
3093           /* Record cpsr for hint and msr(immediate) instructions.  */
3094           else if (reg_crn == 0x02 || reg_crn == 0x04)
3095             {
3096               record_buf[0] = AARCH64_CPSR_REGNUM;
3097               aarch64_insn_r->reg_rec_count = 1;
3098             }
3099         }
3100       /* Unconditional branch (register).  */
3101       else if((insn_bits24_27 & 0x0e) == 0x06)
3102         {
3103           record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3104           if (bits (aarch64_insn_r->aarch64_insn, 21, 22) == 0x01)
3105             record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_LR_REGNUM;
3106         }
3107       else
3108         return AARCH64_RECORD_UNKNOWN;
3109     }
3110   /* Unconditional branch (immediate).  */
3111   else if ((insn_bits28_31 & 0x07) == 0x01 && (insn_bits24_27 & 0x0c) == 0x04)
3112     {
3113       record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3114       if (bit (aarch64_insn_r->aarch64_insn, 31))
3115         record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_LR_REGNUM;
3116     }
3117   else
3118     /* Compare & branch (immediate), Test & branch (immediate) and
3119        Conditional branch (immediate).  */
3120     record_buf[aarch64_insn_r->reg_rec_count++] = AARCH64_PC_REGNUM;
3121
3122   REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3123              record_buf);
3124   return AARCH64_RECORD_SUCCESS;
3125 }
3126
3127 /* Record handler for advanced SIMD load and store instructions.  */
3128
3129 static unsigned int
3130 aarch64_record_asimd_load_store (insn_decode_record *aarch64_insn_r)
3131 {
3132   CORE_ADDR address;
3133   uint64_t addr_offset = 0;
3134   uint32_t record_buf[24];
3135   uint64_t record_buf_mem[24];
3136   uint32_t reg_rn, reg_rt;
3137   uint32_t reg_index = 0, mem_index = 0;
3138   uint8_t opcode_bits, size_bits;
3139
3140   reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3141   reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
3142   size_bits = bits (aarch64_insn_r->aarch64_insn, 10, 11);
3143   opcode_bits = bits (aarch64_insn_r->aarch64_insn, 12, 15);
3144   regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn, &address);
3145
3146   if (record_debug)
3147     debug_printf ("Process record: Advanced SIMD load/store\n");
3148
3149   /* Load/store single structure.  */
3150   if (bit (aarch64_insn_r->aarch64_insn, 24))
3151     {
3152       uint8_t sindex, scale, selem, esize, replicate = 0;
3153       scale = opcode_bits >> 2;
3154       selem = ((opcode_bits & 0x02) |
3155               bit (aarch64_insn_r->aarch64_insn, 21)) + 1;
3156       switch (scale)
3157         {
3158         case 1:
3159           if (size_bits & 0x01)
3160             return AARCH64_RECORD_UNKNOWN;
3161           break;
3162         case 2:
3163           if ((size_bits >> 1) & 0x01)
3164             return AARCH64_RECORD_UNKNOWN;
3165           if (size_bits & 0x01)
3166             {
3167               if (!((opcode_bits >> 1) & 0x01))
3168                 scale = 3;
3169               else
3170                 return AARCH64_RECORD_UNKNOWN;
3171             }
3172           break;
3173         case 3:
3174           if (bit (aarch64_insn_r->aarch64_insn, 22) && !(opcode_bits & 0x01))
3175             {
3176               scale = size_bits;
3177               replicate = 1;
3178               break;
3179             }
3180           else
3181             return AARCH64_RECORD_UNKNOWN;
3182         default:
3183           break;
3184         }
3185       esize = 8 << scale;
3186       if (replicate)
3187         for (sindex = 0; sindex < selem; sindex++)
3188           {
3189             record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
3190             reg_rt = (reg_rt + 1) % 32;
3191           }
3192       else
3193         {
3194           for (sindex = 0; sindex < selem; sindex++)
3195             {
3196               if (bit (aarch64_insn_r->aarch64_insn, 22))
3197                 record_buf[reg_index++] = reg_rt + AARCH64_V0_REGNUM;
3198               else
3199                 {
3200                   record_buf_mem[mem_index++] = esize / 8;
3201                   record_buf_mem[mem_index++] = address + addr_offset;
3202                 }
3203               addr_offset = addr_offset + (esize / 8);
3204               reg_rt = (reg_rt + 1) % 32;
3205             }
3206         }
3207     }
3208   /* Load/store multiple structure.  */
3209   else
3210     {
3211       uint8_t selem, esize, rpt, elements;
3212       uint8_t eindex, rindex;
3213
3214       esize = 8 << size_bits;
3215       if (bit (aarch64_insn_r->aarch64_insn, 30))
3216         elements = 128 / esize;
3217       else
3218         elements = 64 / esize;
3219
3220       switch (opcode_bits)
3221         {
3222         /*LD/ST4 (4 Registers).  */
3223         case 0:
3224           rpt = 1;
3225           selem = 4;
3226           break;
3227         /*LD/ST1 (4 Registers).  */
3228         case 2:
3229           rpt = 4;
3230           selem = 1;
3231           break;
3232         /*LD/ST3 (3 Registers).  */
3233         case 4:
3234           rpt = 1;
3235           selem = 3;
3236           break;
3237         /*LD/ST1 (3 Registers).  */
3238         case 6:
3239           rpt = 3;
3240           selem = 1;
3241           break;
3242         /*LD/ST1 (1 Register).  */
3243         case 7:
3244           rpt = 1;
3245           selem = 1;
3246           break;
3247         /*LD/ST2 (2 Registers).  */
3248         case 8:
3249           rpt = 1;
3250           selem = 2;
3251           break;
3252         /*LD/ST1 (2 Registers).  */
3253         case 10:
3254           rpt = 2;
3255           selem = 1;
3256           break;
3257         default:
3258           return AARCH64_RECORD_UNSUPPORTED;
3259           break;
3260         }
3261       for (rindex = 0; rindex < rpt; rindex++)
3262         for (eindex = 0; eindex < elements; eindex++)
3263           {
3264             uint8_t reg_tt, sindex;
3265             reg_tt = (reg_rt + rindex) % 32;
3266             for (sindex = 0; sindex < selem; sindex++)
3267               {
3268                 if (bit (aarch64_insn_r->aarch64_insn, 22))
3269                   record_buf[reg_index++] = reg_tt + AARCH64_V0_REGNUM;
3270                 else
3271                   {
3272                     record_buf_mem[mem_index++] = esize / 8;
3273                     record_buf_mem[mem_index++] = address + addr_offset;
3274                   }
3275                 addr_offset = addr_offset + (esize / 8);
3276                 reg_tt = (reg_tt + 1) % 32;
3277               }
3278           }
3279     }
3280
3281   if (bit (aarch64_insn_r->aarch64_insn, 23))
3282     record_buf[reg_index++] = reg_rn;
3283
3284   aarch64_insn_r->reg_rec_count = reg_index;
3285   aarch64_insn_r->mem_rec_count = mem_index / 2;
3286   MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
3287              record_buf_mem);
3288   REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3289              record_buf);
3290   return AARCH64_RECORD_SUCCESS;
3291 }
3292
3293 /* Record handler for load and store instructions.  */
3294
3295 static unsigned int
3296 aarch64_record_load_store (insn_decode_record *aarch64_insn_r)
3297 {
3298   uint8_t insn_bits24_27, insn_bits28_29, insn_bits10_11;
3299   uint8_t insn_bit23, insn_bit21;
3300   uint8_t opc, size_bits, ld_flag, vector_flag;
3301   uint32_t reg_rn, reg_rt, reg_rt2;
3302   uint64_t datasize, offset;
3303   uint32_t record_buf[8];
3304   uint64_t record_buf_mem[8];
3305   CORE_ADDR address;
3306
3307   insn_bits10_11 = bits (aarch64_insn_r->aarch64_insn, 10, 11);
3308   insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3309   insn_bits28_29 = bits (aarch64_insn_r->aarch64_insn, 28, 29);
3310   insn_bit21 = bit (aarch64_insn_r->aarch64_insn, 21);
3311   insn_bit23 = bit (aarch64_insn_r->aarch64_insn, 23);
3312   ld_flag = bit (aarch64_insn_r->aarch64_insn, 22);
3313   vector_flag = bit (aarch64_insn_r->aarch64_insn, 26);
3314   reg_rt = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3315   reg_rn = bits (aarch64_insn_r->aarch64_insn, 5, 9);
3316   reg_rt2 = bits (aarch64_insn_r->aarch64_insn, 10, 14);
3317   size_bits = bits (aarch64_insn_r->aarch64_insn, 30, 31);
3318
3319   /* Load/store exclusive.  */
3320   if (insn_bits24_27 == 0x08 && insn_bits28_29 == 0x00)
3321     {
3322       if (record_debug)
3323         debug_printf ("Process record: load/store exclusive\n");
3324
3325       if (ld_flag)
3326         {
3327           record_buf[0] = reg_rt;
3328           aarch64_insn_r->reg_rec_count = 1;
3329           if (insn_bit21)
3330             {
3331               record_buf[1] = reg_rt2;
3332               aarch64_insn_r->reg_rec_count = 2;
3333             }
3334         }
3335       else
3336         {
3337           if (insn_bit21)
3338             datasize = (8 << size_bits) * 2;
3339           else
3340             datasize = (8 << size_bits);
3341           regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3342                                       &address);
3343           record_buf_mem[0] = datasize / 8;
3344           record_buf_mem[1] = address;
3345           aarch64_insn_r->mem_rec_count = 1;
3346           if (!insn_bit23)
3347             {
3348               /* Save register rs.  */
3349               record_buf[0] = bits (aarch64_insn_r->aarch64_insn, 16, 20);
3350               aarch64_insn_r->reg_rec_count = 1;
3351             }
3352         }
3353     }
3354   /* Load register (literal) instructions decoding.  */
3355   else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x01)
3356     {
3357       if (record_debug)
3358         debug_printf ("Process record: load register (literal)\n");
3359       if (vector_flag)
3360         record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3361       else
3362         record_buf[0] = reg_rt;
3363       aarch64_insn_r->reg_rec_count = 1;
3364     }
3365   /* All types of load/store pair instructions decoding.  */
3366   else if ((insn_bits24_27 & 0x0a) == 0x08 && insn_bits28_29 == 0x02)
3367     {
3368       if (record_debug)
3369         debug_printf ("Process record: load/store pair\n");
3370
3371       if (ld_flag)
3372         {
3373           if (vector_flag)
3374             {
3375               record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3376               record_buf[1] = reg_rt2 + AARCH64_V0_REGNUM;
3377             }
3378           else
3379             {
3380               record_buf[0] = reg_rt;
3381               record_buf[1] = reg_rt2;
3382             }
3383           aarch64_insn_r->reg_rec_count = 2;
3384         }
3385       else
3386         {
3387           uint16_t imm7_off;
3388           imm7_off = bits (aarch64_insn_r->aarch64_insn, 15, 21);
3389           if (!vector_flag)
3390             size_bits = size_bits >> 1;
3391           datasize = 8 << (2 + size_bits);
3392           offset = (imm7_off & 0x40) ? (~imm7_off & 0x007f) + 1 : imm7_off;
3393           offset = offset << (2 + size_bits);
3394           regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3395                                       &address);
3396           if (!((insn_bits24_27 & 0x0b) == 0x08 && insn_bit23))
3397             {
3398               if (imm7_off & 0x40)
3399                 address = address - offset;
3400               else
3401                 address = address + offset;
3402             }
3403
3404           record_buf_mem[0] = datasize / 8;
3405           record_buf_mem[1] = address;
3406           record_buf_mem[2] = datasize / 8;
3407           record_buf_mem[3] = address + (datasize / 8);
3408           aarch64_insn_r->mem_rec_count = 2;
3409         }
3410       if (bit (aarch64_insn_r->aarch64_insn, 23))
3411         record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
3412     }
3413   /* Load/store register (unsigned immediate) instructions.  */
3414   else if ((insn_bits24_27 & 0x0b) == 0x09 && insn_bits28_29 == 0x03)
3415     {
3416       opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3417       if (!(opc >> 1))
3418         if (opc & 0x01)
3419           ld_flag = 0x01;
3420         else
3421           ld_flag = 0x0;
3422       else
3423         if (size_bits != 0x03)
3424           ld_flag = 0x01;
3425         else
3426           return AARCH64_RECORD_UNKNOWN;
3427
3428       if (record_debug)
3429         {
3430           debug_printf ("Process record: load/store (unsigned immediate):"
3431                         " size %x V %d opc %x\n", size_bits, vector_flag,
3432                         opc);
3433         }
3434
3435       if (!ld_flag)
3436         {
3437           offset = bits (aarch64_insn_r->aarch64_insn, 10, 21);
3438           datasize = 8 << size_bits;
3439           regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3440                                       &address);
3441           offset = offset << size_bits;
3442           address = address + offset;
3443
3444           record_buf_mem[0] = datasize >> 3;
3445           record_buf_mem[1] = address;
3446           aarch64_insn_r->mem_rec_count = 1;
3447         }
3448       else
3449         {
3450           if (vector_flag)
3451             record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3452           else
3453             record_buf[0] = reg_rt;
3454           aarch64_insn_r->reg_rec_count = 1;
3455         }
3456     }
3457   /* Load/store register (register offset) instructions.  */
3458   else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
3459            && insn_bits10_11 == 0x02 && insn_bit21)
3460     {
3461       if (record_debug)
3462         debug_printf ("Process record: load/store (register offset)\n");
3463       opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3464       if (!(opc >> 1))
3465         if (opc & 0x01)
3466           ld_flag = 0x01;
3467         else
3468           ld_flag = 0x0;
3469       else
3470         if (size_bits != 0x03)
3471           ld_flag = 0x01;
3472         else
3473           return AARCH64_RECORD_UNKNOWN;
3474
3475       if (!ld_flag)
3476         {
3477           ULONGEST reg_rm_val;
3478
3479           regcache_raw_read_unsigned (aarch64_insn_r->regcache,
3480                      bits (aarch64_insn_r->aarch64_insn, 16, 20), &reg_rm_val);
3481           if (bit (aarch64_insn_r->aarch64_insn, 12))
3482             offset = reg_rm_val << size_bits;
3483           else
3484             offset = reg_rm_val;
3485           datasize = 8 << size_bits;
3486           regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3487                                       &address);
3488           address = address + offset;
3489           record_buf_mem[0] = datasize >> 3;
3490           record_buf_mem[1] = address;
3491           aarch64_insn_r->mem_rec_count = 1;
3492         }
3493       else
3494         {
3495           if (vector_flag)
3496             record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3497           else
3498             record_buf[0] = reg_rt;
3499           aarch64_insn_r->reg_rec_count = 1;
3500         }
3501     }
3502   /* Load/store register (immediate and unprivileged) instructions.  */
3503   else if ((insn_bits24_27 & 0x0b) == 0x08 && insn_bits28_29 == 0x03
3504            && !insn_bit21)
3505     {
3506       if (record_debug)
3507         {
3508           debug_printf ("Process record: load/store "
3509                         "(immediate and unprivileged)\n");
3510         }
3511       opc = bits (aarch64_insn_r->aarch64_insn, 22, 23);
3512       if (!(opc >> 1))
3513         if (opc & 0x01)
3514           ld_flag = 0x01;
3515         else
3516           ld_flag = 0x0;
3517       else
3518         if (size_bits != 0x03)
3519           ld_flag = 0x01;
3520         else
3521           return AARCH64_RECORD_UNKNOWN;
3522
3523       if (!ld_flag)
3524         {
3525           uint16_t imm9_off;
3526           imm9_off = bits (aarch64_insn_r->aarch64_insn, 12, 20);
3527           offset = (imm9_off & 0x0100) ? (((~imm9_off) & 0x01ff) + 1) : imm9_off;
3528           datasize = 8 << size_bits;
3529           regcache_raw_read_unsigned (aarch64_insn_r->regcache, reg_rn,
3530                                       &address);
3531           if (insn_bits10_11 != 0x01)
3532             {
3533               if (imm9_off & 0x0100)
3534                 address = address - offset;
3535               else
3536                 address = address + offset;
3537             }
3538           record_buf_mem[0] = datasize >> 3;
3539           record_buf_mem[1] = address;
3540           aarch64_insn_r->mem_rec_count = 1;
3541         }
3542       else
3543         {
3544           if (vector_flag)
3545             record_buf[0] = reg_rt + AARCH64_V0_REGNUM;
3546           else
3547             record_buf[0] = reg_rt;
3548           aarch64_insn_r->reg_rec_count = 1;
3549         }
3550       if (insn_bits10_11 == 0x01 || insn_bits10_11 == 0x03)
3551         record_buf[aarch64_insn_r->reg_rec_count++] = reg_rn;
3552     }
3553   /* Advanced SIMD load/store instructions.  */
3554   else
3555     return aarch64_record_asimd_load_store (aarch64_insn_r);
3556
3557   MEM_ALLOC (aarch64_insn_r->aarch64_mems, aarch64_insn_r->mem_rec_count,
3558              record_buf_mem);
3559   REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3560              record_buf);
3561   return AARCH64_RECORD_SUCCESS;
3562 }
3563
3564 /* Record handler for data processing SIMD and floating point instructions.  */
3565
3566 static unsigned int
3567 aarch64_record_data_proc_simd_fp (insn_decode_record *aarch64_insn_r)
3568 {
3569   uint8_t insn_bit21, opcode, rmode, reg_rd;
3570   uint8_t insn_bits24_27, insn_bits28_31, insn_bits10_11, insn_bits12_15;
3571   uint8_t insn_bits11_14;
3572   uint32_t record_buf[2];
3573
3574   insn_bits24_27 = bits (aarch64_insn_r->aarch64_insn, 24, 27);
3575   insn_bits28_31 = bits (aarch64_insn_r->aarch64_insn, 28, 31);
3576   insn_bits10_11 = bits (aarch64_insn_r->aarch64_insn, 10, 11);
3577   insn_bits12_15 = bits (aarch64_insn_r->aarch64_insn, 12, 15);
3578   insn_bits11_14 = bits (aarch64_insn_r->aarch64_insn, 11, 14);
3579   opcode = bits (aarch64_insn_r->aarch64_insn, 16, 18);
3580   rmode = bits (aarch64_insn_r->aarch64_insn, 19, 20);
3581   reg_rd = bits (aarch64_insn_r->aarch64_insn, 0, 4);
3582   insn_bit21 = bit (aarch64_insn_r->aarch64_insn, 21);
3583
3584   if (record_debug)
3585     debug_printf ("Process record: data processing SIMD/FP: ");
3586
3587   if ((insn_bits28_31 & 0x05) == 0x01 && insn_bits24_27 == 0x0e)
3588     {
3589       /* Floating point - fixed point conversion instructions.  */
3590       if (!insn_bit21)
3591         {
3592           if (record_debug)
3593             debug_printf ("FP - fixed point conversion");
3594
3595           if ((opcode >> 1) == 0x0 && rmode == 0x03)
3596             record_buf[0] = reg_rd;
3597           else
3598             record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3599         }
3600       /* Floating point - conditional compare instructions.  */
3601       else if (insn_bits10_11 == 0x01)
3602         {
3603           if (record_debug)
3604             debug_printf ("FP - conditional compare");
3605
3606           record_buf[0] = AARCH64_CPSR_REGNUM;
3607         }
3608       /* Floating point - data processing (2-source) and
3609          conditional select instructions.  */
3610       else if (insn_bits10_11 == 0x02 || insn_bits10_11 == 0x03)
3611         {
3612           if (record_debug)
3613             debug_printf ("FP - DP (2-source)");
3614
3615           record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3616         }
3617       else if (insn_bits10_11 == 0x00)
3618         {
3619           /* Floating point - immediate instructions.  */
3620           if ((insn_bits12_15 & 0x01) == 0x01
3621               || (insn_bits12_15 & 0x07) == 0x04)
3622             {
3623               if (record_debug)
3624                 debug_printf ("FP - immediate");
3625               record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3626             }
3627           /* Floating point - compare instructions.  */
3628           else if ((insn_bits12_15 & 0x03) == 0x02)
3629             {
3630               if (record_debug)
3631                 debug_printf ("FP - immediate");
3632               record_buf[0] = AARCH64_CPSR_REGNUM;
3633             }
3634           /* Floating point - integer conversions instructions.  */
3635           else if (insn_bits12_15 == 0x00)
3636             {
3637               /* Convert float to integer instruction.  */
3638               if (!(opcode >> 1) || ((opcode >> 1) == 0x02 && !rmode))
3639                 {
3640                   if (record_debug)
3641                     debug_printf ("float to int conversion");
3642
3643                   record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
3644                 }
3645               /* Convert integer to float instruction.  */
3646               else if ((opcode >> 1) == 0x01 && !rmode)
3647                 {
3648                   if (record_debug)
3649                     debug_printf ("int to float conversion");
3650
3651                   record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3652                 }
3653               /* Move float to integer instruction.  */
3654               else if ((opcode >> 1) == 0x03)
3655                 {
3656                   if (record_debug)
3657                     debug_printf ("move float to int");
3658
3659                   if (!(opcode & 0x01))
3660                     record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
3661                   else
3662                     record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3663                 }
3664               else
3665                 return AARCH64_RECORD_UNKNOWN;
3666             }
3667           else
3668             return AARCH64_RECORD_UNKNOWN;
3669         }
3670       else
3671         return AARCH64_RECORD_UNKNOWN;
3672     }
3673   else if ((insn_bits28_31 & 0x09) == 0x00 && insn_bits24_27 == 0x0e)
3674     {
3675       if (record_debug)
3676         debug_printf ("SIMD copy");
3677
3678       /* Advanced SIMD copy instructions.  */
3679       if (!bits (aarch64_insn_r->aarch64_insn, 21, 23)
3680           && !bit (aarch64_insn_r->aarch64_insn, 15)
3681           && bit (aarch64_insn_r->aarch64_insn, 10))
3682         {
3683           if (insn_bits11_14 == 0x05 || insn_bits11_14 == 0x07)
3684             record_buf[0] = reg_rd + AARCH64_X0_REGNUM;
3685           else
3686             record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3687         }
3688       else
3689         record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3690     }
3691   /* All remaining floating point or advanced SIMD instructions.  */
3692   else
3693     {
3694       if (record_debug)
3695         debug_printf ("all remain");
3696
3697       record_buf[0] = reg_rd + AARCH64_V0_REGNUM;
3698     }
3699
3700   if (record_debug)
3701     debug_printf ("\n");
3702
3703   aarch64_insn_r->reg_rec_count++;
3704   gdb_assert (aarch64_insn_r->reg_rec_count == 1);
3705   REG_ALLOC (aarch64_insn_r->aarch64_regs, aarch64_insn_r->reg_rec_count,
3706              record_buf);
3707   return AARCH64_RECORD_SUCCESS;
3708 }
3709
3710 /* Decodes insns type and invokes its record handler.  */
3711
3712 static unsigned int
3713 aarch64_record_decode_insn_handler (insn_decode_record *aarch64_insn_r)
3714 {
3715   uint32_t ins_bit25, ins_bit26, ins_bit27, ins_bit28;
3716
3717   ins_bit25 = bit (aarch64_insn_r->aarch64_insn, 25);
3718   ins_bit26 = bit (aarch64_insn_r->aarch64_insn, 26);
3719   ins_bit27 = bit (aarch64_insn_r->aarch64_insn, 27);
3720   ins_bit28 = bit (aarch64_insn_r->aarch64_insn, 28);
3721
3722   /* Data processing - immediate instructions.  */
3723   if (!ins_bit26 && !ins_bit27 && ins_bit28)
3724     return aarch64_record_data_proc_imm (aarch64_insn_r);
3725
3726   /* Branch, exception generation and system instructions.  */
3727   if (ins_bit26 && !ins_bit27 && ins_bit28)
3728     return aarch64_record_branch_except_sys (aarch64_insn_r);
3729
3730   /* Load and store instructions.  */
3731   if (!ins_bit25 && ins_bit27)
3732     return aarch64_record_load_store (aarch64_insn_r);
3733
3734   /* Data processing - register instructions.  */
3735   if (ins_bit25 && !ins_bit26 && ins_bit27)
3736     return aarch64_record_data_proc_reg (aarch64_insn_r);
3737
3738   /* Data processing - SIMD and floating point instructions.  */
3739   if (ins_bit25 && ins_bit26 && ins_bit27)
3740     return aarch64_record_data_proc_simd_fp (aarch64_insn_r);
3741
3742   return AARCH64_RECORD_UNSUPPORTED;
3743 }
3744
3745 /* Cleans up local record registers and memory allocations.  */
3746
3747 static void
3748 deallocate_reg_mem (insn_decode_record *record)
3749 {
3750   xfree (record->aarch64_regs);
3751   xfree (record->aarch64_mems);
3752 }
3753
3754 /* Parse the current instruction and record the values of the registers and
3755    memory that will be changed in current instruction to record_arch_list
3756    return -1 if something is wrong.  */
3757
3758 int
3759 aarch64_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
3760                         CORE_ADDR insn_addr)
3761 {
3762   uint32_t rec_no = 0;
3763   uint8_t insn_size = 4;
3764   uint32_t ret = 0;
3765   ULONGEST t_bit = 0, insn_id = 0;
3766   gdb_byte buf[insn_size];
3767   insn_decode_record aarch64_record;
3768
3769   memset (&buf[0], 0, insn_size);
3770   memset (&aarch64_record, 0, sizeof (insn_decode_record));
3771   target_read_memory (insn_addr, &buf[0], insn_size);
3772   aarch64_record.aarch64_insn
3773     = (uint32_t) extract_unsigned_integer (&buf[0],
3774                                            insn_size,
3775                                            gdbarch_byte_order (gdbarch));
3776   aarch64_record.regcache = regcache;
3777   aarch64_record.this_addr = insn_addr;
3778   aarch64_record.gdbarch = gdbarch;
3779
3780   ret = aarch64_record_decode_insn_handler (&aarch64_record);
3781   if (ret == AARCH64_RECORD_UNSUPPORTED)
3782     {
3783       printf_unfiltered (_("Process record does not support instruction "
3784                            "0x%0x at address %s.\n"),
3785                          aarch64_record.aarch64_insn,
3786                          paddress (gdbarch, insn_addr));
3787       ret = -1;
3788     }
3789
3790   if (0 == ret)
3791     {
3792       /* Record registers.  */
3793       record_full_arch_list_add_reg (aarch64_record.regcache,
3794                                      AARCH64_PC_REGNUM);
3795       /* Always record register CPSR.  */
3796       record_full_arch_list_add_reg (aarch64_record.regcache,
3797                                      AARCH64_CPSR_REGNUM);
3798       if (aarch64_record.aarch64_regs)
3799         for (rec_no = 0; rec_no < aarch64_record.reg_rec_count; rec_no++)
3800           if (record_full_arch_list_add_reg (aarch64_record.regcache,
3801                                              aarch64_record.aarch64_regs[rec_no]))
3802             ret = -1;
3803
3804       /* Record memories.  */
3805       if (aarch64_record.aarch64_mems)
3806         for (rec_no = 0; rec_no < aarch64_record.mem_rec_count; rec_no++)
3807           if (record_full_arch_list_add_mem
3808               ((CORE_ADDR)aarch64_record.aarch64_mems[rec_no].addr,
3809                aarch64_record.aarch64_mems[rec_no].len))
3810             ret = -1;
3811
3812       if (record_full_arch_list_add_end ())
3813         ret = -1;
3814     }
3815
3816   deallocate_reg_mem (&aarch64_record);
3817   return ret;
3818 }