blob: 9c02aefdc4cd0a99462afb685dad623798a76add [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048#include "llvm/Support/Compiler.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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053using namespace llvm;
54
55STATISTIC(EmittedInsts, "Number of machine instrs printed");
56
57namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000058 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
59 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
73 void Init(const std::string &GV, Mangler *Mang) {
74 // Already initialized.
75 if (!Stub.empty()) return;
Bill Wendling41a07852009-07-20 01:03:30 +000076 Stub = Mang->makeNameProper(GV + "$stub",
Bill Wendlinge4699112009-07-20 19:41:27 +000077 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000078 LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr",
Bill Wendlinge4699112009-07-20 19:41:27 +000079 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000080 AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp",
Bill Wendlinge4699112009-07-20 19:41:27 +000081 Mangler::Private);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000082 }
83 };
84
85 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000086 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000088 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +000089 public:
David Greene302008d2009-07-14 20:18:05 +000090 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +000091 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000092 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +000093 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 virtual const char *getPassName() const {
96 return "PowerPC Assembly Printer";
97 }
98
99 PPCTargetMachine &getTM() {
100 return static_cast<PPCTargetMachine&>(TM);
101 }
102
103 unsigned enumRegToMachineReg(unsigned enumReg) {
104 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000105 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 case PPC::CR0: return 0;
107 case PPC::CR1: return 1;
108 case PPC::CR2: return 2;
109 case PPC::CR3: return 3;
110 case PPC::CR4: return 4;
111 case PPC::CR5: return 5;
112 case PPC::CR6: return 6;
113 case PPC::CR7: return 7;
114 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000115 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 }
117
118 /// printInstruction - This method is automatically generated by tablegen
119 /// from the instruction set description. This method returns true if the
120 /// machine instruction was sufficiently described to print it, otherwise it
121 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000122 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +0000123 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +0000124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125
126 void printMachineInstruction(const MachineInstr *MI);
127 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 /// stripRegisterPrefix - This method strips the character prefix from a
130 /// register name so that only the number is left. Used by for linux asm.
131 const char *stripRegisterPrefix(const char *RegName) {
132 switch (RegName[0]) {
133 case 'r':
134 case 'f':
135 case 'v': return RegName + 1;
136 case 'c': if (RegName[1] == 'r') return RegName + 2;
137 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 return RegName;
140 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 /// printRegister - Print register according to target requirements.
143 ///
144 void printRegister(const MachineOperand &MO, bool R0AsZero) {
145 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000146 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000147
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 // If we should use 0 for R0.
149 if (R0AsZero && RegNo == PPC::R0) {
150 O << "0";
151 return;
152 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000153
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000154 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 // Linux assembler (Others?) does not take register mnemonics.
156 // FIXME - What about special registers used in mfspr/mtspr?
157 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
158 O << RegName;
159 }
160
161 void printOperand(const MachineInstr *MI, unsigned OpNo) {
162 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000163 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000165 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000166 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 } else {
168 printOp(MO);
169 }
170 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
173 unsigned AsmVariant, const char *ExtraCode);
174 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
175 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000176
177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000179 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 value = (value << (32-5)) >> (32-5);
181 O << (int)value;
182 }
183 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000184 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 assert(value <= 31 && "Invalid u5imm argument!");
186 O << (unsigned int)value;
187 }
188 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000189 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 assert(value <= 63 && "Invalid u6imm argument!");
191 O << (unsigned int)value;
192 }
193 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000194 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000197 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 }
199 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000200 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000201 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 } else {
203 O << "lo16(";
204 printOp(MI->getOperand(OpNo));
205 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000206 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 else
208 O << ')';
209 }
210 }
211 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
212 // Branches can take an immediate operand. This is used by the branch
213 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000214 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000215 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 } else {
217 printOp(MI->getOperand(OpNo));
218 }
219 }
220 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
221 const MachineOperand &MO = MI->getOperand(OpNo);
222 if (TM.getRelocationModel() != Reloc::Static) {
223 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
224 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000225 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000227 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
228 FnInfo.Init(GV, Mang);
229 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 return;
231 }
232 }
233 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000234 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
235 FnInfo.Init(MO.getSymbolName(), Mang);
236 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 return;
238 }
239 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 printOp(MI->getOperand(OpNo));
242 }
243 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000244 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 }
246 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000247 O << "\"L" << getFunctionNumber() << "$pb\"\n";
248 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 }
250 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000251 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 printS16ImmOperand(MI, OpNo);
253 } else {
254 if (Subtarget.isDarwin()) O << "ha16(";
255 printOp(MI->getOperand(OpNo));
256 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000257 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 if (Subtarget.isDarwin())
259 O << ')';
260 else
261 O << "@ha";
262 }
263 }
264 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000265 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 printS16ImmOperand(MI, OpNo);
267 } else {
268 if (Subtarget.isDarwin()) O << "lo16(";
269 printOp(MI->getOperand(OpNo));
270 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000271 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (Subtarget.isDarwin())
273 O << ')';
274 else
275 O << "@l";
276 }
277 }
278 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
279 unsigned CCReg = MI->getOperand(OpNo).getReg();
280 unsigned RegNo = enumRegToMachineReg(CCReg);
281 O << (0x80 >> RegNo);
282 }
283 // The new addressing mode printers.
284 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
285 printSymbolLo(MI, OpNo);
286 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000287 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 MI->getOperand(OpNo+1).getReg() == PPC::R0)
289 O << "0";
290 else
291 printOperand(MI, OpNo+1);
292 O << ')';
293 }
294 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000295 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000297 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 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 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000307
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
309 // When used as the base register, r0 reads constant zero rather than
310 // the value contained in the register. For this reason, the darwin
311 // assembler requires that we print r0 as 0 (no r) when used as the base.
312 const MachineOperand &MO = MI->getOperand(OpNo);
313 printRegister(MO, true);
314 O << ", ";
315 printOperand(MI, OpNo+1);
316 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000317
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000318 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
319 const MachineOperand &MO = MI->getOperand(OpNo);
320
321 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
322
323 GlobalValue *GV = MO.getGlobal();
324
325 std::string Name = Mang->getMangledName(GV);
326
327 // Map symbol -> label of TOC entry.
328 if (TOC.count(Name) == 0) {
329 std::string Label;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000330 Label += MAI->getPrivateGlobalPrefix();
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000331 Label += "C";
332 Label += utostr(LabelID++);
333
334 TOC[Name] = Label;
335 }
336
337 O << TOC[Name] << "@toc";
338 }
339
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000340 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344
345 virtual void EmitExternalGlobal(const GlobalVariable *GV);
346 };
347
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000348 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Bill Wendling4f405312009-02-24 08:30:20 +0000349 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000350 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000351 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000352 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000353 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354
355 virtual const char *getPassName() const {
356 return "Linux PPC Assembly Printer";
357 }
358
359 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000360 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000361
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 void getAnalysisUsage(AnalysisUsage &AU) const {
363 AU.setPreservesAll();
364 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000365 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 PPCAsmPrinter::getAnalysisUsage(AU);
367 }
368
Chris Lattnerae982212009-07-21 18:38:57 +0000369 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 };
371
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000372 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
373 /// OS X
Bill Wendling4f405312009-02-24 08:30:20 +0000374 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000375 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000376 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000377 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000378 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000379 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380
381 virtual const char *getPassName() const {
382 return "Darwin PPC Assembly Printer";
383 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000384
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 bool runOnMachineFunction(MachineFunction &F);
386 bool doInitialization(Module &M);
387 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000388
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 void getAnalysisUsage(AnalysisUsage &AU) const {
390 AU.setPreservesAll();
391 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000392 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 PPCAsmPrinter::getAnalysisUsage(AU);
394 }
395
Chris Lattnerae982212009-07-21 18:38:57 +0000396 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 };
398} // end of anonymous namespace
399
400// Include the auto-generated portion of the assembly writer
401#include "PPCGenAsmWriter.inc"
402
403void PPCAsmPrinter::printOp(const MachineOperand &MO) {
404 switch (MO.getType()) {
405 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000406 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407
408 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000409 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 return;
411 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000412 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000413 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 // FIXME: PIC relocation model
415 return;
416 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000417 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000418 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000420 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 // Computing the address of an external symbol, not calling it.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000422 std::string Name(MAI->getGlobalPrefix());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000423 Name += MO.getSymbolName();
424
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000426 GVStubs[Name] = Name+"$non_lazy_ptr";
427 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000429 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000431 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 case MachineOperand::MO_GlobalAddress: {
433 // Computing the address of a global symbol, not calling it.
434 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000435 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436
437 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000438 if (TM.getRelocationModel() != Reloc::Static &&
439 (GV->isDeclaration() || GV->isWeakForLinker())) {
440 if (!GV->hasHiddenVisibility()) {
441 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
442 GVStubs[Mang->getMangledName(GV)] = Name;
443 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
444 GV->hasAvailableExternallyLinkage()) {
445 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
446 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
447 } else {
448 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000450 } else {
451 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 }
453 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000454
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000455 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 return;
457 }
458
459 default:
460 O << "<unknown operand type: " << MO.getType() << ">";
461 return;
462 }
463}
464
465/// EmitExternalGlobal - In this case we need to use the indirect symbol.
466///
467void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000468 std::string Name;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000469
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000471 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000472 } else {
473 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 }
475 O << Name;
476}
477
478/// PrintAsmOperand - Print out an operand for an inline asm expression.
479///
480bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000481 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 const char *ExtraCode) {
483 // Does this asm operand have a single letter operand modifier?
484 if (ExtraCode && ExtraCode[0]) {
485 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000486
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 switch (ExtraCode[0]) {
488 default: return true; // Unknown modifier.
489 case 'c': // Don't print "$" before a global var name or constant.
490 // PPC never has a prefix.
491 printOperand(MI, OpNo);
492 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000493 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000495 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000497 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 return true;
499 ++OpNo; // Return the high-part.
500 break;
501 case 'I':
502 // Write 'i' if an integer constant, otherwise nothing. Used to print
503 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000504 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 O << "i";
506 return false;
507 }
508 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000509
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 printOperand(MI, OpNo);
511 return false;
512}
513
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000514// At the moment, all inline asm memory operands are a single register.
515// In any case, the output of this routine should always be just one
516// assembler operand.
517
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000519 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 const char *ExtraCode) {
521 if (ExtraCode && ExtraCode[0])
522 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000523 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000524 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000525 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000526 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 return false;
528}
529
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000530void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 const char *Modifier) {
532 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
533 unsigned Code = MI->getOperand(OpNo).getImm();
534 if (!strcmp(Modifier, "cc")) {
535 switch ((PPC::Predicate)Code) {
536 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
537 case PPC::PRED_LT: O << "lt"; return;
538 case PPC::PRED_LE: O << "le"; return;
539 case PPC::PRED_EQ: O << "eq"; return;
540 case PPC::PRED_GE: O << "ge"; return;
541 case PPC::PRED_GT: O << "gt"; return;
542 case PPC::PRED_NE: O << "ne"; return;
543 case PPC::PRED_UN: O << "un"; return;
544 case PPC::PRED_NU: O << "nu"; return;
545 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000546
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 } else {
548 assert(!strcmp(Modifier, "reg") &&
549 "Need to specify 'cc' or 'reg' as predicate op modifier!");
550 // Don't print the register for 'always'.
551 if (Code == PPC::PRED_ALWAYS) return;
552 printOperand(MI, OpNo+1);
553 }
554}
555
556
557/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
558/// the current output stream.
559///
560void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
561 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000562
563 processDebugLoc(MI->getDebugLoc());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564
565 // Check for slwi/srwi mnemonics.
566 if (MI->getOpcode() == PPC::RLWINM) {
567 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000568 unsigned char SH = MI->getOperand(2).getImm();
569 unsigned char MB = MI->getOperand(3).getImm();
570 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000572 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 }
574 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000575 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 SH = 32-SH;
577 }
578 if (FoundMnemonic) {
579 printOperand(MI, 0);
580 O << ", ";
581 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000582 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 return;
584 }
585 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
586 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000587 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 printOperand(MI, 0);
589 O << ", ";
590 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000591 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 return;
593 }
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) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000599 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 printOperand(MI, 0);
601 O << ", ";
602 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000603 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 return;
605 }
606 }
607
Chris Lattner7d337492009-08-08 00:05:42 +0000608 printInstruction(MI);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000609
610 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
611 EmitComments(*MI);
612 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613}
614
615/// runOnMachineFunction - This uses the printMachineInstruction()
616/// method to print assembly for each instruction.
617///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000618bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000619 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620
621 SetupMachineFunction(MF);
622 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000623
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 // Print out constants referenced by the function
625 EmitConstantPool(MF.getConstantPool());
626
627 // Print out labels for the function.
628 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000629 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000630
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000632 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000633 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 case Function::InternalLinkage: // Symbols default to internal.
635 break;
636 case Function::ExternalLinkage:
637 O << "\t.global\t" << CurrentFnName << '\n'
638 << "\t.type\t" << CurrentFnName << ", @function\n";
639 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000640 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000641 case Function::WeakAnyLinkage:
642 case Function::WeakODRLinkage:
643 case Function::LinkOnceAnyLinkage:
644 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 O << "\t.global\t" << CurrentFnName << '\n';
646 O << "\t.weak\t" << CurrentFnName << '\n';
647 break;
648 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000649
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000650 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000651
Bill Wendling25a8ae32009-06-30 22:38:32 +0000652 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000653
654 if (Subtarget.isPPC64()) {
655 // Emit an official procedure descriptor.
656 // FIXME 64-bit SVR4: Use MCSection here?
657 O << "\t.section\t\".opd\",\"aw\"\n";
658 O << "\t.align 3\n";
659 O << CurrentFnName << ":\n";
660 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
661 O << "\t.previous\n";
662 O << ".L." << CurrentFnName << ":\n";
663 } else {
664 O << CurrentFnName << ":\n";
665 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666
667 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000668 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669
670 // Print out code for the function.
671 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
672 I != E; ++I) {
673 // Print a label for the basic block.
674 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000675 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 O << '\n';
677 }
678 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
679 II != E; ++II) {
680 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 printMachineInstruction(II);
682 }
683 }
684
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000685 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686
687 // Print out jump tables referenced by the function.
688 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000689
Chris Lattner73266f92009-08-19 05:49:37 +0000690 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000691
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000693 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000694
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 // We didn't modify anything.
696 return false;
697}
698
Chris Lattnerae982212009-07-21 18:38:57 +0000699void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 const TargetData *TD = TM.getTargetData();
701
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000702 if (!GVar->hasInitializer())
703 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000704
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000705 // Check to see if this is a special global used by LLVM, if so, emit it.
706 if (EmitSpecialLLVMGlobal(GVar))
707 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000709 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000711 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000712
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000713 Constant *C = GVar->getInitializer();
714 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000715 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000716 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717
Chris Lattner73266f92009-08-19 05:49:37 +0000718 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
719 TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000720
721 if (C->isNullValue() && /* FIXME: Verify correct */
722 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000723 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000724 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000726
727 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 O << "\t.global " << name << '\n';
729 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000730 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000731 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000732 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000733 O << MAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000735 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 }
Evan Cheng11db8142009-03-24 00:17:40 +0000737 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000738 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000739 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000740 O << "'";
741 }
742 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000743 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 }
745
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000746 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000747 case GlobalValue::LinkOnceAnyLinkage:
748 case GlobalValue::LinkOnceODRLinkage:
749 case GlobalValue::WeakAnyLinkage:
750 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000751 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000752 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000753 O << "\t.global " << name << '\n'
754 << "\t.type " << name << ", @object\n"
755 << "\t.weak " << name << '\n';
756 break;
757 case GlobalValue::AppendingLinkage:
758 // FIXME: appending linkage variables should go into a section of
759 // their name or something. For now, just emit them as external.
760 case GlobalValue::ExternalLinkage:
761 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000762 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000763 << "\t.type " << name << ", @object\n";
764 // FALL THROUGH
765 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000766 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000767 break;
768 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000769 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000770 }
771
772 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000773 O << name << ":";
774 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000775 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000776 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000777 O << "'";
778 }
779 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000780
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000781 EmitGlobalConstant(C);
782 O << '\n';
783}
784
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000785bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
786 const TargetData *TD = TM.getTargetData();
787
788 bool isPPC64 = TD->getPointerSizeInBits() == 64;
789
790 if (isPPC64 && !TOC.empty()) {
791 // FIXME 64-bit SVR4: Use MCSection here?
792 O << "\t.section\t\".toc\",\"aw\"\n";
793
794 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
795 I != E; ++I) {
796 O << I->second << ":\n";
797 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
798 }
799 }
800
801 return AsmPrinter::doFinalization(M);
802}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804/// runOnMachineFunction - This uses the printMachineInstruction()
805/// method to print assembly for each instruction.
806///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000807bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000808 this->MF = &MF;
809
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 SetupMachineFunction(MF);
811 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000812
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 // Print out constants referenced by the function
814 EmitConstantPool(MF.getConstantPool());
815
816 // Print out labels for the function.
817 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000818 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000819
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000821 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000822 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 case Function::InternalLinkage: // Symbols default to internal.
824 break;
825 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000826 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000828 case Function::WeakAnyLinkage:
829 case Function::WeakODRLinkage:
830 case Function::LinkOnceAnyLinkage:
831 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000832 case Function::LinkerPrivateLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000833 O << "\t.globl\t" << CurrentFnName << '\n';
834 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 break;
836 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000837
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000838 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000839
Bill Wendling25a8ae32009-06-30 22:38:32 +0000840 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 O << CurrentFnName << ":\n";
842
843 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000844 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845
Bill Wendling36ccaea2008-01-26 06:51:24 +0000846 // If the function is empty, then we need to emit *something*. Otherwise, the
847 // function's label might be associated with something that it wasn't meant to
848 // be associated with. We emit a noop in this situation.
849 MachineFunction::iterator I = MF.begin();
850
Bill Wendlingb5880a72008-01-26 09:03:52 +0000851 if (++I == MF.end() && MF.front().empty())
852 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000853
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 // Print out code for the function.
855 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
856 I != E; ++I) {
857 // Print a label for the basic block.
858 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000859 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 O << '\n';
861 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000862 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
863 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 printMachineInstruction(II);
866 }
867 }
868
869 // Print out jump tables referenced by the function.
870 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000871
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000873 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000874
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 // We didn't modify anything.
876 return false;
877}
878
879
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000880bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000881 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000882 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 "ppc",
884 "ppc601",
885 "ppc602",
886 "ppc603",
887 "ppc7400",
888 "ppc750",
889 "ppc970",
890 "ppc64"
891 };
892
893 unsigned Directive = Subtarget.getDarwinDirective();
894 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
895 Directive = PPC::DIR_970;
896 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
897 Directive = PPC::DIR_7400;
898 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
899 Directive = PPC::DIR_64;
900 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000901 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000902
Dan Gohman4a558a32007-07-25 19:33:14 +0000903 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000904 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000905
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 // Prime text sections so they are adjacent. This reduces the likelihood a
907 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000908 TargetLoweringObjectFileMachO &TLOFMacho =
909 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000910 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000912 OutStreamer.SwitchSection(
913 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
914 MCSectionMachO::S_SYMBOL_STUBS |
915 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
916 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000918 OutStreamer.SwitchSection(
919 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
920 MCSectionMachO::S_SYMBOL_STUBS |
921 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
922 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 }
Chris Lattner73266f92009-08-19 05:49:37 +0000924 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000925
Dan Gohman4a558a32007-07-25 19:33:14 +0000926 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927}
928
Chris Lattnerae982212009-07-21 18:38:57 +0000929void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000930 const TargetData *TD = TM.getTargetData();
931
932 if (!GVar->hasInitializer())
933 return; // External global require no code
934
935 // Check to see if this is a special global used by LLVM, if so, emit it.
936 if (EmitSpecialLLVMGlobal(GVar)) {
937 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000938 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000939 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000940 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000941 O << ".reference .destructors_used\n";
942 }
943 return;
944 }
945
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000946 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000947 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000948
949 Constant *C = GVar->getInitializer();
950 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000951 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000952 unsigned Align = TD->getPreferredAlignmentLog(GVar);
953
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000954 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000955 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000956 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000957
Chris Lattnerdb727932009-08-04 05:35:56 +0000958 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000959 if (C->isNullValue() && /* FIXME: Verify correct */
960 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000961 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000962 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000963 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000964 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000965 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
966
967 if (GVar->hasExternalLinkage()) {
968 O << "\t.globl " << name << '\n';
969 O << "\t.zerofill __DATA, __common, " << name << ", "
970 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000971 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000972 O << MAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000973 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000974 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000975 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000976 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000977 O << name << ":";
978 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000979 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000980 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000981 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000982 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000983 EmitGlobalConstant(C);
984 return;
985 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000986 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000987 // Darwin 9 and above support aligned common data.
988 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000989 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000990 }
Evan Cheng11db8142009-03-24 00:17:40 +0000991 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000992 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000993 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000994 O << "'";
995 }
996 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000997 return;
998 }
999
1000 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +00001001 case GlobalValue::LinkOnceAnyLinkage:
1002 case GlobalValue::LinkOnceODRLinkage:
1003 case GlobalValue::WeakAnyLinkage:
1004 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +00001005 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +00001006 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001007 O << "\t.globl " << name << '\n'
1008 << "\t.weak_definition " << name << '\n';
1009 break;
1010 case GlobalValue::AppendingLinkage:
1011 // FIXME: appending linkage variables should go into a section of
1012 // their name or something. For now, just emit them as external.
1013 case GlobalValue::ExternalLinkage:
1014 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001015 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001016 // FALL THROUGH
1017 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +00001018 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001019 break;
1020 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001021 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001022 }
1023
1024 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001025 O << name << ":";
1026 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 O << "'";
1030 }
1031 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001032
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001033 EmitGlobalConstant(C);
1034 O << '\n';
1035}
1036
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001037bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001038 const TargetData *TD = TM.getTargetData();
1039
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1041
Chris Lattnerf4815552009-08-03 22:52:21 +00001042 // Darwin/PPC always uses mach-o.
1043 TargetLoweringObjectFileMachO &TLOFMacho =
1044 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1045
Chris Lattner72a676a2009-08-10 01:39:42 +00001046
1047 const MCSection *LSPSection = 0;
1048 if (!FnStubs.empty()) // .lazy_symbol_pointer
1049 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1050
1051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001053 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001054 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001055 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1056 MCSectionMachO::S_SYMBOL_STUBS |
1057 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1058 32, SectionKind::getText());
1059 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001060 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001061 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001063 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001064 O << Info.Stub << ":\n";
1065 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001067 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1068 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001070 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001071 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001073 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1074 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 O << "\tmtctr r12\n";
1076 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001077
Chris Lattner73266f92009-08-19 05:49:37 +00001078 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001079 O << Info.LazyPtr << ":\n";
1080 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001081 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 }
Chris Lattner189198f2009-07-15 00:55:58 +00001083 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001084 const MCSection *StubSection =
1085 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1086 MCSectionMachO::S_SYMBOL_STUBS |
1087 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1088 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001089
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001090 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001091 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001092 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001094 const FnStubInfo &Info = I->second;
1095 O << Info.Stub << ":\n";
1096 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1097 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001098 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001099 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 O << "\tmtctr r12\n";
1101 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001102 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001103 O << Info.LazyPtr << ":\n";
1104 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001105 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 }
1107 }
1108
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001109 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001111 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001112 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001113 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001114 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001115 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001116 E = Personalities.end(); I != E; ++I) {
1117 if (*I)
1118 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001119 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001120 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001121 }
1122
Chris Lattnerf4815552009-08-03 22:52:21 +00001123 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001124 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001125 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001126 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001127 EmitAlignment(isPPC64 ? 3 : 2);
1128
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001129 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1130 E = GVStubs.end(); I != E; ++I) {
1131 O << I->second << ":\n";
1132 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1133 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001134 }
1135 }
1136
Evan Chenga65854f2008-12-05 01:06:39 +00001137 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001138 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001139 EmitAlignment(isPPC64 ? 3 : 2);
1140 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1141 E = HiddenGVStubs.end(); I != E; ++I) {
1142 O << I->second << ":\n";
1143 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001144 }
1145 }
1146
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001147 // Funny Darwin hack: This flag tells the linker that no global symbols
1148 // contain code that falls through to other global symbols (e.g. the obvious
1149 // implementation of multiple entry points). If this doesn't occur, the
1150 // linker can safely perform dead code stripping. Since LLVM never generates
1151 // code that does this, it is always safe to set.
1152 O << "\t.subsections_via_symbols\n";
1153
Dan Gohman4a558a32007-07-25 19:33:14 +00001154 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155}
1156
1157
1158
1159/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1160/// for a MachineFunction to the given output stream, in a format that the
1161/// Darwin assembler can deal with.
1162///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001163static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1164 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001165 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001166 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001167 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1168
Chris Lattnerae982212009-07-21 18:38:57 +00001169 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001170 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1171 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001173
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001174// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001175extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001176 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001177 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1178}