@@ -2303,14 +2303,16 @@ struct DIEToCASConverter {
2303
2303
bool IsLittleEndian;
2304
2304
uint8_t AddressSize;
2305
2305
2306
- Error convertInNewDIEBlock (
2307
- DWARFDie DIE, DistinctDataWriter &DistinctWriter,
2308
- AbbrevSetWriter &AbbrevWriter,
2309
- SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters);
2306
+ struct ParentAndChildDIE {
2307
+ DWARFDie Parent;
2308
+ bool ParentAlreadyWritten;
2309
+ DIEDataWriter &Writer;
2310
+ std::optional<DWARFDie> Child;
2311
+ };
2310
2312
2311
2313
Error
2312
- convertImpl (DWARFDie & DIE, DIEDataWriter &DIEWriter ,
2313
- DistinctDataWriter &DistinctWriter, AbbrevSetWriter &AbbrevWriter,
2314
+ convertImpl (DWARFDie DIE, DistinctDataWriter &DistinctWriter ,
2315
+ AbbrevSetWriter &AbbrevWriter,
2314
2316
SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters);
2315
2317
};
2316
2318
@@ -3660,6 +3662,12 @@ static void writeDIEAttrs(DWARFDie &DIE, ArrayRef<char> DebugInfoData,
3660
3662
}
3661
3663
}
3662
3664
3665
+ static void
3666
+ pushNewDIEWriter (SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters) {
3667
+ auto DIEWriter = std::make_unique<DIEDataWriter>();
3668
+ DIEWriters.push_back (std::move (DIEWriter));
3669
+ }
3670
+
3663
3671
// / Creates an abbreviation for DIE using AbbrevWriter.
3664
3672
// / Stores the contents of the DIE using DistinctWriter and DIEWriter following
3665
3673
// / the format:
@@ -3675,61 +3683,54 @@ static void writeDIEAttrs(DWARFDie &DIE, ArrayRef<char> DebugInfoData,
3675
3683
// / DIEAbbrevSetRef block. In this case, raw_data should be interpreted
3676
3684
// / according to the corresponding DIEAbbrevRefs block.
3677
3685
Error DIEToCASConverter::convertImpl (
3678
- DWARFDie & DIE, DIEDataWriter &DIEWriter , DistinctDataWriter &DistinctWriter,
3686
+ DWARFDie DIE, DistinctDataWriter &DistinctWriter,
3679
3687
AbbrevSetWriter &AbbrevWriter,
3680
3688
SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters) {
3681
- Expected<unsigned > MaybeAbbrevIndex =
3682
- AbbrevWriter.createAbbrevEntry (DIE, CASBuilder);
3683
- if (!MaybeAbbrevIndex)
3684
- return MaybeAbbrevIndex.takeError ();
3685
-
3686
- DistinctWriter.writeULEB128 (encodeAbbrevIndex (*MaybeAbbrevIndex));
3687
- writeDIEAttrs (DIE, DebugInfoData, DIEWriter, DistinctWriter, IsLittleEndian,
3688
- AddressSize);
3689
-
3690
- for (DWARFDie Child = DIE.getFirstChild (); Child;
3691
- Child = Child.getSibling ()) {
3692
- dwarf::Tag ChildTag = Child.getTag ();
3693
- if (ChildTag == dwarf::Tag::DW_TAG_null) {
3694
- DistinctWriter.writeULEB128 (getEndOfDIESiblingsMarker ());
3695
- break ;
3689
+ SmallVector<ParentAndChildDIE> DIEStack;
3690
+ pushNewDIEWriter (DIEWriters);
3691
+ DIEStack.push_back ({DIE, false , *DIEWriters.back (), std::nullopt});
3692
+ while (!DIEStack.empty ()) {
3693
+ auto ParentAndChild = DIEStack.pop_back_val ();
3694
+ DWARFDie CurrDIE = ParentAndChild.Parent ;
3695
+
3696
+ if (!ParentAndChild.ParentAlreadyWritten ) {
3697
+ Expected<unsigned > MaybeAbbrevIndex =
3698
+ AbbrevWriter.createAbbrevEntry (CurrDIE, CASBuilder);
3699
+ if (!MaybeAbbrevIndex)
3700
+ return MaybeAbbrevIndex.takeError ();
3701
+
3702
+ DistinctWriter.writeULEB128 (encodeAbbrevIndex (*MaybeAbbrevIndex));
3703
+ writeDIEAttrs (CurrDIE, DebugInfoData, ParentAndChild.Writer ,
3704
+ DistinctWriter, IsLittleEndian, AddressSize);
3696
3705
}
3697
3706
3698
- // FIXME: don't use recursion.
3699
- if (shouldCreateSeparateBlockFor (Child)) {
3700
- DistinctWriter.writeULEB128 (getDIEInAnotherBlockMarker ());
3701
- if (auto E = convertInNewDIEBlock (Child, DistinctWriter, AbbrevWriter,
3702
- DIEWriters))
3703
- return E;
3704
- continue ;
3707
+ DWARFDie Child = ParentAndChild.Child ? ParentAndChild.Child ->getSibling ()
3708
+ : CurrDIE.getFirstChild ();
3709
+ if (Child) {
3710
+ dwarf::Tag ChildTag = Child.getTag ();
3711
+ if (ChildTag == dwarf::Tag::DW_TAG_null)
3712
+ DistinctWriter.writeULEB128 (getEndOfDIESiblingsMarker ());
3713
+ else if (shouldCreateSeparateBlockFor (Child)) {
3714
+ DistinctWriter.writeULEB128 (getDIEInAnotherBlockMarker ());
3715
+ DIEStack.push_back ({CurrDIE, true , ParentAndChild.Writer , Child});
3716
+ pushNewDIEWriter (DIEWriters);
3717
+ DIEStack.push_back ({Child, false , *DIEWriters.back (), std::nullopt});
3718
+ } else {
3719
+ DIEStack.push_back ({CurrDIE, true , ParentAndChild.Writer , Child});
3720
+ DIEStack.push_back ({Child, false , ParentAndChild.Writer , std::nullopt});
3721
+ }
3705
3722
}
3706
- if (auto E = convertImpl (Child, DIEWriter, DistinctWriter, AbbrevWriter,
3707
- DIEWriters))
3708
- return E;
3709
3723
}
3710
3724
return Error::success ();
3711
3725
}
3712
3726
3713
- Error DIEToCASConverter::convertInNewDIEBlock (
3714
- DWARFDie DIE, DistinctDataWriter &DistinctWriter,
3715
- AbbrevSetWriter &AbbrevWriter,
3716
- SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters) {
3717
- auto DIEWriter = std::make_unique<DIEDataWriter>();
3718
- DIEWriters.push_back (std::move (DIEWriter));
3719
- if (auto E = convertImpl (DIE, *DIEWriters.back (), DistinctWriter,
3720
- AbbrevWriter, DIEWriters))
3721
- return E;
3722
- return Error::success ();
3723
- }
3724
-
3725
3727
Expected<DIETopLevelRef>
3726
3728
DIEToCASConverter::convert (DWARFDie DIE, ArrayRef<char > HeaderData,
3727
3729
AbbrevSetWriter &AbbrevWriter) {
3728
3730
DistinctDataWriter DistinctWriter;
3729
3731
DistinctWriter.writeData (HeaderData);
3730
3732
SmallVector<std::unique_ptr<DIEDataWriter>> DIEWriters;
3731
- if (Error E =
3732
- convertInNewDIEBlock (DIE, DistinctWriter, AbbrevWriter, DIEWriters))
3733
+ if (Error E = convertImpl (DIE, DistinctWriter, AbbrevWriter, DIEWriters))
3733
3734
return std::move (E);
3734
3735
3735
3736
Expected<DIEAbbrevSetRef> MaybeAbbrevSet =
0 commit comments