blob: 12c1e53ed7d0ffa6cb28114265c9ccccbcbc953b [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 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,
146 unsigned AsmVariant, const char *ExtraCode);
147 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
148 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000149
150
Chris Lattner4e972532010-04-04 04:47:45 +0000151 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo,
152 raw_ostream &O) {
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 }
Chris Lattner4e972532010-04-04 04:47:45 +0000157 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
158 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000159 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 assert(value <= 31 && "Invalid u5imm argument!");
161 O << (unsigned int)value;
162 }
Chris Lattner4e972532010-04-04 04:47:45 +0000163 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
164 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000165 unsigned char value = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 assert(value <= 63 && "Invalid u6imm argument!");
167 O << (unsigned int)value;
168 }
Chris Lattner4e972532010-04-04 04:47:45 +0000169 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
170 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000171 O << (short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 }
Chris Lattner4e972532010-04-04 04:47:45 +0000173 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
174 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000175 O << (unsigned short)MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 }
Chris Lattner4e972532010-04-04 04:47:45 +0000177 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo,
178 raw_ostream &O) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000179 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000180 O << (short)(MI->getOperand(OpNo).getImm()*4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 } else {
182 O << "lo16(";
Chris Lattner4e972532010-04-04 04:47:45 +0000183 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000185 O << "-\"L" << getFunctionNumber() << "$pb\")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 else
187 O << ')';
188 }
189 }
Chris Lattner4e972532010-04-04 04:47:45 +0000190 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
191 raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 // Branches can take an immediate operand. This is used by the branch
193 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000194 if (MI->getOperand(OpNo).isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000195 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 } else {
Chris Lattner4e972532010-04-04 04:47:45 +0000197 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 }
199 }
Chris Lattner4e972532010-04-04 04:47:45 +0000200 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
201 raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 const MachineOperand &MO = MI->getOperand(OpNo);
203 if (TM.getRelocationModel() != Reloc::Static) {
204 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
205 GlobalValue *GV = MO.getGlobal();
Chris Lattner0fd4feb2009-07-02 16:08:53 +0000206 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 // Dynamically-resolved functions need a stub for the function.
Chris Lattnerd5a83252010-01-20 21:36:48 +0000208 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingce996512010-03-10 22:34:10 +0000209 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd5a83252010-01-20 21:36:48 +0000210 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingce996512010-03-10 22:34:10 +0000211 if (StubSym.getPointer() == 0)
212 StubSym = MachineModuleInfoImpl::
Chris Lattner2acc03c2010-03-12 21:19:23 +0000213 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000214 O << *Sym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 return;
216 }
217 }
218 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattnerd5a83252010-01-20 21:36:48 +0000219 SmallString<128> TempNameStr;
220 TempNameStr += StringRef(MO.getSymbolName());
221 TempNameStr += StringRef("$stub");
222
Chris Lattner77653062010-02-03 06:18:30 +0000223 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Bill Wendlingce996512010-03-10 22:34:10 +0000224 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd5a83252010-01-20 21:36:48 +0000225 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingce996512010-03-10 22:34:10 +0000226 if (StubSym.getPointer() == 0)
227 StubSym = MachineModuleInfoImpl::
228 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000229 O << *Sym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 return;
231 }
232 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000233
Chris Lattner4e972532010-04-04 04:47:45 +0000234 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 }
Chris Lattner4e972532010-04-04 04:47:45 +0000236 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
237 raw_ostream &O) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000238 O << (int)MI->getOperand(OpNo).getImm()*4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 }
Chris Lattner4e972532010-04-04 04:47:45 +0000240 void printPICLabel(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Evan Cheng477013c2007-10-14 05:57:21 +0000241 O << "\"L" << getFunctionNumber() << "$pb\"\n";
242 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 }
Chris Lattner4e972532010-04-04 04:47:45 +0000244 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000245 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner4e972532010-04-04 04:47:45 +0000246 printS16ImmOperand(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 } else {
248 if (Subtarget.isDarwin()) O << "ha16(";
Chris Lattner4e972532010-04-04 04:47:45 +0000249 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000251 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 if (Subtarget.isDarwin())
253 O << ')';
254 else
255 O << "@ha";
256 }
257 }
Chris Lattner4e972532010-04-04 04:47:45 +0000258 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000259 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner4e972532010-04-04 04:47:45 +0000260 printS16ImmOperand(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 } else {
262 if (Subtarget.isDarwin()) O << "lo16(";
Chris Lattner4e972532010-04-04 04:47:45 +0000263 printOp(MI->getOperand(OpNo), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng477013c2007-10-14 05:57:21 +0000265 O << "-\"L" << getFunctionNumber() << "$pb\"";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (Subtarget.isDarwin())
267 O << ')';
268 else
269 O << "@l";
270 }
271 }
Chris Lattner4e972532010-04-04 04:47:45 +0000272 void printcrbitm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 unsigned CCReg = MI->getOperand(OpNo).getReg();
274 unsigned RegNo = enumRegToMachineReg(CCReg);
275 O << (0x80 >> RegNo);
276 }
277 // The new addressing mode printers.
Chris Lattner4e972532010-04-04 04:47:45 +0000278 void printMemRegImm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
279 printSymbolLo(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000281 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 MI->getOperand(OpNo+1).getReg() == PPC::R0)
283 O << "0";
284 else
Chris Lattner4e972532010-04-04 04:47:45 +0000285 printOperand(MI, OpNo+1, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 O << ')';
287 }
Chris Lattner4e972532010-04-04 04:47:45 +0000288 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo,
289 raw_ostream &O) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000290 if (MI->getOperand(OpNo).isImm())
Chris Lattner4e972532010-04-04 04:47:45 +0000291 printS16X4ImmOperand(MI, OpNo, O);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000292 else
Chris Lattner4e972532010-04-04 04:47:45 +0000293 printSymbolLo(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 O << '(';
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000295 if (MI->getOperand(OpNo+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 MI->getOperand(OpNo+1).getReg() == PPC::R0)
297 O << "0";
298 else
Chris Lattner4e972532010-04-04 04:47:45 +0000299 printOperand(MI, OpNo+1, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 O << ')';
301 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000302
Chris Lattner4e972532010-04-04 04:47:45 +0000303 void printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 // When used as the base register, r0 reads constant zero rather than
305 // the value contained in the register. For this reason, the darwin
306 // assembler requires that we print r0 as 0 (no r) when used as the base.
307 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner4e972532010-04-04 04:47:45 +0000308 printRegister(MO, true, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 O << ", ";
Chris Lattner4e972532010-04-04 04:47:45 +0000310 printOperand(MI, OpNo+1, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000312
Chris Lattner4e972532010-04-04 04:47:45 +0000313 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo,
314 raw_ostream &O) {
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000315 const MachineOperand &MO = MI->getOperand(OpNo);
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000316 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
Chris Lattner2acc03c2010-03-12 21:19:23 +0000317 const MCSymbol *Sym = Mang->getSymbol(MO.getGlobal());
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000318
319 // Map symbol -> label of TOC entry.
Chris Lattnerc3009922010-01-16 02:09:06 +0000320 const MCSymbol *&TOCEntry = TOC[Sym];
321 if (TOCEntry == 0)
322 TOCEntry = OutContext.
Chris Lattner3b197832010-03-30 18:10:53 +0000323 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) +
324 "C" + Twine(LabelID++));
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000325
Chris Lattnerce409842010-01-17 21:43:43 +0000326 O << *TOCEntry << "@toc";
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000327 }
328
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000329 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner4e972532010-04-04 04:47:45 +0000330 raw_ostream &O, const char *Modifier);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 };
332
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000333 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky492d06e2009-10-25 06:33:48 +0000334 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling4f405312009-02-24 08:30:20 +0000335 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000336 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnerf4853452010-03-13 20:55:24 +0000337 MCStreamer &Streamer)
338 : PPCAsmPrinter(O, TM, Streamer) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
340 virtual const char *getPassName() const {
341 return "Linux PPC Assembly Printer";
342 }
343
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000344 bool doFinalization(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000345
Chris Lattneraec7a8a2010-01-27 07:21:55 +0000346 virtual void EmitFunctionEntryLabel();
347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 void getAnalysisUsage(AnalysisUsage &AU) const {
349 AU.setPreservesAll();
350 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000351 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 PPCAsmPrinter::getAnalysisUsage(AU);
353 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 };
355
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000356 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
357 /// OS X
Nick Lewycky492d06e2009-10-25 06:33:48 +0000358 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene302008d2009-07-14 20:18:05 +0000359 formatted_raw_ostream &OS;
Bill Wendling4f405312009-02-24 08:30:20 +0000360 public:
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000361 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnerf4853452010-03-13 20:55:24 +0000362 MCStreamer &Streamer)
363 : PPCAsmPrinter(O, TM, Streamer), OS(O) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364
365 virtual const char *getPassName() const {
366 return "Darwin PPC Assembly Printer";
367 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000368
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 bool doFinalization(Module &M);
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000370 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000371
Chris Lattnerd5a83252010-01-20 21:36:48 +0000372 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Chris Lattner5b187502010-01-20 21:19:44 +0000373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 void getAnalysisUsage(AnalysisUsage &AU) const {
375 AU.setPreservesAll();
376 AU.addRequired<MachineModuleInfo>();
Devang Patelaa1e8432009-01-08 23:40:34 +0000377 AU.addRequired<DwarfWriter>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 PPCAsmPrinter::getAnalysisUsage(AU);
379 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 };
381} // end of anonymous namespace
382
383// Include the auto-generated portion of the assembly writer
384#include "PPCGenAsmWriter.inc"
385
Chris Lattner4e972532010-04-04 04:47:45 +0000386void PPCAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 switch (MO.getType()) {
388 case MachineOperand::MO_Immediate:
Edwin Törökbd448e32009-07-14 16:55:14 +0000389 llvm_unreachable("printOp() does not handle immediate values");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390
391 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerd13daf82010-03-13 21:04:28 +0000392 O << *MO.getMBB()->getSymbol();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 return;
394 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000395 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000396 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 // FIXME: PIC relocation model
398 return;
399 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000400 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000401 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 return;
Bob Wilsone8cbca92009-11-04 21:31:18 +0000403 case MachineOperand::MO_BlockAddress:
Chris Lattnerce409842010-01-17 21:43:43 +0000404 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilsone8cbca92009-11-04 21:31:18 +0000405 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000406 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 // Computing the address of an external symbol, not calling it.
Chris Lattnerc3009922010-01-16 02:09:06 +0000408 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000409 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattnerc3009922010-01-16 02:09:06 +0000410 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 }
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000412
Chris Lattner77653062010-02-03 06:18:30 +0000413 MCSymbol *NLPSym =
Chris Lattnerc3009922010-01-16 02:09:06 +0000414 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
415 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000416 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000417 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlingce996512010-03-10 22:34:10 +0000418 if (StubSym.getPointer() == 0)
419 StubSym = MachineModuleInfoImpl::
420 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000421
Chris Lattnerce409842010-01-17 21:43:43 +0000422 O << *NLPSym;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 return;
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000424 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 case MachineOperand::MO_GlobalAddress: {
426 // Computing the address of a global symbol, not calling it.
427 GlobalValue *GV = MO.getGlobal();
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000428 MCSymbol *SymToPrint;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429
430 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000431 if (TM.getRelocationModel() != Reloc::Static &&
432 (GV->isDeclaration() || GV->isWeakForLinker())) {
433 if (!GV->hasHiddenVisibility()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000434 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000435 MachineModuleInfoImpl::StubValueTy &StubSym =
436 MMI->getObjFileInfo<MachineModuleInfoMachO>()
437 .getGVStubEntry(SymToPrint);
438 if (StubSym.getPointer() == 0)
439 StubSym = MachineModuleInfoImpl::
Chris Lattner2acc03c2010-03-12 21:19:23 +0000440 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000441 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
442 GV->hasAvailableExternallyLinkage()) {
Chris Lattnera6f2a102010-01-16 18:37:32 +0000443 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000444
Bill Wendlingce996512010-03-10 22:34:10 +0000445 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000446 MMI->getObjFileInfo<MachineModuleInfoMachO>().
447 getHiddenGVStubEntry(SymToPrint);
Bill Wendlingce996512010-03-10 22:34:10 +0000448 if (StubSym.getPointer() == 0)
449 StubSym = MachineModuleInfoImpl::
Chris Lattner2acc03c2010-03-12 21:19:23 +0000450 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000451 } else {
Chris Lattner2acc03c2010-03-12 21:19:23 +0000452 SymToPrint = Mang->getSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000454 } else {
Chris Lattner2acc03c2010-03-12 21:19:23 +0000455 SymToPrint = Mang->getSymbol(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 }
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000457
Chris Lattnerce409842010-01-17 21:43:43 +0000458 O << *SymToPrint;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000459
Chris Lattner6d718812010-04-03 22:28:33 +0000460 printOffset(MO.getOffset(), O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 return;
462 }
463
464 default:
465 O << "<unknown operand type: " << MO.getType() << ">";
466 return;
467 }
468}
469
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470/// PrintAsmOperand - Print out an operand for an inline asm expression.
471///
472bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000473 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 const char *ExtraCode) {
475 // Does this asm operand have a single letter operand modifier?
476 if (ExtraCode && ExtraCode[0]) {
477 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000478
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479 switch (ExtraCode[0]) {
480 default: return true; // Unknown modifier.
481 case 'c': // Don't print "$" before a global var name or constant.
482 // PPC never has a prefix.
Chris Lattner4e972532010-04-04 04:47:45 +0000483 printOperand(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 return false;
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000485 case 'L': // Write second word of DImode reference.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 // Verify that this operand has two consecutive registers.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000487 if (!MI->getOperand(OpNo).isReg() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 OpNo+1 == MI->getNumOperands() ||
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000489 !MI->getOperand(OpNo+1).isReg())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 return true;
491 ++OpNo; // Return the high-part.
492 break;
493 case 'I':
494 // Write 'i' if an integer constant, otherwise nothing. Used to print
495 // addi vs add, etc.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000496 if (MI->getOperand(OpNo).isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 O << "i";
498 return false;
499 }
500 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000501
Chris Lattner4e972532010-04-04 04:47:45 +0000502 printOperand(MI, OpNo, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 return false;
504}
505
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000506// At the moment, all inline asm memory operands are a single register.
507// In any case, the output of this routine should always be just one
508// assembler operand.
509
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000511 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 const char *ExtraCode) {
513 if (ExtraCode && ExtraCode[0])
514 return true; // Unknown modifier.
Dale Johannesen0a42b9662009-08-18 00:18:39 +0000515 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen5e699502009-08-26 18:10:32 +0000516 O << "0(";
Chris Lattner4e972532010-04-04 04:47:45 +0000517 printOperand(MI, OpNo, O);
Dale Johannesen5e699502009-08-26 18:10:32 +0000518 O << ")";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 return false;
520}
521
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000522void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner4e972532010-04-04 04:47:45 +0000523 raw_ostream &O, const char *Modifier){
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
525 unsigned Code = MI->getOperand(OpNo).getImm();
526 if (!strcmp(Modifier, "cc")) {
527 switch ((PPC::Predicate)Code) {
528 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
529 case PPC::PRED_LT: O << "lt"; return;
530 case PPC::PRED_LE: O << "le"; return;
531 case PPC::PRED_EQ: O << "eq"; return;
532 case PPC::PRED_GE: O << "ge"; return;
533 case PPC::PRED_GT: O << "gt"; return;
534 case PPC::PRED_NE: O << "ne"; return;
535 case PPC::PRED_UN: O << "un"; return;
536 case PPC::PRED_NU: O << "nu"; return;
537 }
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000538
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 } else {
540 assert(!strcmp(Modifier, "reg") &&
541 "Need to specify 'cc' or 'reg' as predicate op modifier!");
542 // Don't print the register for 'always'.
543 if (Code == PPC::PRED_ALWAYS) return;
Chris Lattner4e972532010-04-04 04:47:45 +0000544 printOperand(MI, OpNo+1, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000545 }
546}
547
548
Chris Lattner1d40b372010-01-28 01:28:58 +0000549/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550/// the current output stream.
551///
Chris Lattner1d40b372010-01-28 01:28:58 +0000552void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 // Check for slwi/srwi mnemonics.
554 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000555 unsigned char SH = MI->getOperand(2).getImm();
556 unsigned char MB = MI->getOperand(3).getImm();
557 unsigned char ME = MI->getOperand(4).getImm();
Chris Lattner1d40b372010-01-28 01:28:58 +0000558 bool useSubstituteMnemonic = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000560 O << "\tslwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 }
562 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesen760acc02010-01-06 02:20:18 +0000563 O << "\tsrwi "; useSubstituteMnemonic = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 SH = 32-SH;
565 }
Dale Johannesen760acc02010-01-06 02:20:18 +0000566 if (useSubstituteMnemonic) {
Chris Lattner4e972532010-04-04 04:47:45 +0000567 printOperand(MI, 0, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 O << ", ";
Chris Lattner4e972532010-04-04 04:47:45 +0000569 printOperand(MI, 1, O);
Dale Johannesen760acc02010-01-06 02:20:18 +0000570 O << ", " << (unsigned int)SH;
Chris Lattner726d3972010-02-10 00:36:00 +0000571 OutStreamer.AddBlankLine();
Chris Lattner1d40b372010-01-28 01:28:58 +0000572 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 }
Chris Lattner1d40b372010-01-28 01:28:58 +0000574 }
575
576 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
577 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
578 O << "\tmr ";
Chris Lattner4e972532010-04-04 04:47:45 +0000579 printOperand(MI, 0, O);
Chris Lattner1d40b372010-01-28 01:28:58 +0000580 O << ", ";
Chris Lattner4e972532010-04-04 04:47:45 +0000581 printOperand(MI, 1, O);
Chris Lattner726d3972010-02-10 00:36:00 +0000582 OutStreamer.AddBlankLine();
Chris Lattner1d40b372010-01-28 01:28:58 +0000583 return;
584 }
585
586 if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000587 unsigned char SH = MI->getOperand(2).getImm();
588 unsigned char ME = MI->getOperand(3).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
590 if (63-SH == ME) {
Nate Begemanbd5cdf12008-02-05 08:49:09 +0000591 O << "\tsldi ";
Chris Lattner4e972532010-04-04 04:47:45 +0000592 printOperand(MI, 0, O);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593 O << ", ";
Chris Lattner4e972532010-04-04 04:47:45 +0000594 printOperand(MI, 1, O);
Dale Johannesen760acc02010-01-06 02:20:18 +0000595 O << ", " << (unsigned int)SH;
Chris Lattner726d3972010-02-10 00:36:00 +0000596 OutStreamer.AddBlankLine();
Chris Lattner1d40b372010-01-28 01:28:58 +0000597 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 }
599 }
600
Chris Lattner4e972532010-04-04 04:47:45 +0000601 printInstruction(MI, O);
Chris Lattner726d3972010-02-10 00:36:00 +0000602 OutStreamer.AddBlankLine();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603}
604
Chris Lattneraec7a8a2010-01-27 07:21:55 +0000605void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
606 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
607 return AsmPrinter::EmitFunctionEntryLabel();
608
609 // Emit an official procedure descriptor.
610 // FIXME 64-bit SVR4: Use MCSection here!
611 O << "\t.section\t\".opd\",\"aw\"\n";
612 O << "\t.align 3\n";
613 OutStreamer.EmitLabel(CurrentFnSym);
614 O << "\t.quad .L." << *CurrentFnSym << ",.TOC.@tocbase\n";
615 O << "\t.previous\n";
616 O << ".L." << *CurrentFnSym << ":\n";
617}
618
619
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000620bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
621 const TargetData *TD = TM.getTargetData();
622
623 bool isPPC64 = TD->getPointerSizeInBits() == 64;
624
625 if (isPPC64 && !TOC.empty()) {
626 // FIXME 64-bit SVR4: Use MCSection here?
627 O << "\t.section\t\".toc\",\"aw\"\n";
628
Chris Lattner5eeaf2a2010-01-16 02:00:23 +0000629 // FIXME: This is nondeterminstic!
Chris Lattnerc3009922010-01-16 02:09:06 +0000630 for (DenseMap<const MCSymbol*, const MCSymbol*>::iterator I = TOC.begin(),
631 E = TOC.end(); I != E; ++I) {
Chris Lattnerce409842010-01-17 21:43:43 +0000632 O << *I->second << ":\n";
633 O << "\t.tc " << *I->first << "[TC]," << *I->first << '\n';
Tilmann Scheller72cf2812009-08-15 11:54:46 +0000634 }
635 }
636
637 return AsmPrinter::doFinalization(M);
638}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000640void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohman12300e12008-03-25 21:45:14 +0000641 static const char *const CPUDirectives[] = {
Dale Johannesen161badc2008-02-14 23:35:16 +0000642 "",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 "ppc",
644 "ppc601",
645 "ppc602",
646 "ppc603",
647 "ppc7400",
648 "ppc750",
649 "ppc970",
650 "ppc64"
651 };
652
653 unsigned Directive = Subtarget.getDarwinDirective();
654 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
655 Directive = PPC::DIR_970;
656 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
657 Directive = PPC::DIR_7400;
658 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
659 Directive = PPC::DIR_64;
660 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000661 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov28f86d12008-08-08 18:22:59 +0000662
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 // Prime text sections so they are adjacent. This reduces the likelihood a
664 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattnerf4815552009-08-03 22:52:21 +0000665 TargetLoweringObjectFileMachO &TLOFMacho =
666 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner73266f92009-08-19 05:49:37 +0000667 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner73266f92009-08-19 05:49:37 +0000669 OutStreamer.SwitchSection(
670 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
671 MCSectionMachO::S_SYMBOL_STUBS |
672 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
673 32, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner73266f92009-08-19 05:49:37 +0000675 OutStreamer.SwitchSection(
676 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
677 MCSectionMachO::S_SYMBOL_STUBS |
678 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
679 16, SectionKind::getText()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 }
Chris Lattner73266f92009-08-19 05:49:37 +0000681 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682}
683
Chris Lattnerd5a83252010-01-20 21:36:48 +0000684static const MCSymbol *GetLazyPtr(const MCSymbol *Sym, MCContext &Ctx) {
685 // Remove $stub suffix, add $lazy_ptr.
686 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
687 TmpStr += "$lazy_ptr";
Chris Lattner3b197832010-03-30 18:10:53 +0000688 return Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000689}
690
691static const MCSymbol *GetAnonSym(const MCSymbol *Sym, MCContext &Ctx) {
692 // Add $tmp suffix to $stub, yielding $stub$tmp.
693 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
694 TmpStr += "$tmp";
Chris Lattner3b197832010-03-30 18:10:53 +0000695 return Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000696}
697
698void PPCDarwinAsmPrinter::
699EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chris Lattner5b187502010-01-20 21:19:44 +0000700 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
701
Chris Lattnerf4815552009-08-03 22:52:21 +0000702 TargetLoweringObjectFileMachO &TLOFMacho =
703 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner5b187502010-01-20 21:19:44 +0000704
705 // .lazy_symbol_pointer
706 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattner72a676a2009-08-10 01:39:42 +0000707
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 // Output stubs for dynamically-linked functions
Chris Lattner5b187502010-01-20 21:19:44 +0000709 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerf4815552009-08-03 22:52:21 +0000710 const MCSection *StubSection =
Chris Lattner5b187502010-01-20 21:19:44 +0000711 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
712 MCSectionMachO::S_SYMBOL_STUBS |
713 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
714 32, SectionKind::getText());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000715 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000716 OutStreamer.SwitchSection(StubSection);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 EmitAlignment(4);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000718
719 const MCSymbol *Stub = Stubs[i].first;
Bill Wendlingce996512010-03-10 22:34:10 +0000720 const MCSymbol *RawSym = Stubs[i].second.getPointer();
Chris Lattnerd5a83252010-01-20 21:36:48 +0000721 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
722 const MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
723
724 O << *Stub << ":\n";
725 O << "\t.indirect_symbol " << *RawSym << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 O << "\tmflr r0\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000727 O << "\tbcl 20,31," << *AnonSymbol << '\n';
728 O << *AnonSymbol << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 O << "\tmflr r11\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000730 O << "\taddis r11,r11,ha16(" << *LazyPtr << '-' << *AnonSymbol
Chris Lattner5b187502010-01-20 21:19:44 +0000731 << ")\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732 O << "\tmtlr r0\n";
Chris Lattnerd5a83252010-01-20 21:36:48 +0000733 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
734 << '-' << *AnonSymbol << ")(r11)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 O << "\tmtctr r12\n";
736 O << "\tbctr\n";
Chris Lattnerc0a7f1d2009-07-16 01:23:26 +0000737
Chris Lattner73266f92009-08-19 05:49:37 +0000738 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000739 O << *LazyPtr << ":\n";
740 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattnerbaa12da2009-07-15 02:36:21 +0000741 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 }
Chris Lattner5b187502010-01-20 21:19:44 +0000743 O << '\n';
744 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 }
Chris Lattner5b187502010-01-20 21:19:44 +0000746
747 const MCSection *StubSection =
748 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
749 MCSectionMachO::S_SYMBOL_STUBS |
750 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
751 16, SectionKind::getText());
Chris Lattnerd5a83252010-01-20 21:36:48 +0000752 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
753 const MCSymbol *Stub = Stubs[i].first;
Bill Wendlingce996512010-03-10 22:34:10 +0000754 const MCSymbol *RawSym = Stubs[i].second.getPointer();
Chris Lattnerd5a83252010-01-20 21:36:48 +0000755 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
756
Chris Lattner5b187502010-01-20 21:19:44 +0000757 OutStreamer.SwitchSection(StubSection);
758 EmitAlignment(4);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000759 O << *Stub << ":\n";
760 O << "\t.indirect_symbol " << *RawSym << '\n';
761 O << "\tlis r11,ha16(" << *LazyPtr << ")\n";
762 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
Chris Lattner5b187502010-01-20 21:19:44 +0000763 << ")(r11)\n";
764 O << "\tmtctr r12\n";
765 O << "\tbctr\n";
766 OutStreamer.SwitchSection(LSPSection);
Chris Lattnerd5a83252010-01-20 21:36:48 +0000767 O << *LazyPtr << ":\n";
768 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattner5b187502010-01-20 21:19:44 +0000769 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
770 }
771
Anton Korobeynikovdaf7efe2008-08-08 18:24:10 +0000772 O << '\n';
Chris Lattner5b187502010-01-20 21:19:44 +0000773}
774
775
776bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
777 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
778
779 // Darwin/PPC always uses mach-o.
780 TargetLoweringObjectFileMachO &TLOFMacho =
781 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
782 MachineModuleInfoMachO &MMIMacho =
783 MMI->getObjFileInfo<MachineModuleInfoMachO>();
784
Chris Lattnerd5a83252010-01-20 21:36:48 +0000785 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
786 if (!Stubs.empty())
787 EmitFunctionStubs(Stubs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000789 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000790 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen85535762008-04-02 00:25:04 +0000791 // Only referenced functions get into the Personalities list.
Chris Lattner2424eac2009-06-24 19:09:55 +0000792 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000793 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000794 E = Personalities.end(); I != E; ++I) {
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000795 if (*I) {
Chris Lattner77653062010-02-03 06:18:30 +0000796 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlingce996512010-03-10 22:34:10 +0000797 MachineModuleInfoImpl::StubValueTy &StubSym =
798 MMIMacho.getGVStubEntry(NLPSym);
Chris Lattner2acc03c2010-03-12 21:19:23 +0000799 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000800 }
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000801 }
Dale Johannesenfb3ac732007-11-20 23:24:42 +0000802 }
803
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000804 // Output stubs for dynamically-linked functions.
Chris Lattnerd5a83252010-01-20 21:36:48 +0000805 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000806
Chris Lattnerf4815552009-08-03 22:52:21 +0000807 // Output macho stubs for external and common global variables.
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000808 if (!Stubs.empty()) {
Chris Lattner72a676a2009-08-10 01:39:42 +0000809 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner73266f92009-08-19 05:49:37 +0000810 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner2d61eaa2009-08-10 17:58:51 +0000811 EmitAlignment(isPPC64 ? 3 : 2);
812
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000813 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingd4fba932010-03-11 23:39:44 +0000814 // L_foo$stub:
815 OutStreamer.EmitLabel(Stubs[i].first);
816 // .indirect_symbol _foo
817 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
818 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendling87913bf2010-03-12 02:00:43 +0000819
820 if (MCSym.getInt())
821 // External to current translation unit.
822 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
823 else
824 // Internal to current translation unit.
Bill Wendlingdfd093f2010-03-31 18:47:10 +0000825 //
826 // When we place the LSDA into the TEXT section, the type info pointers
827 // need to be indirect and pc-rel. We accomplish this by using NLPs.
828 // However, sometimes the types are local to the file. So we need to
829 // fill in the value for the NLP in those cases.
Bill Wendling87913bf2010-03-12 02:00:43 +0000830 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
831 OutContext),
832 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 }
Bill Wendlingd4fba932010-03-11 23:39:44 +0000834
835 Stubs.clear();
836 OutStreamer.AddBlankLine();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000837 }
838
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000839 Stubs = MMIMacho.GetHiddenGVStubList();
840 if (!Stubs.empty()) {
Chris Lattner73266f92009-08-19 05:49:37 +0000841 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner83cf1ec2009-07-15 01:14:44 +0000842 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerc2cfe152010-01-20 21:16:14 +0000843
844 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingd4fba932010-03-11 23:39:44 +0000845 // L_foo$stub:
846 OutStreamer.EmitLabel(Stubs[i].first);
847 // .long _foo
848 OutStreamer.EmitValue(MCSymbolRefExpr::
849 Create(Stubs[i].second.getPointer(),
850 OutContext),
851 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Evan Chenga65854f2008-12-05 01:06:39 +0000852 }
Bill Wendlingd4fba932010-03-11 23:39:44 +0000853
854 Stubs.clear();
855 OutStreamer.AddBlankLine();
Evan Chenga65854f2008-12-05 01:06:39 +0000856 }
857
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 // Funny Darwin hack: This flag tells the linker that no global symbols
859 // contain code that falls through to other global symbols (e.g. the obvious
860 // implementation of multiple entry points). If this doesn't occur, the
861 // linker can safely perform dead code stripping. Since LLVM never generates
862 // code that does this, it is always safe to set.
Chris Lattner2d7c8142010-01-23 06:39:22 +0000863 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864
Dan Gohman4a558a32007-07-25 19:33:14 +0000865 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866}
867
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000868/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
869/// for a MachineFunction to the given output stream, in a format that the
870/// Darwin assembler can deal with.
871///
Daniel Dunbar4cb63652009-08-13 23:48:47 +0000872static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
873 TargetMachine &tm,
Chris Lattnerf4853452010-03-13 20:55:24 +0000874 MCStreamer &Streamer) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
876
Chris Lattnerae982212009-07-21 18:38:57 +0000877 if (Subtarget->isDarwin())
Chris Lattnerf4853452010-03-13 20:55:24 +0000878 return new PPCDarwinAsmPrinter(o, tm, Streamer);
879 return new PPCLinuxAsmPrinter(o, tm, Streamer);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880}
Anton Korobeynikov01c0e9f2008-08-17 13:54:28 +0000881
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000882// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000883extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000884 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000885 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
886}