blob: 40d6778b646e6d214259b47e06dac3e0ad0fa036 [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"
Anton Korobeynikov84d365c2010-02-15 22:37:53 +000034#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000035#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9bc87482010-01-13 19:00:57 +000036#include "llvm/MC/MCContext.h"
Bill Wendlingd4fba932010-03-11 23:39:44 +000037#include "llvm/MC/MCExpr.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000038#include "llvm/MC/MCSectionMachO.h"
Chris Lattner73266f92009-08-19 05:49:37 +000039#include "llvm/MC/MCStreamer.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattner31a54742010-01-16 21:57:06 +000041#include "llvm/Target/Mangler.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000042#include "llvm/Target/TargetRegisterInfo.h"
43#include "llvm/Target/TargetInstrInfo.h"
44#include "llvm/Target/TargetOptions.h"
45#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046#include "llvm/Support/MathExtras.h"
47#include "llvm/Support/CommandLine.h"
48#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000049#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000050#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051#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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000057 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +000058 protected:
Chris Lattnerc2cfe152010-01-20 21:16:14 +000059 DenseMap<const MCSymbol*, const MCSymbol*> TOC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 const PPCSubtarget &Subtarget;
Tilmann Scheller72cf2812009-08-15 11:54:46 +000061 uint64_t LabelID;
Bill Wendling4f405312009-02-24 08:30:20 +000062 public:
David Greene302008d2009-07-14 20:18:05 +000063 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner8d2970b2010-02-02 23:37:42 +000064 MCContext &Ctx, MCStreamer &Streamer,
65 const MCAsmInfo *T)
66 : AsmPrinter(O, TM, Ctx, Streamer, T),
Tilmann Scheller72cf2812009-08-15 11:54:46 +000067 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
69 virtual const char *getPassName() const {
70 return "PowerPC Assembly Printer";
71 }
72
73 PPCTargetMachine &getTM() {
74 return static_cast<PPCTargetMachine&>(TM);
75 }
76
77 unsigned enumRegToMachineReg(unsigned enumReg) {
78 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +000079 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 case PPC::CR0: return 0;
81 case PPC::CR1: return 1;
82 case PPC::CR2: return 2;
83 case PPC::CR3: return 3;
84 case PPC::CR4: return 4;
85 case PPC::CR5: return 5;
86 case PPC::CR6: return 6;
87 case PPC::CR7: return 7;
88 }
Edwin Törökbd448e32009-07-14 16:55:14 +000089 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 }
91
92 /// printInstruction - This method is automatically generated by tablegen
93 /// from the instruction set description. This method returns true if the
94 /// machine instruction was sufficiently described to print it, otherwise it
95 /// returns false.
Chris Lattnerddb259a2009-08-08 01:32:19 +000096 void printInstruction(const MachineInstr *MI);
Chris Lattner213703c2009-09-13 20:19:22 +000097 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +000098
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
Chris Lattner1d40b372010-01-28 01:28:58 +0000100 virtual void EmitInstruction(const MachineInstr *MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 void printOp(const MachineOperand &MO);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000102
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 /// stripRegisterPrefix - This method strips the character prefix from a
104 /// register name so that only the number is left. Used by for linux asm.
105 const char *stripRegisterPrefix(const char *RegName) {
106 switch (RegName[0]) {
107 case 'r':
108 case 'f':
109 case 'v': return RegName + 1;
110 case 'c': if (RegName[1] == 'r') return RegName + 2;
111 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000112
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 return RegName;
114 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 /// printRegister - Print register according to target requirements.
117 ///
118 void printRegister(const MachineOperand &MO, bool R0AsZero) {
119 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000120 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 // If we should use 0 for R0.
123 if (R0AsZero && RegNo == PPC::R0) {
124 O << "0";
125 return;
126 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000127
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000128 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 // Linux assembler (Others?) does not take register mnemonics.
130 // FIXME - What about special registers used in mfspr/mtspr?
131 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
132 O << RegName;
133 }
134
135 void printOperand(const MachineInstr *MI, unsigned OpNo) {
136 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000137 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 printRegister(MO, false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000139 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000140 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } else {
142 printOp(MO);
143 }
144 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
147 unsigned AsmVariant, const char *ExtraCode);
148 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
149 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000150
151
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000153 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 value = (value << (32-5)) >> (32-5);
155 O << (int)value;
156 }
157 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000158 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 assert(value <= 31 && "Invalid u5imm argument!");
160 O << (unsigned int)value;
161 }
162 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000163 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 assert(value <= 63 && "Invalid u6imm argument!");
165 O << (unsigned int)value;
166 }
167 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000168 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 }
170 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000171 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 }
173 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000174 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000175 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 } else {
177 O << "lo16(";
178 printOp(MI->getOperand(OpNo));
179 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000180 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 else
182 O << ')';
183 }
184 }
185 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
186 // Branches can take an immediate operand. This is used by the branch
187 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000188 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000189 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 } else {
191 printOp(MI->getOperand(OpNo));
192 }
193 }
194 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
195 const MachineOperand &MO = MI->getOperand(OpNo);
196 if (TM.getRelocationModel() != Reloc::Static) {
197 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
198 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000199 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 // Dynamically-resolved functions need a stub for the function.
Chris Lattnerd5a83252010-01-20 21:36:48 +0000201 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingce996512010-03-10 22:34:10 +0000202 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd5a83252010-01-20 21:36:48 +0000203 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingce996512010-03-10 22:34:10 +0000204 if (StubSym.getPointer() == 0)
205 StubSym = MachineModuleInfoImpl::
206 StubValueTy(GetGlobalValueSymbol(GV),!GV->hasInternalLinkage());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000207 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
Chris Lattner77653062010-02-03 06:18:30 +0000216 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Bill Wendlingce996512010-03-10 22:34:10 +0000217 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd5a83252010-01-20 21:36:48 +0000218 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingce996512010-03-10 22:34:10 +0000219 if (StubSym.getPointer() == 0)
220 StubSym = MachineModuleInfoImpl::
221 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000222 O << *Sym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 return;
224 }
225 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 printOp(MI->getOperand(OpNo));
228 }
229 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000230 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 }
232 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng477013c2007-10-14 05:57:21 +0000233 O << "\"L" << getFunctionNumber() << "$pb\"\n";
234 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 }
236 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000237 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 printS16ImmOperand(MI, OpNo);
239 } else {
240 if (Subtarget.isDarwin()) O << "ha16(";
241 printOp(MI->getOperand(OpNo));
242 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000243 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 if (Subtarget.isDarwin())
245 O << ')';
246 else
247 O << "@ha";
248 }
249 }
250 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000251 if (MI->getOperand(OpNo).isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 printS16ImmOperand(MI, OpNo);
253 } else {
254 if (Subtarget.isDarwin()) O << "lo16(";
255 printOp(MI->getOperand(OpNo));
256 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000257 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 if (Subtarget.isDarwin())
259 O << ')';
260 else
261 O << "@l";
262 }
263 }
264 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
265 unsigned CCReg = MI->getOperand(OpNo).getReg();
266 unsigned RegNo = enumRegToMachineReg(CCReg);
267 O << (0x80 >> RegNo);
268 }
269 // The new addressing mode printers.
270 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
271 printSymbolLo(MI, OpNo);
272 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000273 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 MI->getOperand(OpNo+1).getReg() == PPC::R0)
275 O << "0";
276 else
277 printOperand(MI, OpNo+1);
278 O << ')';
279 }
280 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000281 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000283 else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 printSymbolLo(MI, OpNo);
285 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000286 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 MI->getOperand(OpNo+1).getReg() == PPC::R0)
288 O << "0";
289 else
290 printOperand(MI, OpNo+1);
291 O << ')';
292 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000293
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
295 // When used as the base register, r0 reads constant zero rather than
296 // the value contained in the register. For this reason, the darwin
297 // assembler requires that we print r0 as 0 (no r) when used as the base.
298 const MachineOperand &MO = MI->getOperand(OpNo);
299 printRegister(MO, true);
300 O << ", ";
301 printOperand(MI, OpNo+1);
302 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000303
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000304 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
305 const MachineOperand &MO = MI->getOperand(OpNo);
306
307 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
308
Chris Lattnerc3009922010-01-16 02:09:06 +0000309 const MCSymbol *Sym = GetGlobalValueSymbol(MO.getGlobal());
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000310
311 // Map symbol -> label of TOC entry.
Chris Lattnerc3009922010-01-16 02:09:06 +0000312 const MCSymbol *&TOCEntry = TOC[Sym];
313 if (TOCEntry == 0)
314 TOCEntry = OutContext.
Chris Lattner72121e42010-03-10 02:25:11 +0000315 GetOrCreateTemporarySymbol(StringRef(MAI->getPrivateGlobalPrefix()) +
316 "C" + Twine(LabelID++));
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000317
Chris Lattnerce409842010-01-17 21:43:43 +0000318 O << *TOCEntry << "@toc";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000319 }
320
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000321 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 const char *Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 };
324
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000325 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000326 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000327 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000328 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner8d2970b2010-02-02 23:37:42 +0000329 MCContext &Ctx, MCStreamer &Streamer,
330 const MCAsmInfo *T)
331 : PPCAsmPrinter(O, TM, Ctx, Streamer, T) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332
333 virtual const char *getPassName() const {
334 return "Linux PPC Assembly Printer";
335 }
336
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000337 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000338
Chris Lattneraec7a8a2010-01-27 07:21:55 +0000339 virtual void EmitFunctionEntryLabel();
340
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 void getAnalysisUsage(AnalysisUsage &AU) const {
342 AU.setPreservesAll();
343 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000344 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 PPCAsmPrinter::getAnalysisUsage(AU);
346 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 };
348
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000349 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
350 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000351 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000352 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000353 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000354 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner8d2970b2010-02-02 23:37:42 +0000355 MCContext &Ctx, MCStreamer &Streamer,
356 const MCAsmInfo *T)
357 : PPCAsmPrinter(O, TM, Ctx, Streamer, T), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358
359 virtual const char *getPassName() const {
360 return "Darwin PPC Assembly Printer";
361 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000362
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000364 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000365
Chris Lattnerd5a83252010-01-20 21:36:48 +0000366 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Chris Lattner5b187502010-01-20 21:19:44 +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 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 };
375} // end of anonymous namespace
376
377// Include the auto-generated portion of the assembly writer
378#include "PPCGenAsmWriter.inc"
379
380void PPCAsmPrinter::printOp(const MachineOperand &MO) {
381 switch (MO.getType()) {
382 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000383 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384
385 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner84d5ca92010-01-26 04:55:51 +0000386 O << *MO.getMBB()->getSymbol(OutContext);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 return;
388 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000389 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000390 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 // FIXME: PIC relocation model
392 return;
393 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000394 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000395 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000397 case MachineOperand::MO_BlockAddress:
Chris Lattnerce409842010-01-17 21:43:43 +0000398 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilsone8cbca92009-11-04 21:31:18 +0000399 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000400 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 // Computing the address of an external symbol, not calling it.
Chris Lattnerc3009922010-01-16 02:09:06 +0000402 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000403 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerc3009922010-01-16 02:09:06 +0000404 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 }
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000406
Chris Lattner77653062010-02-03 06:18:30 +0000407 MCSymbol *NLPSym =
Chris Lattnerc3009922010-01-16 02:09:06 +0000408 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
409 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000410 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000411 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlingce996512010-03-10 22:34:10 +0000412 if (StubSym.getPointer() == 0)
413 StubSym = MachineModuleInfoImpl::
414 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000415
Chris Lattnerce409842010-01-17 21:43:43 +0000416 O << *NLPSym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000418 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 case MachineOperand::MO_GlobalAddress: {
420 // Computing the address of a global symbol, not calling it.
421 GlobalValue *GV = MO.getGlobal();
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000422 MCSymbol *SymToPrint;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423
424 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000425 if (TM.getRelocationModel() != Reloc::Static &&
426 (GV->isDeclaration() || GV->isWeakForLinker())) {
427 if (!GV->hasHiddenVisibility()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000428 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000429 MachineModuleInfoImpl::StubValueTy &StubSym =
430 MMI->getObjFileInfo<MachineModuleInfoMachO>()
431 .getGVStubEntry(SymToPrint);
432 if (StubSym.getPointer() == 0)
433 StubSym = MachineModuleInfoImpl::
434 StubValueTy(GetGlobalValueSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000435 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
436 GV->hasAvailableExternallyLinkage()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000437 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000438
Bill Wendlingce996512010-03-10 22:34:10 +0000439 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000440 MMI->getObjFileInfo<MachineModuleInfoMachO>().
441 getHiddenGVStubEntry(SymToPrint);
Bill Wendlingce996512010-03-10 22:34:10 +0000442 if (StubSym.getPointer() == 0)
443 StubSym = MachineModuleInfoImpl::
444 StubValueTy(GetGlobalValueSymbol(GV),
445 !GV->hasInternalLinkage());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000446 } else {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000447 SymToPrint = GetGlobalValueSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000449 } else {
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000450 SymToPrint = GetGlobalValueSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 }
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000452
Chris Lattnerce409842010-01-17 21:43:43 +0000453 O << *SymToPrint;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000454
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000455 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 return;
457 }
458
459 default:
460 O << "<unknown operand type: " << MO.getType() << ">";
461 return;
462 }
463}
464
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465/// PrintAsmOperand - Print out an operand for an inline asm expression.
466///
467bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000468 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 const char *ExtraCode) {
470 // Does this asm operand have a single letter operand modifier?
471 if (ExtraCode && ExtraCode[0]) {
472 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000473
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 switch (ExtraCode[0]) {
475 default: return true; // Unknown modifier.
476 case 'c': // Don't print "$" before a global var name or constant.
477 // PPC never has a prefix.
478 printOperand(MI, OpNo);
479 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000480 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000482 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000484 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 return true;
486 ++OpNo; // Return the high-part.
487 break;
488 case 'I':
489 // Write 'i' if an integer constant, otherwise nothing. Used to print
490 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000491 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 O << "i";
493 return false;
494 }
495 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000496
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 printOperand(MI, OpNo);
498 return false;
499}
500
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000501// At the moment, all inline asm memory operands are a single register.
502// In any case, the output of this routine should always be just one
503// assembler operand.
504
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000506 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 const char *ExtraCode) {
508 if (ExtraCode && ExtraCode[0])
509 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000510 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000511 O << "0(";
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000512 printOperand(MI, OpNo);
Dale Johannesen5e699502009-08-26 18:10:32 +0000513 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 return false;
515}
516
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000517void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000518 const char *Modifier) {
519 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
520 unsigned Code = MI->getOperand(OpNo).getImm();
521 if (!strcmp(Modifier, "cc")) {
522 switch ((PPC::Predicate)Code) {
523 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
524 case PPC::PRED_LT: O << "lt"; return;
525 case PPC::PRED_LE: O << "le"; return;
526 case PPC::PRED_EQ: O << "eq"; return;
527 case PPC::PRED_GE: O << "ge"; return;
528 case PPC::PRED_GT: O << "gt"; return;
529 case PPC::PRED_NE: O << "ne"; return;
530 case PPC::PRED_UN: O << "un"; return;
531 case PPC::PRED_NU: O << "nu"; return;
532 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000533
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 } else {
535 assert(!strcmp(Modifier, "reg") &&
536 "Need to specify 'cc' or 'reg' as predicate op modifier!");
537 // Don't print the register for 'always'.
538 if (Code == PPC::PRED_ALWAYS) return;
539 printOperand(MI, OpNo+1);
540 }
541}
542
543
Chris Lattner1d40b372010-01-28 01:28:58 +0000544/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545/// the current output stream.
546///
Chris Lattner1d40b372010-01-28 01:28:58 +0000547void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 // Check for slwi/srwi mnemonics.
549 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000550 unsigned char SH = MI->getOperand(2).getImm();
551 unsigned char MB = MI->getOperand(3).getImm();
552 unsigned char ME = MI->getOperand(4).getImm();
Chris Lattner1d40b372010-01-28 01:28:58 +0000553 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000555 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 }
557 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000558 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 SH = 32-SH;
560 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000561 if (useSubstituteMnemonic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 printOperand(MI, 0);
563 O << ", ";
564 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000565 O << ", " << (unsigned int)SH;
Chris Lattner726d3972010-02-10 00:36:00 +0000566 OutStreamer.AddBlankLine();
Chris Lattner1d40b372010-01-28 01:28:58 +0000567 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 }
Chris Lattner1d40b372010-01-28 01:28:58 +0000569 }
570
571 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
572 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
573 O << "\tmr ";
574 printOperand(MI, 0);
575 O << ", ";
576 printOperand(MI, 1);
Chris Lattner726d3972010-02-10 00:36:00 +0000577 OutStreamer.AddBlankLine();
Chris Lattner1d40b372010-01-28 01:28:58 +0000578 return;
579 }
580
581 if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000582 unsigned char SH = MI->getOperand(2).getImm();
583 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
585 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000586 O << "\tsldi ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 printOperand(MI, 0);
588 O << ", ";
589 printOperand(MI, 1);
Dale Johannesen760acc02010-01-06 02:20:18 +0000590 O << ", " << (unsigned int)SH;
Chris Lattner726d3972010-02-10 00:36:00 +0000591 OutStreamer.AddBlankLine();
Chris Lattner1d40b372010-01-28 01:28:58 +0000592 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 }
594 }
595
Chris Lattner1d40b372010-01-28 01:28:58 +0000596 printInstruction(MI);
Chris Lattner726d3972010-02-10 00:36:00 +0000597 OutStreamer.AddBlankLine();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598}
599
Chris Lattneraec7a8a2010-01-27 07:21:55 +0000600void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
601 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
602 return AsmPrinter::EmitFunctionEntryLabel();
603
604 // Emit an official procedure descriptor.
605 // FIXME 64-bit SVR4: Use MCSection here!
606 O << "\t.section\t\".opd\",\"aw\"\n";
607 O << "\t.align 3\n";
608 OutStreamer.EmitLabel(CurrentFnSym);
609 O << "\t.quad .L." << *CurrentFnSym << ",.TOC.@tocbase\n";
610 O << "\t.previous\n";
611 O << ".L." << *CurrentFnSym << ":\n";
612}
613
614
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000615bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
616 const TargetData *TD = TM.getTargetData();
617
618 bool isPPC64 = TD->getPointerSizeInBits() == 64;
619
620 if (isPPC64 && !TOC.empty()) {
621 // FIXME 64-bit SVR4: Use MCSection here?
622 O << "\t.section\t\".toc\",\"aw\"\n";
623
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000624 // FIXME: This is nondeterminstic!
Chris Lattnerc3009922010-01-16 02:09:06 +0000625 for (DenseMap<const MCSymbol*, const MCSymbol*>::iterator I = TOC.begin(),
626 E = TOC.end(); I != E; ++I) {
Chris Lattnerce409842010-01-17 21:43:43 +0000627 O << *I->second << ":\n";
628 O << "\t.tc " << *I->first << "[TC]," << *I->first << '\n';
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000629 }
630 }
631
632 return AsmPrinter::doFinalization(M);
633}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000635void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000636 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000637 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 "ppc",
639 "ppc601",
640 "ppc602",
641 "ppc603",
642 "ppc7400",
643 "ppc750",
644 "ppc970",
645 "ppc64"
646 };
647
648 unsigned Directive = Subtarget.getDarwinDirective();
649 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
650 Directive = PPC::DIR_970;
651 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
652 Directive = PPC::DIR_7400;
653 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
654 Directive = PPC::DIR_64;
655 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000656 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000657
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658 // Prime text sections so they are adjacent. This reduces the likelihood a
659 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000660 TargetLoweringObjectFileMachO &TLOFMacho =
661 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000662 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000664 OutStreamer.SwitchSection(
665 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
666 MCSectionMachO::S_SYMBOL_STUBS |
667 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
668 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000670 OutStreamer.SwitchSection(
671 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
672 MCSectionMachO::S_SYMBOL_STUBS |
673 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
674 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675 }
Chris Lattner73266f92009-08-19 05:49:37 +0000676 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677}
678
Chris Lattnerd5a83252010-01-20 21:36:48 +0000679static const MCSymbol *GetLazyPtr(const MCSymbol *Sym, MCContext &Ctx) {
680 // Remove $stub suffix, add $lazy_ptr.
681 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
682 TmpStr += "$lazy_ptr";
Chris Lattner72121e42010-03-10 02:25:11 +0000683 return Ctx.GetOrCreateTemporarySymbol(TmpStr.str());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000684}
685
686static const MCSymbol *GetAnonSym(const MCSymbol *Sym, MCContext &Ctx) {
687 // Add $tmp suffix to $stub, yielding $stub$tmp.
688 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
689 TmpStr += "$tmp";
Chris Lattner72121e42010-03-10 02:25:11 +0000690 return Ctx.GetOrCreateTemporarySymbol(TmpStr.str());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000691}
692
693void PPCDarwinAsmPrinter::
694EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chris Lattner5b187502010-01-20 21:19:44 +0000695 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
696
Chris Lattnerf4815552009-08-03 22:52:21 +0000697 TargetLoweringObjectFileMachO &TLOFMacho =
698 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner5b187502010-01-20 21:19:44 +0000699
700 // .lazy_symbol_pointer
701 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattner72a676a2009-08-10 01:39:42 +0000702
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000703 // Output stubs for dynamically-linked functions
Chris Lattner5b187502010-01-20 21:19:44 +0000704 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000705 const MCSection *StubSection =
Chris Lattner5b187502010-01-20 21:19:44 +0000706 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
707 MCSectionMachO::S_SYMBOL_STUBS |
708 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
709 32, SectionKind::getText());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000710 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000711 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 EmitAlignment(4);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000713
714 const MCSymbol *Stub = Stubs[i].first;
Bill Wendlingce996512010-03-10 22:34:10 +0000715 const MCSymbol *RawSym = Stubs[i].second.getPointer();
Chris Lattnerd5a83252010-01-20 21:36:48 +0000716 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
717 const MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
718
719 O << *Stub << ":\n";
720 O << "\t.indirect_symbol " << *RawSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 O << "\tmflr r0\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000722 O << "\tbcl 20,31," << *AnonSymbol << '\n';
723 O << *AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 O << "\tmflr r11\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000725 O << "\taddis r11,r11,ha16(" << *LazyPtr << '-' << *AnonSymbol
Chris Lattner5b187502010-01-20 21:19:44 +0000726 << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 O << "\tmtlr r0\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000728 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
729 << '-' << *AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 O << "\tmtctr r12\n";
731 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +0000732
Chris Lattner73266f92009-08-19 05:49:37 +0000733 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000734 O << *LazyPtr << ":\n";
735 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +0000736 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 }
Chris Lattner5b187502010-01-20 21:19:44 +0000738 O << '\n';
739 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 }
Chris Lattner5b187502010-01-20 21:19:44 +0000741
742 const MCSection *StubSection =
743 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
744 MCSectionMachO::S_SYMBOL_STUBS |
745 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
746 16, SectionKind::getText());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000747 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
748 const MCSymbol *Stub = Stubs[i].first;
Bill Wendlingce996512010-03-10 22:34:10 +0000749 const MCSymbol *RawSym = Stubs[i].second.getPointer();
Chris Lattnerd5a83252010-01-20 21:36:48 +0000750 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
751
Chris Lattner5b187502010-01-20 21:19:44 +0000752 OutStreamer.SwitchSection(StubSection);
753 EmitAlignment(4);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000754 O << *Stub << ":\n";
755 O << "\t.indirect_symbol " << *RawSym << '\n';
756 O << "\tlis r11,ha16(" << *LazyPtr << ")\n";
757 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
Chris Lattner5b187502010-01-20 21:19:44 +0000758 << ")(r11)\n";
759 O << "\tmtctr r12\n";
760 O << "\tbctr\n";
761 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000762 O << *LazyPtr << ":\n";
763 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattner5b187502010-01-20 21:19:44 +0000764 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
765 }
766
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000767 O << '\n';
Chris Lattner5b187502010-01-20 21:19:44 +0000768}
769
770
771bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
772 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
773
774 // Darwin/PPC always uses mach-o.
775 TargetLoweringObjectFileMachO &TLOFMacho =
776 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
777 MachineModuleInfoMachO &MMIMacho =
778 MMI->getObjFileInfo<MachineModuleInfoMachO>();
779
Chris Lattnerd5a83252010-01-20 21:36:48 +0000780 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
781 if (!Stubs.empty())
782 EmitFunctionStubs(Stubs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000784 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000785 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +0000786 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +0000787 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000788 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000789 E = Personalities.end(); I != E; ++I) {
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000790 if (*I) {
Chris Lattner77653062010-02-03 06:18:30 +0000791 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000792 MachineModuleInfoImpl::StubValueTy &StubSym =
793 MMIMacho.getGVStubEntry(NLPSym);
794 StubSym = MachineModuleInfoImpl::
795 StubValueTy(GetGlobalValueSymbol(*I), true);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000796 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000797 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000798 }
799
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000800 // Output stubs for dynamically-linked functions.
Chris Lattnerd5a83252010-01-20 21:36:48 +0000801 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000802
Chris Lattnerf4815552009-08-03 22:52:21 +0000803 // Output macho stubs for external and common global variables.
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000804 if (!Stubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +0000805 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +0000806 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +0000807 EmitAlignment(isPPC64 ? 3 : 2);
808
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000809 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingd4fba932010-03-11 23:39:44 +0000810 // L_foo$stub:
811 OutStreamer.EmitLabel(Stubs[i].first);
812 // .indirect_symbol _foo
813 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
814 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendling87913bf2010-03-12 02:00:43 +0000815
816 if (MCSym.getInt())
817 // External to current translation unit.
818 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
819 else
820 // Internal to current translation unit.
821 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
822 OutContext),
823 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 }
Bill Wendlingd4fba932010-03-11 23:39:44 +0000825
826 Stubs.clear();
827 OutStreamer.AddBlankLine();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 }
829
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000830 Stubs = MMIMacho.GetHiddenGVStubList();
831 if (!Stubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +0000832 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000833 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000834
835 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingd4fba932010-03-11 23:39:44 +0000836 // L_foo$stub:
837 OutStreamer.EmitLabel(Stubs[i].first);
838 // .long _foo
839 OutStreamer.EmitValue(MCSymbolRefExpr::
840 Create(Stubs[i].second.getPointer(),
841 OutContext),
842 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Evan Chenga65854f2008-12-05 01:06:39 +0000843 }
Bill Wendlingd4fba932010-03-11 23:39:44 +0000844
845 Stubs.clear();
846 OutStreamer.AddBlankLine();
Evan Chenga65854f2008-12-05 01:06:39 +0000847 }
848
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 // Funny Darwin hack: This flag tells the linker that no global symbols
850 // contain code that falls through to other global symbols (e.g. the obvious
851 // implementation of multiple entry points). If this doesn't occur, the
852 // linker can safely perform dead code stripping. Since LLVM never generates
853 // code that does this, it is always safe to set.
Chris Lattner2d7c8142010-01-23 06:39:22 +0000854 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855
Dan Gohman4a558a32007-07-25 19:33:14 +0000856 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857}
858
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
860/// for a MachineFunction to the given output stream, in a format that the
861/// Darwin assembler can deal with.
862///
Daniel Dunbar4cb63652009-08-13 23:48:47 +0000863static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
864 TargetMachine &tm,
Chris Lattner8d2970b2010-02-02 23:37:42 +0000865 MCContext &Ctx, MCStreamer &Streamer,
866 const MCAsmInfo *tai) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
868
Chris Lattnerae982212009-07-21 18:38:57 +0000869 if (Subtarget->isDarwin())
Chris Lattner8d2970b2010-02-02 23:37:42 +0000870 return new PPCDarwinAsmPrinter(o, tm, Ctx, Streamer, tai);
871 return new PPCLinuxAsmPrinter(o, tm, Ctx, Streamer, tai);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000873
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000874// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000875extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000876 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000877 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
878}