summaryrefslogtreecommitdiff
path: root/lib/CodeGen/AsmPrinter/DwarfPrinter.cpp
blob: 1c72f5ae6812ec9ce2ff156eb59f6ae58b6e5d0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//===--- lib/CodeGen/DwarfPrinter.cpp - Dwarf Printer ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Emit general DWARF directives.
// 
//===----------------------------------------------------------------------===//

#include "DwarfPrinter.h"
#include "llvm/Module.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetFrameInfo.h"
#include "llvm/Target/TargetRegisterInfo.h"

using namespace llvm;

Dwarf::Dwarf(raw_ostream &OS, AsmPrinter *A, const MCAsmInfo *T,
             const char *flavor)
: O(OS), Asm(A), TAI(T), TD(Asm->TM.getTargetData()),
  RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
  SubprogramCount(0), Flavor(flavor), SetCounter(1) {}

void Dwarf::PrintRelDirective(bool Force32Bit, bool isInSection) const {
  if (isInSection && TAI->getDwarfSectionOffsetDirective())
    O << TAI->getDwarfSectionOffsetDirective();
  else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t))
    O << TAI->getData32bitsDirective();
  else
    O << TAI->getData64bitsDirective();
}

/// PrintLabelName - Print label name in form used by Dwarf writer.
///
void Dwarf::PrintLabelName(const char *Tag, unsigned Number) const {
  O << TAI->getPrivateGlobalPrefix() << Tag;
  if (Number) O << Number;
}
void Dwarf::PrintLabelName(const char *Tag, unsigned Number,
                           const char *Suffix) const {
  O << TAI->getPrivateGlobalPrefix() << Tag;
  if (Number) O << Number;
  O << Suffix;
}

/// EmitLabel - Emit location label for internal use by Dwarf.
///
void Dwarf::EmitLabel(const char *Tag, unsigned Number) const {
  PrintLabelName(Tag, Number);
  O << ":\n";
}

/// EmitReference - Emit a reference to a label.
///
void Dwarf::EmitReference(const char *Tag, unsigned Number,
                          bool IsPCRelative, bool Force32Bit) const {
  PrintRelDirective(Force32Bit);
  PrintLabelName(Tag, Number);
  if (IsPCRelative) O << "-" << TAI->getPCSymbol();
}
void Dwarf::EmitReference(const std::string &Name, bool IsPCRelative,
                          bool Force32Bit) const {
  PrintRelDirective(Force32Bit);
  O << Name;
  if (IsPCRelative) O << "-" << TAI->getPCSymbol();
}

/// EmitDifference - Emit the difference between two labels.  Some assemblers do
/// not behave with absolute expressions with data directives, so there is an
/// option (needsSet) to use an intermediary set expression.
void Dwarf::EmitDifference(const char *TagHi, unsigned NumberHi,
                           const char *TagLo, unsigned NumberLo,
                           bool IsSmall) {
  if (TAI->needsSet()) {
    O << "\t.set\t";
    PrintLabelName("set", SetCounter, Flavor);
    O << ",";
    PrintLabelName(TagHi, NumberHi);
    O << "-";
    PrintLabelName(TagLo, NumberLo);
    O << "\n";

    PrintRelDirective(IsSmall);
    PrintLabelName("set", SetCounter, Flavor);
    ++SetCounter;
  } else {
    PrintRelDirective(IsSmall);
    PrintLabelName(TagHi, NumberHi);
    O << "-";
    PrintLabelName(TagLo, NumberLo);
  }
}

void Dwarf::EmitSectionOffset(const char* Label, const char* Section,
                              unsigned LabelNumber, unsigned SectionNumber,
                              bool IsSmall, bool isEH,
                              bool useSet) {
  bool printAbsolute = false;
  if (isEH)
    printAbsolute = TAI->isAbsoluteEHSectionOffsets();
  else
    printAbsolute = TAI->isAbsoluteDebugSectionOffsets();

  if (TAI->needsSet() && useSet) {
    O << "\t.set\t";
    PrintLabelName("set", SetCounter, Flavor);
    O << ",";
    PrintLabelName(Label, LabelNumber);

    if (!printAbsolute) {
      O << "-";
      PrintLabelName(Section, SectionNumber);
    }

    O << "\n";
    PrintRelDirective(IsSmall);
    PrintLabelName("set", SetCounter, Flavor);
    ++SetCounter;
  } else {
    PrintRelDirective(IsSmall, true);
    PrintLabelName(Label, LabelNumber);

    if (!printAbsolute) {
      O << "-";
      PrintLabelName(Section, SectionNumber);
    }
  }
}

/// EmitFrameMoves - Emit frame instructions to describe the layout of the
/// frame.
void Dwarf::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
                           const std::vector<MachineMove> &Moves, bool isEH) {
  int stackGrowth =
    Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
    TargetFrameInfo::StackGrowsUp ?
    TD->getPointerSize() : -TD->getPointerSize();
  bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;

  for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
    const MachineMove &Move = Moves[i];
    unsigned LabelID = Move.getLabelID();

    if (LabelID) {
      LabelID = MMI->MappedLabel(LabelID);

      // Throw out move if the label is invalid.
      if (!LabelID) continue;
    }

    const MachineLocation &Dst = Move.getDestination();
    const MachineLocation &Src = Move.getSource();

    // Advance row if new location.
    if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
      Asm->EmitInt8(dwarf::DW_CFA_advance_loc4);
      Asm->EOL("DW_CFA_advance_loc4");
      EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
      Asm->EOL();

      BaseLabelID = LabelID;
      BaseLabel = "label";
      IsLocal = true;
    }

    // If advancing cfa.
    if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
      if (!Src.isReg()) {
        if (Src.getReg() == MachineLocation::VirtualFP) {
          Asm->EmitInt8(dwarf::DW_CFA_def_cfa_offset);
          Asm->EOL("DW_CFA_def_cfa_offset");
        } else {
          Asm->EmitInt8(dwarf::DW_CFA_def_cfa);
          Asm->EOL("DW_CFA_def_cfa");
          Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH));
          Asm->EOL("Register");
        }

        int Offset = -Src.getOffset();

        Asm->EmitULEB128Bytes(Offset);
        Asm->EOL("Offset");
      } else {
        llvm_unreachable("Machine move not supported yet.");
      }
    } else if (Src.isReg() &&
               Src.getReg() == MachineLocation::VirtualFP) {
      if (Dst.isReg()) {
        Asm->EmitInt8(dwarf::DW_CFA_def_cfa_register);
        Asm->EOL("DW_CFA_def_cfa_register");
        Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH));
        Asm->EOL("Register");
      } else {
        llvm_unreachable("Machine move not supported yet.");
      }
    } else {
      unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
      int Offset = Dst.getOffset() / stackGrowth;

      if (Offset < 0) {
        Asm->EmitInt8(dwarf::DW_CFA_offset_extended_sf);
        Asm->EOL("DW_CFA_offset_extended_sf");
        Asm->EmitULEB128Bytes(Reg);
        Asm->EOL("Reg");
        Asm->EmitSLEB128Bytes(Offset);
        Asm->EOL("Offset");
      } else if (Reg < 64) {
        Asm->EmitInt8(dwarf::DW_CFA_offset + Reg);
        if (Asm->isVerbose())
          Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")");
        else
          Asm->EOL();
        Asm->EmitULEB128Bytes(Offset);
        Asm->EOL("Offset");
      } else {
        Asm->EmitInt8(dwarf::DW_CFA_offset_extended);
        Asm->EOL("DW_CFA_offset_extended");
        Asm->EmitULEB128Bytes(Reg);
        Asm->EOL("Reg");
        Asm->EmitULEB128Bytes(Offset);
        Asm->EOL("Offset");
      }
    }
  }
}