blob: 6fff961c465d82e3f19278246ff89b905e2ba982 [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 Lattner9ea6e2a2009-07-15 02:28:57 +000074 // Already initialized.
75 if (!Stub.empty()) return;
Chris Lattnera38b2872010-01-13 06:38:18 +000076 SmallString<128> TmpStr;
77 Mang->makeNameProper(TmpStr, GVName + "$stub", Mangler::Private);
78 Stub = TmpStr.str();
79 TmpStr.clear();
80
81 Mang->makeNameProper(TmpStr, GVName + "$lazy_ptr", Mangler::Private);
82 LazyPtr = TmpStr.str();
83 TmpStr.clear();
84
85 Mang->makeNameProper(TmpStr, GVName + "$stub$tmp", Mangler::Private);
86 AnonSymbol = TmpStr.str();
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000087 }
88 };
89
90 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000091 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000093 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +000094 public:
David Greene302008d2009-07-14 20:18:05 +000095 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +000096 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000097 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +000098 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 virtual const char *getPassName() const {
101 return "PowerPC Assembly Printer";
102 }
103
104 PPCTargetMachine &getTM() {
105 return static_cast<PPCTargetMachine&>(TM);
106 }
107
108 unsigned enumRegToMachineReg(unsigned enumReg) {
109 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000110 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 case PPC::CR0: return 0;
112 case PPC::CR1: return 1;
113 case PPC::CR2: return 2;
114 case PPC::CR3: return 3;
115 case PPC::CR4: return 4;
116 case PPC::CR5: return 5;
117 case PPC::CR6: return 6;
118 case PPC::CR7: return 7;
119 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000120 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 }
122
123 /// printInstruction - This method is automatically generated by tablegen
124 /// from the instruction set description. This method returns true if the
125 /// machine instruction was sufficiently described to print it, otherwise it
126 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000127 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +0000128 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +0000129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130
131 void printMachineInstruction(const MachineInstr *MI);
132 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 /// stripRegisterPrefix - This method strips the character prefix from a
135 /// register name so that only the number is left. Used by for linux asm.
136 const char *stripRegisterPrefix(const char *RegName) {
137 switch (RegName[0]) {
138 case 'r':
139 case 'f':
140 case 'v': return RegName + 1;
141 case 'c': if (RegName[1] == 'r') return RegName + 2;
142 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 return RegName;
145 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000146
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 /// printRegister - Print register according to target requirements.
148 ///
149 void printRegister(const MachineOperand &MO, bool R0AsZero) {
150 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000151 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000152
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 // If we should use 0 for R0.
154 if (R0AsZero && RegNo == PPC::R0) {
155 O << "0";
156 return;
157 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000158
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000159 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 // Linux assembler (Others?) does not take register mnemonics.
161 // FIXME - What about special registers used in mfspr/mtspr?
162 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
163 O << RegName;
164 }
165
166 void printOperand(const MachineInstr *MI, unsigned OpNo) {
167 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000168 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000170 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000171 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 } else {
173 printOp(MO);
174 }
175 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000176
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
178 unsigned AsmVariant, const char *ExtraCode);
179 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
180 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000181
182
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000184 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 value = (value << (32-5)) >> (32-5);
186 O << (int)value;
187 }
188 void printU5ImmOperand(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 <= 31 && "Invalid u5imm argument!");
191 O << (unsigned int)value;
192 }
193 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000194 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 assert(value <= 63 && "Invalid u6imm argument!");
196 O << (unsigned int)value;
197 }
198 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000199 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 }
201 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000202 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 }
204 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000205 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000206 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 } else {
208 O << "lo16(";
209 printOp(MI->getOperand(OpNo));
210 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000211 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 else
213 O << ')';
214 }
215 }
216 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
217 // Branches can take an immediate operand. This is used by the branch
218 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000219 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000220 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 } else {
222 printOp(MI->getOperand(OpNo));
223 }
224 }
225 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
226 const MachineOperand &MO = MI->getOperand(OpNo);
227 if (TM.getRelocationModel() != Reloc::Static) {
228 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
229 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000230 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000232 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
233 FnInfo.Init(GV, Mang);
234 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 return;
236 }
237 }
238 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000239 SmallString<128> MangledName;
240 Mang->makeNameProper(MangledName, MO.getSymbolName());
241 FnStubInfo &FnInfo = FnStubs[MangledName.str()];
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000242 FnInfo.Init(MO.getSymbolName(), Mang);
243 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 return;
245 }
246 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000247
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 printOp(MI->getOperand(OpNo));
249 }
250 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000251 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 }
253 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000254 O << "\"L" << getFunctionNumber() << "$pb\"\n";
255 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 }
257 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000258 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 printS16ImmOperand(MI, OpNo);
260 } else {
261 if (Subtarget.isDarwin()) O << "ha16(";
262 printOp(MI->getOperand(OpNo));
263 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000264 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 if (Subtarget.isDarwin())
266 O << ')';
267 else
268 O << "@ha";
269 }
270 }
271 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000272 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 printS16ImmOperand(MI, OpNo);
274 } else {
275 if (Subtarget.isDarwin()) O << "lo16(";
276 printOp(MI->getOperand(OpNo));
277 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000278 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 if (Subtarget.isDarwin())
280 O << ')';
281 else
282 O << "@l";
283 }
284 }
285 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
286 unsigned CCReg = MI->getOperand(OpNo).getReg();
287 unsigned RegNo = enumRegToMachineReg(CCReg);
288 O << (0x80 >> RegNo);
289 }
290 // The new addressing mode printers.
291 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
292 printSymbolLo(MI, OpNo);
293 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000294 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 MI->getOperand(OpNo+1).getReg() == PPC::R0)
296 O << "0";
297 else
298 printOperand(MI, OpNo+1);
299 O << ')';
300 }
301 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000302 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000304 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 printSymbolLo(MI, OpNo);
306 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000307 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 MI->getOperand(OpNo+1).getReg() == PPC::R0)
309 O << "0";
310 else
311 printOperand(MI, OpNo+1);
312 O << ')';
313 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000314
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
316 // When used as the base register, r0 reads constant zero rather than
317 // the value contained in the register. For this reason, the darwin
318 // assembler requires that we print r0 as 0 (no r) when used as the base.
319 const MachineOperand &MO = MI->getOperand(OpNo);
320 printRegister(MO, true);
321 O << ", ";
322 printOperand(MI, OpNo+1);
323 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000324
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000325 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
326 const MachineOperand &MO = MI->getOperand(OpNo);
327
328 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
329
330 GlobalValue *GV = MO.getGlobal();
331
332 std::string Name = Mang->getMangledName(GV);
333
334 // Map symbol -> label of TOC entry.
335 if (TOC.count(Name) == 0) {
336 std::string Label;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000337 Label += MAI->getPrivateGlobalPrefix();
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000338 Label += "C";
339 Label += utostr(LabelID++);
340
341 TOC[Name] = Label;
342 }
343
344 O << TOC[Name] << "@toc";
345 }
346
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000347 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000349
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 };
352
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000353 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000354 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000355 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000356 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000357 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000358 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359
360 virtual const char *getPassName() const {
361 return "Linux PPC Assembly Printer";
362 }
363
364 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000365 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 void getAnalysisUsage(AnalysisUsage &AU) const {
368 AU.setPreservesAll();
369 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000370 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 PPCAsmPrinter::getAnalysisUsage(AU);
372 }
373
Chris Lattnerae982212009-07-21 18:38:57 +0000374 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 };
376
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000377 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
378 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000379 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000380 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000381 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000382 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000383 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000384 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385
386 virtual const char *getPassName() const {
387 return "Darwin PPC Assembly Printer";
388 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000389
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000392 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000393
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 void getAnalysisUsage(AnalysisUsage &AU) const {
395 AU.setPreservesAll();
396 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000397 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 PPCAsmPrinter::getAnalysisUsage(AU);
399 }
400
Chris Lattnerae982212009-07-21 18:38:57 +0000401 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 };
403} // end of anonymous namespace
404
405// Include the auto-generated portion of the assembly writer
406#include "PPCGenAsmWriter.inc"
407
408void PPCAsmPrinter::printOp(const MachineOperand &MO) {
409 switch (MO.getType()) {
410 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000411 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
413 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000414 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 return;
416 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000417 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000418 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 // FIXME: PIC relocation model
420 return;
421 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000422 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000423 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000425 case MachineOperand::MO_BlockAddress:
426 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
427 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000428 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 // Computing the address of an external symbol, not calling it.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000430 std::string Name(MAI->getGlobalPrefix());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000431 Name += MO.getSymbolName();
432
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000434 GVStubs[Name] = Name+"$non_lazy_ptr";
435 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000437 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000439 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 case MachineOperand::MO_GlobalAddress: {
441 // Computing the address of a global symbol, not calling it.
442 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000443 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444
445 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000446 if (TM.getRelocationModel() != Reloc::Static &&
447 (GV->isDeclaration() || GV->isWeakForLinker())) {
448 if (!GV->hasHiddenVisibility()) {
449 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
450 GVStubs[Mang->getMangledName(GV)] = Name;
451 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
452 GV->hasAvailableExternallyLinkage()) {
453 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
454 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
455 } else {
456 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000458 } else {
459 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 }
461 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000462
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000463 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 return;
465 }
466
467 default:
468 O << "<unknown operand type: " << MO.getType() << ">";
469 return;
470 }
471}
472
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473/// PrintAsmOperand - Print out an operand for an inline asm expression.
474///
475bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000476 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 const char *ExtraCode) {
478 // Does this asm operand have a single letter operand modifier?
479 if (ExtraCode && ExtraCode[0]) {
480 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000481
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 switch (ExtraCode[0]) {
483 default: return true; // Unknown modifier.
484 case 'c': // Don't print "$" before a global var name or constant.
485 // PPC never has a prefix.
486 printOperand(MI, OpNo);
487 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000488 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000490 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000492 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 return true;
494 ++OpNo; // Return the high-part.
495 break;
496 case 'I':
497 // Write 'i' if an integer constant, otherwise nothing. Used to print
498 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000499 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 O << "i";
501 return false;
502 }
503 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000504
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 printOperand(MI, OpNo);
506 return false;
507}
508
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000509// At the moment, all inline asm memory operands are a single register.
510// In any case, the output of this routine should always be just one
511// assembler operand.
512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000514 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 const char *ExtraCode) {
516 if (ExtraCode && ExtraCode[0])
517 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000518 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000519 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000520 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000521 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 return false;
523}
524
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000525void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 const char *Modifier) {
527 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
528 unsigned Code = MI->getOperand(OpNo).getImm();
529 if (!strcmp(Modifier, "cc")) {
530 switch ((PPC::Predicate)Code) {
531 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
532 case PPC::PRED_LT: O << "lt"; return;
533 case PPC::PRED_LE: O << "le"; return;
534 case PPC::PRED_EQ: O << "eq"; return;
535 case PPC::PRED_GE: O << "ge"; return;
536 case PPC::PRED_GT: O << "gt"; return;
537 case PPC::PRED_NE: O << "ne"; return;
538 case PPC::PRED_UN: O << "un"; return;
539 case PPC::PRED_NU: O << "nu"; return;
540 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000541
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 } else {
543 assert(!strcmp(Modifier, "reg") &&
544 "Need to specify 'cc' or 'reg' as predicate op modifier!");
545 // Don't print the register for 'always'.
546 if (Code == PPC::PRED_ALWAYS) return;
547 printOperand(MI, OpNo+1);
548 }
549}
550
551
552/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
553/// the current output stream.
554///
555void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
556 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000557
Devang Patel5450fc12009-10-06 02:19:11 +0000558 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559
560 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000561 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000563 unsigned char SH = MI->getOperand(2).getImm();
564 unsigned char MB = MI->getOperand(3).getImm();
565 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000567 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 }
569 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000570 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 SH = 32-SH;
572 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000573 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 printOperand(MI, 0);
575 O << ", ";
576 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000577 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 }
579 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
580 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000581 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000582 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 printOperand(MI, 0);
584 O << ", ";
585 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 }
587 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000588 unsigned char SH = MI->getOperand(2).getImm();
589 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
591 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000592 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000593 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 printOperand(MI, 0);
595 O << ", ";
596 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000597 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 }
599 }
600
Dale Johannesen760acc02010-01-06 02:20:18 +0000601 if (!useSubstituteMnemonic)
602 printInstruction(MI);
603
David Greeneca9b04b2009-11-13 21:34:57 +0000604 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000605 EmitComments(*MI);
606 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000607
608 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609}
610
611/// runOnMachineFunction - This uses the printMachineInstruction()
612/// method to print assembly for each instruction.
613///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000614bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000615 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616
617 SetupMachineFunction(MF);
618 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000619
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 // Print out constants referenced by the function
621 EmitConstantPool(MF.getConstantPool());
622
623 // Print out labels for the function.
624 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000625 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000626
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000628 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000629 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 case Function::InternalLinkage: // Symbols default to internal.
631 break;
632 case Function::ExternalLinkage:
633 O << "\t.global\t" << CurrentFnName << '\n'
634 << "\t.type\t" << CurrentFnName << ", @function\n";
635 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000636 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000637 case Function::WeakAnyLinkage:
638 case Function::WeakODRLinkage:
639 case Function::LinkOnceAnyLinkage:
640 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 O << "\t.global\t" << CurrentFnName << '\n';
642 O << "\t.weak\t" << CurrentFnName << '\n';
643 break;
644 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000645
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000646 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000647
Bill Wendling25a8ae32009-06-30 22:38:32 +0000648 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000649
650 if (Subtarget.isPPC64()) {
651 // Emit an official procedure descriptor.
652 // FIXME 64-bit SVR4: Use MCSection here?
653 O << "\t.section\t\".opd\",\"aw\"\n";
654 O << "\t.align 3\n";
655 O << CurrentFnName << ":\n";
656 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
657 O << "\t.previous\n";
658 O << ".L." << CurrentFnName << ":\n";
659 } else {
660 O << CurrentFnName << ":\n";
661 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662
663 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000664 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665
666 // Print out code for the function.
667 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
668 I != E; ++I) {
669 // Print a label for the basic block.
670 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000671 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 }
673 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
674 II != E; ++II) {
675 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 printMachineInstruction(II);
677 }
678 }
679
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000680 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681
Chris Lattner73266f92009-08-19 05:49:37 +0000682 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000683
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000685 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000686
Bill Wendling6fb58e12009-11-09 21:20:14 +0000687 // Print out jump tables referenced by the function.
688 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
689
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 // We didn't modify anything.
691 return false;
692}
693
Chris Lattnerae982212009-07-21 18:38:57 +0000694void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 const TargetData *TD = TM.getTargetData();
696
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000697 if (!GVar->hasInitializer())
698 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000699
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000700 // Check to see if this is a special global used by LLVM, if so, emit it.
701 if (EmitSpecialLLVMGlobal(GVar))
702 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000704 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000706 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000707
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000708 Constant *C = GVar->getInitializer();
709 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000710 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000711 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712
Chris Lattner73266f92009-08-19 05:49:37 +0000713 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
714 TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000715
716 if (C->isNullValue() && /* FIXME: Verify correct */
717 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000718 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000719 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000721
722 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 O << "\t.global " << name << '\n';
724 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000725 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000726 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000727 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000728 O << MAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000730 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 }
Evan Cheng11db8142009-03-24 00:17:40 +0000732 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000733 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000734 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000735 O << "'";
736 }
737 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000738 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 }
740
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000741 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000742 case GlobalValue::LinkOnceAnyLinkage:
743 case GlobalValue::LinkOnceODRLinkage:
744 case GlobalValue::WeakAnyLinkage:
745 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000746 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000747 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000748 O << "\t.global " << name << '\n'
749 << "\t.type " << name << ", @object\n"
750 << "\t.weak " << name << '\n';
751 break;
752 case GlobalValue::AppendingLinkage:
753 // FIXME: appending linkage variables should go into a section of
754 // their name or something. For now, just emit them as external.
755 case GlobalValue::ExternalLinkage:
756 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000757 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000758 << "\t.type " << name << ", @object\n";
759 // FALL THROUGH
760 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000761 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000762 break;
763 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000764 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000765 }
766
767 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000768 O << name << ":";
769 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000770 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000771 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000772 O << "'";
773 }
774 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000775
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000776 EmitGlobalConstant(C);
777 O << '\n';
778}
779
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000780bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
781 const TargetData *TD = TM.getTargetData();
782
783 bool isPPC64 = TD->getPointerSizeInBits() == 64;
784
785 if (isPPC64 && !TOC.empty()) {
786 // FIXME 64-bit SVR4: Use MCSection here?
787 O << "\t.section\t\".toc\",\"aw\"\n";
788
789 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
790 I != E; ++I) {
791 O << I->second << ":\n";
792 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
793 }
794 }
795
796 return AsmPrinter::doFinalization(M);
797}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799/// runOnMachineFunction - This uses the printMachineInstruction()
800/// method to print assembly for each instruction.
801///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000802bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000803 this->MF = &MF;
804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 SetupMachineFunction(MF);
806 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000807
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 // Print out constants referenced by the function
809 EmitConstantPool(MF.getConstantPool());
810
811 // Print out labels for the function.
812 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000813 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000814
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000816 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000817 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 case Function::InternalLinkage: // Symbols default to internal.
819 break;
820 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000821 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000823 case Function::WeakAnyLinkage:
824 case Function::WeakODRLinkage:
825 case Function::LinkOnceAnyLinkage:
826 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000827 case Function::LinkerPrivateLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000828 O << "\t.globl\t" << CurrentFnName << '\n';
829 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 break;
831 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000832
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000833 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000834
Bill Wendling25a8ae32009-06-30 22:38:32 +0000835 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836 O << CurrentFnName << ":\n";
837
838 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000839 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840
Bill Wendling36ccaea2008-01-26 06:51:24 +0000841 // If the function is empty, then we need to emit *something*. Otherwise, the
842 // function's label might be associated with something that it wasn't meant to
843 // be associated with. We emit a noop in this situation.
844 MachineFunction::iterator I = MF.begin();
845
Bill Wendlingb5880a72008-01-26 09:03:52 +0000846 if (++I == MF.end() && MF.front().empty())
847 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000848
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 // Print out code for the function.
850 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
851 I != E; ++I) {
852 // Print a label for the basic block.
853 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000854 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000856 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
857 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 printMachineInstruction(II);
860 }
861 }
862
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000864 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000865
Bill Wendling5019fe02009-11-09 21:45:26 +0000866 // Print out jump tables referenced by the function.
867 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
868
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 // We didn't modify anything.
870 return false;
871}
872
873
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000874void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000875 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000876 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 "ppc",
878 "ppc601",
879 "ppc602",
880 "ppc603",
881 "ppc7400",
882 "ppc750",
883 "ppc970",
884 "ppc64"
885 };
886
887 unsigned Directive = Subtarget.getDarwinDirective();
888 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
889 Directive = PPC::DIR_970;
890 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
891 Directive = PPC::DIR_7400;
892 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
893 Directive = PPC::DIR_64;
894 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000895 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000896
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 // Prime text sections so they are adjacent. This reduces the likelihood a
898 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000899 TargetLoweringObjectFileMachO &TLOFMacho =
900 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000901 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000903 OutStreamer.SwitchSection(
904 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
905 MCSectionMachO::S_SYMBOL_STUBS |
906 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
907 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000909 OutStreamer.SwitchSection(
910 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
911 MCSectionMachO::S_SYMBOL_STUBS |
912 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
913 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 }
Chris Lattner73266f92009-08-19 05:49:37 +0000915 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916}
917
Chris Lattnerae982212009-07-21 18:38:57 +0000918void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000919 const TargetData *TD = TM.getTargetData();
920
921 if (!GVar->hasInitializer())
922 return; // External global require no code
923
924 // Check to see if this is a special global used by LLVM, if so, emit it.
925 if (EmitSpecialLLVMGlobal(GVar)) {
926 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000927 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000928 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000929 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000930 O << ".reference .destructors_used\n";
931 }
932 return;
933 }
934
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000935 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000936 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000937
938 Constant *C = GVar->getInitializer();
939 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000940 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000941 unsigned Align = TD->getPreferredAlignmentLog(GVar);
942
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000943 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000944 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000945 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000946
Chris Lattnerdb727932009-08-04 05:35:56 +0000947 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000948 if (C->isNullValue() && /* FIXME: Verify correct */
949 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000950 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000951 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000952 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000953 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000954 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
955
956 if (GVar->hasExternalLinkage()) {
957 O << "\t.globl " << name << '\n';
958 O << "\t.zerofill __DATA, __common, " << name << ", "
959 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000960 } else if (GVar->hasLocalLinkage()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000961 O << MAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000962 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000963 O << "\t.globl " << name << '\n'
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000964 << MAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000965 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000966 O << name << ":";
967 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000968 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000969 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000970 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000971 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000972 EmitGlobalConstant(C);
973 return;
974 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000975 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000976 // Darwin 9 and above support aligned common data.
977 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000978 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000979 }
Evan Cheng11db8142009-03-24 00:17:40 +0000980 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000981 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000982 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000983 O << "'";
984 }
985 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000986 return;
987 }
988
989 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000990 case GlobalValue::LinkOnceAnyLinkage:
991 case GlobalValue::LinkOnceODRLinkage:
992 case GlobalValue::WeakAnyLinkage:
993 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000994 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000995 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000996 O << "\t.globl " << name << '\n'
997 << "\t.weak_definition " << name << '\n';
998 break;
999 case GlobalValue::AppendingLinkage:
1000 // FIXME: appending linkage variables should go into a section of
1001 // their name or something. For now, just emit them as external.
1002 case GlobalValue::ExternalLinkage:
1003 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001004 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001005 // FALL THROUGH
1006 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +00001007 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001008 break;
1009 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001010 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001011 }
1012
1013 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001014 O << name << ":";
1015 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001016 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001017 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001018 O << "'";
1019 }
1020 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001021
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001022 EmitGlobalConstant(C);
1023 O << '\n';
1024}
1025
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001026bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027 const TargetData *TD = TM.getTargetData();
1028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1030
Chris Lattnerf4815552009-08-03 22:52:21 +00001031 // Darwin/PPC always uses mach-o.
1032 TargetLoweringObjectFileMachO &TLOFMacho =
1033 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1034
Chris Lattner72a676a2009-08-10 01:39:42 +00001035
1036 const MCSection *LSPSection = 0;
1037 if (!FnStubs.empty()) // .lazy_symbol_pointer
1038 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1039
1040
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001042 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001043 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001044 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1045 MCSectionMachO::S_SYMBOL_STUBS |
1046 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1047 32, SectionKind::getText());
1048 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001049 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001050 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001052 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001053 O << Info.Stub << ":\n";
1054 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001056 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1057 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001059 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001060 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001062 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1063 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 O << "\tmtctr r12\n";
1065 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001066
Chris Lattner73266f92009-08-19 05:49:37 +00001067 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001068 O << Info.LazyPtr << ":\n";
1069 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001070 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 }
Chris Lattner189198f2009-07-15 00:55:58 +00001072 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001073 const MCSection *StubSection =
1074 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1075 MCSectionMachO::S_SYMBOL_STUBS |
1076 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1077 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001078
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001079 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001080 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001081 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001083 const FnStubInfo &Info = I->second;
1084 O << Info.Stub << ":\n";
1085 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1086 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001087 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001088 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 O << "\tmtctr r12\n";
1090 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001091 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001092 O << Info.LazyPtr << ":\n";
1093 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001094 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095 }
1096 }
1097
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001098 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001100 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001101 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001102 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001103 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001104 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001105 E = Personalities.end(); I != E; ++I) {
1106 if (*I)
1107 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001108 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001109 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001110 }
1111
Chris Lattnerf4815552009-08-03 22:52:21 +00001112 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001113 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001114 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001115 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001116 EmitAlignment(isPPC64 ? 3 : 2);
1117
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001118 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1119 E = GVStubs.end(); I != E; ++I) {
1120 O << I->second << ":\n";
1121 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1122 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001123 }
1124 }
1125
Evan Chenga65854f2008-12-05 01:06:39 +00001126 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001127 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001128 EmitAlignment(isPPC64 ? 3 : 2);
1129 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1130 E = HiddenGVStubs.end(); I != E; ++I) {
1131 O << I->second << ":\n";
1132 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001133 }
1134 }
1135
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136 // Funny Darwin hack: This flag tells the linker that no global symbols
1137 // contain code that falls through to other global symbols (e.g. the obvious
1138 // implementation of multiple entry points). If this doesn't occur, the
1139 // linker can safely perform dead code stripping. Since LLVM never generates
1140 // code that does this, it is always safe to set.
Chris Lattnerfe284f72009-10-19 18:03:08 +00001141 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142
Dan Gohman4a558a32007-07-25 19:33:14 +00001143 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001144}
1145
1146
1147
1148/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1149/// for a MachineFunction to the given output stream, in a format that the
1150/// Darwin assembler can deal with.
1151///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001152static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1153 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001154 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001155 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1157
Chris Lattnerae982212009-07-21 18:38:57 +00001158 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001159 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1160 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001161}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001162
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001163// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001164extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001165 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001166 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1167}