blob: 93019d067cb05a61d27dd65b3bd3019f76a3abb9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
12// the output mechanism used by `llc'.
13//
14// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "asmprinter"
20#include "PPC.h"
21#include "PPCPredicates.h"
22#include "PPCTargetMachine.h"
23#include "PPCSubtarget.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Assembly/Writer.h"
28#include "llvm/CodeGen/AsmPrinter.h"
29#include "llvm/CodeGen/DwarfWriter.h"
30#include "llvm/CodeGen/MachineModuleInfo.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
32#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling36ccaea2008-01-26 06:51:24 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000034#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9bc87482010-01-13 19:00:57 +000035#include "llvm/MC/MCContext.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000036#include "llvm/MC/MCSectionMachO.h"
Chris Lattner73266f92009-08-19 05:49:37 +000037#include "llvm/MC/MCStreamer.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000038#include "llvm/MC/MCSymbol.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
40#include "llvm/Target/TargetRegisterInfo.h"
41#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetOptions.h"
43#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/Support/Mangler.h"
45#include "llvm/Support/MathExtras.h"
46#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000048#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000049#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050#include "llvm/ADT/Statistic.h"
51#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000052#include "llvm/ADT/StringSet.h"
Chris Lattnera38b2872010-01-13 06:38:18 +000053#include "llvm/ADT/SmallString.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054using namespace llvm;
55
56STATISTIC(EmittedInsts, "Number of machine instrs printed");
57
58namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000059 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +000060 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000061 struct FnStubInfo {
Chris Lattner9bc87482010-01-13 19:00:57 +000062 std::string StubName, LazyPtrName, AnonSymbolName;
63 MCSymbol *StubSym, *LazyPtrSym, *AnonSymbolSym;
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000064
Chris Lattner9bc87482010-01-13 19:00:57 +000065 FnStubInfo() {
66 StubSym = LazyPtrSym = AnonSymbolSym = 0;
67 }
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000068
69 void Init(const GlobalValue *GV, Mangler *Mang) {
70 // Already initialized.
Chris Lattner9bc87482010-01-13 19:00:57 +000071 if (!StubName.empty()) return;
72 StubName = Mang->getMangledName(GV, "$stub", true);
73 LazyPtrName = Mang->getMangledName(GV, "$lazy_ptr", true);
74 AnonSymbolName = Mang->getMangledName(GV, "$stub$tmp", true);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000075 }
76
Chris Lattner9bc87482010-01-13 19:00:57 +000077 void Init(StringRef GVName, Mangler *Mang, MCContext &Ctx) {
Chris Lattnerf80743e2010-01-13 07:56:59 +000078 assert(!GVName.empty());
Chris Lattner9bc87482010-01-13 19:00:57 +000079 if (StubSym != 0) return; // Already initialized.
Chris Lattnerf80743e2010-01-13 07:56:59 +000080 // Get the names for the external symbol name.
Chris Lattnera38b2872010-01-13 06:38:18 +000081 SmallString<128> TmpStr;
Chris Lattnerf80743e2010-01-13 07:56:59 +000082 Mang->getNameWithPrefix(TmpStr, GVName + "$stub", Mangler::Private);
Chris Lattner9bc87482010-01-13 19:00:57 +000083 StubSym = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattnera38b2872010-01-13 06:38:18 +000084 TmpStr.clear();
85
Chris Lattnerf80743e2010-01-13 07:56:59 +000086 Mang->getNameWithPrefix(TmpStr, GVName + "$lazy_ptr", Mangler::Private);
Chris Lattner9bc87482010-01-13 19:00:57 +000087 LazyPtrSym = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattnera38b2872010-01-13 06:38:18 +000088 TmpStr.clear();
89
Chris Lattnerf80743e2010-01-13 07:56:59 +000090 Mang->getNameWithPrefix(TmpStr, GVName + "$stub$tmp", Mangler::Private);
Chris Lattner9bc87482010-01-13 19:00:57 +000091 AnonSymbolSym = Ctx.GetOrCreateSymbol(TmpStr.str());
92 }
93
94 void printStub(raw_ostream &OS, const MCAsmInfo *MAI) const {
95 if (StubSym)
96 StubSym->print(OS, MAI);
97 else
98 OS << StubName;
99 }
100 void printLazyPtr(raw_ostream &OS, const MCAsmInfo *MAI) const {
101 if (LazyPtrSym)
102 LazyPtrSym->print(OS, MAI);
103 else
104 OS << LazyPtrName;
105 }
106 void printAnonSymbol(raw_ostream &OS, const MCAsmInfo *MAI) const {
107 if (AnonSymbolSym)
108 AnonSymbolSym->print(OS, MAI);
109 else
110 OS << AnonSymbolName;
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000111 }
112 };
113
114 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000115 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000117 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +0000118 public:
David Greene302008d2009-07-14 20:18:05 +0000119 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000120 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000121 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000122 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
124 virtual const char *getPassName() const {
125 return "PowerPC Assembly Printer";
126 }
127
128 PPCTargetMachine &getTM() {
129 return static_cast<PPCTargetMachine&>(TM);
130 }
131
132 unsigned enumRegToMachineReg(unsigned enumReg) {
133 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000134 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 case PPC::CR0: return 0;
136 case PPC::CR1: return 1;
137 case PPC::CR2: return 2;
138 case PPC::CR3: return 3;
139 case PPC::CR4: return 4;
140 case PPC::CR5: return 5;
141 case PPC::CR6: return 6;
142 case PPC::CR7: return 7;
143 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000144 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 }
146
147 /// printInstruction - This method is automatically generated by tablegen
148 /// from the instruction set description. This method returns true if the
149 /// machine instruction was sufficiently described to print it, otherwise it
150 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000151 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +0000152 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +0000153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154
155 void printMachineInstruction(const MachineInstr *MI);
156 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 /// stripRegisterPrefix - This method strips the character prefix from a
159 /// register name so that only the number is left. Used by for linux asm.
160 const char *stripRegisterPrefix(const char *RegName) {
161 switch (RegName[0]) {
162 case 'r':
163 case 'f':
164 case 'v': return RegName + 1;
165 case 'c': if (RegName[1] == 'r') return RegName + 2;
166 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000167
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 return RegName;
169 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 /// printRegister - Print register according to target requirements.
172 ///
173 void printRegister(const MachineOperand &MO, bool R0AsZero) {
174 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000175 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000176
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 // If we should use 0 for R0.
178 if (R0AsZero && RegNo == PPC::R0) {
179 O << "0";
180 return;
181 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000182
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000183 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 // Linux assembler (Others?) does not take register mnemonics.
185 // FIXME - What about special registers used in mfspr/mtspr?
186 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
187 O << RegName;
188 }
189
190 void printOperand(const MachineInstr *MI, unsigned OpNo) {
191 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000192 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000194 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000195 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 } else {
197 printOp(MO);
198 }
199 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000200
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
202 unsigned AsmVariant, const char *ExtraCode);
203 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
204 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000205
206
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000208 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 value = (value << (32-5)) >> (32-5);
210 O << (int)value;
211 }
212 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000213 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 assert(value <= 31 && "Invalid u5imm argument!");
215 O << (unsigned int)value;
216 }
217 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000218 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 assert(value <= 63 && "Invalid u6imm argument!");
220 O << (unsigned int)value;
221 }
222 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000223 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 }
225 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000226 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 }
228 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000229 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000230 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 } else {
232 O << "lo16(";
233 printOp(MI->getOperand(OpNo));
234 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000235 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 else
237 O << ')';
238 }
239 }
240 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
241 // Branches can take an immediate operand. This is used by the branch
242 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000243 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000244 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 } else {
246 printOp(MI->getOperand(OpNo));
247 }
248 }
249 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
250 const MachineOperand &MO = MI->getOperand(OpNo);
251 if (TM.getRelocationModel() != Reloc::Static) {
252 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
253 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000254 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000256 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
257 FnInfo.Init(GV, Mang);
Chris Lattner9bc87482010-01-13 19:00:57 +0000258 FnInfo.printStub(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 return;
260 }
261 }
262 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000263 SmallString<128> MangledName;
Chris Lattnerf80743e2010-01-13 07:56:59 +0000264 Mang->getNameWithPrefix(MangledName, MO.getSymbolName());
Chris Lattnera38b2872010-01-13 06:38:18 +0000265 FnStubInfo &FnInfo = FnStubs[MangledName.str()];
Chris Lattner9bc87482010-01-13 19:00:57 +0000266 FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
267 FnInfo.printStub(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 return;
269 }
270 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000271
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 printOp(MI->getOperand(OpNo));
273 }
274 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000275 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 }
277 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000278 O << "\"L" << getFunctionNumber() << "$pb\"\n";
279 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 }
281 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000282 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 printS16ImmOperand(MI, OpNo);
284 } else {
285 if (Subtarget.isDarwin()) O << "ha16(";
286 printOp(MI->getOperand(OpNo));
287 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000288 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 if (Subtarget.isDarwin())
290 O << ')';
291 else
292 O << "@ha";
293 }
294 }
295 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000296 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 printS16ImmOperand(MI, OpNo);
298 } else {
299 if (Subtarget.isDarwin()) O << "lo16(";
300 printOp(MI->getOperand(OpNo));
301 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000302 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 if (Subtarget.isDarwin())
304 O << ')';
305 else
306 O << "@l";
307 }
308 }
309 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
310 unsigned CCReg = MI->getOperand(OpNo).getReg();
311 unsigned RegNo = enumRegToMachineReg(CCReg);
312 O << (0x80 >> RegNo);
313 }
314 // The new addressing mode printers.
315 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
316 printSymbolLo(MI, OpNo);
317 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000318 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 MI->getOperand(OpNo+1).getReg() == PPC::R0)
320 O << "0";
321 else
322 printOperand(MI, OpNo+1);
323 O << ')';
324 }
325 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000326 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000328 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 printSymbolLo(MI, OpNo);
330 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000331 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 MI->getOperand(OpNo+1).getReg() == PPC::R0)
333 O << "0";
334 else
335 printOperand(MI, OpNo+1);
336 O << ')';
337 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
340 // When used as the base register, r0 reads constant zero rather than
341 // the value contained in the register. For this reason, the darwin
342 // assembler requires that we print r0 as 0 (no r) when used as the base.
343 const MachineOperand &MO = MI->getOperand(OpNo);
344 printRegister(MO, true);
345 O << ", ";
346 printOperand(MI, OpNo+1);
347 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000348
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000349 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
350 const MachineOperand &MO = MI->getOperand(OpNo);
351
352 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
353
354 GlobalValue *GV = MO.getGlobal();
355
356 std::string Name = Mang->getMangledName(GV);
357
358 // Map symbol -> label of TOC entry.
359 if (TOC.count(Name) == 0) {
360 std::string Label;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000361 Label += MAI->getPrivateGlobalPrefix();
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000362 Label += "C";
363 Label += utostr(LabelID++);
364
365 TOC[Name] = Label;
366 }
367
368 O << TOC[Name] << "@toc";
369 }
370
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000371 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 };
376
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000377 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000378 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000379 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000380 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000381 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000382 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383
384 virtual const char *getPassName() const {
385 return "Linux PPC Assembly Printer";
386 }
387
388 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000389 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000390
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 void getAnalysisUsage(AnalysisUsage &AU) const {
392 AU.setPreservesAll();
393 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000394 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 PPCAsmPrinter::getAnalysisUsage(AU);
396 }
397
Chris Lattnerae982212009-07-21 18:38:57 +0000398 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 };
400
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000401 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
402 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000403 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000404 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000405 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000406 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000407 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000408 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409
410 virtual const char *getPassName() const {
411 return "Darwin PPC Assembly Printer";
412 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000413
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000416 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000417
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 void getAnalysisUsage(AnalysisUsage &AU) const {
419 AU.setPreservesAll();
420 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000421 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 PPCAsmPrinter::getAnalysisUsage(AU);
423 }
424
Chris Lattnerae982212009-07-21 18:38:57 +0000425 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 };
427} // end of anonymous namespace
428
429// Include the auto-generated portion of the assembly writer
430#include "PPCGenAsmWriter.inc"
431
432void PPCAsmPrinter::printOp(const MachineOperand &MO) {
433 switch (MO.getType()) {
434 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000435 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436
437 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000438 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 return;
440 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000441 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000442 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 // FIXME: PIC relocation model
444 return;
445 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000446 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000447 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000449 case MachineOperand::MO_BlockAddress:
450 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
451 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000452 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 // Computing the address of an external symbol, not calling it.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000454 std::string Name(MAI->getGlobalPrefix());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000455 Name += MO.getSymbolName();
456
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000458 GVStubs[Name] = Name+"$non_lazy_ptr";
459 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000461 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000463 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 case MachineOperand::MO_GlobalAddress: {
465 // Computing the address of a global symbol, not calling it.
466 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000467 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468
469 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000470 if (TM.getRelocationModel() != Reloc::Static &&
471 (GV->isDeclaration() || GV->isWeakForLinker())) {
472 if (!GV->hasHiddenVisibility()) {
473 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
474 GVStubs[Mang->getMangledName(GV)] = Name;
475 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
476 GV->hasAvailableExternallyLinkage()) {
477 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
478 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
479 } else {
480 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000482 } else {
483 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 }
485 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000486
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000487 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 return;
489 }
490
491 default:
492 O << "<unknown operand type: " << MO.getType() << ">";
493 return;
494 }
495}
496
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497/// PrintAsmOperand - Print out an operand for an inline asm expression.
498///
499bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000500 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 const char *ExtraCode) {
502 // Does this asm operand have a single letter operand modifier?
503 if (ExtraCode && ExtraCode[0]) {
504 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000505
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 switch (ExtraCode[0]) {
507 default: return true; // Unknown modifier.
508 case 'c': // Don't print "$" before a global var name or constant.
509 // PPC never has a prefix.
510 printOperand(MI, OpNo);
511 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000512 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000514 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000516 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 return true;
518 ++OpNo; // Return the high-part.
519 break;
520 case 'I':
521 // Write 'i' if an integer constant, otherwise nothing. Used to print
522 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000523 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 O << "i";
525 return false;
526 }
527 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000528
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 printOperand(MI, OpNo);
530 return false;
531}
532
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000533// At the moment, all inline asm memory operands are a single register.
534// In any case, the output of this routine should always be just one
535// assembler operand.
536
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000538 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 const char *ExtraCode) {
540 if (ExtraCode && ExtraCode[0])
541 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000542 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000543 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000544 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000545 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 return false;
547}
548
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000549void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 const char *Modifier) {
551 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
552 unsigned Code = MI->getOperand(OpNo).getImm();
553 if (!strcmp(Modifier, "cc")) {
554 switch ((PPC::Predicate)Code) {
555 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
556 case PPC::PRED_LT: O << "lt"; return;
557 case PPC::PRED_LE: O << "le"; return;
558 case PPC::PRED_EQ: O << "eq"; return;
559 case PPC::PRED_GE: O << "ge"; return;
560 case PPC::PRED_GT: O << "gt"; return;
561 case PPC::PRED_NE: O << "ne"; return;
562 case PPC::PRED_UN: O << "un"; return;
563 case PPC::PRED_NU: O << "nu"; return;
564 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000565
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 } else {
567 assert(!strcmp(Modifier, "reg") &&
568 "Need to specify 'cc' or 'reg' as predicate op modifier!");
569 // Don't print the register for 'always'.
570 if (Code == PPC::PRED_ALWAYS) return;
571 printOperand(MI, OpNo+1);
572 }
573}
574
575
576/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
577/// the current output stream.
578///
579void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
580 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000581
Devang Patel5450fc12009-10-06 02:19:11 +0000582 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583
584 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000585 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000587 unsigned char SH = MI->getOperand(2).getImm();
588 unsigned char MB = MI->getOperand(3).getImm();
589 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000591 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 }
593 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000594 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 SH = 32-SH;
596 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000597 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 printOperand(MI, 0);
599 O << ", ";
600 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000601 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 }
603 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
604 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000605 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000606 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 printOperand(MI, 0);
608 O << ", ";
609 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 }
611 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000612 unsigned char SH = MI->getOperand(2).getImm();
613 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
615 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000616 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000617 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 printOperand(MI, 0);
619 O << ", ";
620 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000621 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 }
623 }
624
Dale Johannesen760acc02010-01-06 02:20:18 +0000625 if (!useSubstituteMnemonic)
626 printInstruction(MI);
627
David Greeneca9b04b2009-11-13 21:34:57 +0000628 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000629 EmitComments(*MI);
630 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000631
632 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633}
634
635/// runOnMachineFunction - This uses the printMachineInstruction()
636/// method to print assembly for each instruction.
637///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000638bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000639 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640
641 SetupMachineFunction(MF);
642 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000643
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 // Print out constants referenced by the function
645 EmitConstantPool(MF.getConstantPool());
646
647 // Print out labels for the function.
648 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000649 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000650
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000652 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000653 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 case Function::InternalLinkage: // Symbols default to internal.
655 break;
656 case Function::ExternalLinkage:
657 O << "\t.global\t" << CurrentFnName << '\n'
658 << "\t.type\t" << CurrentFnName << ", @function\n";
659 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000660 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000661 case Function::WeakAnyLinkage:
662 case Function::WeakODRLinkage:
663 case Function::LinkOnceAnyLinkage:
664 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 O << "\t.global\t" << CurrentFnName << '\n';
666 O << "\t.weak\t" << CurrentFnName << '\n';
667 break;
668 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000669
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000670 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000671
Bill Wendling25a8ae32009-06-30 22:38:32 +0000672 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000673
674 if (Subtarget.isPPC64()) {
675 // Emit an official procedure descriptor.
676 // FIXME 64-bit SVR4: Use MCSection here?
677 O << "\t.section\t\".opd\",\"aw\"\n";
678 O << "\t.align 3\n";
679 O << CurrentFnName << ":\n";
680 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
681 O << "\t.previous\n";
682 O << ".L." << CurrentFnName << ":\n";
683 } else {
684 O << CurrentFnName << ":\n";
685 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686
687 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000688 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000689
690 // Print out code for the function.
691 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
692 I != E; ++I) {
693 // Print a label for the basic block.
694 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000695 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 }
697 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
698 II != E; ++II) {
699 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 printMachineInstruction(II);
701 }
702 }
703
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000704 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
Chris Lattner73266f92009-08-19 05:49:37 +0000706 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000707
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000709 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000710
Bill Wendling6fb58e12009-11-09 21:20:14 +0000711 // Print out jump tables referenced by the function.
712 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
713
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 // We didn't modify anything.
715 return false;
716}
717
Chris Lattnerae982212009-07-21 18:38:57 +0000718void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 const TargetData *TD = TM.getTargetData();
720
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000721 if (!GVar->hasInitializer())
722 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000723
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000724 // Check to see if this is a special global used by LLVM, if so, emit it.
725 if (EmitSpecialLLVMGlobal(GVar))
726 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000728 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000730 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000731
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000732 Constant *C = GVar->getInitializer();
733 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000734 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000735 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736
Chris Lattner73266f92009-08-19 05:49:37 +0000737 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
738 TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000739
740 if (C->isNullValue() && /* FIXME: Verify correct */
741 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000742 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000743 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000745
746 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 O << "\t.global " << name << '\n';
748 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000749 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000750 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000751 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000752 O << MAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000754 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755 }
Evan Cheng11db8142009-03-24 00:17:40 +0000756 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000757 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000758 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000759 O << "'";
760 }
761 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000762 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 }
764
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000765 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000766 case GlobalValue::LinkOnceAnyLinkage:
767 case GlobalValue::LinkOnceODRLinkage:
768 case GlobalValue::WeakAnyLinkage:
769 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000770 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000771 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000772 O << "\t.global " << name << '\n'
773 << "\t.type " << name << ", @object\n"
774 << "\t.weak " << name << '\n';
775 break;
776 case GlobalValue::AppendingLinkage:
777 // FIXME: appending linkage variables should go into a section of
778 // their name or something. For now, just emit them as external.
779 case GlobalValue::ExternalLinkage:
780 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000781 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000782 << "\t.type " << name << ", @object\n";
783 // FALL THROUGH
784 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000785 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000786 break;
787 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000788 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000789 }
790
791 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000792 O << name << ":";
793 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000794 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000795 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000796 O << "'";
797 }
798 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000799
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000800 EmitGlobalConstant(C);
801 O << '\n';
802}
803
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000804bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
805 const TargetData *TD = TM.getTargetData();
806
807 bool isPPC64 = TD->getPointerSizeInBits() == 64;
808
809 if (isPPC64 && !TOC.empty()) {
810 // FIXME 64-bit SVR4: Use MCSection here?
811 O << "\t.section\t\".toc\",\"aw\"\n";
812
813 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
814 I != E; ++I) {
815 O << I->second << ":\n";
816 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
817 }
818 }
819
820 return AsmPrinter::doFinalization(M);
821}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823/// runOnMachineFunction - This uses the printMachineInstruction()
824/// method to print assembly for each instruction.
825///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000826bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000827 this->MF = &MF;
828
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000829 SetupMachineFunction(MF);
830 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000831
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 // Print out constants referenced by the function
833 EmitConstantPool(MF.getConstantPool());
834
835 // Print out labels for the function.
836 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000837 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000840 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000841 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 case Function::InternalLinkage: // Symbols default to internal.
843 break;
844 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000845 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000847 case Function::WeakAnyLinkage:
848 case Function::WeakODRLinkage:
849 case Function::LinkOnceAnyLinkage:
850 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000851 case Function::LinkerPrivateLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000852 O << "\t.globl\t" << CurrentFnName << '\n';
853 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 break;
855 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000856
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000857 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000858
Bill Wendling25a8ae32009-06-30 22:38:32 +0000859 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 O << CurrentFnName << ":\n";
861
862 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000863 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864
Bill Wendling36ccaea2008-01-26 06:51:24 +0000865 // If the function is empty, then we need to emit *something*. Otherwise, the
866 // function's label might be associated with something that it wasn't meant to
867 // be associated with. We emit a noop in this situation.
868 MachineFunction::iterator I = MF.begin();
869
Bill Wendlingb5880a72008-01-26 09:03:52 +0000870 if (++I == MF.end() && MF.front().empty())
871 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000872
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 // Print out code for the function.
874 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
875 I != E; ++I) {
876 // Print a label for the basic block.
877 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000878 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000880 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
881 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 printMachineInstruction(II);
884 }
885 }
886
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000888 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000889
Bill Wendling5019fe02009-11-09 21:45:26 +0000890 // Print out jump tables referenced by the function.
891 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
892
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 // We didn't modify anything.
894 return false;
895}
896
897
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000898void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000899 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000900 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 "ppc",
902 "ppc601",
903 "ppc602",
904 "ppc603",
905 "ppc7400",
906 "ppc750",
907 "ppc970",
908 "ppc64"
909 };
910
911 unsigned Directive = Subtarget.getDarwinDirective();
912 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
913 Directive = PPC::DIR_970;
914 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
915 Directive = PPC::DIR_7400;
916 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
917 Directive = PPC::DIR_64;
918 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000919 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000920
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 // Prime text sections so they are adjacent. This reduces the likelihood a
922 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000923 TargetLoweringObjectFileMachO &TLOFMacho =
924 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000925 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000927 OutStreamer.SwitchSection(
928 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
929 MCSectionMachO::S_SYMBOL_STUBS |
930 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
931 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000933 OutStreamer.SwitchSection(
934 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
935 MCSectionMachO::S_SYMBOL_STUBS |
936 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
937 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938 }
Chris Lattner73266f92009-08-19 05:49:37 +0000939 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940}
941
Chris Lattnerae982212009-07-21 18:38:57 +0000942void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000943 const TargetData *TD = TM.getTargetData();
944
945 if (!GVar->hasInitializer())
946 return; // External global require no code
947
948 // Check to see if this is a special global used by LLVM, if so, emit it.
949 if (EmitSpecialLLVMGlobal(GVar)) {
950 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000951 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000952 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000953 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000954 O << ".reference .destructors_used\n";
955 }
956 return;
957 }
958
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000959 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000960 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000961
962 Constant *C = GVar->getInitializer();
963 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000964 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000965 unsigned Align = TD->getPreferredAlignmentLog(GVar);
966
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000967 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000968 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000969 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000970
Chris Lattnerdb727932009-08-04 05:35:56 +0000971 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000972 if (C->isNullValue() && /* FIXME: Verify correct */
973 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000974 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000975 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000976 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000977 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000978 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
979
980 if (GVar->hasExternalLinkage()) {
981 O << "\t.globl " << name << '\n';
982 O << "\t.zerofill __DATA, __common, " << name << ", "
983 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000984 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000985 O << MAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000986 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000987 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000988 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000989 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000990 O << name << ":";
991 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000992 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000993 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000994 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000995 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000996 EmitGlobalConstant(C);
997 return;
998 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000999 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001000 // Darwin 9 and above support aligned common data.
1001 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001002 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001003 }
Evan Cheng11db8142009-03-24 00:17:40 +00001004 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001005 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001006 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001007 O << "'";
1008 }
1009 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001010 return;
1011 }
1012
1013 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +00001014 case GlobalValue::LinkOnceAnyLinkage:
1015 case GlobalValue::LinkOnceODRLinkage:
1016 case GlobalValue::WeakAnyLinkage:
1017 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +00001018 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +00001019 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001020 O << "\t.globl " << name << '\n'
1021 << "\t.weak_definition " << name << '\n';
1022 break;
1023 case GlobalValue::AppendingLinkage:
1024 // FIXME: appending linkage variables should go into a section of
1025 // their name or something. For now, just emit them as external.
1026 case GlobalValue::ExternalLinkage:
1027 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001028 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001029 // FALL THROUGH
1030 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +00001031 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001032 break;
1033 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001034 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001035 }
1036
1037 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001038 O << name << ":";
1039 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001040 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001041 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001042 O << "'";
1043 }
1044 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001045
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001046 EmitGlobalConstant(C);
1047 O << '\n';
1048}
1049
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001050bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 const TargetData *TD = TM.getTargetData();
1052
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1054
Chris Lattnerf4815552009-08-03 22:52:21 +00001055 // Darwin/PPC always uses mach-o.
1056 TargetLoweringObjectFileMachO &TLOFMacho =
1057 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1058
Chris Lattner72a676a2009-08-10 01:39:42 +00001059
1060 const MCSection *LSPSection = 0;
1061 if (!FnStubs.empty()) // .lazy_symbol_pointer
1062 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1063
1064
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001066 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001067 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001068 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1069 MCSectionMachO::S_SYMBOL_STUBS |
1070 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1071 32, SectionKind::getText());
Chris Lattner9bc87482010-01-13 19:00:57 +00001072 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001073 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001074 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001076 const FnStubInfo &Info = I->second;
Chris Lattner9bc87482010-01-13 19:00:57 +00001077 Info.printStub(O, MAI);
1078 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001079 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080 O << "\tmflr r0\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001081 O << "\tbcl 20,31,";
1082 Info.printAnonSymbol(O, MAI);
1083 O << '\n';
1084 Info.printAnonSymbol(O, MAI);
1085 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 O << "\tmflr r11\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001087 O << "\taddis r11,r11,ha16(";
1088 Info.printLazyPtr(O, MAI);
1089 O << '-';
1090 Info.printAnonSymbol(O, MAI);
Evan Chenga65854f2008-12-05 01:06:39 +00001091 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001093 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner9bc87482010-01-13 19:00:57 +00001094 Info.printLazyPtr(O, MAI);
1095 O << '-';
1096 Info.printAnonSymbol(O, MAI);
1097 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 O << "\tmtctr r12\n";
1099 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001100
Chris Lattner73266f92009-08-19 05:49:37 +00001101 OutStreamer.SwitchSection(LSPSection);
Chris Lattner9bc87482010-01-13 19:00:57 +00001102 Info.printLazyPtr(O, MAI);
1103 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001104 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001105 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 }
Chris Lattner189198f2009-07-15 00:55:58 +00001107 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001108 const MCSection *StubSection =
1109 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1110 MCSectionMachO::S_SYMBOL_STUBS |
1111 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1112 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001113
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001114 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001115 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001116 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001118 const FnStubInfo &Info = I->second;
Chris Lattner9bc87482010-01-13 19:00:57 +00001119 Info.printStub(O, MAI);
1120 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001121 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner9bc87482010-01-13 19:00:57 +00001122 O << "\tlis r11,ha16(";
1123 Info.printLazyPtr(O, MAI);
1124 O << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001125 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner9bc87482010-01-13 19:00:57 +00001126 Info.printLazyPtr(O, MAI);
1127 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001128 O << "\tmtctr r12\n";
1129 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001130 OutStreamer.SwitchSection(LSPSection);
Chris Lattner9bc87482010-01-13 19:00:57 +00001131 Info.printLazyPtr(O, MAI);
1132 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001133 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001134 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001135 }
1136 }
1137
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001138 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001139
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001140 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001141 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001142 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001143 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001144 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001145 E = Personalities.end(); I != E; ++I) {
1146 if (*I)
1147 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001148 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001149 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001150 }
1151
Chris Lattnerf4815552009-08-03 22:52:21 +00001152 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001153 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001154 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001155 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001156 EmitAlignment(isPPC64 ? 3 : 2);
1157
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001158 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1159 E = GVStubs.end(); I != E; ++I) {
1160 O << I->second << ":\n";
1161 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1162 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001163 }
1164 }
1165
Evan Chenga65854f2008-12-05 01:06:39 +00001166 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001167 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001168 EmitAlignment(isPPC64 ? 3 : 2);
1169 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1170 E = HiddenGVStubs.end(); I != E; ++I) {
1171 O << I->second << ":\n";
1172 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001173 }
1174 }
1175
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 // Funny Darwin hack: This flag tells the linker that no global symbols
1177 // contain code that falls through to other global symbols (e.g. the obvious
1178 // implementation of multiple entry points). If this doesn't occur, the
1179 // linker can safely perform dead code stripping. Since LLVM never generates
1180 // code that does this, it is always safe to set.
Chris Lattnerfe284f72009-10-19 18:03:08 +00001181 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001182
Dan Gohman4a558a32007-07-25 19:33:14 +00001183 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184}
1185
1186
1187
1188/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1189/// for a MachineFunction to the given output stream, in a format that the
1190/// Darwin assembler can deal with.
1191///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001192static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1193 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001194 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001195 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001196 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1197
Chris Lattnerae982212009-07-21 18:38:57 +00001198 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001199 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1200 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001201}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001202
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001203// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001204extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001205 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001206 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1207}