blob: da29572676153c26983554917b4637042387fc48 [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;
Chris Lattner53087e52010-01-13 19:05:36 +000072
73 // Get the names.
Chris Lattner9bc87482010-01-13 19:00:57 +000074 StubName = Mang->getMangledName(GV, "$stub", true);
75 LazyPtrName = Mang->getMangledName(GV, "$lazy_ptr", true);
76 AnonSymbolName = Mang->getMangledName(GV, "$stub$tmp", true);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000077 }
78
Chris Lattner9bc87482010-01-13 19:00:57 +000079 void Init(StringRef GVName, Mangler *Mang, MCContext &Ctx) {
Chris Lattnerf80743e2010-01-13 07:56:59 +000080 assert(!GVName.empty());
Chris Lattner9bc87482010-01-13 19:00:57 +000081 if (StubSym != 0) return; // Already initialized.
Chris Lattnerf80743e2010-01-13 07:56:59 +000082 // Get the names for the external symbol name.
Chris Lattnera38b2872010-01-13 06:38:18 +000083 SmallString<128> TmpStr;
Chris Lattnerf80743e2010-01-13 07:56:59 +000084 Mang->getNameWithPrefix(TmpStr, GVName + "$stub", Mangler::Private);
Chris Lattner9bc87482010-01-13 19:00:57 +000085 StubSym = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner53087e52010-01-13 19:05:36 +000086 TmpStr.erase(TmpStr.end()-5, TmpStr.end()); // Remove $stub
87
88 TmpStr += "$lazy_ptr";
Chris Lattner9bc87482010-01-13 19:00:57 +000089 LazyPtrSym = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner53087e52010-01-13 19:05:36 +000090 TmpStr.erase(TmpStr.end()-9, TmpStr.end()); // Remove $lazy_ptr
Chris Lattnera38b2872010-01-13 06:38:18 +000091
Chris Lattner53087e52010-01-13 19:05:36 +000092 TmpStr += "$stub$tmp";
Chris Lattner9bc87482010-01-13 19:00:57 +000093 AnonSymbolSym = Ctx.GetOrCreateSymbol(TmpStr.str());
94 }
95
96 void printStub(raw_ostream &OS, const MCAsmInfo *MAI) const {
97 if (StubSym)
98 StubSym->print(OS, MAI);
99 else
100 OS << StubName;
101 }
102 void printLazyPtr(raw_ostream &OS, const MCAsmInfo *MAI) const {
103 if (LazyPtrSym)
104 LazyPtrSym->print(OS, MAI);
105 else
106 OS << LazyPtrName;
107 }
108 void printAnonSymbol(raw_ostream &OS, const MCAsmInfo *MAI) const {
109 if (AnonSymbolSym)
110 AnonSymbolSym->print(OS, MAI);
111 else
112 OS << AnonSymbolName;
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000113 }
114 };
115
116 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000117 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000119 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +0000120 public:
David Greene302008d2009-07-14 20:18:05 +0000121 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000122 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000123 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000124 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125
126 virtual const char *getPassName() const {
127 return "PowerPC Assembly Printer";
128 }
129
130 PPCTargetMachine &getTM() {
131 return static_cast<PPCTargetMachine&>(TM);
132 }
133
134 unsigned enumRegToMachineReg(unsigned enumReg) {
135 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000136 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 case PPC::CR0: return 0;
138 case PPC::CR1: return 1;
139 case PPC::CR2: return 2;
140 case PPC::CR3: return 3;
141 case PPC::CR4: return 4;
142 case PPC::CR5: return 5;
143 case PPC::CR6: return 6;
144 case PPC::CR7: return 7;
145 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000146 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 }
148
149 /// printInstruction - This method is automatically generated by tablegen
150 /// from the instruction set description. This method returns true if the
151 /// machine instruction was sufficiently described to print it, otherwise it
152 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000153 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +0000154 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +0000155
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156
157 void printMachineInstruction(const MachineInstr *MI);
158 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 /// stripRegisterPrefix - This method strips the character prefix from a
161 /// register name so that only the number is left. Used by for linux asm.
162 const char *stripRegisterPrefix(const char *RegName) {
163 switch (RegName[0]) {
164 case 'r':
165 case 'f':
166 case 'v': return RegName + 1;
167 case 'c': if (RegName[1] == 'r') return RegName + 2;
168 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 return RegName;
171 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 /// printRegister - Print register according to target requirements.
174 ///
175 void printRegister(const MachineOperand &MO, bool R0AsZero) {
176 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000177 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000178
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 // If we should use 0 for R0.
180 if (R0AsZero && RegNo == PPC::R0) {
181 O << "0";
182 return;
183 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000184
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000185 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 // Linux assembler (Others?) does not take register mnemonics.
187 // FIXME - What about special registers used in mfspr/mtspr?
188 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
189 O << RegName;
190 }
191
192 void printOperand(const MachineInstr *MI, unsigned OpNo) {
193 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000194 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000196 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000197 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 } else {
199 printOp(MO);
200 }
201 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000202
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
204 unsigned AsmVariant, const char *ExtraCode);
205 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
206 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000207
208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000210 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 value = (value << (32-5)) >> (32-5);
212 O << (int)value;
213 }
214 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000215 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 assert(value <= 31 && "Invalid u5imm argument!");
217 O << (unsigned int)value;
218 }
219 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000220 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 assert(value <= 63 && "Invalid u6imm argument!");
222 O << (unsigned int)value;
223 }
224 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000225 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 }
227 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000228 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 }
230 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000231 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000232 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 } else {
234 O << "lo16(";
235 printOp(MI->getOperand(OpNo));
236 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000237 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 else
239 O << ')';
240 }
241 }
242 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
243 // Branches can take an immediate operand. This is used by the branch
244 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000245 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000246 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 } else {
248 printOp(MI->getOperand(OpNo));
249 }
250 }
251 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
252 const MachineOperand &MO = MI->getOperand(OpNo);
253 if (TM.getRelocationModel() != Reloc::Static) {
254 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
255 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000256 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000258 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
259 FnInfo.Init(GV, Mang);
Chris Lattner9bc87482010-01-13 19:00:57 +0000260 FnInfo.printStub(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 return;
262 }
263 }
264 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000265 SmallString<128> MangledName;
Chris Lattnerf80743e2010-01-13 07:56:59 +0000266 Mang->getNameWithPrefix(MangledName, MO.getSymbolName());
Chris Lattnera38b2872010-01-13 06:38:18 +0000267 FnStubInfo &FnInfo = FnStubs[MangledName.str()];
Chris Lattner9bc87482010-01-13 19:00:57 +0000268 FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
269 FnInfo.printStub(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 return;
271 }
272 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 printOp(MI->getOperand(OpNo));
275 }
276 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000277 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 }
279 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000280 O << "\"L" << getFunctionNumber() << "$pb\"\n";
281 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 }
283 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000284 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 printS16ImmOperand(MI, OpNo);
286 } else {
287 if (Subtarget.isDarwin()) O << "ha16(";
288 printOp(MI->getOperand(OpNo));
289 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000290 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 if (Subtarget.isDarwin())
292 O << ')';
293 else
294 O << "@ha";
295 }
296 }
297 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000298 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 printS16ImmOperand(MI, OpNo);
300 } else {
301 if (Subtarget.isDarwin()) O << "lo16(";
302 printOp(MI->getOperand(OpNo));
303 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000304 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 if (Subtarget.isDarwin())
306 O << ')';
307 else
308 O << "@l";
309 }
310 }
311 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
312 unsigned CCReg = MI->getOperand(OpNo).getReg();
313 unsigned RegNo = enumRegToMachineReg(CCReg);
314 O << (0x80 >> RegNo);
315 }
316 // The new addressing mode printers.
317 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
318 printSymbolLo(MI, OpNo);
319 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000320 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 MI->getOperand(OpNo+1).getReg() == PPC::R0)
322 O << "0";
323 else
324 printOperand(MI, OpNo+1);
325 O << ')';
326 }
327 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000328 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000330 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 printSymbolLo(MI, OpNo);
332 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000333 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 MI->getOperand(OpNo+1).getReg() == PPC::R0)
335 O << "0";
336 else
337 printOperand(MI, OpNo+1);
338 O << ')';
339 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000340
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
342 // When used as the base register, r0 reads constant zero rather than
343 // the value contained in the register. For this reason, the darwin
344 // assembler requires that we print r0 as 0 (no r) when used as the base.
345 const MachineOperand &MO = MI->getOperand(OpNo);
346 printRegister(MO, true);
347 O << ", ";
348 printOperand(MI, OpNo+1);
349 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000350
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000351 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
352 const MachineOperand &MO = MI->getOperand(OpNo);
353
354 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
355
356 GlobalValue *GV = MO.getGlobal();
357
358 std::string Name = Mang->getMangledName(GV);
359
360 // Map symbol -> label of TOC entry.
361 if (TOC.count(Name) == 0) {
362 std::string Label;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000363 Label += MAI->getPrivateGlobalPrefix();
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000364 Label += "C";
365 Label += utostr(LabelID++);
366
367 TOC[Name] = Label;
368 }
369
370 O << TOC[Name] << "@toc";
371 }
372
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000373 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000375
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 };
378
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000379 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000380 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000381 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000382 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000383 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000384 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385
386 virtual const char *getPassName() const {
387 return "Linux PPC Assembly Printer";
388 }
389
390 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000391 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000392
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 void getAnalysisUsage(AnalysisUsage &AU) const {
394 AU.setPreservesAll();
395 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000396 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 PPCAsmPrinter::getAnalysisUsage(AU);
398 }
399
Chris Lattnerae982212009-07-21 18:38:57 +0000400 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 };
402
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000403 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
404 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000405 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000406 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000407 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000408 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000409 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000410 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411
412 virtual const char *getPassName() const {
413 return "Darwin PPC Assembly Printer";
414 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000415
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000418 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000419
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 void getAnalysisUsage(AnalysisUsage &AU) const {
421 AU.setPreservesAll();
422 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000423 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 PPCAsmPrinter::getAnalysisUsage(AU);
425 }
426
Chris Lattnerae982212009-07-21 18:38:57 +0000427 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 };
429} // end of anonymous namespace
430
431// Include the auto-generated portion of the assembly writer
432#include "PPCGenAsmWriter.inc"
433
434void PPCAsmPrinter::printOp(const MachineOperand &MO) {
435 switch (MO.getType()) {
436 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000437 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438
439 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000440 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 return;
442 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000443 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000444 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 // FIXME: PIC relocation model
446 return;
447 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000448 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000449 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000451 case MachineOperand::MO_BlockAddress:
452 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
453 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000454 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 // Computing the address of an external symbol, not calling it.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000456 std::string Name(MAI->getGlobalPrefix());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000457 Name += MO.getSymbolName();
458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000460 GVStubs[Name] = Name+"$non_lazy_ptr";
461 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000463 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000465 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 case MachineOperand::MO_GlobalAddress: {
467 // Computing the address of a global symbol, not calling it.
468 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000469 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470
471 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000472 if (TM.getRelocationModel() != Reloc::Static &&
473 (GV->isDeclaration() || GV->isWeakForLinker())) {
474 if (!GV->hasHiddenVisibility()) {
475 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
476 GVStubs[Mang->getMangledName(GV)] = Name;
477 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
478 GV->hasAvailableExternallyLinkage()) {
479 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
480 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
481 } else {
482 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000484 } else {
485 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 }
487 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000488
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000489 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 return;
491 }
492
493 default:
494 O << "<unknown operand type: " << MO.getType() << ">";
495 return;
496 }
497}
498
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499/// PrintAsmOperand - Print out an operand for an inline asm expression.
500///
501bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000502 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 const char *ExtraCode) {
504 // Does this asm operand have a single letter operand modifier?
505 if (ExtraCode && ExtraCode[0]) {
506 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000507
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 switch (ExtraCode[0]) {
509 default: return true; // Unknown modifier.
510 case 'c': // Don't print "$" before a global var name or constant.
511 // PPC never has a prefix.
512 printOperand(MI, OpNo);
513 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000514 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000516 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000518 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 return true;
520 ++OpNo; // Return the high-part.
521 break;
522 case 'I':
523 // Write 'i' if an integer constant, otherwise nothing. Used to print
524 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000525 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 O << "i";
527 return false;
528 }
529 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000530
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 printOperand(MI, OpNo);
532 return false;
533}
534
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000535// At the moment, all inline asm memory operands are a single register.
536// In any case, the output of this routine should always be just one
537// assembler operand.
538
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000540 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 const char *ExtraCode) {
542 if (ExtraCode && ExtraCode[0])
543 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000544 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000545 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000546 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000547 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 return false;
549}
550
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000551void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 const char *Modifier) {
553 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
554 unsigned Code = MI->getOperand(OpNo).getImm();
555 if (!strcmp(Modifier, "cc")) {
556 switch ((PPC::Predicate)Code) {
557 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
558 case PPC::PRED_LT: O << "lt"; return;
559 case PPC::PRED_LE: O << "le"; return;
560 case PPC::PRED_EQ: O << "eq"; return;
561 case PPC::PRED_GE: O << "ge"; return;
562 case PPC::PRED_GT: O << "gt"; return;
563 case PPC::PRED_NE: O << "ne"; return;
564 case PPC::PRED_UN: O << "un"; return;
565 case PPC::PRED_NU: O << "nu"; return;
566 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000567
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 } else {
569 assert(!strcmp(Modifier, "reg") &&
570 "Need to specify 'cc' or 'reg' as predicate op modifier!");
571 // Don't print the register for 'always'.
572 if (Code == PPC::PRED_ALWAYS) return;
573 printOperand(MI, OpNo+1);
574 }
575}
576
577
578/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
579/// the current output stream.
580///
581void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
582 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000583
Devang Patel5450fc12009-10-06 02:19:11 +0000584 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585
586 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000587 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000589 unsigned char SH = MI->getOperand(2).getImm();
590 unsigned char MB = MI->getOperand(3).getImm();
591 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000593 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 }
595 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000596 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 SH = 32-SH;
598 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000599 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 printOperand(MI, 0);
601 O << ", ";
602 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000603 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 }
605 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
606 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000607 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000608 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 printOperand(MI, 0);
610 O << ", ";
611 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 }
613 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000614 unsigned char SH = MI->getOperand(2).getImm();
615 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
617 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000618 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000619 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 printOperand(MI, 0);
621 O << ", ";
622 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000623 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 }
625 }
626
Dale Johannesen760acc02010-01-06 02:20:18 +0000627 if (!useSubstituteMnemonic)
628 printInstruction(MI);
629
David Greeneca9b04b2009-11-13 21:34:57 +0000630 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000631 EmitComments(*MI);
632 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000633
634 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635}
636
637/// runOnMachineFunction - This uses the printMachineInstruction()
638/// method to print assembly for each instruction.
639///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000640bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000641 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642
643 SetupMachineFunction(MF);
644 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000645
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 // Print out constants referenced by the function
647 EmitConstantPool(MF.getConstantPool());
648
649 // Print out labels for the function.
650 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000651 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000652
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000654 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000655 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000656 case Function::InternalLinkage: // Symbols default to internal.
657 break;
658 case Function::ExternalLinkage:
659 O << "\t.global\t" << CurrentFnName << '\n'
660 << "\t.type\t" << CurrentFnName << ", @function\n";
661 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000662 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000663 case Function::WeakAnyLinkage:
664 case Function::WeakODRLinkage:
665 case Function::LinkOnceAnyLinkage:
666 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 O << "\t.global\t" << CurrentFnName << '\n';
668 O << "\t.weak\t" << CurrentFnName << '\n';
669 break;
670 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000671
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000672 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000673
Bill Wendling25a8ae32009-06-30 22:38:32 +0000674 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000675
676 if (Subtarget.isPPC64()) {
677 // Emit an official procedure descriptor.
678 // FIXME 64-bit SVR4: Use MCSection here?
679 O << "\t.section\t\".opd\",\"aw\"\n";
680 O << "\t.align 3\n";
681 O << CurrentFnName << ":\n";
682 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
683 O << "\t.previous\n";
684 O << ".L." << CurrentFnName << ":\n";
685 } else {
686 O << CurrentFnName << ":\n";
687 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688
689 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000690 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691
692 // Print out code for the function.
693 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
694 I != E; ++I) {
695 // Print a label for the basic block.
696 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000697 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 }
699 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
700 II != E; ++II) {
701 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 printMachineInstruction(II);
703 }
704 }
705
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000706 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707
Chris Lattner73266f92009-08-19 05:49:37 +0000708 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000709
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000711 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000712
Bill Wendling6fb58e12009-11-09 21:20:14 +0000713 // Print out jump tables referenced by the function.
714 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
715
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 // We didn't modify anything.
717 return false;
718}
719
Chris Lattnerae982212009-07-21 18:38:57 +0000720void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 const TargetData *TD = TM.getTargetData();
722
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000723 if (!GVar->hasInitializer())
724 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000725
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000726 // Check to see if this is a special global used by LLVM, if so, emit it.
727 if (EmitSpecialLLVMGlobal(GVar))
728 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000730 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000732 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000733
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000734 Constant *C = GVar->getInitializer();
735 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000736 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000737 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738
Chris Lattner73266f92009-08-19 05:49:37 +0000739 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
740 TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000741
742 if (C->isNullValue() && /* FIXME: Verify correct */
743 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000744 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000745 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000747
748 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 O << "\t.global " << name << '\n';
750 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000751 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000752 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000753 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000754 O << MAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000756 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 }
Evan Cheng11db8142009-03-24 00:17:40 +0000758 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000759 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000760 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000761 O << "'";
762 }
763 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000764 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 }
766
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000767 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000768 case GlobalValue::LinkOnceAnyLinkage:
769 case GlobalValue::LinkOnceODRLinkage:
770 case GlobalValue::WeakAnyLinkage:
771 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000772 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000773 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000774 O << "\t.global " << name << '\n'
775 << "\t.type " << name << ", @object\n"
776 << "\t.weak " << name << '\n';
777 break;
778 case GlobalValue::AppendingLinkage:
779 // FIXME: appending linkage variables should go into a section of
780 // their name or something. For now, just emit them as external.
781 case GlobalValue::ExternalLinkage:
782 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000783 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000784 << "\t.type " << name << ", @object\n";
785 // FALL THROUGH
786 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000787 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000788 break;
789 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000790 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000791 }
792
793 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000794 O << name << ":";
795 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000796 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000797 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000798 O << "'";
799 }
800 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000801
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000802 EmitGlobalConstant(C);
803 O << '\n';
804}
805
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000806bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
807 const TargetData *TD = TM.getTargetData();
808
809 bool isPPC64 = TD->getPointerSizeInBits() == 64;
810
811 if (isPPC64 && !TOC.empty()) {
812 // FIXME 64-bit SVR4: Use MCSection here?
813 O << "\t.section\t\".toc\",\"aw\"\n";
814
815 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
816 I != E; ++I) {
817 O << I->second << ":\n";
818 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
819 }
820 }
821
822 return AsmPrinter::doFinalization(M);
823}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825/// runOnMachineFunction - This uses the printMachineInstruction()
826/// method to print assembly for each instruction.
827///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000828bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000829 this->MF = &MF;
830
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 SetupMachineFunction(MF);
832 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000833
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 // Print out constants referenced by the function
835 EmitConstantPool(MF.getConstantPool());
836
837 // Print out labels for the function.
838 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000839 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000840
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000842 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000843 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 case Function::InternalLinkage: // Symbols default to internal.
845 break;
846 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000847 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000849 case Function::WeakAnyLinkage:
850 case Function::WeakODRLinkage:
851 case Function::LinkOnceAnyLinkage:
852 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000853 case Function::LinkerPrivateLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000854 O << "\t.globl\t" << CurrentFnName << '\n';
855 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 break;
857 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000858
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000859 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000860
Bill Wendling25a8ae32009-06-30 22:38:32 +0000861 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 O << CurrentFnName << ":\n";
863
864 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000865 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866
Bill Wendling36ccaea2008-01-26 06:51:24 +0000867 // If the function is empty, then we need to emit *something*. Otherwise, the
868 // function's label might be associated with something that it wasn't meant to
869 // be associated with. We emit a noop in this situation.
870 MachineFunction::iterator I = MF.begin();
871
Bill Wendlingb5880a72008-01-26 09:03:52 +0000872 if (++I == MF.end() && MF.front().empty())
873 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000874
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 // Print out code for the function.
876 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
877 I != E; ++I) {
878 // Print a label for the basic block.
879 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000880 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000882 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
883 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885 printMachineInstruction(II);
886 }
887 }
888
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000890 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000891
Bill Wendling5019fe02009-11-09 21:45:26 +0000892 // Print out jump tables referenced by the function.
893 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
894
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 // We didn't modify anything.
896 return false;
897}
898
899
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000900void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000901 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000902 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 "ppc",
904 "ppc601",
905 "ppc602",
906 "ppc603",
907 "ppc7400",
908 "ppc750",
909 "ppc970",
910 "ppc64"
911 };
912
913 unsigned Directive = Subtarget.getDarwinDirective();
914 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
915 Directive = PPC::DIR_970;
916 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
917 Directive = PPC::DIR_7400;
918 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
919 Directive = PPC::DIR_64;
920 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000921 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000922
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 // Prime text sections so they are adjacent. This reduces the likelihood a
924 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000925 TargetLoweringObjectFileMachO &TLOFMacho =
926 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000927 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000928 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000929 OutStreamer.SwitchSection(
930 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
931 MCSectionMachO::S_SYMBOL_STUBS |
932 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
933 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000934 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000935 OutStreamer.SwitchSection(
936 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
937 MCSectionMachO::S_SYMBOL_STUBS |
938 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
939 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000940 }
Chris Lattner73266f92009-08-19 05:49:37 +0000941 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000942}
943
Chris Lattnerae982212009-07-21 18:38:57 +0000944void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000945 const TargetData *TD = TM.getTargetData();
946
947 if (!GVar->hasInitializer())
948 return; // External global require no code
949
950 // Check to see if this is a special global used by LLVM, if so, emit it.
951 if (EmitSpecialLLVMGlobal(GVar)) {
952 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000953 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000954 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000955 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000956 O << ".reference .destructors_used\n";
957 }
958 return;
959 }
960
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000961 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000962 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000963
964 Constant *C = GVar->getInitializer();
965 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000966 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000967 unsigned Align = TD->getPreferredAlignmentLog(GVar);
968
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000969 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000970 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000971 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000972
Chris Lattnerdb727932009-08-04 05:35:56 +0000973 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000974 if (C->isNullValue() && /* FIXME: Verify correct */
975 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000976 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000977 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000978 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000979 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000980 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
981
982 if (GVar->hasExternalLinkage()) {
983 O << "\t.globl " << name << '\n';
984 O << "\t.zerofill __DATA, __common, " << name << ", "
985 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000986 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000987 O << MAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000988 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000989 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000990 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000991 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000992 O << name << ":";
993 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000994 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000995 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000996 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000997 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000998 EmitGlobalConstant(C);
999 return;
1000 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001001 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001002 // Darwin 9 and above support aligned common data.
1003 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001004 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001005 }
Evan Cheng11db8142009-03-24 00:17:40 +00001006 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001007 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001008 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001009 O << "'";
1010 }
1011 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001012 return;
1013 }
1014
1015 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +00001016 case GlobalValue::LinkOnceAnyLinkage:
1017 case GlobalValue::LinkOnceODRLinkage:
1018 case GlobalValue::WeakAnyLinkage:
1019 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +00001020 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +00001021 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001022 O << "\t.globl " << name << '\n'
1023 << "\t.weak_definition " << name << '\n';
1024 break;
1025 case GlobalValue::AppendingLinkage:
1026 // FIXME: appending linkage variables should go into a section of
1027 // their name or something. For now, just emit them as external.
1028 case GlobalValue::ExternalLinkage:
1029 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001030 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001031 // FALL THROUGH
1032 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +00001033 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001034 break;
1035 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001036 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001037 }
1038
1039 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001040 O << name << ":";
1041 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001042 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001043 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001044 O << "'";
1045 }
1046 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001047
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001048 EmitGlobalConstant(C);
1049 O << '\n';
1050}
1051
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001052bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001053 const TargetData *TD = TM.getTargetData();
1054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1056
Chris Lattnerf4815552009-08-03 22:52:21 +00001057 // Darwin/PPC always uses mach-o.
1058 TargetLoweringObjectFileMachO &TLOFMacho =
1059 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1060
Chris Lattner72a676a2009-08-10 01:39:42 +00001061
1062 const MCSection *LSPSection = 0;
1063 if (!FnStubs.empty()) // .lazy_symbol_pointer
1064 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1065
1066
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001068 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001069 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001070 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1071 MCSectionMachO::S_SYMBOL_STUBS |
1072 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1073 32, SectionKind::getText());
Chris Lattner9bc87482010-01-13 19:00:57 +00001074 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001075 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001076 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001077 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001078 const FnStubInfo &Info = I->second;
Chris Lattner9bc87482010-01-13 19:00:57 +00001079 Info.printStub(O, MAI);
1080 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001081 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 O << "\tmflr r0\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001083 O << "\tbcl 20,31,";
1084 Info.printAnonSymbol(O, MAI);
1085 O << '\n';
1086 Info.printAnonSymbol(O, MAI);
1087 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088 O << "\tmflr r11\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001089 O << "\taddis r11,r11,ha16(";
1090 Info.printLazyPtr(O, MAI);
1091 O << '-';
1092 Info.printAnonSymbol(O, MAI);
Evan Chenga65854f2008-12-05 01:06:39 +00001093 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001095 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner9bc87482010-01-13 19:00:57 +00001096 Info.printLazyPtr(O, MAI);
1097 O << '-';
1098 Info.printAnonSymbol(O, MAI);
1099 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 O << "\tmtctr r12\n";
1101 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001102
Chris Lattner73266f92009-08-19 05:49:37 +00001103 OutStreamer.SwitchSection(LSPSection);
Chris Lattner9bc87482010-01-13 19:00:57 +00001104 Info.printLazyPtr(O, MAI);
1105 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001106 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001107 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108 }
Chris Lattner189198f2009-07-15 00:55:58 +00001109 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001110 const MCSection *StubSection =
1111 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1112 MCSectionMachO::S_SYMBOL_STUBS |
1113 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1114 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001115
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001116 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001117 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001118 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001120 const FnStubInfo &Info = I->second;
Chris Lattner9bc87482010-01-13 19:00:57 +00001121 Info.printStub(O, MAI);
1122 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001123 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner9bc87482010-01-13 19:00:57 +00001124 O << "\tlis r11,ha16(";
1125 Info.printLazyPtr(O, MAI);
1126 O << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001127 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner9bc87482010-01-13 19:00:57 +00001128 Info.printLazyPtr(O, MAI);
1129 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 O << "\tmtctr r12\n";
1131 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001132 OutStreamer.SwitchSection(LSPSection);
Chris Lattner9bc87482010-01-13 19:00:57 +00001133 Info.printLazyPtr(O, MAI);
1134 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001135 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001136 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 }
1138 }
1139
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001140 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001142 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001143 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001144 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001145 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001146 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001147 E = Personalities.end(); I != E; ++I) {
1148 if (*I)
1149 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001150 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001151 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001152 }
1153
Chris Lattnerf4815552009-08-03 22:52:21 +00001154 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001155 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001156 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001157 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001158 EmitAlignment(isPPC64 ? 3 : 2);
1159
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001160 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1161 E = GVStubs.end(); I != E; ++I) {
1162 O << I->second << ":\n";
1163 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1164 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 }
1166 }
1167
Evan Chenga65854f2008-12-05 01:06:39 +00001168 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001169 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001170 EmitAlignment(isPPC64 ? 3 : 2);
1171 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1172 E = HiddenGVStubs.end(); I != E; ++I) {
1173 O << I->second << ":\n";
1174 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001175 }
1176 }
1177
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001178 // Funny Darwin hack: This flag tells the linker that no global symbols
1179 // contain code that falls through to other global symbols (e.g. the obvious
1180 // implementation of multiple entry points). If this doesn't occur, the
1181 // linker can safely perform dead code stripping. Since LLVM never generates
1182 // code that does this, it is always safe to set.
Chris Lattnerfe284f72009-10-19 18:03:08 +00001183 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001184
Dan Gohman4a558a32007-07-25 19:33:14 +00001185 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186}
1187
1188
1189
1190/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1191/// for a MachineFunction to the given output stream, in a format that the
1192/// Darwin assembler can deal with.
1193///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001194static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1195 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001196 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001197 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1199
Chris Lattnerae982212009-07-21 18:38:57 +00001200 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001201 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1202 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001204
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001205// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001206extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001207 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001208 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1209}