Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
35271 views
//===- llvm/CodeGen/DwarfExpression.h - Dwarf Compile Unit ------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains support for writing dwarf compile unit.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H13#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H1415#include "ByteStreamer.h"16#include "llvm/ADT/ArrayRef.h"17#include "llvm/ADT/SmallVector.h"18#include "llvm/IR/DebugInfoMetadata.h"19#include <cassert>20#include <cstdint>21#include <iterator>22#include <optional>2324namespace llvm {2526class AsmPrinter;27class APInt;28class DwarfCompileUnit;29class DIELoc;30class TargetRegisterInfo;31class MachineLocation;3233/// Base class containing the logic for constructing DWARF expressions34/// independently of whether they are emitted into a DIE or into a .debug_loc35/// entry.36///37/// Some DWARF operations, e.g. DW_OP_entry_value, need to calculate the size38/// of a succeeding DWARF block before the latter is emitted to the output.39/// To handle such cases, data can conditionally be emitted to a temporary40/// buffer, which can later on be committed to the main output. The size of the41/// temporary buffer is queryable, allowing for the size of the data to be42/// emitted before the data is committed.43class DwarfExpression {44protected:45/// Holds information about all subregisters comprising a register location.46struct Register {47int DwarfRegNo;48unsigned SubRegSize;49const char *Comment;5051/// Create a full register, no extra DW_OP_piece operators necessary.52static Register createRegister(int RegNo, const char *Comment) {53return {RegNo, 0, Comment};54}5556/// Create a subregister that needs a DW_OP_piece operator with SizeInBits.57static Register createSubRegister(int RegNo, unsigned SizeInBits,58const char *Comment) {59return {RegNo, SizeInBits, Comment};60}6162bool isSubRegister() const { return SubRegSize; }63};6465/// Whether we are currently emitting an entry value operation.66bool IsEmittingEntryValue = false;6768DwarfCompileUnit &CU;6970/// The register location, if any.71SmallVector<Register, 2> DwarfRegs;7273/// Current Fragment Offset in Bits.74uint64_t OffsetInBits = 0;7576/// Sometimes we need to add a DW_OP_bit_piece to describe a subregister.77unsigned SubRegisterSizeInBits : 16;78unsigned SubRegisterOffsetInBits : 16;7980/// The kind of location description being produced.81enum { Unknown = 0, Register, Memory, Implicit };8283/// Additional location flags which may be combined with any location kind.84/// Currently, entry values are not supported for the Memory location kind.85enum { EntryValue = 1 << 0, Indirect = 1 << 1, CallSiteParamValue = 1 << 2 };8687unsigned LocationKind : 3;88unsigned SavedLocationKind : 3;89unsigned LocationFlags : 3;90unsigned DwarfVersion : 4;9192public:93/// Set the location (\p Loc) and \ref DIExpression (\p DIExpr) to describe.94void setLocation(const MachineLocation &Loc, const DIExpression *DIExpr);9596bool isUnknownLocation() const { return LocationKind == Unknown; }9798bool isMemoryLocation() const { return LocationKind == Memory; }99100bool isRegisterLocation() const { return LocationKind == Register; }101102bool isImplicitLocation() const { return LocationKind == Implicit; }103104bool isEntryValue() const { return LocationFlags & EntryValue; }105106bool isIndirect() const { return LocationFlags & Indirect; }107108bool isParameterValue() { return LocationFlags & CallSiteParamValue; }109110std::optional<uint8_t> TagOffset;111112protected:113/// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed114/// to represent a subregister.115void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) {116assert(SizeInBits < 65536 && OffsetInBits < 65536);117SubRegisterSizeInBits = SizeInBits;118SubRegisterOffsetInBits = OffsetInBits;119}120121/// Add masking operations to stencil out a subregister.122void maskSubRegister();123124/// Output a dwarf operand and an optional assembler comment.125virtual void emitOp(uint8_t Op, const char *Comment = nullptr) = 0;126127/// Emit a raw signed value.128virtual void emitSigned(int64_t Value) = 0;129130/// Emit a raw unsigned value.131virtual void emitUnsigned(uint64_t Value) = 0;132133virtual void emitData1(uint8_t Value) = 0;134135virtual void emitBaseTypeRef(uint64_t Idx) = 0;136137/// Start emitting data to the temporary buffer. The data stored in the138/// temporary buffer can be committed to the main output using139/// commitTemporaryBuffer().140virtual void enableTemporaryBuffer() = 0;141142/// Disable emission to the temporary buffer. This does not commit data143/// in the temporary buffer to the main output.144virtual void disableTemporaryBuffer() = 0;145146/// Return the emitted size, in number of bytes, for the data stored in the147/// temporary buffer.148virtual unsigned getTemporaryBufferSize() = 0;149150/// Commit the data stored in the temporary buffer to the main output.151virtual void commitTemporaryBuffer() = 0;152153/// Emit a normalized unsigned constant.154void emitConstu(uint64_t Value);155156/// Return whether the given machine register is the frame register in the157/// current function.158virtual bool isFrameRegister(const TargetRegisterInfo &TRI,159llvm::Register MachineReg) = 0;160161/// Emit a DW_OP_reg operation. Note that this is only legal inside a DWARF162/// register location description.163void addReg(int DwarfReg, const char *Comment = nullptr);164165/// Emit a DW_OP_breg operation.166void addBReg(int DwarfReg, int Offset);167168/// Emit DW_OP_fbreg <Offset>.169void addFBReg(int Offset);170171/// Emit a partial DWARF register operation.172///173/// \param MachineReg The register number.174/// \param MaxSize If the register must be composed from175/// sub-registers this is an upper bound176/// for how many bits the emitted DW_OP_piece177/// may cover.178///179/// If size and offset is zero an operation for the entire register is180/// emitted: Some targets do not provide a DWARF register number for every181/// register. If this is the case, this function will attempt to emit a DWARF182/// register by emitting a fragment of a super-register or by piecing together183/// multiple subregisters that alias the register.184///185/// \return false if no DWARF register exists for MachineReg.186bool addMachineReg(const TargetRegisterInfo &TRI, llvm::Register MachineReg,187unsigned MaxSize = ~1U);188189/// Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment.190/// \param OffsetInBits This is an optional offset into the location that191/// is at the top of the DWARF stack.192void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);193194/// Emit a shift-right dwarf operation.195void addShr(unsigned ShiftBy);196197/// Emit a bitwise and dwarf operation.198void addAnd(unsigned Mask);199200/// Emit a DW_OP_stack_value, if supported.201///202/// The proper way to describe a constant value is DW_OP_constu <const>,203/// DW_OP_stack_value. Unfortunately, DW_OP_stack_value was not available204/// until DWARF 4, so we will continue to generate DW_OP_constu <const> for205/// DWARF 2 and DWARF 3. Technically, this is incorrect since DW_OP_const206/// <const> actually describes a value at a constant address, not a constant207/// value. However, in the past there was no better way to describe a208/// constant value, so the producers and consumers started to rely on209/// heuristics to disambiguate the value vs. location status of the210/// expression. See PR21176 for more details.211void addStackValue();212213/// Finalize an entry value by emitting its size operand, and committing the214/// DWARF block which has been emitted to the temporary buffer.215void finalizeEntryValue();216217/// Cancel the emission of an entry value.218void cancelEntryValue();219220~DwarfExpression() = default;221222public:223DwarfExpression(unsigned DwarfVersion, DwarfCompileUnit &CU)224: CU(CU), SubRegisterSizeInBits(0), SubRegisterOffsetInBits(0),225LocationKind(Unknown), SavedLocationKind(Unknown),226LocationFlags(Unknown), DwarfVersion(DwarfVersion) {}227228/// This needs to be called last to commit any pending changes.229void finalize();230231/// Emit a signed constant.232void addSignedConstant(int64_t Value);233234/// Emit an unsigned constant.235void addUnsignedConstant(uint64_t Value);236237/// Emit an unsigned constant.238void addUnsignedConstant(const APInt &Value);239240/// Emit an floating point constant.241void addConstantFP(const APFloat &Value, const AsmPrinter &AP);242243/// Lock this down to become a memory location description.244void setMemoryLocationKind() {245assert(isUnknownLocation());246LocationKind = Memory;247}248249/// Lock this down to become an entry value location.250void setEntryValueFlags(const MachineLocation &Loc);251252/// Lock this down to become a call site parameter location.253void setCallSiteParamValueFlag() { LocationFlags |= CallSiteParamValue; }254255/// Emit a machine register location. As an optimization this may also consume256/// the prefix of a DwarfExpression if a more efficient representation for257/// combining the register location and the first operation exists.258///259/// \param FragmentOffsetInBits If this is one fragment out of a260/// fragmented261/// location, this is the offset of the262/// fragment inside the entire variable.263/// \return false if no DWARF register exists264/// for MachineReg.265bool addMachineRegExpression(const TargetRegisterInfo &TRI,266DIExpressionCursor &Expr,267llvm::Register MachineReg,268unsigned FragmentOffsetInBits = 0);269270/// Begin emission of an entry value dwarf operation. The entry value's271/// first operand is the size of the DWARF block (its second operand),272/// which needs to be calculated at time of emission, so we don't emit273/// any operands here.274void beginEntryValueExpression(DIExpressionCursor &ExprCursor);275276/// Return the index of a base type with the given properties and277/// create one if necessary.278unsigned getOrCreateBaseType(unsigned BitSize, dwarf::TypeKind Encoding);279280/// Emit all remaining operations in the DIExpressionCursor. The281/// cursor must not contain any DW_OP_LLVM_arg operations.282void addExpression(DIExpressionCursor &&Expr);283284/// Emit all remaining operations in the DIExpressionCursor.285/// DW_OP_LLVM_arg operations are resolved by calling (\p InsertArg).286//287/// \return false if any call to (\p InsertArg) returns false.288bool addExpression(289DIExpressionCursor &&Expr,290llvm::function_ref<bool(unsigned, DIExpressionCursor &)> InsertArg);291292/// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to293/// the fragment described by \c Expr.294void addFragmentOffset(const DIExpression *Expr);295296void emitLegacySExt(unsigned FromBits);297void emitLegacyZExt(unsigned FromBits);298299/// Emit location information expressed via WebAssembly location + offset300/// The Index is an identifier for locals, globals or operand stack.301void addWasmLocation(unsigned Index, uint64_t Offset);302};303304/// DwarfExpression implementation for .debug_loc entries.305class DebugLocDwarfExpression final : public DwarfExpression {306307struct TempBuffer {308SmallString<32> Bytes;309std::vector<std::string> Comments;310BufferByteStreamer BS;311312TempBuffer(bool GenerateComments) : BS(Bytes, Comments, GenerateComments) {}313};314315std::unique_ptr<TempBuffer> TmpBuf;316BufferByteStreamer &OutBS;317bool IsBuffering = false;318319/// Return the byte streamer that currently is being emitted to.320ByteStreamer &getActiveStreamer() { return IsBuffering ? TmpBuf->BS : OutBS; }321322void emitOp(uint8_t Op, const char *Comment = nullptr) override;323void emitSigned(int64_t Value) override;324void emitUnsigned(uint64_t Value) override;325void emitData1(uint8_t Value) override;326void emitBaseTypeRef(uint64_t Idx) override;327328void enableTemporaryBuffer() override;329void disableTemporaryBuffer() override;330unsigned getTemporaryBufferSize() override;331void commitTemporaryBuffer() override;332333bool isFrameRegister(const TargetRegisterInfo &TRI,334llvm::Register MachineReg) override;335336public:337DebugLocDwarfExpression(unsigned DwarfVersion, BufferByteStreamer &BS,338DwarfCompileUnit &CU)339: DwarfExpression(DwarfVersion, CU), OutBS(BS) {}340};341342/// DwarfExpression implementation for singular DW_AT_location.343class DIEDwarfExpression final : public DwarfExpression {344const AsmPrinter &AP;345DIELoc &OutDIE;346DIELoc TmpDIE;347bool IsBuffering = false;348349/// Return the DIE that currently is being emitted to.350DIELoc &getActiveDIE() { return IsBuffering ? TmpDIE : OutDIE; }351352void emitOp(uint8_t Op, const char *Comment = nullptr) override;353void emitSigned(int64_t Value) override;354void emitUnsigned(uint64_t Value) override;355void emitData1(uint8_t Value) override;356void emitBaseTypeRef(uint64_t Idx) override;357358void enableTemporaryBuffer() override;359void disableTemporaryBuffer() override;360unsigned getTemporaryBufferSize() override;361void commitTemporaryBuffer() override;362363bool isFrameRegister(const TargetRegisterInfo &TRI,364llvm::Register MachineReg) override;365366public:367DIEDwarfExpression(const AsmPrinter &AP, DwarfCompileUnit &CU, DIELoc &DIE);368369DIELoc *finalize() {370DwarfExpression::finalize();371return &OutDIE;372}373};374375} // end namespace llvm376377#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H378379380