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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "rcc/TableGen/Parser.h"
#include "rcc/Support/YAML.h"
#include <algorithm>
#include <array>
#include <charconv>
#include <cstdint>
#include <limits>
#include <optional>
#include <ranges>
#include <span>
#include <string>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <vector>
namespace rcc::targetgen {
namespace {
class ASTParser final {
public:
explicit ASTParser(const yaml::Stream &Input) : Input(Input) {}
[[nodiscard]] std::optional<DescriptionAST> parse() {
const std::span<const yaml::Document> Documents = Input.documents();
if (Documents.size() != 1) {
fail(Input.source().range(0, Input.source().text().size()),
"target description must contain exactly one YAML document");
return std::nullopt;
}
const yaml::Node *Root = Input.node(Documents.front().Root);
if (!requireMapping(Root, "target description root"))
return std::nullopt;
if (!checkKeys(*Root,
{"schema", "target", "register_units", "registers", "register_banks",
"register_classes", "register_masks", "mir_operand_flags", "named_operands",
"immediate_predicates", "scheduling_classes", "encoding_categories",
"assembly_formats", "calling_conventions", "instructions"},
"target description root")) {
return std::nullopt;
}
const yaml::Node *SchemaNode = requiredNode(*Root, "schema", "target description root");
if (SchemaNode == nullptr)
return std::nullopt;
const auto Schema = scalar(*SchemaNode, "target description schema");
if (!Schema || *Schema != "rcc-target-description-v2") {
if (Schema)
fail(SchemaNode->range(), "unsupported target description schema");
return std::nullopt;
}
DescriptionAST Result;
const yaml::Node *TargetNode = requiredNode(*Root, "target", "target description root");
if (TargetNode == nullptr || !parseTarget(*TargetNode, Result.Target))
return std::nullopt;
const yaml::Node *UnitsNode = requiredNode(*Root, "register_units", "target description root");
const yaml::Node *RegistersNode = requiredNode(*Root, "registers", "target description root");
const yaml::Node *BanksNode = requiredNode(*Root, "register_banks", "target description root");
const yaml::Node *ClassesNode =
requiredNode(*Root, "register_classes", "target description root");
const yaml::Node *MasksNode = requiredNode(*Root, "register_masks", "target description root");
const yaml::Node *MIROperandFlagsNode = optionalNode(*Root, "mir_operand_flags");
const yaml::Node *NamedOperandsNode =
requiredNode(*Root, "named_operands", "target description root");
const yaml::Node *ImmediatePredicatesNode =
requiredNode(*Root, "immediate_predicates", "target description root");
const yaml::Node *SchedulingClassesNode =
requiredNode(*Root, "scheduling_classes", "target description root");
const yaml::Node *EncodingCategoriesNode =
requiredNode(*Root, "encoding_categories", "target description root");
const yaml::Node *AssemblyFormatsNode =
requiredNode(*Root, "assembly_formats", "target description root");
const yaml::Node *CallingConventionsNode =
requiredNode(*Root, "calling_conventions", "target description root");
const yaml::Node *InstructionsNode =
requiredNode(*Root, "instructions", "target description root");
if (UnitsNode == nullptr || RegistersNode == nullptr || BanksNode == nullptr ||
ClassesNode == nullptr || MasksNode == nullptr || NamedOperandsNode == nullptr ||
ImmediatePredicatesNode == nullptr || SchedulingClassesNode == nullptr ||
EncodingCategoriesNode == nullptr || AssemblyFormatsNode == nullptr ||
CallingConventionsNode == nullptr || InstructionsNode == nullptr ||
!parseRegisterUnits(*UnitsNode, Result.RegisterUnits) ||
!parseRegisters(*RegistersNode, Result.Registers) ||
!parseRegisterBanks(*BanksNode, Result.RegisterBanks) ||
!parseRegisterClasses(*ClassesNode, Result.RegisterClasses) ||
!parseRegisterMasks(*MasksNode, Result.RegisterMasks) ||
(MIROperandFlagsNode != nullptr &&
!parseMIROperandFlags(*MIROperandFlagsNode, Result.MIROperandFlags)) ||
!parseNamedOperands(*NamedOperandsNode, Result.NamedOperands) ||
!parseImmediatePredicates(*ImmediatePredicatesNode, Result.ImmediatePredicates) ||
!parseSchedulingClasses(*SchedulingClassesNode, Result.SchedulingClasses) ||
!parseEncodingCategories(*EncodingCategoriesNode, Result.EncodingCategories) ||
!parseAssemblyFormats(*AssemblyFormatsNode, Result.AssemblyFormats) ||
!parseCallingConventions(*CallingConventionsNode, Result.CallingConventions) ||
!parseInstructions(*InstructionsNode, Result.Instructions)) {
return std::nullopt;
}
return Result;
}
[[nodiscard]] std::vector<TextDiagnostic> takeDiagnostics() {
return std::move(Diagnostics);
}
private:
void fail(TextRange Range, std::string Message) {
Diagnostics.push_back({
.Severity = DiagnosticSeverity::Error,
.Category = "target-description",
.Message = std::move(Message),
.SourceName = std::string(Input.source().name()),
.Range = Range,
});
}
[[nodiscard]] bool requireMapping(const yaml::Node *Node, std::string_view Context) {
if (Node != nullptr && Node->kind() == yaml::NodeKind::Mapping)
return true;
fail(Node == nullptr ? Input.source().range(0, 0) : Node->range(),
std::string(Context) + " must be a mapping");
return false;
}
[[nodiscard]] bool requireSequence(const yaml::Node *Node, std::string_view Context) {
if (Node != nullptr && Node->kind() == yaml::NodeKind::Sequence)
return true;
fail(Node == nullptr ? Input.source().range(0, 0) : Node->range(),
std::string(Context) + " must be a sequence");
return false;
}
[[nodiscard]] bool checkKeys(const yaml::Node &Mapping,
std::initializer_list<std::string_view> Allowed,
std::string_view Context) {
for (const yaml::MappingEntry &Entry : Mapping.mapping()) {
const bool IsAllowed =
std::ranges::any_of(Allowed, [&Entry](std::string_view Key) { return Entry.Key == Key; });
if (!IsAllowed) {
fail(Entry.KeyRange, std::string(Context) + " has unknown key '" + Entry.Key + "'");
return false;
}
}
return true;
}
[[nodiscard]] const yaml::Node *requiredNode(const yaml::Node &Mapping, std::string_view Key,
std::string_view Context) {
const std::optional<yaml::NodeID> Value = Mapping.mappingValue(Key);
if (Value) {
const yaml::Node *Node = Input.node(*Value);
if (Node != nullptr)
return Node;
}
fail(Mapping.range(),
std::string(Context) + " is missing required key '" + std::string(Key) + "'");
return nullptr;
}
[[nodiscard]] std::optional<std::string>
requiredScalar(const yaml::Node &Mapping, std::string_view Key, std::string_view Context) {
const yaml::Node *Node = requiredNode(Mapping, Key, Context);
if (Node == nullptr)
return std::nullopt;
return scalar(*Node, std::string(Context) + " key '" + std::string(Key) + "'");
}
[[nodiscard]] std::optional<std::string> scalar(const yaml::Node &Node,
std::string_view Context) {
if (Node.kind() != yaml::NodeKind::Scalar) {
fail(Node.range(), std::string(Context) + " must be a scalar");
return std::nullopt;
}
return std::string(Node.scalar());
}
[[nodiscard]] std::optional<std::uint64_t> unsignedInteger(const yaml::Node &Node,
std::string_view Context) {
const auto Text = scalar(Node, Context);
if (!Text)
return std::nullopt;
std::uint64_t Result = 0;
const auto [End, Error] =
std::from_chars(Text->data(), Text->data() + Text->size(), Result, 10);
if (Error != std::errc{} || End != Text->data() + Text->size()) {
fail(Node.range(), std::string(Context) + " must be an unsigned decimal integer");
return std::nullopt;
}
return Result;
}
[[nodiscard]] std::optional<std::uint32_t> unsignedInteger32(const yaml::Node &Node,
std::string_view Context) {
const auto Value = unsignedInteger(Node, Context);
if (!Value)
return std::nullopt;
if (*Value > std::numeric_limits<std::uint32_t>::max()) {
fail(Node.range(), std::string(Context) + " is outside the uint32 range");
return std::nullopt;
}
return static_cast<std::uint32_t>(*Value);
}
[[nodiscard]] std::optional<std::int64_t> signedInteger(const yaml::Node &Node,
std::string_view Context) {
const auto Text = scalar(Node, Context);
if (!Text)
return std::nullopt;
std::int64_t Result = 0;
const auto [End, Error] =
std::from_chars(Text->data(), Text->data() + Text->size(), Result, 10);
if (Error != std::errc{} || End != Text->data() + Text->size()) {
fail(Node.range(), std::string(Context) + " must be a signed decimal integer");
return std::nullopt;
}
return Result;
}
[[nodiscard]] std::optional<bool> boolean(const yaml::Node &Node, std::string_view Context) {
const auto Text = scalar(Node, Context);
if (!Text)
return std::nullopt;
if (*Text == "true")
return true;
if (*Text == "false")
return false;
fail(Node.range(), std::string(Context) + " must be 'true' or 'false'");
return std::nullopt;
}
[[nodiscard]] std::optional<std::vector<std::string>> scalarSequence(const yaml::Node &Node,
std::string_view Context) {
if (!requireSequence(&Node, Context))
return std::nullopt;
std::vector<std::string> Result;
for (const yaml::NodeID Value : Node.sequence()) {
const yaml::Node *Element = Input.node(Value);
if (Element == nullptr)
return std::nullopt;
const auto Text = scalar(*Element, Context);
if (!Text)
return std::nullopt;
Result.push_back(*Text);
}
return Result;
}
[[nodiscard]] std::optional<std::vector<std::int64_t>>
signedIntegerSequence(const yaml::Node &Node, std::string_view Context) {
if (!requireSequence(&Node, Context))
return std::nullopt;
std::vector<std::int64_t> Result;
for (const yaml::NodeID Value : Node.sequence()) {
const yaml::Node *Element = Input.node(Value);
if (Element == nullptr)
return std::nullopt;
const auto Parsed = signedInteger(*Element, Context);
if (!Parsed)
return std::nullopt;
Result.push_back(*Parsed);
}
return Result;
}
[[nodiscard]] bool parseTarget(const yaml::Node &Node, SourceTarget &Result) {
if (!requireMapping(&Node, "target"))
return false;
if (!checkKeys(Node, {"namespace", "name", "triple", "cpu", "abi", "data_layout", "features"},
"target")) {
return false;
}
const auto Namespace = requiredScalar(Node, "namespace", "target");
const auto Name = requiredScalar(Node, "name", "target");
const auto Triple = requiredScalar(Node, "triple", "target");
const auto CPU = requiredScalar(Node, "cpu", "target");
const auto ABI = requiredScalar(Node, "abi", "target");
const auto DataLayout = requiredScalar(Node, "data_layout", "target");
const yaml::Node *Features = requiredNode(Node, "features", "target");
if (!Namespace || !Name || !Triple || !CPU || !ABI || !DataLayout || Features == nullptr)
return false;
const auto ParsedFeatures = scalarSequence(*Features, "target features");
if (!ParsedFeatures)
return false;
Result = {
.Namespace = *Namespace,
.Name = *Name,
.Triple = *Triple,
.CPU = *CPU,
.ABI = *ABI,
.DataLayout = *DataLayout,
.Features = *ParsedFeatures,
.Range = Node.range(),
};
return true;
}
[[nodiscard]] bool parseRegisterUnits(const yaml::Node &Node,
std::vector<SourceRegisterUnit> &Result) {
if (!requireSequence(&Node, "register_units"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "register unit"))
return false;
if (!checkKeys(*Element, {"id", "symbol", "name"}, "register unit"))
return false;
const yaml::Node *IDNode = requiredNode(*Element, "id", "register unit");
const auto Symbol = requiredScalar(*Element, "symbol", "register unit");
const auto Name = requiredScalar(*Element, "name", "register unit");
if (IDNode == nullptr || !Symbol || !Name)
return false;
const auto ID = unsignedInteger32(*IDNode, "register unit id");
if (!ID)
return false;
Result.push_back({.ID = *ID, .Symbol = *Symbol, .Name = *Name, .Range = Element->range()});
}
return true;
}
[[nodiscard]] std::optional<std::vector<SourceRegisterUnitLane>>
parseRegisterUnitLanes(const yaml::Node &Node) {
if (!requireSequence(&Node, "register units"))
return std::nullopt;
std::vector<SourceRegisterUnitLane> Result;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "register unit lane"))
return std::nullopt;
if (!checkKeys(*Element, {"unit", "lanes"}, "register unit lane"))
return std::nullopt;
const auto Unit = requiredScalar(*Element, "unit", "register unit lane");
const yaml::Node *LanesNode = requiredNode(*Element, "lanes", "register unit lane");
if (!Unit || LanesNode == nullptr)
return std::nullopt;
const auto Lanes = unsignedInteger(*LanesNode, "register unit lane lanes");
if (!Lanes)
return std::nullopt;
Result.push_back({.UnitSymbol = *Unit, .Lanes = *Lanes, .Range = Element->range()});
}
return Result;
}
[[nodiscard]] bool parseRegisters(const yaml::Node &Node, std::vector<SourceRegister> &Result) {
if (!requireSequence(&Node, "registers"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "register"))
return false;
if (!checkKeys(*Element,
{"id", "symbol", "name", "encoding", "dwarf_register", "units", "constant",
"artificial", "reserved", "aliases"},
"register")) {
return false;
}
const yaml::Node *IDNode = requiredNode(*Element, "id", "register");
const auto Symbol = requiredScalar(*Element, "symbol", "register");
const auto Name = requiredScalar(*Element, "name", "register");
const yaml::Node *EncodingNode = requiredNode(*Element, "encoding", "register");
const yaml::Node *DwarfNode = requiredNode(*Element, "dwarf_register", "register");
const yaml::Node *UnitsNode = requiredNode(*Element, "units", "register");
if (IDNode == nullptr || !Symbol || !Name || EncodingNode == nullptr ||
DwarfNode == nullptr || UnitsNode == nullptr) {
return false;
}
const auto ID = unsignedInteger32(*IDNode, "register id");
const auto Encoding = unsignedInteger32(*EncodingNode, "register encoding");
const auto DwarfRegister = unsignedInteger32(*DwarfNode, "register DWARF number");
const auto Units = parseRegisterUnitLanes(*UnitsNode);
if (!ID || !Encoding || !DwarfRegister || !Units)
return false;
SourceRegister Register{
.ID = *ID,
.Symbol = *Symbol,
.Name = *Name,
.Encoding = *Encoding,
.DwarfRegister = *DwarfRegister,
.Units = *Units,
.IsConstant = false,
.IsArtificial = false,
.IsReserved = false,
.AlternativeNames = {},
.Range = Element->range(),
};
if (const yaml::Node *Constant = optionalNode(*Element, "constant")) {
const auto Value = boolean(*Constant, "register constant");
if (!Value)
return false;
Register.IsConstant = *Value;
}
if (const yaml::Node *Artificial = optionalNode(*Element, "artificial")) {
const auto Value = boolean(*Artificial, "register artificial");
if (!Value)
return false;
Register.IsArtificial = *Value;
}
if (const yaml::Node *Reserved = optionalNode(*Element, "reserved")) {
const auto Value = boolean(*Reserved, "register reserved");
if (!Value)
return false;
Register.IsReserved = *Value;
}
if (const yaml::Node *Aliases = optionalNode(*Element, "aliases")) {
const auto Values = scalarSequence(*Aliases, "register aliases");
if (!Values)
return false;
Register.AlternativeNames = *Values;
}
Result.push_back(std::move(Register));
}
return true;
}
[[nodiscard]] bool parseRegisterBanks(const yaml::Node &Node,
std::vector<SourceRegisterBank> &Result) {
if (!requireSequence(&Node, "register_banks"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "register bank"))
return false;
if (!checkKeys(*Element, {"id", "symbol", "name", "maximum_size_bits"}, "register bank"))
return false;
const yaml::Node *IDNode = requiredNode(*Element, "id", "register bank");
const auto Symbol = requiredScalar(*Element, "symbol", "register bank");
const auto Name = requiredScalar(*Element, "name", "register bank");
const yaml::Node *SizeNode = requiredNode(*Element, "maximum_size_bits", "register bank");
if (IDNode == nullptr || !Symbol || !Name || SizeNode == nullptr)
return false;
const auto ID = unsignedInteger32(*IDNode, "register bank id");
const auto Size = unsignedInteger32(*SizeNode, "register bank maximum_size_bits");
if (!ID || !Size)
return false;
Result.push_back({.ID = *ID,
.Symbol = *Symbol,
.Name = *Name,
.MaximumSizeInBits = *Size,
.Range = Element->range()});
}
return true;
}
[[nodiscard]] bool parseRegisterClasses(const yaml::Node &Node,
std::vector<SourceRegisterClass> &Result) {
if (!requireSequence(&Node, "register_classes"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "register class"))
return false;
if (!checkKeys(*Element,
{"id", "symbol", "name", "bank", "size_bits", "spill_size_bits",
"spill_alignment_bits", "lanes", "registers", "allocation_order",
"allocatable", "copy_cost"},
"register class")) {
return false;
}
const yaml::Node *IDNode = requiredNode(*Element, "id", "register class");
const auto Symbol = requiredScalar(*Element, "symbol", "register class");
const auto Name = requiredScalar(*Element, "name", "register class");
const auto Bank = requiredScalar(*Element, "bank", "register class");
const yaml::Node *SizeNode = requiredNode(*Element, "size_bits", "register class");
const yaml::Node *SpillSizeNode = requiredNode(*Element, "spill_size_bits", "register class");
const yaml::Node *SpillAlignmentNode =
requiredNode(*Element, "spill_alignment_bits", "register class");
const yaml::Node *LanesNode = requiredNode(*Element, "lanes", "register class");
const yaml::Node *RegistersNode = requiredNode(*Element, "registers", "register class");
const yaml::Node *AllocationNode =
requiredNode(*Element, "allocation_order", "register class");
if (IDNode == nullptr || !Symbol || !Name || !Bank || SizeNode == nullptr ||
SpillSizeNode == nullptr || SpillAlignmentNode == nullptr || LanesNode == nullptr ||
RegistersNode == nullptr || AllocationNode == nullptr) {
return false;
}
const auto ID = unsignedInteger32(*IDNode, "register class id");
const auto Size = unsignedInteger32(*SizeNode, "register class size_bits");
const auto SpillSize = unsignedInteger32(*SpillSizeNode, "register class spill_size_bits");
const auto SpillAlignment =
unsignedInteger32(*SpillAlignmentNode, "register class spill_alignment_bits");
const auto Lanes = unsignedInteger(*LanesNode, "register class lanes");
const auto Registers = scalarSequence(*RegistersNode, "register class registers");
const auto AllocationOrder =
scalarSequence(*AllocationNode, "register class allocation_order");
if (!ID || !Size || !SpillSize || !SpillAlignment || !Lanes || !Registers ||
!AllocationOrder) {
return false;
}
SourceRegisterClass RegisterClass{
.ID = *ID,
.Symbol = *Symbol,
.Name = *Name,
.BankSymbol = *Bank,
.SizeInBits = *Size,
.SpillSizeInBits = *SpillSize,
.SpillAlignmentInBits = *SpillAlignment,
.Lanes = *Lanes,
.RegisterSymbols = *Registers,
.AllocationOrderSymbols = *AllocationOrder,
.Range = Element->range(),
};
if (const yaml::Node *Allocatable = optionalNode(*Element, "allocatable")) {
const auto Value = boolean(*Allocatable, "register class allocatable");
if (!Value)
return false;
RegisterClass.Allocatable = *Value;
}
if (const yaml::Node *CopyCost = optionalNode(*Element, "copy_cost")) {
const auto Value = unsignedInteger32(*CopyCost, "register class copy_cost");
if (!Value)
return false;
RegisterClass.CopyCost = *Value;
}
Result.push_back(std::move(RegisterClass));
}
return true;
}
[[nodiscard]] bool parseRegisterMasks(const yaml::Node &Node,
std::vector<SourceRegisterMask> &Result) {
if (!requireSequence(&Node, "register_masks"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "register mask"))
return false;
if (!checkKeys(*Element, {"name", "preserved"}, "register mask"))
return false;
const auto Name = requiredScalar(*Element, "name", "register mask");
const yaml::Node *PreservedNode = requiredNode(*Element, "preserved", "register mask");
if (!Name || PreservedNode == nullptr)
return false;
const auto Preserved = scalarSequence(*PreservedNode, "register mask preserved");
if (!Preserved)
return false;
Result.push_back(
{.Name = *Name, .PreservedRegisterSymbols = *Preserved, .Range = Element->range()});
}
return true;
}
[[nodiscard]] bool parseMIROperandFlagSequence(const yaml::Node &Node, std::string_view Context,
std::vector<SourceMIROperandFlag> &Result) {
if (!requireSequence(&Node, Context))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, Context) || !checkKeys(*Element, {"name", "value"}, Context)) {
return false;
}
const auto Name = requiredScalar(*Element, "name", Context);
const yaml::Node *ValueNode = requiredNode(*Element, "value", Context);
if (!Name || ValueNode == nullptr)
return false;
const auto Value = unsignedInteger32(*ValueNode, std::string(Context) + " value");
if (!Value)
return false;
Result.push_back({.Name = *Name, .Value = *Value, .Range = Element->range()});
}
return true;
}
[[nodiscard]] bool parseMIROperandFlags(const yaml::Node &Node, SourceMIROperandFlags &Result) {
if (!requireMapping(&Node, "MIR operand flags") ||
!checkKeys(Node, {"direct_mask", "direct", "bitmask"}, "MIR operand flags")) {
return false;
}
const yaml::Node *DirectMaskNode = requiredNode(Node, "direct_mask", "MIR operand flags");
const yaml::Node *DirectNode = requiredNode(Node, "direct", "MIR operand flags");
const yaml::Node *BitmaskNode = requiredNode(Node, "bitmask", "MIR operand flags");
if (DirectMaskNode == nullptr || DirectNode == nullptr || BitmaskNode == nullptr)
return false;
const auto DirectMask = unsignedInteger32(*DirectMaskNode, "MIR operand flag direct_mask");
if (!DirectMask ||
!parseMIROperandFlagSequence(*DirectNode, "direct MIR operand flag", Result.Direct) ||
!parseMIROperandFlagSequence(*BitmaskNode, "bitmask MIR operand flag", Result.Bitmask)) {
return false;
}
Result.DirectMask = *DirectMask;
Result.Range = Node.range();
return true;
}
[[nodiscard]] bool parseNamedOperands(const yaml::Node &Node,
std::vector<SourceNamedOperand> &Result) {
if (!requireSequence(&Node, "named_operands"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "named operand"))
return false;
if (!checkKeys(*Element, {"id", "symbol", "name"}, "named operand"))
return false;
const yaml::Node *IDNode = requiredNode(*Element, "id", "named operand");
const auto Symbol = requiredScalar(*Element, "symbol", "named operand");
const auto Name = requiredScalar(*Element, "name", "named operand");
if (IDNode == nullptr || !Symbol || !Name)
return false;
const auto ID = unsignedInteger32(*IDNode, "named operand id");
if (!ID)
return false;
Result.push_back({.ID = *ID, .Symbol = *Symbol, .Name = *Name, .Range = Element->range()});
}
return true;
}
[[nodiscard]] bool parseImmediatePredicates(const yaml::Node &Node,
std::vector<SourceImmediatePredicate> &Result) {
if (!requireSequence(&Node, "immediate_predicates"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "immediate predicate"))
return false;
if (!checkKeys(
*Element,
{"id", "symbol", "name", "kind", "bits", "alignment", "nonzero", "allowed_values"},
"immediate predicate")) {
return false;
}
const yaml::Node *IDNode = requiredNode(*Element, "id", "immediate predicate");
const auto Symbol = requiredScalar(*Element, "symbol", "immediate predicate");
const auto Name = requiredScalar(*Element, "name", "immediate predicate");
const auto Kind = requiredScalar(*Element, "kind", "immediate predicate");
const yaml::Node *BitsNode = requiredNode(*Element, "bits", "immediate predicate");
if (IDNode == nullptr || !Symbol || !Name || !Kind || BitsNode == nullptr)
return false;
const auto ID = unsignedInteger32(*IDNode, "immediate predicate id");
const auto Bits = unsignedInteger32(*BitsNode, "immediate predicate bits");
if (!ID || !Bits)
return false;
SourceImmediatePredicate Predicate{
.ID = *ID,
.Symbol = *Symbol,
.Name = *Name,
.Kind = *Kind,
.Bits = *Bits,
.Alignment = 1,
.NonZero = false,
.AllowedValues = {},
.Range = Element->range(),
};
if (const yaml::Node *Alignment = optionalNode(*Element, "alignment")) {
const auto Value = unsignedInteger(*Alignment, "immediate predicate alignment");
if (!Value)
return false;
Predicate.Alignment = *Value;
}
if (const yaml::Node *NonZero = optionalNode(*Element, "nonzero")) {
const auto Value = boolean(*NonZero, "immediate predicate nonzero");
if (!Value)
return false;
Predicate.NonZero = *Value;
}
if (const yaml::Node *AllowedValues = optionalNode(*Element, "allowed_values")) {
const auto Values =
signedIntegerSequence(*AllowedValues, "immediate predicate allowed_values");
if (!Values || Values->empty()) {
if (Values)
fail(AllowedValues->range(), "immediate predicate allowed_values must be nonempty");
return false;
}
Predicate.AllowedValues = *Values;
}
Result.push_back(std::move(Predicate));
}
return true;
}
[[nodiscard]] bool parseSchedulingClasses(const yaml::Node &Node,
std::vector<SourceSchedulingClass> &Result) {
if (!requireSequence(&Node, "scheduling_classes"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "scheduling class"))
return false;
if (!checkKeys(*Element, {"id", "symbol", "name", "micro_ops", "latency"},
"scheduling class")) {
return false;
}
const yaml::Node *IDNode = requiredNode(*Element, "id", "scheduling class");
const auto Symbol = requiredScalar(*Element, "symbol", "scheduling class");
const auto Name = requiredScalar(*Element, "name", "scheduling class");
const yaml::Node *MicroOpsNode = requiredNode(*Element, "micro_ops", "scheduling class");
const yaml::Node *LatencyNode = requiredNode(*Element, "latency", "scheduling class");
if (IDNode == nullptr || !Symbol || !Name || MicroOpsNode == nullptr ||
LatencyNode == nullptr)
return false;
const auto ID = unsignedInteger32(*IDNode, "scheduling class id");
const auto MicroOps = unsignedInteger32(*MicroOpsNode, "scheduling class micro_ops");
const auto Latency = unsignedInteger32(*LatencyNode, "scheduling class latency");
if (!ID || !MicroOps || !Latency)
return false;
Result.push_back({
.ID = *ID,
.Symbol = *Symbol,
.Name = *Name,
.MicroOps = *MicroOps,
.Latency = *Latency,
.Range = Element->range(),
});
}
return true;
}
[[nodiscard]] bool parseEncodingCategories(const yaml::Node &Node,
std::vector<SourceEncodingCategory> &Result) {
if (!requireSequence(&Node, "encoding_categories"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "encoding category"))
return false;
if (!checkKeys(*Element, {"id", "symbol", "name", "size_bytes"}, "encoding category"))
return false;
const yaml::Node *IDNode = requiredNode(*Element, "id", "encoding category");
const auto Symbol = requiredScalar(*Element, "symbol", "encoding category");
const auto Name = requiredScalar(*Element, "name", "encoding category");
const yaml::Node *SizeNode = requiredNode(*Element, "size_bytes", "encoding category");
if (IDNode == nullptr || !Symbol || !Name || SizeNode == nullptr)
return false;
const auto ID = unsignedInteger32(*IDNode, "encoding category id");
const auto Size = unsignedInteger32(*SizeNode, "encoding category size_bytes");
if (!ID || !Size)
return false;
Result.push_back({
.ID = *ID,
.Symbol = *Symbol,
.Name = *Name,
.SizeInBytes = *Size,
.Range = Element->range(),
});
}
return true;
}
[[nodiscard]] bool parseAssemblyFormats(const yaml::Node &Node,
std::vector<SourceAssemblyFormat> &Result) {
if (!requireSequence(&Node, "assembly_formats"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "assembly format"))
return false;
if (!checkKeys(*Element, {"id", "symbol", "name"}, "assembly format"))
return false;
const yaml::Node *IDNode = requiredNode(*Element, "id", "assembly format");
const auto Symbol = requiredScalar(*Element, "symbol", "assembly format");
const auto Name = requiredScalar(*Element, "name", "assembly format");
if (IDNode == nullptr || !Symbol || !Name)
return false;
const auto ID = unsignedInteger32(*IDNode, "assembly format id");
if (!ID)
return false;
Result.push_back({.ID = *ID, .Symbol = *Symbol, .Name = *Name, .Range = Element->range()});
}
return true;
}
[[nodiscard]] bool parseCallingConventions(const yaml::Node &Node,
std::vector<SourceCallingConvention> &Result) {
if (!requireSequence(&Node, "calling_conventions"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "calling convention"))
return false;
if (!checkKeys(*Element,
{"id", "symbol", "name", "integer_register_class", "floating_register_class",
"integer_argument_registers", "floating_argument_registers",
"integer_return_registers", "floating_return_registers", "preserved_mask",
"stack_alignment_bytes", "stack_argument_alignment_bytes",
"shadow_space_bytes", "supports_varargs", "supports_tail_calls"},
"calling convention")) {
return false;
}
const yaml::Node *IDNode = requiredNode(*Element, "id", "calling convention");
const auto Symbol = requiredScalar(*Element, "symbol", "calling convention");
const auto Name = requiredScalar(*Element, "name", "calling convention");
const auto IntegerClass =
requiredScalar(*Element, "integer_register_class", "calling convention");
const auto FloatingClass =
requiredScalar(*Element, "floating_register_class", "calling convention");
const yaml::Node *IntegerArguments =
requiredNode(*Element, "integer_argument_registers", "calling convention");
const yaml::Node *FloatingArguments =
requiredNode(*Element, "floating_argument_registers", "calling convention");
const yaml::Node *IntegerReturns =
requiredNode(*Element, "integer_return_registers", "calling convention");
const yaml::Node *FloatingReturns =
requiredNode(*Element, "floating_return_registers", "calling convention");
const auto PreservedMask = requiredScalar(*Element, "preserved_mask", "calling convention");
const yaml::Node *StackAlignment =
requiredNode(*Element, "stack_alignment_bytes", "calling convention");
const yaml::Node *StackArgumentAlignment =
requiredNode(*Element, "stack_argument_alignment_bytes", "calling convention");
const yaml::Node *ShadowSpace =
requiredNode(*Element, "shadow_space_bytes", "calling convention");
const yaml::Node *SupportsVarArgs =
requiredNode(*Element, "supports_varargs", "calling convention");
const yaml::Node *SupportsTailCalls =
requiredNode(*Element, "supports_tail_calls", "calling convention");
if (IDNode == nullptr || !Symbol || !Name || !IntegerClass || !FloatingClass ||
IntegerArguments == nullptr || FloatingArguments == nullptr ||
IntegerReturns == nullptr || FloatingReturns == nullptr || !PreservedMask ||
StackAlignment == nullptr || StackArgumentAlignment == nullptr ||
ShadowSpace == nullptr || SupportsVarArgs == nullptr || SupportsTailCalls == nullptr) {
return false;
}
const auto ID = unsignedInteger32(*IDNode, "calling convention id");
const auto IntegerArgumentList =
scalarSequence(*IntegerArguments, "calling convention integer arguments");
const auto FloatingArgumentList =
scalarSequence(*FloatingArguments, "calling convention floating arguments");
const auto IntegerReturnList =
scalarSequence(*IntegerReturns, "calling convention integer returns");
const auto FloatingReturnList =
scalarSequence(*FloatingReturns, "calling convention floating returns");
const auto StackAlignmentValue =
unsignedInteger32(*StackAlignment, "calling convention stack alignment");
const auto StackArgumentAlignmentValue =
unsignedInteger32(*StackArgumentAlignment, "calling convention stack argument alignment");
const auto ShadowSpaceValue =
unsignedInteger32(*ShadowSpace, "calling convention shadow space");
const auto SupportsVarArgsValue = boolean(*SupportsVarArgs, "calling convention varargs");
const auto SupportsTailCallsValue =
boolean(*SupportsTailCalls, "calling convention tail calls");
if (!ID || !IntegerArgumentList || !FloatingArgumentList || !IntegerReturnList ||
!FloatingReturnList || !StackAlignmentValue || !StackArgumentAlignmentValue ||
!ShadowSpaceValue || !SupportsVarArgsValue || !SupportsTailCallsValue) {
return false;
}
Result.push_back({
.ID = *ID,
.Symbol = *Symbol,
.Name = *Name,
.IntegerRegisterClassSymbol = *IntegerClass,
.FloatingRegisterClassSymbol = *FloatingClass,
.IntegerArgumentRegisterSymbols = *IntegerArgumentList,
.FloatingArgumentRegisterSymbols = *FloatingArgumentList,
.IntegerReturnRegisterSymbols = *IntegerReturnList,
.FloatingReturnRegisterSymbols = *FloatingReturnList,
.PreservedMaskName = *PreservedMask,
.StackAlignmentBytes = *StackAlignmentValue,
.StackArgumentAlignmentBytes = *StackArgumentAlignmentValue,
.ShadowSpaceBytes = *ShadowSpaceValue,
.SupportsVarArgs = *SupportsVarArgsValue,
.SupportsTailCalls = *SupportsTailCallsValue,
.Range = Element->range(),
});
}
return true;
}
[[nodiscard]] std::optional<SourceOperand> parseOperand(const yaml::Node &Node) {
if (!requireMapping(&Node, "instruction operand"))
return std::nullopt;
if (!checkKeys(Node,
{"kinds", "role", "register_class", "register_bank", "immediate_predicate",
"memory_role", "default_tied_operand", "early_clobber", "optional",
"variadic_group", "allow_subreg", "name", "allowed_target_flags"},
"instruction operand")) {
return std::nullopt;
}
const yaml::Node *KindsNode = requiredNode(Node, "kinds", "instruction operand");
if (KindsNode == nullptr)
return std::nullopt;
const auto Kinds = scalarSequence(*KindsNode, "instruction operand kinds");
if (!Kinds)
return std::nullopt;
SourceOperand Result;
Result.Kinds = *Kinds;
Result.Range = Node.range();
if (const yaml::Node *Role = optionalNode(Node, "role")) {
const auto Value = scalar(*Role, "instruction operand role");
if (!Value)
return std::nullopt;
Result.RegisterRole = *Value;
}
if (const yaml::Node *RegisterClass = optionalNode(Node, "register_class")) {
const auto Value = scalar(*RegisterClass, "instruction operand register_class");
if (!Value)
return std::nullopt;
Result.RegisterClassSymbol = *Value;
}
if (const yaml::Node *RegisterBank = optionalNode(Node, "register_bank")) {
const auto Value = scalar(*RegisterBank, "instruction operand register_bank");
if (!Value)
return std::nullopt;
Result.RegisterBankSymbol = *Value;
}
if (const yaml::Node *Predicate = optionalNode(Node, "immediate_predicate")) {
const auto Value = scalar(*Predicate, "instruction operand immediate_predicate");
if (!Value)
return std::nullopt;
Result.ImmediatePredicateSymbol = *Value;
}
if (const yaml::Node *MemoryRole = optionalNode(Node, "memory_role")) {
const auto Value = scalar(*MemoryRole, "instruction operand memory_role");
if (!Value)
return std::nullopt;
Result.MemoryRole = *Value;
}
if (const yaml::Node *Tie = optionalNode(Node, "default_tied_operand")) {
const auto Value = unsignedInteger32(*Tie, "instruction operand default_tied_operand");
if (!Value)
return std::nullopt;
Result.DefaultTiedOperand = Value;
}
if (const yaml::Node *EarlyClobber = optionalNode(Node, "early_clobber")) {
const auto Value = boolean(*EarlyClobber, "instruction operand early_clobber");
if (!Value)
return std::nullopt;
Result.EarlyClobber = *Value;
}
if (const yaml::Node *Optional = optionalNode(Node, "optional")) {
const auto Value = boolean(*Optional, "instruction operand optional");
if (!Value)
return std::nullopt;
Result.Optional = *Value;
}
if (const yaml::Node *VariadicGroup = optionalNode(Node, "variadic_group")) {
const auto Value = boolean(*VariadicGroup, "instruction operand variadic_group");
if (!Value)
return std::nullopt;
Result.VariadicGroup = *Value;
}
if (const yaml::Node *AllowSubReg = optionalNode(Node, "allow_subreg")) {
const auto Value = boolean(*AllowSubReg, "instruction operand allow_subreg");
if (!Value)
return std::nullopt;
Result.AllowSubReg = *Value;
}
if (const yaml::Node *Name = optionalNode(Node, "name")) {
const auto Value = scalar(*Name, "instruction operand name");
if (!Value)
return std::nullopt;
Result.NameSymbol = *Value;
}
if (const yaml::Node *TargetFlags = optionalNode(Node, "allowed_target_flags")) {
const auto Value =
unsignedInteger32(*TargetFlags, "instruction operand allowed_target_flags");
if (!Value)
return std::nullopt;
Result.AllowedTargetFlags = *Value;
}
return Result;
}
[[nodiscard]] std::optional<std::vector<SourceOperand>> parseOperands(const yaml::Node &Node) {
if (!requireSequence(&Node, "instruction operands"))
return std::nullopt;
std::vector<SourceOperand> Result;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (Element == nullptr)
return std::nullopt;
const auto Operand = parseOperand(*Element);
if (!Operand)
return std::nullopt;
Result.push_back(*Operand);
}
return Result;
}
[[nodiscard]] std::optional<SourceImplicitRegister>
parseImplicitRegister(const yaml::Node &Node, std::string_view Context) {
if (!requireMapping(&Node, Context))
return std::nullopt;
if (!checkKeys(Node, {"register", "early_clobber", "renamable"}, Context))
return std::nullopt;
const auto Register = requiredScalar(Node, "register", Context);
if (!Register)
return std::nullopt;
SourceImplicitRegister Result{
.RegisterSymbol = *Register,
.Range = Node.range(),
};
if (const yaml::Node *EarlyClobber = optionalNode(Node, "early_clobber")) {
const auto Value = boolean(*EarlyClobber, std::string(Context) + " early_clobber");
if (!Value)
return std::nullopt;
Result.EarlyClobber = *Value;
}
if (const yaml::Node *Renamable = optionalNode(Node, "renamable")) {
const auto Value = boolean(*Renamable, std::string(Context) + " renamable");
if (!Value)
return std::nullopt;
Result.Renamable = *Value;
}
return Result;
}
[[nodiscard]] std::optional<std::vector<SourceImplicitRegister>>
parseImplicitRegisters(const yaml::Node &Node, std::string_view Context) {
if (!requireSequence(&Node, Context))
return std::nullopt;
std::vector<SourceImplicitRegister> Result;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (Element == nullptr)
return std::nullopt;
const auto Register = parseImplicitRegister(*Element, Context);
if (!Register)
return std::nullopt;
Result.push_back(*Register);
}
return Result;
}
[[nodiscard]] bool parseInstructions(const yaml::Node &Node,
std::vector<SourceInstruction> &Result) {
if (!requireSequence(&Node, "instructions"))
return false;
for (const yaml::NodeID ElementID : Node.sequence()) {
const yaml::Node *Element = Input.node(ElementID);
if (!requireMapping(Element, "instruction"))
return false;
if (!checkKeys(*Element,
{"id", "symbol", "name", "assembly_mnemonic", "encoding", "operands",
"num_explicit_defs", "variadic", "implicit_defs", "implicit_uses",
"allow_extra_implicit_operands", "flags", "scheduling_class",
"encoding_category", "size", "assembly_format", "requires_memory_operands",
"owner", "deadline"},
"instruction")) {
return false;
}
const yaml::Node *IDNode = requiredNode(*Element, "id", "instruction");
const auto Symbol = requiredScalar(*Element, "symbol", "instruction");
const auto Name = requiredScalar(*Element, "name", "instruction");
const yaml::Node *OperandsNode = requiredNode(*Element, "operands", "instruction");
const yaml::Node *DefsNode = requiredNode(*Element, "num_explicit_defs", "instruction");
const yaml::Node *FlagsNode = requiredNode(*Element, "flags", "instruction");
if (IDNode == nullptr || !Symbol || !Name || OperandsNode == nullptr || DefsNode == nullptr ||
FlagsNode == nullptr) {
return false;
}
const auto ID = unsignedInteger32(*IDNode, "instruction id");
const auto Defs = unsignedInteger32(*DefsNode, "instruction num_explicit_defs");
const auto Operands = parseOperands(*OperandsNode);
const auto Flags = scalarSequence(*FlagsNode, "instruction flags");
if (!ID || !Defs || !Operands || !Flags)
return false;
SourceInstruction Instruction;
Instruction.ID = *ID;
Instruction.Symbol = *Symbol;
Instruction.Name = *Name;
Instruction.Operands = *Operands;
Instruction.NumExplicitDefs = *Defs;
Instruction.Flags = *Flags;
Instruction.Range = Element->range();
if (const yaml::Node *Mnemonic = optionalNode(*Element, "assembly_mnemonic")) {
const auto Value = scalar(*Mnemonic, "instruction assembly_mnemonic");
if (!Value)
return false;
Instruction.AssemblyMnemonic = *Value;
}
if (const yaml::Node *Encoding = optionalNode(*Element, "encoding")) {
const auto Value = unsignedInteger32(*Encoding, "instruction encoding");
if (!Value)
return false;
Instruction.BinaryEncoding = Value;
}
if (const yaml::Node *SchedulingClass = optionalNode(*Element, "scheduling_class")) {
const auto Value = scalar(*SchedulingClass, "instruction scheduling_class");
if (!Value)
return false;
Instruction.SchedulingClassSymbol = *Value;
}
if (const yaml::Node *EncodingCategory = optionalNode(*Element, "encoding_category")) {
const auto Value = scalar(*EncodingCategory, "instruction encoding_category");
if (!Value)
return false;
Instruction.EncodingCategorySymbol = *Value;
}
if (const yaml::Node *Size = optionalNode(*Element, "size")) {
const auto Value = unsignedInteger32(*Size, "instruction size");
if (!Value)
return false;
Instruction.SizeInBytes = *Value;
}
if (const yaml::Node *AssemblyFormat = optionalNode(*Element, "assembly_format")) {
const auto Value = scalar(*AssemblyFormat, "instruction assembly_format");
if (!Value)
return false;
Instruction.AssemblyFormatSymbol = *Value;
}
if (const yaml::Node *Variadic = optionalNode(*Element, "variadic")) {
const auto Value = boolean(*Variadic, "instruction variadic");
if (!Value)
return false;
Instruction.Variadic = *Value;
}
if (const yaml::Node *ImplicitDefs = optionalNode(*Element, "implicit_defs")) {
const auto Values = parseImplicitRegisters(*ImplicitDefs, "instruction implicit_defs");
if (!Values)
return false;
Instruction.ImplicitDefs = *Values;
}
if (const yaml::Node *ImplicitUses = optionalNode(*Element, "implicit_uses")) {
const auto Values = parseImplicitRegisters(*ImplicitUses, "instruction implicit_uses");
if (!Values)
return false;
Instruction.ImplicitUses = *Values;
}
if (const yaml::Node *AllowExtra = optionalNode(*Element, "allow_extra_implicit_operands")) {
const auto Value = boolean(*AllowExtra, "instruction allow_extra_implicit_operands");
if (!Value)
return false;
Instruction.AllowExtraImplicitOperands = *Value;
}
if (const yaml::Node *MemoryOperands = optionalNode(*Element, "requires_memory_operands")) {
const auto Value = boolean(*MemoryOperands, "instruction requires_memory_operands");
if (!Value)
return false;
Instruction.RequiresMemoryOperands = *Value;
}
if (const yaml::Node *Owner = optionalNode(*Element, "owner")) {
const auto Value = scalar(*Owner, "instruction owner");
if (!Value)
return false;
Instruction.Owner = *Value;
}
if (const yaml::Node *Deadline = optionalNode(*Element, "deadline")) {
const auto Value = scalar(*Deadline, "instruction deadline");
if (!Value)
return false;
Instruction.Deadline = *Value;
}
Result.push_back(std::move(Instruction));
}
return true;
}
[[nodiscard]] const yaml::Node *optionalNode(const yaml::Node &Mapping,
std::string_view Key) const noexcept {
const std::optional<yaml::NodeID> Value = Mapping.mappingValue(Key);
return Value ? Input.node(*Value) : nullptr;
}
const yaml::Stream &Input;
std::vector<TextDiagnostic> Diagnostics;
};
} // namespace
DescriptionParseResult parseDescription(std::string SourceName, std::string SourceText) {
yaml::ParseResult YAMLResult = yaml::parse(std::move(SourceName), std::move(SourceText));
if (!YAMLResult.succeeded()) {
DescriptionParseResult Result;
Result.Diagnostics.assign(YAMLResult.diagnostics().begin(), YAMLResult.diagnostics().end());
return Result;
}
ASTParser Parser(*YAMLResult.stream());
auto Records = Parser.parse();
if (!Records)
return {.Records = std::nullopt, .Diagnostics = Parser.takeDiagnostics()};
return {.Records = std::move(Records), .Diagnostics = {}};
}
} // namespace rcc::targetgen
|