blob: 44b0d0c6a3537235894d580dbc67a2cc0e8bdcf6 [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"
Devang Patelf667ab42009-06-25 00:47:42 +000027#include "llvm/MDNode.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Assembly/Writer.h"
29#include "llvm/CodeGen/AsmPrinter.h"
30#include "llvm/CodeGen/DwarfWriter.h"
31#include "llvm/CodeGen/MachineModuleInfo.h"
32#include "llvm/CodeGen/MachineFunctionPass.h"
33#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling36ccaea2008-01-26 06:51:24 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/Mangler.h"
36#include "llvm/Support/MathExtras.h"
37#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000039#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000041#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/Target/TargetAsmInfo.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000043#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetOptions.h"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000046#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047#include "llvm/ADT/Statistic.h"
48#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000049#include "llvm/ADT/StringSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050using namespace llvm;
51
52STATISTIC(EmittedInsts, "Number of machine instrs printed");
53
54namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000055 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
56 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000057 struct FnStubInfo {
58 std::string Stub, LazyPtr, AnonSymbol;
59
60 FnStubInfo() {}
61
62 void Init(const GlobalValue *GV, Mangler *Mang) {
63 // Already initialized.
64 if (!Stub.empty()) return;
65 Stub = Mang->getMangledName(GV, "$stub", true);
66 LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true);
67 AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true);
68 }
69
70 void Init(const std::string &GV, Mangler *Mang) {
71 // Already initialized.
72 if (!Stub.empty()) return;
73 Stub = Mang->makeNameProper(GV+"$stub", true);
74 LazyPtr = Mang->makeNameProper(GV+"$lazy_ptr", true);
75 AnonSymbol = Mang->makeNameProper(GV+"$stub$tmp", true);
76 }
77 };
78
79 StringMap<FnStubInfo> FnStubs;
Chris Lattner83cf1ec2009-07-15 01:14:44 +000080 StringMap<std::string> GVStubs, HiddenGVStubs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 const PPCSubtarget &Subtarget;
Bill Wendling4f405312009-02-24 08:30:20 +000082 public:
David Greene302008d2009-07-14 20:18:05 +000083 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000084 const TargetAsmInfo *T, bool V)
85 : AsmPrinter(O, TM, T, V),
Bill Wendling4f405312009-02-24 08:30:20 +000086 Subtarget(TM.getSubtarget<PPCSubtarget>()) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
88 virtual const char *getPassName() const {
89 return "PowerPC Assembly Printer";
90 }
91
92 PPCTargetMachine &getTM() {
93 return static_cast<PPCTargetMachine&>(TM);
94 }
95
96 unsigned enumRegToMachineReg(unsigned enumReg) {
97 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +000098 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 case PPC::CR0: return 0;
100 case PPC::CR1: return 1;
101 case PPC::CR2: return 2;
102 case PPC::CR3: return 3;
103 case PPC::CR4: return 4;
104 case PPC::CR5: return 5;
105 case PPC::CR6: return 6;
106 case PPC::CR7: return 7;
107 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000108 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 }
110
111 /// printInstruction - This method is automatically generated by tablegen
112 /// from the instruction set description. This method returns true if the
113 /// machine instruction was sufficiently described to print it, otherwise it
114 /// returns false.
115 bool printInstruction(const MachineInstr *MI);
116
117 void printMachineInstruction(const MachineInstr *MI);
118 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 /// stripRegisterPrefix - This method strips the character prefix from a
121 /// register name so that only the number is left. Used by for linux asm.
122 const char *stripRegisterPrefix(const char *RegName) {
123 switch (RegName[0]) {
124 case 'r':
125 case 'f':
126 case 'v': return RegName + 1;
127 case 'c': if (RegName[1] == 'r') return RegName + 2;
128 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 return RegName;
131 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000132
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 /// printRegister - Print register according to target requirements.
134 ///
135 void printRegister(const MachineOperand &MO, bool R0AsZero) {
136 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000137 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 // If we should use 0 for R0.
140 if (R0AsZero && RegNo == PPC::R0) {
141 O << "0";
142 return;
143 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000144
Bill Wendling8eeb9792008-02-26 21:11:01 +0000145 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 // Linux assembler (Others?) does not take register mnemonics.
147 // FIXME - What about special registers used in mfspr/mtspr?
148 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
149 O << RegName;
150 }
151
152 void printOperand(const MachineInstr *MI, unsigned OpNo) {
153 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000154 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000156 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000157 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 } else {
159 printOp(MO);
160 }
161 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000162
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
164 unsigned AsmVariant, const char *ExtraCode);
165 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
166 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000167
168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000170 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 value = (value << (32-5)) >> (32-5);
172 O << (int)value;
173 }
174 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000175 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 assert(value <= 31 && "Invalid u5imm argument!");
177 O << (unsigned int)value;
178 }
179 void printU6ImmOperand(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 <= 63 && "Invalid u6imm argument!");
182 O << (unsigned int)value;
183 }
184 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000185 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 }
187 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000188 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000191 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000192 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 } else {
194 O << "lo16(";
195 printOp(MI->getOperand(OpNo));
196 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000197 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 else
199 O << ')';
200 }
201 }
202 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
203 // Branches can take an immediate operand. This is used by the branch
204 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000205 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000206 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 } else {
208 printOp(MI->getOperand(OpNo));
209 }
210 }
211 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
212 const MachineOperand &MO = MI->getOperand(OpNo);
213 if (TM.getRelocationModel() != Reloc::Static) {
214 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
215 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000216 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000218 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
219 FnInfo.Init(GV, Mang);
220 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 return;
222 }
223 }
224 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000225 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
226 FnInfo.Init(MO.getSymbolName(), Mang);
227 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 return;
229 }
230 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 printOp(MI->getOperand(OpNo));
233 }
234 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000235 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 }
237 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000238 O << "\"L" << getFunctionNumber() << "$pb\"\n";
239 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 }
241 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000242 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 printS16ImmOperand(MI, OpNo);
244 } else {
245 if (Subtarget.isDarwin()) O << "ha16(";
246 printOp(MI->getOperand(OpNo));
247 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000248 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 if (Subtarget.isDarwin())
250 O << ')';
251 else
252 O << "@ha";
253 }
254 }
255 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000256 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 printS16ImmOperand(MI, OpNo);
258 } else {
259 if (Subtarget.isDarwin()) O << "lo16(";
260 printOp(MI->getOperand(OpNo));
261 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000262 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 if (Subtarget.isDarwin())
264 O << ')';
265 else
266 O << "@l";
267 }
268 }
269 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
270 unsigned CCReg = MI->getOperand(OpNo).getReg();
271 unsigned RegNo = enumRegToMachineReg(CCReg);
272 O << (0x80 >> RegNo);
273 }
274 // The new addressing mode printers.
275 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
276 printSymbolLo(MI, OpNo);
277 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000278 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 MI->getOperand(OpNo+1).getReg() == PPC::R0)
280 O << "0";
281 else
282 printOperand(MI, OpNo+1);
283 O << ')';
284 }
285 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000286 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000288 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 printSymbolLo(MI, OpNo);
290 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000291 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 MI->getOperand(OpNo+1).getReg() == PPC::R0)
293 O << "0";
294 else
295 printOperand(MI, OpNo+1);
296 O << ')';
297 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000298
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
300 // When used as the base register, r0 reads constant zero rather than
301 // the value contained in the register. For this reason, the darwin
302 // assembler requires that we print r0 as 0 (no r) when used as the base.
303 const MachineOperand &MO = MI->getOperand(OpNo);
304 printRegister(MO, true);
305 O << ", ";
306 printOperand(MI, OpNo+1);
307 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000308
309 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000311
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
313 virtual bool doFinalization(Module &M) = 0;
314
315 virtual void EmitExternalGlobal(const GlobalVariable *GV);
316 };
317
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000318 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Bill Wendling4f405312009-02-24 08:30:20 +0000319 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000320 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000321 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000322 const TargetAsmInfo *T, bool V)
323 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 virtual const char *getPassName() const {
326 return "Linux PPC Assembly Printer";
327 }
328
329 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000331
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 void getAnalysisUsage(AnalysisUsage &AU) const {
333 AU.setPreservesAll();
334 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000335 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 PPCAsmPrinter::getAnalysisUsage(AU);
337 }
338
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000339 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 };
341
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000342 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
343 /// OS X
Bill Wendling4f405312009-02-24 08:30:20 +0000344 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000345 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000346 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000347 explicit PPCDarwinAsmPrinter(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), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350
351 virtual const char *getPassName() const {
352 return "Darwin PPC Assembly Printer";
353 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000354
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 bool runOnMachineFunction(MachineFunction &F);
356 bool doInitialization(Module &M);
357 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000358
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 void getAnalysisUsage(AnalysisUsage &AU) const {
360 AU.setPreservesAll();
361 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000362 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 PPCAsmPrinter::getAnalysisUsage(AU);
364 }
365
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000366 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 };
368} // end of anonymous namespace
369
370// Include the auto-generated portion of the assembly writer
371#include "PPCGenAsmWriter.inc"
372
373void PPCAsmPrinter::printOp(const MachineOperand &MO) {
374 switch (MO.getType()) {
375 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000376 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377
378 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000379 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 return;
381 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000382 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000383 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 // FIXME: PIC relocation model
385 return;
386 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000387 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000388 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000390 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 // Computing the address of an external symbol, not calling it.
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000392 std::string Name(TAI->getGlobalPrefix());
393 Name += MO.getSymbolName();
394
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000396 GVStubs[Name] = Name+"$non_lazy_ptr";
397 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000399 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000401 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 case MachineOperand::MO_GlobalAddress: {
403 // Computing the address of a global symbol, not calling it.
404 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000405 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406
407 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000408 if (TM.getRelocationModel() != Reloc::Static &&
409 (GV->isDeclaration() || GV->isWeakForLinker())) {
410 if (!GV->hasHiddenVisibility()) {
411 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
412 GVStubs[Mang->getMangledName(GV)] = Name;
413 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
414 GV->hasAvailableExternallyLinkage()) {
415 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
416 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
417 } else {
418 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000420 } else {
421 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 }
423 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000424
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000425 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 return;
427 }
428
429 default:
430 O << "<unknown operand type: " << MO.getType() << ">";
431 return;
432 }
433}
434
435/// EmitExternalGlobal - In this case we need to use the indirect symbol.
436///
437void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000438 std::string Name;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000439
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000441 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000442 } else {
443 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 }
445 O << Name;
446}
447
448/// PrintAsmOperand - Print out an operand for an inline asm expression.
449///
450bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000451 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 const char *ExtraCode) {
453 // Does this asm operand have a single letter operand modifier?
454 if (ExtraCode && ExtraCode[0]) {
455 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000456
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 switch (ExtraCode[0]) {
458 default: return true; // Unknown modifier.
459 case 'c': // Don't print "$" before a global var name or constant.
460 // PPC never has a prefix.
461 printOperand(MI, OpNo);
462 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000463 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000465 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000467 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 return true;
469 ++OpNo; // Return the high-part.
470 break;
471 case 'I':
472 // Write 'i' if an integer constant, otherwise nothing. Used to print
473 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000474 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 O << "i";
476 return false;
477 }
478 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000479
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 printOperand(MI, OpNo);
481 return false;
482}
483
484bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000485 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 const char *ExtraCode) {
487 if (ExtraCode && ExtraCode[0])
488 return true; // Unknown modifier.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000489 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 printMemRegReg(MI, OpNo);
491 else
492 printMemRegImm(MI, OpNo);
493 return false;
494}
495
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000496void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 const char *Modifier) {
498 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
499 unsigned Code = MI->getOperand(OpNo).getImm();
500 if (!strcmp(Modifier, "cc")) {
501 switch ((PPC::Predicate)Code) {
502 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
503 case PPC::PRED_LT: O << "lt"; return;
504 case PPC::PRED_LE: O << "le"; return;
505 case PPC::PRED_EQ: O << "eq"; return;
506 case PPC::PRED_GE: O << "ge"; return;
507 case PPC::PRED_GT: O << "gt"; return;
508 case PPC::PRED_NE: O << "ne"; return;
509 case PPC::PRED_UN: O << "un"; return;
510 case PPC::PRED_NU: O << "nu"; return;
511 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000512
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 } else {
514 assert(!strcmp(Modifier, "reg") &&
515 "Need to specify 'cc' or 'reg' as predicate op modifier!");
516 // Don't print the register for 'always'.
517 if (Code == PPC::PRED_ALWAYS) return;
518 printOperand(MI, OpNo+1);
519 }
520}
521
522
523/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
524/// the current output stream.
525///
526void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
527 ++EmittedInsts;
528
529 // Check for slwi/srwi mnemonics.
530 if (MI->getOpcode() == PPC::RLWINM) {
531 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000532 unsigned char SH = MI->getOperand(2).getImm();
533 unsigned char MB = MI->getOperand(3).getImm();
534 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000536 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 }
538 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000539 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 SH = 32-SH;
541 }
542 if (FoundMnemonic) {
543 printOperand(MI, 0);
544 O << ", ";
545 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000546 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 return;
548 }
549 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
550 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000551 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 printOperand(MI, 0);
553 O << ", ";
554 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000555 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 return;
557 }
558 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000559 unsigned char SH = MI->getOperand(2).getImm();
560 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
562 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000563 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 printOperand(MI, 0);
565 O << ", ";
566 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000567 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 return;
569 }
570 }
571
572 if (printInstruction(MI))
573 return; // Printer was automatically generated
574
Edwin Törökbd448e32009-07-14 16:55:14 +0000575 llvm_unreachable("Unhandled instruction in asm writer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576}
577
578/// runOnMachineFunction - This uses the printMachineInstruction()
579/// method to print assembly for each instruction.
580///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000581bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000582 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583
584 SetupMachineFunction(MF);
585 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000586
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 // Print out constants referenced by the function
588 EmitConstantPool(MF.getConstantPool());
589
590 // Print out labels for the function.
591 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000592 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000593
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000595 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000596 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 case Function::InternalLinkage: // Symbols default to internal.
598 break;
599 case Function::ExternalLinkage:
600 O << "\t.global\t" << CurrentFnName << '\n'
601 << "\t.type\t" << CurrentFnName << ", @function\n";
602 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000603 case Function::WeakAnyLinkage:
604 case Function::WeakODRLinkage:
605 case Function::LinkOnceAnyLinkage:
606 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 O << "\t.global\t" << CurrentFnName << '\n';
608 O << "\t.weak\t" << CurrentFnName << '\n';
609 break;
610 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000611
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000612 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000613
Bill Wendling25a8ae32009-06-30 22:38:32 +0000614 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 O << CurrentFnName << ":\n";
616
617 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000618 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619
620 // Print out code for the function.
621 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
622 I != E; ++I) {
623 // Print a label for the basic block.
624 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000625 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 O << '\n';
627 }
628 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
629 II != E; ++II) {
630 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 printMachineInstruction(II);
632 }
633 }
634
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000635 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636
637 // Print out jump tables referenced by the function.
638 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000639
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000640 SwitchToSection(TAI->SectionForGlobal(F));
641
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000643 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000644
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000645 O.flush();
646
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 // We didn't modify anything.
648 return false;
649}
650
Chris Lattner2b638a72008-02-15 19:04:54 +0000651/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +0000652/// Don't print things like \\n or \\0.
David Greene302008d2009-07-14 20:18:05 +0000653static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
Chris Lattner2b638a72008-02-15 19:04:54 +0000654 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
655 Name != E; ++Name)
656 if (isprint(*Name))
657 OS << *Name;
658}
659
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000660void PPCLinuxAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000661 const TargetData *TD = TM.getTargetData();
662
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000663 if (!GVar->hasInitializer())
664 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000665
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000666 // Check to see if this is a special global used by LLVM, if so, emit it.
667 if (EmitSpecialLLVMGlobal(GVar))
668 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000670 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000672 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000673
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000674 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000675 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000676 return;
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000677 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000678 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000679 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000681 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000682
683 if (C->isNullValue() && /* FIXME: Verify correct */
684 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000685 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000686 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000688
689 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 O << "\t.global " << name << '\n';
691 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000692 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000693 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000694 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000695 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000697 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 }
Evan Cheng11db8142009-03-24 00:17:40 +0000699 if (VerboseAsm) {
700 O << "\t\t" << TAI->getCommentString() << " '";
701 PrintUnmangledNameSafely(GVar, O);
702 O << "'";
703 }
704 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000705 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 }
707
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000708 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000709 case GlobalValue::LinkOnceAnyLinkage:
710 case GlobalValue::LinkOnceODRLinkage:
711 case GlobalValue::WeakAnyLinkage:
712 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000713 case GlobalValue::CommonLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000714 O << "\t.global " << name << '\n'
715 << "\t.type " << name << ", @object\n"
716 << "\t.weak " << name << '\n';
717 break;
718 case GlobalValue::AppendingLinkage:
719 // FIXME: appending linkage variables should go into a section of
720 // their name or something. For now, just emit them as external.
721 case GlobalValue::ExternalLinkage:
722 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000723 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000724 << "\t.type " << name << ", @object\n";
725 // FALL THROUGH
726 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000727 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000728 break;
729 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000730 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000731 }
732
733 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000734 O << name << ":";
735 if (VerboseAsm) {
736 O << "\t\t\t\t" << TAI->getCommentString() << " '";
737 PrintUnmangledNameSafely(GVar, O);
738 O << "'";
739 }
740 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000741
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000742 EmitGlobalConstant(C);
743 O << '\n';
744}
745
746bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
747 // Print out module-level global variables here.
748 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
749 I != E; ++I)
750 printModuleLevelGV(I);
751
Dan Gohman4a558a32007-07-25 19:33:14 +0000752 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753}
754
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755/// runOnMachineFunction - This uses the printMachineInstruction()
756/// method to print assembly for each instruction.
757///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000758bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000759 this->MF = &MF;
760
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 SetupMachineFunction(MF);
762 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000763
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 // Print out constants referenced by the function
765 EmitConstantPool(MF.getConstantPool());
766
767 // Print out labels for the function.
768 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000769 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000770
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000772 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000773 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774 case Function::InternalLinkage: // Symbols default to internal.
775 break;
776 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000777 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000779 case Function::WeakAnyLinkage:
780 case Function::WeakODRLinkage:
781 case Function::LinkOnceAnyLinkage:
782 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000783 O << "\t.globl\t" << CurrentFnName << '\n';
784 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 break;
786 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000787
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000788 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000789
Bill Wendling25a8ae32009-06-30 22:38:32 +0000790 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791 O << CurrentFnName << ":\n";
792
793 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000794 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795
Bill Wendling36ccaea2008-01-26 06:51:24 +0000796 // If the function is empty, then we need to emit *something*. Otherwise, the
797 // function's label might be associated with something that it wasn't meant to
798 // be associated with. We emit a noop in this situation.
799 MachineFunction::iterator I = MF.begin();
800
Bill Wendlingb5880a72008-01-26 09:03:52 +0000801 if (++I == MF.end() && MF.front().empty())
802 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 // Print out code for the function.
805 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
806 I != E; ++I) {
807 // Print a label for the basic block.
808 if (I != MF.begin()) {
Evan Cheng11db8142009-03-24 00:17:40 +0000809 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 O << '\n';
811 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000812 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
813 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 printMachineInstruction(II);
816 }
817 }
818
819 // Print out jump tables referenced by the function.
820 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000821
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000823 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000824
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825 // We didn't modify anything.
826 return false;
827}
828
829
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000830bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000831 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000832 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 "ppc",
834 "ppc601",
835 "ppc602",
836 "ppc603",
837 "ppc7400",
838 "ppc750",
839 "ppc970",
840 "ppc64"
841 };
842
843 unsigned Directive = Subtarget.getDarwinDirective();
844 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
845 Directive = PPC::DIR_970;
846 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
847 Directive = PPC::DIR_7400;
848 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
849 Directive = PPC::DIR_64;
850 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000851 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000852
Dan Gohman4a558a32007-07-25 19:33:14 +0000853 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000854 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000855
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 // Prime text sections so they are adjacent. This reduces the likelihood a
857 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesen3c788322008-01-11 00:54:37 +0000858 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 "pure_instructions");
860 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000861 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 "pure_instructions,32");
863 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000864 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 "pure_instructions,16");
866 }
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000867 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000868
Dan Gohman4a558a32007-07-25 19:33:14 +0000869 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870}
871
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000872void PPCDarwinAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
873 const TargetData *TD = TM.getTargetData();
874
875 if (!GVar->hasInitializer())
876 return; // External global require no code
877
878 // Check to see if this is a special global used by LLVM, if so, emit it.
879 if (EmitSpecialLLVMGlobal(GVar)) {
880 if (TM.getRelocationModel() == Reloc::Static) {
881 if (GVar->getName() == "llvm.global_ctors")
882 O << ".reference .constructors_used\n";
883 else if (GVar->getName() == "llvm.global_dtors")
884 O << ".reference .destructors_used\n";
885 }
886 return;
887 }
888
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000889 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000890 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000891
892 Constant *C = GVar->getInitializer();
893 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000894 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000895 unsigned Align = TD->getPreferredAlignmentLog(GVar);
896
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000897 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000898
899 if (C->isNullValue() && /* FIXME: Verify correct */
900 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000901 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000902 GVar->isWeakForLinker()) &&
Evan Chengcf84b142009-02-18 02:19:52 +0000903 TAI->SectionKindForGlobal(GVar) != SectionKind::RODataMergeStr) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000904 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
905
906 if (GVar->hasExternalLinkage()) {
907 O << "\t.globl " << name << '\n';
908 O << "\t.zerofill __DATA, __common, " << name << ", "
909 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000910 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000911 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000912 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000913 O << "\t.globl " << name << '\n'
914 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000915 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000916 O << name << ":";
917 if (VerboseAsm) {
918 O << "\t\t\t\t" << TAI->getCommentString() << " ";
919 PrintUnmangledNameSafely(GVar, O);
920 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000921 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000922 EmitGlobalConstant(C);
923 return;
924 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000925 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000926 // Darwin 9 and above support aligned common data.
927 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000928 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000929 }
Evan Cheng11db8142009-03-24 00:17:40 +0000930 if (VerboseAsm) {
931 O << "\t\t" << TAI->getCommentString() << " '";
932 PrintUnmangledNameSafely(GVar, O);
933 O << "'";
934 }
935 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000936 return;
937 }
938
939 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000940 case GlobalValue::LinkOnceAnyLinkage:
941 case GlobalValue::LinkOnceODRLinkage:
942 case GlobalValue::WeakAnyLinkage:
943 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000944 case GlobalValue::CommonLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000945 O << "\t.globl " << name << '\n'
946 << "\t.weak_definition " << name << '\n';
947 break;
948 case GlobalValue::AppendingLinkage:
949 // FIXME: appending linkage variables should go into a section of
950 // their name or something. For now, just emit them as external.
951 case GlobalValue::ExternalLinkage:
952 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000953 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000954 // FALL THROUGH
955 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +0000956 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000957 break;
958 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000959 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000960 }
961
962 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000963 O << name << ":";
964 if (VerboseAsm) {
965 O << "\t\t\t\t" << TAI->getCommentString() << " '";
966 PrintUnmangledNameSafely(GVar, O);
967 O << "'";
968 }
969 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000970
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000971 EmitGlobalConstant(C);
972 O << '\n';
973}
974
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000975bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 const TargetData *TD = TM.getTargetData();
977
978 // Print out module-level global variables here.
979 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000980 I != E; ++I)
981 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982
983 bool isPPC64 = TD->getPointerSizeInBits() == 64;
984
985 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +0000986 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
987 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
988 "pure_instructions,32");
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000989 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000990 I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +0000992 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +0000993 O << Info.Stub << ":\n";
994 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +0000996 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
997 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000998 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +0000999 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001000 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001002 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1003 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 O << "\tmtctr r12\n";
1005 O << "\tbctr\n";
1006 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001007 O << Info.LazyPtr << ":\n";
1008 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001009 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 }
Chris Lattner189198f2009-07-15 00:55:58 +00001011 } else if (!FnStubs.empty()) {
1012 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1013 "pure_instructions,16");
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001014 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001015 I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001017 const FnStubInfo &Info = I->second;
1018 O << Info.Stub << ":\n";
1019 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1020 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001021 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001022 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 O << "\tmtctr r12\n";
1024 O << "\tbctr\n";
1025 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001026 O << Info.LazyPtr << ":\n";
1027 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001028 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 }
1030 }
1031
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001032 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001033
Dale Johannesen85535762008-04-02 00:25:04 +00001034 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001035 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001036 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001037 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001038 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001039 E = Personalities.end(); I != E; ++I) {
1040 if (*I)
1041 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001042 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001043 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001044 }
1045
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 // Output stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001047 if (!GVStubs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048 SwitchToDataSection(".non_lazy_symbol_pointer");
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001049 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1050 E = GVStubs.end(); I != E; ++I) {
1051 O << I->second << ":\n";
1052 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1053 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054 }
1055 }
1056
Evan Chenga65854f2008-12-05 01:06:39 +00001057 if (!HiddenGVStubs.empty()) {
1058 SwitchToSection(TAI->getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001059 EmitAlignment(isPPC64 ? 3 : 2);
1060 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1061 E = HiddenGVStubs.end(); I != E; ++I) {
1062 O << I->second << ":\n";
1063 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001064 }
1065 }
1066
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067 // Funny Darwin hack: This flag tells the linker that no global symbols
1068 // contain code that falls through to other global symbols (e.g. the obvious
1069 // implementation of multiple entry points). If this doesn't occur, the
1070 // linker can safely perform dead code stripping. Since LLVM never generates
1071 // code that does this, it is always safe to set.
1072 O << "\t.subsections_via_symbols\n";
1073
Dan Gohman4a558a32007-07-25 19:33:14 +00001074 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075}
1076
1077
1078
1079/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1080/// for a MachineFunction to the given output stream, in a format that the
1081/// Darwin assembler can deal with.
1082///
David Greene302008d2009-07-14 20:18:05 +00001083FunctionPass *llvm::createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001084 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +00001085 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1087
1088 if (Subtarget->isDarwin()) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +00001089 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 } else {
Daniel Dunbarb10d2222009-07-01 01:48:54 +00001091 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 }
1093}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001094
Oscar Fuentes4f012352008-11-15 21:36:30 +00001095extern "C" int PowerPCAsmPrinterForceLink;
1096int PowerPCAsmPrinterForceLink = 0;
Douglas Gregor1dc5ff42009-06-16 20:12:29 +00001097
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001098// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001099extern "C" void LLVMInitializePowerPCAsmPrinter() {
1100 extern Target ThePPC32Target;
1101 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1102
1103 extern Target ThePPC64Target;
1104 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1105}