c65e2414bf8a35b120498adb338983a73abe914f
[binutils-gdb.git] / gdb / loongarch-tdep.c
1 /* Target-dependent code for the LoongArch architecture, for GDB.
2
3 Copyright (C) 2022-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "dwarf2/frame.h"
23 #include "elf-bfd.h"
24 #include "frame-unwind.h"
25 #include "gdbcore.h"
26 #include "loongarch-tdep.h"
27 #include "reggroups.h"
28 #include "target.h"
29 #include "target-descriptions.h"
30 #include "trad-frame.h"
31 #include "user-regs.h"
32
33 /* Fetch the instruction at PC. */
34
35 static insn_t
36 loongarch_fetch_instruction (CORE_ADDR pc)
37 {
38 size_t insn_len = loongarch_insn_length (0);
39 gdb_byte buf[insn_len];
40 int err;
41
42 err = target_read_memory (pc, buf, insn_len);
43 if (err)
44 memory_error (TARGET_XFER_E_IO, pc);
45
46 return extract_unsigned_integer (buf, insn_len, BFD_ENDIAN_LITTLE);
47 }
48
49 /* Return TRUE if INSN is a unconditional branch instruction, otherwise return FALSE. */
50
51 static bool
52 loongarch_insn_is_uncond_branch (insn_t insn)
53 {
54 if ((insn & 0xfc000000) == 0x4c000000 /* jirl */
55 || (insn & 0xfc000000) == 0x50000000 /* b */
56 || (insn & 0xfc000000) == 0x54000000) /* bl */
57 return true;
58 return false;
59 }
60
61 /* Return TRUE if INSN is a conditional branch instruction, otherwise return FALSE. */
62
63 static bool
64 loongarch_insn_is_cond_branch (insn_t insn)
65 {
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 */
74 return true;
75 return false;
76 }
77
78 /* Return TRUE if INSN is a branch instruction, otherwise return FALSE. */
79
80 static bool
81 loongarch_insn_is_branch (insn_t insn)
82 {
83 bool is_uncond = loongarch_insn_is_uncond_branch (insn);
84 bool is_cond = loongarch_insn_is_cond_branch (insn);
85
86 return (is_uncond || is_cond);
87 }
88
89 /* Return TRUE if INSN is a Load Linked instruction, otherwise return FALSE. */
90
91 static bool
92 loongarch_insn_is_ll (insn_t insn)
93 {
94 if ((insn & 0xff000000) == 0x20000000 /* ll.w */
95 || (insn & 0xff000000) == 0x22000000) /* ll.d */
96 return true;
97 return false;
98 }
99
100 /* Return TRUE if INSN is a Store Conditional instruction, otherwise return FALSE. */
101
102 static bool
103 loongarch_insn_is_sc (insn_t insn)
104 {
105 if ((insn & 0xff000000) == 0x21000000 /* sc.w */
106 || (insn & 0xff000000) == 0x23000000) /* sc.d */
107 return true;
108 return false;
109 }
110
111 /* Analyze the function prologue from START_PC to LIMIT_PC.
112 Return the address of the first instruction past the prologue. */
113
114 static CORE_ADDR
115 loongarch_scan_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc,
116 CORE_ADDR limit_pc, frame_info_ptr this_frame,
117 struct trad_frame_cache *this_cache)
118 {
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};
124
125 while (cur_pc < limit_pc)
126 {
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);
134
135 if ((insn & 0xffc00000) == 0x02c00000 /* addi.d sp,sp,si12 */
136 && rd == sp && rj == sp && si12 < 0)
137 {
138 prologue_end = cur_pc + insn_len;
139 }
140 else if ((insn & 0xffc00000) == 0x02c00000 /* addi.d fp,sp,si12 */
141 && rd == fp && rj == sp && si12 > 0)
142 {
143 prologue_end = cur_pc + insn_len;
144 }
145 else if ((insn & 0xffc00000) == 0x29c00000 /* st.d rd,sp,si12 */
146 && rj == sp)
147 {
148 prologue_end = cur_pc + insn_len;
149 }
150 else if ((insn & 0xff000000) == 0x27000000 /* stptr.d rd,sp,si14 */
151 && rj == sp)
152 {
153 prologue_end = cur_pc + insn_len;
154 }
155 else if ((insn & 0xfe000000) == 0x14000000) /* lu12i.w rd,si20 */
156 {
157 reg_value[rd] = si20 << 12;
158 reg_used[rd] = 1;
159 }
160 else if ((insn & 0xffc00000) == 0x03800000) /* ori rd,rj,si12 */
161 {
162 if (reg_used[rj])
163 {
164 reg_value[rd] = reg_value[rj] | (si12 & 0xfff);
165 reg_used[rd] = 1;
166 }
167 }
168 else if ((insn & 0xffff8000) == 0x00108000 /* add.d sp,sp,rk */
169 && rd == sp && rj == sp)
170 {
171 if (reg_used[rk] == 1 && reg_value[rk] < 0)
172 {
173 prologue_end = cur_pc + insn_len;
174 break;
175 }
176 }
177 else if (loongarch_insn_is_branch (insn))
178 {
179 break;
180 }
181
182 cur_pc += insn_len;
183 }
184
185 if (prologue_end == 0)
186 prologue_end = cur_pc;
187
188 return prologue_end;
189 }
190
191 /* Implement the loongarch_skip_prologue gdbarch method. */
192
193 static CORE_ADDR
194 loongarch_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
195 {
196 CORE_ADDR func_addr;
197
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
200 is greater. */
201 if (find_pc_partial_function (pc, nullptr, &func_addr, nullptr))
202 {
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);
207 }
208
209 /* Can't determine prologue from the symbol table, need to examine
210 instructions. */
211
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);
216 if (limit_pc == 0)
217 limit_pc = pc + 100; /* Arbitrary large number. */
218
219 return loongarch_scan_prologue (gdbarch, pc, limit_pc, nullptr, nullptr);
220 }
221
222 /* Decode the current instruction and determine the address of the
223 next instruction. */
224
225 static CORE_ADDR
226 loongarch_next_pc (struct regcache *regcache, CORE_ADDR cur_pc)
227 {
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;
233
234 if ((insn & 0xfc000000) == 0x4c000000) /* jirl rd, rj, offs16 */
235 {
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);
239 }
240 else if ((insn & 0xfc000000) == 0x50000000 /* b offs26 */
241 || (insn & 0xfc000000) == 0x54000000) /* bl offs26 */
242 {
243 next_pc = cur_pc + loongarch_decode_imm ("0:10|10:16<<2", insn, 1);
244 }
245 else if ((insn & 0xfc000000) == 0x58000000) /* beq rj, rd, offs16 */
246 {
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));
251 if (rj == rd)
252 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
253 }
254 else if ((insn & 0xfc000000) == 0x5c000000) /* bne rj, rd, offs16 */
255 {
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));
260 if (rj != rd)
261 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
262 }
263 else if ((insn & 0xfc000000) == 0x60000000) /* blt rj, rd, offs16 */
264 {
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));
269 if (rj < rd)
270 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
271 }
272 else if ((insn & 0xfc000000) == 0x64000000) /* bge rj, rd, offs16 */
273 {
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));
278 if (rj >= rd)
279 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
280 }
281 else if ((insn & 0xfc000000) == 0x68000000) /* bltu rj, rd, offs16 */
282 {
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));
287 if (rj < rd)
288 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
289 }
290 else if ((insn & 0xfc000000) == 0x6c000000) /* bgeu rj, rd, offs16 */
291 {
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));
296 if (rj >= rd)
297 next_pc = cur_pc + loongarch_decode_imm ("10:16<<2", insn, 1);
298 }
299 else if ((insn & 0xfc000000) == 0x40000000) /* beqz rj, offs21 */
300 {
301 LONGEST rj = regcache_raw_get_signed (regcache,
302 loongarch_decode_imm ("5:5", insn, 0));
303 if (rj == 0)
304 next_pc = cur_pc + loongarch_decode_imm ("0:5|10:16<<2", insn, 1);
305 }
306 else if ((insn & 0xfc000000) == 0x44000000) /* bnez rj, offs21 */
307 {
308 LONGEST rj = regcache_raw_get_signed (regcache,
309 loongarch_decode_imm ("5:5", insn, 0));
310 if (rj != 0)
311 next_pc = cur_pc + loongarch_decode_imm ("0:5|10:16<<2", insn, 1);
312 }
313 else if ((insn & 0xffff8000) == 0x002b0000) /* syscall */
314 {
315 if (tdep->syscall_next_pc != nullptr)
316 next_pc = tdep->syscall_next_pc (get_current_frame ());
317 }
318
319 return next_pc;
320 }
321
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. */
324
325 static std::vector<CORE_ADDR>
326 loongarch_deal_with_atomic_sequence (struct regcache *regcache, CORE_ADDR cur_pc)
327 {
328 CORE_ADDR next_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;
334
335 /* Look for a Load Linked instruction which begins the atomic sequence. */
336 if (!loongarch_insn_is_ll (insn))
337 return {};
338
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)
341 {
342 cur_pc += insn_len;
343 insn = loongarch_fetch_instruction (cur_pc);
344
345 /* Look for a unconditional branch instruction, fallback to the standard code. */
346 if (loongarch_insn_is_uncond_branch (insn))
347 {
348 return {};
349 }
350 /* Look for a conditional branch instruction, put a breakpoint in its destination address. */
351 else if (loongarch_insn_is_cond_branch (insn))
352 {
353 next_pc = loongarch_next_pc (regcache, cur_pc);
354 next_pcs.push_back (next_pc);
355 }
356 /* Look for a Store Conditional instruction which closes the atomic sequence. */
357 else if (loongarch_insn_is_sc (insn))
358 {
359 found_atomic_sequence_endpoint = true;
360 next_pc = cur_pc + insn_len;
361 next_pcs.push_back (next_pc);
362 break;
363 }
364 }
365
366 /* We didn't find a closing Store Conditional instruction, fallback to the standard code. */
367 if (!found_atomic_sequence_endpoint)
368 return {};
369
370 return next_pcs;
371 }
372
373 /* Implement the software_single_step gdbarch method */
374
375 static std::vector<CORE_ADDR>
376 loongarch_software_single_step (struct regcache *regcache)
377 {
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);
381
382 if (!next_pcs.empty ())
383 return next_pcs;
384
385 CORE_ADDR next_pc = loongarch_next_pc (regcache, cur_pc);
386
387 return {next_pc};
388 }
389
390 /* Callback function for user_reg_add. */
391
392 static struct value *
393 value_of_loongarch_user_reg (frame_info_ptr frame, const void *baton)
394 {
395 return value_of_register ((long long) baton, frame);
396 }
397
398 /* Implement the frame_align gdbarch method. */
399
400 static CORE_ADDR
401 loongarch_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
402 {
403 return align_down (addr, 16);
404 }
405
406 /* Generate, or return the cached frame cache for frame unwinder. */
407
408 static struct trad_frame_cache *
409 loongarch_frame_cache (frame_info_ptr this_frame, void **this_cache)
410 {
411 struct trad_frame_cache *cache;
412 CORE_ADDR pc;
413
414 if (*this_cache != nullptr)
415 return (struct trad_frame_cache *) *this_cache;
416
417 cache = trad_frame_cache_zalloc (this_frame);
418 *this_cache = cache;
419
420 trad_frame_set_reg_realreg (cache, LOONGARCH_PC_REGNUM, LOONGARCH_RA_REGNUM);
421
422 pc = get_frame_address_in_block (this_frame);
423 trad_frame_set_id (cache, frame_id_build_unavailable_stack (pc));
424
425 return cache;
426 }
427
428 /* Implement the this_id callback for frame unwinder. */
429
430 static void
431 loongarch_frame_this_id (frame_info_ptr this_frame, void **prologue_cache,
432 struct frame_id *this_id)
433 {
434 struct trad_frame_cache *info;
435
436 info = loongarch_frame_cache (this_frame, prologue_cache);
437 trad_frame_get_id (info, this_id);
438 }
439
440 /* Implement the prev_register callback for frame unwinder. */
441
442 static struct value *
443 loongarch_frame_prev_register (frame_info_ptr this_frame,
444 void **prologue_cache, int regnum)
445 {
446 struct trad_frame_cache *info;
447
448 info = loongarch_frame_cache (this_frame, prologue_cache);
449 return trad_frame_get_register (info, this_frame, regnum);
450 }
451
452 static const struct frame_unwind loongarch_frame_unwind = {
453 "loongarch prologue",
454 /*.type =*/NORMAL_FRAME,
455 /*.stop_reason =*/default_frame_unwind_stop_reason,
456 /*.this_id =*/loongarch_frame_this_id,
457 /*.prev_register =*/loongarch_frame_prev_register,
458 /*.unwind_data =*/nullptr,
459 /*.sniffer =*/default_frame_sniffer,
460 /*.dealloc_cache =*/nullptr,
461 /*.prev_arch =*/nullptr,
462 };
463
464 /* Write the contents of buffer VAL into the general-purpose argument
465 register defined by GAR in REGCACHE. GAR indicates the available
466 general-purpose argument registers which should be a value in the
467 range 1 to 8 (LOONGARCH_ARG_REGNUM), which correspond to registers
468 a7 and a0 respectively, that is to say, regnum is a7 if GAR is 1,
469 regnum is a6 if GAR is 2, regnum is a5 if GAR is 3, regnum is a4
470 if GAR is 4, regnum is a3 if GAR is 5, regnum is a2 if GAR is 6,
471 regnum is a1 if GAR is 7, regnum is a0 if GAR is 8. */
472
473 static void
474 pass_in_gar (struct regcache *regcache, unsigned int gar, const gdb_byte *val)
475 {
476 unsigned int regnum = LOONGARCH_ARG_REGNUM - gar + LOONGARCH_A0_REGNUM;
477 regcache->cooked_write (regnum, val);
478 }
479
480 /* Write the contents of buffer VAL into the floating-point argument
481 register defined by FAR in REGCACHE. FAR indicates the available
482 floating-point argument registers which should be a value in the
483 range 1 to 8 (LOONGARCH_ARG_REGNUM), which correspond to registers
484 f7 and f0 respectively, that is to say, regnum is f7 if FAR is 1,
485 regnum is f6 if FAR is 2, regnum is f5 if FAR is 3, regnum is f4
486 if FAR is 4, regnum is f3 if FAR is 5, regnum is f2 if FAR is 6,
487 regnum is f1 if FAR is 7, regnum is f0 if FAR is 8. */
488
489 static void
490 pass_in_far (struct regcache *regcache, unsigned int far, const gdb_byte *val)
491 {
492 unsigned int regnum = LOONGARCH_ARG_REGNUM - far + LOONGARCH_FIRST_FP_REGNUM;
493 regcache->cooked_write (regnum, val);
494 }
495
496 /* Pass a value on the stack. */
497
498 static void
499 pass_on_stack (struct regcache *regcache, const gdb_byte *val,
500 size_t len, int align, gdb_byte **addr)
501 {
502 align = align_up (align, 8);
503 if (align > 16)
504 align = 16;
505
506 CORE_ADDR align_addr = (CORE_ADDR) (*addr);
507 align_addr = align_up (align_addr, align);
508 *addr = (gdb_byte *) align_addr;
509 memcpy (*addr, val, len);
510 *addr += len;
511 }
512
513 /* Compute the numbers of struct member. */
514
515 static void
516 compute_struct_member (struct type *type,
517 unsigned int *fixed_point_members,
518 unsigned int *floating_point_members,
519 bool *first_member_is_fixed_point,
520 bool *has_long_double)
521 {
522 for (int i = 0; i < type->num_fields (); i++)
523 {
524 /* Ignore any static fields. */
525 if (type->field (i).is_static ())
526 continue;
527
528 struct type *field_type = check_typedef (type->field (i).type ());
529
530 if ((field_type->code () == TYPE_CODE_FLT
531 && field_type->length () == 16)
532 || (field_type->code () == TYPE_CODE_COMPLEX
533 && field_type->length () == 32))
534 *has_long_double = true;
535
536 if (field_type->code () == TYPE_CODE_INT
537 || field_type->code () == TYPE_CODE_BOOL
538 || field_type->code () == TYPE_CODE_CHAR
539 || field_type->code () == TYPE_CODE_RANGE
540 || field_type->code () == TYPE_CODE_ENUM
541 || field_type->code () == TYPE_CODE_PTR)
542 {
543 (*fixed_point_members)++;
544
545 if (*floating_point_members == 0)
546 *first_member_is_fixed_point = true;
547 }
548 else if (field_type->code () == TYPE_CODE_FLT)
549 (*floating_point_members)++;
550 else if (field_type->code () == TYPE_CODE_STRUCT)
551 compute_struct_member (field_type,
552 fixed_point_members,
553 floating_point_members,
554 first_member_is_fixed_point,
555 has_long_double);
556 else if (field_type->code () == TYPE_CODE_COMPLEX)
557 (*floating_point_members) += 2;
558 }
559 }
560
561 /* Compute the lengths and offsets of struct member. */
562
563 static void
564 struct_member_info (struct type *type,
565 unsigned int *member_offsets,
566 unsigned int *member_lens,
567 unsigned int offset,
568 unsigned int *fields)
569 {
570 unsigned int count = type->num_fields ();
571 unsigned int i;
572
573 for (i = 0; i < count; ++i)
574 {
575 if (type->field (i).loc_kind () != FIELD_LOC_KIND_BITPOS)
576 continue;
577
578 struct type *field_type = check_typedef (type->field (i).type ());
579 int field_offset
580 = offset + type->field (i).loc_bitpos () / TARGET_CHAR_BIT;
581
582 switch (field_type->code ())
583 {
584 case TYPE_CODE_STRUCT:
585 struct_member_info (field_type, member_offsets, member_lens,
586 field_offset, fields);
587 break;
588
589 case TYPE_CODE_COMPLEX:
590 if (*fields == 0)
591 {
592 /* _Complex float */
593 if (field_type->length () == 8)
594 {
595 member_offsets[0] = field_offset;
596 member_offsets[1] = field_offset + 4;
597 member_lens[0] = member_lens[1] = 4;
598 *fields = 2;
599 }
600 /* _Complex double */
601 else if (field_type->length () == 16)
602 {
603 member_offsets[0] = field_offset;
604 member_offsets[1] = field_offset + 8;
605 member_lens[0] = member_lens[1] = 8;
606 *fields = 2;
607 }
608 }
609 break;
610
611 default:
612 if (*fields < 2)
613 {
614 member_offsets[*fields] = field_offset;
615 member_lens[*fields] = field_type->length ();
616 }
617 (*fields)++;
618 break;
619 }
620
621 /* only has special handling for structures with 1 or 2 fields. */
622 if (*fields > 2)
623 return;
624 }
625 }
626
627 /* Implement the push_dummy_call gdbarch method. */
628
629 static CORE_ADDR
630 loongarch_push_dummy_call (struct gdbarch *gdbarch,
631 struct value *function,
632 struct regcache *regcache,
633 CORE_ADDR bp_addr,
634 int nargs,
635 struct value **args,
636 CORE_ADDR sp,
637 function_call_return_method return_method,
638 CORE_ADDR struct_addr)
639 {
640 int regsize = register_size (gdbarch, 0);
641 unsigned int gar = LOONGARCH_ARG_REGNUM;
642 unsigned int far = LOONGARCH_ARG_REGNUM;
643 unsigned int fixed_point_members;
644 unsigned int floating_point_members;
645 bool first_member_is_fixed_point;
646 bool has_long_double;
647 unsigned int member_offsets[2];
648 unsigned int member_lens[2];
649 unsigned int fields;
650 gdb_byte buf[1024] = { 0 };
651 gdb_byte *addr = buf;
652
653 if (return_method != return_method_normal)
654 pass_in_gar (regcache, gar--, (gdb_byte *) &struct_addr);
655
656 for (int i = 0; i < nargs; i++)
657 {
658 struct value *arg = args[i];
659 const gdb_byte *val = arg->contents ().data ();
660 struct type *type = check_typedef (arg->type ());
661 size_t len = type->length ();
662 int align = type_align (type);
663 enum type_code code = type->code ();
664 struct type *func_type = check_typedef (function->type ());
665 bool varargs = (func_type->has_varargs () && i >= func_type->num_fields ());
666
667 switch (code)
668 {
669 case TYPE_CODE_INT:
670 case TYPE_CODE_BOOL:
671 case TYPE_CODE_CHAR:
672 case TYPE_CODE_RANGE:
673 case TYPE_CODE_ENUM:
674 case TYPE_CODE_PTR:
675 {
676 /* integer or pointer type is passed in GAR.
677 If no GAR is available, it's passed on the stack.
678 When passed in registers or on the stack,
679 the unsigned integer scalars are zero-extended to GRLEN bits,
680 and the signed integer scalars are sign-extended. */
681 if (type->is_unsigned ())
682 {
683 ULONGEST data = extract_unsigned_integer (val, len, BFD_ENDIAN_LITTLE);
684 if (gar > 0)
685 pass_in_gar (regcache, gar--, (gdb_byte *) &data);
686 else
687 pass_on_stack (regcache, (gdb_byte *) &data, len, align, &addr);
688 }
689 else
690 {
691 LONGEST data = extract_signed_integer (val, len, BFD_ENDIAN_LITTLE);
692 if (gar > 0)
693 pass_in_gar (regcache, gar--, (gdb_byte *) &data);
694 else
695 pass_on_stack (regcache, (gdb_byte *) &data, len, align, &addr);
696 }
697 }
698 break;
699 case TYPE_CODE_FLT:
700 if (len == 2 * regsize)
701 {
702 if (!varargs)
703 {
704 /* long double type is passed in a pair of GAR,
705 with the low-order GRLEN bits in the lower-numbered register
706 and the high-order GRLEN bits in the higher-numbered register.
707 If exactly one register is available,
708 the low-order GRLEN bits are passed in the register
709 and the high-order GRLEN bits are passed on the stack.
710 If no GAR is available, it's passed on the stack. */
711 if (gar >= 2)
712 {
713 pass_in_gar (regcache, gar--, val);
714 pass_in_gar (regcache, gar--, val + regsize);
715 }
716 else if (gar == 1)
717 {
718 pass_in_gar (regcache, gar--, val);
719 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
720 }
721 else
722 {
723 pass_on_stack (regcache, val, len, align, &addr);
724 }
725 }
726 else
727 {
728 /* Variadic arguments are passed in GARs
729 in the same manner as named arguments.
730 And after a variadic argument has been passed on the stack,
731 all future arguments will also be passed on the stack,
732 i.e., the last argument register may be left unused
733 due to the aligned register pair rule.
734 long double data type is passed in an aligned GAR pair,
735 the first register in the pair is even-numbered. */
736 if (gar >= 2)
737 {
738 if (gar % 2 == 0)
739 {
740 pass_in_gar (regcache, gar--, val);
741 pass_in_gar (regcache, gar--, val + regsize);
742 }
743 else
744 {
745 gar--;
746 pass_in_gar (regcache, gar--, val);
747 pass_in_gar (regcache, gar--, val + regsize);
748 }
749 }
750 else if (gar == 1)
751 {
752 gar--;
753 pass_on_stack (regcache, val, len, align, &addr);
754 }
755 else
756 {
757 pass_on_stack (regcache, val, len, align, &addr);
758 }
759 }
760 }
761 else
762 {
763 /* The other floating-point type is passed in FAR.
764 If no FAR is available, it's passed in GAR.
765 If no GAR is available, it's passed on the stack. */
766 if (!varargs && far > 0)
767 pass_in_far (regcache, far--, val);
768 else if (gar > 0)
769 pass_in_gar (regcache, gar--, val);
770 else
771 pass_on_stack (regcache, val, len, align, &addr);
772 }
773 break;
774 case TYPE_CODE_STRUCT:
775 {
776 fixed_point_members = 0;
777 floating_point_members = 0;
778 first_member_is_fixed_point = false;
779 has_long_double = false;
780 member_offsets[0] = member_offsets[1] = 0;
781 member_lens[0] = member_offsets[1] = 0;
782 fields = 0;
783 compute_struct_member (type,
784 &fixed_point_members,
785 &floating_point_members,
786 &first_member_is_fixed_point,
787 &has_long_double);
788 struct_member_info (type, member_offsets, member_lens, 0, &fields);
789 /* If the structure consists of one floating-point member within
790 FRLEN bits wide, it is passed in an FAR if available. If the
791 structure consists of two floating-point members both within
792 FRLEN bits wide, it is passed in two FARs if available. If the
793 structure consists of one integer member within GRLEN bits wide
794 and one floating-point member within FRLEN bits wide, it is
795 passed in a GAR and an FAR if available. */
796 if (has_long_double == false
797 && ((fixed_point_members == 0 && floating_point_members == 1
798 && far >= 1)
799 || (fixed_point_members == 0 && floating_point_members == 2
800 && far >= 2)
801 || (fixed_point_members == 1 && floating_point_members == 1
802 && far >= 1 && gar >= 1)))
803 {
804 if (fixed_point_members == 0 && floating_point_members == 1)
805 {
806 pass_in_far (regcache, far--, val + member_offsets[0]);
807 }
808 else if (fixed_point_members == 0 && floating_point_members == 2)
809 {
810 pass_in_far (regcache, far--, val + member_offsets[0]);
811 pass_in_far (regcache, far--, val + member_offsets[1]);
812 }
813 else if (fixed_point_members == 1 && floating_point_members == 1)
814 {
815 if (first_member_is_fixed_point == false)
816 {
817 pass_in_far (regcache, far--, val + member_offsets[0]);
818 pass_in_gar (regcache, gar--, val + member_offsets[1]);
819 }
820 else
821 {
822 pass_in_gar (regcache, gar--, val + member_offsets[0]);
823 pass_in_far (regcache, far--, val + member_offsets[1]);
824 }
825 }
826 }
827 else if (len > 0 && len <= regsize)
828 {
829 /* The structure has only fixed-point members. */
830 if (fixed_point_members > 0 && floating_point_members == 0)
831 {
832 /* If there is an available GAR,
833 the structure is passed through the GAR by value passing;
834 If no GAR is available, it's passed on the stack. */
835 if (gar > 0)
836 pass_in_gar (regcache, gar--, val);
837 else
838 pass_on_stack (regcache, val, len, align, &addr);
839 }
840 /* The structure has only floating-point members. */
841 else if (fixed_point_members == 0 && floating_point_members > 0)
842 {
843 /* The structure has one floating-point member.
844 The argument is passed in a FAR.
845 If no FAR is available, the value is passed in a GAR.
846 if no GAR is available, the value is passed on the stack. */
847 if (floating_point_members == 1)
848 {
849 if (!varargs && far > 0)
850 pass_in_far (regcache, far--, val);
851 else if (gar > 0)
852 pass_in_gar (regcache, gar--, val);
853 else
854 pass_on_stack (regcache, val, len, align, &addr);
855 }
856 /* The structure has two floating-point members.
857 The argument is passed in a pair of available FAR,
858 with the low-order float member bits in the lower-numbered FAR
859 and the high-order float member bits in the higher-numbered FAR.
860 If the number of available FAR is less than 2, it's passed in a GAR,
861 and passed on the stack if no GAR is available. */
862 else if (floating_point_members == 2)
863 {
864 if (!varargs && far >= 2)
865 {
866 pass_in_far (regcache, far--, val);
867 pass_in_far (regcache, far--, val + align);
868 }
869 else if (gar > 0)
870 {
871 pass_in_gar (regcache, gar--, val);
872 }
873 else
874 {
875 pass_on_stack (regcache, val, len, align, &addr);
876 }
877 }
878 }
879 /* The structure has both fixed-point and floating-point members. */
880 else if (fixed_point_members > 0 && floating_point_members > 0)
881 {
882 /* The structure has one float member and multiple fixed-point members.
883 If there are available GAR, the structure is passed in a GAR,
884 and passed on the stack if no GAR is available. */
885 if (floating_point_members == 1 && fixed_point_members > 1)
886 {
887 if (gar > 0)
888 pass_in_gar (regcache, gar--, val);
889 else
890 pass_on_stack (regcache, val, len, align, &addr);
891 }
892 /* The structure has one float member and one fixed-point member.
893 If one FAR and one GAR are available,
894 the floating-point member of the structure is passed in the FAR,
895 and the fixed-point member of the structure is passed in the GAR.
896 If no floating-point register but one GAR is available, it's passed in GAR;
897 If no GAR is available, it's passed on the stack. */
898 else if (floating_point_members == 1 && fixed_point_members == 1)
899 {
900 if (!varargs && far > 0 && gar > 0)
901 {
902 if (first_member_is_fixed_point == false)
903 {
904 pass_in_far (regcache, far--, val);
905 pass_in_gar (regcache, gar--, val + align);
906 }
907 else
908 {
909 pass_in_gar (regcache, gar--, val);
910 pass_in_far (regcache, far--, val + align);
911 }
912 }
913 else
914 {
915 if (gar > 0)
916 pass_in_gar (regcache, gar--, val);
917 else
918 pass_on_stack (regcache, val, len, align, &addr);
919 }
920 }
921 }
922 }
923 else if (len > regsize && len <= 2 * regsize)
924 {
925 /* The structure has only fixed-point members. */
926 if (fixed_point_members > 0 && floating_point_members == 0)
927 {
928 /* The argument is passed in a pair of available GAR,
929 with the low-order bits in the lower-numbered GAR
930 and the high-order bits in the higher-numbered GAR.
931 If only one GAR is available,
932 the low-order bits are in the GAR
933 and the high-order bits are on the stack,
934 and passed on the stack if no GAR is available. */
935 if (gar >= 2)
936 {
937 pass_in_gar (regcache, gar--, val);
938 pass_in_gar (regcache, gar--, val + regsize);
939 }
940 else if (gar == 1)
941 {
942 pass_in_gar (regcache, gar--, val);
943 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
944 }
945 else
946 {
947 pass_on_stack (regcache, val, len, align, &addr);
948 }
949 }
950 /* The structure has only floating-point members. */
951 else if (fixed_point_members == 0 && floating_point_members > 0)
952 {
953 /* The structure has one long double member
954 or one double member and two adjacent float members
955 or 3-4 float members.
956 The argument is passed in a pair of available GAR,
957 with the low-order bits in the lower-numbered GAR
958 and the high-order bits in the higher-numbered GAR.
959 If only one GAR is available,
960 the low-order bits are in the GAR
961 and the high-order bits are on the stack,
962 and passed on the stack if no GAR is available. */
963 if ((len == 16 && floating_point_members == 1)
964 || (len == 16 && floating_point_members == 3)
965 || (len == 12 && floating_point_members == 3)
966 || (len == 16 && floating_point_members == 4))
967 {
968 if (gar >= 2)
969 {
970 pass_in_gar (regcache, gar--, val);
971 pass_in_gar (regcache, gar--, val + regsize);
972 }
973 else if (gar == 1)
974 {
975 if (!varargs)
976 {
977 pass_in_gar (regcache, gar--, val);
978 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
979 }
980 else
981 {
982 gar--;
983 pass_on_stack (regcache, val, len, align, &addr);
984 }
985 }
986 else
987 {
988 pass_on_stack (regcache, val, len, align, &addr);
989 }
990 }
991 /* The structure has two double members
992 or one double member and one float member.
993 The argument is passed in a pair of available FAR,
994 with the low-order bits in the lower-numbered FAR
995 and the high-order bits in the higher-numbered FAR.
996 If no a pair of available FAR,
997 it's passed in a pair of available GAR,
998 with the low-order bits in the lower-numbered GAR
999 and the high-order bits in the higher-numbered GAR.
1000 If only one GAR is available,
1001 the low-order bits are in the GAR
1002 and the high-order bits are on stack,
1003 and passed on the stack if no GAR is available. */
1004 else if ((len == 16 && floating_point_members == 2)
1005 || (len == 12 && floating_point_members == 2))
1006 {
1007 if (!varargs && far >= 2)
1008 {
1009 pass_in_far (regcache, far--, val);
1010 pass_in_far (regcache, far--, val + regsize);
1011 }
1012 else if (gar >= 2)
1013 {
1014 pass_in_gar (regcache, gar--, val);
1015 pass_in_gar (regcache, gar--, val + regsize);
1016 }
1017 else if (gar == 1)
1018 {
1019 pass_in_gar (regcache, gar--, val);
1020 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
1021 }
1022 else
1023 {
1024 pass_on_stack (regcache, val, len, align, &addr);
1025 }
1026 }
1027 }
1028 /* The structure has both fixed-point and floating-point members. */
1029 else if (fixed_point_members > 0 && floating_point_members > 0)
1030 {
1031 /* The structure has one floating-point member and one fixed-point member. */
1032 if (floating_point_members == 1 && fixed_point_members == 1)
1033 {
1034 /* If one FAR and one GAR are available,
1035 the floating-point member of the structure is passed in the FAR,
1036 and the fixed-point member of the structure is passed in the GAR;
1037 If no floating-point registers but two GARs are available,
1038 it's passed in the two GARs;
1039 If only one GAR is available,
1040 the low-order bits are in the GAR
1041 and the high-order bits are on the stack;
1042 And it's passed on the stack if no GAR is available. */
1043 if (!varargs && far > 0 && gar > 0)
1044 {
1045 if (first_member_is_fixed_point == false)
1046 {
1047 pass_in_far (regcache, far--, val);
1048 pass_in_gar (regcache, gar--, val + regsize);
1049 }
1050 else
1051 {
1052 pass_in_gar (regcache, gar--, val);
1053 pass_in_far (regcache, far--, val + regsize);
1054 }
1055 }
1056 else if ((!varargs && far == 0 && gar >= 2) || (varargs && gar >= 2))
1057 {
1058 pass_in_gar (regcache, gar--, val);
1059 pass_in_gar (regcache, gar--, val + regsize);
1060 }
1061 else if ((!varargs && far == 0 && gar == 1) || (varargs && gar == 1))
1062 {
1063 pass_in_gar (regcache, gar--, val);
1064 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
1065 }
1066 else if ((!varargs && far == 0 && gar == 0) || (varargs && gar == 0))
1067 {
1068 pass_on_stack (regcache, val, len, align, &addr);
1069 }
1070 }
1071 else
1072 {
1073 /* The argument is passed in a pair of available GAR,
1074 with the low-order bits in the lower-numbered GAR
1075 and the high-order bits in the higher-numbered GAR.
1076 If only one GAR is available,
1077 the low-order bits are in the GAR
1078 and the high-order bits are on the stack,
1079 and passed on the stack if no GAR is available. */
1080 if (gar >= 2)
1081 {
1082 pass_in_gar (regcache, gar--, val);
1083 pass_in_gar (regcache, gar--, val + regsize);
1084 }
1085 else if (gar == 1)
1086 {
1087 pass_in_gar (regcache, gar--, val);
1088 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
1089 }
1090 else
1091 {
1092 pass_on_stack (regcache, val, len, align, &addr);
1093 }
1094 }
1095 }
1096 }
1097 else if (len > 2 * regsize)
1098 {
1099 /* It's passed by reference and are replaced in the argument list with the address.
1100 If there is an available GAR, the reference is passed in the GAR,
1101 and passed on the stack if no GAR is available. */
1102 sp = align_down (sp - len, 16);
1103 write_memory (sp, val, len);
1104
1105 if (gar > 0)
1106 pass_in_gar (regcache, gar--, (const gdb_byte *) &sp);
1107 else
1108 pass_on_stack (regcache, (const gdb_byte*) &sp, len, regsize, &addr);
1109 }
1110 }
1111 break;
1112 case TYPE_CODE_UNION:
1113 /* Union is passed in GAR or stack. */
1114 if (len > 0 && len <= regsize)
1115 {
1116 /* The argument is passed in a GAR,
1117 or on the stack by value if no GAR is available. */
1118 if (gar > 0)
1119 pass_in_gar (regcache, gar--, val);
1120 else
1121 pass_on_stack (regcache, val, len, align, &addr);
1122 }
1123 else if (len > regsize && len <= 2 * regsize)
1124 {
1125 /* The argument is passed in a pair of available GAR,
1126 with the low-order bits in the lower-numbered GAR
1127 and the high-order bits in the higher-numbered GAR.
1128 If only one GAR is available,
1129 the low-order bits are in the GAR
1130 and the high-order bits are on the stack.
1131 The arguments are passed on the stack when no GAR is available. */
1132 if (gar >= 2)
1133 {
1134 pass_in_gar (regcache, gar--, val);
1135 pass_in_gar (regcache, gar--, val + regsize);
1136 }
1137 else if (gar == 1)
1138 {
1139 pass_in_gar (regcache, gar--, val);
1140 pass_on_stack (regcache, val + regsize, len - regsize, align, &addr);
1141 }
1142 else
1143 {
1144 pass_on_stack (regcache, val, len, align, &addr);
1145 }
1146 }
1147 else if (len > 2 * regsize)
1148 {
1149 /* It's passed by reference and are replaced in the argument list with the address.
1150 If there is an available GAR, the reference is passed in the GAR,
1151 and passed on the stack if no GAR is available. */
1152 sp = align_down (sp - len, 16);
1153 write_memory (sp, val, len);
1154
1155 if (gar > 0)
1156 pass_in_gar (regcache, gar--, (const gdb_byte *) &sp);
1157 else
1158 pass_on_stack (regcache, (const gdb_byte*) &sp, len, regsize, &addr);
1159 }
1160 break;
1161 case TYPE_CODE_COMPLEX:
1162 {
1163 struct type *target_type = check_typedef (type->target_type ());
1164 size_t target_len = target_type->length ();
1165
1166 if (target_len < regsize)
1167 {
1168 /* The complex with two float members
1169 is passed in a pair of available FAR,
1170 with the low-order float member bits in the lower-numbered FAR
1171 and the high-order float member bits in the higher-numbered FAR.
1172 If the number of available FAR is less than 2, it's passed in a GAR,
1173 and passed on the stack if no GAR is available. */
1174 if (!varargs && far >= 2)
1175 {
1176 pass_in_far (regcache, far--, val);
1177 pass_in_far (regcache, far--, val + align);
1178 }
1179 else if (gar > 0)
1180 {
1181 pass_in_gar (regcache, gar--, val);
1182 }
1183 else
1184 {
1185 pass_on_stack (regcache, val, len, align, &addr);
1186 }
1187 }
1188 else if (target_len == regsize)
1189 {
1190 /* The complex with two double members
1191 is passed in a pair of available FAR,
1192 with the low-order bits in the lower-numbered FAR
1193 and the high-order bits in the higher-numbered FAR.
1194 If no a pair of available FAR,
1195 it's passed in a pair of available GAR,
1196 with the low-order bits in the lower-numbered GAR
1197 and the high-order bits in the higher-numbered GAR.
1198 If only one GAR is available,
1199 the low-order bits are in the GAR
1200 and the high-order bits are on stack,
1201 and passed on the stack if no GAR is available. */
1202 {
1203 if (!varargs && far >= 2)
1204 {
1205 pass_in_far (regcache, far--, val);
1206 pass_in_far (regcache, far--, val + align);
1207 }
1208 else if (gar >= 2)
1209 {
1210 pass_in_gar (regcache, gar--, val);
1211 pass_in_gar (regcache, gar--, val + align);
1212 }
1213 else if (gar == 1)
1214 {
1215 pass_in_gar (regcache, gar--, val);
1216 pass_on_stack (regcache, val + align, len - align, align, &addr);
1217 }
1218 else
1219 {
1220 pass_on_stack (regcache, val, len, align, &addr);
1221 }
1222 }
1223 }
1224 else if (target_len == 2 * regsize)
1225 {
1226 /* The complex with two long double members
1227 is passed by reference and are replaced in the argument list with the address.
1228 If there is an available GAR, the reference is passed in the GAR,
1229 and passed on the stack if no GAR is available. */
1230 sp = align_down (sp - len, 16);
1231 write_memory (sp, val, len);
1232
1233 if (gar > 0)
1234 pass_in_gar (regcache, gar--, (const gdb_byte *) &sp);
1235 else
1236 pass_on_stack (regcache, (const gdb_byte*) &sp, regsize, regsize, &addr);
1237 }
1238 }
1239 break;
1240 default:
1241 break;
1242 }
1243 }
1244
1245 if (addr > buf)
1246 {
1247 sp -= addr - buf;
1248 sp = align_down (sp, 16);
1249 write_memory (sp, buf, addr - buf);
1250 }
1251
1252 regcache_cooked_write_unsigned (regcache, LOONGARCH_RA_REGNUM, bp_addr);
1253 regcache_cooked_write_unsigned (regcache, LOONGARCH_SP_REGNUM, sp);
1254
1255 return sp;
1256 }
1257
1258 /* Partial transfer of a cooked register. */
1259
1260 static void
1261 loongarch_xfer_reg (struct regcache *regcache,
1262 int regnum, int len, gdb_byte *readbuf,
1263 const gdb_byte *writebuf, size_t offset)
1264 {
1265 if (readbuf)
1266 regcache->cooked_read_part (regnum, 0, len, readbuf + offset);
1267 if (writebuf)
1268 regcache->cooked_write_part (regnum, 0, len, writebuf + offset);
1269 }
1270
1271 /* Implement the return_value gdbarch method. */
1272
1273 static enum return_value_convention
1274 loongarch_return_value (struct gdbarch *gdbarch, struct value *function,
1275 struct type *type, struct regcache *regcache,
1276 gdb_byte *readbuf, const gdb_byte *writebuf)
1277 {
1278 int regsize = register_size (gdbarch, 0);
1279 enum type_code code = type->code ();
1280 size_t len = type->length ();
1281 unsigned int fixed_point_members;
1282 unsigned int floating_point_members;
1283 bool first_member_is_fixed_point;
1284 bool has_long_double;
1285 unsigned int member_offsets[2];
1286 unsigned int member_lens[2];
1287 unsigned int fields;
1288 int a0 = LOONGARCH_A0_REGNUM;
1289 int a1 = LOONGARCH_A0_REGNUM + 1;
1290 int f0 = LOONGARCH_FIRST_FP_REGNUM;
1291 int f1 = LOONGARCH_FIRST_FP_REGNUM + 1;
1292
1293 switch (code)
1294 {
1295 case TYPE_CODE_INT:
1296 case TYPE_CODE_BOOL:
1297 case TYPE_CODE_CHAR:
1298 case TYPE_CODE_RANGE:
1299 case TYPE_CODE_ENUM:
1300 case TYPE_CODE_PTR:
1301 {
1302 /* integer or pointer type.
1303 The return value is passed in a0,
1304 the unsigned integer scalars are zero-extended to GRLEN bits,
1305 and the signed integer scalars are sign-extended. */
1306 if (writebuf)
1307 {
1308 gdb_byte buf[regsize];
1309 if (type->is_unsigned ())
1310 {
1311 ULONGEST data = extract_unsigned_integer (writebuf, len, BFD_ENDIAN_LITTLE);
1312 store_unsigned_integer (buf, regsize, BFD_ENDIAN_LITTLE, data);
1313 }
1314 else
1315 {
1316 LONGEST data = extract_signed_integer (writebuf, len, BFD_ENDIAN_LITTLE);
1317 store_signed_integer (buf, regsize, BFD_ENDIAN_LITTLE, data);
1318 }
1319 loongarch_xfer_reg (regcache, a0, regsize, nullptr, buf, 0);
1320 }
1321 else
1322 loongarch_xfer_reg (regcache, a0, len, readbuf, nullptr, 0);
1323 }
1324 break;
1325 case TYPE_CODE_FLT:
1326 /* long double type.
1327 The return value is passed in a0 and a1. */
1328 if (len == 2 * regsize)
1329 {
1330 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1331 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1332 }
1333 /* float or double type.
1334 The return value is passed in f0. */
1335 else
1336 {
1337 loongarch_xfer_reg (regcache, f0, len, readbuf, writebuf, 0);
1338 }
1339 break;
1340 case TYPE_CODE_STRUCT:
1341 {
1342 fixed_point_members = 0;
1343 floating_point_members = 0;
1344 first_member_is_fixed_point = false;
1345 has_long_double = false;
1346 member_offsets[0] = member_offsets[1] = 0;
1347 member_lens[0] = member_offsets[1] = 0;
1348 fields = 0;
1349 compute_struct_member (type,
1350 &fixed_point_members,
1351 &floating_point_members,
1352 &first_member_is_fixed_point,
1353 &has_long_double);
1354 struct_member_info (type, member_offsets, member_lens, 0, &fields);
1355 /* struct consists of one floating-point member;
1356 struct consists of two floating-point members;
1357 struct consists of one floating-point member
1358 and one integer member. */
1359 if (has_long_double == false
1360 && ((fixed_point_members == 0 && floating_point_members == 1)
1361 || (fixed_point_members == 0 && floating_point_members == 2)
1362 || (fixed_point_members == 1 && floating_point_members == 1)))
1363 {
1364 if (fixed_point_members == 0 && floating_point_members == 1)
1365 {
1366 loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
1367 writebuf, member_offsets[0]);
1368 }
1369 else if (fixed_point_members == 0 && floating_point_members == 2)
1370 {
1371 loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
1372 writebuf, member_offsets[0]);
1373 loongarch_xfer_reg (regcache, f1, member_lens[1], readbuf,
1374 writebuf, member_offsets[1]);
1375 }
1376 else if (fixed_point_members == 1 && floating_point_members == 1)
1377 {
1378 if (first_member_is_fixed_point == false)
1379 {
1380 loongarch_xfer_reg (regcache, f0, member_lens[0], readbuf,
1381 writebuf, member_offsets[0]);
1382 loongarch_xfer_reg (regcache, a0, member_lens[1], readbuf,
1383 writebuf, member_offsets[1]);
1384 }
1385 else
1386 {
1387 loongarch_xfer_reg (regcache, a0, member_lens[0], readbuf,
1388 writebuf, member_offsets[0]);
1389 loongarch_xfer_reg (regcache, f0, member_lens[1], readbuf,
1390 writebuf, member_offsets[1]);
1391 }
1392 }
1393 }
1394 else if (len > 0 && len <= regsize)
1395 {
1396 /* The structure has only fixed-point members. */
1397 if (fixed_point_members > 0 && floating_point_members == 0)
1398 {
1399 /* The return value is passed in a0. */
1400 loongarch_xfer_reg (regcache, a0, len, readbuf, writebuf, 0);
1401 }
1402 /* The structure has only floating-point members. */
1403 else if (fixed_point_members == 0 && floating_point_members > 0)
1404 {
1405 /* The structure has one floating-point member.
1406 The return value is passed in f0. */
1407 if (floating_point_members == 1)
1408 {
1409 loongarch_xfer_reg (regcache, f0, len, readbuf, writebuf, 0);
1410 }
1411 /* The structure has two floating-point members.
1412 The return value is passed in f0 and f1. */
1413 else if (floating_point_members == 2)
1414 {
1415 loongarch_xfer_reg (regcache, f0, len / 2, readbuf, writebuf, 0);
1416 loongarch_xfer_reg (regcache, f1, len / 2, readbuf, writebuf, len / 2);
1417 }
1418 }
1419 /* The structure has both fixed-point and floating-point members. */
1420 else if (fixed_point_members > 0 && floating_point_members > 0)
1421 {
1422 /* The structure has one float member and multiple fixed-point members.
1423 The return value is passed in a0. */
1424 if (floating_point_members == 1 && fixed_point_members > 1)
1425 {
1426 loongarch_xfer_reg (regcache, a0, len, readbuf, writebuf, 0);
1427 }
1428 /* The structure has one float member and one fixed-point member. */
1429 else if (floating_point_members == 1 && fixed_point_members == 1)
1430 {
1431 /* The return value is passed in f0 and a0 if the first member is floating-point. */
1432 if (first_member_is_fixed_point == false)
1433 {
1434 loongarch_xfer_reg (regcache, f0, regsize / 2, readbuf, writebuf, 0);
1435 loongarch_xfer_reg (regcache, a0, regsize / 2, readbuf, writebuf, regsize / 2);
1436 }
1437 /* The return value is passed in a0 and f0 if the first member is fixed-point. */
1438 else
1439 {
1440 loongarch_xfer_reg (regcache, a0, regsize / 2, readbuf, writebuf, 0);
1441 loongarch_xfer_reg (regcache, f0, regsize / 2, readbuf, writebuf, regsize / 2);
1442 }
1443 }
1444 }
1445 }
1446 else if (len > regsize && len <= 2 * regsize)
1447 {
1448 /* The structure has only fixed-point members. */
1449 if (fixed_point_members > 0 && floating_point_members == 0)
1450 {
1451 /* The return value is passed in a0 and a1. */
1452 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1453 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1454 }
1455 /* The structure has only floating-point members. */
1456 else if (fixed_point_members == 0 && floating_point_members > 0)
1457 {
1458 /* The structure has one long double member
1459 or one double member and two adjacent float members
1460 or 3-4 float members.
1461 The return value is passed in a0 and a1. */
1462 if ((len == 16 && floating_point_members == 1)
1463 || (len == 16 && floating_point_members == 3)
1464 || (len == 12 && floating_point_members == 3)
1465 || (len == 16 && floating_point_members == 4))
1466 {
1467 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1468 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1469 }
1470 /* The structure has two double members
1471 or one double member and one float member.
1472 The return value is passed in f0 and f1. */
1473 else if ((len == 16 && floating_point_members == 2)
1474 || (len == 12 && floating_point_members == 2))
1475 {
1476 loongarch_xfer_reg (regcache, f0, regsize, readbuf, writebuf, 0);
1477 loongarch_xfer_reg (regcache, f1, len - regsize, readbuf, writebuf, regsize);
1478 }
1479 }
1480 /* The structure has both fixed-point and floating-point members. */
1481 else if (fixed_point_members > 0 && floating_point_members > 0)
1482 {
1483 /* The structure has one floating-point member and one fixed-point member. */
1484 if (floating_point_members == 1 && fixed_point_members == 1)
1485 {
1486 /* The return value is passed in f0 and a0 if the first member is floating-point. */
1487 if (first_member_is_fixed_point == false)
1488 {
1489 loongarch_xfer_reg (regcache, f0, regsize, readbuf, writebuf, 0);
1490 loongarch_xfer_reg (regcache, a0, len - regsize, readbuf, writebuf, regsize);
1491 }
1492 /* The return value is passed in a0 and f0 if the first member is fixed-point. */
1493 else
1494 {
1495 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1496 loongarch_xfer_reg (regcache, f0, len - regsize, readbuf, writebuf, regsize);
1497 }
1498 }
1499 else
1500 {
1501 /* The return value is passed in a0 and a1. */
1502 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1503 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1504 }
1505 }
1506 }
1507 else if (len > 2 * regsize)
1508 return RETURN_VALUE_STRUCT_CONVENTION;
1509 }
1510 break;
1511 case TYPE_CODE_UNION:
1512 if (len > 0 && len <= regsize)
1513 {
1514 /* The return value is passed in a0. */
1515 loongarch_xfer_reg (regcache, a0, len, readbuf, writebuf, 0);
1516 }
1517 else if (len > regsize && len <= 2 * regsize)
1518 {
1519 /* The return value is passed in a0 and a1. */
1520 loongarch_xfer_reg (regcache, a0, regsize, readbuf, writebuf, 0);
1521 loongarch_xfer_reg (regcache, a1, len - regsize, readbuf, writebuf, regsize);
1522 }
1523 else if (len > 2 * regsize)
1524 return RETURN_VALUE_STRUCT_CONVENTION;
1525 break;
1526 case TYPE_CODE_COMPLEX:
1527 if (len > 0 && len <= 2 * regsize)
1528 {
1529 /* The return value is passed in f0 and f1. */
1530 loongarch_xfer_reg (regcache, f0, len / 2, readbuf, writebuf, 0);
1531 loongarch_xfer_reg (regcache, f1, len / 2, readbuf, writebuf, len / 2);
1532 }
1533 else if (len > 2 * regsize)
1534 return RETURN_VALUE_STRUCT_CONVENTION;
1535 break;
1536 default:
1537 break;
1538 }
1539
1540 return RETURN_VALUE_REGISTER_CONVENTION;
1541 }
1542
1543 /* Implement the dwarf2_reg_to_regnum gdbarch method. */
1544
1545 static int
1546 loongarch_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int regnum)
1547 {
1548 if (regnum >= 0 && regnum < 32)
1549 return regnum;
1550 else if (regnum >= 32 && regnum < 66)
1551 return LOONGARCH_FIRST_FP_REGNUM + regnum - 32;
1552 else
1553 return -1;
1554 }
1555
1556 static constexpr gdb_byte loongarch_default_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
1557 typedef BP_MANIPULATION (loongarch_default_breakpoint) loongarch_breakpoint;
1558
1559 /* Extract a set of required target features out of ABFD. If ABFD is nullptr
1560 then a LOONGARCH_GDBARCH_FEATURES is returned in its default state. */
1561
1562 static struct loongarch_gdbarch_features
1563 loongarch_features_from_bfd (const bfd *abfd)
1564 {
1565 struct loongarch_gdbarch_features features;
1566
1567 /* Now try to improve on the defaults by looking at the binary we are
1568 going to execute. We assume the user knows what they are doing and
1569 that the target will match the binary. Remember, this code path is
1570 only used at all if the target hasn't given us a description, so this
1571 is really a last ditched effort to do something sane before giving
1572 up. */
1573 if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1574 {
1575 unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
1576 int e_flags = elf_elfheader (abfd)->e_flags;
1577
1578 if (eclass == ELFCLASS32)
1579 features.xlen = 4;
1580 else if (eclass == ELFCLASS64)
1581 features.xlen = 8;
1582 else
1583 internal_error (_("unknown ELF header class %d"), eclass);
1584
1585 if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
1586 features.fputype = SINGLE_FLOAT;
1587 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
1588 features.fputype = DOUBLE_FLOAT;
1589 }
1590
1591 return features;
1592 }
1593
1594 /* Find a suitable default target description. Use the contents of INFO,
1595 specifically the bfd object being executed, to guide the selection of a
1596 suitable default target description. */
1597
1598 static const struct target_desc *
1599 loongarch_find_default_target_description (const struct gdbarch_info info)
1600 {
1601 /* Extract desired feature set from INFO. */
1602 struct loongarch_gdbarch_features features
1603 = loongarch_features_from_bfd (info.abfd);
1604
1605 /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
1606 maybe there was no bfd object. In this case we fall back to a minimal
1607 useful target, the x-register size is selected based on the architecture
1608 from INFO. */
1609 if (features.xlen == 0)
1610 features.xlen = info.bfd_arch_info->bits_per_address == 32 ? 4 : 8;
1611
1612 /* If the FPUTYPE 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 usual useful
1614 target with double float. */
1615 if (features.fputype == 0)
1616 features.fputype = DOUBLE_FLOAT;
1617
1618 /* Now build a target description based on the feature set. */
1619 return loongarch_lookup_target_description (features);
1620 }
1621
1622 static int
1623 loongarch_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1624 const struct reggroup *group)
1625 {
1626 if (gdbarch_register_name (gdbarch, regnum) == NULL
1627 || *gdbarch_register_name (gdbarch, regnum) == '\0')
1628 return 0;
1629
1630 int raw_p = regnum < gdbarch_num_regs (gdbarch);
1631
1632 if (group == save_reggroup || group == restore_reggroup)
1633 return raw_p;
1634
1635 if (group == all_reggroup)
1636 return 1;
1637
1638 if (0 <= regnum && regnum <= LOONGARCH_BADV_REGNUM)
1639 return group == general_reggroup;
1640
1641 /* Only ORIG_A0, PC, BADV in general_reggroup */
1642 if (group == general_reggroup)
1643 return 0;
1644
1645 if (LOONGARCH_FIRST_FP_REGNUM <= regnum && regnum <= LOONGARCH_FCSR_REGNUM)
1646 return group == float_reggroup;
1647
1648 /* Only $fx / $fccx / $fcsr in float_reggroup */
1649 if (group == float_reggroup)
1650 return 0;
1651
1652 int ret = tdesc_register_in_reggroup_p (gdbarch, regnum, group);
1653 if (ret != -1)
1654 return ret;
1655
1656 return default_register_reggroup_p (gdbarch, regnum, group);
1657 }
1658
1659 /* Initialize the current architecture based on INFO */
1660
1661 static struct gdbarch *
1662 loongarch_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1663 {
1664 size_t regnum = 0;
1665 struct loongarch_gdbarch_features features;
1666 tdesc_arch_data_up tdesc_data = tdesc_data_alloc ();
1667 const struct target_desc *tdesc = info.target_desc;
1668
1669 /* Ensure we always have a target description. */
1670 if (!tdesc_has_registers (tdesc))
1671 tdesc = loongarch_find_default_target_description (info);
1672
1673 const struct tdesc_feature *feature_cpu
1674 = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.base");
1675 if (feature_cpu == nullptr)
1676 return nullptr;
1677
1678
1679 /* Validate the description provides the mandatory base registers
1680 and allocate their numbers. */
1681 bool valid_p = true;
1682 for (int i = 0; i < 32; i++)
1683 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++,
1684 loongarch_r_normal_name[i] + 1);
1685 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++, "orig_a0");
1686 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++, "pc");
1687 valid_p &= tdesc_numbered_register (feature_cpu, tdesc_data.get (), regnum++, "badv");
1688 if (!valid_p)
1689 return nullptr;
1690
1691 const struct tdesc_feature *feature_fpu
1692 = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.fpu");
1693 if (feature_fpu == nullptr)
1694 return nullptr;
1695
1696 /* Validate the description provides the fpu registers and
1697 allocate their numbers. */
1698 regnum = LOONGARCH_FIRST_FP_REGNUM;
1699 for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++)
1700 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data.get (), regnum++,
1701 loongarch_f_normal_name[i] + 1);
1702 for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++)
1703 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data.get (), regnum++,
1704 loongarch_c_normal_name[i] + 1);
1705 valid_p &= tdesc_numbered_register (feature_fpu, tdesc_data.get (), regnum++, "fcsr");
1706 if (!valid_p)
1707 return nullptr;
1708
1709 /* LoongArch code is always little-endian. */
1710 info.byte_order_for_code = BFD_ENDIAN_LITTLE;
1711
1712 /* Have a look at what the supplied (if any) bfd object requires of the
1713 target, then check that this matches with what the target is
1714 providing. */
1715 struct loongarch_gdbarch_features abi_features
1716 = loongarch_features_from_bfd (info.abfd);
1717
1718 /* If the ABI_FEATURES xlen or fputype is 0 then this indicates we got
1719 no useful abi features from the INFO object. In this case we just
1720 treat the hardware features as defining the abi. */
1721 if (abi_features.xlen == 0)
1722 {
1723 int xlen_bitsize = tdesc_register_bitsize (feature_cpu, "pc");
1724 features.xlen = (xlen_bitsize / 8);
1725 features.fputype = abi_features.fputype;
1726 abi_features = features;
1727 }
1728 if (abi_features.fputype == 0)
1729 {
1730 features.xlen = abi_features.xlen;
1731 features.fputype = DOUBLE_FLOAT;
1732 abi_features = features;
1733 }
1734
1735 /* Find a candidate among the list of pre-declared architectures. */
1736 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1737 arches != nullptr;
1738 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1739 {
1740 /* Check that the feature set of the ARCHES matches the feature set
1741 we are looking for. If it doesn't then we can't reuse this
1742 gdbarch. */
1743 loongarch_gdbarch_tdep *candidate_tdep
1744 = gdbarch_tdep<loongarch_gdbarch_tdep> (arches->gdbarch);
1745
1746 if (candidate_tdep->abi_features != abi_features)
1747 continue;
1748
1749 break;
1750 }
1751
1752 if (arches != nullptr)
1753 return arches->gdbarch;
1754
1755 /* None found, so create a new architecture from the information provided. */
1756 gdbarch *gdbarch
1757 = gdbarch_alloc (&info, gdbarch_tdep_up (new loongarch_gdbarch_tdep));
1758 loongarch_gdbarch_tdep *tdep = gdbarch_tdep<loongarch_gdbarch_tdep> (gdbarch);
1759
1760 tdep->abi_features = abi_features;
1761
1762 /* Target data types. */
1763 set_gdbarch_short_bit (gdbarch, 16);
1764 set_gdbarch_int_bit (gdbarch, 32);
1765 set_gdbarch_long_bit (gdbarch, info.bfd_arch_info->bits_per_address);
1766 set_gdbarch_long_long_bit (gdbarch, 64);
1767 set_gdbarch_float_bit (gdbarch, 32);
1768 set_gdbarch_double_bit (gdbarch, 64);
1769 set_gdbarch_long_double_bit (gdbarch, 128);
1770 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_quad);
1771 set_gdbarch_ptr_bit (gdbarch, info.bfd_arch_info->bits_per_address);
1772 set_gdbarch_char_signed (gdbarch, 0);
1773
1774 info.target_desc = tdesc;
1775 info.tdesc_data = tdesc_data.get ();
1776
1777 for (int i = 0; i < ARRAY_SIZE (loongarch_r_lp64_name); ++i)
1778 if (loongarch_r_lp64_name[i][0] != '\0')
1779 user_reg_add (gdbarch, loongarch_r_lp64_name[i] + 1,
1780 value_of_loongarch_user_reg, (void *) (size_t) i);
1781
1782 for (int i = 0; i < ARRAY_SIZE (loongarch_f_lp64_name); ++i)
1783 {
1784 if (loongarch_f_lp64_name[i][0] != '\0')
1785 user_reg_add (gdbarch, loongarch_f_lp64_name[i] + 1,
1786 value_of_loongarch_user_reg,
1787 (void *) (size_t) (LOONGARCH_FIRST_FP_REGNUM + i));
1788 }
1789
1790 /* Information about registers. */
1791 set_gdbarch_num_regs (gdbarch, regnum);
1792 set_gdbarch_sp_regnum (gdbarch, LOONGARCH_SP_REGNUM);
1793 set_gdbarch_pc_regnum (gdbarch, LOONGARCH_PC_REGNUM);
1794
1795 /* Finalise the target description registers. */
1796 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
1797
1798 /* Functions handling dummy frames. */
1799 set_gdbarch_push_dummy_call (gdbarch, loongarch_push_dummy_call);
1800
1801 /* Return value info */
1802 set_gdbarch_return_value (gdbarch, loongarch_return_value);
1803
1804 /* Advance PC across function entry code. */
1805 set_gdbarch_skip_prologue (gdbarch, loongarch_skip_prologue);
1806
1807 /* Stack grows downward. */
1808 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1809
1810 /* Frame info. */
1811 set_gdbarch_frame_align (gdbarch, loongarch_frame_align);
1812
1813 /* Breakpoint manipulation. */
1814 set_gdbarch_software_single_step (gdbarch, loongarch_software_single_step);
1815 set_gdbarch_breakpoint_kind_from_pc (gdbarch, loongarch_breakpoint::kind_from_pc);
1816 set_gdbarch_sw_breakpoint_from_kind (gdbarch, loongarch_breakpoint::bp_from_kind);
1817
1818 /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder. */
1819 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, loongarch_dwarf2_reg_to_regnum);
1820 dwarf2_append_unwinders (gdbarch);
1821 frame_unwind_append_unwinder (gdbarch, &loongarch_frame_unwind);
1822
1823 /* Hook in OS ABI-specific overrides, if they have been registered. */
1824 gdbarch_init_osabi (info, gdbarch);
1825 set_gdbarch_register_reggroup_p (gdbarch, loongarch_register_reggroup_p);
1826
1827 return gdbarch;
1828 }
1829
1830 void _initialize_loongarch_tdep ();
1831 void
1832 _initialize_loongarch_tdep ()
1833 {
1834 gdbarch_register (bfd_arch_loongarch, loongarch_gdbarch_init, nullptr);
1835 }