blob: cdf8dc72b0753180cf82741cdda098ece7e3efa0 [file] [log] [blame]
Reid Klecknerf9c275f2016-02-10 20:55:49 +00001//===-- 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.
Yonghong Songf4873342018-11-30 16:54:43 +000011// LLVM currently supports DWARF and CodeView.
Reid Klecknerf9c275f2016-02-10 20:55:49 +000012//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGHANDLERBASE_H
16#define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGHANDLERBASE_H
17
18#include "AsmPrinterHandler.h"
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000019#include "DbgEntityHistoryCalculator.h"
Bob Haarman223303c2017-08-29 20:59:25 +000020#include "llvm/ADT/Optional.h"
Reid Klecknerf9c275f2016-02-10 20:55:49 +000021#include "llvm/CodeGen/LexicalScopes.h"
Benjamin Kramer3a16e2a2016-02-18 18:02:48 +000022#include "llvm/CodeGen/MachineInstr.h"
Bob Haarman223303c2017-08-29 20:59:25 +000023#include "llvm/IR/DebugInfoMetadata.h"
Reid Klecknerf9c275f2016-02-10 20:55:49 +000024
25namespace llvm {
26
27class AsmPrinter;
Bob Haarman223303c2017-08-29 20:59:25 +000028class MachineInstr;
Reid Klecknerf9c275f2016-02-10 20:55:49 +000029class MachineModuleInfo;
30
Bob Haarman223303c2017-08-29 20:59:25 +000031/// Represents the location at which a variable is stored.
32struct DbgVariableLocation {
Bob Haarman223303c2017-08-29 20:59:25 +000033 /// Base register.
34 unsigned Register;
35
Reid Kleckner08f5fd52017-08-31 15:56:49 +000036 /// Chain of offsetted loads necessary to load the value if it lives in
37 /// memory. Every load except for the last is pointer-sized.
38 SmallVector<int64_t, 1> LoadChain;
Bob Haarman223303c2017-08-29 20:59:25 +000039
40 /// Present if the location is part of a larger variable.
41 llvm::Optional<llvm::DIExpression::FragmentInfo> FragmentInfo;
42
Bob Haarman1a4cbbe2017-08-30 17:50:21 +000043 /// Extract a VariableLocation from a MachineInstr.
44 /// This will only work if Instruction is a debug value instruction
45 /// and the associated DIExpression is in one of the supported forms.
46 /// If these requirements are not met, the returned Optional will not
47 /// have a value.
48 static Optional<DbgVariableLocation>
49 extractFromMachineInstruction(const MachineInstr &Instruction);
Bob Haarman223303c2017-08-29 20:59:25 +000050};
51
Reid Klecknerf9c275f2016-02-10 20:55:49 +000052/// Base class for debug information backends. Common functionality related to
53/// tracking which variables and scopes are alive at a given PC live here.
Benjamin Kramere3b963d2016-02-11 15:41:56 +000054class DebugHandlerBase : public AsmPrinterHandler {
Reid Klecknerf9c275f2016-02-10 20:55:49 +000055protected:
56 DebugHandlerBase(AsmPrinter *A);
57
58 /// Target of debug info emission.
59 AsmPrinter *Asm;
60
61 /// Collected machine module information.
62 MachineModuleInfo *MMI;
63
64 /// Previous instruction's location information. This is used to
Paul Robinsonac7fe5e2016-12-12 20:49:11 +000065 /// determine label location to indicate scope boundaries in debug info.
66 /// We track the previous instruction's source location (if not line 0),
67 /// whether it was a label, and its parent BB.
Reid Klecknerf9c275f2016-02-10 20:55:49 +000068 DebugLoc PrevInstLoc;
69 MCSymbol *PrevLabel = nullptr;
Paul Robinsonac7fe5e2016-12-12 20:49:11 +000070 const MachineBasicBlock *PrevInstBB = nullptr;
Reid Klecknerf9c275f2016-02-10 20:55:49 +000071
72 /// This location indicates end of function prologue and beginning of
73 /// function body.
74 DebugLoc PrologEndLoc;
75
76 /// If nonnull, stores the current machine instruction we're processing.
77 const MachineInstr *CurMI = nullptr;
78
79 LexicalScopes LScopes;
80
81 /// History of DBG_VALUE and clobber instructions for each user
82 /// variable. Variables are listed in order of appearance.
83 DbgValueHistoryMap DbgValues;
84
Hsiangkai Wang2532ac82018-08-17 15:22:04 +000085 /// Mapping of inlined labels and DBG_LABEL machine instruction.
86 DbgLabelInstrMap DbgLabels;
87
Reid Klecknerf9c275f2016-02-10 20:55:49 +000088 /// Maps instruction with label emitted before instruction.
89 /// FIXME: Make this private from DwarfDebug, we have the necessary accessors
90 /// for it.
91 DenseMap<const MachineInstr *, MCSymbol *> LabelsBeforeInsn;
92
93 /// Maps instruction with label emitted after instruction.
94 DenseMap<const MachineInstr *, MCSymbol *> LabelsAfterInsn;
95
96 /// Indentify instructions that are marking the beginning of or
97 /// ending of a scope.
98 void identifyScopeMarkers();
99
100 /// Ensure that a label will be emitted before MI.
101 void requestLabelBeforeInsn(const MachineInstr *MI) {
102 LabelsBeforeInsn.insert(std::make_pair(MI, nullptr));
103 }
104
105 /// Ensure that a label will be emitted after MI.
106 void requestLabelAfterInsn(const MachineInstr *MI) {
107 LabelsAfterInsn.insert(std::make_pair(MI, nullptr));
108 }
109
David Blaikieb2fbb4b2017-02-16 18:48:33 +0000110 virtual void beginFunctionImpl(const MachineFunction *MF) = 0;
111 virtual void endFunctionImpl(const MachineFunction *MF) = 0;
112 virtual void skippedNonDebugFunction() {}
113
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000114 // AsmPrinterHandler overrides.
115public:
116 void beginInstruction(const MachineInstr *MI) override;
117 void endInstruction() override;
118
119 void beginFunction(const MachineFunction *MF) override;
120 void endFunction(const MachineFunction *MF) override;
121
122 /// Return Label preceding the instruction.
123 MCSymbol *getLabelBeforeInsn(const MachineInstr *MI);
124
125 /// Return Label immediately following the instruction.
126 MCSymbol *getLabelAfterInsn(const MachineInstr *MI);
127
Vedant Kumar74533bd2018-10-22 21:44:21 +0000128 /// Return the function-local offset of an instruction. A label for the
129 /// instruction \p MI should exist (\ref getLabelAfterInsn).
130 const MCExpr *getFunctionLocalOffsetAfterInsn(const MachineInstr *MI);
131
Amjad Aboudacee5682016-07-12 12:06:34 +0000132 /// If this type is derived from a base type then return base type size.
133 static uint64_t getBaseTypeSize(const DITypeRef TyRef);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000134};
135
136}
137
138#endif