blob: 5dca6e6ad24b421fdf397207d5d63c6463b90f37 [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
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000510// At the moment, all inline asm memory operands are a single register.
511// In any case, the output of this routine should always be just one
512// assembler operand.
513
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000515 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 const char *ExtraCode) {
517 if (ExtraCode && ExtraCode[0])
518 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000519 assert (MI->getOperand(OpNo).isReg());
520 printOperand(MI, OpNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 return false;
522}
523
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000524void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 const char *Modifier) {
526 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
527 unsigned Code = MI->getOperand(OpNo).getImm();
528 if (!strcmp(Modifier, "cc")) {
529 switch ((PPC::Predicate)Code) {
530 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
531 case PPC::PRED_LT: O << "lt"; return;
532 case PPC::PRED_LE: O << "le"; return;
533 case PPC::PRED_EQ: O << "eq"; return;
534 case PPC::PRED_GE: O << "ge"; return;
535 case PPC::PRED_GT: O << "gt"; return;
536 case PPC::PRED_NE: O << "ne"; return;
537 case PPC::PRED_UN: O << "un"; return;
538 case PPC::PRED_NU: O << "nu"; return;
539 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000540
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 } else {
542 assert(!strcmp(Modifier, "reg") &&
543 "Need to specify 'cc' or 'reg' as predicate op modifier!");
544 // Don't print the register for 'always'.
545 if (Code == PPC::PRED_ALWAYS) return;
546 printOperand(MI, OpNo+1);
547 }
548}
549
550
551/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
552/// the current output stream.
553///
554void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
555 ++EmittedInsts;
556
557 // Check for slwi/srwi mnemonics.
558 if (MI->getOpcode() == PPC::RLWINM) {
559 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000560 unsigned char SH = MI->getOperand(2).getImm();
561 unsigned char MB = MI->getOperand(3).getImm();
562 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000564 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 }
566 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000567 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 SH = 32-SH;
569 }
570 if (FoundMnemonic) {
571 printOperand(MI, 0);
572 O << ", ";
573 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000574 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 return;
576 }
577 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
578 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000579 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 printOperand(MI, 0);
581 O << ", ";
582 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000583 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 return;
585 }
586 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000587 unsigned char SH = MI->getOperand(2).getImm();
588 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
590 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000591 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 printOperand(MI, 0);
593 O << ", ";
594 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000595 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 return;
597 }
598 }
599
Chris Lattner7d337492009-08-08 00:05:42 +0000600 printInstruction(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601}
602
603/// runOnMachineFunction - This uses the printMachineInstruction()
604/// method to print assembly for each instruction.
605///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000606bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000607 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000608
609 SetupMachineFunction(MF);
610 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000611
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 // Print out constants referenced by the function
613 EmitConstantPool(MF.getConstantPool());
614
615 // Print out labels for the function.
616 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000617 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000618
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000620 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000621 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000622 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 case Function::InternalLinkage: // Symbols default to internal.
624 break;
625 case Function::ExternalLinkage:
626 O << "\t.global\t" << CurrentFnName << '\n'
627 << "\t.type\t" << CurrentFnName << ", @function\n";
628 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000629 case Function::WeakAnyLinkage:
630 case Function::WeakODRLinkage:
631 case Function::LinkOnceAnyLinkage:
632 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 O << "\t.global\t" << CurrentFnName << '\n';
634 O << "\t.weak\t" << CurrentFnName << '\n';
635 break;
636 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000637
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000638 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000639
Bill Wendling25a8ae32009-06-30 22:38:32 +0000640 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000641
642 if (Subtarget.isPPC64()) {
643 // Emit an official procedure descriptor.
644 // FIXME 64-bit SVR4: Use MCSection here?
645 O << "\t.section\t\".opd\",\"aw\"\n";
646 O << "\t.align 3\n";
647 O << CurrentFnName << ":\n";
648 O << "\t.quad .L." << CurrentFnName << ",.TOC.@tocbase\n";
649 O << "\t.previous\n";
650 O << ".L." << CurrentFnName << ":\n";
651 } else {
652 O << CurrentFnName << ":\n";
653 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654
655 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000656 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657
658 // Print out code for the function.
659 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
660 I != E; ++I) {
661 // Print a label for the basic block.
662 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000663 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 O << '\n';
665 }
666 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
667 II != E; ++II) {
668 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 printMachineInstruction(II);
670 }
671 }
672
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000673 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674
675 // Print out jump tables referenced by the function.
676 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000677
Chris Lattner2931fe42009-07-29 05:09:30 +0000678 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000679
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000681 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000682
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 // We didn't modify anything.
684 return false;
685}
686
Chris Lattnerae982212009-07-21 18:38:57 +0000687void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 const TargetData *TD = TM.getTargetData();
689
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000690 if (!GVar->hasInitializer())
691 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000692
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000693 // Check to see if this is a special global used by LLVM, if so, emit it.
694 if (EmitSpecialLLVMGlobal(GVar))
695 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000697 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000699 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000700
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000701 Constant *C = GVar->getInitializer();
702 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000703 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000704 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
Chris Lattner2931fe42009-07-29 05:09:30 +0000706 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000707
708 if (C->isNullValue() && /* FIXME: Verify correct */
709 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000710 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000711 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000713
714 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 O << "\t.global " << name << '\n';
716 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000717 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000718 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000719 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000720 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000722 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 }
Evan Cheng11db8142009-03-24 00:17:40 +0000724 if (VerboseAsm) {
725 O << "\t\t" << TAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000726 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000727 O << "'";
728 }
729 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000730 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 }
732
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000733 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000734 case GlobalValue::LinkOnceAnyLinkage:
735 case GlobalValue::LinkOnceODRLinkage:
736 case GlobalValue::WeakAnyLinkage:
737 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000738 case GlobalValue::CommonLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000739 O << "\t.global " << name << '\n'
740 << "\t.type " << name << ", @object\n"
741 << "\t.weak " << name << '\n';
742 break;
743 case GlobalValue::AppendingLinkage:
744 // FIXME: appending linkage variables should go into a section of
745 // their name or something. For now, just emit them as external.
746 case GlobalValue::ExternalLinkage:
747 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000748 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000749 << "\t.type " << name << ", @object\n";
750 // FALL THROUGH
751 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000752 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000753 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000754 break;
755 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000756 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000757 }
758
759 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000760 O << name << ":";
761 if (VerboseAsm) {
762 O << "\t\t\t\t" << TAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000763 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000764 O << "'";
765 }
766 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000767
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000768 EmitGlobalConstant(C);
769 O << '\n';
770}
771
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000772bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
773 const TargetData *TD = TM.getTargetData();
774
775 bool isPPC64 = TD->getPointerSizeInBits() == 64;
776
777 if (isPPC64 && !TOC.empty()) {
778 // FIXME 64-bit SVR4: Use MCSection here?
779 O << "\t.section\t\".toc\",\"aw\"\n";
780
781 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
782 I != E; ++I) {
783 O << I->second << ":\n";
784 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
785 }
786 }
787
788 return AsmPrinter::doFinalization(M);
789}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791/// runOnMachineFunction - This uses the printMachineInstruction()
792/// method to print assembly for each instruction.
793///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000794bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000795 this->MF = &MF;
796
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 SetupMachineFunction(MF);
798 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000799
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 // Print out constants referenced by the function
801 EmitConstantPool(MF.getConstantPool());
802
803 // Print out labels for the function.
804 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000805 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000806
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000808 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000809 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000810 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 case Function::InternalLinkage: // Symbols default to internal.
812 break;
813 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000814 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000816 case Function::WeakAnyLinkage:
817 case Function::WeakODRLinkage:
818 case Function::LinkOnceAnyLinkage:
819 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000820 O << "\t.globl\t" << CurrentFnName << '\n';
821 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 break;
823 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000824
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000825 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000826
Bill Wendling25a8ae32009-06-30 22:38:32 +0000827 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 O << CurrentFnName << ":\n";
829
830 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000831 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832
Bill Wendling36ccaea2008-01-26 06:51:24 +0000833 // If the function is empty, then we need to emit *something*. Otherwise, the
834 // function's label might be associated with something that it wasn't meant to
835 // be associated with. We emit a noop in this situation.
836 MachineFunction::iterator I = MF.begin();
837
Bill Wendlingb5880a72008-01-26 09:03:52 +0000838 if (++I == MF.end() && MF.front().empty())
839 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000840
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 // Print out code for the function.
842 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
843 I != E; ++I) {
844 // Print a label for the basic block.
845 if (I != MF.begin()) {
Evan Cheng11db8142009-03-24 00:17:40 +0000846 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 O << '\n';
848 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000849 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
850 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 printMachineInstruction(II);
853 }
854 }
855
856 // Print out jump tables referenced by the function.
857 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000858
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000860 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000861
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 // We didn't modify anything.
863 return false;
864}
865
866
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000867bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000868 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000869 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 "ppc",
871 "ppc601",
872 "ppc602",
873 "ppc603",
874 "ppc7400",
875 "ppc750",
876 "ppc970",
877 "ppc64"
878 };
879
880 unsigned Directive = Subtarget.getDarwinDirective();
881 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
882 Directive = PPC::DIR_970;
883 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
884 Directive = PPC::DIR_7400;
885 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
886 Directive = PPC::DIR_64;
887 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000888 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000889
Dan Gohman4a558a32007-07-25 19:33:14 +0000890 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000891 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000892
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 // Prime text sections so they are adjacent. This reduces the likelihood a
894 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000895 TargetLoweringObjectFileMachO &TLOFMacho =
896 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner72a676a2009-08-10 01:39:42 +0000897 SwitchToSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner72a676a2009-08-10 01:39:42 +0000899 SwitchToSection(TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
900 MCSectionMachO::S_SYMBOL_STUBS |
901 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
902 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner72a676a2009-08-10 01:39:42 +0000904 SwitchToSection(TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
905 MCSectionMachO::S_SYMBOL_STUBS |
906 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
907 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 }
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000909 SwitchToSection(getObjFileLowering().getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000910
Dan Gohman4a558a32007-07-25 19:33:14 +0000911 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912}
913
Chris Lattnerae982212009-07-21 18:38:57 +0000914void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000915 const TargetData *TD = TM.getTargetData();
916
917 if (!GVar->hasInitializer())
918 return; // External global require no code
919
920 // Check to see if this is a special global used by LLVM, if so, emit it.
921 if (EmitSpecialLLVMGlobal(GVar)) {
922 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000923 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000924 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000925 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000926 O << ".reference .destructors_used\n";
927 }
928 return;
929 }
930
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000931 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000932 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000933
934 Constant *C = GVar->getInitializer();
935 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000936 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000937 unsigned Align = TD->getPreferredAlignmentLog(GVar);
938
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000939 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000940 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000941 SwitchToSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000942
Chris Lattnerdb727932009-08-04 05:35:56 +0000943 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000944 if (C->isNullValue() && /* FIXME: Verify correct */
945 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000946 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000947 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000948 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000949 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000950 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
951
952 if (GVar->hasExternalLinkage()) {
953 O << "\t.globl " << name << '\n';
954 O << "\t.zerofill __DATA, __common, " << name << ", "
955 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000956 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000957 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000958 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000959 O << "\t.globl " << name << '\n'
960 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000961 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000962 O << name << ":";
963 if (VerboseAsm) {
964 O << "\t\t\t\t" << TAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000965 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000966 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000967 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000968 EmitGlobalConstant(C);
969 return;
970 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000971 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000972 // Darwin 9 and above support aligned common data.
973 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000974 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000975 }
Evan Cheng11db8142009-03-24 00:17:40 +0000976 if (VerboseAsm) {
977 O << "\t\t" << TAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000978 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000979 O << "'";
980 }
981 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000982 return;
983 }
984
985 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000986 case GlobalValue::LinkOnceAnyLinkage:
987 case GlobalValue::LinkOnceODRLinkage:
988 case GlobalValue::WeakAnyLinkage:
989 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000990 case GlobalValue::CommonLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000991 O << "\t.globl " << name << '\n'
992 << "\t.weak_definition " << name << '\n';
993 break;
994 case GlobalValue::AppendingLinkage:
995 // FIXME: appending linkage variables should go into a section of
996 // their name or something. For now, just emit them as external.
997 case GlobalValue::ExternalLinkage:
998 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000999 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001000 // FALL THROUGH
1001 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +00001002 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +00001003 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001004 break;
1005 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001006 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001007 }
1008
1009 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +00001010 O << name << ":";
1011 if (VerboseAsm) {
1012 O << "\t\t\t\t" << TAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001013 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001014 O << "'";
1015 }
1016 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001017
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001018 EmitGlobalConstant(C);
1019 O << '\n';
1020}
1021
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001022bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 const TargetData *TD = TM.getTargetData();
1024
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1026
Chris Lattnerf4815552009-08-03 22:52:21 +00001027 // Darwin/PPC always uses mach-o.
1028 TargetLoweringObjectFileMachO &TLOFMacho =
1029 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1030
Chris Lattner72a676a2009-08-10 01:39:42 +00001031
1032 const MCSection *LSPSection = 0;
1033 if (!FnStubs.empty()) // .lazy_symbol_pointer
1034 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1035
1036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001038 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001039 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001040 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1041 MCSectionMachO::S_SYMBOL_STUBS |
1042 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1043 32, SectionKind::getText());
1044 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001045 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001046 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001048 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001049 O << Info.Stub << ":\n";
1050 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001052 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1053 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001055 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001056 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001058 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1059 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 O << "\tmtctr r12\n";
1061 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001062
Chris Lattnerf4815552009-08-03 22:52:21 +00001063 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001064 O << Info.LazyPtr << ":\n";
1065 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001066 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 }
Chris Lattner189198f2009-07-15 00:55:58 +00001068 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001069 const MCSection *StubSection =
1070 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1071 MCSectionMachO::S_SYMBOL_STUBS |
1072 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1073 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001074
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001075 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001076 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001077 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001079 const FnStubInfo &Info = I->second;
1080 O << Info.Stub << ":\n";
1081 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1082 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001083 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001084 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085 O << "\tmtctr r12\n";
1086 O << "\tbctr\n";
Chris Lattnerf4815552009-08-03 22:52:21 +00001087 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001088 O << Info.LazyPtr << ":\n";
1089 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001090 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001091 }
1092 }
1093
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001094 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095
Dale Johannesen85535762008-04-02 00:25:04 +00001096 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001097 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001098 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001099 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001100 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001101 E = Personalities.end(); I != E; ++I) {
1102 if (*I)
1103 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001104 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001105 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001106 }
1107
Chris Lattnerf4815552009-08-03 22:52:21 +00001108 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001109 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001110 // Switch with ".non_lazy_symbol_pointer" directive.
1111 SwitchToSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001112 EmitAlignment(isPPC64 ? 3 : 2);
1113
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001114 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1115 E = GVStubs.end(); I != E; ++I) {
1116 O << I->second << ":\n";
1117 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1118 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119 }
1120 }
1121
Evan Chenga65854f2008-12-05 01:06:39 +00001122 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001123 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001124 EmitAlignment(isPPC64 ? 3 : 2);
1125 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1126 E = HiddenGVStubs.end(); I != E; ++I) {
1127 O << I->second << ":\n";
1128 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001129 }
1130 }
1131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001132 // Funny Darwin hack: This flag tells the linker that no global symbols
1133 // contain code that falls through to other global symbols (e.g. the obvious
1134 // implementation of multiple entry points). If this doesn't occur, the
1135 // linker can safely perform dead code stripping. Since LLVM never generates
1136 // code that does this, it is always safe to set.
1137 O << "\t.subsections_via_symbols\n";
1138
Dan Gohman4a558a32007-07-25 19:33:14 +00001139 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140}
1141
1142
1143
1144/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1145/// for a MachineFunction to the given output stream, in a format that the
1146/// Darwin assembler can deal with.
1147///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001148static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1149 TargetMachine &tm,
1150 const TargetAsmInfo *tai,
1151 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1153
Chris Lattnerae982212009-07-21 18:38:57 +00001154 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001155 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1156 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001157}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001158
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001159// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001160extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001161 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001162 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1163}