1 //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Dwarf emissions parts of AsmPrinter.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "asm-printer"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/Target/TargetLoweringObjectFile.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Support/Dwarf.h"
27 /// EmitSLEB128 - emit the specified signed leb128 value.
28 void AsmPrinter::EmitSLEB128(int Value, const char *Desc) const {
29 if (isVerbose() && Desc)
30 OutStreamer.AddComment(Desc);
32 if (MAI->hasLEB128()) {
34 OutStreamer.EmitRawText("\t.sleb128\t" + Twine(Value));
38 // If we don't have .sleb128, emit as .bytes.
39 int Sign = Value >> (8 * sizeof(Value) - 1);
43 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
45 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
46 if (IsMore) Byte |= 0x80;
47 OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
51 /// EmitULEB128 - emit the specified signed leb128 value.
52 void AsmPrinter::EmitULEB128(unsigned Value, const char *Desc,
53 unsigned PadTo) const {
54 if (isVerbose() && Desc)
55 OutStreamer.AddComment(Desc);
57 if (MAI->hasLEB128() && PadTo == 0) {
59 OutStreamer.EmitRawText("\t.uleb128\t" + Twine(Value));
63 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
65 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
67 if (Value || PadTo != 0) Byte |= 0x80;
68 OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
73 OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
74 OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
78 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
79 void AsmPrinter::EmitCFAByte(unsigned Val) const {
81 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
82 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
83 Twine(Val-dwarf::DW_CFA_offset) + ")");
85 OutStreamer.AddComment(dwarf::CallFrameString(Val));
87 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
90 static const char *DecodeDWARFEncoding(unsigned Encoding) {
92 case dwarf::DW_EH_PE_absptr: return "absptr";
93 case dwarf::DW_EH_PE_omit: return "omit";
94 case dwarf::DW_EH_PE_pcrel: return "pcrel";
95 case dwarf::DW_EH_PE_udata4: return "udata4";
96 case dwarf::DW_EH_PE_udata8: return "udata8";
97 case dwarf::DW_EH_PE_sdata4: return "sdata4";
98 case dwarf::DW_EH_PE_sdata8: return "sdata8";
99 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
100 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
101 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
102 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
103 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
104 return "indirect pcrel udata4";
105 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
106 return "indirect pcrel sdata4";
107 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
108 return "indirect pcrel udata8";
109 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
110 return "indirect pcrel sdata8";
113 return "<unknown encoding>";
117 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
118 /// encoding. If verbose assembly output is enabled, we output comments
119 /// describing the encoding. Desc is an optional string saying what the
120 /// encoding is specifying (e.g. "LSDA").
121 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
124 OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
125 Twine(DecodeDWARFEncoding(Val)));
127 OutStreamer.AddComment(Twine("Encoding = ") +
128 DecodeDWARFEncoding(Val));
131 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
134 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
135 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
136 if (Encoding == dwarf::DW_EH_PE_omit)
139 switch (Encoding & 0x07) {
140 default: assert(0 && "Invalid encoded value.");
141 case dwarf::DW_EH_PE_absptr: return TM.getTargetData()->getPointerSize();
142 case dwarf::DW_EH_PE_udata2: return 2;
143 case dwarf::DW_EH_PE_udata4: return 4;
144 case dwarf::DW_EH_PE_udata8: return 8;
148 void AsmPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
149 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
152 TLOF.getExprForDwarfReference(Sym, Mang, MMI, Encoding, OutStreamer);
153 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
156 void AsmPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
157 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
160 TLOF.getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
161 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
164 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
165 /// section. This can be done with a special directive if the target supports
166 /// it (e.g. cygwin) or by emitting it as an offset from a label at the start
169 /// SectionLabel is a temporary label emitted at the start of the section that
171 void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
172 const MCSymbol *SectionLabel) const {
173 // On COFF targets, we have to emit the special .secrel32 directive.
174 if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective()) {
176 OutStreamer.EmitRawText(SecOffDir + Twine(Label->getName()));
180 // Get the section that we're referring to, based on SectionLabel.
181 const MCSection &Section = SectionLabel->getSection();
183 // If Label has already been emitted, verify that it is in the same section as
184 // section label for sanity.
185 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
186 "Section offset using wrong section base for label");
188 // If the section in question will end up with an address of 0 anyway, we can
189 // just emit an absolute reference to save a relocation.
190 if (Section.isBaseAddressKnownZero()) {
191 OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/);
195 // Otherwise, emit it as a label difference from the start of the section.
196 EmitLabelDifference(Label, SectionLabel, 4);