blob: bc0a794154333b64a7746c915aac797f3ec31abf [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;
Bill Wendling41a07852009-07-20 01:03:30 +000073 Stub = Mang->makeNameProper(GV + "$stub",
Bill Wendlinge4699112009-07-20 19:41:27 +000074 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000075 LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr",
Bill Wendlinge4699112009-07-20 19:41:27 +000076 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000077 AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp",
Bill Wendlinge4699112009-07-20 19:41:27 +000078 Mangler::Private);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000079 }
80 };
81
82 StringMap<FnStubInfo> FnStubs;
Chris Lattner83cf1ec2009-07-15 01:14:44 +000083 StringMap<std::string> GVStubs, HiddenGVStubs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 const PPCSubtarget &Subtarget;
Bill Wendling4f405312009-02-24 08:30:20 +000085 public:
David Greene302008d2009-07-14 20:18:05 +000086 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000087 const TargetAsmInfo *T, bool V)
88 : AsmPrinter(O, TM, T, V),
Bill Wendling4f405312009-02-24 08:30:20 +000089 Subtarget(TM.getSubtarget<PPCSubtarget>()) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090
91 virtual const char *getPassName() const {
92 return "PowerPC Assembly Printer";
93 }
94
95 PPCTargetMachine &getTM() {
96 return static_cast<PPCTargetMachine&>(TM);
97 }
98
99 unsigned enumRegToMachineReg(unsigned enumReg) {
100 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000101 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 case PPC::CR0: return 0;
103 case PPC::CR1: return 1;
104 case PPC::CR2: return 2;
105 case PPC::CR3: return 3;
106 case PPC::CR4: return 4;
107 case PPC::CR5: return 5;
108 case PPC::CR6: return 6;
109 case PPC::CR7: return 7;
110 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000111 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 }
113
114 /// printInstruction - This method is automatically generated by tablegen
115 /// from the instruction set description. This method returns true if the
116 /// machine instruction was sufficiently described to print it, otherwise it
117 /// returns false.
118 bool printInstruction(const MachineInstr *MI);
119
120 void printMachineInstruction(const MachineInstr *MI);
121 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 /// stripRegisterPrefix - This method strips the character prefix from a
124 /// register name so that only the number is left. Used by for linux asm.
125 const char *stripRegisterPrefix(const char *RegName) {
126 switch (RegName[0]) {
127 case 'r':
128 case 'f':
129 case 'v': return RegName + 1;
130 case 'c': if (RegName[1] == 'r') return RegName + 2;
131 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000132
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 return RegName;
134 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000135
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 /// printRegister - Print register according to target requirements.
137 ///
138 void printRegister(const MachineOperand &MO, bool R0AsZero) {
139 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000140 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 // If we should use 0 for R0.
143 if (R0AsZero && RegNo == PPC::R0) {
144 O << "0";
145 return;
146 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000147
Bill Wendling8eeb9792008-02-26 21:11:01 +0000148 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 // Linux assembler (Others?) does not take register mnemonics.
150 // FIXME - What about special registers used in mfspr/mtspr?
151 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
152 O << RegName;
153 }
154
155 void printOperand(const MachineInstr *MI, unsigned OpNo) {
156 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000157 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000159 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000160 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 } else {
162 printOp(MO);
163 }
164 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000165
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
167 unsigned AsmVariant, const char *ExtraCode);
168 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
169 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000170
171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000173 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 value = (value << (32-5)) >> (32-5);
175 O << (int)value;
176 }
177 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000178 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 assert(value <= 31 && "Invalid u5imm argument!");
180 O << (unsigned int)value;
181 }
182 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000183 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 assert(value <= 63 && "Invalid u6imm argument!");
185 O << (unsigned int)value;
186 }
187 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000188 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000191 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 }
193 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000194 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000195 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 } else {
197 O << "lo16(";
198 printOp(MI->getOperand(OpNo));
199 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000200 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 else
202 O << ')';
203 }
204 }
205 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
206 // Branches can take an immediate operand. This is used by the branch
207 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000208 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000209 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 } else {
211 printOp(MI->getOperand(OpNo));
212 }
213 }
214 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
215 const MachineOperand &MO = MI->getOperand(OpNo);
216 if (TM.getRelocationModel() != Reloc::Static) {
217 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
218 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000219 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000221 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
222 FnInfo.Init(GV, Mang);
223 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return;
225 }
226 }
227 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000228 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
229 FnInfo.Init(MO.getSymbolName(), Mang);
230 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 return;
232 }
233 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 printOp(MI->getOperand(OpNo));
236 }
237 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000238 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 }
240 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000241 O << "\"L" << getFunctionNumber() << "$pb\"\n";
242 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 }
244 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000245 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 printS16ImmOperand(MI, OpNo);
247 } else {
248 if (Subtarget.isDarwin()) O << "ha16(";
249 printOp(MI->getOperand(OpNo));
250 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000251 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 if (Subtarget.isDarwin())
253 O << ')';
254 else
255 O << "@ha";
256 }
257 }
258 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000259 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 printS16ImmOperand(MI, OpNo);
261 } else {
262 if (Subtarget.isDarwin()) O << "lo16(";
263 printOp(MI->getOperand(OpNo));
264 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000265 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (Subtarget.isDarwin())
267 O << ')';
268 else
269 O << "@l";
270 }
271 }
272 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
273 unsigned CCReg = MI->getOperand(OpNo).getReg();
274 unsigned RegNo = enumRegToMachineReg(CCReg);
275 O << (0x80 >> RegNo);
276 }
277 // The new addressing mode printers.
278 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
279 printSymbolLo(MI, OpNo);
280 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000281 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 MI->getOperand(OpNo+1).getReg() == PPC::R0)
283 O << "0";
284 else
285 printOperand(MI, OpNo+1);
286 O << ')';
287 }
288 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000289 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000291 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 printSymbolLo(MI, OpNo);
293 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000294 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 MI->getOperand(OpNo+1).getReg() == PPC::R0)
296 O << "0";
297 else
298 printOperand(MI, OpNo+1);
299 O << ')';
300 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000301
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
303 // When used as the base register, r0 reads constant zero rather than
304 // the value contained in the register. For this reason, the darwin
305 // assembler requires that we print r0 as 0 (no r) when used as the base.
306 const MachineOperand &MO = MI->getOperand(OpNo);
307 printRegister(MO, true);
308 O << ", ";
309 printOperand(MI, OpNo+1);
310 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000311
312 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000314
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316
317 virtual void EmitExternalGlobal(const GlobalVariable *GV);
318 };
319
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000320 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Bill Wendling4f405312009-02-24 08:30:20 +0000321 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000322 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000323 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000324 const TargetAsmInfo *T, bool V)
325 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326
327 virtual const char *getPassName() const {
328 return "Linux PPC Assembly Printer";
329 }
330
331 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 void getAnalysisUsage(AnalysisUsage &AU) const {
334 AU.setPreservesAll();
335 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000336 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 PPCAsmPrinter::getAnalysisUsage(AU);
338 }
339
Chris Lattnerae982212009-07-21 18:38:57 +0000340 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 };
342
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000343 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
344 /// OS X
Bill Wendling4f405312009-02-24 08:30:20 +0000345 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000346 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000347 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000348 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000349 const TargetAsmInfo *T, bool V)
350 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351
352 virtual const char *getPassName() const {
353 return "Darwin PPC Assembly Printer";
354 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 bool runOnMachineFunction(MachineFunction &F);
357 bool doInitialization(Module &M);
358 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000359
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 void getAnalysisUsage(AnalysisUsage &AU) const {
361 AU.setPreservesAll();
362 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000363 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 PPCAsmPrinter::getAnalysisUsage(AU);
365 }
366
Chris Lattnerae982212009-07-21 18:38:57 +0000367 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 };
369} // end of anonymous namespace
370
371// Include the auto-generated portion of the assembly writer
372#include "PPCGenAsmWriter.inc"
373
374void PPCAsmPrinter::printOp(const MachineOperand &MO) {
375 switch (MO.getType()) {
376 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000377 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378
379 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000380 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 return;
382 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000383 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000384 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 // FIXME: PIC relocation model
386 return;
387 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000388 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000389 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000391 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 // Computing the address of an external symbol, not calling it.
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000393 std::string Name(TAI->getGlobalPrefix());
394 Name += MO.getSymbolName();
395
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000397 GVStubs[Name] = Name+"$non_lazy_ptr";
398 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000400 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000402 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 case MachineOperand::MO_GlobalAddress: {
404 // Computing the address of a global symbol, not calling it.
405 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000406 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407
408 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000409 if (TM.getRelocationModel() != Reloc::Static &&
410 (GV->isDeclaration() || GV->isWeakForLinker())) {
411 if (!GV->hasHiddenVisibility()) {
412 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
413 GVStubs[Mang->getMangledName(GV)] = Name;
414 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
415 GV->hasAvailableExternallyLinkage()) {
416 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
417 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
418 } else {
419 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000421 } else {
422 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 }
424 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000425
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000426 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 return;
428 }
429
430 default:
431 O << "<unknown operand type: " << MO.getType() << ">";
432 return;
433 }
434}
435
436/// EmitExternalGlobal - In this case we need to use the indirect symbol.
437///
438void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000439 std::string Name;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000440
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000442 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000443 } else {
444 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 }
446 O << Name;
447}
448
449/// PrintAsmOperand - Print out an operand for an inline asm expression.
450///
451bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000452 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 const char *ExtraCode) {
454 // Does this asm operand have a single letter operand modifier?
455 if (ExtraCode && ExtraCode[0]) {
456 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000457
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 switch (ExtraCode[0]) {
459 default: return true; // Unknown modifier.
460 case 'c': // Don't print "$" before a global var name or constant.
461 // PPC never has a prefix.
462 printOperand(MI, OpNo);
463 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000464 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000466 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000468 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 return true;
470 ++OpNo; // Return the high-part.
471 break;
472 case 'I':
473 // Write 'i' if an integer constant, otherwise nothing. Used to print
474 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000475 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476 O << "i";
477 return false;
478 }
479 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000480
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 printOperand(MI, OpNo);
482 return false;
483}
484
485bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000486 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 const char *ExtraCode) {
488 if (ExtraCode && ExtraCode[0])
489 return true; // Unknown modifier.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000490 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 printMemRegReg(MI, OpNo);
492 else
493 printMemRegImm(MI, OpNo);
494 return false;
495}
496
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000497void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 const char *Modifier) {
499 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
500 unsigned Code = MI->getOperand(OpNo).getImm();
501 if (!strcmp(Modifier, "cc")) {
502 switch ((PPC::Predicate)Code) {
503 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
504 case PPC::PRED_LT: O << "lt"; return;
505 case PPC::PRED_LE: O << "le"; return;
506 case PPC::PRED_EQ: O << "eq"; return;
507 case PPC::PRED_GE: O << "ge"; return;
508 case PPC::PRED_GT: O << "gt"; return;
509 case PPC::PRED_NE: O << "ne"; return;
510 case PPC::PRED_UN: O << "un"; return;
511 case PPC::PRED_NU: O << "nu"; return;
512 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000513
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 } else {
515 assert(!strcmp(Modifier, "reg") &&
516 "Need to specify 'cc' or 'reg' as predicate op modifier!");
517 // Don't print the register for 'always'.
518 if (Code == PPC::PRED_ALWAYS) return;
519 printOperand(MI, OpNo+1);
520 }
521}
522
523
524/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
525/// the current output stream.
526///
527void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
528 ++EmittedInsts;
529
530 // Check for slwi/srwi mnemonics.
531 if (MI->getOpcode() == PPC::RLWINM) {
532 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000533 unsigned char SH = MI->getOperand(2).getImm();
534 unsigned char MB = MI->getOperand(3).getImm();
535 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000537 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 }
539 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000540 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 SH = 32-SH;
542 }
543 if (FoundMnemonic) {
544 printOperand(MI, 0);
545 O << ", ";
546 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000547 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 return;
549 }
550 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
551 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000552 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 printOperand(MI, 0);
554 O << ", ";
555 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000556 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 return;
558 }
559 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000560 unsigned char SH = MI->getOperand(2).getImm();
561 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
563 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000564 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 printOperand(MI, 0);
566 O << ", ";
567 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000568 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 return;
570 }
571 }
572
573 if (printInstruction(MI))
574 return; // Printer was automatically generated
575
Edwin Törökbd448e32009-07-14 16:55:14 +0000576 llvm_unreachable("Unhandled instruction in asm writer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000577}
578
579/// runOnMachineFunction - This uses the printMachineInstruction()
580/// method to print assembly for each instruction.
581///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000582bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000583 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584
585 SetupMachineFunction(MF);
586 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000587
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 // Print out constants referenced by the function
589 EmitConstantPool(MF.getConstantPool());
590
591 // Print out labels for the function.
592 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000593 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000594
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000596 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000597 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000598 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 case Function::InternalLinkage: // Symbols default to internal.
600 break;
601 case Function::ExternalLinkage:
602 O << "\t.global\t" << CurrentFnName << '\n'
603 << "\t.type\t" << CurrentFnName << ", @function\n";
604 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000605 case Function::WeakAnyLinkage:
606 case Function::WeakODRLinkage:
607 case Function::LinkOnceAnyLinkage:
608 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 O << "\t.global\t" << CurrentFnName << '\n';
610 O << "\t.weak\t" << CurrentFnName << '\n';
611 break;
612 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000613
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000614 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000615
Bill Wendling25a8ae32009-06-30 22:38:32 +0000616 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 O << CurrentFnName << ":\n";
618
619 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000620 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621
622 // Print out code for the function.
623 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
624 I != E; ++I) {
625 // Print a label for the basic block.
626 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000627 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628 O << '\n';
629 }
630 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
631 II != E; ++II) {
632 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 printMachineInstruction(II);
634 }
635 }
636
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000637 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638
639 // Print out jump tables referenced by the function.
640 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000641
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000642 SwitchToSection(TAI->SectionForGlobal(F));
643
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000645 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000646
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000647 O.flush();
648
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 // We didn't modify anything.
650 return false;
651}
652
Chris Lattner2b638a72008-02-15 19:04:54 +0000653/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +0000654/// Don't print things like \\n or \\0.
Daniel Dunbar23e2b802009-07-26 07:49:05 +0000655static void PrintUnmangledNameSafely(const Value *V,
656 formatted_raw_ostream &OS) {
657 for (StringRef::iterator it = V->getName().begin(),
658 ie = V->getName().end(); it != ie; ++it)
659 if (isprint(*it))
660 OS << *it;
Chris Lattner2b638a72008-02-15 19:04:54 +0000661}
662
Chris Lattnerae982212009-07-21 18:38:57 +0000663void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 const TargetData *TD = TM.getTargetData();
665
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000666 if (!GVar->hasInitializer())
667 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000668
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000669 // Check to see if this is a special global used by LLVM, if so, emit it.
670 if (EmitSpecialLLVMGlobal(GVar))
671 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000673 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000675 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000676
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000677 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000678 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000679 return;
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000680 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000681 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000682 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000684 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000685
686 if (C->isNullValue() && /* FIXME: Verify correct */
687 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000688 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000689 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000691
692 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 O << "\t.global " << name << '\n';
694 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000695 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000696 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000697 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000698 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000700 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 }
Evan Cheng11db8142009-03-24 00:17:40 +0000702 if (VerboseAsm) {
703 O << "\t\t" << TAI->getCommentString() << " '";
704 PrintUnmangledNameSafely(GVar, O);
705 O << "'";
706 }
707 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000708 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 }
710
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000711 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000712 case GlobalValue::LinkOnceAnyLinkage:
713 case GlobalValue::LinkOnceODRLinkage:
714 case GlobalValue::WeakAnyLinkage:
715 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000716 case GlobalValue::CommonLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000717 O << "\t.global " << name << '\n'
718 << "\t.type " << name << ", @object\n"
719 << "\t.weak " << name << '\n';
720 break;
721 case GlobalValue::AppendingLinkage:
722 // FIXME: appending linkage variables should go into a section of
723 // their name or something. For now, just emit them as external.
724 case GlobalValue::ExternalLinkage:
725 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000726 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000727 << "\t.type " << name << ", @object\n";
728 // FALL THROUGH
729 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000730 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000731 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000732 break;
733 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000734 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000735 }
736
737 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000738 O << name << ":";
739 if (VerboseAsm) {
740 O << "\t\t\t\t" << TAI->getCommentString() << " '";
741 PrintUnmangledNameSafely(GVar, O);
742 O << "'";
743 }
744 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000745
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000746 EmitGlobalConstant(C);
747 O << '\n';
748}
749
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751/// runOnMachineFunction - This uses the printMachineInstruction()
752/// method to print assembly for each instruction.
753///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000754bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000755 this->MF = &MF;
756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 SetupMachineFunction(MF);
758 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000759
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 // Print out constants referenced by the function
761 EmitConstantPool(MF.getConstantPool());
762
763 // Print out labels for the function.
764 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000765 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000766
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000768 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000769 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000770 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771 case Function::InternalLinkage: // Symbols default to internal.
772 break;
773 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000774 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000776 case Function::WeakAnyLinkage:
777 case Function::WeakODRLinkage:
778 case Function::LinkOnceAnyLinkage:
779 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000780 O << "\t.globl\t" << CurrentFnName << '\n';
781 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 break;
783 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000784
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000785 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000786
Bill Wendling25a8ae32009-06-30 22:38:32 +0000787 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 O << CurrentFnName << ":\n";
789
790 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000791 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792
Bill Wendling36ccaea2008-01-26 06:51:24 +0000793 // If the function is empty, then we need to emit *something*. Otherwise, the
794 // function's label might be associated with something that it wasn't meant to
795 // be associated with. We emit a noop in this situation.
796 MachineFunction::iterator I = MF.begin();
797
Bill Wendlingb5880a72008-01-26 09:03:52 +0000798 if (++I == MF.end() && MF.front().empty())
799 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000800
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 // Print out code for the function.
802 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
803 I != E; ++I) {
804 // Print a label for the basic block.
805 if (I != MF.begin()) {
Evan Cheng11db8142009-03-24 00:17:40 +0000806 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 O << '\n';
808 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000809 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
810 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000811 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 printMachineInstruction(II);
813 }
814 }
815
816 // Print out jump tables referenced by the function.
817 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000818
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000820 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000821
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 // We didn't modify anything.
823 return false;
824}
825
826
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000827bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000828 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000829 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 "ppc",
831 "ppc601",
832 "ppc602",
833 "ppc603",
834 "ppc7400",
835 "ppc750",
836 "ppc970",
837 "ppc64"
838 };
839
840 unsigned Directive = Subtarget.getDarwinDirective();
841 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
842 Directive = PPC::DIR_970;
843 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
844 Directive = PPC::DIR_7400;
845 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
846 Directive = PPC::DIR_64;
847 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000848 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000849
Dan Gohman4a558a32007-07-25 19:33:14 +0000850 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000851 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000852
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 // Prime text sections so they are adjacent. This reduces the likelihood a
854 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesen3c788322008-01-11 00:54:37 +0000855 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 "pure_instructions");
857 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000858 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 "pure_instructions,32");
860 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000861 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 "pure_instructions,16");
863 }
Anton Korobeynikov55b94962008-09-24 22:15:21 +0000864 SwitchToSection(TAI->getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000865
Dan Gohman4a558a32007-07-25 19:33:14 +0000866 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867}
868
Chris Lattnerae982212009-07-21 18:38:57 +0000869void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000870 const TargetData *TD = TM.getTargetData();
871
872 if (!GVar->hasInitializer())
873 return; // External global require no code
874
875 // Check to see if this is a special global used by LLVM, if so, emit it.
876 if (EmitSpecialLLVMGlobal(GVar)) {
877 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000878 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000879 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000880 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000881 O << ".reference .destructors_used\n";
882 }
883 return;
884 }
885
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000886 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000887 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000888
889 Constant *C = GVar->getInitializer();
890 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000891 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000892 unsigned Align = TD->getPreferredAlignmentLog(GVar);
893
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000894 const Section *TheSection = TAI->SectionForGlobal(GVar);
895 SwitchToSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000896
897 if (C->isNullValue() && /* FIXME: Verify correct */
898 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000899 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000900 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000901 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000902 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000903 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
904
905 if (GVar->hasExternalLinkage()) {
906 O << "\t.globl " << name << '\n';
907 O << "\t.zerofill __DATA, __common, " << name << ", "
908 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000909 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000910 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000911 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000912 O << "\t.globl " << name << '\n'
913 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000914 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000915 O << name << ":";
916 if (VerboseAsm) {
917 O << "\t\t\t\t" << TAI->getCommentString() << " ";
918 PrintUnmangledNameSafely(GVar, O);
919 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000920 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000921 EmitGlobalConstant(C);
922 return;
923 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000924 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000925 // Darwin 9 and above support aligned common data.
926 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000927 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000928 }
Evan Cheng11db8142009-03-24 00:17:40 +0000929 if (VerboseAsm) {
930 O << "\t\t" << TAI->getCommentString() << " '";
931 PrintUnmangledNameSafely(GVar, O);
932 O << "'";
933 }
934 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000935 return;
936 }
937
938 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000939 case GlobalValue::LinkOnceAnyLinkage:
940 case GlobalValue::LinkOnceODRLinkage:
941 case GlobalValue::WeakAnyLinkage:
942 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000943 case GlobalValue::CommonLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000944 O << "\t.globl " << name << '\n'
945 << "\t.weak_definition " << name << '\n';
946 break;
947 case GlobalValue::AppendingLinkage:
948 // FIXME: appending linkage variables should go into a section of
949 // their name or something. For now, just emit them as external.
950 case GlobalValue::ExternalLinkage:
951 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000952 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000953 // FALL THROUGH
954 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +0000955 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000956 case GlobalValue::LinkerPrivateLinkage:
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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978 bool isPPC64 = TD->getPointerSizeInBits() == 64;
979
980 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +0000981 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000982 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000983 I != E; ++I) {
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +0000984 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
985 "pure_instructions,32");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +0000987 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +0000988 O << Info.Stub << ":\n";
989 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +0000991 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
992 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +0000994 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +0000995 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +0000997 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
998 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 O << "\tmtctr r12\n";
1000 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001002 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001003 O << Info.LazyPtr << ":\n";
1004 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001005 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 }
Chris Lattner189198f2009-07-15 00:55:58 +00001007 } else if (!FnStubs.empty()) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001008 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001009 I != E; ++I) {
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001010 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1011 "pure_instructions,16");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001013 const FnStubInfo &Info = I->second;
1014 O << Info.Stub << ":\n";
1015 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1016 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001017 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001018 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019 O << "\tmtctr r12\n";
1020 O << "\tbctr\n";
1021 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001022 O << Info.LazyPtr << ":\n";
1023 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001024 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 }
1026 }
1027
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001028 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029
Dale Johannesen85535762008-04-02 00:25:04 +00001030 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001031 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001032 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001033 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001034 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001035 E = Personalities.end(); I != E; ++I) {
1036 if (*I)
1037 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001038 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001039 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001040 }
1041
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001042 // Output stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001043 if (!GVStubs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 SwitchToDataSection(".non_lazy_symbol_pointer");
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001045 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1046 E = GVStubs.end(); I != E; ++I) {
1047 O << I->second << ":\n";
1048 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1049 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 }
1051 }
1052
Evan Chenga65854f2008-12-05 01:06:39 +00001053 if (!HiddenGVStubs.empty()) {
1054 SwitchToSection(TAI->getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001055 EmitAlignment(isPPC64 ? 3 : 2);
1056 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1057 E = HiddenGVStubs.end(); I != E; ++I) {
1058 O << I->second << ":\n";
1059 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001060 }
1061 }
1062
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063 // Funny Darwin hack: This flag tells the linker that no global symbols
1064 // contain code that falls through to other global symbols (e.g. the obvious
1065 // implementation of multiple entry points). If this doesn't occur, the
1066 // linker can safely perform dead code stripping. Since LLVM never generates
1067 // code that does this, it is always safe to set.
1068 O << "\t.subsections_via_symbols\n";
1069
Dan Gohman4a558a32007-07-25 19:33:14 +00001070 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071}
1072
1073
1074
1075/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1076/// for a MachineFunction to the given output stream, in a format that the
1077/// Darwin assembler can deal with.
1078///
Daniel Dunbarc680b012009-07-25 06:49:55 +00001079static FunctionPass *createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001080 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +00001081 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1083
Chris Lattnerae982212009-07-21 18:38:57 +00001084 if (Subtarget->isDarwin())
Daniel Dunbarb10d2222009-07-01 01:48:54 +00001085 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Chris Lattnerae982212009-07-21 18:38:57 +00001086 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001088
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001089// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001090extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001091 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1092
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001093 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1094}