xref: /netbsd/external/gpl3/gdb.old/dist/gdb/arc-tdep.c (revision 56bb7041)
1*56bb7041Schristos /* Target dependent code for ARC architecture, for GDB.
2e88b412bSchristos 
3*56bb7041Schristos    Copyright 2005-2020 Free Software Foundation, Inc.
4e88b412bSchristos    Contributed by Synopsys Inc.
5e88b412bSchristos 
6e88b412bSchristos    This file is part of GDB.
7e88b412bSchristos 
8e88b412bSchristos    This program is free software; you can redistribute it and/or modify
9e88b412bSchristos    it under the terms of the GNU General Public License as published by
10e88b412bSchristos    the Free Software Foundation; either version 3 of the License, or
11e88b412bSchristos    (at your option) any later version.
12e88b412bSchristos 
13e88b412bSchristos    This program is distributed in the hope that it will be useful,
14e88b412bSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
15e88b412bSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16e88b412bSchristos    GNU General Public License for more details.
17e88b412bSchristos 
18e88b412bSchristos    You should have received a copy of the GNU General Public License
19e88b412bSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20e88b412bSchristos 
21e88b412bSchristos /* GDB header files.  */
22e88b412bSchristos #include "defs.h"
23e88b412bSchristos #include "arch-utils.h"
24*56bb7041Schristos #include "elf-bfd.h"
25e88b412bSchristos #include "disasm.h"
26*56bb7041Schristos #include "dwarf2/frame.h"
27e88b412bSchristos #include "frame-base.h"
28e88b412bSchristos #include "frame-unwind.h"
29e88b412bSchristos #include "gdbcore.h"
30e88b412bSchristos #include "gdbcmd.h"
31e88b412bSchristos #include "objfiles.h"
32*56bb7041Schristos #include "osabi.h"
33e88b412bSchristos #include "prologue-value.h"
34*56bb7041Schristos #include "target-descriptions.h"
35e88b412bSchristos #include "trad-frame.h"
36e88b412bSchristos 
37e88b412bSchristos /* ARC header files.  */
38e88b412bSchristos #include "opcode/arc.h"
39*56bb7041Schristos #include "opcodes/arc-dis.h"
40e88b412bSchristos #include "arc-tdep.h"
41*56bb7041Schristos #include "arch/arc.h"
42e88b412bSchristos 
43e88b412bSchristos /* Standard headers.  */
44e88b412bSchristos #include <algorithm>
45*56bb7041Schristos #include <sstream>
46e88b412bSchristos 
47e88b412bSchristos /* The frame unwind cache for ARC.  */
48e88b412bSchristos 
49e88b412bSchristos struct arc_frame_cache
50e88b412bSchristos {
51e88b412bSchristos   /* The stack pointer at the time this frame was created; i.e. the caller's
52e88b412bSchristos      stack pointer when this function was called.  It is used to identify this
53e88b412bSchristos      frame.  */
54e88b412bSchristos   CORE_ADDR prev_sp;
55e88b412bSchristos 
56e88b412bSchristos   /* Register that is a base for this frame - FP for normal frame, SP for
57e88b412bSchristos      non-FP frames.  */
58e88b412bSchristos   int frame_base_reg;
59e88b412bSchristos 
60e88b412bSchristos   /* Offset from the previous SP to the current frame base.  If GCC uses
61e88b412bSchristos      `SUB SP,SP,offset` to allocate space for local variables, then it will be
62e88b412bSchristos      done after setting up a frame pointer, but it still will be considered
63e88b412bSchristos      part of prologue, therefore SP will be lesser than FP at the end of the
64e88b412bSchristos      prologue analysis.  In this case that would be an offset from old SP to a
65e88b412bSchristos      new FP.  But in case of non-FP frames, frame base is an SP and thus that
66e88b412bSchristos      would be an offset from old SP to new SP.  What is important is that this
67e88b412bSchristos      is an offset from old SP to a known register, so it can be used to find
68e88b412bSchristos      old SP.
69e88b412bSchristos 
70e88b412bSchristos      Using FP is preferable, when possible, because SP can change in function
71e88b412bSchristos      body after prologue due to alloca, variadic arguments or other shenanigans.
72e88b412bSchristos      If that is the case in the caller frame, then PREV_SP will point to SP at
73e88b412bSchristos      the moment of function call, but it will be different from SP value at the
74e88b412bSchristos      end of the caller prologue.  As a result it will not be possible to
75e88b412bSchristos      reconstruct caller's frame and go past it in the backtrace.  Those things
76e88b412bSchristos      are unlikely to happen to FP - FP value at the moment of function call (as
77e88b412bSchristos      stored on stack in callee prologue) is also an FP value at the end of the
78e88b412bSchristos      caller's prologue.  */
79e88b412bSchristos 
80e88b412bSchristos   LONGEST frame_base_offset;
81e88b412bSchristos 
82e88b412bSchristos   /* Store addresses for registers saved in prologue.  During prologue analysis
83e88b412bSchristos      GDB stores offsets relatively to "old SP", then after old SP is evaluated,
84e88b412bSchristos      offsets are replaced with absolute addresses.  */
85e88b412bSchristos   struct trad_frame_saved_reg *saved_regs;
86e88b412bSchristos };
87e88b412bSchristos 
88e88b412bSchristos /* Global debug flag.  */
89e88b412bSchristos 
90e88b412bSchristos int arc_debug;
91e88b412bSchristos 
92e88b412bSchristos /* List of "maintenance print arc" commands.  */
93e88b412bSchristos 
94e88b412bSchristos static struct cmd_list_element *maintenance_print_arc_list = NULL;
95e88b412bSchristos 
96*56bb7041Schristos /* A set of registers that we expect to find in a tdesc_feature.  These
97*56bb7041Schristos    are used in ARC_TDESC_INIT when processing the target description.  */
98e88b412bSchristos 
99*56bb7041Schristos struct arc_register_feature
100*56bb7041Schristos {
101*56bb7041Schristos   /* Information for a single register.  */
102*56bb7041Schristos   struct register_info
103*56bb7041Schristos   {
104*56bb7041Schristos     /* The GDB register number for this register.  */
105*56bb7041Schristos     int regnum;
106e88b412bSchristos 
107*56bb7041Schristos     /* List of names for this register.  The first name in this list is the
108*56bb7041Schristos        preferred name, the name GDB will use when describing this register.  */
109*56bb7041Schristos     std::vector<const char *> names;
110e88b412bSchristos 
111*56bb7041Schristos     /* When true, this register must be present in this feature set.  */
112*56bb7041Schristos     bool required_p;
113e88b412bSchristos   };
114e88b412bSchristos 
115*56bb7041Schristos   /* The name for this feature.  This is the name used to find this feature
116*56bb7041Schristos      within the target description.  */
117*56bb7041Schristos   const char *name;
118*56bb7041Schristos 
119*56bb7041Schristos   /* List of all the registers that we expect to encounter in this register
120*56bb7041Schristos      set.  */
121*56bb7041Schristos   std::vector<struct register_info> registers;
122e88b412bSchristos };
123e88b412bSchristos 
124*56bb7041Schristos /* Obsolete feature names for backward compatibility.  */
125*56bb7041Schristos static const char *ARC_CORE_V1_OBSOLETE_FEATURE_NAME
126*56bb7041Schristos   = "org.gnu.gdb.arc.core.arcompact";
127*56bb7041Schristos static const char *ARC_CORE_V2_OBSOLETE_FEATURE_NAME
128*56bb7041Schristos   = "org.gnu.gdb.arc.core.v2";
129*56bb7041Schristos static const char *ARC_CORE_V2_REDUCED_OBSOLETE_FEATURE_NAME
130*56bb7041Schristos   = "org.gnu.gdb.arc.core-reduced.v2";
131*56bb7041Schristos static const char *ARC_AUX_OBSOLETE_FEATURE_NAME
132*56bb7041Schristos   = "org.gnu.gdb.arc.aux-minimal";
133*56bb7041Schristos /* Modern feature names.  */
134*56bb7041Schristos static const char *ARC_CORE_FEATURE_NAME = "org.gnu.gdb.arc.core";
135*56bb7041Schristos static const char *ARC_AUX_FEATURE_NAME = "org.gnu.gdb.arc.aux";
136*56bb7041Schristos 
137*56bb7041Schristos /* ARCv1 (ARC600, ARC601, ARC700) general core registers feature set.
138*56bb7041Schristos    See also arc_update_acc_reg_names() for "accl/acch" names.  */
139*56bb7041Schristos 
140*56bb7041Schristos static struct arc_register_feature arc_v1_core_reg_feature =
141*56bb7041Schristos {
142*56bb7041Schristos   ARC_CORE_FEATURE_NAME,
143*56bb7041Schristos   {
144*56bb7041Schristos     { ARC_R0_REGNUM + 0, { "r0" }, true },
145*56bb7041Schristos     { ARC_R0_REGNUM + 1, { "r1" }, true },
146*56bb7041Schristos     { ARC_R0_REGNUM + 2, { "r2" }, true },
147*56bb7041Schristos     { ARC_R0_REGNUM + 3, { "r3" }, true },
148*56bb7041Schristos     { ARC_R0_REGNUM + 4, { "r4" }, false },
149*56bb7041Schristos     { ARC_R0_REGNUM + 5, { "r5" }, false },
150*56bb7041Schristos     { ARC_R0_REGNUM + 6, { "r6" }, false },
151*56bb7041Schristos     { ARC_R0_REGNUM + 7, { "r7" }, false },
152*56bb7041Schristos     { ARC_R0_REGNUM + 8, { "r8" }, false },
153*56bb7041Schristos     { ARC_R0_REGNUM + 9, { "r9" }, false },
154*56bb7041Schristos     { ARC_R0_REGNUM + 10, { "r10" }, true },
155*56bb7041Schristos     { ARC_R0_REGNUM + 11, { "r11" }, true },
156*56bb7041Schristos     { ARC_R0_REGNUM + 12, { "r12" }, true },
157*56bb7041Schristos     { ARC_R0_REGNUM + 13, { "r13" }, true },
158*56bb7041Schristos     { ARC_R0_REGNUM + 14, { "r14" }, true },
159*56bb7041Schristos     { ARC_R0_REGNUM + 15, { "r15" }, true },
160*56bb7041Schristos     { ARC_R0_REGNUM + 16, { "r16" }, false },
161*56bb7041Schristos     { ARC_R0_REGNUM + 17, { "r17" }, false },
162*56bb7041Schristos     { ARC_R0_REGNUM + 18, { "r18" }, false },
163*56bb7041Schristos     { ARC_R0_REGNUM + 19, { "r19" }, false },
164*56bb7041Schristos     { ARC_R0_REGNUM + 20, { "r20" }, false },
165*56bb7041Schristos     { ARC_R0_REGNUM + 21, { "r21" }, false },
166*56bb7041Schristos     { ARC_R0_REGNUM + 22, { "r22" }, false },
167*56bb7041Schristos     { ARC_R0_REGNUM + 23, { "r23" }, false },
168*56bb7041Schristos     { ARC_R0_REGNUM + 24, { "r24" }, false },
169*56bb7041Schristos     { ARC_R0_REGNUM + 25, { "r25" }, false },
170*56bb7041Schristos     { ARC_R0_REGNUM + 26, { "gp" }, true },
171*56bb7041Schristos     { ARC_R0_REGNUM + 27, { "fp" }, true },
172*56bb7041Schristos     { ARC_R0_REGNUM + 28, { "sp" }, true },
173*56bb7041Schristos     { ARC_R0_REGNUM + 29, { "ilink1" }, false },
174*56bb7041Schristos     { ARC_R0_REGNUM + 30, { "ilink2" }, false },
175*56bb7041Schristos     { ARC_R0_REGNUM + 31, { "blink" }, true },
176*56bb7041Schristos     { ARC_R0_REGNUM + 32, { "r32" }, false },
177*56bb7041Schristos     { ARC_R0_REGNUM + 33, { "r33" }, false },
178*56bb7041Schristos     { ARC_R0_REGNUM + 34, { "r34" }, false },
179*56bb7041Schristos     { ARC_R0_REGNUM + 35, { "r35" }, false },
180*56bb7041Schristos     { ARC_R0_REGNUM + 36, { "r36" }, false },
181*56bb7041Schristos     { ARC_R0_REGNUM + 37, { "r37" }, false },
182*56bb7041Schristos     { ARC_R0_REGNUM + 38, { "r38" }, false },
183*56bb7041Schristos     { ARC_R0_REGNUM + 39, { "r39" }, false },
184*56bb7041Schristos     { ARC_R0_REGNUM + 40, { "r40" }, false },
185*56bb7041Schristos     { ARC_R0_REGNUM + 41, { "r41" }, false },
186*56bb7041Schristos     { ARC_R0_REGNUM + 42, { "r42" }, false },
187*56bb7041Schristos     { ARC_R0_REGNUM + 43, { "r43" }, false },
188*56bb7041Schristos     { ARC_R0_REGNUM + 44, { "r44" }, false },
189*56bb7041Schristos     { ARC_R0_REGNUM + 45, { "r45" }, false },
190*56bb7041Schristos     { ARC_R0_REGNUM + 46, { "r46" }, false },
191*56bb7041Schristos     { ARC_R0_REGNUM + 47, { "r47" }, false },
192*56bb7041Schristos     { ARC_R0_REGNUM + 48, { "r48" }, false },
193*56bb7041Schristos     { ARC_R0_REGNUM + 49, { "r49" }, false },
194*56bb7041Schristos     { ARC_R0_REGNUM + 50, { "r50" }, false },
195*56bb7041Schristos     { ARC_R0_REGNUM + 51, { "r51" }, false },
196*56bb7041Schristos     { ARC_R0_REGNUM + 52, { "r52" }, false },
197*56bb7041Schristos     { ARC_R0_REGNUM + 53, { "r53" }, false },
198*56bb7041Schristos     { ARC_R0_REGNUM + 54, { "r54" }, false },
199*56bb7041Schristos     { ARC_R0_REGNUM + 55, { "r55" }, false },
200*56bb7041Schristos     { ARC_R0_REGNUM + 56, { "r56" }, false },
201*56bb7041Schristos     { ARC_R0_REGNUM + 57, { "r57" }, false },
202*56bb7041Schristos     { ARC_R0_REGNUM + 58, { "r58", "accl" }, false },
203*56bb7041Schristos     { ARC_R0_REGNUM + 59, { "r59", "acch" }, false },
204*56bb7041Schristos     { ARC_R0_REGNUM + 60, { "lp_count" }, false },
205*56bb7041Schristos     { ARC_R0_REGNUM + 61, { "reserved" }, false },
206*56bb7041Schristos     { ARC_R0_REGNUM + 62, { "limm" }, false },
207*56bb7041Schristos     { ARC_R0_REGNUM + 63, { "pcl" }, true }
208*56bb7041Schristos   }
209*56bb7041Schristos };
210*56bb7041Schristos 
211*56bb7041Schristos /* ARCv2 (ARCHS) general core registers feature set.  See also
212*56bb7041Schristos    arc_update_acc_reg_names() for "accl/acch" names.  */
213*56bb7041Schristos 
214*56bb7041Schristos static struct arc_register_feature arc_v2_core_reg_feature =
215*56bb7041Schristos {
216*56bb7041Schristos   ARC_CORE_FEATURE_NAME,
217*56bb7041Schristos   {
218*56bb7041Schristos     { ARC_R0_REGNUM + 0, { "r0" }, true },
219*56bb7041Schristos     { ARC_R0_REGNUM + 1, { "r1" }, true },
220*56bb7041Schristos     { ARC_R0_REGNUM + 2, { "r2" }, true },
221*56bb7041Schristos     { ARC_R0_REGNUM + 3, { "r3" }, true },
222*56bb7041Schristos     { ARC_R0_REGNUM + 4, { "r4" }, false },
223*56bb7041Schristos     { ARC_R0_REGNUM + 5, { "r5" }, false },
224*56bb7041Schristos     { ARC_R0_REGNUM + 6, { "r6" }, false },
225*56bb7041Schristos     { ARC_R0_REGNUM + 7, { "r7" }, false },
226*56bb7041Schristos     { ARC_R0_REGNUM + 8, { "r8" }, false },
227*56bb7041Schristos     { ARC_R0_REGNUM + 9, { "r9" }, false },
228*56bb7041Schristos     { ARC_R0_REGNUM + 10, { "r10" }, true },
229*56bb7041Schristos     { ARC_R0_REGNUM + 11, { "r11" }, true },
230*56bb7041Schristos     { ARC_R0_REGNUM + 12, { "r12" }, true },
231*56bb7041Schristos     { ARC_R0_REGNUM + 13, { "r13" }, true },
232*56bb7041Schristos     { ARC_R0_REGNUM + 14, { "r14" }, true },
233*56bb7041Schristos     { ARC_R0_REGNUM + 15, { "r15" }, true },
234*56bb7041Schristos     { ARC_R0_REGNUM + 16, { "r16" }, false },
235*56bb7041Schristos     { ARC_R0_REGNUM + 17, { "r17" }, false },
236*56bb7041Schristos     { ARC_R0_REGNUM + 18, { "r18" }, false },
237*56bb7041Schristos     { ARC_R0_REGNUM + 19, { "r19" }, false },
238*56bb7041Schristos     { ARC_R0_REGNUM + 20, { "r20" }, false },
239*56bb7041Schristos     { ARC_R0_REGNUM + 21, { "r21" }, false },
240*56bb7041Schristos     { ARC_R0_REGNUM + 22, { "r22" }, false },
241*56bb7041Schristos     { ARC_R0_REGNUM + 23, { "r23" }, false },
242*56bb7041Schristos     { ARC_R0_REGNUM + 24, { "r24" }, false },
243*56bb7041Schristos     { ARC_R0_REGNUM + 25, { "r25" }, false },
244*56bb7041Schristos     { ARC_R0_REGNUM + 26, { "gp" }, true },
245*56bb7041Schristos     { ARC_R0_REGNUM + 27, { "fp" }, true },
246*56bb7041Schristos     { ARC_R0_REGNUM + 28, { "sp" }, true },
247*56bb7041Schristos     { ARC_R0_REGNUM + 29, { "ilink" }, false },
248*56bb7041Schristos     { ARC_R0_REGNUM + 30, { "r30" }, true },
249*56bb7041Schristos     { ARC_R0_REGNUM + 31, { "blink" }, true },
250*56bb7041Schristos     { ARC_R0_REGNUM + 32, { "r32" }, false },
251*56bb7041Schristos     { ARC_R0_REGNUM + 33, { "r33" }, false },
252*56bb7041Schristos     { ARC_R0_REGNUM + 34, { "r34" }, false },
253*56bb7041Schristos     { ARC_R0_REGNUM + 35, { "r35" }, false },
254*56bb7041Schristos     { ARC_R0_REGNUM + 36, { "r36" }, false },
255*56bb7041Schristos     { ARC_R0_REGNUM + 37, { "r37" }, false },
256*56bb7041Schristos     { ARC_R0_REGNUM + 38, { "r38" }, false },
257*56bb7041Schristos     { ARC_R0_REGNUM + 39, { "r39" }, false },
258*56bb7041Schristos     { ARC_R0_REGNUM + 40, { "r40" }, false },
259*56bb7041Schristos     { ARC_R0_REGNUM + 41, { "r41" }, false },
260*56bb7041Schristos     { ARC_R0_REGNUM + 42, { "r42" }, false },
261*56bb7041Schristos     { ARC_R0_REGNUM + 43, { "r43" }, false },
262*56bb7041Schristos     { ARC_R0_REGNUM + 44, { "r44" }, false },
263*56bb7041Schristos     { ARC_R0_REGNUM + 45, { "r45" }, false },
264*56bb7041Schristos     { ARC_R0_REGNUM + 46, { "r46" }, false },
265*56bb7041Schristos     { ARC_R0_REGNUM + 47, { "r47" }, false },
266*56bb7041Schristos     { ARC_R0_REGNUM + 48, { "r48" }, false },
267*56bb7041Schristos     { ARC_R0_REGNUM + 49, { "r49" }, false },
268*56bb7041Schristos     { ARC_R0_REGNUM + 50, { "r50" }, false },
269*56bb7041Schristos     { ARC_R0_REGNUM + 51, { "r51" }, false },
270*56bb7041Schristos     { ARC_R0_REGNUM + 52, { "r52" }, false },
271*56bb7041Schristos     { ARC_R0_REGNUM + 53, { "r53" }, false },
272*56bb7041Schristos     { ARC_R0_REGNUM + 54, { "r54" }, false },
273*56bb7041Schristos     { ARC_R0_REGNUM + 55, { "r55" }, false },
274*56bb7041Schristos     { ARC_R0_REGNUM + 56, { "r56" }, false },
275*56bb7041Schristos     { ARC_R0_REGNUM + 57, { "r57" }, false },
276*56bb7041Schristos     { ARC_R0_REGNUM + 58, { "r58", "accl" }, false },
277*56bb7041Schristos     { ARC_R0_REGNUM + 59, { "r59", "acch" }, false },
278*56bb7041Schristos     { ARC_R0_REGNUM + 60, { "lp_count" }, false },
279*56bb7041Schristos     { ARC_R0_REGNUM + 61, { "reserved" }, false },
280*56bb7041Schristos     { ARC_R0_REGNUM + 62, { "limm" }, false },
281*56bb7041Schristos     { ARC_R0_REGNUM + 63, { "pcl" }, true }
282*56bb7041Schristos   }
283*56bb7041Schristos };
284*56bb7041Schristos 
285*56bb7041Schristos /* The common auxiliary registers feature set.  The REGNUM field
286*56bb7041Schristos    must match the ARC_REGNUM enum in arc-tdep.h.  */
287*56bb7041Schristos 
288*56bb7041Schristos static const struct arc_register_feature arc_common_aux_reg_feature =
289*56bb7041Schristos {
290*56bb7041Schristos   ARC_AUX_FEATURE_NAME,
291*56bb7041Schristos   {
292*56bb7041Schristos     { ARC_FIRST_AUX_REGNUM + 0, { "pc" }, true },
293*56bb7041Schristos     { ARC_FIRST_AUX_REGNUM + 1, { "status32" }, true },
294*56bb7041Schristos     { ARC_FIRST_AUX_REGNUM + 2, { "lp_start" }, false },
295*56bb7041Schristos     { ARC_FIRST_AUX_REGNUM + 3, { "lp_end" }, false },
296*56bb7041Schristos     { ARC_FIRST_AUX_REGNUM + 4, { "bta" }, false }
297*56bb7041Schristos   }
298e88b412bSchristos };
299e88b412bSchristos 
3003aed4a8bSchristos static char *arc_disassembler_options = NULL;
3013aed4a8bSchristos 
302e88b412bSchristos /* Functions are sorted in the order as they are used in the
303e88b412bSchristos    _initialize_arc_tdep (), which uses the same order as gdbarch.h.  Static
304e88b412bSchristos    functions are defined before the first invocation.  */
305e88b412bSchristos 
306e88b412bSchristos /* Returns an unsigned value of OPERAND_NUM in instruction INSN.
307e88b412bSchristos    For relative branch instructions returned value is an offset, not an actual
308e88b412bSchristos    branch target.  */
309e88b412bSchristos 
310e88b412bSchristos static ULONGEST
arc_insn_get_operand_value(const struct arc_instruction & insn,unsigned int operand_num)311e88b412bSchristos arc_insn_get_operand_value (const struct arc_instruction &insn,
312e88b412bSchristos 			    unsigned int operand_num)
313e88b412bSchristos {
314e88b412bSchristos   switch (insn.operands[operand_num].kind)
315e88b412bSchristos     {
316e88b412bSchristos     case ARC_OPERAND_KIND_LIMM:
317e88b412bSchristos       gdb_assert (insn.limm_p);
318e88b412bSchristos       return insn.limm_value;
319e88b412bSchristos     case ARC_OPERAND_KIND_SHIMM:
320e88b412bSchristos       return insn.operands[operand_num].value;
321e88b412bSchristos     default:
322e88b412bSchristos       /* Value in instruction is a register number.  */
323e88b412bSchristos       struct regcache *regcache = get_current_regcache ();
324e88b412bSchristos       ULONGEST value;
325e88b412bSchristos       regcache_cooked_read_unsigned (regcache,
326e88b412bSchristos 				     insn.operands[operand_num].value,
327e88b412bSchristos 				     &value);
328e88b412bSchristos       return value;
329e88b412bSchristos     }
330e88b412bSchristos }
331e88b412bSchristos 
332e88b412bSchristos /* Like arc_insn_get_operand_value, but returns a signed value.  */
333e88b412bSchristos 
334e88b412bSchristos static LONGEST
arc_insn_get_operand_value_signed(const struct arc_instruction & insn,unsigned int operand_num)335e88b412bSchristos arc_insn_get_operand_value_signed (const struct arc_instruction &insn,
336e88b412bSchristos 				   unsigned int operand_num)
337e88b412bSchristos {
338e88b412bSchristos   switch (insn.operands[operand_num].kind)
339e88b412bSchristos     {
340e88b412bSchristos     case ARC_OPERAND_KIND_LIMM:
341e88b412bSchristos       gdb_assert (insn.limm_p);
342e88b412bSchristos       /* Convert unsigned raw value to signed one.  This assumes 2's
343e88b412bSchristos 	 complement arithmetic, but so is the LONG_MIN value from generic
344e88b412bSchristos 	 defs.h and that assumption is true for ARC.  */
345e88b412bSchristos       gdb_static_assert (sizeof (insn.limm_value) == sizeof (int));
346e88b412bSchristos       return (((LONGEST) insn.limm_value) ^ INT_MIN) - INT_MIN;
347e88b412bSchristos     case ARC_OPERAND_KIND_SHIMM:
348e88b412bSchristos       /* Sign conversion has been done by binutils.  */
349e88b412bSchristos       return insn.operands[operand_num].value;
350e88b412bSchristos     default:
351e88b412bSchristos       /* Value in instruction is a register number.  */
352e88b412bSchristos       struct regcache *regcache = get_current_regcache ();
353e88b412bSchristos       LONGEST value;
354e88b412bSchristos       regcache_cooked_read_signed (regcache,
355e88b412bSchristos 				   insn.operands[operand_num].value,
356e88b412bSchristos 				   &value);
357e88b412bSchristos       return value;
358e88b412bSchristos     }
359e88b412bSchristos }
360e88b412bSchristos 
361e88b412bSchristos /* Get register with base address of memory operation.  */
362e88b412bSchristos 
363*56bb7041Schristos static int
arc_insn_get_memory_base_reg(const struct arc_instruction & insn)364e88b412bSchristos arc_insn_get_memory_base_reg (const struct arc_instruction &insn)
365e88b412bSchristos {
366e88b412bSchristos   /* POP_S and PUSH_S have SP as an implicit argument in a disassembler.  */
367e88b412bSchristos   if (insn.insn_class == PUSH || insn.insn_class == POP)
368e88b412bSchristos     return ARC_SP_REGNUM;
369e88b412bSchristos 
370e88b412bSchristos   gdb_assert (insn.insn_class == LOAD || insn.insn_class == STORE);
371e88b412bSchristos 
372e88b412bSchristos   /* Other instructions all have at least two operands: operand 0 is data,
373e88b412bSchristos      operand 1 is address.  Operand 2 is offset from address.  However, see
374e88b412bSchristos      comment to arc_instruction.operands - in some cases, third operand may be
375e88b412bSchristos      missing, namely if it is 0.  */
376e88b412bSchristos   gdb_assert (insn.operands_count >= 2);
377e88b412bSchristos   return insn.operands[1].value;
378e88b412bSchristos }
379e88b412bSchristos 
380e88b412bSchristos /* Get offset of a memory operation INSN.  */
381e88b412bSchristos 
382*56bb7041Schristos static CORE_ADDR
arc_insn_get_memory_offset(const struct arc_instruction & insn)383e88b412bSchristos arc_insn_get_memory_offset (const struct arc_instruction &insn)
384e88b412bSchristos {
385e88b412bSchristos   /* POP_S and PUSH_S have offset as an implicit argument in a
386e88b412bSchristos      disassembler.  */
387e88b412bSchristos   if (insn.insn_class == POP)
388e88b412bSchristos     return 4;
389e88b412bSchristos   else if (insn.insn_class == PUSH)
390e88b412bSchristos     return -4;
391e88b412bSchristos 
392e88b412bSchristos   gdb_assert (insn.insn_class == LOAD || insn.insn_class == STORE);
393e88b412bSchristos 
394e88b412bSchristos   /* Other instructions all have at least two operands: operand 0 is data,
395e88b412bSchristos      operand 1 is address.  Operand 2 is offset from address.  However, see
396e88b412bSchristos      comment to arc_instruction.operands - in some cases, third operand may be
397e88b412bSchristos      missing, namely if it is 0.  */
398e88b412bSchristos   if (insn.operands_count < 3)
399e88b412bSchristos     return 0;
400e88b412bSchristos 
401e88b412bSchristos   CORE_ADDR value = arc_insn_get_operand_value (insn, 2);
402e88b412bSchristos   /* Handle scaling.  */
403e88b412bSchristos   if (insn.writeback_mode == ARC_WRITEBACK_AS)
404e88b412bSchristos     {
405e88b412bSchristos       /* Byte data size is not valid for AS.  Halfword means shift by 1 bit.
406e88b412bSchristos 	 Word and double word means shift by 2 bits.  */
407e88b412bSchristos       gdb_assert (insn.data_size_mode != ARC_SCALING_B);
408e88b412bSchristos       if (insn.data_size_mode == ARC_SCALING_H)
409e88b412bSchristos 	value <<= 1;
410e88b412bSchristos       else
411e88b412bSchristos 	value <<= 2;
412e88b412bSchristos     }
413e88b412bSchristos   return value;
414e88b412bSchristos }
415e88b412bSchristos 
416e88b412bSchristos CORE_ADDR
arc_insn_get_branch_target(const struct arc_instruction & insn)417e88b412bSchristos arc_insn_get_branch_target (const struct arc_instruction &insn)
418e88b412bSchristos {
419e88b412bSchristos   gdb_assert (insn.is_control_flow);
420e88b412bSchristos 
421e88b412bSchristos   /* BI [c]: PC = nextPC + (c << 2).  */
422e88b412bSchristos   if (insn.insn_class == BI)
423e88b412bSchristos     {
424e88b412bSchristos       ULONGEST reg_value = arc_insn_get_operand_value (insn, 0);
425e88b412bSchristos       return arc_insn_get_linear_next_pc (insn) + (reg_value << 2);
426e88b412bSchristos     }
427e88b412bSchristos   /* BIH [c]: PC = nextPC + (c << 1).  */
428e88b412bSchristos   else if (insn.insn_class == BIH)
429e88b412bSchristos     {
430e88b412bSchristos       ULONGEST reg_value = arc_insn_get_operand_value (insn, 0);
431e88b412bSchristos       return arc_insn_get_linear_next_pc (insn) + (reg_value << 1);
432e88b412bSchristos     }
433e88b412bSchristos   /* JLI and EI.  */
434e88b412bSchristos   /* JLI and EI depend on optional AUX registers.  Not supported right now.  */
435e88b412bSchristos   else if (insn.insn_class == JLI)
436e88b412bSchristos     {
437e88b412bSchristos       fprintf_unfiltered (gdb_stderr,
438e88b412bSchristos 			  "JLI_S instruction is not supported by the GDB.");
439e88b412bSchristos       return 0;
440e88b412bSchristos     }
441e88b412bSchristos   else if (insn.insn_class == EI)
442e88b412bSchristos     {
443e88b412bSchristos       fprintf_unfiltered (gdb_stderr,
444e88b412bSchristos 			  "EI_S instruction is not supported by the GDB.");
445e88b412bSchristos       return 0;
446e88b412bSchristos     }
447e88b412bSchristos   /* LEAVE_S: PC = BLINK.  */
448e88b412bSchristos   else if (insn.insn_class == LEAVE)
449e88b412bSchristos     {
450e88b412bSchristos       struct regcache *regcache = get_current_regcache ();
451e88b412bSchristos       ULONGEST value;
452e88b412bSchristos       regcache_cooked_read_unsigned (regcache, ARC_BLINK_REGNUM, &value);
453e88b412bSchristos       return value;
454e88b412bSchristos     }
455e88b412bSchristos   /* BBIT0/1, BRcc: PC = currentPC + operand.  */
456e88b412bSchristos   else if (insn.insn_class == BBIT0 || insn.insn_class == BBIT1
457e88b412bSchristos 	   || insn.insn_class == BRCC)
458e88b412bSchristos     {
459e88b412bSchristos       /* Most instructions has branch target as their sole argument.  However
460e88b412bSchristos 	 conditional brcc/bbit has it as a third operand.  */
461e88b412bSchristos       CORE_ADDR pcrel_addr = arc_insn_get_operand_value (insn, 2);
462e88b412bSchristos 
463e88b412bSchristos       /* Offset is relative to the 4-byte aligned address of the current
464e88b412bSchristos 	 instruction, hence last two bits should be truncated.  */
465e88b412bSchristos       return pcrel_addr + align_down (insn.address, 4);
466e88b412bSchristos     }
467e88b412bSchristos   /* B, Bcc, BL, BLcc, LP, LPcc: PC = currentPC + operand.  */
468e88b412bSchristos   else if (insn.insn_class == BRANCH || insn.insn_class == LOOP)
469e88b412bSchristos     {
470e88b412bSchristos       CORE_ADDR pcrel_addr = arc_insn_get_operand_value (insn, 0);
471e88b412bSchristos 
472e88b412bSchristos       /* Offset is relative to the 4-byte aligned address of the current
473e88b412bSchristos 	 instruction, hence last two bits should be truncated.  */
474e88b412bSchristos       return pcrel_addr + align_down (insn.address, 4);
475e88b412bSchristos     }
476e88b412bSchristos   /* J, Jcc, JL, JLcc: PC = operand.  */
477e88b412bSchristos   else if (insn.insn_class == JUMP)
478e88b412bSchristos     {
479e88b412bSchristos       /* All jumps are single-operand.  */
480e88b412bSchristos       return arc_insn_get_operand_value (insn, 0);
481e88b412bSchristos     }
482e88b412bSchristos 
483e88b412bSchristos   /* This is some new and unknown instruction.  */
484e88b412bSchristos   gdb_assert_not_reached ("Unknown branch instruction.");
485e88b412bSchristos }
486e88b412bSchristos 
487e88b412bSchristos /* Dump INSN into gdb_stdlog.  */
488e88b412bSchristos 
489*56bb7041Schristos static void
arc_insn_dump(const struct arc_instruction & insn)490e88b412bSchristos arc_insn_dump (const struct arc_instruction &insn)
491e88b412bSchristos {
492e88b412bSchristos   struct gdbarch *gdbarch = target_gdbarch ();
493e88b412bSchristos 
494e88b412bSchristos   arc_print ("Dumping arc_instruction at %s\n",
495e88b412bSchristos 	     paddress (gdbarch, insn.address));
496e88b412bSchristos   arc_print ("\tlength = %u\n", insn.length);
497e88b412bSchristos 
498e88b412bSchristos   if (!insn.valid)
499e88b412bSchristos     {
500e88b412bSchristos       arc_print ("\tThis is not a valid ARC instruction.\n");
501e88b412bSchristos       return;
502e88b412bSchristos     }
503e88b412bSchristos 
504e88b412bSchristos   arc_print ("\tlength_with_limm = %u\n", insn.length + (insn.limm_p ? 4 : 0));
505e88b412bSchristos   arc_print ("\tcc = 0x%x\n", insn.condition_code);
506e88b412bSchristos   arc_print ("\tinsn_class = %u\n", insn.insn_class);
507e88b412bSchristos   arc_print ("\tis_control_flow = %i\n", insn.is_control_flow);
508e88b412bSchristos   arc_print ("\thas_delay_slot = %i\n", insn.has_delay_slot);
509e88b412bSchristos 
510e88b412bSchristos   CORE_ADDR next_pc = arc_insn_get_linear_next_pc (insn);
511e88b412bSchristos   arc_print ("\tlinear_next_pc = %s\n", paddress (gdbarch, next_pc));
512e88b412bSchristos 
513e88b412bSchristos   if (insn.is_control_flow)
514e88b412bSchristos     {
515e88b412bSchristos       CORE_ADDR t = arc_insn_get_branch_target (insn);
516e88b412bSchristos       arc_print ("\tbranch_target = %s\n", paddress (gdbarch, t));
517e88b412bSchristos     }
518e88b412bSchristos 
519e88b412bSchristos   arc_print ("\tlimm_p = %i\n", insn.limm_p);
520e88b412bSchristos   if (insn.limm_p)
521e88b412bSchristos     arc_print ("\tlimm_value = 0x%08x\n", insn.limm_value);
522e88b412bSchristos 
523e88b412bSchristos   if (insn.insn_class == STORE || insn.insn_class == LOAD
524e88b412bSchristos       || insn.insn_class == PUSH || insn.insn_class == POP)
525e88b412bSchristos     {
526e88b412bSchristos       arc_print ("\twriteback_mode = %u\n", insn.writeback_mode);
527e88b412bSchristos       arc_print ("\tdata_size_mode = %u\n", insn.data_size_mode);
528e88b412bSchristos       arc_print ("\tmemory_base_register = %s\n",
529e88b412bSchristos 		 gdbarch_register_name (gdbarch,
530e88b412bSchristos 					arc_insn_get_memory_base_reg (insn)));
531e88b412bSchristos       /* get_memory_offset returns an unsigned CORE_ADDR, but treat it as a
532e88b412bSchristos 	 LONGEST for a nicer representation.  */
533e88b412bSchristos       arc_print ("\taddr_offset = %s\n",
534e88b412bSchristos 		 plongest (arc_insn_get_memory_offset (insn)));
535e88b412bSchristos     }
536e88b412bSchristos 
537e88b412bSchristos   arc_print ("\toperands_count = %u\n", insn.operands_count);
538e88b412bSchristos   for (unsigned int i = 0; i < insn.operands_count; ++i)
539e88b412bSchristos     {
540e88b412bSchristos       int is_reg = (insn.operands[i].kind == ARC_OPERAND_KIND_REG);
541e88b412bSchristos 
542e88b412bSchristos       arc_print ("\toperand[%u] = {\n", i);
543e88b412bSchristos       arc_print ("\t\tis_reg = %i\n", is_reg);
544e88b412bSchristos       if (is_reg)
545e88b412bSchristos 	arc_print ("\t\tregister = %s\n",
546e88b412bSchristos 		   gdbarch_register_name (gdbarch, insn.operands[i].value));
547e88b412bSchristos       /* Don't know if this value is signed or not, so print both
548e88b412bSchristos 	 representations.  This tends to look quite ugly, especially for big
549e88b412bSchristos 	 numbers.  */
550e88b412bSchristos       arc_print ("\t\tunsigned value = %s\n",
551e88b412bSchristos 		 pulongest (arc_insn_get_operand_value (insn, i)));
552e88b412bSchristos       arc_print ("\t\tsigned value = %s\n",
553e88b412bSchristos 		 plongest (arc_insn_get_operand_value_signed (insn, i)));
554e88b412bSchristos       arc_print ("\t}\n");
555e88b412bSchristos     }
556e88b412bSchristos }
557e88b412bSchristos 
558e88b412bSchristos CORE_ADDR
arc_insn_get_linear_next_pc(const struct arc_instruction & insn)559e88b412bSchristos arc_insn_get_linear_next_pc (const struct arc_instruction &insn)
560e88b412bSchristos {
561e88b412bSchristos   /* In ARC long immediate is always 4 bytes.  */
562e88b412bSchristos   return (insn.address + insn.length + (insn.limm_p ? 4 : 0));
563e88b412bSchristos }
564e88b412bSchristos 
565e88b412bSchristos /* Implement the "write_pc" gdbarch method.
566e88b412bSchristos 
567e88b412bSchristos    In ARC PC register is a normal register so in most cases setting PC value
568e88b412bSchristos    is a straightforward process: debugger just writes PC value.  However it
569e88b412bSchristos    gets trickier in case when current instruction is an instruction in delay
570e88b412bSchristos    slot.  In this case CPU will execute instruction at current PC value, then
571e88b412bSchristos    will set PC to the current value of BTA register; also current instruction
572e88b412bSchristos    cannot be branch/jump and some of the other instruction types.  Thus if
573e88b412bSchristos    debugger would try to just change PC value in this case, this instruction
574e88b412bSchristos    will get executed, but then core will "jump" to the original branch target.
575e88b412bSchristos 
576e88b412bSchristos    Whether current instruction is a delay-slot instruction or not is indicated
577e88b412bSchristos    by DE bit in STATUS32 register indicates if current instruction is a delay
578e88b412bSchristos    slot instruction.  This bit is writable by debug host, which allows debug
579e88b412bSchristos    host to prevent core from jumping after the delay slot instruction.  It
580e88b412bSchristos    also works in another direction: setting this bit will make core to treat
581e88b412bSchristos    any current instructions as a delay slot instruction and to set PC to the
582e88b412bSchristos    current value of BTA register.
583e88b412bSchristos 
584e88b412bSchristos    To workaround issues with changing PC register while in delay slot
585e88b412bSchristos    instruction, debugger should check for the STATUS32.DE bit and reset it if
586e88b412bSchristos    it is set.  No other change is required in this function.  Most common
587e88b412bSchristos    case, where this function might be required is calling inferior functions
588e88b412bSchristos    from debugger.  Generic GDB logic handles this pretty well: current values
589e88b412bSchristos    of registers are stored, value of PC is changed (that is the job of this
590e88b412bSchristos    function), and after inferior function is executed, GDB restores all
591e88b412bSchristos    registers, include BTA and STATUS32, which also means that core is returned
592e88b412bSchristos    to its original state of being halted on delay slot instructions.
593e88b412bSchristos 
594e88b412bSchristos    This method is useless for ARC 600, because it doesn't have externally
595e88b412bSchristos    exposed BTA register.  In the case of ARC 600 it is impossible to restore
596e88b412bSchristos    core to its state in all occasions thus core should never be halted (from
597e88b412bSchristos    the perspective of debugger host) in the delay slot.  */
598e88b412bSchristos 
599e88b412bSchristos static void
arc_write_pc(struct regcache * regcache,CORE_ADDR new_pc)600e88b412bSchristos arc_write_pc (struct regcache *regcache, CORE_ADDR new_pc)
601e88b412bSchristos {
6023aed4a8bSchristos   struct gdbarch *gdbarch = regcache->arch ();
603e88b412bSchristos 
604e88b412bSchristos   if (arc_debug)
605e88b412bSchristos     debug_printf ("arc: Writing PC, new value=%s\n",
606e88b412bSchristos 		  paddress (gdbarch, new_pc));
607e88b412bSchristos 
608e88b412bSchristos   regcache_cooked_write_unsigned (regcache, gdbarch_pc_regnum (gdbarch),
609e88b412bSchristos 				  new_pc);
610e88b412bSchristos 
611e88b412bSchristos   ULONGEST status32;
612e88b412bSchristos   regcache_cooked_read_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
613e88b412bSchristos 				 &status32);
614e88b412bSchristos 
615*56bb7041Schristos   if ((status32 & ARC_STATUS32_DE_MASK) != 0)
616e88b412bSchristos     {
617e88b412bSchristos       if (arc_debug)
618e88b412bSchristos 	{
619e88b412bSchristos 	  debug_printf ("arc: Changing PC while in delay slot.  Will "
620e88b412bSchristos 			"reset STATUS32.DE bit to zero.  Value of STATUS32 "
621e88b412bSchristos 			"register is 0x%s\n",
622e88b412bSchristos 			phex (status32, ARC_REGISTER_SIZE));
623e88b412bSchristos 	}
624e88b412bSchristos 
625e88b412bSchristos       /* Reset bit and write to the cache.  */
626e88b412bSchristos       status32 &= ~0x40;
627e88b412bSchristos       regcache_cooked_write_unsigned (regcache, gdbarch_ps_regnum (gdbarch),
628e88b412bSchristos 				      status32);
629e88b412bSchristos     }
630e88b412bSchristos }
631e88b412bSchristos 
632e88b412bSchristos /* Implement the "virtual_frame_pointer" gdbarch method.
633e88b412bSchristos 
634e88b412bSchristos    According to ABI the FP (r27) is used to point to the middle of the current
635e88b412bSchristos    stack frame, just below the saved FP and before local variables, register
636e88b412bSchristos    spill area and outgoing args.  However for optimization levels above O2 and
637e88b412bSchristos    in any case in leaf functions, the frame pointer is usually not set at all.
638e88b412bSchristos    The exception being when handling nested functions.
639e88b412bSchristos 
640e88b412bSchristos    We use this function to return a "virtual" frame pointer, marking the start
641e88b412bSchristos    of the current stack frame as a register-offset pair.  If the FP is not
642e88b412bSchristos    being used, then it should return SP, with an offset of the frame size.
643e88b412bSchristos 
644e88b412bSchristos    The current implementation doesn't actually know the frame size, nor
645e88b412bSchristos    whether the FP is actually being used, so for now we just return SP and an
646e88b412bSchristos    offset of zero.  This is no worse than other architectures, but is needed
647e88b412bSchristos    to avoid assertion failures.
648e88b412bSchristos 
649e88b412bSchristos    TODO: Can we determine the frame size to get a correct offset?
650e88b412bSchristos 
651e88b412bSchristos    PC is a program counter where we need the virtual FP.  REG_PTR is the base
652e88b412bSchristos    register used for the virtual FP.  OFFSET_PTR is the offset used for the
653e88b412bSchristos    virtual FP.  */
654e88b412bSchristos 
655e88b412bSchristos static void
arc_virtual_frame_pointer(struct gdbarch * gdbarch,CORE_ADDR pc,int * reg_ptr,LONGEST * offset_ptr)656e88b412bSchristos arc_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
657e88b412bSchristos 			   int *reg_ptr, LONGEST *offset_ptr)
658e88b412bSchristos {
659e88b412bSchristos   *reg_ptr = gdbarch_sp_regnum (gdbarch);
660e88b412bSchristos   *offset_ptr = 0;
661e88b412bSchristos }
662e88b412bSchristos 
663e88b412bSchristos /* Implement the "push_dummy_call" gdbarch method.
664e88b412bSchristos 
665e88b412bSchristos    Stack Frame Layout
666e88b412bSchristos 
667e88b412bSchristos    This shows the layout of the stack frame for the general case of a
668e88b412bSchristos    function call; a given function might not have a variable number of
669e88b412bSchristos    arguments or local variables, or might not save any registers, so it would
670e88b412bSchristos    not have the corresponding frame areas.  Additionally, a leaf function
671e88b412bSchristos    (i.e. one which calls no other functions) does not need to save the
672e88b412bSchristos    contents of the BLINK register (which holds its return address), and a
673e88b412bSchristos    function might not have a frame pointer.
674e88b412bSchristos 
675e88b412bSchristos    The stack grows downward, so SP points below FP in memory; SP always
676e88b412bSchristos    points to the last used word on the stack, not the first one.
677e88b412bSchristos 
678e88b412bSchristos                       |                       |   |
679e88b412bSchristos                       |      arg word N       |   | caller's
680e88b412bSchristos                       |           :           |   | frame
681e88b412bSchristos                       |      arg word 10      |   |
682e88b412bSchristos                       |      arg word 9       |   |
683e88b412bSchristos           old SP ---> +-----------------------+ --+
684e88b412bSchristos                       |                       |   |
685e88b412bSchristos                       |      callee-saved     |   |
686e88b412bSchristos                       |       registers       |   |
687e88b412bSchristos                       |  including fp, blink  |   |
688e88b412bSchristos                       |                       |   | callee's
689e88b412bSchristos           new FP ---> +-----------------------+   | frame
690e88b412bSchristos                       |                       |   |
691e88b412bSchristos                       |         local         |   |
692e88b412bSchristos                       |       variables       |   |
693e88b412bSchristos                       |                       |   |
694e88b412bSchristos                       |       register        |   |
695e88b412bSchristos                       |      spill area       |   |
696e88b412bSchristos                       |                       |   |
697e88b412bSchristos                       |     outgoing args     |   |
698e88b412bSchristos                       |                       |   |
699e88b412bSchristos           new SP ---> +-----------------------+ --+
700e88b412bSchristos                       |                       |
701e88b412bSchristos                       |         unused        |
702e88b412bSchristos                       |                       |
703e88b412bSchristos                                   |
704e88b412bSchristos                                   |
705e88b412bSchristos                                   V
706e88b412bSchristos                               downwards
707e88b412bSchristos 
708e88b412bSchristos    The list of arguments to be passed to a function is considered to be a
709e88b412bSchristos    sequence of _N_ words (as though all the parameters were stored in order in
710e88b412bSchristos    memory with each parameter occupying an integral number of words).  Words
711e88b412bSchristos    1..8 are passed in registers 0..7; if the function has more than 8 words of
712e88b412bSchristos    arguments then words 9..@em N are passed on the stack in the caller's frame.
713e88b412bSchristos 
714e88b412bSchristos    If the function has a variable number of arguments, e.g. it has a form such
715e88b412bSchristos    as `function (p1, p2, ...);' and _P_ words are required to hold the values
716e88b412bSchristos    of the named parameters (which are passed in registers 0..@em P -1), then
717e88b412bSchristos    the remaining 8 - _P_ words passed in registers _P_..7 are spilled into the
718e88b412bSchristos    top of the frame so that the anonymous parameter words occupy a continuous
719e88b412bSchristos    region.
720e88b412bSchristos 
721e88b412bSchristos    Any arguments are already in target byte order.  We just need to store
722e88b412bSchristos    them!
723e88b412bSchristos 
724e88b412bSchristos    BP_ADDR is the return address where breakpoint must be placed.  NARGS is
725e88b412bSchristos    the number of arguments to the function.  ARGS is the arguments values (in
726e88b412bSchristos    target byte order).  SP is the Current value of SP register.  STRUCT_RETURN
727e88b412bSchristos    is TRUE if structures are returned by the function.  STRUCT_ADDR is the
728e88b412bSchristos    hidden address for returning a struct.  Returns SP of a new frame.  */
729e88b412bSchristos 
730e88b412bSchristos static CORE_ADDR
arc_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,function_call_return_method return_method,CORE_ADDR struct_addr)731e88b412bSchristos arc_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
732e88b412bSchristos 		     struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
7333aed4a8bSchristos 		     struct value **args, CORE_ADDR sp,
7343aed4a8bSchristos 		     function_call_return_method return_method,
735e88b412bSchristos 		     CORE_ADDR struct_addr)
736e88b412bSchristos {
737e88b412bSchristos   if (arc_debug)
738e88b412bSchristos     debug_printf ("arc: push_dummy_call (nargs = %d)\n", nargs);
739e88b412bSchristos 
740e88b412bSchristos   int arg_reg = ARC_FIRST_ARG_REGNUM;
741e88b412bSchristos 
742e88b412bSchristos   /* Push the return address.  */
743e88b412bSchristos   regcache_cooked_write_unsigned (regcache, ARC_BLINK_REGNUM, bp_addr);
744e88b412bSchristos 
745e88b412bSchristos   /* Are we returning a value using a structure return instead of a normal
746e88b412bSchristos      value return?  If so, struct_addr is the address of the reserved space for
747e88b412bSchristos      the return structure to be written on the stack, and that address is
748e88b412bSchristos      passed to that function as a hidden first argument.  */
7493aed4a8bSchristos   if (return_method == return_method_struct)
750e88b412bSchristos     {
751e88b412bSchristos       /* Pass the return address in the first argument register.  */
752e88b412bSchristos       regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
753e88b412bSchristos 
754e88b412bSchristos       if (arc_debug)
755e88b412bSchristos 	debug_printf ("arc: struct return address %s passed in R%d",
756e88b412bSchristos 		      print_core_address (gdbarch, struct_addr), arg_reg);
757e88b412bSchristos 
758e88b412bSchristos       arg_reg++;
759e88b412bSchristos     }
760e88b412bSchristos 
761e88b412bSchristos   if (nargs > 0)
762e88b412bSchristos     {
763e88b412bSchristos       unsigned int total_space = 0;
764e88b412bSchristos 
765e88b412bSchristos       /* How much space do the arguments occupy in total?  Must round each
766e88b412bSchristos 	 argument's size up to an integral number of words.  */
767e88b412bSchristos       for (int i = 0; i < nargs; i++)
768e88b412bSchristos 	{
769e88b412bSchristos 	  unsigned int len = TYPE_LENGTH (value_type (args[i]));
770e88b412bSchristos 	  unsigned int space = align_up (len, 4);
771e88b412bSchristos 
772e88b412bSchristos 	  total_space += space;
773e88b412bSchristos 
774e88b412bSchristos 	  if (arc_debug)
775e88b412bSchristos 	    debug_printf ("arc: arg %d: %u bytes -> %u\n", i, len, space);
776e88b412bSchristos 	}
777e88b412bSchristos 
778e88b412bSchristos       /* Allocate a buffer to hold a memory image of the arguments.  */
779e88b412bSchristos       gdb_byte *memory_image = XCNEWVEC (gdb_byte, total_space);
780e88b412bSchristos 
781e88b412bSchristos       /* Now copy all of the arguments into the buffer, correctly aligned.  */
782e88b412bSchristos       gdb_byte *data = memory_image;
783e88b412bSchristos       for (int i = 0; i < nargs; i++)
784e88b412bSchristos 	{
785e88b412bSchristos 	  unsigned int len = TYPE_LENGTH (value_type (args[i]));
786e88b412bSchristos 	  unsigned int space = align_up (len, 4);
787e88b412bSchristos 
788e88b412bSchristos 	  memcpy (data, value_contents (args[i]), (size_t) len);
789e88b412bSchristos 	  if (arc_debug)
790e88b412bSchristos 	    debug_printf ("arc: copying arg %d, val 0x%08x, len %d to mem\n",
791e88b412bSchristos 			  i, *((int *) value_contents (args[i])), len);
792e88b412bSchristos 
793e88b412bSchristos 	  data += space;
794e88b412bSchristos 	}
795e88b412bSchristos 
796e88b412bSchristos       /* Now load as much as possible of the memory image into registers.  */
797e88b412bSchristos       data = memory_image;
798e88b412bSchristos       while (arg_reg <= ARC_LAST_ARG_REGNUM)
799e88b412bSchristos 	{
800e88b412bSchristos 	  if (arc_debug)
801e88b412bSchristos 	    debug_printf ("arc: passing 0x%02x%02x%02x%02x in register R%d\n",
802e88b412bSchristos 			  data[0], data[1], data[2], data[3], arg_reg);
803e88b412bSchristos 
804e88b412bSchristos 	  /* Note we don't use write_unsigned here, since that would convert
805e88b412bSchristos 	     the byte order, but we are already in the correct byte order.  */
8063aed4a8bSchristos 	  regcache->cooked_write (arg_reg, data);
807e88b412bSchristos 
808e88b412bSchristos 	  data += ARC_REGISTER_SIZE;
809e88b412bSchristos 	  total_space -= ARC_REGISTER_SIZE;
810e88b412bSchristos 
811e88b412bSchristos 	  /* All the data is now in registers.  */
812e88b412bSchristos 	  if (total_space == 0)
813e88b412bSchristos 	    break;
814e88b412bSchristos 
815e88b412bSchristos 	  arg_reg++;
816e88b412bSchristos 	}
817e88b412bSchristos 
818e88b412bSchristos       /* If there is any data left, push it onto the stack (in a single write
819e88b412bSchristos 	 operation).  */
820e88b412bSchristos       if (total_space > 0)
821e88b412bSchristos 	{
822e88b412bSchristos 	  if (arc_debug)
823e88b412bSchristos 	    debug_printf ("arc: passing %d bytes on stack\n", total_space);
824e88b412bSchristos 
825e88b412bSchristos 	  sp -= total_space;
826e88b412bSchristos 	  write_memory (sp, data, (int) total_space);
827e88b412bSchristos 	}
828e88b412bSchristos 
829e88b412bSchristos       xfree (memory_image);
830e88b412bSchristos     }
831e88b412bSchristos 
832e88b412bSchristos   /* Finally, update the SP register.  */
833e88b412bSchristos   regcache_cooked_write_unsigned (regcache, gdbarch_sp_regnum (gdbarch), sp);
834e88b412bSchristos 
835e88b412bSchristos   return sp;
836e88b412bSchristos }
837e88b412bSchristos 
838e88b412bSchristos /* Implement the "push_dummy_code" gdbarch method.
839e88b412bSchristos 
840e88b412bSchristos    We don't actually push any code.  We just identify where a breakpoint can
841e88b412bSchristos    be inserted to which we are can return and the resume address where we
842e88b412bSchristos    should be called.
843e88b412bSchristos 
844e88b412bSchristos    ARC does not necessarily have an executable stack, so we can't put the
845e88b412bSchristos    return breakpoint there.  Instead we put it at the entry point of the
846e88b412bSchristos    function.  This means the SP is unchanged.
847e88b412bSchristos 
848e88b412bSchristos    SP is a current stack pointer FUNADDR is an address of the function to be
849e88b412bSchristos    called.  ARGS is arguments to pass.  NARGS is a number of args to pass.
850e88b412bSchristos    VALUE_TYPE is a type of value returned.  REAL_PC is a resume address when
851e88b412bSchristos    the function is called.  BP_ADDR is an address where breakpoint should be
852e88b412bSchristos    set.  Returns the updated stack pointer.  */
853e88b412bSchristos 
854e88b412bSchristos static CORE_ADDR
arc_push_dummy_code(struct gdbarch * gdbarch,CORE_ADDR sp,CORE_ADDR funaddr,struct value ** args,int nargs,struct type * value_type,CORE_ADDR * real_pc,CORE_ADDR * bp_addr,struct regcache * regcache)855e88b412bSchristos arc_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp, CORE_ADDR funaddr,
856e88b412bSchristos 		     struct value **args, int nargs, struct type *value_type,
857e88b412bSchristos 		     CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
858e88b412bSchristos 		     struct regcache *regcache)
859e88b412bSchristos {
860e88b412bSchristos   *real_pc = funaddr;
861e88b412bSchristos   *bp_addr = entry_point_address ();
862e88b412bSchristos   return sp;
863e88b412bSchristos }
864e88b412bSchristos 
865e88b412bSchristos /* Implement the "cannot_fetch_register" gdbarch method.  */
866e88b412bSchristos 
867e88b412bSchristos static int
arc_cannot_fetch_register(struct gdbarch * gdbarch,int regnum)868e88b412bSchristos arc_cannot_fetch_register (struct gdbarch *gdbarch, int regnum)
869e88b412bSchristos {
870e88b412bSchristos   /* Assume that register is readable if it is unknown.  LIMM and RESERVED are
871e88b412bSchristos      not real registers, but specific register numbers.  They are available as
872e88b412bSchristos      regnums to align architectural register numbers with GDB internal regnums,
873e88b412bSchristos      but they shouldn't appear in target descriptions generated by
874e88b412bSchristos      GDB-servers.  */
875e88b412bSchristos   switch (regnum)
876e88b412bSchristos     {
877e88b412bSchristos     case ARC_RESERVED_REGNUM:
878e88b412bSchristos     case ARC_LIMM_REGNUM:
879e88b412bSchristos       return true;
880e88b412bSchristos     default:
881e88b412bSchristos       return false;
882e88b412bSchristos     }
883e88b412bSchristos }
884e88b412bSchristos 
885e88b412bSchristos /* Implement the "cannot_store_register" gdbarch method.  */
886e88b412bSchristos 
887e88b412bSchristos static int
arc_cannot_store_register(struct gdbarch * gdbarch,int regnum)888e88b412bSchristos arc_cannot_store_register (struct gdbarch *gdbarch, int regnum)
889e88b412bSchristos {
890e88b412bSchristos   /* Assume that register is writable if it is unknown.  See comment in
891e88b412bSchristos      arc_cannot_fetch_register about LIMM and RESERVED.  */
892e88b412bSchristos   switch (regnum)
893e88b412bSchristos     {
894e88b412bSchristos     case ARC_RESERVED_REGNUM:
895e88b412bSchristos     case ARC_LIMM_REGNUM:
896e88b412bSchristos     case ARC_PCL_REGNUM:
897e88b412bSchristos       return true;
898e88b412bSchristos     default:
899e88b412bSchristos       return false;
900e88b412bSchristos     }
901e88b412bSchristos }
902e88b412bSchristos 
903e88b412bSchristos /* Get the return value of a function from the registers/memory used to
904e88b412bSchristos    return it, according to the convention used by the ABI - 4-bytes values are
905e88b412bSchristos    in the R0, while 8-byte values are in the R0-R1.
906e88b412bSchristos 
907e88b412bSchristos    TODO: This implementation ignores the case of "complex double", where
908e88b412bSchristos    according to ABI, value is returned in the R0-R3 registers.
909e88b412bSchristos 
910e88b412bSchristos    TYPE is a returned value's type.  VALBUF is a buffer for the returned
911e88b412bSchristos    value.  */
912e88b412bSchristos 
913e88b412bSchristos static void
arc_extract_return_value(struct gdbarch * gdbarch,struct type * type,struct regcache * regcache,gdb_byte * valbuf)914e88b412bSchristos arc_extract_return_value (struct gdbarch *gdbarch, struct type *type,
915e88b412bSchristos 			  struct regcache *regcache, gdb_byte *valbuf)
916e88b412bSchristos {
917e88b412bSchristos   unsigned int len = TYPE_LENGTH (type);
918e88b412bSchristos 
919e88b412bSchristos   if (arc_debug)
920e88b412bSchristos     debug_printf ("arc: extract_return_value\n");
921e88b412bSchristos 
922e88b412bSchristos   if (len <= ARC_REGISTER_SIZE)
923e88b412bSchristos     {
924e88b412bSchristos       ULONGEST val;
925e88b412bSchristos 
926e88b412bSchristos       /* Get the return value from one register.  */
927e88b412bSchristos       regcache_cooked_read_unsigned (regcache, ARC_R0_REGNUM, &val);
928e88b412bSchristos       store_unsigned_integer (valbuf, (int) len,
929e88b412bSchristos 			      gdbarch_byte_order (gdbarch), val);
930e88b412bSchristos 
931e88b412bSchristos       if (arc_debug)
932e88b412bSchristos 	debug_printf ("arc: returning 0x%s\n", phex (val, ARC_REGISTER_SIZE));
933e88b412bSchristos     }
934e88b412bSchristos   else if (len <= ARC_REGISTER_SIZE * 2)
935e88b412bSchristos     {
936e88b412bSchristos       ULONGEST low, high;
937e88b412bSchristos 
938e88b412bSchristos       /* Get the return value from two registers.  */
939e88b412bSchristos       regcache_cooked_read_unsigned (regcache, ARC_R0_REGNUM, &low);
940e88b412bSchristos       regcache_cooked_read_unsigned (regcache, ARC_R1_REGNUM, &high);
941e88b412bSchristos 
942e88b412bSchristos       store_unsigned_integer (valbuf, ARC_REGISTER_SIZE,
943e88b412bSchristos 			      gdbarch_byte_order (gdbarch), low);
944e88b412bSchristos       store_unsigned_integer (valbuf + ARC_REGISTER_SIZE,
945e88b412bSchristos 			      (int) len - ARC_REGISTER_SIZE,
946e88b412bSchristos 			      gdbarch_byte_order (gdbarch), high);
947e88b412bSchristos 
948e88b412bSchristos       if (arc_debug)
949e88b412bSchristos 	debug_printf ("arc: returning 0x%s%s\n",
950e88b412bSchristos 		      phex (high, ARC_REGISTER_SIZE),
951e88b412bSchristos 		      phex (low, ARC_REGISTER_SIZE));
952e88b412bSchristos     }
953e88b412bSchristos   else
954e88b412bSchristos     error (_("arc: extract_return_value: type length %u too large"), len);
955e88b412bSchristos }
956e88b412bSchristos 
957e88b412bSchristos 
958e88b412bSchristos /* Store the return value of a function into the registers/memory used to
959e88b412bSchristos    return it, according to the convention used by the ABI.
960e88b412bSchristos 
961e88b412bSchristos    TODO: This implementation ignores the case of "complex double", where
962e88b412bSchristos    according to ABI, value is returned in the R0-R3 registers.
963e88b412bSchristos 
964e88b412bSchristos    TYPE is a returned value's type.  VALBUF is a buffer with the value to
965e88b412bSchristos    return.  */
966e88b412bSchristos 
967e88b412bSchristos static void
arc_store_return_value(struct gdbarch * gdbarch,struct type * type,struct regcache * regcache,const gdb_byte * valbuf)968e88b412bSchristos arc_store_return_value (struct gdbarch *gdbarch, struct type *type,
969e88b412bSchristos 			struct regcache *regcache, const gdb_byte *valbuf)
970e88b412bSchristos {
971e88b412bSchristos   unsigned int len = TYPE_LENGTH (type);
972e88b412bSchristos 
973e88b412bSchristos   if (arc_debug)
974e88b412bSchristos     debug_printf ("arc: store_return_value\n");
975e88b412bSchristos 
976e88b412bSchristos   if (len <= ARC_REGISTER_SIZE)
977e88b412bSchristos     {
978e88b412bSchristos       ULONGEST val;
979e88b412bSchristos 
980e88b412bSchristos       /* Put the return value into one register.  */
981e88b412bSchristos       val = extract_unsigned_integer (valbuf, (int) len,
982e88b412bSchristos 				      gdbarch_byte_order (gdbarch));
983e88b412bSchristos       regcache_cooked_write_unsigned (regcache, ARC_R0_REGNUM, val);
984e88b412bSchristos 
985e88b412bSchristos       if (arc_debug)
986e88b412bSchristos 	debug_printf ("arc: storing 0x%s\n", phex (val, ARC_REGISTER_SIZE));
987e88b412bSchristos     }
988e88b412bSchristos   else if (len <= ARC_REGISTER_SIZE * 2)
989e88b412bSchristos     {
990e88b412bSchristos       ULONGEST low, high;
991e88b412bSchristos 
992e88b412bSchristos       /* Put the return value into  two registers.  */
993e88b412bSchristos       low = extract_unsigned_integer (valbuf, ARC_REGISTER_SIZE,
994e88b412bSchristos 				      gdbarch_byte_order (gdbarch));
995e88b412bSchristos       high = extract_unsigned_integer (valbuf + ARC_REGISTER_SIZE,
996e88b412bSchristos 				       (int) len - ARC_REGISTER_SIZE,
997e88b412bSchristos 				       gdbarch_byte_order (gdbarch));
998e88b412bSchristos 
999e88b412bSchristos       regcache_cooked_write_unsigned (regcache, ARC_R0_REGNUM, low);
1000e88b412bSchristos       regcache_cooked_write_unsigned (regcache, ARC_R1_REGNUM, high);
1001e88b412bSchristos 
1002e88b412bSchristos       if (arc_debug)
1003e88b412bSchristos 	debug_printf ("arc: storing 0x%s%s\n",
1004e88b412bSchristos 		      phex (high, ARC_REGISTER_SIZE),
1005e88b412bSchristos 		      phex (low, ARC_REGISTER_SIZE));
1006e88b412bSchristos     }
1007e88b412bSchristos   else
1008e88b412bSchristos     error (_("arc_store_return_value: type length too large."));
1009e88b412bSchristos }
1010e88b412bSchristos 
1011e88b412bSchristos /* Implement the "get_longjmp_target" gdbarch method.  */
1012e88b412bSchristos 
1013e88b412bSchristos static int
arc_get_longjmp_target(struct frame_info * frame,CORE_ADDR * pc)1014e88b412bSchristos arc_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
1015e88b412bSchristos {
1016e88b412bSchristos   if (arc_debug)
1017e88b412bSchristos     debug_printf ("arc: get_longjmp_target\n");
1018e88b412bSchristos 
1019e88b412bSchristos   struct gdbarch *gdbarch = get_frame_arch (frame);
1020e88b412bSchristos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1021e88b412bSchristos   int pc_offset = tdep->jb_pc * ARC_REGISTER_SIZE;
1022e88b412bSchristos   gdb_byte buf[ARC_REGISTER_SIZE];
1023e88b412bSchristos   CORE_ADDR jb_addr = get_frame_register_unsigned (frame, ARC_FIRST_ARG_REGNUM);
1024e88b412bSchristos 
1025e88b412bSchristos   if (target_read_memory (jb_addr + pc_offset, buf, ARC_REGISTER_SIZE))
1026e88b412bSchristos     return 0; /* Failed to read from memory.  */
1027e88b412bSchristos 
1028e88b412bSchristos   *pc = extract_unsigned_integer (buf, ARC_REGISTER_SIZE,
1029e88b412bSchristos 				  gdbarch_byte_order (gdbarch));
1030e88b412bSchristos   return 1;
1031e88b412bSchristos }
1032e88b412bSchristos 
1033e88b412bSchristos /* Implement the "return_value" gdbarch method.  */
1034e88b412bSchristos 
1035e88b412bSchristos static enum return_value_convention
arc_return_value(struct gdbarch * gdbarch,struct value * function,struct type * valtype,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)1036e88b412bSchristos arc_return_value (struct gdbarch *gdbarch, struct value *function,
1037e88b412bSchristos 		  struct type *valtype, struct regcache *regcache,
1038e88b412bSchristos 		  gdb_byte *readbuf, const gdb_byte *writebuf)
1039e88b412bSchristos {
1040e88b412bSchristos   /* If the return type is a struct, or a union, or would occupy more than two
1041e88b412bSchristos      registers, the ABI uses the "struct return convention": the calling
1042e88b412bSchristos      function passes a hidden first parameter to the callee (in R0).  That
1043e88b412bSchristos      parameter is the address at which the value being returned should be
1044e88b412bSchristos      stored.  Otherwise, the result is returned in registers.  */
1045*56bb7041Schristos   int is_struct_return = (valtype->code () == TYPE_CODE_STRUCT
1046*56bb7041Schristos 			  || valtype->code () == TYPE_CODE_UNION
1047e88b412bSchristos 			  || TYPE_LENGTH (valtype) > 2 * ARC_REGISTER_SIZE);
1048e88b412bSchristos 
1049e88b412bSchristos   if (arc_debug)
1050e88b412bSchristos     debug_printf ("arc: return_value (readbuf = %s, writebuf = %s)\n",
1051e88b412bSchristos 		  host_address_to_string (readbuf),
1052e88b412bSchristos 		  host_address_to_string (writebuf));
1053e88b412bSchristos 
1054e88b412bSchristos   if (writebuf != NULL)
1055e88b412bSchristos     {
1056e88b412bSchristos       /* Case 1.  GDB should not ask us to set a struct return value: it
1057e88b412bSchristos 	 should know the struct return location and write the value there
1058e88b412bSchristos 	 itself.  */
1059e88b412bSchristos       gdb_assert (!is_struct_return);
1060e88b412bSchristos       arc_store_return_value (gdbarch, valtype, regcache, writebuf);
1061e88b412bSchristos     }
1062e88b412bSchristos   else if (readbuf != NULL)
1063e88b412bSchristos     {
1064e88b412bSchristos       /* Case 2.  GDB should not ask us to get a struct return value: it
1065e88b412bSchristos 	 should know the struct return location and read the value from there
1066e88b412bSchristos 	 itself.  */
1067e88b412bSchristos       gdb_assert (!is_struct_return);
1068e88b412bSchristos       arc_extract_return_value (gdbarch, valtype, regcache, readbuf);
1069e88b412bSchristos     }
1070e88b412bSchristos 
1071e88b412bSchristos   return (is_struct_return
1072e88b412bSchristos 	  ? RETURN_VALUE_STRUCT_CONVENTION
1073e88b412bSchristos 	  : RETURN_VALUE_REGISTER_CONVENTION);
1074e88b412bSchristos }
1075e88b412bSchristos 
1076e88b412bSchristos /* Return the base address of the frame.  For ARC, the base address is the
1077e88b412bSchristos    frame pointer.  */
1078e88b412bSchristos 
1079e88b412bSchristos static CORE_ADDR
arc_frame_base_address(struct frame_info * this_frame,void ** prologue_cache)1080e88b412bSchristos arc_frame_base_address (struct frame_info *this_frame, void **prologue_cache)
1081e88b412bSchristos {
1082e88b412bSchristos   return (CORE_ADDR) get_frame_register_unsigned (this_frame, ARC_FP_REGNUM);
1083e88b412bSchristos }
1084e88b412bSchristos 
1085e88b412bSchristos /* Helper function that returns valid pv_t for an instruction operand:
1086e88b412bSchristos    either a register or a constant.  */
1087e88b412bSchristos 
1088e88b412bSchristos static pv_t
arc_pv_get_operand(pv_t * regs,const struct arc_instruction & insn,int operand)1089e88b412bSchristos arc_pv_get_operand (pv_t *regs, const struct arc_instruction &insn, int operand)
1090e88b412bSchristos {
1091e88b412bSchristos   if (insn.operands[operand].kind == ARC_OPERAND_KIND_REG)
1092e88b412bSchristos     return regs[insn.operands[operand].value];
1093e88b412bSchristos   else
1094e88b412bSchristos     return pv_constant (arc_insn_get_operand_value (insn, operand));
1095e88b412bSchristos }
1096e88b412bSchristos 
1097e88b412bSchristos /* Determine whether the given disassembled instruction may be part of a
1098e88b412bSchristos    function prologue.  If it is, the information in the frame unwind cache will
1099e88b412bSchristos    be updated.  */
1100e88b412bSchristos 
1101e88b412bSchristos static bool
arc_is_in_prologue(struct gdbarch * gdbarch,const struct arc_instruction & insn,pv_t * regs,struct pv_area * stack)1102e88b412bSchristos arc_is_in_prologue (struct gdbarch *gdbarch, const struct arc_instruction &insn,
1103e88b412bSchristos 		    pv_t *regs, struct pv_area *stack)
1104e88b412bSchristos {
1105e88b412bSchristos   /* It might be that currently analyzed address doesn't contain an
1106e88b412bSchristos      instruction, hence INSN is not valid.  It likely means that address points
1107e88b412bSchristos      to a data, non-initialized memory, or middle of a 32-bit instruction.  In
1108e88b412bSchristos      practice this may happen if GDB connects to a remote target that has
1109e88b412bSchristos      non-zeroed memory.  GDB would read PC value and would try to analyze
1110e88b412bSchristos      prologue, but there is no guarantee that memory contents at the address
1111e88b412bSchristos      specified in PC is address is a valid instruction.  There is not much that
1112e88b412bSchristos      that can be done about that.  */
1113e88b412bSchristos   if (!insn.valid)
1114e88b412bSchristos     return false;
1115e88b412bSchristos 
1116e88b412bSchristos   /* Branch/jump or a predicated instruction.  */
1117e88b412bSchristos   if (insn.is_control_flow || insn.condition_code != ARC_CC_AL)
1118e88b412bSchristos     return false;
1119e88b412bSchristos 
1120e88b412bSchristos   /* Store of some register.  May or may not update base address register.  */
1121e88b412bSchristos   if (insn.insn_class == STORE || insn.insn_class == PUSH)
1122e88b412bSchristos     {
1123*56bb7041Schristos       /* There is definitely at least one operand - register/value being
1124e88b412bSchristos 	 stored.  */
1125e88b412bSchristos       gdb_assert (insn.operands_count > 0);
1126e88b412bSchristos 
1127e88b412bSchristos       /* Store at some constant address.  */
1128e88b412bSchristos       if (insn.operands_count > 1
1129e88b412bSchristos 	  && insn.operands[1].kind != ARC_OPERAND_KIND_REG)
1130e88b412bSchristos 	return false;
1131e88b412bSchristos 
1132e88b412bSchristos       /* Writeback modes:
1133e88b412bSchristos 	 Mode	Address used		    Writeback value
1134e88b412bSchristos 	 --------------------------------------------------
1135e88b412bSchristos 	 No	reg + offset		    no
1136e88b412bSchristos 	 A/AW	reg + offset		    reg + offset
1137e88b412bSchristos 	 AB	reg			    reg + offset
1138e88b412bSchristos 	 AS	reg + (offset << scaling)   no
1139e88b412bSchristos 
1140e88b412bSchristos 	 "PUSH reg" is an alias to "ST.AW reg, [SP, -4]" encoding.  However
1141e88b412bSchristos 	 16-bit PUSH_S is a distinct instruction encoding, where offset and
1142e88b412bSchristos 	 base register are implied through opcode.  */
1143e88b412bSchristos 
1144e88b412bSchristos       /* Register with base memory address.  */
1145e88b412bSchristos       int base_reg = arc_insn_get_memory_base_reg (insn);
1146e88b412bSchristos 
1147e88b412bSchristos       /* Address where to write.  arc_insn_get_memory_offset returns scaled
1148e88b412bSchristos 	 value for ARC_WRITEBACK_AS.  */
1149e88b412bSchristos       pv_t addr;
1150e88b412bSchristos       if (insn.writeback_mode == ARC_WRITEBACK_AB)
1151e88b412bSchristos 	addr = regs[base_reg];
1152e88b412bSchristos       else
1153e88b412bSchristos 	addr = pv_add_constant (regs[base_reg],
1154e88b412bSchristos 				arc_insn_get_memory_offset (insn));
1155e88b412bSchristos 
11563aed4a8bSchristos       if (stack->store_would_trash (addr))
1157e88b412bSchristos 	return false;
1158e88b412bSchristos 
1159e88b412bSchristos       if (insn.data_size_mode != ARC_SCALING_D)
1160e88b412bSchristos 	{
1161e88b412bSchristos 	  /* Find the value being stored.  */
1162e88b412bSchristos 	  pv_t store_value = arc_pv_get_operand (regs, insn, 0);
1163e88b412bSchristos 
1164e88b412bSchristos 	  /* What is the size of a the stored value?  */
1165e88b412bSchristos 	  CORE_ADDR size;
1166e88b412bSchristos 	  if (insn.data_size_mode == ARC_SCALING_B)
1167e88b412bSchristos 	    size = 1;
1168e88b412bSchristos 	  else if (insn.data_size_mode == ARC_SCALING_H)
1169e88b412bSchristos 	    size = 2;
1170e88b412bSchristos 	  else
1171e88b412bSchristos 	    size = ARC_REGISTER_SIZE;
1172e88b412bSchristos 
11733aed4a8bSchristos 	  stack->store (addr, size, store_value);
1174e88b412bSchristos 	}
1175e88b412bSchristos       else
1176e88b412bSchristos 	{
1177e88b412bSchristos 	  if (insn.operands[0].kind == ARC_OPERAND_KIND_REG)
1178e88b412bSchristos 	    {
1179e88b412bSchristos 	      /* If this is a double store, than write N+1 register as well.  */
1180e88b412bSchristos 	      pv_t store_value1 = regs[insn.operands[0].value];
1181e88b412bSchristos 	      pv_t store_value2 = regs[insn.operands[0].value + 1];
11823aed4a8bSchristos 	      stack->store (addr, ARC_REGISTER_SIZE, store_value1);
11833aed4a8bSchristos 	      stack->store (pv_add_constant (addr, ARC_REGISTER_SIZE),
1184e88b412bSchristos 			    ARC_REGISTER_SIZE, store_value2);
1185e88b412bSchristos 	    }
1186e88b412bSchristos 	  else
1187e88b412bSchristos 	    {
1188e88b412bSchristos 	      pv_t store_value
1189e88b412bSchristos 		= pv_constant (arc_insn_get_operand_value (insn, 0));
11903aed4a8bSchristos 	      stack->store (addr, ARC_REGISTER_SIZE * 2, store_value);
1191e88b412bSchristos 	    }
1192e88b412bSchristos 	}
1193e88b412bSchristos 
1194e88b412bSchristos       /* Is base register updated?  */
1195e88b412bSchristos       if (insn.writeback_mode == ARC_WRITEBACK_A
1196e88b412bSchristos 	  || insn.writeback_mode == ARC_WRITEBACK_AB)
1197e88b412bSchristos 	regs[base_reg] = pv_add_constant (regs[base_reg],
1198e88b412bSchristos 					  arc_insn_get_memory_offset (insn));
1199e88b412bSchristos 
1200e88b412bSchristos       return true;
1201e88b412bSchristos     }
1202e88b412bSchristos   else if (insn.insn_class == MOVE)
1203e88b412bSchristos     {
1204e88b412bSchristos       gdb_assert (insn.operands_count == 2);
1205e88b412bSchristos 
1206e88b412bSchristos       /* Destination argument can be "0", so nothing will happen.  */
1207e88b412bSchristos       if (insn.operands[0].kind == ARC_OPERAND_KIND_REG)
1208e88b412bSchristos 	{
1209e88b412bSchristos 	  int dst_regnum = insn.operands[0].value;
1210e88b412bSchristos 	  regs[dst_regnum] = arc_pv_get_operand (regs, insn, 1);
1211e88b412bSchristos 	}
1212e88b412bSchristos       return true;
1213e88b412bSchristos     }
1214e88b412bSchristos   else if (insn.insn_class == SUB)
1215e88b412bSchristos     {
1216e88b412bSchristos       gdb_assert (insn.operands_count == 3);
1217e88b412bSchristos 
1218e88b412bSchristos       /* SUB 0,b,c.  */
1219e88b412bSchristos       if (insn.operands[0].kind != ARC_OPERAND_KIND_REG)
1220e88b412bSchristos 	return true;
1221e88b412bSchristos 
1222e88b412bSchristos       int dst_regnum = insn.operands[0].value;
1223e88b412bSchristos       regs[dst_regnum] = pv_subtract (arc_pv_get_operand (regs, insn, 1),
1224e88b412bSchristos 				      arc_pv_get_operand (regs, insn, 2));
1225e88b412bSchristos       return true;
1226e88b412bSchristos     }
1227e88b412bSchristos   else if (insn.insn_class == ENTER)
1228e88b412bSchristos     {
1229e88b412bSchristos       /* ENTER_S is a prologue-in-instruction - it saves all callee-saved
1230e88b412bSchristos 	 registers according to given arguments thus greatly reducing code
1231e88b412bSchristos 	 size.  Which registers will be actually saved depends on arguments.
1232e88b412bSchristos 
1233e88b412bSchristos 	 ENTER_S {R13-...,FP,BLINK} stores registers in following order:
1234e88b412bSchristos 
1235e88b412bSchristos 	 new SP ->
1236e88b412bSchristos 		   BLINK
1237e88b412bSchristos 		   R13
1238e88b412bSchristos 		   R14
1239e88b412bSchristos 		   R15
1240e88b412bSchristos 		   ...
1241e88b412bSchristos 		   FP
1242e88b412bSchristos 	 old SP ->
1243e88b412bSchristos 
1244e88b412bSchristos 	 There are up to three arguments for this opcode, as presented by ARC
1245e88b412bSchristos 	 disassembler:
1246e88b412bSchristos 	 1) amount of general-purpose registers to be saved - this argument is
1247e88b412bSchristos 	    always present even when it is 0;
1248e88b412bSchristos 	 2) FP register number (27) if FP has to be stored, otherwise argument
1249e88b412bSchristos 	    is not present;
1250e88b412bSchristos 	 3) BLINK register number (31) if BLINK has to be stored, otherwise
1251e88b412bSchristos 	    argument is not present.  If both FP and BLINK are stored, then FP
1252e88b412bSchristos 	    is present before BLINK in argument list.  */
1253e88b412bSchristos       gdb_assert (insn.operands_count > 0);
1254e88b412bSchristos 
1255e88b412bSchristos       int regs_saved = arc_insn_get_operand_value (insn, 0);
1256e88b412bSchristos 
1257e88b412bSchristos       bool is_fp_saved;
1258e88b412bSchristos       if (insn.operands_count > 1)
1259e88b412bSchristos 	is_fp_saved = (insn.operands[1].value  == ARC_FP_REGNUM);
1260e88b412bSchristos       else
1261e88b412bSchristos 	is_fp_saved = false;
1262e88b412bSchristos 
1263e88b412bSchristos       bool is_blink_saved;
1264e88b412bSchristos       if (insn.operands_count > 1)
1265e88b412bSchristos 	is_blink_saved = (insn.operands[insn.operands_count - 1].value
1266e88b412bSchristos 			  == ARC_BLINK_REGNUM);
1267e88b412bSchristos       else
1268e88b412bSchristos 	is_blink_saved = false;
1269e88b412bSchristos 
1270e88b412bSchristos       /* Amount of bytes to be allocated to store specified registers.  */
1271e88b412bSchristos       CORE_ADDR st_size = ((regs_saved + is_fp_saved + is_blink_saved)
1272e88b412bSchristos 			   * ARC_REGISTER_SIZE);
1273e88b412bSchristos       pv_t new_sp = pv_add_constant (regs[ARC_SP_REGNUM], -st_size);
1274e88b412bSchristos 
1275e88b412bSchristos       /* Assume that if the last register (closest to new SP) can be written,
1276e88b412bSchristos 	 then it is possible to write all of them.  */
12773aed4a8bSchristos       if (stack->store_would_trash (new_sp))
1278e88b412bSchristos 	return false;
1279e88b412bSchristos 
1280e88b412bSchristos       /* Current store address.  */
1281e88b412bSchristos       pv_t addr = regs[ARC_SP_REGNUM];
1282e88b412bSchristos 
1283e88b412bSchristos       if (is_fp_saved)
1284e88b412bSchristos 	{
1285e88b412bSchristos 	  addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
12863aed4a8bSchristos 	  stack->store (addr, ARC_REGISTER_SIZE, regs[ARC_FP_REGNUM]);
1287e88b412bSchristos 	}
1288e88b412bSchristos 
1289e88b412bSchristos       /* Registers are stored in backward order: from GP (R26) to R13.  */
1290e88b412bSchristos       for (int i = ARC_R13_REGNUM + regs_saved - 1; i >= ARC_R13_REGNUM; i--)
1291e88b412bSchristos 	{
1292e88b412bSchristos 	  addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
12933aed4a8bSchristos 	  stack->store (addr, ARC_REGISTER_SIZE, regs[i]);
1294e88b412bSchristos 	}
1295e88b412bSchristos 
1296e88b412bSchristos       if (is_blink_saved)
1297e88b412bSchristos 	{
1298e88b412bSchristos 	  addr = pv_add_constant (addr, -ARC_REGISTER_SIZE);
12993aed4a8bSchristos 	  stack->store (addr, ARC_REGISTER_SIZE,
1300e88b412bSchristos 			regs[ARC_BLINK_REGNUM]);
1301e88b412bSchristos 	}
1302e88b412bSchristos 
1303e88b412bSchristos       gdb_assert (pv_is_identical (addr, new_sp));
1304e88b412bSchristos 
1305e88b412bSchristos       regs[ARC_SP_REGNUM] = new_sp;
1306e88b412bSchristos 
1307e88b412bSchristos       if (is_fp_saved)
1308e88b412bSchristos 	regs[ARC_FP_REGNUM] = regs[ARC_SP_REGNUM];
1309e88b412bSchristos 
1310e88b412bSchristos       return true;
1311e88b412bSchristos     }
1312e88b412bSchristos 
1313e88b412bSchristos   /* Some other architectures, like nds32 or arm, try to continue as far as
1314e88b412bSchristos      possible when building a prologue cache (as opposed to when skipping
1315e88b412bSchristos      prologue), so that cache will be as full as possible.  However current
1316e88b412bSchristos      code for ARC doesn't recognize some instructions that may modify SP, like
1317e88b412bSchristos      ADD, AND, OR, etc, hence there is no way to guarantee that SP wasn't
1318e88b412bSchristos      clobbered by the skipped instruction.  Potential existence of extension
1319e88b412bSchristos      instruction, which may do anything they want makes this even more complex,
1320e88b412bSchristos      so it is just better to halt on a first unrecognized instruction.  */
1321e88b412bSchristos 
1322e88b412bSchristos   return false;
1323e88b412bSchristos }
1324e88b412bSchristos 
1325e88b412bSchristos /* Copy of gdb_buffered_insn_length_fprintf from disasm.c.  */
1326e88b412bSchristos 
1327e88b412bSchristos static int ATTRIBUTE_PRINTF (2, 3)
arc_fprintf_disasm(void * stream,const char * format,...)1328e88b412bSchristos arc_fprintf_disasm (void *stream, const char *format, ...)
1329e88b412bSchristos {
1330e88b412bSchristos   return 0;
1331e88b412bSchristos }
1332e88b412bSchristos 
1333e88b412bSchristos struct disassemble_info
arc_disassemble_info(struct gdbarch * gdbarch)1334e88b412bSchristos arc_disassemble_info (struct gdbarch *gdbarch)
1335e88b412bSchristos {
1336e88b412bSchristos   struct disassemble_info di;
1337e88b412bSchristos   init_disassemble_info (&di, &null_stream, arc_fprintf_disasm);
1338e88b412bSchristos   di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1339e88b412bSchristos   di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1340e88b412bSchristos   di.endian = gdbarch_byte_order (gdbarch);
1341e88b412bSchristos   di.read_memory_func = [](bfd_vma memaddr, gdb_byte *myaddr,
1342e88b412bSchristos 			   unsigned int len, struct disassemble_info *info)
1343e88b412bSchristos     {
1344e88b412bSchristos       return target_read_code (memaddr, myaddr, len);
1345e88b412bSchristos     };
1346e88b412bSchristos   return di;
1347e88b412bSchristos }
1348e88b412bSchristos 
1349e88b412bSchristos /* Analyze the prologue and update the corresponding frame cache for the frame
1350e88b412bSchristos    unwinder for unwinding frames that doesn't have debug info.  In such
1351e88b412bSchristos    situation GDB attempts to parse instructions in the prologue to understand
1352e88b412bSchristos    where each register is saved.
1353e88b412bSchristos 
1354e88b412bSchristos    If CACHE is not NULL, then it will be filled with information about saved
1355e88b412bSchristos    registers.
1356e88b412bSchristos 
1357*56bb7041Schristos    There are several variations of prologue which GDB may encounter.  "Full"
1358e88b412bSchristos    prologue looks like this:
1359e88b412bSchristos 
1360e88b412bSchristos 	sub	sp,sp,<imm>   ; Space for variadic arguments.
1361e88b412bSchristos 	push	blink	      ; Store return address.
1362e88b412bSchristos 	push	r13	      ; Store callee saved registers (up to R26/GP).
1363e88b412bSchristos 	push	r14
1364e88b412bSchristos 	push	fp	      ; Store frame pointer.
1365e88b412bSchristos 	mov	fp,sp	      ; Update frame pointer.
1366e88b412bSchristos 	sub	sp,sp,<imm>   ; Create space for local vars on the stack.
1367e88b412bSchristos 
1368e88b412bSchristos    Depending on compiler options lots of things may change:
1369e88b412bSchristos 
1370e88b412bSchristos     1) BLINK is not saved in leaf functions.
1371e88b412bSchristos     2) Frame pointer is not saved and updated if -fomit-frame-pointer is used.
1372e88b412bSchristos     3) 16-bit versions of those instructions may be used.
1373e88b412bSchristos     4) Instead of a sequence of several push'es, compiler may instead prefer to
1374e88b412bSchristos     do one subtract on stack pointer and then store registers using normal
1375e88b412bSchristos     store, that doesn't update SP.  Like this:
1376e88b412bSchristos 
1377e88b412bSchristos 
1378*56bb7041Schristos 	sub	sp,sp,8		; Create space for callee-saved registers.
1379e88b412bSchristos 	st	r13,[sp,4]      ; Store callee saved registers (up to R26/GP).
1380e88b412bSchristos 	st	r14,[sp,0]
1381e88b412bSchristos 
1382e88b412bSchristos     5) ENTER_S instruction can encode most of prologue sequence in one
1383e88b412bSchristos     instruction (except for those subtracts for variadic arguments and local
1384e88b412bSchristos     variables).
1385e88b412bSchristos     6) GCC may use "millicode" functions from libgcc to store callee-saved
1386e88b412bSchristos     registers with minimal code-size requirements.  This function currently
1387e88b412bSchristos     doesn't support this.
1388e88b412bSchristos 
1389e88b412bSchristos    ENTRYPOINT is a function entry point where prologue starts.
1390e88b412bSchristos 
1391e88b412bSchristos    LIMIT_PC is a maximum possible end address of prologue (meaning address
1392e88b412bSchristos    of first instruction after the prologue).  It might also point to the middle
1393e88b412bSchristos    of prologue if execution has been stopped by the breakpoint at this address
1394e88b412bSchristos    - in this case debugger should analyze prologue only up to this address,
1395e88b412bSchristos    because further instructions haven't been executed yet.
1396e88b412bSchristos 
1397e88b412bSchristos    Returns address of the first instruction after the prologue.  */
1398e88b412bSchristos 
1399e88b412bSchristos static CORE_ADDR
arc_analyze_prologue(struct gdbarch * gdbarch,const CORE_ADDR entrypoint,const CORE_ADDR limit_pc,struct arc_frame_cache * cache)1400e88b412bSchristos arc_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR entrypoint,
1401e88b412bSchristos 		      const CORE_ADDR limit_pc, struct arc_frame_cache *cache)
1402e88b412bSchristos {
1403e88b412bSchristos   if (arc_debug)
1404e88b412bSchristos     debug_printf ("arc: analyze_prologue (entrypoint=%s, limit_pc=%s)\n",
1405e88b412bSchristos 		  paddress (gdbarch, entrypoint),
1406e88b412bSchristos 		  paddress (gdbarch, limit_pc));
1407e88b412bSchristos 
1408e88b412bSchristos   /* Prologue values.  Only core registers can be stored.  */
1409e88b412bSchristos   pv_t regs[ARC_LAST_CORE_REGNUM + 1];
1410e88b412bSchristos   for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1411e88b412bSchristos     regs[i] = pv_register (i, 0);
14123aed4a8bSchristos   pv_area stack (ARC_SP_REGNUM, gdbarch_addr_bit (gdbarch));
1413e88b412bSchristos 
1414e88b412bSchristos   CORE_ADDR current_prologue_end = entrypoint;
1415e88b412bSchristos 
1416e88b412bSchristos   /* Look at each instruction in the prologue.  */
1417e88b412bSchristos   while (current_prologue_end < limit_pc)
1418e88b412bSchristos     {
1419e88b412bSchristos       struct arc_instruction insn;
1420e88b412bSchristos       struct disassemble_info di = arc_disassemble_info (gdbarch);
1421e88b412bSchristos       arc_insn_decode (current_prologue_end, &di, arc_delayed_print_insn,
1422e88b412bSchristos 		       &insn);
1423e88b412bSchristos 
1424e88b412bSchristos       if (arc_debug >= 2)
1425e88b412bSchristos 	arc_insn_dump (insn);
1426e88b412bSchristos 
1427e88b412bSchristos       /* If this instruction is in the prologue, fields in the cache will be
1428e88b412bSchristos 	 updated, and the saved registers mask may be updated.  */
14293aed4a8bSchristos       if (!arc_is_in_prologue (gdbarch, insn, regs, &stack))
1430e88b412bSchristos 	{
1431e88b412bSchristos 	  /* Found an instruction that is not in the prologue.  */
1432e88b412bSchristos 	  if (arc_debug)
1433e88b412bSchristos 	    debug_printf ("arc: End of prologue reached at address %s\n",
1434e88b412bSchristos 			  paddress (gdbarch, insn.address));
1435e88b412bSchristos 	  break;
1436e88b412bSchristos 	}
1437e88b412bSchristos 
1438e88b412bSchristos       current_prologue_end = arc_insn_get_linear_next_pc (insn);
1439e88b412bSchristos     }
1440e88b412bSchristos 
1441e88b412bSchristos   if (cache != NULL)
1442e88b412bSchristos     {
1443e88b412bSchristos       /* Figure out if it is a frame pointer or just a stack pointer.  */
1444e88b412bSchristos       if (pv_is_register (regs[ARC_FP_REGNUM], ARC_SP_REGNUM))
1445e88b412bSchristos 	{
1446e88b412bSchristos 	  cache->frame_base_reg = ARC_FP_REGNUM;
1447e88b412bSchristos 	  cache->frame_base_offset = -regs[ARC_FP_REGNUM].k;
1448e88b412bSchristos 	}
1449e88b412bSchristos       else
1450e88b412bSchristos 	{
1451e88b412bSchristos 	  cache->frame_base_reg = ARC_SP_REGNUM;
1452e88b412bSchristos 	  cache->frame_base_offset = -regs[ARC_SP_REGNUM].k;
1453e88b412bSchristos 	}
1454e88b412bSchristos 
1455e88b412bSchristos       /* Assign offset from old SP to all saved registers.  */
1456e88b412bSchristos       for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1457e88b412bSchristos 	{
1458e88b412bSchristos 	  CORE_ADDR offset;
14593aed4a8bSchristos 	  if (stack.find_reg (gdbarch, i, &offset))
1460e88b412bSchristos 	    cache->saved_regs[i].addr = offset;
1461e88b412bSchristos 	}
1462e88b412bSchristos     }
1463e88b412bSchristos 
1464e88b412bSchristos   return current_prologue_end;
1465e88b412bSchristos }
1466e88b412bSchristos 
1467e88b412bSchristos /* Estimated maximum prologue length in bytes.  This should include:
1468e88b412bSchristos    1) Store instruction for each callee-saved register (R25 - R13 + 1)
1469e88b412bSchristos    2) Two instructions for FP
1470e88b412bSchristos    3) One for BLINK
1471e88b412bSchristos    4) Three substract instructions for SP (for variadic args, for
1472e88b412bSchristos    callee saved regs and for local vars) and assuming that those SUB use
1473e88b412bSchristos    long-immediate (hence double length).
1474e88b412bSchristos    5) Stores of arguments registers are considered part of prologue too
1475e88b412bSchristos       (R7 - R1 + 1).
1476e88b412bSchristos    This is quite an extreme case, because even with -O0 GCC will collapse first
1477e88b412bSchristos    two SUBs into one and long immediate values are quite unlikely to appear in
1478e88b412bSchristos    this case, but still better to overshoot a bit - prologue analysis will
1479e88b412bSchristos    anyway stop at the first instruction that doesn't fit prologue, so this
1480e88b412bSchristos    limit will be rarely reached.  */
1481e88b412bSchristos 
1482e88b412bSchristos const static int MAX_PROLOGUE_LENGTH
1483e88b412bSchristos   = 4 * (ARC_R25_REGNUM - ARC_R13_REGNUM + 1 + 2 + 1 + 6
1484e88b412bSchristos 	 + ARC_LAST_ARG_REGNUM - ARC_FIRST_ARG_REGNUM + 1);
1485e88b412bSchristos 
1486e88b412bSchristos /* Implement the "skip_prologue" gdbarch method.
1487e88b412bSchristos 
1488e88b412bSchristos    Skip the prologue for the function at PC.  This is done by checking from
1489e88b412bSchristos    the line information read from the DWARF, if possible; otherwise, we scan
1490e88b412bSchristos    the function prologue to find its end.  */
1491e88b412bSchristos 
1492e88b412bSchristos static CORE_ADDR
arc_skip_prologue(struct gdbarch * gdbarch,CORE_ADDR pc)1493e88b412bSchristos arc_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1494e88b412bSchristos {
1495e88b412bSchristos   if (arc_debug)
1496e88b412bSchristos     debug_printf ("arc: skip_prologue\n");
1497e88b412bSchristos 
1498e88b412bSchristos   CORE_ADDR func_addr;
1499e88b412bSchristos   const char *func_name;
1500e88b412bSchristos 
1501e88b412bSchristos   /* See what the symbol table says.  */
1502e88b412bSchristos   if (find_pc_partial_function (pc, &func_name, &func_addr, NULL))
1503e88b412bSchristos     {
1504e88b412bSchristos       /* Found a function.  */
1505e88b412bSchristos       CORE_ADDR postprologue_pc
1506e88b412bSchristos 	= skip_prologue_using_sal (gdbarch, func_addr);
1507e88b412bSchristos 
1508e88b412bSchristos       if (postprologue_pc != 0)
1509e88b412bSchristos 	return std::max (pc, postprologue_pc);
1510e88b412bSchristos     }
1511e88b412bSchristos 
1512e88b412bSchristos   /* No prologue info in symbol table, have to analyze prologue.  */
1513e88b412bSchristos 
1514e88b412bSchristos   /* Find an upper limit on the function prologue using the debug
1515e88b412bSchristos      information.  If there is no debug information about prologue end, then
1516e88b412bSchristos      skip_prologue_using_sal will return 0.  */
1517e88b412bSchristos   CORE_ADDR limit_pc = skip_prologue_using_sal (gdbarch, pc);
1518e88b412bSchristos 
1519e88b412bSchristos   /* If there is no debug information at all, it is required to give some
1520e88b412bSchristos      semi-arbitrary hard limit on amount of bytes to scan during prologue
1521e88b412bSchristos      analysis.  */
1522e88b412bSchristos   if (limit_pc == 0)
1523e88b412bSchristos     limit_pc = pc + MAX_PROLOGUE_LENGTH;
1524e88b412bSchristos 
1525e88b412bSchristos   /* Find the address of the first instruction after the prologue by scanning
1526e88b412bSchristos      through it - no other information is needed, so pass NULL as a cache.  */
1527e88b412bSchristos   return arc_analyze_prologue (gdbarch, pc, limit_pc, NULL);
1528e88b412bSchristos }
1529e88b412bSchristos 
1530e88b412bSchristos /* Implement the "print_insn" gdbarch method.
1531e88b412bSchristos 
1532e88b412bSchristos    arc_get_disassembler () may return different functions depending on bfd
1533e88b412bSchristos    type, so it is not possible to pass print_insn directly to
1534e88b412bSchristos    set_gdbarch_print_insn ().  Instead this wrapper function is used.  It also
1535e88b412bSchristos    may be used by other functions to get disassemble_info for address.  It is
1536e88b412bSchristos    important to note, that those print_insn from opcodes always print
1537e88b412bSchristos    instruction to the stream specified in the INFO.  If this is not desired,
1538e88b412bSchristos    then either `print_insn` function in INFO should be set to some function
1539e88b412bSchristos    that will not print, or `stream` should be different from standard
1540e88b412bSchristos    gdb_stdlog.  */
1541e88b412bSchristos 
1542e88b412bSchristos int
arc_delayed_print_insn(bfd_vma addr,struct disassemble_info * info)1543e88b412bSchristos arc_delayed_print_insn (bfd_vma addr, struct disassemble_info *info)
1544e88b412bSchristos {
1545*56bb7041Schristos   /* Standard BFD "machine number" field allows libopcodes disassembler to
15463aed4a8bSchristos      distinguish ARC 600, 700 and v2 cores, however v2 encompasses both ARC EM
15473aed4a8bSchristos      and HS, which have some difference between.  There are two ways to specify
15483aed4a8bSchristos      what is the target core:
15493aed4a8bSchristos      1) via the disassemble_info->disassembler_options;
15503aed4a8bSchristos      2) otherwise libopcodes will use private (architecture-specific) ELF
15513aed4a8bSchristos      header.
15523aed4a8bSchristos 
15533aed4a8bSchristos      Using disassembler_options is preferable, because it comes directly from
15543aed4a8bSchristos      GDBserver which scanned an actual ARC core identification info.  However,
15553aed4a8bSchristos      not all GDBservers report core architecture, so as a fallback GDB still
15563aed4a8bSchristos      should support analysis of ELF header.  The libopcodes disassembly code
15573aed4a8bSchristos      uses the section to find the BFD and the BFD to find the ELF header,
15583aed4a8bSchristos      therefore this function should set disassemble_info->section properly.
15593aed4a8bSchristos 
15603aed4a8bSchristos      disassembler_options was already set by non-target specific code with
15613aed4a8bSchristos      proper options obtained via gdbarch_disassembler_options ().
15623aed4a8bSchristos 
15633aed4a8bSchristos      This function might be called multiple times in a sequence, reusing same
15643aed4a8bSchristos      disassemble_info.  */
15653aed4a8bSchristos   if ((info->disassembler_options == NULL) && (info->section == NULL))
15663aed4a8bSchristos     {
15673aed4a8bSchristos       struct obj_section *s = find_pc_section (addr);
15683aed4a8bSchristos       if (s != NULL)
15693aed4a8bSchristos 	info->section = s->the_bfd_section;
15703aed4a8bSchristos     }
15713aed4a8bSchristos 
15723aed4a8bSchristos   return default_print_insn (addr, info);
1573e88b412bSchristos }
1574e88b412bSchristos 
1575e88b412bSchristos /* Baremetal breakpoint instructions.
1576e88b412bSchristos 
1577e88b412bSchristos    ARC supports both big- and little-endian.  However, instructions for
1578e88b412bSchristos    little-endian processors are encoded in the middle-endian: half-words are
1579e88b412bSchristos    in big-endian, while bytes inside the half-words are in little-endian; data
1580e88b412bSchristos    is represented in the "normal" little-endian.  Big-endian processors treat
1581e88b412bSchristos    data and code identically.
1582e88b412bSchristos 
1583e88b412bSchristos    Assuming the number 0x01020304, it will be presented this way:
1584e88b412bSchristos 
1585e88b412bSchristos    Address            :  N   N+1  N+2  N+3
1586e88b412bSchristos    little-endian      : 0x04 0x03 0x02 0x01
1587e88b412bSchristos    big-endian         : 0x01 0x02 0x03 0x04
1588e88b412bSchristos    ARC middle-endian  : 0x02 0x01 0x04 0x03
1589e88b412bSchristos   */
1590e88b412bSchristos 
1591e88b412bSchristos static const gdb_byte arc_brk_s_be[] = { 0x7f, 0xff };
1592e88b412bSchristos static const gdb_byte arc_brk_s_le[] = { 0xff, 0x7f };
1593e88b412bSchristos static const gdb_byte arc_brk_be[] = { 0x25, 0x6f, 0x00, 0x3f };
1594e88b412bSchristos static const gdb_byte arc_brk_le[] = { 0x6f, 0x25, 0x3f, 0x00 };
1595e88b412bSchristos 
1596e88b412bSchristos /* For ARC ELF, breakpoint uses the 16-bit BRK_S instruction, which is 0x7fff
1597e88b412bSchristos    (little endian) or 0xff7f (big endian).  We used to insert BRK_S even
1598e88b412bSchristos    instead of 32-bit instructions, which works mostly ok, unless breakpoint is
1599e88b412bSchristos    inserted into delay slot instruction.  In this case if branch is taken
1600e88b412bSchristos    BLINK value will be set to address of instruction after delay slot, however
1601e88b412bSchristos    if we replaced 32-bit instruction in delay slot with 16-bit long BRK_S,
1602e88b412bSchristos    then BLINK value will have an invalid value - it will point to the address
1603e88b412bSchristos    after the BRK_S (which was there at the moment of branch execution) while
1604e88b412bSchristos    it should point to the address after the 32-bit long instruction.  To avoid
1605e88b412bSchristos    such issues this function disassembles instruction at target location and
1606e88b412bSchristos    evaluates it value.
1607e88b412bSchristos 
1608e88b412bSchristos    ARC 600 supports only 16-bit BRK_S.
1609e88b412bSchristos 
1610e88b412bSchristos    NB: Baremetal GDB uses BRK[_S], while user-space GDB uses TRAP_S.  BRK[_S]
1611e88b412bSchristos    is much better because it doesn't commit unlike TRAP_S, so it can be set in
1612e88b412bSchristos    delay slots; however it cannot be used in user-mode, hence usage of TRAP_S
1613e88b412bSchristos    in GDB for user-space.  */
1614e88b412bSchristos 
1615e88b412bSchristos /* Implement the "breakpoint_kind_from_pc" gdbarch method.  */
1616e88b412bSchristos 
1617e88b412bSchristos static int
arc_breakpoint_kind_from_pc(struct gdbarch * gdbarch,CORE_ADDR * pcptr)1618e88b412bSchristos arc_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
1619e88b412bSchristos {
1620e88b412bSchristos   size_t length_with_limm = gdb_insn_length (gdbarch, *pcptr);
1621e88b412bSchristos 
1622e88b412bSchristos   /* Replace 16-bit instruction with BRK_S, replace 32-bit instructions with
1623e88b412bSchristos      BRK.  LIMM is part of instruction length, so it can be either 4 or 8
1624e88b412bSchristos      bytes for 32-bit instructions.  */
1625e88b412bSchristos   if ((length_with_limm == 4 || length_with_limm == 8)
1626e88b412bSchristos       && !arc_mach_is_arc600 (gdbarch))
1627e88b412bSchristos     return sizeof (arc_brk_le);
1628e88b412bSchristos   else
1629e88b412bSchristos     return sizeof (arc_brk_s_le);
1630e88b412bSchristos }
1631e88b412bSchristos 
1632e88b412bSchristos /* Implement the "sw_breakpoint_from_kind" gdbarch method.  */
1633e88b412bSchristos 
1634e88b412bSchristos static const gdb_byte *
arc_sw_breakpoint_from_kind(struct gdbarch * gdbarch,int kind,int * size)1635e88b412bSchristos arc_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
1636e88b412bSchristos {
1637e88b412bSchristos   *size = kind;
1638e88b412bSchristos 
1639e88b412bSchristos   if (kind == sizeof (arc_brk_le))
1640e88b412bSchristos     {
1641e88b412bSchristos       return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1642e88b412bSchristos 	      ? arc_brk_be
1643e88b412bSchristos 	      : arc_brk_le);
1644e88b412bSchristos     }
1645e88b412bSchristos   else
1646e88b412bSchristos     {
1647e88b412bSchristos       return ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1648e88b412bSchristos 	      ? arc_brk_s_be
1649e88b412bSchristos 	      : arc_brk_s_le);
1650e88b412bSchristos     }
1651e88b412bSchristos }
1652e88b412bSchristos 
1653e88b412bSchristos /* Implement the "frame_align" gdbarch method.  */
1654e88b412bSchristos 
1655e88b412bSchristos static CORE_ADDR
arc_frame_align(struct gdbarch * gdbarch,CORE_ADDR sp)1656e88b412bSchristos arc_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1657e88b412bSchristos {
1658e88b412bSchristos   return align_down (sp, 4);
1659e88b412bSchristos }
1660e88b412bSchristos 
1661e88b412bSchristos /* Dump the frame info.  Used for internal debugging only.  */
1662e88b412bSchristos 
1663e88b412bSchristos static void
arc_print_frame_cache(struct gdbarch * gdbarch,const char * message,struct arc_frame_cache * cache,int addresses_known)1664e88b412bSchristos arc_print_frame_cache (struct gdbarch *gdbarch, const char *message,
1665e88b412bSchristos 		       struct arc_frame_cache *cache, int addresses_known)
1666e88b412bSchristos {
1667e88b412bSchristos   debug_printf ("arc: frame_info %s\n", message);
1668e88b412bSchristos   debug_printf ("arc: prev_sp = %s\n", paddress (gdbarch, cache->prev_sp));
1669e88b412bSchristos   debug_printf ("arc: frame_base_reg = %i\n", cache->frame_base_reg);
1670e88b412bSchristos   debug_printf ("arc: frame_base_offset = %s\n",
1671e88b412bSchristos 		plongest (cache->frame_base_offset));
1672e88b412bSchristos 
1673e88b412bSchristos   for (int i = 0; i <= ARC_BLINK_REGNUM; i++)
1674e88b412bSchristos     {
1675e88b412bSchristos       if (trad_frame_addr_p (cache->saved_regs, i))
1676e88b412bSchristos 	debug_printf ("arc: saved register %s at %s %s\n",
1677e88b412bSchristos 		      gdbarch_register_name (gdbarch, i),
1678e88b412bSchristos 		      (addresses_known) ? "address" : "offset",
1679e88b412bSchristos 		      paddress (gdbarch, cache->saved_regs[i].addr));
1680e88b412bSchristos     }
1681e88b412bSchristos }
1682e88b412bSchristos 
1683e88b412bSchristos /* Frame unwinder for normal frames.  */
1684e88b412bSchristos 
1685e88b412bSchristos static struct arc_frame_cache *
arc_make_frame_cache(struct frame_info * this_frame)1686e88b412bSchristos arc_make_frame_cache (struct frame_info *this_frame)
1687e88b412bSchristos {
1688e88b412bSchristos   if (arc_debug)
1689e88b412bSchristos     debug_printf ("arc: frame_cache\n");
1690e88b412bSchristos 
1691e88b412bSchristos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1692e88b412bSchristos 
1693e88b412bSchristos   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1694e88b412bSchristos   CORE_ADDR entrypoint, prologue_end;
1695e88b412bSchristos   if (find_pc_partial_function (block_addr, NULL, &entrypoint, &prologue_end))
1696e88b412bSchristos     {
1697e88b412bSchristos       struct symtab_and_line sal = find_pc_line (entrypoint, 0);
1698e88b412bSchristos       CORE_ADDR prev_pc = get_frame_pc (this_frame);
1699e88b412bSchristos       if (sal.line == 0)
1700e88b412bSchristos 	/* No line info so use current PC.  */
1701e88b412bSchristos 	prologue_end = prev_pc;
1702e88b412bSchristos       else if (sal.end < prologue_end)
1703e88b412bSchristos 	/* The next line begins after the function end.  */
1704e88b412bSchristos 	prologue_end = sal.end;
1705e88b412bSchristos 
1706e88b412bSchristos       prologue_end = std::min (prologue_end, prev_pc);
1707e88b412bSchristos     }
1708e88b412bSchristos   else
1709e88b412bSchristos     {
1710e88b412bSchristos       /* If find_pc_partial_function returned nothing then there is no symbol
1711e88b412bSchristos 	 information at all for this PC.  Currently it is assumed in this case
1712e88b412bSchristos 	 that current PC is entrypoint to function and try to construct the
1713e88b412bSchristos 	 frame from that.  This is, probably, suboptimal, for example ARM
1714e88b412bSchristos 	 assumes in this case that program is inside the normal frame (with
1715e88b412bSchristos 	 frame pointer).  ARC, perhaps, should try to do the same.  */
1716e88b412bSchristos       entrypoint = get_frame_register_unsigned (this_frame,
1717e88b412bSchristos 						gdbarch_pc_regnum (gdbarch));
1718e88b412bSchristos       prologue_end = entrypoint + MAX_PROLOGUE_LENGTH;
1719e88b412bSchristos     }
1720e88b412bSchristos 
1721e88b412bSchristos   /* Allocate new frame cache instance and space for saved register info.
1722e88b412bSchristos      FRAME_OBSTACK_ZALLOC will initialize fields to zeroes.  */
1723e88b412bSchristos   struct arc_frame_cache *cache
1724e88b412bSchristos     = FRAME_OBSTACK_ZALLOC (struct arc_frame_cache);
1725e88b412bSchristos   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1726e88b412bSchristos 
1727e88b412bSchristos   arc_analyze_prologue (gdbarch, entrypoint, prologue_end, cache);
1728e88b412bSchristos 
1729e88b412bSchristos   if (arc_debug)
1730e88b412bSchristos     arc_print_frame_cache (gdbarch, "after prologue", cache, false);
1731e88b412bSchristos 
1732e88b412bSchristos   CORE_ADDR unwound_fb = get_frame_register_unsigned (this_frame,
1733e88b412bSchristos 						      cache->frame_base_reg);
1734e88b412bSchristos   if (unwound_fb == 0)
1735e88b412bSchristos     return cache;
1736e88b412bSchristos   cache->prev_sp = unwound_fb + cache->frame_base_offset;
1737e88b412bSchristos 
1738e88b412bSchristos   for (int i = 0; i <= ARC_LAST_CORE_REGNUM; i++)
1739e88b412bSchristos     {
1740e88b412bSchristos       if (trad_frame_addr_p (cache->saved_regs, i))
1741e88b412bSchristos 	cache->saved_regs[i].addr += cache->prev_sp;
1742e88b412bSchristos     }
1743e88b412bSchristos 
1744e88b412bSchristos   if (arc_debug)
1745e88b412bSchristos     arc_print_frame_cache (gdbarch, "after previous SP found", cache, true);
1746e88b412bSchristos 
1747e88b412bSchristos   return cache;
1748e88b412bSchristos }
1749e88b412bSchristos 
1750e88b412bSchristos /* Implement the "this_id" frame_unwind method.  */
1751e88b412bSchristos 
1752e88b412bSchristos static void
arc_frame_this_id(struct frame_info * this_frame,void ** this_cache,struct frame_id * this_id)1753e88b412bSchristos arc_frame_this_id (struct frame_info *this_frame, void **this_cache,
1754e88b412bSchristos 		   struct frame_id *this_id)
1755e88b412bSchristos {
1756e88b412bSchristos   if (arc_debug)
1757e88b412bSchristos     debug_printf ("arc: frame_this_id\n");
1758e88b412bSchristos 
1759e88b412bSchristos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1760e88b412bSchristos 
1761e88b412bSchristos   if (*this_cache == NULL)
1762e88b412bSchristos     *this_cache = arc_make_frame_cache (this_frame);
1763e88b412bSchristos   struct arc_frame_cache *cache = (struct arc_frame_cache *) (*this_cache);
1764e88b412bSchristos 
1765e88b412bSchristos   CORE_ADDR stack_addr = cache->prev_sp;
1766e88b412bSchristos 
1767e88b412bSchristos   /* There are 4 possible situation which decide how frame_id->code_addr is
1768e88b412bSchristos      evaluated:
1769e88b412bSchristos 
1770e88b412bSchristos      1) Function is compiled with option -g.  Then frame_id will be created
1771e88b412bSchristos      in dwarf_* function and not in this function.  NB: even if target
1772e88b412bSchristos      binary is compiled with -g, some std functions like __start and _init
1773e88b412bSchristos      are not, so they still will follow one of the following choices.
1774e88b412bSchristos 
1775e88b412bSchristos      2) Function is compiled without -g and binary hasn't been stripped in
1776e88b412bSchristos      any way.  In this case GDB still has enough information to evaluate
1777e88b412bSchristos      frame code_addr properly.  This case is covered by call to
1778e88b412bSchristos      get_frame_func ().
1779e88b412bSchristos 
1780e88b412bSchristos      3) Binary has been striped with option -g (strip debug symbols).  In
1781e88b412bSchristos      this case there is still enough symbols for get_frame_func () to work
1782e88b412bSchristos      properly, so this case is also covered by it.
1783e88b412bSchristos 
1784e88b412bSchristos      4) Binary has been striped with option -s (strip all symbols).  In this
1785e88b412bSchristos      case GDB cannot get function start address properly, so we return current
1786e88b412bSchristos      PC value instead.
1787e88b412bSchristos    */
1788e88b412bSchristos   CORE_ADDR code_addr = get_frame_func (this_frame);
1789e88b412bSchristos   if (code_addr == 0)
1790e88b412bSchristos     code_addr = get_frame_register_unsigned (this_frame,
1791e88b412bSchristos 					     gdbarch_pc_regnum (gdbarch));
1792e88b412bSchristos 
1793e88b412bSchristos   *this_id = frame_id_build (stack_addr, code_addr);
1794e88b412bSchristos }
1795e88b412bSchristos 
1796e88b412bSchristos /* Implement the "prev_register" frame_unwind method.  */
1797e88b412bSchristos 
1798e88b412bSchristos static struct value *
arc_frame_prev_register(struct frame_info * this_frame,void ** this_cache,int regnum)1799e88b412bSchristos arc_frame_prev_register (struct frame_info *this_frame,
1800e88b412bSchristos 			 void **this_cache, int regnum)
1801e88b412bSchristos {
1802e88b412bSchristos   if (*this_cache == NULL)
1803e88b412bSchristos     *this_cache = arc_make_frame_cache (this_frame);
1804e88b412bSchristos   struct arc_frame_cache *cache = (struct arc_frame_cache *) (*this_cache);
1805e88b412bSchristos 
1806e88b412bSchristos   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1807e88b412bSchristos 
1808e88b412bSchristos   /* If we are asked to unwind the PC, then we need to return BLINK instead:
1809e88b412bSchristos      the saved value of PC points into this frame's function's prologue, not
1810e88b412bSchristos      the next frame's function's resume location.  */
1811e88b412bSchristos   if (regnum == gdbarch_pc_regnum (gdbarch))
1812e88b412bSchristos     regnum = ARC_BLINK_REGNUM;
1813e88b412bSchristos 
1814e88b412bSchristos   /* SP is a special case - we should return prev_sp, because
1815e88b412bSchristos      trad_frame_get_prev_register will return _current_ SP value.
1816e88b412bSchristos      Alternatively we could have stored cache->prev_sp in the cache->saved
1817e88b412bSchristos      regs, but here we follow the lead of AArch64, ARM and Xtensa and will
1818e88b412bSchristos      leave that logic in this function, instead of prologue analyzers.  That I
1819e88b412bSchristos      think is a bit more clear as `saved_regs` should contain saved regs, not
1820e88b412bSchristos      computable.
1821e88b412bSchristos 
1822e88b412bSchristos      Because value has been computed, "got_constant" should be used, so that
1823e88b412bSchristos      returned value will be a "not_lval" - immutable.  */
1824e88b412bSchristos 
1825e88b412bSchristos   if (regnum == gdbarch_sp_regnum (gdbarch))
1826e88b412bSchristos     return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp);
1827e88b412bSchristos 
1828e88b412bSchristos   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
1829e88b412bSchristos }
1830e88b412bSchristos 
1831e88b412bSchristos /* Implement the "init_reg" dwarf2_frame method.  */
1832e88b412bSchristos 
1833e88b412bSchristos static void
arc_dwarf2_frame_init_reg(struct gdbarch * gdbarch,int regnum,struct dwarf2_frame_state_reg * reg,struct frame_info * info)1834e88b412bSchristos arc_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1835e88b412bSchristos 			   struct dwarf2_frame_state_reg *reg,
1836e88b412bSchristos 			   struct frame_info *info)
1837e88b412bSchristos {
1838e88b412bSchristos   if (regnum == gdbarch_pc_regnum (gdbarch))
1839e88b412bSchristos     /* The return address column.  */
1840e88b412bSchristos     reg->how = DWARF2_FRAME_REG_RA;
1841e88b412bSchristos   else if (regnum == gdbarch_sp_regnum (gdbarch))
1842e88b412bSchristos     /* The call frame address.  */
1843e88b412bSchristos     reg->how = DWARF2_FRAME_REG_CFA;
1844e88b412bSchristos }
1845e88b412bSchristos 
1846e88b412bSchristos /* Structure defining the ARC ordinary frame unwind functions.  Since we are
1847e88b412bSchristos    the fallback unwinder, we use the default frame sniffer, which always
1848e88b412bSchristos    accepts the frame.  */
1849e88b412bSchristos 
1850e88b412bSchristos static const struct frame_unwind arc_frame_unwind = {
1851e88b412bSchristos   NORMAL_FRAME,
1852e88b412bSchristos   default_frame_unwind_stop_reason,
1853e88b412bSchristos   arc_frame_this_id,
1854e88b412bSchristos   arc_frame_prev_register,
1855e88b412bSchristos   NULL,
1856e88b412bSchristos   default_frame_sniffer,
1857e88b412bSchristos   NULL,
1858e88b412bSchristos   NULL
1859e88b412bSchristos };
1860e88b412bSchristos 
1861e88b412bSchristos 
1862e88b412bSchristos static const struct frame_base arc_normal_base = {
1863e88b412bSchristos   &arc_frame_unwind,
1864e88b412bSchristos   arc_frame_base_address,
1865e88b412bSchristos   arc_frame_base_address,
1866e88b412bSchristos   arc_frame_base_address
1867e88b412bSchristos };
1868e88b412bSchristos 
1869*56bb7041Schristos static enum arc_isa
mach_type_to_arc_isa(const unsigned long mach)1870*56bb7041Schristos mach_type_to_arc_isa (const unsigned long mach)
1871*56bb7041Schristos {
1872*56bb7041Schristos   switch (mach)
1873*56bb7041Schristos     {
1874*56bb7041Schristos     case bfd_mach_arc_arc600:
1875*56bb7041Schristos     case bfd_mach_arc_arc601:
1876*56bb7041Schristos     case bfd_mach_arc_arc700:
1877*56bb7041Schristos       return ARC_ISA_ARCV1;
1878*56bb7041Schristos     case bfd_mach_arc_arcv2:
1879*56bb7041Schristos       return ARC_ISA_ARCV2;
1880*56bb7041Schristos     default:
1881*56bb7041Schristos 	internal_error (__FILE__, __LINE__,
1882*56bb7041Schristos 			_("unknown machine id %lu"), mach);
1883*56bb7041Schristos     }
1884*56bb7041Schristos }
1885*56bb7041Schristos 
1886*56bb7041Schristos /* Common construction code for ARC_GDBARCH_FEATURES struct.  If there
1887*56bb7041Schristos    is no ABFD, then a FEATURE with default values is returned.  */
1888*56bb7041Schristos 
1889*56bb7041Schristos static arc_gdbarch_features
arc_gdbarch_features_create(const bfd * abfd,const unsigned long mach)1890*56bb7041Schristos arc_gdbarch_features_create (const bfd *abfd, const unsigned long mach)
1891*56bb7041Schristos {
1892*56bb7041Schristos   /* Use 4 as a fallback value.  */
1893*56bb7041Schristos   int reg_size = 4;
1894*56bb7041Schristos 
1895*56bb7041Schristos   /* Try to guess the features parameters by looking at the binary to be
1896*56bb7041Schristos      executed.  If the user is providing a binary that does not match the
1897*56bb7041Schristos      target, then tough luck.  This is the last effort to makes sense of
1898*56bb7041Schristos      what's going on.  */
1899*56bb7041Schristos   if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1900*56bb7041Schristos     {
1901*56bb7041Schristos       unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
1902*56bb7041Schristos 
1903*56bb7041Schristos       if (eclass == ELFCLASS32)
1904*56bb7041Schristos 	reg_size = 4;
1905*56bb7041Schristos       else if (eclass == ELFCLASS64)
1906*56bb7041Schristos 	reg_size = 8;
1907*56bb7041Schristos       else
1908*56bb7041Schristos 	internal_error (__FILE__, __LINE__,
1909*56bb7041Schristos 			_("unknown ELF header class %d"), eclass);
1910*56bb7041Schristos     }
1911*56bb7041Schristos 
1912*56bb7041Schristos   /* MACH from a bfd_arch_info struct is used here.  It should be a safe
1913*56bb7041Schristos      bet, as it looks like the struct is always initialized even when we
1914*56bb7041Schristos      don't pass any elf file to GDB at all (it uses default arch in that
1915*56bb7041Schristos      case).  */
1916*56bb7041Schristos   arc_isa isa = mach_type_to_arc_isa (mach);
1917*56bb7041Schristos 
1918*56bb7041Schristos   return arc_gdbarch_features (reg_size, isa);
1919*56bb7041Schristos }
1920*56bb7041Schristos 
1921*56bb7041Schristos /* Look for obsolete core feature names in TDESC.  */
1922*56bb7041Schristos 
1923*56bb7041Schristos static const struct tdesc_feature *
find_obsolete_core_names(const struct target_desc * tdesc)1924*56bb7041Schristos find_obsolete_core_names (const struct target_desc *tdesc)
1925*56bb7041Schristos {
1926*56bb7041Schristos   const struct tdesc_feature *feat = nullptr;
1927*56bb7041Schristos 
1928*56bb7041Schristos   feat = tdesc_find_feature (tdesc, ARC_CORE_V1_OBSOLETE_FEATURE_NAME);
1929*56bb7041Schristos 
1930*56bb7041Schristos   if (feat == nullptr)
1931*56bb7041Schristos     feat = tdesc_find_feature (tdesc, ARC_CORE_V2_OBSOLETE_FEATURE_NAME);
1932*56bb7041Schristos 
1933*56bb7041Schristos   if (feat == nullptr)
1934*56bb7041Schristos     feat = tdesc_find_feature
1935*56bb7041Schristos       (tdesc, ARC_CORE_V2_REDUCED_OBSOLETE_FEATURE_NAME);
1936*56bb7041Schristos 
1937*56bb7041Schristos   return feat;
1938*56bb7041Schristos }
1939*56bb7041Schristos 
1940*56bb7041Schristos /* Look for obsolete aux feature names in TDESC.  */
1941*56bb7041Schristos 
1942*56bb7041Schristos static const struct tdesc_feature *
find_obsolete_aux_names(const struct target_desc * tdesc)1943*56bb7041Schristos find_obsolete_aux_names (const struct target_desc *tdesc)
1944*56bb7041Schristos {
1945*56bb7041Schristos   return tdesc_find_feature (tdesc, ARC_AUX_OBSOLETE_FEATURE_NAME);
1946*56bb7041Schristos }
1947*56bb7041Schristos 
1948*56bb7041Schristos /* Based on the MACH value, determines which core register features set
1949*56bb7041Schristos    must be used.  */
1950*56bb7041Schristos 
1951*56bb7041Schristos static arc_register_feature *
determine_core_reg_feature_set(const unsigned long mach)1952*56bb7041Schristos determine_core_reg_feature_set (const unsigned long mach)
1953*56bb7041Schristos {
1954*56bb7041Schristos   switch (mach_type_to_arc_isa (mach))
1955*56bb7041Schristos     {
1956*56bb7041Schristos     case ARC_ISA_ARCV1:
1957*56bb7041Schristos       return &arc_v1_core_reg_feature;
1958*56bb7041Schristos     case ARC_ISA_ARCV2:
1959*56bb7041Schristos       return &arc_v2_core_reg_feature;
1960*56bb7041Schristos     default:
1961*56bb7041Schristos       gdb_assert_not_reached
1962*56bb7041Schristos         ("Unknown machine type to determine the core feature set.");
1963*56bb7041Schristos     }
1964*56bb7041Schristos }
1965*56bb7041Schristos 
1966*56bb7041Schristos /* At the moment, there is only 1 auxiliary register features set.
1967*56bb7041Schristos    This is a place holder for future extendability.  */
1968*56bb7041Schristos 
1969*56bb7041Schristos static const arc_register_feature *
determine_aux_reg_feature_set()1970*56bb7041Schristos determine_aux_reg_feature_set ()
1971*56bb7041Schristos {
1972*56bb7041Schristos   return &arc_common_aux_reg_feature;
1973*56bb7041Schristos }
1974*56bb7041Schristos 
1975*56bb7041Schristos /* Update accumulator register names (ACCH/ACCL) for r58 and r59 in the
1976*56bb7041Schristos    register sets.  The endianness determines the assignment:
1977*56bb7041Schristos 
1978*56bb7041Schristos         ,------.------.
1979*56bb7041Schristos         | acch | accl |
1980*56bb7041Schristos    ,----|------+------|
1981*56bb7041Schristos    | LE | r59  | r58  |
1982*56bb7041Schristos    | BE | r58  | r59  |
1983*56bb7041Schristos    `----^------^------'  */
1984*56bb7041Schristos 
1985*56bb7041Schristos static void
arc_update_acc_reg_names(const int byte_order)1986*56bb7041Schristos arc_update_acc_reg_names (const int byte_order)
1987*56bb7041Schristos {
1988*56bb7041Schristos   const char *r58_alias
1989*56bb7041Schristos     = byte_order == BFD_ENDIAN_LITTLE ? "accl" : "acch";
1990*56bb7041Schristos   const char *r59_alias
1991*56bb7041Schristos     = byte_order == BFD_ENDIAN_LITTLE ? "acch" : "accl";
1992*56bb7041Schristos 
1993*56bb7041Schristos   /* Subscript 1 must be OK because those registers have 2 names.  */
1994*56bb7041Schristos   arc_v1_core_reg_feature.registers[ARC_R58_REGNUM].names[1] = r58_alias;
1995*56bb7041Schristos   arc_v1_core_reg_feature.registers[ARC_R59_REGNUM].names[1] = r59_alias;
1996*56bb7041Schristos   arc_v2_core_reg_feature.registers[ARC_R58_REGNUM].names[1] = r58_alias;
1997*56bb7041Schristos   arc_v2_core_reg_feature.registers[ARC_R59_REGNUM].names[1] = r59_alias;
1998*56bb7041Schristos }
1999*56bb7041Schristos 
2000*56bb7041Schristos /* Go through all the registers in REG_SET and check if they exist
2001*56bb7041Schristos    in FEATURE.  The TDESC_DATA is updated with the register number
2002*56bb7041Schristos    in REG_SET if it is found in the feature.  If a required register
2003*56bb7041Schristos    is not found, this function returns false.  */
2004*56bb7041Schristos 
2005*56bb7041Schristos static bool
arc_check_tdesc_feature(struct tdesc_arch_data * tdesc_data,const struct tdesc_feature * feature,const struct arc_register_feature * reg_set)2006*56bb7041Schristos arc_check_tdesc_feature (struct tdesc_arch_data *tdesc_data,
2007*56bb7041Schristos 			 const struct tdesc_feature *feature,
2008*56bb7041Schristos 			 const struct arc_register_feature *reg_set)
2009*56bb7041Schristos {
2010*56bb7041Schristos   for (const auto &reg : reg_set->registers)
2011*56bb7041Schristos     {
2012*56bb7041Schristos       bool found = false;
2013*56bb7041Schristos 
2014*56bb7041Schristos       for (const char *name : reg.names)
2015*56bb7041Schristos 	{
2016*56bb7041Schristos 	  found
2017*56bb7041Schristos 	    = tdesc_numbered_register (feature, tdesc_data, reg.regnum, name);
2018*56bb7041Schristos 
2019*56bb7041Schristos 	  if (found)
2020*56bb7041Schristos 	    break;
2021*56bb7041Schristos 	}
2022*56bb7041Schristos 
2023*56bb7041Schristos       if (!found && reg.required_p)
2024*56bb7041Schristos 	{
2025*56bb7041Schristos 	  std::ostringstream reg_names;
2026*56bb7041Schristos 	  for (std::size_t i = 0; i < reg.names.size(); ++i)
2027*56bb7041Schristos 	    {
2028*56bb7041Schristos 	      if (i == 0)
2029*56bb7041Schristos 		reg_names << "'" << reg.names[0] << "'";
2030*56bb7041Schristos 	      else
2031*56bb7041Schristos 		reg_names << " or '" << reg.names[0] << "'";
2032*56bb7041Schristos 	    }
2033*56bb7041Schristos 	  arc_print (_("Error: Cannot find required register(s) %s "
2034*56bb7041Schristos 		       "in feature '%s'.\n"), reg_names.str ().c_str (),
2035*56bb7041Schristos 		       feature->name.c_str ());
2036*56bb7041Schristos 	  return false;
2037*56bb7041Schristos 	}
2038*56bb7041Schristos     }
2039*56bb7041Schristos 
2040*56bb7041Schristos   return true;
2041*56bb7041Schristos }
2042*56bb7041Schristos 
2043*56bb7041Schristos /* Check for the existance of "lp_start" and "lp_end" in target description.
2044*56bb7041Schristos    If both are present, assume there is hardware loop support in the target.
2045*56bb7041Schristos    This can be improved by looking into "lpc_size" field of "isa_config"
2046*56bb7041Schristos    auxiliary register.  */
2047*56bb7041Schristos 
2048*56bb7041Schristos static bool
arc_check_for_hw_loops(const struct target_desc * tdesc,struct tdesc_arch_data * data)2049*56bb7041Schristos arc_check_for_hw_loops (const struct target_desc *tdesc,
2050*56bb7041Schristos 			struct tdesc_arch_data *data)
2051*56bb7041Schristos {
2052*56bb7041Schristos   const auto feature_aux = tdesc_find_feature (tdesc, ARC_AUX_FEATURE_NAME);
2053*56bb7041Schristos   const auto aux_regset = determine_aux_reg_feature_set ();
2054*56bb7041Schristos 
2055*56bb7041Schristos   if (feature_aux == nullptr)
2056*56bb7041Schristos     return false;
2057*56bb7041Schristos 
2058*56bb7041Schristos   bool hw_loop_p = false;
2059*56bb7041Schristos   const auto lp_start_name =
2060*56bb7041Schristos     aux_regset->registers[ARC_LP_START_REGNUM - ARC_FIRST_AUX_REGNUM].names[0];
2061*56bb7041Schristos   const auto lp_end_name =
2062*56bb7041Schristos     aux_regset->registers[ARC_LP_END_REGNUM - ARC_FIRST_AUX_REGNUM].names[0];
2063*56bb7041Schristos 
2064*56bb7041Schristos   hw_loop_p = tdesc_numbered_register (feature_aux, data,
2065*56bb7041Schristos 				       ARC_LP_START_REGNUM, lp_start_name);
2066*56bb7041Schristos   hw_loop_p &= tdesc_numbered_register (feature_aux, data,
2067*56bb7041Schristos 				       ARC_LP_END_REGNUM, lp_end_name);
2068*56bb7041Schristos 
2069*56bb7041Schristos   return hw_loop_p;
2070*56bb7041Schristos }
2071*56bb7041Schristos 
2072e88b412bSchristos /* Initialize target description for the ARC.
2073e88b412bSchristos 
2074*56bb7041Schristos    Returns true if input TDESC was valid and in this case it will assign TDESC
2075e88b412bSchristos    and TDESC_DATA output parameters.  */
2076e88b412bSchristos 
2077*56bb7041Schristos static bool
arc_tdesc_init(struct gdbarch_info info,const struct target_desc ** tdesc,struct tdesc_arch_data ** tdesc_data)2078e88b412bSchristos arc_tdesc_init (struct gdbarch_info info, const struct target_desc **tdesc,
2079e88b412bSchristos 		struct tdesc_arch_data **tdesc_data)
2080e88b412bSchristos {
2081*56bb7041Schristos   const struct target_desc *tdesc_loc = info.target_desc;
2082e88b412bSchristos   if (arc_debug)
2083e88b412bSchristos     debug_printf ("arc: Target description initialization.\n");
2084e88b412bSchristos 
2085*56bb7041Schristos   /* If target doesn't provide a description, use the default ones.  */
2086e88b412bSchristos   if (!tdesc_has_registers (tdesc_loc))
2087e88b412bSchristos     {
2088*56bb7041Schristos       arc_gdbarch_features features
2089*56bb7041Schristos 	= arc_gdbarch_features_create (info.abfd,
2090*56bb7041Schristos 				       info.bfd_arch_info->mach);
2091*56bb7041Schristos       tdesc_loc = arc_lookup_target_description (features);
2092*56bb7041Schristos     }
2093*56bb7041Schristos   gdb_assert (tdesc_loc != nullptr);
2094*56bb7041Schristos 
2095e88b412bSchristos   if (arc_debug)
2096*56bb7041Schristos     debug_printf ("arc: Have got a target description\n");
2097*56bb7041Schristos 
2098*56bb7041Schristos   const struct tdesc_feature *feature_core
2099*56bb7041Schristos     = tdesc_find_feature (tdesc_loc, ARC_CORE_FEATURE_NAME);
2100*56bb7041Schristos   const struct tdesc_feature *feature_aux
2101*56bb7041Schristos     = tdesc_find_feature (tdesc_loc, ARC_AUX_FEATURE_NAME);
2102*56bb7041Schristos 
2103*56bb7041Schristos   /* Maybe there still is a chance to salvage the input.  */
2104*56bb7041Schristos   if (feature_core == nullptr)
2105*56bb7041Schristos     feature_core = find_obsolete_core_names (tdesc_loc);
2106*56bb7041Schristos   if (feature_aux == nullptr)
2107*56bb7041Schristos     feature_aux = find_obsolete_aux_names (tdesc_loc);
2108*56bb7041Schristos 
2109*56bb7041Schristos   if (feature_core == nullptr)
2110e88b412bSchristos     {
2111*56bb7041Schristos       arc_print (_("Error: Cannot find required feature '%s' in supplied "
2112*56bb7041Schristos 		   "target description.\n"), ARC_CORE_FEATURE_NAME);
2113*56bb7041Schristos       return false;
2114e88b412bSchristos     }
2115e88b412bSchristos 
2116*56bb7041Schristos   if (feature_aux == nullptr)
2117e88b412bSchristos     {
2118*56bb7041Schristos       arc_print (_("Error: Cannot find required feature '%s' in supplied "
2119*56bb7041Schristos 		   "target description.\n"), ARC_AUX_FEATURE_NAME);
2120*56bb7041Schristos       return false;
2121e88b412bSchristos     }
2122e88b412bSchristos 
2123*56bb7041Schristos   const arc_register_feature *arc_core_reg_feature
2124*56bb7041Schristos     = determine_core_reg_feature_set (info.bfd_arch_info->mach);
2125*56bb7041Schristos   const arc_register_feature *arc_aux_reg_feature
2126*56bb7041Schristos     = determine_aux_reg_feature_set ();
2127e88b412bSchristos 
2128e88b412bSchristos   struct tdesc_arch_data *tdesc_data_loc = tdesc_data_alloc ();
2129e88b412bSchristos 
2130*56bb7041Schristos   arc_update_acc_reg_names (info.byte_order);
2131e88b412bSchristos 
2132*56bb7041Schristos   bool valid_p = arc_check_tdesc_feature (tdesc_data_loc,
2133*56bb7041Schristos 					  feature_core,
2134*56bb7041Schristos 					  arc_core_reg_feature);
2135e88b412bSchristos 
2136*56bb7041Schristos   valid_p &= arc_check_tdesc_feature (tdesc_data_loc,
2137*56bb7041Schristos 				      feature_aux,
2138*56bb7041Schristos 				      arc_aux_reg_feature);
2139e88b412bSchristos 
2140e88b412bSchristos   if (!valid_p)
2141e88b412bSchristos     {
2142*56bb7041Schristos       if (arc_debug)
2143*56bb7041Schristos         debug_printf ("arc: Target description is not valid\n");
2144e88b412bSchristos       tdesc_data_cleanup (tdesc_data_loc);
2145*56bb7041Schristos       return false;
2146e88b412bSchristos     }
2147e88b412bSchristos 
2148e88b412bSchristos   *tdesc = tdesc_loc;
2149e88b412bSchristos   *tdesc_data = tdesc_data_loc;
2150e88b412bSchristos 
2151*56bb7041Schristos   return true;
2152e88b412bSchristos }
2153e88b412bSchristos 
21543aed4a8bSchristos /* Implement the type_align gdbarch function.  */
21553aed4a8bSchristos 
21563aed4a8bSchristos static ULONGEST
arc_type_align(struct gdbarch * gdbarch,struct type * type)21573aed4a8bSchristos arc_type_align (struct gdbarch *gdbarch, struct type *type)
21583aed4a8bSchristos {
2159*56bb7041Schristos   switch (type->code ())
2160*56bb7041Schristos     {
2161*56bb7041Schristos     case TYPE_CODE_PTR:
2162*56bb7041Schristos     case TYPE_CODE_FUNC:
2163*56bb7041Schristos     case TYPE_CODE_FLAGS:
2164*56bb7041Schristos     case TYPE_CODE_INT:
2165*56bb7041Schristos     case TYPE_CODE_RANGE:
2166*56bb7041Schristos     case TYPE_CODE_FLT:
2167*56bb7041Schristos     case TYPE_CODE_ENUM:
2168*56bb7041Schristos     case TYPE_CODE_REF:
2169*56bb7041Schristos     case TYPE_CODE_RVALUE_REF:
2170*56bb7041Schristos     case TYPE_CODE_CHAR:
2171*56bb7041Schristos     case TYPE_CODE_BOOL:
2172*56bb7041Schristos     case TYPE_CODE_DECFLOAT:
2173*56bb7041Schristos     case TYPE_CODE_METHODPTR:
2174*56bb7041Schristos     case TYPE_CODE_MEMBERPTR:
21753aed4a8bSchristos       type = check_typedef (type);
21763aed4a8bSchristos       return std::min<ULONGEST> (4, TYPE_LENGTH (type));
2177*56bb7041Schristos     default:
2178*56bb7041Schristos       return 0;
2179*56bb7041Schristos     }
21803aed4a8bSchristos }
21813aed4a8bSchristos 
2182e88b412bSchristos /* Implement the "init" gdbarch method.  */
2183e88b412bSchristos 
2184e88b412bSchristos static struct gdbarch *
arc_gdbarch_init(struct gdbarch_info info,struct gdbarch_list * arches)2185e88b412bSchristos arc_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2186e88b412bSchristos {
2187e88b412bSchristos   const struct target_desc *tdesc;
2188e88b412bSchristos   struct tdesc_arch_data *tdesc_data;
2189e88b412bSchristos 
2190e88b412bSchristos   if (arc_debug)
2191e88b412bSchristos     debug_printf ("arc: Architecture initialization.\n");
2192e88b412bSchristos 
2193e88b412bSchristos   if (!arc_tdesc_init (info, &tdesc, &tdesc_data))
2194*56bb7041Schristos     return nullptr;
2195e88b412bSchristos 
2196e88b412bSchristos   /* Allocate the ARC-private target-dependent information structure, and the
2197e88b412bSchristos      GDB target-independent information structure.  */
2198*56bb7041Schristos   gdb::unique_xmalloc_ptr<struct gdbarch_tdep> tdep
2199*56bb7041Schristos     (XCNEW (struct gdbarch_tdep));
2200e88b412bSchristos   tdep->jb_pc = -1; /* No longjmp support by default.  */
2201*56bb7041Schristos   tdep->has_hw_loops = arc_check_for_hw_loops (tdesc, tdesc_data);
2202*56bb7041Schristos   struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep.release ());
2203e88b412bSchristos 
2204e88b412bSchristos   /* Data types.  */
2205e88b412bSchristos   set_gdbarch_short_bit (gdbarch, 16);
2206e88b412bSchristos   set_gdbarch_int_bit (gdbarch, 32);
2207e88b412bSchristos   set_gdbarch_long_bit (gdbarch, 32);
2208e88b412bSchristos   set_gdbarch_long_long_bit (gdbarch, 64);
22093aed4a8bSchristos   set_gdbarch_type_align (gdbarch, arc_type_align);
2210e88b412bSchristos   set_gdbarch_float_bit (gdbarch, 32);
2211e88b412bSchristos   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
2212e88b412bSchristos   set_gdbarch_double_bit (gdbarch, 64);
2213e88b412bSchristos   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
2214e88b412bSchristos   set_gdbarch_ptr_bit (gdbarch, 32);
2215e88b412bSchristos   set_gdbarch_addr_bit (gdbarch, 32);
2216e88b412bSchristos   set_gdbarch_char_signed (gdbarch, 0);
2217e88b412bSchristos 
2218e88b412bSchristos   set_gdbarch_write_pc (gdbarch, arc_write_pc);
2219e88b412bSchristos 
2220e88b412bSchristos   set_gdbarch_virtual_frame_pointer (gdbarch, arc_virtual_frame_pointer);
2221e88b412bSchristos 
2222e88b412bSchristos   /* tdesc_use_registers expects gdbarch_num_regs to return number of registers
2223e88b412bSchristos      parsed by gdbarch_init, and then it will add all of the remaining
2224e88b412bSchristos      registers and will increase number of registers.  */
2225e88b412bSchristos   set_gdbarch_num_regs (gdbarch, ARC_LAST_REGNUM + 1);
2226e88b412bSchristos   set_gdbarch_num_pseudo_regs (gdbarch, 0);
2227e88b412bSchristos   set_gdbarch_sp_regnum (gdbarch, ARC_SP_REGNUM);
2228e88b412bSchristos   set_gdbarch_pc_regnum (gdbarch, ARC_PC_REGNUM);
2229e88b412bSchristos   set_gdbarch_ps_regnum (gdbarch, ARC_STATUS32_REGNUM);
2230e88b412bSchristos   set_gdbarch_fp0_regnum (gdbarch, -1);	/* No FPU registers.  */
2231e88b412bSchristos 
2232e88b412bSchristos   set_gdbarch_push_dummy_call (gdbarch, arc_push_dummy_call);
2233e88b412bSchristos   set_gdbarch_push_dummy_code (gdbarch, arc_push_dummy_code);
2234e88b412bSchristos 
2235e88b412bSchristos   set_gdbarch_cannot_fetch_register (gdbarch, arc_cannot_fetch_register);
2236e88b412bSchristos   set_gdbarch_cannot_store_register (gdbarch, arc_cannot_store_register);
2237e88b412bSchristos 
2238e88b412bSchristos   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2239e88b412bSchristos 
2240e88b412bSchristos   set_gdbarch_return_value (gdbarch, arc_return_value);
2241e88b412bSchristos 
2242e88b412bSchristos   set_gdbarch_skip_prologue (gdbarch, arc_skip_prologue);
2243e88b412bSchristos   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2244e88b412bSchristos 
2245e88b412bSchristos   set_gdbarch_breakpoint_kind_from_pc (gdbarch, arc_breakpoint_kind_from_pc);
2246e88b412bSchristos   set_gdbarch_sw_breakpoint_from_kind (gdbarch, arc_sw_breakpoint_from_kind);
2247e88b412bSchristos 
2248e88b412bSchristos   /* On ARC 600 BRK_S instruction advances PC, unlike other ARC cores.  */
2249e88b412bSchristos   if (!arc_mach_is_arc600 (gdbarch))
2250e88b412bSchristos     set_gdbarch_decr_pc_after_break (gdbarch, 0);
2251e88b412bSchristos   else
2252e88b412bSchristos     set_gdbarch_decr_pc_after_break (gdbarch, 2);
2253e88b412bSchristos 
2254e88b412bSchristos   set_gdbarch_frame_align (gdbarch, arc_frame_align);
2255e88b412bSchristos 
2256e88b412bSchristos   set_gdbarch_print_insn (gdbarch, arc_delayed_print_insn);
2257e88b412bSchristos 
2258e88b412bSchristos   set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
2259e88b412bSchristos 
2260e88b412bSchristos   /* "nonsteppable" watchpoint means that watchpoint triggers before
2261e88b412bSchristos      instruction is committed, therefore it is required to remove watchpoint
2262e88b412bSchristos      to step though instruction that triggers it.  ARC watchpoints trigger
2263e88b412bSchristos      only after instruction is committed, thus there is no need to remove
2264e88b412bSchristos      them.  In fact on ARC watchpoint for memory writes may trigger with more
2265e88b412bSchristos      significant delay, like one or two instructions, depending on type of
2266e88b412bSchristos      memory where write is performed (CCM or external) and next instruction
2267e88b412bSchristos      after the memory write.  */
2268e88b412bSchristos   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 0);
2269e88b412bSchristos 
2270e88b412bSchristos   /* This doesn't include possible long-immediate value.  */
2271e88b412bSchristos   set_gdbarch_max_insn_length (gdbarch, 4);
2272e88b412bSchristos 
2273e88b412bSchristos   /* Frame unwinders and sniffers.  */
2274e88b412bSchristos   dwarf2_frame_set_init_reg (gdbarch, arc_dwarf2_frame_init_reg);
2275e88b412bSchristos   dwarf2_append_unwinders (gdbarch);
2276e88b412bSchristos   frame_unwind_append_unwinder (gdbarch, &arc_frame_unwind);
2277e88b412bSchristos   frame_base_set_default (gdbarch, &arc_normal_base);
2278e88b412bSchristos 
2279e88b412bSchristos   /* Setup stuff specific to a particular environment (baremetal or Linux).
2280e88b412bSchristos      It can override functions set earlier.  */
2281e88b412bSchristos   gdbarch_init_osabi (info, gdbarch);
2282e88b412bSchristos 
2283*56bb7041Schristos   if (gdbarch_tdep (gdbarch)->jb_pc >= 0)
2284e88b412bSchristos     set_gdbarch_get_longjmp_target (gdbarch, arc_get_longjmp_target);
2285e88b412bSchristos 
22863aed4a8bSchristos   /* Disassembler options.  Enforce CPU if it was specified in XML target
22873aed4a8bSchristos      description, otherwise use default method of determining CPU (ELF private
22883aed4a8bSchristos      header).  */
22893aed4a8bSchristos   if (info.target_desc != NULL)
22903aed4a8bSchristos     {
22913aed4a8bSchristos       const struct bfd_arch_info *tdesc_arch
22923aed4a8bSchristos 	= tdesc_architecture (info.target_desc);
22933aed4a8bSchristos       if (tdesc_arch != NULL)
22943aed4a8bSchristos 	{
22953aed4a8bSchristos 	  xfree (arc_disassembler_options);
22963aed4a8bSchristos 	  /* FIXME: It is not really good to change disassembler options
22973aed4a8bSchristos 	     behind the scene, because that might override options
22983aed4a8bSchristos 	     specified by the user.  However as of now ARC doesn't support
22993aed4a8bSchristos 	     `set disassembler-options' hence this code is the only place
23003aed4a8bSchristos 	     where options are changed.  It also changes options for all
23013aed4a8bSchristos 	     existing gdbarches, which also can be problematic, if
23023aed4a8bSchristos 	     arc_gdbarch_init will start reusing existing gdbarch
23033aed4a8bSchristos 	     instances.  */
23043aed4a8bSchristos 	  /* Target description specifies a BFD architecture, which is
23053aed4a8bSchristos 	     different from ARC cpu, as accepted by disassembler (and most
23063aed4a8bSchristos 	     other ARC tools), because cpu values are much more fine grained -
23073aed4a8bSchristos 	     there can be multiple cpu values per single BFD architecture.  As
23083aed4a8bSchristos 	     a result this code should translate architecture to some cpu
23093aed4a8bSchristos 	     value.  Since there is no info on exact cpu configuration, it is
23103aed4a8bSchristos 	     best to use the most feature-rich CPU, so that disassembler will
23113aed4a8bSchristos 	     recognize all instructions available to the specified
23123aed4a8bSchristos 	     architecture.  */
23133aed4a8bSchristos 	  switch (tdesc_arch->mach)
23143aed4a8bSchristos 	    {
23153aed4a8bSchristos 	    case bfd_mach_arc_arc601:
23163aed4a8bSchristos 	      arc_disassembler_options = xstrdup ("cpu=arc601");
23173aed4a8bSchristos 	      break;
23183aed4a8bSchristos 	    case bfd_mach_arc_arc600:
23193aed4a8bSchristos 	      arc_disassembler_options = xstrdup ("cpu=arc600");
23203aed4a8bSchristos 	      break;
23213aed4a8bSchristos 	    case bfd_mach_arc_arc700:
23223aed4a8bSchristos 	      arc_disassembler_options = xstrdup ("cpu=arc700");
23233aed4a8bSchristos 	      break;
23243aed4a8bSchristos 	    case bfd_mach_arc_arcv2:
23253aed4a8bSchristos 	      /* Machine arcv2 has three arches: ARCv2, EM and HS; where ARCv2
23263aed4a8bSchristos 		 is treated as EM.  */
23273aed4a8bSchristos 	      if (arc_arch_is_hs (tdesc_arch))
23283aed4a8bSchristos 		arc_disassembler_options = xstrdup ("cpu=hs38_linux");
23293aed4a8bSchristos 	      else
23303aed4a8bSchristos 		arc_disassembler_options = xstrdup ("cpu=em4_fpuda");
23313aed4a8bSchristos 	      break;
23323aed4a8bSchristos 	    default:
23333aed4a8bSchristos 	      arc_disassembler_options = NULL;
23343aed4a8bSchristos 	      break;
23353aed4a8bSchristos 	    }
23363aed4a8bSchristos 	  set_gdbarch_disassembler_options (gdbarch,
23373aed4a8bSchristos 					    &arc_disassembler_options);
23383aed4a8bSchristos 	}
23393aed4a8bSchristos     }
23403aed4a8bSchristos 
2341e88b412bSchristos   tdesc_use_registers (gdbarch, tdesc, tdesc_data);
2342e88b412bSchristos 
2343e88b412bSchristos   return gdbarch;
2344e88b412bSchristos }
2345e88b412bSchristos 
2346e88b412bSchristos /* Implement the "dump_tdep" gdbarch method.  */
2347e88b412bSchristos 
2348e88b412bSchristos static void
arc_dump_tdep(struct gdbarch * gdbarch,struct ui_file * file)2349e88b412bSchristos arc_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
2350e88b412bSchristos {
2351e88b412bSchristos   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2352e88b412bSchristos 
2353e88b412bSchristos   fprintf_unfiltered (file, "arc_dump_tdep: jb_pc = %i\n", tdep->jb_pc);
2354e88b412bSchristos }
2355e88b412bSchristos 
2356e88b412bSchristos /* This command accepts single argument - address of instruction to
2357e88b412bSchristos    disassemble.  */
2358e88b412bSchristos 
2359e88b412bSchristos static void
dump_arc_instruction_command(const char * args,int from_tty)23603aed4a8bSchristos dump_arc_instruction_command (const char *args, int from_tty)
2361e88b412bSchristos {
2362e88b412bSchristos   struct value *val;
2363e88b412bSchristos   if (args != NULL && strlen (args) > 0)
2364e88b412bSchristos     val = evaluate_expression (parse_expression (args).get ());
2365e88b412bSchristos   else
2366e88b412bSchristos     val = access_value_history (0);
2367e88b412bSchristos   record_latest_value (val);
2368e88b412bSchristos 
2369e88b412bSchristos   CORE_ADDR address = value_as_address (val);
2370e88b412bSchristos   struct arc_instruction insn;
2371e88b412bSchristos   struct disassemble_info di = arc_disassemble_info (target_gdbarch ());
2372e88b412bSchristos   arc_insn_decode (address, &di, arc_delayed_print_insn, &insn);
2373e88b412bSchristos   arc_insn_dump (insn);
2374e88b412bSchristos }
2375e88b412bSchristos 
2376*56bb7041Schristos void _initialize_arc_tdep ();
2377e88b412bSchristos void
_initialize_arc_tdep()2378*56bb7041Schristos _initialize_arc_tdep ()
2379e88b412bSchristos {
2380e88b412bSchristos   gdbarch_register (bfd_arch_arc, arc_gdbarch_init, arc_dump_tdep);
2381e88b412bSchristos 
2382e88b412bSchristos   /* Register ARC-specific commands with gdb.  */
2383e88b412bSchristos 
2384e88b412bSchristos   /* Add root prefix command for "maintenance print arc" commands.  */
2385*56bb7041Schristos   add_show_prefix_cmd ("arc", class_maintenance,
2386e88b412bSchristos 		       _("ARC-specific maintenance commands for printing GDB "
2387e88b412bSchristos 			 "internal state."),
2388*56bb7041Schristos 		       &maintenance_print_arc_list, "maintenance print arc ",
2389*56bb7041Schristos 		       0, &maintenanceprintlist);
2390e88b412bSchristos 
2391e88b412bSchristos   add_cmd ("arc-instruction", class_maintenance,
2392e88b412bSchristos 	   dump_arc_instruction_command,
2393e88b412bSchristos 	   _("Dump arc_instruction structure for specified address."),
2394e88b412bSchristos 	   &maintenance_print_arc_list);
2395e88b412bSchristos 
2396e88b412bSchristos   /* Debug internals for ARC GDB.  */
2397e88b412bSchristos   add_setshow_zinteger_cmd ("arc", class_maintenance,
2398e88b412bSchristos 			    &arc_debug,
2399e88b412bSchristos 			    _("Set ARC specific debugging."),
2400e88b412bSchristos 			    _("Show ARC specific debugging."),
2401e88b412bSchristos 			    _("Non-zero enables ARC specific debugging."),
2402e88b412bSchristos 			    NULL, NULL, &setdebuglist, &showdebuglist);
2403e88b412bSchristos }
2404