blob: d7e050f64097afc4686c36d8bc148153c6f55755 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
12// the output mechanism used by `llc'.
13//
14// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "asmprinter"
20#include "PPC.h"
21#include "PPCPredicates.h"
22#include "PPCTargetMachine.h"
23#include "PPCSubtarget.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Assembly/Writer.h"
28#include "llvm/CodeGen/AsmPrinter.h"
29#include "llvm/CodeGen/DwarfWriter.h"
30#include "llvm/CodeGen/MachineModuleInfo.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
32#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling36ccaea2008-01-26 06:51:24 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000034#include "llvm/MC/MCSection.h"
35#include "llvm/Target/TargetAsmInfo.h"
36#include "llvm/Target/TargetLoweringObjectFile.h"
37#include "llvm/Target/TargetRegisterInfo.h"
38#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetOptions.h"
40#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/Support/Mangler.h"
42#include "llvm/Support/MathExtras.h"
43#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000045#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000047#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048#include "llvm/ADT/Statistic.h"
49#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000050#include "llvm/ADT/StringSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051using namespace llvm;
52
53STATISTIC(EmittedInsts, "Number of machine instrs printed");
54
55namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000056 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
57 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000058 struct FnStubInfo {
59 std::string Stub, LazyPtr, AnonSymbol;
60
61 FnStubInfo() {}
62
63 void Init(const GlobalValue *GV, Mangler *Mang) {
64 // Already initialized.
65 if (!Stub.empty()) return;
66 Stub = Mang->getMangledName(GV, "$stub", true);
67 LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true);
68 AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true);
69 }
70
71 void Init(const std::string &GV, Mangler *Mang) {
72 // Already initialized.
73 if (!Stub.empty()) return;
Bill Wendling41a07852009-07-20 01:03:30 +000074 Stub = Mang->makeNameProper(GV + "$stub",
Bill Wendlinge4699112009-07-20 19:41:27 +000075 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000076 LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr",
Bill Wendlinge4699112009-07-20 19:41:27 +000077 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000078 AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp",
Bill Wendlinge4699112009-07-20 19:41:27 +000079 Mangler::Private);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000080 }
81 };
82
83 StringMap<FnStubInfo> FnStubs;
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 Lattner2931fe42009-07-29 05:09:30 +0000594 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, 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 Lattner2931fe42009-07-29 05:09:30 +0000643 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, 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 Gohmanf17a25c2007-07-18 16:29:46 +0000648 // We didn't modify anything.
649 return false;
650}
651
Chris Lattner2b638a72008-02-15 19:04:54 +0000652/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +0000653/// Don't print things like \\n or \\0.
Daniel Dunbar23e2b802009-07-26 07:49:05 +0000654static void PrintUnmangledNameSafely(const Value *V,
655 formatted_raw_ostream &OS) {
656 for (StringRef::iterator it = V->getName().begin(),
657 ie = V->getName().end(); it != ie; ++it)
658 if (isprint(*it))
659 OS << *it;
Chris Lattner2b638a72008-02-15 19:04:54 +0000660}
661
Chris Lattnerae982212009-07-21 18:38:57 +0000662void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 const TargetData *TD = TM.getTargetData();
664
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000665 if (!GVar->hasInitializer())
666 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000667
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000668 // Check to see if this is a special global used by LLVM, if so, emit it.
669 if (EmitSpecialLLVMGlobal(GVar))
670 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000672 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000674 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000675
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000676 Constant *C = GVar->getInitializer();
677 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000678 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000679 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680
Chris Lattner2931fe42009-07-29 05:09:30 +0000681 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000682
683 if (C->isNullValue() && /* FIXME: Verify correct */
684 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000685 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000686 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000688
689 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690 O << "\t.global " << name << '\n';
691 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000692 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000693 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000694 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000695 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000697 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 }
Evan Cheng11db8142009-03-24 00:17:40 +0000699 if (VerboseAsm) {
700 O << "\t\t" << TAI->getCommentString() << " '";
701 PrintUnmangledNameSafely(GVar, O);
702 O << "'";
703 }
704 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000705 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 }
707
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000708 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000709 case GlobalValue::LinkOnceAnyLinkage:
710 case GlobalValue::LinkOnceODRLinkage:
711 case GlobalValue::WeakAnyLinkage:
712 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000713 case GlobalValue::CommonLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000714 O << "\t.global " << name << '\n'
715 << "\t.type " << name << ", @object\n"
716 << "\t.weak " << name << '\n';
717 break;
718 case GlobalValue::AppendingLinkage:
719 // FIXME: appending linkage variables should go into a section of
720 // their name or something. For now, just emit them as external.
721 case GlobalValue::ExternalLinkage:
722 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000723 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000724 << "\t.type " << name << ", @object\n";
725 // FALL THROUGH
726 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000727 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000728 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000729 break;
730 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000731 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000732 }
733
734 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000735 O << name << ":";
736 if (VerboseAsm) {
737 O << "\t\t\t\t" << TAI->getCommentString() << " '";
738 PrintUnmangledNameSafely(GVar, O);
739 O << "'";
740 }
741 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000742
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000743 EmitGlobalConstant(C);
744 O << '\n';
745}
746
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000748/// runOnMachineFunction - This uses the printMachineInstruction()
749/// method to print assembly for each instruction.
750///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000751bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000752 this->MF = &MF;
753
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 SetupMachineFunction(MF);
755 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 // Print out constants referenced by the function
758 EmitConstantPool(MF.getConstantPool());
759
760 // Print out labels for the function.
761 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000762 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000763
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000765 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000766 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000767 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 case Function::InternalLinkage: // Symbols default to internal.
769 break;
770 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000771 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000773 case Function::WeakAnyLinkage:
774 case Function::WeakODRLinkage:
775 case Function::LinkOnceAnyLinkage:
776 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000777 O << "\t.globl\t" << CurrentFnName << '\n';
778 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 break;
780 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000781
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000782 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000783
Bill Wendling25a8ae32009-06-30 22:38:32 +0000784 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785 O << CurrentFnName << ":\n";
786
787 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000788 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789
Bill Wendling36ccaea2008-01-26 06:51:24 +0000790 // If the function is empty, then we need to emit *something*. Otherwise, the
791 // function's label might be associated with something that it wasn't meant to
792 // be associated with. We emit a noop in this situation.
793 MachineFunction::iterator I = MF.begin();
794
Bill Wendlingb5880a72008-01-26 09:03:52 +0000795 if (++I == MF.end() && MF.front().empty())
796 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000797
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 // Print out code for the function.
799 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
800 I != E; ++I) {
801 // Print a label for the basic block.
802 if (I != MF.begin()) {
Evan Cheng11db8142009-03-24 00:17:40 +0000803 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 O << '\n';
805 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000806 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
807 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 printMachineInstruction(II);
810 }
811 }
812
813 // Print out jump tables referenced by the function.
814 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000815
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000817 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000818
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 // We didn't modify anything.
820 return false;
821}
822
823
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000824bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000825 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000826 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000827 "ppc",
828 "ppc601",
829 "ppc602",
830 "ppc603",
831 "ppc7400",
832 "ppc750",
833 "ppc970",
834 "ppc64"
835 };
836
837 unsigned Directive = Subtarget.getDarwinDirective();
838 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
839 Directive = PPC::DIR_970;
840 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
841 Directive = PPC::DIR_7400;
842 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
843 Directive = PPC::DIR_64;
844 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000845 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000846
Dan Gohman4a558a32007-07-25 19:33:14 +0000847 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000848 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000849
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 // Prime text sections so they are adjacent. This reduces the likelihood a
851 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000852
853 TargetLoweringObjectFileMachO &TLOFMacho =
854 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
855 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__textcoal_nt,"
856 "coalesced,pure_instructions", true,
857 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000859 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstu"
860 "b1,symbol_stubs,"
861 "pure_instructions,32", true,
862 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000864 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1"
865 ",symbol_stubs,"
866 "pure_instructions,16", true,
867 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868 }
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000869 SwitchToSection(getObjFileLowering().getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000870
Dan Gohman4a558a32007-07-25 19:33:14 +0000871 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872}
873
Chris Lattnerae982212009-07-21 18:38:57 +0000874void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000875 const TargetData *TD = TM.getTargetData();
876
877 if (!GVar->hasInitializer())
878 return; // External global require no code
879
880 // Check to see if this is a special global used by LLVM, if so, emit it.
881 if (EmitSpecialLLVMGlobal(GVar)) {
882 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000883 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000884 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000885 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000886 O << ".reference .destructors_used\n";
887 }
888 return;
889 }
890
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000891 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000892 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000893
894 Constant *C = GVar->getInitializer();
895 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000896 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000897 unsigned Align = TD->getPreferredAlignmentLog(GVar);
898
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000899 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000900 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000901 SwitchToSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000902
Chris Lattnerdb727932009-08-04 05:35:56 +0000903 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000904 if (C->isNullValue() && /* FIXME: Verify correct */
905 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000906 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000907 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000908 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000909 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000910 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
911
912 if (GVar->hasExternalLinkage()) {
913 O << "\t.globl " << name << '\n';
914 O << "\t.zerofill __DATA, __common, " << name << ", "
915 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000916 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000917 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000918 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000919 O << "\t.globl " << name << '\n'
920 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000921 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000922 O << name << ":";
923 if (VerboseAsm) {
924 O << "\t\t\t\t" << TAI->getCommentString() << " ";
925 PrintUnmangledNameSafely(GVar, O);
926 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000927 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000928 EmitGlobalConstant(C);
929 return;
930 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000931 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000932 // Darwin 9 and above support aligned common data.
933 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000934 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000935 }
Evan Cheng11db8142009-03-24 00:17:40 +0000936 if (VerboseAsm) {
937 O << "\t\t" << TAI->getCommentString() << " '";
938 PrintUnmangledNameSafely(GVar, O);
939 O << "'";
940 }
941 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000942 return;
943 }
944
945 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000946 case GlobalValue::LinkOnceAnyLinkage:
947 case GlobalValue::LinkOnceODRLinkage:
948 case GlobalValue::WeakAnyLinkage:
949 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000950 case GlobalValue::CommonLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000951 O << "\t.globl " << name << '\n'
952 << "\t.weak_definition " << name << '\n';
953 break;
954 case GlobalValue::AppendingLinkage:
955 // FIXME: appending linkage variables should go into a section of
956 // their name or something. For now, just emit them as external.
957 case GlobalValue::ExternalLinkage:
958 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000959 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000960 // FALL THROUGH
961 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +0000962 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000963 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000964 break;
965 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000966 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000967 }
968
969 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000970 O << name << ":";
971 if (VerboseAsm) {
972 O << "\t\t\t\t" << TAI->getCommentString() << " '";
973 PrintUnmangledNameSafely(GVar, O);
974 O << "'";
975 }
976 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000977
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000978 EmitGlobalConstant(C);
979 O << '\n';
980}
981
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000982bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 const TargetData *TD = TM.getTargetData();
984
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 bool isPPC64 = TD->getPointerSizeInBits() == 64;
986
Chris Lattnerf4815552009-08-03 22:52:21 +0000987 // Darwin/PPC always uses mach-o.
988 TargetLoweringObjectFileMachO &TLOFMacho =
989 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
990
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +0000992 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000993 const MCSection *StubSection =
994 TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstub1,"
995 "symbol_stubs,pure_instructions,32", true,
996 SectionKind::getText());
997 const MCSection *LSPSection =
998 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
999 SectionKind::getMetadata());
1000
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001001 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001002 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001003 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001004 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001005 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001006 O << Info.Stub << ":\n";
1007 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001009 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1010 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001012 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001013 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001015 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1016 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 O << "\tmtctr r12\n";
1018 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001019
Chris Lattnerf4815552009-08-03 22:52:21 +00001020 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001021 O << Info.LazyPtr << ":\n";
1022 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001023 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024 }
Chris Lattner189198f2009-07-15 00:55:58 +00001025 } else if (!FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001026 const MCSection *StubSection =
1027 TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1028 "pure_instructions,16", true,
1029 SectionKind::getText());
1030 const MCSection *LSPSection =
1031 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
1032 SectionKind::getMetadata());
1033
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001034 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001035 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001036 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001038 const FnStubInfo &Info = I->second;
1039 O << Info.Stub << ":\n";
1040 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1041 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001042 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001043 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 O << "\tmtctr r12\n";
1045 O << "\tbctr\n";
Chris Lattnerf4815552009-08-03 22:52:21 +00001046 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001047 O << Info.LazyPtr << ":\n";
1048 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001049 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001050 }
1051 }
1052
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001053 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054
Dale Johannesen85535762008-04-02 00:25:04 +00001055 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001056 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001057 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001058 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001059 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001060 E = Personalities.end(); I != E; ++I) {
1061 if (*I)
1062 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001063 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001064 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001065 }
1066
Chris Lattnerf4815552009-08-03 22:52:21 +00001067 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001068 if (!GVStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001069 const MCSection *TheSection =
1070 TLOFMacho.getMachOSection(".non_lazy_symbol_pointer", true,
1071 SectionKind::getMetadata());
1072 SwitchToSection(TheSection);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001073 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1074 E = GVStubs.end(); I != E; ++I) {
1075 O << I->second << ":\n";
1076 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1077 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 }
1079 }
1080
Evan Chenga65854f2008-12-05 01:06:39 +00001081 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001082 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001083 EmitAlignment(isPPC64 ? 3 : 2);
1084 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1085 E = HiddenGVStubs.end(); I != E; ++I) {
1086 O << I->second << ":\n";
1087 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001088 }
1089 }
1090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001091 // Funny Darwin hack: This flag tells the linker that no global symbols
1092 // contain code that falls through to other global symbols (e.g. the obvious
1093 // implementation of multiple entry points). If this doesn't occur, the
1094 // linker can safely perform dead code stripping. Since LLVM never generates
1095 // code that does this, it is always safe to set.
1096 O << "\t.subsections_via_symbols\n";
1097
Dan Gohman4a558a32007-07-25 19:33:14 +00001098 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099}
1100
1101
1102
1103/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1104/// for a MachineFunction to the given output stream, in a format that the
1105/// Darwin assembler can deal with.
1106///
Daniel Dunbarc680b012009-07-25 06:49:55 +00001107static FunctionPass *createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001108 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +00001109 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001110 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1111
Chris Lattnerae982212009-07-21 18:38:57 +00001112 if (Subtarget->isDarwin())
Daniel Dunbarb10d2222009-07-01 01:48:54 +00001113 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Chris Lattnerae982212009-07-21 18:38:57 +00001114 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001115}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001116
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001117// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001118extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001119 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1120
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001121 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1122}