blob: 6b0931480e3431216d4c4477cd97370596f1034b [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
31#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling36ccaea2008-01-26 06:51:24 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerc2cfe152010-01-20 21:16:14 +000033#include "llvm/CodeGen/MachineModuleInfoImpls.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 Lattnerc2cfe152010-01-20 21:16:14 +000061 DenseMap<const MCSymbol*, const MCSymbol*> TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000063 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +000064 public:
David Greene302008d2009-07-14 20:18:05 +000065 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +000066 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000067 : AsmPrinter(O, TM, T, V),
Tilmann Scheller72cf2812009-08-15 11:54:46 +000068 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
70 virtual const char *getPassName() const {
71 return "PowerPC Assembly Printer";
72 }
73
74 PPCTargetMachine &getTM() {
75 return static_cast<PPCTargetMachine&>(TM);
76 }
77
78 unsigned enumRegToMachineReg(unsigned enumReg) {
79 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +000080 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 case PPC::CR0: return 0;
82 case PPC::CR1: return 1;
83 case PPC::CR2: return 2;
84 case PPC::CR3: return 3;
85 case PPC::CR4: return 4;
86 case PPC::CR5: return 5;
87 case PPC::CR6: return 6;
88 case PPC::CR7: return 7;
89 }
Edwin Törökbd448e32009-07-14 16:55:14 +000090 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 }
92
93 /// printInstruction - This method is automatically generated by tablegen
94 /// from the instruction set description. This method returns true if the
95 /// machine instruction was sufficiently described to print it, otherwise it
96 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +000097 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +000098 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +000099
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 void printMachineInstruction(const MachineInstr *MI);
102 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000103
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 /// stripRegisterPrefix - This method strips the character prefix from a
105 /// register name so that only the number is left. Used by for linux asm.
106 const char *stripRegisterPrefix(const char *RegName) {
107 switch (RegName[0]) {
108 case 'r':
109 case 'f':
110 case 'v': return RegName + 1;
111 case 'c': if (RegName[1] == 'r') return RegName + 2;
112 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 return RegName;
115 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 /// printRegister - Print register according to target requirements.
118 ///
119 void printRegister(const MachineOperand &MO, bool R0AsZero) {
120 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000121 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 // If we should use 0 for R0.
124 if (R0AsZero && RegNo == PPC::R0) {
125 O << "0";
126 return;
127 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000128
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000129 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 // Linux assembler (Others?) does not take register mnemonics.
131 // FIXME - What about special registers used in mfspr/mtspr?
132 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
133 O << RegName;
134 }
135
136 void printOperand(const MachineInstr *MI, unsigned OpNo) {
137 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000138 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000140 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000141 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 } else {
143 printOp(MO);
144 }
145 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000146
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
148 unsigned AsmVariant, const char *ExtraCode);
149 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
150 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000151
152
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000154 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 value = (value << (32-5)) >> (32-5);
156 O << (int)value;
157 }
158 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000159 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 assert(value <= 31 && "Invalid u5imm argument!");
161 O << (unsigned int)value;
162 }
163 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000164 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 assert(value <= 63 && "Invalid u6imm argument!");
166 O << (unsigned int)value;
167 }
168 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000169 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 }
171 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000172 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 }
174 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000175 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000176 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 } else {
178 O << "lo16(";
179 printOp(MI->getOperand(OpNo));
180 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000181 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 else
183 O << ')';
184 }
185 }
186 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
187 // Branches can take an immediate operand. This is used by the branch
188 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000189 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000190 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 } else {
192 printOp(MI->getOperand(OpNo));
193 }
194 }
195 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
196 const MachineOperand &MO = MI->getOperand(OpNo);
197 if (TM.getRelocationModel() != Reloc::Static) {
198 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
199 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000200 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 // Dynamically-resolved functions need a stub for the function.
Chris Lattnerd5a83252010-01-20 21:36:48 +0000202 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
203 const MCSymbol *&StubSym =
204 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
205 if (StubSym == 0)
206 StubSym = GetGlobalValueSymbol(GV);
207 O << *Sym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 return;
209 }
210 }
211 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnerd5a83252010-01-20 21:36:48 +0000212 SmallString<128> TempNameStr;
213 TempNameStr += StringRef(MO.getSymbolName());
214 TempNameStr += StringRef("$stub");
215
216 const MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
217 const MCSymbol *&StubSym =
218 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Chris Lattner27595f22010-01-21 20:43:39 +0000219 if (StubSym == 0)
Chris Lattner922f32f2010-01-21 19:58:19 +0000220 StubSym = GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000221 O << *Sym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 return;
223 }
224 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 printOp(MI->getOperand(OpNo));
227 }
228 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000229 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 }
231 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000232 O << "\"L" << getFunctionNumber() << "$pb\"\n";
233 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 }
235 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000236 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 printS16ImmOperand(MI, OpNo);
238 } else {
239 if (Subtarget.isDarwin()) O << "ha16(";
240 printOp(MI->getOperand(OpNo));
241 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000242 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 if (Subtarget.isDarwin())
244 O << ')';
245 else
246 O << "@ha";
247 }
248 }
249 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000250 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 printS16ImmOperand(MI, OpNo);
252 } else {
253 if (Subtarget.isDarwin()) O << "lo16(";
254 printOp(MI->getOperand(OpNo));
255 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000256 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 if (Subtarget.isDarwin())
258 O << ')';
259 else
260 O << "@l";
261 }
262 }
263 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
264 unsigned CCReg = MI->getOperand(OpNo).getReg();
265 unsigned RegNo = enumRegToMachineReg(CCReg);
266 O << (0x80 >> RegNo);
267 }
268 // The new addressing mode printers.
269 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
270 printSymbolLo(MI, OpNo);
271 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000272 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 MI->getOperand(OpNo+1).getReg() == PPC::R0)
274 O << "0";
275 else
276 printOperand(MI, OpNo+1);
277 O << ')';
278 }
279 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000280 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000282 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 printSymbolLo(MI, OpNo);
284 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000285 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 MI->getOperand(OpNo+1).getReg() == PPC::R0)
287 O << "0";
288 else
289 printOperand(MI, OpNo+1);
290 O << ')';
291 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000292
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
294 // When used as the base register, r0 reads constant zero rather than
295 // the value contained in the register. For this reason, the darwin
296 // assembler requires that we print r0 as 0 (no r) when used as the base.
297 const MachineOperand &MO = MI->getOperand(OpNo);
298 printRegister(MO, true);
299 O << ", ";
300 printOperand(MI, OpNo+1);
301 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000302
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000303 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
304 const MachineOperand &MO = MI->getOperand(OpNo);
305
306 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
307
Chris Lattnerc3009922010-01-16 02:09:06 +0000308 const MCSymbol *Sym = GetGlobalValueSymbol(MO.getGlobal());
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000309
310 // Map symbol -> label of TOC entry.
Chris Lattnerc3009922010-01-16 02:09:06 +0000311 const MCSymbol *&TOCEntry = TOC[Sym];
312 if (TOCEntry == 0)
313 TOCEntry = OutContext.
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000314 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) + "C" +
315 Twine(LabelID++));
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000316
Chris Lattnerce409842010-01-17 21:43:43 +0000317 O << *TOCEntry << "@toc";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000318 }
319
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000320 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 const char *Modifier);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000322
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 virtual bool runOnMachineFunction(MachineFunction &F) = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 };
325
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000326 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000327 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000328 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000329 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000330 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000331 : PPCAsmPrinter(O, TM, T, V){}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332
333 virtual const char *getPassName() const {
334 return "Linux PPC Assembly Printer";
335 }
336
337 bool runOnMachineFunction(MachineFunction &F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000338 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000339
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 void getAnalysisUsage(AnalysisUsage &AU) const {
341 AU.setPreservesAll();
342 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000343 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 PPCAsmPrinter::getAnalysisUsage(AU);
345 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 };
347
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000348 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
349 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000350 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000351 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000352 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000353 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +0000354 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +0000355 : PPCAsmPrinter(O, TM, T, V), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356
357 virtual const char *getPassName() const {
358 return "Darwin PPC Assembly Printer";
359 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000360
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000363 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000364
Chris Lattnerd5a83252010-01-20 21:36:48 +0000365 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Chris Lattner5b187502010-01-20 21:19:44 +0000366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 void getAnalysisUsage(AnalysisUsage &AU) const {
368 AU.setPreservesAll();
369 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000370 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 PPCAsmPrinter::getAnalysisUsage(AU);
372 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 };
374} // end of anonymous namespace
375
376// Include the auto-generated portion of the assembly writer
377#include "PPCGenAsmWriter.inc"
378
379void PPCAsmPrinter::printOp(const MachineOperand &MO) {
380 switch (MO.getType()) {
381 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000382 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383
384 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerce409842010-01-17 21:43:43 +0000385 O << *GetMBBSymbol(MO.getMBB()->getNumber());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 return;
387 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000388 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000389 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 // FIXME: PIC relocation model
391 return;
392 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000393 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000394 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000396 case MachineOperand::MO_BlockAddress:
Chris Lattnerce409842010-01-17 21:43:43 +0000397 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilsone8cbca92009-11-04 21:31:18 +0000398 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000399 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 // Computing the address of an external symbol, not calling it.
Chris Lattnerc3009922010-01-16 02:09:06 +0000401 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000402 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerc3009922010-01-16 02:09:06 +0000403 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 }
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000405
Chris Lattnerc3009922010-01-16 02:09:06 +0000406 const MCSymbol *NLPSym =
407 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
408 MO.getSymbolName()+"$non_lazy_ptr");
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000409 const MCSymbol *&StubSym =
410 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
411 if (StubSym == 0)
412 StubSym = GetExternalSymbolSymbol(MO.getSymbolName());
413
Chris Lattnerce409842010-01-17 21:43:43 +0000414 O << *NLPSym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000416 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 case MachineOperand::MO_GlobalAddress: {
418 // Computing the address of a global symbol, not calling it.
419 GlobalValue *GV = MO.getGlobal();
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000420 MCSymbol *SymToPrint;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421
422 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000423 if (TM.getRelocationModel() != Reloc::Static &&
424 (GV->isDeclaration() || GV->isWeakForLinker())) {
425 if (!GV->hasHiddenVisibility()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000426 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000427 const MCSymbol *&StubSym =
428 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(SymToPrint);
429 if (StubSym == 0)
430 StubSym = GetGlobalValueSymbol(GV);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000431 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
432 GV->hasAvailableExternallyLinkage()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000433 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000434
435 const MCSymbol *&StubSym =
436 MMI->getObjFileInfo<MachineModuleInfoMachO>().
437 getHiddenGVStubEntry(SymToPrint);
438 if (StubSym == 0)
439 StubSym = GetGlobalValueSymbol(GV);
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000440 } else {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000441 SymToPrint = GetGlobalValueSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000443 } else {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000444 SymToPrint = GetGlobalValueSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445 }
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000446
Chris Lattnerce409842010-01-17 21:43:43 +0000447 O << *SymToPrint;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000448
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000449 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 return;
451 }
452
453 default:
454 O << "<unknown operand type: " << MO.getType() << ">";
455 return;
456 }
457}
458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459/// PrintAsmOperand - Print out an operand for an inline asm expression.
460///
461bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000462 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 const char *ExtraCode) {
464 // Does this asm operand have a single letter operand modifier?
465 if (ExtraCode && ExtraCode[0]) {
466 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000467
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 switch (ExtraCode[0]) {
469 default: return true; // Unknown modifier.
470 case 'c': // Don't print "$" before a global var name or constant.
471 // PPC never has a prefix.
472 printOperand(MI, OpNo);
473 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000474 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000476 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000478 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 return true;
480 ++OpNo; // Return the high-part.
481 break;
482 case 'I':
483 // Write 'i' if an integer constant, otherwise nothing. Used to print
484 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000485 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 O << "i";
487 return false;
488 }
489 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000490
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000491 printOperand(MI, OpNo);
492 return false;
493}
494
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000495// At the moment, all inline asm memory operands are a single register.
496// In any case, the output of this routine should always be just one
497// assembler operand.
498
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000500 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 const char *ExtraCode) {
502 if (ExtraCode && ExtraCode[0])
503 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000504 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000505 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000506 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000507 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 return false;
509}
510
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000511void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 const char *Modifier) {
513 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
514 unsigned Code = MI->getOperand(OpNo).getImm();
515 if (!strcmp(Modifier, "cc")) {
516 switch ((PPC::Predicate)Code) {
517 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
518 case PPC::PRED_LT: O << "lt"; return;
519 case PPC::PRED_LE: O << "le"; return;
520 case PPC::PRED_EQ: O << "eq"; return;
521 case PPC::PRED_GE: O << "ge"; return;
522 case PPC::PRED_GT: O << "gt"; return;
523 case PPC::PRED_NE: O << "ne"; return;
524 case PPC::PRED_UN: O << "un"; return;
525 case PPC::PRED_NU: O << "nu"; return;
526 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000527
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 } else {
529 assert(!strcmp(Modifier, "reg") &&
530 "Need to specify 'cc' or 'reg' as predicate op modifier!");
531 // Don't print the register for 'always'.
532 if (Code == PPC::PRED_ALWAYS) return;
533 printOperand(MI, OpNo+1);
534 }
535}
536
537
538/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to
539/// the current output stream.
540///
541void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
542 ++EmittedInsts;
Chris Lattnere34788c2009-09-09 20:34:59 +0000543
Devang Patel5450fc12009-10-06 02:19:11 +0000544 processDebugLoc(MI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545
546 // Check for slwi/srwi mnemonics.
Dale Johannesen760acc02010-01-06 02:20:18 +0000547 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000549 unsigned char SH = MI->getOperand(2).getImm();
550 unsigned char MB = MI->getOperand(3).getImm();
551 unsigned char ME = MI->getOperand(4).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000553 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 }
555 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000556 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 SH = 32-SH;
558 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000559 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 printOperand(MI, 0);
561 O << ", ";
562 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000563 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 }
565 } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
566 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000567 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000568 O << "\tmr ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 printOperand(MI, 0);
570 O << ", ";
571 printOperand(MI, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572 }
573 } else if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000574 unsigned char SH = MI->getOperand(2).getImm();
575 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000576 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
577 if (63-SH == ME) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000578 useSubstituteMnemonic = true;
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000579 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 printOperand(MI, 0);
581 O << ", ";
582 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000583 O << ", " << (unsigned int)SH;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 }
585 }
586
Dale Johannesen760acc02010-01-06 02:20:18 +0000587 if (!useSubstituteMnemonic)
588 printInstruction(MI);
589
David Greeneca9b04b2009-11-13 21:34:57 +0000590 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000591 EmitComments(*MI);
592 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000593
594 processDebugLoc(MI, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595}
596
597/// runOnMachineFunction - This uses the printMachineInstruction()
598/// method to print assembly for each instruction.
599///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000600bool PPCLinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000601 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602
603 SetupMachineFunction(MF);
604 O << "\n\n";
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000605
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 // Print out constants referenced by the function
607 EmitConstantPool(MF.getConstantPool());
608
609 // Print out labels for the function.
610 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000611 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000612
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000614 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000615 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 case Function::InternalLinkage: // Symbols default to internal.
617 break;
618 case Function::ExternalLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000619 O << "\t.global\t" << *CurrentFnSym << '\n' << "\t.type\t";
620 O << *CurrentFnSym << ", @function\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621 break;
Dale Johannesenda551282009-08-24 01:03:42 +0000622 case Function::LinkerPrivateLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000623 case Function::WeakAnyLinkage:
624 case Function::WeakODRLinkage:
625 case Function::LinkOnceAnyLinkage:
626 case Function::LinkOnceODRLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000627 O << "\t.global\t" << *CurrentFnSym << '\n';
628 O << "\t.weak\t" << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 break;
630 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000631
Chris Lattner651adf32010-01-16 00:21:18 +0000632 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000633
Bill Wendling25a8ae32009-06-30 22:38:32 +0000634 EmitAlignment(MF.getAlignment(), F);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000635
636 if (Subtarget.isPPC64()) {
637 // Emit an official procedure descriptor.
Chris Lattner651adf32010-01-16 00:21:18 +0000638 // FIXME 64-bit SVR4: Use MCSection here!
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000639 O << "\t.section\t\".opd\",\"aw\"\n";
640 O << "\t.align 3\n";
Chris Lattnerce409842010-01-17 21:43:43 +0000641 O << *CurrentFnSym << ":\n";
642 O << "\t.quad .L." << *CurrentFnSym << ",.TOC.@tocbase\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000643 O << "\t.previous\n";
Chris Lattnerce409842010-01-17 21:43:43 +0000644 O << ".L." << *CurrentFnSym << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000645 } else {
Chris Lattnerce409842010-01-17 21:43:43 +0000646 O << *CurrentFnSym << ":\n";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000647 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648
649 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000650 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651
652 // Print out code for the function.
653 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
654 I != E; ++I) {
655 // Print a label for the basic block.
656 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000657 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 }
659 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
660 II != E; ++II) {
661 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 printMachineInstruction(II);
663 }
664 }
665
Chris Lattnerce409842010-01-17 21:43:43 +0000666 O << "\t.size\t" << *CurrentFnSym << ",.-" << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667
Chris Lattner73266f92009-08-19 05:49:37 +0000668 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dale Johannesendd9e1c92008-12-03 19:33:10 +0000669
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000671 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000672
Bill Wendling6fb58e12009-11-09 21:20:14 +0000673 // Print out jump tables referenced by the function.
Chris Lattner1f8c8922010-01-25 22:41:33 +0000674 EmitJumpTableInfo(MF);
Bill Wendling6fb58e12009-11-09 21:20:14 +0000675
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 // We didn't modify anything.
677 return false;
678}
679
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000680bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
681 const TargetData *TD = TM.getTargetData();
682
683 bool isPPC64 = TD->getPointerSizeInBits() == 64;
684
685 if (isPPC64 && !TOC.empty()) {
686 // FIXME 64-bit SVR4: Use MCSection here?
687 O << "\t.section\t\".toc\",\"aw\"\n";
688
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000689 // FIXME: This is nondeterminstic!
Chris Lattnerc3009922010-01-16 02:09:06 +0000690 for (DenseMap<const MCSymbol*, const MCSymbol*>::iterator I = TOC.begin(),
691 E = TOC.end(); I != E; ++I) {
Chris Lattnerce409842010-01-17 21:43:43 +0000692 O << *I->second << ":\n";
693 O << "\t.tc " << *I->first << "[TC]," << *I->first << '\n';
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000694 }
695 }
696
697 return AsmPrinter::doFinalization(M);
698}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700/// runOnMachineFunction - This uses the printMachineInstruction()
701/// method to print assembly for each instruction.
702///
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000703bool PPCDarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000704 this->MF = &MF;
705
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000706 SetupMachineFunction(MF);
707 O << "\n\n";
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000708
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709 // Print out constants referenced by the function
710 EmitConstantPool(MF.getConstantPool());
711
712 // Print out labels for the function.
713 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000714 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000715
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000717 default: llvm_unreachable("Unknown linkage type!");
evancheng47ae8142009-01-25 06:32:01 +0000718 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719 case Function::InternalLinkage: // Symbols default to internal.
720 break;
721 case Function::ExternalLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000722 O << "\t.globl\t" << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 break;
Duncan Sands19d161f2009-03-07 15:45:40 +0000724 case Function::WeakAnyLinkage:
725 case Function::WeakODRLinkage:
726 case Function::LinkOnceAnyLinkage:
727 case Function::LinkOnceODRLinkage:
Dale Johannesenda551282009-08-24 01:03:42 +0000728 case Function::LinkerPrivateLinkage:
Chris Lattnerce409842010-01-17 21:43:43 +0000729 O << "\t.globl\t" << *CurrentFnSym << '\n';
730 O << "\t.weak_definition\t" << *CurrentFnSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 break;
732 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000733
Chris Lattner651adf32010-01-16 00:21:18 +0000734 printVisibility(CurrentFnSym, F->getVisibility());
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000735
Bill Wendling25a8ae32009-06-30 22:38:32 +0000736 EmitAlignment(MF.getAlignment(), F);
Chris Lattnerce409842010-01-17 21:43:43 +0000737 O << *CurrentFnSym << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738
739 // Emit pre-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000740 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741
Bill Wendling36ccaea2008-01-26 06:51:24 +0000742 // If the function is empty, then we need to emit *something*. Otherwise, the
743 // function's label might be associated with something that it wasn't meant to
744 // be associated with. We emit a noop in this situation.
745 MachineFunction::iterator I = MF.begin();
746
Bill Wendlingb5880a72008-01-26 09:03:52 +0000747 if (++I == MF.end() && MF.front().empty())
748 O << "\tnop\n";
Bill Wendling36ccaea2008-01-26 06:51:24 +0000749
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 // Print out code for the function.
751 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
752 I != E; ++I) {
753 // Print a label for the basic block.
754 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000755 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 }
Bill Wendling36ccaea2008-01-26 06:51:24 +0000757 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
758 II != IE; ++II) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760 printMachineInstruction(II);
761 }
762 }
763
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 // Emit post-function debug information.
Devang Patelaa1e8432009-01-08 23:40:34 +0000765 DW->EndFunction(&MF);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000766
Bill Wendling5019fe02009-11-09 21:45:26 +0000767 // Print out jump tables referenced by the function.
Chris Lattner1f8c8922010-01-25 22:41:33 +0000768 EmitJumpTableInfo(MF);
Bill Wendling5019fe02009-11-09 21:45:26 +0000769
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 // We didn't modify anything.
771 return false;
772}
773
774
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000775void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000776 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000777 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 "ppc",
779 "ppc601",
780 "ppc602",
781 "ppc603",
782 "ppc7400",
783 "ppc750",
784 "ppc970",
785 "ppc64"
786 };
787
788 unsigned Directive = Subtarget.getDarwinDirective();
789 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
790 Directive = PPC::DIR_970;
791 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
792 Directive = PPC::DIR_7400;
793 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
794 Directive = PPC::DIR_64;
795 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000796 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000797
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 // Prime text sections so they are adjacent. This reduces the likelihood a
799 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000800 TargetLoweringObjectFileMachO &TLOFMacho =
801 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000802 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000804 OutStreamer.SwitchSection(
805 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
806 MCSectionMachO::S_SYMBOL_STUBS |
807 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
808 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000810 OutStreamer.SwitchSection(
811 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
812 MCSectionMachO::S_SYMBOL_STUBS |
813 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
814 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000815 }
Chris Lattner73266f92009-08-19 05:49:37 +0000816 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817}
818
Chris Lattnerd5a83252010-01-20 21:36:48 +0000819static const MCSymbol *GetLazyPtr(const MCSymbol *Sym, MCContext &Ctx) {
820 // Remove $stub suffix, add $lazy_ptr.
821 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
822 TmpStr += "$lazy_ptr";
823 return Ctx.GetOrCreateSymbol(TmpStr.str());
824}
825
826static const MCSymbol *GetAnonSym(const MCSymbol *Sym, MCContext &Ctx) {
827 // Add $tmp suffix to $stub, yielding $stub$tmp.
828 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
829 TmpStr += "$tmp";
830 return Ctx.GetOrCreateSymbol(TmpStr.str());
831}
832
833void PPCDarwinAsmPrinter::
834EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chris Lattner5b187502010-01-20 21:19:44 +0000835 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
836
Chris Lattnerf4815552009-08-03 22:52:21 +0000837 TargetLoweringObjectFileMachO &TLOFMacho =
838 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner5b187502010-01-20 21:19:44 +0000839
840 // .lazy_symbol_pointer
841 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattner72a676a2009-08-10 01:39:42 +0000842
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843 // Output stubs for dynamically-linked functions
Chris Lattner5b187502010-01-20 21:19:44 +0000844 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000845 const MCSection *StubSection =
Chris Lattner5b187502010-01-20 21:19:44 +0000846 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
847 MCSectionMachO::S_SYMBOL_STUBS |
848 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
849 32, SectionKind::getText());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000850 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000851 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 EmitAlignment(4);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000853
854 const MCSymbol *Stub = Stubs[i].first;
855 const MCSymbol *RawSym = Stubs[i].second;
856 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
857 const MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
858
859 O << *Stub << ":\n";
860 O << "\t.indirect_symbol " << *RawSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 O << "\tmflr r0\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000862 O << "\tbcl 20,31," << *AnonSymbol << '\n';
863 O << *AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 O << "\tmflr r11\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000865 O << "\taddis r11,r11,ha16(" << *LazyPtr << '-' << *AnonSymbol
Chris Lattner5b187502010-01-20 21:19:44 +0000866 << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 O << "\tmtlr r0\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000868 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
869 << '-' << *AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 O << "\tmtctr r12\n";
871 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +0000872
Chris Lattner73266f92009-08-19 05:49:37 +0000873 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000874 O << *LazyPtr << ":\n";
875 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +0000876 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877 }
Chris Lattner5b187502010-01-20 21:19:44 +0000878 O << '\n';
879 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 }
Chris Lattner5b187502010-01-20 21:19:44 +0000881
882 const MCSection *StubSection =
883 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
884 MCSectionMachO::S_SYMBOL_STUBS |
885 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
886 16, SectionKind::getText());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000887 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
888 const MCSymbol *Stub = Stubs[i].first;
889 const MCSymbol *RawSym = Stubs[i].second;
890 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
891
Chris Lattner5b187502010-01-20 21:19:44 +0000892 OutStreamer.SwitchSection(StubSection);
893 EmitAlignment(4);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000894 O << *Stub << ":\n";
895 O << "\t.indirect_symbol " << *RawSym << '\n';
896 O << "\tlis r11,ha16(" << *LazyPtr << ")\n";
897 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
Chris Lattner5b187502010-01-20 21:19:44 +0000898 << ")(r11)\n";
899 O << "\tmtctr r12\n";
900 O << "\tbctr\n";
901 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000902 O << *LazyPtr << ":\n";
903 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattner5b187502010-01-20 21:19:44 +0000904 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
905 }
906
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000907 O << '\n';
Chris Lattner5b187502010-01-20 21:19:44 +0000908}
909
910
911bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
912 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
913
914 // Darwin/PPC always uses mach-o.
915 TargetLoweringObjectFileMachO &TLOFMacho =
916 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
917 MachineModuleInfoMachO &MMIMacho =
918 MMI->getObjFileInfo<MachineModuleInfoMachO>();
919
Chris Lattnerd5a83252010-01-20 21:36:48 +0000920 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
921 if (!Stubs.empty())
922 EmitFunctionStubs(Stubs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000924 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000925 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +0000926 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +0000927 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000928 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000929 E = Personalities.end(); I != E; ++I) {
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000930 if (*I) {
931 const MCSymbol *NLPSym =
Chris Lattnera6f2a102010-01-16 18:37:32 +0000932 GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000933 const MCSymbol *&StubSym = MMIMacho.getGVStubEntry(NLPSym);
934 StubSym = GetGlobalValueSymbol(*I);
935 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000936 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000937 }
938
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000939 // Output stubs for dynamically-linked functions.
Chris Lattnerd5a83252010-01-20 21:36:48 +0000940 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000941
Chris Lattnerf4815552009-08-03 22:52:21 +0000942 // Output macho stubs for external and common global variables.
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000943 if (!Stubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +0000944 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +0000945 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +0000946 EmitAlignment(isPPC64 ? 3 : 2);
947
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000948 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
949 O << *Stubs[i].first << ":\n";
950 O << "\t.indirect_symbol " << *Stubs[i].second << '\n';
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000951 O << (isPPC64 ? "\t.quad\t0\n" : "\t.long\t0\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000952 }
953 }
954
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000955 Stubs = MMIMacho.GetHiddenGVStubList();
956 if (!Stubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +0000957 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000958 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000959
960 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
961 O << *Stubs[i].first << ":\n";
962 O << (isPPC64 ? "\t.quad\t" : "\t.long\t") << *Stubs[i].second << '\n';
Evan Chenga65854f2008-12-05 01:06:39 +0000963 }
964 }
965
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 // Funny Darwin hack: This flag tells the linker that no global symbols
967 // contain code that falls through to other global symbols (e.g. the obvious
968 // implementation of multiple entry points). If this doesn't occur, the
969 // linker can safely perform dead code stripping. Since LLVM never generates
970 // code that does this, it is always safe to set.
Chris Lattner2d7c8142010-01-23 06:39:22 +0000971 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972
Dan Gohman4a558a32007-07-25 19:33:14 +0000973 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974}
975
976
977
978/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
979/// for a MachineFunction to the given output stream, in a format that the
980/// Darwin assembler can deal with.
981///
Daniel Dunbar4cb63652009-08-13 23:48:47 +0000982static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
983 TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +0000984 const MCAsmInfo *tai,
Daniel Dunbar4cb63652009-08-13 23:48:47 +0000985 bool verbose) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
987
Chris Lattnerae982212009-07-21 18:38:57 +0000988 if (Subtarget->isDarwin())
Daniel Dunbaref5abb42009-08-13 19:38:51 +0000989 return new PPCDarwinAsmPrinter(o, tm, tai, verbose);
990 return new PPCLinuxAsmPrinter(o, tm, tai, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000992
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000993// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000994extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000995 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000996 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
997}