@@ -1982,14 +1982,16 @@ struct DIEToCASConverter {
1982
1982
bool IsLittleEndian;
1983
1983
uint8_t AddressSize;
1984
1984
1985
- Error convertInNewDIEBlock (
1986
- DWARFDie DIE, DistinctDataWriter &DistinctWriter,
1987
- AbbrevSetWriter &AbbrevWriter,
1988
- SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters);
1985
+ struct ParentAndChildDIE {
1986
+ DWARFDie Parent;
1987
+ bool ParentAlreadyWritten;
1988
+ DIEDataWriter &Writer;
1989
+ std::optional<DWARFDie> Child;
1990
+ };
1989
1991
1990
1992
Error
1991
- convertImpl (DWARFDie & DIE, DIEDataWriter &DIEWriter ,
1992
- DistinctDataWriter &DistinctWriter, AbbrevSetWriter &AbbrevWriter,
1993
+ convertImpl (DWARFDie DIE, DistinctDataWriter &DistinctWriter ,
1994
+ AbbrevSetWriter &AbbrevWriter,
1993
1995
SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters);
1994
1996
};
1995
1997
@@ -3006,6 +3008,12 @@ static void writeDIEAttrs(DWARFDie &DIE, ArrayRef<char> DebugInfoData,
3006
3008
}
3007
3009
}
3008
3010
3011
+ static void
3012
+ pushNewDIEWriter (SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters) {
3013
+ auto DIEWriter = std::make_unique<DIEDataWriter>();
3014
+ DIEWriters.push_back (std::move (DIEWriter));
3015
+ }
3016
+
3009
3017
// / Creates an abbreviation for DIE using AbbrevWriter.
3010
3018
// / Stores the contents of the DIE using DistinctWriter and DIEWriter following
3011
3019
// / the format:
@@ -3021,61 +3029,54 @@ static void writeDIEAttrs(DWARFDie &DIE, ArrayRef<char> DebugInfoData,
3021
3029
// / DIEAbbrevSetRef block. In this case, raw_data should be interpreted
3022
3030
// / according to the corresponding DIEAbbrevRefs block.
3023
3031
Error DIEToCASConverter::convertImpl (
3024
- DWARFDie & DIE, DIEDataWriter &DIEWriter , DistinctDataWriter &DistinctWriter,
3032
+ DWARFDie DIE, DistinctDataWriter &DistinctWriter,
3025
3033
AbbrevSetWriter &AbbrevWriter,
3026
3034
SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters) {
3027
- Expected<unsigned > MaybeAbbrevIndex =
3028
- AbbrevWriter.createAbbrevEntry (DIE, CASBuilder);
3029
- if (!MaybeAbbrevIndex)
3030
- return MaybeAbbrevIndex.takeError ();
3031
-
3032
- DistinctWriter.writeULEB128 (encodeAbbrevIndex (*MaybeAbbrevIndex));
3033
- writeDIEAttrs (DIE, DebugInfoData, DIEWriter, DistinctWriter, IsLittleEndian,
3034
- AddressSize);
3035
-
3036
- for (DWARFDie Child = DIE.getFirstChild (); Child;
3037
- Child = Child.getSibling ()) {
3038
- dwarf::Tag ChildTag = Child.getTag ();
3039
- if (ChildTag == dwarf::Tag::DW_TAG_null) {
3040
- DistinctWriter.writeULEB128 (getEndOfDIESiblingsMarker ());
3041
- break ;
3035
+ SmallVector<ParentAndChildDIE> DIEStack;
3036
+ pushNewDIEWriter (DIEWriters);
3037
+ DIEStack.push_back ({DIE, false , *DIEWriters.back (), std::nullopt});
3038
+ while (!DIEStack.empty ()) {
3039
+ auto ParentAndChild = DIEStack.pop_back_val ();
3040
+ DWARFDie CurrDIE = ParentAndChild.Parent ;
3041
+
3042
+ if (!ParentAndChild.ParentAlreadyWritten ) {
3043
+ Expected<unsigned > MaybeAbbrevIndex =
3044
+ AbbrevWriter.createAbbrevEntry (CurrDIE, CASBuilder);
3045
+ if (!MaybeAbbrevIndex)
3046
+ return MaybeAbbrevIndex.takeError ();
3047
+
3048
+ DistinctWriter.writeULEB128 (encodeAbbrevIndex (*MaybeAbbrevIndex));
3049
+ writeDIEAttrs (CurrDIE, DebugInfoData, ParentAndChild.Writer ,
3050
+ DistinctWriter, IsLittleEndian, AddressSize);
3042
3051
}
3043
3052
3044
- // FIXME: don't use recursion.
3045
- if (shouldCreateSeparateBlockFor (Child)) {
3046
- DistinctWriter.writeULEB128 (getDIEInAnotherBlockMarker ());
3047
- if (auto E = convertInNewDIEBlock (Child, DistinctWriter, AbbrevWriter,
3048
- DIEWriters))
3049
- return E;
3050
- continue ;
3053
+ DWARFDie Child = ParentAndChild.Child ? ParentAndChild.Child ->getSibling ()
3054
+ : CurrDIE.getFirstChild ();
3055
+ if (Child) {
3056
+ dwarf::Tag ChildTag = Child.getTag ();
3057
+ if (ChildTag == dwarf::Tag::DW_TAG_null)
3058
+ DistinctWriter.writeULEB128 (getEndOfDIESiblingsMarker ());
3059
+ else if (shouldCreateSeparateBlockFor (Child)) {
3060
+ DistinctWriter.writeULEB128 (getDIEInAnotherBlockMarker ());
3061
+ DIEStack.push_back ({CurrDIE, true , ParentAndChild.Writer , Child});
3062
+ pushNewDIEWriter (DIEWriters);
3063
+ DIEStack.push_back ({Child, false , *DIEWriters.back (), std::nullopt});
3064
+ } else {
3065
+ DIEStack.push_back ({CurrDIE, true , ParentAndChild.Writer , Child});
3066
+ DIEStack.push_back ({Child, false , ParentAndChild.Writer , std::nullopt});
3067
+ }
3051
3068
}
3052
- if (auto E = convertImpl (Child, DIEWriter, DistinctWriter, AbbrevWriter,
3053
- DIEWriters))
3054
- return E;
3055
3069
}
3056
3070
return Error::success ();
3057
3071
}
3058
3072
3059
- Error DIEToCASConverter::convertInNewDIEBlock (
3060
- DWARFDie DIE, DistinctDataWriter &DistinctWriter,
3061
- AbbrevSetWriter &AbbrevWriter,
3062
- SmallVectorImpl<std::unique_ptr<DIEDataWriter>> &DIEWriters) {
3063
- auto DIEWriter = std::make_unique<DIEDataWriter>();
3064
- DIEWriters.push_back (std::move (DIEWriter));
3065
- if (auto E = convertImpl (DIE, *DIEWriters.back (), DistinctWriter,
3066
- AbbrevWriter, DIEWriters))
3067
- return E;
3068
- return Error::success ();
3069
- }
3070
-
3071
3073
Expected<DIETopLevelRef>
3072
3074
DIEToCASConverter::convert (DWARFDie DIE, ArrayRef<char > HeaderData,
3073
3075
AbbrevSetWriter &AbbrevWriter) {
3074
3076
DistinctDataWriter DistinctWriter;
3075
3077
DistinctWriter.writeData (HeaderData);
3076
3078
SmallVector<std::unique_ptr<DIEDataWriter>> DIEWriters;
3077
- if (Error E =
3078
- convertInNewDIEBlock (DIE, DistinctWriter, AbbrevWriter, DIEWriters))
3079
+ if (Error E = convertImpl (DIE, DistinctWriter, AbbrevWriter, DIEWriters))
3079
3080
return std::move (E);
3080
3081
3081
3082
Expected<DIEAbbrevSetRef> MaybeAbbrevSet =
0 commit comments