|
10 | 10 | #include "llvm/ADT/ArrayRef.h" |
11 | 11 | #include "llvm/MC/MCContext.h" |
12 | 12 | #include "llvm/MC/MCDwarf.h" |
| 13 | +#include "llvm/MC/MCInst.h" |
13 | 14 | #include "llvm/MC/MCInstrInfo.h" |
14 | 15 | #include "llvm/MC/MCStreamer.h" |
15 | 16 | #include <optional> |
16 | 17 |
|
17 | 18 | using namespace llvm; |
18 | 19 |
|
19 | | -std::pair<unsigned, unsigned> |
20 | | -CFIFunctionFrameStreamer::updateDirectivesRange() { |
21 | | - auto Frames = getDwarfFrameInfos(); |
22 | | - unsigned CurrentCFIDirectiveIndex = 0; |
23 | | - if (hasUnfinishedDwarfFrameInfo()) { |
24 | | - assert(!FrameIndices.empty() && "FunctionUnitStreamer frame indices should " |
25 | | - "be synced with MCStreamer's"); |
26 | | - assert(FrameIndices.back() < Frames.size()); |
27 | | - CurrentCFIDirectiveIndex = Frames[FrameIndices.back()].Instructions.size(); |
28 | | - } |
29 | | - |
30 | | - assert(CurrentCFIDirectiveIndex >= LastDirectiveIndex); |
31 | | - std::pair<unsigned, unsigned> CFIDirectivesRange(LastDirectiveIndex, |
32 | | - CurrentCFIDirectiveIndex); |
33 | | - LastDirectiveIndex = CurrentCFIDirectiveIndex; |
34 | | - return CFIDirectivesRange; |
35 | | -} |
36 | | - |
37 | | -void CFIFunctionFrameStreamer::updateReceiver() { |
38 | | - if (FrameIndices.empty()) { |
39 | | - auto CFIDirectivesRange = updateDirectivesRange(); |
40 | | - assert(CFIDirectivesRange.first == CFIDirectivesRange.second && |
41 | | - "CFI directives should be in some frame"); |
42 | | - return; |
43 | | - } |
| 20 | +void CFIFunctionFrameStreamer::updateReceiver( |
| 21 | + const std::optional<MCInst> &NewInst) { |
| 22 | + assert(!FrameIndices.empty() && hasUnfinishedDwarfFrameInfo() && |
| 23 | + "FunctionUnitStreamer frame indices should be synced with " |
| 24 | + "MCStreamer's"); //! FIXME split this assertions and also check another |
| 25 | + //! vectors |
| 26 | + //! Add tests for nested frames |
44 | 27 |
|
45 | | - const MCDwarfFrameInfo *LastFrame = |
46 | | - &getDwarfFrameInfos()[FrameIndices.back()]; |
| 28 | + auto Frames = getDwarfFrameInfos(); |
| 29 | + assert(FrameIndices.back() < Frames.size()); |
| 30 | + unsigned LastDirectiveIndex = FrameLastDirectiveIndices.back(); |
| 31 | + unsigned CurrentDirectiveIndex = |
| 32 | + Frames[FrameIndices.back()].Instructions.size(); |
| 33 | + assert(CurrentDirectiveIndex >= LastDirectiveIndex); |
47 | 34 |
|
48 | | - auto DirectivesRange = updateDirectivesRange(); |
| 35 | + const MCDwarfFrameInfo *LastFrame = &Frames[FrameIndices.back()]; |
49 | 36 | ArrayRef<MCCFIInstruction> Directives; |
50 | | - if (DirectivesRange.first < DirectivesRange.second) { |
| 37 | + if (LastDirectiveIndex < CurrentDirectiveIndex) { |
51 | 38 | Directives = ArrayRef<MCCFIInstruction>(LastFrame->Instructions); |
52 | 39 | Directives = |
53 | | - Directives.drop_front(DirectivesRange.first) |
54 | | - .drop_back(LastFrame->Instructions.size() - DirectivesRange.second); |
| 40 | + Directives.drop_front(LastDirectiveIndex) |
| 41 | + .drop_back(LastFrame->Instructions.size() - CurrentDirectiveIndex); |
55 | 42 | } |
56 | 43 |
|
57 | | - if (LastInstruction) { |
58 | | - Receiver->emitInstructionAndDirectives(LastInstruction.value(), Directives); |
59 | | - } else { |
| 44 | + auto MaybeLastInstruction = FrameLastInstructions.back(); |
| 45 | + if (MaybeLastInstruction) |
| 46 | + Receiver->emitInstructionAndDirectives(*MaybeLastInstruction, Directives); |
| 47 | + else |
60 | 48 | Receiver->startFunctionFrame(false /* TODO: should put isEH here */, |
61 | 49 | Directives); |
62 | | - } |
| 50 | + |
| 51 | + FrameLastInstructions.back() = NewInst; |
| 52 | + FrameLastDirectiveIndices.back() = CurrentDirectiveIndex; |
63 | 53 | } |
64 | 54 |
|
65 | 55 | void CFIFunctionFrameStreamer::emitInstruction(const MCInst &Inst, |
66 | 56 | const MCSubtargetInfo &STI) { |
67 | | - updateReceiver(); |
68 | | - LastInstruction = Inst; |
| 57 | + if (hasUnfinishedDwarfFrameInfo()) { |
| 58 | + updateReceiver(Inst); |
| 59 | + } |
69 | 60 | } |
70 | 61 |
|
71 | 62 | void CFIFunctionFrameStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) { |
72 | | - updateReceiver(); |
| 63 | + FrameLastInstructions.push_back(std::nullopt); |
| 64 | + FrameLastDirectiveIndices.push_back(0); |
73 | 65 | FrameIndices.push_back(getNumFrameInfos()); |
74 | | - LastInstruction = std::nullopt; |
75 | | - LastDirectiveIndex = 0; |
| 66 | + |
76 | 67 | MCStreamer::emitCFIStartProcImpl(Frame); |
77 | 68 | } |
78 | 69 |
|
79 | 70 | void CFIFunctionFrameStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame) { |
80 | | - updateReceiver(); |
| 71 | + updateReceiver(std::nullopt); |
81 | 72 |
|
82 | 73 | assert(!FrameIndices.empty() && "There should be at least one frame to pop"); |
| 74 | + FrameLastDirectiveIndices.pop_back(); |
| 75 | + FrameLastInstructions.pop_back(); |
83 | 76 | FrameIndices.pop_back(); |
84 | 77 |
|
| 78 | + dbgs() << "finishing frame\n"; |
85 | 79 | Receiver->finishFunctionFrame(); |
86 | 80 |
|
87 | | - LastInstruction = std::nullopt; |
88 | | - LastDirectiveIndex = 0; |
89 | | - if (!FrameIndices.empty()) { |
90 | | - auto DwarfFrameInfos = getDwarfFrameInfos(); |
91 | | - assert(FrameIndices.back() < DwarfFrameInfos.size()); |
92 | | - |
93 | | - LastDirectiveIndex = |
94 | | - DwarfFrameInfos[FrameIndices.back()].Instructions.size(); |
95 | | - } |
96 | 81 | MCStreamer::emitCFIEndProcImpl(CurFrame); |
97 | 82 | } |
0 commit comments