More updated translations
[binutils-gdb.git] / gdb / loongarch-tdep.c
blob1cab29bbecbe9b519160de522aee624d599244cd
1 /* Target-dependent code for the LoongArch architecture, for GDB.
3 Copyright (C) 2022-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "arch-utils.h"
21 #include "arch/loongarch-insn.h"
22 #include "dwarf2/frame.h"
23 #include "elf-bfd.h"
24 #include "extract-store-integer.h"
25 #include "frame-unwind.h"
26 #include "gdbcore.h"
27 #include "linux-record.h"
28 #include "loongarch-tdep.h"
29 #include "record.h"
30 #include "record-full.h"
31 #include "reggroups.h"
32 #include "target.h"
33 #include "target-descriptions.h"
34 #include "trad-frame.h"
35 #include "user-regs.h"
37 /* Fetch the instruction at PC. */
39 static insn_t
40 loongarch_fetch_instruction (CORE_ADDR pc)
42 size_t insn_len = loongarch_insn_length (0);
43 gdb::byte_vector buf (insn_len);
44 int err;
46 err = target_read_memory (pc, buf.data (), insn_len);
47 if (err)
48 memory_error (TARGET_XFER_E_IO, pc);
50 return extract_unsigned_integer (buf.data (), insn_len, BFD_ENDIAN_LITTLE);
53 /* Return TRUE if INSN is a unconditional branch instruction, otherwise return FALSE. */
55 static bool
56 loongarch_insn_is_uncond_branch (insn_t insn)
58 if ((insn & 0xfc000000) == 0x4c000000 /* jirl */
59 || (insn & 0xfc000000) == 0x50000000 /* b */
60 || (insn & 0xfc000000) == 0x54000000) /* bl */
61 return true;
62 return false;
65 /* Return TRUE if INSN is a conditional branch instruction, otherwise return FALSE. */
67 static bool
68 loongarch_insn_is_cond_branch (insn_t insn)
70 if ((insn & 0xfc000000) == 0x58000000 /* beq */
71 || (insn & 0xfc000000) == 0x5c000000 /* bne */
72 || (insn & 0xfc000000) == 0x60000000 /* blt */
73 || (insn & 0xfc000000) == 0x64000000 /* bge */
74 || (insn & 0xfc000000) == 0x68000000 /* bltu */
75 || (insn & 0xfc000000) == 0x6c000000 /* bgeu */
76 || (insn & 0xfc000000) == 0x40000000 /* beqz */
77 || (insn & 0xfc000000) == 0x44000000) /* bnez */
78 return true;
79 return false;
82 /* Return TRUE if INSN is a branch instruction, otherwise return FALSE. */
84 static bool
85 loongarch_insn_is_branch (insn_t insn)
87 bool is_uncond = loongarch_insn_is_uncond_branch (insn);
88 bool is_cond = loongarch_insn_is_cond_branch (insn);
90 return (is_uncond || is_cond);
93 /* Return TRUE if INSN is a Load Linked instruction, otherwise return FALSE. */
95 static bool
96 loongarch_insn_is_ll (insn_t insn)
98 if ((insn & 0xff000000) == 0x20000000 /* ll.w */
99 || (insn & 0xff000000) == 0x22000000) /* ll.d */
100 return true;
101 return false;
104 /* Return TRUE if INSN is a Store Conditional instruction, otherwise return FALSE. */
106 static bool
107 loongarch_insn_is_sc (insn_t insn)
109 if ((insn & 0xff000000) == 0x21000000 /* sc.w */
110 || (insn & 0xff000000) == 0x23000000) /* sc.d */
111 return true;
112 return false;
115 /* Analyze the function prologue from START_PC to LIMIT_PC.
116 Return the address of the first instruction past the prologue. */
118 static CORE_ADDR
119 loongarch_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc,
120 CORE_ADDR limit_pc, const frame_info_ptr &this_frame,
121 struct trad_frame_cache *this_cache)
123 CORE_ADDR cur_pc = start_pc, prologue_end = 0;
124 int32_t sp = LOONGARCH_SP_REGNUM;
125 int32_t fp = LOONGARCH_FP_REGNUM;
126 int32_t reg_value[32] = {0};
127 int32_t reg_used[32] = {1, 0};
129 while (cur_pc < limit_pc)
131 insn_t insn = loongarch_fetch_instruction (cur_pc);
132 size_t insn_len = loongarch_insn_length (insn);
133 int32_t rd = loongarch_decode_imm ("0:5", insn, 0);
134 int32_t rj = loongarch_decode_imm ("5:5", insn, 0);
135 int32_t rk = loongarch_decode_imm ("10:5", insn, 0);
136 int32_t si12 = loongarch_decode_imm ("10:12", insn, 1);
137 int32_t si20 = loongarch_decode_imm ("5:20", insn, 1);
139 if ((insn & 0xffc00000) == 0x02c00000 /* addi.d sp,sp,si12 */
140 && rd == sp && rj == sp && si12 < 0)
142 prologue_end = cur_pc + insn_len;
144 else if ((insn & 0xffc00000) == 0x02c00000 /* addi.d fp,sp,si12 */
145 && rd == fp && rj == sp && si12 > 0)
147 prologue_end = cur_pc + insn_len;
149 else if ((insn & 0xffc00000) == 0x29c00000 /* st.d rd,sp,si12 */
150 && rj == sp)
152 prologue_end = cur_pc + insn_len;
154 else if ((insn & 0xff000000) == 0x27000000 /* stptr.d rd,sp,si14 */
155 && rj == sp)
157 prologue_end = cur_pc + insn_len;
159 else if ((insn & 0xfe000000) == 0x14000000) /* lu12i.w rd,si20 */
161 reg_value[rd] = si20 << 12;
162 reg_used[rd] = 1;
164 else if ((insn & 0xffc00000) == 0x03800000) /* ori rd,rj,si12 */
166 if (reg_used[rj])
168 reg_value[rd] = reg_value[rj] | (si12 & 0xfff);
169 reg_used[rd] = 1;
172 else if ((insn & 0xffff8000) == 0x00108000 /* add.d sp,sp,rk */
173 && rd == sp && rj == sp)
175 if (reg_used[rk] == 1 && reg_value[rk] < 0)
177 prologue_end = cur_pc + insn_len;
178 break;
181 else if (loongarch_insn_is_branch (insn))
183 break;
186 cur_pc += insn_len;
189 if (prologue_end == 0)
190 prologue_end = cur_pc;
192 return prologue_end;
195 /* Implement the loongarch_skip_prologue gdbarch method. */
197 static CORE_ADDR
198 loongarch_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
200 CORE_ADDR func_addr;
202 /* See if we can determine the end of the prologue via the symbol table.
203 If so, then return either PC, or the PC after the prologue, whichever
204 is greater. */
205 if (find_pc_partial_function (pc, nullptr, &func_addr, nullptr))
207 CORE_ADDR post_prologue_pc
208 = skip_prologue_using_sal (gdbarch, func_addr);
209 if (post_prologue_pc != 0)
210 return std::max (pc, post_prologue_pc);
213 /* Can't determine prologue from the symbol table, need to examine
214 instructions. */
216 /* Find an upper limit on the function prologue using the debug
217 information. If the debug information could not be used to provide
218 that bound, then use an arbitrary large number as the upper bound. */
219 CORE_ADDR limit_pc = skip_prologue_using_sal (gdbarch, pc);
220 if (limit_pc == 0)
221 limit_pc = pc + 100; /* Arbitrary large number. */
223 return loongarch_scan_prologue (gdbarch, pc, limit_pc, nullptr, nullptr);
226 /* Decode the current instruction and determine the address of the
227 next instruction. */
229 static CORE_ADDR
230 loongarch_next_pc (struct regcache *regcache, CORE_ADDR cur_pc)
232 struct gdbarch *gdbarch = regcache->arch ();
233 loongarch_gdbarch_tdep *tdep = gdbarch_tdep<loongarch_gdbarch_tdep> (gdbarch);
234 insn_t insn = loongarch_fetch_instruction (cur_pc);
235 size_t insn_len = loongarch_insn_length (insn);
236 CORE_ADDR next_pc = cur_pc + insn_len;
238 if ((insn & 0xfc000000) == 0x4c000000) /* jirl rd, rj, offs16 */
240 LONGEST rj = regcache_raw_get_signed (regcache,
241 loongarch_decode_imm ("5:5", insn, 0));
242 next_pc = rj + loongarch_decode_imm ("10:16<<2", insn, 1);
244 else if ((insn & 0xfc000000) == 0x50000000 /* b offs26 */
245 || (insn & 0xfc000000) == 0x54000000) /* bl offs26 */
247 next_pc = cur_pc + loongarch_decode_imm ("0:10|10:16<<2", insn, 1);
249 else if ((insn & 0xfc000000) == 0x58000000) /* beq rj, rd, offs16 */
251 LONGEST rj = regcache_raw_get_signed (regcache,
252 loongarch_decode_imm ("5:5", insn, 0));
253 LONGEST rd = regcache_raw_get_signed (regcache,
254 loongarch_decode_imm ("0:5", insn, 0));
255 if (rj == rd)
256 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
258 else if ((insn & 0xfc000000) == 0x5c000000) /* bne rj, rd, offs16 */
260 LONGEST rj = regcache_raw_get_signed (regcache,
261 loongarch_decode_imm ("5:5", insn, 0));
262 LONGEST rd = regcache_raw_get_signed (regcache,
263 loongarch_decode_imm ("0:5", insn, 0));
264 if (rj != rd)
265 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
267 else if ((insn & 0xfc000000) == 0x60000000) /* blt rj, rd, offs16 */
269 LONGEST rj = regcache_raw_get_signed (regcache,
270 loongarch_decode_imm ("5:5", insn, 0));
271 LONGEST rd = regcache_raw_get_signed (regcache,
272 loongarch_decode_imm ("0:5", insn, 0));
273 if (rj < rd)
274 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
276 else if ((insn & 0xfc000000) == 0x64000000) /* bge rj, rd, offs16 */
278 LONGEST rj = regcache_raw_get_signed (regcache,
279 loongarch_decode_imm ("5:5", insn, 0));
280 LONGEST rd = regcache_raw_get_signed (regcache,
281 loongarch_decode_imm ("0:5", insn, 0));
282 if (rj >= rd)
283 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
285 else if ((insn & 0xfc000000) == 0x68000000) /* bltu rj, rd, offs16 */
287 ULONGEST rj = regcache_raw_get_unsigned (regcache,
288 loongarch_decode_imm ("5:5", insn, 0));
289 ULONGEST rd = regcache_raw_get_unsigned (regcache,
290 loongarch_decode_imm ("0:5", insn, 0));
291 if (rj < rd)
292 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
294 else if ((insn & 0xfc000000) == 0x6c000000) /* bgeu rj, rd, offs16 */
296 ULONGEST rj = regcache_raw_get_unsigned (regcache,
297 loongarch_decode_imm ("5:5", insn, 0));
298 ULONGEST rd = regcache_raw_get_unsigned (regcache,
299 loongarch_decode_imm ("0:5", insn, 0));
300 if (rj >= rd)
301 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
303 else if ((insn & 0xfc000000) == 0x40000000) /* beqz rj, offs21 */
305 LONGEST rj = regcache_raw_get_signed (regcache,
306 loongarch_decode_imm ("5:5", insn, 0));
307 if (rj == 0)
308 next_pc = cur_pc + loongarch_decode_imm ("0:5|10:16<<2", insn, 1);
310 else if ((insn & 0xfc000000) == 0x44000000) /* bnez rj, offs21 */
312 LONGEST rj = regcache_raw_get_signed (regcache,
313 loongarch_decode_imm ("5:5", insn, 0));
314 if (rj != 0)
315 next_pc = cur_pc + loongarch_decode_imm ("0:5|10:16<<2", insn, 1);
317 else if ((insn & 0xffff8000) == 0x002b0000) /* syscall */
319 if (tdep->syscall_next_pc != nullptr)
320 next_pc = tdep->syscall_next_pc (get_current_frame ());
323 return next_pc;
326 /* We can't put a breakpoint in the middle of a ll/sc atomic sequence,
327 so look for the end of the sequence and put the breakpoint there. */
329 static std::vector<CORE_ADDR>
330 loongarch_deal_with_atomic_sequence (struct regcache *regcache, CORE_ADDR cur_pc)
332 CORE_ADDR next_pc;
333 std::vector<CORE_ADDR> next_pcs;
334 insn_t insn = loongarch_fetch_instruction (cur_pc);
335 size_t insn_len = loongarch_insn_length (insn);
336 const int atomic_sequence_length = 16;
337 bool found_atomic_sequence_endpoint = false;
339 /* Look for a Load Linked instruction which begins the atomic sequence. */
340 if (!loongarch_insn_is_ll (insn))
341 return {};
343 /* Assume that no atomic sequence is longer than "atomic_sequence_length" instructions. */
344 for (int insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
346 cur_pc += insn_len;
347 insn = loongarch_fetch_instruction (cur_pc);
349 /* Look for a unconditional branch instruction, fallback to the standard code. */
350 if (loongarch_insn_is_uncond_branch (insn))
352 return {};
354 /* Look for a conditional branch instruction, put a breakpoint in its destination address. */
355 else if (loongarch_insn_is_cond_branch (insn))
357 next_pc = loongarch_next_pc (regcache, cur_pc);
358 next_pcs.push_back (next_pc);
360 /* Look for a Store Conditional instruction which closes the atomic sequence. */
361 else if (loongarch_insn_is_sc (insn))
363 found_atomic_sequence_endpoint = true;
364 next_pc = cur_pc + insn_len;
365 next_pcs.push_back (next_pc);
366 break;
370 /* We didn't find a closing Store Conditional instruction, fallback to the standard code. */
371 if (!found_atomic_sequence_endpoint)
372 return {};
374 return next_pcs;
377 /* Implement the software_single_step gdbarch method */
379 static std::vector<CORE_ADDR>
380 loongarch_software_single_step (struct regcache *regcache)
382 CORE_ADDR cur_pc = regcache_read_pc (regcache);
383 std::vector<CORE_ADDR> next_pcs
384 = loongarch_deal_with_atomic_sequence (regcache, cur_pc);
386 if (!next_pcs.empty ())
387 return next_pcs;
389 CORE_ADDR next_pc = loongarch_next_pc (regcache, cur_pc);
391 return {next_pc};
394 /* Callback function for user_reg_add. */
396 static struct value *
397 value_of_loongarch_user_reg (const frame_info_ptr &frame, const void *baton)
399 return value_of_register ((long long) baton,
400 get_next_frame_sentinel_okay (frame));
403 /* Implement the frame_align gdbarch method. */
405 static CORE_ADDR
406 loongarch_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
408 return align_down (addr, 16);
411 /* Generate, or return the cached frame cache for frame unwinder. */
413 static struct trad_frame_cache *
414 loongarch_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
416 struct trad_frame_cache *cache;
417 CORE_ADDR pc;
419 if (*this_cache != nullptr)
420 return (struct trad_frame_cache *) *this_cache;
422 cache = trad_frame_cache_zalloc (this_frame);
423 *this_cache = cache;
425 trad_frame_set_reg_realreg (cache, LOONGARCH_PC_REGNUM, LOONGARCH_RA_REGNUM);
427 pc = get_frame_address_in_block (this_frame);
428 trad_frame_set_id (cache, frame_id_build_unavailable_stack (pc));
430 return cache;
433 /* Implement the this_id callback for frame unwinder. */
435 static void
436 loongarch_frame_this_id (const frame_info_ptr &this_frame, void **prologue_cache,
437 struct frame_id *this_id)
439 struct trad_frame_cache *info;
441 info = loongarch_frame_cache (this_frame, prologue_cache);
442 trad_frame_get_id (info, this_id);
445 /* Implement the prev_register callback for frame unwinder. */
447 static struct value *
448 loongarch_frame_prev_register (const frame_info_ptr &this_frame,
449 void **prologue_cache, int regnum)
451 struct trad_frame_cache *info;
453 info = loongarch_frame_cache (this_frame, prologue_cache);
454 return trad_frame_get_register (info, this_frame, regnum);
457 static const struct frame_unwind_legacy loongarch_frame_unwind (
458 "loongarch prologue",
459 /*.type =*/NORMAL_FRAME,
460 /*.unwinder_class=*/FRAME_UNWIND_ARCH,
461 /*.stop_reason =*/default_frame_unwind_stop_reason,
462 /*.this_id =*/loongarch_frame_this_id,
463 /*.prev_register =*/loongarch_frame_prev_register,
464 /*.unwind_data =*/nullptr,
465 /*.sniffer =*/default_frame_sniffer,
466 /*.dealloc_cache =*/nullptr,
467 /*.prev_arch =*/nullptr
470 /* Write the contents of buffer VAL into the general-purpose argument
471 register defined by GAR in REGCACHE. GAR indicates the available
472 general-purpose argument registers which should be a value in the
473 range 1 to 8 (LOONGARCH_ARG_REGNUM), which correspond to registers
474 a7 and a0 respectively, that is to say, regnum is a7 if GAR is 1,
475 regnum is a6 if GAR is 2, regnum is a5 if GAR is 3, regnum is a4
476 if GAR is 4, regnum is a3 if GAR is 5, regnum is a2 if GAR is 6,
477 regnum is a1 if GAR is 7, regnum is a0 if GAR is 8. */
479 static void
480 pass_in_gar (struct regcache *regcache, unsigned int gar, const gdb_byte *val)
482 unsigned int regnum = LOONGARCH_ARG_REGNUM - gar + LOONGARCH_A0_REGNUM;
483 regcache->cooked_write (regnum, val);
486 /* Write the contents of buffer VAL into the floating-point argument
487 register defined by FAR in REGCACHE. FAR indicates the available
488 floating-point argument registers which should be a value in the
489 range 1 to 8 (LOONGARCH_ARG_REGNUM), which correspond to registers
490 f7 and f0 respectively, that is to say, regnum is f7 if FAR is 1,
491 regnum is f6 if FAR is 2, regnum is f5 if FAR is 3, regnum is f4
492 if FAR is 4, regnum is f3 if FAR is 5, regnum is f2 if FAR is 6,
493 regnum is f1 if FAR is 7, regnum is f0 if FAR is 8. */
495 static void
496 pass_in_far (struct regcache *regcache, unsigned int far, const gdb_byte *val)
498 unsigned int regnum = LOONGARCH_ARG_REGNUM - far + LOONGARCH_FIRST_FP_REGNUM;
499 regcache->cooked_write (regnum, val);
502 /* Pass a value on the stack. */
504 static void
505 pass_on_stack (struct regcache *regcache, const gdb_byte *val,
506 size_t len, int align, gdb_byte **addr)
508 align = align_up (align, 8);
509 if (align > 16)
510 align = 16;
512 CORE_ADDR align_addr = (CORE_ADDR) (*addr);
513 align_addr = align_up (align_addr, align);
514 *addr = (gdb_byte *) align_addr;
515 memcpy (*addr, val, len);
516 *addr += len;
519 /* Compute the numbers of struct member. */
521 static void
522 compute_struct_member (struct type *type,
523 unsigned int *fixed_point_members,
524 unsigned int *floating_point_members,
525 bool *first_member_is_fixed_point,
526 bool *has_long_double)
528 for (int i = 0; i < type->num_fields (); i++)
530 /* Ignore any static fields. */
531 if (type->field (i).is_static ())
532 continue;
534 struct type *field_type = check_typedef (type->field (i).type ());
536 if ((field_type->code () == TYPE_CODE_FLT
537 && field_type->length () == 16)
538 || (field_type->code () == TYPE_CODE_COMPLEX
539 && field_type->length () == 32))
540 *has_long_double = true;
542 if (field_type->code () == TYPE_CODE_INT
543 || field_type->code () == TYPE_CODE_BOOL
544 || field_type->code () == TYPE_CODE_CHAR
545 || field_type->code () == TYPE_CODE_RANGE
546 || field_type->code () == TYPE_CODE_ENUM
547 || field_type->code () == TYPE_CODE_PTR)
549 (*fixed_point_members)++;
551 if (*floating_point_members == 0)
552 *first_member_is_fixed_point = true;
554 else if (field_type->code () == TYPE_CODE_FLT)
555 (*floating_point_members)++;
556 else if (field_type->code () == TYPE_CODE_STRUCT)
557 compute_struct_member (field_type,
558 fixed_point_members,
559 floating_point_members,
560 first_member_is_fixed_point,
561 has_long_double);
562 else if (field_type->code () == TYPE_CODE_COMPLEX)
563 (*floating_point_members) += 2;
567 /* Compute the lengths and offsets of struct member. */
569 static void
570 struct_member_info (struct type *type,
571 unsigned int *member_offsets,
572 unsigned int *member_lens,
573 unsigned int offset,
574 unsigned int *fields)
576 unsigned int count = type->num_fields ();
577 unsigned int i;
579 for (i = 0; i < count; ++i)
581 if (type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
582 continue;
584 struct type *field_type = check_typedef (type->field (i).type ());
585 int field_offset
586 = offset + type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
588 switch (field_type->code ())
590 case TYPE_CODE_STRUCT:
591 struct_member_info (field_type, member_offsets, member_lens,
592 field_offset, fields);
593 break;
595 case TYPE_CODE_COMPLEX:
596 if (*fields == 0)
598 /* _Complex float */
599 if (field_type->length () == 8)
601 member_offsets[0] = field_offset;
602 member_offsets[1] = field_offset + 4;
603 member_lens[0] = member_lens[1] = 4;
604 *fields = 2;
606 /* _Complex double */
607 else if (field_type->length () == 16)
609 member_offsets[0] = field_offset;
610 member_offsets[1] = field_offset + 8;
611 member_lens[0] = member_lens[1] = 8;
612 *fields = 2;
615 break;
617 default:
618 if (*fields < 2)
620 member_offsets[*fields] = field_offset;
621 member_lens[*fields] = field_type->length ();
623 (*fields)++;
624 break;
627 /* only has special handling for structures with 1 or 2 fields. */
628 if (*fields > 2)
629 return;
633 /* Implement the push_dummy_call gdbarch method. */
635 static CORE_ADDR
636 loongarch_push_dummy_call (struct gdbarch *gdbarch,
637 struct value *function,
638 struct regcache *regcache,
639 CORE_ADDR bp_addr,
640 int nargs,
641 struct value **args,
642 CORE_ADDR sp,
643 function_call_return_method return_method,
644 CORE_ADDR struct_addr)
646 int regsize = register_size (gdbarch, 0);
647 unsigned int gar = LOONGARCH_ARG_REGNUM;
648 unsigned int far = LOONGARCH_ARG_REGNUM;
649 unsigned int fixed_point_members;
650 unsigned int floating_point_members;
651 bool first_member_is_fixed_point;
652 bool has_long_double;
653 unsigned int member_offsets[2];
654 unsigned int member_lens[2];
655 unsigned int fields;
656 gdb_byte buf[1024] = { 0 };
657 gdb_byte *addr = buf;
659 if (return_method != return_method_normal)
660 pass_in_gar (regcache, gar--, (gdb_byte *) &struct_addr);
662 for (int i = 0; i < nargs; i++)
664 struct value *arg = args[i];
665 const gdb_byte *val = arg->contents ().data ();
666 struct type *type = check_typedef (arg->type ());
667 size_t len = type->length ();
668 int align = type_align (type);
669 enum type_code code = type->code ();
670 struct type *func_type = check_typedef (function->type ());
671 bool varargs = (func_type->has_varargs () && i >= func_type->num_fields ());
673 switch (code)
675 case TYPE_CODE_INT:
676 case TYPE_CODE_BOOL:
677 case TYPE_CODE_CHAR:
678 case TYPE_CODE_RANGE:
679 case TYPE_CODE_ENUM:
680 case TYPE_CODE_PTR:
682 /* integer or pointer type is passed in GAR.
683 If no GAR is available, it's passed on the stack.
684 When passed in registers or on the stack,
685 the unsigned integer scalars are zero-extended to GRLEN bits,
686 and the signed integer scalars are sign-extended. */
687 if (type->is_unsigned ())
689 ULONGEST data = extract_unsigned_integer (val, len, BFD_ENDIAN_LITTLE);
690 if (gar > 0)
691 pass_in_gar (regcache, gar--, (gdb_byte *) &data);
692 else
693 pass_on_stack (regcache, (gdb_byte *) &data, len, align, &addr);
695 else
697 LONGEST data = extract_signed_integer (val, len, BFD_ENDIAN_LITTLE);
698 if (gar > 0)
699 pass_in_gar (regcache, gar--, (gdb_byte *) &data);
700 else
701 pass_on_stack (regcache, (gdb_byte *) &data, len, align, &addr);
704 break;
705 case TYPE_CODE_FLT:
706 if (len == 2 * regsize)
708 if (!varargs)
710 /* long double type is passed in a pair of GAR,
711 with the low-order GRLEN bits in the lower-numbered register
712 and the high-order GRLEN bits in the higher-numbered register.
713 If exactly one register is available,
714 the low-order GRLEN bits are passed in the register
715 and the high-order GRLEN bits are passed on the stack.
716 If no GAR is available, it's passed on the stack. */
717 if (gar >= 2)
719 pass_in_gar (regcache, gar--, val);
720 pass_in_gar (regcache, gar--, val + regsize);
722 else if (gar == 1)
724 pass_in_gar (regcache, gar--, val);
725 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
727 else
729 pass_on_stack (regcache, val, len, align, &addr);
732 else
734 /* Variadic arguments are passed in GARs
735 in the same manner as named arguments.
736 And after a variadic argument has been passed on the stack,
737 all future arguments will also be passed on the stack,
738 i.e., the last argument register may be left unused
739 due to the aligned register pair rule.
740 long double data type is passed in an aligned GAR pair,
741 the first register in the pair is even-numbered. */
742 if (gar >= 2)
744 if (gar % 2 == 0)
746 pass_in_gar (regcache, gar--, val);
747 pass_in_gar (regcache, gar--, val + regsize);
749 else
751 gar--;
752 pass_in_gar (regcache, gar--, val);
753 pass_in_gar (regcache, gar--, val + regsize);
756 else if (gar == 1)
758 gar--;
759 pass_on_stack (regcache, val, len, align, &addr);
761 else
763 pass_on_stack (regcache, val, len, align, &addr);
767 else
769 /* The other floating-point type is passed in FAR.
770 If no FAR is available, it's passed in GAR.
771 If no GAR is available, it's passed on the stack. */
772 if (!varargs && far > 0)
773 pass_in_far (regcache, far--, val);
774 else if (gar > 0)
775 pass_in_gar (regcache, gar--, val);
776 else
777 pass_on_stack (regcache, val, len, align, &addr);
779 break;
780 case TYPE_CODE_STRUCT:
782 fixed_point_members = 0;
783 floating_point_members = 0;
784 first_member_is_fixed_point = false;
785 has_long_double = false;
786 member_offsets[0] = member_offsets[1] = 0;
787 member_lens[0] = member_offsets[1] = 0;
788 fields = 0;
789 compute_struct_member (type,
790 &fixed_point_members,
791 &floating_point_members,
792 &first_member_is_fixed_point,
793 &has_long_double);
794 struct_member_info (type, member_offsets, member_lens, 0, &fields);
795 /* If the structure consists of one floating-point member within
796 FRLEN bits wide, it is passed in an FAR if available. If the
797 structure consists of two floating-point members both within
798 FRLEN bits wide, it is passed in two FARs if available. If the
799 structure consists of one integer member within GRLEN bits wide
800 and one floating-point member within FRLEN bits wide, it is
801 passed in a GAR and an FAR if available. */
802 if (has_long_double == false
803 && ((fixed_point_members == 0 && floating_point_members == 1
804 && far >= 1)
805 || (fixed_point_members == 0 && floating_point_members == 2
806 && far >= 2)
807 || (fixed_point_members == 1 && floating_point_members == 1
808 && far >= 1 && gar >= 1)))
810 if (fixed_point_members == 0 && floating_point_members == 1)
812 pass_in_far (regcache, far--, val + member_offsets[0]);
814 else if (fixed_point_members == 0 && floating_point_members == 2)
816 pass_in_far (regcache, far--, val + member_offsets[0]);
817 pass_in_far (regcache, far--, val + member_offsets[1]);
819 else if (fixed_point_members == 1 && floating_point_members == 1)
821 if (first_member_is_fixed_point == false)
823 pass_in_far (regcache, far--, val + member_offsets[0]);
824 pass_in_gar (regcache, gar--, val + member_offsets[1]);
826 else
828 pass_in_gar (regcache, gar--, val + member_offsets[0]);
829 pass_in_far (regcache, far--, val + member_offsets[1]);
833 else if (len > 0 && len <= regsize)
835 /* The structure has only fixed-point members. */
836 if (fixed_point_members > 0 && floating_point_members == 0)
838 /* If there is an available GAR,
839 the structure is passed through the GAR by value passing;
840 If no GAR is available, it's passed on the stack. */
841 if (gar > 0)
842 pass_in_gar (regcache, gar--, val);
843 else
844 pass_on_stack (regcache, val, len, align, &addr);
846 /* The structure has only floating-point members. */
847 else if (fixed_point_members == 0 && floating_point_members > 0)
849 /* The structure has one floating-point member.
850 The argument is passed in a FAR.
851 If no FAR is available, the value is passed in a GAR.
852 if no GAR is available, the value is passed on the stack. */
853 if (floating_point_members == 1)
855 if (!varargs && far > 0)
856 pass_in_far (regcache, far--, val);
857 else if (gar > 0)
858 pass_in_gar (regcache, gar--, val);
859 else
860 pass_on_stack (regcache, val, len, align, &addr);
862 /* The structure has two floating-point members.
863 The argument is passed in a pair of available FAR,
864 with the low-order float member bits in the lower-numbered FAR
865 and the high-order float member bits in the higher-numbered FAR.
866 If the number of available FAR is less than 2, it's passed in a GAR,
867 and passed on the stack if no GAR is available. */
868 else if (floating_point_members == 2)
870 if (!varargs && far >= 2)
872 pass_in_far (regcache, far--, val);
873 pass_in_far (regcache, far--, val + align);
875 else if (gar > 0)
877 pass_in_gar (regcache, gar--, val);
879 else
881 pass_on_stack (regcache, val, len, align, &addr);
885 /* The structure has both fixed-point and floating-point members. */
886 else if (fixed_point_members > 0 && floating_point_members > 0)
888 /* The structure has one float member and multiple fixed-point members.
889 If there are available GAR, the structure is passed in a GAR,
890 and passed on the stack if no GAR is available. */
891 if (floating_point_members == 1 && fixed_point_members > 1)
893 if (gar > 0)
894 pass_in_gar (regcache, gar--, val);
895 else
896 pass_on_stack (regcache, val, len, align, &addr);
898 /* The structure has one float member and one fixed-point member.
899 If one FAR and one GAR are available,
900 the floating-point member of the structure is passed in the FAR,
901 and the fixed-point member of the structure is passed in the GAR.
902 If no floating-point register but one GAR is available, it's passed in GAR;
903 If no GAR is available, it's passed on the stack. */
904 else if (floating_point_members == 1 && fixed_point_members == 1)
906 if (!varargs && far > 0 && gar > 0)
908 if (first_member_is_fixed_point == false)
910 pass_in_far (regcache, far--, val);
911 pass_in_gar (regcache, gar--, val + align);
913 else
915 pass_in_gar (regcache, gar--, val);
916 pass_in_far (regcache, far--, val + align);
919 else
921 if (gar > 0)
922 pass_in_gar (regcache, gar--, val);
923 else
924 pass_on_stack (regcache, val, len, align, &addr);
929 else if (len > regsize && len <= 2 * regsize)
931 /* The structure has only fixed-point members. */
932 if (fixed_point_members > 0 && floating_point_members == 0)
934 /* The argument is passed in a pair of available GAR,
935 with the low-order bits in the lower-numbered GAR
936 and the high-order bits in the higher-numbered GAR.
937 If only one GAR is available,
938 the low-order bits are in the GAR
939 and the high-order bits are on the stack,
940 and passed on the stack if no GAR is available. */
941 if (gar >= 2)
943 pass_in_gar (regcache, gar--, val);
944 pass_in_gar (regcache, gar--, val + regsize);
946 else if (gar == 1)
948 pass_in_gar (regcache, gar--, val);
949 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
951 else
953 pass_on_stack (regcache, val, len, align, &addr);
956 /* The structure has only floating-point members. */
957 else if (fixed_point_members == 0 && floating_point_members > 0)
959 /* The structure has one long double member
960 or one double member and two adjacent float members
961 or 3-4 float members.
962 The argument is passed in a pair of available GAR,
963 with the low-order bits in the lower-numbered GAR
964 and the high-order bits in the higher-numbered GAR.
965 If only one GAR is available,
966 the low-order bits are in the GAR
967 and the high-order bits are on the stack,
968 and passed on the stack if no GAR is available. */
969 if ((len == 16 && floating_point_members == 1)
970 || (len == 16 && floating_point_members == 3)
971 || (len == 12 && floating_point_members == 3)
972 || (len == 16 && floating_point_members == 4))
974 if (gar >= 2)
976 pass_in_gar (regcache, gar--, val);
977 pass_in_gar (regcache, gar--, val + regsize);
979 else if (gar == 1)
981 if (!varargs)
983 pass_in_gar (regcache, gar--, val);
984 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
986 else
988 gar--;
989 pass_on_stack (regcache, val, len, align, &addr);
992 else
994 pass_on_stack (regcache, val, len, align, &addr);
997 /* The structure has two double members
998 or one double member and one float member.
999 The argument is passed in a pair of available FAR,
1000 with the low-order bits in the lower-numbered FAR
1001 and the high-order bits in the higher-numbered FAR.
1002 If no a pair of available FAR,
1003 it's passed in a pair of available GAR,
1004 with the low-order bits in the lower-numbered GAR
1005 and the high-order bits in the higher-numbered GAR.
1006 If only one GAR is available,
1007 the low-order bits are in the GAR
1008 and the high-order bits are on stack,
1009 and passed on the stack if no GAR is available. */
1010 else if ((len == 16 && floating_point_members == 2)
1011 || (len == 12 && floating_point_members == 2))
1013 if (!varargs && far >= 2)
1015 pass_in_far (regcache, far--, val);
1016 pass_in_far (regcache, far--, val + regsize);
1018 else if (gar >= 2)
1020 pass_in_gar (regcache, gar--, val);
1021 pass_in_gar (regcache, gar--, val + regsize);
1023 else if (gar == 1)
1025 pass_in_gar (regcache, gar--, val);
1026 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
1028 else
1030 pass_on_stack (regcache, val, len, align, &addr);
1034 /* The structure has both fixed-point and floating-point members. */
1035 else if (fixed_point_members > 0 && floating_point_members > 0)
1037 /* The structure has one floating-point member and one fixed-point member. */
1038 if (floating_point_members == 1 && fixed_point_members == 1)
1040 /* If one FAR and one GAR are available,
1041 the floating-point member of the structure is passed in the FAR,
1042 and the fixed-point member of the structure is passed in the GAR;
1043 If no floating-point registers but two GARs are available,
1044 it's passed in the two GARs;
1045 If only one GAR is available,
1046 the low-order bits are in the GAR
1047 and the high-order bits are on the stack;
1048 And it's passed on the stack if no GAR is available. */
1049 if (!varargs && far > 0 && gar > 0)
1051 if (first_member_is_fixed_point == false)
1053 pass_in_far (regcache, far--, val);
1054 pass_in_gar (regcache, gar--, val + regsize);
1056 else
1058 pass_in_gar (regcache, gar--, val);
1059 pass_in_far (regcache, far--, val + regsize);
1062 else if ((!varargs && far == 0 && gar >= 2) || (varargs && gar >= 2))
1064 pass_in_gar (regcache, gar--, val);
1065 pass_in_gar (regcache, gar--, val + regsize);
1067 else if ((!varargs && far == 0 && gar == 1) || (varargs && gar == 1))
1069 pass_in_gar (regcache, gar--, val);
1070 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
1072 else if ((!varargs && far == 0 && gar == 0) || (varargs && gar == 0))
1074 pass_on_stack (regcache, val, len, align, &addr);
1077 else
1079 /* The argument is passed in a pair of available GAR,
1080 with the low-order bits in the lower-numbered GAR
1081 and the high-order bits in the higher-numbered GAR.
1082 If only one GAR is available,
1083 the low-order bits are in the GAR
1084 and the high-order bits are on the stack,
1085 and passed on the stack if no GAR is available. */
1086 if (gar >= 2)
1088 pass_in_gar (regcache, gar--, val);
1089 pass_in_gar (regcache, gar--, val + regsize);
1091 else if (gar == 1)
1093 pass_in_gar (regcache, gar--, val);
1094 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
1096 else
1098 pass_on_stack (regcache, val, len, align, &addr);
1103 else if (len > 2 * regsize)
1105 /* It's passed by reference and are replaced in the argument list with the address.
1106 If there is an available GAR, the reference is passed in the GAR,
1107 and passed on the stack if no GAR is available. */
1108 sp = align_down (sp - len, 16);
1109 write_memory (sp, val, len);
1111 if (gar > 0)
1112 pass_in_gar (regcache, gar--, (const gdb_byte *) &sp);
1113 else
1114 pass_on_stack (regcache, (const gdb_byte*) &sp, len, regsize, &addr);
1117 break;
1118 case TYPE_CODE_UNION:
1119 /* Union is passed in GAR or stack. */
1120 if (len > 0 && len <= regsize)
1122 /* The argument is passed in a GAR,
1123 or on the stack by value if no GAR is available. */
1124 if (gar > 0)
1125 pass_in_gar (regcache, gar--, val);
1126 else
1127 pass_on_stack (regcache, val, len, align, &addr);
1129 else if (len > regsize && len <= 2 * regsize)
1131 /* The argument is passed in a pair of available GAR,
1132 with the low-order bits in the lower-numbered GAR
1133 and the high-order bits in the higher-numbered GAR.
1134 If only one GAR is available,
1135 the low-order bits are in the GAR
1136 and the high-order bits are on the stack.
1137 The arguments are passed on the stack when no GAR is available. */
1138 if (gar >= 2)
1140 pass_in_gar (regcache, gar--, val);
1141 pass_in_gar (regcache, gar--, val + regsize);
1143 else if (gar == 1)
1145 pass_in_gar (regcache, gar--, val);
1146 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
1148 else
1150 pass_on_stack (regcache, val, len, align, &addr);
1153 else if (len > 2 * regsize)
1155 /* It's passed by reference and are replaced in the argument list with the address.
1156 If there is an available GAR, the reference is passed in the GAR,
1157 and passed on the stack if no GAR is available. */
1158 sp = align_down (sp - len, 16);
1159 write_memory (sp, val, len);
1161 if (gar > 0)
1162 pass_in_gar (regcache, gar--, (const gdb_byte *) &sp);
1163 else
1164 pass_on_stack (regcache, (const gdb_byte*) &sp, len, regsize, &addr);
1166 break;
1167 case TYPE_CODE_COMPLEX:
1169 struct type *target_type = check_typedef (type->target_type ());
1170 size_t target_len = target_type->length ();
1172 if (target_len < regsize)
1174 /* The complex with two float members
1175 is passed in a pair of available FAR,
1176 with the low-order float member bits in the lower-numbered FAR
1177 and the high-order float member bits in the higher-numbered FAR.
1178 If the number of available FAR is less than 2, it's passed in a GAR,
1179 and passed on the stack if no GAR is available. */
1180 if (!varargs && far >= 2)
1182 pass_in_far (regcache, far--, val);
1183 pass_in_far (regcache, far--, val + align);
1185 else if (gar > 0)
1187 pass_in_gar (regcache, gar--, val);
1189 else
1191 pass_on_stack (regcache, val, len, align, &addr);
1194 else if (target_len == regsize)
1196 /* The complex with two double members
1197 is passed in a pair of available FAR,
1198 with the low-order bits in the lower-numbered FAR
1199 and the high-order bits in the higher-numbered FAR.
1200 If no a pair of available FAR,
1201 it's passed in a pair of available GAR,
1202 with the low-order bits in the lower-numbered GAR
1203 and the high-order bits in the higher-numbered GAR.
1204 If only one GAR is available,
1205 the low-order bits are in the GAR
1206 and the high-order bits are on stack,
1207 and passed on the stack if no GAR is available. */
1209 if (!varargs && far >= 2)
1211 pass_in_far (regcache, far--, val);
1212 pass_in_far (regcache, far--, val + align);
1214 else if (gar >= 2)
1216 pass_in_gar (regcache, gar--, val);
1217 pass_in_gar (regcache, gar--, val + align);
1219 else if (gar == 1)
1221 pass_in_gar (regcache, gar--, val);
1222 pass_on_stack (regcache, val + align, len - align, align, &addr);
1224 else
1226 pass_on_stack (regcache, val, len, align, &addr);
1230 else if (target_len == 2 * regsize)
1232 /* The complex with two long double members
1233 is passed by reference and are replaced in the argument list with the address.
1234 If there is an available GAR, the reference is passed in the GAR,
1235 and passed on the stack if no GAR is available. */
1236 sp = align_down (sp - len, 16);
1237 write_memory (sp, val, len);
1239 if (gar > 0)
1240 pass_in_gar (regcache, gar--, (const gdb_byte *) &sp);
1241 else
1242 pass_on_stack (regcache, (const gdb_byte*) &sp, regsize, regsize, &addr);
1245 break;
1246 default:
1247 break;
1251 if (addr > buf)
1253 sp -= addr - buf;
1254 sp = align_down (sp, 16);
1255 write_memory (sp, buf, addr - buf);
1258 regcache_cooked_write_unsigned (regcache, LOONGARCH_RA_REGNUM, bp_addr);
1259 regcache_cooked_write_unsigned (regcache, LOONGARCH_SP_REGNUM, sp);
1261 return sp;
1264 /* Partial transfer of a cooked register. */
1266 static void
1267 loongarch_xfer_reg (struct regcache *regcache,
1268 int regnum, int len, gdb_byte *readbuf,
1269 const gdb_byte *writebuf, size_t offset)
1271 if (readbuf)
1272 regcache->cooked_read_part (regnum, 0, len, readbuf + offset);
1273 if (writebuf)
1274 regcache->cooked_write_part (regnum, 0, len, writebuf + offset);
1277 /* Implement the return_value gdbarch method. */
1279 static enum return_value_convention
1280 loongarch_return_value (struct gdbarch *gdbarch, struct value *function,
1281 struct type *type, struct regcache *regcache,
1282 gdb_byte *readbuf, const gdb_byte *writebuf)
1284 int regsize = register_size (gdbarch, 0);
1285 enum type_code code = type->code ();
1286 size_t len = type->length ();
1287 unsigned int fixed_point_members;
1288 unsigned int floating_point_members;
1289 bool first_member_is_fixed_point;
1290 bool has_long_double;
1291 unsigned int member_offsets[2];
1292 unsigned int member_lens[2];
1293 unsigned int fields;
1294 int a0 = LOONGARCH_A0_REGNUM;
1295 int a1 = LOONGARCH_A0_REGNUM + 1;
1296 int f0 = LOONGARCH_FIRST_FP_REGNUM;
1297 int f1 = LOONGARCH_FIRST_FP_REGNUM + 1;
1299 switch (code)
1301 case TYPE_CODE_INT:
1302 case TYPE_CODE_BOOL:
1303 case TYPE_CODE_CHAR:
1304 case TYPE_CODE_RANGE:
1305 case TYPE_CODE_ENUM:
1306 case TYPE_CODE_PTR:
1308 /* integer or pointer type.
1309 The return value is passed in a0,
1310 the unsigned integer scalars are zero-extended to GRLEN bits,
1311 and the signed integer scalars are sign-extended. */
1312 if (writebuf)
1314 gdb::byte_vector buf (regsize);
1315 if (type->is_unsigned ())
1317 ULONGEST data = extract_unsigned_integer (writebuf, len,
1318 BFD_ENDIAN_LITTLE);
1319 store_unsigned_integer (buf.data (), regsize,
1320 BFD_ENDIAN_LITTLE, data);
1322 else
1324 LONGEST data
1325 = extract_signed_integer (writebuf, len, BFD_ENDIAN_LITTLE);
1326 store_signed_integer (buf.data (), regsize, BFD_ENDIAN_LITTLE,
1327 data);
1330 loongarch_xfer_reg (regcache, a0, regsize, nullptr, buf.data (),
1333 else
1334 loongarch_xfer_reg (regcache, a0, len, readbuf, nullptr, 0);
1336 break;
1337 case TYPE_CODE_FLT:
1338 /* long double type.
1339 The return value is passed in a0 and a1. */
1340 if (len == 2 * regsize)
1342 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1343 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1345 /* float or double type.
1346 The return value is passed in f0. */
1347 else
1349 loongarch_xfer_reg (regcache, f0, len, readbuf, writebuf, 0);
1351 break;
1352 case TYPE_CODE_STRUCT:
1354 fixed_point_members = 0;
1355 floating_point_members = 0;
1356 first_member_is_fixed_point = false;
1357 has_long_double = false;
1358 member_offsets[0] = member_offsets[1] = 0;
1359 member_lens[0] = member_offsets[1] = 0;
1360 fields = 0;
1361 compute_struct_member (type,
1362 &fixed_point_members,
1363 &floating_point_members,
1364 &first_member_is_fixed_point,
1365 &has_long_double);
1366 struct_member_info (type, member_offsets, member_lens, 0, &fields);
1367 /* struct consists of one floating-point member;
1368 struct consists of two floating-point members;
1369 struct consists of one floating-point member
1370 and one integer member. */
1371 if (has_long_double == false
1372 && ((fixed_point_members == 0 && floating_point_members == 1)
1373 || (fixed_point_members == 0 && floating_point_members == 2)
1374 || (fixed_point_members == 1 && floating_point_members == 1)))
1376 if (fixed_point_members == 0 && floating_point_members == 1)
1378 loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
1379 writebuf, member_offsets[0]);
1381 else if (fixed_point_members == 0 && floating_point_members == 2)
1383 loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
1384 writebuf, member_offsets[0]);
1385 loongarch_xfer_reg (regcache, f1, member_lens[1], readbuf,
1386 writebuf, member_offsets[1]);
1388 else if (fixed_point_members == 1 && floating_point_members == 1)
1390 if (first_member_is_fixed_point == false)
1392 loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
1393 writebuf, member_offsets[0]);
1394 loongarch_xfer_reg (regcache, a0, member_lens[1], readbuf,
1395 writebuf, member_offsets[1]);
1397 else
1399 loongarch_xfer_reg (regcache, a0, member_lens[0], readbuf,
1400 writebuf, member_offsets[0]);
1401 loongarch_xfer_reg (regcache, f0, member_lens[1], readbuf,
1402 writebuf, member_offsets[1]);
1406 else if (len > 0 && len <= regsize)
1408 /* The structure has only fixed-point members. */
1409 if (fixed_point_members > 0 && floating_point_members == 0)
1411 /* The return value is passed in a0. */
1412 loongarch_xfer_reg (regcache, a0, len, readbuf, writebuf, 0);
1414 /* The structure has only floating-point members. */
1415 else if (fixed_point_members == 0 && floating_point_members > 0)
1417 /* The structure has one floating-point member.
1418 The return value is passed in f0. */
1419 if (floating_point_members == 1)
1421 loongarch_xfer_reg (regcache, f0, len, readbuf, writebuf, 0);
1423 /* The structure has two floating-point members.
1424 The return value is passed in f0 and f1. */
1425 else if (floating_point_members == 2)
1427 loongarch_xfer_reg (regcache, f0, len / 2, readbuf, writebuf, 0);
1428 loongarch_xfer_reg (regcache, f1, len / 2, readbuf, writebuf, len / 2);
1431 /* The structure has both fixed-point and floating-point members. */
1432 else if (fixed_point_members > 0 && floating_point_members > 0)
1434 /* The structure has one float member and multiple fixed-point members.
1435 The return value is passed in a0. */
1436 if (floating_point_members == 1 && fixed_point_members > 1)
1438 loongarch_xfer_reg (regcache, a0, len, readbuf, writebuf, 0);
1440 /* The structure has one float member and one fixed-point member. */
1441 else if (floating_point_members == 1 && fixed_point_members == 1)
1443 /* The return value is passed in f0 and a0 if the first member is floating-point. */
1444 if (first_member_is_fixed_point == false)
1446 loongarch_xfer_reg (regcache, f0, regsize / 2, readbuf, writebuf, 0);
1447 loongarch_xfer_reg (regcache, a0, regsize / 2, readbuf, writebuf, regsize / 2);
1449 /* The return value is passed in a0 and f0 if the first member is fixed-point. */
1450 else
1452 loongarch_xfer_reg (regcache, a0, regsize / 2, readbuf, writebuf, 0);
1453 loongarch_xfer_reg (regcache, f0, regsize / 2, readbuf, writebuf, regsize / 2);
1458 else if (len > regsize && len <= 2 * regsize)
1460 /* The structure has only fixed-point members. */
1461 if (fixed_point_members > 0 && floating_point_members == 0)
1463 /* The return value is passed in a0 and a1. */
1464 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1465 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1467 /* The structure has only floating-point members. */
1468 else if (fixed_point_members == 0 && floating_point_members > 0)
1470 /* The structure has one long double member
1471 or one double member and two adjacent float members
1472 or 3-4 float members.
1473 The return value is passed in a0 and a1. */
1474 if ((len == 16 && floating_point_members == 1)
1475 || (len == 16 && floating_point_members == 3)
1476 || (len == 12 && floating_point_members == 3)
1477 || (len == 16 && floating_point_members == 4))
1479 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1480 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1482 /* The structure has two double members
1483 or one double member and one float member.
1484 The return value is passed in f0 and f1. */
1485 else if ((len == 16 && floating_point_members == 2)
1486 || (len == 12 && floating_point_members == 2))
1488 loongarch_xfer_reg (regcache, f0, regsize, readbuf, writebuf, 0);
1489 loongarch_xfer_reg (regcache, f1, len - regsize, readbuf, writebuf, regsize);
1492 /* The structure has both fixed-point and floating-point members. */
1493 else if (fixed_point_members > 0 && floating_point_members > 0)
1495 /* The structure has one floating-point member and one fixed-point member. */
1496 if (floating_point_members == 1 && fixed_point_members == 1)
1498 /* The return value is passed in f0 and a0 if the first member is floating-point. */
1499 if (first_member_is_fixed_point == false)
1501 loongarch_xfer_reg (regcache, f0, regsize, readbuf, writebuf, 0);
1502 loongarch_xfer_reg (regcache, a0, len - regsize, readbuf, writebuf, regsize);
1504 /* The return value is passed in a0 and f0 if the first member is fixed-point. */
1505 else
1507 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1508 loongarch_xfer_reg (regcache, f0, len - regsize, readbuf, writebuf, regsize);
1511 else
1513 /* The return value is passed in a0 and a1. */
1514 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1515 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1519 else if (len > 2 * regsize)
1520 return RETURN_VALUE_STRUCT_CONVENTION;
1522 break;
1523 case TYPE_CODE_UNION:
1524 if (len > 0 && len <= regsize)
1526 /* The return value is passed in a0. */
1527 loongarch_xfer_reg (regcache, a0, len, readbuf, writebuf, 0);
1529 else if (len > regsize && len <= 2 * regsize)
1531 /* The return value is passed in a0 and a1. */
1532 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1533 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1535 else if (len > 2 * regsize)
1536 return RETURN_VALUE_STRUCT_CONVENTION;
1537 break;
1538 case TYPE_CODE_COMPLEX:
1539 if (len > 0 && len <= 2 * regsize)
1541 /* The return value is passed in f0 and f1. */
1542 loongarch_xfer_reg (regcache, f0, len / 2, readbuf, writebuf, 0);
1543 loongarch_xfer_reg (regcache, f1, len / 2, readbuf, writebuf, len / 2);
1545 else if (len > 2 * regsize)
1546 return RETURN_VALUE_STRUCT_CONVENTION;
1547 break;
1548 default:
1549 break;
1552 return RETURN_VALUE_REGISTER_CONVENTION;
1555 /* Implement the dwarf2_reg_to_regnum gdbarch method. */
1557 static int
1558 loongarch_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
1560 if (regnum >= 0 && regnum < 32)
1561 return regnum;
1562 else if (regnum >= 32 && regnum < 66)
1563 return LOONGARCH_FIRST_FP_REGNUM + regnum - 32;
1564 else
1565 return -1;
1568 static constexpr gdb_byte loongarch_default_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
1569 typedef BP_MANIPULATION (loongarch_default_breakpoint) loongarch_breakpoint;
1571 /* Extract a set of required target features out of ABFD. If ABFD is nullptr
1572 then a LOONGARCH_GDBARCH_FEATURES is returned in its default state. */
1574 static struct loongarch_gdbarch_features
1575 loongarch_features_from_bfd (const bfd *abfd)
1577 struct loongarch_gdbarch_features features;
1579 /* Now try to improve on the defaults by looking at the binary we are
1580 going to execute. We assume the user knows what they are doing and
1581 that the target will match the binary. Remember, this code path is
1582 only used at all if the target hasn't given us a description, so this
1583 is really a last ditched effort to do something sane before giving
1584 up. */
1585 if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1587 unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
1588 int e_flags = elf_elfheader (abfd)->e_flags;
1590 if (eclass == ELFCLASS32)
1591 features.xlen = 4;
1592 else if (eclass == ELFCLASS64)
1593 features.xlen = 8;
1594 else
1595 internal_error (_("unknown ELF header class %d"), eclass);
1597 if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
1598 features.fputype = SINGLE_FLOAT;
1599 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
1600 features.fputype = DOUBLE_FLOAT;
1603 return features;
1606 /* Find a suitable default target description. Use the contents of INFO,
1607 specifically the bfd object being executed, to guide the selection of a
1608 suitable default target description. */
1610 static const struct target_desc *
1611 loongarch_find_default_target_description (const struct gdbarch_info info)
1613 /* Extract desired feature set from INFO. */
1614 struct loongarch_gdbarch_features features
1615 = loongarch_features_from_bfd (info.abfd);
1617 /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
1618 maybe there was no bfd object. In this case we fall back to a minimal
1619 useful target, the x-register size is selected based on the architecture
1620 from INFO. */
1621 if (features.xlen == 0)
1622 features.xlen = info.bfd_arch_info->bits_per_address == 32 ? 4 : 8;
1624 /* If the FPUTYPE field is still 0 then we got nothing useful from INFO.BFD,
1625 maybe there was no bfd object. In this case we fall back to a usual useful
1626 target with double float. */
1627 if (features.fputype == 0)
1628 features.fputype = DOUBLE_FLOAT;
1630 /* Now build a target description based on the feature set. */
1631 return loongarch_lookup_target_description (features);
1634 static int
1635 loongarch_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1636 const struct reggroup *group)
1638 if (gdbarch_register_name (gdbarch, regnum) == NULL
1639 || *gdbarch_register_name (gdbarch, regnum) == '\0')
1640 return 0;
1642 int raw_p = regnum < gdbarch_num_regs (gdbarch);
1644 if (group == save_reggroup || group == restore_reggroup)
1645 return raw_p;
1647 if (group == all_reggroup)
1648 return 1;
1650 if (0 <= regnum && regnum <= LOONGARCH_BADV_REGNUM)
1651 return group == general_reggroup;
1653 /* Only ORIG_A0, PC, BADV in general_reggroup */
1654 if (group == general_reggroup)
1655 return 0;
1657 if (LOONGARCH_FIRST_FP_REGNUM <= regnum && regnum <= LOONGARCH_FCSR_REGNUM)
1658 return group == float_reggroup;
1660 /* Only $fx / $fccx / $fcsr in float_reggroup */
1661 if (group == float_reggroup)
1662 return 0;
1664 if (LOONGARCH_FIRST_LSX_REGNUM <= regnum
1665 && regnum < LOONGARCH_FIRST_LASX_REGNUM + LOONGARCH_LINUX_NUM_LASXREGSET)
1666 return group == vector_reggroup;
1668 /* Only $vrx / $xrx in vector_reggroup */
1669 if (group == vector_reggroup)
1670 return 0;
1672 int ret = tdesc_register_in_reggroup_p (gdbarch, regnum, group);
1673 if (ret != -1)
1674 return ret;
1676 return default_register_reggroup_p (gdbarch, regnum, group);
1679 /* Initialize the current architecture based on INFO */
1681 static struct gdbarch *
1682 loongarch_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1684 size_t regnum = 0;
1685 struct loongarch_gdbarch_features features;
1686 tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
1687 const struct target_desc *tdesc = info.target_desc;
1689 /* Ensure we always have a target description. */
1690 if (!tdesc_has_registers (tdesc))
1691 tdesc = loongarch_find_default_target_description (info);
1693 const struct tdesc_feature *feature_cpu
1694 = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.base");
1695 if (feature_cpu == nullptr)
1696 return nullptr;
1699 /* Validate the description provides the mandatory base registers
1700 and allocate their numbers. */
1701 bool valid_p = true;
1702 for (int i = 0; i < 32; i++)
1703 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++,
1704 loongarch_r_normal_name[i] + 1);
1705 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++, "orig_a0");
1706 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++, "pc");
1707 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++, "badv");
1708 if (!valid_p)
1709 return nullptr;
1711 const struct tdesc_feature *feature_fpu
1712 = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.fpu");
1713 if (feature_fpu == nullptr)
1714 return nullptr;
1716 /* Validate the description provides the fpu registers and
1717 allocate their numbers. */
1718 regnum = LOONGARCH_FIRST_FP_REGNUM;
1719 for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++)
1720 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data.get (), regnum++,
1721 loongarch_f_normal_name[i] + 1);
1722 for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++)
1723 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data.get (), regnum++,
1724 loongarch_c_normal_name[i] + 1);
1725 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data.get (), regnum++, "fcsr");
1726 if (!valid_p)
1727 return nullptr;
1729 const struct tdesc_feature *feature_lsx
1730 = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.lsx");
1731 if (feature_lsx == nullptr)
1732 return nullptr;
1734 /* Validate the description provides the lsx registers and
1735 allocate their numbers. */
1736 regnum = LOONGARCH_FIRST_LSX_REGNUM;
1737 for (int i = 0; i < LOONGARCH_LINUX_NUM_LSXREGSET; i++)
1738 valid_p &= tdesc_numbered_register (feature_lsx, tdesc_data.get (), regnum++,
1739 loongarch_v_normal_name[i] + 1);
1740 if (!valid_p)
1741 return nullptr;
1743 const struct tdesc_feature *feature_lasx
1744 = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.lasx");
1745 if (feature_lasx == nullptr)
1746 return nullptr;
1748 /* Validate the description provides the lasx registers and
1749 allocate their numbers. */
1750 regnum = LOONGARCH_FIRST_LASX_REGNUM;
1751 for (int i = 0; i < LOONGARCH_LINUX_NUM_LASXREGSET; i++)
1752 valid_p &= tdesc_numbered_register (feature_lasx, tdesc_data.get (), regnum++,
1753 loongarch_x_normal_name[i] + 1);
1754 if (!valid_p)
1755 return nullptr;
1757 const struct tdesc_feature *feature_lbt
1758 = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.lbt");
1759 if (feature_lbt == nullptr)
1760 return nullptr;
1762 /* Validate the description provides the lbt registers and
1763 allocate their numbers. */
1764 regnum = LOONGARCH_FIRST_SCR_REGNUM;
1765 for (int i = 0; i < LOONGARCH_LINUX_NUM_SCR; i++)
1766 valid_p &= tdesc_numbered_register (feature_lbt, tdesc_data.get (), regnum++,
1767 loongarch_cr_normal_name[i] + 1);
1768 valid_p &= tdesc_numbered_register (feature_lbt, tdesc_data.get (), regnum++,
1769 "eflags");
1770 valid_p &= tdesc_numbered_register (feature_lbt, tdesc_data.get (), regnum++,
1771 "ftop");
1772 if (!valid_p)
1773 return nullptr;
1775 /* LoongArch code is always little-endian. */
1776 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
1778 /* Have a look at what the supplied (if any) bfd object requires of the
1779 target, then check that this matches with what the target is
1780 providing. */
1781 struct loongarch_gdbarch_features abi_features
1782 = loongarch_features_from_bfd (info.abfd);
1784 /* If the ABI_FEATURES xlen or fputype is 0 then this indicates we got
1785 no useful abi features from the INFO object. In this case we just
1786 treat the hardware features as defining the abi. */
1787 if (abi_features.xlen == 0)
1789 int xlen_bitsize = tdesc_register_bitsize (feature_cpu, "pc");
1790 features.xlen = (xlen_bitsize / 8);
1791 features.fputype = abi_features.fputype;
1792 abi_features = features;
1794 if (abi_features.fputype == 0)
1796 features.xlen = abi_features.xlen;
1797 features.fputype = DOUBLE_FLOAT;
1798 abi_features = features;
1801 /* Find a candidate among the list of pre-declared architectures. */
1802 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1803 arches != nullptr;
1804 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1806 /* Check that the feature set of the ARCHES matches the feature set
1807 we are looking for. If it doesn't then we can't reuse this
1808 gdbarch. */
1809 loongarch_gdbarch_tdep *candidate_tdep
1810 = gdbarch_tdep<loongarch_gdbarch_tdep> (arches->gdbarch);
1812 if (candidate_tdep->abi_features != abi_features)
1813 continue;
1815 break;
1818 if (arches != nullptr)
1819 return arches->gdbarch;
1821 /* None found, so create a new architecture from the information provided. */
1822 gdbarch *gdbarch
1823 = gdbarch_alloc (&info, gdbarch_tdep_up (new loongarch_gdbarch_tdep));
1824 loongarch_gdbarch_tdep *tdep = gdbarch_tdep<loongarch_gdbarch_tdep> (gdbarch);
1826 tdep->abi_features = abi_features;
1828 /* Target data types. */
1829 set_gdbarch_short_bit (gdbarch, 16);
1830 set_gdbarch_int_bit (gdbarch, 32);
1831 set_gdbarch_long_bit (gdbarch, info.bfd_arch_info->bits_per_address);
1832 set_gdbarch_long_long_bit (gdbarch, 64);
1833 set_gdbarch_float_bit (gdbarch, 32);
1834 set_gdbarch_double_bit (gdbarch, 64);
1835 set_gdbarch_long_double_bit (gdbarch, 128);
1836 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
1837 set_gdbarch_ptr_bit (gdbarch, info.bfd_arch_info->bits_per_address);
1838 set_gdbarch_char_signed (gdbarch, 0);
1840 info.target_desc = tdesc;
1841 info.tdesc_data = tdesc_data.get ();
1843 for (int i = 0; i < ARRAY_SIZE (loongarch_r_alias); ++i)
1844 if (loongarch_r_alias[i][0] != '\0')
1845 user_reg_add (gdbarch, loongarch_r_alias[i] + 1,
1846 value_of_loongarch_user_reg, (void *) (size_t) i);
1848 for (int i = 0; i < ARRAY_SIZE (loongarch_f_alias); ++i)
1850 if (loongarch_f_alias[i][0] != '\0')
1851 user_reg_add (gdbarch, loongarch_f_alias[i] + 1,
1852 value_of_loongarch_user_reg,
1853 (void *) (size_t) (LOONGARCH_FIRST_FP_REGNUM + i));
1856 /* Information about registers. */
1857 set_gdbarch_num_regs (gdbarch, regnum);
1858 set_gdbarch_sp_regnum (gdbarch, LOONGARCH_SP_REGNUM);
1859 set_gdbarch_pc_regnum (gdbarch, LOONGARCH_PC_REGNUM);
1861 /* Finalise the target description registers. */
1862 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
1864 /* Functions handling dummy frames. */
1865 set_gdbarch_push_dummy_call (gdbarch, loongarch_push_dummy_call);
1867 /* Return value info */
1868 set_gdbarch_return_value (gdbarch, loongarch_return_value);
1870 /* Advance PC across function entry code. */
1871 set_gdbarch_skip_prologue (gdbarch, loongarch_skip_prologue);
1873 /* Stack grows downward. */
1874 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1876 /* Frame info. */
1877 set_gdbarch_frame_align (gdbarch, loongarch_frame_align);
1879 /* Breakpoint manipulation. */
1880 set_gdbarch_software_single_step (gdbarch, loongarch_software_single_step);
1881 set_gdbarch_breakpoint_kind_from_pc (gdbarch, loongarch_breakpoint::kind_from_pc);
1882 set_gdbarch_sw_breakpoint_from_kind (gdbarch, loongarch_breakpoint::bp_from_kind);
1883 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
1885 /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder. */
1886 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, loongarch_dwarf2_reg_to_regnum);
1887 dwarf2_append_unwinders (gdbarch);
1888 frame_unwind_append_unwinder (gdbarch, &loongarch_frame_unwind);
1890 /* Hook in OS ABI-specific overrides, if they have been registered. */
1891 gdbarch_init_osabi (info, gdbarch);
1892 set_gdbarch_register_reggroup_p (gdbarch, loongarch_register_reggroup_p);
1894 return gdbarch;
1897 /* LoongArch record/replay enumerations and structures. */
1899 enum loongarch_record_result
1901 LOONGARCH_RECORD_SUCCESS,
1902 LOONGARCH_RECORD_UNSUPPORTED,
1903 LOONGARCH_RECORD_UNKNOWN
1906 struct loongarch_record_s
1908 struct gdbarch *gdbarch;
1909 struct regcache *regcache;
1910 CORE_ADDR this_addr; /* Addr of insn to be recorded. */
1911 uint32_t insn; /* Insn to be recorded. */
1914 /* Record handler for data processing instructions. */
1916 static int
1917 loongarch_record_data_proc_insn (loongarch_record_s *loongarch_record)
1919 int rd_num;
1920 rd_num = loongarch_decode_imm ("0:5", loongarch_record->insn, 0);
1921 if (record_full_arch_list_add_reg (loongarch_record->regcache, rd_num))
1922 return -1;
1924 return LOONGARCH_RECORD_SUCCESS;
1927 /* Record handler for read time instructions. */
1929 static int
1930 loongarch_record_read_time_insn (loongarch_record_s *loongarch_record)
1932 int rd_num, rj_num;
1933 rd_num = loongarch_decode_imm ("0:5", loongarch_record->insn, 0);
1934 rj_num = loongarch_decode_imm ("5:5", loongarch_record->insn, 0);
1935 if (record_full_arch_list_add_reg (loongarch_record->regcache, rd_num))
1936 return -1;
1937 if (record_full_arch_list_add_reg (loongarch_record->regcache, rj_num))
1938 return -1;
1940 return LOONGARCH_RECORD_SUCCESS;
1943 /* Record handler for branch instructions. */
1945 static int
1946 loongarch_record_branch_insn (loongarch_record_s *loongarch_record)
1948 if (is_jirl_insn (loongarch_record->insn))
1950 int rd_num;
1951 rd_num = loongarch_decode_imm ("0:5", loongarch_record->insn, 0);
1952 if (record_full_arch_list_add_reg (loongarch_record->regcache, rd_num))
1953 return -1;
1955 else if (is_bl_insn (loongarch_record->insn))
1957 if (record_full_arch_list_add_reg (loongarch_record->regcache,
1958 LOONGARCH_RA_REGNUM))
1959 return -1;
1962 return LOONGARCH_RECORD_SUCCESS;
1965 /* Record handler for float data processing instructions. */
1967 static int
1968 loongarch_record_float_data_proc_insn (loongarch_record_s *loongarch_record)
1970 int fd_num;
1971 fd_num = loongarch_decode_imm ("0:5", loongarch_record->insn, 0)
1972 + LOONGARCH_FIRST_FP_REGNUM;
1974 if (record_full_arch_list_add_reg (loongarch_record->regcache, fd_num))
1975 return -1;
1977 return LOONGARCH_RECORD_SUCCESS;
1980 /* Record handler for move gr to fcsr instructions. */
1982 static int
1983 loongarch_record_movgr2fcsr_insn (loongarch_record_s *loongarch_record)
1985 if (record_full_arch_list_add_reg (loongarch_record->regcache,
1986 LOONGARCH_FCSR_REGNUM))
1987 return -1;
1989 return LOONGARCH_RECORD_SUCCESS;
1992 /* Record handler for move gr/fr to fcc instructions. */
1994 static int
1995 loongarch_record_mov2cf_insn (loongarch_record_s *loongarch_record)
1997 int cd;
1998 cd = loongarch_decode_imm ("0:3", loongarch_record->insn, 0);
1999 if (record_full_arch_list_add_reg (loongarch_record->regcache, cd))
2000 return -1;
2002 return LOONGARCH_RECORD_SUCCESS;
2005 /* Record handler for float instructions. */
2007 static unsigned int
2008 loongarch_record_float_insn (loongarch_record_s *loongarch_record)
2010 if (is_movgr2fcsr_insn (loongarch_record->insn))
2011 return loongarch_record_movgr2fcsr_insn (loongarch_record);
2012 else if (is_mov2cf_insn (loongarch_record->insn))
2013 return loongarch_record_mov2cf_insn (loongarch_record);
2014 else
2015 return loongarch_record_float_data_proc_insn (loongarch_record);
2018 /* Record handler for store instructions. */
2020 static int
2021 loongarch_record_store_insn (loongarch_record_s *loongarch_record)
2023 enum store_types
2025 STB, STH, STW, STD, STXB, STXH, STXW, STXD, STPTRW, STPTRD,
2026 SCW, SCD, FSTS, FSTD, FSTXS, FSTXD, VST, XVST, NOT_STORE
2028 int store_type, data_size, rj_num;
2029 uint64_t address, rj_val;
2031 store_type = is_st_b_insn (loongarch_record->insn) ? STB :
2032 is_st_h_insn (loongarch_record->insn) ? STH :
2033 is_st_w_insn (loongarch_record->insn) ? STW :
2034 is_st_d_insn (loongarch_record->insn) ? STD :
2035 is_stx_b_insn (loongarch_record->insn) ? STXB :
2036 is_stx_h_insn (loongarch_record->insn) ? STXH :
2037 is_stx_w_insn (loongarch_record->insn) ? STXW :
2038 is_stx_d_insn (loongarch_record->insn) ? STXD :
2039 is_stptr_w_insn (loongarch_record->insn) ? STPTRW :
2040 is_stptr_d_insn (loongarch_record->insn) ? STPTRD :
2041 is_sc_w_insn (loongarch_record->insn) ? SCW :
2042 is_sc_d_insn (loongarch_record->insn) ? SCD :
2043 is_fst_s_insn (loongarch_record->insn) ? FSTS :
2044 is_fst_d_insn (loongarch_record->insn) ? FSTD :
2045 is_fstx_s_insn (loongarch_record->insn) ? FSTXS :
2046 is_fstx_d_insn (loongarch_record->insn) ? FSTXD :
2047 is_vst_insn (loongarch_record->insn) ? VST :
2048 is_xvst_insn (loongarch_record->insn) ? XVST :
2049 NOT_STORE;
2050 rj_num = loongarch_decode_imm ("5:5", loongarch_record->insn, 0);
2051 regcache_raw_read_unsigned (loongarch_record->regcache, rj_num, &rj_val);
2053 if (store_type == STB || store_type == STH || store_type == STW
2054 || store_type == STD || store_type == FSTS || store_type == FSTD
2055 || store_type == VST || store_type == XVST)
2057 int imm;
2058 imm = loongarch_decode_imm ("10:12", loongarch_record->insn, 1);
2059 address = rj_val + imm;
2060 switch (store_type)
2062 case STB:
2063 data_size = 1;
2064 break;
2065 case STH:
2066 data_size = 2;
2067 break;
2068 case STW:
2069 case FSTS:
2070 data_size = 4;
2071 break;
2072 case STD:
2073 case FSTD:
2074 data_size = 8;
2075 break;
2076 case VST:
2077 data_size = 16;
2078 break;
2079 case XVST:
2080 data_size = 32;
2081 break;
2082 default:
2083 data_size = 0;
2084 break;
2087 if (record_full_arch_list_add_mem (address, data_size))
2088 return -1;
2090 else if (store_type == STXB || store_type == STXH || store_type == STXW
2091 || store_type == STXD || store_type == FSTXS || store_type == FSTXD)
2093 int rk_num;
2094 uint64_t rk_val;
2095 rk_num = loongarch_decode_imm ("10:5", loongarch_record->insn, 0);
2096 regcache_raw_read_unsigned (loongarch_record->regcache, rk_num, &rk_val);
2097 address = rj_val + rk_val;
2098 switch (store_type)
2100 case STXB:
2101 data_size = 1;
2102 break;
2103 case STXH:
2104 data_size = 2;
2105 break;
2106 case STXW:
2107 case FSTXS:
2108 data_size = 4;
2109 break;
2110 case STXD:
2111 case FSTXD:
2112 data_size = 8;
2113 break;
2114 default:
2115 data_size = 0;
2116 break;
2119 if (record_full_arch_list_add_mem (address, data_size))
2120 return -1;
2122 else if (store_type == STPTRW || store_type == STPTRD || store_type == SCW
2123 || store_type == SCD)
2125 int imm;
2126 imm = loongarch_decode_imm ("10:14<<2", loongarch_record->insn, 1);
2127 address = rj_val + imm;
2128 switch (store_type)
2130 case STPTRW:
2131 case SCW:
2132 data_size = 4;
2133 break;
2134 case STPTRD:
2135 case SCD:
2136 data_size = 8;
2137 break;
2138 default:
2139 data_size = 0;
2140 break;
2143 if (record_full_arch_list_add_mem (address, data_size))
2144 return -1;
2147 return LOONGARCH_RECORD_SUCCESS;
2150 /* Record handler for atomic memory access instructions. */
2152 static int
2153 loongarch_record_atomic_access_insn (loongarch_record_s *loongarch_record)
2155 int rj_num, rd_num, rk_num, length;
2156 int data_size;
2157 uint64_t address;
2158 rd_num = loongarch_decode_imm ("0:5", loongarch_record->insn, 0);
2159 rj_num = loongarch_decode_imm ("5:5", loongarch_record->insn, 0);
2160 rk_num = loongarch_decode_imm ("10:5", loongarch_record->insn, 0);
2161 regcache_raw_read_unsigned (loongarch_record->regcache, rj_num, &address);
2162 if (is_basic_am_w_d_insn (loongarch_record->insn))
2164 length = loongarch_decode_imm ("15:1", loongarch_record->insn, 0);
2165 data_size = length == 1 ? 8 : 4;
2166 if (record_full_arch_list_add_mem (address, data_size))
2167 return -1;
2169 if (is_am_b_h_insn (loongarch_record->insn))
2171 length = loongarch_decode_imm ("15:1", loongarch_record->insn, 0);
2172 data_size = length == 1 ? 2 : 1;
2173 if (record_full_arch_list_add_mem (address, data_size))
2174 return -1;
2176 if (is_amcas_insn (loongarch_record->insn))
2178 length = loongarch_decode_imm ("15:2", loongarch_record->insn, 0);
2179 switch (length)
2181 case 0x0:
2182 data_size = 1;
2183 break;
2184 case 0x1:
2185 data_size = 2;
2186 break;
2187 case 0x2:
2188 data_size = 4;
2189 break;
2190 case 0x3:
2191 data_size = 8;
2192 break;
2193 default:
2194 data_size = 0;
2195 break;
2197 if (record_full_arch_list_add_mem (address, data_size))
2198 return -1;
2201 if (record_full_arch_list_add_reg (loongarch_record->regcache, rd_num))
2202 return -1;
2204 if (is_amswap_insn (loongarch_record->insn))
2206 if (record_full_arch_list_add_reg (loongarch_record->regcache, rk_num))
2207 return -1;
2210 return LOONGARCH_RECORD_SUCCESS;
2213 /* Record handler for bound check load instructions. */
2215 static int
2216 loongarch_record_bound_check_load_insn (loongarch_record_s *loongarch_record)
2218 int rd_num, rj_num, rk_num, fd_num;
2219 uint64_t rj_val, rk_val;
2220 rd_num = loongarch_decode_imm ("0:5", loongarch_record->insn, 0);
2221 fd_num = loongarch_decode_imm ("0:5", loongarch_record->insn, 0);
2222 rj_num = loongarch_decode_imm ("5:5", loongarch_record->insn, 0);
2223 rk_num = loongarch_decode_imm ("10:5", loongarch_record->insn, 0);
2224 regcache_raw_read_unsigned (loongarch_record->regcache, rj_num, &rj_val);
2225 regcache_raw_read_unsigned (loongarch_record->regcache, rk_num, &rk_val);
2227 if ((is_ldgt_insn (loongarch_record->insn) && (rj_val > rk_val))
2228 || (is_ldle_insn (loongarch_record->insn) && (rj_val <= rk_val)))
2230 if (record_full_arch_list_add_reg (loongarch_record->regcache, rd_num))
2231 return -1;
2233 else if ((is_fldgt_insn (loongarch_record->insn) && (rj_val > rk_val))
2234 || (is_fldle_insn (loongarch_record->insn) && (rj_val <= rk_val)))
2236 if (record_full_arch_list_add_reg (loongarch_record->regcache, fd_num))
2237 return -1;
2240 return LOONGARCH_RECORD_SUCCESS;
2243 /* Record handler for bound check store instructions. */
2245 static int
2246 loongarch_record_bound_check_store_insn (loongarch_record_s *loongarch_record)
2248 int rj_num, rk_num;
2249 int data_size;
2250 uint64_t rj_val, rk_val;
2251 uint32_t length_opcode;
2252 rj_num = loongarch_decode_imm ("5:5", loongarch_record->insn, 0);
2253 rk_num = loongarch_decode_imm ("10:5", loongarch_record->insn, 0);
2254 regcache_raw_read_unsigned (loongarch_record->regcache, rj_num, &rj_val);
2255 regcache_raw_read_unsigned (loongarch_record->regcache, rk_num, &rk_val);
2257 if ((is_stgt_insn (loongarch_record->insn) && (rj_val > rk_val))
2258 || (is_stle_insn (loongarch_record->insn) && (rj_val <= rk_val)))
2260 length_opcode = loongarch_record->insn & 0x00018000;
2261 switch (length_opcode)
2263 case 0x00000000:
2264 data_size = 1;
2265 break;
2266 case 0x00008000:
2267 data_size = 2;
2268 break;
2269 case 0x00010000:
2270 data_size = 4;
2271 break;
2272 case 0x00018000:
2273 data_size = 8;
2274 break;
2275 default:
2276 data_size = 0;
2277 break;
2280 if (record_full_arch_list_add_mem (rj_val, data_size))
2281 return -1;
2283 else if ((is_fstgt_insn (loongarch_record->insn) && (rj_val > rk_val))
2284 || (is_fstle_insn (loongarch_record->insn) && (rj_val <= rk_val)))
2286 length_opcode = loongarch_record->insn & 0x00008000;
2287 switch (length_opcode)
2289 case 0x00000000:
2290 data_size = 4;
2291 break;
2292 case 0x00008000:
2293 data_size = 8;
2294 break;
2295 default:
2296 data_size = 0;
2297 break;
2300 if (record_full_arch_list_add_mem (rj_val, data_size))
2301 return -1;
2304 return LOONGARCH_RECORD_SUCCESS;
2307 /* Record handler for special instructions like privilege instructions,
2308 barrier instructions and cache related instructions etc. */
2310 static int
2311 loongarch_record_special_insn (loongarch_record_s *loongarch_record)
2313 return LOONGARCH_RECORD_SUCCESS;
2316 /* Record handler for syscall instructions. */
2318 static int
2319 loongarch_record_syscall_insn (loongarch_record_s *loongarch_record)
2321 uint64_t syscall_number;
2322 struct loongarch_gdbarch_tdep *tdep
2323 = gdbarch_tdep<loongarch_gdbarch_tdep> (loongarch_record->gdbarch);
2325 regcache_raw_read_unsigned (loongarch_record->regcache, LOONGARCH_A7_REGNUM,
2326 &syscall_number);
2328 return tdep->loongarch_syscall_record (loongarch_record->regcache,
2329 syscall_number);
2332 /* Decode insns type and invoke its record handler. */
2334 static int
2335 loongarch_record_decode_insn_handler (loongarch_record_s *loongarch_record)
2337 if (is_data_process_insn (loongarch_record->insn))
2338 return loongarch_record_data_proc_insn (loongarch_record);
2339 else if (is_branch_insn (loongarch_record->insn))
2340 return loongarch_record_branch_insn (loongarch_record);
2341 else if (is_store_insn (loongarch_record->insn))
2342 return loongarch_record_store_insn (loongarch_record);
2343 else if (is_read_time_insn (loongarch_record->insn))
2344 return loongarch_record_read_time_insn (loongarch_record);
2345 else if (is_float_insn (loongarch_record->insn))
2346 return loongarch_record_float_insn (loongarch_record);
2347 else if (is_special_insn (loongarch_record->insn))
2348 return loongarch_record_special_insn (loongarch_record);
2349 else if (is_atomic_access_insn (loongarch_record->insn))
2350 return loongarch_record_atomic_access_insn (loongarch_record);
2351 else if (is_bound_check_load_insn (loongarch_record->insn))
2352 return loongarch_record_bound_check_load_insn (loongarch_record);
2353 else if (is_bound_check_store_insn (loongarch_record->insn))
2354 return loongarch_record_bound_check_store_insn (loongarch_record);
2355 else if (is_syscall_insn (loongarch_record->insn))
2356 return loongarch_record_syscall_insn (loongarch_record);
2358 return LOONGARCH_RECORD_UNSUPPORTED;
2361 /* Parse the current instruction and record the values of the registers and
2362 memory that will be changed in current instruction to record_arch_list
2363 return -1 if something is wrong. */
2366 loongarch_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
2367 CORE_ADDR insn_addr)
2369 int ret = 0;
2370 loongarch_record_s loongarch_record;
2372 /* reset the content of loongarch_record */
2373 memset (&loongarch_record, 0, sizeof (loongarch_record_s));
2375 /* write the loongarch_record */
2376 loongarch_record.gdbarch = gdbarch;
2377 loongarch_record.regcache = regcache;
2378 loongarch_record.this_addr = insn_addr;
2380 /* Get the current instruction */
2381 loongarch_record.insn = (uint32_t) loongarch_fetch_instruction (insn_addr);
2382 ret = loongarch_record_decode_insn_handler (&loongarch_record);
2383 if (ret == LOONGARCH_RECORD_UNSUPPORTED)
2385 gdb_printf (gdb_stderr,
2386 _("Process record does not support instruction "
2387 "0x%0x at address %s.\n"),
2388 loongarch_record.insn,
2389 paddress (gdbarch, insn_addr));
2390 return -1;
2392 if (ret == LOONGARCH_RECORD_SUCCESS)
2394 /* Record PC registers. */
2395 if (record_full_arch_list_add_reg (loongarch_record.regcache,
2396 LOONGARCH_PC_REGNUM))
2397 return -1;
2399 if (record_full_arch_list_add_end ())
2400 return -1;
2403 return ret;
2406 void _initialize_loongarch_tdep ();
2407 void
2408 _initialize_loongarch_tdep ()
2410 gdbarch_register (bfd_arch_loongarch, loongarch_gdbarch_init, nullptr);