blob: d71397d4606f1ff5544270c94f8f3fb3a05d2401 [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 Lattner7f1ac7f2009-08-10 18:15:01 +000035#include "llvm/MC/MCSectionMachO.h"
Chris Lattner73266f92009-08-19 05:49:37 +000036#include "llvm/MC/MCStreamer.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000037#include "llvm/MC/MCSymbol.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000038#include "llvm/Target/TargetLoweringObjectFile.h"
39#include "llvm/Target/TargetRegisterInfo.h"
40#include "llvm/Target/TargetInstrInfo.h"
41#include "llvm/Target/TargetOptions.h"
42#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043#include "llvm/Support/Mangler.h"
44#include "llvm/Support/MathExtras.h"
45#include "llvm/Support/CommandLine.h"
46#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000047#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000048#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049#include "llvm/ADT/Statistic.h"
50#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000051#include "llvm/ADT/StringSet.h"
Chris Lattnera38b2872010-01-13 06:38:18 +000052#include "llvm/ADT/SmallString.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053using namespace llvm;
54
55STATISTIC(EmittedInsts, "Number of machine instrs printed");
56
57namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000058 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +000059 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000060 struct FnStubInfo {
61 std::string Stub, LazyPtr, AnonSymbol;
62
63 FnStubInfo() {}
64
65 void Init(const GlobalValue *GV, Mangler *Mang) {
66 // Already initialized.
67 if (!Stub.empty()) return;
68 Stub = Mang->getMangledName(GV, "$stub", true);
69 LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true);
70 AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true);
71 }
72
Chris Lattnera38b2872010-01-13 06:38:18 +000073 void Init(StringRef GVName, Mangler *Mang) {
Chris Lattnerf80743e2010-01-13 07:56:59 +000074 assert(!GVName.empty());
75 if (!Stub.empty()) return; // Already initialized.
76 // Get the names for the external symbol name.
Chris Lattnera38b2872010-01-13 06:38:18 +000077 SmallString<128> TmpStr;
Chris Lattnerf80743e2010-01-13 07:56:59 +000078 Mang->getNameWithPrefix(TmpStr, GVName + "$stub", Mangler::Private);
Chris Lattnera38b2872010-01-13 06:38:18 +000079 Stub = TmpStr.str();
80 TmpStr.clear();
81
Chris Lattnerf80743e2010-01-13 07:56:59 +000082 Mang->getNameWithPrefix(TmpStr, GVName + "$lazy_ptr", Mangler::Private);
Chris Lattnera38b2872010-01-13 06:38:18 +000083 LazyPtr = TmpStr.str();
84 TmpStr.clear();
85
Chris Lattnerf80743e2010-01-13 07:56:59 +000086 Mang->getNameWithPrefix(TmpStr, GVName + "$stub$tmp", Mangler::Private);
Chris Lattnera38b2872010-01-13 06:38:18 +000087 AnonSymbol = TmpStr.str();
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000088 }
89 };
90
91 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000092 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000094 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +000095 public:
David Greene302008d2009-07-14 20:18:05 +000096 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +000097 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000098 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +000099 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 virtual const char *getPassName() const {
102 return "PowerPC Assembly Printer";
103 }
104
105 PPCTargetMachine &getTM() {
106 return static_cast<PPCTargetMachine&>(TM);
107 }
108
109 unsigned enumRegToMachineReg(unsigned enumReg) {
110 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000111 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 case PPC::CR0: return 0;
113 case PPC::CR1: return 1;
114 case PPC::CR2: return 2;
115 case PPC::CR3: return 3;
116 case PPC::CR4: return 4;
117 case PPC::CR5: return 5;
118 case PPC::CR6: return 6;
119 case PPC::CR7: return 7;
120 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000121 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 }
123
124 /// printInstruction - This method is automatically generated by tablegen
125 /// from the instruction set description. This method returns true if the
126 /// machine instruction was sufficiently described to print it, otherwise it
127 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000128 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +0000129 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +0000130
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 void printMachineInstruction(const MachineInstr *MI);
133 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 /// stripRegisterPrefix - This method strips the character prefix from a
136 /// register name so that only the number is left. Used by for linux asm.
137 const char *stripRegisterPrefix(const char *RegName) {
138 switch (RegName[0]) {
139 case 'r':
140 case 'f':
141 case 'v': return RegName + 1;
142 case 'c': if (RegName[1] == 'r') return RegName + 2;
143 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 return RegName;
146 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000147
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 /// printRegister - Print register according to target requirements.
149 ///
150 void printRegister(const MachineOperand &MO, bool R0AsZero) {
151 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000152 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 // If we should use 0 for R0.
155 if (R0AsZero && RegNo == PPC::R0) {
156 O << "0";
157 return;
158 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000159
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000160 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 // Linux assembler (Others?) does not take register mnemonics.
162 // FIXME - What about special registers used in mfspr/mtspr?
163 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
164 O << RegName;
165 }
166
167 void printOperand(const MachineInstr *MI, unsigned OpNo) {
168 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000169 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000171 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000172 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 } else {
174 printOp(MO);
175 }
176 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
179 unsigned AsmVariant, const char *ExtraCode);
180 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
181 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000182
183
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000185 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 value = (value << (32-5)) >> (32-5);
187 O << (int)value;
188 }
189 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000190 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 assert(value <= 31 && "Invalid u5imm argument!");
192 O << (unsigned int)value;
193 }
194 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000195 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 assert(value <= 63 && "Invalid u6imm argument!");
197 O << (unsigned int)value;
198 }
199 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000200 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 }
202 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000203 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 }
205 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000206 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000207 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 } else {
209 O << "lo16(";
210 printOp(MI->getOperand(OpNo));
211 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000212 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 else
214 O << ')';
215 }
216 }
217 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
218 // Branches can take an immediate operand. This is used by the branch
219 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000220 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000221 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 } else {
223 printOp(MI->getOperand(OpNo));
224 }
225 }
226 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
227 const MachineOperand &MO = MI->getOperand(OpNo);
228 if (TM.getRelocationModel() != Reloc::Static) {
229 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
230 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000231 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000233 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
234 FnInfo.Init(GV, Mang);
235 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 return;
237 }
238 }
239 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000240 SmallString<128> MangledName;
Chris Lattnerf80743e2010-01-13 07:56:59 +0000241 Mang->getNameWithPrefix(MangledName, MO.getSymbolName());
Chris Lattnera38b2872010-01-13 06:38:18 +0000242 FnStubInfo &FnInfo = FnStubs[MangledName.str()];
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000243 FnInfo.Init(MO.getSymbolName(), Mang);
244 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 return;
246 }
247 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 printOp(MI->getOperand(OpNo));
250 }
251 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000252 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 }
254 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000255 O << "\"L" << getFunctionNumber() << "$pb\"\n";
256 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 }
258 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000259 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 printS16ImmOperand(MI, OpNo);
261 } else {
262 if (Subtarget.isDarwin()) O << "ha16(";
263 printOp(MI->getOperand(OpNo));
264 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000265 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (Subtarget.isDarwin())
267 O << ')';
268 else
269 O << "@ha";
270 }
271 }
272 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000273 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 printS16ImmOperand(MI, OpNo);
275 } else {
276 if (Subtarget.isDarwin()) O << "lo16(";
277 printOp(MI->getOperand(OpNo));
278 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000279 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 if (Subtarget.isDarwin())
281 O << ')';
282 else
283 O << "@l";
284 }
285 }
286 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
287 unsigned CCReg = MI->getOperand(OpNo).getReg();
288 unsigned RegNo = enumRegToMachineReg(CCReg);
289 O << (0x80 >> RegNo);
290 }
291 // The new addressing mode printers.
292 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
293 printSymbolLo(MI, OpNo);
294 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000295 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 MI->getOperand(OpNo+1).getReg() == PPC::R0)
297 O << "0";
298 else
299 printOperand(MI, OpNo+1);
300 O << ')';
301 }
302 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000303 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000305 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 printSymbolLo(MI, OpNo);
307 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000308 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 MI->getOperand(OpNo+1).getReg() == PPC::R0)
310 O << "0";
311 else
312 printOperand(MI, OpNo+1);
313 O << ')';
314 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000315
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
317 // When used as the base register, r0 reads constant zero rather than
318 // the value contained in the register. For this reason, the darwin
319 // assembler requires that we print r0 as 0 (no r) when used as the base.
320 const MachineOperand &MO = MI->getOperand(OpNo);
321 printRegister(MO, true);
322 O << ", ";
323 printOperand(MI, OpNo+1);
324 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000325
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000326 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
327 const MachineOperand &MO = MI->getOperand(OpNo);
328
329 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
330
331 GlobalValue *GV = MO.getGlobal();
332
333 std::string Name = Mang->getMangledName(GV);
334
335 // Map symbol -> label of TOC entry.
336 if (TOC.count(Name) == 0) {
337 std::string Label;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000338 Label += MAI->getPrivateGlobalPrefix();
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000339 Label += "C";
340 Label += utostr(LabelID++);
341
342 TOC[Name] = Label;
343 }
344
345 O << TOC[Name] << "@toc";
346 }
347
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000348 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000350
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 };
353
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000354 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000355 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000356 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000357 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000358 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000359 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360
361 virtual const char *getPassName() const {
362 return "Linux PPC Assembly Printer";
363 }
364
365 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000366 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000367
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 void getAnalysisUsage(AnalysisUsage &AU) const {
369 AU.setPreservesAll();
370 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000371 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 PPCAsmPrinter::getAnalysisUsage(AU);
373 }
374
Chris Lattnerae982212009-07-21 18:38:57 +0000375 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 };
377
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000378 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
379 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000380 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000381 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000382 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000383 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000384 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000385 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386
387 virtual const char *getPassName() const {
388 return "Darwin PPC Assembly Printer";
389 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000390
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000393 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000394
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 void getAnalysisUsage(AnalysisUsage &AU) const {
396 AU.setPreservesAll();
397 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000398 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 PPCAsmPrinter::getAnalysisUsage(AU);
400 }
401
Chris Lattnerae982212009-07-21 18:38:57 +0000402 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 };
404} // end of anonymous namespace
405
406// Include the auto-generated portion of the assembly writer
407#include "PPCGenAsmWriter.inc"
408
409void PPCAsmPrinter::printOp(const MachineOperand &MO) {
410 switch (MO.getType()) {
411 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000412 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413
414 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000415 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 return;
417 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000418 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000419 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 // FIXME: PIC relocation model
421 return;
422 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000423 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000424 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000426 case MachineOperand::MO_BlockAddress:
427 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
428 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000429 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 // Computing the address of an external symbol, not calling it.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000431 std::string Name(MAI->getGlobalPrefix());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000432 Name += MO.getSymbolName();
433
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000435 GVStubs[Name] = Name+"$non_lazy_ptr";
436 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000437 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000438 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000440 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 case MachineOperand::MO_GlobalAddress: {
442 // Computing the address of a global symbol, not calling it.
443 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000444 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445
446 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000447 if (TM.getRelocationModel() != Reloc::Static &&
448 (GV->isDeclaration() || GV->isWeakForLinker())) {
449 if (!GV->hasHiddenVisibility()) {
450 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
451 GVStubs[Mang->getMangledName(GV)] = Name;
452 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
453 GV->hasAvailableExternallyLinkage()) {
454 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
455 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
456 } else {
457 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000459 } else {
460 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 }
462 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000463
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000464 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 return;
466 }
467
468 default:
469 O << "<unknown operand type: " << MO.getType() << ">";
470 return;
471 }
472}
473
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474/// PrintAsmOperand - Print out an operand for an inline asm expression.
475///
476bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000477 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 const char *ExtraCode) {
479 // Does this asm operand have a single letter operand modifier?
480 if (ExtraCode && ExtraCode[0]) {
481 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000482
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 switch (ExtraCode[0]) {
484 default: return true; // Unknown modifier.
485 case 'c': // Don't print "$" before a global var name or constant.
486 // PPC never has a prefix.
487 printOperand(MI, OpNo);
488 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000489 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000491 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000493 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 return true;
495 ++OpNo; // Return the high-part.
496 break;
497 case 'I':
498 // Write 'i' if an integer constant, otherwise nothing. Used to print
499 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000500 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 O << "i";
502 return false;
503 }
504 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000505
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 printOperand(MI, OpNo);
507 return false;
508}
509
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000510// At the moment, all inline asm memory operands are a single register.
511// In any case, the output of this routine should always be just one
512// assembler operand.
513
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000515 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 const char *ExtraCode) {
517 if (ExtraCode && ExtraCode[0])
518 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000519 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000520 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000521 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000522 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 return false;
524}
525
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000526void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 const char *Modifier) {
528 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
529 unsigned Code = MI->getOperand(OpNo).getImm();
530 if (!strcmp(Modifier, "cc")) {
531 switch ((PPC::Predicate)Code) {
532 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
533 case PPC::PRED_LT: O << "lt"; return;
534 case PPC::PRED_LE: O << "le"; return;
535 case PPC::PRED_EQ: O << "eq"; return;
536 case PPC::PRED_GE: O << "ge"; return;
537 case PPC::PRED_GT: O << "gt"; return;
538 case PPC::PRED_NE: O << "ne"; return;
539 case PPC::PRED_UN: O << "un"; return;
540 case PPC::PRED_NU: O << "nu"; return;
541 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000542
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 } else {
544 assert(!strcmp(Modifier, "reg") &&
545 "Need to specify 'cc' or 'reg' as predicate op modifier!");
546 // Don't print the register for 'always'.
547 if (Code == PPC::PRED_ALWAYS) return;
548 printOperand(MI, OpNo+1);
549 }
550}
551
552
553/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
554/// the current output stream.
555///
556void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
557 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000558
Devang Patel5450fc12009-10-06 02:19:11 +0000559 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560
561 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000562 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000564 unsigned char SH = MI->getOperand(2).getImm();
565 unsigned char MB = MI->getOperand(3).getImm();
566 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000568 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 }
570 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000571 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 SH = 32-SH;
573 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000574 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 printOperand(MI, 0);
576 O << ", ";
577 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000578 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 }
580 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
581 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000582 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000583 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 printOperand(MI, 0);
585 O << ", ";
586 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 }
588 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000589 unsigned char SH = MI->getOperand(2).getImm();
590 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
592 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000593 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000594 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 printOperand(MI, 0);
596 O << ", ";
597 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000598 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 }
600 }
601
Dale Johannesen760acc02010-01-06 02:20:18 +0000602 if (!useSubstituteMnemonic)
603 printInstruction(MI);
604
David Greeneca9b04b2009-11-13 21:34:57 +0000605 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000606 EmitComments(*MI);
607 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000608
609 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610}
611
612/// runOnMachineFunction - This uses the printMachineInstruction()
613/// method to print assembly for each instruction.
614///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000615bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000616 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617
618 SetupMachineFunction(MF);
619 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000620
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 // Print out constants referenced by the function
622 EmitConstantPool(MF.getConstantPool());
623
624 // Print out labels for the function.
625 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000626 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000627
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000629 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000630 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 case Function::InternalLinkage: // Symbols default to internal.
632 break;
633 case Function::ExternalLinkage:
634 O << "\t.global\t" << CurrentFnName << '\n'
635 << "\t.type\t" << CurrentFnName << ", @function\n";
636 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000637 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000638 case Function::WeakAnyLinkage:
639 case Function::WeakODRLinkage:
640 case Function::LinkOnceAnyLinkage:
641 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 O << "\t.global\t" << CurrentFnName << '\n';
643 O << "\t.weak\t" << CurrentFnName << '\n';
644 break;
645 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000646
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000647 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000648
Bill Wendling25a8ae32009-06-30 22:38:32 +0000649 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000650
651 if (Subtarget.isPPC64()) {
652 // Emit an official procedure descriptor.
653 // FIXME 64-bit SVR4: Use MCSection here?
654 O << "\t.section\t\".opd\",\"aw\"\n";
655 O << "\t.align 3\n";
656 O << CurrentFnName << ":\n";
657 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
658 O << "\t.previous\n";
659 O << ".L." << CurrentFnName << ":\n";
660 } else {
661 O << CurrentFnName << ":\n";
662 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663
664 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000665 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666
667 // Print out code for the function.
668 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
669 I != E; ++I) {
670 // Print a label for the basic block.
671 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000672 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 }
674 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
675 II != E; ++II) {
676 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 printMachineInstruction(II);
678 }
679 }
680
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000681 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682
Chris Lattner73266f92009-08-19 05:49:37 +0000683 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000684
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000686 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000687
Bill Wendling6fb58e12009-11-09 21:20:14 +0000688 // Print out jump tables referenced by the function.
689 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
690
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 // We didn't modify anything.
692 return false;
693}
694
Chris Lattnerae982212009-07-21 18:38:57 +0000695void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 const TargetData *TD = TM.getTargetData();
697
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000698 if (!GVar->hasInitializer())
699 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000700
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000701 // Check to see if this is a special global used by LLVM, if so, emit it.
702 if (EmitSpecialLLVMGlobal(GVar))
703 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000705 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000707 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000708
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000709 Constant *C = GVar->getInitializer();
710 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000711 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000712 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713
Chris Lattner73266f92009-08-19 05:49:37 +0000714 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
715 TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000716
717 if (C->isNullValue() && /* FIXME: Verify correct */
718 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000719 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000720 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000722
723 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 O << "\t.global " << name << '\n';
725 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000726 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000727 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000728 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000729 O << MAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000731 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732 }
Evan Cheng11db8142009-03-24 00:17:40 +0000733 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000734 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000735 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000736 O << "'";
737 }
738 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000739 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 }
741
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000742 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000743 case GlobalValue::LinkOnceAnyLinkage:
744 case GlobalValue::LinkOnceODRLinkage:
745 case GlobalValue::WeakAnyLinkage:
746 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000747 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000748 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000749 O << "\t.global " << name << '\n'
750 << "\t.type " << name << ", @object\n"
751 << "\t.weak " << name << '\n';
752 break;
753 case GlobalValue::AppendingLinkage:
754 // FIXME: appending linkage variables should go into a section of
755 // their name or something. For now, just emit them as external.
756 case GlobalValue::ExternalLinkage:
757 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000758 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000759 << "\t.type " << name << ", @object\n";
760 // FALL THROUGH
761 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000762 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000763 break;
764 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000765 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000766 }
767
768 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000769 O << name << ":";
770 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000771 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000772 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000773 O << "'";
774 }
775 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000776
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000777 EmitGlobalConstant(C);
778 O << '\n';
779}
780
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000781bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
782 const TargetData *TD = TM.getTargetData();
783
784 bool isPPC64 = TD->getPointerSizeInBits() == 64;
785
786 if (isPPC64 && !TOC.empty()) {
787 // FIXME 64-bit SVR4: Use MCSection here?
788 O << "\t.section\t\".toc\",\"aw\"\n";
789
790 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
791 I != E; ++I) {
792 O << I->second << ":\n";
793 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
794 }
795 }
796
797 return AsmPrinter::doFinalization(M);
798}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800/// runOnMachineFunction - This uses the printMachineInstruction()
801/// method to print assembly for each instruction.
802///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000803bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000804 this->MF = &MF;
805
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 SetupMachineFunction(MF);
807 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000808
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 // Print out constants referenced by the function
810 EmitConstantPool(MF.getConstantPool());
811
812 // Print out labels for the function.
813 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000814 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000815
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000817 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000818 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 case Function::InternalLinkage: // Symbols default to internal.
820 break;
821 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000822 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000824 case Function::WeakAnyLinkage:
825 case Function::WeakODRLinkage:
826 case Function::LinkOnceAnyLinkage:
827 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000828 case Function::LinkerPrivateLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000829 O << "\t.globl\t" << CurrentFnName << '\n';
830 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 break;
832 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000833
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000834 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000835
Bill Wendling25a8ae32009-06-30 22:38:32 +0000836 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 O << CurrentFnName << ":\n";
838
839 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000840 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841
Bill Wendling36ccaea2008-01-26 06:51:24 +0000842 // If the function is empty, then we need to emit *something*. Otherwise, the
843 // function's label might be associated with something that it wasn't meant to
844 // be associated with. We emit a noop in this situation.
845 MachineFunction::iterator I = MF.begin();
846
Bill Wendlingb5880a72008-01-26 09:03:52 +0000847 if (++I == MF.end() && MF.front().empty())
848 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 // Print out code for the function.
851 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
852 I != E; ++I) {
853 // Print a label for the basic block.
854 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000855 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000857 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
858 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 printMachineInstruction(II);
861 }
862 }
863
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000865 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000866
Bill Wendling5019fe02009-11-09 21:45:26 +0000867 // Print out jump tables referenced by the function.
868 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
869
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 // We didn't modify anything.
871 return false;
872}
873
874
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000875void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000876 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000877 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 "ppc",
879 "ppc601",
880 "ppc602",
881 "ppc603",
882 "ppc7400",
883 "ppc750",
884 "ppc970",
885 "ppc64"
886 };
887
888 unsigned Directive = Subtarget.getDarwinDirective();
889 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
890 Directive = PPC::DIR_970;
891 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
892 Directive = PPC::DIR_7400;
893 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
894 Directive = PPC::DIR_64;
895 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000896 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000897
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 // Prime text sections so they are adjacent. This reduces the likelihood a
899 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000900 TargetLoweringObjectFileMachO &TLOFMacho =
901 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000902 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000904 OutStreamer.SwitchSection(
905 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
906 MCSectionMachO::S_SYMBOL_STUBS |
907 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
908 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000910 OutStreamer.SwitchSection(
911 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
912 MCSectionMachO::S_SYMBOL_STUBS |
913 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
914 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915 }
Chris Lattner73266f92009-08-19 05:49:37 +0000916 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917}
918
Chris Lattnerae982212009-07-21 18:38:57 +0000919void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000920 const TargetData *TD = TM.getTargetData();
921
922 if (!GVar->hasInitializer())
923 return; // External global require no code
924
925 // Check to see if this is a special global used by LLVM, if so, emit it.
926 if (EmitSpecialLLVMGlobal(GVar)) {
927 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000928 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000929 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000930 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000931 O << ".reference .destructors_used\n";
932 }
933 return;
934 }
935
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000936 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000937 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000938
939 Constant *C = GVar->getInitializer();
940 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000941 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000942 unsigned Align = TD->getPreferredAlignmentLog(GVar);
943
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000944 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000945 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000946 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000947
Chris Lattnerdb727932009-08-04 05:35:56 +0000948 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000949 if (C->isNullValue() && /* FIXME: Verify correct */
950 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000951 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000952 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000953 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000954 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000955 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
956
957 if (GVar->hasExternalLinkage()) {
958 O << "\t.globl " << name << '\n';
959 O << "\t.zerofill __DATA, __common, " << name << ", "
960 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000961 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000962 O << MAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000963 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000964 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000965 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000966 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000967 O << name << ":";
968 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000969 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000970 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000971 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000972 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000973 EmitGlobalConstant(C);
974 return;
975 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000976 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000977 // Darwin 9 and above support aligned common data.
978 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000979 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000980 }
Evan Cheng11db8142009-03-24 00:17:40 +0000981 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000982 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000983 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000984 O << "'";
985 }
986 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000987 return;
988 }
989
990 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000991 case GlobalValue::LinkOnceAnyLinkage:
992 case GlobalValue::LinkOnceODRLinkage:
993 case GlobalValue::WeakAnyLinkage:
994 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000995 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000996 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000997 O << "\t.globl " << name << '\n'
998 << "\t.weak_definition " << name << '\n';
999 break;
1000 case GlobalValue::AppendingLinkage:
1001 // FIXME: appending linkage variables should go into a section of
1002 // their name or something. For now, just emit them as external.
1003 case GlobalValue::ExternalLinkage:
1004 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001005 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001006 // FALL THROUGH
1007 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +00001008 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001009 break;
1010 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001011 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001012 }
1013
1014 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001015 O << name << ":";
1016 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001017 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001018 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001019 O << "'";
1020 }
1021 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001022
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001023 EmitGlobalConstant(C);
1024 O << '\n';
1025}
1026
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001027bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 const TargetData *TD = TM.getTargetData();
1029
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1031
Chris Lattnerf4815552009-08-03 22:52:21 +00001032 // Darwin/PPC always uses mach-o.
1033 TargetLoweringObjectFileMachO &TLOFMacho =
1034 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1035
Chris Lattner72a676a2009-08-10 01:39:42 +00001036
1037 const MCSection *LSPSection = 0;
1038 if (!FnStubs.empty()) // .lazy_symbol_pointer
1039 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1040
1041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001043 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001044 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001045 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1046 MCSectionMachO::S_SYMBOL_STUBS |
1047 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1048 32, SectionKind::getText());
1049 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001050 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001051 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001053 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001054 O << Info.Stub << ":\n";
1055 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001057 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1058 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001060 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001061 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001063 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1064 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 O << "\tmtctr r12\n";
1066 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001067
Chris Lattner73266f92009-08-19 05:49:37 +00001068 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001069 O << Info.LazyPtr << ":\n";
1070 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001071 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 }
Chris Lattner189198f2009-07-15 00:55:58 +00001073 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001074 const MCSection *StubSection =
1075 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1076 MCSectionMachO::S_SYMBOL_STUBS |
1077 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1078 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001079
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001080 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001081 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001082 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001084 const FnStubInfo &Info = I->second;
1085 O << Info.Stub << ":\n";
1086 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1087 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001088 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001089 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 O << "\tmtctr r12\n";
1091 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001092 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001093 O << Info.LazyPtr << ":\n";
1094 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001095 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096 }
1097 }
1098
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001099 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001101 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001102 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001103 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001104 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001105 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001106 E = Personalities.end(); I != E; ++I) {
1107 if (*I)
1108 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001109 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001110 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001111 }
1112
Chris Lattnerf4815552009-08-03 22:52:21 +00001113 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001114 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001115 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001116 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001117 EmitAlignment(isPPC64 ? 3 : 2);
1118
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001119 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1120 E = GVStubs.end(); I != E; ++I) {
1121 O << I->second << ":\n";
1122 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1123 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 }
1125 }
1126
Evan Chenga65854f2008-12-05 01:06:39 +00001127 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001128 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001129 EmitAlignment(isPPC64 ? 3 : 2);
1130 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1131 E = HiddenGVStubs.end(); I != E; ++I) {
1132 O << I->second << ":\n";
1133 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001134 }
1135 }
1136
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 // Funny Darwin hack: This flag tells the linker that no global symbols
1138 // contain code that falls through to other global symbols (e.g. the obvious
1139 // implementation of multiple entry points). If this doesn't occur, the
1140 // linker can safely perform dead code stripping. Since LLVM never generates
1141 // code that does this, it is always safe to set.
Chris Lattnerfe284f72009-10-19 18:03:08 +00001142 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143
Dan Gohman4a558a32007-07-25 19:33:14 +00001144 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145}
1146
1147
1148
1149/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1150/// for a MachineFunction to the given output stream, in a format that the
1151/// Darwin assembler can deal with.
1152///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001153static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1154 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001155 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001156 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1158
Chris Lattnerae982212009-07-21 18:38:57 +00001159 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001160 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1161 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001162}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001163
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001164// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001165extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001166 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001167 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1168}