move some more stuff to asmprinter.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / AsmPrinterDwarf.cpp
1 //===-- 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"
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"
23 using namespace llvm;
24
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);
29     
30   if (MAI->hasLEB128()) {
31     // FIXME: MCize.
32     OutStreamer.EmitRawText("\t.sleb128\t" + Twine(Value));
33     return;
34   }
35
36   // If we don't have .sleb128, emit as .bytes.
37   int Sign = Value >> (8 * sizeof(Value) - 1);
38   bool IsMore;
39   
40   do {
41     unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
42     Value >>= 7;
43     IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
44     if (IsMore) Byte |= 0x80;
45     OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
46   } while (IsMore);
47 }
48
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);
54  
55   if (MAI->hasLEB128() && PadTo == 0) {
56     // FIXME: MCize.
57     OutStreamer.EmitRawText("\t.uleb128\t" + Twine(Value));
58     return;
59   }
60   
61   // If we don't have .uleb128 or we want to emit padding, emit as .bytes.
62   do {
63     unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
64     Value >>= 7;
65     if (Value || PadTo != 0) Byte |= 0x80;
66     OutStreamer.EmitIntValue(Byte, 1, /*addrspace*/0);
67   } while (Value);
68
69   if (PadTo) {
70     if (PadTo > 1)
71       OutStreamer.EmitFill(PadTo - 1, 0x80/*fillval*/, 0/*addrspace*/);
72     OutStreamer.EmitFill(1, 0/*fillval*/, 0/*addrspace*/);
73   }
74 }
75
76 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
77 void AsmPrinter::EmitCFAByte(unsigned Val) const {
78   if (isVerbose()) {
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) + ")");
82     else
83       OutStreamer.AddComment(dwarf::CallFrameString(Val));
84   }
85   OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
86 }
87
88 static const char *DecodeDWARFEncoding(unsigned Encoding) {
89   switch (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";
109   }
110   
111   return "<unknown encoding>";
112 }
113
114
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) {
120   if (isVerbose()) {
121     if (Desc != 0)
122       OutStreamer.AddComment(Twine(Desc)+" Encoding = " +
123                              Twine(DecodeDWARFEncoding(Val)));
124     else
125       OutStreamer.AddComment(Twine("Encoding = ") +
126                              DecodeDWARFEncoding(Val));
127   }
128   
129   OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
130 }
131
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)
135     return 0;
136   
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;
143   }
144 }
145
146 void AsmPrinter::EmitReference(const MCSymbol *Sym, unsigned Encoding) const {
147   const TargetLoweringObjectFile &TLOF = getObjFileLowering();
148   
149   const MCExpr *Exp =
150     TLOF.getExprForDwarfReference(Sym, Mang, MMI, Encoding, OutStreamer);
151   OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
152 }
153
154 void AsmPrinter::EmitReference(const GlobalValue *GV, unsigned Encoding)const{
155   const TargetLoweringObjectFile &TLOF = getObjFileLowering();
156   
157   const MCExpr *Exp =
158     TLOF.getExprForDwarfGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
159   OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
160 }