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