Automatic date update in version.in
[binutils-gdb.git] / gdb / or1k-tdep.c
blobd4ac0ac1b9d146036f075b4ab53fe803c66157ac
1 /* Target-dependent code for the 32-bit OpenRISC 1000, for the GDB.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "frame.h"
20 #include "inferior.h"
21 #include "symtab.h"
22 #include "value.h"
23 #include "gdbcmd.h"
24 #include "language.h"
25 #include "gdbcore.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "gdbtypes.h"
29 #include "target.h"
30 #include "regcache.h"
31 #include "gdbsupport/gdb-safe-ctype.h"
32 #include "reggroups.h"
33 #include "arch-utils.h"
34 #include "frame-unwind.h"
35 #include "frame-base.h"
36 #include "dwarf2/frame.h"
37 #include "trad-frame.h"
38 #include "regset.h"
39 #include "remote.h"
40 #include "target-descriptions.h"
41 #include <inttypes.h>
42 #include "dis-asm.h"
43 #include "gdbarch.h"
45 /* OpenRISC specific includes. */
46 #include "or1k-tdep.h"
47 #include "features/or1k.c"
50 /* Global debug flag. */
52 static bool or1k_debug = false;
54 static void
55 show_or1k_debug (struct ui_file *file, int from_tty,
56 struct cmd_list_element *c, const char *value)
58 gdb_printf (file, _("OpenRISC debugging is %s.\n"), value);
62 /* The target-dependent structure for gdbarch. */
64 struct or1k_gdbarch_tdep : gdbarch_tdep_base
66 int bytes_per_word = 0;
67 int bytes_per_address = 0;
68 CGEN_CPU_DESC gdb_cgen_cpu_desc = nullptr;
71 /* Support functions for the architecture definition. */
73 /* Get an instruction from memory. */
75 static ULONGEST
76 or1k_fetch_instruction (struct gdbarch *gdbarch, CORE_ADDR addr)
78 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
79 gdb_byte buf[OR1K_INSTLEN];
81 if (target_read_code (addr, buf, OR1K_INSTLEN)) {
82 memory_error (TARGET_XFER_E_IO, addr);
85 return extract_unsigned_integer (buf, OR1K_INSTLEN, byte_order);
88 /* Generic function to read bits from an instruction. */
90 static bool
91 or1k_analyse_inst (uint32_t inst, const char *format, ...)
93 /* Break out each field in turn, validating as we go. */
94 va_list ap;
95 int i;
96 int iptr = 0; /* Instruction pointer */
98 va_start (ap, format);
100 for (i = 0; 0 != format[i];)
102 const char *start_ptr;
103 char *end_ptr;
105 uint32_t bits; /* Bit substring of interest */
106 uint32_t width; /* Substring width */
107 uint32_t *arg_ptr;
109 switch (format[i])
111 case ' ':
112 i++;
113 break; /* Formatting: ignored */
115 case '0':
116 case '1': /* Constant bit field */
117 bits = (inst >> (OR1K_INSTBITLEN - iptr - 1)) & 0x1;
119 if ((format[i] - '0') != bits)
120 return false;
122 iptr++;
123 i++;
124 break;
126 case '%': /* Bit field */
127 i++;
128 start_ptr = &(format[i]);
129 width = strtoul (start_ptr, &end_ptr, 10);
131 /* Check we got something, and if so skip on. */
132 if (start_ptr == end_ptr)
133 error (_("bitstring \"%s\" at offset %d has no length field."),
134 format, i);
136 i += end_ptr - start_ptr;
138 /* Look for and skip the terminating 'b'. If it's not there, we
139 still give a fatal error, because these are fixed strings that
140 just should not be wrong. */
141 if ('b' != format[i++])
142 error (_("bitstring \"%s\" at offset %d has no terminating 'b'."),
143 format, i);
145 /* Break out the field. There is a special case with a bit width
146 of 32. */
147 if (32 == width)
148 bits = inst;
149 else
150 bits =
151 (inst >> (OR1K_INSTBITLEN - iptr - width)) & ((1 << width) - 1);
153 arg_ptr = va_arg (ap, uint32_t *);
154 *arg_ptr = bits;
155 iptr += width;
156 break;
158 default:
159 error (_("invalid character in bitstring \"%s\" at offset %d."),
160 format, i);
161 break;
165 /* Is the length OK? */
166 gdb_assert (OR1K_INSTBITLEN == iptr);
168 return true; /* Success */
171 /* This is used to parse l.addi instructions during various prologue
172 analysis routines. The l.addi instruction has semantics:
174 assembly: l.addi rD,rA,I
175 implementation: rD = rA + sign_extend(Immediate)
177 The rd_ptr, ra_ptr and simm_ptr must be non NULL pointers and are used
178 to store the parse results. Upon successful parsing true is returned,
179 false on failure. */
181 static bool
182 or1k_analyse_l_addi (uint32_t inst, unsigned int *rd_ptr,
183 unsigned int *ra_ptr, int *simm_ptr)
185 /* Instruction fields */
186 uint32_t rd, ra, i;
188 if (or1k_analyse_inst (inst, "10 0111 %5b %5b %16b", &rd, &ra, &i))
190 /* Found it. Construct the result fields. */
191 *rd_ptr = (unsigned int) rd;
192 *ra_ptr = (unsigned int) ra;
193 *simm_ptr = (int) (((i & 0x8000) == 0x8000) ? 0xffff0000 | i : i);
195 return true; /* Success */
197 else
198 return false; /* Failure */
201 /* This is used to to parse store instructions during various prologue
202 analysis routines. The l.sw instruction has semantics:
204 assembly: l.sw I(rA),rB
205 implementation: store rB contents to memory at effective address of
206 rA + sign_extend(Immediate)
208 The simm_ptr, ra_ptr and rb_ptr must be non NULL pointers and are used
209 to store the parse results. Upon successful parsing true is returned,
210 false on failure. */
212 static bool
213 or1k_analyse_l_sw (uint32_t inst, int *simm_ptr, unsigned int *ra_ptr,
214 unsigned int *rb_ptr)
216 /* Instruction fields */
217 uint32_t ihi, ilo, ra, rb;
219 if (or1k_analyse_inst (inst, "11 0101 %5b %5b %5b %11b", &ihi, &ra, &rb,
220 &ilo))
223 /* Found it. Construct the result fields. */
224 *simm_ptr = (int) ((ihi << 11) | ilo);
225 *simm_ptr |= ((ihi & 0x10) == 0x10) ? 0xffff0000 : 0;
227 *ra_ptr = (unsigned int) ra;
228 *rb_ptr = (unsigned int) rb;
230 return true; /* Success */
232 else
233 return false; /* Failure */
237 /* Functions defining the architecture. */
239 /* Implement the return_value gdbarch method. */
241 static enum return_value_convention
242 or1k_return_value (struct gdbarch *gdbarch, struct value *functype,
243 struct type *valtype, struct regcache *regcache,
244 gdb_byte *readbuf, const gdb_byte *writebuf)
246 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
247 enum type_code rv_type = valtype->code ();
248 unsigned int rv_size = valtype->length ();
249 or1k_gdbarch_tdep *tdep = gdbarch_tdep<or1k_gdbarch_tdep> (gdbarch);
250 int bpw = tdep->bytes_per_word;
252 /* Deal with struct/union as addresses. If an array won't fit in a
253 single register it is returned as address. Anything larger than 2
254 registers needs to also be passed as address (matches gcc
255 default_return_in_memory). */
256 if ((TYPE_CODE_STRUCT == rv_type) || (TYPE_CODE_UNION == rv_type)
257 || ((TYPE_CODE_ARRAY == rv_type) && (rv_size > bpw))
258 || (rv_size > 2 * bpw))
260 if (readbuf != NULL)
262 ULONGEST tmp;
264 regcache_cooked_read_unsigned (regcache, OR1K_RV_REGNUM, &tmp);
265 read_memory (tmp, readbuf, rv_size);
267 if (writebuf != NULL)
269 ULONGEST tmp;
271 regcache_cooked_read_unsigned (regcache, OR1K_RV_REGNUM, &tmp);
272 write_memory (tmp, writebuf, rv_size);
275 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
278 if (rv_size <= bpw)
280 /* Up to one word scalars are returned in R11. */
281 if (readbuf != NULL)
283 ULONGEST tmp;
285 regcache_cooked_read_unsigned (regcache, OR1K_RV_REGNUM, &tmp);
286 store_unsigned_integer (readbuf, rv_size, byte_order, tmp);
289 if (writebuf != NULL)
291 gdb_byte *buf = XCNEWVEC(gdb_byte, bpw);
293 if (BFD_ENDIAN_BIG == byte_order)
294 memcpy (buf + (sizeof (gdb_byte) * bpw) - rv_size, writebuf,
295 rv_size);
296 else
297 memcpy (buf, writebuf, rv_size);
299 regcache->cooked_write (OR1K_RV_REGNUM, buf);
301 free (buf);
304 else
306 /* 2 word scalars are returned in r11/r12 (with the MS word in r11). */
307 if (readbuf != NULL)
309 ULONGEST tmp_lo;
310 ULONGEST tmp_hi;
311 ULONGEST tmp;
313 regcache_cooked_read_unsigned (regcache, OR1K_RV_REGNUM,
314 &tmp_hi);
315 regcache_cooked_read_unsigned (regcache, OR1K_RV_REGNUM + 1,
316 &tmp_lo);
317 tmp = (tmp_hi << (bpw * 8)) | tmp_lo;
319 store_unsigned_integer (readbuf, rv_size, byte_order, tmp);
321 if (writebuf != NULL)
323 gdb_byte *buf_lo = XCNEWVEC(gdb_byte, bpw);
324 gdb_byte *buf_hi = XCNEWVEC(gdb_byte, bpw);
326 /* This is cheating. We assume that we fit in 2 words exactly,
327 which wouldn't work if we had (say) a 6-byte scalar type on a
328 big endian architecture (with the OpenRISC 1000 usually is). */
329 memcpy (buf_hi, writebuf, rv_size - bpw);
330 memcpy (buf_lo, writebuf + bpw, bpw);
332 regcache->cooked_write (OR1K_RV_REGNUM, buf_hi);
333 regcache->cooked_write (OR1K_RV_REGNUM + 1, buf_lo);
335 free (buf_lo);
336 free (buf_hi);
340 return RETURN_VALUE_REGISTER_CONVENTION;
343 /* OR1K always uses a l.trap instruction for breakpoints. */
345 constexpr gdb_byte or1k_break_insn[] = {0x21, 0x00, 0x00, 0x01};
347 typedef BP_MANIPULATION (or1k_break_insn) or1k_breakpoint;
349 static int
350 or1k_delay_slot_p (struct gdbarch *gdbarch, CORE_ADDR pc)
352 const CGEN_INSN *insn;
353 CGEN_FIELDS tmp_fields;
354 or1k_gdbarch_tdep *tdep = gdbarch_tdep<or1k_gdbarch_tdep> (gdbarch);
356 insn = cgen_lookup_insn (tdep->gdb_cgen_cpu_desc,
357 NULL,
358 or1k_fetch_instruction (gdbarch, pc),
359 NULL, 32, &tmp_fields, 0);
361 /* NULL here would mean the last instruction was not understood by cgen.
362 This should not usually happen, but if it does it's not a delay slot. */
363 if (insn == NULL)
364 return 0;
366 /* TODO: we should add a delay slot flag to the CGEN_INSN and remove
367 this hard coded test. */
368 return ((CGEN_INSN_NUM (insn) == OR1K_INSN_L_J)
369 || (CGEN_INSN_NUM (insn) == OR1K_INSN_L_JAL)
370 || (CGEN_INSN_NUM (insn) == OR1K_INSN_L_JR)
371 || (CGEN_INSN_NUM (insn) == OR1K_INSN_L_JALR)
372 || (CGEN_INSN_NUM (insn) == OR1K_INSN_L_BNF)
373 || (CGEN_INSN_NUM (insn) == OR1K_INSN_L_BF));
376 /* Implement the single_step_through_delay gdbarch method. */
378 static int
379 or1k_single_step_through_delay (struct gdbarch *gdbarch,
380 const frame_info_ptr &this_frame)
382 ULONGEST val;
383 CORE_ADDR ppc;
384 CORE_ADDR npc;
385 regcache *regcache = get_thread_regcache (inferior_thread ());
387 /* Get the previous and current instruction addresses. If they are not
388 adjacent, we cannot be in a delay slot. */
389 regcache_cooked_read_unsigned (regcache, OR1K_PPC_REGNUM, &val);
390 ppc = (CORE_ADDR) val;
391 regcache_cooked_read_unsigned (regcache, OR1K_NPC_REGNUM, &val);
392 npc = (CORE_ADDR) val;
394 if (0x4 != (npc - ppc))
395 return 0;
397 return or1k_delay_slot_p (gdbarch, ppc);
400 /* or1k_software_single_step() is called just before we want to resume
401 the inferior, if we want to single-step it but there is no hardware
402 or kernel single-step support (OpenRISC on GNU/Linux for example). We
403 find the target of the coming instruction skipping over delay slots
404 and breakpoint it. */
406 std::vector<CORE_ADDR>
407 or1k_software_single_step (struct regcache *regcache)
409 struct gdbarch *gdbarch = regcache->arch ();
410 CORE_ADDR pc, next_pc;
412 pc = regcache_read_pc (regcache);
413 next_pc = pc + 4;
415 if (or1k_delay_slot_p (gdbarch, pc))
416 next_pc += 4;
418 return {next_pc};
421 /* Name for or1k general registers. */
423 static const char *const or1k_reg_names[OR1K_NUM_REGS] = {
424 /* general purpose registers */
425 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
426 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
427 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
428 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
430 /* previous program counter, next program counter and status register */
431 "ppc", "npc", "sr"
434 static int
435 or1k_is_arg_reg (unsigned int regnum)
437 return (OR1K_FIRST_ARG_REGNUM <= regnum)
438 && (regnum <= OR1K_LAST_ARG_REGNUM);
441 static int
442 or1k_is_callee_saved_reg (unsigned int regnum)
444 return (OR1K_FIRST_SAVED_REGNUM <= regnum) && (0 == regnum % 2);
447 /* Implement the skip_prologue gdbarch method. */
449 static CORE_ADDR
450 or1k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
452 CORE_ADDR start_pc;
453 CORE_ADDR addr;
454 uint32_t inst;
456 unsigned int ra, rb, rd; /* for instruction analysis */
457 int simm;
459 int frame_size = 0;
461 /* Try using SAL first if we have symbolic information available. This
462 only works for DWARF 2, not STABS. */
464 if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
466 CORE_ADDR prologue_end = skip_prologue_using_sal (gdbarch, pc);
468 if (0 != prologue_end)
470 struct symtab_and_line prologue_sal = find_pc_line (start_pc, 0);
471 struct compunit_symtab *compunit
472 = prologue_sal.symtab->compunit ();
473 const char *debug_format = compunit->debugformat ();
475 if ((NULL != debug_format)
476 && (strlen ("dwarf") <= strlen (debug_format))
477 && (0 == strncasecmp ("dwarf", debug_format, strlen ("dwarf"))))
478 return (prologue_end > pc) ? prologue_end : pc;
482 /* Look to see if we can find any of the standard prologue sequence. All
483 quite difficult, since any or all of it may be missing. So this is
484 just a best guess! */
486 addr = pc; /* Where we have got to */
487 inst = or1k_fetch_instruction (gdbarch, addr);
489 /* Look for the new stack pointer being set up. */
490 if (or1k_analyse_l_addi (inst, &rd, &ra, &simm)
491 && (OR1K_SP_REGNUM == rd) && (OR1K_SP_REGNUM == ra)
492 && (simm < 0) && (0 == (simm % 4)))
494 frame_size = -simm;
495 addr += OR1K_INSTLEN;
496 inst = or1k_fetch_instruction (gdbarch, addr);
499 /* Look for the frame pointer being manipulated. */
500 if (or1k_analyse_l_sw (inst, &simm, &ra, &rb)
501 && (OR1K_SP_REGNUM == ra) && (OR1K_FP_REGNUM == rb)
502 && (simm >= 0) && (0 == (simm % 4)))
504 addr += OR1K_INSTLEN;
505 inst = or1k_fetch_instruction (gdbarch, addr);
507 gdb_assert (or1k_analyse_l_addi (inst, &rd, &ra, &simm)
508 && (OR1K_FP_REGNUM == rd) && (OR1K_SP_REGNUM == ra)
509 && (simm == frame_size));
511 addr += OR1K_INSTLEN;
512 inst = or1k_fetch_instruction (gdbarch, addr);
515 /* Look for the link register being saved. */
516 if (or1k_analyse_l_sw (inst, &simm, &ra, &rb)
517 && (OR1K_SP_REGNUM == ra) && (OR1K_LR_REGNUM == rb)
518 && (simm >= 0) && (0 == (simm % 4)))
520 addr += OR1K_INSTLEN;
521 inst = or1k_fetch_instruction (gdbarch, addr);
524 /* Look for arguments or callee-saved register being saved. The register
525 must be one of the arguments (r3-r8) or the 10 callee saved registers
526 (r10, r12, r14, r16, r18, r20, r22, r24, r26, r28, r30). The base
527 register must be the FP (for the args) or the SP (for the callee_saved
528 registers). */
529 while (1)
531 if (or1k_analyse_l_sw (inst, &simm, &ra, &rb)
532 && (((OR1K_FP_REGNUM == ra) && or1k_is_arg_reg (rb))
533 || ((OR1K_SP_REGNUM == ra) && or1k_is_callee_saved_reg (rb)))
534 && (0 == (simm % 4)))
536 addr += OR1K_INSTLEN;
537 inst = or1k_fetch_instruction (gdbarch, addr);
539 else
541 /* Nothing else to look for. We have found the end of the
542 prologue. */
543 break;
546 return addr;
549 /* Implement the frame_align gdbarch method. */
551 static CORE_ADDR
552 or1k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
554 return align_down (sp, OR1K_STACK_ALIGN);
557 /* Implement the unwind_pc gdbarch method. */
559 static CORE_ADDR
560 or1k_unwind_pc (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
562 CORE_ADDR pc;
564 if (or1k_debug)
565 gdb_printf (gdb_stdlog, "or1k_unwind_pc, next_frame=%d\n",
566 frame_relative_level (next_frame));
568 pc = frame_unwind_register_unsigned (next_frame, OR1K_NPC_REGNUM);
570 if (or1k_debug)
571 gdb_printf (gdb_stdlog, "or1k_unwind_pc, pc=%s\n",
572 paddress (gdbarch, pc));
574 return pc;
577 /* Implement the unwind_sp gdbarch method. */
579 static CORE_ADDR
580 or1k_unwind_sp (struct gdbarch *gdbarch, const frame_info_ptr &next_frame)
582 CORE_ADDR sp;
584 if (or1k_debug)
585 gdb_printf (gdb_stdlog, "or1k_unwind_sp, next_frame=%d\n",
586 frame_relative_level (next_frame));
588 sp = frame_unwind_register_unsigned (next_frame, OR1K_SP_REGNUM);
590 if (or1k_debug)
591 gdb_printf (gdb_stdlog, "or1k_unwind_sp, sp=%s\n",
592 paddress (gdbarch, sp));
594 return sp;
597 /* Implement the push_dummy_code gdbarch method. */
599 static CORE_ADDR
600 or1k_push_dummy_code (struct gdbarch *gdbarch, CORE_ADDR sp,
601 CORE_ADDR function, struct value **args, int nargs,
602 struct type *value_type, CORE_ADDR * real_pc,
603 CORE_ADDR * bp_addr, struct regcache *regcache)
605 CORE_ADDR bp_slot;
607 /* Reserve enough room on the stack for our breakpoint instruction. */
608 bp_slot = sp - 4;
609 /* Store the address of that breakpoint. */
610 *bp_addr = bp_slot;
611 /* keeping the stack aligned. */
612 sp = or1k_frame_align (gdbarch, bp_slot);
613 /* The call starts at the callee's entry point. */
614 *real_pc = function;
616 return sp;
619 /* Implement the push_dummy_call gdbarch method. */
621 static CORE_ADDR
622 or1k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
623 struct regcache *regcache, CORE_ADDR bp_addr,
624 int nargs, struct value **args, CORE_ADDR sp,
625 function_call_return_method return_method,
626 CORE_ADDR struct_addr)
629 int argreg;
630 int argnum;
631 int first_stack_arg;
632 int stack_offset = 0;
633 int heap_offset = 0;
634 CORE_ADDR heap_sp = sp - 128;
635 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
636 or1k_gdbarch_tdep *tdep = gdbarch_tdep<or1k_gdbarch_tdep> (gdbarch);
637 int bpa = tdep->bytes_per_address;
638 int bpw = tdep->bytes_per_word;
639 struct type *func_type = function->type ();
641 /* Return address */
642 regcache_cooked_write_unsigned (regcache, OR1K_LR_REGNUM, bp_addr);
644 /* Register for the next argument. */
645 argreg = OR1K_FIRST_ARG_REGNUM;
647 /* Location for a returned structure. This is passed as a silent first
648 argument. */
649 if (return_method == return_method_struct)
651 regcache_cooked_write_unsigned (regcache, OR1K_FIRST_ARG_REGNUM,
652 struct_addr);
653 argreg++;
656 /* Put as many args as possible in registers. */
657 for (argnum = 0; argnum < nargs; argnum++)
659 const gdb_byte *val;
660 gdb_byte valbuf[sizeof (ULONGEST)];
662 struct value *arg = args[argnum];
663 struct type *arg_type = check_typedef (arg->type ());
664 int len = arg_type->length ();
665 enum type_code typecode = arg_type->code ();
667 if (func_type->has_varargs () && argnum >= func_type->num_fields ())
668 break; /* end or regular args, varargs go to stack. */
670 /* Extract the value, either a reference or the data. */
671 if ((TYPE_CODE_STRUCT == typecode) || (TYPE_CODE_UNION == typecode)
672 || (len > bpw * 2))
674 CORE_ADDR valaddr = arg->address ();
676 /* If the arg is fabricated (i.e. 3*i, instead of i) valaddr is
677 undefined. */
678 if (valaddr == 0)
680 /* The argument needs to be copied into the target space.
681 Since the bottom of the stack is reserved for function
682 arguments we store this at the these at the top growing
683 down. */
684 heap_offset += align_up (len, bpw);
685 valaddr = heap_sp + heap_offset;
687 write_memory (valaddr, arg->contents ().data (), len);
690 /* The ABI passes all structures by reference, so get its
691 address. */
692 store_unsigned_integer (valbuf, bpa, byte_order, valaddr);
693 len = bpa;
694 val = valbuf;
696 else
698 /* Everything else, we just get the value. */
699 val = arg->contents ().data ();
702 /* Stick the value in a register. */
703 if (len > bpw)
705 /* Big scalars use two registers, but need NOT be pair aligned. */
707 if (argreg <= (OR1K_LAST_ARG_REGNUM - 1))
709 ULONGEST regval = extract_unsigned_integer (val, len,
710 byte_order);
712 unsigned int bits_per_word = bpw * 8;
713 ULONGEST mask = (((ULONGEST) 1) << bits_per_word) - 1;
714 ULONGEST lo = regval & mask;
715 ULONGEST hi = regval >> bits_per_word;
717 regcache_cooked_write_unsigned (regcache, argreg, hi);
718 regcache_cooked_write_unsigned (regcache, argreg + 1, lo);
719 argreg += 2;
721 else
723 /* Run out of regs */
724 break;
727 else if (argreg <= OR1K_LAST_ARG_REGNUM)
729 /* Smaller scalars fit in a single register. */
730 regcache_cooked_write_unsigned
731 (regcache, argreg, extract_unsigned_integer (val, len,
732 byte_order));
733 argreg++;
735 else
737 /* Ran out of regs. */
738 break;
742 first_stack_arg = argnum;
744 /* If we get here with argnum < nargs, then arguments remain to be
745 placed on the stack. This is tricky, since they must be pushed in
746 reverse order and the stack in the end must be aligned. The only
747 solution is to do it in two stages, the first to compute the stack
748 size, the second to save the args. */
750 for (argnum = first_stack_arg; argnum < nargs; argnum++)
752 struct value *arg = args[argnum];
753 struct type *arg_type = check_typedef (arg->type ());
754 int len = arg_type->length ();
755 enum type_code typecode = arg_type->code ();
757 if ((TYPE_CODE_STRUCT == typecode) || (TYPE_CODE_UNION == typecode)
758 || (len > bpw * 2))
760 /* Structures are passed as addresses. */
761 sp -= bpa;
763 else
765 /* Big scalars use more than one word. Code here allows for
766 future quad-word entities (e.g. long double.) */
767 sp -= align_up (len, bpw);
770 /* Ensure our dummy heap doesn't touch the stack, this could only
771 happen if we have many arguments including fabricated arguments. */
772 gdb_assert (heap_offset == 0 || ((heap_sp + heap_offset) < sp));
775 sp = gdbarch_frame_align (gdbarch, sp);
776 stack_offset = 0;
778 /* Push the remaining args on the stack. */
779 for (argnum = first_stack_arg; argnum < nargs; argnum++)
781 const gdb_byte *val;
782 gdb_byte valbuf[sizeof (ULONGEST)];
784 struct value *arg = args[argnum];
785 struct type *arg_type = check_typedef (arg->type ());
786 int len = arg_type->length ();
787 enum type_code typecode = arg_type->code ();
788 /* The EABI passes structures that do not fit in a register by
789 reference. In all other cases, pass the structure by value. */
790 if ((TYPE_CODE_STRUCT == typecode) || (TYPE_CODE_UNION == typecode)
791 || (len > bpw * 2))
793 store_unsigned_integer (valbuf, bpa, byte_order,
794 arg->address ());
795 len = bpa;
796 val = valbuf;
798 else
799 val = arg->contents ().data ();
801 while (len > 0)
803 int partial_len = (len < bpw ? len : bpw);
805 write_memory (sp + stack_offset, val, partial_len);
806 stack_offset += align_up (partial_len, bpw);
807 len -= partial_len;
808 val += partial_len;
812 /* Save the updated stack pointer. */
813 regcache_cooked_write_unsigned (regcache, OR1K_SP_REGNUM, sp);
815 if (heap_offset > 0)
816 sp = heap_sp;
818 return sp;
823 /* Support functions for frame handling. */
825 /* Initialize a prologue cache
827 We build a cache, saying where registers of the prev frame can be found
828 from the data so far set up in this this.
830 We also compute a unique ID for this frame, based on the function start
831 address and the stack pointer (as it will be, even if it has yet to be
832 computed.
834 STACK FORMAT
835 ============
837 The OR1K has a falling stack frame and a simple prolog. The Stack
838 pointer is R1 and the frame pointer R2. The frame base is therefore the
839 address held in R2 and the stack pointer (R1) is the frame base of the
840 next frame.
842 l.addi r1,r1,-frame_size # SP now points to end of new stack frame
844 The stack pointer may not be set up in a frameless function (e.g. a
845 simple leaf function).
847 l.sw fp_loc(r1),r2 # old FP saved in new stack frame
848 l.addi r2,r1,frame_size # FP now points to base of new stack frame
850 The frame pointer is not necessarily saved right at the end of the stack
851 frame - OR1K saves enough space for any args to called functions right
852 at the end (this is a difference from the Architecture Manual).
854 l.sw lr_loc(r1),r9 # Link (return) address
856 The link register is usually saved at fp_loc - 4. It may not be saved at
857 all in a leaf function.
859 l.sw reg_loc(r1),ry # Save any callee saved regs
861 The offsets x for the callee saved registers generally (always?) rise in
862 increments of 4, starting at fp_loc + 4. If the frame pointer is
863 omitted (an option to GCC), then it may not be saved at all. There may
864 be no callee saved registers.
866 So in summary none of this may be present. However what is present
867 seems always to follow this fixed order, and occur before any
868 substantive code (it is possible for GCC to have more flexible
869 scheduling of the prologue, but this does not seem to occur for OR1K).
871 ANALYSIS
872 ========
874 This prolog is used, even for -O3 with GCC.
876 All this analysis must allow for the possibility that the PC is in the
877 middle of the prologue. Data in the cache should only be set up insofar
878 as it has been computed.
880 HOWEVER. The frame_id must be created with the SP *as it will be* at
881 the end of the Prologue. Otherwise a recursive call, checking the frame
882 with the PC at the start address will end up with the same frame_id as
883 the caller.
885 A suite of "helper" routines are used, allowing reuse for
886 or1k_skip_prologue().
888 Reportedly, this is only valid for frames less than 0x7fff in size. */
890 static struct trad_frame_cache *
891 or1k_frame_cache (const frame_info_ptr &this_frame, void **prologue_cache)
893 struct gdbarch *gdbarch;
894 struct trad_frame_cache *info;
896 CORE_ADDR this_pc;
897 CORE_ADDR this_sp;
898 CORE_ADDR this_sp_for_id;
899 int frame_size = 0;
901 CORE_ADDR start_addr;
902 CORE_ADDR end_addr;
904 if (or1k_debug)
905 gdb_printf (gdb_stdlog,
906 "or1k_frame_cache, prologue_cache = %s\n",
907 host_address_to_string (*prologue_cache));
909 /* Nothing to do if we already have this info. */
910 if (NULL != *prologue_cache)
911 return (struct trad_frame_cache *) *prologue_cache;
913 /* Get a new prologue cache and populate it with default values. */
914 info = trad_frame_cache_zalloc (this_frame);
915 *prologue_cache = info;
917 /* Find the start address of this function (which is a normal frame, even
918 if the next frame is the sentinel frame) and the end of its prologue. */
919 this_pc = get_frame_pc (this_frame);
920 find_pc_partial_function (this_pc, NULL, &start_addr, NULL);
922 /* Get the stack pointer if we have one (if there's no process executing
923 yet we won't have a frame. */
924 this_sp = (NULL == this_frame) ? 0 :
925 get_frame_register_unsigned (this_frame, OR1K_SP_REGNUM);
927 /* Return early if GDB couldn't find the function. */
928 if (start_addr == 0)
930 if (or1k_debug)
931 gdb_printf (gdb_stdlog, " couldn't find function\n");
933 /* JPB: 28-Apr-11. This is a temporary patch, to get round GDB
934 crashing right at the beginning. Build the frame ID as best we
935 can. */
936 trad_frame_set_id (info, frame_id_build (this_sp, this_pc));
938 return info;
941 /* The default frame base of this frame (for ID purposes only - frame
942 base is an overloaded term) is its stack pointer. For now we use the
943 value of the SP register in this frame. However if the PC is in the
944 prologue of this frame, before the SP has been set up, then the value
945 will actually be that of the prev frame, and we'll need to adjust it
946 later. */
947 trad_frame_set_this_base (info, this_sp);
948 this_sp_for_id = this_sp;
950 /* The default is to find the PC of the previous frame in the link
951 register of this frame. This may be changed if we find the link
952 register was saved on the stack. */
953 trad_frame_set_reg_realreg (info, OR1K_NPC_REGNUM, OR1K_LR_REGNUM);
955 /* We should only examine code that is in the prologue. This is all code
956 up to (but not including) end_addr. We should only populate the cache
957 while the address is up to (but not including) the PC or end_addr,
958 whichever is first. */
959 gdbarch = get_frame_arch (this_frame);
960 end_addr = or1k_skip_prologue (gdbarch, start_addr);
962 /* All the following analysis only occurs if we are in the prologue and
963 have executed the code. Check we have a sane prologue size, and if
964 zero we are frameless and can give up here. */
965 if (end_addr < start_addr)
966 error (_("end addr %s is less than start addr %s"),
967 paddress (gdbarch, end_addr), paddress (gdbarch, start_addr));
969 if (end_addr == start_addr)
970 frame_size = 0;
971 else
973 /* We have a frame. Look for the various components. */
974 CORE_ADDR addr = start_addr; /* Where we have got to */
975 uint32_t inst = or1k_fetch_instruction (gdbarch, addr);
977 unsigned int ra, rb, rd; /* for instruction analysis */
978 int simm;
980 /* Look for the new stack pointer being set up. */
981 if (or1k_analyse_l_addi (inst, &rd, &ra, &simm)
982 && (OR1K_SP_REGNUM == rd) && (OR1K_SP_REGNUM == ra)
983 && (simm < 0) && (0 == (simm % 4)))
985 frame_size = -simm;
986 addr += OR1K_INSTLEN;
987 inst = or1k_fetch_instruction (gdbarch, addr);
989 /* If the PC has not actually got to this point, then the frame
990 base will be wrong, and we adjust it.
992 If we are past this point, then we need to populate the stack
993 accordingly. */
994 if (this_pc <= addr)
996 /* Only do if executing. */
997 if (0 != this_sp)
999 this_sp_for_id = this_sp + frame_size;
1000 trad_frame_set_this_base (info, this_sp_for_id);
1003 else
1005 /* We are past this point, so the stack pointer of the prev
1006 frame is frame_size greater than the stack pointer of this
1007 frame. */
1008 trad_frame_set_reg_value (info, OR1K_SP_REGNUM,
1009 this_sp + frame_size);
1013 /* From now on we are only populating the cache, so we stop once we
1014 get to either the end OR the current PC. */
1015 end_addr = (this_pc < end_addr) ? this_pc : end_addr;
1017 /* Look for the frame pointer being manipulated. */
1018 if ((addr < end_addr)
1019 && or1k_analyse_l_sw (inst, &simm, &ra, &rb)
1020 && (OR1K_SP_REGNUM == ra) && (OR1K_FP_REGNUM == rb)
1021 && (simm >= 0) && (0 == (simm % 4)))
1023 addr += OR1K_INSTLEN;
1024 inst = or1k_fetch_instruction (gdbarch, addr);
1026 /* At this stage, we can find the frame pointer of the previous
1027 frame on the stack of the current frame. */
1028 trad_frame_set_reg_addr (info, OR1K_FP_REGNUM, this_sp + simm);
1030 /* Look for the new frame pointer being set up. */
1031 if ((addr < end_addr)
1032 && or1k_analyse_l_addi (inst, &rd, &ra, &simm)
1033 && (OR1K_FP_REGNUM == rd) && (OR1K_SP_REGNUM == ra)
1034 && (simm == frame_size))
1036 addr += OR1K_INSTLEN;
1037 inst = or1k_fetch_instruction (gdbarch, addr);
1039 /* If we have got this far, the stack pointer of the previous
1040 frame is the frame pointer of this frame. */
1041 trad_frame_set_reg_realreg (info, OR1K_SP_REGNUM,
1042 OR1K_FP_REGNUM);
1046 /* Look for the link register being saved. */
1047 if ((addr < end_addr)
1048 && or1k_analyse_l_sw (inst, &simm, &ra, &rb)
1049 && (OR1K_SP_REGNUM == ra) && (OR1K_LR_REGNUM == rb)
1050 && (simm >= 0) && (0 == (simm % 4)))
1052 addr += OR1K_INSTLEN;
1053 inst = or1k_fetch_instruction (gdbarch, addr);
1055 /* If the link register is saved in the this frame, it holds the
1056 value of the PC in the previous frame. This overwrites the
1057 previous information about finding the PC in the link
1058 register. */
1059 trad_frame_set_reg_addr (info, OR1K_NPC_REGNUM, this_sp + simm);
1062 /* Look for arguments or callee-saved register being saved. The
1063 register must be one of the arguments (r3-r8) or the 10 callee
1064 saved registers (r10, r12, r14, r16, r18, r20, r22, r24, r26, r28,
1065 r30). The base register must be the FP (for the args) or the SP
1066 (for the callee_saved registers). */
1067 while (addr < end_addr)
1069 if (or1k_analyse_l_sw (inst, &simm, &ra, &rb)
1070 && (((OR1K_FP_REGNUM == ra) && or1k_is_arg_reg (rb))
1071 || ((OR1K_SP_REGNUM == ra)
1072 && or1k_is_callee_saved_reg (rb)))
1073 && (0 == (simm % 4)))
1075 addr += OR1K_INSTLEN;
1076 inst = or1k_fetch_instruction (gdbarch, addr);
1078 /* The register in the previous frame can be found at this
1079 location in this frame. */
1080 trad_frame_set_reg_addr (info, rb, this_sp + simm);
1082 else
1083 break; /* Not a register save instruction. */
1087 /* Build the frame ID */
1088 trad_frame_set_id (info, frame_id_build (this_sp_for_id, start_addr));
1090 if (or1k_debug)
1092 gdb_printf (gdb_stdlog, " this_sp_for_id = %s\n",
1093 paddress (gdbarch, this_sp_for_id));
1094 gdb_printf (gdb_stdlog, " start_addr = %s\n",
1095 paddress (gdbarch, start_addr));
1098 return info;
1101 /* Implement the this_id function for the stub unwinder. */
1103 static void
1104 or1k_frame_this_id (const frame_info_ptr &this_frame,
1105 void **prologue_cache, struct frame_id *this_id)
1107 struct trad_frame_cache *info = or1k_frame_cache (this_frame,
1108 prologue_cache);
1110 trad_frame_get_id (info, this_id);
1113 /* Implement the prev_register function for the stub unwinder. */
1115 static struct value *
1116 or1k_frame_prev_register (const frame_info_ptr &this_frame,
1117 void **prologue_cache, int regnum)
1119 struct trad_frame_cache *info = or1k_frame_cache (this_frame,
1120 prologue_cache);
1122 return trad_frame_get_register (info, this_frame, regnum);
1125 /* Data structures for the normal prologue-analysis-based unwinder. */
1127 static const struct frame_unwind or1k_frame_unwind = {
1128 "or1k prologue",
1129 NORMAL_FRAME,
1130 default_frame_unwind_stop_reason,
1131 or1k_frame_this_id,
1132 or1k_frame_prev_register,
1133 NULL,
1134 default_frame_sniffer,
1135 NULL,
1138 /* Architecture initialization for OpenRISC 1000. */
1140 static struct gdbarch *
1141 or1k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1143 const struct bfd_arch_info *binfo;
1144 tdesc_arch_data_up tdesc_data;
1145 const struct target_desc *tdesc = info.target_desc;
1147 /* Find a candidate among the list of pre-declared architectures. */
1148 arches = gdbarch_list_lookup_by_info (arches, &info);
1149 if (NULL != arches)
1150 return arches->gdbarch;
1152 /* None found, create a new architecture from the information
1153 provided. Can't initialize all the target dependencies until we
1154 actually know which target we are talking to, but put in some defaults
1155 for now. */
1156 binfo = info.bfd_arch_info;
1157 gdbarch *gdbarch
1158 = gdbarch_alloc (&info, gdbarch_tdep_up (new or1k_gdbarch_tdep));
1159 or1k_gdbarch_tdep *tdep = gdbarch_tdep<or1k_gdbarch_tdep> (gdbarch);
1161 tdep->bytes_per_word = binfo->bits_per_word / binfo->bits_per_byte;
1162 tdep->bytes_per_address = binfo->bits_per_address / binfo->bits_per_byte;
1164 /* Target data types */
1165 set_gdbarch_short_bit (gdbarch, 16);
1166 set_gdbarch_int_bit (gdbarch, 32);
1167 set_gdbarch_long_bit (gdbarch, 32);
1168 set_gdbarch_long_long_bit (gdbarch, 64);
1169 set_gdbarch_float_bit (gdbarch, 32);
1170 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
1171 set_gdbarch_double_bit (gdbarch, 64);
1172 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
1173 set_gdbarch_long_double_bit (gdbarch, 64);
1174 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
1175 set_gdbarch_ptr_bit (gdbarch, binfo->bits_per_address);
1176 set_gdbarch_addr_bit (gdbarch, binfo->bits_per_address);
1177 set_gdbarch_char_signed (gdbarch, 1);
1179 /* Information about the target architecture */
1180 set_gdbarch_return_value (gdbarch, or1k_return_value);
1181 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1182 or1k_breakpoint::kind_from_pc);
1183 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1184 or1k_breakpoint::bp_from_kind);
1185 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
1187 /* Register architecture */
1188 set_gdbarch_num_regs (gdbarch, OR1K_NUM_REGS);
1189 set_gdbarch_num_pseudo_regs (gdbarch, OR1K_NUM_PSEUDO_REGS);
1190 set_gdbarch_sp_regnum (gdbarch, OR1K_SP_REGNUM);
1191 set_gdbarch_pc_regnum (gdbarch, OR1K_NPC_REGNUM);
1192 set_gdbarch_ps_regnum (gdbarch, OR1K_SR_REGNUM);
1193 set_gdbarch_deprecated_fp_regnum (gdbarch, OR1K_FP_REGNUM);
1195 /* Functions to analyse frames */
1196 set_gdbarch_skip_prologue (gdbarch, or1k_skip_prologue);
1197 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1198 set_gdbarch_frame_align (gdbarch, or1k_frame_align);
1199 set_gdbarch_frame_red_zone_size (gdbarch, OR1K_FRAME_RED_ZONE_SIZE);
1201 /* Functions to access frame data */
1202 set_gdbarch_unwind_pc (gdbarch, or1k_unwind_pc);
1203 set_gdbarch_unwind_sp (gdbarch, or1k_unwind_sp);
1205 /* Functions handling dummy frames */
1206 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1207 set_gdbarch_push_dummy_code (gdbarch, or1k_push_dummy_code);
1208 set_gdbarch_push_dummy_call (gdbarch, or1k_push_dummy_call);
1210 /* Frame unwinders. Use DWARF debug info if available, otherwise use our
1211 own unwinder. */
1212 dwarf2_append_unwinders (gdbarch);
1213 frame_unwind_append_unwinder (gdbarch, &or1k_frame_unwind);
1215 /* Get a CGEN CPU descriptor for this architecture. */
1218 const char *mach_name = binfo->printable_name;
1219 enum cgen_endian endian = (info.byte_order == BFD_ENDIAN_BIG
1220 ? CGEN_ENDIAN_BIG : CGEN_ENDIAN_LITTLE);
1222 tdep->gdb_cgen_cpu_desc =
1223 or1k_cgen_cpu_open (CGEN_CPU_OPEN_BFDMACH, mach_name,
1224 CGEN_CPU_OPEN_ENDIAN, endian, CGEN_CPU_OPEN_END);
1226 or1k_cgen_init_asm (tdep->gdb_cgen_cpu_desc);
1229 /* If this mach has a delay slot. */
1230 if (binfo->mach == bfd_mach_or1k)
1231 set_gdbarch_single_step_through_delay (gdbarch,
1232 or1k_single_step_through_delay);
1234 if (!tdesc_has_registers (info.target_desc))
1235 /* Pick a default target description. */
1236 tdesc = tdesc_or1k;
1238 /* Check any target description for validity. */
1239 if (tdesc_has_registers (tdesc))
1241 const struct tdesc_feature *feature;
1242 int valid_p;
1243 int i;
1245 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.or1k.group0");
1246 if (feature == NULL)
1247 return NULL;
1249 tdesc_data = tdesc_data_alloc ();
1251 valid_p = 1;
1253 for (i = 0; i < OR1K_NUM_REGS; i++)
1254 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
1255 or1k_reg_names[i]);
1257 if (!valid_p)
1258 return NULL;
1261 if (tdesc_data != NULL)
1262 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
1264 /* Hook in ABI-specific overrides, if they have been registered. */
1265 gdbarch_init_osabi (info, gdbarch);
1267 return gdbarch;
1270 /* Dump the target specific data for this architecture. */
1272 static void
1273 or1k_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
1275 or1k_gdbarch_tdep *tdep = gdbarch_tdep<or1k_gdbarch_tdep> (gdbarch);
1277 if (NULL == tdep)
1278 return; /* Nothing to report */
1280 gdb_printf (file, "or1k_dump_tdep: %d bytes per word\n",
1281 tdep->bytes_per_word);
1282 gdb_printf (file, "or1k_dump_tdep: %d bytes per address\n",
1283 tdep->bytes_per_address);
1287 void _initialize_or1k_tdep ();
1288 void
1289 _initialize_or1k_tdep ()
1291 /* Register this architecture. */
1292 gdbarch_register (bfd_arch_or1k, or1k_gdbarch_init, or1k_dump_tdep);
1294 initialize_tdesc_or1k ();
1296 /* Debugging flag. */
1297 add_setshow_boolean_cmd ("or1k", class_maintenance, &or1k_debug,
1298 _("Set OpenRISC debugging."),
1299 _("Show OpenRISC debugging."),
1300 _("When on, OpenRISC specific debugging is enabled."),
1301 NULL,
1302 show_or1k_debug,
1303 &setdebuglist, &showdebuglist);