blob: b44339718079543c133072e697b9e63ee3fc0e5a [file] [log] [blame]
Chris Lattner9efd1182010-04-04 19:09:29 +00001//===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
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// This file implements the Dwarf emissions parts of AsmPrinter.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "asm-printer"
15#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Twine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000018#include "llvm/MC/MCAsmInfo.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000019#include "llvm/MC/MCSection.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000020#include "llvm/MC/MCStreamer.h"
Chris Lattner70a4fce2010-04-04 23:25:33 +000021#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/MC/MachineLocation.h"
23#include "llvm/Support/Dwarf.h"
24#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov2f931282011-01-10 12:39:04 +000025#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnere619c0d2010-04-04 20:20:50 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
27#include "llvm/Target/TargetMachine.h"
Chris Lattneraabc6042010-04-04 23:41:46 +000028#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner9efd1182010-04-04 19:09:29 +000029using namespace llvm;
30
Chris Lattneraabc6042010-04-04 23:41:46 +000031//===----------------------------------------------------------------------===//
32// Dwarf Emission Helper Routines
33//===----------------------------------------------------------------------===//
34
Chris Lattner9efd1182010-04-04 19:09:29 +000035/// EmitSLEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000036void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
Chris Lattner9efd1182010-04-04 19:09:29 +000037 if (isVerbose() && Desc)
38 OutStreamer.AddComment(Desc);
Chris Lattner9efd1182010-04-04 19:09:29 +000039
Benjamin Kramerc74798d2011-11-05 11:52:44 +000040 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner9efd1182010-04-04 19:09:29 +000041}
42
43/// EmitULEB128 - emit the specified signed leb128 value.
David Blaikie5acff7e2013-06-23 18:31:11 +000044void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
Chris Lattner9efd1182010-04-04 19:09:29 +000045 unsigned PadTo) const {
46 if (isVerbose() && Desc)
47 OutStreamer.AddComment(Desc);
Rafael Espindola38d07562010-11-04 18:17:08 +000048
Eric Christopherbf7bc492013-01-09 03:52:05 +000049 OutStreamer.EmitULEB128IntValue(Value, PadTo);
Chris Lattner9efd1182010-04-04 19:09:29 +000050}
51
Chris Lattnerbaf2be02010-04-04 20:01:25 +000052/// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
53void AsmPrinter::EmitCFAByte(unsigned Val) const {
54 if (isVerbose()) {
Eric Christopher596077b2013-12-04 22:26:43 +000055 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset + 64)
Eric Christopher1d6bd412012-11-20 20:34:47 +000056 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
Eric Christopher596077b2013-12-04 22:26:43 +000057 Twine(Val - dwarf::DW_CFA_offset) + ")");
Chris Lattnerbaf2be02010-04-04 20:01:25 +000058 else
59 OutStreamer.AddComment(dwarf::CallFrameString(Val));
60 }
Eric Christopherce0cfce2013-01-09 01:35:34 +000061 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerbaf2be02010-04-04 20:01:25 +000062}
63
Chris Lattnerb75af3c2010-04-04 20:04:21 +000064static const char *DecodeDWARFEncoding(unsigned Encoding) {
65 switch (Encoding) {
Eric Christopher596077b2013-12-04 22:26:43 +000066 case dwarf::DW_EH_PE_absptr:
67 return "absptr";
68 case dwarf::DW_EH_PE_omit:
69 return "omit";
70 case dwarf::DW_EH_PE_pcrel:
71 return "pcrel";
72 case dwarf::DW_EH_PE_udata4:
73 return "udata4";
74 case dwarf::DW_EH_PE_udata8:
75 return "udata8";
76 case dwarf::DW_EH_PE_sdata4:
77 return "sdata4";
78 case dwarf::DW_EH_PE_sdata8:
79 return "sdata8";
80 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
81 return "pcrel udata4";
82 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
83 return "pcrel sdata4";
84 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
85 return "pcrel udata8";
86 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
87 return "pcrel sdata8";
88 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
89 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000090 return "indirect pcrel udata4";
Eric Christopher596077b2013-12-04 22:26:43 +000091 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
92 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000093 return "indirect pcrel sdata4";
Eric Christopher596077b2013-12-04 22:26:43 +000094 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
95 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000096 return "indirect pcrel udata8";
Eric Christopher596077b2013-12-04 22:26:43 +000097 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
98 :
Chris Lattnerb75af3c2010-04-04 20:04:21 +000099 return "indirect pcrel sdata8";
100 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000101
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000102 return "<unknown encoding>";
103}
104
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000105/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
106/// encoding. If verbose assembly output is enabled, we output comments
107/// describing the encoding. Desc is an optional string saying what the
108/// encoding is specifying (e.g. "LSDA").
Chris Lattneraabc6042010-04-04 23:41:46 +0000109void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000110 if (isVerbose()) {
111 if (Desc != 0)
Eric Christopher596077b2013-12-04 22:26:43 +0000112 OutStreamer.AddComment(Twine(Desc) + " Encoding = " +
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000113 Twine(DecodeDWARFEncoding(Val)));
114 else
Eric Christopher596077b2013-12-04 22:26:43 +0000115 OutStreamer.AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000116 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000117
Eric Christopherce0cfce2013-01-09 01:35:34 +0000118 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerb75af3c2010-04-04 20:04:21 +0000119}
120
Chris Lattnere619c0d2010-04-04 20:20:50 +0000121/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
122unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
123 if (Encoding == dwarf::DW_EH_PE_omit)
124 return 0;
Eric Christopher1d6bd412012-11-20 20:34:47 +0000125
Chris Lattnere619c0d2010-04-04 20:20:50 +0000126 switch (Encoding & 0x07) {
Eric Christopher596077b2013-12-04 22:26:43 +0000127 default:
128 llvm_unreachable("Invalid encoded value.");
129 case dwarf::DW_EH_PE_absptr:
130 return TM.getDataLayout()->getPointerSize();
131 case dwarf::DW_EH_PE_udata2:
132 return 2;
133 case dwarf::DW_EH_PE_udata4:
134 return 4;
135 case dwarf::DW_EH_PE_udata8:
136 return 8;
Chris Lattnere619c0d2010-04-04 20:20:50 +0000137 }
138}
139
Eric Christopher1d6bd412012-11-20 20:34:47 +0000140void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
141 unsigned Encoding) const {
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000142 if (GV) {
143 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000144
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000145 const MCExpr *Exp =
Eric Christopher596077b2013-12-04 22:26:43 +0000146 TLOF.getTTypeGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
Eric Christopherce0cfce2013-01-09 01:35:34 +0000147 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov097b0e92012-11-19 21:17:20 +0000148 } else
Eric Christopherce0cfce2013-01-09 01:35:34 +0000149 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnere619c0d2010-04-04 20:20:50 +0000150}
Chris Lattner70a4fce2010-04-04 23:25:33 +0000151
152/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
153/// section. This can be done with a special directive if the target supports
154/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
155/// of the section.
156///
157/// SectionLabel is a temporary label emitted at the start of the section that
158/// Label lives in.
159void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
160 const MCSymbol *SectionLabel) const {
161 // On COFF targets, we have to emit the special .secrel32 directive.
Matt Arsenault034ca0f2013-04-22 22:49:11 +0000162 if (MAI->needsDwarfSectionOffsetDirective()) {
Rafael Espindolad3df3d32011-12-17 01:14:52 +0000163 OutStreamer.EmitCOFFSecRel32(Label);
Chris Lattner70a4fce2010-04-04 23:25:33 +0000164 return;
165 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000166
Chris Lattner70a4fce2010-04-04 23:25:33 +0000167 // Get the section that we're referring to, based on SectionLabel.
168 const MCSection &Section = SectionLabel->getSection();
Eric Christopher1d6bd412012-11-20 20:34:47 +0000169
Chris Lattner70a4fce2010-04-04 23:25:33 +0000170 // If Label has already been emitted, verify that it is in the same section as
171 // section label for sanity.
172 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
173 "Section offset using wrong section base for label");
Eric Christopher1d6bd412012-11-20 20:34:47 +0000174
Duncan Sandsb847bf52011-03-12 13:07:37 +0000175 // If the section in question will end up with an address of 0 anyway, we can
176 // just emit an absolute reference to save a relocation.
177 if (Section.isBaseAddressKnownZero()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +0000178 OutStreamer.EmitSymbolValue(Label, 4);
Duncan Sandsb847bf52011-03-12 13:07:37 +0000179 return;
180 }
Eric Christopher1d6bd412012-11-20 20:34:47 +0000181
Chris Lattner70a4fce2010-04-04 23:25:33 +0000182 // Otherwise, emit it as a label difference from the start of the section.
183 EmitLabelDifference(Label, SectionLabel, 4);
184}
185
Chris Lattneraabc6042010-04-04 23:41:46 +0000186//===----------------------------------------------------------------------===//
187// Dwarf Lowering Routines
188//===----------------------------------------------------------------------===//
Chris Lattner70a4fce2010-04-04 23:25:33 +0000189
Rafael Espindola227144c2013-05-13 01:16:13 +0000190void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
191 switch (Inst.getOperation()) {
192 default:
193 llvm_unreachable("Unexpected instruction");
194 case MCCFIInstruction::OpDefCfaOffset:
195 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
196 break;
197 case MCCFIInstruction::OpDefCfa:
198 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
199 break;
200 case MCCFIInstruction::OpDefCfaRegister:
201 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
202 break;
203 case MCCFIInstruction::OpOffset:
204 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
205 break;
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000206 case MCCFIInstruction::OpRegister:
207 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
208 break;
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000209 case MCCFIInstruction::OpWindowSave:
210 OutStreamer.EmitCFIWindowSave();
211 break;
Rafael Espindolabeb74c32011-04-15 20:32:03 +0000212 }
213}