blob: efb9e5c62cdcdcfabe2a24d4e290c95fe09bd72c [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 Lattner7f1ac7f2009-08-10 18:15:01 +000034#include "llvm/MC/MCSectionMachO.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000035#include "llvm/Target/TargetAsmInfo.h"
36#include "llvm/Target/TargetLoweringObjectFile.h"
37#include "llvm/Target/TargetRegisterInfo.h"
38#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetOptions.h"
40#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/Support/Mangler.h"
42#include "llvm/Support/MathExtras.h"
43#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000045#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000047#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048#include "llvm/ADT/Statistic.h"
49#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000050#include "llvm/ADT/StringSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051using namespace llvm;
52
53STATISTIC(EmittedInsts, "Number of machine instrs printed");
54
55namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000056 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
57 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000058 struct FnStubInfo {
59 std::string Stub, LazyPtr, AnonSymbol;
60
61 FnStubInfo() {}
62
63 void Init(const GlobalValue *GV, Mangler *Mang) {
64 // Already initialized.
65 if (!Stub.empty()) return;
66 Stub = Mang->getMangledName(GV, "$stub", true);
67 LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true);
68 AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true);
69 }
70
71 void Init(const std::string &GV, Mangler *Mang) {
72 // Already initialized.
73 if (!Stub.empty()) return;
Bill Wendling41a07852009-07-20 01:03:30 +000074 Stub = Mang->makeNameProper(GV + "$stub",
Bill Wendlinge4699112009-07-20 19:41:27 +000075 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000076 LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr",
Bill Wendlinge4699112009-07-20 19:41:27 +000077 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000078 AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp",
Bill Wendlinge4699112009-07-20 19:41:27 +000079 Mangler::Private);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000080 }
81 };
82
83 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000084 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000086 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +000087 public:
David Greene302008d2009-07-14 20:18:05 +000088 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000089 const TargetAsmInfo *T, bool V)
90 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +000091 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93 virtual const char *getPassName() const {
94 return "PowerPC Assembly Printer";
95 }
96
97 PPCTargetMachine &getTM() {
98 return static_cast<PPCTargetMachine&>(TM);
99 }
100
101 unsigned enumRegToMachineReg(unsigned enumReg) {
102 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000103 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 case PPC::CR0: return 0;
105 case PPC::CR1: return 1;
106 case PPC::CR2: return 2;
107 case PPC::CR3: return 3;
108 case PPC::CR4: return 4;
109 case PPC::CR5: return 5;
110 case PPC::CR6: return 6;
111 case PPC::CR7: return 7;
112 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000113 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 }
115
116 /// printInstruction - This method is automatically generated by tablegen
117 /// from the instruction set description. This method returns true if the
118 /// machine instruction was sufficiently described to print it, otherwise it
119 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000120 void printInstruction(const MachineInstr *MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 void printMachineInstruction(const MachineInstr *MI);
123 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 /// stripRegisterPrefix - This method strips the character prefix from a
126 /// register name so that only the number is left. Used by for linux asm.
127 const char *stripRegisterPrefix(const char *RegName) {
128 switch (RegName[0]) {
129 case 'r':
130 case 'f':
131 case 'v': return RegName + 1;
132 case 'c': if (RegName[1] == 'r') return RegName + 2;
133 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 return RegName;
136 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 /// printRegister - Print register according to target requirements.
139 ///
140 void printRegister(const MachineOperand &MO, bool R0AsZero) {
141 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000142 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 // If we should use 0 for R0.
145 if (R0AsZero && RegNo == PPC::R0) {
146 O << "0";
147 return;
148 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000149
Bill Wendling8eeb9792008-02-26 21:11:01 +0000150 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 // Linux assembler (Others?) does not take register mnemonics.
152 // FIXME - What about special registers used in mfspr/mtspr?
153 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
154 O << RegName;
155 }
156
157 void printOperand(const MachineInstr *MI, unsigned OpNo) {
158 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000159 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000161 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000162 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 } else {
164 printOp(MO);
165 }
166 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000167
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
169 unsigned AsmVariant, const char *ExtraCode);
170 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
171 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000172
173
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000175 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 value = (value << (32-5)) >> (32-5);
177 O << (int)value;
178 }
179 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000180 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 assert(value <= 31 && "Invalid u5imm argument!");
182 O << (unsigned int)value;
183 }
184 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000185 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 assert(value <= 63 && "Invalid u6imm argument!");
187 O << (unsigned int)value;
188 }
189 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000190 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 }
192 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000193 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 }
195 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000196 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000197 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 } else {
199 O << "lo16(";
200 printOp(MI->getOperand(OpNo));
201 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000202 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 else
204 O << ')';
205 }
206 }
207 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
208 // Branches can take an immediate operand. This is used by the branch
209 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000210 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000211 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 } else {
213 printOp(MI->getOperand(OpNo));
214 }
215 }
216 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
217 const MachineOperand &MO = MI->getOperand(OpNo);
218 if (TM.getRelocationModel() != Reloc::Static) {
219 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
220 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000221 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000223 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
224 FnInfo.Init(GV, Mang);
225 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 return;
227 }
228 }
229 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000230 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
231 FnInfo.Init(MO.getSymbolName(), Mang);
232 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 return;
234 }
235 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000236
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 printOp(MI->getOperand(OpNo));
238 }
239 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000240 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 }
242 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000243 O << "\"L" << getFunctionNumber() << "$pb\"\n";
244 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 }
246 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000247 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 printS16ImmOperand(MI, OpNo);
249 } else {
250 if (Subtarget.isDarwin()) O << "ha16(";
251 printOp(MI->getOperand(OpNo));
252 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000253 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 if (Subtarget.isDarwin())
255 O << ')';
256 else
257 O << "@ha";
258 }
259 }
260 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000261 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 printS16ImmOperand(MI, OpNo);
263 } else {
264 if (Subtarget.isDarwin()) O << "lo16(";
265 printOp(MI->getOperand(OpNo));
266 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000267 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 if (Subtarget.isDarwin())
269 O << ')';
270 else
271 O << "@l";
272 }
273 }
274 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
275 unsigned CCReg = MI->getOperand(OpNo).getReg();
276 unsigned RegNo = enumRegToMachineReg(CCReg);
277 O << (0x80 >> RegNo);
278 }
279 // The new addressing mode printers.
280 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
281 printSymbolLo(MI, OpNo);
282 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000283 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 MI->getOperand(OpNo+1).getReg() == PPC::R0)
285 O << "0";
286 else
287 printOperand(MI, OpNo+1);
288 O << ')';
289 }
290 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000291 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000293 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 printSymbolLo(MI, OpNo);
295 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000296 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 MI->getOperand(OpNo+1).getReg() == PPC::R0)
298 O << "0";
299 else
300 printOperand(MI, OpNo+1);
301 O << ')';
302 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000303
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
305 // When used as the base register, r0 reads constant zero rather than
306 // the value contained in the register. For this reason, the darwin
307 // assembler requires that we print r0 as 0 (no r) when used as the base.
308 const MachineOperand &MO = MI->getOperand(OpNo);
309 printRegister(MO, true);
310 O << ", ";
311 printOperand(MI, OpNo+1);
312 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000313
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000314 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
315 const MachineOperand &MO = MI->getOperand(OpNo);
316
317 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
318
319 GlobalValue *GV = MO.getGlobal();
320
321 std::string Name = Mang->getMangledName(GV);
322
323 // Map symbol -> label of TOC entry.
324 if (TOC.count(Name) == 0) {
325 std::string Label;
326 Label += TAI->getPrivateGlobalPrefix();
327 Label += "C";
328 Label += utostr(LabelID++);
329
330 TOC[Name] = Label;
331 }
332
333 O << TOC[Name] << "@toc";
334 }
335
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000336 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340
341 virtual void EmitExternalGlobal(const GlobalVariable *GV);
342 };
343
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000344 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Bill Wendling4f405312009-02-24 08:30:20 +0000345 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000346 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000347 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000348 const TargetAsmInfo *T, bool V)
349 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350
351 virtual const char *getPassName() const {
352 return "Linux PPC Assembly Printer";
353 }
354
355 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000356 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000357
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 void getAnalysisUsage(AnalysisUsage &AU) const {
359 AU.setPreservesAll();
360 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000361 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 PPCAsmPrinter::getAnalysisUsage(AU);
363 }
364
Chris Lattnerae982212009-07-21 18:38:57 +0000365 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 };
367
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000368 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
369 /// OS X
Bill Wendling4f405312009-02-24 08:30:20 +0000370 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000371 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000372 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000373 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000374 const TargetAsmInfo *T, bool V)
375 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
377 virtual const char *getPassName() const {
378 return "Darwin PPC Assembly Printer";
379 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000380
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 bool runOnMachineFunction(MachineFunction &F);
382 bool doInitialization(Module &M);
383 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000384
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 void getAnalysisUsage(AnalysisUsage &AU) const {
386 AU.setPreservesAll();
387 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000388 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 PPCAsmPrinter::getAnalysisUsage(AU);
390 }
391
Chris Lattnerae982212009-07-21 18:38:57 +0000392 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 };
394} // end of anonymous namespace
395
396// Include the auto-generated portion of the assembly writer
397#include "PPCGenAsmWriter.inc"
398
399void PPCAsmPrinter::printOp(const MachineOperand &MO) {
400 switch (MO.getType()) {
401 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000402 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403
404 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000405 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 return;
407 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000408 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000409 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 // FIXME: PIC relocation model
411 return;
412 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000413 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000414 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000416 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 // Computing the address of an external symbol, not calling it.
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000418 std::string Name(TAI->getGlobalPrefix());
419 Name += MO.getSymbolName();
420
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000422 GVStubs[Name] = Name+"$non_lazy_ptr";
423 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000425 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000427 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 case MachineOperand::MO_GlobalAddress: {
429 // Computing the address of a global symbol, not calling it.
430 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000431 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432
433 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000434 if (TM.getRelocationModel() != Reloc::Static &&
435 (GV->isDeclaration() || GV->isWeakForLinker())) {
436 if (!GV->hasHiddenVisibility()) {
437 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
438 GVStubs[Mang->getMangledName(GV)] = Name;
439 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
440 GV->hasAvailableExternallyLinkage()) {
441 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
442 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
443 } else {
444 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000446 } else {
447 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 }
449 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000450
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000451 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 return;
453 }
454
455 default:
456 O << "<unknown operand type: " << MO.getType() << ">";
457 return;
458 }
459}
460
461/// EmitExternalGlobal - In this case we need to use the indirect symbol.
462///
463void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000464 std::string Name;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000465
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000467 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000468 } else {
469 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 }
471 O << Name;
472}
473
474/// PrintAsmOperand - Print out an operand for an inline asm expression.
475///
476bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000477 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 const char *ExtraCode) {
479 // Does this asm operand have a single letter operand modifier?
480 if (ExtraCode && ExtraCode[0]) {
481 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000482
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 switch (ExtraCode[0]) {
484 default: return true; // Unknown modifier.
485 case 'c': // Don't print "$" before a global var name or constant.
486 // PPC never has a prefix.
487 printOperand(MI, OpNo);
488 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000489 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000491 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000493 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 return true;
495 ++OpNo; // Return the high-part.
496 break;
497 case 'I':
498 // Write 'i' if an integer constant, otherwise nothing. Used to print
499 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000500 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 O << "i";
502 return false;
503 }
504 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000505
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 printOperand(MI, OpNo);
507 return false;
508}
509
510bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000511 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 const char *ExtraCode) {
513 if (ExtraCode && ExtraCode[0])
514 return true; // Unknown modifier.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000515 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 printMemRegReg(MI, OpNo);
517 else
518 printMemRegImm(MI, OpNo);
519 return false;
520}
521
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000522void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 const char *Modifier) {
524 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
525 unsigned Code = MI->getOperand(OpNo).getImm();
526 if (!strcmp(Modifier, "cc")) {
527 switch ((PPC::Predicate)Code) {
528 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
529 case PPC::PRED_LT: O << "lt"; return;
530 case PPC::PRED_LE: O << "le"; return;
531 case PPC::PRED_EQ: O << "eq"; return;
532 case PPC::PRED_GE: O << "ge"; return;
533 case PPC::PRED_GT: O << "gt"; return;
534 case PPC::PRED_NE: O << "ne"; return;
535 case PPC::PRED_UN: O << "un"; return;
536 case PPC::PRED_NU: O << "nu"; return;
537 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000538
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 } else {
540 assert(!strcmp(Modifier, "reg") &&
541 "Need to specify 'cc' or 'reg' as predicate op modifier!");
542 // Don't print the register for 'always'.
543 if (Code == PPC::PRED_ALWAYS) return;
544 printOperand(MI, OpNo+1);
545 }
546}
547
548
549/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
550/// the current output stream.
551///
552void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
553 ++EmittedInsts;
554
555 // Check for slwi/srwi mnemonics.
556 if (MI->getOpcode() == PPC::RLWINM) {
557 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000558 unsigned char SH = MI->getOperand(2).getImm();
559 unsigned char MB = MI->getOperand(3).getImm();
560 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000562 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 }
564 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000565 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 SH = 32-SH;
567 }
568 if (FoundMnemonic) {
569 printOperand(MI, 0);
570 O << ", ";
571 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000572 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 return;
574 }
575 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
576 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000577 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 printOperand(MI, 0);
579 O << ", ";
580 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000581 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 return;
583 }
584 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000585 unsigned char SH = MI->getOperand(2).getImm();
586 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
588 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000589 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 printOperand(MI, 0);
591 O << ", ";
592 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000593 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 return;
595 }
596 }
597
Chris Lattner7d337492009-08-08 00:05:42 +0000598 printInstruction(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599}
600
601/// runOnMachineFunction - This uses the printMachineInstruction()
602/// method to print assembly for each instruction.
603///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000604bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000605 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606
607 SetupMachineFunction(MF);
608 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000609
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 // Print out constants referenced by the function
611 EmitConstantPool(MF.getConstantPool());
612
613 // Print out labels for the function.
614 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000615 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000616
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000618 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000619 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000620 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 case Function::InternalLinkage: // Symbols default to internal.
622 break;
623 case Function::ExternalLinkage:
624 O << "\t.global\t" << CurrentFnName << '\n'
625 << "\t.type\t" << CurrentFnName << ", @function\n";
626 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000627 case Function::WeakAnyLinkage:
628 case Function::WeakODRLinkage:
629 case Function::LinkOnceAnyLinkage:
630 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 O << "\t.global\t" << CurrentFnName << '\n';
632 O << "\t.weak\t" << CurrentFnName << '\n';
633 break;
634 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000635
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000636 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000637
Bill Wendling25a8ae32009-06-30 22:38:32 +0000638 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000639
640 if (Subtarget.isPPC64()) {
641 // Emit an official procedure descriptor.
642 // FIXME 64-bit SVR4: Use MCSection here?
643 O << "\t.section\t\".opd\",\"aw\"\n";
644 O << "\t.align 3\n";
645 O << CurrentFnName << ":\n";
646 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
647 O << "\t.previous\n";
648 O << ".L." << CurrentFnName << ":\n";
649 } else {
650 O << CurrentFnName << ":\n";
651 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000652
653 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000654 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655
656 // Print out code for the function.
657 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
658 I != E; ++I) {
659 // Print a label for the basic block.
660 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000661 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 O << '\n';
663 }
664 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
665 II != E; ++II) {
666 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667 printMachineInstruction(II);
668 }
669 }
670
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000671 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672
673 // Print out jump tables referenced by the function.
674 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000675
Chris Lattner2931fe42009-07-29 05:09:30 +0000676 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000677
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000679 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000680
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681 // We didn't modify anything.
682 return false;
683}
684
Chris Lattnerae982212009-07-21 18:38:57 +0000685void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 const TargetData *TD = TM.getTargetData();
687
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000688 if (!GVar->hasInitializer())
689 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000690
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000691 // Check to see if this is a special global used by LLVM, if so, emit it.
692 if (EmitSpecialLLVMGlobal(GVar))
693 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000695 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000697 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000698
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000699 Constant *C = GVar->getInitializer();
700 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000701 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000702 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703
Chris Lattner2931fe42009-07-29 05:09:30 +0000704 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000705
706 if (C->isNullValue() && /* FIXME: Verify correct */
707 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000708 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000709 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000711
712 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 O << "\t.global " << name << '\n';
714 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000715 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000716 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000717 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000718 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000720 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 }
Evan Cheng11db8142009-03-24 00:17:40 +0000722 if (VerboseAsm) {
723 O << "\t\t" << TAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000724 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000725 O << "'";
726 }
727 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000728 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 }
730
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000731 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000732 case GlobalValue::LinkOnceAnyLinkage:
733 case GlobalValue::LinkOnceODRLinkage:
734 case GlobalValue::WeakAnyLinkage:
735 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000736 case GlobalValue::CommonLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000737 O << "\t.global " << name << '\n'
738 << "\t.type " << name << ", @object\n"
739 << "\t.weak " << name << '\n';
740 break;
741 case GlobalValue::AppendingLinkage:
742 // FIXME: appending linkage variables should go into a section of
743 // their name or something. For now, just emit them as external.
744 case GlobalValue::ExternalLinkage:
745 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000746 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000747 << "\t.type " << name << ", @object\n";
748 // FALL THROUGH
749 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000750 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000751 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000752 break;
753 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000754 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000755 }
756
757 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000758 O << name << ":";
759 if (VerboseAsm) {
760 O << "\t\t\t\t" << TAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000761 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000762 O << "'";
763 }
764 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000765
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000766 EmitGlobalConstant(C);
767 O << '\n';
768}
769
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000770bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
771 const TargetData *TD = TM.getTargetData();
772
773 bool isPPC64 = TD->getPointerSizeInBits() == 64;
774
775 if (isPPC64 && !TOC.empty()) {
776 // FIXME 64-bit SVR4: Use MCSection here?
777 O << "\t.section\t\".toc\",\"aw\"\n";
778
779 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
780 I != E; ++I) {
781 O << I->second << ":\n";
782 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
783 }
784 }
785
786 return AsmPrinter::doFinalization(M);
787}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789/// runOnMachineFunction - This uses the printMachineInstruction()
790/// method to print assembly for each instruction.
791///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000792bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000793 this->MF = &MF;
794
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795 SetupMachineFunction(MF);
796 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000797
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 // Print out constants referenced by the function
799 EmitConstantPool(MF.getConstantPool());
800
801 // Print out labels for the function.
802 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000803 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000804
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000806 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000807 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000808 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 case Function::InternalLinkage: // Symbols default to internal.
810 break;
811 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000812 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000814 case Function::WeakAnyLinkage:
815 case Function::WeakODRLinkage:
816 case Function::LinkOnceAnyLinkage:
817 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000818 O << "\t.globl\t" << CurrentFnName << '\n';
819 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 break;
821 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000822
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000823 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000824
Bill Wendling25a8ae32009-06-30 22:38:32 +0000825 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 O << CurrentFnName << ":\n";
827
828 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000829 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830
Bill Wendling36ccaea2008-01-26 06:51:24 +0000831 // If the function is empty, then we need to emit *something*. Otherwise, the
832 // function's label might be associated with something that it wasn't meant to
833 // be associated with. We emit a noop in this situation.
834 MachineFunction::iterator I = MF.begin();
835
Bill Wendlingb5880a72008-01-26 09:03:52 +0000836 if (++I == MF.end() && MF.front().empty())
837 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000838
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839 // Print out code for the function.
840 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
841 I != E; ++I) {
842 // Print a label for the basic block.
843 if (I != MF.begin()) {
Evan Cheng11db8142009-03-24 00:17:40 +0000844 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 O << '\n';
846 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000847 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
848 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 printMachineInstruction(II);
851 }
852 }
853
854 // Print out jump tables referenced by the function.
855 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000856
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000858 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000859
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 // We didn't modify anything.
861 return false;
862}
863
864
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000865bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000866 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000867 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868 "ppc",
869 "ppc601",
870 "ppc602",
871 "ppc603",
872 "ppc7400",
873 "ppc750",
874 "ppc970",
875 "ppc64"
876 };
877
878 unsigned Directive = Subtarget.getDarwinDirective();
879 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
880 Directive = PPC::DIR_970;
881 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
882 Directive = PPC::DIR_7400;
883 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
884 Directive = PPC::DIR_64;
885 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000886 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000887
Dan Gohman4a558a32007-07-25 19:33:14 +0000888 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000889 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000890
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 // Prime text sections so they are adjacent. This reduces the likelihood a
892 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000893 TargetLoweringObjectFileMachO &TLOFMacho =
894 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner72a676a2009-08-10 01:39:42 +0000895 SwitchToSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner72a676a2009-08-10 01:39:42 +0000897 SwitchToSection(TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
898 MCSectionMachO::S_SYMBOL_STUBS |
899 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
900 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner72a676a2009-08-10 01:39:42 +0000902 SwitchToSection(TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
903 MCSectionMachO::S_SYMBOL_STUBS |
904 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
905 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 }
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000907 SwitchToSection(getObjFileLowering().getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000908
Dan Gohman4a558a32007-07-25 19:33:14 +0000909 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910}
911
Chris Lattnerae982212009-07-21 18:38:57 +0000912void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000913 const TargetData *TD = TM.getTargetData();
914
915 if (!GVar->hasInitializer())
916 return; // External global require no code
917
918 // Check to see if this is a special global used by LLVM, if so, emit it.
919 if (EmitSpecialLLVMGlobal(GVar)) {
920 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000921 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000922 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000923 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000924 O << ".reference .destructors_used\n";
925 }
926 return;
927 }
928
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000929 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000930 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000931
932 Constant *C = GVar->getInitializer();
933 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000934 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000935 unsigned Align = TD->getPreferredAlignmentLog(GVar);
936
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000937 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000938 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000939 SwitchToSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000940
Chris Lattnerdb727932009-08-04 05:35:56 +0000941 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000942 if (C->isNullValue() && /* FIXME: Verify correct */
943 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000944 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000945 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000946 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000947 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000948 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
949
950 if (GVar->hasExternalLinkage()) {
951 O << "\t.globl " << name << '\n';
952 O << "\t.zerofill __DATA, __common, " << name << ", "
953 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000954 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000955 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000956 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000957 O << "\t.globl " << name << '\n'
958 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000959 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000960 O << name << ":";
961 if (VerboseAsm) {
962 O << "\t\t\t\t" << TAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000963 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000964 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000965 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000966 EmitGlobalConstant(C);
967 return;
968 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000969 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000970 // Darwin 9 and above support aligned common data.
971 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000972 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000973 }
Evan Cheng11db8142009-03-24 00:17:40 +0000974 if (VerboseAsm) {
975 O << "\t\t" << TAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000976 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000977 O << "'";
978 }
979 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000980 return;
981 }
982
983 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000984 case GlobalValue::LinkOnceAnyLinkage:
985 case GlobalValue::LinkOnceODRLinkage:
986 case GlobalValue::WeakAnyLinkage:
987 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000988 case GlobalValue::CommonLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000989 O << "\t.globl " << name << '\n'
990 << "\t.weak_definition " << name << '\n';
991 break;
992 case GlobalValue::AppendingLinkage:
993 // FIXME: appending linkage variables should go into a section of
994 // their name or something. For now, just emit them as external.
995 case GlobalValue::ExternalLinkage:
996 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000997 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000998 // FALL THROUGH
999 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +00001000 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +00001001 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001002 break;
1003 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001004 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001005 }
1006
1007 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001008 O << name << ":";
1009 if (VerboseAsm) {
1010 O << "\t\t\t\t" << TAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001011 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001012 O << "'";
1013 }
1014 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001015
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001016 EmitGlobalConstant(C);
1017 O << '\n';
1018}
1019
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001020bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 const TargetData *TD = TM.getTargetData();
1022
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1024
Chris Lattnerf4815552009-08-03 22:52:21 +00001025 // Darwin/PPC always uses mach-o.
1026 TargetLoweringObjectFileMachO &TLOFMacho =
1027 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1028
Chris Lattner72a676a2009-08-10 01:39:42 +00001029
1030 const MCSection *LSPSection = 0;
1031 if (!FnStubs.empty()) // .lazy_symbol_pointer
1032 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1033
1034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001036 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001037 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001038 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1039 MCSectionMachO::S_SYMBOL_STUBS |
1040 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1041 32, SectionKind::getText());
1042 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001043 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001044 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001046 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001047 O << Info.Stub << ":\n";
1048 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001050 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1051 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001052 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001053 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001054 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001056 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1057 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 O << "\tmtctr r12\n";
1059 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001060
Chris Lattnerf4815552009-08-03 22:52:21 +00001061 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001062 O << Info.LazyPtr << ":\n";
1063 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001064 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 }
Chris Lattner189198f2009-07-15 00:55:58 +00001066 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001067 const MCSection *StubSection =
1068 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1069 MCSectionMachO::S_SYMBOL_STUBS |
1070 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1071 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001072
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001073 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001074 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001075 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001077 const FnStubInfo &Info = I->second;
1078 O << Info.Stub << ":\n";
1079 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1080 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001081 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001082 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 O << "\tmtctr r12\n";
1084 O << "\tbctr\n";
Chris Lattnerf4815552009-08-03 22:52:21 +00001085 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001086 O << Info.LazyPtr << ":\n";
1087 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001088 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 }
1090 }
1091
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001092 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093
Dale Johannesen85535762008-04-02 00:25:04 +00001094 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001095 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001096 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001097 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001098 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001099 E = Personalities.end(); I != E; ++I) {
1100 if (*I)
1101 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001102 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001103 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001104 }
1105
Chris Lattnerf4815552009-08-03 22:52:21 +00001106 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001107 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001108 // Switch with ".non_lazy_symbol_pointer" directive.
1109 SwitchToSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001110 EmitAlignment(isPPC64 ? 3 : 2);
1111
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001112 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1113 E = GVStubs.end(); I != E; ++I) {
1114 O << I->second << ":\n";
1115 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1116 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117 }
1118 }
1119
Evan Chenga65854f2008-12-05 01:06:39 +00001120 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001121 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001122 EmitAlignment(isPPC64 ? 3 : 2);
1123 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1124 E = HiddenGVStubs.end(); I != E; ++I) {
1125 O << I->second << ":\n";
1126 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001127 }
1128 }
1129
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001130 // Funny Darwin hack: This flag tells the linker that no global symbols
1131 // contain code that falls through to other global symbols (e.g. the obvious
1132 // implementation of multiple entry points). If this doesn't occur, the
1133 // linker can safely perform dead code stripping. Since LLVM never generates
1134 // code that does this, it is always safe to set.
1135 O << "\t.subsections_via_symbols\n";
1136
Dan Gohman4a558a32007-07-25 19:33:14 +00001137 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001138}
1139
1140
1141
1142/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1143/// for a MachineFunction to the given output stream, in a format that the
1144/// Darwin assembler can deal with.
1145///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001146static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1147 TargetMachine &tm,
1148 const TargetAsmInfo *tai,
1149 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1151
Chris Lattnerae982212009-07-21 18:38:57 +00001152 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001153 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1154 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001156
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001157// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001158extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001159 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001160 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1161}