blob: 5430fbd523459f6fcfece82fc912afb4d8542e95 [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"
Chris Lattner6f3b9542005-10-14 23:59:06 +000021#include "PPCTargetMachine.h"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000022#include "PPCSubtarget.h"
Craig Topperb25fda92012-03-17 18:46:09 +000023#include "InstPrinter/PPCInstPrinter.h"
Evan Cheng11424442011-07-26 00:24:13 +000024#include "MCTargetDesc/PPCPredicates.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000025#include "llvm/Constants.h"
Bill Wendlinge38859d2012-06-28 00:05:13 +000026#include "llvm/DebugInfo.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000027#include "llvm/DerivedTypes.h"
28#include "llvm/Module.h"
29#include "llvm/Assembly/Writer.h"
Chris Lattner0ced9052004-08-16 23:25:21 +000030#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukmanf57c3cd2004-06-24 17:31:42 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmane05203f2004-06-21 16:55:25 +000032#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling50794832008-01-26 06:51:24 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1ff47942010-01-20 21:16:14 +000034#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000035#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9148f362010-01-13 19:00:57 +000037#include "llvm/MC/MCContext.h"
Bill Wendlingfaec0812010-03-11 23:39:44 +000038#include "llvm/MC/MCExpr.h"
Chris Lattner686a0952010-11-14 19:53:02 +000039#include "llvm/MC/MCInst.h"
Benjamin Kramer4e629f72012-11-26 13:34:22 +000040#include "llvm/MC/MCInstBuilder.h"
Chris Lattner6c203912009-08-10 18:15:01 +000041#include "llvm/MC/MCSectionMachO.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000042#include "llvm/MC/MCStreamer.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000043#include "llvm/MC/MCSymbol.h"
Roman Divacky8fe40cd2012-02-27 20:20:47 +000044#include "llvm/MC/MCSectionELF.h"
Chris Lattnerf62e3ee2010-01-16 21:57:06 +000045#include "llvm/Target/Mangler.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000046#include "llvm/Target/TargetRegisterInfo.h"
47#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetOptions.h"
Chris Lattner686a0952010-11-14 19:53:02 +000049#include "llvm/Support/CommandLine.h"
Devang Patel6e9a9792010-08-04 22:07:50 +000050#include "llvm/Support/Debug.h"
Nate Begeman4d847042004-09-04 14:51:26 +000051#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000052#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000053#include "llvm/Support/TargetRegistry.h"
Chris Lattnerd20699b2010-04-04 08:18:47 +000054#include "llvm/Support/raw_ostream.h"
Roman Divacky8fe40cd2012-02-27 20:20:47 +000055#include "llvm/Support/ELF.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000056#include "llvm/ADT/StringExtras.h"
Chris Lattner209aeca2010-01-13 06:38:18 +000057#include "llvm/ADT/SmallString.h"
Ulrich Weigand2c93acd2012-11-12 19:13:24 +000058#include "llvm/ADT/MapVector.h"
Chris Lattner0ced9052004-08-16 23:25:21 +000059using namespace llvm;
Misha Brukmane05203f2004-06-21 16:55:25 +000060
Chris Lattner1ef9cd42006-12-19 22:59:26 +000061namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000062 class PPCAsmPrinter : public AsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000063 protected:
Ulrich Weigand2c93acd2012-11-12 19:13:24 +000064 MapVector<MCSymbol*, MCSymbol*> TOC;
Chris Lattner8597a2f2006-09-20 17:07:15 +000065 const PPCSubtarget &Subtarget;
Chris Lattner8bcfdab2010-11-15 03:42:54 +000066 uint64_t TOCLabelID;
Bill Wendlingc5437ea2009-02-24 08:30:20 +000067 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +000068 explicit PPCAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
69 : AsmPrinter(TM, Streamer),
Chris Lattner8bcfdab2010-11-15 03:42:54 +000070 Subtarget(TM.getSubtarget<PPCSubtarget>()), TOCLabelID(0) {}
Misha Brukmanb4402432005-04-21 23:30:14 +000071
Misha Brukmane05203f2004-06-21 16:55:25 +000072 virtual const char *getPassName() const {
Nate Begeman4bfceb12004-09-04 05:00:00 +000073 return "PowerPC Assembly Printer";
Misha Brukmane05203f2004-06-21 16:55:25 +000074 }
75
Bill Schmidt34627e32012-11-27 17:35:46 +000076 MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym);
Nate Begeman0ad7f812004-08-14 22:09:10 +000077
Chris Lattner73de5fb2010-01-28 01:28:58 +000078 virtual void EmitInstruction(const MachineInstr *MI);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +000079
Chris Lattner510c66f2010-11-15 03:39:06 +000080 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +000081
Chris Lattnerf7f05672006-02-01 22:38:46 +000082 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000083 unsigned AsmVariant, const char *ExtraCode,
84 raw_ostream &O);
Chris Lattner7674d902006-02-24 20:27:40 +000085 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner3bb09762010-04-04 05:29:35 +000086 unsigned AsmVariant, const char *ExtraCode,
87 raw_ostream &O);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +000088
Devang Patel6e9a9792010-08-04 22:07:50 +000089 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
Devang Patel6e9a9792010-08-04 22:07:50 +000090 MachineLocation Location;
Chris Lattner510c66f2010-11-15 03:39:06 +000091 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
Devang Patel6e9a9792010-08-04 22:07:50 +000092 // Frame address. Currently handles register +- offset only.
93 if (MI->getOperand(0).isReg() && MI->getOperand(2).isImm())
94 Location.set(MI->getOperand(0).getReg(), MI->getOperand(2).getImm());
95 else {
96 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
97 }
98 return Location;
99 }
Nate Begeman4bfceb12004-09-04 05:00:00 +0000100 };
Misha Brukmanb4402432005-04-21 23:30:14 +0000101
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000102 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky02d5f772009-10-25 06:33:48 +0000103 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +0000104 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +0000105 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
106 : PPCAsmPrinter(TM, Streamer) {}
Jim Laskey28663c72006-12-21 20:26:09 +0000107
108 virtual const char *getPassName() const {
109 return "Linux PPC Assembly Printer";
110 }
111
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000112 bool doFinalization(Module &M);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000113
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000114 virtual void EmitFunctionEntryLabel();
Roman Divacky8c4b6a32012-08-28 19:06:55 +0000115
116 void EmitFunctionBodyEnd();
Jim Laskey28663c72006-12-21 20:26:09 +0000117 };
118
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000119 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
120 /// OS X
Nick Lewycky02d5f772009-10-25 06:33:48 +0000121 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
Bill Wendlingc5437ea2009-02-24 08:30:20 +0000122 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +0000123 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
124 : PPCAsmPrinter(TM, Streamer) {}
Nate Begeman4bfceb12004-09-04 05:00:00 +0000125
126 virtual const char *getPassName() const {
127 return "Darwin PPC Assembly Printer";
128 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000129
Misha Brukmane05203f2004-06-21 16:55:25 +0000130 bool doFinalization(Module &M);
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000131 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000132
Chris Lattner32fb9562010-01-20 21:36:48 +0000133 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Misha Brukmane05203f2004-06-21 16:55:25 +0000134 };
135} // end of anonymous namespace
136
Chris Lattner510c66f2010-11-15 03:39:06 +0000137/// stripRegisterPrefix - This method strips the character prefix from a
138/// register name so that only the number is left. Used by for linux asm.
139static const char *stripRegisterPrefix(const char *RegName) {
140 switch (RegName[0]) {
141 case 'r':
142 case 'f':
143 case 'v': return RegName + 1;
144 case 'c': if (RegName[1] == 'r') return RegName + 2;
145 }
146
147 return RegName;
148}
Nate Begeman0ad7f812004-08-14 22:09:10 +0000149
Chris Lattner510c66f2010-11-15 03:39:06 +0000150void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
151 raw_ostream &O) {
152 const MachineOperand &MO = MI->getOperand(OpNo);
153
Misha Brukmane05203f2004-06-21 16:55:25 +0000154 switch (MO.getType()) {
Chris Lattner510c66f2010-11-15 03:39:06 +0000155 case MachineOperand::MO_Register: {
156 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
157 // Linux assembler (Others?) does not take register mnemonics.
158 // FIXME - What about special registers used in mfspr/mtspr?
159 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
160 O << RegName;
161 return;
162 }
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000163 case MachineOperand::MO_Immediate:
Chris Lattner510c66f2010-11-15 03:39:06 +0000164 O << MO.getImm();
165 return;
Misha Brukman8d75aa42004-07-21 20:11:11 +0000166
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000167 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000168 O << *MO.getMBB()->getSymbol();
Misha Brukmane05203f2004-06-21 16:55:25 +0000169 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000170 case MachineOperand::MO_JumpTableIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000171 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattnera5bb3702007-12-30 23:10:15 +0000172 << '_' << MO.getIndex();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000173 // FIXME: PIC relocation model
174 return;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000175 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000176 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattnera5bb3702007-12-30 23:10:15 +0000177 << '_' << MO.getIndex();
Misha Brukmane05203f2004-06-21 16:55:25 +0000178 return;
Bob Wilsonf84f7102009-11-04 21:31:18 +0000179 case MachineOperand::MO_BlockAddress:
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000180 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilsonf84f7102009-11-04 21:31:18 +0000181 return;
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000182 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner57575112005-12-16 00:22:14 +0000183 // Computing the address of an external symbol, not calling it.
Chris Lattner3d571ff2010-01-16 02:09:06 +0000184 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattner1ff47942010-01-20 21:16:14 +0000185 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner3d571ff2010-01-16 02:09:06 +0000186 return;
Chris Lattner57575112005-12-16 00:22:14 +0000187 }
Chris Lattner1ff47942010-01-20 21:16:14 +0000188
Chris Lattner082f4842010-02-03 06:18:30 +0000189 MCSymbol *NLPSym =
Chris Lattner3d571ff2010-01-16 02:09:06 +0000190 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
191 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000192 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner1ff47942010-01-20 21:16:14 +0000193 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000194 if (StubSym.getPointer() == 0)
195 StubSym = MachineModuleInfoImpl::
196 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattner1ff47942010-01-20 21:16:14 +0000197
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000198 O << *NLPSym;
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000199 return;
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000200 }
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000201 case MachineOperand::MO_GlobalAddress: {
Chris Lattner57575112005-12-16 00:22:14 +0000202 // Computing the address of a global symbol, not calling it.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000203 const GlobalValue *GV = MO.getGlobal();
Chris Lattner1e26ec32010-01-16 02:00:23 +0000204 MCSymbol *SymToPrint;
Misha Brukman7dba17d2004-07-23 16:08:20 +0000205
Nate Begeman844186b2004-10-17 23:01:34 +0000206 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000207 if (TM.getRelocationModel() != Reloc::Static &&
208 (GV->isDeclaration() || GV->isWeakForLinker())) {
209 if (!GV->hasHiddenVisibility()) {
Chris Lattner555ceabe2010-01-16 18:37:32 +0000210 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000211 MachineModuleInfoImpl::StubValueTy &StubSym =
212 MMI->getObjFileInfo<MachineModuleInfoMachO>()
213 .getGVStubEntry(SymToPrint);
214 if (StubSym.getPointer() == 0)
215 StubSym = MachineModuleInfoImpl::
Chris Lattner0b822ab2010-03-12 21:19:23 +0000216 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000217 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
218 GV->hasAvailableExternallyLinkage()) {
Chris Lattner555ceabe2010-01-16 18:37:32 +0000219 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner1ff47942010-01-20 21:16:14 +0000220
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000221 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner1ff47942010-01-20 21:16:14 +0000222 MMI->getObjFileInfo<MachineModuleInfoMachO>().
223 getHiddenGVStubEntry(SymToPrint);
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000224 if (StubSym.getPointer() == 0)
225 StubSym = MachineModuleInfoImpl::
Chris Lattner0b822ab2010-03-12 21:19:23 +0000226 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000227 } else {
Chris Lattner0b822ab2010-03-12 21:19:23 +0000228 SymToPrint = Mang->getSymbol(GV);
Chris Lattner57575112005-12-16 00:22:14 +0000229 }
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000230 } else {
Chris Lattner0b822ab2010-03-12 21:19:23 +0000231 SymToPrint = Mang->getSymbol(GV);
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000232 }
Chris Lattner1e26ec32010-01-16 02:00:23 +0000233
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000234 O << *SymToPrint;
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000235
Chris Lattnerf33c7fc2010-04-03 22:28:33 +0000236 printOffset(MO.getOffset(), O);
Misha Brukmane05203f2004-06-21 16:55:25 +0000237 return;
Nate Begeman5bf9bfe2004-08-13 09:32:01 +0000238 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000239
Misha Brukmane05203f2004-06-21 16:55:25 +0000240 default:
Misha Brukmanb604b4d2004-07-08 17:58:04 +0000241 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukmana2737582004-06-25 15:11:34 +0000242 return;
Misha Brukmane05203f2004-06-21 16:55:25 +0000243 }
244}
245
Chris Lattner1bad2542006-02-23 19:31:10 +0000246/// PrintAsmOperand - Print out an operand for an inline asm expression.
247///
248bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000249 unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +0000250 const char *ExtraCode, raw_ostream &O) {
Chris Lattner1bad2542006-02-23 19:31:10 +0000251 // Does this asm operand have a single letter operand modifier?
252 if (ExtraCode && ExtraCode[0]) {
253 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000254
Chris Lattner1bad2542006-02-23 19:31:10 +0000255 switch (ExtraCode[0]) {
Jack Carter5e69cff2012-06-26 13:49:27 +0000256 default:
257 // See if this is a generic print operand
258 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
Chris Lattner48518542007-01-25 02:52:50 +0000259 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattner510c66f2010-11-15 03:39:06 +0000260 break; // PPC never has a prefix.
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000261 case 'L': // Write second word of DImode reference.
Chris Lattner1bad2542006-02-23 19:31:10 +0000262 // Verify that this operand has two consecutive registers.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000263 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattner1bad2542006-02-23 19:31:10 +0000264 OpNo+1 == MI->getNumOperands() ||
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000265 !MI->getOperand(OpNo+1).isReg())
Chris Lattner1bad2542006-02-23 19:31:10 +0000266 return true;
267 ++OpNo; // Return the high-part.
268 break;
Chris Lattnercb35c612007-04-24 22:51:03 +0000269 case 'I':
270 // Write 'i' if an integer constant, otherwise nothing. Used to print
271 // addi vs add, etc.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000272 if (MI->getOperand(OpNo).isImm())
Chris Lattnercb35c612007-04-24 22:51:03 +0000273 O << "i";
274 return false;
Chris Lattner1bad2542006-02-23 19:31:10 +0000275 }
276 }
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000277
Chris Lattner76c564b2010-04-04 04:47:45 +0000278 printOperand(MI, OpNo, O);
Chris Lattner1bad2542006-02-23 19:31:10 +0000279 return false;
280}
281
Dale Johannesen4a50e682009-08-18 00:18:39 +0000282// At the moment, all inline asm memory operands are a single register.
283// In any case, the output of this routine should always be just one
284// assembler operand.
285
Chris Lattner7674d902006-02-24 20:27:40 +0000286bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000287 unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +0000288 const char *ExtraCode,
289 raw_ostream &O) {
Hal Finkel4f24c622012-11-05 18:18:42 +0000290 if (ExtraCode && ExtraCode[0]) {
291 if (ExtraCode[1] != 0) return true; // Unknown modifier.
292
293 switch (ExtraCode[0]) {
294 default: return true; // Unknown modifier.
295 case 'y': // A memory reference for an X-form instruction
296 {
297 const char *RegName = "r0";
298 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
299 O << RegName << ", ";
300 printOperand(MI, OpNo, O);
301 return false;
302 }
303 }
304 }
305
Chris Lattner510c66f2010-11-15 03:39:06 +0000306 assert(MI->getOperand(OpNo).isReg());
Dale Johannesenf582ac72009-08-26 18:10:32 +0000307 O << "0(";
Chris Lattner76c564b2010-04-04 04:47:45 +0000308 printOperand(MI, OpNo, O);
Dale Johannesenf582ac72009-08-26 18:10:32 +0000309 O << ")";
Chris Lattner7674d902006-02-24 20:27:40 +0000310 return false;
311}
Chris Lattner1bad2542006-02-23 19:31:10 +0000312
Chris Lattner6be72602006-11-04 05:27:39 +0000313
Bill Schmidt34627e32012-11-27 17:35:46 +0000314/// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
315/// exists for it. If not, create one. Then return a symbol that references
316/// the TOC entry.
317MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) {
318
319 MCSymbol *&TOCEntry = TOC[Sym];
320
321 // To avoid name clash check if the name already exists.
322 while (TOCEntry == 0) {
323 if (OutContext.LookupSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
324 "C" + Twine(TOCLabelID++)) == 0) {
325 TOCEntry = GetTempSymbol("C", TOCLabelID);
326 }
327 }
328
329 return TOCEntry;
330}
331
332
Chris Lattner73de5fb2010-01-28 01:28:58 +0000333/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Nate Begeman0ad7f812004-08-14 22:09:10 +0000334/// the current output stream.
Misha Brukmane05203f2004-06-21 16:55:25 +0000335///
Chris Lattner73de5fb2010-01-28 01:28:58 +0000336void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner510c66f2010-11-15 03:39:06 +0000337 MCInst TmpInst;
338
339 // Lower multi-instruction pseudo operations.
340 switch (MI->getOpcode()) {
341 default: break;
Chris Lattner15e9d5e2010-11-15 07:52:06 +0000342 case TargetOpcode::DBG_VALUE: {
343 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return;
344
345 SmallString<32> Str;
346 raw_svector_ostream O(Str);
347 unsigned NOps = MI->getNumOperands();
348 assert(NOps==4);
349 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
350 // cast away const; DIetc do not take const operands for some reason.
351 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
352 O << V.getName();
353 O << " <- ";
354 // Frame address. Currently handles register +- offset only.
355 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
356 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O);
357 O << ']';
358 O << "+";
359 printOperand(MI, NOps-2, O);
360 OutStreamer.EmitRawText(O.str());
361 return;
362 }
363
Chris Lattner510c66f2010-11-15 03:39:06 +0000364 case PPC::MovePCtoLR:
365 case PPC::MovePCtoLR8: {
366 // Transform %LR = MovePCtoLR
367 // Into this, where the label is the PIC base:
368 // bl L1$pb
369 // L1$pb:
370 MCSymbol *PICBase = MF->getPICBaseSymbol();
Chris Lattner2f9f63a2010-11-14 22:03:15 +0000371
Chris Lattner510c66f2010-11-15 03:39:06 +0000372 // Emit the 'bl'.
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000373 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL_Darwin) // Darwin vs SVR4 doesn't matter here.
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000374 // FIXME: We would like an efficient form for this, so we don't have to do
375 // a lot of extra uniquing.
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000376 .addExpr(MCSymbolRefExpr::Create(PICBase, OutContext)));
Chris Lattner510c66f2010-11-15 03:39:06 +0000377
378 // Emit the label.
379 OutStreamer.EmitLabel(PICBase);
380 return;
381 }
Roman Divackyace47072012-08-24 16:26:02 +0000382 case PPC::LDtocJTI:
383 case PPC::LDtocCPT:
Chris Lattner510c66f2010-11-15 03:39:06 +0000384 case PPC::LDtoc: {
385 // Transform %X3 = LDtoc <ga:@min1>, %X2
Roman Divacky4b5665a2011-06-09 20:25:38 +0000386 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Roman Divackyace47072012-08-24 16:26:02 +0000387
Chris Lattner510c66f2010-11-15 03:39:06 +0000388 // Change the opcode to LD, and the global address operand to be a
389 // reference to the TOC entry we will synthesize later.
390 TmpInst.setOpcode(PPC::LD);
391 const MachineOperand &MO = MI->getOperand(1);
Roman Divackyace47072012-08-24 16:26:02 +0000392
393 // Map symbol -> label of TOC entry
394 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
395 MCSymbol *MOSymbol = 0;
396 if (MO.isGlobal())
397 MOSymbol = Mang->getSymbol(MO.getGlobal());
398 else if (MO.isCPI())
399 MOSymbol = GetCPISymbol(MO.getIndex());
400 else if (MO.isJTI())
401 MOSymbol = GetJTISymbol(MO.getIndex());
Bill Schmidt34627e32012-11-27 17:35:46 +0000402
403 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
Roman Divacky0be33592012-09-18 17:10:37 +0000404
Chris Lattner510c66f2010-11-15 03:39:06 +0000405 const MCExpr *Exp =
Roman Divackyace47072012-08-24 16:26:02 +0000406 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC_ENTRY,
Chris Lattner510c66f2010-11-15 03:39:06 +0000407 OutContext);
408 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
Chris Lattner686a0952010-11-14 19:53:02 +0000409 OutStreamer.EmitInstruction(TmpInst);
410 return;
411 }
Chris Lattner510c66f2010-11-15 03:39:06 +0000412
Bill Schmidt34627e32012-11-27 17:35:46 +0000413 case PPC::ADDIStocHA: {
414 // Transform %Xd = ADDIStocHA %X2, <ga:@sym>
415 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
416
417 // Change the opcode to ADDIS8. If the global address is external,
418 // has common linkage, is a function address, or is a jump table
419 // address, then generate a TOC entry and reference that. Otherwise
420 // reference the symbol directly.
421 TmpInst.setOpcode(PPC::ADDIS8);
422 const MachineOperand &MO = MI->getOperand(2);
423 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI()) &&
424 "Invalid operand for ADDIStocHA!");
425 MCSymbol *MOSymbol = 0;
426 bool IsExternal = false;
427 bool IsFunction = false;
428 bool IsCommon = false;
429
430 if (MO.isGlobal()) {
431 const GlobalValue *GValue = MO.getGlobal();
432 MOSymbol = Mang->getSymbol(GValue);
433 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GValue);
434 IsExternal = GVar && !GVar->hasInitializer();
435 IsCommon = GVar && GValue->hasCommonLinkage();
436 IsFunction = !GVar;
437 } else if (MO.isCPI())
438 MOSymbol = GetCPISymbol(MO.getIndex());
439 else if (MO.isJTI())
440 MOSymbol = GetJTISymbol(MO.getIndex());
441
442 if (IsExternal || IsFunction || IsCommon || MO.isJTI())
443 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
444
445 const MCExpr *Exp =
446 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_HA,
447 OutContext);
448 TmpInst.getOperand(2) = MCOperand::CreateExpr(Exp);
449 OutStreamer.EmitInstruction(TmpInst);
450 return;
451 }
452 case PPC::LDtocL: {
453 // Transform %Xd = LDtocL <ga:@sym>, %Xs
454 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
455
456 // Change the opcode to LDrs, which is a form of LD with the offset
457 // specified by a SymbolLo. If the global address is external, has
458 // common linkage, or is a jump table address, then reference the
459 // associated TOC entry. Otherwise reference the symbol directly.
460 TmpInst.setOpcode(PPC::LDrs);
461 const MachineOperand &MO = MI->getOperand(1);
462 assert((MO.isGlobal() || MO.isJTI()) && "Invalid operand for LDtocL!");
463 MCSymbol *MOSymbol = 0;
464
465 if (MO.isJTI())
466 MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex()));
467 else {
468 const GlobalValue *GValue = MO.getGlobal();
469 MOSymbol = Mang->getSymbol(GValue);
470 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GValue);
471
472 if (!GVar || !GVar->hasInitializer() || GValue->hasCommonLinkage())
473 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
474 }
475
476 const MCExpr *Exp =
477 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_LO,
478 OutContext);
479 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
480 OutStreamer.EmitInstruction(TmpInst);
481 return;
482 }
483 case PPC::ADDItocL: {
484 // Transform %Xd = ADDItocL %Xs, <ga:@sym>
485 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
486
487 // Change the opcode to ADDI8L. If the global address is external, then
488 // generate a TOC entry and reference that. Otherwise reference the
489 // symbol directly.
490 TmpInst.setOpcode(PPC::ADDI8L);
491 const MachineOperand &MO = MI->getOperand(2);
492 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL");
493 MCSymbol *MOSymbol = 0;
494 bool IsExternal = false;
495 bool IsFunction = false;
496
497 if (MO.isGlobal()) {
498 const GlobalValue *GValue = MO.getGlobal();
499 MOSymbol = Mang->getSymbol(GValue);
500 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GValue);
501 IsExternal = GVar && !GVar->hasInitializer();
502 IsFunction = !GVar;
503 } else if (MO.isCPI())
504 MOSymbol = GetCPISymbol(MO.getIndex());
505
506 if (IsFunction || IsExternal)
507 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
508
509 const MCExpr *Exp =
510 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_LO,
511 OutContext);
512 TmpInst.getOperand(2) = MCOperand::CreateExpr(Exp);
513 OutStreamer.EmitInstruction(TmpInst);
514 return;
515 }
Chris Lattner510c66f2010-11-15 03:39:06 +0000516 case PPC::MFCRpseud:
Hal Finkelac9df3d2011-12-07 06:34:06 +0000517 case PPC::MFCR8pseud:
Chris Lattner510c66f2010-11-15 03:39:06 +0000518 // Transform: %R3 = MFCRpseud %CR7
519 // Into: %R3 = MFCR ;; cr7
520 OutStreamer.AddComment(PPCInstPrinter::
521 getRegisterName(MI->getOperand(1).getReg()));
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000522 OutStreamer.EmitInstruction(MCInstBuilder(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR)
523 .addReg(MI->getOperand(0).getReg()));
Dale Johannesenbc41cfa2010-04-26 20:05:01 +0000524 return;
Hal Finkel6fa56972011-10-17 04:03:49 +0000525 case PPC::SYNC:
526 // In Book E sync is called msync, handle this special case here...
527 if (Subtarget.isBookE()) {
528 OutStreamer.EmitRawText(StringRef("\tmsync"));
529 return;
530 }
Dale Johannesenbc41cfa2010-04-26 20:05:01 +0000531 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000532
Roman Divacky4b5665a2011-06-09 20:25:38 +0000533 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Chris Lattner510c66f2010-11-15 03:39:06 +0000534 OutStreamer.EmitInstruction(TmpInst);
Misha Brukmane05203f2004-06-21 16:55:25 +0000535}
536
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000537void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
538 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
539 return AsmPrinter::EmitFunctionEntryLabel();
540
541 // Emit an official procedure descriptor.
Roman Divacky8fe40cd2012-02-27 20:20:47 +0000542 const MCSection *Current = OutStreamer.getCurrentSection();
543 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
544 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
545 SectionKind::getReadOnly());
546 OutStreamer.SwitchSection(Section);
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000547 OutStreamer.EmitLabel(CurrentFnSym);
Roman Divacky8fe40cd2012-02-27 20:20:47 +0000548 OutStreamer.EmitValueToAlignment(8);
549 MCSymbol *Symbol1 =
550 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000551 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
552 // entry point.
Roman Divacky8fe40cd2012-02-27 20:20:47 +0000553 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
Roman Divacky2be394bd2012-09-03 16:55:42 +0000554 8/*size*/, 0/*addrspace*/);
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000555 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC."));
556 // Generates a R_PPC64_TOC relocation for TOC base insertion.
557 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2,
558 MCSymbolRefExpr::VK_PPC_TOC, OutContext),
Roman Divacky2be394bd2012-09-03 16:55:42 +0000559 8/*size*/, 0/*addrspace*/);
Roman Divackyd4f6f422012-09-18 16:55:29 +0000560 // Emit a null environment pointer.
561 OutStreamer.EmitIntValue(0, 8 /* size */, 0 /* addrspace */);
Roman Divacky8fe40cd2012-02-27 20:20:47 +0000562 OutStreamer.SwitchSection(Current);
Hal Finkelad4d9f52012-02-22 21:11:47 +0000563
564 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
565 ".L." + Twine(CurrentFnSym->getName()));
566 OutStreamer.EmitLabel(RealFnSym);
567 CurrentFnSymForSize = RealFnSym;
Chris Lattnerbc1e6f02010-01-27 07:21:55 +0000568}
569
570
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000571bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000572 const DataLayout *TD = TM.getDataLayout();
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000573
Chandler Carruth5da3f052012-11-01 09:14:31 +0000574 bool isPPC64 = TD->getPointerSizeInBits() == 64;
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000575
576 if (isPPC64 && !TOC.empty()) {
Roman Divacky34d4b962012-02-28 18:15:25 +0000577 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
578 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
579 SectionKind::getReadOnly());
580 OutStreamer.SwitchSection(Section);
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000581
Ulrich Weigand2c93acd2012-11-12 19:13:24 +0000582 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
Chris Lattner3d571ff2010-01-16 02:09:06 +0000583 E = TOC.end(); I != E; ++I) {
Chris Lattner5e596182010-04-04 07:05:53 +0000584 OutStreamer.EmitLabel(I->second);
Adhemerval Zanellaef206f12012-10-15 15:43:14 +0000585 MCSymbol *S = OutContext.GetOrCreateSymbol(I->first->getName());
586 OutStreamer.EmitTCEntry(*S);
Tilmann Schellerd1aaa322009-08-15 11:54:46 +0000587 }
588 }
589
590 return AsmPrinter::doFinalization(M);
591}
Jim Laskey28663c72006-12-21 20:26:09 +0000592
Roman Divacky8c4b6a32012-08-28 19:06:55 +0000593/// EmitFunctionBodyEnd - Print the traceback table before the .size
594/// directive.
595///
596void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
597 // Only the 64-bit target requires a traceback table. For now,
598 // we only emit the word of zeroes that GDB requires to find
Hal Finkel1859d262012-08-29 20:22:24 +0000599 // the end of the function, and zeroes for the eight-byte
600 // mandatory fields.
601 // FIXME: We should fill in the eight-byte mandatory fields as described in
602 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
603 // currently make use of these fields).
604 if (Subtarget.isPPC64()) {
Roman Divacky8c4b6a32012-08-28 19:06:55 +0000605 OutStreamer.EmitIntValue(0, 4/*size*/);
Hal Finkel1859d262012-08-29 20:22:24 +0000606 OutStreamer.EmitIntValue(0, 8/*size*/);
607 }
Roman Divacky8c4b6a32012-08-28 19:06:55 +0000608}
609
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000610void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmanbdc24ad2008-03-25 21:45:14 +0000611 static const char *const CPUDirectives[] = {
Dale Johannesen6ca3ccf2008-02-14 23:35:16 +0000612 "",
Jim Laskey59e7a772006-12-12 20:57:08 +0000613 "ppc",
Hal Finkel6fa56972011-10-17 04:03:49 +0000614 "ppc440",
Jim Laskey59e7a772006-12-12 20:57:08 +0000615 "ppc601",
616 "ppc602",
617 "ppc603",
618 "ppc7400",
619 "ppc750",
620 "ppc970",
Hal Finkel9f9f8922012-04-01 19:22:40 +0000621 "ppcA2",
Hal Finkel742b5352012-08-28 16:12:39 +0000622 "ppce500mc",
623 "ppce5500",
Hal Finkelf2b9c382012-06-11 15:43:08 +0000624 "power6",
625 "power7",
Jim Laskey59e7a772006-12-12 20:57:08 +0000626 "ppc64"
627 };
628
629 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelbfd3d082012-06-11 19:57:01 +0000630 if (Subtarget.hasMFOCRF() && Directive < PPC::DIR_970)
Jim Laskey59e7a772006-12-12 20:57:08 +0000631 Directive = PPC::DIR_970;
632 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
633 Directive = PPC::DIR_7400;
Eli Friedmanc1870b262011-12-01 01:43:47 +0000634 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
Jim Laskey59e7a772006-12-12 20:57:08 +0000635 Directive = PPC::DIR_64;
636 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Chris Lattneraac9fa72010-11-15 08:49:58 +0000637
638 // FIXME: This is a total hack, finish mc'izing the PPC backend.
639 if (OutStreamer.hasRawTextSupport())
640 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
Anton Korobeynikov7c20ede2008-08-08 18:22:59 +0000641
Jim Laskeyec05b042006-11-28 18:21:52 +0000642 // Prime text sections so they are adjacent. This reduces the likelihood a
643 // large data or debug section causes a branch to exceed 16M limit.
Dan Gohman53d4a082010-04-17 16:44:48 +0000644 const TargetLoweringObjectFileMachO &TLOFMacho =
645 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000646 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskeyec05b042006-11-28 18:21:52 +0000647 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000648 OutStreamer.SwitchSection(
Chris Lattner433d4062010-04-08 20:40:11 +0000649 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000650 MCSectionMachO::S_SYMBOL_STUBS |
651 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
652 32, SectionKind::getText()));
Jim Laskeyec05b042006-11-28 18:21:52 +0000653 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000654 OutStreamer.SwitchSection(
Chris Lattner433d4062010-04-08 20:40:11 +0000655 OutContext.getMachOSection("__TEXT","__symbol_stub1",
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000656 MCSectionMachO::S_SYMBOL_STUBS |
657 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
658 16, SectionKind::getText()));
Jim Laskeyec05b042006-11-28 18:21:52 +0000659 }
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000660 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman15527112005-07-21 01:25:49 +0000661}
662
Chris Lattner794b2f12010-04-04 07:12:28 +0000663static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner32fb9562010-01-20 21:36:48 +0000664 // Remove $stub suffix, add $lazy_ptr.
Benjamin Kramer69dc3fe2012-11-24 13:18:11 +0000665 StringRef NoStub = Sym->getName().substr(0, Sym->getName().size()-5);
666 return Ctx.GetOrCreateSymbol(NoStub + "$lazy_ptr");
Chris Lattner32fb9562010-01-20 21:36:48 +0000667}
668
Chris Lattner794b2f12010-04-04 07:12:28 +0000669static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner32fb9562010-01-20 21:36:48 +0000670 // Add $tmp suffix to $stub, yielding $stub$tmp.
Benjamin Kramer69dc3fe2012-11-24 13:18:11 +0000671 return Ctx.GetOrCreateSymbol(Sym->getName() + "$tmp");
Chris Lattner32fb9562010-01-20 21:36:48 +0000672}
673
674void PPCDarwinAsmPrinter::
675EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chandler Carruth5da3f052012-11-01 09:14:31 +0000676 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
Chris Lattner2f7c2792010-01-20 21:19:44 +0000677
Dan Gohman53d4a082010-04-17 16:44:48 +0000678 const TargetLoweringObjectFileMachO &TLOFMacho =
679 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2f7c2792010-01-20 21:19:44 +0000680
681 // .lazy_symbol_pointer
682 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattnercb307a272009-08-10 01:39:42 +0000683
Misha Brukmand4ac8182004-07-16 20:29:04 +0000684 // Output stubs for dynamically-linked functions
Chris Lattner2f7c2792010-01-20 21:19:44 +0000685 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner661710c2009-08-03 22:52:21 +0000686 const MCSection *StubSection =
Chris Lattner433d4062010-04-08 20:40:11 +0000687 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
688 MCSectionMachO::S_SYMBOL_STUBS |
689 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
690 32, SectionKind::getText());
Chris Lattner32fb9562010-01-20 21:36:48 +0000691 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000692 OutStreamer.SwitchSection(StubSection);
Chris Lattner1df08392006-06-27 01:02:25 +0000693 EmitAlignment(4);
Chris Lattner32fb9562010-01-20 21:36:48 +0000694
Chris Lattner5e596182010-04-04 07:05:53 +0000695 MCSymbol *Stub = Stubs[i].first;
Chris Lattner794b2f12010-04-04 07:12:28 +0000696 MCSymbol *RawSym = Stubs[i].second.getPointer();
697 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
698 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
Chris Lattner32fb9562010-01-20 21:36:48 +0000699
Chris Lattner5e596182010-04-04 07:05:53 +0000700 OutStreamer.EmitLabel(Stub);
Chris Lattner794b2f12010-04-04 07:12:28 +0000701 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000702
703 // mflr r0
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000704 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R0));
Chris Lattner794b2f12010-04-04 07:12:28 +0000705 // FIXME: MCize this.
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000706 OutStreamer.EmitRawText("\tbcl 20, 31, " + Twine(AnonSymbol->getName()));
Chris Lattner794b2f12010-04-04 07:12:28 +0000707 OutStreamer.EmitLabel(AnonSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000708 // mflr r11
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000709 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R11));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000710 // addis r11, r11, ha16(LazyPtr - AnonSymbol)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000711 const MCExpr *Sub =
712 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(LazyPtr, OutContext),
713 MCSymbolRefExpr::Create(AnonSymbol, OutContext),
714 OutContext);
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000715 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000716 .addReg(PPC::R11)
717 .addReg(PPC::R11)
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000718 .addExpr(Sub));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000719 // mtlr r0
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000720 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTLR).addReg(PPC::R0));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000721
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000722 // ldu r12, lo16(LazyPtr - AnonSymbol)(r11)
723 // lwzu r12, lo16(LazyPtr - AnonSymbol)(r11)
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000724 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000725 .addReg(PPC::R12)
726 .addExpr(Sub).addExpr(Sub)
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000727 .addReg(PPC::R11));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000728 // mtctr r12
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000729 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000730 // bctr
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000731 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000732
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000733 OutStreamer.SwitchSection(LSPSection);
Chris Lattner794b2f12010-04-04 07:12:28 +0000734 OutStreamer.EmitLabel(LazyPtr);
735 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000736
737 MCSymbol *DyldStubBindingHelper =
738 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
739 if (isPPC64) {
740 // .quad dyld_stub_binding_helper
741 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
742 } else {
743 // .long dyld_stub_binding_helper
744 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
745 }
Chris Lattner54a11df2005-12-13 04:33:58 +0000746 }
Chris Lattner5e596182010-04-04 07:05:53 +0000747 OutStreamer.AddBlankLine();
Chris Lattner2f7c2792010-01-20 21:19:44 +0000748 return;
Misha Brukmanf62ee7a2004-06-24 23:04:11 +0000749 }
Chris Lattner2f7c2792010-01-20 21:19:44 +0000750
751 const MCSection *StubSection =
Chris Lattner433d4062010-04-08 20:40:11 +0000752 OutContext.getMachOSection("__TEXT","__symbol_stub1",
753 MCSectionMachO::S_SYMBOL_STUBS |
754 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
755 16, SectionKind::getText());
Chris Lattner32fb9562010-01-20 21:36:48 +0000756 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner5e596182010-04-04 07:05:53 +0000757 MCSymbol *Stub = Stubs[i].first;
Chris Lattner794b2f12010-04-04 07:12:28 +0000758 MCSymbol *RawSym = Stubs[i].second.getPointer();
759 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
Chris Lattner32fb9562010-01-20 21:36:48 +0000760
Chris Lattner2f7c2792010-01-20 21:19:44 +0000761 OutStreamer.SwitchSection(StubSection);
762 EmitAlignment(4);
Chris Lattner5e596182010-04-04 07:05:53 +0000763 OutStreamer.EmitLabel(Stub);
Chris Lattner794b2f12010-04-04 07:12:28 +0000764 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000765 // lis r11, ha16(LazyPtr)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000766 const MCExpr *LazyPtrHa16 =
767 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_HA16,
768 OutContext);
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000769 OutStreamer.EmitInstruction(MCInstBuilder(PPC::LIS)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000770 .addReg(PPC::R11)
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000771 .addExpr(LazyPtrHa16));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000772
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000773 const MCExpr *LazyPtrLo16 =
774 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_LO16,
775 OutContext);
776 // ldu r12, lo16(LazyPtr)(r11)
777 // lwzu r12, lo16(LazyPtr)(r11)
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000778 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000779 .addReg(PPC::R12)
780 .addExpr(LazyPtrLo16).addExpr(LazyPtrLo16)
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000781 .addReg(PPC::R11));
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000782
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000783 // mtctr r12
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000784 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000785 // bctr
Benjamin Kramerebf576d2012-11-26 18:05:52 +0000786 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
Benjamin Kramer4e629f72012-11-26 13:34:22 +0000787
Chris Lattner2f7c2792010-01-20 21:19:44 +0000788 OutStreamer.SwitchSection(LSPSection);
Chris Lattner794b2f12010-04-04 07:12:28 +0000789 OutStreamer.EmitLabel(LazyPtr);
790 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramerdd76a932012-11-24 13:18:25 +0000791
792 MCSymbol *DyldStubBindingHelper =
793 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
794 if (isPPC64) {
795 // .quad dyld_stub_binding_helper
796 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
797 } else {
798 // .long dyld_stub_binding_helper
799 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
800 }
Chris Lattner2f7c2792010-01-20 21:19:44 +0000801 }
802
Chris Lattner794b2f12010-04-04 07:12:28 +0000803 OutStreamer.AddBlankLine();
Chris Lattner2f7c2792010-01-20 21:19:44 +0000804}
805
806
807bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Chandler Carruth5da3f052012-11-01 09:14:31 +0000808 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
Chris Lattner2f7c2792010-01-20 21:19:44 +0000809
810 // Darwin/PPC always uses mach-o.
Dan Gohman53d4a082010-04-17 16:44:48 +0000811 const TargetLoweringObjectFileMachO &TLOFMacho =
812 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner2f7c2792010-01-20 21:19:44 +0000813 MachineModuleInfoMachO &MMIMacho =
814 MMI->getObjFileInfo<MachineModuleInfoMachO>();
815
Chris Lattner32fb9562010-01-20 21:36:48 +0000816 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
817 if (!Stubs.empty())
818 EmitFunctionStubs(Stubs);
Misha Brukmand4ac8182004-07-16 20:29:04 +0000819
Chris Lattnere9a75a62009-08-22 21:43:10 +0000820 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen763e1102007-11-20 23:24:42 +0000821 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesenfd967cf2008-04-02 00:25:04 +0000822 // Only referenced functions get into the Personalities list.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000823 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
824 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000825 E = Personalities.end(); I != E; ++I) {
Chris Lattner1ff47942010-01-20 21:16:14 +0000826 if (*I) {
Chris Lattner082f4842010-02-03 06:18:30 +0000827 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000828 MachineModuleInfoImpl::StubValueTy &StubSym =
829 MMIMacho.getGVStubEntry(NLPSym);
Chris Lattner0b822ab2010-03-12 21:19:23 +0000830 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
Chris Lattner1ff47942010-01-20 21:16:14 +0000831 }
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000832 }
Dale Johannesen763e1102007-11-20 23:24:42 +0000833 }
834
Chris Lattner1ff47942010-01-20 21:16:14 +0000835 // Output stubs for dynamically-linked functions.
Chris Lattner32fb9562010-01-20 21:36:48 +0000836 Stubs = MMIMacho.GetGVStubList();
Chris Lattner1ff47942010-01-20 21:16:14 +0000837
Chris Lattner661710c2009-08-03 22:52:21 +0000838 // Output macho stubs for external and common global variables.
Chris Lattner1ff47942010-01-20 21:16:14 +0000839 if (!Stubs.empty()) {
Chris Lattnercb307a272009-08-10 01:39:42 +0000840 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000841 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerec64b732009-08-10 17:58:51 +0000842 EmitAlignment(isPPC64 ? 3 : 2);
843
Chris Lattner1ff47942010-01-20 21:16:14 +0000844 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingfaec0812010-03-11 23:39:44 +0000845 // L_foo$stub:
846 OutStreamer.EmitLabel(Stubs[i].first);
847 // .indirect_symbol _foo
848 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
849 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendlingdd3fe942010-03-12 02:00:43 +0000850
851 if (MCSym.getInt())
852 // External to current translation unit.
853 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
854 else
855 // Internal to current translation unit.
Bill Wendling866f5762010-03-31 18:47:10 +0000856 //
857 // When we place the LSDA into the TEXT section, the type info pointers
858 // need to be indirect and pc-rel. We accomplish this by using NLPs.
859 // However, sometimes the types are local to the file. So we need to
860 // fill in the value for the NLP in those cases.
Bill Wendlingdd3fe942010-03-12 02:00:43 +0000861 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
862 OutContext),
863 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Chris Lattner54a11df2005-12-13 04:33:58 +0000864 }
Bill Wendlingfaec0812010-03-11 23:39:44 +0000865
866 Stubs.clear();
867 OutStreamer.AddBlankLine();
Nate Begeman0ad7f812004-08-14 22:09:10 +0000868 }
Misha Brukmanb4402432005-04-21 23:30:14 +0000869
Chris Lattner1ff47942010-01-20 21:16:14 +0000870 Stubs = MMIMacho.GetHiddenGVStubList();
871 if (!Stubs.empty()) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000872 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner9ffa4e22009-07-15 01:14:44 +0000873 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattner1ff47942010-01-20 21:16:14 +0000874
875 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingfaec0812010-03-11 23:39:44 +0000876 // L_foo$stub:
877 OutStreamer.EmitLabel(Stubs[i].first);
878 // .long _foo
879 OutStreamer.EmitValue(MCSymbolRefExpr::
880 Create(Stubs[i].second.getPointer(),
881 OutContext),
882 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000883 }
Bill Wendlingfaec0812010-03-11 23:39:44 +0000884
885 Stubs.clear();
886 OutStreamer.AddBlankLine();
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000887 }
888
Chris Lattner7432ceef2005-11-01 00:12:36 +0000889 // Funny Darwin hack: This flag tells the linker that no global symbols
890 // contain code that falls through to other global symbols (e.g. the obvious
891 // implementation of multiple entry points). If this doesn't occur, the
892 // linker can safely perform dead code stripping. Since LLVM never generates
893 // code that does this, it is always safe to set.
Chris Lattner685508c2010-01-23 06:39:22 +0000894 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Chris Lattner7432ceef2005-11-01 00:12:36 +0000895
Dan Gohmancf0a5342007-07-25 19:33:14 +0000896 return AsmPrinter::doFinalization(M);
Misha Brukmane05203f2004-06-21 16:55:25 +0000897}
Nate Begeman4bfceb12004-09-04 05:00:00 +0000898
Jim Laskey41621a72006-12-20 20:56:46 +0000899/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
900/// for a MachineFunction to the given output stream, in a format that the
Chris Lattnera81a75c2006-09-20 17:12:19 +0000901/// Darwin assembler can deal with.
902///
Chris Lattnerd20699b2010-04-04 08:18:47 +0000903static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
Chris Lattnere468f882010-03-13 20:55:24 +0000904 MCStreamer &Streamer) {
Jim Laskey28663c72006-12-21 20:26:09 +0000905 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
906
Chris Lattner100865e2009-07-21 18:38:57 +0000907 if (Subtarget->isDarwin())
Chris Lattnerd20699b2010-04-04 08:18:47 +0000908 return new PPCDarwinAsmPrinter(tm, Streamer);
909 return new PPCLinuxAsmPrinter(tm, Streamer);
Chris Lattnera81a75c2006-09-20 17:12:19 +0000910}
Anton Korobeynikov28dc9d02008-08-17 13:54:28 +0000911
Bob Wilson5a495fe2009-06-23 23:59:40 +0000912// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +0000913extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbare8338102009-07-15 20:24:03 +0000914 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbare8338102009-07-15 20:24:03 +0000915 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
916}