blob: 9457c387c2fde6783ad493847843b278355d6b80 [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 Lattner82d07b72010-04-04 07:05:53 +000059 DenseMap<MCSymbol*, 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 Lattnerf4853452010-03-13 20:55:24 +000064 MCStreamer &Streamer)
65 : AsmPrinter(O, TM, Streamer),
Tilmann Scheller72cf2812009-08-15 11:54:46 +000066 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
68 virtual const char *getPassName() const {
69 return "PowerPC Assembly Printer";
70 }
71
72 PPCTargetMachine &getTM() {
73 return static_cast<PPCTargetMachine&>(TM);
74 }
75
76 unsigned enumRegToMachineReg(unsigned enumReg) {
77 switch (enumReg) {
Edwin Törökbd448e32009-07-14 16:55:14 +000078 default: llvm_unreachable("Unhandled register!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 case PPC::CR0: return 0;
80 case PPC::CR1: return 1;
81 case PPC::CR2: return 2;
82 case PPC::CR3: return 3;
83 case PPC::CR4: return 4;
84 case PPC::CR5: return 5;
85 case PPC::CR6: return 6;
86 case PPC::CR7: return 7;
87 }
Edwin Törökbd448e32009-07-14 16:55:14 +000088 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 }
90
91 /// printInstruction - This method is automatically generated by tablegen
92 /// from the instruction set description. This method returns true if the
93 /// machine instruction was sufficiently described to print it, otherwise it
94 /// returns false.
Chris Lattner4e972532010-04-04 04:47:45 +000095 void printInstruction(const MachineInstr *MI, raw_ostream &O);
Chris Lattner213703c2009-09-13 20:19:22 +000096 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +000097
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098
Chris Lattner1d40b372010-01-28 01:28:58 +000099 virtual void EmitInstruction(const MachineInstr *MI);
Chris Lattner4e972532010-04-04 04:47:45 +0000100 void printOp(const MachineOperand &MO, raw_ostream &O);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000101
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 /// stripRegisterPrefix - This method strips the character prefix from a
103 /// register name so that only the number is left. Used by for linux asm.
104 const char *stripRegisterPrefix(const char *RegName) {
105 switch (RegName[0]) {
106 case 'r':
107 case 'f':
108 case 'v': return RegName + 1;
109 case 'c': if (RegName[1] == 'r') return RegName + 2;
110 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000111
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return RegName;
113 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000114
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 /// printRegister - Print register according to target requirements.
116 ///
Chris Lattner4e972532010-04-04 04:47:45 +0000117 void printRegister(const MachineOperand &MO, bool R0AsZero, raw_ostream &O){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 unsigned RegNo = MO.getReg();
Dan Gohman1e57df32008-02-10 18:45:23 +0000119 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 // If we should use 0 for R0.
122 if (R0AsZero && RegNo == PPC::R0) {
123 O << "0";
124 return;
125 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000126
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000127 const char *RegName = getRegisterName(RegNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 // Linux assembler (Others?) does not take register mnemonics.
129 // FIXME - What about special registers used in mfspr/mtspr?
130 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
131 O << RegName;
132 }
133
Chris Lattner4e972532010-04-04 04:47:45 +0000134 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000136 if (MO.isReg()) {
Chris Lattner4e972532010-04-04 04:47:45 +0000137 printRegister(MO, false, O);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000138 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000139 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 } else {
Chris Lattner4e972532010-04-04 04:47:45 +0000141 printOp(MO, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 }
143 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000144
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3eb5dc12010-04-04 05:29:35 +0000146 unsigned AsmVariant, const char *ExtraCode,
147 raw_ostream &O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3eb5dc12010-04-04 05:29:35 +0000149 unsigned AsmVariant, const char *ExtraCode,
150 raw_ostream &O);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000151
152
Chris Lattner4e972532010-04-04 04:47:45 +0000153 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo,
154 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000155 char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 value = (value << (32-5)) >> (32-5);
157 O << (int)value;
158 }
Chris Lattner4e972532010-04-04 04:47:45 +0000159 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
160 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000161 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 assert(value <= 31 && "Invalid u5imm argument!");
163 O << (unsigned int)value;
164 }
Chris Lattner4e972532010-04-04 04:47:45 +0000165 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
166 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000167 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 assert(value <= 63 && "Invalid u6imm argument!");
169 O << (unsigned int)value;
170 }
Chris Lattner4e972532010-04-04 04:47:45 +0000171 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
172 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000173 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 }
Chris Lattner4e972532010-04-04 04:47:45 +0000175 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
176 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000177 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 }
Chris Lattner4e972532010-04-04 04:47:45 +0000179 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo,
180 raw_ostream &O) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000181 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000182 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 } else {
184 O << "lo16(";
Chris Lattner4e972532010-04-04 04:47:45 +0000185 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000187 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 else
189 O << ')';
190 }
191 }
Chris Lattner4e972532010-04-04 04:47:45 +0000192 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
193 raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 // Branches can take an immediate operand. This is used by the branch
195 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000196 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000197 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 } else {
Chris Lattner4e972532010-04-04 04:47:45 +0000199 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 }
201 }
Chris Lattner4e972532010-04-04 04:47:45 +0000202 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
203 raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 const MachineOperand &MO = MI->getOperand(OpNo);
205 if (TM.getRelocationModel() != Reloc::Static) {
206 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
207 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000208 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 // Dynamically-resolved functions need a stub for the function.
Chris Lattnerd5a83252010-01-20 21:36:48 +0000210 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingce996512010-03-10 22:34:10 +0000211 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd5a83252010-01-20 21:36:48 +0000212 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingce996512010-03-10 22:34:10 +0000213 if (StubSym.getPointer() == 0)
214 StubSym = MachineModuleInfoImpl::
Chris Lattner2acc03c2010-03-12 21:19:23 +0000215 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000216 O << *Sym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 return;
218 }
219 }
220 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnerd5a83252010-01-20 21:36:48 +0000221 SmallString<128> TempNameStr;
222 TempNameStr += StringRef(MO.getSymbolName());
223 TempNameStr += StringRef("$stub");
224
Chris Lattner77653062010-02-03 06:18:30 +0000225 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Bill Wendlingce996512010-03-10 22:34:10 +0000226 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd5a83252010-01-20 21:36:48 +0000227 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingce996512010-03-10 22:34:10 +0000228 if (StubSym.getPointer() == 0)
229 StubSym = MachineModuleInfoImpl::
230 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000231 O << *Sym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 return;
233 }
234 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000235
Chris Lattner4e972532010-04-04 04:47:45 +0000236 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 }
Chris Lattner4e972532010-04-04 04:47:45 +0000238 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
239 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000240 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 }
Chris Lattner4e972532010-04-04 04:47:45 +0000242 void printPICLabel(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Evan Cheng477013c2007-10-14 05:57:21 +0000243 O << "\"L" << getFunctionNumber() << "$pb\"\n";
244 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 }
Chris Lattner4e972532010-04-04 04:47:45 +0000246 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000247 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner4e972532010-04-04 04:47:45 +0000248 printS16ImmOperand(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 } else {
250 if (Subtarget.isDarwin()) O << "ha16(";
Chris Lattner4e972532010-04-04 04:47:45 +0000251 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000253 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 if (Subtarget.isDarwin())
255 O << ')';
256 else
257 O << "@ha";
258 }
259 }
Chris Lattner4e972532010-04-04 04:47:45 +0000260 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000261 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner4e972532010-04-04 04:47:45 +0000262 printS16ImmOperand(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 } else {
264 if (Subtarget.isDarwin()) O << "lo16(";
Chris Lattner4e972532010-04-04 04:47:45 +0000265 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000267 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 if (Subtarget.isDarwin())
269 O << ')';
270 else
271 O << "@l";
272 }
273 }
Chris Lattner4e972532010-04-04 04:47:45 +0000274 void printcrbitm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 unsigned CCReg = MI->getOperand(OpNo).getReg();
276 unsigned RegNo = enumRegToMachineReg(CCReg);
277 O << (0x80 >> RegNo);
278 }
279 // The new addressing mode printers.
Chris Lattner4e972532010-04-04 04:47:45 +0000280 void printMemRegImm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
281 printSymbolLo(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000283 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 MI->getOperand(OpNo+1).getReg() == PPC::R0)
285 O << "0";
286 else
Chris Lattner4e972532010-04-04 04:47:45 +0000287 printOperand(MI, OpNo+1, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 O << ')';
289 }
Chris Lattner4e972532010-04-04 04:47:45 +0000290 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo,
291 raw_ostream &O) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000292 if (MI->getOperand(OpNo).isImm())
Chris Lattner4e972532010-04-04 04:47:45 +0000293 printS16X4ImmOperand(MI, OpNo, O);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000294 else
Chris Lattner4e972532010-04-04 04:47:45 +0000295 printSymbolLo(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000297 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 MI->getOperand(OpNo+1).getReg() == PPC::R0)
299 O << "0";
300 else
Chris Lattner4e972532010-04-04 04:47:45 +0000301 printOperand(MI, OpNo+1, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 O << ')';
303 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000304
Chris Lattner4e972532010-04-04 04:47:45 +0000305 void printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 // When used as the base register, r0 reads constant zero rather than
307 // the value contained in the register. For this reason, the darwin
308 // assembler requires that we print r0 as 0 (no r) when used as the base.
309 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner4e972532010-04-04 04:47:45 +0000310 printRegister(MO, true, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 O << ", ";
Chris Lattner4e972532010-04-04 04:47:45 +0000312 printOperand(MI, OpNo+1, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000314
Chris Lattner4e972532010-04-04 04:47:45 +0000315 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo,
316 raw_ostream &O) {
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000317 const MachineOperand &MO = MI->getOperand(OpNo);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000318 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
Chris Lattner82d07b72010-04-04 07:05:53 +0000319 MCSymbol *Sym = Mang->getSymbol(MO.getGlobal());
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000320
321 // Map symbol -> label of TOC entry.
Chris Lattner82d07b72010-04-04 07:05:53 +0000322 MCSymbol *&TOCEntry = TOC[Sym];
Chris Lattnerc3009922010-01-16 02:09:06 +0000323 if (TOCEntry == 0)
324 TOCEntry = OutContext.
Chris Lattner3b197832010-03-30 18:10:53 +0000325 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) +
326 "C" + Twine(LabelID++));
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000327
Chris Lattnerce409842010-01-17 21:43:43 +0000328 O << *TOCEntry << "@toc";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000329 }
330
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000331 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner4e972532010-04-04 04:47:45 +0000332 raw_ostream &O, const char *Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 };
334
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000335 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000336 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000337 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000338 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnerf4853452010-03-13 20:55:24 +0000339 MCStreamer &Streamer)
340 : PPCAsmPrinter(O, TM, Streamer) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341
342 virtual const char *getPassName() const {
343 return "Linux PPC Assembly Printer";
344 }
345
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000346 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000347
Chris Lattneraec7a8a2010-01-27 07:21:55 +0000348 virtual void EmitFunctionEntryLabel();
349
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 void getAnalysisUsage(AnalysisUsage &AU) const {
351 AU.setPreservesAll();
352 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000353 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 PPCAsmPrinter::getAnalysisUsage(AU);
355 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 };
357
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000358 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
359 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000360 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000361 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000362 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000363 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnerf4853452010-03-13 20:55:24 +0000364 MCStreamer &Streamer)
365 : PPCAsmPrinter(O, TM, Streamer), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366
367 virtual const char *getPassName() const {
368 return "Darwin PPC Assembly Printer";
369 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000370
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000372 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000373
Chris Lattnerd5a83252010-01-20 21:36:48 +0000374 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Chris Lattner5b187502010-01-20 21:19:44 +0000375
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 void getAnalysisUsage(AnalysisUsage &AU) const {
377 AU.setPreservesAll();
378 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000379 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 PPCAsmPrinter::getAnalysisUsage(AU);
381 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 };
383} // end of anonymous namespace
384
385// Include the auto-generated portion of the assembly writer
386#include "PPCGenAsmWriter.inc"
387
Chris Lattner4e972532010-04-04 04:47:45 +0000388void PPCAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 switch (MO.getType()) {
390 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000391 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392
393 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerd13daf82010-03-13 21:04:28 +0000394 O << *MO.getMBB()->getSymbol();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 return;
396 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000397 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000398 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 // FIXME: PIC relocation model
400 return;
401 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000402 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000403 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000405 case MachineOperand::MO_BlockAddress:
Chris Lattnerce409842010-01-17 21:43:43 +0000406 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilsone8cbca92009-11-04 21:31:18 +0000407 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000408 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 // Computing the address of an external symbol, not calling it.
Chris Lattnerc3009922010-01-16 02:09:06 +0000410 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000411 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerc3009922010-01-16 02:09:06 +0000412 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413 }
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000414
Chris Lattner77653062010-02-03 06:18:30 +0000415 MCSymbol *NLPSym =
Chris Lattnerc3009922010-01-16 02:09:06 +0000416 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
417 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000418 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000419 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlingce996512010-03-10 22:34:10 +0000420 if (StubSym.getPointer() == 0)
421 StubSym = MachineModuleInfoImpl::
422 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000423
Chris Lattnerce409842010-01-17 21:43:43 +0000424 O << *NLPSym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000426 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 case MachineOperand::MO_GlobalAddress: {
428 // Computing the address of a global symbol, not calling it.
429 GlobalValue *GV = MO.getGlobal();
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000430 MCSymbol *SymToPrint;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
432 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000433 if (TM.getRelocationModel() != Reloc::Static &&
434 (GV->isDeclaration() || GV->isWeakForLinker())) {
435 if (!GV->hasHiddenVisibility()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000436 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000437 MachineModuleInfoImpl::StubValueTy &StubSym =
438 MMI->getObjFileInfo<MachineModuleInfoMachO>()
439 .getGVStubEntry(SymToPrint);
440 if (StubSym.getPointer() == 0)
441 StubSym = MachineModuleInfoImpl::
Chris Lattner2acc03c2010-03-12 21:19:23 +0000442 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000443 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
444 GV->hasAvailableExternallyLinkage()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000445 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000446
Bill Wendlingce996512010-03-10 22:34:10 +0000447 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000448 MMI->getObjFileInfo<MachineModuleInfoMachO>().
449 getHiddenGVStubEntry(SymToPrint);
Bill Wendlingce996512010-03-10 22:34:10 +0000450 if (StubSym.getPointer() == 0)
451 StubSym = MachineModuleInfoImpl::
Chris Lattner2acc03c2010-03-12 21:19:23 +0000452 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000453 } else {
Chris Lattner2acc03c2010-03-12 21:19:23 +0000454 SymToPrint = Mang->getSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000456 } else {
Chris Lattner2acc03c2010-03-12 21:19:23 +0000457 SymToPrint = Mang->getSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 }
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000459
Chris Lattnerce409842010-01-17 21:43:43 +0000460 O << *SymToPrint;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000461
Chris Lattner6d718812010-04-03 22:28:33 +0000462 printOffset(MO.getOffset(), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 return;
464 }
465
466 default:
467 O << "<unknown operand type: " << MO.getType() << ">";
468 return;
469 }
470}
471
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472/// PrintAsmOperand - Print out an operand for an inline asm expression.
473///
474bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000475 unsigned AsmVariant,
Chris Lattner3eb5dc12010-04-04 05:29:35 +0000476 const char *ExtraCode, raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477 // Does this asm operand have a single letter operand modifier?
478 if (ExtraCode && ExtraCode[0]) {
479 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000480
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 switch (ExtraCode[0]) {
482 default: return true; // Unknown modifier.
483 case 'c': // Don't print "$" before a global var name or constant.
484 // PPC never has a prefix.
Chris Lattner4e972532010-04-04 04:47:45 +0000485 printOperand(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000487 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000489 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000491 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 return true;
493 ++OpNo; // Return the high-part.
494 break;
495 case 'I':
496 // Write 'i' if an integer constant, otherwise nothing. Used to print
497 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000498 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 O << "i";
500 return false;
501 }
502 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000503
Chris Lattner4e972532010-04-04 04:47:45 +0000504 printOperand(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 return false;
506}
507
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000508// At the moment, all inline asm memory operands are a single register.
509// In any case, the output of this routine should always be just one
510// assembler operand.
511
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000513 unsigned AsmVariant,
Chris Lattner3eb5dc12010-04-04 05:29:35 +0000514 const char *ExtraCode,
515 raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 if (ExtraCode && ExtraCode[0])
517 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000518 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000519 O << "0(";
Chris Lattner4e972532010-04-04 04:47:45 +0000520 printOperand(MI, OpNo, O);
Dale Johannesen5e699502009-08-26 18:10:32 +0000521 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 return false;
523}
524
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000525void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner4e972532010-04-04 04:47:45 +0000526 raw_ostream &O, const char *Modifier){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
528 unsigned Code = MI->getOperand(OpNo).getImm();
529 if (!strcmp(Modifier, "cc")) {
530 switch ((PPC::Predicate)Code) {
531 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
532 case PPC::PRED_LT: O << "lt"; return;
533 case PPC::PRED_LE: O << "le"; return;
534 case PPC::PRED_EQ: O << "eq"; return;
535 case PPC::PRED_GE: O << "ge"; return;
536 case PPC::PRED_GT: O << "gt"; return;
537 case PPC::PRED_NE: O << "ne"; return;
538 case PPC::PRED_UN: O << "un"; return;
539 case PPC::PRED_NU: O << "nu"; return;
540 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000541
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 } else {
543 assert(!strcmp(Modifier, "reg") &&
544 "Need to specify 'cc' or 'reg' as predicate op modifier!");
545 // Don't print the register for 'always'.
546 if (Code == PPC::PRED_ALWAYS) return;
Chris Lattner4e972532010-04-04 04:47:45 +0000547 printOperand(MI, OpNo+1, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 }
549}
550
551
Chris Lattner1d40b372010-01-28 01:28:58 +0000552/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553/// the current output stream.
554///
Chris Lattner1d40b372010-01-28 01:28:58 +0000555void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner82d07b72010-04-04 07:05:53 +0000556 SmallString<128> Str;
557 raw_svector_ostream O(Str);
558
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 // Check for slwi/srwi mnemonics.
560 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000561 unsigned char SH = MI->getOperand(2).getImm();
562 unsigned char MB = MI->getOperand(3).getImm();
563 unsigned char ME = MI->getOperand(4).getImm();
Chris Lattner1d40b372010-01-28 01:28:58 +0000564 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000566 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 }
568 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000569 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 SH = 32-SH;
571 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000572 if (useSubstituteMnemonic) {
Chris Lattner4e972532010-04-04 04:47:45 +0000573 printOperand(MI, 0, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 O << ", ";
Chris Lattner4e972532010-04-04 04:47:45 +0000575 printOperand(MI, 1, O);
Dale Johannesen760acc02010-01-06 02:20:18 +0000576 O << ", " << (unsigned int)SH;
Chris Lattner82d07b72010-04-04 07:05:53 +0000577 OutStreamer.EmitRawText(O.str());
Chris Lattner1d40b372010-01-28 01:28:58 +0000578 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 }
Chris Lattner1d40b372010-01-28 01:28:58 +0000580 }
581
582 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
583 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
584 O << "\tmr ";
Chris Lattner4e972532010-04-04 04:47:45 +0000585 printOperand(MI, 0, O);
Chris Lattner1d40b372010-01-28 01:28:58 +0000586 O << ", ";
Chris Lattner4e972532010-04-04 04:47:45 +0000587 printOperand(MI, 1, O);
Chris Lattner82d07b72010-04-04 07:05:53 +0000588 OutStreamer.EmitRawText(O.str());
Chris Lattner1d40b372010-01-28 01:28:58 +0000589 return;
590 }
591
592 if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000593 unsigned char SH = MI->getOperand(2).getImm();
594 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
596 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000597 O << "\tsldi ";
Chris Lattner4e972532010-04-04 04:47:45 +0000598 printOperand(MI, 0, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 O << ", ";
Chris Lattner4e972532010-04-04 04:47:45 +0000600 printOperand(MI, 1, O);
Dale Johannesen760acc02010-01-06 02:20:18 +0000601 O << ", " << (unsigned int)SH;
Chris Lattner82d07b72010-04-04 07:05:53 +0000602 OutStreamer.EmitRawText(O.str());
Chris Lattner1d40b372010-01-28 01:28:58 +0000603 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604 }
605 }
606
Chris Lattner82d07b72010-04-04 07:05:53 +0000607 printInstruction(MI, O);
608 OutStreamer.EmitRawText(O.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609}
610
Chris Lattneraec7a8a2010-01-27 07:21:55 +0000611void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
612 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
613 return AsmPrinter::EmitFunctionEntryLabel();
614
615 // Emit an official procedure descriptor.
616 // FIXME 64-bit SVR4: Use MCSection here!
Chris Lattner82d07b72010-04-04 07:05:53 +0000617 OutStreamer.EmitRawText(StringRef("\t.section\t\".opd\",\"aw\""));
618 OutStreamer.EmitRawText(StringRef("\t.align 3"));
Chris Lattneraec7a8a2010-01-27 07:21:55 +0000619 OutStreamer.EmitLabel(CurrentFnSym);
Chris Lattner82d07b72010-04-04 07:05:53 +0000620 OutStreamer.EmitRawText("\t.quad .L." + Twine(CurrentFnSym->getName()) +
621 ",.TOC.@tocbase");
622 OutStreamer.EmitRawText(StringRef("\t.previous"));
623 OutStreamer.EmitRawText(".L." + Twine(CurrentFnSym->getName()) + ":");
Chris Lattneraec7a8a2010-01-27 07:21:55 +0000624}
625
626
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000627bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
628 const TargetData *TD = TM.getTargetData();
629
630 bool isPPC64 = TD->getPointerSizeInBits() == 64;
631
632 if (isPPC64 && !TOC.empty()) {
633 // FIXME 64-bit SVR4: Use MCSection here?
Chris Lattner82d07b72010-04-04 07:05:53 +0000634 OutStreamer.EmitRawText(StringRef("\t.section\t\".toc\",\"aw\""));
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000635
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000636 // FIXME: This is nondeterminstic!
Chris Lattner82d07b72010-04-04 07:05:53 +0000637 for (DenseMap<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
Chris Lattnerc3009922010-01-16 02:09:06 +0000638 E = TOC.end(); I != E; ++I) {
Chris Lattner82d07b72010-04-04 07:05:53 +0000639 OutStreamer.EmitLabel(I->second);
640 OutStreamer.EmitRawText("\t.tc " + Twine(I->first->getName()) +
641 "[TC]," + I->first->getName());
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000642 }
643 }
644
645 return AsmPrinter::doFinalization(M);
646}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000647
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000648void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000649 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000650 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000651 "ppc",
652 "ppc601",
653 "ppc602",
654 "ppc603",
655 "ppc7400",
656 "ppc750",
657 "ppc970",
658 "ppc64"
659 };
660
661 unsigned Directive = Subtarget.getDarwinDirective();
662 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
663 Directive = PPC::DIR_970;
664 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
665 Directive = PPC::DIR_7400;
666 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
667 Directive = PPC::DIR_64;
668 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Chris Lattner82d07b72010-04-04 07:05:53 +0000669 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000670
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 // Prime text sections so they are adjacent. This reduces the likelihood a
672 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000673 TargetLoweringObjectFileMachO &TLOFMacho =
674 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000675 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000677 OutStreamer.SwitchSection(
678 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
679 MCSectionMachO::S_SYMBOL_STUBS |
680 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
681 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000683 OutStreamer.SwitchSection(
684 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
685 MCSectionMachO::S_SYMBOL_STUBS |
686 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
687 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000688 }
Chris Lattner73266f92009-08-19 05:49:37 +0000689 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690}
691
Chris Lattnerd5a83252010-01-20 21:36:48 +0000692static const MCSymbol *GetLazyPtr(const MCSymbol *Sym, MCContext &Ctx) {
693 // Remove $stub suffix, add $lazy_ptr.
694 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
695 TmpStr += "$lazy_ptr";
Chris Lattner3b197832010-03-30 18:10:53 +0000696 return Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000697}
698
699static const MCSymbol *GetAnonSym(const MCSymbol *Sym, MCContext &Ctx) {
700 // Add $tmp suffix to $stub, yielding $stub$tmp.
701 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
702 TmpStr += "$tmp";
Chris Lattner3b197832010-03-30 18:10:53 +0000703 return Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000704}
705
706void PPCDarwinAsmPrinter::
707EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chris Lattner5b187502010-01-20 21:19:44 +0000708 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
709
Chris Lattnerf4815552009-08-03 22:52:21 +0000710 TargetLoweringObjectFileMachO &TLOFMacho =
711 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner5b187502010-01-20 21:19:44 +0000712
713 // .lazy_symbol_pointer
714 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattner72a676a2009-08-10 01:39:42 +0000715
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716 // Output stubs for dynamically-linked functions
Chris Lattner5b187502010-01-20 21:19:44 +0000717 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000718 const MCSection *StubSection =
Chris Lattner5b187502010-01-20 21:19:44 +0000719 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
720 MCSectionMachO::S_SYMBOL_STUBS |
721 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
722 32, SectionKind::getText());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000723 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000724 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 EmitAlignment(4);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000726
Chris Lattner82d07b72010-04-04 07:05:53 +0000727 MCSymbol *Stub = Stubs[i].first;
Bill Wendlingce996512010-03-10 22:34:10 +0000728 const MCSymbol *RawSym = Stubs[i].second.getPointer();
Chris Lattnerd5a83252010-01-20 21:36:48 +0000729 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
730 const MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
731
Chris Lattner82d07b72010-04-04 07:05:53 +0000732 OutStreamer.EmitLabel(Stub);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000733 O << "\t.indirect_symbol " << *RawSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 O << "\tmflr r0\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000735 O << "\tbcl 20,31," << *AnonSymbol << '\n';
736 O << *AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 O << "\tmflr r11\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000738 O << "\taddis r11,r11,ha16(" << *LazyPtr << '-' << *AnonSymbol
Chris Lattner5b187502010-01-20 21:19:44 +0000739 << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 O << "\tmtlr r0\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000741 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
742 << '-' << *AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743 O << "\tmtctr r12\n";
744 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +0000745
Chris Lattner73266f92009-08-19 05:49:37 +0000746 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000747 O << *LazyPtr << ":\n";
748 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +0000749 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 }
Chris Lattner82d07b72010-04-04 07:05:53 +0000751 OutStreamer.AddBlankLine();
Chris Lattner5b187502010-01-20 21:19:44 +0000752 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000753 }
Chris Lattner5b187502010-01-20 21:19:44 +0000754
755 const MCSection *StubSection =
756 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
757 MCSectionMachO::S_SYMBOL_STUBS |
758 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
759 16, SectionKind::getText());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000760 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner82d07b72010-04-04 07:05:53 +0000761 MCSymbol *Stub = Stubs[i].first;
Bill Wendlingce996512010-03-10 22:34:10 +0000762 const MCSymbol *RawSym = Stubs[i].second.getPointer();
Chris Lattnerd5a83252010-01-20 21:36:48 +0000763 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
764
Chris Lattner5b187502010-01-20 21:19:44 +0000765 OutStreamer.SwitchSection(StubSection);
766 EmitAlignment(4);
Chris Lattner82d07b72010-04-04 07:05:53 +0000767 OutStreamer.EmitLabel(Stub);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000768 O << "\t.indirect_symbol " << *RawSym << '\n';
769 O << "\tlis r11,ha16(" << *LazyPtr << ")\n";
770 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
Chris Lattner5b187502010-01-20 21:19:44 +0000771 << ")(r11)\n";
772 O << "\tmtctr r12\n";
773 O << "\tbctr\n";
774 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000775 O << *LazyPtr << ":\n";
776 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattner5b187502010-01-20 21:19:44 +0000777 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
778 }
779
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000780 O << '\n';
Chris Lattner5b187502010-01-20 21:19:44 +0000781}
782
783
784bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
785 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
786
787 // Darwin/PPC always uses mach-o.
788 TargetLoweringObjectFileMachO &TLOFMacho =
789 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
790 MachineModuleInfoMachO &MMIMacho =
791 MMI->getObjFileInfo<MachineModuleInfoMachO>();
792
Chris Lattnerd5a83252010-01-20 21:36:48 +0000793 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
794 if (!Stubs.empty())
795 EmitFunctionStubs(Stubs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000797 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000798 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +0000799 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +0000800 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000801 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000802 E = Personalities.end(); I != E; ++I) {
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000803 if (*I) {
Chris Lattner77653062010-02-03 06:18:30 +0000804 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000805 MachineModuleInfoImpl::StubValueTy &StubSym =
806 MMIMacho.getGVStubEntry(NLPSym);
Chris Lattner2acc03c2010-03-12 21:19:23 +0000807 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000808 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000809 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000810 }
811
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000812 // Output stubs for dynamically-linked functions.
Chris Lattnerd5a83252010-01-20 21:36:48 +0000813 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000814
Chris Lattnerf4815552009-08-03 22:52:21 +0000815 // Output macho stubs for external and common global variables.
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000816 if (!Stubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +0000817 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +0000818 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +0000819 EmitAlignment(isPPC64 ? 3 : 2);
820
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000821 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingd4fba932010-03-11 23:39:44 +0000822 // L_foo$stub:
823 OutStreamer.EmitLabel(Stubs[i].first);
824 // .indirect_symbol _foo
825 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
826 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendling87913bf2010-03-12 02:00:43 +0000827
828 if (MCSym.getInt())
829 // External to current translation unit.
830 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
831 else
832 // Internal to current translation unit.
Bill Wendlingdfd093f2010-03-31 18:47:10 +0000833 //
834 // When we place the LSDA into the TEXT section, the type info pointers
835 // need to be indirect and pc-rel. We accomplish this by using NLPs.
836 // However, sometimes the types are local to the file. So we need to
837 // fill in the value for the NLP in those cases.
Bill Wendling87913bf2010-03-12 02:00:43 +0000838 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
839 OutContext),
840 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841 }
Bill Wendlingd4fba932010-03-11 23:39:44 +0000842
843 Stubs.clear();
844 OutStreamer.AddBlankLine();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 }
846
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000847 Stubs = MMIMacho.GetHiddenGVStubList();
848 if (!Stubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +0000849 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000850 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000851
852 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingd4fba932010-03-11 23:39:44 +0000853 // L_foo$stub:
854 OutStreamer.EmitLabel(Stubs[i].first);
855 // .long _foo
856 OutStreamer.EmitValue(MCSymbolRefExpr::
857 Create(Stubs[i].second.getPointer(),
858 OutContext),
859 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Evan Chenga65854f2008-12-05 01:06:39 +0000860 }
Bill Wendlingd4fba932010-03-11 23:39:44 +0000861
862 Stubs.clear();
863 OutStreamer.AddBlankLine();
Evan Chenga65854f2008-12-05 01:06:39 +0000864 }
865
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 // Funny Darwin hack: This flag tells the linker that no global symbols
867 // contain code that falls through to other global symbols (e.g. the obvious
868 // implementation of multiple entry points). If this doesn't occur, the
869 // linker can safely perform dead code stripping. Since LLVM never generates
870 // code that does this, it is always safe to set.
Chris Lattner2d7c8142010-01-23 06:39:22 +0000871 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872
Dan Gohman4a558a32007-07-25 19:33:14 +0000873 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874}
875
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000876/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
877/// for a MachineFunction to the given output stream, in a format that the
878/// Darwin assembler can deal with.
879///
Daniel Dunbar4cb63652009-08-13 23:48:47 +0000880static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
881 TargetMachine &tm,
Chris Lattnerf4853452010-03-13 20:55:24 +0000882 MCStreamer &Streamer) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
884
Chris Lattnerae982212009-07-21 18:38:57 +0000885 if (Subtarget->isDarwin())
Chris Lattnerf4853452010-03-13 20:55:24 +0000886 return new PPCDarwinAsmPrinter(o, tm, Streamer);
887 return new PPCLinuxAsmPrinter(o, tm, Streamer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000889
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000890// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000891extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000892 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000893 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
894}