Use TARGET2 relocation for TType references on ARM.
[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/MachineLocation.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/DataLayout.h"
22 #include "llvm/Target/TargetFrameLowering.h"
23 #include "llvm/Target/TargetLoweringObjectFile.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
29 using namespace llvm;
30
31 //===----------------------------------------------------------------------===//
32 // Dwarf Emission Helper Routines
33 //===----------------------------------------------------------------------===//
34
35 /// EmitSLEB128 - emit the specified signed leb128 value.
36 void AsmPrinter::EmitSLEB128(int Value, const char *Desc) const {
37   if (isVerbose() && Desc)
38     OutStreamer.AddComment(Desc);
39
40   OutStreamer.EmitSLEB128IntValue(Value);
41 }
42
43 /// EmitULEB128 - emit the specified signed leb128 value.
44 void AsmPrinter::EmitULEB128(unsigned Value, const char *Desc,
45                              unsigned PadTo) const {
46   if (isVerbose() && Desc)
47     OutStreamer.AddComment(Desc);
48
49   OutStreamer.EmitULEB128IntValue(Value, 0/*addrspace*/, PadTo);
50 }
51
52 /// EmitCFAByte - Emit a .byte 42 directive for a DW_CFA_xxx value.
53 void AsmPrinter::EmitCFAByte(unsigned Val) const {
54   if (isVerbose()) {
55     if (Val >= dwarf::DW_CFA_offset && Val < dwarf::DW_CFA_offset+64)
56       OutStreamer.AddComment("DW_CFA_offset + Reg (" + 
57                              Twine(Val-dwarf::DW_CFA_offset) + ")");
58     else
59       OutStreamer.AddComment(dwarf::CallFrameString(Val));
60   }
61   OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
62 }
63
64 static 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   }
86   
87   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").
95 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
96   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   }
104   
105   OutStreamer.EmitIntValue(Val, 1, 0/*addrspace*/);
106 }
107
108 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
109 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
110   if (Encoding == dwarf::DW_EH_PE_omit)
111     return 0;
112   
113   switch (Encoding & 0x07) {
114   default: llvm_unreachable("Invalid encoded value.");
115   case dwarf::DW_EH_PE_absptr: return TM.getDataLayout()->getPointerSize();
116   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
122 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV, unsigned Encoding)const{
123   const TargetLoweringObjectFile &TLOF = getObjFileLowering();
124   
125   const MCExpr *Exp =
126     TLOF.getTTypeGlobalReference(GV, Mang, MMI, Encoding, OutStreamer);
127   OutStreamer.EmitValue(Exp, GetSizeOfEncodedValue(Encoding), /*addrspace*/0);
128 }
129
130 /// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
131 /// section.  This can be done with a special directive if the target supports
132 /// it (e.g. cygwin) or by emitting it as an offset from a label at the start
133 /// of the section.
134 ///
135 /// SectionLabel is a temporary label emitted at the start of the section that
136 /// Label lives in.
137 void AsmPrinter::EmitSectionOffset(const MCSymbol *Label,
138                                    const MCSymbol *SectionLabel) const {
139   // On COFF targets, we have to emit the special .secrel32 directive.
140   if (MAI->getDwarfSectionOffsetDirective()) {
141     OutStreamer.EmitCOFFSecRel32(Label);
142     return;
143   }
144   
145   // Get the section that we're referring to, based on SectionLabel.
146   const MCSection &Section = SectionLabel->getSection();
147   
148   // If Label has already been emitted, verify that it is in the same section as
149   // section label for sanity.
150   assert((!Label->isInSection() || &Label->getSection() == &Section) &&
151          "Section offset using wrong section base for label");
152   
153   // If the section in question will end up with an address of 0 anyway, we can
154   // just emit an absolute reference to save a relocation.
155   if (Section.isBaseAddressKnownZero()) {
156     OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/);
157     return;
158   }
159   
160   // Otherwise, emit it as a label difference from the start of the section.
161   EmitLabelDifference(Label, SectionLabel, 4);
162 }
163
164 //===----------------------------------------------------------------------===//
165 // Dwarf Lowering Routines
166 //===----------------------------------------------------------------------===//
167
168 /// EmitCFIFrameMove - Emit a frame instruction.
169 void AsmPrinter::EmitCFIFrameMove(const MachineMove &Move) const {
170   const TargetRegisterInfo *RI = TM.getRegisterInfo();
171
172   const MachineLocation &Dst = Move.getDestination();
173   const MachineLocation &Src = Move.getSource();
174
175   // If advancing cfa.
176   if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
177     if (Src.getReg() == MachineLocation::VirtualFP) {
178       OutStreamer.EmitCFIDefCfaOffset(-Src.getOffset());
179     } else {
180       // Reg + Offset
181       OutStreamer.EmitCFIDefCfa(RI->getDwarfRegNum(Src.getReg(), true),
182                                 Src.getOffset());
183     }
184   } else if (Src.isReg() && Src.getReg() == MachineLocation::VirtualFP) {
185     assert(Dst.isReg() && "Machine move not supported yet.");
186     OutStreamer.EmitCFIDefCfaRegister(RI->getDwarfRegNum(Dst.getReg(), true));
187   } else {
188     assert(!Dst.isReg() && "Machine move not supported yet.");
189     OutStreamer.EmitCFIOffset(RI->getDwarfRegNum(Src.getReg(), true),
190                               Dst.getOffset());
191   }
192 }