Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.h --------*- C++ -*--===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Common functionality for different debug information format backends. |
| 11 | // LLVM currently supports DWARF and CodeView. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGHANDLERBASE_H |
| 16 | #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGHANDLERBASE_H |
| 17 | |
| 18 | #include "AsmPrinterHandler.h" |
| 19 | #include "DbgValueHistoryCalculator.h" |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Optional.h" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LexicalScopes.h" |
Benjamin Kramer | 3a16e2a | 2016-02-18 18:02:48 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstr.h" |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DebugInfoMetadata.h" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 24 | |
| 25 | namespace llvm { |
| 26 | |
| 27 | class AsmPrinter; |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 28 | class MachineInstr; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 29 | class MachineModuleInfo; |
| 30 | |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 31 | /// Represents the location at which a variable is stored. |
| 32 | struct DbgVariableLocation { |
| 33 | /// Offset relative to base register. |
| 34 | int64_t Offset; |
| 35 | |
| 36 | /// Base register. |
| 37 | unsigned Register; |
| 38 | |
| 39 | /// If false, Register is the location. If true, |
| 40 | /// Register+Offset point at the location. |
| 41 | unsigned InMemory : 1; |
| 42 | |
| 43 | /// If false, the location holds the variable's value. |
| 44 | /// If true, the location holds the variable's address. |
| 45 | unsigned Deref : 1; |
| 46 | |
| 47 | /// Present if the location is part of a larger variable. |
| 48 | llvm::Optional<llvm::DIExpression::FragmentInfo> FragmentInfo; |
| 49 | |
Bob Haarman | 1a4cbbe | 2017-08-30 17:50:21 +0000 | [diff] [blame^] | 50 | /// Extract a VariableLocation from a MachineInstr. |
| 51 | /// This will only work if Instruction is a debug value instruction |
| 52 | /// and the associated DIExpression is in one of the supported forms. |
| 53 | /// If these requirements are not met, the returned Optional will not |
| 54 | /// have a value. |
| 55 | static Optional<DbgVariableLocation> |
| 56 | extractFromMachineInstruction(const MachineInstr &Instruction); |
Bob Haarman | 223303c | 2017-08-29 20:59:25 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 59 | /// Base class for debug information backends. Common functionality related to |
| 60 | /// tracking which variables and scopes are alive at a given PC live here. |
Benjamin Kramer | e3b963d | 2016-02-11 15:41:56 +0000 | [diff] [blame] | 61 | class DebugHandlerBase : public AsmPrinterHandler { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 62 | protected: |
| 63 | DebugHandlerBase(AsmPrinter *A); |
| 64 | |
| 65 | /// Target of debug info emission. |
| 66 | AsmPrinter *Asm; |
| 67 | |
| 68 | /// Collected machine module information. |
| 69 | MachineModuleInfo *MMI; |
| 70 | |
| 71 | /// Previous instruction's location information. This is used to |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 72 | /// determine label location to indicate scope boundaries in debug info. |
| 73 | /// We track the previous instruction's source location (if not line 0), |
| 74 | /// whether it was a label, and its parent BB. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 75 | DebugLoc PrevInstLoc; |
| 76 | MCSymbol *PrevLabel = nullptr; |
Paul Robinson | ac7fe5e | 2016-12-12 20:49:11 +0000 | [diff] [blame] | 77 | const MachineBasicBlock *PrevInstBB = nullptr; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 78 | |
| 79 | /// This location indicates end of function prologue and beginning of |
| 80 | /// function body. |
| 81 | DebugLoc PrologEndLoc; |
| 82 | |
| 83 | /// If nonnull, stores the current machine instruction we're processing. |
| 84 | const MachineInstr *CurMI = nullptr; |
| 85 | |
| 86 | LexicalScopes LScopes; |
| 87 | |
| 88 | /// History of DBG_VALUE and clobber instructions for each user |
| 89 | /// variable. Variables are listed in order of appearance. |
| 90 | DbgValueHistoryMap DbgValues; |
| 91 | |
| 92 | /// Maps instruction with label emitted before instruction. |
| 93 | /// FIXME: Make this private from DwarfDebug, we have the necessary accessors |
| 94 | /// for it. |
| 95 | DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn; |
| 96 | |
| 97 | /// Maps instruction with label emitted after instruction. |
| 98 | DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn; |
| 99 | |
| 100 | /// Indentify instructions that are marking the beginning of or |
| 101 | /// ending of a scope. |
| 102 | void identifyScopeMarkers(); |
| 103 | |
| 104 | /// Ensure that a label will be emitted before MI. |
| 105 | void requestLabelBeforeInsn(const MachineInstr *MI) { |
| 106 | LabelsBeforeInsn.insert(std::make_pair(MI, nullptr)); |
| 107 | } |
| 108 | |
| 109 | /// Ensure that a label will be emitted after MI. |
| 110 | void requestLabelAfterInsn(const MachineInstr *MI) { |
| 111 | LabelsAfterInsn.insert(std::make_pair(MI, nullptr)); |
| 112 | } |
| 113 | |
David Blaikie | b2fbb4b | 2017-02-16 18:48:33 +0000 | [diff] [blame] | 114 | virtual void beginFunctionImpl(const MachineFunction *MF) = 0; |
| 115 | virtual void endFunctionImpl(const MachineFunction *MF) = 0; |
| 116 | virtual void skippedNonDebugFunction() {} |
| 117 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 118 | // AsmPrinterHandler overrides. |
| 119 | public: |
| 120 | void beginInstruction(const MachineInstr *MI) override; |
| 121 | void endInstruction() override; |
| 122 | |
| 123 | void beginFunction(const MachineFunction *MF) override; |
| 124 | void endFunction(const MachineFunction *MF) override; |
| 125 | |
| 126 | /// Return Label preceding the instruction. |
| 127 | MCSymbol *getLabelBeforeInsn(const MachineInstr *MI); |
| 128 | |
| 129 | /// Return Label immediately following the instruction. |
| 130 | MCSymbol *getLabelAfterInsn(const MachineInstr *MI); |
| 131 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 132 | /// Determine the relative position of the fragments described by P1 and P2. |
| 133 | /// Returns -1 if P1 is entirely before P2, 0 if P1 and P2 overlap, 1 if P1 is |
| 134 | /// entirely after P2. |
| 135 | static int fragmentCmp(const DIExpression *P1, const DIExpression *P2); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 136 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 137 | /// Determine whether two variable fragments overlap. |
| 138 | static bool fragmentsOverlap(const DIExpression *P1, const DIExpression *P2); |
Amjad Aboud | acee568 | 2016-07-12 12:06:34 +0000 | [diff] [blame] | 139 | |
| 140 | /// If this type is derived from a base type then return base type size. |
| 141 | static uint64_t getBaseTypeSize(const DITypeRef TyRef); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | } |
| 145 | |
| 146 | #endif |