blob: a1b0780e63608990651b8524b78b3ff870daeb4f [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"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000043#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000044#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetOptions.h"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000047#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048#include "llvm/ADT/Statistic.h"
49#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000050#include "llvm/ADT/StringSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051using namespace llvm;
52
53STATISTIC(EmittedInsts, "Number of machine instrs printed");
54
55namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000056 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
57 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000058 struct FnStubInfo {
59 std::string Stub, LazyPtr, AnonSymbol;
60
61 FnStubInfo() {}
62
63 void Init(const GlobalValue *GV, Mangler *Mang) {
64 // Already initialized.
65 if (!Stub.empty()) return;
66 Stub = Mang->getMangledName(GV, "$stub", true);
67 LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true);
68 AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true);
69 }
70
71 void Init(const std::string &GV, Mangler *Mang) {
72 // Already initialized.
73 if (!Stub.empty()) return;
Bill Wendling41a07852009-07-20 01:03:30 +000074 Stub = Mang->makeNameProper(GV + "$stub",
Bill Wendlinge4699112009-07-20 19:41:27 +000075 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000076 LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr",
Bill Wendlinge4699112009-07-20 19:41:27 +000077 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000078 AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp",
Bill Wendlinge4699112009-07-20 19:41:27 +000079 Mangler::Private);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000080 }
81 };
82
83 StringMap<FnStubInfo> FnStubs;
Chris Lattner83cf1ec2009-07-15 01:14:44 +000084 StringMap<std::string> GVStubs, HiddenGVStubs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 const PPCSubtarget &Subtarget;
Bill Wendling4f405312009-02-24 08:30:20 +000086 public:
David Greene302008d2009-07-14 20:18:05 +000087 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000088 const TargetAsmInfo *T, bool V)
89 : AsmPrinter(O, TM, T, V),
Bill Wendling4f405312009-02-24 08:30:20 +000090 Subtarget(TM.getSubtarget<PPCSubtarget>()) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091
92 virtual const char *getPassName() const {
93 return "PowerPC Assembly Printer";
94 }
95
96 PPCTargetMachine &getTM() {
97 return static_cast<PPCTargetMachine&>(TM);
98 }
99
100 unsigned enumRegToMachineReg(unsigned enumReg) {
101 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000102 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 case PPC::CR0: return 0;
104 case PPC::CR1: return 1;
105 case PPC::CR2: return 2;
106 case PPC::CR3: return 3;
107 case PPC::CR4: return 4;
108 case PPC::CR5: return 5;
109 case PPC::CR6: return 6;
110 case PPC::CR7: return 7;
111 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000112 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 }
114
115 /// printInstruction - This method is automatically generated by tablegen
116 /// from the instruction set description. This method returns true if the
117 /// machine instruction was sufficiently described to print it, otherwise it
118 /// returns false.
119 bool printInstruction(const MachineInstr *MI);
120
121 void printMachineInstruction(const MachineInstr *MI);
122 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000123
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 /// stripRegisterPrefix - This method strips the character prefix from a
125 /// register name so that only the number is left. Used by for linux asm.
126 const char *stripRegisterPrefix(const char *RegName) {
127 switch (RegName[0]) {
128 case 'r':
129 case 'f':
130 case 'v': return RegName + 1;
131 case 'c': if (RegName[1] == 'r') return RegName + 2;
132 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000133
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 return RegName;
135 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 /// printRegister - Print register according to target requirements.
138 ///
139 void printRegister(const MachineOperand &MO, bool R0AsZero) {
140 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000141 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000142
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 // If we should use 0 for R0.
144 if (R0AsZero && RegNo == PPC::R0) {
145 O << "0";
146 return;
147 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000148
Bill Wendling8eeb9792008-02-26 21:11:01 +0000149 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 // Linux assembler (Others?) does not take register mnemonics.
151 // FIXME - What about special registers used in mfspr/mtspr?
152 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
153 O << RegName;
154 }
155
156 void printOperand(const MachineInstr *MI, unsigned OpNo) {
157 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000158 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000160 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000161 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 } else {
163 printOp(MO);
164 }
165 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000166
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
168 unsigned AsmVariant, const char *ExtraCode);
169 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
170 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000171
172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000174 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 value = (value << (32-5)) >> (32-5);
176 O << (int)value;
177 }
178 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000179 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 assert(value <= 31 && "Invalid u5imm argument!");
181 O << (unsigned int)value;
182 }
183 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000184 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 assert(value <= 63 && "Invalid u6imm argument!");
186 O << (unsigned int)value;
187 }
188 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000189 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 }
191 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000192 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 }
194 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000195 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000196 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 } else {
198 O << "lo16(";
199 printOp(MI->getOperand(OpNo));
200 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000201 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 else
203 O << ')';
204 }
205 }
206 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
207 // Branches can take an immediate operand. This is used by the branch
208 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000209 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000210 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 } else {
212 printOp(MI->getOperand(OpNo));
213 }
214 }
215 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
216 const MachineOperand &MO = MI->getOperand(OpNo);
217 if (TM.getRelocationModel() != Reloc::Static) {
218 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
219 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000220 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000222 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
223 FnInfo.Init(GV, Mang);
224 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 return;
226 }
227 }
228 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000229 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
230 FnInfo.Init(MO.getSymbolName(), Mang);
231 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 return;
233 }
234 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000235
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 printOp(MI->getOperand(OpNo));
237 }
238 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000239 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 }
241 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000242 O << "\"L" << getFunctionNumber() << "$pb\"\n";
243 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 }
245 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000246 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 printS16ImmOperand(MI, OpNo);
248 } else {
249 if (Subtarget.isDarwin()) O << "ha16(";
250 printOp(MI->getOperand(OpNo));
251 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000252 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 if (Subtarget.isDarwin())
254 O << ')';
255 else
256 O << "@ha";
257 }
258 }
259 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000260 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 printS16ImmOperand(MI, OpNo);
262 } else {
263 if (Subtarget.isDarwin()) O << "lo16(";
264 printOp(MI->getOperand(OpNo));
265 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000266 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 if (Subtarget.isDarwin())
268 O << ')';
269 else
270 O << "@l";
271 }
272 }
273 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
274 unsigned CCReg = MI->getOperand(OpNo).getReg();
275 unsigned RegNo = enumRegToMachineReg(CCReg);
276 O << (0x80 >> RegNo);
277 }
278 // The new addressing mode printers.
279 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
280 printSymbolLo(MI, OpNo);
281 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000282 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 MI->getOperand(OpNo+1).getReg() == PPC::R0)
284 O << "0";
285 else
286 printOperand(MI, OpNo+1);
287 O << ')';
288 }
289 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000290 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000292 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 printSymbolLo(MI, OpNo);
294 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000295 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 MI->getOperand(OpNo+1).getReg() == PPC::R0)
297 O << "0";
298 else
299 printOperand(MI, OpNo+1);
300 O << ')';
301 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000302
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
304 // When used as the base register, r0 reads constant zero rather than
305 // the value contained in the register. For this reason, the darwin
306 // assembler requires that we print r0 as 0 (no r) when used as the base.
307 const MachineOperand &MO = MI->getOperand(OpNo);
308 printRegister(MO, true);
309 O << ", ";
310 printOperand(MI, OpNo+1);
311 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000312
313 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000315
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317
318 virtual void EmitExternalGlobal(const GlobalVariable *GV);
319 };
320
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000321 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Bill Wendling4f405312009-02-24 08:30:20 +0000322 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000323 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000324 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000325 const TargetAsmInfo *T, bool V)
326 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
328 virtual const char *getPassName() const {
329 return "Linux PPC Assembly Printer";
330 }
331
332 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000333
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 void getAnalysisUsage(AnalysisUsage &AU) const {
335 AU.setPreservesAll();
336 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000337 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 PPCAsmPrinter::getAnalysisUsage(AU);
339 }
340
Chris Lattnerae982212009-07-21 18:38:57 +0000341 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 };
343
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000344 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
345 /// OS X
Bill Wendling4f405312009-02-24 08:30:20 +0000346 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000347 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000348 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000349 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000350 const TargetAsmInfo *T, bool V)
351 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352
353 virtual const char *getPassName() const {
354 return "Darwin PPC Assembly Printer";
355 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 bool runOnMachineFunction(MachineFunction &F);
358 bool doInitialization(Module &M);
359 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000360
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 void getAnalysisUsage(AnalysisUsage &AU) const {
362 AU.setPreservesAll();
363 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000364 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 PPCAsmPrinter::getAnalysisUsage(AU);
366 }
367
Chris Lattnerae982212009-07-21 18:38:57 +0000368 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 };
370} // end of anonymous namespace
371
372// Include the auto-generated portion of the assembly writer
373#include "PPCGenAsmWriter.inc"
374
375void PPCAsmPrinter::printOp(const MachineOperand &MO) {
376 switch (MO.getType()) {
377 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000378 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379
380 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000381 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 return;
383 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000384 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000385 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 // FIXME: PIC relocation model
387 return;
388 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000389 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000390 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000392 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 // Computing the address of an external symbol, not calling it.
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000394 std::string Name(TAI->getGlobalPrefix());
395 Name += MO.getSymbolName();
396
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000398 GVStubs[Name] = Name+"$non_lazy_ptr";
399 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000401 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000403 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 case MachineOperand::MO_GlobalAddress: {
405 // Computing the address of a global symbol, not calling it.
406 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000407 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408
409 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000410 if (TM.getRelocationModel() != Reloc::Static &&
411 (GV->isDeclaration() || GV->isWeakForLinker())) {
412 if (!GV->hasHiddenVisibility()) {
413 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
414 GVStubs[Mang->getMangledName(GV)] = Name;
415 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
416 GV->hasAvailableExternallyLinkage()) {
417 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
418 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
419 } else {
420 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000422 } else {
423 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 }
425 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000426
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000427 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 return;
429 }
430
431 default:
432 O << "<unknown operand type: " << MO.getType() << ">";
433 return;
434 }
435}
436
437/// EmitExternalGlobal - In this case we need to use the indirect symbol.
438///
439void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000440 std::string Name;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000441
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000443 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000444 } else {
445 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 }
447 O << Name;
448}
449
450/// PrintAsmOperand - Print out an operand for an inline asm expression.
451///
452bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000453 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 const char *ExtraCode) {
455 // Does this asm operand have a single letter operand modifier?
456 if (ExtraCode && ExtraCode[0]) {
457 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 switch (ExtraCode[0]) {
460 default: return true; // Unknown modifier.
461 case 'c': // Don't print "$" before a global var name or constant.
462 // PPC never has a prefix.
463 printOperand(MI, OpNo);
464 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000465 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000467 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000469 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 return true;
471 ++OpNo; // Return the high-part.
472 break;
473 case 'I':
474 // Write 'i' if an integer constant, otherwise nothing. Used to print
475 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000476 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 O << "i";
478 return false;
479 }
480 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000481
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 printOperand(MI, OpNo);
483 return false;
484}
485
486bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000487 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 const char *ExtraCode) {
489 if (ExtraCode && ExtraCode[0])
490 return true; // Unknown modifier.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000491 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 printMemRegReg(MI, OpNo);
493 else
494 printMemRegImm(MI, OpNo);
495 return false;
496}
497
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000498void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 const char *Modifier) {
500 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
501 unsigned Code = MI->getOperand(OpNo).getImm();
502 if (!strcmp(Modifier, "cc")) {
503 switch ((PPC::Predicate)Code) {
504 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
505 case PPC::PRED_LT: O << "lt"; return;
506 case PPC::PRED_LE: O << "le"; return;
507 case PPC::PRED_EQ: O << "eq"; return;
508 case PPC::PRED_GE: O << "ge"; return;
509 case PPC::PRED_GT: O << "gt"; return;
510 case PPC::PRED_NE: O << "ne"; return;
511 case PPC::PRED_UN: O << "un"; return;
512 case PPC::PRED_NU: O << "nu"; return;
513 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000514
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 } else {
516 assert(!strcmp(Modifier, "reg") &&
517 "Need to specify 'cc' or 'reg' as predicate op modifier!");
518 // Don't print the register for 'always'.
519 if (Code == PPC::PRED_ALWAYS) return;
520 printOperand(MI, OpNo+1);
521 }
522}
523
524
525/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
526/// the current output stream.
527///
528void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
529 ++EmittedInsts;
530
531 // Check for slwi/srwi mnemonics.
532 if (MI->getOpcode() == PPC::RLWINM) {
533 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000534 unsigned char SH = MI->getOperand(2).getImm();
535 unsigned char MB = MI->getOperand(3).getImm();
536 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000538 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 }
540 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000541 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 SH = 32-SH;
543 }
544 if (FoundMnemonic) {
545 printOperand(MI, 0);
546 O << ", ";
547 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000548 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 return;
550 }
551 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
552 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000553 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 printOperand(MI, 0);
555 O << ", ";
556 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000557 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 return;
559 }
560 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000561 unsigned char SH = MI->getOperand(2).getImm();
562 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
564 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000565 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 printOperand(MI, 0);
567 O << ", ";
568 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000569 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 return;
571 }
572 }
573
574 if (printInstruction(MI))
575 return; // Printer was automatically generated
576
Edwin Törökbd448e32009-07-14 16:55:14 +0000577 llvm_unreachable("Unhandled instruction in asm writer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578}
579
580/// runOnMachineFunction - This uses the printMachineInstruction()
581/// method to print assembly for each instruction.
582///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000583bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000584 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585
586 SetupMachineFunction(MF);
587 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000588
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 // Print out constants referenced by the function
590 EmitConstantPool(MF.getConstantPool());
591
592 // Print out labels for the function.
593 const Function *F = MF.getFunction();
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000594 SwitchToSection(getObjFileLowering().SectionForGlobal(F, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000595
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000597 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000598 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000599 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 case Function::InternalLinkage: // Symbols default to internal.
601 break;
602 case Function::ExternalLinkage:
603 O << "\t.global\t" << CurrentFnName << '\n'
604 << "\t.type\t" << CurrentFnName << ", @function\n";
605 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000606 case Function::WeakAnyLinkage:
607 case Function::WeakODRLinkage:
608 case Function::LinkOnceAnyLinkage:
609 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000610 O << "\t.global\t" << CurrentFnName << '\n';
611 O << "\t.weak\t" << CurrentFnName << '\n';
612 break;
613 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000614
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000615 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000616
Bill Wendling25a8ae32009-06-30 22:38:32 +0000617 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 O << CurrentFnName << ":\n";
619
620 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000621 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622
623 // Print out code for the function.
624 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
625 I != E; ++I) {
626 // Print a label for the basic block.
627 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000628 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 O << '\n';
630 }
631 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
632 II != E; ++II) {
633 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 printMachineInstruction(II);
635 }
636 }
637
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000638 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639
640 // Print out jump tables referenced by the function.
641 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000642
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000643 SwitchToSection(getObjFileLowering().SectionForGlobal(F, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000644
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000646 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000647
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000648 O.flush();
649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 // We didn't modify anything.
651 return false;
652}
653
Chris Lattner2b638a72008-02-15 19:04:54 +0000654/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +0000655/// Don't print things like \\n or \\0.
Daniel Dunbar23e2b802009-07-26 07:49:05 +0000656static void PrintUnmangledNameSafely(const Value *V,
657 formatted_raw_ostream &OS) {
658 for (StringRef::iterator it = V->getName().begin(),
659 ie = V->getName().end(); it != ie; ++it)
660 if (isprint(*it))
661 OS << *it;
Chris Lattner2b638a72008-02-15 19:04:54 +0000662}
663
Chris Lattnerae982212009-07-21 18:38:57 +0000664void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665 const TargetData *TD = TM.getTargetData();
666
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000667 if (!GVar->hasInitializer())
668 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000669
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000670 // Check to see if this is a special global used by LLVM, if so, emit it.
671 if (EmitSpecialLLVMGlobal(GVar))
672 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000674 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000676 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000677
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000678 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000679 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000680 return;
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000681 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000682 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000683 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000685 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000686
687 if (C->isNullValue() && /* FIXME: Verify correct */
688 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000689 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000690 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000692
693 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 O << "\t.global " << name << '\n';
695 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000696 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000697 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000698 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000699 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000701 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 }
Evan Cheng11db8142009-03-24 00:17:40 +0000703 if (VerboseAsm) {
704 O << "\t\t" << TAI->getCommentString() << " '";
705 PrintUnmangledNameSafely(GVar, O);
706 O << "'";
707 }
708 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000709 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000710 }
711
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000712 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000713 case GlobalValue::LinkOnceAnyLinkage:
714 case GlobalValue::LinkOnceODRLinkage:
715 case GlobalValue::WeakAnyLinkage:
716 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000717 case GlobalValue::CommonLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000718 O << "\t.global " << name << '\n'
719 << "\t.type " << name << ", @object\n"
720 << "\t.weak " << name << '\n';
721 break;
722 case GlobalValue::AppendingLinkage:
723 // FIXME: appending linkage variables should go into a section of
724 // their name or something. For now, just emit them as external.
725 case GlobalValue::ExternalLinkage:
726 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000727 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000728 << "\t.type " << name << ", @object\n";
729 // FALL THROUGH
730 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000731 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000732 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000733 break;
734 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000735 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000736 }
737
738 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000739 O << name << ":";
740 if (VerboseAsm) {
741 O << "\t\t\t\t" << TAI->getCommentString() << " '";
742 PrintUnmangledNameSafely(GVar, O);
743 O << "'";
744 }
745 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000746
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000747 EmitGlobalConstant(C);
748 O << '\n';
749}
750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752/// runOnMachineFunction - This uses the printMachineInstruction()
753/// method to print assembly for each instruction.
754///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000755bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000756 this->MF = &MF;
757
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 SetupMachineFunction(MF);
759 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000760
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 // Print out constants referenced by the function
762 EmitConstantPool(MF.getConstantPool());
763
764 // Print out labels for the function.
765 const Function *F = MF.getFunction();
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000766 SwitchToSection(getObjFileLowering().SectionForGlobal(F, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000767
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000769 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000770 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000771 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 case Function::InternalLinkage: // Symbols default to internal.
773 break;
774 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000775 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000777 case Function::WeakAnyLinkage:
778 case Function::WeakODRLinkage:
779 case Function::LinkOnceAnyLinkage:
780 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000781 O << "\t.globl\t" << CurrentFnName << '\n';
782 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783 break;
784 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000785
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000786 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000787
Bill Wendling25a8ae32009-06-30 22:38:32 +0000788 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 O << CurrentFnName << ":\n";
790
791 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000792 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000793
Bill Wendling36ccaea2008-01-26 06:51:24 +0000794 // If the function is empty, then we need to emit *something*. Otherwise, the
795 // function's label might be associated with something that it wasn't meant to
796 // be associated with. We emit a noop in this situation.
797 MachineFunction::iterator I = MF.begin();
798
Bill Wendlingb5880a72008-01-26 09:03:52 +0000799 if (++I == MF.end() && MF.front().empty())
800 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000801
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802 // Print out code for the function.
803 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
804 I != E; ++I) {
805 // Print a label for the basic block.
806 if (I != MF.begin()) {
Evan Cheng11db8142009-03-24 00:17:40 +0000807 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 O << '\n';
809 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000810 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
811 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 printMachineInstruction(II);
814 }
815 }
816
817 // Print out jump tables referenced by the function.
818 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000819
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000821 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000822
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823 // We didn't modify anything.
824 return false;
825}
826
827
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000828bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000829 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000830 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 "ppc",
832 "ppc601",
833 "ppc602",
834 "ppc603",
835 "ppc7400",
836 "ppc750",
837 "ppc970",
838 "ppc64"
839 };
840
841 unsigned Directive = Subtarget.getDarwinDirective();
842 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
843 Directive = PPC::DIR_970;
844 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
845 Directive = PPC::DIR_7400;
846 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
847 Directive = PPC::DIR_64;
848 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000849 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000850
Dan Gohman4a558a32007-07-25 19:33:14 +0000851 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000852 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000853
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854 // Prime text sections so they are adjacent. This reduces the likelihood a
855 // large data or debug section causes a branch to exceed 16M limit.
Dale Johannesen3c788322008-01-11 00:54:37 +0000856 SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 "pure_instructions");
858 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000859 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 "pure_instructions,32");
861 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Dale Johannesen3c788322008-01-11 00:54:37 +0000862 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 "pure_instructions,16");
864 }
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000865 SwitchToSection(getObjFileLowering().getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000866
Dan Gohman4a558a32007-07-25 19:33:14 +0000867 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868}
869
Chris Lattnerae982212009-07-21 18:38:57 +0000870void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000871 const TargetData *TD = TM.getTargetData();
872
873 if (!GVar->hasInitializer())
874 return; // External global require no code
875
876 // Check to see if this is a special global used by LLVM, if so, emit it.
877 if (EmitSpecialLLVMGlobal(GVar)) {
878 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000879 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000880 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000881 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000882 O << ".reference .destructors_used\n";
883 }
884 return;
885 }
886
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000887 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000888 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000889
890 Constant *C = GVar->getInitializer();
891 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000892 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000893 unsigned Align = TD->getPreferredAlignmentLog(GVar);
894
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000895 const Section *TheSection = getObjFileLowering().SectionForGlobal(GVar, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000896 SwitchToSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000897
898 if (C->isNullValue() && /* FIXME: Verify correct */
899 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000900 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000901 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000902 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000903 !TheSection->getKind().isMergeableCString()) {
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:
Bill Wendling41a07852009-07-20 01:03:30 +0000957 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000958 break;
959 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000960 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000961 }
962
963 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000964 O << name << ":";
965 if (VerboseAsm) {
966 O << "\t\t\t\t" << TAI->getCommentString() << " '";
967 PrintUnmangledNameSafely(GVar, O);
968 O << "'";
969 }
970 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000971
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000972 EmitGlobalConstant(C);
973 O << '\n';
974}
975
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000976bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 const TargetData *TD = TM.getTargetData();
978
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979 bool isPPC64 = TD->getPointerSizeInBits() == 64;
980
981 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +0000982 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000983 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000984 I != E; ++I) {
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +0000985 SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
986 "pure_instructions,32");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +0000988 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +0000989 O << Info.Stub << ":\n";
990 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +0000992 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
993 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +0000995 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +0000996 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +0000998 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
999 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 O << "\tmtctr r12\n";
1001 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001002
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001003 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001004 O << Info.LazyPtr << ":\n";
1005 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001006 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007 }
Chris Lattner189198f2009-07-15 00:55:58 +00001008 } else if (!FnStubs.empty()) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001009 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001010 I != E; ++I) {
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001011 SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1012 "pure_instructions,16");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001014 const FnStubInfo &Info = I->second;
1015 O << Info.Stub << ":\n";
1016 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1017 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001018 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001019 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 O << "\tmtctr r12\n";
1021 O << "\tbctr\n";
1022 SwitchToDataSection(".lazy_symbol_pointer");
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001023 O << Info.LazyPtr << ":\n";
1024 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001025 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 }
1027 }
1028
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001029 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030
Dale Johannesen85535762008-04-02 00:25:04 +00001031 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001032 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001033 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001034 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001035 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001036 E = Personalities.end(); I != E; ++I) {
1037 if (*I)
1038 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001039 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001040 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001041 }
1042
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043 // Output stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001044 if (!GVStubs.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045 SwitchToDataSection(".non_lazy_symbol_pointer");
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001046 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1047 E = GVStubs.end(); I != E; ++I) {
1048 O << I->second << ":\n";
1049 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1050 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051 }
1052 }
1053
Evan Chenga65854f2008-12-05 01:06:39 +00001054 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001055 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001056 EmitAlignment(isPPC64 ? 3 : 2);
1057 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1058 E = HiddenGVStubs.end(); I != E; ++I) {
1059 O << I->second << ":\n";
1060 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001061 }
1062 }
1063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 // Funny Darwin hack: This flag tells the linker that no global symbols
1065 // contain code that falls through to other global symbols (e.g. the obvious
1066 // implementation of multiple entry points). If this doesn't occur, the
1067 // linker can safely perform dead code stripping. Since LLVM never generates
1068 // code that does this, it is always safe to set.
1069 O << "\t.subsections_via_symbols\n";
1070
Dan Gohman4a558a32007-07-25 19:33:14 +00001071 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072}
1073
1074
1075
1076/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1077/// for a MachineFunction to the given output stream, in a format that the
1078/// Darwin assembler can deal with.
1079///
Daniel Dunbarc680b012009-07-25 06:49:55 +00001080static FunctionPass *createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001081 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +00001082 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001083 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1084
Chris Lattnerae982212009-07-21 18:38:57 +00001085 if (Subtarget->isDarwin())
Daniel Dunbarb10d2222009-07-01 01:48:54 +00001086 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Chris Lattnerae982212009-07-21 18:38:57 +00001087 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001089
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001090// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001091extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001092 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1093
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001094 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1095}