blob: b92f49cfaed8af34dae2f28eb995a3e6a00f3651 [file] [log] [blame]
Chris Lattner7e1a8f82010-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 Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/Twine.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000018#include "llvm/MC/MCAsmInfo.h"
Chris Lattner6189ed12010-04-04 23:25:33 +000019#include "llvm/MC/MCSection.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000020#include "llvm/MC/MCStreamer.h"
Chris Lattner6189ed12010-04-04 23:25:33 +000021#include "llvm/MC/MCSymbol.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/MC/MachineLocation.h"
23#include "llvm/Support/Dwarf.h"
24#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000025#include "llvm/Target/TargetFrameLowering.h"
Chris Lattnerd2af7852010-04-04 20:20:50 +000026#include "llvm/Target/TargetLoweringObjectFile.h"
27#include "llvm/Target/TargetMachine.h"
Chris Lattner02b86b92010-04-04 23:41:46 +000028#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner7e1a8f82010-04-04 19:09:29 +000029using namespace llvm;
30
Chris Lattner02b86b92010-04-04 23:41:46 +000031//===----------------------------------------------------------------------===//
32// Dwarf Emission Helper Routines
33//===----------------------------------------------------------------------===//
34
Chris Lattner7e1a8f82010-04-04 19:09:29 +000035/// EmitSLEB128 - emit the specified signed leb128 value.
David Blaikiefe2e66a2013-06-23 18:31:11 +000036void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
Chris Lattner7e1a8f82010-04-04 19:09:29 +000037 if (isVerbose() && Desc)
38 OutStreamer.AddComment(Desc);
Chris Lattner7e1a8f82010-04-04 19:09:29 +000039
Benjamin Kramerc25c9082011-11-05 11:52:44 +000040 OutStreamer.EmitSLEB128IntValue(Value);
Chris Lattner7e1a8f82010-04-04 19:09:29 +000041}
42
43/// EmitULEB128 - emit the specified signed leb128 value.
David Blaikiefe2e66a2013-06-23 18:31:11 +000044void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc,
Chris Lattner7e1a8f82010-04-04 19:09:29 +000045 unsigned PadTo) const {
46 if (isVerbose() && Desc)
47 OutStreamer.AddComment(Desc);
Rafael Espindola73873452010-11-04 18:17:08 +000048
Eric Christopher1ced2082013-01-09 03:52:05 +000049 OutStreamer.EmitULEB128IntValue(Value, PadTo);
Chris Lattner7e1a8f82010-04-04 19:09:29 +000050}
51
Chris Lattner7a101f42010-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()) {
55 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
Eric Christopheraaa50b92012-11-20 20:34:47 +000056 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
Chris Lattner7a101f42010-04-04 20:01:25 +000057 Twine(Val-dwarf::DW_CFA_offset) + ")");
58 else
59 OutStreamer.AddComment(dwarf::CallFrameString(Val));
60 }
Eric Christopherca1dd052013-01-09 01:35:34 +000061 OutStreamer.EmitIntValue(Val, 1);
Chris Lattner7a101f42010-04-04 20:01:25 +000062}
63
Chris Lattnerca6190b2010-04-04 20:04:21 +000064static const char *DecodeDWARFEncoding(unsigned Encoding) {
65 switch (Encoding) {
66 case dwarf::DW_EH_PE_absptr: return "absptr";
67 case dwarf::DW_EH_PE_omit: return "omit";
68 case dwarf::DW_EH_PE_pcrel: return "pcrel";
69 case dwarf::DW_EH_PE_udata4: return "udata4";
70 case dwarf::DW_EH_PE_udata8: return "udata8";
71 case dwarf::DW_EH_PE_sdata4: return "sdata4";
72 case dwarf::DW_EH_PE_sdata8: return "sdata8";
73 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
74 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
75 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
76 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
77 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
78 return "indirect pcrel udata4";
79 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
80 return "indirect pcrel sdata4";
81 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
82 return "indirect pcrel udata8";
83 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
84 return "indirect pcrel sdata8";
85 }
Eric Christopheraaa50b92012-11-20 20:34:47 +000086
Chris Lattnerca6190b2010-04-04 20:04:21 +000087 return "<unknown encoding>";
88}
89
90
91/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
92/// encoding. If verbose assembly output is enabled, we output comments
93/// describing the encoding. Desc is an optional string saying what the
94/// encoding is specifying (e.g. "LSDA").
Chris Lattner02b86b92010-04-04 23:41:46 +000095void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
Chris Lattnerca6190b2010-04-04 20:04:21 +000096 if (isVerbose()) {
97 if (Desc != 0)
98 OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
99 Twine(DecodeDWARFEncoding(Val)));
100 else
101 OutStreamer.AddComment(Twine("Encoding = ") +
102 DecodeDWARFEncoding(Val));
103 }
Eric Christopheraaa50b92012-11-20 20:34:47 +0000104
Eric Christopherca1dd052013-01-09 01:35:34 +0000105 OutStreamer.EmitIntValue(Val, 1);
Chris Lattnerca6190b2010-04-04 20:04:21 +0000106}
107
Chris Lattnerd2af7852010-04-04 20:20:50 +0000108/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
109unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
110 if (Encoding == dwarf::DW_EH_PE_omit)
111 return 0;
Eric Christopheraaa50b92012-11-20 20:34:47 +0000112
Chris Lattnerd2af7852010-04-04 20:20:50 +0000113 switch (Encoding & 0x07) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000114 default: llvm_unreachable("Invalid encoded value.");
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000115 case dwarf::DW_EH_PE_absptr: return TM.getDataLayout()->getPointerSize();
Chris Lattnerd2af7852010-04-04 20:20:50 +0000116 case dwarf::DW_EH_PE_udata2: return 2;
117 case dwarf::DW_EH_PE_udata4: return 4;
118 case dwarf::DW_EH_PE_udata8: return 8;
119 }
120}
121
Eric Christopheraaa50b92012-11-20 20:34:47 +0000122void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
123 unsigned Encoding) const {
Anton Korobeynikov239938f2012-11-19 21:17:20 +0000124 if (GV) {
125 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
Eric Christopheraaa50b92012-11-20 20:34:47 +0000126
Anton Korobeynikov239938f2012-11-19 21:17:20 +0000127 const MCExpr *Exp =
128 TLOF.getTTypeGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
Eric Christopherca1dd052013-01-09 01:35:34 +0000129 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
Anton Korobeynikov239938f2012-11-19 21:17:20 +0000130 } else
Eric Christopherca1dd052013-01-09 01:35:34 +0000131 OutStreamer.EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
Chris Lattnerd2af7852010-04-04 20:20:50 +0000132}
Chris Lattner6189ed12010-04-04 23:25:33 +0000133
134/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
135/// section. This can be done with a special directive if the target supports
136/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
137/// of the section.
138///
139/// SectionLabel is a temporary label emitted at the start of the section that
140/// Label lives in.
141void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
142 const MCSymbol *SectionLabel) const {
143 // On COFF targets, we have to emit the special .secrel32 directive.
Matt Arsenault9a0e12a2013-04-22 22:49:11 +0000144 if (MAI->needsDwarfSectionOffsetDirective()) {
Rafael Espindola8f7d12c2011-12-17 01:14:52 +0000145 OutStreamer.EmitCOFFSecRel32(Label);
Chris Lattner6189ed12010-04-04 23:25:33 +0000146 return;
147 }
Eric Christopheraaa50b92012-11-20 20:34:47 +0000148
Chris Lattner6189ed12010-04-04 23:25:33 +0000149 // Get the section that we're referring to, based on SectionLabel.
150 const MCSection &Section = SectionLabel->getSection();
Eric Christopheraaa50b92012-11-20 20:34:47 +0000151
Chris Lattner6189ed12010-04-04 23:25:33 +0000152 // If Label has already been emitted, verify that it is in the same section as
153 // section label for sanity.
154 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
155 "Section offset using wrong section base for label");
Eric Christopheraaa50b92012-11-20 20:34:47 +0000156
Duncan Sandsba993462011-03-12 13:07:37 +0000157 // If the section in question will end up with an address of 0 anyway, we can
158 // just emit an absolute reference to save a relocation.
159 if (Section.isBaseAddressKnownZero()) {
Eric Christopherca1dd052013-01-09 01:35:34 +0000160 OutStreamer.EmitSymbolValue(Label, 4);
Duncan Sandsba993462011-03-12 13:07:37 +0000161 return;
162 }
Eric Christopheraaa50b92012-11-20 20:34:47 +0000163
Chris Lattner6189ed12010-04-04 23:25:33 +0000164 // Otherwise, emit it as a label difference from the start of the section.
165 EmitLabelDifference(Label, SectionLabel, 4);
166}
167
Chris Lattner02b86b92010-04-04 23:41:46 +0000168//===----------------------------------------------------------------------===//
169// Dwarf Lowering Routines
170//===----------------------------------------------------------------------===//
Chris Lattner6189ed12010-04-04 23:25:33 +0000171
Rafael Espindola4a971702013-05-13 01:16:13 +0000172void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
173 switch (Inst.getOperation()) {
174 default:
175 llvm_unreachable("Unexpected instruction");
176 case MCCFIInstruction::OpDefCfaOffset:
177 OutStreamer.EmitCFIDefCfaOffset(Inst.getOffset());
178 break;
179 case MCCFIInstruction::OpDefCfa:
180 OutStreamer.EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
181 break;
182 case MCCFIInstruction::OpDefCfaRegister:
183 OutStreamer.EmitCFIDefCfaRegister(Inst.getRegister());
184 break;
185 case MCCFIInstruction::OpOffset:
186 OutStreamer.EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
187 break;
Venkatraman Govindaraju30ec8a32013-09-26 15:11:00 +0000188 case MCCFIInstruction::OpRegister:
189 OutStreamer.EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
190 break;
Venkatraman Govindaraju83ba58e2013-09-26 14:49:40 +0000191 case MCCFIInstruction::OpWindowSave:
192 OutStreamer.EmitCFIWindowSave();
193 break;
Rafael Espindola126ae682011-04-15 20:32:03 +0000194 }
195}