blob: 5794a1797e9ed874bc7531d13fe6681065212c35 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly --------=//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
12// the output mechanism used by `llc'.
13//
14// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "asmprinter"
20#include "PPC.h"
21#include "PPCPredicates.h"
22#include "PPCTargetMachine.h"
23#include "PPCSubtarget.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
Devang Patel7c368852009-07-28 21:49:47 +000027#include "llvm/Metadata.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Assembly/Writer.h"
29#include "llvm/CodeGen/AsmPrinter.h"
30#include "llvm/CodeGen/DwarfWriter.h"
31#include "llvm/CodeGen/MachineModuleInfo.h"
32#include "llvm/CodeGen/MachineFunctionPass.h"
33#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling36ccaea2008-01-26 06:51:24 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000035#include "llvm/MC/MCSection.h"
36#include "llvm/Target/TargetAsmInfo.h"
37#include "llvm/Target/TargetLoweringObjectFile.h"
38#include "llvm/Target/TargetRegisterInfo.h"
39#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetOptions.h"
41#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include "llvm/Support/Mangler.h"
43#include "llvm/Support/MathExtras.h"
44#include "llvm/Support/CommandLine.h"
45#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000046#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047#include "llvm/Support/Compiler.h"
David Greene302008d2009-07-14 20:18:05 +000048#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049#include "llvm/ADT/Statistic.h"
50#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000051#include "llvm/ADT/StringSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052using namespace llvm;
53
54STATISTIC(EmittedInsts, "Number of machine instrs printed");
55
56namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000057 class VISIBILITY_HIDDEN PPCAsmPrinter : public AsmPrinter {
58 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000059 struct FnStubInfo {
60 std::string Stub, LazyPtr, AnonSymbol;
61
62 FnStubInfo() {}
63
64 void Init(const GlobalValue *GV, Mangler *Mang) {
65 // Already initialized.
66 if (!Stub.empty()) return;
67 Stub = Mang->getMangledName(GV, "$stub", true);
68 LazyPtr = Mang->getMangledName(GV, "$lazy_ptr", true);
69 AnonSymbol = Mang->getMangledName(GV, "$stub$tmp", true);
70 }
71
72 void Init(const std::string &GV, Mangler *Mang) {
73 // Already initialized.
74 if (!Stub.empty()) return;
Bill Wendling41a07852009-07-20 01:03:30 +000075 Stub = Mang->makeNameProper(GV + "$stub",
Bill Wendlinge4699112009-07-20 19:41:27 +000076 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000077 LazyPtr = Mang->makeNameProper(GV + "$lazy_ptr",
Bill Wendlinge4699112009-07-20 19:41:27 +000078 Mangler::Private);
Bill Wendling41a07852009-07-20 01:03:30 +000079 AnonSymbol = Mang->makeNameProper(GV + "$stub$tmp",
Bill Wendlinge4699112009-07-20 19:41:27 +000080 Mangler::Private);
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000081 }
82 };
83
84 StringMap<FnStubInfo> FnStubs;
Chris Lattner83cf1ec2009-07-15 01:14:44 +000085 StringMap<std::string> GVStubs, HiddenGVStubs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 const PPCSubtarget &Subtarget;
Bill Wendling4f405312009-02-24 08:30:20 +000087 public:
David Greene302008d2009-07-14 20:18:05 +000088 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000089 const TargetAsmInfo *T, bool V)
90 : AsmPrinter(O, TM, T, V),
Bill Wendling4f405312009-02-24 08:30:20 +000091 Subtarget(TM.getSubtarget<PPCSubtarget>()) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092
93 virtual const char *getPassName() const {
94 return "PowerPC Assembly Printer";
95 }
96
97 PPCTargetMachine &getTM() {
98 return static_cast<PPCTargetMachine&>(TM);
99 }
100
101 unsigned enumRegToMachineReg(unsigned enumReg) {
102 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000103 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 case PPC::CR0: return 0;
105 case PPC::CR1: return 1;
106 case PPC::CR2: return 2;
107 case PPC::CR3: return 3;
108 case PPC::CR4: return 4;
109 case PPC::CR5: return 5;
110 case PPC::CR6: return 6;
111 case PPC::CR7: return 7;
112 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000113 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 }
115
116 /// printInstruction - This method is automatically generated by tablegen
117 /// from the instruction set description. This method returns true if the
118 /// machine instruction was sufficiently described to print it, otherwise it
119 /// returns false.
120 bool printInstruction(const MachineInstr *MI);
121
122 void printMachineInstruction(const MachineInstr *MI);
123 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 /// stripRegisterPrefix - This method strips the character prefix from a
126 /// register name so that only the number is left. Used by for linux asm.
127 const char *stripRegisterPrefix(const char *RegName) {
128 switch (RegName[0]) {
129 case 'r':
130 case 'f':
131 case 'v': return RegName + 1;
132 case 'c': if (RegName[1] == 'r') return RegName + 2;
133 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 return RegName;
136 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 /// printRegister - Print register according to target requirements.
139 ///
140 void printRegister(const MachineOperand &MO, bool R0AsZero) {
141 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000142 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000143
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 // If we should use 0 for R0.
145 if (R0AsZero && RegNo == PPC::R0) {
146 O << "0";
147 return;
148 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000149
Bill Wendling8eeb9792008-02-26 21:11:01 +0000150 const char *RegName = TM.getRegisterInfo()->get(RegNo).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 // Linux assembler (Others?) does not take register mnemonics.
152 // FIXME - What about special registers used in mfspr/mtspr?
153 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
154 O << RegName;
155 }
156
157 void printOperand(const MachineInstr *MI, unsigned OpNo) {
158 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000159 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000161 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000162 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 } else {
164 printOp(MO);
165 }
166 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000167
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
169 unsigned AsmVariant, const char *ExtraCode);
170 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
171 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000172
173
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000175 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 value = (value << (32-5)) >> (32-5);
177 O << (int)value;
178 }
179 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000180 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 assert(value <= 31 && "Invalid u5imm argument!");
182 O << (unsigned int)value;
183 }
184 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000185 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 assert(value <= 63 && "Invalid u6imm argument!");
187 O << (unsigned int)value;
188 }
189 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000190 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 }
192 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000193 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 }
195 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000196 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000197 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 } else {
199 O << "lo16(";
200 printOp(MI->getOperand(OpNo));
201 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000202 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 else
204 O << ')';
205 }
206 }
207 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
208 // Branches can take an immediate operand. This is used by the branch
209 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000210 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000211 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 } else {
213 printOp(MI->getOperand(OpNo));
214 }
215 }
216 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
217 const MachineOperand &MO = MI->getOperand(OpNo);
218 if (TM.getRelocationModel() != Reloc::Static) {
219 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
220 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000221 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000223 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
224 FnInfo.Init(GV, Mang);
225 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 return;
227 }
228 }
229 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000230 FnStubInfo &FnInfo =FnStubs[Mang->makeNameProper(MO.getSymbolName())];
231 FnInfo.Init(MO.getSymbolName(), Mang);
232 O << FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 return;
234 }
235 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000236
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 printOp(MI->getOperand(OpNo));
238 }
239 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000240 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 }
242 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000243 O << "\"L" << getFunctionNumber() << "$pb\"\n";
244 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 }
246 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000247 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 printS16ImmOperand(MI, OpNo);
249 } else {
250 if (Subtarget.isDarwin()) O << "ha16(";
251 printOp(MI->getOperand(OpNo));
252 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000253 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 if (Subtarget.isDarwin())
255 O << ')';
256 else
257 O << "@ha";
258 }
259 }
260 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000261 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 printS16ImmOperand(MI, OpNo);
263 } else {
264 if (Subtarget.isDarwin()) O << "lo16(";
265 printOp(MI->getOperand(OpNo));
266 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000267 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 if (Subtarget.isDarwin())
269 O << ')';
270 else
271 O << "@l";
272 }
273 }
274 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
275 unsigned CCReg = MI->getOperand(OpNo).getReg();
276 unsigned RegNo = enumRegToMachineReg(CCReg);
277 O << (0x80 >> RegNo);
278 }
279 // The new addressing mode printers.
280 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
281 printSymbolLo(MI, OpNo);
282 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000283 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 MI->getOperand(OpNo+1).getReg() == PPC::R0)
285 O << "0";
286 else
287 printOperand(MI, OpNo+1);
288 O << ')';
289 }
290 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000291 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000293 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 printSymbolLo(MI, OpNo);
295 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000296 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 MI->getOperand(OpNo+1).getReg() == PPC::R0)
298 O << "0";
299 else
300 printOperand(MI, OpNo+1);
301 O << ')';
302 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000303
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
305 // When used as the base register, r0 reads constant zero rather than
306 // the value contained in the register. For this reason, the darwin
307 // assembler requires that we print r0 as 0 (no r) when used as the base.
308 const MachineOperand &MO = MI->getOperand(OpNo);
309 printRegister(MO, true);
310 O << ", ";
311 printOperand(MI, OpNo+1);
312 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000313
314 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000316
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318
319 virtual void EmitExternalGlobal(const GlobalVariable *GV);
320 };
321
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000322 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Bill Wendling4f405312009-02-24 08:30:20 +0000323 class VISIBILITY_HIDDEN PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000324 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000325 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000326 const TargetAsmInfo *T, bool V)
327 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328
329 virtual const char *getPassName() const {
330 return "Linux PPC Assembly Printer";
331 }
332
333 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000334
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 void getAnalysisUsage(AnalysisUsage &AU) const {
336 AU.setPreservesAll();
337 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000338 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 PPCAsmPrinter::getAnalysisUsage(AU);
340 }
341
Chris Lattnerae982212009-07-21 18:38:57 +0000342 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 };
344
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000345 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
346 /// OS X
Bill Wendling4f405312009-02-24 08:30:20 +0000347 class VISIBILITY_HIDDEN PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000348 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000349 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000350 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000351 const TargetAsmInfo *T, bool V)
352 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353
354 virtual const char *getPassName() const {
355 return "Darwin PPC Assembly Printer";
356 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000357
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 bool runOnMachineFunction(MachineFunction &F);
359 bool doInitialization(Module &M);
360 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000361
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 void getAnalysisUsage(AnalysisUsage &AU) const {
363 AU.setPreservesAll();
364 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000365 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 PPCAsmPrinter::getAnalysisUsage(AU);
367 }
368
Chris Lattnerae982212009-07-21 18:38:57 +0000369 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 };
371} // end of anonymous namespace
372
373// Include the auto-generated portion of the assembly writer
374#include "PPCGenAsmWriter.inc"
375
376void PPCAsmPrinter::printOp(const MachineOperand &MO) {
377 switch (MO.getType()) {
378 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000379 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380
381 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000382 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 return;
384 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000385 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000386 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 // FIXME: PIC relocation model
388 return;
389 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000390 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000391 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000393 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 // Computing the address of an external symbol, not calling it.
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000395 std::string Name(TAI->getGlobalPrefix());
396 Name += MO.getSymbolName();
397
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000399 GVStubs[Name] = Name+"$non_lazy_ptr";
400 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000402 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000404 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 case MachineOperand::MO_GlobalAddress: {
406 // Computing the address of a global symbol, not calling it.
407 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000408 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409
410 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000411 if (TM.getRelocationModel() != Reloc::Static &&
412 (GV->isDeclaration() || GV->isWeakForLinker())) {
413 if (!GV->hasHiddenVisibility()) {
414 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
415 GVStubs[Mang->getMangledName(GV)] = Name;
416 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
417 GV->hasAvailableExternallyLinkage()) {
418 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
419 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
420 } else {
421 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000423 } else {
424 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 }
426 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000427
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000428 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 return;
430 }
431
432 default:
433 O << "<unknown operand type: " << MO.getType() << ">";
434 return;
435 }
436}
437
438/// EmitExternalGlobal - In this case we need to use the indirect symbol.
439///
440void PPCAsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000441 std::string Name;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000442
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000444 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000445 } else {
446 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 }
448 O << Name;
449}
450
451/// PrintAsmOperand - Print out an operand for an inline asm expression.
452///
453bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000454 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 const char *ExtraCode) {
456 // Does this asm operand have a single letter operand modifier?
457 if (ExtraCode && ExtraCode[0]) {
458 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000459
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 switch (ExtraCode[0]) {
461 default: return true; // Unknown modifier.
462 case 'c': // Don't print "$" before a global var name or constant.
463 // PPC never has a prefix.
464 printOperand(MI, OpNo);
465 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000466 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000468 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000470 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 return true;
472 ++OpNo; // Return the high-part.
473 break;
474 case 'I':
475 // Write 'i' if an integer constant, otherwise nothing. Used to print
476 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000477 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 O << "i";
479 return false;
480 }
481 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000482
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 printOperand(MI, OpNo);
484 return false;
485}
486
487bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000488 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 const char *ExtraCode) {
490 if (ExtraCode && ExtraCode[0])
491 return true; // Unknown modifier.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000492 if (MI->getOperand(OpNo).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 printMemRegReg(MI, OpNo);
494 else
495 printMemRegImm(MI, OpNo);
496 return false;
497}
498
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000499void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 const char *Modifier) {
501 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
502 unsigned Code = MI->getOperand(OpNo).getImm();
503 if (!strcmp(Modifier, "cc")) {
504 switch ((PPC::Predicate)Code) {
505 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
506 case PPC::PRED_LT: O << "lt"; return;
507 case PPC::PRED_LE: O << "le"; return;
508 case PPC::PRED_EQ: O << "eq"; return;
509 case PPC::PRED_GE: O << "ge"; return;
510 case PPC::PRED_GT: O << "gt"; return;
511 case PPC::PRED_NE: O << "ne"; return;
512 case PPC::PRED_UN: O << "un"; return;
513 case PPC::PRED_NU: O << "nu"; return;
514 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000515
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 } else {
517 assert(!strcmp(Modifier, "reg") &&
518 "Need to specify 'cc' or 'reg' as predicate op modifier!");
519 // Don't print the register for 'always'.
520 if (Code == PPC::PRED_ALWAYS) return;
521 printOperand(MI, OpNo+1);
522 }
523}
524
525
526/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
527/// the current output stream.
528///
529void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
530 ++EmittedInsts;
531
532 // Check for slwi/srwi mnemonics.
533 if (MI->getOpcode() == PPC::RLWINM) {
534 bool FoundMnemonic = false;
Chris Lattnera96056a2007-12-30 20:49:49 +0000535 unsigned char SH = MI->getOperand(2).getImm();
536 unsigned char MB = MI->getOperand(3).getImm();
537 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000539 O << "\tslwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 }
541 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000542 O << "\tsrwi "; FoundMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 SH = 32-SH;
544 }
545 if (FoundMnemonic) {
546 printOperand(MI, 0);
547 O << ", ";
548 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000549 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 return;
551 }
552 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
553 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000554 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 printOperand(MI, 0);
556 O << ", ";
557 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000558 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 return;
560 }
561 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000562 unsigned char SH = MI->getOperand(2).getImm();
563 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
565 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000566 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 printOperand(MI, 0);
568 O << ", ";
569 printOperand(MI, 1);
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000570 O << ", " << (unsigned int)SH << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 return;
572 }
573 }
574
575 if (printInstruction(MI))
576 return; // Printer was automatically generated
577
Edwin Törökbd448e32009-07-14 16:55:14 +0000578 llvm_unreachable("Unhandled instruction in asm writer!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579}
580
581/// runOnMachineFunction - This uses the printMachineInstruction()
582/// method to print assembly for each instruction.
583///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000584bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000585 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586
587 SetupMachineFunction(MF);
588 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000589
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 // Print out constants referenced by the function
591 EmitConstantPool(MF.getConstantPool());
592
593 // Print out labels for the function.
594 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000595 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000596
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000598 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000599 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000600 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 case Function::InternalLinkage: // Symbols default to internal.
602 break;
603 case Function::ExternalLinkage:
604 O << "\t.global\t" << CurrentFnName << '\n'
605 << "\t.type\t" << CurrentFnName << ", @function\n";
606 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000607 case Function::WeakAnyLinkage:
608 case Function::WeakODRLinkage:
609 case Function::LinkOnceAnyLinkage:
610 case Function::LinkOnceODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 O << "\t.global\t" << CurrentFnName << '\n';
612 O << "\t.weak\t" << CurrentFnName << '\n';
613 break;
614 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000615
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000616 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000617
Bill Wendling25a8ae32009-06-30 22:38:32 +0000618 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000619 O << CurrentFnName << ":\n";
620
621 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000622 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623
624 // Print out code for the function.
625 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
626 I != E; ++I) {
627 // Print a label for the basic block.
628 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000629 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 O << '\n';
631 }
632 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
633 II != E; ++II) {
634 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 printMachineInstruction(II);
636 }
637 }
638
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000639 O << "\t.size\t" << CurrentFnName << ",.-" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640
641 // Print out jump tables referenced by the function.
642 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000643
Chris Lattner2931fe42009-07-29 05:09:30 +0000644 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000645
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000647 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000648
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000649 O.flush();
650
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 // We didn't modify anything.
652 return false;
653}
654
Chris Lattner2b638a72008-02-15 19:04:54 +0000655/// PrintUnmangledNameSafely - Print out the printable characters in the name.
Dan Gohman8387bb32009-03-03 02:55:14 +0000656/// Don't print things like \\n or \\0.
Daniel Dunbar23e2b802009-07-26 07:49:05 +0000657static void PrintUnmangledNameSafely(const Value *V,
658 formatted_raw_ostream &OS) {
659 for (StringRef::iterator it = V->getName().begin(),
660 ie = V->getName().end(); it != ie; ++it)
661 if (isprint(*it))
662 OS << *it;
Chris Lattner2b638a72008-02-15 19:04:54 +0000663}
664
Chris Lattnerae982212009-07-21 18:38:57 +0000665void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 const TargetData *TD = TM.getTargetData();
667
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000668 if (!GVar->hasInitializer())
669 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000670
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000671 // Check to see if this is a special global used by LLVM, if so, emit it.
672 if (EmitSpecialLLVMGlobal(GVar))
673 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000675 std::string name = Mang->getMangledName(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000677 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000678
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000679 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000680 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000681 return;
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000682 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000683 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000684 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685
Chris Lattner2931fe42009-07-29 05:09:30 +0000686 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000687
688 if (C->isNullValue() && /* FIXME: Verify correct */
689 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000690 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000691 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000693
694 if (GVar->hasExternalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000695 O << "\t.global " << name << '\n';
696 O << "\t.type " << name << ", @object\n";
Nick Lewycky3246a9c2007-07-25 03:48:45 +0000697 O << name << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000698 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000699 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000700 O << TAI->getLCOMMDirective() << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000702 O << ".comm " << name << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 }
Evan Cheng11db8142009-03-24 00:17:40 +0000704 if (VerboseAsm) {
705 O << "\t\t" << TAI->getCommentString() << " '";
706 PrintUnmangledNameSafely(GVar, O);
707 O << "'";
708 }
709 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000710 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 }
712
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000713 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000714 case GlobalValue::LinkOnceAnyLinkage:
715 case GlobalValue::LinkOnceODRLinkage:
716 case GlobalValue::WeakAnyLinkage:
717 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000718 case GlobalValue::CommonLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000719 O << "\t.global " << name << '\n'
720 << "\t.type " << name << ", @object\n"
721 << "\t.weak " << name << '\n';
722 break;
723 case GlobalValue::AppendingLinkage:
724 // FIXME: appending linkage variables should go into a section of
725 // their name or something. For now, just emit them as external.
726 case GlobalValue::ExternalLinkage:
727 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000728 O << "\t.global " << name << '\n'
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000729 << "\t.type " << name << ", @object\n";
730 // FALL THROUGH
731 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000732 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000733 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000734 break;
735 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000736 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000737 }
738
739 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000740 O << name << ":";
741 if (VerboseAsm) {
742 O << "\t\t\t\t" << TAI->getCommentString() << " '";
743 PrintUnmangledNameSafely(GVar, O);
744 O << "'";
745 }
746 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000747
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000748 EmitGlobalConstant(C);
749 O << '\n';
750}
751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753/// runOnMachineFunction - This uses the printMachineInstruction()
754/// method to print assembly for each instruction.
755///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000756bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000757 this->MF = &MF;
758
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 SetupMachineFunction(MF);
760 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000761
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 // Print out constants referenced by the function
763 EmitConstantPool(MF.getConstantPool());
764
765 // Print out labels for the function.
766 const Function *F = MF.getFunction();
Chris Lattner2931fe42009-07-29 05:09:30 +0000767 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000768
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000769 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000770 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000771 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000772 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 case Function::InternalLinkage: // Symbols default to internal.
774 break;
775 case Function::ExternalLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000776 O << "\t.globl\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000777 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000778 case Function::WeakAnyLinkage:
779 case Function::WeakODRLinkage:
780 case Function::LinkOnceAnyLinkage:
781 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000782 O << "\t.globl\t" << CurrentFnName << '\n';
783 O << "\t.weak_definition\t" << CurrentFnName << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784 break;
785 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000786
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000787 printVisibility(CurrentFnName, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000788
Bill Wendling25a8ae32009-06-30 22:38:32 +0000789 EmitAlignment(MF.getAlignment(), F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790 O << CurrentFnName << ":\n";
791
792 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000793 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794
Bill Wendling36ccaea2008-01-26 06:51:24 +0000795 // If the function is empty, then we need to emit *something*. Otherwise, the
796 // function's label might be associated with something that it wasn't meant to
797 // be associated with. We emit a noop in this situation.
798 MachineFunction::iterator I = MF.begin();
799
Bill Wendlingb5880a72008-01-26 09:03:52 +0000800 if (++I == MF.end() && MF.front().empty())
801 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000802
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 // Print out code for the function.
804 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
805 I != E; ++I) {
806 // Print a label for the basic block.
807 if (I != MF.begin()) {
Evan Cheng11db8142009-03-24 00:17:40 +0000808 printBasicBlockLabel(I, true, true, VerboseAsm);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 O << '\n';
810 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000811 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
812 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 printMachineInstruction(II);
815 }
816 }
817
818 // Print out jump tables referenced by the function.
819 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000820
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000822 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000823
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 // We didn't modify anything.
825 return false;
826}
827
828
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000829bool PPCDarwinAsmPrinter::doInitialization(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000830 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000831 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000832 "ppc",
833 "ppc601",
834 "ppc602",
835 "ppc603",
836 "ppc7400",
837 "ppc750",
838 "ppc970",
839 "ppc64"
840 };
841
842 unsigned Directive = Subtarget.getDarwinDirective();
843 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
844 Directive = PPC::DIR_970;
845 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
846 Directive = PPC::DIR_7400;
847 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
848 Directive = PPC::DIR_64;
849 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000850 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000851
Dan Gohman4a558a32007-07-25 19:33:14 +0000852 bool Result = AsmPrinter::doInitialization(M);
Devang Patelc20079e2009-06-20 01:00:07 +0000853 assert(MMI);
Dale Johannesen58e0eeb2008-07-09 20:43:39 +0000854
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855 // Prime text sections so they are adjacent. This reduces the likelihood a
856 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000857
858 TargetLoweringObjectFileMachO &TLOFMacho =
859 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
860 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__textcoal_nt,"
861 "coalesced,pure_instructions", true,
862 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000864 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstu"
865 "b1,symbol_stubs,"
866 "pure_instructions,32", true,
867 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000869 SwitchToSection(TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1"
870 ",symbol_stubs,"
871 "pure_instructions,16", true,
872 SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 }
Chris Lattnerc4c40a92009-07-28 03:13:23 +0000874 SwitchToSection(getObjFileLowering().getTextSection());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000875
Dan Gohman4a558a32007-07-25 19:33:14 +0000876 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877}
878
Chris Lattnerae982212009-07-21 18:38:57 +0000879void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000880 const TargetData *TD = TM.getTargetData();
881
882 if (!GVar->hasInitializer())
883 return; // External global require no code
884
885 // Check to see if this is a special global used by LLVM, if so, emit it.
886 if (EmitSpecialLLVMGlobal(GVar)) {
887 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000888 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000889 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000890 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000891 O << ".reference .destructors_used\n";
892 }
893 return;
894 }
895
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000896 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000897 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000898
899 Constant *C = GVar->getInitializer();
900 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000901 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000902 unsigned Align = TD->getPreferredAlignmentLog(GVar);
903
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000904 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000905 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner16b8d7f2009-07-24 03:49:17 +0000906 SwitchToSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000907
908 if (C->isNullValue() && /* FIXME: Verify correct */
909 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000910 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000911 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000912 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000913 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000914 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
915
916 if (GVar->hasExternalLinkage()) {
917 O << "\t.globl " << name << '\n';
918 O << "\t.zerofill __DATA, __common, " << name << ", "
919 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000920 } else if (GVar->hasLocalLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000921 O << TAI->getLCOMMDirective() << name << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000922 } else if (!GVar->hasCommonLinkage()) {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000923 O << "\t.globl " << name << '\n'
924 << TAI->getWeakDefDirective() << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000925 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000926 O << name << ":";
927 if (VerboseAsm) {
928 O << "\t\t\t\t" << TAI->getCommentString() << " ";
929 PrintUnmangledNameSafely(GVar, O);
930 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000931 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000932 EmitGlobalConstant(C);
933 return;
934 } else {
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000935 O << ".comm " << name << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000936 // Darwin 9 and above support aligned common data.
937 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000938 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000939 }
Evan Cheng11db8142009-03-24 00:17:40 +0000940 if (VerboseAsm) {
941 O << "\t\t" << TAI->getCommentString() << " '";
942 PrintUnmangledNameSafely(GVar, O);
943 O << "'";
944 }
945 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000946 return;
947 }
948
949 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000950 case GlobalValue::LinkOnceAnyLinkage:
951 case GlobalValue::LinkOnceODRLinkage:
952 case GlobalValue::WeakAnyLinkage:
953 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000954 case GlobalValue::CommonLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000955 O << "\t.globl " << name << '\n'
956 << "\t.weak_definition " << name << '\n';
957 break;
958 case GlobalValue::AppendingLinkage:
959 // FIXME: appending linkage variables should go into a section of
960 // their name or something. For now, just emit them as external.
961 case GlobalValue::ExternalLinkage:
962 // If external or appending, declare as a global symbol
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000963 O << "\t.globl " << name << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000964 // FALL THROUGH
965 case GlobalValue::InternalLinkage:
evancheng47ae8142009-01-25 06:32:01 +0000966 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000967 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000968 break;
969 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000970 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000971 }
972
973 EmitAlignment(Align, GVar);
Evan Cheng11db8142009-03-24 00:17:40 +0000974 O << name << ":";
975 if (VerboseAsm) {
976 O << "\t\t\t\t" << TAI->getCommentString() << " '";
977 PrintUnmangledNameSafely(GVar, O);
978 O << "'";
979 }
980 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000981
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000982 EmitGlobalConstant(C);
983 O << '\n';
984}
985
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000986bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000987 const TargetData *TD = TM.getTargetData();
988
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 bool isPPC64 = TD->getPointerSizeInBits() == 64;
990
Chris Lattnerf4815552009-08-03 22:52:21 +0000991 // Darwin/PPC always uses mach-o.
992 TargetLoweringObjectFileMachO &TLOFMacho =
993 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
994
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +0000996 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000997 const MCSection *StubSection =
998 TLOFMacho.getMachOSection("\t.section __TEXT,__picsymbolstub1,"
999 "symbol_stubs,pure_instructions,32", true,
1000 SectionKind::getText());
1001 const MCSection *LSPSection =
1002 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
1003 SectionKind::getMetadata());
1004
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001005 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001006 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001007 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001009 const FnStubInfo &Info = I->second;
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001010 O << Info.Stub << ":\n";
1011 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 O << "\tmflr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001013 O << "\tbcl 20,31," << Info.AnonSymbol << '\n';
1014 O << Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 O << "\tmflr r11\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001016 O << "\taddis r11,r11,ha16(" << Info.LazyPtr << "-" << Info.AnonSymbol;
Evan Chenga65854f2008-12-05 01:06:39 +00001017 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001019 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
1020 O << Info.LazyPtr << "-" << Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 O << "\tmtctr r12\n";
1022 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001023
Chris Lattnerf4815552009-08-03 22:52:21 +00001024 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001025 O << Info.LazyPtr << ":\n";
1026 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001027 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001028 }
Chris Lattner189198f2009-07-15 00:55:58 +00001029 } else if (!FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001030 const MCSection *StubSection =
1031 TLOFMacho.getMachOSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
1032 "pure_instructions,16", true,
1033 SectionKind::getText());
1034 const MCSection *LSPSection =
1035 TLOFMacho.getMachOSection(".lazy_symbol_pointer", true,
1036 SectionKind::getMetadata());
1037
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001038 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001039 I != E; ++I) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001040 SwitchToSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001041 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001042 const FnStubInfo &Info = I->second;
1043 O << Info.Stub << ":\n";
1044 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1045 O << "\tlis r11,ha16(" << Info.LazyPtr << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001046 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001047 O << Info.LazyPtr << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048 O << "\tmtctr r12\n";
1049 O << "\tbctr\n";
Chris Lattnerf4815552009-08-03 22:52:21 +00001050 SwitchToSection(LSPSection);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001051 O << Info.LazyPtr << ":\n";
1052 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001053 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054 }
1055 }
1056
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001057 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058
Dale Johannesen85535762008-04-02 00:25:04 +00001059 if (TAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001060 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001061 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001062 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001063 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001064 E = Personalities.end(); I != E; ++I) {
1065 if (*I)
1066 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001067 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001068 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001069 }
1070
Chris Lattnerf4815552009-08-03 22:52:21 +00001071 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001072 if (!GVStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001073 const MCSection *TheSection =
1074 TLOFMacho.getMachOSection(".non_lazy_symbol_pointer", true,
1075 SectionKind::getMetadata());
1076 SwitchToSection(TheSection);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001077 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1078 E = GVStubs.end(); I != E; ++I) {
1079 O << I->second << ":\n";
1080 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1081 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 }
1083 }
1084
Evan Chenga65854f2008-12-05 01:06:39 +00001085 if (!HiddenGVStubs.empty()) {
Chris Lattnerc4c40a92009-07-28 03:13:23 +00001086 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001087 EmitAlignment(isPPC64 ? 3 : 2);
1088 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1089 E = HiddenGVStubs.end(); I != E; ++I) {
1090 O << I->second << ":\n";
1091 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001092 }
1093 }
1094
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001095 // Funny Darwin hack: This flag tells the linker that no global symbols
1096 // contain code that falls through to other global symbols (e.g. the obvious
1097 // implementation of multiple entry points). If this doesn't occur, the
1098 // linker can safely perform dead code stripping. Since LLVM never generates
1099 // code that does this, it is always safe to set.
1100 O << "\t.subsections_via_symbols\n";
1101
Dan Gohman4a558a32007-07-25 19:33:14 +00001102 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001103}
1104
1105
1106
1107/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1108/// for a MachineFunction to the given output stream, in a format that the
1109/// Darwin assembler can deal with.
1110///
Daniel Dunbarc680b012009-07-25 06:49:55 +00001111static FunctionPass *createPPCAsmPrinterPass(formatted_raw_ostream &o,
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001112 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +00001113 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001114 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1115
Chris Lattnerae982212009-07-21 18:38:57 +00001116 if (Subtarget->isDarwin())
Daniel Dunbarb10d2222009-07-01 01:48:54 +00001117 return new PPCDarwinAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Chris Lattnerae982212009-07-21 18:38:57 +00001118 return new PPCLinuxAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001119}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001120
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001121// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001122extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001123 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
1124
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001125 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1126}