forked from WerWolv/PatternLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern_bitfield.hpp
More file actions
817 lines (621 loc) · 28.6 KB
/
pattern_bitfield.hpp
File metadata and controls
817 lines (621 loc) · 28.6 KB
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
#pragma once
#include <pl/patterns/pattern.hpp>
#include <pl/patterns/pattern_enum.hpp>
namespace pl::ptrn {
class PatternBitfieldMember : public Pattern {
public:
using Pattern::Pattern;
[[nodiscard]] const PatternBitfieldMember& getTopmostBitfield() const {
const PatternBitfieldMember* topBitfield = this;
while (auto parent = topBitfield->getParent()) {
auto parentBitfield = dynamic_cast<const PatternBitfieldMember*>(parent);
if (parentBitfield == nullptr)
break;
topBitfield = parentBitfield;
}
return *topBitfield;
}
[[nodiscard]] virtual u8 getBitOffset() const = 0;
virtual void setBitOffset(u8 bitOffset) = 0;
[[nodiscard]] virtual u64 getBitSize() const = 0;
[[nodiscard]] u128 getTotalBitOffset(u64 fromByteOffset = 0) const {
return ((this->getOffset() - fromByteOffset) << 3) + this->getBitOffset();
}
[[nodiscard]] u128 getBitOffsetForDisplay() const {
return (this->getTotalBitOffset() - getTopmostBitfield().getTotalBitOffset()) % 8;
}
[[nodiscard]] virtual bool isPadding() const { return false; }
[[nodiscard]] u128 getOffsetForSorting() const override {
return this->getTotalBitOffset();
}
[[nodiscard]] u128 getSizeForSorting() const override {
return this->getBitSize();
}
std::vector<u8> getRawBytes() override {
return { };
}
};
class PatternBitfieldField : public PatternBitfieldMember {
public:
PatternBitfieldField(core::Evaluator *evaluator, u64 offset, u8 bitOffset, u8 bitSize, u32 line, PatternBitfieldMember *parentBitfield = nullptr)
: PatternBitfieldMember(evaluator, offset, (bitOffset + bitSize + 7) / 8, line), m_bitOffset(bitOffset % 8), m_bitSize(bitSize) {
if (parentBitfield != nullptr)
this->setParent(parentBitfield->reference());
}
PatternBitfieldField(const PatternBitfieldField &other) : PatternBitfieldMember(other) {
this->m_padding = other.m_padding;
this->m_bitOffset = other.m_bitOffset;
this->m_bitSize = other.m_bitSize;
}
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternBitfieldField(*this));
}
[[nodiscard]] u128 readValue() const {
return this->getEvaluator()->readBits(this->getOffset(), this->getBitOffset(), this->getBitSize(), this->getSection(), this->getEndian());
}
[[nodiscard]] core::Token::Literal getValue() const override {
return transformValue(this->readValue());
}
[[nodiscard]] std::string getFormattedName() const override {
return this->m_bitSize == 1 ? "bit" : "bits";
}
[[nodiscard]] u8 getBitOffset() const override {
return this->m_bitOffset;
}
void setBitOffset(u8 bitOffset) override {
this->m_bitOffset = bitOffset;
}
[[nodiscard]] u64 getBitSize() const override {
return this->m_bitSize;
}
[[nodiscard]] bool operator==(const Pattern &other) const override {
if (!compareCommonProperties<decltype(*this)>(other))
return false;
auto &otherBitfieldField = *static_cast<const PatternBitfieldField *>(&other);
return this->m_bitOffset == otherBitfieldField.m_bitOffset && this->m_bitSize == otherBitfieldField.m_bitSize;
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
std::string formatDisplayValue() override {
auto literal = this->getValue();
auto value = literal.toUnsigned();
return Pattern::callUserFormatFunc(literal).value_or(fmt::format("{}", value));
}
[[nodiscard]] std::string toString() override {
auto value = this->readValue();
return Pattern::callUserFormatFunc(value, true).value_or(fmt::format("{}", value));
}
[[nodiscard]] bool isPadding() const override { return this->m_padding; }
void setPadding(bool padding) { this->m_padding = padding; }
void setValue(const core::Token::Literal &value) override {
std::vector<u8> result;
const auto &formatterFunctionName = this->getWriteFormatterFunction();
if (formatterFunctionName.empty()) {
result = this->getBytesOf(value);
} else {
try {
const auto function = this->getEvaluator()->findFunction(formatterFunctionName);
if (function.has_value()) {
auto formatterResult = function->func(this->getEvaluator(), { value });
if (formatterResult.has_value()) {
result = this->getBytesOf(*formatterResult);
}
}
} catch (core::err::EvaluatorError::Exception &error) {
wolv::util::unused(error);
}
}
if (!result.empty() && result.size() <= sizeof(u128)) {
u128 writeValue = 0;
std::memcpy(&writeValue, result.data(), result.size());
this->getEvaluator()->writeBits(this->getOffset(), this->getBitOffset(), this->getBitSize(), this->getSection(), this->getEndian(), writeValue);
this->clearFormatCache();
this->getParent()->clearFormatCache();
}
}
private:
u8 m_bitOffset;
u8 m_bitSize;
bool m_padding = false;
};
class PatternBitfieldFieldSigned : public PatternBitfieldField {
public:
using PatternBitfieldField::PatternBitfieldField;
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternBitfieldFieldSigned(*this));
}
[[nodiscard]] core::Token::Literal getValue() const override {
return transformValue(hlp::signExtend(this->getBitSize(), this->readValue()));
}
std::string formatDisplayValue() override {
auto rawValue = this->readValue();
auto value = hlp::signExtend(this->getBitSize(), i128(rawValue));
return Pattern::callUserFormatFunc(value).value_or(fmt::format("{}", value));
}
[[nodiscard]] std::string toString() override {
auto result = fmt::format("{}", this->getValue().toSigned());
return Pattern::callUserFormatFunc(this->getValue(), true).value_or(result);
}
};
class PatternBitfieldFieldBoolean : public PatternBitfieldField {
public:
using PatternBitfieldField::PatternBitfieldField;
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternBitfieldFieldBoolean(*this));
}
[[nodiscard]] core::Token::Literal getValue() const override {
return transformValue(this->readValue());
}
[[nodiscard]] std::string getFormattedName() const override {
return "bool";
}
std::string formatDisplayValue() override {
auto value = this->getValue().toUnsigned();
if (value == 0)
return "false";
else if (value == 1)
return "true";
else
return "true*";
}
[[nodiscard]] std::string toString() override {
auto value = this->getValue();
return Pattern::callUserFormatFunc(value, true).value_or(fmt::format("{}", value.toBoolean() ? "true" : "false"));
}
};
class PatternBitfieldFieldEnum : public PatternBitfieldField {
public:
using PatternBitfieldField::PatternBitfieldField;
[[nodiscard]] std::string getFormattedName() const override {
return "enum " + Pattern::getTypeName();
}
[[nodiscard]] std::string getTypeName() const override {
return Pattern::getTypeName();
}
void setEnumValues(const std::map<std::string, PatternEnum::EnumValue> &enumValues) {
this->m_enumValues = enumValues;
}
const auto& getEnumValues() const {
return this->m_enumValues;
}
[[nodiscard]] bool operator==(const Pattern &other) const override {
if (!compareCommonProperties<decltype(*this)>(other))
return false;
auto &otherEnum = *static_cast<const PatternBitfieldFieldEnum *>(&other);
if (this->m_enumValues != otherEnum.m_enumValues)
return false;
return true;
}
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternBitfieldFieldEnum(*this));
}
std::string formatDisplayValue() override {
auto value = this->readValue();
auto enumName = PatternEnum::getEnumName(this->getTypeName(), value, this->getEnumValues());
return Pattern::callUserFormatFunc(value).value_or(fmt::format("{}", enumName));
}
[[nodiscard]] std::string toString() override {
auto enumName = PatternEnum::getEnumName(this->getTypeName(), this->readValue(), this->getEnumValues());
return Pattern::callUserFormatFunc(this->getValue(), true).value_or(enumName);
}
private:
std::map<std::string, PatternEnum::EnumValue> m_enumValues;
};
class PatternBitfieldArray : public PatternBitfieldMember,
public IInlinable,
public IIndexable {
public:
PatternBitfieldArray(core::Evaluator *evaluator, u64 offset, u8 firstBitOffset, u128 totalBitSize, u32 line)
: PatternBitfieldMember(evaluator, offset, size_t((totalBitSize + 7) / 8), line), m_firstBitOffset(firstBitOffset), m_totalBitSize(totalBitSize) { }
PatternBitfieldArray(const PatternBitfieldArray &other) : PatternBitfieldMember(other) {
std::vector<std::shared_ptr<Pattern>> entries;
for (const auto &entry : other.m_entries)
entries.push_back(entry->clone());
this->setEntries(std::move(entries));
this->m_firstBitOffset = other.m_firstBitOffset;
this->m_totalBitSize = other.m_totalBitSize;
}
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
auto other = std::make_shared<PatternBitfieldArray>(*this);
for (const auto &entry : other->m_entries)
entry->setParent(other->reference());
return other;
}
[[nodiscard]] u8 getBitOffset() const override {
return m_firstBitOffset;
}
void setBitOffset(u8 bitOffset) override {
this->m_firstBitOffset = bitOffset;
}
void setBitSize(u128 bitSize) {
this->m_totalBitSize = bitSize;
this->setSize(size_t((bitSize + 7) / 8));
}
[[nodiscard]] u64 getBitSize() const override {
return u64(this->m_totalBitSize);
}
[[nodiscard]] bool isReversed() const {
return this->m_reversed;
}
void setReversed(bool reversed) {
this->m_reversed = reversed;
}
void setColor(u32 color) override {
Pattern::setColor(color);
for (auto &entry : this->m_entries) {
if (!entry->hasOverriddenColor())
entry->setColor(color);
}
}
[[nodiscard]] std::string getFormattedName() const override {
if (this->m_entries.empty())
return "???";
return this->m_entries.front()->getTypeName() + "[" + std::to_string(this->m_entries.size()) + "]";
}
[[nodiscard]] std::string getTypeName() const override {
if (this->m_entries.empty())
return "???";
return this->m_entries.front()->getTypeName();
}
void setSection(u64 id) override {
if (this->getSection() == id)
return;
for (auto &entry : this->m_entries)
entry->setSection(id);
PatternBitfieldMember::setSection(id);
}
[[nodiscard]] std::vector<std::pair<u64, Pattern*>> getChildren() override {
if (this->getVisibility() == Visibility::HighlightHidden)
return { };
std::vector<std::pair<u64, Pattern*>> result;
for (const auto &entry : this->m_entries) {
auto children = entry->getChildren();
std::move(children.begin(), children.end(), std::back_inserter(result));
}
return result;
}
void setLocal(bool local) override {
for (auto &pattern : this->m_entries)
pattern->setLocal(local);
PatternBitfieldMember::setLocal(local);
}
void setReference(bool reference) override {
for (auto &pattern : this->m_entries)
pattern->setReference(reference);
PatternBitfieldMember::setReference(reference);
}
[[nodiscard]] std::shared_ptr<Pattern> getEntry(size_t index) const override {
return this->m_entries[index];
}
[[nodiscard]] size_t getEntryCount() const override {
return this->m_entries.size();
}
[[nodiscard]] std::vector<std::shared_ptr<Pattern>> getEntries() override {
return this->m_entries;
}
void setOffset(u64 offset) override {
for (auto &entry : this->m_entries) {
if (entry->getSection() == this->getSection() && entry->getSection() != ptrn::Pattern::PatternLocalSectionId)
entry->setOffset(entry->getOffset() - this->getOffset() + offset);
}
PatternBitfieldMember::setOffset(offset);
}
void forEachEntry(u64 start, u64 end, const std::function<void(u64, Pattern*)>& fn) override {
auto evaluator = this->getEvaluator();
auto startArrayIndex = evaluator->getCurrentArrayIndex();
ON_SCOPE_EXIT {
if (startArrayIndex.has_value())
evaluator->setCurrentArrayIndex(*startArrayIndex);
else
evaluator->clearCurrentArrayIndex();
};
for (u64 i = start; i < std::min<u64>(end, this->m_entries.size()); i++) {
evaluator->setCurrentArrayIndex(i);
auto &entry = this->m_entries[i];
if (!entry->isPatternLocal() || entry->hasAttribute("export"))
fn(i, entry.get());
}
}
void setEntries(const std::vector<std::shared_ptr<Pattern>> &entries) override {
this->m_entries = entries;
for (auto &entry : this->m_entries) {
if (!entry->hasOverriddenColor())
entry->setBaseColor(this->getColor());
this->m_sortedEntries.push_back(entry.get());
}
if (!this->m_entries.empty())
this->setBaseColor(this->m_entries.front()->getColor());
}
[[nodiscard]] std::string toString() override {
std::string result;
result += "[ ";
size_t entryCount = 0;
for (const auto &entry : this->m_entries) {
if (entryCount > 50) {
result += fmt::format("..., ");
break;
}
result += fmt::format("{}, ", entry->toString());
entryCount++;
}
if (entryCount > 0) {
// Remove trailing ", "
result.pop_back();
result.pop_back();
}
result += " ]";
return Pattern::callUserFormatFunc(this->reference(), true).value_or(result);
}
[[nodiscard]] bool operator==(const Pattern &other) const override {
if (!compareCommonProperties<decltype(*this)>(other))
return false;
auto &otherArray = *static_cast<const PatternBitfieldArray *>(&other);
if (this->m_firstBitOffset != otherArray.m_firstBitOffset)
return false;
if (this->m_totalBitSize != otherArray.m_totalBitSize)
return false;
if (this->m_entries.size() != otherArray.m_entries.size())
return false;
for (u64 i = 0; i < this->m_entries.size(); i++) {
if (*this->m_entries[i] != *otherArray.m_entries[i])
return false;
}
return true;
}
void setEndian(std::endian endian) override {
if (this->isLocal()) return;
Pattern::setEndian(endian);
for (auto &entry : this->m_entries) {
entry->setEndian(endian);
}
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
std::string formatDisplayValue() override {
return Pattern::callUserFormatFunc(this->reference()).value_or("[ ... ]");
}
void sort(const std::function<bool (const Pattern *, const Pattern *)> &comparator) override {
this->m_sortedEntries.clear();
for (auto &member : this->m_entries)
this->m_sortedEntries.push_back(member.get());
std::sort(this->m_sortedEntries.begin(), this->m_sortedEntries.end(), comparator);
if (this->isReversed())
std::reverse(this->m_sortedEntries.begin(), this->m_sortedEntries.end());
for (auto &member : this->m_entries)
member->sort(comparator);
}
std::vector<u8> getRawBytes() override {
std::vector<u8> result;
result.resize(this->getSize());
this->getEvaluator()->readData(this->getOffset(), result.data(), result.size(), this->getSection());
if (this->getEndian() != std::endian::native)
std::reverse(result.begin(), result.end());
return result;
}
void clearFormatCache() override {
this->forEachEntry(0, this->getEntryCount(), [&](u64, pl::ptrn::Pattern *entry) {
entry->clearFormatCache();
});
Pattern::clearFormatCache();
}
private:
std::vector<std::shared_ptr<Pattern>> m_entries;
std::vector<Pattern *> m_sortedEntries;
u8 m_firstBitOffset = 0;
u128 m_totalBitSize = 0;
bool m_reversed = false;
};
class PatternBitfield : public PatternBitfieldMember,
public IInlinable,
public IIterable {
public:
PatternBitfield(core::Evaluator *evaluator, u64 offset, u8 firstBitOffset, u128 totalBitSize, u32 line)
: PatternBitfieldMember(evaluator, offset, size_t((totalBitSize + 7) / 8), line), m_firstBitOffset(firstBitOffset), m_totalBitSize(totalBitSize) { }
PatternBitfield(const PatternBitfield &other) : PatternBitfieldMember(other) {
for (auto &field : other.m_fields)
this->m_fields.push_back(field->clone());
this->m_firstBitOffset = other.m_firstBitOffset;
this->m_totalBitSize = other.m_totalBitSize;
}
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::unique_ptr<Pattern>(new PatternBitfield(*this));
}
[[nodiscard]] u8 getBitOffset() const override {
return m_firstBitOffset;
}
void setBitOffset(u8 bitOffset) override {
this->m_firstBitOffset = bitOffset;
}
void setBitSize(u128 bitSize) {
this->m_totalBitSize = u64(bitSize);
this->setSize(size_t((bitSize + 7) / 8));
}
[[nodiscard]] u64 getBitSize() const override {
return this->m_totalBitSize;
}
[[nodiscard]] bool isReversed() const {
return this->m_reversed;
}
void setReversed(bool reversed) {
this->m_reversed = reversed;
}
void setSection(u64 id) override {
if (this->getSection() == id)
return;
for (auto &field : this->m_fields)
field->setSection(id);
Pattern::setSection(id);
}
[[nodiscard]] std::vector<std::pair<u64, Pattern*>> getChildren() override {
if (this->getVisibility() == Visibility::HighlightHidden)
return { };
if (this->isSealed()) {
return { { this->getOffset(), this } };
} else {
std::vector<std::pair<u64, Pattern*>> result;
for (const auto &entry : this->m_fields) {
auto children = entry->getChildren();
std::move(children.begin(), children.end(), std::back_inserter(result));
}
return result;
}
}
void setColor(u32 color) override {
Pattern::setColor(color);
for (auto &entry : this->m_fields) {
if (!entry->hasOverriddenColor())
entry->setColor(color);
}
}
void setLocal(bool local) override {
for (auto &pattern : this->m_fields)
pattern->setLocal(local);
Pattern::setLocal(local);
}
void setReference(bool reference) override {
for (auto &pattern : this->m_fields)
pattern->setReference(reference);
Pattern::setReference(reference);
}
[[nodiscard]] std::string getFormattedName() const override {
return "bitfield " + Pattern::getTypeName();
}
void setFields(std::vector<std::shared_ptr<Pattern>> fields) {
this->m_fields = std::move(fields);
if (!this->m_fields.empty())
this->setBaseColor(this->m_fields.front()->getColor());
for (const auto &field : this->m_fields) {
field->setParent(this->reference());
this->m_sortedFields.push_back(field.get());
}
}
[[nodiscard]] bool operator==(const Pattern &other) const override {
if (!compareCommonProperties<decltype(*this)>(other))
return false;
auto &otherBitfield = *static_cast<const PatternBitfield *>(&other);
if (this->m_firstBitOffset != otherBitfield.m_firstBitOffset)
return false;
if (this->m_totalBitSize != otherBitfield.m_totalBitSize)
return false;
if (this->m_fields.size() != otherBitfield.m_fields.size())
return false;
for (u64 i = 0; i < this->m_fields.size(); i++) {
if (*this->m_fields[i] != *otherBitfield.m_fields[i])
return false;
}
return true;
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
[[nodiscard]] std::string toString() override {
std::string result = this->getFormattedName();
result += " { ";
for (const auto &field : this->m_fields) {
if (field->getVariableName().starts_with("$"))
continue;
result += fmt::format("{} = {}, ", field->getVariableName(), field->toString());
}
// Remove trailing ", "
result.pop_back();
result.pop_back();
result += " }";
return Pattern::callUserFormatFunc(this->reference(), true).value_or(result);
}
std::string formatDisplayValue() override {
std::vector<u8> bytes(this->getSize(), 0);
this->getEvaluator()->readData(this->getOffset(), bytes.data(), bytes.size(), this->getSection());
if (this->getEndian() == std::endian::little)
std::reverse(bytes.begin(), bytes.end());
std::string valueString;
for (const auto &pattern : this->m_fields) {
if (auto *field = dynamic_cast<PatternBitfieldField *>(pattern.get()); field != nullptr) {
auto fieldValue = field->getValue().toUnsigned();
if (field->getBitSize() == 1) {
if (fieldValue > 0)
valueString += fmt::format("{} | ", field->getVariableName());
} else {
valueString += fmt::format("{}({}) | ", field->getVariableName(), field->toString());
}
} else if (auto *member = dynamic_cast<PatternBitfieldMember *>(pattern.get()); member != nullptr) {
valueString += fmt::format("{} = {} | ", member->getVariableName(), member->toString());
} else if (auto *bitfield = dynamic_cast<PatternBitfield *>(pattern.get()); bitfield != nullptr) {
valueString += fmt::format("{} = {} | ", bitfield->getVariableName(), bitfield->formatDisplayValue());
}
}
if (valueString.size() >= 3) {
valueString.resize(valueString.size() - 3);
}
if (valueString.size() > 64)
return Pattern::callUserFormatFunc(this->reference()).value_or(fmt::format("{{ ... }}", valueString));
else
return Pattern::callUserFormatFunc(this->reference()).value_or(fmt::format("{{ {} }}", valueString));
}
void setEndian(std::endian endian) override {
if (this->isLocal()) return;
Pattern::setEndian(endian);
for (auto &field : this->m_fields)
field->setEndian(endian);
}
std::shared_ptr<Pattern> getEntry(size_t index) const override {
return this->m_fields[index];
}
size_t getEntryCount() const override {
return this->m_fields.size();
}
[[nodiscard]] std::vector<std::shared_ptr<Pattern>> getEntries() override {
return this->m_fields;
}
void setEntries(const std::vector<std::shared_ptr<Pattern>> &entries) override {
this->m_fields = entries;
}
void setOffset(u64 offset) override {
for (auto &field : this->m_fields) {
if (field->getSection() == this->getSection() && field->getSection() != ptrn::Pattern::PatternLocalSectionId)
field->setOffset(field->getOffset() - this->getOffset() + offset);
}
PatternBitfieldMember::setOffset(offset);
}
void forEachEntry(u64 start, u64 end, const std::function<void (u64, Pattern *)> &fn) override {
if (this->isSealed())
return;
for (auto i = start; i < end; i++) {
auto &pattern = this->m_fields[i];
if (!pattern->isPatternLocal() || pattern->hasAttribute("export"))
fn(i, pattern.get());
}
}
void sort(const std::function<bool (const Pattern *, const Pattern *)> &comparator) override {
this->m_sortedFields.clear();
for (auto &member : this->m_fields)
this->m_sortedFields.push_back(member.get());
std::sort(this->m_sortedFields.begin(), this->m_sortedFields.end(), comparator);
if (this->isReversed())
std::reverse(this->m_sortedFields.begin(), this->m_sortedFields.end());
for (auto &member : this->m_fields)
member->sort(comparator);
}
std::vector<u8> getRawBytes() override {
std::vector<u8> result;
result.resize(this->getSize());
this->getEvaluator()->readData(this->getOffset(), result.data(), result.size(), this->getSection());
if (this->getEndian() != std::endian::native)
std::reverse(result.begin(), result.end());
return result;
}
void clearFormatCache() override {
this->forEachEntry(0, this->getEntryCount(), [&](u64, pl::ptrn::Pattern *entry) {
entry->clearFormatCache();
});
Pattern::clearFormatCache();
}
private:
std::vector<std::shared_ptr<Pattern>> m_fields;
std::vector<Pattern *> m_sortedFields;
u8 m_firstBitOffset = 0;
u64 m_totalBitSize = 0;
bool m_reversed = false;
};
}