blob: 4175d9c26cf05bb6535a002b374545d2f3825798 [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 Lattnerc6f802d2009-09-13 17:14:04 +000034#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9bc87482010-01-13 19:00:57 +000035#include "llvm/MC/MCContext.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000036#include "llvm/MC/MCSectionMachO.h"
Chris Lattner73266f92009-08-19 05:49:37 +000037#include "llvm/MC/MCStreamer.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000038#include "llvm/MC/MCSymbol.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
40#include "llvm/Target/TargetRegisterInfo.h"
41#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetOptions.h"
43#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/Support/Mangler.h"
45#include "llvm/Support/MathExtras.h"
46#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000048#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000049#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050#include "llvm/ADT/Statistic.h"
51#include "llvm/ADT/StringExtras.h"
Evan Chenga65854f2008-12-05 01:06:39 +000052#include "llvm/ADT/StringSet.h"
Chris Lattnera38b2872010-01-13 06:38:18 +000053#include "llvm/ADT/SmallString.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054using namespace llvm;
55
56STATISTIC(EmittedInsts, "Number of machine instrs printed");
57
58namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000059 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +000060 protected:
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000061 struct FnStubInfo {
Chris Lattner1774fd32010-01-13 19:13:16 +000062 MCSymbol *Stub, *LazyPtr, *AnonSymbol;
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000063
Chris Lattner9bc87482010-01-13 19:00:57 +000064 FnStubInfo() {
Chris Lattner1774fd32010-01-13 19:13:16 +000065 Stub = LazyPtr = AnonSymbol = 0;
Chris Lattner9bc87482010-01-13 19:00:57 +000066 }
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000067
Chris Lattner9f9fd9d2010-01-16 01:45:47 +000068 void Init(const GlobalValue *GV, AsmPrinter *Printer) {
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000069 // Already initialized.
Chris Lattner1774fd32010-01-13 19:13:16 +000070 if (Stub != 0) return;
Chris Lattner53087e52010-01-13 19:05:36 +000071
72 // Get the names.
Chris Lattner9f9fd9d2010-01-16 01:45:47 +000073 Stub = Printer->GetPrivateGlobalValueSymbolStub(GV, "$stub");
74 LazyPtr = Printer->GetPrivateGlobalValueSymbolStub(GV, "$lazy_ptr");
75 AnonSymbol = Printer->GetPrivateGlobalValueSymbolStub(GV, "$stub$tmp");
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000076 }
77
Chris Lattner9bc87482010-01-13 19:00:57 +000078 void Init(StringRef GVName, Mangler *Mang, MCContext &Ctx) {
Chris Lattner1774fd32010-01-13 19:13:16 +000079 assert(!GVName.empty() && "external symbol name shouldn't be empty");
80 if (Stub != 0) return; // Already initialized.
Chris Lattnerf80743e2010-01-13 07:56:59 +000081 // Get the names for the external symbol name.
Chris Lattnera38b2872010-01-13 06:38:18 +000082 SmallString<128> TmpStr;
Chris Lattner1774fd32010-01-13 19:13:16 +000083 Mang->getNameWithPrefix(TmpStr, GVName, Mangler::Private);
Chris Lattner1774fd32010-01-13 19:13:16 +000084 TmpStr += "$stub";
85 Stub = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner53087e52010-01-13 19:05:36 +000086 TmpStr.erase(TmpStr.end()-5, TmpStr.end()); // Remove $stub
87
88 TmpStr += "$lazy_ptr";
Chris Lattner1774fd32010-01-13 19:13:16 +000089 LazyPtr = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner53087e52010-01-13 19:05:36 +000090 TmpStr.erase(TmpStr.end()-9, TmpStr.end()); // Remove $lazy_ptr
Chris Lattnera38b2872010-01-13 06:38:18 +000091
Chris Lattner53087e52010-01-13 19:05:36 +000092 TmpStr += "$stub$tmp";
Chris Lattner1774fd32010-01-13 19:13:16 +000093 AnonSymbol = Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner9ea6e2a2009-07-15 02:28:57 +000094 }
95 };
96
97 StringMap<FnStubInfo> FnStubs;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000098 StringMap<std::string> GVStubs, HiddenGVStubs, TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000100 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +0000101 public:
David Greene302008d2009-07-14 20:18:05 +0000102 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000103 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000104 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000105 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 virtual const char *getPassName() const {
108 return "PowerPC Assembly Printer";
109 }
110
111 PPCTargetMachine &getTM() {
112 return static_cast<PPCTargetMachine&>(TM);
113 }
114
115 unsigned enumRegToMachineReg(unsigned enumReg) {
116 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000117 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 case PPC::CR0: return 0;
119 case PPC::CR1: return 1;
120 case PPC::CR2: return 2;
121 case PPC::CR3: return 3;
122 case PPC::CR4: return 4;
123 case PPC::CR5: return 5;
124 case PPC::CR6: return 6;
125 case PPC::CR7: return 7;
126 }
Edwin Törökbd448e32009-07-14 16:55:14 +0000127 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 }
129
130 /// printInstruction - This method is automatically generated by tablegen
131 /// from the instruction set description. This method returns true if the
132 /// machine instruction was sufficiently described to print it, otherwise it
133 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +0000134 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +0000135 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +0000136
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137
138 void printMachineInstruction(const MachineInstr *MI);
139 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000140
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 /// stripRegisterPrefix - This method strips the character prefix from a
142 /// register name so that only the number is left. Used by for linux asm.
143 const char *stripRegisterPrefix(const char *RegName) {
144 switch (RegName[0]) {
145 case 'r':
146 case 'f':
147 case 'v': return RegName + 1;
148 case 'c': if (RegName[1] == 'r') return RegName + 2;
149 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 return RegName;
152 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 /// printRegister - Print register according to target requirements.
155 ///
156 void printRegister(const MachineOperand &MO, bool R0AsZero) {
157 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000158 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 // If we should use 0 for R0.
161 if (R0AsZero && RegNo == PPC::R0) {
162 O << "0";
163 return;
164 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000165
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000166 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 // Linux assembler (Others?) does not take register mnemonics.
168 // FIXME - What about special registers used in mfspr/mtspr?
169 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
170 O << RegName;
171 }
172
173 void printOperand(const MachineInstr *MI, unsigned OpNo) {
174 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000175 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000177 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000178 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 } else {
180 printOp(MO);
181 }
182 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000183
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
185 unsigned AsmVariant, const char *ExtraCode);
186 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
187 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000188
189
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000191 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 value = (value << (32-5)) >> (32-5);
193 O << (int)value;
194 }
195 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000196 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 assert(value <= 31 && "Invalid u5imm argument!");
198 O << (unsigned int)value;
199 }
200 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000201 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 assert(value <= 63 && "Invalid u6imm argument!");
203 O << (unsigned int)value;
204 }
205 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000206 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 }
208 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000209 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 }
211 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000212 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000213 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 } else {
215 O << "lo16(";
216 printOp(MI->getOperand(OpNo));
217 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000218 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 else
220 O << ')';
221 }
222 }
223 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
224 // Branches can take an immediate operand. This is used by the branch
225 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000226 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000227 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 } else {
229 printOp(MI->getOperand(OpNo));
230 }
231 }
232 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
233 const MachineOperand &MO = MI->getOperand(OpNo);
234 if (TM.getRelocationModel() != Reloc::Static) {
235 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
236 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000237 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 // Dynamically-resolved functions need a stub for the function.
Chris Lattner9ea6e2a2009-07-15 02:28:57 +0000239 FnStubInfo &FnInfo = FnStubs[Mang->getMangledName(GV)];
Chris Lattner9f9fd9d2010-01-16 01:45:47 +0000240 FnInfo.Init(GV, this);
Chris Lattner1774fd32010-01-13 19:13:16 +0000241 FnInfo.Stub->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 return;
243 }
244 }
245 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnera38b2872010-01-13 06:38:18 +0000246 SmallString<128> MangledName;
Chris Lattnerf80743e2010-01-13 07:56:59 +0000247 Mang->getNameWithPrefix(MangledName, MO.getSymbolName());
Chris Lattnera38b2872010-01-13 06:38:18 +0000248 FnStubInfo &FnInfo = FnStubs[MangledName.str()];
Chris Lattner9bc87482010-01-13 19:00:57 +0000249 FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
Chris Lattner1774fd32010-01-13 19:13:16 +0000250 FnInfo.Stub->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 return;
252 }
253 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 printOp(MI->getOperand(OpNo));
256 }
257 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000258 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 }
260 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000261 O << "\"L" << getFunctionNumber() << "$pb\"\n";
262 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 }
264 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000265 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 printS16ImmOperand(MI, OpNo);
267 } else {
268 if (Subtarget.isDarwin()) O << "ha16(";
269 printOp(MI->getOperand(OpNo));
270 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000271 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (Subtarget.isDarwin())
273 O << ')';
274 else
275 O << "@ha";
276 }
277 }
278 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000279 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 printS16ImmOperand(MI, OpNo);
281 } else {
282 if (Subtarget.isDarwin()) O << "lo16(";
283 printOp(MI->getOperand(OpNo));
284 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000285 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 if (Subtarget.isDarwin())
287 O << ')';
288 else
289 O << "@l";
290 }
291 }
292 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
293 unsigned CCReg = MI->getOperand(OpNo).getReg();
294 unsigned RegNo = enumRegToMachineReg(CCReg);
295 O << (0x80 >> RegNo);
296 }
297 // The new addressing mode printers.
298 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
299 printSymbolLo(MI, OpNo);
300 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000301 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 MI->getOperand(OpNo+1).getReg() == PPC::R0)
303 O << "0";
304 else
305 printOperand(MI, OpNo+1);
306 O << ')';
307 }
308 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000309 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000311 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 printSymbolLo(MI, OpNo);
313 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000314 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 MI->getOperand(OpNo+1).getReg() == PPC::R0)
316 O << "0";
317 else
318 printOperand(MI, OpNo+1);
319 O << ')';
320 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000321
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
323 // When used as the base register, r0 reads constant zero rather than
324 // the value contained in the register. For this reason, the darwin
325 // assembler requires that we print r0 as 0 (no r) when used as the base.
326 const MachineOperand &MO = MI->getOperand(OpNo);
327 printRegister(MO, true);
328 O << ", ";
329 printOperand(MI, OpNo+1);
330 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000331
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000332 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
333 const MachineOperand &MO = MI->getOperand(OpNo);
334
335 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
336
337 GlobalValue *GV = MO.getGlobal();
338
339 std::string Name = Mang->getMangledName(GV);
340
341 // Map symbol -> label of TOC entry.
342 if (TOC.count(Name) == 0) {
343 std::string Label;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000344 Label += MAI->getPrivateGlobalPrefix();
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000345 Label += "C";
346 Label += utostr(LabelID++);
347
348 TOC[Name] = Label;
349 }
350
351 O << TOC[Name] << "@toc";
352 }
353
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000354 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 };
359
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000360 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000361 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000362 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000363 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000364 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000365 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366
367 virtual const char *getPassName() const {
368 return "Linux PPC Assembly Printer";
369 }
370
371 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000372 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 void getAnalysisUsage(AnalysisUsage &AU) const {
375 AU.setPreservesAll();
376 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000377 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 PPCAsmPrinter::getAnalysisUsage(AU);
379 }
380
Chris Lattnerae982212009-07-21 18:38:57 +0000381 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 };
383
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000384 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
385 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000386 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000387 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000388 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000389 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000390 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000391 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392
393 virtual const char *getPassName() const {
394 return "Darwin PPC Assembly Printer";
395 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000396
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000399 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000400
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 void getAnalysisUsage(AnalysisUsage &AU) const {
402 AU.setPreservesAll();
403 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000404 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 PPCAsmPrinter::getAnalysisUsage(AU);
406 }
407
Chris Lattnerae982212009-07-21 18:38:57 +0000408 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 };
410} // end of anonymous namespace
411
412// Include the auto-generated portion of the assembly writer
413#include "PPCGenAsmWriter.inc"
414
415void PPCAsmPrinter::printOp(const MachineOperand &MO) {
416 switch (MO.getType()) {
417 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000418 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419
420 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000421 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 return;
423 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000424 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000425 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 // FIXME: PIC relocation model
427 return;
428 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000429 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000430 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000432 case MachineOperand::MO_BlockAddress:
433 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
434 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000435 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 // Computing the address of an external symbol, not calling it.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000437 std::string Name(MAI->getGlobalPrefix());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000438 Name += MO.getSymbolName();
439
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000441 GVStubs[Name] = Name+"$non_lazy_ptr";
442 Name += "$non_lazy_ptr";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000444 O << Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000446 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 case MachineOperand::MO_GlobalAddress: {
448 // Computing the address of a global symbol, not calling it.
449 GlobalValue *GV = MO.getGlobal();
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000450 std::string Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451
452 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000453 if (TM.getRelocationModel() != Reloc::Static &&
454 (GV->isDeclaration() || GV->isWeakForLinker())) {
455 if (!GV->hasHiddenVisibility()) {
456 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
457 GVStubs[Mang->getMangledName(GV)] = Name;
458 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
459 GV->hasAvailableExternallyLinkage()) {
460 Name = Mang->getMangledName(GV, "$non_lazy_ptr", true);
461 HiddenGVStubs[Mang->getMangledName(GV)] = Name;
462 } else {
463 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000465 } else {
466 Name = Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 }
468 O << Name;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000469
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000470 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 return;
472 }
473
474 default:
475 O << "<unknown operand type: " << MO.getType() << ">";
476 return;
477 }
478}
479
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480/// PrintAsmOperand - Print out an operand for an inline asm expression.
481///
482bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000483 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 const char *ExtraCode) {
485 // Does this asm operand have a single letter operand modifier?
486 if (ExtraCode && ExtraCode[0]) {
487 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000488
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000489 switch (ExtraCode[0]) {
490 default: return true; // Unknown modifier.
491 case 'c': // Don't print "$" before a global var name or constant.
492 // PPC never has a prefix.
493 printOperand(MI, OpNo);
494 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000495 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000497 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000499 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 return true;
501 ++OpNo; // Return the high-part.
502 break;
503 case 'I':
504 // Write 'i' if an integer constant, otherwise nothing. Used to print
505 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000506 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 O << "i";
508 return false;
509 }
510 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000511
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 printOperand(MI, OpNo);
513 return false;
514}
515
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000516// At the moment, all inline asm memory operands are a single register.
517// In any case, the output of this routine should always be just one
518// assembler operand.
519
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000521 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 const char *ExtraCode) {
523 if (ExtraCode && ExtraCode[0])
524 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000525 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000526 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000527 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000528 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 return false;
530}
531
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000532void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 const char *Modifier) {
534 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
535 unsigned Code = MI->getOperand(OpNo).getImm();
536 if (!strcmp(Modifier, "cc")) {
537 switch ((PPC::Predicate)Code) {
538 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
539 case PPC::PRED_LT: O << "lt"; return;
540 case PPC::PRED_LE: O << "le"; return;
541 case PPC::PRED_EQ: O << "eq"; return;
542 case PPC::PRED_GE: O << "ge"; return;
543 case PPC::PRED_GT: O << "gt"; return;
544 case PPC::PRED_NE: O << "ne"; return;
545 case PPC::PRED_UN: O << "un"; return;
546 case PPC::PRED_NU: O << "nu"; return;
547 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000548
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 } else {
550 assert(!strcmp(Modifier, "reg") &&
551 "Need to specify 'cc' or 'reg' as predicate op modifier!");
552 // Don't print the register for 'always'.
553 if (Code == PPC::PRED_ALWAYS) return;
554 printOperand(MI, OpNo+1);
555 }
556}
557
558
559/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
560/// the current output stream.
561///
562void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
563 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000564
Devang Patel5450fc12009-10-06 02:19:11 +0000565 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566
567 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000568 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000570 unsigned char SH = MI->getOperand(2).getImm();
571 unsigned char MB = MI->getOperand(3).getImm();
572 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000574 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 }
576 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000577 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 SH = 32-SH;
579 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000580 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 printOperand(MI, 0);
582 O << ", ";
583 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000584 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 }
586 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
587 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000588 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000589 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 printOperand(MI, 0);
591 O << ", ";
592 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 }
594 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000595 unsigned char SH = MI->getOperand(2).getImm();
596 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
598 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000599 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000600 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000601 printOperand(MI, 0);
602 O << ", ";
603 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000604 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 }
606 }
607
Dale Johannesen760acc02010-01-06 02:20:18 +0000608 if (!useSubstituteMnemonic)
609 printInstruction(MI);
610
David Greeneca9b04b2009-11-13 21:34:57 +0000611 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000612 EmitComments(*MI);
613 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000614
615 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616}
617
618/// runOnMachineFunction - This uses the printMachineInstruction()
619/// method to print assembly for each instruction.
620///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000621bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000622 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623
624 SetupMachineFunction(MF);
625 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000626
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 // Print out constants referenced by the function
628 EmitConstantPool(MF.getConstantPool());
629
630 // Print out labels for the function.
631 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000632 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000633
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000635 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000636 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000637 case Function::InternalLinkage: // Symbols default to internal.
638 break;
639 case Function::ExternalLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000640 O << "\t.global\t";
641 CurrentFnSym->print(O, MAI);
642 O << '\n' << "\t.type\t";
643 CurrentFnSym->print(O, MAI);
644 O << ", @function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000646 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000647 case Function::WeakAnyLinkage:
648 case Function::WeakODRLinkage:
649 case Function::LinkOnceAnyLinkage:
650 case Function::LinkOnceODRLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000651 O << "\t.global\t";
652 CurrentFnSym->print(O, MAI);
653 O << '\n';
654 O << "\t.weak\t";
655 CurrentFnSym->print(O, MAI);
656 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 break;
658 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000659
Chris Lattner651adf32010-01-16 00:21:18 +0000660 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000661
Bill Wendling25a8ae32009-06-30 22:38:32 +0000662 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000663
664 if (Subtarget.isPPC64()) {
665 // Emit an official procedure descriptor.
Chris Lattner651adf32010-01-16 00:21:18 +0000666 // FIXME 64-bit SVR4: Use MCSection here!
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000667 O << "\t.section\t\".opd\",\"aw\"\n";
668 O << "\t.align 3\n";
Chris Lattner651adf32010-01-16 00:21:18 +0000669 CurrentFnSym->print(O, MAI);
670 O << ":\n";
671 O << "\t.quad .L.";
672 CurrentFnSym->print(O, MAI);
673 O << ",.TOC.@tocbase\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000674 O << "\t.previous\n";
Chris Lattner651adf32010-01-16 00:21:18 +0000675 O << ".L.";
676 CurrentFnSym->print(O, MAI);
677 O << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000678 } else {
Chris Lattner651adf32010-01-16 00:21:18 +0000679 CurrentFnSym->print(O, MAI);
680 O << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000681 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682
683 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000684 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685
686 // Print out code for the function.
687 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
688 I != E; ++I) {
689 // Print a label for the basic block.
690 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000691 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692 }
693 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
694 II != E; ++II) {
695 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 printMachineInstruction(II);
697 }
698 }
699
Chris Lattner651adf32010-01-16 00:21:18 +0000700 O << "\t.size\t";
701 CurrentFnSym->print(O, MAI);
702 O << ",.-";
703 CurrentFnSym->print(O, MAI);
704 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
Chris Lattner73266f92009-08-19 05:49:37 +0000706 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000707
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000709 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000710
Bill Wendling6fb58e12009-11-09 21:20:14 +0000711 // Print out jump tables referenced by the function.
712 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
713
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 // We didn't modify anything.
715 return false;
716}
717
Chris Lattnerae982212009-07-21 18:38:57 +0000718void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 const TargetData *TD = TM.getTargetData();
720
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000721 if (!GVar->hasInitializer())
722 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000723
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000724 // Check to see if this is a special global used by LLVM, if so, emit it.
725 if (EmitSpecialLLVMGlobal(GVar))
726 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727
Chris Lattner22b24952010-01-16 01:12:01 +0000728 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729
Chris Lattner22b24952010-01-16 01:12:01 +0000730 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000731
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000732 Constant *C = GVar->getInitializer();
733 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000734 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000735 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736
Chris Lattner73266f92009-08-19 05:49:37 +0000737 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
738 TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000739
740 if (C->isNullValue() && /* FIXME: Verify correct */
741 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000742 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000743 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000745
746 if (GVar->hasExternalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +0000747 O << "\t.global ";
748 GVarSym->print(O, MAI);
749 O << '\n';
750 O << "\t.type ";
751 GVarSym->print(O, MAI);
752 O << ", @object\n";
753 GVarSym->print(O, MAI);
754 O << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000755 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000756 } else if (GVar->hasLocalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +0000757 O << MAI->getLCOMMDirective();
758 GVarSym->print(O, MAI);
759 O << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 } else {
Chris Lattner22b24952010-01-16 01:12:01 +0000761 O << ".comm ";
762 GVarSym->print(O, MAI);
763 O << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 }
Evan Cheng11db8142009-03-24 00:17:40 +0000765 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000766 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000767 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000768 O << "'";
769 }
770 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000771 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 }
773
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000774 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000775 case GlobalValue::LinkOnceAnyLinkage:
776 case GlobalValue::LinkOnceODRLinkage:
777 case GlobalValue::WeakAnyLinkage:
778 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000779 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000780 case GlobalValue::LinkerPrivateLinkage:
Chris Lattner22b24952010-01-16 01:12:01 +0000781 O << "\t.global ";
782 GVarSym->print(O, MAI);
783 O << "\n\t.type ";
784 GVarSym->print(O, MAI);
785 O << ", @object\n\t.weak ";
786 GVarSym->print(O, MAI);
787 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000788 break;
789 case GlobalValue::AppendingLinkage:
790 // FIXME: appending linkage variables should go into a section of
791 // their name or something. For now, just emit them as external.
792 case GlobalValue::ExternalLinkage:
793 // If external or appending, declare as a global symbol
Chris Lattner22b24952010-01-16 01:12:01 +0000794 O << "\t.global ";
795 GVarSym->print(O, MAI);
796 O << "\n\t.type ";
797 GVarSym->print(O, MAI);
798 O << ", @object\n";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000799 // FALL THROUGH
800 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000801 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000802 break;
803 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000804 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000805 }
806
807 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +0000808 GVarSym->print(O, MAI);
809 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +0000810 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000811 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000812 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000813 O << "'";
814 }
815 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000816
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000817 EmitGlobalConstant(C);
818 O << '\n';
819}
820
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000821bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
822 const TargetData *TD = TM.getTargetData();
823
824 bool isPPC64 = TD->getPointerSizeInBits() == 64;
825
826 if (isPPC64 && !TOC.empty()) {
827 // FIXME 64-bit SVR4: Use MCSection here?
828 O << "\t.section\t\".toc\",\"aw\"\n";
829
830 for (StringMap<std::string>::iterator I = TOC.begin(), E = TOC.end();
831 I != E; ++I) {
832 O << I->second << ":\n";
833 O << "\t.tc " << I->getKeyData() << "[TC]," << I->getKeyData() << '\n';
834 }
835 }
836
837 return AsmPrinter::doFinalization(M);
838}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840/// runOnMachineFunction - This uses the printMachineInstruction()
841/// method to print assembly for each instruction.
842///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000843bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000844 this->MF = &MF;
845
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 SetupMachineFunction(MF);
847 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000848
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 // Print out constants referenced by the function
850 EmitConstantPool(MF.getConstantPool());
851
852 // Print out labels for the function.
853 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000854 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000855
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000856 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000857 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000858 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 case Function::InternalLinkage: // Symbols default to internal.
860 break;
861 case Function::ExternalLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000862 O << "\t.globl\t";
863 CurrentFnSym->print(O, MAI);
864 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000865 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000866 case Function::WeakAnyLinkage:
867 case Function::WeakODRLinkage:
868 case Function::LinkOnceAnyLinkage:
869 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000870 case Function::LinkerPrivateLinkage:
Chris Lattner651adf32010-01-16 00:21:18 +0000871 O << "\t.globl\t";
872 CurrentFnSym->print(O, MAI);
873 O << '\n';
874 O << "\t.weak_definition\t";
875 CurrentFnSym->print(O, MAI);
876 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 break;
878 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000879
Chris Lattner651adf32010-01-16 00:21:18 +0000880 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000881
Bill Wendling25a8ae32009-06-30 22:38:32 +0000882 EmitAlignment(MF.getAlignment(), F);
Chris Lattner651adf32010-01-16 00:21:18 +0000883 CurrentFnSym->print(O, MAI);
884 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885
886 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000887 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888
Bill Wendling36ccaea2008-01-26 06:51:24 +0000889 // If the function is empty, then we need to emit *something*. Otherwise, the
890 // function's label might be associated with something that it wasn't meant to
891 // be associated with. We emit a noop in this situation.
892 MachineFunction::iterator I = MF.begin();
893
Bill Wendlingb5880a72008-01-26 09:03:52 +0000894 if (++I == MF.end() && MF.front().empty())
895 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000896
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 // Print out code for the function.
898 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
899 I != E; ++I) {
900 // Print a label for the basic block.
901 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000902 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000904 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
905 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907 printMachineInstruction(II);
908 }
909 }
910
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000912 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000913
Bill Wendling5019fe02009-11-09 21:45:26 +0000914 // Print out jump tables referenced by the function.
915 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
916
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 // We didn't modify anything.
918 return false;
919}
920
921
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000922void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000923 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000924 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 "ppc",
926 "ppc601",
927 "ppc602",
928 "ppc603",
929 "ppc7400",
930 "ppc750",
931 "ppc970",
932 "ppc64"
933 };
934
935 unsigned Directive = Subtarget.getDarwinDirective();
936 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
937 Directive = PPC::DIR_970;
938 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
939 Directive = PPC::DIR_7400;
940 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
941 Directive = PPC::DIR_64;
942 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000943 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000944
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 // Prime text sections so they are adjacent. This reduces the likelihood a
946 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000947 TargetLoweringObjectFileMachO &TLOFMacho =
948 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000949 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000951 OutStreamer.SwitchSection(
952 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
953 MCSectionMachO::S_SYMBOL_STUBS |
954 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
955 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000957 OutStreamer.SwitchSection(
958 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
959 MCSectionMachO::S_SYMBOL_STUBS |
960 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
961 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000962 }
Chris Lattner73266f92009-08-19 05:49:37 +0000963 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964}
965
Chris Lattnerae982212009-07-21 18:38:57 +0000966void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000967 const TargetData *TD = TM.getTargetData();
968
969 if (!GVar->hasInitializer())
970 return; // External global require no code
971
972 // Check to see if this is a special global used by LLVM, if so, emit it.
973 if (EmitSpecialLLVMGlobal(GVar)) {
974 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000975 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000976 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000977 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000978 O << ".reference .destructors_used\n";
979 }
980 return;
981 }
982
Chris Lattner22b24952010-01-16 01:12:01 +0000983 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
984 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000985
986 Constant *C = GVar->getInitializer();
987 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000988 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000989 unsigned Align = TD->getPreferredAlignmentLog(GVar);
990
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000991 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000992 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000993 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000994
Chris Lattnerdb727932009-08-04 05:35:56 +0000995 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000996 if (C->isNullValue() && /* FIXME: Verify correct */
997 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000998 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000999 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +00001000 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +00001001 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001002 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
1003
1004 if (GVar->hasExternalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001005 O << "\t.globl ";
1006 GVarSym->print(O, MAI);
1007 O << '\n';
1008 O << "\t.zerofill __DATA, __common, ";
1009 GVarSym->print(O, MAI);
1010 O << ", " << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +00001011 } else if (GVar->hasLocalLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001012 O << MAI->getLCOMMDirective();
1013 GVarSym->print(O, MAI);
1014 O << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001015 } else if (!GVar->hasCommonLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001016 O << "\t.globl ";
1017 GVarSym->print(O, MAI);
1018 O << '\n' << MAI->getWeakDefDirective();
1019 GVarSym->print(O, MAI);
1020 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001021 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +00001022 GVarSym->print(O, MAI);
1023 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +00001024 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001025 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001026 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001027 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001028 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001029 EmitGlobalConstant(C);
1030 return;
1031 } else {
Chris Lattner22b24952010-01-16 01:12:01 +00001032 O << ".comm ";
1033 GVarSym->print(O, MAI);
1034 O << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001035 // Darwin 9 and above support aligned common data.
1036 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001037 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001038 }
Evan Cheng11db8142009-03-24 00:17:40 +00001039 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001040 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001041 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001042 O << "'";
1043 }
1044 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001045 return;
1046 }
1047
1048 switch (GVar->getLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +00001049 case GlobalValue::LinkOnceAnyLinkage:
1050 case GlobalValue::LinkOnceODRLinkage:
1051 case GlobalValue::WeakAnyLinkage:
1052 case GlobalValue::WeakODRLinkage:
1053 case GlobalValue::CommonLinkage:
1054 case GlobalValue::LinkerPrivateLinkage:
1055 O << "\t.globl ";
1056 GVarSym->print(O, MAI);
1057 O << "\n\t.weak_definition ";
1058 GVarSym->print(O, MAI);
1059 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001060 break;
Chris Lattner22b24952010-01-16 01:12:01 +00001061 case GlobalValue::AppendingLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001062 // FIXME: appending linkage variables should go into a section of
1063 // their name or something. For now, just emit them as external.
Chris Lattner22b24952010-01-16 01:12:01 +00001064 case GlobalValue::ExternalLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001065 // If external or appending, declare as a global symbol
Chris Lattner22b24952010-01-16 01:12:01 +00001066 O << "\t.globl ";
1067 GVarSym->print(O, MAI);
1068 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001069 // FALL THROUGH
Chris Lattner22b24952010-01-16 01:12:01 +00001070 case GlobalValue::InternalLinkage:
1071 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001072 break;
Chris Lattner22b24952010-01-16 01:12:01 +00001073 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001074 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001075 }
1076
1077 EmitAlignment(Align, GVar);
Chris Lattner22b24952010-01-16 01:12:01 +00001078 GVarSym->print(O, MAI);
1079 O << ":";
Evan Cheng11db8142009-03-24 00:17:40 +00001080 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001081 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001082 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001083 O << "'";
1084 }
1085 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001086
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001087 EmitGlobalConstant(C);
1088 O << '\n';
1089}
1090
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001091bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001092 const TargetData *TD = TM.getTargetData();
1093
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1095
Chris Lattnerf4815552009-08-03 22:52:21 +00001096 // Darwin/PPC always uses mach-o.
1097 TargetLoweringObjectFileMachO &TLOFMacho =
1098 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1099
Chris Lattner72a676a2009-08-10 01:39:42 +00001100
1101 const MCSection *LSPSection = 0;
1102 if (!FnStubs.empty()) // .lazy_symbol_pointer
1103 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1104
1105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001106 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001107 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001108 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001109 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1110 MCSectionMachO::S_SYMBOL_STUBS |
1111 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1112 32, SectionKind::getText());
Chris Lattner9bc87482010-01-13 19:00:57 +00001113 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001114 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001115 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001116 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001117 const FnStubInfo &Info = I->second;
Chris Lattner1774fd32010-01-13 19:13:16 +00001118 Info.Stub->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001119 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001120 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 O << "\tmflr r0\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001122 O << "\tbcl 20,31,";
Chris Lattner1774fd32010-01-13 19:13:16 +00001123 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001124 O << '\n';
Chris Lattner1774fd32010-01-13 19:13:16 +00001125 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001126 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127 O << "\tmflr r11\n";
Chris Lattner9bc87482010-01-13 19:00:57 +00001128 O << "\taddis r11,r11,ha16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001129 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001130 O << '-';
Chris Lattner1774fd32010-01-13 19:13:16 +00001131 Info.AnonSymbol->print(O, MAI);
Evan Chenga65854f2008-12-05 01:06:39 +00001132 O << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001133 O << "\tmtlr r0\n";
Chris Lattner63e910f2009-07-15 02:56:53 +00001134 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001135 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001136 O << '-';
Chris Lattner1774fd32010-01-13 19:13:16 +00001137 Info.AnonSymbol->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001138 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001139 O << "\tmtctr r12\n";
1140 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001141
Chris Lattner73266f92009-08-19 05:49:37 +00001142 OutStreamer.SwitchSection(LSPSection);
Chris Lattner1774fd32010-01-13 19:13:16 +00001143 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001144 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001145 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001146 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001147 }
Chris Lattner189198f2009-07-15 00:55:58 +00001148 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001149 const MCSection *StubSection =
1150 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1151 MCSectionMachO::S_SYMBOL_STUBS |
1152 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1153 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001154
Chris Lattner9ea6e2a2009-07-15 02:28:57 +00001155 for (StringMap<FnStubInfo>::iterator I = FnStubs.begin(), E = FnStubs.end();
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001156 I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001157 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001158 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001159 const FnStubInfo &Info = I->second;
Chris Lattner1774fd32010-01-13 19:13:16 +00001160 Info.Stub->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001161 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001162 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattner9bc87482010-01-13 19:00:57 +00001163 O << "\tlis r11,ha16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001164 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001165 O << ")\n";
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001166 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(";
Chris Lattner1774fd32010-01-13 19:13:16 +00001167 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001168 O << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169 O << "\tmtctr r12\n";
1170 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001171 OutStreamer.SwitchSection(LSPSection);
Chris Lattner1774fd32010-01-13 19:13:16 +00001172 Info.LazyPtr->print(O, MAI);
Chris Lattner9bc87482010-01-13 19:00:57 +00001173 O << ":\n";
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001174 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001175 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 }
1177 }
1178
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001179 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001181 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001182 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001183 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001184 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001185 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001186 E = Personalities.end(); I != E; ++I) {
1187 if (*I)
1188 GVStubs[Mang->getMangledName(*I)] =
Chris Lattner65353d52009-07-15 01:16:38 +00001189 Mang->getMangledName(*I, "$non_lazy_ptr", true);
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001190 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001191 }
1192
Chris Lattnerf4815552009-08-03 22:52:21 +00001193 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001194 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001195 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001196 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001197 EmitAlignment(isPPC64 ? 3 : 2);
1198
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001199 for (StringMap<std::string>::iterator I = GVStubs.begin(),
1200 E = GVStubs.end(); I != E; ++I) {
1201 O << I->second << ":\n";
1202 O << "\t.indirect_symbol " << I->getKeyData() << '\n';
1203 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 }
1205 }
1206
Evan Chenga65854f2008-12-05 01:06:39 +00001207 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001208 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001209 EmitAlignment(isPPC64 ? 3 : 2);
1210 for (StringMap<std::string>::iterator I = HiddenGVStubs.begin(),
1211 E = HiddenGVStubs.end(); I != E; ++I) {
1212 O << I->second << ":\n";
1213 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << I->getKeyData() << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001214 }
1215 }
1216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001217 // Funny Darwin hack: This flag tells the linker that no global symbols
1218 // contain code that falls through to other global symbols (e.g. the obvious
1219 // implementation of multiple entry points). If this doesn't occur, the
1220 // linker can safely perform dead code stripping. Since LLVM never generates
1221 // code that does this, it is always safe to set.
Chris Lattnerfe284f72009-10-19 18:03:08 +00001222 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001223
Dan Gohman4a558a32007-07-25 19:33:14 +00001224 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225}
1226
1227
1228
1229/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1230/// for a MachineFunction to the given output stream, in a format that the
1231/// Darwin assembler can deal with.
1232///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001233static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1234 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001235 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001236 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1238
Chris Lattnerae982212009-07-21 18:38:57 +00001239 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001240 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1241 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001242}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001243
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001244// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001245extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001246 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001247 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1248}