xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/arc-tdep.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Target dependent code for ARC architecture, for GDB.
2699b0f92Schristos 
3*6881a400Schristos    Copyright 2005-2023 Free Software Foundation, Inc.
4699b0f92Schristos    Contributed by Synopsys Inc.
5699b0f92Schristos 
6699b0f92Schristos    This file is part of GDB.
7699b0f92Schristos 
8699b0f92Schristos    This program is free software; you can redistribute it and/or modify
9699b0f92Schristos    it under the terms of the GNU General Public License as published by
10699b0f92Schristos    the Free Software Foundation; either version 3 of the License, or
11699b0f92Schristos    (at your option) any later version.
12699b0f92Schristos 
13699b0f92Schristos    This program is distributed in the hope that it will be useful,
14699b0f92Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
15699b0f92Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16699b0f92Schristos    GNU General Public License for more details.
17699b0f92Schristos 
18699b0f92Schristos    You should have received a copy of the GNU General Public License
19699b0f92Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20699b0f92Schristos 
21699b0f92Schristos /* GDB header files.  */
22699b0f92Schristos #include "defs.h"
23699b0f92Schristos #include "arch-utils.h"
247d62b00eSchristos #include "elf-bfd.h"
25699b0f92Schristos #include "disasm.h"
267d62b00eSchristos #include "dwarf2/frame.h"
27699b0f92Schristos #include "frame-base.h"
28699b0f92Schristos #include "frame-unwind.h"
29699b0f92Schristos #include "gdbcore.h"
30*6881a400Schristos #include "reggroups.h"
31699b0f92Schristos #include "gdbcmd.h"
32699b0f92Schristos #include "objfiles.h"
337d62b00eSchristos #include "osabi.h"
34699b0f92Schristos #include "prologue-value.h"
357d62b00eSchristos #include "target-descriptions.h"
36699b0f92Schristos #include "trad-frame.h"
37699b0f92Schristos 
38699b0f92Schristos /* ARC header files.  */
39699b0f92Schristos #include "opcode/arc.h"
407d62b00eSchristos #include "opcodes/arc-dis.h"
41699b0f92Schristos #include "arc-tdep.h"
427d62b00eSchristos #include "arch/arc.h"
43699b0f92Schristos 
44699b0f92Schristos /* Standard headers.  */
45699b0f92Schristos #include <algorithm>
467d62b00eSchristos #include <sstream>
47699b0f92Schristos 
48699b0f92Schristos /* The frame unwind cache for ARC.  */
49699b0f92Schristos 
50699b0f92Schristos struct arc_frame_cache
51699b0f92Schristos {
52699b0f92Schristos   /* The stack pointer at the time this frame was created; i.e. the caller's
53699b0f92Schristos      stack pointer when this function was called.  It is used to identify this
54699b0f92Schristos      frame.  */
55699b0f92Schristos   CORE_ADDR prev_sp;
56699b0f92Schristos 
57699b0f92Schristos   /* Register that is a base for this frame - FP for normal frame, SP for
58699b0f92Schristos      non-FP frames.  */
59699b0f92Schristos   int frame_base_reg;
60699b0f92Schristos 
61699b0f92Schristos   /* Offset from the previous SP to the current frame base.  If GCC uses
62699b0f92Schristos      `SUB SP,SP,offset` to allocate space for local variables, then it will be
63699b0f92Schristos      done after setting up a frame pointer, but it still will be considered
64699b0f92Schristos      part of prologue, therefore SP will be lesser than FP at the end of the
65699b0f92Schristos      prologue analysis.  In this case that would be an offset from old SP to a
66699b0f92Schristos      new FP.  But in case of non-FP frames, frame base is an SP and thus that
67699b0f92Schristos      would be an offset from old SP to new SP.  What is important is that this
68699b0f92Schristos      is an offset from old SP to a known register, so it can be used to find
69699b0f92Schristos      old SP.
70699b0f92Schristos 
71699b0f92Schristos      Using FP is preferable, when possible, because SP can change in function
72699b0f92Schristos      body after prologue due to alloca, variadic arguments or other shenanigans.
73699b0f92Schristos      If that is the case in the caller frame, then PREV_SP will point to SP at
74699b0f92Schristos      the moment of function call, but it will be different from SP value at the
75699b0f92Schristos      end of the caller prologue.  As a result it will not be possible to
76699b0f92Schristos      reconstruct caller's frame and go past it in the backtrace.  Those things
77699b0f92Schristos      are unlikely to happen to FP - FP value at the moment of function call (as
78699b0f92Schristos      stored on stack in callee prologue) is also an FP value at the end of the
79699b0f92Schristos      caller's prologue.  */
80699b0f92Schristos 
81699b0f92Schristos   LONGEST frame_base_offset;
82699b0f92Schristos 
83699b0f92Schristos   /* Store addresses for registers saved in prologue.  During prologue analysis
84699b0f92Schristos      GDB stores offsets relatively to "old SP", then after old SP is evaluated,
85699b0f92Schristos      offsets are replaced with absolute addresses.  */
86*6881a400Schristos   trad_frame_saved_reg *saved_regs;
87699b0f92Schristos };
88699b0f92Schristos 
89699b0f92Schristos /* Global debug flag.  */
90699b0f92Schristos 
91*6881a400Schristos bool arc_debug;
92699b0f92Schristos 
93699b0f92Schristos /* List of "maintenance print arc" commands.  */
94699b0f92Schristos 
95699b0f92Schristos static struct cmd_list_element *maintenance_print_arc_list = NULL;
96699b0f92Schristos 
977d62b00eSchristos /* A set of registers that we expect to find in a tdesc_feature.  These
987d62b00eSchristos    are used in ARC_TDESC_INIT when processing the target description.  */
99699b0f92Schristos 
1007d62b00eSchristos struct arc_register_feature
1017d62b00eSchristos {
1027d62b00eSchristos   /* Information for a single register.  */
1037d62b00eSchristos   struct register_info
1047d62b00eSchristos   {
1057d62b00eSchristos     /* The GDB register number for this register.  */
1067d62b00eSchristos     int regnum;
107699b0f92Schristos 
1087d62b00eSchristos     /* List of names for this register.  The first name in this list is the
1097d62b00eSchristos        preferred name, the name GDB will use when describing this register.  */
1107d62b00eSchristos     std::vector<const char *> names;
111699b0f92Schristos 
1127d62b00eSchristos     /* When true, this register must be present in this feature set.  */
1137d62b00eSchristos     bool required_p;
114699b0f92Schristos   };
115699b0f92Schristos 
1167d62b00eSchristos   /* The name for this feature.  This is the name used to find this feature
1177d62b00eSchristos      within the target description.  */
1187d62b00eSchristos   const char *name;
1197d62b00eSchristos 
1207d62b00eSchristos   /* List of all the registers that we expect to encounter in this register
1217d62b00eSchristos      set.  */
1227d62b00eSchristos   std::vector<struct register_info> registers;
123699b0f92Schristos };
124699b0f92Schristos 
1257d62b00eSchristos /* Obsolete feature names for backward compatibility.  */
1267d62b00eSchristos static const char *ARC_CORE_V1_OBSOLETE_FEATURE_NAME
1277d62b00eSchristos   = "org.gnu.gdb.arc.core.arcompact";
1287d62b00eSchristos static const char *ARC_CORE_V2_OBSOLETE_FEATURE_NAME
1297d62b00eSchristos   = "org.gnu.gdb.arc.core.v2";
1307d62b00eSchristos static const char *ARC_CORE_V2_REDUCED_OBSOLETE_FEATURE_NAME
1317d62b00eSchristos   = "org.gnu.gdb.arc.core-reduced.v2";
1327d62b00eSchristos static const char *ARC_AUX_OBSOLETE_FEATURE_NAME
1337d62b00eSchristos   = "org.gnu.gdb.arc.aux-minimal";
1347d62b00eSchristos /* Modern feature names.  */
1357d62b00eSchristos static const char *ARC_CORE_FEATURE_NAME = "org.gnu.gdb.arc.core";
1367d62b00eSchristos static const char *ARC_AUX_FEATURE_NAME = "org.gnu.gdb.arc.aux";
1377d62b00eSchristos 
1387d62b00eSchristos /* ARCv1 (ARC600, ARC601, ARC700) general core registers feature set.
1397d62b00eSchristos    See also arc_update_acc_reg_names() for "accl/acch" names.  */
1407d62b00eSchristos 
1417d62b00eSchristos static struct arc_register_feature arc_v1_core_reg_feature =
1427d62b00eSchristos {
1437d62b00eSchristos   ARC_CORE_FEATURE_NAME,
1447d62b00eSchristos   {
1457d62b00eSchristos     { ARC_R0_REGNUM + 0, { "r0" }, true },
1467d62b00eSchristos     { ARC_R0_REGNUM + 1, { "r1" }, true },
1477d62b00eSchristos     { ARC_R0_REGNUM + 2, { "r2" }, true },
1487d62b00eSchristos     { ARC_R0_REGNUM + 3, { "r3" }, true },
1497d62b00eSchristos     { ARC_R0_REGNUM + 4, { "r4" }, false },
1507d62b00eSchristos     { ARC_R0_REGNUM + 5, { "r5" }, false },
1517d62b00eSchristos     { ARC_R0_REGNUM + 6, { "r6" }, false },
1527d62b00eSchristos     { ARC_R0_REGNUM + 7, { "r7" }, false },
1537d62b00eSchristos     { ARC_R0_REGNUM + 8, { "r8" }, false },
1547d62b00eSchristos     { ARC_R0_REGNUM + 9, { "r9" }, false },
1557d62b00eSchristos     { ARC_R0_REGNUM + 10, { "r10" }, true },
1567d62b00eSchristos     { ARC_R0_REGNUM + 11, { "r11" }, true },
1577d62b00eSchristos     { ARC_R0_REGNUM + 12, { "r12" }, true },
1587d62b00eSchristos     { ARC_R0_REGNUM + 13, { "r13" }, true },
1597d62b00eSchristos     { ARC_R0_REGNUM + 14, { "r14" }, true },
1607d62b00eSchristos     { ARC_R0_REGNUM + 15, { "r15" }, true },
1617d62b00eSchristos     { ARC_R0_REGNUM + 16, { "r16" }, false },
1627d62b00eSchristos     { ARC_R0_REGNUM + 17, { "r17" }, false },
1637d62b00eSchristos     { ARC_R0_REGNUM + 18, { "r18" }, false },
1647d62b00eSchristos     { ARC_R0_REGNUM + 19, { "r19" }, false },
1657d62b00eSchristos     { ARC_R0_REGNUM + 20, { "r20" }, false },
1667d62b00eSchristos     { ARC_R0_REGNUM + 21, { "r21" }, false },
1677d62b00eSchristos     { ARC_R0_REGNUM + 22, { "r22" }, false },
1687d62b00eSchristos     { ARC_R0_REGNUM + 23, { "r23" }, false },
1697d62b00eSchristos     { ARC_R0_REGNUM + 24, { "r24" }, false },
1707d62b00eSchristos     { ARC_R0_REGNUM + 25, { "r25" }, false },
1717d62b00eSchristos     { ARC_R0_REGNUM + 26, { "gp" }, true },
1727d62b00eSchristos     { ARC_R0_REGNUM + 27, { "fp" }, true },
1737d62b00eSchristos     { ARC_R0_REGNUM + 28, { "sp" }, true },
1747d62b00eSchristos     { ARC_R0_REGNUM + 29, { "ilink1" }, false },
1757d62b00eSchristos     { ARC_R0_REGNUM + 30, { "ilink2" }, false },
1767d62b00eSchristos     { ARC_R0_REGNUM + 31, { "blink" }, true },
1777d62b00eSchristos     { ARC_R0_REGNUM + 32, { "r32" }, false },
1787d62b00eSchristos     { ARC_R0_REGNUM + 33, { "r33" }, false },
1797d62b00eSchristos     { ARC_R0_REGNUM + 34, { "r34" }, false },
1807d62b00eSchristos     { ARC_R0_REGNUM + 35, { "r35" }, false },
1817d62b00eSchristos     { ARC_R0_REGNUM + 36, { "r36" }, false },
1827d62b00eSchristos     { ARC_R0_REGNUM + 37, { "r37" }, false },
1837d62b00eSchristos     { ARC_R0_REGNUM + 38, { "r38" }, false },
1847d62b00eSchristos     { ARC_R0_REGNUM + 39, { "r39" }, false },
1857d62b00eSchristos     { ARC_R0_REGNUM + 40, { "r40" }, false },
1867d62b00eSchristos     { ARC_R0_REGNUM + 41, { "r41" }, false },
1877d62b00eSchristos     { ARC_R0_REGNUM + 42, { "r42" }, false },
1887d62b00eSchristos     { ARC_R0_REGNUM + 43, { "r43" }, false },
1897d62b00eSchristos     { ARC_R0_REGNUM + 44, { "r44" }, false },
1907d62b00eSchristos     { ARC_R0_REGNUM + 45, { "r45" }, false },
1917d62b00eSchristos     { ARC_R0_REGNUM + 46, { "r46" }, false },
1927d62b00eSchristos     { ARC_R0_REGNUM + 47, { "r47" }, false },
1937d62b00eSchristos     { ARC_R0_REGNUM + 48, { "r48" }, false },
1947d62b00eSchristos     { ARC_R0_REGNUM + 49, { "r49" }, false },
1957d62b00eSchristos     { ARC_R0_REGNUM + 50, { "r50" }, false },
1967d62b00eSchristos     { ARC_R0_REGNUM + 51, { "r51" }, false },
1977d62b00eSchristos     { ARC_R0_REGNUM + 52, { "r52" }, false },
1987d62b00eSchristos     { ARC_R0_REGNUM + 53, { "r53" }, false },
1997d62b00eSchristos     { ARC_R0_REGNUM + 54, { "r54" }, false },
2007d62b00eSchristos     { ARC_R0_REGNUM + 55, { "r55" }, false },
2017d62b00eSchristos     { ARC_R0_REGNUM + 56, { "r56" }, false },
2027d62b00eSchristos     { ARC_R0_REGNUM + 57, { "r57" }, false },
2037d62b00eSchristos     { ARC_R0_REGNUM + 58, { "r58", "accl" }, false },
2047d62b00eSchristos     { ARC_R0_REGNUM + 59, { "r59", "acch" }, false },
2057d62b00eSchristos     { ARC_R0_REGNUM + 60, { "lp_count" }, false },
2067d62b00eSchristos     { ARC_R0_REGNUM + 61, { "reserved" }, false },
2077d62b00eSchristos     { ARC_R0_REGNUM + 62, { "limm" }, false },
2087d62b00eSchristos     { ARC_R0_REGNUM + 63, { "pcl" }, true }
2097d62b00eSchristos   }
2107d62b00eSchristos };
2117d62b00eSchristos 
2127d62b00eSchristos /* ARCv2 (ARCHS) general core registers feature set.  See also
2137d62b00eSchristos    arc_update_acc_reg_names() for "accl/acch" names.  */
2147d62b00eSchristos 
2157d62b00eSchristos static struct arc_register_feature arc_v2_core_reg_feature =
2167d62b00eSchristos {
2177d62b00eSchristos   ARC_CORE_FEATURE_NAME,
2187d62b00eSchristos   {
2197d62b00eSchristos     { ARC_R0_REGNUM + 0, { "r0" }, true },
2207d62b00eSchristos     { ARC_R0_REGNUM + 1, { "r1" }, true },
2217d62b00eSchristos     { ARC_R0_REGNUM + 2, { "r2" }, true },
2227d62b00eSchristos     { ARC_R0_REGNUM + 3, { "r3" }, true },
2237d62b00eSchristos     { ARC_R0_REGNUM + 4, { "r4" }, false },
2247d62b00eSchristos     { ARC_R0_REGNUM + 5, { "r5" }, false },
2257d62b00eSchristos     { ARC_R0_REGNUM + 6, { "r6" }, false },
2267d62b00eSchristos     { ARC_R0_REGNUM + 7, { "r7" }, false },
2277d62b00eSchristos     { ARC_R0_REGNUM + 8, { "r8" }, false },
2287d62b00eSchristos     { ARC_R0_REGNUM + 9, { "r9" }, false },
2297d62b00eSchristos     { ARC_R0_REGNUM + 10, { "r10" }, true },
2307d62b00eSchristos     { ARC_R0_REGNUM + 11, { "r11" }, true },
2317d62b00eSchristos     { ARC_R0_REGNUM + 12, { "r12" }, true },
2327d62b00eSchristos     { ARC_R0_REGNUM + 13, { "r13" }, true },
2337d62b00eSchristos     { ARC_R0_REGNUM + 14, { "r14" }, true },
2347d62b00eSchristos     { ARC_R0_REGNUM + 15, { "r15" }, true },
2357d62b00eSchristos     { ARC_R0_REGNUM + 16, { "r16" }, false },
2367d62b00eSchristos     { ARC_R0_REGNUM + 17, { "r17" }, false },
2377d62b00eSchristos     { ARC_R0_REGNUM + 18, { "r18" }, false },
2387d62b00eSchristos     { ARC_R0_REGNUM + 19, { "r19" }, false },
2397d62b00eSchristos     { ARC_R0_REGNUM + 20, { "r20" }, false },
2407d62b00eSchristos     { ARC_R0_REGNUM + 21, { "r21" }, false },
2417d62b00eSchristos     { ARC_R0_REGNUM + 22, { "r22" }, false },
2427d62b00eSchristos     { ARC_R0_REGNUM + 23, { "r23" }, false },
2437d62b00eSchristos     { ARC_R0_REGNUM + 24, { "r24" }, false },
2447d62b00eSchristos     { ARC_R0_REGNUM + 25, { "r25" }, false },
2457d62b00eSchristos     { ARC_R0_REGNUM + 26, { "gp" }, true },
2467d62b00eSchristos     { ARC_R0_REGNUM + 27, { "fp" }, true },
2477d62b00eSchristos     { ARC_R0_REGNUM + 28, { "sp" }, true },
2487d62b00eSchristos     { ARC_R0_REGNUM + 29, { "ilink" }, false },
2497d62b00eSchristos     { ARC_R0_REGNUM + 30, { "r30" }, true },
2507d62b00eSchristos     { ARC_R0_REGNUM + 31, { "blink" }, true },
2517d62b00eSchristos     { ARC_R0_REGNUM + 32, { "r32" }, false },
2527d62b00eSchristos     { ARC_R0_REGNUM + 33, { "r33" }, false },
2537d62b00eSchristos     { ARC_R0_REGNUM + 34, { "r34" }, false },
2547d62b00eSchristos     { ARC_R0_REGNUM + 35, { "r35" }, false },
2557d62b00eSchristos     { ARC_R0_REGNUM + 36, { "r36" }, false },
2567d62b00eSchristos     { ARC_R0_REGNUM + 37, { "r37" }, false },
2577d62b00eSchristos     { ARC_R0_REGNUM + 38, { "r38" }, false },
2587d62b00eSchristos     { ARC_R0_REGNUM + 39, { "r39" }, false },
2597d62b00eSchristos     { ARC_R0_REGNUM + 40, { "r40" }, false },
2607d62b00eSchristos     { ARC_R0_REGNUM + 41, { "r41" }, false },
2617d62b00eSchristos     { ARC_R0_REGNUM + 42, { "r42" }, false },
2627d62b00eSchristos     { ARC_R0_REGNUM + 43, { "r43" }, false },
2637d62b00eSchristos     { ARC_R0_REGNUM + 44, { "r44" }, false },
2647d62b00eSchristos     { ARC_R0_REGNUM + 45, { "r45" }, false },
2657d62b00eSchristos     { ARC_R0_REGNUM + 46, { "r46" }, false },
2667d62b00eSchristos     { ARC_R0_REGNUM + 47, { "r47" }, false },
2677d62b00eSchristos     { ARC_R0_REGNUM + 48, { "r48" }, false },
2687d62b00eSchristos     { ARC_R0_REGNUM + 49, { "r49" }, false },
2697d62b00eSchristos     { ARC_R0_REGNUM + 50, { "r50" }, false },
2707d62b00eSchristos     { ARC_R0_REGNUM + 51, { "r51" }, false },
2717d62b00eSchristos     { ARC_R0_REGNUM + 52, { "r52" }, false },
2727d62b00eSchristos     { ARC_R0_REGNUM + 53, { "r53" }, false },
2737d62b00eSchristos     { ARC_R0_REGNUM + 54, { "r54" }, false },
2747d62b00eSchristos     { ARC_R0_REGNUM + 55, { "r55" }, false },
2757d62b00eSchristos     { ARC_R0_REGNUM + 56, { "r56" }, false },
2767d62b00eSchristos     { ARC_R0_REGNUM + 57, { "r57" }, false },
2777d62b00eSchristos     { ARC_R0_REGNUM + 58, { "r58", "accl" }, false },
2787d62b00eSchristos     { ARC_R0_REGNUM + 59, { "r59", "acch" }, false },
2797d62b00eSchristos     { ARC_R0_REGNUM + 60, { "lp_count" }, false },
2807d62b00eSchristos     { ARC_R0_REGNUM + 61, { "reserved" }, false },
2817d62b00eSchristos     { ARC_R0_REGNUM + 62, { "limm" }, false },
2827d62b00eSchristos     { ARC_R0_REGNUM + 63, { "pcl" }, true }
2837d62b00eSchristos   }
2847d62b00eSchristos };
2857d62b00eSchristos 
2867d62b00eSchristos /* The common auxiliary registers feature set.  The REGNUM field
2877d62b00eSchristos    must match the ARC_REGNUM enum in arc-tdep.h.  */
2887d62b00eSchristos 
2897d62b00eSchristos static const struct arc_register_feature arc_common_aux_reg_feature =
2907d62b00eSchristos {
2917d62b00eSchristos   ARC_AUX_FEATURE_NAME,
2927d62b00eSchristos   {
2937d62b00eSchristos     { ARC_FIRST_AUX_REGNUM + 0, { "pc" }, true },
2947d62b00eSchristos     { ARC_FIRST_AUX_REGNUM + 1, { "status32" }, true },
2957d62b00eSchristos     { ARC_FIRST_AUX_REGNUM + 2, { "lp_start" }, false },
2967d62b00eSchristos     { ARC_FIRST_AUX_REGNUM + 3, { "lp_end" }, false },
2977d62b00eSchristos     { ARC_FIRST_AUX_REGNUM + 4, { "bta" }, false }
2987d62b00eSchristos   }
299699b0f92Schristos };
300699b0f92Schristos 
3017f2ac410Schristos static char *arc_disassembler_options = NULL;
3027f2ac410Schristos 
303699b0f92Schristos /* Functions are sorted in the order as they are used in the
304699b0f92Schristos    _initialize_arc_tdep (), which uses the same order as gdbarch.h.  Static
305699b0f92Schristos    functions are defined before the first invocation.  */
306699b0f92Schristos 
307699b0f92Schristos /* Returns an unsigned value of OPERAND_NUM in instruction INSN.
308699b0f92Schristos    For relative branch instructions returned value is an offset, not an actual
309699b0f92Schristos    branch target.  */
310699b0f92Schristos 
311699b0f92Schristos static ULONGEST
312699b0f92Schristos arc_insn_get_operand_value (const struct arc_instruction &insn,
313699b0f92Schristos 			    unsigned int operand_num)
314699b0f92Schristos {
315699b0f92Schristos   switch (insn.operands[operand_num].kind)
316699b0f92Schristos     {
317699b0f92Schristos     case ARC_OPERAND_KIND_LIMM:
318699b0f92Schristos       gdb_assert (insn.limm_p);
319699b0f92Schristos       return insn.limm_value;
320699b0f92Schristos     case ARC_OPERAND_KIND_SHIMM:
321699b0f92Schristos       return insn.operands[operand_num].value;
322699b0f92Schristos     default:
323699b0f92Schristos       /* Value in instruction is a register number.  */
324699b0f92Schristos       struct regcache *regcache = get_current_regcache ();
325699b0f92Schristos       ULONGEST value;
326699b0f92Schristos       regcache_cooked_read_unsigned (regcache,
327699b0f92Schristos 				     insn.operands[operand_num].value,
328699b0f92Schristos 				     &value);
329699b0f92Schristos       return value;
330699b0f92Schristos     }
331699b0f92Schristos }
332699b0f92Schristos 
333699b0f92Schristos /* Like arc_insn_get_operand_value, but returns a signed value.  */
334699b0f92Schristos 
335699b0f92Schristos static LONGEST
336699b0f92Schristos arc_insn_get_operand_value_signed (const struct arc_instruction &insn,
337699b0f92Schristos 				   unsigned int operand_num)
338699b0f92Schristos {
339699b0f92Schristos   switch (insn.operands[operand_num].kind)
340699b0f92Schristos     {
341699b0f92Schristos     case ARC_OPERAND_KIND_LIMM:
342699b0f92Schristos       gdb_assert (insn.limm_p);
343699b0f92Schristos       /* Convert unsigned raw value to signed one.  This assumes 2's
344699b0f92Schristos 	 complement arithmetic, but so is the LONG_MIN value from generic
345699b0f92Schristos 	 defs.h and that assumption is true for ARC.  */
346699b0f92Schristos       gdb_static_assert (sizeof (insn.limm_value) == sizeof (int));
347699b0f92Schristos       return (((LONGEST) insn.limm_value) ^ INT_MIN) - INT_MIN;
348699b0f92Schristos     case ARC_OPERAND_KIND_SHIMM:
349699b0f92Schristos       /* Sign conversion has been done by binutils.  */
350699b0f92Schristos       return insn.operands[operand_num].value;
351699b0f92Schristos     default:
352699b0f92Schristos       /* Value in instruction is a register number.  */
353699b0f92Schristos       struct regcache *regcache = get_current_regcache ();
354699b0f92Schristos       LONGEST value;
355699b0f92Schristos       regcache_cooked_read_signed (regcache,
356699b0f92Schristos 				   insn.operands[operand_num].value,
357699b0f92Schristos 				   &value);
358699b0f92Schristos       return value;
359699b0f92Schristos     }
360699b0f92Schristos }
361699b0f92Schristos 
362699b0f92Schristos /* Get register with base address of memory operation.  */
363699b0f92Schristos 
3647d62b00eSchristos static int
365699b0f92Schristos arc_insn_get_memory_base_reg (const struct arc_instruction &insn)
366699b0f92Schristos {
367699b0f92Schristos   /* POP_S and PUSH_S have SP as an implicit argument in a disassembler.  */
368699b0f92Schristos   if (insn.insn_class == PUSH || insn.insn_class == POP)
369699b0f92Schristos     return ARC_SP_REGNUM;
370699b0f92Schristos 
371699b0f92Schristos   gdb_assert (insn.insn_class == LOAD || insn.insn_class == STORE);
372699b0f92Schristos 
373699b0f92Schristos   /* Other instructions all have at least two operands: operand 0 is data,
374699b0f92Schristos      operand 1 is address.  Operand 2 is offset from address.  However, see
375699b0f92Schristos      comment to arc_instruction.operands - in some cases, third operand may be
376699b0f92Schristos      missing, namely if it is 0.  */
377699b0f92Schristos   gdb_assert (insn.operands_count >= 2);
378699b0f92Schristos   return insn.operands[1].value;
379699b0f92Schristos }
380699b0f92Schristos 
381699b0f92Schristos /* Get offset of a memory operation INSN.  */
382699b0f92Schristos 
3837d62b00eSchristos static CORE_ADDR
384699b0f92Schristos arc_insn_get_memory_offset (const struct arc_instruction &insn)
385699b0f92Schristos {
386699b0f92Schristos   /* POP_S and PUSH_S have offset as an implicit argument in a
387699b0f92Schristos      disassembler.  */
388699b0f92Schristos   if (insn.insn_class == POP)
389699b0f92Schristos     return 4;
390699b0f92Schristos   else if (insn.insn_class == PUSH)
391699b0f92Schristos     return -4;
392699b0f92Schristos 
393699b0f92Schristos   gdb_assert (insn.insn_class == LOAD || insn.insn_class == STORE);
394699b0f92Schristos 
395699b0f92Schristos   /* Other instructions all have at least two operands: operand 0 is data,
396699b0f92Schristos      operand 1 is address.  Operand 2 is offset from address.  However, see
397699b0f92Schristos      comment to arc_instruction.operands - in some cases, third operand may be
398699b0f92Schristos      missing, namely if it is 0.  */
399699b0f92Schristos   if (insn.operands_count < 3)
400699b0f92Schristos     return 0;
401699b0f92Schristos 
402699b0f92Schristos   CORE_ADDR value = arc_insn_get_operand_value (insn, 2);
403699b0f92Schristos   /* Handle scaling.  */
404699b0f92Schristos   if (insn.writeback_mode == ARC_WRITEBACK_AS)
405699b0f92Schristos     {
406699b0f92Schristos       /* Byte data size is not valid for AS.  Halfword means shift by 1 bit.
407699b0f92Schristos 	 Word and double word means shift by 2 bits.  */
408699b0f92Schristos       gdb_assert (insn.data_size_mode != ARC_SCALING_B);
409699b0f92Schristos       if (insn.data_size_mode == ARC_SCALING_H)
410699b0f92Schristos 	value <<= 1;
411699b0f92Schristos       else
412699b0f92Schristos 	value <<= 2;
413699b0f92Schristos     }
414699b0f92Schristos   return value;
415699b0f92Schristos }
416699b0f92Schristos 
417699b0f92Schristos CORE_ADDR
418699b0f92Schristos arc_insn_get_branch_target (const struct arc_instruction &insn)
419699b0f92Schristos {
420699b0f92Schristos   gdb_assert (insn.is_control_flow);
421699b0f92Schristos 
422699b0f92Schristos   /* BI [c]: PC = nextPC + (c << 2).  */
423699b0f92Schristos   if (insn.insn_class == BI)
424699b0f92Schristos     {
425699b0f92Schristos       ULONGEST reg_value = arc_insn_get_operand_value (insn, 0);
426699b0f92Schristos       return arc_insn_get_linear_next_pc (insn) + (reg_value << 2);
427699b0f92Schristos     }
428699b0f92Schristos   /* BIH [c]: PC = nextPC + (c << 1).  */
429699b0f92Schristos   else if (insn.insn_class == BIH)
430699b0f92Schristos     {
431699b0f92Schristos       ULONGEST reg_value = arc_insn_get_operand_value (insn, 0);
432699b0f92Schristos       return arc_insn_get_linear_next_pc (insn) + (reg_value << 1);
433699b0f92Schristos     }
434699b0f92Schristos   /* JLI and EI.  */
435699b0f92Schristos   /* JLI and EI depend on optional AUX registers.  Not supported right now.  */
436699b0f92Schristos   else if (insn.insn_class == JLI)
437699b0f92Schristos     {
438*6881a400Schristos       gdb_printf (gdb_stderr,
439699b0f92Schristos 		  "JLI_S instruction is not supported by the GDB.");
440699b0f92Schristos       return 0;
441699b0f92Schristos     }
442699b0f92Schristos   else if (insn.insn_class == EI)
443699b0f92Schristos     {
444*6881a400Schristos       gdb_printf (gdb_stderr,
445699b0f92Schristos 		  "EI_S instruction is not supported by the GDB.");
446699b0f92Schristos       return 0;
447699b0f92Schristos     }
448699b0f92Schristos   /* LEAVE_S: PC = BLINK.  */
449699b0f92Schristos   else if (insn.insn_class == LEAVE)
450699b0f92Schristos     {
451699b0f92Schristos       struct regcache *regcache = get_current_regcache ();
452699b0f92Schristos       ULONGEST value;
453699b0f92Schristos       regcache_cooked_read_unsigned (regcache, ARC_BLINK_REGNUM, &value);
454699b0f92Schristos       return value;
455699b0f92Schristos     }
456699b0f92Schristos   /* BBIT0/1, BRcc: PC = currentPC + operand.  */
457699b0f92Schristos   else if (insn.insn_class == BBIT0 || insn.insn_class == BBIT1
458699b0f92Schristos 	   || insn.insn_class == BRCC)
459699b0f92Schristos     {
460699b0f92Schristos       /* Most instructions has branch target as their sole argument.  However
461699b0f92Schristos 	 conditional brcc/bbit has it as a third operand.  */
462699b0f92Schristos       CORE_ADDR pcrel_addr = arc_insn_get_operand_value (insn, 2);
463699b0f92Schristos 
464699b0f92Schristos       /* Offset is relative to the 4-byte aligned address of the current
465699b0f92Schristos 	 instruction, hence last two bits should be truncated.  */
466699b0f92Schristos       return pcrel_addr + align_down (insn.address, 4);
467699b0f92Schristos     }
468699b0f92Schristos   /* B, Bcc, BL, BLcc, LP, LPcc: PC = currentPC + operand.  */
469699b0f92Schristos   else if (insn.insn_class == BRANCH || insn.insn_class == LOOP)
470699b0f92Schristos     {
471699b0f92Schristos       CORE_ADDR pcrel_addr = arc_insn_get_operand_value (insn, 0);
472699b0f92Schristos 
473699b0f92Schristos       /* Offset is relative to the 4-byte aligned address of the current
474699b0f92Schristos 	 instruction, hence last two bits should be truncated.  */
475699b0f92Schristos       return pcrel_addr + align_down (insn.address, 4);
476699b0f92Schristos     }
477699b0f92Schristos   /* J, Jcc, JL, JLcc: PC = operand.  */
478699b0f92Schristos   else if (insn.insn_class == JUMP)
479699b0f92Schristos     {
480699b0f92Schristos       /* All jumps are single-operand.  */
481699b0f92Schristos       return arc_insn_get_operand_value (insn, 0);
482699b0f92Schristos     }
483699b0f92Schristos 
484699b0f92Schristos   /* This is some new and unknown instruction.  */
485699b0f92Schristos   gdb_assert_not_reached ("Unknown branch instruction.");
486699b0f92Schristos }
487699b0f92Schristos 
488699b0f92Schristos /* Dump INSN into gdb_stdlog.  */
489699b0f92Schristos 
4907d62b00eSchristos static void
491699b0f92Schristos arc_insn_dump (const struct arc_instruction &insn)
492699b0f92Schristos {
493699b0f92Schristos   struct gdbarch *gdbarch = target_gdbarch ();
494699b0f92Schristos 
495699b0f92Schristos   arc_print ("Dumping arc_instruction at %s\n",
496699b0f92Schristos 	     paddress (gdbarch, insn.address));
497699b0f92Schristos   arc_print ("\tlength = %u\n", insn.length);
498699b0f92Schristos 
499699b0f92Schristos   if (!insn.valid)
500699b0f92Schristos     {
501699b0f92Schristos       arc_print ("\tThis is not a valid ARC instruction.\n");
502699b0f92Schristos       return;
503699b0f92Schristos     }
504699b0f92Schristos 
505699b0f92Schristos   arc_print ("\tlength_with_limm = %u\n", insn.length + (insn.limm_p ? 4 : 0));
506699b0f92Schristos   arc_print ("\tcc = 0x%x\n", insn.condition_code);
507699b0f92Schristos   arc_print ("\tinsn_class = %u\n", insn.insn_class);
508699b0f92Schristos   arc_print ("\tis_control_flow = %i\n", insn.is_control_flow);
509699b0f92Schristos   arc_print ("\thas_delay_slot = %i\n", insn.has_delay_slot);
510699b0f92Schristos 
511699b0f92Schristos   CORE_ADDR next_pc = arc_insn_get_linear_next_pc (insn);
512699b0f92Schristos   arc_print ("\tlinear_next_pc = %s\n", paddress (gdbarch, next_pc));
513699b0f92Schristos 
514699b0f92Schristos   if (insn.is_control_flow)
515699b0f92Schristos     {
516699b0f92Schristos       CORE_ADDR t = arc_insn_get_branch_target (insn);
517699b0f92Schristos       arc_print ("\tbranch_target = %s\n", paddress (gdbarch, t));
518699b0f92Schristos     }
519699b0f92Schristos 
520699b0f92Schristos   arc_print ("\tlimm_p = %i\n", insn.limm_p);
521699b0f92Schristos   if (insn.limm_p)
522699b0f92Schristos     arc_print ("\tlimm_value = 0x%08x\n", insn.limm_value);
523699b0f92Schristos 
524699b0f92Schristos   if (insn.insn_class == STORE || insn.insn_class == LOAD
525699b0f92Schristos       || insn.insn_class == PUSH || insn.insn_class == POP)
526699b0f92Schristos     {
527699b0f92Schristos       arc_print ("\twriteback_mode = %u\n", insn.writeback_mode);
528699b0f92Schristos       arc_print ("\tdata_size_mode = %u\n", insn.data_size_mode);
529699b0f92Schristos       arc_print ("\tmemory_base_register = %s\n",
530699b0f92Schristos 		 gdbarch_register_name (gdbarch,
531699b0f92Schristos 					arc_insn_get_memory_base_reg (insn)));
532699b0f92Schristos       /* get_memory_offset returns an unsigned CORE_ADDR, but treat it as a
533699b0f92Schristos 	 LONGEST for a nicer representation.  */
534699b0f92Schristos       arc_print ("\taddr_offset = %s\n",
535699b0f92Schristos 		 plongest (arc_insn_get_memory_offset (insn)));
536699b0f92Schristos     }
537699b0f92Schristos 
538699b0f92Schristos   arc_print ("\toperands_count = %u\n", insn.operands_count);
539699b0f92Schristos   for (unsigned int i = 0; i < insn.operands_count; ++i)
540699b0f92Schristos     {
541699b0f92Schristos       int is_reg = (insn.operands[i].kind == ARC_OPERAND_KIND_REG);
542699b0f92Schristos 
543699b0f92Schristos       arc_print ("\toperand[%u] = {\n", i);
544699b0f92Schristos       arc_print ("\t\tis_reg = %i\n", is_reg);
545699b0f92Schristos       if (is_reg)
546699b0f92Schristos 	arc_print ("\t\tregister = %s\n",
547699b0f92Schristos 		   gdbarch_register_name (gdbarch, insn.operands[i].value));
548699b0f92Schristos       /* Don't know if this value is signed or not, so print both
549699b0f92Schristos 	 representations.  This tends to look quite ugly, especially for big
550699b0f92Schristos 	 numbers.  */
551699b0f92Schristos       arc_print ("\t\tunsigned value = %s\n",
552699b0f92Schristos 		 pulongest (arc_insn_get_operand_value (insn, i)));
553699b0f92Schristos       arc_print ("\t\tsigned value = %s\n",
554699b0f92Schristos 		 plongest (arc_insn_get_operand_value_signed (insn, i)));
555699b0f92Schristos       arc_print ("\t}\n");
556699b0f92Schristos     }
557699b0f92Schristos }
558699b0f92Schristos 
559699b0f92Schristos CORE_ADDR
560699b0f92Schristos arc_insn_get_linear_next_pc (const struct arc_instruction &insn)
561699b0f92Schristos {
562699b0f92Schristos   /* In ARC long immediate is always 4 bytes.  */
563699b0f92Schristos   return (insn.address + insn.length + (insn.limm_p ? 4 : 0));
564699b0f92Schristos }
565699b0f92Schristos 
566699b0f92Schristos /* Implement the "write_pc" gdbarch method.
567699b0f92Schristos 
568699b0f92Schristos    In ARC PC register is a normal register so in most cases setting PC value
569699b0f92Schristos    is a straightforward process: debugger just writes PC value.  However it
570699b0f92Schristos    gets trickier in case when current instruction is an instruction in delay
571699b0f92Schristos    slot.  In this case CPU will execute instruction at current PC value, then
572699b0f92Schristos    will set PC to the current value of BTA register; also current instruction
573699b0f92Schristos    cannot be branch/jump and some of the other instruction types.  Thus if
574699b0f92Schristos    debugger would try to just change PC value in this case, this instruction
575699b0f92Schristos    will get executed, but then core will "jump" to the original branch target.
576699b0f92Schristos 
577699b0f92Schristos    Whether current instruction is a delay-slot instruction or not is indicated
578699b0f92Schristos    by DE bit in STATUS32 register indicates if current instruction is a delay
579699b0f92Schristos    slot instruction.  This bit is writable by debug host, which allows debug
580699b0f92Schristos    host to prevent core from jumping after the delay slot instruction.  It
581699b0f92Schristos    also works in another direction: setting this bit will make core to treat
582699b0f92Schristos    any current instructions as a delay slot instruction and to set PC to the
583699b0f92Schristos    current value of BTA register.
584699b0f92Schristos 
585699b0f92Schristos    To workaround issues with changing PC register while in delay slot
586699b0f92Schristos    instruction, debugger should check for the STATUS32.DE bit and reset it if
587699b0f92Schristos    it is set.  No other change is required in this function.  Most common
588699b0f92Schristos    case, where this function might be required is calling inferior functions
589699b0f92Schristos    from debugger.  Generic GDB logic handles this pretty well: current values
590699b0f92Schristos    of registers are stored, value of PC is changed (that is the job of this
591699b0f92Schristos    function), and after inferior function is executed, GDB restores all
592699b0f92Schristos    registers, include BTA and STATUS32, which also means that core is returned
593699b0f92Schristos    to its original state of being halted on delay slot instructions.
594699b0f92Schristos 
595699b0f92Schristos    This method is useless for ARC 600, because it doesn't have externally
596699b0f92Schristos    exposed BTA register.  In the case of ARC 600 it is impossible to restore
597699b0f92Schristos    core to its state in all occasions thus core should never be halted (from
598699b0f92Schristos    the perspective of debugger host) in the delay slot.  */
599699b0f92Schristos 
600699b0f92Schristos static void
601699b0f92Schristos arc_write_pc (struct regcache *regcache, CORE_ADDR new_pc)
602699b0f92Schristos {
6037f2ac410Schristos   struct gdbarch *gdbarch = regcache->arch ();
604699b0f92Schristos 
605*6881a400Schristos   arc_debug_printf ("Writing PC, new value=%s",
606699b0f92Schristos 		    paddress (gdbarch, new_pc));
607699b0f92Schristos 
608699b0f92Schristos   regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch),
609699b0f92Schristos 				  new_pc);
610699b0f92Schristos 
611699b0f92Schristos   ULONGEST status32;
612699b0f92Schristos   regcache_cooked_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
613699b0f92Schristos 				 &status32);
614699b0f92Schristos 
6157d62b00eSchristos   if ((status32 & ARC_STATUS32_DE_MASK) != 0)
616699b0f92Schristos     {
617*6881a400Schristos       arc_debug_printf ("Changing PC while in delay slot.  Will "
618699b0f92Schristos 			"reset STATUS32.DE bit to zero.  Value of STATUS32 "
619*6881a400Schristos 			"register is 0x%s",
620699b0f92Schristos 			phex (status32, ARC_REGISTER_SIZE));
621699b0f92Schristos 
622699b0f92Schristos       /* Reset bit and write to the cache.  */
623699b0f92Schristos       status32 &= ~0x40;
624699b0f92Schristos       regcache_cooked_write_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
625699b0f92Schristos 				      status32);
626699b0f92Schristos     }
627699b0f92Schristos }
628699b0f92Schristos 
629699b0f92Schristos /* Implement the "virtual_frame_pointer" gdbarch method.
630699b0f92Schristos 
631699b0f92Schristos    According to ABI the FP (r27) is used to point to the middle of the current
632699b0f92Schristos    stack frame, just below the saved FP and before local variables, register
633699b0f92Schristos    spill area and outgoing args.  However for optimization levels above O2 and
634699b0f92Schristos    in any case in leaf functions, the frame pointer is usually not set at all.
635699b0f92Schristos    The exception being when handling nested functions.
636699b0f92Schristos 
637699b0f92Schristos    We use this function to return a "virtual" frame pointer, marking the start
638699b0f92Schristos    of the current stack frame as a register-offset pair.  If the FP is not
639699b0f92Schristos    being used, then it should return SP, with an offset of the frame size.
640699b0f92Schristos 
641699b0f92Schristos    The current implementation doesn't actually know the frame size, nor
642699b0f92Schristos    whether the FP is actually being used, so for now we just return SP and an
643699b0f92Schristos    offset of zero.  This is no worse than other architectures, but is needed
644699b0f92Schristos    to avoid assertion failures.
645699b0f92Schristos 
646699b0f92Schristos    TODO: Can we determine the frame size to get a correct offset?
647699b0f92Schristos 
648699b0f92Schristos    PC is a program counter where we need the virtual FP.  REG_PTR is the base
649699b0f92Schristos    register used for the virtual FP.  OFFSET_PTR is the offset used for the
650699b0f92Schristos    virtual FP.  */
651699b0f92Schristos 
652699b0f92Schristos static void
653699b0f92Schristos arc_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
654699b0f92Schristos 			   int *reg_ptr, LONGEST *offset_ptr)
655699b0f92Schristos {
656699b0f92Schristos   *reg_ptr = gdbarch_sp_regnum (gdbarch);
657699b0f92Schristos   *offset_ptr = 0;
658699b0f92Schristos }
659699b0f92Schristos 
660699b0f92Schristos /* Implement the "push_dummy_call" gdbarch method.
661699b0f92Schristos 
662699b0f92Schristos    Stack Frame Layout
663699b0f92Schristos 
664699b0f92Schristos    This shows the layout of the stack frame for the general case of a
665699b0f92Schristos    function call; a given function might not have a variable number of
666699b0f92Schristos    arguments or local variables, or might not save any registers, so it would
667699b0f92Schristos    not have the corresponding frame areas.  Additionally, a leaf function
668699b0f92Schristos    (i.e. one which calls no other functions) does not need to save the
669699b0f92Schristos    contents of the BLINK register (which holds its return address), and a
670699b0f92Schristos    function might not have a frame pointer.
671699b0f92Schristos 
672699b0f92Schristos    The stack grows downward, so SP points below FP in memory; SP always
673699b0f92Schristos    points to the last used word on the stack, not the first one.
674699b0f92Schristos 
675699b0f92Schristos 		      |                       |   |
676699b0f92Schristos 		      |      arg word N       |   | caller's
677699b0f92Schristos 		      |           :           |   | frame
678699b0f92Schristos 		      |      arg word 10      |   |
679699b0f92Schristos 		      |      arg word 9       |   |
680699b0f92Schristos 	  old SP ---> +-----------------------+ --+
681699b0f92Schristos 		      |                       |   |
682699b0f92Schristos 		      |      callee-saved     |   |
683699b0f92Schristos 		      |       registers       |   |
684699b0f92Schristos 		      |  including fp, blink  |   |
685699b0f92Schristos 		      |                       |   | callee's
686699b0f92Schristos 	  new FP ---> +-----------------------+   | frame
687699b0f92Schristos 		      |                       |   |
688699b0f92Schristos 		      |         local         |   |
689699b0f92Schristos 		      |       variables       |   |
690699b0f92Schristos 		      |                       |   |
691699b0f92Schristos 		      |       register        |   |
692699b0f92Schristos 		      |      spill area       |   |
693699b0f92Schristos 		      |                       |   |
694699b0f92Schristos 		      |     outgoing args     |   |
695699b0f92Schristos 		      |                       |   |
696699b0f92Schristos 	  new SP ---> +-----------------------+ --+
697699b0f92Schristos 		      |                       |
698699b0f92Schristos 		      |         unused        |
699699b0f92Schristos 		      |                       |
700699b0f92Schristos 				  |
701699b0f92Schristos 				  |
702699b0f92Schristos 				  V
703699b0f92Schristos 			      downwards
704699b0f92Schristos 
705699b0f92Schristos    The list of arguments to be passed to a function is considered to be a
706699b0f92Schristos    sequence of _N_ words (as though all the parameters were stored in order in
707699b0f92Schristos    memory with each parameter occupying an integral number of words).  Words
708699b0f92Schristos    1..8 are passed in registers 0..7; if the function has more than 8 words of
709699b0f92Schristos    arguments then words 9..@em N are passed on the stack in the caller's frame.
710699b0f92Schristos 
711699b0f92Schristos    If the function has a variable number of arguments, e.g. it has a form such
712699b0f92Schristos    as `function (p1, p2, ...);' and _P_ words are required to hold the values
713699b0f92Schristos    of the named parameters (which are passed in registers 0..@em P -1), then
714699b0f92Schristos    the remaining 8 - _P_ words passed in registers _P_..7 are spilled into the
715699b0f92Schristos    top of the frame so that the anonymous parameter words occupy a continuous
716699b0f92Schristos    region.
717699b0f92Schristos 
718699b0f92Schristos    Any arguments are already in target byte order.  We just need to store
719699b0f92Schristos    them!
720699b0f92Schristos 
721699b0f92Schristos    BP_ADDR is the return address where breakpoint must be placed.  NARGS is
722699b0f92Schristos    the number of arguments to the function.  ARGS is the arguments values (in
723699b0f92Schristos    target byte order).  SP is the Current value of SP register.  STRUCT_RETURN
724699b0f92Schristos    is TRUE if structures are returned by the function.  STRUCT_ADDR is the
725699b0f92Schristos    hidden address for returning a struct.  Returns SP of a new frame.  */
726699b0f92Schristos 
727699b0f92Schristos static CORE_ADDR
728699b0f92Schristos arc_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
729699b0f92Schristos 		     struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
7307f2ac410Schristos 		     struct value **args, CORE_ADDR sp,
7317f2ac410Schristos 		     function_call_return_method return_method,
732699b0f92Schristos 		     CORE_ADDR struct_addr)
733699b0f92Schristos {
734*6881a400Schristos   arc_debug_printf ("nargs = %d", nargs);
735699b0f92Schristos 
736699b0f92Schristos   int arg_reg = ARC_FIRST_ARG_REGNUM;
737699b0f92Schristos 
738699b0f92Schristos   /* Push the return address.  */
739699b0f92Schristos   regcache_cooked_write_unsigned (regcache, ARC_BLINK_REGNUM, bp_addr);
740699b0f92Schristos 
741699b0f92Schristos   /* Are we returning a value using a structure return instead of a normal
742699b0f92Schristos      value return?  If so, struct_addr is the address of the reserved space for
743699b0f92Schristos      the return structure to be written on the stack, and that address is
744699b0f92Schristos      passed to that function as a hidden first argument.  */
7457f2ac410Schristos   if (return_method == return_method_struct)
746699b0f92Schristos     {
747699b0f92Schristos       /* Pass the return address in the first argument register.  */
748699b0f92Schristos       regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
749699b0f92Schristos 
750*6881a400Schristos       arc_debug_printf ("struct return address %s passed in R%d",
751699b0f92Schristos 			print_core_address (gdbarch, struct_addr), arg_reg);
752699b0f92Schristos 
753699b0f92Schristos       arg_reg++;
754699b0f92Schristos     }
755699b0f92Schristos 
756699b0f92Schristos   if (nargs > 0)
757699b0f92Schristos     {
758699b0f92Schristos       unsigned int total_space = 0;
759699b0f92Schristos 
760699b0f92Schristos       /* How much space do the arguments occupy in total?  Must round each
761699b0f92Schristos 	 argument's size up to an integral number of words.  */
762699b0f92Schristos       for (int i = 0; i < nargs; i++)
763699b0f92Schristos 	{
764*6881a400Schristos 	  unsigned int len = value_type (args[i])->length ();
765699b0f92Schristos 	  unsigned int space = align_up (len, 4);
766699b0f92Schristos 
767699b0f92Schristos 	  total_space += space;
768699b0f92Schristos 
769*6881a400Schristos 	  arc_debug_printf ("arg %d: %u bytes -> %u", i, len, space);
770699b0f92Schristos 	}
771699b0f92Schristos 
772699b0f92Schristos       /* Allocate a buffer to hold a memory image of the arguments.  */
773699b0f92Schristos       gdb_byte *memory_image = XCNEWVEC (gdb_byte, total_space);
774699b0f92Schristos 
775699b0f92Schristos       /* Now copy all of the arguments into the buffer, correctly aligned.  */
776699b0f92Schristos       gdb_byte *data = memory_image;
777699b0f92Schristos       for (int i = 0; i < nargs; i++)
778699b0f92Schristos 	{
779*6881a400Schristos 	  unsigned int len = value_type (args[i])->length ();
780699b0f92Schristos 	  unsigned int space = align_up (len, 4);
781699b0f92Schristos 
782*6881a400Schristos 	  memcpy (data, value_contents (args[i]).data (), (size_t) len);
783*6881a400Schristos 	  arc_debug_printf ("copying arg %d, val 0x%08x, len %d to mem",
784*6881a400Schristos 			    i, *((int *) value_contents (args[i]).data ()),
785*6881a400Schristos 			    len);
786699b0f92Schristos 
787699b0f92Schristos 	  data += space;
788699b0f92Schristos 	}
789699b0f92Schristos 
790699b0f92Schristos       /* Now load as much as possible of the memory image into registers.  */
791699b0f92Schristos       data = memory_image;
792699b0f92Schristos       while (arg_reg <= ARC_LAST_ARG_REGNUM)
793699b0f92Schristos 	{
794*6881a400Schristos 	  arc_debug_printf ("passing 0x%02x%02x%02x%02x in register R%d",
795699b0f92Schristos 			    data[0], data[1], data[2], data[3], arg_reg);
796699b0f92Schristos 
797699b0f92Schristos 	  /* Note we don't use write_unsigned here, since that would convert
798699b0f92Schristos 	     the byte order, but we are already in the correct byte order.  */
7997f2ac410Schristos 	  regcache->cooked_write (arg_reg, data);
800699b0f92Schristos 
801699b0f92Schristos 	  data += ARC_REGISTER_SIZE;
802699b0f92Schristos 	  total_space -= ARC_REGISTER_SIZE;
803699b0f92Schristos 
804699b0f92Schristos 	  /* All the data is now in registers.  */
805699b0f92Schristos 	  if (total_space == 0)
806699b0f92Schristos 	    break;
807699b0f92Schristos 
808699b0f92Schristos 	  arg_reg++;
809699b0f92Schristos 	}
810699b0f92Schristos 
811699b0f92Schristos       /* If there is any data left, push it onto the stack (in a single write
812699b0f92Schristos 	 operation).  */
813699b0f92Schristos       if (total_space > 0)
814699b0f92Schristos 	{
815*6881a400Schristos 	  arc_debug_printf ("passing %d bytes on stack\n", total_space);
816699b0f92Schristos 
817699b0f92Schristos 	  sp -= total_space;
818699b0f92Schristos 	  write_memory (sp, data, (int) total_space);
819699b0f92Schristos 	}
820699b0f92Schristos 
821699b0f92Schristos       xfree (memory_image);
822699b0f92Schristos     }
823699b0f92Schristos 
824699b0f92Schristos   /* Finally, update the SP register.  */
825699b0f92Schristos   regcache_cooked_write_unsigned (regcache, gdbarch_sp_regnum (gdbarch), sp);
826699b0f92Schristos 
827699b0f92Schristos   return sp;
828699b0f92Schristos }
829699b0f92Schristos 
830699b0f92Schristos /* Implement the "push_dummy_code" gdbarch method.
831699b0f92Schristos 
832699b0f92Schristos    We don't actually push any code.  We just identify where a breakpoint can
833699b0f92Schristos    be inserted to which we are can return and the resume address where we
834699b0f92Schristos    should be called.
835699b0f92Schristos 
836699b0f92Schristos    ARC does not necessarily have an executable stack, so we can't put the
837699b0f92Schristos    return breakpoint there.  Instead we put it at the entry point of the
838699b0f92Schristos    function.  This means the SP is unchanged.
839699b0f92Schristos 
840699b0f92Schristos    SP is a current stack pointer FUNADDR is an address of the function to be
841699b0f92Schristos    called.  ARGS is arguments to pass.  NARGS is a number of args to pass.
842699b0f92Schristos    VALUE_TYPE is a type of value returned.  REAL_PC is a resume address when
843699b0f92Schristos    the function is called.  BP_ADDR is an address where breakpoint should be
844699b0f92Schristos    set.  Returns the updated stack pointer.  */
845699b0f92Schristos 
846699b0f92Schristos static CORE_ADDR
847699b0f92Schristos arc_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, CORE_ADDR funaddr,
848699b0f92Schristos 		     struct value **args, int nargs, struct type *value_type,
849699b0f92Schristos 		     CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
850699b0f92Schristos 		     struct regcache *regcache)
851699b0f92Schristos {
852699b0f92Schristos   *real_pc = funaddr;
853699b0f92Schristos   *bp_addr = entry_point_address ();
854699b0f92Schristos   return sp;
855699b0f92Schristos }
856699b0f92Schristos 
857699b0f92Schristos /* Implement the "cannot_fetch_register" gdbarch method.  */
858699b0f92Schristos 
859699b0f92Schristos static int
860699b0f92Schristos arc_cannot_fetch_register (struct gdbarch *gdbarch, int regnum)
861699b0f92Schristos {
862699b0f92Schristos   /* Assume that register is readable if it is unknown.  LIMM and RESERVED are
863699b0f92Schristos      not real registers, but specific register numbers.  They are available as
864699b0f92Schristos      regnums to align architectural register numbers with GDB internal regnums,
865699b0f92Schristos      but they shouldn't appear in target descriptions generated by
866699b0f92Schristos      GDB-servers.  */
867699b0f92Schristos   switch (regnum)
868699b0f92Schristos     {
869699b0f92Schristos     case ARC_RESERVED_REGNUM:
870699b0f92Schristos     case ARC_LIMM_REGNUM:
871699b0f92Schristos       return true;
872699b0f92Schristos     default:
873699b0f92Schristos       return false;
874699b0f92Schristos     }
875699b0f92Schristos }
876699b0f92Schristos 
877699b0f92Schristos /* Implement the "cannot_store_register" gdbarch method.  */
878699b0f92Schristos 
879699b0f92Schristos static int
880699b0f92Schristos arc_cannot_store_register (struct gdbarch *gdbarch, int regnum)
881699b0f92Schristos {
882699b0f92Schristos   /* Assume that register is writable if it is unknown.  See comment in
883699b0f92Schristos      arc_cannot_fetch_register about LIMM and RESERVED.  */
884699b0f92Schristos   switch (regnum)
885699b0f92Schristos     {
886699b0f92Schristos     case ARC_RESERVED_REGNUM:
887699b0f92Schristos     case ARC_LIMM_REGNUM:
888699b0f92Schristos     case ARC_PCL_REGNUM:
889699b0f92Schristos       return true;
890699b0f92Schristos     default:
891699b0f92Schristos       return false;
892699b0f92Schristos     }
893699b0f92Schristos }
894699b0f92Schristos 
895699b0f92Schristos /* Get the return value of a function from the registers/memory used to
896699b0f92Schristos    return it, according to the convention used by the ABI - 4-bytes values are
897699b0f92Schristos    in the R0, while 8-byte values are in the R0-R1.
898699b0f92Schristos 
899699b0f92Schristos    TODO: This implementation ignores the case of "complex double", where
900699b0f92Schristos    according to ABI, value is returned in the R0-R3 registers.
901699b0f92Schristos 
902699b0f92Schristos    TYPE is a returned value's type.  VALBUF is a buffer for the returned
903699b0f92Schristos    value.  */
904699b0f92Schristos 
905699b0f92Schristos static void
906699b0f92Schristos arc_extract_return_value (struct gdbarch *gdbarch, struct type *type,
907699b0f92Schristos 			  struct regcache *regcache, gdb_byte *valbuf)
908699b0f92Schristos {
909*6881a400Schristos   unsigned int len = type->length ();
910699b0f92Schristos 
911*6881a400Schristos   arc_debug_printf ("called");
912699b0f92Schristos 
913699b0f92Schristos   if (len <= ARC_REGISTER_SIZE)
914699b0f92Schristos     {
915699b0f92Schristos       ULONGEST val;
916699b0f92Schristos 
917699b0f92Schristos       /* Get the return value from one register.  */
918699b0f92Schristos       regcache_cooked_read_unsigned (regcache, ARC_R0_REGNUM, &val);
919699b0f92Schristos       store_unsigned_integer (valbuf, (int) len,
920699b0f92Schristos 			      gdbarch_byte_order (gdbarch), val);
921699b0f92Schristos 
922*6881a400Schristos       arc_debug_printf ("returning 0x%s", phex (val, ARC_REGISTER_SIZE));
923699b0f92Schristos     }
924699b0f92Schristos   else if (len <= ARC_REGISTER_SIZE * 2)
925699b0f92Schristos     {
926699b0f92Schristos       ULONGEST low, high;
927699b0f92Schristos 
928699b0f92Schristos       /* Get the return value from two registers.  */
929699b0f92Schristos       regcache_cooked_read_unsigned (regcache, ARC_R0_REGNUM, &low);
930699b0f92Schristos       regcache_cooked_read_unsigned (regcache, ARC_R1_REGNUM, &high);
931699b0f92Schristos 
932699b0f92Schristos       store_unsigned_integer (valbuf, ARC_REGISTER_SIZE,
933699b0f92Schristos 			      gdbarch_byte_order (gdbarch), low);
934699b0f92Schristos       store_unsigned_integer (valbuf + ARC_REGISTER_SIZE,
935699b0f92Schristos 			      (int) len - ARC_REGISTER_SIZE,
936699b0f92Schristos 			      gdbarch_byte_order (gdbarch), high);
937699b0f92Schristos 
938*6881a400Schristos       arc_debug_printf ("returning 0x%s%s",
939699b0f92Schristos 			phex (high, ARC_REGISTER_SIZE),
940699b0f92Schristos 			phex (low, ARC_REGISTER_SIZE));
941699b0f92Schristos     }
942699b0f92Schristos   else
943699b0f92Schristos     error (_("arc: extract_return_value: type length %u too large"), len);
944699b0f92Schristos }
945699b0f92Schristos 
946699b0f92Schristos 
947699b0f92Schristos /* Store the return value of a function into the registers/memory used to
948699b0f92Schristos    return it, according to the convention used by the ABI.
949699b0f92Schristos 
950699b0f92Schristos    TODO: This implementation ignores the case of "complex double", where
951699b0f92Schristos    according to ABI, value is returned in the R0-R3 registers.
952699b0f92Schristos 
953699b0f92Schristos    TYPE is a returned value's type.  VALBUF is a buffer with the value to
954699b0f92Schristos    return.  */
955699b0f92Schristos 
956699b0f92Schristos static void
957699b0f92Schristos arc_store_return_value (struct gdbarch *gdbarch, struct type *type,
958699b0f92Schristos 			struct regcache *regcache, const gdb_byte *valbuf)
959699b0f92Schristos {
960*6881a400Schristos   unsigned int len = type->length ();
961699b0f92Schristos 
962*6881a400Schristos   arc_debug_printf ("called");
963699b0f92Schristos 
964699b0f92Schristos   if (len <= ARC_REGISTER_SIZE)
965699b0f92Schristos     {
966699b0f92Schristos       ULONGEST val;
967699b0f92Schristos 
968699b0f92Schristos       /* Put the return value into one register.  */
969699b0f92Schristos       val = extract_unsigned_integer (valbuf, (int) len,
970699b0f92Schristos 				      gdbarch_byte_order (gdbarch));
971699b0f92Schristos       regcache_cooked_write_unsigned (regcache, ARC_R0_REGNUM, val);
972699b0f92Schristos 
973*6881a400Schristos       arc_debug_printf ("storing 0x%s", phex (val, ARC_REGISTER_SIZE));
974699b0f92Schristos     }
975699b0f92Schristos   else if (len <= ARC_REGISTER_SIZE * 2)
976699b0f92Schristos     {
977699b0f92Schristos       ULONGEST low, high;
978699b0f92Schristos 
979699b0f92Schristos       /* Put the return value into  two registers.  */
980699b0f92Schristos       low = extract_unsigned_integer (valbuf, ARC_REGISTER_SIZE,
981699b0f92Schristos 				      gdbarch_byte_order (gdbarch));
982699b0f92Schristos       high = extract_unsigned_integer (valbuf + ARC_REGISTER_SIZE,
983699b0f92Schristos 				       (int) len - ARC_REGISTER_SIZE,
984699b0f92Schristos 				       gdbarch_byte_order (gdbarch));
985699b0f92Schristos 
986699b0f92Schristos       regcache_cooked_write_unsigned (regcache, ARC_R0_REGNUM, low);
987699b0f92Schristos       regcache_cooked_write_unsigned (regcache, ARC_R1_REGNUM, high);
988699b0f92Schristos 
989*6881a400Schristos       arc_debug_printf ("storing 0x%s%s",
990699b0f92Schristos 			phex (high, ARC_REGISTER_SIZE),
991699b0f92Schristos 			phex (low, ARC_REGISTER_SIZE));
992699b0f92Schristos     }
993699b0f92Schristos   else
994699b0f92Schristos     error (_("arc_store_return_value: type length too large."));
995699b0f92Schristos }
996699b0f92Schristos 
997699b0f92Schristos /* Implement the "get_longjmp_target" gdbarch method.  */
998699b0f92Schristos 
999699b0f92Schristos static int
1000*6881a400Schristos arc_get_longjmp_target (frame_info_ptr frame, CORE_ADDR *pc)
1001699b0f92Schristos {
1002*6881a400Schristos   arc_debug_printf ("called");
1003699b0f92Schristos 
1004699b0f92Schristos   struct gdbarch *gdbarch = get_frame_arch (frame);
1005*6881a400Schristos   arc_gdbarch_tdep *tdep = gdbarch_tdep<arc_gdbarch_tdep> (gdbarch);
1006699b0f92Schristos   int pc_offset = tdep->jb_pc * ARC_REGISTER_SIZE;
1007699b0f92Schristos   gdb_byte buf[ARC_REGISTER_SIZE];
1008699b0f92Schristos   CORE_ADDR jb_addr = get_frame_register_unsigned (frame, ARC_FIRST_ARG_REGNUM);
1009699b0f92Schristos 
1010699b0f92Schristos   if (target_read_memory (jb_addr + pc_offset, buf, ARC_REGISTER_SIZE))
1011699b0f92Schristos     return 0; /* Failed to read from memory.  */
1012699b0f92Schristos 
1013699b0f92Schristos   *pc = extract_unsigned_integer (buf, ARC_REGISTER_SIZE,
1014699b0f92Schristos 				  gdbarch_byte_order (gdbarch));
1015699b0f92Schristos   return 1;
1016699b0f92Schristos }
1017699b0f92Schristos 
1018699b0f92Schristos /* Implement the "return_value" gdbarch method.  */
1019699b0f92Schristos 
1020699b0f92Schristos static enum return_value_convention
1021699b0f92Schristos arc_return_value (struct gdbarch *gdbarch, struct value *function,
1022699b0f92Schristos 		  struct type *valtype, struct regcache *regcache,
1023699b0f92Schristos 		  gdb_byte *readbuf, const gdb_byte *writebuf)
1024699b0f92Schristos {
1025699b0f92Schristos   /* If the return type is a struct, or a union, or would occupy more than two
1026699b0f92Schristos      registers, the ABI uses the "struct return convention": the calling
1027699b0f92Schristos      function passes a hidden first parameter to the callee (in R0).  That
1028699b0f92Schristos      parameter is the address at which the value being returned should be
1029699b0f92Schristos      stored.  Otherwise, the result is returned in registers.  */
10307d62b00eSchristos   int is_struct_return = (valtype->code () == TYPE_CODE_STRUCT
10317d62b00eSchristos 			  || valtype->code () == TYPE_CODE_UNION
1032*6881a400Schristos 			  || valtype->length () > 2 * ARC_REGISTER_SIZE);
1033699b0f92Schristos 
1034*6881a400Schristos   arc_debug_printf ("readbuf = %s, writebuf = %s",
1035699b0f92Schristos 		    host_address_to_string (readbuf),
1036699b0f92Schristos 		    host_address_to_string (writebuf));
1037699b0f92Schristos 
1038699b0f92Schristos   if (writebuf != NULL)
1039699b0f92Schristos     {
1040699b0f92Schristos       /* Case 1.  GDB should not ask us to set a struct return value: it
1041699b0f92Schristos 	 should know the struct return location and write the value there
1042699b0f92Schristos 	 itself.  */
1043699b0f92Schristos       gdb_assert (!is_struct_return);
1044699b0f92Schristos       arc_store_return_value (gdbarch, valtype, regcache, writebuf);
1045699b0f92Schristos     }
1046699b0f92Schristos   else if (readbuf != NULL)
1047699b0f92Schristos     {
1048699b0f92Schristos       /* Case 2.  GDB should not ask us to get a struct return value: it
1049699b0f92Schristos 	 should know the struct return location and read the value from there
1050699b0f92Schristos 	 itself.  */
1051699b0f92Schristos       gdb_assert (!is_struct_return);
1052699b0f92Schristos       arc_extract_return_value (gdbarch, valtype, regcache, readbuf);
1053699b0f92Schristos     }
1054699b0f92Schristos 
1055699b0f92Schristos   return (is_struct_return
1056699b0f92Schristos 	  ? RETURN_VALUE_STRUCT_CONVENTION
1057699b0f92Schristos 	  : RETURN_VALUE_REGISTER_CONVENTION);
1058699b0f92Schristos }
1059699b0f92Schristos 
1060699b0f92Schristos /* Return the base address of the frame.  For ARC, the base address is the
1061699b0f92Schristos    frame pointer.  */
1062699b0f92Schristos 
1063699b0f92Schristos static CORE_ADDR
1064*6881a400Schristos arc_frame_base_address (frame_info_ptr this_frame, void **prologue_cache)
1065699b0f92Schristos {
1066699b0f92Schristos   return (CORE_ADDR) get_frame_register_unsigned (this_frame, ARC_FP_REGNUM);
1067699b0f92Schristos }
1068699b0f92Schristos 
1069699b0f92Schristos /* Helper function that returns valid pv_t for an instruction operand:
1070699b0f92Schristos    either a register or a constant.  */
1071699b0f92Schristos 
1072699b0f92Schristos static pv_t
1073699b0f92Schristos arc_pv_get_operand (pv_t *regs, const struct arc_instruction &insn, int operand)
1074699b0f92Schristos {
1075699b0f92Schristos   if (insn.operands[operand].kind == ARC_OPERAND_KIND_REG)
1076699b0f92Schristos     return regs[insn.operands[operand].value];
1077699b0f92Schristos   else
1078699b0f92Schristos     return pv_constant (arc_insn_get_operand_value (insn, operand));
1079699b0f92Schristos }
1080699b0f92Schristos 
1081699b0f92Schristos /* Determine whether the given disassembled instruction may be part of a
1082699b0f92Schristos    function prologue.  If it is, the information in the frame unwind cache will
1083699b0f92Schristos    be updated.  */
1084699b0f92Schristos 
1085699b0f92Schristos static bool
1086699b0f92Schristos arc_is_in_prologue (struct gdbarch *gdbarch, const struct arc_instruction &insn,
1087699b0f92Schristos 		    pv_t *regs, struct pv_area *stack)
1088699b0f92Schristos {
1089699b0f92Schristos   /* It might be that currently analyzed address doesn't contain an
1090699b0f92Schristos      instruction, hence INSN is not valid.  It likely means that address points
1091699b0f92Schristos      to a data, non-initialized memory, or middle of a 32-bit instruction.  In
1092699b0f92Schristos      practice this may happen if GDB connects to a remote target that has
1093699b0f92Schristos      non-zeroed memory.  GDB would read PC value and would try to analyze
1094699b0f92Schristos      prologue, but there is no guarantee that memory contents at the address
1095699b0f92Schristos      specified in PC is address is a valid instruction.  There is not much that
1096699b0f92Schristos      that can be done about that.  */
1097699b0f92Schristos   if (!insn.valid)
1098699b0f92Schristos     return false;
1099699b0f92Schristos 
1100699b0f92Schristos   /* Branch/jump or a predicated instruction.  */
1101699b0f92Schristos   if (insn.is_control_flow || insn.condition_code != ARC_CC_AL)
1102699b0f92Schristos     return false;
1103699b0f92Schristos 
1104699b0f92Schristos   /* Store of some register.  May or may not update base address register.  */
1105699b0f92Schristos   if (insn.insn_class == STORE || insn.insn_class == PUSH)
1106699b0f92Schristos     {
11077d62b00eSchristos       /* There is definitely at least one operand - register/value being
1108699b0f92Schristos 	 stored.  */
1109699b0f92Schristos       gdb_assert (insn.operands_count > 0);
1110699b0f92Schristos 
1111699b0f92Schristos       /* Store at some constant address.  */
1112699b0f92Schristos       if (insn.operands_count > 1
1113699b0f92Schristos 	  && insn.operands[1].kind != ARC_OPERAND_KIND_REG)
1114699b0f92Schristos 	return false;
1115699b0f92Schristos 
1116699b0f92Schristos       /* Writeback modes:
1117699b0f92Schristos 	 Mode	Address used		    Writeback value
1118699b0f92Schristos 	 --------------------------------------------------
1119699b0f92Schristos 	 No	reg + offset		    no
1120699b0f92Schristos 	 A/AW	reg + offset		    reg + offset
1121699b0f92Schristos 	 AB	reg			    reg + offset
1122699b0f92Schristos 	 AS	reg + (offset << scaling)   no
1123699b0f92Schristos 
1124699b0f92Schristos 	 "PUSH reg" is an alias to "ST.AW reg, [SP, -4]" encoding.  However
1125699b0f92Schristos 	 16-bit PUSH_S is a distinct instruction encoding, where offset and
1126699b0f92Schristos 	 base register are implied through opcode.  */
1127699b0f92Schristos 
1128699b0f92Schristos       /* Register with base memory address.  */
1129699b0f92Schristos       int base_reg = arc_insn_get_memory_base_reg (insn);
1130699b0f92Schristos 
1131699b0f92Schristos       /* Address where to write.  arc_insn_get_memory_offset returns scaled
1132699b0f92Schristos 	 value for ARC_WRITEBACK_AS.  */
1133699b0f92Schristos       pv_t addr;
1134699b0f92Schristos       if (insn.writeback_mode == ARC_WRITEBACK_AB)
1135699b0f92Schristos 	addr = regs[base_reg];
1136699b0f92Schristos       else
1137699b0f92Schristos 	addr = pv_add_constant (regs[base_reg],
1138699b0f92Schristos 				arc_insn_get_memory_offset (insn));
1139699b0f92Schristos 
11407f2ac410Schristos       if (stack->store_would_trash (addr))
1141699b0f92Schristos 	return false;
1142699b0f92Schristos 
1143699b0f92Schristos       if (insn.data_size_mode != ARC_SCALING_D)
1144699b0f92Schristos 	{
1145699b0f92Schristos 	  /* Find the value being stored.  */
1146699b0f92Schristos 	  pv_t store_value = arc_pv_get_operand (regs, insn, 0);
1147699b0f92Schristos 
1148699b0f92Schristos 	  /* What is the size of a the stored value?  */
1149699b0f92Schristos 	  CORE_ADDR size;
1150699b0f92Schristos 	  if (insn.data_size_mode == ARC_SCALING_B)
1151699b0f92Schristos 	    size = 1;
1152699b0f92Schristos 	  else if (insn.data_size_mode == ARC_SCALING_H)
1153699b0f92Schristos 	    size = 2;
1154699b0f92Schristos 	  else
1155699b0f92Schristos 	    size = ARC_REGISTER_SIZE;
1156699b0f92Schristos 
11577f2ac410Schristos 	  stack->store (addr, size, store_value);
1158699b0f92Schristos 	}
1159699b0f92Schristos       else
1160699b0f92Schristos 	{
1161699b0f92Schristos 	  if (insn.operands[0].kind == ARC_OPERAND_KIND_REG)
1162699b0f92Schristos 	    {
1163699b0f92Schristos 	      /* If this is a double store, than write N+1 register as well.  */
1164699b0f92Schristos 	      pv_t store_value1 = regs[insn.operands[0].value];
1165699b0f92Schristos 	      pv_t store_value2 = regs[insn.operands[0].value + 1];
11667f2ac410Schristos 	      stack->store (addr, ARC_REGISTER_SIZE, store_value1);
11677f2ac410Schristos 	      stack->store (pv_add_constant (addr, ARC_REGISTER_SIZE),
1168699b0f92Schristos 			    ARC_REGISTER_SIZE, store_value2);
1169699b0f92Schristos 	    }
1170699b0f92Schristos 	  else
1171699b0f92Schristos 	    {
1172699b0f92Schristos 	      pv_t store_value
1173699b0f92Schristos 		= pv_constant (arc_insn_get_operand_value (insn, 0));
11747f2ac410Schristos 	      stack->store (addr, ARC_REGISTER_SIZE * 2, store_value);
1175699b0f92Schristos 	    }
1176699b0f92Schristos 	}
1177699b0f92Schristos 
1178699b0f92Schristos       /* Is base register updated?  */
1179699b0f92Schristos       if (insn.writeback_mode == ARC_WRITEBACK_A
1180699b0f92Schristos 	  || insn.writeback_mode == ARC_WRITEBACK_AB)
1181699b0f92Schristos 	regs[base_reg] = pv_add_constant (regs[base_reg],
1182699b0f92Schristos 					  arc_insn_get_memory_offset (insn));
1183699b0f92Schristos 
1184699b0f92Schristos       return true;
1185699b0f92Schristos     }
1186699b0f92Schristos   else if (insn.insn_class == MOVE)
1187699b0f92Schristos     {
1188699b0f92Schristos       gdb_assert (insn.operands_count == 2);
1189699b0f92Schristos 
1190699b0f92Schristos       /* Destination argument can be "0", so nothing will happen.  */
1191699b0f92Schristos       if (insn.operands[0].kind == ARC_OPERAND_KIND_REG)
1192699b0f92Schristos 	{
1193699b0f92Schristos 	  int dst_regnum = insn.operands[0].value;
1194699b0f92Schristos 	  regs[dst_regnum] = arc_pv_get_operand (regs, insn, 1);
1195699b0f92Schristos 	}
1196699b0f92Schristos       return true;
1197699b0f92Schristos     }
1198699b0f92Schristos   else if (insn.insn_class == SUB)
1199699b0f92Schristos     {
1200699b0f92Schristos       gdb_assert (insn.operands_count == 3);
1201699b0f92Schristos 
1202699b0f92Schristos       /* SUB 0,b,c.  */
1203699b0f92Schristos       if (insn.operands[0].kind != ARC_OPERAND_KIND_REG)
1204699b0f92Schristos 	return true;
1205699b0f92Schristos 
1206699b0f92Schristos       int dst_regnum = insn.operands[0].value;
1207699b0f92Schristos       regs[dst_regnum] = pv_subtract (arc_pv_get_operand (regs, insn, 1),
1208699b0f92Schristos 				      arc_pv_get_operand (regs, insn, 2));
1209699b0f92Schristos       return true;
1210699b0f92Schristos     }
1211699b0f92Schristos   else if (insn.insn_class == ENTER)
1212699b0f92Schristos     {
1213699b0f92Schristos       /* ENTER_S is a prologue-in-instruction - it saves all callee-saved
1214699b0f92Schristos 	 registers according to given arguments thus greatly reducing code
1215699b0f92Schristos 	 size.  Which registers will be actually saved depends on arguments.
1216699b0f92Schristos 
1217699b0f92Schristos 	 ENTER_S {R13-...,FP,BLINK} stores registers in following order:
1218699b0f92Schristos 
1219699b0f92Schristos 	 new SP ->
1220699b0f92Schristos 		   BLINK
1221699b0f92Schristos 		   R13
1222699b0f92Schristos 		   R14
1223699b0f92Schristos 		   R15
1224699b0f92Schristos 		   ...
1225699b0f92Schristos 		   FP
1226699b0f92Schristos 	 old SP ->
1227699b0f92Schristos 
1228699b0f92Schristos 	 There are up to three arguments for this opcode, as presented by ARC
1229699b0f92Schristos 	 disassembler:
1230699b0f92Schristos 	 1) amount of general-purpose registers to be saved - this argument is
1231699b0f92Schristos 	    always present even when it is 0;
1232699b0f92Schristos 	 2) FP register number (27) if FP has to be stored, otherwise argument
1233699b0f92Schristos 	    is not present;
1234699b0f92Schristos 	 3) BLINK register number (31) if BLINK has to be stored, otherwise
1235699b0f92Schristos 	    argument is not present.  If both FP and BLINK are stored, then FP
1236699b0f92Schristos 	    is present before BLINK in argument list.  */
1237699b0f92Schristos       gdb_assert (insn.operands_count > 0);
1238699b0f92Schristos 
1239699b0f92Schristos       int regs_saved = arc_insn_get_operand_value (insn, 0);
1240699b0f92Schristos 
1241699b0f92Schristos       bool is_fp_saved;
1242699b0f92Schristos       if (insn.operands_count > 1)
1243699b0f92Schristos 	is_fp_saved = (insn.operands[1].value  == ARC_FP_REGNUM);
1244699b0f92Schristos       else
1245699b0f92Schristos 	is_fp_saved = false;
1246699b0f92Schristos 
1247699b0f92Schristos       bool is_blink_saved;
1248699b0f92Schristos       if (insn.operands_count > 1)
1249699b0f92Schristos 	is_blink_saved = (insn.operands[insn.operands_count - 1].value
1250699b0f92Schristos 			  == ARC_BLINK_REGNUM);
1251699b0f92Schristos       else
1252699b0f92Schristos 	is_blink_saved = false;
1253699b0f92Schristos 
1254699b0f92Schristos       /* Amount of bytes to be allocated to store specified registers.  */
1255699b0f92Schristos       CORE_ADDR st_size = ((regs_saved + is_fp_saved + is_blink_saved)
1256699b0f92Schristos 			   * ARC_REGISTER_SIZE);
1257699b0f92Schristos       pv_t new_sp = pv_add_constant (regs[ARC_SP_REGNUM], -st_size);
1258699b0f92Schristos 
1259699b0f92Schristos       /* Assume that if the last register (closest to new SP) can be written,
1260699b0f92Schristos 	 then it is possible to write all of them.  */
12617f2ac410Schristos       if (stack->store_would_trash (new_sp))
1262699b0f92Schristos 	return false;
1263699b0f92Schristos 
1264699b0f92Schristos       /* Current store address.  */
1265699b0f92Schristos       pv_t addr = regs[ARC_SP_REGNUM];
1266699b0f92Schristos 
1267699b0f92Schristos       if (is_fp_saved)
1268699b0f92Schristos 	{
1269699b0f92Schristos 	  addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
12707f2ac410Schristos 	  stack->store (addr, ARC_REGISTER_SIZE, regs[ARC_FP_REGNUM]);
1271699b0f92Schristos 	}
1272699b0f92Schristos 
1273699b0f92Schristos       /* Registers are stored in backward order: from GP (R26) to R13.  */
1274699b0f92Schristos       for (int i = ARC_R13_REGNUM + regs_saved - 1; i >= ARC_R13_REGNUM; i--)
1275699b0f92Schristos 	{
1276699b0f92Schristos 	  addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
12777f2ac410Schristos 	  stack->store (addr, ARC_REGISTER_SIZE, regs[i]);
1278699b0f92Schristos 	}
1279699b0f92Schristos 
1280699b0f92Schristos       if (is_blink_saved)
1281699b0f92Schristos 	{
1282699b0f92Schristos 	  addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
12837f2ac410Schristos 	  stack->store (addr, ARC_REGISTER_SIZE,
1284699b0f92Schristos 			regs[ARC_BLINK_REGNUM]);
1285699b0f92Schristos 	}
1286699b0f92Schristos 
1287699b0f92Schristos       gdb_assert (pv_is_identical (addr, new_sp));
1288699b0f92Schristos 
1289699b0f92Schristos       regs[ARC_SP_REGNUM] = new_sp;
1290699b0f92Schristos 
1291699b0f92Schristos       if (is_fp_saved)
1292699b0f92Schristos 	regs[ARC_FP_REGNUM] = regs[ARC_SP_REGNUM];
1293699b0f92Schristos 
1294699b0f92Schristos       return true;
1295699b0f92Schristos     }
1296699b0f92Schristos 
1297699b0f92Schristos   /* Some other architectures, like nds32 or arm, try to continue as far as
1298699b0f92Schristos      possible when building a prologue cache (as opposed to when skipping
1299699b0f92Schristos      prologue), so that cache will be as full as possible.  However current
1300699b0f92Schristos      code for ARC doesn't recognize some instructions that may modify SP, like
1301699b0f92Schristos      ADD, AND, OR, etc, hence there is no way to guarantee that SP wasn't
1302699b0f92Schristos      clobbered by the skipped instruction.  Potential existence of extension
1303699b0f92Schristos      instruction, which may do anything they want makes this even more complex,
1304699b0f92Schristos      so it is just better to halt on a first unrecognized instruction.  */
1305699b0f92Schristos 
1306699b0f92Schristos   return false;
1307699b0f92Schristos }
1308699b0f92Schristos 
1309699b0f92Schristos /* Analyze the prologue and update the corresponding frame cache for the frame
1310699b0f92Schristos    unwinder for unwinding frames that doesn't have debug info.  In such
1311699b0f92Schristos    situation GDB attempts to parse instructions in the prologue to understand
1312699b0f92Schristos    where each register is saved.
1313699b0f92Schristos 
1314699b0f92Schristos    If CACHE is not NULL, then it will be filled with information about saved
1315699b0f92Schristos    registers.
1316699b0f92Schristos 
13177d62b00eSchristos    There are several variations of prologue which GDB may encounter.  "Full"
1318699b0f92Schristos    prologue looks like this:
1319699b0f92Schristos 
1320699b0f92Schristos 	sub	sp,sp,<imm>   ; Space for variadic arguments.
1321699b0f92Schristos 	push	blink	      ; Store return address.
1322699b0f92Schristos 	push	r13	      ; Store callee saved registers (up to R26/GP).
1323699b0f92Schristos 	push	r14
1324699b0f92Schristos 	push	fp	      ; Store frame pointer.
1325699b0f92Schristos 	mov	fp,sp	      ; Update frame pointer.
1326699b0f92Schristos 	sub	sp,sp,<imm>   ; Create space for local vars on the stack.
1327699b0f92Schristos 
1328699b0f92Schristos    Depending on compiler options lots of things may change:
1329699b0f92Schristos 
1330699b0f92Schristos     1) BLINK is not saved in leaf functions.
1331699b0f92Schristos     2) Frame pointer is not saved and updated if -fomit-frame-pointer is used.
1332699b0f92Schristos     3) 16-bit versions of those instructions may be used.
1333699b0f92Schristos     4) Instead of a sequence of several push'es, compiler may instead prefer to
1334699b0f92Schristos     do one subtract on stack pointer and then store registers using normal
1335699b0f92Schristos     store, that doesn't update SP.  Like this:
1336699b0f92Schristos 
1337699b0f92Schristos 
13387d62b00eSchristos 	sub	sp,sp,8		; Create space for callee-saved registers.
1339699b0f92Schristos 	st	r13,[sp,4]      ; Store callee saved registers (up to R26/GP).
1340699b0f92Schristos 	st	r14,[sp,0]
1341699b0f92Schristos 
1342699b0f92Schristos     5) ENTER_S instruction can encode most of prologue sequence in one
1343699b0f92Schristos     instruction (except for those subtracts for variadic arguments and local
1344699b0f92Schristos     variables).
1345699b0f92Schristos     6) GCC may use "millicode" functions from libgcc to store callee-saved
1346699b0f92Schristos     registers with minimal code-size requirements.  This function currently
1347699b0f92Schristos     doesn't support this.
1348699b0f92Schristos 
1349699b0f92Schristos    ENTRYPOINT is a function entry point where prologue starts.
1350699b0f92Schristos 
1351699b0f92Schristos    LIMIT_PC is a maximum possible end address of prologue (meaning address
1352699b0f92Schristos    of first instruction after the prologue).  It might also point to the middle
1353699b0f92Schristos    of prologue if execution has been stopped by the breakpoint at this address
1354699b0f92Schristos    - in this case debugger should analyze prologue only up to this address,
1355699b0f92Schristos    because further instructions haven't been executed yet.
1356699b0f92Schristos 
1357699b0f92Schristos    Returns address of the first instruction after the prologue.  */
1358699b0f92Schristos 
1359699b0f92Schristos static CORE_ADDR
1360699b0f92Schristos arc_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR entrypoint,
1361699b0f92Schristos 		      const CORE_ADDR limit_pc, struct arc_frame_cache *cache)
1362699b0f92Schristos {
1363*6881a400Schristos   arc_debug_printf ("entrypoint=%s, limit_pc=%s",
1364699b0f92Schristos 		    paddress (gdbarch, entrypoint),
1365699b0f92Schristos 		    paddress (gdbarch, limit_pc));
1366699b0f92Schristos 
1367699b0f92Schristos   /* Prologue values.  Only core registers can be stored.  */
1368699b0f92Schristos   pv_t regs[ARC_LAST_CORE_REGNUM + 1];
1369699b0f92Schristos   for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1370699b0f92Schristos     regs[i] = pv_register (i, 0);
13717f2ac410Schristos   pv_area stack (ARC_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1372699b0f92Schristos 
1373699b0f92Schristos   CORE_ADDR current_prologue_end = entrypoint;
1374699b0f92Schristos 
1375699b0f92Schristos   /* Look at each instruction in the prologue.  */
1376699b0f92Schristos   while (current_prologue_end < limit_pc)
1377699b0f92Schristos     {
1378699b0f92Schristos       struct arc_instruction insn;
1379699b0f92Schristos 
1380*6881a400Schristos       struct gdb_non_printing_memory_disassembler dis (gdbarch);
1381*6881a400Schristos       arc_insn_decode (current_prologue_end, dis.disasm_info (),
1382*6881a400Schristos 		       arc_delayed_print_insn, &insn);
1383*6881a400Schristos 
1384*6881a400Schristos       if (arc_debug)
1385699b0f92Schristos 	arc_insn_dump (insn);
1386699b0f92Schristos 
1387699b0f92Schristos       /* If this instruction is in the prologue, fields in the cache will be
1388699b0f92Schristos 	 updated, and the saved registers mask may be updated.  */
13897f2ac410Schristos       if (!arc_is_in_prologue (gdbarch, insn, regs, &stack))
1390699b0f92Schristos 	{
1391699b0f92Schristos 	  /* Found an instruction that is not in the prologue.  */
1392*6881a400Schristos 	  arc_debug_printf ("End of prologue reached at address %s",
1393699b0f92Schristos 			    paddress (gdbarch, insn.address));
1394699b0f92Schristos 	  break;
1395699b0f92Schristos 	}
1396699b0f92Schristos 
1397699b0f92Schristos       current_prologue_end = arc_insn_get_linear_next_pc (insn);
1398699b0f92Schristos     }
1399699b0f92Schristos 
1400699b0f92Schristos   if (cache != NULL)
1401699b0f92Schristos     {
1402699b0f92Schristos       /* Figure out if it is a frame pointer or just a stack pointer.  */
1403699b0f92Schristos       if (pv_is_register (regs[ARC_FP_REGNUM], ARC_SP_REGNUM))
1404699b0f92Schristos 	{
1405699b0f92Schristos 	  cache->frame_base_reg = ARC_FP_REGNUM;
1406699b0f92Schristos 	  cache->frame_base_offset = -regs[ARC_FP_REGNUM].k;
1407699b0f92Schristos 	}
1408699b0f92Schristos       else
1409699b0f92Schristos 	{
1410699b0f92Schristos 	  cache->frame_base_reg = ARC_SP_REGNUM;
1411699b0f92Schristos 	  cache->frame_base_offset = -regs[ARC_SP_REGNUM].k;
1412699b0f92Schristos 	}
1413699b0f92Schristos 
1414699b0f92Schristos       /* Assign offset from old SP to all saved registers.  */
1415699b0f92Schristos       for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1416699b0f92Schristos 	{
1417699b0f92Schristos 	  CORE_ADDR offset;
14187f2ac410Schristos 	  if (stack.find_reg (gdbarch, i, &offset))
1419*6881a400Schristos 	    cache->saved_regs[i].set_addr (offset);
1420699b0f92Schristos 	}
1421699b0f92Schristos     }
1422699b0f92Schristos 
1423699b0f92Schristos   return current_prologue_end;
1424699b0f92Schristos }
1425699b0f92Schristos 
1426699b0f92Schristos /* Estimated maximum prologue length in bytes.  This should include:
1427699b0f92Schristos    1) Store instruction for each callee-saved register (R25 - R13 + 1)
1428699b0f92Schristos    2) Two instructions for FP
1429699b0f92Schristos    3) One for BLINK
1430699b0f92Schristos    4) Three substract instructions for SP (for variadic args, for
1431699b0f92Schristos    callee saved regs and for local vars) and assuming that those SUB use
1432699b0f92Schristos    long-immediate (hence double length).
1433699b0f92Schristos    5) Stores of arguments registers are considered part of prologue too
1434699b0f92Schristos       (R7 - R1 + 1).
1435699b0f92Schristos    This is quite an extreme case, because even with -O0 GCC will collapse first
1436699b0f92Schristos    two SUBs into one and long immediate values are quite unlikely to appear in
1437699b0f92Schristos    this case, but still better to overshoot a bit - prologue analysis will
1438699b0f92Schristos    anyway stop at the first instruction that doesn't fit prologue, so this
1439699b0f92Schristos    limit will be rarely reached.  */
1440699b0f92Schristos 
1441699b0f92Schristos const static int MAX_PROLOGUE_LENGTH
1442699b0f92Schristos   = 4 * (ARC_R25_REGNUM - ARC_R13_REGNUM + 1 + 2 + 1 + 6
1443699b0f92Schristos 	 + ARC_LAST_ARG_REGNUM - ARC_FIRST_ARG_REGNUM + 1);
1444699b0f92Schristos 
1445699b0f92Schristos /* Implement the "skip_prologue" gdbarch method.
1446699b0f92Schristos 
1447699b0f92Schristos    Skip the prologue for the function at PC.  This is done by checking from
1448699b0f92Schristos    the line information read from the DWARF, if possible; otherwise, we scan
1449699b0f92Schristos    the function prologue to find its end.  */
1450699b0f92Schristos 
1451699b0f92Schristos static CORE_ADDR
1452699b0f92Schristos arc_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1453699b0f92Schristos {
1454*6881a400Schristos   arc_debug_printf ("pc = %s", paddress (gdbarch, pc));
1455699b0f92Schristos 
1456699b0f92Schristos   CORE_ADDR func_addr;
1457699b0f92Schristos   const char *func_name;
1458699b0f92Schristos 
1459699b0f92Schristos   /* See what the symbol table says.  */
1460699b0f92Schristos   if (find_pc_partial_function (pc, &func_name, &func_addr, NULL))
1461699b0f92Schristos     {
1462699b0f92Schristos       /* Found a function.  */
1463699b0f92Schristos       CORE_ADDR postprologue_pc
1464699b0f92Schristos 	= skip_prologue_using_sal (gdbarch, func_addr);
1465699b0f92Schristos 
1466699b0f92Schristos       if (postprologue_pc != 0)
1467699b0f92Schristos 	return std::max (pc, postprologue_pc);
1468699b0f92Schristos     }
1469699b0f92Schristos 
1470699b0f92Schristos   /* No prologue info in symbol table, have to analyze prologue.  */
1471699b0f92Schristos 
1472699b0f92Schristos   /* Find an upper limit on the function prologue using the debug
1473699b0f92Schristos      information.  If there is no debug information about prologue end, then
1474699b0f92Schristos      skip_prologue_using_sal will return 0.  */
1475699b0f92Schristos   CORE_ADDR limit_pc = skip_prologue_using_sal (gdbarch, pc);
1476699b0f92Schristos 
1477699b0f92Schristos   /* If there is no debug information at all, it is required to give some
1478699b0f92Schristos      semi-arbitrary hard limit on amount of bytes to scan during prologue
1479699b0f92Schristos      analysis.  */
1480699b0f92Schristos   if (limit_pc == 0)
1481699b0f92Schristos     limit_pc = pc + MAX_PROLOGUE_LENGTH;
1482699b0f92Schristos 
1483699b0f92Schristos   /* Find the address of the first instruction after the prologue by scanning
1484699b0f92Schristos      through it - no other information is needed, so pass NULL as a cache.  */
1485699b0f92Schristos   return arc_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1486699b0f92Schristos }
1487699b0f92Schristos 
1488699b0f92Schristos /* Implement the "print_insn" gdbarch method.
1489699b0f92Schristos 
1490699b0f92Schristos    arc_get_disassembler () may return different functions depending on bfd
1491699b0f92Schristos    type, so it is not possible to pass print_insn directly to
1492699b0f92Schristos    set_gdbarch_print_insn ().  Instead this wrapper function is used.  It also
1493699b0f92Schristos    may be used by other functions to get disassemble_info for address.  It is
1494699b0f92Schristos    important to note, that those print_insn from opcodes always print
1495699b0f92Schristos    instruction to the stream specified in the INFO.  If this is not desired,
1496699b0f92Schristos    then either `print_insn` function in INFO should be set to some function
1497699b0f92Schristos    that will not print, or `stream` should be different from standard
1498699b0f92Schristos    gdb_stdlog.  */
1499699b0f92Schristos 
1500699b0f92Schristos int
1501699b0f92Schristos arc_delayed_print_insn (bfd_vma addr, struct disassemble_info *info)
1502699b0f92Schristos {
15037d62b00eSchristos   /* Standard BFD "machine number" field allows libopcodes disassembler to
15047f2ac410Schristos      distinguish ARC 600, 700 and v2 cores, however v2 encompasses both ARC EM
15057f2ac410Schristos      and HS, which have some difference between.  There are two ways to specify
15067f2ac410Schristos      what is the target core:
15077f2ac410Schristos      1) via the disassemble_info->disassembler_options;
15087f2ac410Schristos      2) otherwise libopcodes will use private (architecture-specific) ELF
15097f2ac410Schristos      header.
15107f2ac410Schristos 
15117f2ac410Schristos      Using disassembler_options is preferable, because it comes directly from
15127f2ac410Schristos      GDBserver which scanned an actual ARC core identification info.  However,
15137f2ac410Schristos      not all GDBservers report core architecture, so as a fallback GDB still
15147f2ac410Schristos      should support analysis of ELF header.  The libopcodes disassembly code
15157f2ac410Schristos      uses the section to find the BFD and the BFD to find the ELF header,
15167f2ac410Schristos      therefore this function should set disassemble_info->section properly.
15177f2ac410Schristos 
15187f2ac410Schristos      disassembler_options was already set by non-target specific code with
15197f2ac410Schristos      proper options obtained via gdbarch_disassembler_options ().
15207f2ac410Schristos 
15217f2ac410Schristos      This function might be called multiple times in a sequence, reusing same
15227f2ac410Schristos      disassemble_info.  */
15237f2ac410Schristos   if ((info->disassembler_options == NULL) && (info->section == NULL))
15247f2ac410Schristos     {
15257f2ac410Schristos       struct obj_section *s = find_pc_section (addr);
15267f2ac410Schristos       if (s != NULL)
15277f2ac410Schristos 	info->section = s->the_bfd_section;
15287f2ac410Schristos     }
15297f2ac410Schristos 
15307f2ac410Schristos   return default_print_insn (addr, info);
1531699b0f92Schristos }
1532699b0f92Schristos 
1533699b0f92Schristos /* Baremetal breakpoint instructions.
1534699b0f92Schristos 
1535699b0f92Schristos    ARC supports both big- and little-endian.  However, instructions for
1536699b0f92Schristos    little-endian processors are encoded in the middle-endian: half-words are
1537699b0f92Schristos    in big-endian, while bytes inside the half-words are in little-endian; data
1538699b0f92Schristos    is represented in the "normal" little-endian.  Big-endian processors treat
1539699b0f92Schristos    data and code identically.
1540699b0f92Schristos 
1541699b0f92Schristos    Assuming the number 0x01020304, it will be presented this way:
1542699b0f92Schristos 
1543699b0f92Schristos    Address            :  N   N+1  N+2  N+3
1544699b0f92Schristos    little-endian      : 0x04 0x03 0x02 0x01
1545699b0f92Schristos    big-endian         : 0x01 0x02 0x03 0x04
1546699b0f92Schristos    ARC middle-endian  : 0x02 0x01 0x04 0x03
1547699b0f92Schristos   */
1548699b0f92Schristos 
1549699b0f92Schristos static const gdb_byte arc_brk_s_be[] = { 0x7f, 0xff };
1550699b0f92Schristos static const gdb_byte arc_brk_s_le[] = { 0xff, 0x7f };
1551699b0f92Schristos static const gdb_byte arc_brk_be[] = { 0x25, 0x6f, 0x00, 0x3f };
1552699b0f92Schristos static const gdb_byte arc_brk_le[] = { 0x6f, 0x25, 0x3f, 0x00 };
1553699b0f92Schristos 
1554699b0f92Schristos /* For ARC ELF, breakpoint uses the 16-bit BRK_S instruction, which is 0x7fff
1555699b0f92Schristos    (little endian) or 0xff7f (big endian).  We used to insert BRK_S even
1556699b0f92Schristos    instead of 32-bit instructions, which works mostly ok, unless breakpoint is
1557699b0f92Schristos    inserted into delay slot instruction.  In this case if branch is taken
1558699b0f92Schristos    BLINK value will be set to address of instruction after delay slot, however
1559699b0f92Schristos    if we replaced 32-bit instruction in delay slot with 16-bit long BRK_S,
1560699b0f92Schristos    then BLINK value will have an invalid value - it will point to the address
1561699b0f92Schristos    after the BRK_S (which was there at the moment of branch execution) while
1562699b0f92Schristos    it should point to the address after the 32-bit long instruction.  To avoid
1563699b0f92Schristos    such issues this function disassembles instruction at target location and
1564699b0f92Schristos    evaluates it value.
1565699b0f92Schristos 
1566699b0f92Schristos    ARC 600 supports only 16-bit BRK_S.
1567699b0f92Schristos 
1568699b0f92Schristos    NB: Baremetal GDB uses BRK[_S], while user-space GDB uses TRAP_S.  BRK[_S]
1569699b0f92Schristos    is much better because it doesn't commit unlike TRAP_S, so it can be set in
1570699b0f92Schristos    delay slots; however it cannot be used in user-mode, hence usage of TRAP_S
1571699b0f92Schristos    in GDB for user-space.  */
1572699b0f92Schristos 
1573699b0f92Schristos /* Implement the "breakpoint_kind_from_pc" gdbarch method.  */
1574699b0f92Schristos 
1575699b0f92Schristos static int
1576699b0f92Schristos arc_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
1577699b0f92Schristos {
1578699b0f92Schristos   size_t length_with_limm = gdb_insn_length (gdbarch, *pcptr);
1579699b0f92Schristos 
1580699b0f92Schristos   /* Replace 16-bit instruction with BRK_S, replace 32-bit instructions with
1581699b0f92Schristos      BRK.  LIMM is part of instruction length, so it can be either 4 or 8
1582699b0f92Schristos      bytes for 32-bit instructions.  */
1583699b0f92Schristos   if ((length_with_limm == 4 || length_with_limm == 8)
1584699b0f92Schristos       && !arc_mach_is_arc600 (gdbarch))
1585699b0f92Schristos     return sizeof (arc_brk_le);
1586699b0f92Schristos   else
1587699b0f92Schristos     return sizeof (arc_brk_s_le);
1588699b0f92Schristos }
1589699b0f92Schristos 
1590699b0f92Schristos /* Implement the "sw_breakpoint_from_kind" gdbarch method.  */
1591699b0f92Schristos 
1592699b0f92Schristos static const gdb_byte *
1593699b0f92Schristos arc_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
1594699b0f92Schristos {
1595*6881a400Schristos   gdb_assert (kind == 2 || kind == 4);
1596699b0f92Schristos   *size = kind;
1597699b0f92Schristos 
1598699b0f92Schristos   if (kind == sizeof (arc_brk_le))
1599699b0f92Schristos     {
1600699b0f92Schristos       return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1601699b0f92Schristos 	      ? arc_brk_be
1602699b0f92Schristos 	      : arc_brk_le);
1603699b0f92Schristos     }
1604699b0f92Schristos   else
1605699b0f92Schristos     {
1606699b0f92Schristos       return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1607699b0f92Schristos 	      ? arc_brk_s_be
1608699b0f92Schristos 	      : arc_brk_s_le);
1609699b0f92Schristos     }
1610699b0f92Schristos }
1611699b0f92Schristos 
1612699b0f92Schristos /* Implement the "frame_align" gdbarch method.  */
1613699b0f92Schristos 
1614699b0f92Schristos static CORE_ADDR
1615699b0f92Schristos arc_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1616699b0f92Schristos {
1617699b0f92Schristos   return align_down (sp, 4);
1618699b0f92Schristos }
1619699b0f92Schristos 
1620699b0f92Schristos /* Dump the frame info.  Used for internal debugging only.  */
1621699b0f92Schristos 
1622699b0f92Schristos static void
1623699b0f92Schristos arc_print_frame_cache (struct gdbarch *gdbarch, const char *message,
1624699b0f92Schristos 		       struct arc_frame_cache *cache, int addresses_known)
1625699b0f92Schristos {
1626*6881a400Schristos   arc_debug_printf ("frame_info %s", message);
1627*6881a400Schristos   arc_debug_printf ("prev_sp = %s", paddress (gdbarch, cache->prev_sp));
1628*6881a400Schristos   arc_debug_printf ("frame_base_reg = %i", cache->frame_base_reg);
1629*6881a400Schristos   arc_debug_printf ("frame_base_offset = %s",
1630699b0f92Schristos 		    plongest (cache->frame_base_offset));
1631699b0f92Schristos 
1632699b0f92Schristos   for (int i = 0; i <= ARC_BLINK_REGNUM; i++)
1633699b0f92Schristos     {
1634*6881a400Schristos       if (cache->saved_regs[i].is_addr ())
1635*6881a400Schristos 	arc_debug_printf ("saved register %s at %s %s",
1636699b0f92Schristos 			  gdbarch_register_name (gdbarch, i),
1637699b0f92Schristos 			  (addresses_known) ? "address" : "offset",
1638*6881a400Schristos 			      paddress (gdbarch, cache->saved_regs[i].addr ()));
1639699b0f92Schristos     }
1640699b0f92Schristos }
1641699b0f92Schristos 
1642699b0f92Schristos /* Frame unwinder for normal frames.  */
1643699b0f92Schristos 
1644699b0f92Schristos static struct arc_frame_cache *
1645*6881a400Schristos arc_make_frame_cache (frame_info_ptr this_frame)
1646699b0f92Schristos {
1647*6881a400Schristos   arc_debug_printf ("called");
1648699b0f92Schristos 
1649699b0f92Schristos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1650699b0f92Schristos 
1651699b0f92Schristos   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1652699b0f92Schristos   CORE_ADDR entrypoint, prologue_end;
1653699b0f92Schristos   if (find_pc_partial_function (block_addr, NULL, &entrypoint, &prologue_end))
1654699b0f92Schristos     {
1655699b0f92Schristos       struct symtab_and_line sal = find_pc_line (entrypoint, 0);
1656699b0f92Schristos       CORE_ADDR prev_pc = get_frame_pc (this_frame);
1657699b0f92Schristos       if (sal.line == 0)
1658699b0f92Schristos 	/* No line info so use current PC.  */
1659699b0f92Schristos 	prologue_end = prev_pc;
1660699b0f92Schristos       else if (sal.end < prologue_end)
1661699b0f92Schristos 	/* The next line begins after the function end.  */
1662699b0f92Schristos 	prologue_end = sal.end;
1663699b0f92Schristos 
1664699b0f92Schristos       prologue_end = std::min (prologue_end, prev_pc);
1665699b0f92Schristos     }
1666699b0f92Schristos   else
1667699b0f92Schristos     {
1668699b0f92Schristos       /* If find_pc_partial_function returned nothing then there is no symbol
1669699b0f92Schristos 	 information at all for this PC.  Currently it is assumed in this case
1670699b0f92Schristos 	 that current PC is entrypoint to function and try to construct the
1671699b0f92Schristos 	 frame from that.  This is, probably, suboptimal, for example ARM
1672699b0f92Schristos 	 assumes in this case that program is inside the normal frame (with
1673699b0f92Schristos 	 frame pointer).  ARC, perhaps, should try to do the same.  */
1674699b0f92Schristos       entrypoint = get_frame_register_unsigned (this_frame,
1675699b0f92Schristos 						gdbarch_pc_regnum (gdbarch));
1676699b0f92Schristos       prologue_end = entrypoint + MAX_PROLOGUE_LENGTH;
1677699b0f92Schristos     }
1678699b0f92Schristos 
1679699b0f92Schristos   /* Allocate new frame cache instance and space for saved register info.
1680699b0f92Schristos      FRAME_OBSTACK_ZALLOC will initialize fields to zeroes.  */
1681699b0f92Schristos   struct arc_frame_cache *cache
1682699b0f92Schristos     = FRAME_OBSTACK_ZALLOC (struct arc_frame_cache);
1683699b0f92Schristos   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1684699b0f92Schristos 
1685699b0f92Schristos   arc_analyze_prologue (gdbarch, entrypoint, prologue_end, cache);
1686699b0f92Schristos 
1687699b0f92Schristos   if (arc_debug)
1688699b0f92Schristos     arc_print_frame_cache (gdbarch, "after prologue", cache, false);
1689699b0f92Schristos 
1690699b0f92Schristos   CORE_ADDR unwound_fb = get_frame_register_unsigned (this_frame,
1691699b0f92Schristos 						      cache->frame_base_reg);
1692699b0f92Schristos   if (unwound_fb == 0)
1693699b0f92Schristos     return cache;
1694699b0f92Schristos   cache->prev_sp = unwound_fb + cache->frame_base_offset;
1695699b0f92Schristos 
1696699b0f92Schristos   for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1697699b0f92Schristos     {
1698*6881a400Schristos       if (cache->saved_regs[i].is_addr ())
1699*6881a400Schristos 	cache->saved_regs[i].set_addr (cache->saved_regs[i].addr ()
1700*6881a400Schristos 				       + cache->prev_sp);
1701699b0f92Schristos     }
1702699b0f92Schristos 
1703699b0f92Schristos   if (arc_debug)
1704699b0f92Schristos     arc_print_frame_cache (gdbarch, "after previous SP found", cache, true);
1705699b0f92Schristos 
1706699b0f92Schristos   return cache;
1707699b0f92Schristos }
1708699b0f92Schristos 
1709699b0f92Schristos /* Implement the "this_id" frame_unwind method.  */
1710699b0f92Schristos 
1711699b0f92Schristos static void
1712*6881a400Schristos arc_frame_this_id (frame_info_ptr this_frame, void **this_cache,
1713699b0f92Schristos 		   struct frame_id *this_id)
1714699b0f92Schristos {
1715*6881a400Schristos   arc_debug_printf ("called");
1716699b0f92Schristos 
1717699b0f92Schristos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1718699b0f92Schristos 
1719699b0f92Schristos   if (*this_cache == NULL)
1720699b0f92Schristos     *this_cache = arc_make_frame_cache (this_frame);
1721699b0f92Schristos   struct arc_frame_cache *cache = (struct arc_frame_cache *) (*this_cache);
1722699b0f92Schristos 
1723699b0f92Schristos   CORE_ADDR stack_addr = cache->prev_sp;
1724699b0f92Schristos 
1725699b0f92Schristos   /* There are 4 possible situation which decide how frame_id->code_addr is
1726699b0f92Schristos      evaluated:
1727699b0f92Schristos 
1728699b0f92Schristos      1) Function is compiled with option -g.  Then frame_id will be created
1729699b0f92Schristos      in dwarf_* function and not in this function.  NB: even if target
1730699b0f92Schristos      binary is compiled with -g, some std functions like __start and _init
1731699b0f92Schristos      are not, so they still will follow one of the following choices.
1732699b0f92Schristos 
1733699b0f92Schristos      2) Function is compiled without -g and binary hasn't been stripped in
1734699b0f92Schristos      any way.  In this case GDB still has enough information to evaluate
1735699b0f92Schristos      frame code_addr properly.  This case is covered by call to
1736699b0f92Schristos      get_frame_func ().
1737699b0f92Schristos 
1738699b0f92Schristos      3) Binary has been striped with option -g (strip debug symbols).  In
1739699b0f92Schristos      this case there is still enough symbols for get_frame_func () to work
1740699b0f92Schristos      properly, so this case is also covered by it.
1741699b0f92Schristos 
1742699b0f92Schristos      4) Binary has been striped with option -s (strip all symbols).  In this
1743699b0f92Schristos      case GDB cannot get function start address properly, so we return current
1744699b0f92Schristos      PC value instead.
1745699b0f92Schristos    */
1746699b0f92Schristos   CORE_ADDR code_addr = get_frame_func (this_frame);
1747699b0f92Schristos   if (code_addr == 0)
1748699b0f92Schristos     code_addr = get_frame_register_unsigned (this_frame,
1749699b0f92Schristos 					     gdbarch_pc_regnum (gdbarch));
1750699b0f92Schristos 
1751699b0f92Schristos   *this_id = frame_id_build (stack_addr, code_addr);
1752699b0f92Schristos }
1753699b0f92Schristos 
1754699b0f92Schristos /* Implement the "prev_register" frame_unwind method.  */
1755699b0f92Schristos 
1756699b0f92Schristos static struct value *
1757*6881a400Schristos arc_frame_prev_register (frame_info_ptr this_frame,
1758699b0f92Schristos 			 void **this_cache, int regnum)
1759699b0f92Schristos {
1760699b0f92Schristos   if (*this_cache == NULL)
1761699b0f92Schristos     *this_cache = arc_make_frame_cache (this_frame);
1762699b0f92Schristos   struct arc_frame_cache *cache = (struct arc_frame_cache *) (*this_cache);
1763699b0f92Schristos 
1764699b0f92Schristos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1765699b0f92Schristos 
1766699b0f92Schristos   /* If we are asked to unwind the PC, then we need to return BLINK instead:
1767699b0f92Schristos      the saved value of PC points into this frame's function's prologue, not
1768699b0f92Schristos      the next frame's function's resume location.  */
1769699b0f92Schristos   if (regnum == gdbarch_pc_regnum (gdbarch))
1770699b0f92Schristos     regnum = ARC_BLINK_REGNUM;
1771699b0f92Schristos 
1772699b0f92Schristos   /* SP is a special case - we should return prev_sp, because
1773699b0f92Schristos      trad_frame_get_prev_register will return _current_ SP value.
1774699b0f92Schristos      Alternatively we could have stored cache->prev_sp in the cache->saved
1775699b0f92Schristos      regs, but here we follow the lead of AArch64, ARM and Xtensa and will
1776699b0f92Schristos      leave that logic in this function, instead of prologue analyzers.  That I
1777699b0f92Schristos      think is a bit more clear as `saved_regs` should contain saved regs, not
1778699b0f92Schristos      computable.
1779699b0f92Schristos 
1780699b0f92Schristos      Because value has been computed, "got_constant" should be used, so that
1781699b0f92Schristos      returned value will be a "not_lval" - immutable.  */
1782699b0f92Schristos 
1783699b0f92Schristos   if (regnum == gdbarch_sp_regnum (gdbarch))
1784699b0f92Schristos     return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp);
1785699b0f92Schristos 
1786699b0f92Schristos   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
1787699b0f92Schristos }
1788699b0f92Schristos 
1789699b0f92Schristos /* Implement the "init_reg" dwarf2_frame method.  */
1790699b0f92Schristos 
1791699b0f92Schristos static void
1792699b0f92Schristos arc_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1793699b0f92Schristos 			   struct dwarf2_frame_state_reg *reg,
1794*6881a400Schristos 			   frame_info_ptr info)
1795699b0f92Schristos {
1796699b0f92Schristos   if (regnum == gdbarch_pc_regnum (gdbarch))
1797699b0f92Schristos     /* The return address column.  */
1798699b0f92Schristos     reg->how = DWARF2_FRAME_REG_RA;
1799699b0f92Schristos   else if (regnum == gdbarch_sp_regnum (gdbarch))
1800699b0f92Schristos     /* The call frame address.  */
1801699b0f92Schristos     reg->how = DWARF2_FRAME_REG_CFA;
1802699b0f92Schristos }
1803699b0f92Schristos 
1804*6881a400Schristos /*  Signal trampoline frame unwinder.  Allows frame unwinding to happen
1805*6881a400Schristos     from within signal handlers.  */
1806*6881a400Schristos 
1807*6881a400Schristos static struct arc_frame_cache *
1808*6881a400Schristos arc_make_sigtramp_frame_cache (frame_info_ptr this_frame)
1809*6881a400Schristos {
1810*6881a400Schristos   arc_debug_printf ("called");
1811*6881a400Schristos 
1812*6881a400Schristos   gdbarch *arch = get_frame_arch (this_frame);
1813*6881a400Schristos   arc_gdbarch_tdep *tdep = gdbarch_tdep<arc_gdbarch_tdep> (arch);
1814*6881a400Schristos 
1815*6881a400Schristos   /* Allocate new frame cache instance and space for saved register info.  */
1816*6881a400Schristos   struct arc_frame_cache *cache = FRAME_OBSTACK_ZALLOC (struct arc_frame_cache);
1817*6881a400Schristos   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1818*6881a400Schristos 
1819*6881a400Schristos   /* Get the stack pointer and use it as the frame base.  */
1820*6881a400Schristos   cache->prev_sp = arc_frame_base_address (this_frame, NULL);
1821*6881a400Schristos 
1822*6881a400Schristos   /* If the ARC-private target-dependent info doesn't have a table of
1823*6881a400Schristos      offsets of saved register contents within an OS signal context
1824*6881a400Schristos      structure, then there is nothing to analyze.  */
1825*6881a400Schristos   if (tdep->sc_reg_offset == NULL)
1826*6881a400Schristos     return cache;
1827*6881a400Schristos 
1828*6881a400Schristos   /* Find the address of the sigcontext structure.  */
1829*6881a400Schristos   CORE_ADDR addr = tdep->sigcontext_addr (this_frame);
1830*6881a400Schristos 
1831*6881a400Schristos   /* For each register, if its contents have been saved within the
1832*6881a400Schristos      sigcontext structure, determine the address of those contents.  */
1833*6881a400Schristos   gdb_assert (tdep->sc_num_regs <= (ARC_LAST_REGNUM + 1));
1834*6881a400Schristos   for (int i = 0; i < tdep->sc_num_regs; i++)
1835*6881a400Schristos     {
1836*6881a400Schristos       if (tdep->sc_reg_offset[i] != ARC_OFFSET_NO_REGISTER)
1837*6881a400Schristos 	cache->saved_regs[i].set_addr (addr + tdep->sc_reg_offset[i]);
1838*6881a400Schristos     }
1839*6881a400Schristos 
1840*6881a400Schristos   return cache;
1841*6881a400Schristos }
1842*6881a400Schristos 
1843*6881a400Schristos /* Implement the "this_id" frame_unwind method for signal trampoline
1844*6881a400Schristos    frames.  */
1845*6881a400Schristos 
1846*6881a400Schristos static void
1847*6881a400Schristos arc_sigtramp_frame_this_id (frame_info_ptr this_frame,
1848*6881a400Schristos 			    void **this_cache, struct frame_id *this_id)
1849*6881a400Schristos {
1850*6881a400Schristos   arc_debug_printf ("called");
1851*6881a400Schristos 
1852*6881a400Schristos   if (*this_cache == NULL)
1853*6881a400Schristos     *this_cache = arc_make_sigtramp_frame_cache (this_frame);
1854*6881a400Schristos 
1855*6881a400Schristos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1856*6881a400Schristos   struct arc_frame_cache *cache = (struct arc_frame_cache *) *this_cache;
1857*6881a400Schristos   CORE_ADDR stack_addr = cache->prev_sp;
1858*6881a400Schristos   CORE_ADDR code_addr
1859*6881a400Schristos     = get_frame_register_unsigned (this_frame, gdbarch_pc_regnum (gdbarch));
1860*6881a400Schristos   *this_id = frame_id_build (stack_addr, code_addr);
1861*6881a400Schristos }
1862*6881a400Schristos 
1863*6881a400Schristos /* Get a register from a signal handler frame.  */
1864*6881a400Schristos 
1865*6881a400Schristos static struct value *
1866*6881a400Schristos arc_sigtramp_frame_prev_register (frame_info_ptr this_frame,
1867*6881a400Schristos 				  void **this_cache, int regnum)
1868*6881a400Schristos {
1869*6881a400Schristos   arc_debug_printf ("regnum = %d", regnum);
1870*6881a400Schristos 
1871*6881a400Schristos   /* Make sure we've initialized the cache.  */
1872*6881a400Schristos   if (*this_cache == NULL)
1873*6881a400Schristos     *this_cache = arc_make_sigtramp_frame_cache (this_frame);
1874*6881a400Schristos 
1875*6881a400Schristos   struct arc_frame_cache *cache = (struct arc_frame_cache *) *this_cache;
1876*6881a400Schristos   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
1877*6881a400Schristos }
1878*6881a400Schristos 
1879*6881a400Schristos /* Frame sniffer for signal handler frame.  Only recognize a frame if we
1880*6881a400Schristos    have a sigcontext_addr handler in the target dependency.  */
1881*6881a400Schristos 
1882*6881a400Schristos static int
1883*6881a400Schristos arc_sigtramp_frame_sniffer (const struct frame_unwind *self,
1884*6881a400Schristos 			    frame_info_ptr this_frame,
1885*6881a400Schristos 			    void **this_cache)
1886*6881a400Schristos {
1887*6881a400Schristos   arc_debug_printf ("called");
1888*6881a400Schristos 
1889*6881a400Schristos   gdbarch *arch = get_frame_arch (this_frame);
1890*6881a400Schristos   arc_gdbarch_tdep *tdep = gdbarch_tdep<arc_gdbarch_tdep> (arch);
1891*6881a400Schristos 
1892*6881a400Schristos   /* If we have a sigcontext_addr handler, then just return 1 (same as the
1893*6881a400Schristos      "default_frame_sniffer ()").  */
1894*6881a400Schristos   return (tdep->sigcontext_addr != NULL && tdep->is_sigtramp != NULL
1895*6881a400Schristos 	  && tdep->is_sigtramp (this_frame));
1896*6881a400Schristos }
1897*6881a400Schristos 
1898699b0f92Schristos /* Structure defining the ARC ordinary frame unwind functions.  Since we are
1899699b0f92Schristos    the fallback unwinder, we use the default frame sniffer, which always
1900699b0f92Schristos    accepts the frame.  */
1901699b0f92Schristos 
1902699b0f92Schristos static const struct frame_unwind arc_frame_unwind = {
1903*6881a400Schristos   "arc prologue",
1904699b0f92Schristos   NORMAL_FRAME,
1905699b0f92Schristos   default_frame_unwind_stop_reason,
1906699b0f92Schristos   arc_frame_this_id,
1907699b0f92Schristos   arc_frame_prev_register,
1908699b0f92Schristos   NULL,
1909699b0f92Schristos   default_frame_sniffer,
1910699b0f92Schristos   NULL,
1911699b0f92Schristos   NULL
1912699b0f92Schristos };
1913699b0f92Schristos 
1914*6881a400Schristos /* Structure defining the ARC signal frame unwind functions.  Custom
1915*6881a400Schristos    sniffer is used, because this frame must be accepted only in the right
1916*6881a400Schristos    context.  */
1917*6881a400Schristos 
1918*6881a400Schristos static const struct frame_unwind arc_sigtramp_frame_unwind = {
1919*6881a400Schristos   "arc sigtramp",
1920*6881a400Schristos   SIGTRAMP_FRAME,
1921*6881a400Schristos   default_frame_unwind_stop_reason,
1922*6881a400Schristos   arc_sigtramp_frame_this_id,
1923*6881a400Schristos   arc_sigtramp_frame_prev_register,
1924*6881a400Schristos   NULL,
1925*6881a400Schristos   arc_sigtramp_frame_sniffer,
1926*6881a400Schristos   NULL,
1927*6881a400Schristos   NULL
1928*6881a400Schristos };
1929*6881a400Schristos 
1930699b0f92Schristos 
1931699b0f92Schristos static const struct frame_base arc_normal_base = {
1932699b0f92Schristos   &arc_frame_unwind,
1933699b0f92Schristos   arc_frame_base_address,
1934699b0f92Schristos   arc_frame_base_address,
1935699b0f92Schristos   arc_frame_base_address
1936699b0f92Schristos };
1937699b0f92Schristos 
19387d62b00eSchristos static enum arc_isa
19397d62b00eSchristos mach_type_to_arc_isa (const unsigned long mach)
19407d62b00eSchristos {
19417d62b00eSchristos   switch (mach)
19427d62b00eSchristos     {
19437d62b00eSchristos     case bfd_mach_arc_arc600:
19447d62b00eSchristos     case bfd_mach_arc_arc601:
19457d62b00eSchristos     case bfd_mach_arc_arc700:
19467d62b00eSchristos       return ARC_ISA_ARCV1;
19477d62b00eSchristos     case bfd_mach_arc_arcv2:
19487d62b00eSchristos       return ARC_ISA_ARCV2;
19497d62b00eSchristos     default:
1950*6881a400Schristos 	internal_error (_("unknown machine id %lu"), mach);
19517d62b00eSchristos     }
19527d62b00eSchristos }
19537d62b00eSchristos 
1954*6881a400Schristos /* See arc-tdep.h.  */
19557d62b00eSchristos 
1956*6881a400Schristos arc_arch_features
1957*6881a400Schristos arc_arch_features_create (const bfd *abfd, const unsigned long mach)
19587d62b00eSchristos {
19597d62b00eSchristos   /* Use 4 as a fallback value.  */
19607d62b00eSchristos   int reg_size = 4;
19617d62b00eSchristos 
19627d62b00eSchristos   /* Try to guess the features parameters by looking at the binary to be
19637d62b00eSchristos      executed.  If the user is providing a binary that does not match the
19647d62b00eSchristos      target, then tough luck.  This is the last effort to makes sense of
19657d62b00eSchristos      what's going on.  */
19667d62b00eSchristos   if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
19677d62b00eSchristos     {
19687d62b00eSchristos       unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
19697d62b00eSchristos 
19707d62b00eSchristos       if (eclass == ELFCLASS32)
19717d62b00eSchristos 	reg_size = 4;
19727d62b00eSchristos       else if (eclass == ELFCLASS64)
19737d62b00eSchristos 	reg_size = 8;
19747d62b00eSchristos       else
1975*6881a400Schristos 	internal_error (_("unknown ELF header class %d"), eclass);
19767d62b00eSchristos     }
19777d62b00eSchristos 
19787d62b00eSchristos   /* MACH from a bfd_arch_info struct is used here.  It should be a safe
19797d62b00eSchristos      bet, as it looks like the struct is always initialized even when we
19807d62b00eSchristos      don't pass any elf file to GDB at all (it uses default arch in that
19817d62b00eSchristos      case).  */
19827d62b00eSchristos   arc_isa isa = mach_type_to_arc_isa (mach);
19837d62b00eSchristos 
1984*6881a400Schristos   return arc_arch_features (reg_size, isa);
19857d62b00eSchristos }
19867d62b00eSchristos 
19877d62b00eSchristos /* Look for obsolete core feature names in TDESC.  */
19887d62b00eSchristos 
19897d62b00eSchristos static const struct tdesc_feature *
19907d62b00eSchristos find_obsolete_core_names (const struct target_desc *tdesc)
19917d62b00eSchristos {
19927d62b00eSchristos   const struct tdesc_feature *feat = nullptr;
19937d62b00eSchristos 
19947d62b00eSchristos   feat = tdesc_find_feature (tdesc, ARC_CORE_V1_OBSOLETE_FEATURE_NAME);
19957d62b00eSchristos 
19967d62b00eSchristos   if (feat == nullptr)
19977d62b00eSchristos     feat = tdesc_find_feature (tdesc, ARC_CORE_V2_OBSOLETE_FEATURE_NAME);
19987d62b00eSchristos 
19997d62b00eSchristos   if (feat == nullptr)
20007d62b00eSchristos     feat = tdesc_find_feature
20017d62b00eSchristos       (tdesc, ARC_CORE_V2_REDUCED_OBSOLETE_FEATURE_NAME);
20027d62b00eSchristos 
20037d62b00eSchristos   return feat;
20047d62b00eSchristos }
20057d62b00eSchristos 
20067d62b00eSchristos /* Look for obsolete aux feature names in TDESC.  */
20077d62b00eSchristos 
20087d62b00eSchristos static const struct tdesc_feature *
20097d62b00eSchristos find_obsolete_aux_names (const struct target_desc *tdesc)
20107d62b00eSchristos {
20117d62b00eSchristos   return tdesc_find_feature (tdesc, ARC_AUX_OBSOLETE_FEATURE_NAME);
20127d62b00eSchristos }
20137d62b00eSchristos 
20147d62b00eSchristos /* Based on the MACH value, determines which core register features set
20157d62b00eSchristos    must be used.  */
20167d62b00eSchristos 
20177d62b00eSchristos static arc_register_feature *
20187d62b00eSchristos determine_core_reg_feature_set (const unsigned long mach)
20197d62b00eSchristos {
20207d62b00eSchristos   switch (mach_type_to_arc_isa (mach))
20217d62b00eSchristos     {
20227d62b00eSchristos     case ARC_ISA_ARCV1:
20237d62b00eSchristos       return &arc_v1_core_reg_feature;
20247d62b00eSchristos     case ARC_ISA_ARCV2:
20257d62b00eSchristos       return &arc_v2_core_reg_feature;
20267d62b00eSchristos     default:
20277d62b00eSchristos       gdb_assert_not_reached
20287d62b00eSchristos 	("Unknown machine type to determine the core feature set.");
20297d62b00eSchristos     }
20307d62b00eSchristos }
20317d62b00eSchristos 
20327d62b00eSchristos /* At the moment, there is only 1 auxiliary register features set.
20337d62b00eSchristos    This is a place holder for future extendability.  */
20347d62b00eSchristos 
20357d62b00eSchristos static const arc_register_feature *
20367d62b00eSchristos determine_aux_reg_feature_set ()
20377d62b00eSchristos {
20387d62b00eSchristos   return &arc_common_aux_reg_feature;
20397d62b00eSchristos }
20407d62b00eSchristos 
20417d62b00eSchristos /* Update accumulator register names (ACCH/ACCL) for r58 and r59 in the
20427d62b00eSchristos    register sets.  The endianness determines the assignment:
20437d62b00eSchristos 
20447d62b00eSchristos 	,------.------.
20457d62b00eSchristos 	| acch | accl |
20467d62b00eSchristos    ,----|------+------|
20477d62b00eSchristos    | LE | r59  | r58  |
20487d62b00eSchristos    | BE | r58  | r59  |
20497d62b00eSchristos    `----^------^------'  */
20507d62b00eSchristos 
20517d62b00eSchristos static void
20527d62b00eSchristos arc_update_acc_reg_names (const int byte_order)
20537d62b00eSchristos {
20547d62b00eSchristos   const char *r58_alias
20557d62b00eSchristos     = byte_order == BFD_ENDIAN_LITTLE ? "accl" : "acch";
20567d62b00eSchristos   const char *r59_alias
20577d62b00eSchristos     = byte_order == BFD_ENDIAN_LITTLE ? "acch" : "accl";
20587d62b00eSchristos 
20597d62b00eSchristos   /* Subscript 1 must be OK because those registers have 2 names.  */
20607d62b00eSchristos   arc_v1_core_reg_feature.registers[ARC_R58_REGNUM].names[1] = r58_alias;
20617d62b00eSchristos   arc_v1_core_reg_feature.registers[ARC_R59_REGNUM].names[1] = r59_alias;
20627d62b00eSchristos   arc_v2_core_reg_feature.registers[ARC_R58_REGNUM].names[1] = r58_alias;
20637d62b00eSchristos   arc_v2_core_reg_feature.registers[ARC_R59_REGNUM].names[1] = r59_alias;
20647d62b00eSchristos }
20657d62b00eSchristos 
20667d62b00eSchristos /* Go through all the registers in REG_SET and check if they exist
20677d62b00eSchristos    in FEATURE.  The TDESC_DATA is updated with the register number
20687d62b00eSchristos    in REG_SET if it is found in the feature.  If a required register
20697d62b00eSchristos    is not found, this function returns false.  */
20707d62b00eSchristos 
20717d62b00eSchristos static bool
20727d62b00eSchristos arc_check_tdesc_feature (struct tdesc_arch_data *tdesc_data,
20737d62b00eSchristos 			 const struct tdesc_feature *feature,
20747d62b00eSchristos 			 const struct arc_register_feature *reg_set)
20757d62b00eSchristos {
20767d62b00eSchristos   for (const auto &reg : reg_set->registers)
20777d62b00eSchristos     {
20787d62b00eSchristos       bool found = false;
20797d62b00eSchristos 
20807d62b00eSchristos       for (const char *name : reg.names)
20817d62b00eSchristos 	{
20827d62b00eSchristos 	  found
20837d62b00eSchristos 	    = tdesc_numbered_register (feature, tdesc_data, reg.regnum, name);
20847d62b00eSchristos 
20857d62b00eSchristos 	  if (found)
20867d62b00eSchristos 	    break;
20877d62b00eSchristos 	}
20887d62b00eSchristos 
20897d62b00eSchristos       if (!found && reg.required_p)
20907d62b00eSchristos 	{
20917d62b00eSchristos 	  std::ostringstream reg_names;
20927d62b00eSchristos 	  for (std::size_t i = 0; i < reg.names.size(); ++i)
20937d62b00eSchristos 	    {
20947d62b00eSchristos 	      if (i == 0)
20957d62b00eSchristos 		reg_names << "'" << reg.names[0] << "'";
20967d62b00eSchristos 	      else
20977d62b00eSchristos 		reg_names << " or '" << reg.names[0] << "'";
20987d62b00eSchristos 	    }
20997d62b00eSchristos 	  arc_print (_("Error: Cannot find required register(s) %s "
21007d62b00eSchristos 		       "in feature '%s'.\n"), reg_names.str ().c_str (),
21017d62b00eSchristos 		       feature->name.c_str ());
21027d62b00eSchristos 	  return false;
21037d62b00eSchristos 	}
21047d62b00eSchristos     }
21057d62b00eSchristos 
21067d62b00eSchristos   return true;
21077d62b00eSchristos }
21087d62b00eSchristos 
21097d62b00eSchristos /* Check for the existance of "lp_start" and "lp_end" in target description.
21107d62b00eSchristos    If both are present, assume there is hardware loop support in the target.
21117d62b00eSchristos    This can be improved by looking into "lpc_size" field of "isa_config"
21127d62b00eSchristos    auxiliary register.  */
21137d62b00eSchristos 
21147d62b00eSchristos static bool
21157d62b00eSchristos arc_check_for_hw_loops (const struct target_desc *tdesc,
21167d62b00eSchristos 			struct tdesc_arch_data *data)
21177d62b00eSchristos {
21187d62b00eSchristos   const auto feature_aux = tdesc_find_feature (tdesc, ARC_AUX_FEATURE_NAME);
21197d62b00eSchristos   const auto aux_regset = determine_aux_reg_feature_set ();
21207d62b00eSchristos 
21217d62b00eSchristos   if (feature_aux == nullptr)
21227d62b00eSchristos     return false;
21237d62b00eSchristos 
21247d62b00eSchristos   bool hw_loop_p = false;
21257d62b00eSchristos   const auto lp_start_name =
21267d62b00eSchristos     aux_regset->registers[ARC_LP_START_REGNUM - ARC_FIRST_AUX_REGNUM].names[0];
21277d62b00eSchristos   const auto lp_end_name =
21287d62b00eSchristos     aux_regset->registers[ARC_LP_END_REGNUM - ARC_FIRST_AUX_REGNUM].names[0];
21297d62b00eSchristos 
21307d62b00eSchristos   hw_loop_p = tdesc_numbered_register (feature_aux, data,
21317d62b00eSchristos 				       ARC_LP_START_REGNUM, lp_start_name);
21327d62b00eSchristos   hw_loop_p &= tdesc_numbered_register (feature_aux, data,
21337d62b00eSchristos 				       ARC_LP_END_REGNUM, lp_end_name);
21347d62b00eSchristos 
21357d62b00eSchristos   return hw_loop_p;
21367d62b00eSchristos }
21377d62b00eSchristos 
2138699b0f92Schristos /* Initialize target description for the ARC.
2139699b0f92Schristos 
21407d62b00eSchristos    Returns true if input TDESC was valid and in this case it will assign TDESC
2141699b0f92Schristos    and TDESC_DATA output parameters.  */
2142699b0f92Schristos 
21437d62b00eSchristos static bool
2144699b0f92Schristos arc_tdesc_init (struct gdbarch_info info, const struct target_desc **tdesc,
2145*6881a400Schristos 		tdesc_arch_data_up *tdesc_data)
2146699b0f92Schristos {
21477d62b00eSchristos   const struct target_desc *tdesc_loc = info.target_desc;
2148*6881a400Schristos   arc_debug_printf ("Target description initialization.");
2149699b0f92Schristos 
21507d62b00eSchristos   /* If target doesn't provide a description, use the default ones.  */
2151699b0f92Schristos   if (!tdesc_has_registers (tdesc_loc))
2152699b0f92Schristos     {
2153*6881a400Schristos       arc_arch_features features
2154*6881a400Schristos 	= arc_arch_features_create (info.abfd,
21557d62b00eSchristos 				    info.bfd_arch_info->mach);
21567d62b00eSchristos       tdesc_loc = arc_lookup_target_description (features);
21577d62b00eSchristos     }
21587d62b00eSchristos   gdb_assert (tdesc_loc != nullptr);
21597d62b00eSchristos 
2160*6881a400Schristos   arc_debug_printf ("Have got a target description");
21617d62b00eSchristos 
21627d62b00eSchristos   const struct tdesc_feature *feature_core
21637d62b00eSchristos     = tdesc_find_feature (tdesc_loc, ARC_CORE_FEATURE_NAME);
21647d62b00eSchristos   const struct tdesc_feature *feature_aux
21657d62b00eSchristos     = tdesc_find_feature (tdesc_loc, ARC_AUX_FEATURE_NAME);
21667d62b00eSchristos 
21677d62b00eSchristos   /* Maybe there still is a chance to salvage the input.  */
21687d62b00eSchristos   if (feature_core == nullptr)
21697d62b00eSchristos     feature_core = find_obsolete_core_names (tdesc_loc);
21707d62b00eSchristos   if (feature_aux == nullptr)
21717d62b00eSchristos     feature_aux = find_obsolete_aux_names (tdesc_loc);
21727d62b00eSchristos 
21737d62b00eSchristos   if (feature_core == nullptr)
2174699b0f92Schristos     {
21757d62b00eSchristos       arc_print (_("Error: Cannot find required feature '%s' in supplied "
21767d62b00eSchristos 		   "target description.\n"), ARC_CORE_FEATURE_NAME);
21777d62b00eSchristos       return false;
2178699b0f92Schristos     }
2179699b0f92Schristos 
21807d62b00eSchristos   if (feature_aux == nullptr)
2181699b0f92Schristos     {
21827d62b00eSchristos       arc_print (_("Error: Cannot find required feature '%s' in supplied "
21837d62b00eSchristos 		   "target description.\n"), ARC_AUX_FEATURE_NAME);
21847d62b00eSchristos       return false;
2185699b0f92Schristos     }
2186699b0f92Schristos 
21877d62b00eSchristos   const arc_register_feature *arc_core_reg_feature
21887d62b00eSchristos     = determine_core_reg_feature_set (info.bfd_arch_info->mach);
21897d62b00eSchristos   const arc_register_feature *arc_aux_reg_feature
21907d62b00eSchristos     = determine_aux_reg_feature_set ();
2191699b0f92Schristos 
2192*6881a400Schristos   tdesc_arch_data_up tdesc_data_loc = tdesc_data_alloc ();
2193699b0f92Schristos 
21947d62b00eSchristos   arc_update_acc_reg_names (info.byte_order);
2195699b0f92Schristos 
2196*6881a400Schristos   bool valid_p = arc_check_tdesc_feature (tdesc_data_loc.get (),
21977d62b00eSchristos 					  feature_core,
21987d62b00eSchristos 					  arc_core_reg_feature);
2199699b0f92Schristos 
2200*6881a400Schristos   valid_p &= arc_check_tdesc_feature (tdesc_data_loc.get (),
22017d62b00eSchristos 				      feature_aux,
22027d62b00eSchristos 				      arc_aux_reg_feature);
2203699b0f92Schristos 
2204699b0f92Schristos   if (!valid_p)
2205699b0f92Schristos     {
2206*6881a400Schristos       arc_debug_printf ("Target description is not valid");
22077d62b00eSchristos       return false;
2208699b0f92Schristos     }
2209699b0f92Schristos 
2210699b0f92Schristos   *tdesc = tdesc_loc;
2211*6881a400Schristos   *tdesc_data = std::move (tdesc_data_loc);
2212699b0f92Schristos 
22137d62b00eSchristos   return true;
2214699b0f92Schristos }
2215699b0f92Schristos 
22167f2ac410Schristos /* Implement the type_align gdbarch function.  */
22177f2ac410Schristos 
22187f2ac410Schristos static ULONGEST
22197f2ac410Schristos arc_type_align (struct gdbarch *gdbarch, struct type *type)
22207f2ac410Schristos {
22217d62b00eSchristos   switch (type->code ())
22227d62b00eSchristos     {
22237d62b00eSchristos     case TYPE_CODE_PTR:
22247d62b00eSchristos     case TYPE_CODE_FUNC:
22257d62b00eSchristos     case TYPE_CODE_FLAGS:
22267d62b00eSchristos     case TYPE_CODE_INT:
22277d62b00eSchristos     case TYPE_CODE_RANGE:
22287d62b00eSchristos     case TYPE_CODE_FLT:
22297d62b00eSchristos     case TYPE_CODE_ENUM:
22307d62b00eSchristos     case TYPE_CODE_REF:
22317d62b00eSchristos     case TYPE_CODE_RVALUE_REF:
22327d62b00eSchristos     case TYPE_CODE_CHAR:
22337d62b00eSchristos     case TYPE_CODE_BOOL:
22347d62b00eSchristos     case TYPE_CODE_DECFLOAT:
22357d62b00eSchristos     case TYPE_CODE_METHODPTR:
22367d62b00eSchristos     case TYPE_CODE_MEMBERPTR:
22377f2ac410Schristos       type = check_typedef (type);
2238*6881a400Schristos       return std::min<ULONGEST> (4, type->length ());
22397d62b00eSchristos     default:
22407d62b00eSchristos       return 0;
22417d62b00eSchristos     }
22427f2ac410Schristos }
22437f2ac410Schristos 
2244699b0f92Schristos /* Implement the "init" gdbarch method.  */
2245699b0f92Schristos 
2246699b0f92Schristos static struct gdbarch *
2247699b0f92Schristos arc_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2248699b0f92Schristos {
2249699b0f92Schristos   const struct target_desc *tdesc;
2250*6881a400Schristos   tdesc_arch_data_up tdesc_data;
2251699b0f92Schristos 
2252*6881a400Schristos   arc_debug_printf ("Architecture initialization.");
2253699b0f92Schristos 
2254699b0f92Schristos   if (!arc_tdesc_init (info, &tdesc, &tdesc_data))
22557d62b00eSchristos     return nullptr;
2256699b0f92Schristos 
2257699b0f92Schristos   /* Allocate the ARC-private target-dependent information structure, and the
2258699b0f92Schristos      GDB target-independent information structure.  */
2259*6881a400Schristos   std::unique_ptr<arc_gdbarch_tdep> tdep_holder (new arc_gdbarch_tdep);
2260*6881a400Schristos   arc_gdbarch_tdep *tdep = tdep_holder.get ();
2261699b0f92Schristos   tdep->jb_pc = -1; /* No longjmp support by default.  */
2262*6881a400Schristos   tdep->has_hw_loops = arc_check_for_hw_loops (tdesc, tdesc_data.get ());
2263*6881a400Schristos   struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep_holder.release ());
2264699b0f92Schristos 
2265699b0f92Schristos   /* Data types.  */
2266699b0f92Schristos   set_gdbarch_short_bit (gdbarch, 16);
2267699b0f92Schristos   set_gdbarch_int_bit (gdbarch, 32);
2268699b0f92Schristos   set_gdbarch_long_bit (gdbarch, 32);
2269699b0f92Schristos   set_gdbarch_long_long_bit (gdbarch, 64);
22707f2ac410Schristos   set_gdbarch_type_align (gdbarch, arc_type_align);
2271699b0f92Schristos   set_gdbarch_float_bit (gdbarch, 32);
2272699b0f92Schristos   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
2273699b0f92Schristos   set_gdbarch_double_bit (gdbarch, 64);
2274699b0f92Schristos   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
2275699b0f92Schristos   set_gdbarch_ptr_bit (gdbarch, 32);
2276699b0f92Schristos   set_gdbarch_addr_bit (gdbarch, 32);
2277699b0f92Schristos   set_gdbarch_char_signed (gdbarch, 0);
2278699b0f92Schristos 
2279699b0f92Schristos   set_gdbarch_write_pc (gdbarch, arc_write_pc);
2280699b0f92Schristos 
2281699b0f92Schristos   set_gdbarch_virtual_frame_pointer (gdbarch, arc_virtual_frame_pointer);
2282699b0f92Schristos 
2283699b0f92Schristos   /* tdesc_use_registers expects gdbarch_num_regs to return number of registers
2284699b0f92Schristos      parsed by gdbarch_init, and then it will add all of the remaining
2285699b0f92Schristos      registers and will increase number of registers.  */
2286699b0f92Schristos   set_gdbarch_num_regs (gdbarch, ARC_LAST_REGNUM + 1);
2287699b0f92Schristos   set_gdbarch_num_pseudo_regs (gdbarch, 0);
2288699b0f92Schristos   set_gdbarch_sp_regnum (gdbarch, ARC_SP_REGNUM);
2289699b0f92Schristos   set_gdbarch_pc_regnum (gdbarch, ARC_PC_REGNUM);
2290699b0f92Schristos   set_gdbarch_ps_regnum (gdbarch, ARC_STATUS32_REGNUM);
2291699b0f92Schristos   set_gdbarch_fp0_regnum (gdbarch, -1);	/* No FPU registers.  */
2292699b0f92Schristos 
2293699b0f92Schristos   set_gdbarch_push_dummy_call (gdbarch, arc_push_dummy_call);
2294699b0f92Schristos   set_gdbarch_push_dummy_code (gdbarch, arc_push_dummy_code);
2295699b0f92Schristos 
2296699b0f92Schristos   set_gdbarch_cannot_fetch_register (gdbarch, arc_cannot_fetch_register);
2297699b0f92Schristos   set_gdbarch_cannot_store_register (gdbarch, arc_cannot_store_register);
2298699b0f92Schristos 
2299699b0f92Schristos   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2300699b0f92Schristos 
2301699b0f92Schristos   set_gdbarch_return_value (gdbarch, arc_return_value);
2302699b0f92Schristos 
2303699b0f92Schristos   set_gdbarch_skip_prologue (gdbarch, arc_skip_prologue);
2304699b0f92Schristos   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2305699b0f92Schristos 
2306699b0f92Schristos   set_gdbarch_breakpoint_kind_from_pc (gdbarch, arc_breakpoint_kind_from_pc);
2307699b0f92Schristos   set_gdbarch_sw_breakpoint_from_kind (gdbarch, arc_sw_breakpoint_from_kind);
2308699b0f92Schristos 
2309699b0f92Schristos   /* On ARC 600 BRK_S instruction advances PC, unlike other ARC cores.  */
2310699b0f92Schristos   if (!arc_mach_is_arc600 (gdbarch))
2311699b0f92Schristos     set_gdbarch_decr_pc_after_break (gdbarch, 0);
2312699b0f92Schristos   else
2313699b0f92Schristos     set_gdbarch_decr_pc_after_break (gdbarch, 2);
2314699b0f92Schristos 
2315699b0f92Schristos   set_gdbarch_frame_align (gdbarch, arc_frame_align);
2316699b0f92Schristos 
2317699b0f92Schristos   set_gdbarch_print_insn (gdbarch, arc_delayed_print_insn);
2318699b0f92Schristos 
2319699b0f92Schristos   set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
2320699b0f92Schristos 
2321699b0f92Schristos   /* "nonsteppable" watchpoint means that watchpoint triggers before
2322699b0f92Schristos      instruction is committed, therefore it is required to remove watchpoint
2323699b0f92Schristos      to step though instruction that triggers it.  ARC watchpoints trigger
2324699b0f92Schristos      only after instruction is committed, thus there is no need to remove
2325699b0f92Schristos      them.  In fact on ARC watchpoint for memory writes may trigger with more
2326699b0f92Schristos      significant delay, like one or two instructions, depending on type of
2327699b0f92Schristos      memory where write is performed (CCM or external) and next instruction
2328699b0f92Schristos      after the memory write.  */
2329699b0f92Schristos   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 0);
2330699b0f92Schristos 
2331699b0f92Schristos   /* This doesn't include possible long-immediate value.  */
2332699b0f92Schristos   set_gdbarch_max_insn_length (gdbarch, 4);
2333699b0f92Schristos 
2334699b0f92Schristos   /* Frame unwinders and sniffers.  */
2335699b0f92Schristos   dwarf2_frame_set_init_reg (gdbarch, arc_dwarf2_frame_init_reg);
2336699b0f92Schristos   dwarf2_append_unwinders (gdbarch);
2337*6881a400Schristos   frame_unwind_append_unwinder (gdbarch, &arc_sigtramp_frame_unwind);
2338699b0f92Schristos   frame_unwind_append_unwinder (gdbarch, &arc_frame_unwind);
2339699b0f92Schristos   frame_base_set_default (gdbarch, &arc_normal_base);
2340699b0f92Schristos 
2341699b0f92Schristos   /* Setup stuff specific to a particular environment (baremetal or Linux).
2342699b0f92Schristos      It can override functions set earlier.  */
2343699b0f92Schristos   gdbarch_init_osabi (info, gdbarch);
2344699b0f92Schristos 
2345*6881a400Schristos   if (tdep->jb_pc >= 0)
2346699b0f92Schristos     set_gdbarch_get_longjmp_target (gdbarch, arc_get_longjmp_target);
2347699b0f92Schristos 
23487f2ac410Schristos   /* Disassembler options.  Enforce CPU if it was specified in XML target
23497f2ac410Schristos      description, otherwise use default method of determining CPU (ELF private
23507f2ac410Schristos      header).  */
23517f2ac410Schristos   if (info.target_desc != NULL)
23527f2ac410Schristos     {
23537f2ac410Schristos       const struct bfd_arch_info *tdesc_arch
23547f2ac410Schristos 	= tdesc_architecture (info.target_desc);
23557f2ac410Schristos       if (tdesc_arch != NULL)
23567f2ac410Schristos 	{
23577f2ac410Schristos 	  xfree (arc_disassembler_options);
23587f2ac410Schristos 	  /* FIXME: It is not really good to change disassembler options
23597f2ac410Schristos 	     behind the scene, because that might override options
23607f2ac410Schristos 	     specified by the user.  However as of now ARC doesn't support
23617f2ac410Schristos 	     `set disassembler-options' hence this code is the only place
23627f2ac410Schristos 	     where options are changed.  It also changes options for all
23637f2ac410Schristos 	     existing gdbarches, which also can be problematic, if
23647f2ac410Schristos 	     arc_gdbarch_init will start reusing existing gdbarch
23657f2ac410Schristos 	     instances.  */
23667f2ac410Schristos 	  /* Target description specifies a BFD architecture, which is
23677f2ac410Schristos 	     different from ARC cpu, as accepted by disassembler (and most
23687f2ac410Schristos 	     other ARC tools), because cpu values are much more fine grained -
23697f2ac410Schristos 	     there can be multiple cpu values per single BFD architecture.  As
23707f2ac410Schristos 	     a result this code should translate architecture to some cpu
23717f2ac410Schristos 	     value.  Since there is no info on exact cpu configuration, it is
23727f2ac410Schristos 	     best to use the most feature-rich CPU, so that disassembler will
23737f2ac410Schristos 	     recognize all instructions available to the specified
23747f2ac410Schristos 	     architecture.  */
23757f2ac410Schristos 	  switch (tdesc_arch->mach)
23767f2ac410Schristos 	    {
23777f2ac410Schristos 	    case bfd_mach_arc_arc601:
23787f2ac410Schristos 	      arc_disassembler_options = xstrdup ("cpu=arc601");
23797f2ac410Schristos 	      break;
23807f2ac410Schristos 	    case bfd_mach_arc_arc600:
23817f2ac410Schristos 	      arc_disassembler_options = xstrdup ("cpu=arc600");
23827f2ac410Schristos 	      break;
23837f2ac410Schristos 	    case bfd_mach_arc_arc700:
23847f2ac410Schristos 	      arc_disassembler_options = xstrdup ("cpu=arc700");
23857f2ac410Schristos 	      break;
23867f2ac410Schristos 	    case bfd_mach_arc_arcv2:
23877f2ac410Schristos 	      /* Machine arcv2 has three arches: ARCv2, EM and HS; where ARCv2
23887f2ac410Schristos 		 is treated as EM.  */
23897f2ac410Schristos 	      if (arc_arch_is_hs (tdesc_arch))
23907f2ac410Schristos 		arc_disassembler_options = xstrdup ("cpu=hs38_linux");
23917f2ac410Schristos 	      else
23927f2ac410Schristos 		arc_disassembler_options = xstrdup ("cpu=em4_fpuda");
23937f2ac410Schristos 	      break;
23947f2ac410Schristos 	    default:
23957f2ac410Schristos 	      arc_disassembler_options = NULL;
23967f2ac410Schristos 	      break;
23977f2ac410Schristos 	    }
23987f2ac410Schristos 	}
23997f2ac410Schristos     }
24007f2ac410Schristos 
2401*6881a400Schristos   set_gdbarch_disassembler_options (gdbarch, &arc_disassembler_options);
2402*6881a400Schristos   set_gdbarch_valid_disassembler_options (gdbarch,
2403*6881a400Schristos 					  disassembler_options_arc ());
2404*6881a400Schristos 
2405*6881a400Schristos   tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
2406699b0f92Schristos 
2407699b0f92Schristos   return gdbarch;
2408699b0f92Schristos }
2409699b0f92Schristos 
2410699b0f92Schristos /* Implement the "dump_tdep" gdbarch method.  */
2411699b0f92Schristos 
2412699b0f92Schristos static void
2413699b0f92Schristos arc_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
2414699b0f92Schristos {
2415*6881a400Schristos   arc_gdbarch_tdep *tdep = gdbarch_tdep<arc_gdbarch_tdep> (gdbarch);
2416699b0f92Schristos 
2417*6881a400Schristos   gdb_printf (file, "arc_dump_tdep: jb_pc = %i\n", tdep->jb_pc);
2418*6881a400Schristos 
2419*6881a400Schristos   gdb_printf (file, "arc_dump_tdep: is_sigtramp = <%s>\n",
2420*6881a400Schristos 	      host_address_to_string (tdep->is_sigtramp));
2421*6881a400Schristos   gdb_printf (file, "arc_dump_tdep: sigcontext_addr = <%s>\n",
2422*6881a400Schristos 	      host_address_to_string (tdep->sigcontext_addr));
2423*6881a400Schristos   gdb_printf (file, "arc_dump_tdep: sc_reg_offset = <%s>\n",
2424*6881a400Schristos 	      host_address_to_string (tdep->sc_reg_offset));
2425*6881a400Schristos   gdb_printf (file, "arc_dump_tdep: sc_num_regs = %d\n",
2426*6881a400Schristos 	      tdep->sc_num_regs);
2427699b0f92Schristos }
2428699b0f92Schristos 
2429699b0f92Schristos /* This command accepts single argument - address of instruction to
2430699b0f92Schristos    disassemble.  */
2431699b0f92Schristos 
2432699b0f92Schristos static void
24337f2ac410Schristos dump_arc_instruction_command (const char *args, int from_tty)
2434699b0f92Schristos {
2435699b0f92Schristos   struct value *val;
2436699b0f92Schristos   if (args != NULL && strlen (args) > 0)
2437699b0f92Schristos     val = evaluate_expression (parse_expression (args).get ());
2438699b0f92Schristos   else
2439699b0f92Schristos     val = access_value_history (0);
2440699b0f92Schristos   record_latest_value (val);
2441699b0f92Schristos 
2442699b0f92Schristos   CORE_ADDR address = value_as_address (val);
2443699b0f92Schristos   struct arc_instruction insn;
2444*6881a400Schristos   struct gdb_non_printing_memory_disassembler dis (target_gdbarch ());
2445*6881a400Schristos   arc_insn_decode (address, dis.disasm_info (), arc_delayed_print_insn, &insn);
2446699b0f92Schristos   arc_insn_dump (insn);
2447699b0f92Schristos }
2448699b0f92Schristos 
24497d62b00eSchristos void _initialize_arc_tdep ();
2450699b0f92Schristos void
24517d62b00eSchristos _initialize_arc_tdep ()
2452699b0f92Schristos {
2453699b0f92Schristos   gdbarch_register (bfd_arch_arc, arc_gdbarch_init, arc_dump_tdep);
2454699b0f92Schristos 
2455699b0f92Schristos   /* Register ARC-specific commands with gdb.  */
2456699b0f92Schristos 
2457699b0f92Schristos   /* Add root prefix command for "maintenance print arc" commands.  */
2458*6881a400Schristos   add_basic_prefix_cmd ("arc", class_maintenance,
2459699b0f92Schristos 			_("ARC-specific maintenance commands for printing GDB "
2460699b0f92Schristos 			  "internal state."),
2461*6881a400Schristos 			&maintenance_print_arc_list,
24627d62b00eSchristos 			0, &maintenanceprintlist);
2463699b0f92Schristos 
2464699b0f92Schristos   add_cmd ("arc-instruction", class_maintenance,
2465699b0f92Schristos 	   dump_arc_instruction_command,
2466699b0f92Schristos 	   _("Dump arc_instruction structure for specified address."),
2467699b0f92Schristos 	   &maintenance_print_arc_list);
2468699b0f92Schristos 
2469699b0f92Schristos   /* Debug internals for ARC GDB.  */
2470*6881a400Schristos   add_setshow_boolean_cmd ("arc", class_maintenance,
2471699b0f92Schristos 			   &arc_debug,
2472699b0f92Schristos 			   _("Set ARC specific debugging."),
2473699b0f92Schristos 			   _("Show ARC specific debugging."),
2474*6881a400Schristos 			   _("When set, ARC specific debugging is enabled."),
2475699b0f92Schristos 			   NULL, NULL, &setdebuglist, &showdebuglist);
2476699b0f92Schristos }
2477