1 /* Target-dependent code for the LoongArch architecture, for GDB.
3 Copyright (C) 2022 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/>. */
21 #include "arch-utils.h"
22 #include "dwarf2/frame.h"
24 #include "frame-unwind.h"
26 #include "loongarch-tdep.h"
28 #include "target-descriptions.h"
29 #include "trad-frame.h"
30 #include "user-regs.h"
32 /* Fetch the instruction at PC. */
35 loongarch_fetch_instruction (CORE_ADDR pc
)
37 size_t insn_len
= loongarch_insn_length (0);
38 gdb_byte buf
[insn_len
];
41 err
= target_read_memory (pc
, buf
, insn_len
);
43 memory_error (TARGET_XFER_E_IO
, pc
);
45 return extract_unsigned_integer (buf
, insn_len
, BFD_ENDIAN_LITTLE
);
48 /* Return TRUE if INSN is a unconditional branch instruction, otherwise return FALSE. */
51 loongarch_insn_is_uncond_branch (insn_t insn
)
53 if ((insn
& 0xfc000000) == 0x4c000000 /* jirl */
54 || (insn
& 0xfc000000) == 0x50000000 /* b */
55 || (insn
& 0xfc000000) == 0x54000000) /* bl */
60 /* Return TRUE if INSN is a conditional branch instruction, otherwise return FALSE. */
63 loongarch_insn_is_cond_branch (insn_t insn
)
65 if ((insn
& 0xfc000000) == 0x58000000 /* beq */
66 || (insn
& 0xfc000000) == 0x5c000000 /* bne */
67 || (insn
& 0xfc000000) == 0x60000000 /* blt */
68 || (insn
& 0xfc000000) == 0x64000000 /* bge */
69 || (insn
& 0xfc000000) == 0x68000000 /* bltu */
70 || (insn
& 0xfc000000) == 0x6c000000 /* bgeu */
71 || (insn
& 0xfc000000) == 0x40000000 /* beqz */
72 || (insn
& 0xfc000000) == 0x44000000) /* bnez */
77 /* Return TRUE if INSN is a branch instruction, otherwise return FALSE. */
80 loongarch_insn_is_branch (insn_t insn
)
82 bool is_uncond
= loongarch_insn_is_uncond_branch (insn
);
83 bool is_cond
= loongarch_insn_is_cond_branch (insn
);
85 return (is_uncond
|| is_cond
);
88 /* Return TRUE if INSN is a Load Linked instruction, otherwise return FALSE. */
91 loongarch_insn_is_ll (insn_t insn
)
93 if ((insn
& 0xff000000) == 0x20000000 /* ll.w */
94 || (insn
& 0xff000000) == 0x22000000) /* ll.d */
99 /* Return TRUE if INSN is a Store Conditional instruction, otherwise return FALSE. */
102 loongarch_insn_is_sc (insn_t insn
)
104 if ((insn
& 0xff000000) == 0x21000000 /* sc.w */
105 || (insn
& 0xff000000) == 0x23000000) /* sc.d */
110 /* Analyze the function prologue from START_PC to LIMIT_PC.
111 Return the address of the first instruction past the prologue. */
114 loongarch_scan_prologue (struct gdbarch
*gdbarch
, CORE_ADDR start_pc
,
115 CORE_ADDR limit_pc
, struct frame_info
*this_frame
,
116 struct trad_frame_cache
*this_cache
)
118 CORE_ADDR cur_pc
= start_pc
, prologue_end
= 0;
119 int32_t sp
= LOONGARCH_SP_REGNUM
;
120 int32_t fp
= LOONGARCH_FP_REGNUM
;
121 int32_t reg_value
[32] = {0};
122 int32_t reg_used
[32] = {1, 0};
124 while (cur_pc
< limit_pc
)
126 insn_t insn
= loongarch_fetch_instruction (cur_pc
);
127 size_t insn_len
= loongarch_insn_length (insn
);
128 int32_t rd
= loongarch_decode_imm ("0:5", insn
, 0);
129 int32_t rj
= loongarch_decode_imm ("5:5", insn
, 0);
130 int32_t rk
= loongarch_decode_imm ("10:5", insn
, 0);
131 int32_t si12
= loongarch_decode_imm ("10:12", insn
, 1);
132 int32_t si20
= loongarch_decode_imm ("5:20", insn
, 1);
134 if ((insn
& 0xffc00000) == 0x02c00000 /* addi.d sp,sp,si12 */
135 && rd
== sp
&& rj
== sp
&& si12
< 0)
137 prologue_end
= cur_pc
+ insn_len
;
139 else if ((insn
& 0xffc00000) == 0x02c00000 /* addi.d fp,sp,si12 */
140 && rd
== fp
&& rj
== sp
&& si12
> 0)
142 prologue_end
= cur_pc
+ insn_len
;
144 else if ((insn
& 0xffc00000) == 0x29c00000 /* st.d rd,sp,si12 */
147 prologue_end
= cur_pc
+ insn_len
;
149 else if ((insn
& 0xff000000) == 0x27000000 /* stptr.d rd,sp,si14 */
152 prologue_end
= cur_pc
+ insn_len
;
154 else if ((insn
& 0xfe000000) == 0x14000000) /* lu12i.w rd,si20 */
156 reg_value
[rd
] = si20
<< 12;
159 else if ((insn
& 0xffc00000) == 0x03800000) /* ori rd,rj,si12 */
163 reg_value
[rd
] = reg_value
[rj
] | (si12
& 0xfff);
167 else if ((insn
& 0xffff8000) == 0x00108000 /* add.d sp,sp,rk */
168 && rd
== sp
&& rj
== sp
)
170 if (reg_used
[rk
] == 1 && reg_value
[rk
] < 0)
172 prologue_end
= cur_pc
+ insn_len
;
176 else if (loongarch_insn_is_branch (insn
))
184 if (prologue_end
== 0)
185 prologue_end
= cur_pc
;
190 /* Implement the loongarch_skip_prologue gdbarch method. */
193 loongarch_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
197 /* See if we can determine the end of the prologue via the symbol table.
198 If so, then return either PC, or the PC after the prologue, whichever
200 if (find_pc_partial_function (pc
, nullptr, &func_addr
, nullptr))
202 CORE_ADDR post_prologue_pc
203 = skip_prologue_using_sal (gdbarch
, func_addr
);
204 if (post_prologue_pc
!= 0)
205 return std::max (pc
, post_prologue_pc
);
208 /* Can't determine prologue from the symbol table, need to examine
211 /* Find an upper limit on the function prologue using the debug
212 information. If the debug information could not be used to provide
213 that bound, then use an arbitrary large number as the upper bound. */
214 CORE_ADDR limit_pc
= skip_prologue_using_sal (gdbarch
, pc
);
216 limit_pc
= pc
+ 100; /* Arbitrary large number. */
218 return loongarch_scan_prologue (gdbarch
, pc
, limit_pc
, nullptr, nullptr);
221 /* Decode the current instruction and determine the address of the
225 loongarch_next_pc (struct regcache
*regcache
, CORE_ADDR cur_pc
)
227 struct gdbarch
*gdbarch
= regcache
->arch ();
228 loongarch_gdbarch_tdep
*tdep
= gdbarch_tdep
<loongarch_gdbarch_tdep
> (gdbarch
);
229 insn_t insn
= loongarch_fetch_instruction (cur_pc
);
230 size_t insn_len
= loongarch_insn_length (insn
);
231 CORE_ADDR next_pc
= cur_pc
+ insn_len
;
233 if ((insn
& 0xfc000000) == 0x4c000000) /* jirl rd, rj, offs16 */
235 LONGEST rj
= regcache_raw_get_signed (regcache
,
236 loongarch_decode_imm ("5:5", insn
, 0));
237 next_pc
= rj
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
239 else if ((insn
& 0xfc000000) == 0x50000000 /* b offs26 */
240 || (insn
& 0xfc000000) == 0x54000000) /* bl offs26 */
242 next_pc
= cur_pc
+ loongarch_decode_imm ("0:10|10:16<<2", insn
, 1);
244 else if ((insn
& 0xfc000000) == 0x58000000) /* beq rj, rd, offs16 */
246 LONGEST rj
= regcache_raw_get_signed (regcache
,
247 loongarch_decode_imm ("5:5", insn
, 0));
248 LONGEST rd
= regcache_raw_get_signed (regcache
,
249 loongarch_decode_imm ("0:5", insn
, 0));
251 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
253 else if ((insn
& 0xfc000000) == 0x5c000000) /* bne rj, rd, offs16 */
255 LONGEST rj
= regcache_raw_get_signed (regcache
,
256 loongarch_decode_imm ("5:5", insn
, 0));
257 LONGEST rd
= regcache_raw_get_signed (regcache
,
258 loongarch_decode_imm ("0:5", insn
, 0));
260 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
262 else if ((insn
& 0xfc000000) == 0x60000000) /* blt rj, rd, offs16 */
264 LONGEST rj
= regcache_raw_get_signed (regcache
,
265 loongarch_decode_imm ("5:5", insn
, 0));
266 LONGEST rd
= regcache_raw_get_signed (regcache
,
267 loongarch_decode_imm ("0:5", insn
, 0));
269 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
271 else if ((insn
& 0xfc000000) == 0x64000000) /* bge rj, rd, offs16 */
273 LONGEST rj
= regcache_raw_get_signed (regcache
,
274 loongarch_decode_imm ("5:5", insn
, 0));
275 LONGEST rd
= regcache_raw_get_signed (regcache
,
276 loongarch_decode_imm ("0:5", insn
, 0));
278 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
280 else if ((insn
& 0xfc000000) == 0x68000000) /* bltu rj, rd, offs16 */
282 ULONGEST rj
= regcache_raw_get_unsigned (regcache
,
283 loongarch_decode_imm ("5:5", insn
, 0));
284 ULONGEST rd
= regcache_raw_get_unsigned (regcache
,
285 loongarch_decode_imm ("0:5", insn
, 0));
287 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
289 else if ((insn
& 0xfc000000) == 0x6c000000) /* bgeu rj, rd, offs16 */
291 ULONGEST rj
= regcache_raw_get_unsigned (regcache
,
292 loongarch_decode_imm ("5:5", insn
, 0));
293 ULONGEST rd
= regcache_raw_get_unsigned (regcache
,
294 loongarch_decode_imm ("0:5", insn
, 0));
296 next_pc
= cur_pc
+ loongarch_decode_imm ("10:16<<2", insn
, 1);
298 else if ((insn
& 0xfc000000) == 0x40000000) /* beqz rj, offs21 */
300 LONGEST rj
= regcache_raw_get_signed (regcache
,
301 loongarch_decode_imm ("5:5", insn
, 0));
303 next_pc
= cur_pc
+ loongarch_decode_imm ("0:5|10:16<<2", insn
, 1);
305 else if ((insn
& 0xfc000000) == 0x44000000) /* bnez rj, offs21 */
307 LONGEST rj
= regcache_raw_get_signed (regcache
,
308 loongarch_decode_imm ("5:5", insn
, 0));
310 next_pc
= cur_pc
+ loongarch_decode_imm ("0:5|10:16<<2", insn
, 1);
312 else if ((insn
& 0xffff8000) == 0x002b0000) /* syscall */
314 if (tdep
->syscall_next_pc
!= nullptr)
315 next_pc
= tdep
->syscall_next_pc (get_current_frame ());
321 /* We can't put a breakpoint in the middle of a ll/sc atomic sequence,
322 so look for the end of the sequence and put the breakpoint there. */
324 static std::vector
<CORE_ADDR
>
325 loongarch_deal_with_atomic_sequence (struct regcache
*regcache
, CORE_ADDR cur_pc
)
328 std::vector
<CORE_ADDR
> next_pcs
;
329 insn_t insn
= loongarch_fetch_instruction (cur_pc
);
330 size_t insn_len
= loongarch_insn_length (insn
);
331 const int atomic_sequence_length
= 16;
332 bool found_atomic_sequence_endpoint
= false;
334 /* Look for a Load Linked instruction which begins the atomic sequence. */
335 if (!loongarch_insn_is_ll (insn
))
338 /* Assume that no atomic sequence is longer than "atomic_sequence_length" instructions. */
339 for (int insn_count
= 0; insn_count
< atomic_sequence_length
; ++insn_count
)
342 insn
= loongarch_fetch_instruction (cur_pc
);
344 /* Look for a unconditional branch instruction, fallback to the standard code. */
345 if (loongarch_insn_is_uncond_branch (insn
))
349 /* Look for a conditional branch instruction, put a breakpoint in its destination address. */
350 else if (loongarch_insn_is_cond_branch (insn
))
352 next_pc
= loongarch_next_pc (regcache
, cur_pc
);
353 next_pcs
.push_back (next_pc
);
355 /* Look for a Store Conditional instruction which closes the atomic sequence. */
356 else if (loongarch_insn_is_sc (insn
))
358 found_atomic_sequence_endpoint
= true;
359 next_pc
= cur_pc
+ insn_len
;
360 next_pcs
.push_back (next_pc
);
365 /* We didn't find a closing Store Conditional instruction, fallback to the standard code. */
366 if (!found_atomic_sequence_endpoint
)
372 /* Implement the software_single_step gdbarch method */
374 static std::vector
<CORE_ADDR
>
375 loongarch_software_single_step (struct regcache
*regcache
)
377 CORE_ADDR cur_pc
= regcache_read_pc (regcache
);
378 std::vector
<CORE_ADDR
> next_pcs
379 = loongarch_deal_with_atomic_sequence (regcache
, cur_pc
);
381 if (!next_pcs
.empty ())
384 CORE_ADDR next_pc
= loongarch_next_pc (regcache
, cur_pc
);
389 /* Implement the frame_align gdbarch method. */
392 loongarch_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
394 return align_down (addr
, 16);
397 /* Generate, or return the cached frame cache for frame unwinder. */
399 static struct trad_frame_cache
*
400 loongarch_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
402 struct trad_frame_cache
*cache
;
405 if (*this_cache
!= nullptr)
406 return (struct trad_frame_cache
*) *this_cache
;
408 cache
= trad_frame_cache_zalloc (this_frame
);
411 trad_frame_set_reg_realreg (cache
, LOONGARCH_PC_REGNUM
, LOONGARCH_RA_REGNUM
);
413 pc
= get_frame_address_in_block (this_frame
);
414 trad_frame_set_id (cache
, frame_id_build_unavailable_stack (pc
));
419 /* Implement the this_id callback for frame unwinder. */
422 loongarch_frame_this_id (struct frame_info
*this_frame
, void **prologue_cache
,
423 struct frame_id
*this_id
)
425 struct trad_frame_cache
*info
;
427 info
= loongarch_frame_cache (this_frame
, prologue_cache
);
428 trad_frame_get_id (info
, this_id
);
431 /* Implement the prev_register callback for frame unwinder. */
433 static struct value
*
434 loongarch_frame_prev_register (struct frame_info
*this_frame
,
435 void **prologue_cache
, int regnum
)
437 struct trad_frame_cache
*info
;
439 info
= loongarch_frame_cache (this_frame
, prologue_cache
);
440 return trad_frame_get_register (info
, this_frame
, regnum
);
443 static const struct frame_unwind loongarch_frame_unwind
= {
444 "loongarch prologue",
445 /*.type =*/NORMAL_FRAME
,
446 /*.stop_reason =*/default_frame_unwind_stop_reason
,
447 /*.this_id =*/loongarch_frame_this_id
,
448 /*.prev_register =*/loongarch_frame_prev_register
,
449 /*.unwind_data =*/nullptr,
450 /*.sniffer =*/default_frame_sniffer
,
451 /*.dealloc_cache =*/nullptr,
452 /*.prev_arch =*/nullptr,
455 /* Write the contents of buffer VAL into the general-purpose argument
456 register defined by GAR in REGCACHE. GAR indicates the available
457 general-purpose argument registers which should be a value in the
458 range 1 to 8 (LOONGARCH_ARG_REGNUM), which correspond to registers
459 a7 and a0 respectively, that is to say, regnum is a7 if GAR is 1,
460 regnum is a6 if GAR is 2, regnum is a5 if GAR is 3, regnum is a4
461 if GAR is 4, regnum is a3 if GAR is 5, regnum is a2 if GAR is 6,
462 regnum is a1 if GAR is 7, regnum is a0 if GAR is 8. */
465 pass_in_gar (struct regcache
*regcache
, unsigned int gar
, const gdb_byte
*val
)
467 unsigned int regnum
= LOONGARCH_ARG_REGNUM
- gar
+ LOONGARCH_A0_REGNUM
;
468 regcache
->cooked_write (regnum
, val
);
471 /* Write the contents of buffer VAL into the floating-point argument
472 register defined by FAR in REGCACHE. FAR indicates the available
473 floating-point argument registers which should be a value in the
474 range 1 to 8 (LOONGARCH_ARG_REGNUM), which correspond to registers
475 f7 and f0 respectively, that is to say, regnum is f7 if FAR is 1,
476 regnum is f6 if FAR is 2, regnum is f5 if FAR is 3, regnum is f4
477 if FAR is 4, regnum is f3 if FAR is 5, regnum is f2 if FAR is 6,
478 regnum is f1 if FAR is 7, regnum is f0 if FAR is 8. */
481 pass_in_far (struct regcache
*regcache
, unsigned int far
, const gdb_byte
*val
)
483 unsigned int regnum
= LOONGARCH_ARG_REGNUM
- far
+ LOONGARCH_FIRST_FP_REGNUM
;
484 regcache
->cooked_write (regnum
, val
);
487 /* Pass a value on the stack. */
490 pass_on_stack (struct regcache
*regcache
, const gdb_byte
*val
,
491 size_t len
, int align
, gdb_byte
**addr
)
493 align
= align_up (align
, 8);
497 CORE_ADDR align_addr
= (CORE_ADDR
) (*addr
);
498 align_addr
= align_up (align_addr
, align
);
499 *addr
= (gdb_byte
*) align_addr
;
500 memcpy (*addr
, val
, len
);
504 /* Compute the numbers of struct member. */
507 compute_struct_member (struct type
*type
,
508 unsigned int *fixed_point_members
,
509 unsigned int *floating_point_members
,
510 bool *first_member_is_fixed_point
)
512 for (int i
= 0; i
< type
->num_fields (); i
++)
514 struct type
*field_type
= check_typedef (type
->field (i
).type ());
516 if (field_type
->code () == TYPE_CODE_INT
517 || field_type
->code () == TYPE_CODE_BOOL
518 || field_type
->code () == TYPE_CODE_CHAR
519 || field_type
->code () == TYPE_CODE_RANGE
520 || field_type
->code () == TYPE_CODE_ENUM
521 || field_type
->code () == TYPE_CODE_PTR
)
523 (*fixed_point_members
)++;
525 if (*floating_point_members
== 0)
526 *first_member_is_fixed_point
= true;
528 else if (field_type
->code () == TYPE_CODE_FLT
)
529 (*floating_point_members
)++;
530 else if (field_type
->code () == TYPE_CODE_STRUCT
)
531 compute_struct_member (field_type
,
533 floating_point_members
,
534 first_member_is_fixed_point
);
535 else if (field_type
->code () == TYPE_CODE_COMPLEX
)
536 (*floating_point_members
) += 2;
540 /* Implement the push_dummy_call gdbarch method. */
543 loongarch_push_dummy_call (struct gdbarch
*gdbarch
,
544 struct value
*function
,
545 struct regcache
*regcache
,
550 function_call_return_method return_method
,
551 CORE_ADDR struct_addr
)
553 int regsize
= register_size (gdbarch
, 0);
554 unsigned int gar
= LOONGARCH_ARG_REGNUM
;
555 unsigned int far
= LOONGARCH_ARG_REGNUM
;
556 unsigned int fixed_point_members
;
557 unsigned int floating_point_members
;
558 bool first_member_is_fixed_point
;
559 gdb_byte buf
[1024] = { 0 };
560 gdb_byte
*addr
= buf
;
562 if (return_method
!= return_method_normal
)
563 pass_in_gar (regcache
, gar
--, (gdb_byte
*) &struct_addr
);
565 for (int i
= 0; i
< nargs
; i
++)
567 struct value
*arg
= args
[i
];
568 const gdb_byte
*val
= value_contents (arg
).data ();
569 struct type
*type
= check_typedef (value_type (arg
));
570 size_t len
= TYPE_LENGTH (type
);
571 int align
= type_align (type
);
572 enum type_code code
= type
->code ();
579 case TYPE_CODE_RANGE
:
583 /* integer or pointer type is passed in GAR.
584 If no GAR is available, it's passed on the stack.
585 When passed in registers or on the stack,
586 the unsigned integer scalars are zero-extended to GRLEN bits,
587 and the signed integer scalars are sign-extended. */
588 if (type
->is_unsigned ())
590 ULONGEST data
= extract_unsigned_integer (val
, len
, BFD_ENDIAN_LITTLE
);
592 pass_in_gar (regcache
, gar
--, (gdb_byte
*) &data
);
594 pass_on_stack (regcache
, (gdb_byte
*) &data
, len
, align
, &addr
);
598 LONGEST data
= extract_signed_integer (val
, len
, BFD_ENDIAN_LITTLE
);
600 pass_in_gar (regcache
, gar
--, (gdb_byte
*) &data
);
602 pass_on_stack (regcache
, (gdb_byte
*) &data
, len
, align
, &addr
);
607 if (len
== 2 * regsize
)
609 /* long double type is passed in a pair of GAR,
610 with the low-order GRLEN bits in the lower-numbered register
611 and the high-order GRLEN bits in the higher-numbered register.
612 If exactly one register is available,
613 the low-order GRLEN bits are passed in the register
614 and the high-order GRLEN bits are passed on the stack.
615 If no GAR is available, it's passed on the stack. */
618 pass_in_gar (regcache
, gar
--, val
);
619 pass_in_gar (regcache
, gar
--, val
+ regsize
);
623 pass_in_gar (regcache
, gar
--, val
);
624 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
628 pass_on_stack (regcache
, val
, len
, align
, &addr
);
633 /* The other floating-point type is passed in FAR.
634 If no FAR is available, it's passed in GAR.
635 If no GAR is available, it's passed on the stack. */
637 pass_in_far (regcache
, far
--, val
);
639 pass_in_gar (regcache
, gar
--, val
);
641 pass_on_stack (regcache
, val
, len
, align
, &addr
);
644 case TYPE_CODE_STRUCT
:
646 fixed_point_members
= 0;
647 floating_point_members
= 0;
648 first_member_is_fixed_point
= false;
649 compute_struct_member (type
,
650 &fixed_point_members
,
651 &floating_point_members
,
652 &first_member_is_fixed_point
);
654 if (len
> 0 && len
<= regsize
)
656 /* The structure has only fixed-point members. */
657 if (fixed_point_members
> 0 && floating_point_members
== 0)
659 /* If there is an available GAR,
660 the structure is passed through the GAR by value passing;
661 If no GAR is available, it's passed on the stack. */
663 pass_in_gar (regcache
, gar
--, val
);
665 pass_on_stack (regcache
, val
, len
, align
, &addr
);
667 /* The structure has only floating-point members. */
668 else if (fixed_point_members
== 0 && floating_point_members
> 0)
670 /* The structure has one floating-point member.
671 The argument is passed in a FAR.
672 If no FAR is available, the value is passed in a GAR.
673 if no GAR is available, the value is passed on the stack. */
674 if (floating_point_members
== 1)
677 pass_in_far (regcache
, far
--, val
);
679 pass_in_gar (regcache
, gar
--, val
);
681 pass_on_stack (regcache
, val
, len
, align
, &addr
);
683 /* The structure has two floating-point members.
684 The argument is passed in a pair of available FAR,
685 with the low-order float member bits in the lower-numbered FAR
686 and the high-order float member bits in the higher-numbered FAR.
687 If the number of available FAR is less than 2, it's passed in a GAR,
688 and passed on the stack if no GAR is available. */
689 else if (floating_point_members
== 2)
693 pass_in_far (regcache
, far
--, val
);
694 pass_in_far (regcache
, far
--, val
+ align
);
698 pass_in_gar (regcache
, gar
--, val
);
702 pass_on_stack (regcache
, val
, len
, align
, &addr
);
706 /* The structure has both fixed-point and floating-point members. */
707 else if (fixed_point_members
> 0 && floating_point_members
> 0)
709 /* The structure has one float member and multiple fixed-point members.
710 If there are available GAR, the structure is passed in a GAR,
711 and passed on the stack if no GAR is available. */
712 if (floating_point_members
== 1 && fixed_point_members
> 1)
715 pass_in_gar (regcache
, gar
--, val
);
717 pass_on_stack (regcache
, val
, len
, align
, &addr
);
719 /* The structure has one float member and one fixed-point member.
720 If one FAR and one GAR are available,
721 the floating-point member of the structure is passed in the FAR,
722 and the fixed-point member of the structure is passed in the GAR.
723 If no floating-point register but one GAR is available, it's passed in GAR;
724 If no GAR is available, it's passed on the stack. */
725 else if (floating_point_members
== 1 && fixed_point_members
== 1)
727 if (far
> 0 && gar
> 0)
729 if (first_member_is_fixed_point
== false)
731 pass_in_far (regcache
, far
--, val
);
732 pass_in_gar (regcache
, gar
--, val
+ align
);
736 pass_in_gar (regcache
, gar
--, val
);
737 pass_in_far (regcache
, far
--, val
+ align
);
743 pass_in_gar (regcache
, gar
--, val
);
745 pass_on_stack (regcache
, val
, len
, align
, &addr
);
750 else if (len
> regsize
&& len
<= 2 * regsize
)
752 /* The structure has only fixed-point members. */
753 if (fixed_point_members
> 0 && floating_point_members
== 0)
755 /* The argument is passed in a pair of available GAR,
756 with the low-order bits in the lower-numbered GAR
757 and the high-order bits in the higher-numbered GAR.
758 If only one GAR is available,
759 the low-order bits are in the GAR
760 and the high-order bits are on the stack,
761 and passed on the stack if no GAR is available. */
764 pass_in_gar (regcache
, gar
--, val
);
765 pass_in_gar (regcache
, gar
--, val
+ regsize
);
769 pass_in_gar (regcache
, gar
--, val
);
770 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
774 pass_on_stack (regcache
, val
, len
, align
, &addr
);
777 /* The structure has only floating-point members. */
778 else if (fixed_point_members
== 0 && floating_point_members
> 0)
780 /* The structure has one long double member
781 or one double member and two adjacent float members
782 or 3-4 float members.
783 The argument is passed in a pair of available GAR,
784 with the low-order bits in the lower-numbered GAR
785 and the high-order bits in the higher-numbered GAR.
786 If only one GAR is available,
787 the low-order bits are in the GAR
788 and the high-order bits are on the stack,
789 and passed on the stack if no GAR is available. */
790 if ((len
== 16 && floating_point_members
== 1)
791 || (len
== 16 && floating_point_members
== 3)
792 || (len
== 12 && floating_point_members
== 3)
793 || (len
== 16 && floating_point_members
== 4))
797 pass_in_gar (regcache
, gar
--, val
);
798 pass_in_gar (regcache
, gar
--, val
+ regsize
);
802 pass_in_gar (regcache
, gar
--, val
);
803 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
807 pass_on_stack (regcache
, val
, len
, align
, &addr
);
810 /* The structure has two double members
811 or one double member and one float member.
812 The argument is passed in a pair of available FAR,
813 with the low-order bits in the lower-numbered FAR
814 and the high-order bits in the higher-numbered FAR.
815 If no a pair of available FAR,
816 it's passed in a pair of available GAR,
817 with the low-order bits in the lower-numbered GAR
818 and the high-order bits in the higher-numbered GAR.
819 If only one GAR is available,
820 the low-order bits are in the GAR
821 and the high-order bits are on stack,
822 and passed on the stack if no GAR is available. */
823 else if ((len
== 16 && floating_point_members
== 2)
824 || (len
== 12 && floating_point_members
== 2))
828 pass_in_far (regcache
, far
--, val
);
829 pass_in_far (regcache
, far
--, val
+ regsize
);
833 pass_in_gar (regcache
, gar
--, val
);
834 pass_in_gar (regcache
, gar
--, val
+ regsize
);
838 pass_in_gar (regcache
, gar
--, val
);
839 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
843 pass_on_stack (regcache
, val
, len
, align
, &addr
);
847 /* The structure has both fixed-point and floating-point members. */
848 else if (fixed_point_members
> 0 && floating_point_members
> 0)
850 /* The structure has one floating-point member and one fixed-point member. */
851 if (floating_point_members
== 1 && fixed_point_members
== 1)
853 /* If one FAR and one GAR are available,
854 the floating-point member of the structure is passed in the FAR,
855 and the fixed-point member of the structure is passed in the GAR;
856 If no floating-point registers but two GARs are available,
857 it's passed in the two GARs;
858 If only one GAR is available,
859 the low-order bits are in the GAR
860 and the high-order bits are on the stack;
861 And it's passed on the stack if no GAR is available. */
862 if (far
> 0 && gar
> 0)
864 if (first_member_is_fixed_point
== false)
866 pass_in_far (regcache
, far
--, val
);
867 pass_in_gar (regcache
, gar
--, val
+ regsize
);
871 pass_in_gar (regcache
, gar
--, val
);
872 pass_in_far (regcache
, far
--, val
+ regsize
);
875 else if (far
== 0 && gar
>= 2)
877 pass_in_gar (regcache
, gar
--, val
);
878 pass_in_gar (regcache
, gar
--, val
+ regsize
);
880 else if (far
== 0 && gar
== 1)
882 pass_in_gar (regcache
, gar
--, val
);
883 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
885 else if (far
== 0 && gar
== 0)
887 pass_on_stack (regcache
, val
, len
, align
, &addr
);
892 /* The argument is passed in a pair of available GAR,
893 with the low-order bits in the lower-numbered GAR
894 and the high-order bits in the higher-numbered GAR.
895 If only one GAR is available,
896 the low-order bits are in the GAR
897 and the high-order bits are on the stack,
898 and passed on the stack if no GAR is available. */
901 pass_in_gar (regcache
, gar
--, val
);
902 pass_in_gar (regcache
, gar
--, val
+ regsize
);
906 pass_in_gar (regcache
, gar
--, val
);
907 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
911 pass_on_stack (regcache
, val
, len
, align
, &addr
);
916 else if (len
> 2 * regsize
)
918 /* It's passed by reference and are replaced in the argument list with the address.
919 If there is an available GAR, the reference is passed in the GAR,
920 and passed on the stack if no GAR is available. */
921 sp
= align_down (sp
- len
, 16);
922 write_memory (sp
, val
, len
);
925 pass_in_gar (regcache
, gar
--, (const gdb_byte
*) &sp
);
927 pass_on_stack (regcache
, (const gdb_byte
*) &sp
, len
, regsize
, &addr
);
931 case TYPE_CODE_UNION
:
932 /* Union is passed in GAR or stack. */
933 if (len
> 0 && len
<= regsize
)
935 /* The argument is passed in a GAR,
936 or on the stack by value if no GAR is available. */
938 pass_in_gar (regcache
, gar
--, val
);
940 pass_on_stack (regcache
, val
, len
, align
, &addr
);
942 else if (len
> regsize
&& len
<= 2 * regsize
)
944 /* The argument is passed in a pair of available GAR,
945 with the low-order bits in the lower-numbered GAR
946 and the high-order bits in the higher-numbered GAR.
947 If only one GAR is available,
948 the low-order bits are in the GAR
949 and the high-order bits are on the stack.
950 The arguments are passed on the stack when no GAR is available. */
953 pass_in_gar (regcache
, gar
--, val
);
954 pass_in_gar (regcache
, gar
--, val
+ regsize
);
958 pass_in_gar (regcache
, gar
--, val
);
959 pass_on_stack (regcache
, val
+ regsize
, len
- regsize
, align
, &addr
);
963 pass_on_stack (regcache
, val
, len
, align
, &addr
);
966 else if (len
> 2 * regsize
)
968 /* It's passed by reference and are replaced in the argument list with the address.
969 If there is an available GAR, the reference is passed in the GAR,
970 and passed on the stack if no GAR is available. */
971 sp
= align_down (sp
- len
, 16);
972 write_memory (sp
, val
, len
);
975 pass_in_gar (regcache
, gar
--, (const gdb_byte
*) &sp
);
977 pass_on_stack (regcache
, (const gdb_byte
*) &sp
, len
, regsize
, &addr
);
980 case TYPE_CODE_COMPLEX
:
982 struct type
*target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
983 size_t target_len
= TYPE_LENGTH (target_type
);
985 if (target_len
< regsize
)
987 /* The complex with two float members
988 is passed in a pair of available FAR,
989 with the low-order float member bits in the lower-numbered FAR
990 and the high-order float member bits in the higher-numbered FAR.
991 If the number of available FAR is less than 2, it's passed in a GAR,
992 and passed on the stack if no GAR is available. */
995 pass_in_far (regcache
, far
--, val
);
996 pass_in_far (regcache
, far
--, val
+ align
);
1000 pass_in_gar (regcache
, gar
--, val
);
1004 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1007 else if (target_len
== regsize
)
1009 /* The complex with two double members
1010 is passed in a pair of available FAR,
1011 with the low-order bits in the lower-numbered FAR
1012 and the high-order bits in the higher-numbered FAR.
1013 If no a pair of available FAR,
1014 it's passed in a pair of available GAR,
1015 with the low-order bits in the lower-numbered GAR
1016 and the high-order bits in the higher-numbered GAR.
1017 If only one GAR is available,
1018 the low-order bits are in the GAR
1019 and the high-order bits are on stack,
1020 and passed on the stack if no GAR is available. */
1024 pass_in_far (regcache
, far
--, val
);
1025 pass_in_far (regcache
, far
--, val
+ align
);
1029 pass_in_gar (regcache
, gar
--, val
);
1030 pass_in_gar (regcache
, gar
--, val
+ align
);
1034 pass_in_gar (regcache
, gar
--, val
);
1035 pass_on_stack (regcache
, val
+ align
, len
- align
, align
, &addr
);
1039 pass_on_stack (regcache
, val
, len
, align
, &addr
);
1043 else if (target_len
== 2 * regsize
)
1045 /* The complex with two long double members
1046 is passed by reference and are replaced in the argument list with the address.
1047 If there is an available GAR, the reference is passed in the GAR,
1048 and passed on the stack if no GAR is available. */
1049 sp
= align_down (sp
- len
, 16);
1050 write_memory (sp
, val
, len
);
1053 pass_in_gar (regcache
, gar
--, (const gdb_byte
*) &sp
);
1055 pass_on_stack (regcache
, (const gdb_byte
*) &sp
, regsize
, regsize
, &addr
);
1067 sp
= align_down (sp
, 16);
1068 write_memory (sp
, buf
, addr
- buf
);
1071 regcache_cooked_write_unsigned (regcache
, LOONGARCH_RA_REGNUM
, bp_addr
);
1072 regcache_cooked_write_unsigned (regcache
, LOONGARCH_SP_REGNUM
, sp
);
1077 /* Partial transfer of a cooked register. */
1080 loongarch_xfer_reg (struct regcache
*regcache
,
1081 int regnum
, int len
, gdb_byte
*readbuf
,
1082 const gdb_byte
*writebuf
, size_t offset
)
1085 regcache
->cooked_read_part (regnum
, 0, len
, readbuf
+ offset
);
1087 regcache
->cooked_write_part (regnum
, 0, len
, writebuf
+ offset
);
1090 /* Implement the return_value gdbarch method. */
1092 static enum return_value_convention
1093 loongarch_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
1094 struct type
*type
, struct regcache
*regcache
,
1095 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
1097 int regsize
= register_size (gdbarch
, 0);
1098 enum type_code code
= type
->code ();
1099 size_t len
= TYPE_LENGTH (type
);
1100 unsigned int fixed_point_members
;
1101 unsigned int floating_point_members
;
1102 bool first_member_is_fixed_point
;
1103 int a0
= LOONGARCH_A0_REGNUM
;
1104 int a1
= LOONGARCH_A0_REGNUM
+ 1;
1105 int f0
= LOONGARCH_FIRST_FP_REGNUM
;
1106 int f1
= LOONGARCH_FIRST_FP_REGNUM
+ 1;
1108 if (len
> 2 * regsize
)
1109 return RETURN_VALUE_STRUCT_CONVENTION
;
1114 case TYPE_CODE_BOOL
:
1115 case TYPE_CODE_CHAR
:
1116 case TYPE_CODE_RANGE
:
1117 case TYPE_CODE_ENUM
:
1120 /* integer or pointer type.
1121 The return value is passed in a0,
1122 the unsigned integer scalars are zero-extended to GRLEN bits,
1123 and the signed integer scalars are sign-extended. */
1126 gdb_byte buf
[regsize
];
1127 if (type
->is_unsigned ())
1129 ULONGEST data
= extract_unsigned_integer (writebuf
, len
, BFD_ENDIAN_LITTLE
);
1130 store_unsigned_integer (buf
, regsize
, BFD_ENDIAN_LITTLE
, data
);
1134 LONGEST data
= extract_signed_integer (writebuf
, len
, BFD_ENDIAN_LITTLE
);
1135 store_signed_integer (buf
, regsize
, BFD_ENDIAN_LITTLE
, data
);
1137 loongarch_xfer_reg (regcache
, a0
, regsize
, nullptr, buf
, 0);
1140 loongarch_xfer_reg (regcache
, a0
, len
, readbuf
, nullptr, 0);
1144 /* long double type.
1145 The return value is passed in a0 and a1. */
1146 if (len
== 2 * regsize
)
1148 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1149 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1151 /* float or double type.
1152 The return value is passed in f0. */
1155 loongarch_xfer_reg (regcache
, f0
, len
, readbuf
, writebuf
, 0);
1158 case TYPE_CODE_STRUCT
:
1160 fixed_point_members
= 0;
1161 floating_point_members
= 0;
1162 first_member_is_fixed_point
= false;
1163 compute_struct_member (type
,
1164 &fixed_point_members
,
1165 &floating_point_members
,
1166 &first_member_is_fixed_point
);
1168 if (len
> 0 && len
<= regsize
)
1170 /* The structure has only fixed-point members. */
1171 if (fixed_point_members
> 0 && floating_point_members
== 0)
1173 /* The return value is passed in a0. */
1174 loongarch_xfer_reg (regcache
, a0
, len
, readbuf
, writebuf
, 0);
1176 /* The structure has only floating-point members. */
1177 else if (fixed_point_members
== 0 && floating_point_members
> 0)
1179 /* The structure has one floating-point member.
1180 The return value is passed in f0. */
1181 if (floating_point_members
== 1)
1183 loongarch_xfer_reg (regcache
, f0
, len
, readbuf
, writebuf
, 0);
1185 /* The structure has two floating-point members.
1186 The return value is passed in f0 and f1. */
1187 else if (floating_point_members
== 2)
1189 loongarch_xfer_reg (regcache
, f0
, len
/ 2, readbuf
, writebuf
, 0);
1190 loongarch_xfer_reg (regcache
, f1
, len
/ 2, readbuf
, writebuf
, len
/ 2);
1193 /* The structure has both fixed-point and floating-point members. */
1194 else if (fixed_point_members
> 0 && floating_point_members
> 0)
1196 /* The structure has one float member and multiple fixed-point members.
1197 The return value is passed in a0. */
1198 if (floating_point_members
== 1 && fixed_point_members
> 1)
1200 loongarch_xfer_reg (regcache
, a0
, len
, readbuf
, writebuf
, 0);
1202 /* The structure has one float member and one fixed-point member. */
1203 else if (floating_point_members
== 1 && fixed_point_members
== 1)
1205 /* The return value is passed in f0 and a0 if the first member is floating-point. */
1206 if (first_member_is_fixed_point
== false)
1208 loongarch_xfer_reg (regcache
, f0
, regsize
/ 2, readbuf
, writebuf
, 0);
1209 loongarch_xfer_reg (regcache
, a0
, regsize
/ 2, readbuf
, writebuf
, regsize
/ 2);
1211 /* The return value is passed in a0 and f0 if the first member is fixed-point. */
1214 loongarch_xfer_reg (regcache
, a0
, regsize
/ 2, readbuf
, writebuf
, 0);
1215 loongarch_xfer_reg (regcache
, f0
, regsize
/ 2, readbuf
, writebuf
, regsize
/ 2);
1220 else if (len
> regsize
&& len
<= 2 * regsize
)
1222 /* The structure has only fixed-point members. */
1223 if (fixed_point_members
> 0 && floating_point_members
== 0)
1225 /* The return value is passed in a0 and a1. */
1226 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1227 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1229 /* The structure has only floating-point members. */
1230 else if (fixed_point_members
== 0 && floating_point_members
> 0)
1232 /* The structure has one long double member
1233 or one double member and two adjacent float members
1234 or 3-4 float members.
1235 The return value is passed in a0 and a1. */
1236 if ((len
== 16 && floating_point_members
== 1)
1237 || (len
== 16 && floating_point_members
== 3)
1238 || (len
== 12 && floating_point_members
== 3)
1239 || (len
== 16 && floating_point_members
== 4))
1241 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1242 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1244 /* The structure has two double members
1245 or one double member and one float member.
1246 The return value is passed in f0 and f1. */
1247 else if ((len
== 16 && floating_point_members
== 2)
1248 || (len
== 12 && floating_point_members
== 2))
1250 loongarch_xfer_reg (regcache
, f0
, regsize
, readbuf
, writebuf
, 0);
1251 loongarch_xfer_reg (regcache
, f1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1254 /* The structure has both fixed-point and floating-point members. */
1255 else if (fixed_point_members
> 0 && floating_point_members
> 0)
1257 /* The structure has one floating-point member and one fixed-point member. */
1258 if (floating_point_members
== 1 && fixed_point_members
== 1)
1260 /* The return value is passed in f0 and a0 if the first member is floating-point. */
1261 if (first_member_is_fixed_point
== false)
1263 loongarch_xfer_reg (regcache
, f0
, regsize
, readbuf
, writebuf
, 0);
1264 loongarch_xfer_reg (regcache
, a0
, len
- regsize
, readbuf
, writebuf
, regsize
);
1266 /* The return value is passed in a0 and f0 if the first member is fixed-point. */
1269 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1270 loongarch_xfer_reg (regcache
, f0
, len
- regsize
, readbuf
, writebuf
, regsize
);
1275 /* The return value is passed in a0 and a1. */
1276 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1277 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1283 case TYPE_CODE_UNION
:
1284 if (len
> 0 && len
<= regsize
)
1286 /* The return value is passed in a0. */
1287 loongarch_xfer_reg (regcache
, a0
, len
, readbuf
, writebuf
, 0);
1289 else if (len
> regsize
&& len
<= 2 * regsize
)
1291 /* The return value is passed in a0 and a1. */
1292 loongarch_xfer_reg (regcache
, a0
, regsize
, readbuf
, writebuf
, 0);
1293 loongarch_xfer_reg (regcache
, a1
, len
- regsize
, readbuf
, writebuf
, regsize
);
1296 case TYPE_CODE_COMPLEX
:
1298 /* The return value is passed in f0 and f1. */
1299 loongarch_xfer_reg (regcache
, f0
, len
/ 2, readbuf
, writebuf
, 0);
1300 loongarch_xfer_reg (regcache
, f1
, len
/ 2, readbuf
, writebuf
, len
/ 2);
1307 return RETURN_VALUE_REGISTER_CONVENTION
;
1310 /* Implement the dwarf2_reg_to_regnum gdbarch method. */
1313 loongarch_dwarf2_reg_to_regnum (struct gdbarch
*gdbarch
, int regnum
)
1315 if (regnum
>= 0 && regnum
< 32)
1317 else if (regnum
>= 32 && regnum
< 66)
1318 return LOONGARCH_FIRST_FP_REGNUM
+ regnum
- 32;
1323 static constexpr gdb_byte loongarch_default_breakpoint
[] = {0x05, 0x00, 0x2a, 0x00};
1324 typedef BP_MANIPULATION (loongarch_default_breakpoint
) loongarch_breakpoint
;
1326 /* Extract a set of required target features out of ABFD. If ABFD is nullptr
1327 then a LOONGARCH_GDBARCH_FEATURES is returned in its default state. */
1329 static struct loongarch_gdbarch_features
1330 loongarch_features_from_bfd (const bfd
*abfd
)
1332 struct loongarch_gdbarch_features features
;
1334 /* Now try to improve on the defaults by looking at the binary we are
1335 going to execute. We assume the user knows what they are doing and
1336 that the target will match the binary. Remember, this code path is
1337 only used at all if the target hasn't given us a description, so this
1338 is really a last ditched effort to do something sane before giving
1340 if (abfd
!= nullptr && bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1342 unsigned char eclass
= elf_elfheader (abfd
)->e_ident
[EI_CLASS
];
1343 int e_flags
= elf_elfheader (abfd
)->e_flags
;
1345 if (eclass
== ELFCLASS32
)
1347 else if (eclass
== ELFCLASS64
)
1350 internal_error (__FILE__
, __LINE__
,
1351 _("unknown ELF header class %d"), eclass
);
1353 if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags
))
1354 features
.fputype
= SINGLE_FLOAT
;
1355 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags
))
1356 features
.fputype
= DOUBLE_FLOAT
;
1362 /* Find a suitable default target description. Use the contents of INFO,
1363 specifically the bfd object being executed, to guide the selection of a
1364 suitable default target description. */
1366 static const struct target_desc
*
1367 loongarch_find_default_target_description (const struct gdbarch_info info
)
1369 /* Extract desired feature set from INFO. */
1370 struct loongarch_gdbarch_features features
1371 = loongarch_features_from_bfd (info
.abfd
);
1373 /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
1374 maybe there was no bfd object. In this case we fall back to a minimal
1375 useful target, the x-register size is selected based on the architecture
1377 if (features
.xlen
== 0)
1378 features
.xlen
= info
.bfd_arch_info
->bits_per_address
== 32 ? 4 : 8;
1380 /* If the FPUTYPE field is still 0 then we got nothing useful from INFO.BFD,
1381 maybe there was no bfd object. In this case we fall back to a usual useful
1382 target with double float. */
1383 if (features
.fputype
== 0)
1384 features
.fputype
= DOUBLE_FLOAT
;
1386 /* Now build a target description based on the feature set. */
1387 return loongarch_lookup_target_description (features
);
1390 /* Initialize the current architecture based on INFO */
1392 static struct gdbarch
*
1393 loongarch_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1396 struct loongarch_gdbarch_features features
;
1397 tdesc_arch_data_up tdesc_data
= tdesc_data_alloc ();
1398 loongarch_gdbarch_tdep
*tdep
= new loongarch_gdbarch_tdep
;
1399 const struct target_desc
*tdesc
= info
.target_desc
;
1401 /* Ensure we always have a target description. */
1402 if (!tdesc_has_registers (tdesc
))
1403 tdesc
= loongarch_find_default_target_description (info
);
1405 const struct tdesc_feature
*feature_cpu
1406 = tdesc_find_feature (tdesc
, "org.gnu.gdb.loongarch.base");
1407 if (feature_cpu
== nullptr)
1411 /* Validate the description provides the mandatory base registers
1412 and allocate their numbers. */
1413 bool valid_p
= true;
1414 for (int i
= 0; i
< 32; i
++)
1415 valid_p
&= tdesc_numbered_register (feature_cpu
, tdesc_data
.get (), regnum
++,
1416 loongarch_r_normal_name
[i
] + 1);
1417 valid_p
&= tdesc_numbered_register (feature_cpu
, tdesc_data
.get (), regnum
++, "orig_a0");
1418 valid_p
&= tdesc_numbered_register (feature_cpu
, tdesc_data
.get (), regnum
++, "pc");
1419 valid_p
&= tdesc_numbered_register (feature_cpu
, tdesc_data
.get (), regnum
++, "badv");
1423 const struct tdesc_feature
*feature_fpu
1424 = tdesc_find_feature (tdesc
, "org.gnu.gdb.loongarch.fpu");
1425 if (feature_fpu
== nullptr)
1428 /* Validate the description provides the fpu registers and
1429 allocate their numbers. */
1430 regnum
= LOONGARCH_FIRST_FP_REGNUM
;
1431 for (int i
= 0; i
< LOONGARCH_LINUX_NUM_FPREGSET
; i
++)
1432 valid_p
&= tdesc_numbered_register (feature_fpu
, tdesc_data
.get (), regnum
++,
1433 loongarch_f_normal_name
[i
] + 1);
1434 for (int i
= 0; i
< LOONGARCH_LINUX_NUM_FCC
; i
++)
1435 valid_p
&= tdesc_numbered_register (feature_fpu
, tdesc_data
.get (), regnum
++,
1436 loongarch_c_normal_name
[i
] + 1);
1437 valid_p
&= tdesc_numbered_register (feature_fpu
, tdesc_data
.get (), regnum
++, "fcsr");
1441 /* LoongArch code is always little-endian. */
1442 info
.byte_order_for_code
= BFD_ENDIAN_LITTLE
;
1444 /* Have a look at what the supplied (if any) bfd object requires of the
1445 target, then check that this matches with what the target is
1447 struct loongarch_gdbarch_features abi_features
1448 = loongarch_features_from_bfd (info
.abfd
);
1450 /* If the ABI_FEATURES xlen or fputype is 0 then this indicates we got
1451 no useful abi features from the INFO object. In this case we just
1452 treat the hardware features as defining the abi. */
1453 if (abi_features
.xlen
== 0)
1455 int xlen_bitsize
= tdesc_register_bitsize (feature_cpu
, "pc");
1456 features
.xlen
= (xlen_bitsize
/ 8);
1457 features
.fputype
= abi_features
.fputype
;
1458 abi_features
= features
;
1460 if (abi_features
.fputype
== 0)
1462 features
.xlen
= abi_features
.xlen
;
1463 features
.fputype
= DOUBLE_FLOAT
;
1464 abi_features
= features
;
1467 /* Find a candidate among the list of pre-declared architectures. */
1468 for (arches
= gdbarch_list_lookup_by_info (arches
, &info
);
1470 arches
= gdbarch_list_lookup_by_info (arches
->next
, &info
))
1472 /* Check that the feature set of the ARCHES matches the feature set
1473 we are looking for. If it doesn't then we can't reuse this
1475 loongarch_gdbarch_tdep
*candidate_tdep
1476 = gdbarch_tdep
<loongarch_gdbarch_tdep
> (arches
->gdbarch
);
1478 if (candidate_tdep
->abi_features
!= abi_features
)
1484 if (arches
!= nullptr)
1485 return arches
->gdbarch
;
1487 /* None found, so create a new architecture from the information provided. */
1488 struct gdbarch
*gdbarch
= gdbarch_alloc (&info
, tdep
);
1489 tdep
->abi_features
= abi_features
;
1491 /* Target data types. */
1492 set_gdbarch_short_bit (gdbarch
, 16);
1493 set_gdbarch_int_bit (gdbarch
, 32);
1494 set_gdbarch_long_bit (gdbarch
, info
.bfd_arch_info
->bits_per_address
);
1495 set_gdbarch_long_long_bit (gdbarch
, 64);
1496 set_gdbarch_float_bit (gdbarch
, 32);
1497 set_gdbarch_double_bit (gdbarch
, 64);
1498 set_gdbarch_long_double_bit (gdbarch
, 128);
1499 set_gdbarch_long_double_format (gdbarch
, floatformats_ieee_quad
);
1500 set_gdbarch_ptr_bit (gdbarch
, info
.bfd_arch_info
->bits_per_address
);
1501 set_gdbarch_char_signed (gdbarch
, 0);
1503 info
.target_desc
= tdesc
;
1504 info
.tdesc_data
= tdesc_data
.get ();
1506 /* Information about registers. */
1507 set_gdbarch_num_regs (gdbarch
, regnum
);
1508 set_gdbarch_sp_regnum (gdbarch
, LOONGARCH_SP_REGNUM
);
1509 set_gdbarch_pc_regnum (gdbarch
, LOONGARCH_PC_REGNUM
);
1511 /* Finalise the target description registers. */
1512 tdesc_use_registers (gdbarch
, tdesc
, std::move (tdesc_data
));
1514 /* Functions handling dummy frames. */
1515 set_gdbarch_push_dummy_call (gdbarch
, loongarch_push_dummy_call
);
1517 /* Return value info */
1518 set_gdbarch_return_value (gdbarch
, loongarch_return_value
);
1520 /* Advance PC across function entry code. */
1521 set_gdbarch_skip_prologue (gdbarch
, loongarch_skip_prologue
);
1523 /* Stack grows downward. */
1524 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
1527 set_gdbarch_frame_align (gdbarch
, loongarch_frame_align
);
1529 /* Breakpoint manipulation. */
1530 set_gdbarch_software_single_step (gdbarch
, loongarch_software_single_step
);
1531 set_gdbarch_breakpoint_kind_from_pc (gdbarch
, loongarch_breakpoint::kind_from_pc
);
1532 set_gdbarch_sw_breakpoint_from_kind (gdbarch
, loongarch_breakpoint::bp_from_kind
);
1534 /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder. */
1535 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, loongarch_dwarf2_reg_to_regnum
);
1536 dwarf2_append_unwinders (gdbarch
);
1537 frame_unwind_append_unwinder (gdbarch
, &loongarch_frame_unwind
);
1539 /* Hook in OS ABI-specific overrides, if they have been registered. */
1540 gdbarch_init_osabi (info
, gdbarch
);
1545 void _initialize_loongarch_tdep ();
1547 _initialize_loongarch_tdep ()
1549 gdbarch_register (bfd_arch_loongarch
, loongarch_gdbarch_init
, nullptr);