LLVM 22.0.0git
DWARFCFIFunctionFrameStreamer.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/MC/MCContext.h"
12#include "llvm/MC/MCDwarf.h"
13#include "llvm/MC/MCInst.h"
14#include "llvm/MC/MCInstrInfo.h"
15#include "llvm/MC/MCStreamer.h"
16#include <optional>
17
18using namespace llvm;
19
20void CFIFunctionFrameStreamer::updateReceiver(
21 const std::optional<MCInst> &NewInst) {
23 "should have an unfinished DWARF frame here");
24 assert(!FrameIndices.empty() &&
25 "there should be an index available for the current frame");
26 assert(FrameIndices.size() == LastInstructions.size());
27 assert(LastInstructions.size() == LastDirectiveIndices.size());
28
29 auto Frames = getDwarfFrameInfos();
30 assert(FrameIndices.back() < Frames.size());
31 unsigned LastDirectiveIndex = LastDirectiveIndices.back();
32 unsigned CurrentDirectiveIndex =
33 Frames[FrameIndices.back()].Instructions.size();
34 assert(CurrentDirectiveIndex >= LastDirectiveIndex);
35
36 const MCDwarfFrameInfo *LastFrame = &Frames[FrameIndices.back()];
38 if (LastDirectiveIndex < CurrentDirectiveIndex) {
39 Directives = ArrayRef<MCCFIInstruction>(LastFrame->Instructions);
40 Directives =
41 Directives.drop_front(LastDirectiveIndex)
42 .drop_back(LastFrame->Instructions.size() - CurrentDirectiveIndex);
43 }
44
45 auto MaybeLastInstruction = LastInstructions.back();
46 if (MaybeLastInstruction)
47 // The directives are associated with an instruction.
48 Receiver->emitInstructionAndDirectives(*MaybeLastInstruction, Directives);
49 else
50 // The directives are the prologue directives.
51 Receiver->startFunctionFrame(false /* TODO: should put isEH here */,
52 Directives);
53
54 // Update the internal state for the top frame.
55 LastInstructions.back() = NewInst;
56 LastDirectiveIndices.back() = CurrentDirectiveIndex;
57}
58
60 const MCSubtargetInfo &STI) {
62 // Send the last instruction with the unsent directives already in the frame
63 // to the receiver.
64 updateReceiver(Inst);
65}
66
68 LastInstructions.push_back(std::nullopt);
69 LastDirectiveIndices.push_back(0);
70 FrameIndices.push_back(getNumFrameInfos());
71
73}
74
76 // Send the last instruction with the final directives of the current frame to
77 // the receiver.
78 updateReceiver(std::nullopt);
79
80 assert(!FrameIndices.empty() && "There should be at least one frame to pop");
81 LastDirectiveIndices.pop_back();
82 LastInstructions.pop_back();
83 FrameIndices.pop_back();
84
85 Receiver->finishFunctionFrame();
86
88}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares CFIFunctionFrameStreamer class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:200
void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void emitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame) override
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
bool hasUnfinishedDwarfFrameInfo()
Definition: MCStreamer.cpp:261
virtual void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame)
Definition: MCStreamer.cpp:444
unsigned getNumFrameInfos()
Definition: MCStreamer.cpp:114
virtual void emitCFIEndProcImpl(MCDwarfFrameInfo &CurFrame)
Definition: MCStreamer.cpp:455
ArrayRef< MCDwarfFrameInfo > getDwarfFrameInfos() const
Definition: MCStreamer.cpp:115
Generic base class for all target subtargets.
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::vector< MCCFIInstruction > Instructions
Definition: MCDwarf.h:776