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/MCStreamer.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Target/TargetLoweringObjectFile.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/Dwarf.h"
25 /// EmitSLEB128 - emit the specified signed leb128 value.
26 void AsmPrinter::EmitSLEB128(int Value, const char *Desc) const {
27 if (isVerbose() && Desc)
28 OutStreamer.AddComment(Desc);
30 if (MAI->hasLEB128()) {
32 OutStreamer.EmitRawText("\t.sleb128\t" + Twine(Value));
36 // If we don't have .sleb128, emit as .bytes.
37 int Sign = Value >> (8 * sizeof(Value) - 1);
41 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
43 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
44 if (IsMore) Byte |= 0x80;
45 OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
49 /// EmitULEB128 - emit the specified signed leb128 value.
50 void AsmPrinter::EmitULEB128(unsigned Value, const char *Desc,
51 unsigned PadTo) const {
52 if (isVerbose() && Desc)
53 OutStreamer.AddComment(Desc);
55 if (MAI->hasLEB128() && PadTo == 0) {
57 OutStreamer.EmitRawText("\t.uleb128\t" + Twine(Value));
61 // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
63 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
65 if (Value || PadTo != 0) Byte |= 0x80;
66 OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
71 OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
72 OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
76 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
77 void AsmPrinter::EmitCFAByte(unsigned Val) const {
79 if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
80 OutStreamer.AddComment("DW_CFA_offset + Reg (" +
81 Twine(Val-dwarf::DW_CFA_offset) + ")");
83 OutStreamer.AddComment(dwarf::CallFrameString(Val));
85 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
88 static const char *DecodeDWARFEncoding(unsigned Encoding) {
90 case dwarf::DW_EH_PE_absptr: return "absptr";
91 case dwarf::DW_EH_PE_omit: return "omit";
92 case dwarf::DW_EH_PE_pcrel: return "pcrel";
93 case dwarf::DW_EH_PE_udata4: return "udata4";
94 case dwarf::DW_EH_PE_udata8: return "udata8";
95 case dwarf::DW_EH_PE_sdata4: return "sdata4";
96 case dwarf::DW_EH_PE_sdata8: return "sdata8";
97 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4: return "pcrel udata4";
98 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4: return "pcrel sdata4";
99 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8: return "pcrel udata8";
100 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8: return "pcrel sdata8";
101 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
102 return "indirect pcrel udata4";
103 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
104 return "indirect pcrel sdata4";
105 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
106 return "indirect pcrel udata8";
107 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
108 return "indirect pcrel sdata8";
111 return "<unknown encoding>";
115 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
116 /// encoding. If verbose assembly output is enabled, we output comments
117 /// describing the encoding. Desc is an optional string saying what the
118 /// encoding is specifying (e.g. "LSDA").
119 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) {
122 OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
123 Twine(DecodeDWARFEncoding(Val)));
125 OutStreamer.AddComment(Twine("Encoding = ") +
126 DecodeDWARFEncoding(Val));
129 OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
132 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
133 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
134 if (Encoding == dwarf::DW_EH_PE_omit)
137 switch (Encoding & 0x07) {
138 default: assert(0 && "Invalid encoded value.");
139 case dwarf::DW_EH_PE_absptr: return TM.getTargetData()->getPointerSize();
140 case dwarf::DW_EH_PE_udata2: return 2;
141 case dwarf::DW_EH_PE_udata4: return 4;
142 case dwarf::DW_EH_PE_udata8: return 8;
146 void AsmPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
147 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
150 TLOF.getExprForDwarfReference(Sym, Mang, MMI, Encoding, OutStreamer);
151 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
154 void AsmPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
155 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
158 TLOF.getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
159 OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);