blob: db37a1ab66025042a8d836ccf1bcdeb89f36cd13 [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 Lattner31a54742010-01-16 21:57:06 +000039#include "llvm/Target/Mangler.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000040#include "llvm/Target/TargetLoweringObjectFile.h"
41#include "llvm/Target/TargetRegisterInfo.h"
42#include "llvm/Target/TargetInstrInfo.h"
43#include "llvm/Target/TargetOptions.h"
44#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045#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 Lattnera6f2a102010-01-16 18:37:32 +000073 Stub = Printer->GetSymbolWithGlobalValueBase(GV, "$stub");
74 LazyPtr = Printer->GetSymbolWithGlobalValueBase(GV, "$lazy_ptr");
75 AnonSymbol = Printer->GetSymbolWithGlobalValueBase(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
Chris Lattner5eeaf2a2010-01-16 02:00:23 +000097 DenseMap<const MCSymbol*, FnStubInfo> FnStubs;
Chris Lattnerc3009922010-01-16 02:09:06 +000098 DenseMap<const MCSymbol*, const MCSymbol*> 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 Lattner5eeaf2a2010-01-16 02:00:23 +0000239 FnStubInfo &FnInfo = FnStubs[GetGlobalValueSymbol(GV)];
Chris Lattner9f9fd9d2010-01-16 01:45:47 +0000240 FnInfo.Init(GV, this);
Chris Lattnerce409842010-01-17 21:43:43 +0000241 O << *FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 return;
243 }
244 }
245 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000246 FnStubInfo &FnInfo =
247 FnStubs[GetExternalSymbolSymbol(MO.getSymbolName())];
Chris Lattner9bc87482010-01-13 19:00:57 +0000248 FnInfo.Init(MO.getSymbolName(), Mang, OutContext);
Chris Lattnerce409842010-01-17 21:43:43 +0000249 O << *FnInfo.Stub;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 return;
251 }
252 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 printOp(MI->getOperand(OpNo));
255 }
256 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000257 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 }
259 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000260 O << "\"L" << getFunctionNumber() << "$pb\"\n";
261 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 }
263 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000264 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 printS16ImmOperand(MI, OpNo);
266 } else {
267 if (Subtarget.isDarwin()) O << "ha16(";
268 printOp(MI->getOperand(OpNo));
269 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000270 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 if (Subtarget.isDarwin())
272 O << ')';
273 else
274 O << "@ha";
275 }
276 }
277 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000278 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 printS16ImmOperand(MI, OpNo);
280 } else {
281 if (Subtarget.isDarwin()) O << "lo16(";
282 printOp(MI->getOperand(OpNo));
283 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000284 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 if (Subtarget.isDarwin())
286 O << ')';
287 else
288 O << "@l";
289 }
290 }
291 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
292 unsigned CCReg = MI->getOperand(OpNo).getReg();
293 unsigned RegNo = enumRegToMachineReg(CCReg);
294 O << (0x80 >> RegNo);
295 }
296 // The new addressing mode printers.
297 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
298 printSymbolLo(MI, OpNo);
299 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000300 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 MI->getOperand(OpNo+1).getReg() == PPC::R0)
302 O << "0";
303 else
304 printOperand(MI, OpNo+1);
305 O << ')';
306 }
307 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000308 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000310 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 printSymbolLo(MI, OpNo);
312 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000313 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 MI->getOperand(OpNo+1).getReg() == PPC::R0)
315 O << "0";
316 else
317 printOperand(MI, OpNo+1);
318 O << ')';
319 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000320
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
322 // When used as the base register, r0 reads constant zero rather than
323 // the value contained in the register. For this reason, the darwin
324 // assembler requires that we print r0 as 0 (no r) when used as the base.
325 const MachineOperand &MO = MI->getOperand(OpNo);
326 printRegister(MO, true);
327 O << ", ";
328 printOperand(MI, OpNo+1);
329 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000330
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000331 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
332 const MachineOperand &MO = MI->getOperand(OpNo);
333
334 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
335
Chris Lattnerc3009922010-01-16 02:09:06 +0000336 const MCSymbol *Sym = GetGlobalValueSymbol(MO.getGlobal());
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000337
338 // Map symbol -> label of TOC entry.
Chris Lattnerc3009922010-01-16 02:09:06 +0000339 const MCSymbol *&TOCEntry = TOC[Sym];
340 if (TOCEntry == 0)
341 TOCEntry = OutContext.
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000342 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) + "C" +
343 Twine(LabelID++));
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000344
Chris Lattnerce409842010-01-17 21:43:43 +0000345 O << *TOCEntry << "@toc";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000346 }
347
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000348 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000350
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 };
353
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000354 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000355 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000356 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000357 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000358 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000359 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360
361 virtual const char *getPassName() const {
362 return "Linux PPC Assembly Printer";
363 }
364
365 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000366 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000367
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 void getAnalysisUsage(AnalysisUsage &AU) const {
369 AU.setPreservesAll();
370 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000371 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 PPCAsmPrinter::getAnalysisUsage(AU);
373 }
374
Chris Lattnerae982212009-07-21 18:38:57 +0000375 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 };
377
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000378 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
379 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000380 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000381 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000382 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000383 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000384 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000385 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386
387 virtual const char *getPassName() const {
388 return "Darwin PPC Assembly Printer";
389 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000390
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000393 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000394
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 void getAnalysisUsage(AnalysisUsage &AU) const {
396 AU.setPreservesAll();
397 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000398 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 PPCAsmPrinter::getAnalysisUsage(AU);
400 }
401
Chris Lattnerae982212009-07-21 18:38:57 +0000402 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 };
404} // end of anonymous namespace
405
406// Include the auto-generated portion of the assembly writer
407#include "PPCGenAsmWriter.inc"
408
409void PPCAsmPrinter::printOp(const MachineOperand &MO) {
410 switch (MO.getType()) {
411 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000412 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413
414 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerce409842010-01-17 21:43:43 +0000415 O << *GetMBBSymbol(MO.getMBB()->getNumber());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 return;
417 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000418 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000419 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 // FIXME: PIC relocation model
421 return;
422 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000423 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000424 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000426 case MachineOperand::MO_BlockAddress:
Chris Lattnerce409842010-01-17 21:43:43 +0000427 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilsone8cbca92009-11-04 21:31:18 +0000428 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000429 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 // Computing the address of an external symbol, not calling it.
Chris Lattnerc3009922010-01-16 02:09:06 +0000431 const MCSymbol *SymName = GetExternalSymbolSymbol(MO.getSymbolName());
432 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerce409842010-01-17 21:43:43 +0000433 O << *SymName;
Chris Lattnerc3009922010-01-16 02:09:06 +0000434 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 }
Chris Lattnerc3009922010-01-16 02:09:06 +0000436 const MCSymbol *NLPSym =
437 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
438 MO.getSymbolName()+"$non_lazy_ptr");
439 GVStubs[SymName] = NLPSym;
Chris Lattnerce409842010-01-17 21:43:43 +0000440 O << *NLPSym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000442 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 case MachineOperand::MO_GlobalAddress: {
444 // Computing the address of a global symbol, not calling it.
445 GlobalValue *GV = MO.getGlobal();
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000446 MCSymbol *SymToPrint;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447
448 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000449 if (TM.getRelocationModel() != Reloc::Static &&
450 (GV->isDeclaration() || GV->isWeakForLinker())) {
451 if (!GV->hasHiddenVisibility()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000452 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc3009922010-01-16 02:09:06 +0000453 GVStubs[GetGlobalValueSymbol(GV)] = SymToPrint;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000454 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
455 GV->hasAvailableExternallyLinkage()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000456 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc3009922010-01-16 02:09:06 +0000457 HiddenGVStubs[GetGlobalValueSymbol(GV)] = SymToPrint;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000458 } else {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000459 SymToPrint = GetGlobalValueSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000461 } else {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000462 SymToPrint = GetGlobalValueSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 }
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000464
Chris Lattnerce409842010-01-17 21:43:43 +0000465 O << *SymToPrint;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000466
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000467 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 return;
469 }
470
471 default:
472 O << "<unknown operand type: " << MO.getType() << ">";
473 return;
474 }
475}
476
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477/// PrintAsmOperand - Print out an operand for an inline asm expression.
478///
479bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000480 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 const char *ExtraCode) {
482 // Does this asm operand have a single letter operand modifier?
483 if (ExtraCode && ExtraCode[0]) {
484 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000485
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 switch (ExtraCode[0]) {
487 default: return true; // Unknown modifier.
488 case 'c': // Don't print "$" before a global var name or constant.
489 // PPC never has a prefix.
490 printOperand(MI, OpNo);
491 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000492 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000494 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000496 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 return true;
498 ++OpNo; // Return the high-part.
499 break;
500 case 'I':
501 // Write 'i' if an integer constant, otherwise nothing. Used to print
502 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000503 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 O << "i";
505 return false;
506 }
507 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000508
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 printOperand(MI, OpNo);
510 return false;
511}
512
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000513// At the moment, all inline asm memory operands are a single register.
514// In any case, the output of this routine should always be just one
515// assembler operand.
516
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000518 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 const char *ExtraCode) {
520 if (ExtraCode && ExtraCode[0])
521 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000522 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000523 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000524 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000525 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 return false;
527}
528
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000529void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 const char *Modifier) {
531 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
532 unsigned Code = MI->getOperand(OpNo).getImm();
533 if (!strcmp(Modifier, "cc")) {
534 switch ((PPC::Predicate)Code) {
535 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
536 case PPC::PRED_LT: O << "lt"; return;
537 case PPC::PRED_LE: O << "le"; return;
538 case PPC::PRED_EQ: O << "eq"; return;
539 case PPC::PRED_GE: O << "ge"; return;
540 case PPC::PRED_GT: O << "gt"; return;
541 case PPC::PRED_NE: O << "ne"; return;
542 case PPC::PRED_UN: O << "un"; return;
543 case PPC::PRED_NU: O << "nu"; return;
544 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000545
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546 } else {
547 assert(!strcmp(Modifier, "reg") &&
548 "Need to specify 'cc' or 'reg' as predicate op modifier!");
549 // Don't print the register for 'always'.
550 if (Code == PPC::PRED_ALWAYS) return;
551 printOperand(MI, OpNo+1);
552 }
553}
554
555
556/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
557/// the current output stream.
558///
559void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
560 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000561
Devang Patel5450fc12009-10-06 02:19:11 +0000562 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563
564 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000565 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000567 unsigned char SH = MI->getOperand(2).getImm();
568 unsigned char MB = MI->getOperand(3).getImm();
569 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000571 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 }
573 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000574 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 SH = 32-SH;
576 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000577 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 printOperand(MI, 0);
579 O << ", ";
580 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000581 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582 }
583 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
584 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000585 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000586 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 printOperand(MI, 0);
588 O << ", ";
589 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 }
591 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000592 unsigned char SH = MI->getOperand(2).getImm();
593 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
595 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000596 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000597 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 printOperand(MI, 0);
599 O << ", ";
600 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000601 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 }
603 }
604
Dale Johannesen760acc02010-01-06 02:20:18 +0000605 if (!useSubstituteMnemonic)
606 printInstruction(MI);
607
David Greeneca9b04b2009-11-13 21:34:57 +0000608 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000609 EmitComments(*MI);
610 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000611
612 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613}
614
615/// runOnMachineFunction - This uses the printMachineInstruction()
616/// method to print assembly for each instruction.
617///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000618bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000619 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620
621 SetupMachineFunction(MF);
622 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000623
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 // Print out constants referenced by the function
625 EmitConstantPool(MF.getConstantPool());
626
627 // Print out labels for the function.
628 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000629 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000630
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000632 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000633 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 case Function::InternalLinkage: // Symbols default to internal.
635 break;
636 case Function::ExternalLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000637 O << "\t.global\t" << *CurrentFnSym << '\n' << "\t.type\t";
638 O << *CurrentFnSym << ", @function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000640 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000641 case Function::WeakAnyLinkage:
642 case Function::WeakODRLinkage:
643 case Function::LinkOnceAnyLinkage:
644 case Function::LinkOnceODRLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000645 O << "\t.global\t" << *CurrentFnSym << '\n';
646 O << "\t.weak\t" << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647 break;
648 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000649
Chris Lattner651adf32010-01-16 00:21:18 +0000650 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000651
Bill Wendling25a8ae32009-06-30 22:38:32 +0000652 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000653
654 if (Subtarget.isPPC64()) {
655 // Emit an official procedure descriptor.
Chris Lattner651adf32010-01-16 00:21:18 +0000656 // FIXME 64-bit SVR4: Use MCSection here!
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000657 O << "\t.section\t\".opd\",\"aw\"\n";
658 O << "\t.align 3\n";
Chris Lattnerce409842010-01-17 21:43:43 +0000659 O << *CurrentFnSym << ":\n";
660 O << "\t.quad .L." << *CurrentFnSym << ",.TOC.@tocbase\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000661 O << "\t.previous\n";
Chris Lattnerce409842010-01-17 21:43:43 +0000662 O << ".L." << *CurrentFnSym << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000663 } else {
Chris Lattnerce409842010-01-17 21:43:43 +0000664 O << *CurrentFnSym << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000665 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666
667 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000668 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669
670 // Print out code for the function.
671 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
672 I != E; ++I) {
673 // Print a label for the basic block.
674 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000675 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 }
677 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
678 II != E; ++II) {
679 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 printMachineInstruction(II);
681 }
682 }
683
Chris Lattnerce409842010-01-17 21:43:43 +0000684 O << "\t.size\t" << *CurrentFnSym << ",.-" << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000685
Chris Lattner73266f92009-08-19 05:49:37 +0000686 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000687
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000689 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000690
Bill Wendling6fb58e12009-11-09 21:20:14 +0000691 // Print out jump tables referenced by the function.
692 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
693
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 // We didn't modify anything.
695 return false;
696}
697
Chris Lattnerae982212009-07-21 18:38:57 +0000698void PPCLinuxAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 const TargetData *TD = TM.getTargetData();
700
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000701 if (!GVar->hasInitializer())
702 return; // External global require no code
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000703
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000704 // Check to see if this is a special global used by LLVM, if so, emit it.
705 if (EmitSpecialLLVMGlobal(GVar))
706 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707
Chris Lattner22b24952010-01-16 01:12:01 +0000708 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709
Chris Lattner22b24952010-01-16 01:12:01 +0000710 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000711
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000712 Constant *C = GVar->getInitializer();
713 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000714 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000715 unsigned Align = TD->getPreferredAlignmentLog(GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
Chris Lattner73266f92009-08-19 05:49:37 +0000717 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
718 TM));
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000719
720 if (C->isNullValue() && /* FIXME: Verify correct */
721 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000722 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000723 GVar->isWeakForLinker())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000725
726 if (GVar->hasExternalLinkage()) {
Chris Lattnerce409842010-01-17 21:43:43 +0000727 O << "\t.global " << *GVarSym << '\n';
728 O << "\t.type " << *GVarSym << ", @object\n";
729 O << *GVarSym << ":\n";
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000730 O << "\t.zero " << Size << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000731 } else if (GVar->hasLocalLinkage()) {
Chris Lattnerce409842010-01-17 21:43:43 +0000732 O << MAI->getLCOMMDirective() << *GVarSym << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733 } else {
Chris Lattnerce409842010-01-17 21:43:43 +0000734 O << ".comm " << *GVarSym << ',' << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 }
Evan Cheng11db8142009-03-24 00:17:40 +0000736 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000737 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000738 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000739 O << "'";
740 }
741 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000742 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743 }
744
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000745 switch (GVar->getLinkage()) {
Duncan Sands19d161f2009-03-07 15:45:40 +0000746 case GlobalValue::LinkOnceAnyLinkage:
747 case GlobalValue::LinkOnceODRLinkage:
748 case GlobalValue::WeakAnyLinkage:
749 case GlobalValue::WeakODRLinkage:
Duncan Sandsb95df792009-03-11 20:14:15 +0000750 case GlobalValue::CommonLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000751 case GlobalValue::LinkerPrivateLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000752 O << "\t.global " << *GVarSym;
753 O << "\n\t.type " << *GVarSym << ", @object\n\t.weak " << *GVarSym << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000754 break;
755 case GlobalValue::AppendingLinkage:
756 // FIXME: appending linkage variables should go into a section of
757 // their name or something. For now, just emit them as external.
758 case GlobalValue::ExternalLinkage:
759 // If external or appending, declare as a global symbol
Chris Lattnerce409842010-01-17 21:43:43 +0000760 O << "\t.global " << *GVarSym;
761 O << "\n\t.type " << *GVarSym << ", @object\n";
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000762 // FALL THROUGH
763 case GlobalValue::InternalLinkage:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000764 case GlobalValue::PrivateLinkage:
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000765 break;
766 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000767 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000768 }
769
770 EmitAlignment(Align, GVar);
Chris Lattnerce409842010-01-17 21:43:43 +0000771 O << *GVarSym << ":";
Evan Cheng11db8142009-03-24 00:17:40 +0000772 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000773 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000774 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000775 O << "'";
776 }
777 O << '\n';
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000778
Anton Korobeynikov894be4b2008-08-08 18:23:49 +0000779 EmitGlobalConstant(C);
780 O << '\n';
781}
782
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000783bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
784 const TargetData *TD = TM.getTargetData();
785
786 bool isPPC64 = TD->getPointerSizeInBits() == 64;
787
788 if (isPPC64 && !TOC.empty()) {
789 // FIXME 64-bit SVR4: Use MCSection here?
790 O << "\t.section\t\".toc\",\"aw\"\n";
791
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000792 // FIXME: This is nondeterminstic!
Chris Lattnerc3009922010-01-16 02:09:06 +0000793 for (DenseMap<const MCSymbol*, const MCSymbol*>::iterator I = TOC.begin(),
794 E = TOC.end(); I != E; ++I) {
Chris Lattnerce409842010-01-17 21:43:43 +0000795 O << *I->second << ":\n";
796 O << "\t.tc " << *I->first << "[TC]," << *I->first << '\n';
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000797 }
798 }
799
800 return AsmPrinter::doFinalization(M);
801}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803/// runOnMachineFunction - This uses the printMachineInstruction()
804/// method to print assembly for each instruction.
805///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000806bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000807 this->MF = &MF;
808
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 SetupMachineFunction(MF);
810 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000811
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 // Print out constants referenced by the function
813 EmitConstantPool(MF.getConstantPool());
814
815 // Print out labels for the function.
816 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000817 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000818
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000820 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000821 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822 case Function::InternalLinkage: // Symbols default to internal.
823 break;
824 case Function::ExternalLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000825 O << "\t.globl\t" << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000827 case Function::WeakAnyLinkage:
828 case Function::WeakODRLinkage:
829 case Function::LinkOnceAnyLinkage:
830 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000831 case Function::LinkerPrivateLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000832 O << "\t.globl\t" << *CurrentFnSym << '\n';
833 O << "\t.weak_definition\t" << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000834 break;
835 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000836
Chris Lattner651adf32010-01-16 00:21:18 +0000837 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000838
Bill Wendling25a8ae32009-06-30 22:38:32 +0000839 EmitAlignment(MF.getAlignment(), F);
Chris Lattnerce409842010-01-17 21:43:43 +0000840 O << *CurrentFnSym << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841
842 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000843 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844
Bill Wendling36ccaea2008-01-26 06:51:24 +0000845 // If the function is empty, then we need to emit *something*. Otherwise, the
846 // function's label might be associated with something that it wasn't meant to
847 // be associated with. We emit a noop in this situation.
848 MachineFunction::iterator I = MF.begin();
849
Bill Wendlingb5880a72008-01-26 09:03:52 +0000850 if (++I == MF.end() && MF.front().empty())
851 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000852
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 // Print out code for the function.
854 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
855 I != E; ++I) {
856 // Print a label for the basic block.
857 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000858 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000860 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
861 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 printMachineInstruction(II);
864 }
865 }
866
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000868 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000869
Bill Wendling5019fe02009-11-09 21:45:26 +0000870 // Print out jump tables referenced by the function.
871 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
872
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 // We didn't modify anything.
874 return false;
875}
876
877
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000878void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000879 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000880 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 "ppc",
882 "ppc601",
883 "ppc602",
884 "ppc603",
885 "ppc7400",
886 "ppc750",
887 "ppc970",
888 "ppc64"
889 };
890
891 unsigned Directive = Subtarget.getDarwinDirective();
892 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
893 Directive = PPC::DIR_970;
894 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
895 Directive = PPC::DIR_7400;
896 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
897 Directive = PPC::DIR_64;
898 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000899 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000900
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 // Prime text sections so they are adjacent. This reduces the likelihood a
902 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000903 TargetLoweringObjectFileMachO &TLOFMacho =
904 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000905 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000907 OutStreamer.SwitchSection(
908 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
909 MCSectionMachO::S_SYMBOL_STUBS |
910 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
911 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000913 OutStreamer.SwitchSection(
914 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
915 MCSectionMachO::S_SYMBOL_STUBS |
916 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
917 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 }
Chris Lattner73266f92009-08-19 05:49:37 +0000919 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920}
921
Chris Lattnerae982212009-07-21 18:38:57 +0000922void PPCDarwinAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000923 const TargetData *TD = TM.getTargetData();
924
925 if (!GVar->hasInitializer())
926 return; // External global require no code
927
928 // Check to see if this is a special global used by LLVM, if so, emit it.
929 if (EmitSpecialLLVMGlobal(GVar)) {
930 if (TM.getRelocationModel() == Reloc::Static) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000931 if (GVar->getName() == "llvm.global_ctors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000932 O << ".reference .constructors_used\n";
Daniel Dunbare03513b2009-07-25 23:55:21 +0000933 else if (GVar->getName() == "llvm.global_dtors")
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000934 O << ".reference .destructors_used\n";
935 }
936 return;
937 }
938
Chris Lattner22b24952010-01-16 01:12:01 +0000939 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
940 printVisibility(GVarSym, GVar->getVisibility());
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000941
942 Constant *C = GVar->getInitializer();
943 const Type *Type = C->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000944 unsigned Size = TD->getTypeAllocSize(Type);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000945 unsigned Align = TD->getPreferredAlignmentLog(GVar);
946
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000947 const MCSection *TheSection =
Chris Lattner2931fe42009-07-29 05:09:30 +0000948 getObjFileLowering().SectionForGlobal(GVar, Mang, TM);
Chris Lattner73266f92009-08-19 05:49:37 +0000949 OutStreamer.SwitchSection(TheSection);
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000950
Chris Lattnerdb727932009-08-04 05:35:56 +0000951 /// FIXME: Drive this off the section!
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000952 if (C->isNullValue() && /* FIXME: Verify correct */
953 !GVar->hasSection() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000954 (GVar->hasLocalLinkage() || GVar->hasExternalLinkage() ||
Duncan Sands19d161f2009-03-07 15:45:40 +0000955 GVar->isWeakForLinker()) &&
Chris Lattner87bc69b2009-07-24 04:08:17 +0000956 // Don't put things that should go in the cstring section into "comm".
Chris Lattnerd8310522009-07-27 05:32:16 +0000957 !TheSection->getKind().isMergeableCString()) {
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000958 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
959
960 if (GVar->hasExternalLinkage()) {
Chris Lattnerce409842010-01-17 21:43:43 +0000961 O << "\t.globl " << *GVarSym << '\n';
962 O << "\t.zerofill __DATA, __common, " << *GVarSym << ", "
963 << Size << ", " << Align;
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000964 } else if (GVar->hasLocalLinkage()) {
Chris Lattnerce409842010-01-17 21:43:43 +0000965 O << MAI->getLCOMMDirective() << *GVarSym << ',' << Size << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000966 } else if (!GVar->hasCommonLinkage()) {
Chris Lattnerce409842010-01-17 21:43:43 +0000967 O << "\t.globl " << *GVarSym << '\n' << MAI->getWeakDefDirective();
968 O << *GVarSym << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000969 EmitAlignment(Align, GVar);
Chris Lattnerce409842010-01-17 21:43:43 +0000970 O << *GVarSym << ":";
Evan Cheng11db8142009-03-24 00:17:40 +0000971 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000972 O << "\t\t\t\t" << MAI->getCommentString() << " ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000973 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000974 }
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000975 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000976 EmitGlobalConstant(C);
977 return;
978 } else {
Chris Lattnerce409842010-01-17 21:43:43 +0000979 O << ".comm " << *GVarSym << ',' << Size;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000980 // Darwin 9 and above support aligned common data.
981 if (Subtarget.isDarwin9())
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000982 O << ',' << Align;
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000983 }
Evan Cheng11db8142009-03-24 00:17:40 +0000984 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000985 O << "\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000986 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +0000987 O << "'";
988 }
989 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +0000990 return;
991 }
992
993 switch (GVar->getLinkage()) {
Chris Lattner22b24952010-01-16 01:12:01 +0000994 case GlobalValue::LinkOnceAnyLinkage:
995 case GlobalValue::LinkOnceODRLinkage:
996 case GlobalValue::WeakAnyLinkage:
997 case GlobalValue::WeakODRLinkage:
998 case GlobalValue::CommonLinkage:
999 case GlobalValue::LinkerPrivateLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +00001000 O << "\t.globl " << *GVarSym << "\n\t.weak_definition " << *GVarSym << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001001 break;
Chris Lattner22b24952010-01-16 01:12:01 +00001002 case GlobalValue::AppendingLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001003 // FIXME: appending linkage variables should go into a section of
1004 // their name or something. For now, just emit them as external.
Chris Lattner22b24952010-01-16 01:12:01 +00001005 case GlobalValue::ExternalLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001006 // If external or appending, declare as a global symbol
Chris Lattnerce409842010-01-17 21:43:43 +00001007 O << "\t.globl " << *GVarSym << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001008 // FALL THROUGH
Chris Lattner22b24952010-01-16 01:12:01 +00001009 case GlobalValue::InternalLinkage:
1010 case GlobalValue::PrivateLinkage:
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001011 break;
Chris Lattner22b24952010-01-16 01:12:01 +00001012 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00001013 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001014 }
1015
1016 EmitAlignment(Align, GVar);
Chris Lattnerce409842010-01-17 21:43:43 +00001017 O << *GVarSym << ":";
Evan Cheng11db8142009-03-24 00:17:40 +00001018 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001019 O << "\t\t\t\t" << MAI->getCommentString() << " '";
Dan Gohman2aa282f2009-08-13 01:36:44 +00001020 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Evan Cheng11db8142009-03-24 00:17:40 +00001021 O << "'";
1022 }
1023 O << '\n';
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001024
Anton Korobeynikov248e9c52008-08-08 18:23:25 +00001025 EmitGlobalConstant(C);
1026 O << '\n';
1027}
1028
Anton Korobeynikov28f86d12008-08-08 18:22:59 +00001029bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 const TargetData *TD = TM.getTargetData();
1031
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001032 bool isPPC64 = TD->getPointerSizeInBits() == 64;
1033
Chris Lattnerf4815552009-08-03 22:52:21 +00001034 // Darwin/PPC always uses mach-o.
1035 TargetLoweringObjectFileMachO &TLOFMacho =
1036 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
1037
Chris Lattner72a676a2009-08-10 01:39:42 +00001038
1039 const MCSection *LSPSection = 0;
1040 if (!FnStubs.empty()) // .lazy_symbol_pointer
1041 LSPSection = TLOFMacho.getLazySymbolPointerSection();
1042
1043
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001044 // Output stubs for dynamically-linked functions
Chris Lattner189198f2009-07-15 00:55:58 +00001045 if (TM.getRelocationModel() == Reloc::PIC_ && !FnStubs.empty()) {
Chris Lattnerf4815552009-08-03 22:52:21 +00001046 const MCSection *StubSection =
Chris Lattner72a676a2009-08-10 01:39:42 +00001047 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
1048 MCSectionMachO::S_SYMBOL_STUBS |
1049 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1050 32, SectionKind::getText());
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001051 // FIXME: This is emitting in nondeterminstic order!
1052 for (DenseMap<const MCSymbol*, FnStubInfo>::iterator I =
1053 FnStubs.begin(), E = FnStubs.end(); I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001054 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001056 const FnStubInfo &Info = I->second;
Chris Lattnerce409842010-01-17 21:43:43 +00001057 O << *Info.Stub << ":\n";
1058 O << "\t.indirect_symbol " << *I->first << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059 O << "\tmflr r0\n";
Chris Lattnerce409842010-01-17 21:43:43 +00001060 O << "\tbcl 20,31," << *Info.AnonSymbol << '\n';
1061 O << *Info.AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 O << "\tmflr r11\n";
Chris Lattnerce409842010-01-17 21:43:43 +00001063 O << "\taddis r11,r11,ha16(" << *Info.LazyPtr << '-' << *Info.AnonSymbol
1064 << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001065 O << "\tmtlr r0\n";
Chris Lattnerce409842010-01-17 21:43:43 +00001066 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *Info.LazyPtr
1067 << '-' << *Info.AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 O << "\tmtctr r12\n";
1069 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +00001070
Chris Lattner73266f92009-08-19 05:49:37 +00001071 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerce409842010-01-17 21:43:43 +00001072 O << *Info.LazyPtr << ":\n";
1073 O << "\t.indirect_symbol " << *I->first << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001074 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 }
Chris Lattner189198f2009-07-15 00:55:58 +00001076 } else if (!FnStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001077 const MCSection *StubSection =
1078 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
1079 MCSectionMachO::S_SYMBOL_STUBS |
1080 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1081 16, SectionKind::getText());
Chris Lattnerf4815552009-08-03 22:52:21 +00001082
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001083 // FIXME: This is emitting in nondeterminstic order!
1084 for (DenseMap<const MCSymbol*, FnStubInfo>::iterator I = FnStubs.begin(),
1085 E = FnStubs.end(); I != E; ++I) {
Chris Lattner73266f92009-08-19 05:49:37 +00001086 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001087 EmitAlignment(4);
Chris Lattnerdc4cce62009-07-15 02:33:19 +00001088 const FnStubInfo &Info = I->second;
Chris Lattnerce409842010-01-17 21:43:43 +00001089 O << *Info.Stub << ":\n";
1090 O << "\t.indirect_symbol " << *I->first << '\n';
1091 O << "\tlis r11,ha16(" << *Info.LazyPtr << ")\n";
1092 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *Info.LazyPtr
1093 << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001094 O << "\tmtctr r12\n";
1095 O << "\tbctr\n";
Chris Lattner73266f92009-08-19 05:49:37 +00001096 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerce409842010-01-17 21:43:43 +00001097 O << *Info.LazyPtr << ":\n";
1098 O << "\t.indirect_symbol " << *I->first << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +00001099 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001100 }
1101 }
1102
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +00001103 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001104
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001105 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001106 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +00001107 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +00001108 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001109 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001110 E = Personalities.end(); I != E; ++I) {
1111 if (*I)
Chris Lattnerc3009922010-01-16 02:09:06 +00001112 GVStubs[GetGlobalValueSymbol(*I)] =
Chris Lattnera6f2a102010-01-16 18:37:32 +00001113 GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001114 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +00001115 }
1116
Chris Lattnerf4815552009-08-03 22:52:21 +00001117 // Output macho stubs for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001118 if (!GVStubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +00001119 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +00001120 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +00001121 EmitAlignment(isPPC64 ? 3 : 2);
1122
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001123 // FIXME: This is nondeterminstic.
Chris Lattnerc3009922010-01-16 02:09:06 +00001124 for (DenseMap<const MCSymbol *, const MCSymbol *>::iterator
1125 I = GVStubs.begin(), E = GVStubs.end(); I != E; ++I) {
Chris Lattnerce409842010-01-17 21:43:43 +00001126 O << *I->second << ":\n";
1127 O << "\t.indirect_symbol " << *I->first << '\n';
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001128 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001129 }
1130 }
1131
Evan Chenga65854f2008-12-05 01:06:39 +00001132 if (!HiddenGVStubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +00001133 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +00001134 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattner5eeaf2a2010-01-16 02:00:23 +00001135 // FIXME: This is nondeterminstic.
Chris Lattnerc3009922010-01-16 02:09:06 +00001136 for (DenseMap<const MCSymbol *, const MCSymbol *>::iterator
1137 I = HiddenGVStubs.begin(), E = HiddenGVStubs.end(); I != E; ++I) {
Chris Lattnerce409842010-01-17 21:43:43 +00001138 O << *I->second << ":\n";
1139 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << *I->first << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +00001140 }
1141 }
1142
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 // Funny Darwin hack: This flag tells the linker that no global symbols
1144 // contain code that falls through to other global symbols (e.g. the obvious
1145 // implementation of multiple entry points). If this doesn't occur, the
1146 // linker can safely perform dead code stripping. Since LLVM never generates
1147 // code that does this, it is always safe to set.
Chris Lattnerfe284f72009-10-19 18:03:08 +00001148 OutStreamer.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001149
Dan Gohman4a558a32007-07-25 19:33:14 +00001150 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001151}
1152
1153
1154
1155/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1156/// for a MachineFunction to the given output stream, in a format that the
1157/// Darwin assembler can deal with.
1158///
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001159static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
1160 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +00001161 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +00001162 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001163 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1164
Chris Lattnerae982212009-07-21 18:38:57 +00001165 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +00001166 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
1167 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +00001169
Bob Wilsonebbc1c42009-06-23 23:59:40 +00001170// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001171extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001172 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +00001173 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1174}