blob: 9884ca32bfa2677d7080b79cf23359aaa21dbc77 [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.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000119 void printInstruction(const MachineInstr *MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
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
Chris Lattner7d337492009-08-08 00:05:42 +0000574 printInstruction(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575}
576
577/// runOnMachineFunction - This uses the printMachineInstruction()
578/// method to print assembly for each instruction.
579///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000580bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000581 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582
583 SetupMachineFunction(MF);
584 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000585
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 // Print out constants referenced by the function
587 EmitConstantPool(MF.getConstantPool());
588
589 // Print out labels for the function.
590 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000591 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000592
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000594 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000595 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000596 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 case Function::InternalLinkage: // Symbols default to internal.
598 break;
599 case Function::ExternalLinkage:
600 O << "\t.global\t" << CurrentFnName << '\n'
601 << "\t.type\t" << CurrentFnName << ", @function\n";
602 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000603 case Function::WeakAnyLinkage:
604 case Function::WeakODRLinkage:
605 case Function::LinkOnceAnyLinkage:
606 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 O << "\t.global\t" << CurrentFnName << '\n';
608 O << "\t.weak\t" << CurrentFnName << '\n';
609 break;
610 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000611
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000612 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000613
Bill Wendling25a8ae32009-06-30 22:38:32 +0000614 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615 O << CurrentFnName << ":\n";
616
617 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000618 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619
620 // Print out code for the function.
621 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
622 I != E; ++I) {
623 // Print a label for the basic block.
624 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000625 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626 O << '\n';
627 }
628 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
629 II != E; ++II) {
630 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 printMachineInstruction(II);
632 }
633 }
634
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000635 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636
637 // Print out jump tables referenced by the function.
638 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000639
Chris Lattner2931fe42009-07-29 05:09:30 +0000640 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000641
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000643 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000644
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 // We didn't modify anything.
646 return false;
647}
648
Chris Lattner2b638a72008-02-15 19:04:54 +0000649/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +0000650/// Don't print things like \\n or \\0.
Daniel Dunbar23e2b802009-07-26 07:49:05 +0000651static void PrintUnmangledNameSafely(const Value *V,
652 formatted_raw_ostream &OS) {
653 for (StringRef::iterator it = V->getName().begin(),
654 ie = V->getName().end(); it != ie; ++it)
655 if (isprint(*it))
656 OS << *it;
Chris Lattner2b638a72008-02-15 19:04:54 +0000657}
658
Chris Lattnerae982212009-07-21 18:38:57 +0000659void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 const TargetData *TD = TM.getTargetData();
661
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000662 if (!GVar->hasInitializer())
663 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000664
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000665 // Check to see if this is a special global used by LLVM, if so, emit it.
666 if (EmitSpecialLLVMGlobal(GVar))
667 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000669 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000671 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000672
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000673 Constant *C = GVar->getInitializer();
674 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000675 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000676 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677
Chris Lattner2931fe42009-07-29 05:09:30 +0000678 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000679
680 if (C->isNullValue() && /* FIXME: Verify correct */
681 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000682 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000683 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000684 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000685
686 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 O << "\t.global " << name << '\n';
688 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000689 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000690 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000691 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000692 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000694 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 }
Evan Cheng11db8142009-03-24 00:17:40 +0000696 if (VerboseAsm) {
697 O << "\t\t" << TAI->getCommentString() << " '";
698 PrintUnmangledNameSafely(GVar, O);
699 O << "'";
700 }
701 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000702 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 }
704
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000705 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000706 case GlobalValue::LinkOnceAnyLinkage:
707 case GlobalValue::LinkOnceODRLinkage:
708 case GlobalValue::WeakAnyLinkage:
709 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000710 case GlobalValue::CommonLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000711 O << "\t.global " << name << '\n'
712 << "\t.type " << name << ", @object\n"
713 << "\t.weak " << name << '\n';
714 break;
715 case GlobalValue::AppendingLinkage:
716 // FIXME: appending linkage variables should go into a section of
717 // their name or something. For now, just emit them as external.
718 case GlobalValue::ExternalLinkage:
719 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000720 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000721 << "\t.type " << name << ", @object\n";
722 // FALL THROUGH
723 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000724 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000725 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000726 break;
727 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000728 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000729 }
730
731 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000732 O << name << ":";
733 if (VerboseAsm) {
734 O << "\t\t\t\t" << TAI->getCommentString() << " '";
735 PrintUnmangledNameSafely(GVar, O);
736 O << "'";
737 }
738 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000739
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000740 EmitGlobalConstant(C);
741 O << '\n';
742}
743
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745/// runOnMachineFunction - This uses the printMachineInstruction()
746/// method to print assembly for each instruction.
747///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000748bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000749 this->MF = &MF;
750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751 SetupMachineFunction(MF);
752 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000753
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 // Print out constants referenced by the function
755 EmitConstantPool(MF.getConstantPool());
756
757 // Print out labels for the function.
758 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000759 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000760
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000762 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000763 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000764 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000765 case Function::InternalLinkage: // Symbols default to internal.
766 break;
767 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000768 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000770 case Function::WeakAnyLinkage:
771 case Function::WeakODRLinkage:
772 case Function::LinkOnceAnyLinkage:
773 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000774 O << "\t.globl\t" << CurrentFnName << '\n';
775 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 break;
777 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000778
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000779 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000780
Bill Wendling25a8ae32009-06-30 22:38:32 +0000781 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 O << CurrentFnName << ":\n";
783
784 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000785 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786
Bill Wendling36ccaea2008-01-26 06:51:24 +0000787 // If the function is empty, then we need to emit *something*. Otherwise, the
788 // function's label might be associated with something that it wasn't meant to
789 // be associated with. We emit a noop in this situation.
790 MachineFunction::iterator I = MF.begin();
791
Bill Wendlingb5880a72008-01-26 09:03:52 +0000792 if (++I == MF.end() && MF.front().empty())
793 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000794
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795 // Print out code for the function.
796 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
797 I != E; ++I) {
798 // Print a label for the basic block.
799 if (I != MF.begin()) {
Evan Cheng11db8142009-03-24 00:17:40 +0000800 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 O << '\n';
802 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000803 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
804 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 printMachineInstruction(II);
807 }
808 }
809
810 // Print out jump tables referenced by the function.
811 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000812
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000814 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000815
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 // We didn't modify anything.
817 return false;
818}
819
820
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000821bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000822 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000823 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 "ppc",
825 "ppc601",
826 "ppc602",
827 "ppc603",
828 "ppc7400",
829 "ppc750",
830 "ppc970",
831 "ppc64"
832 };
833
834 unsigned Directive = Subtarget.getDarwinDirective();
835 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
836 Directive = PPC::DIR_970;
837 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
838 Directive = PPC::DIR_7400;
839 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
840 Directive = PPC::DIR_64;
841 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000842 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000843
Dan Gohman4a558a32007-07-25 19:33:14 +0000844 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000845 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000846
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 // Prime text sections so they are adjacent. This reduces the likelihood a
848 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000849
850 TargetLoweringObjectFileMachO &TLOFMacho =
851 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
852 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__textcoal_nt,"
853 "coalesced,pure_instructions", true,
854 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000856 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstu"
857 "b1,symbol_stubs,"
858 "pure_instructions,32", true,
859 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000860 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000861 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1"
862 ",symbol_stubs,"
863 "pure_instructions,16", true,
864 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 }
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000866 SwitchToSection(getObjFileLowering().getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000867
Dan Gohman4a558a32007-07-25 19:33:14 +0000868 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869}
870
Chris Lattnerae982212009-07-21 18:38:57 +0000871void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000872 const TargetData *TD = TM.getTargetData();
873
874 if (!GVar->hasInitializer())
875 return; // External global require no code
876
877 // Check to see if this is a special global used by LLVM, if so, emit it.
878 if (EmitSpecialLLVMGlobal(GVar)) {
879 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000880 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000881 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000882 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000883 O << ".reference .destructors_used\n";
884 }
885 return;
886 }
887
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000888 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000889 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000890
891 Constant *C = GVar->getInitializer();
892 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000893 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000894 unsigned Align = TD->getPreferredAlignmentLog(GVar);
895
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000896 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000897 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000898 SwitchToSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000899
Chris Lattnerdb727932009-08-04 05:35:56 +0000900 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000901 if (C->isNullValue() && /* FIXME: Verify correct */
902 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000903 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000904 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000905 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000906 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000907 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
908
909 if (GVar->hasExternalLinkage()) {
910 O << "\t.globl " << name << '\n';
911 O << "\t.zerofill __DATA, __common, " << name << ", "
912 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000913 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000914 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000915 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000916 O << "\t.globl " << name << '\n'
917 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000918 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000919 O << name << ":";
920 if (VerboseAsm) {
921 O << "\t\t\t\t" << TAI->getCommentString() << " ";
922 PrintUnmangledNameSafely(GVar, O);
923 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000924 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000925 EmitGlobalConstant(C);
926 return;
927 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000928 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000929 // Darwin 9 and above support aligned common data.
930 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000931 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000932 }
Evan Cheng11db8142009-03-24 00:17:40 +0000933 if (VerboseAsm) {
934 O << "\t\t" << TAI->getCommentString() << " '";
935 PrintUnmangledNameSafely(GVar, O);
936 O << "'";
937 }
938 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000939 return;
940 }
941
942 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000943 case GlobalValue::LinkOnceAnyLinkage:
944 case GlobalValue::LinkOnceODRLinkage:
945 case GlobalValue::WeakAnyLinkage:
946 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000947 case GlobalValue::CommonLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000948 O << "\t.globl " << name << '\n'
949 << "\t.weak_definition " << name << '\n';
950 break;
951 case GlobalValue::AppendingLinkage:
952 // FIXME: appending linkage variables should go into a section of
953 // their name or something. For now, just emit them as external.
954 case GlobalValue::ExternalLinkage:
955 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000956 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000957 // FALL THROUGH
958 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +0000959 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000960 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000961 break;
962 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000963 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000964 }
965
966 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000967 O << name << ":";
968 if (VerboseAsm) {
969 O << "\t\t\t\t" << TAI->getCommentString() << " '";
970 PrintUnmangledNameSafely(GVar, O);
971 O << "'";
972 }
973 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000974
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000975 EmitGlobalConstant(C);
976 O << '\n';
977}
978
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000979bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980 const TargetData *TD = TM.getTargetData();
981
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 bool isPPC64 = TD->getPointerSizeInBits() == 64;
983
Chris Lattnerf4815552009-08-03 22:52:21 +0000984 // Darwin/PPC always uses mach-o.
985 TargetLoweringObjectFileMachO &TLOFMacho =
986 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
987
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +0000989 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000990 const MCSection *StubSection =
991 TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstub1,"
992 "symbol_stubs,pure_instructions,32", true,
993 SectionKind::getText());
994 const MCSection *LSPSection =
995 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
996 SectionKind::getMetadata());
997
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000998 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000999 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001000 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001002 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001003 O << Info.Stub << ":\n";
1004 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001006 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1007 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001009 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001010 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001012 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1013 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014 O << "\tmtctr r12\n";
1015 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001016
Chris Lattnerf4815552009-08-03 22:52:21 +00001017 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001018 O << Info.LazyPtr << ":\n";
1019 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001020 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 }
Chris Lattner189198f2009-07-15 00:55:58 +00001022 } else if (!FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001023 const MCSection *StubSection =
1024 TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1025 "pure_instructions,16", true,
1026 SectionKind::getText());
1027 const MCSection *LSPSection =
1028 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
1029 SectionKind::getMetadata());
1030
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001031 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001032 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001033 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001035 const FnStubInfo &Info = I->second;
1036 O << Info.Stub << ":\n";
1037 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1038 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001039 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001040 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 O << "\tmtctr r12\n";
1042 O << "\tbctr\n";
Chris Lattnerf4815552009-08-03 22:52:21 +00001043 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001044 O << Info.LazyPtr << ":\n";
1045 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001046 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047 }
1048 }
1049
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001050 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001051
Dale Johannesen85535762008-04-02 00:25:04 +00001052 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001053 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001054 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001055 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001056 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001057 E = Personalities.end(); I != E; ++I) {
1058 if (*I)
1059 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001060 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001061 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001062 }
1063
Chris Lattnerf4815552009-08-03 22:52:21 +00001064 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001065 if (!GVStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001066 const MCSection *TheSection =
1067 TLOFMacho.getMachOSection(".non_lazy_symbol_pointer", true,
1068 SectionKind::getMetadata());
1069 SwitchToSection(TheSection);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001070 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1071 E = GVStubs.end(); I != E; ++I) {
1072 O << I->second << ":\n";
1073 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1074 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 }
1076 }
1077
Evan Chenga65854f2008-12-05 01:06:39 +00001078 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001079 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001080 EmitAlignment(isPPC64 ? 3 : 2);
1081 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1082 E = HiddenGVStubs.end(); I != E; ++I) {
1083 O << I->second << ":\n";
1084 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001085 }
1086 }
1087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088 // Funny Darwin hack: This flag tells the linker that no global symbols
1089 // contain code that falls through to other global symbols (e.g. the obvious
1090 // implementation of multiple entry points). If this doesn't occur, the
1091 // linker can safely perform dead code stripping. Since LLVM never generates
1092 // code that does this, it is always safe to set.
1093 O << "\t.subsections_via_symbols\n";
1094
Dan Gohman4a558a32007-07-25 19:33:14 +00001095 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096}
1097
1098
1099
1100/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1101/// for a MachineFunction to the given output stream, in a format that the
1102/// Darwin assembler can deal with.
1103///
Daniel Dunbarc680b012009-07-25 06:49:55 +00001104static FunctionPass *createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001105 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +00001106 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001107 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1108
Chris Lattnerae982212009-07-21 18:38:57 +00001109 if (Subtarget->isDarwin())
Daniel Dunbarb10d2222009-07-01 01:48:54 +00001110 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Chris Lattnerae982212009-07-21 18:38:57 +00001111 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001113
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001114// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001115extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001116 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1117
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001118 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1119}