LLVM API Documentation
00001 //===--- lib/CodeGen/DwarfPrinter.cpp - Dwarf Printer ---------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Emit general DWARF directives. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "DwarfPrinter.h" 00015 #include "llvm/Module.h" 00016 #include "llvm/CodeGen/AsmPrinter.h" 00017 #include "llvm/CodeGen/MachineFrameInfo.h" 00018 #include "llvm/CodeGen/MachineFunction.h" 00019 #include "llvm/CodeGen/MachineModuleInfo.h" 00020 #include "llvm/Support/Dwarf.h" 00021 #include "llvm/Support/ErrorHandling.h" 00022 #include "llvm/Target/TargetAsmInfo.h" 00023 #include "llvm/Target/TargetData.h" 00024 #include "llvm/Target/TargetFrameInfo.h" 00025 #include "llvm/Target/TargetRegisterInfo.h" 00026 00027 using namespace llvm; 00028 00029 Dwarf::Dwarf(raw_ostream &OS, AsmPrinter *A, const TargetAsmInfo *T, 00030 const char *flavor) 00031 : O(OS), Asm(A), TAI(T), TD(Asm->TM.getTargetData()), 00032 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL), 00033 SubprogramCount(0), Flavor(flavor), SetCounter(1) {} 00034 00035 void Dwarf::PrintRelDirective(bool Force32Bit, bool isInSection) const { 00036 if (isInSection && TAI->getDwarfSectionOffsetDirective()) 00037 O << TAI->getDwarfSectionOffsetDirective(); 00038 else if (Force32Bit || TD->getPointerSize() == sizeof(int32_t)) 00039 O << TAI->getData32bitsDirective(); 00040 else 00041 O << TAI->getData64bitsDirective(); 00042 } 00043 00044 /// PrintLabelName - Print label name in form used by Dwarf writer. 00045 /// 00046 void Dwarf::PrintLabelName(const char *Tag, unsigned Number) const { 00047 O << TAI->getPrivateGlobalPrefix() << Tag; 00048 if (Number) O << Number; 00049 } 00050 void Dwarf::PrintLabelName(const char *Tag, unsigned Number, 00051 const char *Suffix) const { 00052 O << TAI->getPrivateGlobalPrefix() << Tag; 00053 if (Number) O << Number; 00054 O << Suffix; 00055 } 00056 00057 /// EmitLabel - Emit location label for internal use by Dwarf. 00058 /// 00059 void Dwarf::EmitLabel(const char *Tag, unsigned Number) const { 00060 PrintLabelName(Tag, Number); 00061 O << ":\n"; 00062 } 00063 00064 /// EmitReference - Emit a reference to a label. 00065 /// 00066 void Dwarf::EmitReference(const char *Tag, unsigned Number, 00067 bool IsPCRelative, bool Force32Bit) const { 00068 PrintRelDirective(Force32Bit); 00069 PrintLabelName(Tag, Number); 00070 if (IsPCRelative) O << "-" << TAI->getPCSymbol(); 00071 } 00072 void Dwarf::EmitReference(const std::string &Name, bool IsPCRelative, 00073 bool Force32Bit) const { 00074 PrintRelDirective(Force32Bit); 00075 O << Name; 00076 if (IsPCRelative) O << "-" << TAI->getPCSymbol(); 00077 } 00078 00079 /// EmitDifference - Emit the difference between two labels. Some assemblers do 00080 /// not behave with absolute expressions with data directives, so there is an 00081 /// option (needsSet) to use an intermediary set expression. 00082 void Dwarf::EmitDifference(const char *TagHi, unsigned NumberHi, 00083 const char *TagLo, unsigned NumberLo, 00084 bool IsSmall) { 00085 if (TAI->needsSet()) { 00086 O << "\t.set\t"; 00087 PrintLabelName("set", SetCounter, Flavor); 00088 O << ","; 00089 PrintLabelName(TagHi, NumberHi); 00090 O << "-"; 00091 PrintLabelName(TagLo, NumberLo); 00092 O << "\n"; 00093 00094 PrintRelDirective(IsSmall); 00095 PrintLabelName("set", SetCounter, Flavor); 00096 ++SetCounter; 00097 } else { 00098 PrintRelDirective(IsSmall); 00099 PrintLabelName(TagHi, NumberHi); 00100 O << "-"; 00101 PrintLabelName(TagLo, NumberLo); 00102 } 00103 } 00104 00105 void Dwarf::EmitSectionOffset(const char* Label, const char* Section, 00106 unsigned LabelNumber, unsigned SectionNumber, 00107 bool IsSmall, bool isEH, 00108 bool useSet) { 00109 bool printAbsolute = false; 00110 if (isEH) 00111 printAbsolute = TAI->isAbsoluteEHSectionOffsets(); 00112 else 00113 printAbsolute = TAI->isAbsoluteDebugSectionOffsets(); 00114 00115 if (TAI->needsSet() && useSet) { 00116 O << "\t.set\t"; 00117 PrintLabelName("set", SetCounter, Flavor); 00118 O << ","; 00119 PrintLabelName(Label, LabelNumber); 00120 00121 if (!printAbsolute) { 00122 O << "-"; 00123 PrintLabelName(Section, SectionNumber); 00124 } 00125 00126 O << "\n"; 00127 PrintRelDirective(IsSmall); 00128 PrintLabelName("set", SetCounter, Flavor); 00129 ++SetCounter; 00130 } else { 00131 PrintRelDirective(IsSmall, true); 00132 PrintLabelName(Label, LabelNumber); 00133 00134 if (!printAbsolute) { 00135 O << "-"; 00136 PrintLabelName(Section, SectionNumber); 00137 } 00138 } 00139 } 00140 00141 /// EmitFrameMoves - Emit frame instructions to describe the layout of the 00142 /// frame. 00143 void Dwarf::EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID, 00144 const std::vector<MachineMove> &Moves, bool isEH) { 00145 int stackGrowth = 00146 Asm->TM.getFrameInfo()->getStackGrowthDirection() == 00147 TargetFrameInfo::StackGrowsUp ? 00148 TD->getPointerSize() : -TD->getPointerSize(); 00149 bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0; 00150 00151 for (unsigned i = 0, N = Moves.size(); i < N; ++i) { 00152 const MachineMove &Move = Moves[i]; 00153 unsigned LabelID = Move.getLabelID(); 00154 00155 if (LabelID) { 00156 LabelID = MMI->MappedLabel(LabelID); 00157 00158 // Throw out move if the label is invalid. 00159 if (!LabelID) continue; 00160 } 00161 00162 const MachineLocation &Dst = Move.getDestination(); 00163 const MachineLocation &Src = Move.getSource(); 00164 00165 // Advance row if new location. 00166 if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) { 00167 Asm->EmitInt8(dwarf::DW_CFA_advance_loc4); 00168 Asm->EOL("DW_CFA_advance_loc4"); 00169 EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true); 00170 Asm->EOL(); 00171 00172 BaseLabelID = LabelID; 00173 BaseLabel = "label"; 00174 IsLocal = true; 00175 } 00176 00177 // If advancing cfa. 00178 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { 00179 if (!Src.isReg()) { 00180 if (Src.getReg() == MachineLocation::VirtualFP) { 00181 Asm->EmitInt8(dwarf::DW_CFA_def_cfa_offset); 00182 Asm->EOL("DW_CFA_def_cfa_offset"); 00183 } else { 00184 Asm->EmitInt8(dwarf::DW_CFA_def_cfa); 00185 Asm->EOL("DW_CFA_def_cfa"); 00186 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), isEH)); 00187 Asm->EOL("Register"); 00188 } 00189 00190 int Offset = -Src.getOffset(); 00191 00192 Asm->EmitULEB128Bytes(Offset); 00193 Asm->EOL("Offset"); 00194 } else { 00195 llvm_unreachable("Machine move not supported yet."); 00196 } 00197 } else if (Src.isReg() && 00198 Src.getReg() == MachineLocation::VirtualFP) { 00199 if (Dst.isReg()) { 00200 Asm->EmitInt8(dwarf::DW_CFA_def_cfa_register); 00201 Asm->EOL("DW_CFA_def_cfa_register"); 00202 Asm->EmitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), isEH)); 00203 Asm->EOL("Register"); 00204 } else { 00205 llvm_unreachable("Machine move not supported yet."); 00206 } 00207 } else { 00208 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH); 00209 int Offset = Dst.getOffset() / stackGrowth; 00210 00211 if (Offset < 0) { 00212 Asm->EmitInt8(dwarf::DW_CFA_offset_extended_sf); 00213 Asm->EOL("DW_CFA_offset_extended_sf"); 00214 Asm->EmitULEB128Bytes(Reg); 00215 Asm->EOL("Reg"); 00216 Asm->EmitSLEB128Bytes(Offset); 00217 Asm->EOL("Offset"); 00218 } else if (Reg < 64) { 00219 Asm->EmitInt8(dwarf::DW_CFA_offset + Reg); 00220 if (Asm->isVerbose()) 00221 Asm->EOL("DW_CFA_offset + Reg (" + utostr(Reg) + ")"); 00222 else 00223 Asm->EOL(); 00224 Asm->EmitULEB128Bytes(Offset); 00225 Asm->EOL("Offset"); 00226 } else { 00227 Asm->EmitInt8(dwarf::DW_CFA_offset_extended); 00228 Asm->EOL("DW_CFA_offset_extended"); 00229 Asm->EmitULEB128Bytes(Reg); 00230 Asm->EOL("Reg"); 00231 Asm->EmitULEB128Bytes(Offset); 00232 Asm->EOL("Offset"); 00233 } 00234 } 00235 } 00236 }