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