blob: ada34ed9e18af0a744b58fb04b36dbe8c0459281 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmane05203f2004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmane05203f2004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukmanb604b4d2004-07-08 17:58:04 +000010// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to PowerPC assembly language. This printer is
Chris Lattner07fad1c2004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukmane05203f2004-06-21 16:55:25 +000013//
Misha Brukmanb604b4d2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman811f5c22004-06-29 17:13:26 +000016//
Misha Brukmane05203f2004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000020#include "PPC.h"
Craig Topperb25fda92012-03-17 18:46:09 +000021#include "InstPrinter/PPCInstPrinter.h"
Evan Cheng11424442011-07-26 00:24:13 +000022#include "MCTargetDesc/PPCPredicates.h"
Ulrich Weigand41789de2013-05-23 22:26:41 +000023#include "MCTargetDesc/PPCMCExpr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "PPCSubtarget.h"
25#include "PPCTargetMachine.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000026#include "PPCTargetStreamer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/ADT/MapVector.h"
28#include "llvm/ADT/SmallString.h"
29#include "llvm/ADT/StringExtras.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000030#include "llvm/Assembly/Writer.h"
Chris Lattner0ced9052004-08-16 23:25:21 +000031#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000033#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling50794832008-01-26 06:51:24 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1ff47942010-01-20 21:16:14 +000035#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000036#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/Constants.h"
39#include "llvm/IR/DerivedTypes.h"
40#include "llvm/IR/Module.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000041#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9148f362010-01-13 19:00:57 +000042#include "llvm/MC/MCContext.h"
Bill Wendlingfaec0812010-03-11 23:39:44 +000043#include "llvm/MC/MCExpr.h"
Chris Lattner686a0952010-11-14 19:53:02 +000044#include "llvm/MC/MCInst.h"
Benjamin Kramer4e629f72012-11-26 13:34:22 +000045#include "llvm/MC/MCInstBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/MC/MCSectionELF.h"
Chris Lattner6c203912009-08-10 18:15:01 +000047#include "llvm/MC/MCSectionMachO.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000048#include "llvm/MC/MCStreamer.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000049#include "llvm/MC/MCSymbol.h"
Chris Lattner686a0952010-11-14 19:53:02 +000050#include "llvm/Support/CommandLine.h"
Devang Patel6e9a9792010-08-04 22:07:50 +000051#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000052#include "llvm/Support/ELF.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000053#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000054#include "llvm/Support/MathExtras.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000055#include "llvm/Support/TargetRegistry.h"
Chris Lattnerd20699b2010-04-04 08:18:47 +000056#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000057#include "llvm/Target/Mangler.h"
58#include "llvm/Target/TargetInstrInfo.h"
59#include "llvm/Target/TargetOptions.h"
60#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner0ced9052004-08-16 23:25:21 +000061using namespace llvm;
Misha Brukmane05203f2004-06-21 16:55:25 +000062
Chris Lattner1ef9cd42006-12-19 22:59:26 +000063namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000064 class PPCAsmPrinter : public AsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000065 protected:
Ulrich Weigand2c93acd2012-11-12 19:13:24 +000066 MapVector<MCSymbol*, MCSymbol*> TOC;
Chris Lattner8597a2f2006-09-20 17:07:15 +000067 const PPCSubtarget &Subtarget;
Chris Lattner8bcfdab2010-11-15 03:42:54 +000068 uint64_t TOCLabelID;
Bill Wendlingc5437ea2009-02-24 08:30:20 +000069 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +000070 explicit PPCAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
71 : AsmPrinter(TM, Streamer),
Chris Lattner8bcfdab2010-11-15 03:42:54 +000072 Subtarget(TM.getSubtarget<PPCSubtarget>()), TOCLabelID(0) {}
Misha Brukmanb4402432005-04-21 23:30:14 +000073
Misha Brukmane05203f2004-06-21 16:55:25 +000074 virtual const char *getPassName() const {
Nate Begeman4bfceb12004-09-04 05:00:00 +000075 return "PowerPC Assembly Printer";
Misha Brukmane05203f2004-06-21 16:55:25 +000076 }
77
Bill Schmidt34627e32012-11-27 17:35:46 +000078 MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym);
Nate Begeman0ad7f812004-08-14 22:09:10 +000079
Chris Lattner73de5fb2010-01-28 01:28:58 +000080 virtual void EmitInstruction(const MachineInstr *MI);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +000081
Chris Lattner510c66f2010-11-15 03:39:06 +000082 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +000083
Chris Lattnerf7f05672006-02-01 22:38:46 +000084 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000085 unsigned AsmVariant, const char *ExtraCode,
86 raw_ostream &O);
Chris Lattner7674d902006-02-24 20:27:40 +000087 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000088 unsigned AsmVariant, const char *ExtraCode,
89 raw_ostream &O);
Nate Begeman4bfceb12004-09-04 05:00:00 +000090 };
Misha Brukmanb4402432005-04-21 23:30:14 +000091
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +000092 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky02d5f772009-10-25 06:33:48 +000093 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000094 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +000095 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
96 : PPCAsmPrinter(TM, Streamer) {}
Jim Laskey28663c72006-12-21 20:26:09 +000097
98 virtual const char *getPassName() const {
99 return "Linux PPC Assembly Printer";
100 }
101
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000102 bool doFinalization(Module &M);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000103
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000104 virtual void EmitFunctionEntryLabel();
Roman Divacky8c4b6a32012-08-28 19:06:55 +0000105
106 void EmitFunctionBodyEnd();
Jim Laskey28663c72006-12-21 20:26:09 +0000107 };
108
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000109 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
110 /// OS X
Nick Lewycky02d5f772009-10-25 06:33:48 +0000111 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +0000112 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +0000113 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
114 : PPCAsmPrinter(TM, Streamer) {}
Nate Begeman4bfceb12004-09-04 05:00:00 +0000115
116 virtual const char *getPassName() const {
117 return "Darwin PPC Assembly Printer";
118 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000119
Misha Brukmane05203f2004-06-21 16:55:25 +0000120 bool doFinalization(Module &M);
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000121 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000122
Chris Lattner32fb9562010-01-20 21:36:48 +0000123 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Misha Brukmane05203f2004-06-21 16:55:25 +0000124 };
125} // end of anonymous namespace
126
Chris Lattner510c66f2010-11-15 03:39:06 +0000127/// stripRegisterPrefix - This method strips the character prefix from a
128/// register name so that only the number is left. Used by for linux asm.
129static const char *stripRegisterPrefix(const char *RegName) {
130 switch (RegName[0]) {
131 case 'r':
132 case 'f':
133 case 'v': return RegName + 1;
134 case 'c': if (RegName[1] == 'r') return RegName + 2;
135 }
136
137 return RegName;
138}
Nate Begeman0ad7f812004-08-14 22:09:10 +0000139
Chris Lattner510c66f2010-11-15 03:39:06 +0000140void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
141 raw_ostream &O) {
142 const MachineOperand &MO = MI->getOperand(OpNo);
143
Misha Brukmane05203f2004-06-21 16:55:25 +0000144 switch (MO.getType()) {
Chris Lattner510c66f2010-11-15 03:39:06 +0000145 case MachineOperand::MO_Register: {
146 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
147 // Linux assembler (Others?) does not take register mnemonics.
148 // FIXME - What about special registers used in mfspr/mtspr?
149 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
150 O << RegName;
151 return;
152 }
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000153 case MachineOperand::MO_Immediate:
Chris Lattner510c66f2010-11-15 03:39:06 +0000154 O << MO.getImm();
155 return;
Misha Brukman8d75aa42004-07-21 20:11:11 +0000156
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000157 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000158 O << *MO.getMBB()->getSymbol();
Misha Brukmane05203f2004-06-21 16:55:25 +0000159 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000160 case MachineOperand::MO_JumpTableIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000161 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattnera5bb3702007-12-30 23:10:15 +0000162 << '_' << MO.getIndex();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000163 // FIXME: PIC relocation model
164 return;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000165 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000166 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattnera5bb3702007-12-30 23:10:15 +0000167 << '_' << MO.getIndex();
Misha Brukmane05203f2004-06-21 16:55:25 +0000168 return;
Bob Wilsonf84f7102009-11-04 21:31:18 +0000169 case MachineOperand::MO_BlockAddress:
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000170 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilsonf84f7102009-11-04 21:31:18 +0000171 return;
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000172 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner57575112005-12-16 00:22:14 +0000173 // Computing the address of an external symbol, not calling it.
Chris Lattner3d571ff2010-01-16 02:09:06 +0000174 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattner1ff47942010-01-20 21:16:14 +0000175 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner3d571ff2010-01-16 02:09:06 +0000176 return;
Chris Lattner57575112005-12-16 00:22:14 +0000177 }
Chris Lattner1ff47942010-01-20 21:16:14 +0000178
Chris Lattner082f4842010-02-03 06:18:30 +0000179 MCSymbol *NLPSym =
Chris Lattner3d571ff2010-01-16 02:09:06 +0000180 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
181 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000182 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner1ff47942010-01-20 21:16:14 +0000183 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000184 if (StubSym.getPointer() == 0)
185 StubSym = MachineModuleInfoImpl::
186 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattner1ff47942010-01-20 21:16:14 +0000187
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000188 O << *NLPSym;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000189 return;
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000190 }
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000191 case MachineOperand::MO_GlobalAddress: {
Chris Lattner57575112005-12-16 00:22:14 +0000192 // Computing the address of a global symbol, not calling it.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000193 const GlobalValue *GV = MO.getGlobal();
Chris Lattner1e26ec32010-01-16 02:00:23 +0000194 MCSymbol *SymToPrint;
Misha Brukman7dba17d2004-07-23 16:08:20 +0000195
Nate Begeman844186b2004-10-17 23:01:34 +0000196 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000197 if (TM.getRelocationModel() != Reloc::Static &&
198 (GV->isDeclaration() || GV->isWeakForLinker())) {
199 if (!GV->hasHiddenVisibility()) {
Chris Lattner555ceabe2010-01-16 18:37:32 +0000200 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000201 MachineModuleInfoImpl::StubValueTy &StubSym =
202 MMI->getObjFileInfo<MachineModuleInfoMachO>()
203 .getGVStubEntry(SymToPrint);
204 if (StubSym.getPointer() == 0)
205 StubSym = MachineModuleInfoImpl::
Rafael Espindola79858aa2013-10-29 17:07:16 +0000206 StubValueTy(getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000207 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
208 GV->hasAvailableExternallyLinkage()) {
Chris Lattner555ceabe2010-01-16 18:37:32 +0000209 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner1ff47942010-01-20 21:16:14 +0000210
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000211 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner1ff47942010-01-20 21:16:14 +0000212 MMI->getObjFileInfo<MachineModuleInfoMachO>().
213 getHiddenGVStubEntry(SymToPrint);
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000214 if (StubSym.getPointer() == 0)
215 StubSym = MachineModuleInfoImpl::
Rafael Espindola79858aa2013-10-29 17:07:16 +0000216 StubValueTy(getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000217 } else {
Rafael Espindola79858aa2013-10-29 17:07:16 +0000218 SymToPrint = getSymbol(GV);
Chris Lattner57575112005-12-16 00:22:14 +0000219 }
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000220 } else {
Rafael Espindola79858aa2013-10-29 17:07:16 +0000221 SymToPrint = getSymbol(GV);
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000222 }
Chris Lattner1e26ec32010-01-16 02:00:23 +0000223
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000224 O << *SymToPrint;
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000225
Chris Lattnerf33c7fc2010-04-03 22:28:33 +0000226 printOffset(MO.getOffset(), O);
Misha Brukmane05203f2004-06-21 16:55:25 +0000227 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000228 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000229
Misha Brukmane05203f2004-06-21 16:55:25 +0000230 default:
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000231 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukmana2737582004-06-25 15:11:34 +0000232 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000233 }
234}
235
Chris Lattner1bad2542006-02-23 19:31:10 +0000236/// PrintAsmOperand - Print out an operand for an inline asm expression.
237///
238bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000239 unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +0000240 const char *ExtraCode, raw_ostream &O) {
Chris Lattner1bad2542006-02-23 19:31:10 +0000241 // Does this asm operand have a single letter operand modifier?
242 if (ExtraCode && ExtraCode[0]) {
243 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000244
Chris Lattner1bad2542006-02-23 19:31:10 +0000245 switch (ExtraCode[0]) {
Jack Carter5e69cff2012-06-26 13:49:27 +0000246 default:
247 // See if this is a generic print operand
248 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
Chris Lattner48518542007-01-25 02:52:50 +0000249 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner510c66f2010-11-15 03:39:06 +0000250 break; // PPC never has a prefix.
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000251 case 'L': // Write second word of DImode reference.
Chris Lattner1bad2542006-02-23 19:31:10 +0000252 // Verify that this operand has two consecutive registers.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000253 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattner1bad2542006-02-23 19:31:10 +0000254 OpNo+1 == MI->getNumOperands() ||
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000255 !MI->getOperand(OpNo+1).isReg())
Chris Lattner1bad2542006-02-23 19:31:10 +0000256 return true;
257 ++OpNo; // Return the high-part.
258 break;
Chris Lattnercb35c612007-04-24 22:51:03 +0000259 case 'I':
260 // Write 'i' if an integer constant, otherwise nothing. Used to print
261 // addi vs add, etc.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000262 if (MI->getOperand(OpNo).isImm())
Chris Lattnercb35c612007-04-24 22:51:03 +0000263 O << "i";
264 return false;
Chris Lattner1bad2542006-02-23 19:31:10 +0000265 }
266 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000267
Chris Lattner76c564b2010-04-04 04:47:45 +0000268 printOperand(MI, OpNo, O);
Chris Lattner1bad2542006-02-23 19:31:10 +0000269 return false;
270}
271
Dale Johannesen4a50e682009-08-18 00:18:39 +0000272// At the moment, all inline asm memory operands are a single register.
273// In any case, the output of this routine should always be just one
274// assembler operand.
275
Chris Lattner7674d902006-02-24 20:27:40 +0000276bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000277 unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +0000278 const char *ExtraCode,
279 raw_ostream &O) {
Hal Finkel4f24c622012-11-05 18:18:42 +0000280 if (ExtraCode && ExtraCode[0]) {
281 if (ExtraCode[1] != 0) return true; // Unknown modifier.
282
283 switch (ExtraCode[0]) {
284 default: return true; // Unknown modifier.
285 case 'y': // A memory reference for an X-form instruction
286 {
287 const char *RegName = "r0";
288 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
289 O << RegName << ", ";
290 printOperand(MI, OpNo, O);
291 return false;
292 }
293 }
294 }
295
Chris Lattner510c66f2010-11-15 03:39:06 +0000296 assert(MI->getOperand(OpNo).isReg());
Dale Johannesenf582ac72009-08-26 18:10:32 +0000297 O << "0(";
Chris Lattner76c564b2010-04-04 04:47:45 +0000298 printOperand(MI, OpNo, O);
Dale Johannesenf582ac72009-08-26 18:10:32 +0000299 O << ")";
Chris Lattner7674d902006-02-24 20:27:40 +0000300 return false;
301}
Chris Lattner1bad2542006-02-23 19:31:10 +0000302
Chris Lattner6be72602006-11-04 05:27:39 +0000303
Bill Schmidt34627e32012-11-27 17:35:46 +0000304/// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
305/// exists for it. If not, create one. Then return a symbol that references
306/// the TOC entry.
307MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) {
308
309 MCSymbol *&TOCEntry = TOC[Sym];
310
311 // To avoid name clash check if the name already exists.
312 while (TOCEntry == 0) {
313 if (OutContext.LookupSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
314 "C" + Twine(TOCLabelID++)) == 0) {
315 TOCEntry = GetTempSymbol("C", TOCLabelID);
316 }
317 }
318
319 return TOCEntry;
320}
321
322
Chris Lattner73de5fb2010-01-28 01:28:58 +0000323/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Nate Begeman0ad7f812004-08-14 22:09:10 +0000324/// the current output stream.
Misha Brukmane05203f2004-06-21 16:55:25 +0000325///
Chris Lattner73de5fb2010-01-28 01:28:58 +0000326void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner510c66f2010-11-15 03:39:06 +0000327 MCInst TmpInst;
328
329 // Lower multi-instruction pseudo operations.
330 switch (MI->getOpcode()) {
331 default: break;
David Blaikieb735b4d2013-06-16 20:34:27 +0000332 case TargetOpcode::DBG_VALUE:
333 llvm_unreachable("Should be handled target independently");
Chris Lattner510c66f2010-11-15 03:39:06 +0000334 case PPC::MovePCtoLR:
335 case PPC::MovePCtoLR8: {
336 // Transform %LR = MovePCtoLR
337 // Into this, where the label is the PIC base:
338 // bl L1$pb
339 // L1$pb:
340 MCSymbol *PICBase = MF->getPICBaseSymbol();
Chris Lattner2f9f63a2010-11-14 22:03:15 +0000341
Chris Lattner510c66f2010-11-15 03:39:06 +0000342 // Emit the 'bl'.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000343 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000344 // FIXME: We would like an efficient form for this, so we don't have to do
345 // a lot of extra uniquing.
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000346 .addExpr(MCSymbolRefExpr::Create(PICBase, OutContext)));
Chris Lattner510c66f2010-11-15 03:39:06 +0000347
348 // Emit the label.
349 OutStreamer.EmitLabel(PICBase);
350 return;
351 }
Roman Divackyace47072012-08-24 16:26:02 +0000352 case PPC::LDtocJTI:
353 case PPC::LDtocCPT:
Chris Lattner510c66f2010-11-15 03:39:06 +0000354 case PPC::LDtoc: {
355 // Transform %X3 = LDtoc <ga:@min1>, %X2
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000356 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Roman Divackyace47072012-08-24 16:26:02 +0000357
Chris Lattner510c66f2010-11-15 03:39:06 +0000358 // Change the opcode to LD, and the global address operand to be a
359 // reference to the TOC entry we will synthesize later.
360 TmpInst.setOpcode(PPC::LD);
361 const MachineOperand &MO = MI->getOperand(1);
Roman Divackyace47072012-08-24 16:26:02 +0000362
363 // Map symbol -> label of TOC entry
364 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
365 MCSymbol *MOSymbol = 0;
366 if (MO.isGlobal())
Rafael Espindola79858aa2013-10-29 17:07:16 +0000367 MOSymbol = getSymbol(MO.getGlobal());
Roman Divackyace47072012-08-24 16:26:02 +0000368 else if (MO.isCPI())
369 MOSymbol = GetCPISymbol(MO.getIndex());
370 else if (MO.isJTI())
371 MOSymbol = GetJTISymbol(MO.getIndex());
Bill Schmidt34627e32012-11-27 17:35:46 +0000372
373 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
Roman Divacky0be33592012-09-18 17:10:37 +0000374
Chris Lattner510c66f2010-11-15 03:39:06 +0000375 const MCExpr *Exp =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000376 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC,
Chris Lattner510c66f2010-11-15 03:39:06 +0000377 OutContext);
378 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
Chris Lattner686a0952010-11-14 19:53:02 +0000379 OutStreamer.EmitInstruction(TmpInst);
380 return;
381 }
Chris Lattner510c66f2010-11-15 03:39:06 +0000382
Bill Schmidt34627e32012-11-27 17:35:46 +0000383 case PPC::ADDIStocHA: {
384 // Transform %Xd = ADDIStocHA %X2, <ga:@sym>
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000385 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Bill Schmidt34627e32012-11-27 17:35:46 +0000386
387 // Change the opcode to ADDIS8. If the global address is external,
388 // has common linkage, is a function address, or is a jump table
389 // address, then generate a TOC entry and reference that. Otherwise
390 // reference the symbol directly.
391 TmpInst.setOpcode(PPC::ADDIS8);
392 const MachineOperand &MO = MI->getOperand(2);
393 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI()) &&
394 "Invalid operand for ADDIStocHA!");
395 MCSymbol *MOSymbol = 0;
396 bool IsExternal = false;
397 bool IsFunction = false;
398 bool IsCommon = false;
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000399 bool IsAvailExt = false;
Bill Schmidt34627e32012-11-27 17:35:46 +0000400
401 if (MO.isGlobal()) {
402 const GlobalValue *GValue = MO.getGlobal();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000403 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
404 const GlobalValue *RealGValue = GAlias ?
405 GAlias->resolveAliasedGlobal(false) : GValue;
Rafael Espindola79858aa2013-10-29 17:07:16 +0000406 MOSymbol = getSymbol(RealGValue);
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000407 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
Bill Schmidt34627e32012-11-27 17:35:46 +0000408 IsExternal = GVar && !GVar->hasInitializer();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000409 IsCommon = GVar && RealGValue->hasCommonLinkage();
Bill Schmidt34627e32012-11-27 17:35:46 +0000410 IsFunction = !GVar;
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000411 IsAvailExt = GVar && RealGValue->hasAvailableExternallyLinkage();
Bill Schmidt34627e32012-11-27 17:35:46 +0000412 } else if (MO.isCPI())
413 MOSymbol = GetCPISymbol(MO.getIndex());
414 else if (MO.isJTI())
415 MOSymbol = GetJTISymbol(MO.getIndex());
416
Bill Schmidtbb381d72013-09-17 20:03:25 +0000417 if (IsExternal || IsFunction || IsCommon || IsAvailExt || MO.isJTI() ||
418 TM.getCodeModel() == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +0000419 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
420
421 const MCExpr *Exp =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000422 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_HA,
Bill Schmidt34627e32012-11-27 17:35:46 +0000423 OutContext);
424 TmpInst.getOperand(2) = MCOperand::CreateExpr(Exp);
425 OutStreamer.EmitInstruction(TmpInst);
426 return;
427 }
428 case PPC::LDtocL: {
429 // Transform %Xd = LDtocL <ga:@sym>, %Xs
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000430 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Bill Schmidt34627e32012-11-27 17:35:46 +0000431
Ulrich Weigand4a083882013-03-26 10:55:45 +0000432 // Change the opcode to LD. If the global address is external, has
Bill Schmidt34627e32012-11-27 17:35:46 +0000433 // common linkage, or is a jump table address, then reference the
434 // associated TOC entry. Otherwise reference the symbol directly.
Ulrich Weigand4a083882013-03-26 10:55:45 +0000435 TmpInst.setOpcode(PPC::LD);
Bill Schmidt34627e32012-11-27 17:35:46 +0000436 const MachineOperand &MO = MI->getOperand(1);
Bill Schmidt27917782013-02-21 17:12:27 +0000437 assert((MO.isGlobal() || MO.isJTI() || MO.isCPI()) &&
438 "Invalid operand for LDtocL!");
Bill Schmidt34627e32012-11-27 17:35:46 +0000439 MCSymbol *MOSymbol = 0;
440
441 if (MO.isJTI())
442 MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex()));
Bill Schmidtbb381d72013-09-17 20:03:25 +0000443 else if (MO.isCPI()) {
Bill Schmidt27917782013-02-21 17:12:27 +0000444 MOSymbol = GetCPISymbol(MO.getIndex());
Bill Schmidtbb381d72013-09-17 20:03:25 +0000445 if (TM.getCodeModel() == CodeModel::Large)
446 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
447 }
Bill Schmidt27917782013-02-21 17:12:27 +0000448 else if (MO.isGlobal()) {
Bill Schmidt34627e32012-11-27 17:35:46 +0000449 const GlobalValue *GValue = MO.getGlobal();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000450 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
451 const GlobalValue *RealGValue = GAlias ?
452 GAlias->resolveAliasedGlobal(false) : GValue;
Rafael Espindola79858aa2013-10-29 17:07:16 +0000453 MOSymbol = getSymbol(RealGValue);
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000454 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
Bill Schmidt34627e32012-11-27 17:35:46 +0000455
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000456 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
Bill Schmidtbb381d72013-09-17 20:03:25 +0000457 RealGValue->hasAvailableExternallyLinkage() ||
458 TM.getCodeModel() == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +0000459 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
460 }
461
462 const MCExpr *Exp =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000463 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,
Bill Schmidt34627e32012-11-27 17:35:46 +0000464 OutContext);
465 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
466 OutStreamer.EmitInstruction(TmpInst);
467 return;
468 }
469 case PPC::ADDItocL: {
470 // Transform %Xd = ADDItocL %Xs, <ga:@sym>
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000471 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Bill Schmidt34627e32012-11-27 17:35:46 +0000472
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +0000473 // Change the opcode to ADDI8. If the global address is external, then
Bill Schmidt34627e32012-11-27 17:35:46 +0000474 // generate a TOC entry and reference that. Otherwise reference the
475 // symbol directly.
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +0000476 TmpInst.setOpcode(PPC::ADDI8);
Bill Schmidt34627e32012-11-27 17:35:46 +0000477 const MachineOperand &MO = MI->getOperand(2);
478 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL");
479 MCSymbol *MOSymbol = 0;
480 bool IsExternal = false;
481 bool IsFunction = false;
482
483 if (MO.isGlobal()) {
484 const GlobalValue *GValue = MO.getGlobal();
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000485 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
486 const GlobalValue *RealGValue = GAlias ?
487 GAlias->resolveAliasedGlobal(false) : GValue;
Rafael Espindola79858aa2013-10-29 17:07:16 +0000488 MOSymbol = getSymbol(RealGValue);
Bill Schmidt9b1e3e22013-01-07 19:29:18 +0000489 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
Bill Schmidt34627e32012-11-27 17:35:46 +0000490 IsExternal = GVar && !GVar->hasInitializer();
491 IsFunction = !GVar;
492 } else if (MO.isCPI())
493 MOSymbol = GetCPISymbol(MO.getIndex());
494
Bill Schmidtbb381d72013-09-17 20:03:25 +0000495 if (IsFunction || IsExternal || TM.getCodeModel() == CodeModel::Large)
Bill Schmidt34627e32012-11-27 17:35:46 +0000496 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
497
498 const MCExpr *Exp =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000499 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,
Bill Schmidt34627e32012-11-27 17:35:46 +0000500 OutContext);
501 TmpInst.getOperand(2) = MCOperand::CreateExpr(Exp);
502 OutStreamer.EmitInstruction(TmpInst);
503 return;
504 }
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000505 case PPC::ADDISgotTprelHA: {
506 // Transform: %Xd = ADDISgotTprelHA %X2, <ga:@sym>
507 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
508 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
509 const MachineOperand &MO = MI->getOperand(2);
510 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000511 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000512 const MCExpr *SymGotTprel =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000513 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA,
Bill Schmidt9f0b4ec2012-12-14 17:02:38 +0000514 OutContext);
515 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
516 .addReg(MI->getOperand(0).getReg())
517 .addReg(PPC::X2)
518 .addExpr(SymGotTprel));
519 return;
520 }
521 case PPC::LDgotTprelL: {
522 // Transform %Xd = LDgotTprelL <ga:@sym>, %Xs
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000523 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000524
Ulrich Weigand4a083882013-03-26 10:55:45 +0000525 // Change the opcode to LD.
526 TmpInst.setOpcode(PPC::LD);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000527 const MachineOperand &MO = MI->getOperand(1);
528 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000529 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000530 const MCExpr *Exp =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000531 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO,
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000532 OutContext);
533 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
534 OutStreamer.EmitInstruction(TmpInst);
535 return;
536 }
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000537 case PPC::ADDIStlsgdHA: {
538 // Transform: %Xd = ADDIStlsgdHA %X2, <ga:@sym>
539 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
540 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
541 const MachineOperand &MO = MI->getOperand(2);
542 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000543 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000544 const MCExpr *SymGotTlsGD =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000545 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA,
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000546 OutContext);
547 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
548 .addReg(MI->getOperand(0).getReg())
549 .addReg(PPC::X2)
550 .addExpr(SymGotTlsGD));
551 return;
552 }
553 case PPC::ADDItlsgdL: {
554 // Transform: %Xd = ADDItlsgdL %Xs, <ga:@sym>
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +0000555 // Into: %Xd = ADDI8 %Xs, sym@got@tlsgd@l
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000556 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
557 const MachineOperand &MO = MI->getOperand(2);
558 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000559 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000560 const MCExpr *SymGotTlsGD =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000561 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO,
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000562 OutContext);
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +0000563 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000564 .addReg(MI->getOperand(0).getReg())
565 .addReg(MI->getOperand(1).getReg())
566 .addExpr(SymGotTlsGD));
567 return;
568 }
569 case PPC::GETtlsADDR: {
570 // Transform: %X3 = GETtlsADDR %X3, <ga:@sym>
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000571 // Into: BL8_NOP_TLS __tls_get_addr(sym@tlsgd)
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000572 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
573
574 StringRef Name = "__tls_get_addr";
575 MCSymbol *TlsGetAddr = OutContext.GetOrCreateSymbol(Name);
576 const MCSymbolRefExpr *TlsRef =
577 MCSymbolRefExpr::Create(TlsGetAddr, MCSymbolRefExpr::VK_None, OutContext);
578 const MachineOperand &MO = MI->getOperand(2);
579 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000580 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000581 const MCExpr *SymVar =
Ulrich Weigand52cf8e42013-07-09 16:41:09 +0000582 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TLSGD,
583 OutContext);
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000584 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL8_NOP_TLS)
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000585 .addExpr(TlsRef)
586 .addExpr(SymVar));
587 return;
588 }
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000589 case PPC::ADDIStlsldHA: {
590 // Transform: %Xd = ADDIStlsldHA %X2, <ga:@sym>
591 // Into: %Xd = ADDIS8 %X2, sym@got@tlsld@ha
592 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
593 const MachineOperand &MO = MI->getOperand(2);
594 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000595 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000596 const MCExpr *SymGotTlsLD =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000597 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA,
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000598 OutContext);
599 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
600 .addReg(MI->getOperand(0).getReg())
601 .addReg(PPC::X2)
602 .addExpr(SymGotTlsLD));
603 return;
604 }
605 case PPC::ADDItlsldL: {
606 // Transform: %Xd = ADDItlsldL %Xs, <ga:@sym>
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +0000607 // Into: %Xd = ADDI8 %Xs, sym@got@tlsld@l
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000608 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
609 const MachineOperand &MO = MI->getOperand(2);
610 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000611 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000612 const MCExpr *SymGotTlsLD =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000613 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO,
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000614 OutContext);
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +0000615 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000616 .addReg(MI->getOperand(0).getReg())
617 .addReg(MI->getOperand(1).getReg())
618 .addExpr(SymGotTlsLD));
619 return;
620 }
621 case PPC::GETtlsldADDR: {
622 // Transform: %X3 = GETtlsldADDR %X3, <ga:@sym>
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000623 // Into: BL8_NOP_TLS __tls_get_addr(sym@tlsld)
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000624 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
625
626 StringRef Name = "__tls_get_addr";
627 MCSymbol *TlsGetAddr = OutContext.GetOrCreateSymbol(Name);
628 const MCSymbolRefExpr *TlsRef =
629 MCSymbolRefExpr::Create(TlsGetAddr, MCSymbolRefExpr::VK_None, OutContext);
630 const MachineOperand &MO = MI->getOperand(2);
631 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000632 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000633 const MCExpr *SymVar =
Ulrich Weigand52cf8e42013-07-09 16:41:09 +0000634 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TLSLD,
635 OutContext);
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000636 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL8_NOP_TLS)
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000637 .addExpr(TlsRef)
638 .addExpr(SymVar));
639 return;
640 }
641 case PPC::ADDISdtprelHA: {
642 // Transform: %Xd = ADDISdtprelHA %X3, <ga:@sym>
643 // Into: %Xd = ADDIS8 %X3, sym@dtprel@ha
644 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
645 const MachineOperand &MO = MI->getOperand(2);
646 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000647 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000648 const MCExpr *SymDtprel =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000649 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_HA,
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000650 OutContext);
651 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
652 .addReg(MI->getOperand(0).getReg())
653 .addReg(PPC::X3)
654 .addExpr(SymDtprel));
655 return;
656 }
657 case PPC::ADDIdtprelL: {
658 // Transform: %Xd = ADDIdtprelL %Xs, <ga:@sym>
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +0000659 // Into: %Xd = ADDI8 %Xs, sym@dtprel@l
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000660 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
661 const MachineOperand &MO = MI->getOperand(2);
662 const GlobalValue *GValue = MO.getGlobal();
Rafael Espindola79858aa2013-10-29 17:07:16 +0000663 MCSymbol *MOSymbol = getSymbol(GValue);
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000664 const MCExpr *SymDtprel =
Ulrich Weigandd51c09f2013-06-21 14:42:20 +0000665 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_LO,
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000666 OutContext);
Ulrich Weigand35f9fdf2013-03-26 10:55:20 +0000667 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000668 .addReg(MI->getOperand(0).getReg())
669 .addReg(MI->getOperand(1).getReg())
670 .addExpr(SymDtprel));
671 return;
672 }
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000673 case PPC::MFOCRF:
674 case PPC::MFOCRF8:
675 if (!Subtarget.hasMFOCRF()) {
676 // Transform: %R3 = MFOCRF %CR7
677 // Into: %R3 = MFCR ;; cr7
678 unsigned NewOpcode =
679 MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8;
680 OutStreamer.AddComment(PPCInstPrinter::
681 getRegisterName(MI->getOperand(1).getReg()));
682 OutStreamer.EmitInstruction(MCInstBuilder(NewOpcode)
683 .addReg(MI->getOperand(0).getReg()));
684 return;
685 }
686 break;
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000687 case PPC::MTOCRF:
688 case PPC::MTOCRF8:
689 if (!Subtarget.hasMFOCRF()) {
690 // Transform: %CR7 = MTOCRF %R3
691 // Into: MTCRF mask, %R3 ;; cr7
692 unsigned NewOpcode =
693 MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8;
694 unsigned Mask = 0x80 >> OutContext.getRegisterInfo()
695 ->getEncodingValue(MI->getOperand(0).getReg());
696 OutStreamer.AddComment(PPCInstPrinter::
697 getRegisterName(MI->getOperand(0).getReg()));
698 OutStreamer.EmitInstruction(MCInstBuilder(NewOpcode)
699 .addImm(Mask)
700 .addReg(MI->getOperand(1).getReg()));
701 return;
702 }
703 break;
Hal Finkel6fa56972011-10-17 04:03:49 +0000704 case PPC::SYNC:
705 // In Book E sync is called msync, handle this special case here...
706 if (Subtarget.isBookE()) {
707 OutStreamer.EmitRawText(StringRef("\tmsync"));
708 return;
709 }
Bill Schmidt48fc20a2013-07-01 20:52:27 +0000710 break;
711 case PPC::LD:
712 case PPC::STD:
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000713 case PPC::LWA_32:
Bill Schmidt48fc20a2013-07-01 20:52:27 +0000714 case PPC::LWA: {
715 // Verify alignment is legal, so we don't create relocations
716 // that can't be supported.
717 // FIXME: This test is currently disabled for Darwin. The test
718 // suite shows a handful of test cases that fail this check for
719 // Darwin. Those need to be investigated before this sanity test
720 // can be enabled for those subtargets.
721 if (!Subtarget.isDarwin()) {
722 unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1;
723 const MachineOperand &MO = MI->getOperand(OpNum);
724 if (MO.isGlobal() && MO.getGlobal()->getAlignment() < 4)
725 llvm_unreachable("Global must be word-aligned for LD, STD, LWA!");
726 }
727 // Now process the instruction normally.
728 break;
729 }
Dale Johannesenbc41cfa2010-04-26 20:05:01 +0000730 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000731
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000732 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Chris Lattner510c66f2010-11-15 03:39:06 +0000733 OutStreamer.EmitInstruction(TmpInst);
Misha Brukmane05203f2004-06-21 16:55:25 +0000734}
735
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000736void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
737 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
738 return AsmPrinter::EmitFunctionEntryLabel();
739
740 // Emit an official procedure descriptor.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000741 MCSectionSubPair Current = OutStreamer.getCurrentSection();
Roman Divacky8fe40cd2012-02-27 20:20:47 +0000742 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
743 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
744 SectionKind::getReadOnly());
745 OutStreamer.SwitchSection(Section);
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000746 OutStreamer.EmitLabel(CurrentFnSym);
Roman Divacky8fe40cd2012-02-27 20:20:47 +0000747 OutStreamer.EmitValueToAlignment(8);
748 MCSymbol *Symbol1 =
749 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000750 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
751 // entry point.
Roman Divacky8fe40cd2012-02-27 20:20:47 +0000752 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
Eric Christophere3ab3d02013-01-09 01:57:54 +0000753 8 /*size*/);
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000754 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC."));
755 // Generates a R_PPC64_TOC relocation for TOC base insertion.
756 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2,
Ulrich Weigand68e2e1b2013-06-20 22:39:42 +0000757 MCSymbolRefExpr::VK_PPC_TOCBASE, OutContext),
Eric Christophere3ab3d02013-01-09 01:57:54 +0000758 8/*size*/);
Roman Divackyd4f6f422012-09-18 16:55:29 +0000759 // Emit a null environment pointer.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000760 OutStreamer.EmitIntValue(0, 8 /* size */);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000761 OutStreamer.SwitchSection(Current.first, Current.second);
Hal Finkelad4d9f52012-02-22 21:11:47 +0000762
763 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
764 ".L." + Twine(CurrentFnSym->getName()));
765 OutStreamer.EmitLabel(RealFnSym);
766 CurrentFnSymForSize = RealFnSym;
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000767}
768
769
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000770bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000771 const DataLayout *TD = TM.getDataLayout();
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000772
Chandler Carruth5da3f052012-11-01 09:14:31 +0000773 bool isPPC64 = TD->getPointerSizeInBits() == 64;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000774
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000775 PPCTargetStreamer &TS =
776 static_cast<PPCTargetStreamer &>(OutStreamer.getTargetStreamer());
777
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000778 if (isPPC64 && !TOC.empty()) {
Roman Divacky34d4b962012-02-28 18:15:25 +0000779 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
780 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
781 SectionKind::getReadOnly());
782 OutStreamer.SwitchSection(Section);
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000783
Ulrich Weigand2c93acd2012-11-12 19:13:24 +0000784 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
Chris Lattner3d571ff2010-01-16 02:09:06 +0000785 E = TOC.end(); I != E; ++I) {
Chris Lattner5e596182010-04-04 07:05:53 +0000786 OutStreamer.EmitLabel(I->second);
Adhemerval Zanellaef206f12012-10-15 15:43:14 +0000787 MCSymbol *S = OutContext.GetOrCreateSymbol(I->first->getName());
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000788 TS.emitTCEntry(*S);
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000789 }
790 }
791
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000792 MachineModuleInfoELF &MMIELF =
793 MMI->getObjFileInfo<MachineModuleInfoELF>();
794
795 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
796 if (!Stubs.empty()) {
797 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
798 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
799 // L_foo$stub:
800 OutStreamer.EmitLabel(Stubs[i].first);
801 // .long _foo
802 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Stubs[i].second.getPointer(),
803 OutContext),
Rafael Espindola64e1af82013-07-02 15:49:13 +0000804 isPPC64 ? 8 : 4/*size*/);
Adhemerval Zanella1ae22482013-01-09 17:08:15 +0000805 }
806
807 Stubs.clear();
808 OutStreamer.AddBlankLine();
809 }
810
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000811 return AsmPrinter::doFinalization(M);
812}
Jim Laskey28663c72006-12-21 20:26:09 +0000813
Roman Divacky8c4b6a32012-08-28 19:06:55 +0000814/// EmitFunctionBodyEnd - Print the traceback table before the .size
815/// directive.
816///
817void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
818 // Only the 64-bit target requires a traceback table. For now,
819 // we only emit the word of zeroes that GDB requires to find
Hal Finkel1859d262012-08-29 20:22:24 +0000820 // the end of the function, and zeroes for the eight-byte
821 // mandatory fields.
822 // FIXME: We should fill in the eight-byte mandatory fields as described in
823 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
824 // currently make use of these fields).
825 if (Subtarget.isPPC64()) {
Roman Divacky8c4b6a32012-08-28 19:06:55 +0000826 OutStreamer.EmitIntValue(0, 4/*size*/);
Hal Finkel1859d262012-08-29 20:22:24 +0000827 OutStreamer.EmitIntValue(0, 8/*size*/);
828 }
Roman Divacky8c4b6a32012-08-28 19:06:55 +0000829}
830
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000831void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmanbdc24ad2008-03-25 21:45:14 +0000832 static const char *const CPUDirectives[] = {
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +0000833 "",
Jim Laskey59e7a772006-12-12 20:57:08 +0000834 "ppc",
Hal Finkel6fa56972011-10-17 04:03:49 +0000835 "ppc440",
Jim Laskey59e7a772006-12-12 20:57:08 +0000836 "ppc601",
837 "ppc602",
838 "ppc603",
839 "ppc7400",
840 "ppc750",
841 "ppc970",
Hal Finkel9f9f8922012-04-01 19:22:40 +0000842 "ppcA2",
Hal Finkel742b5352012-08-28 16:12:39 +0000843 "ppce500mc",
844 "ppce5500",
NAKAMURA Takumi3d591ae02013-02-04 00:47:33 +0000845 "power3",
846 "power4",
847 "power5",
848 "power5x",
Hal Finkelf2b9c382012-06-11 15:43:08 +0000849 "power6",
NAKAMURA Takumi3d591ae02013-02-04 00:47:33 +0000850 "power6x",
Hal Finkelf2b9c382012-06-11 15:43:08 +0000851 "power7",
Bill Schmidt0a9170d2013-07-26 01:35:43 +0000852 "ppc64",
853 "ppc64le"
Jim Laskey59e7a772006-12-12 20:57:08 +0000854 };
855
856 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelbfd3d082012-06-11 19:57:01 +0000857 if (Subtarget.hasMFOCRF() && Directive < PPC::DIR_970)
Jim Laskey59e7a772006-12-12 20:57:08 +0000858 Directive = PPC::DIR_970;
859 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
860 Directive = PPC::DIR_7400;
Eli Friedmanc1870b262011-12-01 01:43:47 +0000861 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
Jim Laskey59e7a772006-12-12 20:57:08 +0000862 Directive = PPC::DIR_64;
863 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Chris Lattneraac9fa72010-11-15 08:49:58 +0000864
865 // FIXME: This is a total hack, finish mc'izing the PPC backend.
NAKAMURA Takumi80159432013-02-04 00:47:38 +0000866 if (OutStreamer.hasRawTextSupport()) {
Craig Topper58713212013-07-15 04:27:47 +0000867 assert(Directive < array_lengthof(CPUDirectives) &&
NAKAMURA Takumi80159432013-02-04 00:47:38 +0000868 "CPUDirectives[] might not be up-to-date!");
Chris Lattneraac9fa72010-11-15 08:49:58 +0000869 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
NAKAMURA Takumi80159432013-02-04 00:47:38 +0000870 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000871
Jim Laskeyec05b042006-11-28 18:21:52 +0000872 // Prime text sections so they are adjacent. This reduces the likelihood a
873 // large data or debug section causes a branch to exceed 16M limit.
Dan Gohman53d4a082010-04-17 16:44:48 +0000874 const TargetLoweringObjectFileMachO &TLOFMacho =
875 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000876 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskeyec05b042006-11-28 18:21:52 +0000877 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000878 OutStreamer.SwitchSection(
Chris Lattner433d4062010-04-08 20:40:11 +0000879 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000880 MCSectionMachO::S_SYMBOL_STUBS |
881 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
882 32, SectionKind::getText()));
Jim Laskeyec05b042006-11-28 18:21:52 +0000883 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000884 OutStreamer.SwitchSection(
Chris Lattner433d4062010-04-08 20:40:11 +0000885 OutContext.getMachOSection("__TEXT","__symbol_stub1",
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000886 MCSectionMachO::S_SYMBOL_STUBS |
887 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
888 16, SectionKind::getText()));
Jim Laskeyec05b042006-11-28 18:21:52 +0000889 }
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000890 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman15527112005-07-21 01:25:49 +0000891}
892
Chris Lattner794b2f12010-04-04 07:12:28 +0000893static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner32fb9562010-01-20 21:36:48 +0000894 // Remove $stub suffix, add $lazy_ptr.
Benjamin Kramer69dc3fe2012-11-24 13:18:11 +0000895 StringRef NoStub = Sym->getName().substr(0, Sym->getName().size()-5);
896 return Ctx.GetOrCreateSymbol(NoStub + "$lazy_ptr");
Chris Lattner32fb9562010-01-20 21:36:48 +0000897}
898
Chris Lattner794b2f12010-04-04 07:12:28 +0000899static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner32fb9562010-01-20 21:36:48 +0000900 // Add $tmp suffix to $stub, yielding $stub$tmp.
Benjamin Kramer69dc3fe2012-11-24 13:18:11 +0000901 return Ctx.GetOrCreateSymbol(Sym->getName() + "$tmp");
Chris Lattner32fb9562010-01-20 21:36:48 +0000902}
903
904void PPCDarwinAsmPrinter::
905EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chandler Carruth5da3f052012-11-01 09:14:31 +0000906 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000907 bool isDarwin = Subtarget.isDarwin();
Chris Lattner2f7c2792010-01-20 21:19:44 +0000908
Dan Gohman53d4a082010-04-17 16:44:48 +0000909 const TargetLoweringObjectFileMachO &TLOFMacho =
910 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2f7c2792010-01-20 21:19:44 +0000911
912 // .lazy_symbol_pointer
913 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattnercb307a272009-08-10 01:39:42 +0000914
Misha Brukmand4ac8182004-07-16 20:29:04 +0000915 // Output stubs for dynamically-linked functions
Chris Lattner2f7c2792010-01-20 21:19:44 +0000916 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner661710c2009-08-03 22:52:21 +0000917 const MCSection *StubSection =
Chris Lattner433d4062010-04-08 20:40:11 +0000918 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
919 MCSectionMachO::S_SYMBOL_STUBS |
920 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
921 32, SectionKind::getText());
Chris Lattner32fb9562010-01-20 21:36:48 +0000922 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000923 OutStreamer.SwitchSection(StubSection);
Chris Lattner1df08392006-06-27 01:02:25 +0000924 EmitAlignment(4);
Chris Lattner32fb9562010-01-20 21:36:48 +0000925
Chris Lattner5e596182010-04-04 07:05:53 +0000926 MCSymbol *Stub = Stubs[i].first;
Chris Lattner794b2f12010-04-04 07:12:28 +0000927 MCSymbol *RawSym = Stubs[i].second.getPointer();
928 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
929 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
Chris Lattner32fb9562010-01-20 21:36:48 +0000930
Chris Lattner5e596182010-04-04 07:05:53 +0000931 OutStreamer.EmitLabel(Stub);
Chris Lattner794b2f12010-04-04 07:12:28 +0000932 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000933
Hal Finkelf07a8e02013-03-23 20:53:15 +0000934 const MCExpr *Anon = MCSymbolRefExpr::Create(AnonSymbol, OutContext);
Ulrich Weigand41789de2013-05-23 22:26:41 +0000935 const MCExpr *LazyPtrExpr = MCSymbolRefExpr::Create(LazyPtr, OutContext);
936 const MCExpr *Sub =
937 MCBinaryExpr::CreateSub(LazyPtrExpr, Anon, OutContext);
Hal Finkelf07a8e02013-03-23 20:53:15 +0000938
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000939 // mflr r0
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000940 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R0));
Hal Finkelf07a8e02013-03-23 20:53:15 +0000941 // bcl 20, 31, AnonSymbol
Hal Finkele5680b32013-04-04 22:55:54 +0000942 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCLalways).addExpr(Anon));
Chris Lattner794b2f12010-04-04 07:12:28 +0000943 OutStreamer.EmitLabel(AnonSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000944 // mflr r11
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000945 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R11));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000946 // addis r11, r11, ha16(LazyPtr - AnonSymbol)
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000947 const MCExpr *SubHa16 = PPCMCExpr::CreateHa(Sub, isDarwin, OutContext);
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000948 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000949 .addReg(PPC::R11)
950 .addReg(PPC::R11)
Ulrich Weigand41789de2013-05-23 22:26:41 +0000951 .addExpr(SubHa16));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000952 // mtlr r0
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000953 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTLR).addReg(PPC::R0));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000954
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000955 // ldu r12, lo16(LazyPtr - AnonSymbol)(r11)
956 // lwzu r12, lo16(LazyPtr - AnonSymbol)(r11)
Ulrich Weigand266db7f2013-07-08 20:20:51 +0000957 const MCExpr *SubLo16 = PPCMCExpr::CreateLo(Sub, isDarwin, OutContext);
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000958 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000959 .addReg(PPC::R12)
Ulrich Weigand41789de2013-05-23 22:26:41 +0000960 .addExpr(SubLo16).addExpr(SubLo16)
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000961 .addReg(PPC::R11));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000962 // mtctr r12
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000963 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000964 // bctr
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000965 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000966
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000967 OutStreamer.SwitchSection(LSPSection);
Chris Lattner794b2f12010-04-04 07:12:28 +0000968 OutStreamer.EmitLabel(LazyPtr);
969 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000970
971 MCSymbol *DyldStubBindingHelper =
972 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
973 if (isPPC64) {
974 // .quad dyld_stub_binding_helper
975 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
976 } else {
977 // .long dyld_stub_binding_helper
978 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
979 }
Chris Lattner54a11df2005-12-13 04:33:58 +0000980 }
Chris Lattner5e596182010-04-04 07:05:53 +0000981 OutStreamer.AddBlankLine();
Chris Lattner2f7c2792010-01-20 21:19:44 +0000982 return;
Misha Brukmanf62ee7a2004-06-24 23:04:11 +0000983 }
Chris Lattner2f7c2792010-01-20 21:19:44 +0000984
985 const MCSection *StubSection =
Chris Lattner433d4062010-04-08 20:40:11 +0000986 OutContext.getMachOSection("__TEXT","__symbol_stub1",
987 MCSectionMachO::S_SYMBOL_STUBS |
988 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
989 16, SectionKind::getText());
Chris Lattner32fb9562010-01-20 21:36:48 +0000990 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner5e596182010-04-04 07:05:53 +0000991 MCSymbol *Stub = Stubs[i].first;
Chris Lattner794b2f12010-04-04 07:12:28 +0000992 MCSymbol *RawSym = Stubs[i].second.getPointer();
993 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
Ulrich Weigand41789de2013-05-23 22:26:41 +0000994 const MCExpr *LazyPtrExpr = MCSymbolRefExpr::Create(LazyPtr, OutContext);
Chris Lattner32fb9562010-01-20 21:36:48 +0000995
Chris Lattner2f7c2792010-01-20 21:19:44 +0000996 OutStreamer.SwitchSection(StubSection);
997 EmitAlignment(4);
Chris Lattner5e596182010-04-04 07:05:53 +0000998 OutStreamer.EmitLabel(Stub);
Chris Lattner794b2f12010-04-04 07:12:28 +0000999 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Ulrich Weigand41789de2013-05-23 22:26:41 +00001000
Benjamin Kramerdd76a932012-11-24 13:18:25 +00001001 // lis r11, ha16(LazyPtr)
Ulrich Weigand266db7f2013-07-08 20:20:51 +00001002 const MCExpr *LazyPtrHa16 =
1003 PPCMCExpr::CreateHa(LazyPtrExpr, isDarwin, OutContext);
Benjamin Kramerebf576d2012-11-26 18:05:52 +00001004 OutStreamer.EmitInstruction(MCInstBuilder(PPC::LIS)
Benjamin Kramer4e629f72012-11-26 13:34:22 +00001005 .addReg(PPC::R11)
Benjamin Kramerebf576d2012-11-26 18:05:52 +00001006 .addExpr(LazyPtrHa16));
Benjamin Kramerdd76a932012-11-24 13:18:25 +00001007
Benjamin Kramer4e629f72012-11-26 13:34:22 +00001008 // ldu r12, lo16(LazyPtr)(r11)
1009 // lwzu r12, lo16(LazyPtr)(r11)
Ulrich Weigand266db7f2013-07-08 20:20:51 +00001010 const MCExpr *LazyPtrLo16 =
1011 PPCMCExpr::CreateLo(LazyPtrExpr, isDarwin, OutContext);
Benjamin Kramerebf576d2012-11-26 18:05:52 +00001012 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
Benjamin Kramer4e629f72012-11-26 13:34:22 +00001013 .addReg(PPC::R12)
1014 .addExpr(LazyPtrLo16).addExpr(LazyPtrLo16)
Benjamin Kramerebf576d2012-11-26 18:05:52 +00001015 .addReg(PPC::R11));
Benjamin Kramer4e629f72012-11-26 13:34:22 +00001016
Benjamin Kramerdd76a932012-11-24 13:18:25 +00001017 // mtctr r12
Benjamin Kramerebf576d2012-11-26 18:05:52 +00001018 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
Benjamin Kramerdd76a932012-11-24 13:18:25 +00001019 // bctr
Benjamin Kramerebf576d2012-11-26 18:05:52 +00001020 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
Benjamin Kramer4e629f72012-11-26 13:34:22 +00001021
Chris Lattner2f7c2792010-01-20 21:19:44 +00001022 OutStreamer.SwitchSection(LSPSection);
Chris Lattner794b2f12010-04-04 07:12:28 +00001023 OutStreamer.EmitLabel(LazyPtr);
1024 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +00001025
1026 MCSymbol *DyldStubBindingHelper =
1027 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
1028 if (isPPC64) {
1029 // .quad dyld_stub_binding_helper
1030 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
1031 } else {
1032 // .long dyld_stub_binding_helper
1033 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
1034 }
Chris Lattner2f7c2792010-01-20 21:19:44 +00001035 }
1036
Chris Lattner794b2f12010-04-04 07:12:28 +00001037 OutStreamer.AddBlankLine();
Chris Lattner2f7c2792010-01-20 21:19:44 +00001038}
1039
1040
1041bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Chandler Carruth5da3f052012-11-01 09:14:31 +00001042 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
Chris Lattner2f7c2792010-01-20 21:19:44 +00001043
1044 // Darwin/PPC always uses mach-o.
Dan Gohman53d4a082010-04-17 16:44:48 +00001045 const TargetLoweringObjectFileMachO &TLOFMacho =
1046 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2f7c2792010-01-20 21:19:44 +00001047 MachineModuleInfoMachO &MMIMacho =
1048 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1049
Chris Lattner32fb9562010-01-20 21:36:48 +00001050 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
1051 if (!Stubs.empty())
1052 EmitFunctionStubs(Stubs);
Misha Brukmand4ac8182004-07-16 20:29:04 +00001053
Chris Lattnere9a75a62009-08-22 21:43:10 +00001054 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen763e1102007-11-20 23:24:42 +00001055 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesenfd967cf2008-04-02 00:25:04 +00001056 // Only referenced functions get into the Personalities list.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001057 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
1058 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001059 E = Personalities.end(); I != E; ++I) {
Chris Lattner1ff47942010-01-20 21:16:14 +00001060 if (*I) {
Chris Lattner082f4842010-02-03 06:18:30 +00001061 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlinga810bdf2010-03-10 22:34:10 +00001062 MachineModuleInfoImpl::StubValueTy &StubSym =
1063 MMIMacho.getGVStubEntry(NLPSym);
Rafael Espindola79858aa2013-10-29 17:07:16 +00001064 StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(*I), true);
Chris Lattner1ff47942010-01-20 21:16:14 +00001065 }
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001066 }
Dale Johannesen763e1102007-11-20 23:24:42 +00001067 }
1068
Chris Lattner1ff47942010-01-20 21:16:14 +00001069 // Output stubs for dynamically-linked functions.
Chris Lattner32fb9562010-01-20 21:36:48 +00001070 Stubs = MMIMacho.GetGVStubList();
Chris Lattner1ff47942010-01-20 21:16:14 +00001071
Chris Lattner661710c2009-08-03 22:52:21 +00001072 // Output macho stubs for external and common global variables.
Chris Lattner1ff47942010-01-20 21:16:14 +00001073 if (!Stubs.empty()) {
Chris Lattnercb307a272009-08-10 01:39:42 +00001074 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001075 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerec64b732009-08-10 17:58:51 +00001076 EmitAlignment(isPPC64 ? 3 : 2);
1077
Chris Lattner1ff47942010-01-20 21:16:14 +00001078 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingfaec0812010-03-11 23:39:44 +00001079 // L_foo$stub:
1080 OutStreamer.EmitLabel(Stubs[i].first);
1081 // .indirect_symbol _foo
1082 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1083 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendlingdd3fe942010-03-12 02:00:43 +00001084
1085 if (MCSym.getInt())
1086 // External to current translation unit.
Eric Christophere3ab3d02013-01-09 01:57:54 +00001087 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/);
Bill Wendlingdd3fe942010-03-12 02:00:43 +00001088 else
1089 // Internal to current translation unit.
Bill Wendling866f5762010-03-31 18:47:10 +00001090 //
1091 // When we place the LSDA into the TEXT section, the type info pointers
1092 // need to be indirect and pc-rel. We accomplish this by using NLPs.
1093 // However, sometimes the types are local to the file. So we need to
1094 // fill in the value for the NLP in those cases.
Bill Wendlingdd3fe942010-03-12 02:00:43 +00001095 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
1096 OutContext),
Eric Christophere3ab3d02013-01-09 01:57:54 +00001097 isPPC64 ? 8 : 4/*size*/);
Chris Lattner54a11df2005-12-13 04:33:58 +00001098 }
Bill Wendlingfaec0812010-03-11 23:39:44 +00001099
1100 Stubs.clear();
1101 OutStreamer.AddBlankLine();
Nate Begeman0ad7f812004-08-14 22:09:10 +00001102 }
Misha Brukmanb4402432005-04-21 23:30:14 +00001103
Chris Lattner1ff47942010-01-20 21:16:14 +00001104 Stubs = MMIMacho.GetHiddenGVStubList();
1105 if (!Stubs.empty()) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001106 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner9ffa4e22009-07-15 01:14:44 +00001107 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattner1ff47942010-01-20 21:16:14 +00001108
1109 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingfaec0812010-03-11 23:39:44 +00001110 // L_foo$stub:
1111 OutStreamer.EmitLabel(Stubs[i].first);
1112 // .long _foo
1113 OutStreamer.EmitValue(MCSymbolRefExpr::
1114 Create(Stubs[i].second.getPointer(),
1115 OutContext),
Eric Christophere3ab3d02013-01-09 01:57:54 +00001116 isPPC64 ? 8 : 4/*size*/);
Evan Cheng2a03c7e2008-12-05 01:06:39 +00001117 }
Bill Wendlingfaec0812010-03-11 23:39:44 +00001118
1119 Stubs.clear();
1120 OutStreamer.AddBlankLine();
Evan Cheng2a03c7e2008-12-05 01:06:39 +00001121 }
1122
Chris Lattner7432ceef2005-11-01 00:12:36 +00001123 // Funny Darwin hack: This flag tells the linker that no global symbols
1124 // contain code that falls through to other global symbols (e.g. the obvious
1125 // implementation of multiple entry points). If this doesn't occur, the
1126 // linker can safely perform dead code stripping. Since LLVM never generates
1127 // code that does this, it is always safe to set.
Chris Lattner685508c2010-01-23 06:39:22 +00001128 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Chris Lattner7432ceef2005-11-01 00:12:36 +00001129
Dan Gohmancf0a5342007-07-25 19:33:14 +00001130 return AsmPrinter::doFinalization(M);
Misha Brukmane05203f2004-06-21 16:55:25 +00001131}
Nate Begeman4bfceb12004-09-04 05:00:00 +00001132
Jim Laskey41621a72006-12-20 20:56:46 +00001133/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1134/// for a MachineFunction to the given output stream, in a format that the
Chris Lattnera81a75c2006-09-20 17:12:19 +00001135/// Darwin assembler can deal with.
1136///
Chris Lattnerd20699b2010-04-04 08:18:47 +00001137static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
Chris Lattnere468f882010-03-13 20:55:24 +00001138 MCStreamer &Streamer) {
Jim Laskey28663c72006-12-21 20:26:09 +00001139 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1140
Chris Lattner100865e2009-07-21 18:38:57 +00001141 if (Subtarget->isDarwin())
Chris Lattnerd20699b2010-04-04 08:18:47 +00001142 return new PPCDarwinAsmPrinter(tm, Streamer);
1143 return new PPCLinuxAsmPrinter(tm, Streamer);
Chris Lattnera81a75c2006-09-20 17:12:19 +00001144}
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +00001145
Bob Wilson5a495fe2009-06-23 23:59:40 +00001146// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +00001147extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbare8338102009-07-15 20:24:03 +00001148 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbare8338102009-07-15 20:24:03 +00001149 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1150}