blob: 3900c8bab4f17d5a8b79a8e1216e272f9ae25c55 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
9//
Misha Brukman05fcd0c2004-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 Lattner83660c52004-07-28 20:18:53 +000012// the output mechanism used by `llc'.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000013//
Misha Brukman05fcd0c2004-07-08 17:58:04 +000014// Documentation at http://developer.apple.com/documentation/DeveloperTools/
15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
Misha Brukman218bec72004-06-29 17:13:26 +000016//
Misha Brukman5dfe3a92004-06-21 16:55:25 +000017//===----------------------------------------------------------------------===//
18
Misha Brukman05794492004-06-24 17:31:42 +000019#define DEBUG_TYPE "asmprinter"
Chris Lattner26689592005-10-14 23:51:18 +000020#include "PPC.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000021#include "PPCTargetMachine.h"
Chris Lattner26689592005-10-14 23:51:18 +000022#include "PPCSubtarget.h"
Craig Topper79aa3412012-03-17 18:46:09 +000023#include "InstPrinter/PPCInstPrinter.h"
Evan Cheng94b95502011-07-26 00:24:13 +000024#include "MCTargetDesc/PPCPredicates.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000025#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000026#include "llvm/DebugInfo.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000027#include "llvm/DerivedTypes.h"
28#include "llvm/Module.h"
29#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000030#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukman05794492004-06-24 17:31:42 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000032#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling381802f2008-01-26 06:51:24 +000033#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerd3ec0b52010-01-20 21:16:14 +000034#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000035#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattner0b4bad52010-01-13 19:00:57 +000037#include "llvm/MC/MCContext.h"
Bill Wendling08d726c2010-03-11 23:39:44 +000038#include "llvm/MC/MCExpr.h"
Chris Lattner293ef9a2010-11-14 19:53:02 +000039#include "llvm/MC/MCInst.h"
Benjamin Kramer391271f2012-11-26 13:34:22 +000040#include "llvm/MC/MCInstBuilder.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000041#include "llvm/MC/MCSectionMachO.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000042#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000043#include "llvm/MC/MCSymbol.h"
Roman Divacky4328f9f2012-02-27 20:20:47 +000044#include "llvm/MC/MCSectionELF.h"
Chris Lattner45111d12010-01-16 21:57:06 +000045#include "llvm/Target/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000046#include "llvm/Target/TargetRegisterInfo.h"
47#include "llvm/Target/TargetInstrInfo.h"
48#include "llvm/Target/TargetOptions.h"
Chris Lattner293ef9a2010-11-14 19:53:02 +000049#include "llvm/Support/CommandLine.h"
Devang Patel40e0bad2010-08-04 22:07:50 +000050#include "llvm/Support/Debug.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000051#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000053#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000054#include "llvm/Support/raw_ostream.h"
Roman Divacky4328f9f2012-02-27 20:20:47 +000055#include "llvm/Support/ELF.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000056#include "llvm/ADT/StringExtras.h"
Chris Lattner48130352010-01-13 06:38:18 +000057#include "llvm/ADT/SmallString.h"
Ulrich Weigand95d8afc2012-11-12 19:13:24 +000058#include "llvm/ADT/MapVector.h"
Chris Lattnera3840792004-08-16 23:25:21 +000059using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000060
Chris Lattner95b2c7d2006-12-19 22:59:26 +000061namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000062 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000063 protected:
Ulrich Weigand95d8afc2012-11-12 19:13:24 +000064 MapVector<MCSymbol*, MCSymbol*> TOC;
Chris Lattnerdadceed2006-09-20 17:07:15 +000065 const PPCSubtarget &Subtarget;
Chris Lattner374c6082010-11-15 03:42:54 +000066 uint64_t TOCLabelID;
Bill Wendling57f0db82009-02-24 08:30:20 +000067 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +000068 explicit PPCAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
69 : AsmPrinter(TM, Streamer),
Chris Lattner374c6082010-11-15 03:42:54 +000070 Subtarget(TM.getSubtarget<PPCSubtarget>()), TOCLabelID(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000071
Misha Brukman5dfe3a92004-06-21 16:55:25 +000072 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000073 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000074 }
75
Nate Begemane59bf592004-08-14 22:09:10 +000076
Chris Lattnerd49fe1b2010-01-28 01:28:58 +000077 virtual void EmitInstruction(const MachineInstr *MI);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000078
Chris Lattnercb22fa62010-11-15 03:39:06 +000079 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000080
Chris Lattner58873272006-02-01 22:38:46 +000081 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000082 unsigned AsmVariant, const char *ExtraCode,
83 raw_ostream &O);
Chris Lattner2c003e22006-02-24 20:27:40 +000084 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000085 unsigned AsmVariant, const char *ExtraCode,
86 raw_ostream &O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000087
Devang Patel40e0bad2010-08-04 22:07:50 +000088 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
Devang Patel40e0bad2010-08-04 22:07:50 +000089 MachineLocation Location;
Chris Lattnercb22fa62010-11-15 03:39:06 +000090 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
Devang Patel40e0bad2010-08-04 22:07:50 +000091 // Frame address. Currently handles register +- offset only.
92 if (MI->getOperand(0).isReg() && MI->getOperand(2).isImm())
93 Location.set(MI->getOperand(0).getReg(), MI->getOperand(2).getImm());
94 else {
95 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
96 }
97 return Location;
98 }
Nate Begemaned428532004-09-04 05:00:00 +000099 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000100
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000101 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000102 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000103 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +0000104 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
105 : PPCAsmPrinter(TM, Streamer) {}
Jim Laskeybf111822006-12-21 20:26:09 +0000106
107 virtual const char *getPassName() const {
108 return "Linux PPC Assembly Printer";
109 }
110
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000111 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000112
Chris Lattner2cf72512010-01-27 07:21:55 +0000113 virtual void EmitFunctionEntryLabel();
Roman Divackyc6c2ced2012-08-28 19:06:55 +0000114
115 void EmitFunctionBodyEnd();
Jim Laskeybf111822006-12-21 20:26:09 +0000116 };
117
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000118 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
119 /// OS X
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000120 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000121 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +0000122 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
123 : PPCAsmPrinter(TM, Streamer) {}
Nate Begemaned428532004-09-04 05:00:00 +0000124
125 virtual const char *getPassName() const {
126 return "Darwin PPC Assembly Printer";
127 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000128
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000129 bool doFinalization(Module &M);
Bob Wilson812209a2009-09-30 22:06:26 +0000130 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000131
Chris Lattner73a1aa02010-01-20 21:36:48 +0000132 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000133 };
134} // end of anonymous namespace
135
Chris Lattnercb22fa62010-11-15 03:39:06 +0000136/// stripRegisterPrefix - This method strips the character prefix from a
137/// register name so that only the number is left. Used by for linux asm.
138static const char *stripRegisterPrefix(const char *RegName) {
139 switch (RegName[0]) {
140 case 'r':
141 case 'f':
142 case 'v': return RegName + 1;
143 case 'c': if (RegName[1] == 'r') return RegName + 2;
144 }
145
146 return RegName;
147}
Nate Begemane59bf592004-08-14 22:09:10 +0000148
Chris Lattnercb22fa62010-11-15 03:39:06 +0000149void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
150 raw_ostream &O) {
151 const MachineOperand &MO = MI->getOperand(OpNo);
152
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000153 switch (MO.getType()) {
Chris Lattnercb22fa62010-11-15 03:39:06 +0000154 case MachineOperand::MO_Register: {
155 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
156 // Linux assembler (Others?) does not take register mnemonics.
157 // FIXME - What about special registers used in mfspr/mtspr?
158 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
159 O << RegName;
160 return;
161 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000162 case MachineOperand::MO_Immediate:
Chris Lattnercb22fa62010-11-15 03:39:06 +0000163 O << MO.getImm();
164 return;
Misha Brukman97a296f2004-07-21 20:11:11 +0000165
Nate Begeman37efe672006-04-22 18:53:45 +0000166 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000167 O << *MO.getMBB()->getSymbol();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000168 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000169 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000170 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000171 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000172 // FIXME: PIC relocation model
173 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000174 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000175 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000176 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000177 return;
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000178 case MachineOperand::MO_BlockAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +0000179 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000180 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000181 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000182 // Computing the address of an external symbol, not calling it.
Chris Lattner75abc682010-01-16 02:09:06 +0000183 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000184 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner75abc682010-01-16 02:09:06 +0000185 return;
Chris Lattnera637c322005-12-16 00:22:14 +0000186 }
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000187
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000188 MCSymbol *NLPSym =
Chris Lattner75abc682010-01-16 02:09:06 +0000189 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
190 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000191 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000192 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000193 if (StubSym.getPointer() == 0)
194 StubSym = MachineModuleInfoImpl::
195 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000196
Chris Lattner10b318b2010-01-17 21:43:43 +0000197 O << *NLPSym;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000198 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000199 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000200 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000201 // Computing the address of a global symbol, not calling it.
Dan Gohman46510a72010-04-15 01:51:59 +0000202 const GlobalValue *GV = MO.getGlobal();
Chris Lattnera40128c2010-01-16 02:00:23 +0000203 MCSymbol *SymToPrint;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000204
Nate Begemanfcf4a422004-10-17 23:01:34 +0000205 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000206 if (TM.getRelocationModel() != Reloc::Static &&
207 (GV->isDeclaration() || GV->isWeakForLinker())) {
208 if (!GV->hasHiddenVisibility()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000209 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000210 MachineModuleInfoImpl::StubValueTy &StubSym =
211 MMI->getObjFileInfo<MachineModuleInfoMachO>()
212 .getGVStubEntry(SymToPrint);
213 if (StubSym.getPointer() == 0)
214 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000215 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000216 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
217 GV->hasAvailableExternallyLinkage()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000218 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000219
Bill Wendlingcebae362010-03-10 22:34:10 +0000220 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000221 MMI->getObjFileInfo<MachineModuleInfoMachO>().
222 getHiddenGVStubEntry(SymToPrint);
Bill Wendlingcebae362010-03-10 22:34:10 +0000223 if (StubSym.getPointer() == 0)
224 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000225 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000226 } else {
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000227 SymToPrint = Mang->getSymbol(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000228 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000229 } else {
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000230 SymToPrint = Mang->getSymbol(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000231 }
Chris Lattnera40128c2010-01-16 02:00:23 +0000232
Chris Lattner10b318b2010-01-17 21:43:43 +0000233 O << *SymToPrint;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000234
Chris Lattner0c08d092010-04-03 22:28:33 +0000235 printOffset(MO.getOffset(), O);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000236 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000237 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000238
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000239 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000240 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000241 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000242 }
243}
244
Chris Lattnere3f01572006-02-23 19:31:10 +0000245/// PrintAsmOperand - Print out an operand for an inline asm expression.
246///
247bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000248 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000249 const char *ExtraCode, raw_ostream &O) {
Chris Lattnere3f01572006-02-23 19:31:10 +0000250 // Does this asm operand have a single letter operand modifier?
251 if (ExtraCode && ExtraCode[0]) {
252 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000253
Chris Lattnere3f01572006-02-23 19:31:10 +0000254 switch (ExtraCode[0]) {
Jack Carter0518fca2012-06-26 13:49:27 +0000255 default:
256 // See if this is a generic print operand
257 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
Chris Lattner78192b62007-01-25 02:52:50 +0000258 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnercb22fa62010-11-15 03:39:06 +0000259 break; // PPC never has a prefix.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000260 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000261 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000262 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000263 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000264 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000265 return true;
266 ++OpNo; // Return the high-part.
267 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000268 case 'I':
269 // Write 'i' if an integer constant, otherwise nothing. Used to print
270 // addi vs add, etc.
Dan Gohmand735b802008-10-03 15:45:36 +0000271 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000272 O << "i";
273 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000274 }
275 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000276
Chris Lattner35c33bd2010-04-04 04:47:45 +0000277 printOperand(MI, OpNo, O);
Chris Lattnere3f01572006-02-23 19:31:10 +0000278 return false;
279}
280
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000281// At the moment, all inline asm memory operands are a single register.
282// In any case, the output of this routine should always be just one
283// assembler operand.
284
Chris Lattner2c003e22006-02-24 20:27:40 +0000285bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000286 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000287 const char *ExtraCode,
288 raw_ostream &O) {
Hal Finkel827b7a02012-11-05 18:18:42 +0000289 if (ExtraCode && ExtraCode[0]) {
290 if (ExtraCode[1] != 0) return true; // Unknown modifier.
291
292 switch (ExtraCode[0]) {
293 default: return true; // Unknown modifier.
294 case 'y': // A memory reference for an X-form instruction
295 {
296 const char *RegName = "r0";
297 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
298 O << RegName << ", ";
299 printOperand(MI, OpNo, O);
300 return false;
301 }
302 }
303 }
304
Chris Lattnercb22fa62010-11-15 03:39:06 +0000305 assert(MI->getOperand(OpNo).isReg());
Dale Johannesen4e68f882009-08-26 18:10:32 +0000306 O << "0(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000307 printOperand(MI, OpNo, O);
Dale Johannesen4e68f882009-08-26 18:10:32 +0000308 O << ")";
Chris Lattner2c003e22006-02-24 20:27:40 +0000309 return false;
310}
Chris Lattnere3f01572006-02-23 19:31:10 +0000311
Chris Lattneraf53a872006-11-04 05:27:39 +0000312
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000313/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Nate Begemane59bf592004-08-14 22:09:10 +0000314/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000315///
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000316void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattnercb22fa62010-11-15 03:39:06 +0000317 MCInst TmpInst;
318
319 // Lower multi-instruction pseudo operations.
320 switch (MI->getOpcode()) {
321 default: break;
Chris Lattnerf3b6e062010-11-15 07:52:06 +0000322 case TargetOpcode::DBG_VALUE: {
323 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return;
324
325 SmallString<32> Str;
326 raw_svector_ostream O(Str);
327 unsigned NOps = MI->getNumOperands();
328 assert(NOps==4);
329 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
330 // cast away const; DIetc do not take const operands for some reason.
331 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
332 O << V.getName();
333 O << " <- ";
334 // Frame address. Currently handles register +- offset only.
335 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
336 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O);
337 O << ']';
338 O << "+";
339 printOperand(MI, NOps-2, O);
340 OutStreamer.EmitRawText(O.str());
341 return;
342 }
343
Chris Lattnercb22fa62010-11-15 03:39:06 +0000344 case PPC::MovePCtoLR:
345 case PPC::MovePCtoLR8: {
346 // Transform %LR = MovePCtoLR
347 // Into this, where the label is the PIC base:
348 // bl L1$pb
349 // L1$pb:
350 MCSymbol *PICBase = MF->getPICBaseSymbol();
Chris Lattner2ead4582010-11-14 22:03:15 +0000351
Chris Lattnercb22fa62010-11-15 03:39:06 +0000352 // Emit the 'bl'.
Benjamin Kramered9e4422012-11-26 18:05:52 +0000353 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL_Darwin) // Darwin vs SVR4 doesn't matter here.
Benjamin Kramer391271f2012-11-26 13:34:22 +0000354 // FIXME: We would like an efficient form for this, so we don't have to do
355 // a lot of extra uniquing.
Benjamin Kramered9e4422012-11-26 18:05:52 +0000356 .addExpr(MCSymbolRefExpr::Create(PICBase, OutContext)));
Chris Lattnercb22fa62010-11-15 03:39:06 +0000357
358 // Emit the label.
359 OutStreamer.EmitLabel(PICBase);
360 return;
361 }
Roman Divacky9fb8b492012-08-24 16:26:02 +0000362 case PPC::LDtocJTI:
363 case PPC::LDtocCPT:
Chris Lattnercb22fa62010-11-15 03:39:06 +0000364 case PPC::LDtoc: {
365 // Transform %X3 = LDtoc <ga:@min1>, %X2
Roman Divackya1000742011-06-09 20:25:38 +0000366 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Roman Divacky9fb8b492012-08-24 16:26:02 +0000367
Chris Lattnercb22fa62010-11-15 03:39:06 +0000368 // Change the opcode to LD, and the global address operand to be a
369 // reference to the TOC entry we will synthesize later.
370 TmpInst.setOpcode(PPC::LD);
371 const MachineOperand &MO = MI->getOperand(1);
Roman Divacky9fb8b492012-08-24 16:26:02 +0000372
373 // Map symbol -> label of TOC entry
374 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI());
375 MCSymbol *MOSymbol = 0;
376 if (MO.isGlobal())
377 MOSymbol = Mang->getSymbol(MO.getGlobal());
378 else if (MO.isCPI())
379 MOSymbol = GetCPISymbol(MO.getIndex());
380 else if (MO.isJTI())
381 MOSymbol = GetJTISymbol(MO.getIndex());
382 MCSymbol *&TOCEntry = TOC[MOSymbol];
Roman Divackyf145c132012-09-18 17:10:37 +0000383 // To avoid name clash check if the name already exists.
384 while (TOCEntry == 0) {
385 if (OutContext.LookupSymbol(Twine(MAI->getPrivateGlobalPrefix()) +
386 "C" + Twine(TOCLabelID++)) == 0) {
387 TOCEntry = GetTempSymbol("C", TOCLabelID);
388 }
389 }
390
Chris Lattnercb22fa62010-11-15 03:39:06 +0000391 const MCExpr *Exp =
Roman Divacky9fb8b492012-08-24 16:26:02 +0000392 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC_ENTRY,
Chris Lattnercb22fa62010-11-15 03:39:06 +0000393 OutContext);
394 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
Chris Lattner293ef9a2010-11-14 19:53:02 +0000395 OutStreamer.EmitInstruction(TmpInst);
396 return;
397 }
Chris Lattnercb22fa62010-11-15 03:39:06 +0000398
399 case PPC::MFCRpseud:
Hal Finkel234bb382011-12-07 06:34:06 +0000400 case PPC::MFCR8pseud:
Chris Lattnercb22fa62010-11-15 03:39:06 +0000401 // Transform: %R3 = MFCRpseud %CR7
402 // Into: %R3 = MFCR ;; cr7
403 OutStreamer.AddComment(PPCInstPrinter::
404 getRegisterName(MI->getOperand(1).getReg()));
Benjamin Kramered9e4422012-11-26 18:05:52 +0000405 OutStreamer.EmitInstruction(MCInstBuilder(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR)
406 .addReg(MI->getOperand(0).getReg()));
Dale Johannesenefc3a632010-04-26 20:05:01 +0000407 return;
Hal Finkelc6d08f12011-10-17 04:03:49 +0000408 case PPC::SYNC:
409 // In Book E sync is called msync, handle this special case here...
410 if (Subtarget.isBookE()) {
411 OutStreamer.EmitRawText(StringRef("\tmsync"));
412 return;
413 }
Dale Johannesenefc3a632010-04-26 20:05:01 +0000414 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000415
Roman Divackya1000742011-06-09 20:25:38 +0000416 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Chris Lattnercb22fa62010-11-15 03:39:06 +0000417 OutStreamer.EmitInstruction(TmpInst);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000418}
419
Chris Lattner2cf72512010-01-27 07:21:55 +0000420void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
421 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
422 return AsmPrinter::EmitFunctionEntryLabel();
423
424 // Emit an official procedure descriptor.
Roman Divacky4328f9f2012-02-27 20:20:47 +0000425 const MCSection *Current = OutStreamer.getCurrentSection();
426 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
427 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
428 SectionKind::getReadOnly());
429 OutStreamer.SwitchSection(Section);
Chris Lattner2cf72512010-01-27 07:21:55 +0000430 OutStreamer.EmitLabel(CurrentFnSym);
Roman Divacky4328f9f2012-02-27 20:20:47 +0000431 OutStreamer.EmitValueToAlignment(8);
432 MCSymbol *Symbol1 =
433 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000434 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
435 // entry point.
Roman Divacky4328f9f2012-02-27 20:20:47 +0000436 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
Roman Divacky94b17f32012-09-03 16:55:42 +0000437 8/*size*/, 0/*addrspace*/);
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000438 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC."));
439 // Generates a R_PPC64_TOC relocation for TOC base insertion.
440 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2,
441 MCSymbolRefExpr::VK_PPC_TOC, OutContext),
Roman Divacky94b17f32012-09-03 16:55:42 +0000442 8/*size*/, 0/*addrspace*/);
Roman Divacky4cd56012012-09-18 16:55:29 +0000443 // Emit a null environment pointer.
444 OutStreamer.EmitIntValue(0, 8 /* size */, 0 /* addrspace */);
Roman Divacky4328f9f2012-02-27 20:20:47 +0000445 OutStreamer.SwitchSection(Current);
Hal Finkeld55a2662012-02-22 21:11:47 +0000446
447 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
448 ".L." + Twine(CurrentFnSym->getName()));
449 OutStreamer.EmitLabel(RealFnSym);
450 CurrentFnSymForSize = RealFnSym;
Chris Lattner2cf72512010-01-27 07:21:55 +0000451}
452
453
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000454bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000455 const DataLayout *TD = TM.getDataLayout();
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000456
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000457 bool isPPC64 = TD->getPointerSizeInBits() == 64;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000458
459 if (isPPC64 && !TOC.empty()) {
Roman Divacky21a14012012-02-28 18:15:25 +0000460 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
461 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
462 SectionKind::getReadOnly());
463 OutStreamer.SwitchSection(Section);
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000464
Ulrich Weigand95d8afc2012-11-12 19:13:24 +0000465 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
Chris Lattner75abc682010-01-16 02:09:06 +0000466 E = TOC.end(); I != E; ++I) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000467 OutStreamer.EmitLabel(I->second);
Adhemerval Zanellaf35c62b2012-10-15 15:43:14 +0000468 MCSymbol *S = OutContext.GetOrCreateSymbol(I->first->getName());
469 OutStreamer.EmitTCEntry(*S);
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000470 }
471 }
472
473 return AsmPrinter::doFinalization(M);
474}
Jim Laskeybf111822006-12-21 20:26:09 +0000475
Roman Divackyc6c2ced2012-08-28 19:06:55 +0000476/// EmitFunctionBodyEnd - Print the traceback table before the .size
477/// directive.
478///
479void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
480 // Only the 64-bit target requires a traceback table. For now,
481 // we only emit the word of zeroes that GDB requires to find
Hal Finkelbbd169b2012-08-29 20:22:24 +0000482 // the end of the function, and zeroes for the eight-byte
483 // mandatory fields.
484 // FIXME: We should fill in the eight-byte mandatory fields as described in
485 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
486 // currently make use of these fields).
487 if (Subtarget.isPPC64()) {
Roman Divackyc6c2ced2012-08-28 19:06:55 +0000488 OutStreamer.EmitIntValue(0, 4/*size*/);
Hal Finkelbbd169b2012-08-29 20:22:24 +0000489 OutStreamer.EmitIntValue(0, 8/*size*/);
490 }
Roman Divackyc6c2ced2012-08-28 19:06:55 +0000491}
492
Bob Wilson812209a2009-09-30 22:06:26 +0000493void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000494 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000495 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000496 "ppc",
Hal Finkelc6d08f12011-10-17 04:03:49 +0000497 "ppc440",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000498 "ppc601",
499 "ppc602",
500 "ppc603",
501 "ppc7400",
502 "ppc750",
503 "ppc970",
Hal Finkel4d989ac2012-04-01 19:22:40 +0000504 "ppcA2",
Hal Finkel621b77a2012-08-28 16:12:39 +0000505 "ppce500mc",
506 "ppce5500",
Hal Finkel622382f2012-06-11 15:43:08 +0000507 "power6",
508 "power7",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000509 "ppc64"
510 };
511
512 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelbd5cafd2012-06-11 19:57:01 +0000513 if (Subtarget.hasMFOCRF() && Directive < PPC::DIR_970)
Jim Laskeyc35010d2006-12-12 20:57:08 +0000514 Directive = PPC::DIR_970;
515 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
516 Directive = PPC::DIR_7400;
Eli Friedman32e698c2011-12-01 01:43:47 +0000517 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
Jim Laskeyc35010d2006-12-12 20:57:08 +0000518 Directive = PPC::DIR_64;
519 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Chris Lattnerb46443a2010-11-15 08:49:58 +0000520
521 // FIXME: This is a total hack, finish mc'izing the PPC backend.
522 if (OutStreamer.hasRawTextSupport())
523 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000524
Jim Laskey2ada0852006-11-28 18:21:52 +0000525 // Prime text sections so they are adjacent. This reduces the likelihood a
526 // large data or debug section causes a branch to exceed 16M limit.
Dan Gohman0d805c32010-04-17 16:44:48 +0000527 const TargetLoweringObjectFileMachO &TLOFMacho =
528 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000529 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskey2ada0852006-11-28 18:21:52 +0000530 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000531 OutStreamer.SwitchSection(
Chris Lattner22772212010-04-08 20:40:11 +0000532 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000533 MCSectionMachO::S_SYMBOL_STUBS |
534 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
535 32, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000536 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000537 OutStreamer.SwitchSection(
Chris Lattner22772212010-04-08 20:40:11 +0000538 OutContext.getMachOSection("__TEXT","__symbol_stub1",
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000539 MCSectionMachO::S_SYMBOL_STUBS |
540 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
541 16, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000542 }
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000543 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman18ed0292005-07-21 01:25:49 +0000544}
545
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000546static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000547 // Remove $stub suffix, add $lazy_ptr.
Benjamin Kramer8f2dce02012-11-24 13:18:11 +0000548 StringRef NoStub = Sym->getName().substr(0, Sym->getName().size()-5);
549 return Ctx.GetOrCreateSymbol(NoStub + "$lazy_ptr");
Chris Lattner73a1aa02010-01-20 21:36:48 +0000550}
551
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000552static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000553 // Add $tmp suffix to $stub, yielding $stub$tmp.
Benjamin Kramer8f2dce02012-11-24 13:18:11 +0000554 return Ctx.GetOrCreateSymbol(Sym->getName() + "$tmp");
Chris Lattner73a1aa02010-01-20 21:36:48 +0000555}
556
557void PPCDarwinAsmPrinter::
558EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000559 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
Chris Lattner917d6282010-01-20 21:19:44 +0000560
Dan Gohman0d805c32010-04-17 16:44:48 +0000561 const TargetLoweringObjectFileMachO &TLOFMacho =
562 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner917d6282010-01-20 21:19:44 +0000563
564 // .lazy_symbol_pointer
565 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattnerff4bc462009-08-10 01:39:42 +0000566
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000567 // Output stubs for dynamically-linked functions
Chris Lattner917d6282010-01-20 21:19:44 +0000568 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2dfddee2009-08-03 22:52:21 +0000569 const MCSection *StubSection =
Chris Lattner22772212010-04-08 20:40:11 +0000570 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
571 MCSectionMachO::S_SYMBOL_STUBS |
572 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
573 32, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000574 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000575 OutStreamer.SwitchSection(StubSection);
Chris Lattner7e097e42006-06-27 01:02:25 +0000576 EmitAlignment(4);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000577
Chris Lattner9d7efd32010-04-04 07:05:53 +0000578 MCSymbol *Stub = Stubs[i].first;
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000579 MCSymbol *RawSym = Stubs[i].second.getPointer();
580 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
581 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000582
Chris Lattner9d7efd32010-04-04 07:05:53 +0000583 OutStreamer.EmitLabel(Stub);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000584 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000585
586 // mflr r0
Benjamin Kramered9e4422012-11-26 18:05:52 +0000587 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R0));
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000588 // FIXME: MCize this.
Benjamin Kramer915558e2012-11-24 13:18:25 +0000589 OutStreamer.EmitRawText("\tbcl 20, 31, " + Twine(AnonSymbol->getName()));
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000590 OutStreamer.EmitLabel(AnonSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000591 // mflr r11
Benjamin Kramered9e4422012-11-26 18:05:52 +0000592 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R11));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000593 // addis r11, r11, ha16(LazyPtr - AnonSymbol)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000594 const MCExpr *Sub =
595 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(LazyPtr, OutContext),
596 MCSymbolRefExpr::Create(AnonSymbol, OutContext),
597 OutContext);
Benjamin Kramered9e4422012-11-26 18:05:52 +0000598 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000599 .addReg(PPC::R11)
600 .addReg(PPC::R11)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000601 .addExpr(Sub));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000602 // mtlr r0
Benjamin Kramered9e4422012-11-26 18:05:52 +0000603 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTLR).addReg(PPC::R0));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000604
Benjamin Kramer391271f2012-11-26 13:34:22 +0000605 // ldu r12, lo16(LazyPtr - AnonSymbol)(r11)
606 // lwzu r12, lo16(LazyPtr - AnonSymbol)(r11)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000607 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000608 .addReg(PPC::R12)
609 .addExpr(Sub).addExpr(Sub)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000610 .addReg(PPC::R11));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000611 // mtctr r12
Benjamin Kramered9e4422012-11-26 18:05:52 +0000612 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000613 // bctr
Benjamin Kramered9e4422012-11-26 18:05:52 +0000614 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000615
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000616 OutStreamer.SwitchSection(LSPSection);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000617 OutStreamer.EmitLabel(LazyPtr);
618 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000619
620 MCSymbol *DyldStubBindingHelper =
621 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
622 if (isPPC64) {
623 // .quad dyld_stub_binding_helper
624 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
625 } else {
626 // .long dyld_stub_binding_helper
627 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
628 }
Chris Lattnerdeea4162005-12-13 04:33:58 +0000629 }
Chris Lattner9d7efd32010-04-04 07:05:53 +0000630 OutStreamer.AddBlankLine();
Chris Lattner917d6282010-01-20 21:19:44 +0000631 return;
Misha Brukman46fd00a2004-06-24 23:04:11 +0000632 }
Chris Lattner917d6282010-01-20 21:19:44 +0000633
634 const MCSection *StubSection =
Chris Lattner22772212010-04-08 20:40:11 +0000635 OutContext.getMachOSection("__TEXT","__symbol_stub1",
636 MCSectionMachO::S_SYMBOL_STUBS |
637 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
638 16, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000639 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000640 MCSymbol *Stub = Stubs[i].first;
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000641 MCSymbol *RawSym = Stubs[i].second.getPointer();
642 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000643
Chris Lattner917d6282010-01-20 21:19:44 +0000644 OutStreamer.SwitchSection(StubSection);
645 EmitAlignment(4);
Chris Lattner9d7efd32010-04-04 07:05:53 +0000646 OutStreamer.EmitLabel(Stub);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000647 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000648 // lis r11, ha16(LazyPtr)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000649 const MCExpr *LazyPtrHa16 =
650 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_HA16,
651 OutContext);
Benjamin Kramered9e4422012-11-26 18:05:52 +0000652 OutStreamer.EmitInstruction(MCInstBuilder(PPC::LIS)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000653 .addReg(PPC::R11)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000654 .addExpr(LazyPtrHa16));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000655
Benjamin Kramer391271f2012-11-26 13:34:22 +0000656 const MCExpr *LazyPtrLo16 =
657 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_LO16,
658 OutContext);
659 // ldu r12, lo16(LazyPtr)(r11)
660 // lwzu r12, lo16(LazyPtr)(r11)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000661 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000662 .addReg(PPC::R12)
663 .addExpr(LazyPtrLo16).addExpr(LazyPtrLo16)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000664 .addReg(PPC::R11));
Benjamin Kramer391271f2012-11-26 13:34:22 +0000665
Benjamin Kramer915558e2012-11-24 13:18:25 +0000666 // mtctr r12
Benjamin Kramered9e4422012-11-26 18:05:52 +0000667 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000668 // bctr
Benjamin Kramered9e4422012-11-26 18:05:52 +0000669 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
Benjamin Kramer391271f2012-11-26 13:34:22 +0000670
Chris Lattner917d6282010-01-20 21:19:44 +0000671 OutStreamer.SwitchSection(LSPSection);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000672 OutStreamer.EmitLabel(LazyPtr);
673 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000674
675 MCSymbol *DyldStubBindingHelper =
676 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
677 if (isPPC64) {
678 // .quad dyld_stub_binding_helper
679 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
680 } else {
681 // .long dyld_stub_binding_helper
682 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
683 }
Chris Lattner917d6282010-01-20 21:19:44 +0000684 }
685
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000686 OutStreamer.AddBlankLine();
Chris Lattner917d6282010-01-20 21:19:44 +0000687}
688
689
690bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000691 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
Chris Lattner917d6282010-01-20 21:19:44 +0000692
693 // Darwin/PPC always uses mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000694 const TargetLoweringObjectFileMachO &TLOFMacho =
695 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner917d6282010-01-20 21:19:44 +0000696 MachineModuleInfoMachO &MMIMacho =
697 MMI->getObjFileInfo<MachineModuleInfoMachO>();
698
Chris Lattner73a1aa02010-01-20 21:36:48 +0000699 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
700 if (!Stubs.empty())
701 EmitFunctionStubs(Stubs);
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000702
Chris Lattner33adcfb2009-08-22 21:43:10 +0000703 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000704 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000705 // Only referenced functions get into the Personalities list.
Dan Gohman46510a72010-04-15 01:51:59 +0000706 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
707 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +0000708 E = Personalities.end(); I != E; ++I) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000709 if (*I) {
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000710 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000711 MachineModuleInfoImpl::StubValueTy &StubSym =
712 MMIMacho.getGVStubEntry(NLPSym);
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000713 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000714 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000715 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000716 }
717
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000718 // Output stubs for dynamically-linked functions.
Chris Lattner73a1aa02010-01-20 21:36:48 +0000719 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000720
Chris Lattner2dfddee2009-08-03 22:52:21 +0000721 // Output macho stubs for external and common global variables.
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000722 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000723 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000724 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnere1e8a822009-08-10 17:58:51 +0000725 EmitAlignment(isPPC64 ? 3 : 2);
726
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000727 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +0000728 // L_foo$stub:
729 OutStreamer.EmitLabel(Stubs[i].first);
730 // .indirect_symbol _foo
731 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
732 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendling53351a12010-03-12 02:00:43 +0000733
734 if (MCSym.getInt())
735 // External to current translation unit.
736 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
737 else
738 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000739 //
740 // When we place the LSDA into the TEXT section, the type info pointers
741 // need to be indirect and pc-rel. We accomplish this by using NLPs.
742 // However, sometimes the types are local to the file. So we need to
743 // fill in the value for the NLP in those cases.
Bill Wendling53351a12010-03-12 02:00:43 +0000744 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
745 OutContext),
746 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000747 }
Bill Wendling08d726c2010-03-11 23:39:44 +0000748
749 Stubs.clear();
750 OutStreamer.AddBlankLine();
Nate Begemane59bf592004-08-14 22:09:10 +0000751 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000752
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000753 Stubs = MMIMacho.GetHiddenGVStubList();
754 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000755 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000756 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000757
758 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +0000759 // L_foo$stub:
760 OutStreamer.EmitLabel(Stubs[i].first);
761 // .long _foo
762 OutStreamer.EmitValue(MCSymbolRefExpr::
763 Create(Stubs[i].second.getPointer(),
764 OutContext),
765 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000766 }
Bill Wendling08d726c2010-03-11 23:39:44 +0000767
768 Stubs.clear();
769 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000770 }
771
Chris Lattner5b0ac992005-11-01 00:12:36 +0000772 // Funny Darwin hack: This flag tells the linker that no global symbols
773 // contain code that falls through to other global symbols (e.g. the obvious
774 // implementation of multiple entry points). If this doesn't occur, the
775 // linker can safely perform dead code stripping. Since LLVM never generates
776 // code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000777 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Chris Lattner5b0ac992005-11-01 00:12:36 +0000778
Dan Gohmanb8275a32007-07-25 19:33:14 +0000779 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000780}
Nate Begemaned428532004-09-04 05:00:00 +0000781
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000782/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
783/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +0000784/// Darwin assembler can deal with.
785///
Chris Lattnerb23569a2010-04-04 08:18:47 +0000786static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
Chris Lattner11d53c12010-03-13 20:55:24 +0000787 MCStreamer &Streamer) {
Jim Laskeybf111822006-12-21 20:26:09 +0000788 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
789
Chris Lattner40bbebd2009-07-21 18:38:57 +0000790 if (Subtarget->isDarwin())
Chris Lattnerb23569a2010-04-04 08:18:47 +0000791 return new PPCDarwinAsmPrinter(tm, Streamer);
792 return new PPCLinuxAsmPrinter(tm, Streamer);
Chris Lattner4da1c822006-09-20 17:12:19 +0000793}
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000794
Bob Wilsona96751f2009-06-23 23:59:40 +0000795// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000796extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000797 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000798 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
799}