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