blob: 7de6109967142adf6ff93463499efa0a5ccce2bc [file] [log] [blame]
Bill Wendling88423ee2009-05-15 00:11:17 +00001//===--- lib/CodeGen/DwarfPrinter.cpp - Dwarf Printer ---------------------===//
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// Emit general DWARF directives.
Anton Korobeynikov9184b252010-02-15 22:35:59 +000011//
Bill Wendling88423ee2009-05-15 00:11:17 +000012//===----------------------------------------------------------------------===//
13
14#include "DwarfPrinter.h"
15#include "llvm/Module.h"
16#include "llvm/CodeGen/AsmPrinter.h"
17#include "llvm/CodeGen/MachineFrameInfo.h"
David Greeneeea1f4c2009-08-19 21:59:18 +000018#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000019#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000021#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
Chris Lattnerf61ed8e2010-01-22 22:38:16 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattner858431d2010-01-16 18:50:28 +000024#include "llvm/MC/MCSymbol.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetFrameInfo.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000027#include "llvm/Target/TargetLoweringObjectFile.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000028#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner9d1c1ad2010-04-04 18:06:11 +000029#include "llvm/Target/TargetMachine.h"
Chris Lattner23132b12009-08-24 03:52:50 +000030#include "llvm/Support/Dwarf.h"
31#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000032#include "llvm/ADT/SmallString.h"
Bill Wendling88423ee2009-05-15 00:11:17 +000033using namespace llvm;
34
Chris Lattner75f50722010-04-04 07:48:20 +000035DwarfPrinter::DwarfPrinter(AsmPrinter *A)
36: Asm(A), MAI(A->MAI), TD(Asm->TM.getTargetData()),
Bill Wendling88423ee2009-05-15 00:11:17 +000037 RI(Asm->TM.getRegisterInfo()), M(NULL), MF(NULL), MMI(NULL),
Chris Lattnerfa070b02010-04-04 22:33:59 +000038 SubprogramCount(0) {
39}
Bill Wendling88423ee2009-05-15 00:11:17 +000040
Anton Korobeynikov9184b252010-02-15 22:35:59 +000041
Chris Lattnerfa070b02010-04-04 22:33:59 +000042/// EmitSectionOffset - Emit the 4-byte offset of Label from the start of its
43/// section. This can be done with a special directive if the target supports
44/// it (e.g. cygwin) or by emitting it as an offset from a label at the start
45/// of the section.
46///
47/// SectionLabel is a temporary label emitted at the start of the section that
48/// Label lives in.
Chris Lattnerb98b1bf2010-03-08 22:23:36 +000049void DwarfPrinter::EmitSectionOffset(const MCSymbol *Label,
Chris Lattnerfa070b02010-04-04 22:33:59 +000050 const MCSymbol *SectionLabel) const {
Chris Lattner2e3ebed2010-04-04 22:25:14 +000051 // On COFF targets, we have to emit the special .secrel32 directive.
Chris Lattner20e32802010-03-12 18:10:35 +000052 if (const char *SecOffDir = MAI->getDwarfSectionOffsetDirective()) {
Chris Lattner188a87d2010-03-10 01:04:13 +000053 // FIXME: MCize.
Chris Lattner0b9bdb42010-04-04 07:25:52 +000054 Asm->OutStreamer.EmitRawText(SecOffDir + Twine(Label->getName()));
Chris Lattner2e3ebed2010-04-04 22:25:14 +000055 return;
Chris Lattneref6b14f2010-03-09 00:17:58 +000056 }
Chris Lattnera33b2a12010-04-04 23:06:31 +000057
58 // Get the section that we're referring to, based on SectionLabel.
59 const MCSection &Section = SectionLabel->getSection();
60
61 // If Label has already been emitted, verify that it is in the same section as
62 // section label for sanity.
63 assert((!Label->isInSection() || &Label->getSection() == &Section) &&
64 "Section offset using wrong section base for label"); (void)Section;
Chris Lattner2e3ebed2010-04-04 22:25:14 +000065
66 // If the section in question will end up with an address of 0 anyway, we can
67 // just emit an absolute reference to save a relocation.
68 if (MAI->isAbsoluteDebugSectionOffsets()) {
69 Asm->OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/);
70 return;
71 }
72
Chris Lattnerfa070b02010-04-04 22:33:59 +000073 Asm->EmitLabelDifference(Label, SectionLabel, 4);
Bill Wendling88423ee2009-05-15 00:11:17 +000074}
75
76/// EmitFrameMoves - Emit frame instructions to describe the layout of the
77/// frame.
Chris Lattner5e6cbe02010-03-13 08:05:25 +000078void DwarfPrinter::EmitFrameMoves(MCSymbol *BaseLabel,
Chris Lattner066c9ac2010-01-22 22:23:57 +000079 const std::vector<MachineMove> &Moves,
80 bool isEH) {
Chris Lattnerfb658072010-03-13 07:40:56 +000081 int stackGrowth = TD->getPointerSize();
82 if (Asm->TM.getFrameInfo()->getStackGrowthDirection() !=
83 TargetFrameInfo::StackGrowsUp)
84 stackGrowth *= -1;
85
Bill Wendling88423ee2009-05-15 00:11:17 +000086 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
87 const MachineMove &Move = Moves[i];
Chris Lattner2e9919a2010-03-14 08:12:40 +000088 MCSymbol *Label = Move.getLabel();
Chris Lattnera34ec2292010-03-09 01:51:43 +000089 // Throw out move if the label is invalid.
Chris Lattner2e9919a2010-03-14 08:12:40 +000090 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
Bill Wendling88423ee2009-05-15 00:11:17 +000091
92 const MachineLocation &Dst = Move.getDestination();
93 const MachineLocation &Src = Move.getSource();
94
95 // Advance row if new location.
Chris Lattnerf14645c2010-03-14 07:02:50 +000096 if (BaseLabel && Label) {
Chris Lattnerb9130602010-03-14 02:20:58 +000097 MCSymbol *ThisSym = Label;
Chris Lattner5e6cbe02010-03-13 08:05:25 +000098 if (ThisSym != BaseLabel) {
Chris Lattner7a101f42010-04-04 20:01:25 +000099 Asm->EmitCFAByte(dwarf::DW_CFA_advance_loc4);
Chris Lattnera6437182010-04-04 19:58:12 +0000100 Asm->EmitLabelDifference(ThisSym, BaseLabel, 4);
Chris Lattner5e6cbe02010-03-13 08:05:25 +0000101 BaseLabel = ThisSym;
102 }
Bill Wendling88423ee2009-05-15 00:11:17 +0000103 }
104
105 // If advancing cfa.
106 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
107 if (!Src.isReg()) {
108 if (Src.getReg() == MachineLocation::VirtualFP) {
Chris Lattner7a101f42010-04-04 20:01:25 +0000109 Asm->EmitCFAByte(dwarf::DW_CFA_def_cfa_offset);
Bill Wendling88423ee2009-05-15 00:11:17 +0000110 } else {
Chris Lattner7a101f42010-04-04 20:01:25 +0000111 Asm->EmitCFAByte(dwarf::DW_CFA_def_cfa);
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000112 Asm->EmitULEB128(RI->getDwarfRegNum(Src.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000113 }
114
115 int Offset = -Src.getOffset();
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000116 Asm->EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000117 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000118 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000119 }
120 } else if (Src.isReg() &&
121 Src.getReg() == MachineLocation::VirtualFP) {
122 if (Dst.isReg()) {
Chris Lattner7a101f42010-04-04 20:01:25 +0000123 Asm->EmitCFAByte(dwarf::DW_CFA_def_cfa_register);
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000124 Asm->EmitULEB128(RI->getDwarfRegNum(Dst.getReg(), isEH), "Register");
Bill Wendling88423ee2009-05-15 00:11:17 +0000125 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000126 llvm_unreachable("Machine move not supported yet.");
Bill Wendling88423ee2009-05-15 00:11:17 +0000127 }
128 } else {
129 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), isEH);
130 int Offset = Dst.getOffset() / stackGrowth;
131
132 if (Offset < 0) {
Chris Lattner7a101f42010-04-04 20:01:25 +0000133 Asm->EmitCFAByte(dwarf::DW_CFA_offset_extended_sf);
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000134 Asm->EmitULEB128(Reg, "Reg");
135 Asm->EmitSLEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000136 } else if (Reg < 64) {
Chris Lattner7a101f42010-04-04 20:01:25 +0000137 Asm->EmitCFAByte(dwarf::DW_CFA_offset + Reg);
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000138 Asm->EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000139 } else {
Chris Lattner7a101f42010-04-04 20:01:25 +0000140 Asm->EmitCFAByte(dwarf::DW_CFA_offset_extended);
Chris Lattner7e1a8f82010-04-04 19:09:29 +0000141 Asm->EmitULEB128(Reg, "Reg");
142 Asm->EmitULEB128(Offset, "Offset");
Bill Wendling88423ee2009-05-15 00:11:17 +0000143 }
144 }
145 }
146}