blob: 90f8287e76e7be19ce60f062b3c554e3d5ad0731 [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.
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 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 {
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 Haarman1a4cbbe2017-08-30 17:50:21 +000050 /// 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 Haarman223303c2017-08-29 20:59:25 +000057};
58
Reid Klecknerf9c275f2016-02-10 20:55:49 +000059/// 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 Kramere3b963d2016-02-11 15:41:56 +000061class DebugHandlerBase : public AsmPrinterHandler {
Reid Klecknerf9c275f2016-02-10 20:55:49 +000062protected:
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 Robinsonac7fe5e2016-12-12 20:49:11 +000072 /// 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 Klecknerf9c275f2016-02-10 20:55:49 +000075 DebugLoc PrevInstLoc;
76 MCSymbol *PrevLabel = nullptr;
Paul Robinsonac7fe5e2016-12-12 20:49:11 +000077 const MachineBasicBlock *PrevInstBB = nullptr;
Reid Klecknerf9c275f2016-02-10 20:55:49 +000078
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 Blaikieb2fbb4b2017-02-16 18:48:33 +0000114 virtual void beginFunctionImpl(const MachineFunction *MF) = 0;
115 virtual void endFunctionImpl(const MachineFunction *MF) = 0;
116 virtual void skippedNonDebugFunction() {}
117
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000118 // AsmPrinterHandler overrides.
119public:
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 Prantl941fa752016-12-05 18:04:47 +0000132 /// 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 Klecknerf9c275f2016-02-10 20:55:49 +0000136
Adrian Prantl941fa752016-12-05 18:04:47 +0000137 /// Determine whether two variable fragments overlap.
138 static bool fragmentsOverlap(const DIExpression *P1, const DIExpression *P2);
Amjad Aboudacee5682016-07-12 12:06:34 +0000139
140 /// If this type is derived from a base type then return base type size.
141 static uint64_t getBaseTypeSize(const DITypeRef TyRef);
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000142};
143
144}
145
146#endif