blob: 7fb3208e8a86eda529cb5915273cb82fc8e61fee [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 Lattner1774fd32010-01-13 19:13:16 +000062 MCSymbol *Stub, *LazyPtr, *AnonSymbol;
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000063
Chris Lattner9bc87482010-01-13 19:00:57 +000064 FnStubInfo() {
Chris Lattner1774fd32010-01-13 19:13:16 +000065 Stub = LazyPtr = AnonSymbol = 0;
Chris Lattner9bc87482010-01-13 19:00:57 +000066 }
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000067
Chris Lattner9f9fd9d2010-01-16 01:45:47 +000068 void Init(const GlobalValue *GV, AsmPrinter *Printer) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000069 // Already initialized.
Chris Lattner1774fd32010-01-13 19:13:16 +000070 if (Stub != 0) return;
Chris Lattner53087e52010-01-13 19:05:36 +000071
72 // Get the names.
Chris Lattner9f9fd9d2010-01-16 01:45:47 +000073 Stub = Printer->GetPrivateGlobalValueSymbolStub(GV, "$stub");
74 LazyPtr = Printer->GetPrivateGlobalValueSymbolStub(GV, "$lazy_ptr");
75 AnonSymbol = Printer->GetPrivateGlobalValueSymbolStub(GV, "$stub$tmp");
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000076 }
77
Chris Lattner9bc87482010-01-13 19:00:57 +000078 void Init(StringRef GVName, Mangler *Mang, MCContext &Ctx) {
Chris Lattner1774fd32010-01-13 19:13:16 +000079 assert(!GVName.empty() && "external symbol name shouldn't be empty");
80 if (Stub != 0) return; // Already initialized.
Chris Lattnerf80743e2010-01-13 07:56:59 +000081 // Get the names for the external symbol name.
Chris Lattnera38b2872010-01-13 06:38:18 +000082 SmallString<128> TmpStr;
Chris Lattner1774fd32010-01-13 19:13:16 +000083 Mang->getNameWithPrefix(TmpStr, GVName, Mangler::Private);
Chris Lattner1774fd32010-01-13 19:13:16 +000084 TmpStr += "$stub";
85 Stub = 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 Lattner1774fd32010-01-13 19:13:16 +000089 LazyPtr = 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 Lattner1774fd32010-01-13 19:13:16 +000093 AnonSymbol = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000094 }
95 };
96
Chris Lattner5eeaf2a2010-01-16 02:00:23 +000097 DenseMap<const MCSymbol*, FnStubInfo> FnStubs;
98 StringMap<const MCSymbol*> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000100 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +0000101 public:
David Greene302008d2009-07-14 20:18:05 +0000102 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000103 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000104 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000105 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 virtual const char *getPassName() const {
108 return "PowerPC Assembly Printer";
109 }
110
111 PPCTargetMachine &getTM() {
112 return static_cast<PPCTargetMachine&>(TM);
113 }
114
115 unsigned enumRegToMachineReg(unsigned enumReg) {
116 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000117 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 case PPC::CR0: return 0;
119 case PPC::CR1: return 1;
120 case PPC::CR2: return 2;
121 case PPC::CR3: return 3;
122 case PPC::CR4: return 4;
123 case PPC::CR5: return 5;
124 case PPC::CR6: return 6;
125 case PPC::CR7: return 7;
126 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000127 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 }
129
130 /// printInstruction - This method is automatically generated by tablegen
131 /// from the instruction set description. This method returns true if the
132 /// machine instruction was sufficiently described to print it, otherwise it
133 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000134 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +0000135 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +0000136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137
138 void printMachineInstruction(const MachineInstr *MI);
139 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 /// stripRegisterPrefix - This method strips the character prefix from a
142 /// register name so that only the number is left. Used by for linux asm.
143 const char *stripRegisterPrefix(const char *RegName) {
144 switch (RegName[0]) {
145 case 'r':
146 case 'f':
147 case 'v': return RegName + 1;
148 case 'c': if (RegName[1] == 'r') return RegName + 2;
149 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 return RegName;
152 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 /// printRegister - Print register according to target requirements.
155 ///
156 void printRegister(const MachineOperand &MO, bool R0AsZero) {
157 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000158 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 // If we should use 0 for R0.
161 if (R0AsZero && RegNo == PPC::R0) {
162 O << "0";
163 return;
164 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000165
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000166 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 // Linux assembler (Others?) does not take register mnemonics.
168 // FIXME - What about special registers used in mfspr/mtspr?
169 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
170 O << RegName;
171 }
172
173 void printOperand(const MachineInstr *MI, unsigned OpNo) {
174 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000175 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000177 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000178 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else {
180 printOp(MO);
181 }
182 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000183
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
185 unsigned AsmVariant, const char *ExtraCode);
186 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
187 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000188
189
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000191 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 value = (value << (32-5)) >> (32-5);
193 O << (int)value;
194 }
195 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000196 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 assert(value <= 31 && "Invalid u5imm argument!");
198 O << (unsigned int)value;
199 }
200 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000201 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 assert(value <= 63 && "Invalid u6imm argument!");
203 O << (unsigned int)value;
204 }
205 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000206 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 }
208 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000209 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 }
211 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000212 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000213 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 } else {
215 O << "lo16(";
216 printOp(MI->getOperand(OpNo));
217 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000218 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 else
220 O << ')';
221 }
222 }
223 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
224 // Branches can take an immediate operand. This is used by the branch
225 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000226 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000227 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 } else {
229 printOp(MI->getOperand(OpNo));
230 }
231 }
232 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
233 const MachineOperand &MO = MI->getOperand(OpNo);
234 if (TM.getRelocationModel() != Reloc::Static) {
235 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
236 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000237 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 // Dynamically-resolved functions need a stub for the function.
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000239 FnStubInfo &FnInfo = FnStubs[GetGlobalValueSymbol(GV)];
Chris Lattner9f9fd9d2010-01-16 01:45:47 +0000240 FnInfo.Init(GV, this);
Chris Lattner1774fd32010-01-13 19:13:16 +0000241 FnInfo.Stub->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 return;
243 }
244 }
245 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000246 FnStubInfo &FnInfo =
247 FnStubs[GetExternalSymbolSymbol(MO.getSymbolName())];
Chris Lattner9bc87482010-01-13 19:00:57 +0000248 FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
Chris Lattner1774fd32010-01-13 19:13:16 +0000249 FnInfo.Stub->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 return;
251 }
252 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 printOp(MI->getOperand(OpNo));
255 }
256 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000257 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 }
259 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000260 O << "\"L" << getFunctionNumber() << "$pb\"\n";
261 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 }
263 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000264 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 printS16ImmOperand(MI, OpNo);
266 } else {
267 if (Subtarget.isDarwin()) O << "ha16(";
268 printOp(MI->getOperand(OpNo));
269 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000270 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 if (Subtarget.isDarwin())
272 O << ')';
273 else
274 O << "@ha";
275 }
276 }
277 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000278 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 printS16ImmOperand(MI, OpNo);
280 } else {
281 if (Subtarget.isDarwin()) O << "lo16(";
282 printOp(MI->getOperand(OpNo));
283 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000284 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 if (Subtarget.isDarwin())
286 O << ')';
287 else
288 O << "@l";
289 }
290 }
291 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
292 unsigned CCReg = MI->getOperand(OpNo).getReg();
293 unsigned RegNo = enumRegToMachineReg(CCReg);
294 O << (0x80 >> RegNo);
295 }
296 // The new addressing mode printers.
297 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
298 printSymbolLo(MI, OpNo);
299 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000300 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 MI->getOperand(OpNo+1).getReg() == PPC::R0)
302 O << "0";
303 else
304 printOperand(MI, OpNo+1);
305 O << ')';
306 }
307 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000308 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000310 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 printSymbolLo(MI, OpNo);
312 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000313 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 MI->getOperand(OpNo+1).getReg() == PPC::R0)
315 O << "0";
316 else
317 printOperand(MI, OpNo+1);
318 O << ')';
319 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000320
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
322 // When used as the base register, r0 reads constant zero rather than
323 // the value contained in the register. For this reason, the darwin
324 // assembler requires that we print r0 as 0 (no r) when used as the base.
325 const MachineOperand &MO = MI->getOperand(OpNo);
326 printRegister(MO, true);
327 O << ", ";
328 printOperand(MI, OpNo+1);
329 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000330
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000331 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
332 const MachineOperand &MO = MI->getOperand(OpNo);
333
334 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
335
336 GlobalValue *GV = MO.getGlobal();
337
338 std::string Name = Mang->getMangledName(GV);
339
340 // Map symbol -> label of TOC entry.
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000341 if (TOC.count(Name) == 0)
342 TOC[Name] = OutContext.
343 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) + "C" +
344 Twine(LabelID++));
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000345
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000346 TOC[Name]->print(O, MAI);
347 O << "@toc";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000348 }
349
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000350 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000352
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 };
355
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000356 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000357 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000358 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000359 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000360 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000361 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362
363 virtual const char *getPassName() const {
364 return "Linux PPC Assembly Printer";
365 }
366
367 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000368 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000369
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 void getAnalysisUsage(AnalysisUsage &AU) const {
371 AU.setPreservesAll();
372 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000373 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 PPCAsmPrinter::getAnalysisUsage(AU);
375 }
376
Chris Lattnerae982212009-07-21 18:38:57 +0000377 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 };
379
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000380 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
381 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000382 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000383 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000384 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000385 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000386 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000387 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388
389 virtual const char *getPassName() const {
390 return "Darwin PPC Assembly Printer";
391 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000392
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000395 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000396
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 void getAnalysisUsage(AnalysisUsage &AU) const {
398 AU.setPreservesAll();
399 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000400 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 PPCAsmPrinter::getAnalysisUsage(AU);
402 }
403
Chris Lattnerae982212009-07-21 18:38:57 +0000404 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 };
406} // end of anonymous namespace
407
408// Include the auto-generated portion of the assembly writer
409#include "PPCGenAsmWriter.inc"
410
411void PPCAsmPrinter::printOp(const MachineOperand &MO) {
412 switch (MO.getType()) {
413 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000414 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415
416 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000417 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 return;
419 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000420 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000421 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 // FIXME: PIC relocation model
423 return;
424 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000425 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000426 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000428 case MachineOperand::MO_BlockAddress:
429 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
430 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000431 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 // Computing the address of an external symbol, not calling it.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000433 std::string Name(MAI->getGlobalPrefix());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000434 Name += MO.getSymbolName();
435
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000437 GVStubs[Name] =
438 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
439 MO.getSymbolName()+"$non_lazy_ptr");
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000440 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000442 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000444 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 case MachineOperand::MO_GlobalAddress: {
446 // Computing the address of a global symbol, not calling it.
447 GlobalValue *GV = MO.getGlobal();
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000448 MCSymbol *SymToPrint;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449
450 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000451 if (TM.getRelocationModel() != Reloc::Static &&
452 (GV->isDeclaration() || GV->isWeakForLinker())) {
453 if (!GV->hasHiddenVisibility()) {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000454 SymToPrint = GetPrivateGlobalValueSymbolStub(GV, "$non_lazy_ptr");
455 GVStubs[Mang->getMangledName(GV)] = SymToPrint;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000456 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
457 GV->hasAvailableExternallyLinkage()) {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000458 SymToPrint = GetPrivateGlobalValueSymbolStub(GV, "$non_lazy_ptr");
459 HiddenGVStubs[Mang->getMangledName(GV)] = SymToPrint;
460 GetGlobalValueSymbol(GV)->print(O, MAI);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000461 } else {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000462 SymToPrint = GetGlobalValueSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000464 } else {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000465 SymToPrint = GetGlobalValueSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 }
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000467
468 SymToPrint->print(O, MAI);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000469
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000470 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 return;
472 }
473
474 default:
475 O << "<unknown operand type: " << MO.getType() << ">";
476 return;
477 }
478}
479
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480/// PrintAsmOperand - Print out an operand for an inline asm expression.
481///
482bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000483 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 const char *ExtraCode) {
485 // Does this asm operand have a single letter operand modifier?
486 if (ExtraCode && ExtraCode[0]) {
487 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000488
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 switch (ExtraCode[0]) {
490 default: return true; // Unknown modifier.
491 case 'c': // Don't print "$" before a global var name or constant.
492 // PPC never has a prefix.
493 printOperand(MI, OpNo);
494 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000495 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000497 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000499 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 return true;
501 ++OpNo; // Return the high-part.
502 break;
503 case 'I':
504 // Write 'i' if an integer constant, otherwise nothing. Used to print
505 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000506 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 O << "i";
508 return false;
509 }
510 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000511
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 printOperand(MI, OpNo);
513 return false;
514}
515
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000516// At the moment, all inline asm memory operands are a single register.
517// In any case, the output of this routine should always be just one
518// assembler operand.
519
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000521 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 const char *ExtraCode) {
523 if (ExtraCode && ExtraCode[0])
524 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000525 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000526 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000527 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000528 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 return false;
530}
531
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000532void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 const char *Modifier) {
534 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
535 unsigned Code = MI->getOperand(OpNo).getImm();
536 if (!strcmp(Modifier, "cc")) {
537 switch ((PPC::Predicate)Code) {
538 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
539 case PPC::PRED_LT: O << "lt"; return;
540 case PPC::PRED_LE: O << "le"; return;
541 case PPC::PRED_EQ: O << "eq"; return;
542 case PPC::PRED_GE: O << "ge"; return;
543 case PPC::PRED_GT: O << "gt"; return;
544 case PPC::PRED_NE: O << "ne"; return;
545 case PPC::PRED_UN: O << "un"; return;
546 case PPC::PRED_NU: O << "nu"; return;
547 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000548
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 } else {
550 assert(!strcmp(Modifier, "reg") &&
551 "Need to specify 'cc' or 'reg' as predicate op modifier!");
552 // Don't print the register for 'always'.
553 if (Code == PPC::PRED_ALWAYS) return;
554 printOperand(MI, OpNo+1);
555 }
556}
557
558
559/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
560/// the current output stream.
561///
562void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
563 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000564
Devang Patel5450fc12009-10-06 02:19:11 +0000565 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566
567 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000568 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000570 unsigned char SH = MI->getOperand(2).getImm();
571 unsigned char MB = MI->getOperand(3).getImm();
572 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000574 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 }
576 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000577 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 SH = 32-SH;
579 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000580 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 printOperand(MI, 0);
582 O << ", ";
583 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000584 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 }
586 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
587 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000588 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000589 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 printOperand(MI, 0);
591 O << ", ";
592 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 }
594 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000595 unsigned char SH = MI->getOperand(2).getImm();
596 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
598 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000599 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000600 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 printOperand(MI, 0);
602 O << ", ";
603 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000604 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 }
606 }
607
Dale Johannesen760acc02010-01-06 02:20:18 +0000608 if (!useSubstituteMnemonic)
609 printInstruction(MI);
610
David Greeneca9b04b2009-11-13 21:34:57 +0000611 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000612 EmitComments(*MI);
613 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000614
615 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616}
617
618/// runOnMachineFunction - This uses the printMachineInstruction()
619/// method to print assembly for each instruction.
620///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000621bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000622 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623
624 SetupMachineFunction(MF);
625 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000626
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 // Print out constants referenced by the function
628 EmitConstantPool(MF.getConstantPool());
629
630 // Print out labels for the function.
631 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000632 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000633
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000635 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000636 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 case Function::InternalLinkage: // Symbols default to internal.
638 break;
639 case Function::ExternalLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000640 O << "\t.global\t";
641 CurrentFnSym->print(O, MAI);
642 O << '\n' << "\t.type\t";
643 CurrentFnSym->print(O, MAI);
644 O << ", @function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000646 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000647 case Function::WeakAnyLinkage:
648 case Function::WeakODRLinkage:
649 case Function::LinkOnceAnyLinkage:
650 case Function::LinkOnceODRLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000651 O << "\t.global\t";
652 CurrentFnSym->print(O, MAI);
653 O << '\n';
654 O << "\t.weak\t";
655 CurrentFnSym->print(O, MAI);
656 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 break;
658 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000659
Chris Lattner651adf32010-01-16 00:21:18 +0000660 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000661
Bill Wendling25a8ae32009-06-30 22:38:32 +0000662 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000663
664 if (Subtarget.isPPC64()) {
665 // Emit an official procedure descriptor.
Chris Lattner651adf32010-01-16 00:21:18 +0000666 // FIXME 64-bit SVR4: Use MCSection here!
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000667 O << "\t.section\t\".opd\",\"aw\"\n";
668 O << "\t.align 3\n";
Chris Lattner651adf32010-01-16 00:21:18 +0000669 CurrentFnSym->print(O, MAI);
670 O << ":\n";
671 O << "\t.quad .L.";
672 CurrentFnSym->print(O, MAI);
673 O << ",.TOC.@tocbase\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000674 O << "\t.previous\n";
Chris Lattner651adf32010-01-16 00:21:18 +0000675 O << ".L.";
676 CurrentFnSym->print(O, MAI);
677 O << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000678 } else {
Chris Lattner651adf32010-01-16 00:21:18 +0000679 CurrentFnSym->print(O, MAI);
680 O << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000681 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682
683 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000684 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685
686 // Print out code for the function.
687 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
688 I != E; ++I) {
689 // Print a label for the basic block.
690 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000691 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 }
693 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
694 II != E; ++II) {
695 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 printMachineInstruction(II);
697 }
698 }
699
Chris Lattner651adf32010-01-16 00:21:18 +0000700 O << "\t.size\t";
701 CurrentFnSym->print(O, MAI);
702 O << ",.-";
703 CurrentFnSym->print(O, MAI);
704 O << '\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 Lattner22b24952010-01-16 01:12:01 +0000728 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729
Chris Lattner22b24952010-01-16 01:12:01 +0000730 printVisibility(GVarSym, 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()) {
Chris Lattner22b24952010-01-16 01:12:01 +0000747 O << "\t.global ";
748 GVarSym->print(O, MAI);
749 O << '\n';
750 O << "\t.type ";
751 GVarSym->print(O, MAI);
752 O << ", @object\n";
753 GVarSym->print(O, MAI);
754 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000755 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000756 } else if (GVar->hasLocalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +0000757 O << MAI->getLCOMMDirective();
758 GVarSym->print(O, MAI);
759 O << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 } else {
Chris Lattner22b24952010-01-16 01:12:01 +0000761 O << ".comm ";
762 GVarSym->print(O, MAI);
763 O << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 }
Evan Cheng11db8142009-03-24 00:17:40 +0000765 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000766 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000767 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000768 O << "'";
769 }
770 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000771 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 }
773
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000774 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000775 case GlobalValue::LinkOnceAnyLinkage:
776 case GlobalValue::LinkOnceODRLinkage:
777 case GlobalValue::WeakAnyLinkage:
778 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000779 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000780 case GlobalValue::LinkerPrivateLinkage:
Chris Lattner22b24952010-01-16 01:12:01 +0000781 O << "\t.global ";
782 GVarSym->print(O, MAI);
783 O << "\n\t.type ";
784 GVarSym->print(O, MAI);
785 O << ", @object\n\t.weak ";
786 GVarSym->print(O, MAI);
787 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000788 break;
789 case GlobalValue::AppendingLinkage:
790 // FIXME: appending linkage variables should go into a section of
791 // their name or something. For now, just emit them as external.
792 case GlobalValue::ExternalLinkage:
793 // If external or appending, declare as a global symbol
Chris Lattner22b24952010-01-16 01:12:01 +0000794 O << "\t.global ";
795 GVarSym->print(O, MAI);
796 O << "\n\t.type ";
797 GVarSym->print(O, MAI);
798 O << ", @object\n";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000799 // FALL THROUGH
800 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000801 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000802 break;
803 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000804 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000805 }
806
807 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +0000808 GVarSym->print(O, MAI);
809 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +0000810 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000811 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000812 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000813 O << "'";
814 }
815 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000816
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000817 EmitGlobalConstant(C);
818 O << '\n';
819}
820
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000821bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
822 const TargetData *TD = TM.getTargetData();
823
824 bool isPPC64 = TD->getPointerSizeInBits() == 64;
825
826 if (isPPC64 && !TOC.empty()) {
827 // FIXME 64-bit SVR4: Use MCSection here?
828 O << "\t.section\t\".toc\",\"aw\"\n";
829
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000830 // FIXME: This is nondeterminstic!
831 for (StringMap<const MCSymbol*>::iterator I = TOC.begin(), E = TOC.end();
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000832 I != E; ++I) {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000833 I->second->print(O, MAI);
834 O << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000835 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
836 }
837 }
838
839 return AsmPrinter::doFinalization(M);
840}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842/// runOnMachineFunction - This uses the printMachineInstruction()
843/// method to print assembly for each instruction.
844///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000845bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000846 this->MF = &MF;
847
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 SetupMachineFunction(MF);
849 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000850
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 // Print out constants referenced by the function
852 EmitConstantPool(MF.getConstantPool());
853
854 // Print out labels for the function.
855 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000856 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000857
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000859 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000860 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 case Function::InternalLinkage: // Symbols default to internal.
862 break;
863 case Function::ExternalLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000864 O << "\t.globl\t";
865 CurrentFnSym->print(O, MAI);
866 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000868 case Function::WeakAnyLinkage:
869 case Function::WeakODRLinkage:
870 case Function::LinkOnceAnyLinkage:
871 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000872 case Function::LinkerPrivateLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000873 O << "\t.globl\t";
874 CurrentFnSym->print(O, MAI);
875 O << '\n';
876 O << "\t.weak_definition\t";
877 CurrentFnSym->print(O, MAI);
878 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 break;
880 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000881
Chris Lattner651adf32010-01-16 00:21:18 +0000882 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000883
Bill Wendling25a8ae32009-06-30 22:38:32 +0000884 EmitAlignment(MF.getAlignment(), F);
Chris Lattner651adf32010-01-16 00:21:18 +0000885 CurrentFnSym->print(O, MAI);
886 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000887
888 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000889 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890
Bill Wendling36ccaea2008-01-26 06:51:24 +0000891 // If the function is empty, then we need to emit *something*. Otherwise, the
892 // function's label might be associated with something that it wasn't meant to
893 // be associated with. We emit a noop in this situation.
894 MachineFunction::iterator I = MF.begin();
895
Bill Wendlingb5880a72008-01-26 09:03:52 +0000896 if (++I == MF.end() && MF.front().empty())
897 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000898
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 // Print out code for the function.
900 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
901 I != E; ++I) {
902 // Print a label for the basic block.
903 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000904 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000906 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
907 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 printMachineInstruction(II);
910 }
911 }
912
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000914 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000915
Bill Wendling5019fe02009-11-09 21:45:26 +0000916 // Print out jump tables referenced by the function.
917 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
918
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 // We didn't modify anything.
920 return false;
921}
922
923
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000924void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000925 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000926 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927 "ppc",
928 "ppc601",
929 "ppc602",
930 "ppc603",
931 "ppc7400",
932 "ppc750",
933 "ppc970",
934 "ppc64"
935 };
936
937 unsigned Directive = Subtarget.getDarwinDirective();
938 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
939 Directive = PPC::DIR_970;
940 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
941 Directive = PPC::DIR_7400;
942 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
943 Directive = PPC::DIR_64;
944 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000945 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000946
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000947 // Prime text sections so they are adjacent. This reduces the likelihood a
948 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000949 TargetLoweringObjectFileMachO &TLOFMacho =
950 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000951 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000953 OutStreamer.SwitchSection(
954 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
955 MCSectionMachO::S_SYMBOL_STUBS |
956 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
957 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000959 OutStreamer.SwitchSection(
960 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
961 MCSectionMachO::S_SYMBOL_STUBS |
962 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
963 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964 }
Chris Lattner73266f92009-08-19 05:49:37 +0000965 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966}
967
Chris Lattnerae982212009-07-21 18:38:57 +0000968void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000969 const TargetData *TD = TM.getTargetData();
970
971 if (!GVar->hasInitializer())
972 return; // External global require no code
973
974 // Check to see if this is a special global used by LLVM, if so, emit it.
975 if (EmitSpecialLLVMGlobal(GVar)) {
976 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000977 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000978 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000979 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000980 O << ".reference .destructors_used\n";
981 }
982 return;
983 }
984
Chris Lattner22b24952010-01-16 01:12:01 +0000985 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
986 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000987
988 Constant *C = GVar->getInitializer();
989 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000990 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000991 unsigned Align = TD->getPreferredAlignmentLog(GVar);
992
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000993 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000994 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000995 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000996
Chris Lattnerdb727932009-08-04 05:35:56 +0000997 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000998 if (C->isNullValue() && /* FIXME: Verify correct */
999 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001000 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +00001001 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +00001002 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +00001003 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001004 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
1005
1006 if (GVar->hasExternalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001007 O << "\t.globl ";
1008 GVarSym->print(O, MAI);
1009 O << '\n';
1010 O << "\t.zerofill __DATA, __common, ";
1011 GVarSym->print(O, MAI);
1012 O << ", " << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001013 } else if (GVar->hasLocalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001014 O << MAI->getLCOMMDirective();
1015 GVarSym->print(O, MAI);
1016 O << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001017 } else if (!GVar->hasCommonLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001018 O << "\t.globl ";
1019 GVarSym->print(O, MAI);
1020 O << '\n' << MAI->getWeakDefDirective();
1021 GVarSym->print(O, MAI);
1022 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001023 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +00001024 GVarSym->print(O, MAI);
1025 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +00001026 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001027 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001028 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001029 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001030 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001031 EmitGlobalConstant(C);
1032 return;
1033 } else {
Chris Lattner22b24952010-01-16 01:12:01 +00001034 O << ".comm ";
1035 GVarSym->print(O, MAI);
1036 O << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001037 // Darwin 9 and above support aligned common data.
1038 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001039 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001040 }
Evan Cheng11db8142009-03-24 00:17:40 +00001041 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001042 O << "\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 return;
1048 }
1049
1050 switch (GVar->getLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001051 case GlobalValue::LinkOnceAnyLinkage:
1052 case GlobalValue::LinkOnceODRLinkage:
1053 case GlobalValue::WeakAnyLinkage:
1054 case GlobalValue::WeakODRLinkage:
1055 case GlobalValue::CommonLinkage:
1056 case GlobalValue::LinkerPrivateLinkage:
1057 O << "\t.globl ";
1058 GVarSym->print(O, MAI);
1059 O << "\n\t.weak_definition ";
1060 GVarSym->print(O, MAI);
1061 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001062 break;
Chris Lattner22b24952010-01-16 01:12:01 +00001063 case GlobalValue::AppendingLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001064 // FIXME: appending linkage variables should go into a section of
1065 // their name or something. For now, just emit them as external.
Chris Lattner22b24952010-01-16 01:12:01 +00001066 case GlobalValue::ExternalLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001067 // If external or appending, declare as a global symbol
Chris Lattner22b24952010-01-16 01:12:01 +00001068 O << "\t.globl ";
1069 GVarSym->print(O, MAI);
1070 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001071 // FALL THROUGH
Chris Lattner22b24952010-01-16 01:12:01 +00001072 case GlobalValue::InternalLinkage:
1073 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001074 break;
Chris Lattner22b24952010-01-16 01:12:01 +00001075 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001076 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001077 }
1078
1079 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +00001080 GVarSym->print(O, MAI);
1081 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +00001082 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001083 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001084 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001085 O << "'";
1086 }
1087 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001088
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001089 EmitGlobalConstant(C);
1090 O << '\n';
1091}
1092
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001093bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 const TargetData *TD = TM.getTargetData();
1095
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1097
Chris Lattnerf4815552009-08-03 22:52:21 +00001098 // Darwin/PPC always uses mach-o.
1099 TargetLoweringObjectFileMachO &TLOFMacho =
1100 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1101
Chris Lattner72a676a2009-08-10 01:39:42 +00001102
1103 const MCSection *LSPSection = 0;
1104 if (!FnStubs.empty()) // .lazy_symbol_pointer
1105 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1106
1107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001109 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001110 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001111 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1112 MCSectionMachO::S_SYMBOL_STUBS |
1113 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1114 32, SectionKind::getText());
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001115 // FIXME: This is emitting in nondeterminstic order!
1116 for (DenseMap<const MCSymbol*, FnStubInfo>::iterator I =
1117 FnStubs.begin(), E = FnStubs.end(); 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 Lattner1774fd32010-01-13 19:13:16 +00001121 Info.Stub->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001122 O << ":\n";
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001123 O << "\t.indirect_symbol ";
1124 I->first->print(O, MAI);
1125 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 O << "\tmflr r0\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001127 O << "\tbcl 20,31,";
Chris Lattner1774fd32010-01-13 19:13:16 +00001128 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001129 O << '\n';
Chris Lattner1774fd32010-01-13 19:13:16 +00001130 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001131 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 O << "\tmflr r11\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001133 O << "\taddis r11,r11,ha16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001134 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001135 O << '-';
Chris Lattner1774fd32010-01-13 19:13:16 +00001136 Info.AnonSymbol->print(O, MAI);
Evan Chenga65854f2008-12-05 01:06:39 +00001137 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001139 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001140 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001141 O << '-';
Chris Lattner1774fd32010-01-13 19:13:16 +00001142 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001143 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144 O << "\tmtctr r12\n";
1145 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001146
Chris Lattner73266f92009-08-19 05:49:37 +00001147 OutStreamer.SwitchSection(LSPSection);
Chris Lattner1774fd32010-01-13 19:13:16 +00001148 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001149 O << ":\n";
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001150 O << "\t.indirect_symbol ";
1151 I->first->print(O, MAI);
1152 O << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001153 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001154 }
Chris Lattner189198f2009-07-15 00:55:58 +00001155 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001156 const MCSection *StubSection =
1157 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1158 MCSectionMachO::S_SYMBOL_STUBS |
1159 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1160 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001161
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001162 // FIXME: This is emitting in nondeterminstic order!
1163 for (DenseMap<const MCSymbol*, FnStubInfo>::iterator I = FnStubs.begin(),
1164 E = FnStubs.end(); I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001165 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001166 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001167 const FnStubInfo &Info = I->second;
Chris Lattner1774fd32010-01-13 19:13:16 +00001168 Info.Stub->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001169 O << ":\n";
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001170 O << "\t.indirect_symbol ";
1171 I->first->print(O, MAI);
1172 O << '\n';
Chris Lattner9bc87482010-01-13 19:00:57 +00001173 O << "\tlis r11,ha16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001174 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001175 O << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001176 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001177 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001178 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001179 O << "\tmtctr r12\n";
1180 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001181 OutStreamer.SwitchSection(LSPSection);
Chris Lattner1774fd32010-01-13 19:13:16 +00001182 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001183 O << ":\n";
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001184 O << "\t.indirect_symbol ";
1185 I->first->print(O, MAI);
1186 O << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001187 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001188 }
1189 }
1190
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001191 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001192
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001193 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001194 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001195 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001196 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001197 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001198 E = Personalities.end(); I != E; ++I) {
1199 if (*I)
1200 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001201 GetPrivateGlobalValueSymbolStub(*I, "$non_lazy_ptr");
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001202 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001203 }
1204
Chris Lattnerf4815552009-08-03 22:52:21 +00001205 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001206 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001207 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001208 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001209 EmitAlignment(isPPC64 ? 3 : 2);
1210
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001211 // FIXME: This is nondeterminstic.
1212 for (StringMap<const MCSymbol *>::iterator I = GVStubs.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001213 E = GVStubs.end(); I != E; ++I) {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001214 I->second->print(O, MAI);
1215 O << ":\n";
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001216 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1217 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001218 }
1219 }
1220
Evan Chenga65854f2008-12-05 01:06:39 +00001221 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001222 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001223 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001224 // FIXME: This is nondeterminstic.
1225 for (StringMap<const MCSymbol *>::iterator I = HiddenGVStubs.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001226 E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001227 I->second->print(O, MAI);
1228 O << ":\n";
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001229 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001230 }
1231 }
1232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 // Funny Darwin hack: This flag tells the linker that no global symbols
1234 // contain code that falls through to other global symbols (e.g. the obvious
1235 // implementation of multiple entry points). If this doesn't occur, the
1236 // linker can safely perform dead code stripping. Since LLVM never generates
1237 // code that does this, it is always safe to set.
Chris Lattnerfe284f72009-10-19 18:03:08 +00001238 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239
Dan Gohman4a558a32007-07-25 19:33:14 +00001240 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241}
1242
1243
1244
1245/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1246/// for a MachineFunction to the given output stream, in a format that the
1247/// Darwin assembler can deal with.
1248///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001249static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1250 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001251 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001252 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1254
Chris Lattnerae982212009-07-21 18:38:57 +00001255 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001256 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1257 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001258}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001259
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001260// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001261extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001262 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001263 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1264}