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 "dwarf2/frame.h"
23 #include "extract-store-integer.h"
24 #include "frame-unwind.h"
26 #include "loongarch-tdep.h"
27 #include "reggroups.h"
29 #include "target-descriptions.h"
30 #include "trad-frame.h"
31 #include "user-regs.h"
33 /* Fetch the instruction at PC. */
36 loongarch_fetch_instruction (CORE_ADDR pc
)
38 size_t insn_len
= loongarch_insn_length (0);
39 gdb::byte_vector
buf (insn_len
);
42 err
= target_read_memory (pc
, buf
.data (), insn_len
);
44 memory_error (TARGET_XFER_E_IO
, pc
);
46 return extract_unsigned_integer (buf
.data (), insn_len
, BFD_ENDIAN_LITTLE
);
49 /* Return TRUE if INSN is a unconditional branch instruction, otherwise return FALSE. */
52 loongarch_insn_is_uncond_branch (insn_t insn
)
54 if ((insn
& 0xfc000000) == 0x4c000000 /* jirl */
55 || (insn
& 0xfc000000) == 0x50000000 /* b */
56 || (insn
& 0xfc000000) == 0x54000000) /* bl */
61 /* Return TRUE if INSN is a conditional branch instruction, otherwise return FALSE. */
64 loongarch_insn_is_cond_branch (insn_t insn
)
66 if ((insn
& 0xfc000000) == 0x58000000 /* beq */
67 || (insn
& 0xfc000000) == 0x5c000000 /* bne */
68 || (insn
& 0xfc000000) == 0x60000000 /* blt */
69 || (insn
& 0xfc000000) == 0x64000000 /* bge */
70 || (insn
& 0xfc000000) == 0x68000000 /* bltu */
71 || (insn
& 0xfc000000) == 0x6c000000 /* bgeu */
72 || (insn
& 0xfc000000) == 0x40000000 /* beqz */
73 || (insn
& 0xfc000000) == 0x44000000) /* bnez */
78 /* Return TRUE if INSN is a branch instruction, otherwise return FALSE. */
81 loongarch_insn_is_branch (insn_t insn
)
83 bool is_uncond
= loongarch_insn_is_uncond_branch (insn
);
84 bool is_cond
= loongarch_insn_is_cond_branch (insn
);
86 return (is_uncond
|| is_cond
);
89 /* Return TRUE if INSN is a Load Linked instruction, otherwise return FALSE. */
92 loongarch_insn_is_ll (insn_t insn
)
94 if ((insn
& 0xff000000) == 0x20000000 /* ll.w */
95 || (insn
& 0xff000000) == 0x22000000) /* ll.d */
100 /* Return TRUE if INSN is a Store Conditional instruction, otherwise return FALSE. */
103 loongarch_insn_is_sc (insn_t insn
)
105 if ((insn
& 0xff000000) == 0x21000000 /* sc.w */
106 || (insn
& 0xff000000) == 0x23000000) /* sc.d */
111 /* Analyze the function prologue from START_PC to LIMIT_PC.
112 Return the address of the first instruction past the prologue. */
115 loongarch_scan_prologue (struct gdbarch
*gdbarch
, CORE_ADDR start_pc
,
116 CORE_ADDR limit_pc
, const frame_info_ptr
&this_frame
,
117 struct trad_frame_cache
*this_cache
)
119 CORE_ADDR cur_pc
= start_pc
, prologue_end
= 0;
120 int32_t sp
= LOONGARCH_SP_REGNUM
;
121 int32_t fp
= LOONGARCH_FP_REGNUM
;
122 int32_t reg_value
[32] = {0};
123 int32_t reg_used
[32] = {1, 0};
125 while (cur_pc
< limit_pc
)
127 insn_t insn
= loongarch_fetch_instruction (cur_pc
);
128 size_t insn_len
= loongarch_insn_length (insn
);
129 int32_t rd
= loongarch_decode_imm ("0:5", insn
, 0);
130 int32_t rj
= loongarch_decode_imm ("5:5", insn
, 0);
131 int32_t rk
= loongarch_decode_imm ("10:5", insn
, 0);
132 int32_t si12
= loongarch_decode_imm ("10:12", insn
, 1);
133 int32_t si20
= loongarch_decode_imm ("5:20", insn
, 1);
135 if ((insn
& 0xffc00000) == 0x02c00000 /* addi.d sp,sp,si12 */
136 && rd
== sp
&& rj
== sp
&& si12
< 0)
138 prologue_end
= cur_pc
+ insn_len
;
140 else if ((insn
& 0xffc00000) == 0x02c00000 /* addi.d fp,sp,si12 */
141 && rd
== fp
&& rj
== sp
&& si12
> 0)
143 prologue_end
= cur_pc
+ insn_len
;
145 else if ((insn
& 0xffc00000) == 0x29c00000 /* st.d rd,sp,si12 */
148 prologue_end
= cur_pc
+ insn_len
;
150 else if ((insn
& 0xff000000) == 0x27000000 /* stptr.d rd,sp,si14 */
153 prologue_end
= cur_pc
+ insn_len
;
155 else if ((insn
& 0xfe000000) == 0x14000000) /* lu12i.w rd,si20 */
157 reg_value
[rd
] = si20
<< 12;
160 else if ((insn
& 0xffc00000) == 0x03800000) /* ori rd,rj,si12 */
164 reg_value
[rd
] = reg_value
[rj
] | (si12
& 0xfff);
168 else if ((insn
& 0xffff8000) == 0x00108000 /* add.d sp,sp,rk */
169 && rd
== sp
&& rj
== sp
)
171 if (reg_used
[rk
] == 1 && reg_value
[rk
] < 0)
173 prologue_end
= cur_pc
+ insn_len
;
177 else if (loongarch_insn_is_branch (insn
))
185 if (prologue_end
== 0)
186 prologue_end
= cur_pc
;
191 /* Implement the loongarch_skip_prologue gdbarch method. */
194 loongarch_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
198 /* See if we can determine the end of the prologue via the symbol table.
199 If so, then return either PC, or the PC after the prologue, whichever
201 if (find_pc_partial_function (pc
, nullptr, &func_addr
, nullptr))
203 CORE_ADDR post_prologue_pc
204 = skip_prologue_using_sal (gdbarch
, func_addr
);
205 if (post_prologue_pc
!= 0)
206 return std::max (pc
, post_prologue_pc
);
209 /* Can't determine prologue from the symbol table, need to examine
212 /* Find an upper limit on the function prologue using the debug
213 information. If the debug information could not be used to provide
214 that bound, then use an arbitrary large number as the upper bound. */
215 CORE_ADDR limit_pc
= skip_prologue_using_sal (gdbarch
, pc
);
217 limit_pc
= pc
+ 100; /* Arbitrary large number. */
219 return loongarch_scan_prologue (gdbarch
, pc
, limit_pc
, nullptr, nullptr);
222 /* Decode the current instruction and determine the address of the
226 loongarch_next_pc (struct regcache
*regcache
, CORE_ADDR cur_pc
)
228 struct gdbarch
*gdbarch
= regcache
->arch ();
229 loongarch_gdbarch_tdep
*tdep
= gdbarch_tdep
<loongarch_gdbarch_tdep
> (gdbarch
);
230 insn_t insn
= loongarch_fetch_instruction (cur_pc
);
231 size_t insn_len
= loongarch_insn_length (insn
);
232 CORE_ADDR next_pc
= cur_pc
+ insn_len
;
234 if ((insn
& 0xfc000000) == 0x4c000000) /* jirl rd, rj, offs16 */
236 LONGEST rj
= regcache_raw_get_signed (regcache
,
237 loongarch_decode_imm ("5:5", insn
, 0));
238 next_pc
= rj
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
240 else if ((insn
& 0xfc000000) == 0x50000000 /* b offs26 */
241 || (insn
& 0xfc000000) == 0x54000000) /* bl offs26 */
243 next_pc
= cur_pc
+ loongarch_decode_imm ("0:10|10:16<<2", insn
, 1);
245 else if ((insn
& 0xfc000000) == 0x58000000) /* beq rj, rd, offs16 */
247 LONGEST rj
= regcache_raw_get_signed (regcache
,
248 loongarch_decode_imm ("5:5", insn
, 0));
249 LONGEST rd
= regcache_raw_get_signed (regcache
,
250 loongarch_decode_imm ("0:5", insn
, 0));
252 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
254 else if ((insn
& 0xfc000000) == 0x5c000000) /* bne rj, rd, offs16 */
256 LONGEST rj
= regcache_raw_get_signed (regcache
,
257 loongarch_decode_imm ("5:5", insn
, 0));
258 LONGEST rd
= regcache_raw_get_signed (regcache
,
259 loongarch_decode_imm ("0:5", insn
, 0));
261 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
263 else if ((insn
& 0xfc000000) == 0x60000000) /* blt rj, rd, offs16 */
265 LONGEST rj
= regcache_raw_get_signed (regcache
,
266 loongarch_decode_imm ("5:5", insn
, 0));
267 LONGEST rd
= regcache_raw_get_signed (regcache
,
268 loongarch_decode_imm ("0:5", insn
, 0));
270 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
272 else if ((insn
& 0xfc000000) == 0x64000000) /* bge rj, rd, offs16 */
274 LONGEST rj
= regcache_raw_get_signed (regcache
,
275 loongarch_decode_imm ("5:5", insn
, 0));
276 LONGEST rd
= regcache_raw_get_signed (regcache
,
277 loongarch_decode_imm ("0:5", insn
, 0));
279 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
281 else if ((insn
& 0xfc000000) == 0x68000000) /* bltu rj, rd, offs16 */
283 ULONGEST rj
= regcache_raw_get_unsigned (regcache
,
284 loongarch_decode_imm ("5:5", insn
, 0));
285 ULONGEST rd
= regcache_raw_get_unsigned (regcache
,
286 loongarch_decode_imm ("0:5", insn
, 0));
288 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
290 else if ((insn
& 0xfc000000) == 0x6c000000) /* bgeu rj, rd, offs16 */
292 ULONGEST rj
= regcache_raw_get_unsigned (regcache
,
293 loongarch_decode_imm ("5:5", insn
, 0));
294 ULONGEST rd
= regcache_raw_get_unsigned (regcache
,
295 loongarch_decode_imm ("0:5", insn
, 0));
297 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
299 else if ((insn
& 0xfc000000) == 0x40000000) /* beqz rj, offs21 */
301 LONGEST rj
= regcache_raw_get_signed (regcache
,
302 loongarch_decode_imm ("5:5", insn
, 0));
304 next_pc
= cur_pc
+ loongarch_decode_imm ("0:5|10:16<<2", insn
, 1);
306 else if ((insn
& 0xfc000000) == 0x44000000) /* bnez rj, offs21 */
308 LONGEST rj
= regcache_raw_get_signed (regcache
,
309 loongarch_decode_imm ("5:5", insn
, 0));
311 next_pc
= cur_pc
+ loongarch_decode_imm ("0:5|10:16<<2", insn
, 1);
313 else if ((insn
& 0xffff8000) == 0x002b0000) /* syscall */
315 if (tdep
->syscall_next_pc
!= nullptr)
316 next_pc
= tdep
->syscall_next_pc (get_current_frame ());
322 /* We can't put a breakpoint in the middle of a ll/sc atomic sequence,
323 so look for the end of the sequence and put the breakpoint there. */
325 static std::vector
<CORE_ADDR
>
326 loongarch_deal_with_atomic_sequence (struct regcache
*regcache
, CORE_ADDR cur_pc
)
329 std::vector
<CORE_ADDR
> next_pcs
;
330 insn_t insn
= loongarch_fetch_instruction (cur_pc
);
331 size_t insn_len
= loongarch_insn_length (insn
);
332 const int atomic_sequence_length
= 16;
333 bool found_atomic_sequence_endpoint
= false;
335 /* Look for a Load Linked instruction which begins the atomic sequence. */
336 if (!loongarch_insn_is_ll (insn
))
339 /* Assume that no atomic sequence is longer than "atomic_sequence_length" instructions. */
340 for (int insn_count
= 0; insn_count
< atomic_sequence_length
; ++insn_count
)
343 insn
= loongarch_fetch_instruction (cur_pc
);
345 /* Look for a unconditional branch instruction, fallback to the standard code. */
346 if (loongarch_insn_is_uncond_branch (insn
))
350 /* Look for a conditional branch instruction, put a breakpoint in its destination address. */
351 else if (loongarch_insn_is_cond_branch (insn
))
353 next_pc
= loongarch_next_pc (regcache
, cur_pc
);
354 next_pcs
.push_back (next_pc
);
356 /* Look for a Store Conditional instruction which closes the atomic sequence. */
357 else if (loongarch_insn_is_sc (insn
))
359 found_atomic_sequence_endpoint
= true;
360 next_pc
= cur_pc
+ insn_len
;
361 next_pcs
.push_back (next_pc
);
366 /* We didn't find a closing Store Conditional instruction, fallback to the standard code. */
367 if (!found_atomic_sequence_endpoint
)
373 /* Implement the software_single_step gdbarch method */
375 static std::vector
<CORE_ADDR
>
376 loongarch_software_single_step (struct regcache
*regcache
)
378 CORE_ADDR cur_pc
= regcache_read_pc (regcache
);
379 std::vector
<CORE_ADDR
> next_pcs
380 = loongarch_deal_with_atomic_sequence (regcache
, cur_pc
);
382 if (!next_pcs
.empty ())
385 CORE_ADDR next_pc
= loongarch_next_pc (regcache
, cur_pc
);
390 /* Callback function for user_reg_add. */
392 static struct value
*
393 value_of_loongarch_user_reg (const frame_info_ptr
&frame
, const void *baton
)
395 return value_of_register ((long long) baton
,
396 get_next_frame_sentinel_okay (frame
));
399 /* Implement the frame_align gdbarch method. */
402 loongarch_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
404 return align_down (addr
, 16);
407 /* Generate, or return the cached frame cache for frame unwinder. */
409 static struct trad_frame_cache
*
410 loongarch_frame_cache (const frame_info_ptr
&this_frame
, void **this_cache
)
412 struct trad_frame_cache
*cache
;
415 if (*this_cache
!= nullptr)
416 return (struct trad_frame_cache
*) *this_cache
;
418 cache
= trad_frame_cache_zalloc (this_frame
);
421 trad_frame_set_reg_realreg (cache
, LOONGARCH_PC_REGNUM
, LOONGARCH_RA_REGNUM
);
423 pc
= get_frame_address_in_block (this_frame
);
424 trad_frame_set_id (cache
, frame_id_build_unavailable_stack (pc
));
429 /* Implement the this_id callback for frame unwinder. */
432 loongarch_frame_this_id (const frame_info_ptr
&this_frame
, void **prologue_cache
,
433 struct frame_id
*this_id
)
435 struct trad_frame_cache
*info
;
437 info
= loongarch_frame_cache (this_frame
, prologue_cache
);
438 trad_frame_get_id (info
, this_id
);
441 /* Implement the prev_register callback for frame unwinder. */
443 static struct value
*
444 loongarch_frame_prev_register (const frame_info_ptr
&this_frame
,
445 void **prologue_cache
, int regnum
)
447 struct trad_frame_cache
*info
;
449 info
= loongarch_frame_cache (this_frame
, prologue_cache
);
450 return trad_frame_get_register (info
, this_frame
, regnum
);
453 static const struct frame_unwind loongarch_frame_unwind
= {
454 "loongarch prologue",
455 /*.type =*/NORMAL_FRAME
,
456 /*.stop_reason =*/default_frame_unwind_stop_reason
,
457 /*.this_id =*/loongarch_frame_this_id
,
458 /*.prev_register =*/loongarch_frame_prev_register
,
459 /*.unwind_data =*/nullptr,
460 /*.sniffer =*/default_frame_sniffer
,
461 /*.dealloc_cache =*/nullptr,
462 /*.prev_arch =*/nullptr,
465 /* Write the contents of buffer VAL into the general-purpose argument
466 register defined by GAR in REGCACHE. GAR indicates the available
467 general-purpose argument registers which should be a value in the
468 range 1 to 8 (LOONGARCH_ARG_REGNUM), which correspond to registers
469 a7 and a0 respectively, that is to say, regnum is a7 if GAR is 1,
470 regnum is a6 if GAR is 2, regnum is a5 if GAR is 3, regnum is a4
471 if GAR is 4, regnum is a3 if GAR is 5, regnum is a2 if GAR is 6,
472 regnum is a1 if GAR is 7, regnum is a0 if GAR is 8. */
475 pass_in_gar (struct regcache
*regcache
, unsigned int gar
, const gdb_byte
*val
)
477 unsigned int regnum
= LOONGARCH_ARG_REGNUM
- gar
+ LOONGARCH_A0_REGNUM
;
478 regcache
->cooked_write (regnum
, val
);
481 /* Write the contents of buffer VAL into the floating-point argument
482 register defined by FAR in REGCACHE. FAR indicates the available
483 floating-point argument registers which should be a value in the
484 range 1 to 8 (LOONGARCH_ARG_REGNUM), which correspond to registers
485 f7 and f0 respectively, that is to say, regnum is f7 if FAR is 1,
486 regnum is f6 if FAR is 2, regnum is f5 if FAR is 3, regnum is f4
487 if FAR is 4, regnum is f3 if FAR is 5, regnum is f2 if FAR is 6,
488 regnum is f1 if FAR is 7, regnum is f0 if FAR is 8. */
491 pass_in_far (struct regcache
*regcache
, unsigned int far
, const gdb_byte
*val
)
493 unsigned int regnum
= LOONGARCH_ARG_REGNUM
- far
+ LOONGARCH_FIRST_FP_REGNUM
;
494 regcache
->cooked_write (regnum
, val
);
497 /* Pass a value on the stack. */
500 pass_on_stack (struct regcache
*regcache
, const gdb_byte
*val
,
501 size_t len
, int align
, gdb_byte
**addr
)
503 align
= align_up (align
, 8);
507 CORE_ADDR align_addr
= (CORE_ADDR
) (*addr
);
508 align_addr
= align_up (align_addr
, align
);
509 *addr
= (gdb_byte
*) align_addr
;
510 memcpy (*addr
, val
, len
);
514 /* Compute the numbers of struct member. */
517 compute_struct_member (struct type
*type
,
518 unsigned int *fixed_point_members
,
519 unsigned int *floating_point_members
,
520 bool *first_member_is_fixed_point
,
521 bool *has_long_double
)
523 for (int i
= 0; i
< type
->num_fields (); i
++)
525 /* Ignore any static fields. */
526 if (type
->field (i
).is_static ())
529 struct type
*field_type
= check_typedef (type
->field (i
).type ());
531 if ((field_type
->code () == TYPE_CODE_FLT
532 && field_type
->length () == 16)
533 || (field_type
->code () == TYPE_CODE_COMPLEX
534 && field_type
->length () == 32))
535 *has_long_double
= true;
537 if (field_type
->code () == TYPE_CODE_INT
538 || field_type
->code () == TYPE_CODE_BOOL
539 || field_type
->code () == TYPE_CODE_CHAR
540 || field_type
->code () == TYPE_CODE_RANGE
541 || field_type
->code () == TYPE_CODE_ENUM
542 || field_type
->code () == TYPE_CODE_PTR
)
544 (*fixed_point_members
)++;
546 if (*floating_point_members
== 0)
547 *first_member_is_fixed_point
= true;
549 else if (field_type
->code () == TYPE_CODE_FLT
)
550 (*floating_point_members
)++;
551 else if (field_type
->code () == TYPE_CODE_STRUCT
)
552 compute_struct_member (field_type
,
554 floating_point_members
,
555 first_member_is_fixed_point
,
557 else if (field_type
->code () == TYPE_CODE_COMPLEX
)
558 (*floating_point_members
) += 2;
562 /* Compute the lengths and offsets of struct member. */
565 struct_member_info (struct type
*type
,
566 unsigned int *member_offsets
,
567 unsigned int *member_lens
,
569 unsigned int *fields
)
571 unsigned int count
= type
->num_fields ();
574 for (i
= 0; i
< count
; ++i
)
576 if (type
->field (i
).loc_kind () != FIELD_LOC_KIND_BITPOS
)
579 struct type
*field_type
= check_typedef (type
->field (i
).type ());
581 = offset
+ type
->field (i
).loc_bitpos () / TARGET_CHAR_BIT
;
583 switch (field_type
->code ())
585 case TYPE_CODE_STRUCT
:
586 struct_member_info (field_type
, member_offsets
, member_lens
,
587 field_offset
, fields
);
590 case TYPE_CODE_COMPLEX
:
594 if (field_type
->length () == 8)
596 member_offsets
[0] = field_offset
;
597 member_offsets
[1] = field_offset
+ 4;
598 member_lens
[0] = member_lens
[1] = 4;
601 /* _Complex double */
602 else if (field_type
->length () == 16)
604 member_offsets
[0] = field_offset
;
605 member_offsets
[1] = field_offset
+ 8;
606 member_lens
[0] = member_lens
[1] = 8;
615 member_offsets
[*fields
] = field_offset
;
616 member_lens
[*fields
] = field_type
->length ();
622 /* only has special handling for structures with 1 or 2 fields. */
628 /* Implement the push_dummy_call gdbarch method. */
631 loongarch_push_dummy_call (struct gdbarch
*gdbarch
,
632 struct value
*function
,
633 struct regcache
*regcache
,
638 function_call_return_method return_method
,
639 CORE_ADDR struct_addr
)
641 int regsize
= register_size (gdbarch
, 0);
642 unsigned int gar
= LOONGARCH_ARG_REGNUM
;
643 unsigned int far
= LOONGARCH_ARG_REGNUM
;
644 unsigned int fixed_point_members
;
645 unsigned int floating_point_members
;
646 bool first_member_is_fixed_point
;
647 bool has_long_double
;
648 unsigned int member_offsets
[2];
649 unsigned int member_lens
[2];
651 gdb_byte buf
[1024] = { 0 };
652 gdb_byte
*addr
= buf
;
654 if (return_method
!= return_method_normal
)
655 pass_in_gar (regcache
, gar
--, (gdb_byte
*) &struct_addr
);
657 for (int i
= 0; i
< nargs
; i
++)
659 struct value
*arg
= args
[i
];
660 const gdb_byte
*val
= arg
->contents ().data ();
661 struct type
*type
= check_typedef (arg
->type ());
662 size_t len
= type
->length ();
663 int align
= type_align (type
);
664 enum type_code code
= type
->code ();
665 struct type
*func_type
= check_typedef (function
->type ());
666 bool varargs
= (func_type
->has_varargs () && i
>= func_type
->num_fields ());
673 case TYPE_CODE_RANGE
:
677 /* integer or pointer type is passed in GAR.
678 If no GAR is available, it's passed on the stack.
679 When passed in registers or on the stack,
680 the unsigned integer scalars are zero-extended to GRLEN bits,
681 and the signed integer scalars are sign-extended. */
682 if (type
->is_unsigned ())
684 ULONGEST data
= extract_unsigned_integer (val
, len
, BFD_ENDIAN_LITTLE
);
686 pass_in_gar (regcache
, gar
--, (gdb_byte
*) &data
);
688 pass_on_stack (regcache
, (gdb_byte
*) &data
, len
, align
, &addr
);
692 LONGEST data
= extract_signed_integer (val
, len
, BFD_ENDIAN_LITTLE
);
694 pass_in_gar (regcache
, gar
--, (gdb_byte
*) &data
);
696 pass_on_stack (regcache
, (gdb_byte
*) &data
, len
, align
, &addr
);
701 if (len
== 2 * regsize
)
705 /* long double type is passed in a pair of GAR,
706 with the low-order GRLEN bits in the lower-numbered register
707 and the high-order GRLEN bits in the higher-numbered register.
708 If exactly one register is available,
709 the low-order GRLEN bits are passed in the register
710 and the high-order GRLEN bits are passed on the stack.
711 If no GAR is available, it's passed on the stack. */
714 pass_in_gar (regcache
, gar
--, val
);
715 pass_in_gar (regcache
, gar
--, val
+ regsize
);
719 pass_in_gar (regcache
, gar
--, val
);
720 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
724 pass_on_stack (regcache
, val
, len
, align
, &addr
);
729 /* Variadic arguments are passed in GARs
730 in the same manner as named arguments.
731 And after a variadic argument has been passed on the stack,
732 all future arguments will also be passed on the stack,
733 i.e., the last argument register may be left unused
734 due to the aligned register pair rule.
735 long double data type is passed in an aligned GAR pair,
736 the first register in the pair is even-numbered. */
741 pass_in_gar (regcache
, gar
--, val
);
742 pass_in_gar (regcache
, gar
--, val
+ regsize
);
747 pass_in_gar (regcache
, gar
--, val
);
748 pass_in_gar (regcache
, gar
--, val
+ regsize
);
754 pass_on_stack (regcache
, val
, len
, align
, &addr
);
758 pass_on_stack (regcache
, val
, len
, align
, &addr
);
764 /* The other floating-point type is passed in FAR.
765 If no FAR is available, it's passed in GAR.
766 If no GAR is available, it's passed on the stack. */
767 if (!varargs
&& far
> 0)
768 pass_in_far (regcache
, far
--, val
);
770 pass_in_gar (regcache
, gar
--, val
);
772 pass_on_stack (regcache
, val
, len
, align
, &addr
);
775 case TYPE_CODE_STRUCT
:
777 fixed_point_members
= 0;
778 floating_point_members
= 0;
779 first_member_is_fixed_point
= false;
780 has_long_double
= false;
781 member_offsets
[0] = member_offsets
[1] = 0;
782 member_lens
[0] = member_offsets
[1] = 0;
784 compute_struct_member (type
,
785 &fixed_point_members
,
786 &floating_point_members
,
787 &first_member_is_fixed_point
,
789 struct_member_info (type
, member_offsets
, member_lens
, 0, &fields
);
790 /* If the structure consists of one floating-point member within
791 FRLEN bits wide, it is passed in an FAR if available. If the
792 structure consists of two floating-point members both within
793 FRLEN bits wide, it is passed in two FARs if available. If the
794 structure consists of one integer member within GRLEN bits wide
795 and one floating-point member within FRLEN bits wide, it is
796 passed in a GAR and an FAR if available. */
797 if (has_long_double
== false
798 && ((fixed_point_members
== 0 && floating_point_members
== 1
800 || (fixed_point_members
== 0 && floating_point_members
== 2
802 || (fixed_point_members
== 1 && floating_point_members
== 1
803 && far
>= 1 && gar
>= 1)))
805 if (fixed_point_members
== 0 && floating_point_members
== 1)
807 pass_in_far (regcache
, far
--, val
+ member_offsets
[0]);
809 else if (fixed_point_members
== 0 && floating_point_members
== 2)
811 pass_in_far (regcache
, far
--, val
+ member_offsets
[0]);
812 pass_in_far (regcache
, far
--, val
+ member_offsets
[1]);
814 else if (fixed_point_members
== 1 && floating_point_members
== 1)
816 if (first_member_is_fixed_point
== false)
818 pass_in_far (regcache
, far
--, val
+ member_offsets
[0]);
819 pass_in_gar (regcache
, gar
--, val
+ member_offsets
[1]);
823 pass_in_gar (regcache
, gar
--, val
+ member_offsets
[0]);
824 pass_in_far (regcache
, far
--, val
+ member_offsets
[1]);
828 else if (len
> 0 && len
<= regsize
)
830 /* The structure has only fixed-point members. */
831 if (fixed_point_members
> 0 && floating_point_members
== 0)
833 /* If there is an available GAR,
834 the structure is passed through the GAR by value passing;
835 If no GAR is available, it's passed on the stack. */
837 pass_in_gar (regcache
, gar
--, val
);
839 pass_on_stack (regcache
, val
, len
, align
, &addr
);
841 /* The structure has only floating-point members. */
842 else if (fixed_point_members
== 0 && floating_point_members
> 0)
844 /* The structure has one floating-point member.
845 The argument is passed in a FAR.
846 If no FAR is available, the value is passed in a GAR.
847 if no GAR is available, the value is passed on the stack. */
848 if (floating_point_members
== 1)
850 if (!varargs
&& far
> 0)
851 pass_in_far (regcache
, far
--, val
);
853 pass_in_gar (regcache
, gar
--, val
);
855 pass_on_stack (regcache
, val
, len
, align
, &addr
);
857 /* The structure has two floating-point members.
858 The argument is passed in a pair of available FAR,
859 with the low-order float member bits in the lower-numbered FAR
860 and the high-order float member bits in the higher-numbered FAR.
861 If the number of available FAR is less than 2, it's passed in a GAR,
862 and passed on the stack if no GAR is available. */
863 else if (floating_point_members
== 2)
865 if (!varargs
&& far
>= 2)
867 pass_in_far (regcache
, far
--, val
);
868 pass_in_far (regcache
, far
--, val
+ align
);
872 pass_in_gar (regcache
, gar
--, val
);
876 pass_on_stack (regcache
, val
, len
, align
, &addr
);
880 /* The structure has both fixed-point and floating-point members. */
881 else if (fixed_point_members
> 0 && floating_point_members
> 0)
883 /* The structure has one float member and multiple fixed-point members.
884 If there are available GAR, the structure is passed in a GAR,
885 and passed on the stack if no GAR is available. */
886 if (floating_point_members
== 1 && fixed_point_members
> 1)
889 pass_in_gar (regcache
, gar
--, val
);
891 pass_on_stack (regcache
, val
, len
, align
, &addr
);
893 /* The structure has one float member and one fixed-point member.
894 If one FAR and one GAR are available,
895 the floating-point member of the structure is passed in the FAR,
896 and the fixed-point member of the structure is passed in the GAR.
897 If no floating-point register but one GAR is available, it's passed in GAR;
898 If no GAR is available, it's passed on the stack. */
899 else if (floating_point_members
== 1 && fixed_point_members
== 1)
901 if (!varargs
&& far
> 0 && gar
> 0)
903 if (first_member_is_fixed_point
== false)
905 pass_in_far (regcache
, far
--, val
);
906 pass_in_gar (regcache
, gar
--, val
+ align
);
910 pass_in_gar (regcache
, gar
--, val
);
911 pass_in_far (regcache
, far
--, val
+ align
);
917 pass_in_gar (regcache
, gar
--, val
);
919 pass_on_stack (regcache
, val
, len
, align
, &addr
);
924 else if (len
> regsize
&& len
<= 2 * regsize
)
926 /* The structure has only fixed-point members. */
927 if (fixed_point_members
> 0 && floating_point_members
== 0)
929 /* The argument is passed in a pair of available GAR,
930 with the low-order bits in the lower-numbered GAR
931 and the high-order bits in the higher-numbered GAR.
932 If only one GAR is available,
933 the low-order bits are in the GAR
934 and the high-order bits are on the stack,
935 and passed on the stack if no GAR is available. */
938 pass_in_gar (regcache
, gar
--, val
);
939 pass_in_gar (regcache
, gar
--, val
+ regsize
);
943 pass_in_gar (regcache
, gar
--, val
);
944 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
948 pass_on_stack (regcache
, val
, len
, align
, &addr
);
951 /* The structure has only floating-point members. */
952 else if (fixed_point_members
== 0 && floating_point_members
> 0)
954 /* The structure has one long double member
955 or one double member and two adjacent float members
956 or 3-4 float members.
957 The argument is passed in a pair of available GAR,
958 with the low-order bits in the lower-numbered GAR
959 and the high-order bits in the higher-numbered GAR.
960 If only one GAR is available,
961 the low-order bits are in the GAR
962 and the high-order bits are on the stack,
963 and passed on the stack if no GAR is available. */
964 if ((len
== 16 && floating_point_members
== 1)
965 || (len
== 16 && floating_point_members
== 3)
966 || (len
== 12 && floating_point_members
== 3)
967 || (len
== 16 && floating_point_members
== 4))
971 pass_in_gar (regcache
, gar
--, val
);
972 pass_in_gar (regcache
, gar
--, val
+ regsize
);
978 pass_in_gar (regcache
, gar
--, val
);
979 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
984 pass_on_stack (regcache
, val
, len
, align
, &addr
);
989 pass_on_stack (regcache
, val
, len
, align
, &addr
);
992 /* The structure has two double members
993 or one double member and one float member.
994 The argument is passed in a pair of available FAR,
995 with the low-order bits in the lower-numbered FAR
996 and the high-order bits in the higher-numbered FAR.
997 If no a pair of available FAR,
998 it's passed in a pair of available GAR,
999 with the low-order bits in the lower-numbered GAR
1000 and the high-order bits in the higher-numbered GAR.
1001 If only one GAR is available,
1002 the low-order bits are in the GAR
1003 and the high-order bits are on stack,
1004 and passed on the stack if no GAR is available. */
1005 else if ((len
== 16 && floating_point_members
== 2)
1006 || (len
== 12 && floating_point_members
== 2))
1008 if (!varargs
&& far
>= 2)
1010 pass_in_far (regcache
, far
--, val
);
1011 pass_in_far (regcache
, far
--, val
+ regsize
);
1015 pass_in_gar (regcache
, gar
--, val
);
1016 pass_in_gar (regcache
, gar
--, val
+ regsize
);
1020 pass_in_gar (regcache
, gar
--, val
);
1021 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
1025 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1029 /* The structure has both fixed-point and floating-point members. */
1030 else if (fixed_point_members
> 0 && floating_point_members
> 0)
1032 /* The structure has one floating-point member and one fixed-point member. */
1033 if (floating_point_members
== 1 && fixed_point_members
== 1)
1035 /* If one FAR and one GAR are available,
1036 the floating-point member of the structure is passed in the FAR,
1037 and the fixed-point member of the structure is passed in the GAR;
1038 If no floating-point registers but two GARs are available,
1039 it's passed in the two GARs;
1040 If only one GAR is available,
1041 the low-order bits are in the GAR
1042 and the high-order bits are on the stack;
1043 And it's passed on the stack if no GAR is available. */
1044 if (!varargs
&& far
> 0 && gar
> 0)
1046 if (first_member_is_fixed_point
== false)
1048 pass_in_far (regcache
, far
--, val
);
1049 pass_in_gar (regcache
, gar
--, val
+ regsize
);
1053 pass_in_gar (regcache
, gar
--, val
);
1054 pass_in_far (regcache
, far
--, val
+ regsize
);
1057 else if ((!varargs
&& far
== 0 && gar
>= 2) || (varargs
&& gar
>= 2))
1059 pass_in_gar (regcache
, gar
--, val
);
1060 pass_in_gar (regcache
, gar
--, val
+ regsize
);
1062 else if ((!varargs
&& far
== 0 && gar
== 1) || (varargs
&& gar
== 1))
1064 pass_in_gar (regcache
, gar
--, val
);
1065 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
1067 else if ((!varargs
&& far
== 0 && gar
== 0) || (varargs
&& gar
== 0))
1069 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1074 /* The argument is passed in a pair of available GAR,
1075 with the low-order bits in the lower-numbered GAR
1076 and the high-order bits in the higher-numbered GAR.
1077 If only one GAR is available,
1078 the low-order bits are in the GAR
1079 and the high-order bits are on the stack,
1080 and passed on the stack if no GAR is available. */
1083 pass_in_gar (regcache
, gar
--, val
);
1084 pass_in_gar (regcache
, gar
--, val
+ regsize
);
1088 pass_in_gar (regcache
, gar
--, val
);
1089 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
1093 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1098 else if (len
> 2 * regsize
)
1100 /* It's passed by reference and are replaced in the argument list with the address.
1101 If there is an available GAR, the reference is passed in the GAR,
1102 and passed on the stack if no GAR is available. */
1103 sp
= align_down (sp
- len
, 16);
1104 write_memory (sp
, val
, len
);
1107 pass_in_gar (regcache
, gar
--, (const gdb_byte
*) &sp
);
1109 pass_on_stack (regcache
, (const gdb_byte
*) &sp
, len
, regsize
, &addr
);
1113 case TYPE_CODE_UNION
:
1114 /* Union is passed in GAR or stack. */
1115 if (len
> 0 && len
<= regsize
)
1117 /* The argument is passed in a GAR,
1118 or on the stack by value if no GAR is available. */
1120 pass_in_gar (regcache
, gar
--, val
);
1122 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1124 else if (len
> regsize
&& len
<= 2 * regsize
)
1126 /* The argument is passed in a pair of available GAR,
1127 with the low-order bits in the lower-numbered GAR
1128 and the high-order bits in the higher-numbered GAR.
1129 If only one GAR is available,
1130 the low-order bits are in the GAR
1131 and the high-order bits are on the stack.
1132 The arguments are passed on the stack when no GAR is available. */
1135 pass_in_gar (regcache
, gar
--, val
);
1136 pass_in_gar (regcache
, gar
--, val
+ regsize
);
1140 pass_in_gar (regcache
, gar
--, val
);
1141 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
1145 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1148 else if (len
> 2 * regsize
)
1150 /* It's passed by reference and are replaced in the argument list with the address.
1151 If there is an available GAR, the reference is passed in the GAR,
1152 and passed on the stack if no GAR is available. */
1153 sp
= align_down (sp
- len
, 16);
1154 write_memory (sp
, val
, len
);
1157 pass_in_gar (regcache
, gar
--, (const gdb_byte
*) &sp
);
1159 pass_on_stack (regcache
, (const gdb_byte
*) &sp
, len
, regsize
, &addr
);
1162 case TYPE_CODE_COMPLEX
:
1164 struct type
*target_type
= check_typedef (type
->target_type ());
1165 size_t target_len
= target_type
->length ();
1167 if (target_len
< regsize
)
1169 /* The complex with two float members
1170 is passed in a pair of available FAR,
1171 with the low-order float member bits in the lower-numbered FAR
1172 and the high-order float member bits in the higher-numbered FAR.
1173 If the number of available FAR is less than 2, it's passed in a GAR,
1174 and passed on the stack if no GAR is available. */
1175 if (!varargs
&& far
>= 2)
1177 pass_in_far (regcache
, far
--, val
);
1178 pass_in_far (regcache
, far
--, val
+ align
);
1182 pass_in_gar (regcache
, gar
--, val
);
1186 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1189 else if (target_len
== regsize
)
1191 /* The complex with two double members
1192 is passed in a pair of available FAR,
1193 with the low-order bits in the lower-numbered FAR
1194 and the high-order bits in the higher-numbered FAR.
1195 If no a pair of available FAR,
1196 it's passed in a pair of available GAR,
1197 with the low-order bits in the lower-numbered GAR
1198 and the high-order bits in the higher-numbered GAR.
1199 If only one GAR is available,
1200 the low-order bits are in the GAR
1201 and the high-order bits are on stack,
1202 and passed on the stack if no GAR is available. */
1204 if (!varargs
&& far
>= 2)
1206 pass_in_far (regcache
, far
--, val
);
1207 pass_in_far (regcache
, far
--, val
+ align
);
1211 pass_in_gar (regcache
, gar
--, val
);
1212 pass_in_gar (regcache
, gar
--, val
+ align
);
1216 pass_in_gar (regcache
, gar
--, val
);
1217 pass_on_stack (regcache
, val
+ align
, len
- align
, align
, &addr
);
1221 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1225 else if (target_len
== 2 * regsize
)
1227 /* The complex with two long double members
1228 is passed by reference and are replaced in the argument list with the address.
1229 If there is an available GAR, the reference is passed in the GAR,
1230 and passed on the stack if no GAR is available. */
1231 sp
= align_down (sp
- len
, 16);
1232 write_memory (sp
, val
, len
);
1235 pass_in_gar (regcache
, gar
--, (const gdb_byte
*) &sp
);
1237 pass_on_stack (regcache
, (const gdb_byte
*) &sp
, regsize
, regsize
, &addr
);
1249 sp
= align_down (sp
, 16);
1250 write_memory (sp
, buf
, addr
- buf
);
1253 regcache_cooked_write_unsigned (regcache
, LOONGARCH_RA_REGNUM
, bp_addr
);
1254 regcache_cooked_write_unsigned (regcache
, LOONGARCH_SP_REGNUM
, sp
);
1259 /* Partial transfer of a cooked register. */
1262 loongarch_xfer_reg (struct regcache
*regcache
,
1263 int regnum
, int len
, gdb_byte
*readbuf
,
1264 const gdb_byte
*writebuf
, size_t offset
)
1267 regcache
->cooked_read_part (regnum
, 0, len
, readbuf
+ offset
);
1269 regcache
->cooked_write_part (regnum
, 0, len
, writebuf
+ offset
);
1272 /* Implement the return_value gdbarch method. */
1274 static enum return_value_convention
1275 loongarch_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
1276 struct type
*type
, struct regcache
*regcache
,
1277 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
1279 int regsize
= register_size (gdbarch
, 0);
1280 enum type_code code
= type
->code ();
1281 size_t len
= type
->length ();
1282 unsigned int fixed_point_members
;
1283 unsigned int floating_point_members
;
1284 bool first_member_is_fixed_point
;
1285 bool has_long_double
;
1286 unsigned int member_offsets
[2];
1287 unsigned int member_lens
[2];
1288 unsigned int fields
;
1289 int a0
= LOONGARCH_A0_REGNUM
;
1290 int a1
= LOONGARCH_A0_REGNUM
+ 1;
1291 int f0
= LOONGARCH_FIRST_FP_REGNUM
;
1292 int f1
= LOONGARCH_FIRST_FP_REGNUM
+ 1;
1297 case TYPE_CODE_BOOL
:
1298 case TYPE_CODE_CHAR
:
1299 case TYPE_CODE_RANGE
:
1300 case TYPE_CODE_ENUM
:
1303 /* integer or pointer type.
1304 The return value is passed in a0,
1305 the unsigned integer scalars are zero-extended to GRLEN bits,
1306 and the signed integer scalars are sign-extended. */
1309 gdb::byte_vector
buf (regsize
);
1310 if (type
->is_unsigned ())
1312 ULONGEST data
= extract_unsigned_integer (writebuf
, len
,
1314 store_unsigned_integer (buf
.data (), regsize
,
1315 BFD_ENDIAN_LITTLE
, data
);
1320 = extract_signed_integer (writebuf
, len
, BFD_ENDIAN_LITTLE
);
1321 store_signed_integer (buf
.data (), regsize
, BFD_ENDIAN_LITTLE
,
1325 loongarch_xfer_reg (regcache
, a0
, regsize
, nullptr, buf
.data (),
1329 loongarch_xfer_reg (regcache
, a0
, len
, readbuf
, nullptr, 0);
1333 /* long double type.
1334 The return value is passed in a0 and a1. */
1335 if (len
== 2 * regsize
)
1337 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1338 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1340 /* float or double type.
1341 The return value is passed in f0. */
1344 loongarch_xfer_reg (regcache
, f0
, len
, readbuf
, writebuf
, 0);
1347 case TYPE_CODE_STRUCT
:
1349 fixed_point_members
= 0;
1350 floating_point_members
= 0;
1351 first_member_is_fixed_point
= false;
1352 has_long_double
= false;
1353 member_offsets
[0] = member_offsets
[1] = 0;
1354 member_lens
[0] = member_offsets
[1] = 0;
1356 compute_struct_member (type
,
1357 &fixed_point_members
,
1358 &floating_point_members
,
1359 &first_member_is_fixed_point
,
1361 struct_member_info (type
, member_offsets
, member_lens
, 0, &fields
);
1362 /* struct consists of one floating-point member;
1363 struct consists of two floating-point members;
1364 struct consists of one floating-point member
1365 and one integer member. */
1366 if (has_long_double
== false
1367 && ((fixed_point_members
== 0 && floating_point_members
== 1)
1368 || (fixed_point_members
== 0 && floating_point_members
== 2)
1369 || (fixed_point_members
== 1 && floating_point_members
== 1)))
1371 if (fixed_point_members
== 0 && floating_point_members
== 1)
1373 loongarch_xfer_reg (regcache
, f0
, member_lens
[0], readbuf
,
1374 writebuf
, member_offsets
[0]);
1376 else if (fixed_point_members
== 0 && floating_point_members
== 2)
1378 loongarch_xfer_reg (regcache
, f0
, member_lens
[0], readbuf
,
1379 writebuf
, member_offsets
[0]);
1380 loongarch_xfer_reg (regcache
, f1
, member_lens
[1], readbuf
,
1381 writebuf
, member_offsets
[1]);
1383 else if (fixed_point_members
== 1 && floating_point_members
== 1)
1385 if (first_member_is_fixed_point
== false)
1387 loongarch_xfer_reg (regcache
, f0
, member_lens
[0], readbuf
,
1388 writebuf
, member_offsets
[0]);
1389 loongarch_xfer_reg (regcache
, a0
, member_lens
[1], readbuf
,
1390 writebuf
, member_offsets
[1]);
1394 loongarch_xfer_reg (regcache
, a0
, member_lens
[0], readbuf
,
1395 writebuf
, member_offsets
[0]);
1396 loongarch_xfer_reg (regcache
, f0
, member_lens
[1], readbuf
,
1397 writebuf
, member_offsets
[1]);
1401 else if (len
> 0 && len
<= regsize
)
1403 /* The structure has only fixed-point members. */
1404 if (fixed_point_members
> 0 && floating_point_members
== 0)
1406 /* The return value is passed in a0. */
1407 loongarch_xfer_reg (regcache
, a0
, len
, readbuf
, writebuf
, 0);
1409 /* The structure has only floating-point members. */
1410 else if (fixed_point_members
== 0 && floating_point_members
> 0)
1412 /* The structure has one floating-point member.
1413 The return value is passed in f0. */
1414 if (floating_point_members
== 1)
1416 loongarch_xfer_reg (regcache
, f0
, len
, readbuf
, writebuf
, 0);
1418 /* The structure has two floating-point members.
1419 The return value is passed in f0 and f1. */
1420 else if (floating_point_members
== 2)
1422 loongarch_xfer_reg (regcache
, f0
, len
/ 2, readbuf
, writebuf
, 0);
1423 loongarch_xfer_reg (regcache
, f1
, len
/ 2, readbuf
, writebuf
, len
/ 2);
1426 /* The structure has both fixed-point and floating-point members. */
1427 else if (fixed_point_members
> 0 && floating_point_members
> 0)
1429 /* The structure has one float member and multiple fixed-point members.
1430 The return value is passed in a0. */
1431 if (floating_point_members
== 1 && fixed_point_members
> 1)
1433 loongarch_xfer_reg (regcache
, a0
, len
, readbuf
, writebuf
, 0);
1435 /* The structure has one float member and one fixed-point member. */
1436 else if (floating_point_members
== 1 && fixed_point_members
== 1)
1438 /* The return value is passed in f0 and a0 if the first member is floating-point. */
1439 if (first_member_is_fixed_point
== false)
1441 loongarch_xfer_reg (regcache
, f0
, regsize
/ 2, readbuf
, writebuf
, 0);
1442 loongarch_xfer_reg (regcache
, a0
, regsize
/ 2, readbuf
, writebuf
, regsize
/ 2);
1444 /* The return value is passed in a0 and f0 if the first member is fixed-point. */
1447 loongarch_xfer_reg (regcache
, a0
, regsize
/ 2, readbuf
, writebuf
, 0);
1448 loongarch_xfer_reg (regcache
, f0
, regsize
/ 2, readbuf
, writebuf
, regsize
/ 2);
1453 else if (len
> regsize
&& len
<= 2 * regsize
)
1455 /* The structure has only fixed-point members. */
1456 if (fixed_point_members
> 0 && floating_point_members
== 0)
1458 /* The return value is passed in a0 and a1. */
1459 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1460 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1462 /* The structure has only floating-point members. */
1463 else if (fixed_point_members
== 0 && floating_point_members
> 0)
1465 /* The structure has one long double member
1466 or one double member and two adjacent float members
1467 or 3-4 float members.
1468 The return value is passed in a0 and a1. */
1469 if ((len
== 16 && floating_point_members
== 1)
1470 || (len
== 16 && floating_point_members
== 3)
1471 || (len
== 12 && floating_point_members
== 3)
1472 || (len
== 16 && floating_point_members
== 4))
1474 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1475 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1477 /* The structure has two double members
1478 or one double member and one float member.
1479 The return value is passed in f0 and f1. */
1480 else if ((len
== 16 && floating_point_members
== 2)
1481 || (len
== 12 && floating_point_members
== 2))
1483 loongarch_xfer_reg (regcache
, f0
, regsize
, readbuf
, writebuf
, 0);
1484 loongarch_xfer_reg (regcache
, f1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1487 /* The structure has both fixed-point and floating-point members. */
1488 else if (fixed_point_members
> 0 && floating_point_members
> 0)
1490 /* The structure has one floating-point member and one fixed-point member. */
1491 if (floating_point_members
== 1 && fixed_point_members
== 1)
1493 /* The return value is passed in f0 and a0 if the first member is floating-point. */
1494 if (first_member_is_fixed_point
== false)
1496 loongarch_xfer_reg (regcache
, f0
, regsize
, readbuf
, writebuf
, 0);
1497 loongarch_xfer_reg (regcache
, a0
, len
- regsize
, readbuf
, writebuf
, regsize
);
1499 /* The return value is passed in a0 and f0 if the first member is fixed-point. */
1502 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1503 loongarch_xfer_reg (regcache
, f0
, len
- regsize
, readbuf
, writebuf
, regsize
);
1508 /* The return value is passed in a0 and a1. */
1509 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1510 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1514 else if (len
> 2 * regsize
)
1515 return RETURN_VALUE_STRUCT_CONVENTION
;
1518 case TYPE_CODE_UNION
:
1519 if (len
> 0 && len
<= regsize
)
1521 /* The return value is passed in a0. */
1522 loongarch_xfer_reg (regcache
, a0
, len
, readbuf
, writebuf
, 0);
1524 else if (len
> regsize
&& len
<= 2 * regsize
)
1526 /* The return value is passed in a0 and a1. */
1527 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1528 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1530 else if (len
> 2 * regsize
)
1531 return RETURN_VALUE_STRUCT_CONVENTION
;
1533 case TYPE_CODE_COMPLEX
:
1534 if (len
> 0 && len
<= 2 * regsize
)
1536 /* The return value is passed in f0 and f1. */
1537 loongarch_xfer_reg (regcache
, f0
, len
/ 2, readbuf
, writebuf
, 0);
1538 loongarch_xfer_reg (regcache
, f1
, len
/ 2, readbuf
, writebuf
, len
/ 2);
1540 else if (len
> 2 * regsize
)
1541 return RETURN_VALUE_STRUCT_CONVENTION
;
1547 return RETURN_VALUE_REGISTER_CONVENTION
;
1550 /* Implement the dwarf2_reg_to_regnum gdbarch method. */
1553 loongarch_dwarf2_reg_to_regnum (struct gdbarch
*gdbarch
, int regnum
)
1555 if (regnum
>= 0 && regnum
< 32)
1557 else if (regnum
>= 32 && regnum
< 66)
1558 return LOONGARCH_FIRST_FP_REGNUM
+ regnum
- 32;
1563 static constexpr gdb_byte loongarch_default_breakpoint
[] = {0x05, 0x00, 0x2a, 0x00};
1564 typedef BP_MANIPULATION (loongarch_default_breakpoint
) loongarch_breakpoint
;
1566 /* Extract a set of required target features out of ABFD. If ABFD is nullptr
1567 then a LOONGARCH_GDBARCH_FEATURES is returned in its default state. */
1569 static struct loongarch_gdbarch_features
1570 loongarch_features_from_bfd (const bfd
*abfd
)
1572 struct loongarch_gdbarch_features features
;
1574 /* Now try to improve on the defaults by looking at the binary we are
1575 going to execute. We assume the user knows what they are doing and
1576 that the target will match the binary. Remember, this code path is
1577 only used at all if the target hasn't given us a description, so this
1578 is really a last ditched effort to do something sane before giving
1580 if (abfd
!= nullptr && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1582 unsigned char eclass
= elf_elfheader (abfd
)->e_ident
[EI_CLASS
];
1583 int e_flags
= elf_elfheader (abfd
)->e_flags
;
1585 if (eclass
== ELFCLASS32
)
1587 else if (eclass
== ELFCLASS64
)
1590 internal_error (_("unknown ELF header class %d"), eclass
);
1592 if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags
))
1593 features
.fputype
= SINGLE_FLOAT
;
1594 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags
))
1595 features
.fputype
= DOUBLE_FLOAT
;
1601 /* Find a suitable default target description. Use the contents of INFO,
1602 specifically the bfd object being executed, to guide the selection of a
1603 suitable default target description. */
1605 static const struct target_desc
*
1606 loongarch_find_default_target_description (const struct gdbarch_info info
)
1608 /* Extract desired feature set from INFO. */
1609 struct loongarch_gdbarch_features features
1610 = loongarch_features_from_bfd (info
.abfd
);
1612 /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
1613 maybe there was no bfd object. In this case we fall back to a minimal
1614 useful target, the x-register size is selected based on the architecture
1616 if (features
.xlen
== 0)
1617 features
.xlen
= info
.bfd_arch_info
->bits_per_address
== 32 ? 4 : 8;
1619 /* If the FPUTYPE field is still 0 then we got nothing useful from INFO.BFD,
1620 maybe there was no bfd object. In this case we fall back to a usual useful
1621 target with double float. */
1622 if (features
.fputype
== 0)
1623 features
.fputype
= DOUBLE_FLOAT
;
1625 /* Now build a target description based on the feature set. */
1626 return loongarch_lookup_target_description (features
);
1630 loongarch_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
1631 const struct reggroup
*group
)
1633 if (gdbarch_register_name (gdbarch
, regnum
) == NULL
1634 || *gdbarch_register_name (gdbarch
, regnum
) == '\0')
1637 int raw_p
= regnum
< gdbarch_num_regs (gdbarch
);
1639 if (group
== save_reggroup
|| group
== restore_reggroup
)
1642 if (group
== all_reggroup
)
1645 if (0 <= regnum
&& regnum
<= LOONGARCH_BADV_REGNUM
)
1646 return group
== general_reggroup
;
1648 /* Only ORIG_A0, PC, BADV in general_reggroup */
1649 if (group
== general_reggroup
)
1652 if (LOONGARCH_FIRST_FP_REGNUM
<= regnum
&& regnum
<= LOONGARCH_FCSR_REGNUM
)
1653 return group
== float_reggroup
;
1655 /* Only $fx / $fccx / $fcsr in float_reggroup */
1656 if (group
== float_reggroup
)
1659 if (LOONGARCH_FIRST_LSX_REGNUM
<= regnum
1660 && regnum
< LOONGARCH_FIRST_LASX_REGNUM
+ LOONGARCH_LINUX_NUM_LASXREGSET
)
1661 return group
== vector_reggroup
;
1663 /* Only $vrx / $xrx in vector_reggroup */
1664 if (group
== vector_reggroup
)
1667 int ret
= tdesc_register_in_reggroup_p (gdbarch
, regnum
, group
);
1671 return default_register_reggroup_p (gdbarch
, regnum
, group
);
1674 /* Initialize the current architecture based on INFO */
1676 static struct gdbarch
*
1677 loongarch_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1680 struct loongarch_gdbarch_features features
;
1681 tdesc_arch_data_up tdesc_data
= tdesc_data_alloc ();
1682 const struct target_desc
*tdesc
= info
.target_desc
;
1684 /* Ensure we always have a target description. */
1685 if (!tdesc_has_registers (tdesc
))
1686 tdesc
= loongarch_find_default_target_description (info
);
1688 const struct tdesc_feature
*feature_cpu
1689 = tdesc_find_feature (tdesc
, "org.gnu.gdb.loongarch.base");
1690 if (feature_cpu
== nullptr)
1694 /* Validate the description provides the mandatory base registers
1695 and allocate their numbers. */
1696 bool valid_p
= true;
1697 for (int i
= 0; i
< 32; i
++)
1698 valid_p
&= tdesc_numbered_register (feature_cpu
, tdesc_data
.get (), regnum
++,
1699 loongarch_r_normal_name
[i
] + 1);
1700 valid_p
&= tdesc_numbered_register (feature_cpu
, tdesc_data
.get (), regnum
++, "orig_a0");
1701 valid_p
&= tdesc_numbered_register (feature_cpu
, tdesc_data
.get (), regnum
++, "pc");
1702 valid_p
&= tdesc_numbered_register (feature_cpu
, tdesc_data
.get (), regnum
++, "badv");
1706 const struct tdesc_feature
*feature_fpu
1707 = tdesc_find_feature (tdesc
, "org.gnu.gdb.loongarch.fpu");
1708 if (feature_fpu
== nullptr)
1711 /* Validate the description provides the fpu registers and
1712 allocate their numbers. */
1713 regnum
= LOONGARCH_FIRST_FP_REGNUM
;
1714 for (int i
= 0; i
< LOONGARCH_LINUX_NUM_FPREGSET
; i
++)
1715 valid_p
&= tdesc_numbered_register (feature_fpu
, tdesc_data
.get (), regnum
++,
1716 loongarch_f_normal_name
[i
] + 1);
1717 for (int i
= 0; i
< LOONGARCH_LINUX_NUM_FCC
; i
++)
1718 valid_p
&= tdesc_numbered_register (feature_fpu
, tdesc_data
.get (), regnum
++,
1719 loongarch_c_normal_name
[i
] + 1);
1720 valid_p
&= tdesc_numbered_register (feature_fpu
, tdesc_data
.get (), regnum
++, "fcsr");
1724 const struct tdesc_feature
*feature_lsx
1725 = tdesc_find_feature (tdesc
, "org.gnu.gdb.loongarch.lsx");
1726 if (feature_lsx
== nullptr)
1729 /* Validate the description provides the lsx registers and
1730 allocate their numbers. */
1731 regnum
= LOONGARCH_FIRST_LSX_REGNUM
;
1732 for (int i
= 0; i
< LOONGARCH_LINUX_NUM_LSXREGSET
; i
++)
1733 valid_p
&= tdesc_numbered_register (feature_lsx
, tdesc_data
.get (), regnum
++,
1734 loongarch_v_normal_name
[i
] + 1);
1738 const struct tdesc_feature
*feature_lasx
1739 = tdesc_find_feature (tdesc
, "org.gnu.gdb.loongarch.lasx");
1740 if (feature_lasx
== nullptr)
1743 /* Validate the description provides the lasx registers and
1744 allocate their numbers. */
1745 regnum
= LOONGARCH_FIRST_LASX_REGNUM
;
1746 for (int i
= 0; i
< LOONGARCH_LINUX_NUM_LASXREGSET
; i
++)
1747 valid_p
&= tdesc_numbered_register (feature_lasx
, tdesc_data
.get (), regnum
++,
1748 loongarch_x_normal_name
[i
] + 1);
1752 const struct tdesc_feature
*feature_lbt
1753 = tdesc_find_feature (tdesc
, "org.gnu.gdb.loongarch.lbt");
1754 if (feature_lbt
== nullptr)
1757 /* Validate the description provides the lbt registers and
1758 allocate their numbers. */
1759 regnum
= LOONGARCH_FIRST_SCR_REGNUM
;
1760 for (int i
= 0; i
< LOONGARCH_LINUX_NUM_SCR
; i
++)
1761 valid_p
&= tdesc_numbered_register (feature_lbt
, tdesc_data
.get (), regnum
++,
1762 loongarch_cr_normal_name
[i
] + 1);
1763 valid_p
&= tdesc_numbered_register (feature_lbt
, tdesc_data
.get (), regnum
++,
1765 valid_p
&= tdesc_numbered_register (feature_lbt
, tdesc_data
.get (), regnum
++,
1770 /* LoongArch code is always little-endian. */
1771 info
.byte_order_for_code
= BFD_ENDIAN_LITTLE
;
1773 /* Have a look at what the supplied (if any) bfd object requires of the
1774 target, then check that this matches with what the target is
1776 struct loongarch_gdbarch_features abi_features
1777 = loongarch_features_from_bfd (info
.abfd
);
1779 /* If the ABI_FEATURES xlen or fputype is 0 then this indicates we got
1780 no useful abi features from the INFO object. In this case we just
1781 treat the hardware features as defining the abi. */
1782 if (abi_features
.xlen
== 0)
1784 int xlen_bitsize
= tdesc_register_bitsize (feature_cpu
, "pc");
1785 features
.xlen
= (xlen_bitsize
/ 8);
1786 features
.fputype
= abi_features
.fputype
;
1787 abi_features
= features
;
1789 if (abi_features
.fputype
== 0)
1791 features
.xlen
= abi_features
.xlen
;
1792 features
.fputype
= DOUBLE_FLOAT
;
1793 abi_features
= features
;
1796 /* Find a candidate among the list of pre-declared architectures. */
1797 for (arches
= gdbarch_list_lookup_by_info (arches
, &info
);
1799 arches
= gdbarch_list_lookup_by_info (arches
->next
, &info
))
1801 /* Check that the feature set of the ARCHES matches the feature set
1802 we are looking for. If it doesn't then we can't reuse this
1804 loongarch_gdbarch_tdep
*candidate_tdep
1805 = gdbarch_tdep
<loongarch_gdbarch_tdep
> (arches
->gdbarch
);
1807 if (candidate_tdep
->abi_features
!= abi_features
)
1813 if (arches
!= nullptr)
1814 return arches
->gdbarch
;
1816 /* None found, so create a new architecture from the information provided. */
1818 = gdbarch_alloc (&info
, gdbarch_tdep_up (new loongarch_gdbarch_tdep
));
1819 loongarch_gdbarch_tdep
*tdep
= gdbarch_tdep
<loongarch_gdbarch_tdep
> (gdbarch
);
1821 tdep
->abi_features
= abi_features
;
1823 /* Target data types. */
1824 set_gdbarch_short_bit (gdbarch
, 16);
1825 set_gdbarch_int_bit (gdbarch
, 32);
1826 set_gdbarch_long_bit (gdbarch
, info
.bfd_arch_info
->bits_per_address
);
1827 set_gdbarch_long_long_bit (gdbarch
, 64);
1828 set_gdbarch_float_bit (gdbarch
, 32);
1829 set_gdbarch_double_bit (gdbarch
, 64);
1830 set_gdbarch_long_double_bit (gdbarch
, 128);
1831 set_gdbarch_long_double_format (gdbarch
, floatformats_ieee_quad
);
1832 set_gdbarch_ptr_bit (gdbarch
, info
.bfd_arch_info
->bits_per_address
);
1833 set_gdbarch_char_signed (gdbarch
, 0);
1835 info
.target_desc
= tdesc
;
1836 info
.tdesc_data
= tdesc_data
.get ();
1838 for (int i
= 0; i
< ARRAY_SIZE (loongarch_r_alias
); ++i
)
1839 if (loongarch_r_alias
[i
][0] != '\0')
1840 user_reg_add (gdbarch
, loongarch_r_alias
[i
] + 1,
1841 value_of_loongarch_user_reg
, (void *) (size_t) i
);
1843 for (int i
= 0; i
< ARRAY_SIZE (loongarch_f_alias
); ++i
)
1845 if (loongarch_f_alias
[i
][0] != '\0')
1846 user_reg_add (gdbarch
, loongarch_f_alias
[i
] + 1,
1847 value_of_loongarch_user_reg
,
1848 (void *) (size_t) (LOONGARCH_FIRST_FP_REGNUM
+ i
));
1851 /* Information about registers. */
1852 set_gdbarch_num_regs (gdbarch
, regnum
);
1853 set_gdbarch_sp_regnum (gdbarch
, LOONGARCH_SP_REGNUM
);
1854 set_gdbarch_pc_regnum (gdbarch
, LOONGARCH_PC_REGNUM
);
1856 /* Finalise the target description registers. */
1857 tdesc_use_registers (gdbarch
, tdesc
, std::move (tdesc_data
));
1859 /* Functions handling dummy frames. */
1860 set_gdbarch_push_dummy_call (gdbarch
, loongarch_push_dummy_call
);
1862 /* Return value info */
1863 set_gdbarch_return_value (gdbarch
, loongarch_return_value
);
1865 /* Advance PC across function entry code. */
1866 set_gdbarch_skip_prologue (gdbarch
, loongarch_skip_prologue
);
1868 /* Stack grows downward. */
1869 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
1872 set_gdbarch_frame_align (gdbarch
, loongarch_frame_align
);
1874 /* Breakpoint manipulation. */
1875 set_gdbarch_software_single_step (gdbarch
, loongarch_software_single_step
);
1876 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, loongarch_breakpoint::kind_from_pc
);
1877 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, loongarch_breakpoint::bp_from_kind
);
1878 set_gdbarch_have_nonsteppable_watchpoint (gdbarch
, 1);
1880 /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder. */
1881 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, loongarch_dwarf2_reg_to_regnum
);
1882 dwarf2_append_unwinders (gdbarch
);
1883 frame_unwind_append_unwinder (gdbarch
, &loongarch_frame_unwind
);
1885 /* Hook in OS ABI-specific overrides, if they have been registered. */
1886 gdbarch_init_osabi (info
, gdbarch
);
1887 set_gdbarch_register_reggroup_p (gdbarch
, loongarch_register_reggroup_p
);
1892 void _initialize_loongarch_tdep ();
1894 _initialize_loongarch_tdep ()
1896 gdbarch_register (bfd_arch_loongarch
, loongarch_gdbarch_init
, nullptr);