blob: 3c7cc4ed0eb31ee5434b3391e3bff2da844c7ad3 [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"
Craig Topper79aa3412012-03-17 18:46:09 +000021#include "InstPrinter/PPCInstPrinter.h"
Evan Cheng94b95502011-07-26 00:24:13 +000022#include "MCTargetDesc/PPCPredicates.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "PPCSubtarget.h"
24#include "PPCTargetMachine.h"
25#include "llvm/ADT/MapVector.h"
26#include "llvm/ADT/SmallString.h"
27#include "llvm/ADT/StringExtras.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000028#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000029#include "llvm/CodeGen/AsmPrinter.h"
Misha Brukman05794492004-06-24 17:31:42 +000030#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000031#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling381802f2008-01-26 06:51:24 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerd3ec0b52010-01-20 21:16:14 +000033#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000034#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000036#include "llvm/IR/Constants.h"
37#include "llvm/IR/DerivedTypes.h"
38#include "llvm/IR/Module.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattner0b4bad52010-01-13 19:00:57 +000040#include "llvm/MC/MCContext.h"
Bill Wendling08d726c2010-03-11 23:39:44 +000041#include "llvm/MC/MCExpr.h"
Chris Lattner293ef9a2010-11-14 19:53:02 +000042#include "llvm/MC/MCInst.h"
Benjamin Kramer391271f2012-11-26 13:34:22 +000043#include "llvm/MC/MCInstBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000044#include "llvm/MC/MCSectionELF.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000045#include "llvm/MC/MCSectionMachO.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000046#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000047#include "llvm/MC/MCSymbol.h"
Chris Lattner293ef9a2010-11-14 19:53:02 +000048#include "llvm/Support/CommandLine.h"
Devang Patel40e0bad2010-08-04 22:07:50 +000049#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000050#include "llvm/Support/ELF.h"
Torok Edwindac237e2009-07-08 20:53:28 +000051#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000052#include "llvm/Support/MathExtras.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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000055#include "llvm/Target/Mangler.h"
56#include "llvm/Target/TargetInstrInfo.h"
57#include "llvm/Target/TargetOptions.h"
58#include "llvm/Target/TargetRegisterInfo.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
Bill Schmidt34a9d4b2012-11-27 17:35:46 +000076 MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym);
Nate Begemane59bf592004-08-14 22:09:10 +000077
Chris Lattnerd49fe1b2010-01-28 01:28:58 +000078 virtual void EmitInstruction(const MachineInstr *MI);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000079
Chris Lattnercb22fa62010-11-15 03:39:06 +000080 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000081
Chris Lattner58873272006-02-01 22:38:46 +000082 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000083 unsigned AsmVariant, const char *ExtraCode,
84 raw_ostream &O);
Chris Lattner2c003e22006-02-24 20:27:40 +000085 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000086 unsigned AsmVariant, const char *ExtraCode,
87 raw_ostream &O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000088
Devang Patel40e0bad2010-08-04 22:07:50 +000089 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
Devang Patel40e0bad2010-08-04 22:07:50 +000090 MachineLocation Location;
Chris Lattnercb22fa62010-11-15 03:39:06 +000091 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
Devang Patel40e0bad2010-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 Begemaned428532004-09-04 05:00:00 +0000100 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000101
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000102 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000103 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000104 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +0000105 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
106 : PPCAsmPrinter(TM, Streamer) {}
Jim Laskeybf111822006-12-21 20:26:09 +0000107
108 virtual const char *getPassName() const {
109 return "Linux PPC Assembly Printer";
110 }
111
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000112 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000113
Chris Lattner2cf72512010-01-27 07:21:55 +0000114 virtual void EmitFunctionEntryLabel();
Roman Divackyc6c2ced2012-08-28 19:06:55 +0000115
116 void EmitFunctionBodyEnd();
Jim Laskeybf111822006-12-21 20:26:09 +0000117 };
118
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000119 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
120 /// OS X
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000121 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000122 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +0000123 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
124 : PPCAsmPrinter(TM, Streamer) {}
Nate Begemaned428532004-09-04 05:00:00 +0000125
126 virtual const char *getPassName() const {
127 return "Darwin PPC Assembly Printer";
128 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000129
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000130 bool doFinalization(Module &M);
Bob Wilson812209a2009-09-30 22:06:26 +0000131 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000132
Chris Lattner73a1aa02010-01-20 21:36:48 +0000133 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000134 };
135} // end of anonymous namespace
136
Chris Lattnercb22fa62010-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 Begemane59bf592004-08-14 22:09:10 +0000149
Chris Lattnercb22fa62010-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 Brukman5dfe3a92004-06-21 16:55:25 +0000154 switch (MO.getType()) {
Chris Lattnercb22fa62010-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 Lattner63b3d712006-05-04 17:21:20 +0000163 case MachineOperand::MO_Immediate:
Chris Lattnercb22fa62010-11-15 03:39:06 +0000164 O << MO.getImm();
165 return;
Misha Brukman97a296f2004-07-21 20:11:11 +0000166
Nate Begeman37efe672006-04-22 18:53:45 +0000167 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000168 O << *MO.getMBB()->getSymbol();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000169 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000170 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000171 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000172 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000173 // FIXME: PIC relocation model
174 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000175 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000176 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000177 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000178 return;
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000179 case MachineOperand::MO_BlockAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +0000180 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000181 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000182 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000183 // Computing the address of an external symbol, not calling it.
Chris Lattner75abc682010-01-16 02:09:06 +0000184 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000185 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner75abc682010-01-16 02:09:06 +0000186 return;
Chris Lattnera637c322005-12-16 00:22:14 +0000187 }
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000188
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000189 MCSymbol *NLPSym =
Chris Lattner75abc682010-01-16 02:09:06 +0000190 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
191 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000192 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000193 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000194 if (StubSym.getPointer() == 0)
195 StubSym = MachineModuleInfoImpl::
196 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000197
Chris Lattner10b318b2010-01-17 21:43:43 +0000198 O << *NLPSym;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000199 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000200 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000201 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000202 // Computing the address of a global symbol, not calling it.
Dan Gohman46510a72010-04-15 01:51:59 +0000203 const GlobalValue *GV = MO.getGlobal();
Chris Lattnera40128c2010-01-16 02:00:23 +0000204 MCSymbol *SymToPrint;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000205
Nate Begemanfcf4a422004-10-17 23:01:34 +0000206 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000207 if (TM.getRelocationModel() != Reloc::Static &&
208 (GV->isDeclaration() || GV->isWeakForLinker())) {
209 if (!GV->hasHiddenVisibility()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000210 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-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 Lattnerd62f1b42010-03-12 21:19:23 +0000216 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000217 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
218 GV->hasAvailableExternallyLinkage()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000219 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000220
Bill Wendlingcebae362010-03-10 22:34:10 +0000221 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000222 MMI->getObjFileInfo<MachineModuleInfoMachO>().
223 getHiddenGVStubEntry(SymToPrint);
Bill Wendlingcebae362010-03-10 22:34:10 +0000224 if (StubSym.getPointer() == 0)
225 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000226 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000227 } else {
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000228 SymToPrint = Mang->getSymbol(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000229 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000230 } else {
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000231 SymToPrint = Mang->getSymbol(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000232 }
Chris Lattnera40128c2010-01-16 02:00:23 +0000233
Chris Lattner10b318b2010-01-17 21:43:43 +0000234 O << *SymToPrint;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000235
Chris Lattner0c08d092010-04-03 22:28:33 +0000236 printOffset(MO.getOffset(), O);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000237 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000238 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000239
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000240 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000241 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000242 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000243 }
244}
245
Chris Lattnere3f01572006-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 Korobeynikov34da1272008-08-08 18:22:59 +0000249 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000250 const char *ExtraCode, raw_ostream &O) {
Chris Lattnere3f01572006-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 Korobeynikov34da1272008-08-08 18:22:59 +0000254
Chris Lattnere3f01572006-02-23 19:31:10 +0000255 switch (ExtraCode[0]) {
Jack Carter0518fca2012-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 Lattner78192b62007-01-25 02:52:50 +0000259 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnercb22fa62010-11-15 03:39:06 +0000260 break; // PPC never has a prefix.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000261 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000262 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000263 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000264 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000265 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000266 return true;
267 ++OpNo; // Return the high-part.
268 break;
Chris Lattner6c2d2602007-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 Gohmand735b802008-10-03 15:45:36 +0000272 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000273 O << "i";
274 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000275 }
276 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000277
Chris Lattner35c33bd2010-04-04 04:47:45 +0000278 printOperand(MI, OpNo, O);
Chris Lattnere3f01572006-02-23 19:31:10 +0000279 return false;
280}
281
Dale Johannesen5cfd4dd2009-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 Lattner2c003e22006-02-24 20:27:40 +0000286bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000287 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000288 const char *ExtraCode,
289 raw_ostream &O) {
Hal Finkel827b7a02012-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 Lattnercb22fa62010-11-15 03:39:06 +0000306 assert(MI->getOperand(OpNo).isReg());
Dale Johannesen4e68f882009-08-26 18:10:32 +0000307 O << "0(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000308 printOperand(MI, OpNo, O);
Dale Johannesen4e68f882009-08-26 18:10:32 +0000309 O << ")";
Chris Lattner2c003e22006-02-24 20:27:40 +0000310 return false;
311}
Chris Lattnere3f01572006-02-23 19:31:10 +0000312
Chris Lattneraf53a872006-11-04 05:27:39 +0000313
Bill Schmidt34a9d4b2012-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 Lattnerd49fe1b2010-01-28 01:28:58 +0000333/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Nate Begemane59bf592004-08-14 22:09:10 +0000334/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000335///
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000336void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattnercb22fa62010-11-15 03:39:06 +0000337 MCInst TmpInst;
338
339 // Lower multi-instruction pseudo operations.
340 switch (MI->getOpcode()) {
341 default: break;
Chris Lattnerf3b6e062010-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 Lattnercb22fa62010-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 Lattner2ead4582010-11-14 22:03:15 +0000371
Chris Lattnercb22fa62010-11-15 03:39:06 +0000372 // Emit the 'bl'.
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000373 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL)
Benjamin Kramer391271f2012-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 Kramered9e4422012-11-26 18:05:52 +0000376 .addExpr(MCSymbolRefExpr::Create(PICBase, OutContext)));
Chris Lattnercb22fa62010-11-15 03:39:06 +0000377
378 // Emit the label.
379 OutStreamer.EmitLabel(PICBase);
380 return;
381 }
Roman Divacky9fb8b492012-08-24 16:26:02 +0000382 case PPC::LDtocJTI:
383 case PPC::LDtocCPT:
Chris Lattnercb22fa62010-11-15 03:39:06 +0000384 case PPC::LDtoc: {
385 // Transform %X3 = LDtoc <ga:@min1>, %X2
Roman Divackya1000742011-06-09 20:25:38 +0000386 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Roman Divacky9fb8b492012-08-24 16:26:02 +0000387
Chris Lattnercb22fa62010-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 Divacky9fb8b492012-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 Schmidt34a9d4b2012-11-27 17:35:46 +0000402
403 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
Roman Divackyf145c132012-09-18 17:10:37 +0000404
Chris Lattnercb22fa62010-11-15 03:39:06 +0000405 const MCExpr *Exp =
Roman Divacky9fb8b492012-08-24 16:26:02 +0000406 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC_ENTRY,
Chris Lattnercb22fa62010-11-15 03:39:06 +0000407 OutContext);
408 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
Chris Lattner293ef9a2010-11-14 19:53:02 +0000409 OutStreamer.EmitInstruction(TmpInst);
410 return;
411 }
Chris Lattnercb22fa62010-11-15 03:39:06 +0000412
Bill Schmidt34a9d4b2012-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;
Bill Schmidt5b7f9212013-01-07 19:29:18 +0000429 bool IsAvailExt = false;
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000430
431 if (MO.isGlobal()) {
432 const GlobalValue *GValue = MO.getGlobal();
Bill Schmidt5b7f9212013-01-07 19:29:18 +0000433 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
434 const GlobalValue *RealGValue = GAlias ?
435 GAlias->resolveAliasedGlobal(false) : GValue;
436 MOSymbol = Mang->getSymbol(RealGValue);
437 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000438 IsExternal = GVar && !GVar->hasInitializer();
Bill Schmidt5b7f9212013-01-07 19:29:18 +0000439 IsCommon = GVar && RealGValue->hasCommonLinkage();
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000440 IsFunction = !GVar;
Bill Schmidt5b7f9212013-01-07 19:29:18 +0000441 IsAvailExt = GVar && RealGValue->hasAvailableExternallyLinkage();
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000442 } else if (MO.isCPI())
443 MOSymbol = GetCPISymbol(MO.getIndex());
444 else if (MO.isJTI())
445 MOSymbol = GetJTISymbol(MO.getIndex());
446
Bill Schmidt5b7f9212013-01-07 19:29:18 +0000447 if (IsExternal || IsFunction || IsCommon || IsAvailExt || MO.isJTI())
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000448 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
449
450 const MCExpr *Exp =
451 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_HA,
452 OutContext);
453 TmpInst.getOperand(2) = MCOperand::CreateExpr(Exp);
454 OutStreamer.EmitInstruction(TmpInst);
455 return;
456 }
457 case PPC::LDtocL: {
458 // Transform %Xd = LDtocL <ga:@sym>, %Xs
459 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
460
Ulrich Weigandd67768d2013-03-26 10:55:45 +0000461 // Change the opcode to LD. If the global address is external, has
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000462 // common linkage, or is a jump table address, then reference the
463 // associated TOC entry. Otherwise reference the symbol directly.
Ulrich Weigandd67768d2013-03-26 10:55:45 +0000464 TmpInst.setOpcode(PPC::LD);
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000465 const MachineOperand &MO = MI->getOperand(1);
Bill Schmidt53b0b0e2013-02-21 17:12:27 +0000466 assert((MO.isGlobal() || MO.isJTI() || MO.isCPI()) &&
467 "Invalid operand for LDtocL!");
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000468 MCSymbol *MOSymbol = 0;
469
470 if (MO.isJTI())
471 MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex()));
Bill Schmidt53b0b0e2013-02-21 17:12:27 +0000472 else if (MO.isCPI())
473 MOSymbol = GetCPISymbol(MO.getIndex());
474 else if (MO.isGlobal()) {
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000475 const GlobalValue *GValue = MO.getGlobal();
Bill Schmidt5b7f9212013-01-07 19:29:18 +0000476 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
477 const GlobalValue *RealGValue = GAlias ?
478 GAlias->resolveAliasedGlobal(false) : GValue;
479 MOSymbol = Mang->getSymbol(RealGValue);
480 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000481
Bill Schmidt5b7f9212013-01-07 19:29:18 +0000482 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
483 RealGValue->hasAvailableExternallyLinkage())
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000484 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
485 }
486
487 const MCExpr *Exp =
488 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_LO,
489 OutContext);
490 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
491 OutStreamer.EmitInstruction(TmpInst);
492 return;
493 }
494 case PPC::ADDItocL: {
495 // Transform %Xd = ADDItocL %Xs, <ga:@sym>
496 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
497
Ulrich Weigand2b0850b2013-03-26 10:55:20 +0000498 // Change the opcode to ADDI8. If the global address is external, then
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000499 // generate a TOC entry and reference that. Otherwise reference the
500 // symbol directly.
Ulrich Weigand2b0850b2013-03-26 10:55:20 +0000501 TmpInst.setOpcode(PPC::ADDI8);
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000502 const MachineOperand &MO = MI->getOperand(2);
503 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL");
504 MCSymbol *MOSymbol = 0;
505 bool IsExternal = false;
506 bool IsFunction = false;
507
508 if (MO.isGlobal()) {
509 const GlobalValue *GValue = MO.getGlobal();
Bill Schmidt5b7f9212013-01-07 19:29:18 +0000510 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
511 const GlobalValue *RealGValue = GAlias ?
512 GAlias->resolveAliasedGlobal(false) : GValue;
513 MOSymbol = Mang->getSymbol(RealGValue);
514 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
Bill Schmidt34a9d4b2012-11-27 17:35:46 +0000515 IsExternal = GVar && !GVar->hasInitializer();
516 IsFunction = !GVar;
517 } else if (MO.isCPI())
518 MOSymbol = GetCPISymbol(MO.getIndex());
519
520 if (IsFunction || IsExternal)
521 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
522
523 const MCExpr *Exp =
524 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC16_LO,
525 OutContext);
526 TmpInst.getOperand(2) = MCOperand::CreateExpr(Exp);
527 OutStreamer.EmitInstruction(TmpInst);
528 return;
529 }
Bill Schmidtb453e162012-12-14 17:02:38 +0000530 case PPC::ADDISgotTprelHA: {
531 // Transform: %Xd = ADDISgotTprelHA %X2, <ga:@sym>
532 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
533 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
534 const MachineOperand &MO = MI->getOperand(2);
535 const GlobalValue *GValue = MO.getGlobal();
536 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
537 const MCExpr *SymGotTprel =
538 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA,
539 OutContext);
540 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
541 .addReg(MI->getOperand(0).getReg())
542 .addReg(PPC::X2)
543 .addExpr(SymGotTprel));
544 return;
545 }
546 case PPC::LDgotTprelL: {
547 // Transform %Xd = LDgotTprelL <ga:@sym>, %Xs
Bill Schmidtd7802bf2012-12-04 16:18:08 +0000548 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
549
Ulrich Weigandd67768d2013-03-26 10:55:45 +0000550 // Change the opcode to LD.
551 TmpInst.setOpcode(PPC::LD);
Bill Schmidtd7802bf2012-12-04 16:18:08 +0000552 const MachineOperand &MO = MI->getOperand(1);
553 const GlobalValue *GValue = MO.getGlobal();
554 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
555 const MCExpr *Exp =
Bill Schmidtb453e162012-12-14 17:02:38 +0000556 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO,
Bill Schmidtd7802bf2012-12-04 16:18:08 +0000557 OutContext);
558 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
559 OutStreamer.EmitInstruction(TmpInst);
560 return;
561 }
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000562 case PPC::ADDIStlsgdHA: {
563 // Transform: %Xd = ADDIStlsgdHA %X2, <ga:@sym>
564 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha
565 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
566 const MachineOperand &MO = MI->getOperand(2);
567 const GlobalValue *GValue = MO.getGlobal();
568 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
569 const MCExpr *SymGotTlsGD =
570 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA,
571 OutContext);
572 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
573 .addReg(MI->getOperand(0).getReg())
574 .addReg(PPC::X2)
575 .addExpr(SymGotTlsGD));
576 return;
577 }
578 case PPC::ADDItlsgdL: {
579 // Transform: %Xd = ADDItlsgdL %Xs, <ga:@sym>
Ulrich Weigand2b0850b2013-03-26 10:55:20 +0000580 // Into: %Xd = ADDI8 %Xs, sym@got@tlsgd@l
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000581 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
582 const MachineOperand &MO = MI->getOperand(2);
583 const GlobalValue *GValue = MO.getGlobal();
584 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
585 const MCExpr *SymGotTlsGD =
586 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO,
587 OutContext);
Ulrich Weigand2b0850b2013-03-26 10:55:20 +0000588 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000589 .addReg(MI->getOperand(0).getReg())
590 .addReg(MI->getOperand(1).getReg())
591 .addExpr(SymGotTlsGD));
592 return;
593 }
594 case PPC::GETtlsADDR: {
595 // Transform: %X3 = GETtlsADDR %X3, <ga:@sym>
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000596 // Into: BL8_NOP_TLSGD __tls_get_addr(sym@tlsgd)
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000597 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
598
599 StringRef Name = "__tls_get_addr";
600 MCSymbol *TlsGetAddr = OutContext.GetOrCreateSymbol(Name);
601 const MCSymbolRefExpr *TlsRef =
602 MCSymbolRefExpr::Create(TlsGetAddr, MCSymbolRefExpr::VK_None, OutContext);
603 const MachineOperand &MO = MI->getOperand(2);
604 const GlobalValue *GValue = MO.getGlobal();
605 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
606 const MCExpr *SymVar =
607 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TLSGD,
608 OutContext);
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000609 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL8_NOP_TLSGD)
Bill Schmidt57ac1f42012-12-11 20:30:11 +0000610 .addExpr(TlsRef)
611 .addExpr(SymVar));
612 return;
613 }
Bill Schmidt349c2782012-12-12 19:29:35 +0000614 case PPC::ADDIStlsldHA: {
615 // Transform: %Xd = ADDIStlsldHA %X2, <ga:@sym>
616 // Into: %Xd = ADDIS8 %X2, sym@got@tlsld@ha
617 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
618 const MachineOperand &MO = MI->getOperand(2);
619 const GlobalValue *GValue = MO.getGlobal();
620 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
621 const MCExpr *SymGotTlsLD =
622 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA,
623 OutContext);
624 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
625 .addReg(MI->getOperand(0).getReg())
626 .addReg(PPC::X2)
627 .addExpr(SymGotTlsLD));
628 return;
629 }
630 case PPC::ADDItlsldL: {
631 // Transform: %Xd = ADDItlsldL %Xs, <ga:@sym>
Ulrich Weigand2b0850b2013-03-26 10:55:20 +0000632 // Into: %Xd = ADDI8 %Xs, sym@got@tlsld@l
Bill Schmidt349c2782012-12-12 19:29:35 +0000633 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
634 const MachineOperand &MO = MI->getOperand(2);
635 const GlobalValue *GValue = MO.getGlobal();
636 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
637 const MCExpr *SymGotTlsLD =
638 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO,
639 OutContext);
Ulrich Weigand2b0850b2013-03-26 10:55:20 +0000640 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
Bill Schmidt349c2782012-12-12 19:29:35 +0000641 .addReg(MI->getOperand(0).getReg())
642 .addReg(MI->getOperand(1).getReg())
643 .addExpr(SymGotTlsLD));
644 return;
645 }
646 case PPC::GETtlsldADDR: {
647 // Transform: %X3 = GETtlsldADDR %X3, <ga:@sym>
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000648 // Into: BL8_NOP_TLSLD __tls_get_addr(sym@tlsld)
Bill Schmidt349c2782012-12-12 19:29:35 +0000649 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
650
651 StringRef Name = "__tls_get_addr";
652 MCSymbol *TlsGetAddr = OutContext.GetOrCreateSymbol(Name);
653 const MCSymbolRefExpr *TlsRef =
654 MCSymbolRefExpr::Create(TlsGetAddr, MCSymbolRefExpr::VK_None, OutContext);
655 const MachineOperand &MO = MI->getOperand(2);
656 const GlobalValue *GValue = MO.getGlobal();
657 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
658 const MCExpr *SymVar =
659 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_TLSLD,
660 OutContext);
Ulrich Weigand86765fb2013-03-22 15:24:13 +0000661 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BL8_NOP_TLSLD)
Bill Schmidt349c2782012-12-12 19:29:35 +0000662 .addExpr(TlsRef)
663 .addExpr(SymVar));
664 return;
665 }
666 case PPC::ADDISdtprelHA: {
667 // Transform: %Xd = ADDISdtprelHA %X3, <ga:@sym>
668 // Into: %Xd = ADDIS8 %X3, sym@dtprel@ha
669 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
670 const MachineOperand &MO = MI->getOperand(2);
671 const GlobalValue *GValue = MO.getGlobal();
672 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
673 const MCExpr *SymDtprel =
674 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL16_HA,
675 OutContext);
676 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS8)
677 .addReg(MI->getOperand(0).getReg())
678 .addReg(PPC::X3)
679 .addExpr(SymDtprel));
680 return;
681 }
682 case PPC::ADDIdtprelL: {
683 // Transform: %Xd = ADDIdtprelL %Xs, <ga:@sym>
Ulrich Weigand2b0850b2013-03-26 10:55:20 +0000684 // Into: %Xd = ADDI8 %Xs, sym@dtprel@l
Bill Schmidt349c2782012-12-12 19:29:35 +0000685 assert(Subtarget.isPPC64() && "Not supported for 32-bit PowerPC");
686 const MachineOperand &MO = MI->getOperand(2);
687 const GlobalValue *GValue = MO.getGlobal();
688 MCSymbol *MOSymbol = Mang->getSymbol(GValue);
689 const MCExpr *SymDtprel =
690 MCSymbolRefExpr::Create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL16_LO,
691 OutContext);
Ulrich Weigand2b0850b2013-03-26 10:55:20 +0000692 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDI8)
Bill Schmidt349c2782012-12-12 19:29:35 +0000693 .addReg(MI->getOperand(0).getReg())
694 .addReg(MI->getOperand(1).getReg())
695 .addExpr(SymDtprel));
696 return;
697 }
Chris Lattnercb22fa62010-11-15 03:39:06 +0000698 case PPC::MFCRpseud:
Hal Finkel234bb382011-12-07 06:34:06 +0000699 case PPC::MFCR8pseud:
Chris Lattnercb22fa62010-11-15 03:39:06 +0000700 // Transform: %R3 = MFCRpseud %CR7
701 // Into: %R3 = MFCR ;; cr7
702 OutStreamer.AddComment(PPCInstPrinter::
703 getRegisterName(MI->getOperand(1).getReg()));
Benjamin Kramered9e4422012-11-26 18:05:52 +0000704 OutStreamer.EmitInstruction(MCInstBuilder(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR)
705 .addReg(MI->getOperand(0).getReg()));
Dale Johannesenefc3a632010-04-26 20:05:01 +0000706 return;
Hal Finkelc6d08f12011-10-17 04:03:49 +0000707 case PPC::SYNC:
708 // In Book E sync is called msync, handle this special case here...
709 if (Subtarget.isBookE()) {
710 OutStreamer.EmitRawText(StringRef("\tmsync"));
711 return;
712 }
Dale Johannesenefc3a632010-04-26 20:05:01 +0000713 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000714
Roman Divackya1000742011-06-09 20:25:38 +0000715 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Chris Lattnercb22fa62010-11-15 03:39:06 +0000716 OutStreamer.EmitInstruction(TmpInst);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000717}
718
Chris Lattner2cf72512010-01-27 07:21:55 +0000719void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
720 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
721 return AsmPrinter::EmitFunctionEntryLabel();
722
723 // Emit an official procedure descriptor.
Peter Collingbournedf39be62013-04-17 21:18:16 +0000724 MCSectionSubPair Current = OutStreamer.getCurrentSection();
Roman Divacky4328f9f2012-02-27 20:20:47 +0000725 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
726 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
727 SectionKind::getReadOnly());
728 OutStreamer.SwitchSection(Section);
Chris Lattner2cf72512010-01-27 07:21:55 +0000729 OutStreamer.EmitLabel(CurrentFnSym);
Roman Divacky4328f9f2012-02-27 20:20:47 +0000730 OutStreamer.EmitValueToAlignment(8);
731 MCSymbol *Symbol1 =
732 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000733 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
734 // entry point.
Roman Divacky4328f9f2012-02-27 20:20:47 +0000735 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
Eric Christopher68ca5622013-01-09 01:57:54 +0000736 8 /*size*/);
Adhemerval Zanellaaa714282012-10-25 12:27:42 +0000737 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC."));
738 // Generates a R_PPC64_TOC relocation for TOC base insertion.
739 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2,
740 MCSymbolRefExpr::VK_PPC_TOC, OutContext),
Eric Christopher68ca5622013-01-09 01:57:54 +0000741 8/*size*/);
Roman Divacky4cd56012012-09-18 16:55:29 +0000742 // Emit a null environment pointer.
Eric Christopher68ca5622013-01-09 01:57:54 +0000743 OutStreamer.EmitIntValue(0, 8 /* size */);
Peter Collingbournedf39be62013-04-17 21:18:16 +0000744 OutStreamer.SwitchSection(Current.first, Current.second);
Hal Finkeld55a2662012-02-22 21:11:47 +0000745
746 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
747 ".L." + Twine(CurrentFnSym->getName()));
748 OutStreamer.EmitLabel(RealFnSym);
749 CurrentFnSymForSize = RealFnSym;
Chris Lattner2cf72512010-01-27 07:21:55 +0000750}
751
752
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000753bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000754 const DataLayout *TD = TM.getDataLayout();
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000755
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000756 bool isPPC64 = TD->getPointerSizeInBits() == 64;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000757
758 if (isPPC64 && !TOC.empty()) {
Roman Divacky21a14012012-02-28 18:15:25 +0000759 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
760 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
761 SectionKind::getReadOnly());
762 OutStreamer.SwitchSection(Section);
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000763
Ulrich Weigand95d8afc2012-11-12 19:13:24 +0000764 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
Chris Lattner75abc682010-01-16 02:09:06 +0000765 E = TOC.end(); I != E; ++I) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000766 OutStreamer.EmitLabel(I->second);
Adhemerval Zanellaf35c62b2012-10-15 15:43:14 +0000767 MCSymbol *S = OutContext.GetOrCreateSymbol(I->first->getName());
768 OutStreamer.EmitTCEntry(*S);
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000769 }
770 }
771
Adhemerval Zanellaa1db5de2013-01-09 17:08:15 +0000772 MachineModuleInfoELF &MMIELF =
773 MMI->getObjFileInfo<MachineModuleInfoELF>();
774
775 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
776 if (!Stubs.empty()) {
777 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
778 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
779 // L_foo$stub:
780 OutStreamer.EmitLabel(Stubs[i].first);
781 // .long _foo
782 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Stubs[i].second.getPointer(),
783 OutContext),
784 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
785 }
786
787 Stubs.clear();
788 OutStreamer.AddBlankLine();
789 }
790
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000791 return AsmPrinter::doFinalization(M);
792}
Jim Laskeybf111822006-12-21 20:26:09 +0000793
Roman Divackyc6c2ced2012-08-28 19:06:55 +0000794/// EmitFunctionBodyEnd - Print the traceback table before the .size
795/// directive.
796///
797void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
798 // Only the 64-bit target requires a traceback table. For now,
799 // we only emit the word of zeroes that GDB requires to find
Hal Finkelbbd169b2012-08-29 20:22:24 +0000800 // the end of the function, and zeroes for the eight-byte
801 // mandatory fields.
802 // FIXME: We should fill in the eight-byte mandatory fields as described in
803 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
804 // currently make use of these fields).
805 if (Subtarget.isPPC64()) {
Roman Divackyc6c2ced2012-08-28 19:06:55 +0000806 OutStreamer.EmitIntValue(0, 4/*size*/);
Hal Finkelbbd169b2012-08-29 20:22:24 +0000807 OutStreamer.EmitIntValue(0, 8/*size*/);
808 }
Roman Divackyc6c2ced2012-08-28 19:06:55 +0000809}
810
Bob Wilson812209a2009-09-30 22:06:26 +0000811void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000812 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000813 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000814 "ppc",
Hal Finkelc6d08f12011-10-17 04:03:49 +0000815 "ppc440",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000816 "ppc601",
817 "ppc602",
818 "ppc603",
819 "ppc7400",
820 "ppc750",
821 "ppc970",
Hal Finkel4d989ac2012-04-01 19:22:40 +0000822 "ppcA2",
Hal Finkel621b77a2012-08-28 16:12:39 +0000823 "ppce500mc",
824 "ppce5500",
NAKAMURA Takumib516e9b2013-02-04 00:47:33 +0000825 "power3",
826 "power4",
827 "power5",
828 "power5x",
Hal Finkel622382f2012-06-11 15:43:08 +0000829 "power6",
NAKAMURA Takumib516e9b2013-02-04 00:47:33 +0000830 "power6x",
Hal Finkel622382f2012-06-11 15:43:08 +0000831 "power7",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000832 "ppc64"
833 };
834
835 unsigned Directive = Subtarget.getDarwinDirective();
Hal Finkelbd5cafd2012-06-11 19:57:01 +0000836 if (Subtarget.hasMFOCRF() && Directive < PPC::DIR_970)
Jim Laskeyc35010d2006-12-12 20:57:08 +0000837 Directive = PPC::DIR_970;
838 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
839 Directive = PPC::DIR_7400;
Eli Friedman32e698c2011-12-01 01:43:47 +0000840 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
Jim Laskeyc35010d2006-12-12 20:57:08 +0000841 Directive = PPC::DIR_64;
842 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Chris Lattnerb46443a2010-11-15 08:49:58 +0000843
844 // FIXME: This is a total hack, finish mc'izing the PPC backend.
NAKAMURA Takumi87b1a452013-02-04 00:47:38 +0000845 if (OutStreamer.hasRawTextSupport()) {
846 assert(Directive < sizeof(CPUDirectives) / sizeof(*CPUDirectives) &&
847 "CPUDirectives[] might not be up-to-date!");
Chris Lattnerb46443a2010-11-15 08:49:58 +0000848 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
NAKAMURA Takumi87b1a452013-02-04 00:47:38 +0000849 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000850
Jim Laskey2ada0852006-11-28 18:21:52 +0000851 // Prime text sections so they are adjacent. This reduces the likelihood a
852 // large data or debug section causes a branch to exceed 16M limit.
Dan Gohman0d805c32010-04-17 16:44:48 +0000853 const TargetLoweringObjectFileMachO &TLOFMacho =
854 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000855 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskey2ada0852006-11-28 18:21:52 +0000856 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000857 OutStreamer.SwitchSection(
Chris Lattner22772212010-04-08 20:40:11 +0000858 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000859 MCSectionMachO::S_SYMBOL_STUBS |
860 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
861 32, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000862 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000863 OutStreamer.SwitchSection(
Chris Lattner22772212010-04-08 20:40:11 +0000864 OutContext.getMachOSection("__TEXT","__symbol_stub1",
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000865 MCSectionMachO::S_SYMBOL_STUBS |
866 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
867 16, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000868 }
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000869 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman18ed0292005-07-21 01:25:49 +0000870}
871
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000872static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000873 // Remove $stub suffix, add $lazy_ptr.
Benjamin Kramer8f2dce02012-11-24 13:18:11 +0000874 StringRef NoStub = Sym->getName().substr(0, Sym->getName().size()-5);
875 return Ctx.GetOrCreateSymbol(NoStub + "$lazy_ptr");
Chris Lattner73a1aa02010-01-20 21:36:48 +0000876}
877
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000878static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000879 // Add $tmp suffix to $stub, yielding $stub$tmp.
Benjamin Kramer8f2dce02012-11-24 13:18:11 +0000880 return Ctx.GetOrCreateSymbol(Sym->getName() + "$tmp");
Chris Lattner73a1aa02010-01-20 21:36:48 +0000881}
882
883void PPCDarwinAsmPrinter::
884EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000885 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
Chris Lattner917d6282010-01-20 21:19:44 +0000886
Dan Gohman0d805c32010-04-17 16:44:48 +0000887 const TargetLoweringObjectFileMachO &TLOFMacho =
888 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner917d6282010-01-20 21:19:44 +0000889
890 // .lazy_symbol_pointer
891 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattnerff4bc462009-08-10 01:39:42 +0000892
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000893 // Output stubs for dynamically-linked functions
Chris Lattner917d6282010-01-20 21:19:44 +0000894 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2dfddee2009-08-03 22:52:21 +0000895 const MCSection *StubSection =
Chris Lattner22772212010-04-08 20:40:11 +0000896 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
897 MCSectionMachO::S_SYMBOL_STUBS |
898 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
899 32, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000900 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000901 OutStreamer.SwitchSection(StubSection);
Chris Lattner7e097e42006-06-27 01:02:25 +0000902 EmitAlignment(4);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000903
Chris Lattner9d7efd32010-04-04 07:05:53 +0000904 MCSymbol *Stub = Stubs[i].first;
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000905 MCSymbol *RawSym = Stubs[i].second.getPointer();
906 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
907 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000908
Chris Lattner9d7efd32010-04-04 07:05:53 +0000909 OutStreamer.EmitLabel(Stub);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000910 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000911
Hal Finkel7d35f742013-03-23 20:53:15 +0000912 const MCExpr *Anon = MCSymbolRefExpr::Create(AnonSymbol, OutContext);
913
Benjamin Kramer915558e2012-11-24 13:18:25 +0000914 // mflr r0
Benjamin Kramered9e4422012-11-26 18:05:52 +0000915 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R0));
Hal Finkel7d35f742013-03-23 20:53:15 +0000916 // bcl 20, 31, AnonSymbol
Hal Finkelcaeeb182013-04-04 22:55:54 +0000917 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCLalways).addExpr(Anon));
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000918 OutStreamer.EmitLabel(AnonSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000919 // mflr r11
Benjamin Kramered9e4422012-11-26 18:05:52 +0000920 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R11));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000921 // addis r11, r11, ha16(LazyPtr - AnonSymbol)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000922 const MCExpr *Sub =
923 MCBinaryExpr::CreateSub(MCSymbolRefExpr::Create(LazyPtr, OutContext),
Hal Finkel7d35f742013-03-23 20:53:15 +0000924 Anon, OutContext);
Benjamin Kramered9e4422012-11-26 18:05:52 +0000925 OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000926 .addReg(PPC::R11)
927 .addReg(PPC::R11)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000928 .addExpr(Sub));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000929 // mtlr r0
Benjamin Kramered9e4422012-11-26 18:05:52 +0000930 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTLR).addReg(PPC::R0));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000931
Benjamin Kramer391271f2012-11-26 13:34:22 +0000932 // ldu r12, lo16(LazyPtr - AnonSymbol)(r11)
933 // lwzu r12, lo16(LazyPtr - AnonSymbol)(r11)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000934 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000935 .addReg(PPC::R12)
936 .addExpr(Sub).addExpr(Sub)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000937 .addReg(PPC::R11));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000938 // mtctr r12
Benjamin Kramered9e4422012-11-26 18:05:52 +0000939 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000940 // bctr
Benjamin Kramered9e4422012-11-26 18:05:52 +0000941 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000942
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000943 OutStreamer.SwitchSection(LSPSection);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000944 OutStreamer.EmitLabel(LazyPtr);
945 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000946
947 MCSymbol *DyldStubBindingHelper =
948 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
949 if (isPPC64) {
950 // .quad dyld_stub_binding_helper
951 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
952 } else {
953 // .long dyld_stub_binding_helper
954 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
955 }
Chris Lattnerdeea4162005-12-13 04:33:58 +0000956 }
Chris Lattner9d7efd32010-04-04 07:05:53 +0000957 OutStreamer.AddBlankLine();
Chris Lattner917d6282010-01-20 21:19:44 +0000958 return;
Misha Brukman46fd00a2004-06-24 23:04:11 +0000959 }
Chris Lattner917d6282010-01-20 21:19:44 +0000960
961 const MCSection *StubSection =
Chris Lattner22772212010-04-08 20:40:11 +0000962 OutContext.getMachOSection("__TEXT","__symbol_stub1",
963 MCSectionMachO::S_SYMBOL_STUBS |
964 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
965 16, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000966 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000967 MCSymbol *Stub = Stubs[i].first;
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000968 MCSymbol *RawSym = Stubs[i].second.getPointer();
969 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000970
Chris Lattner917d6282010-01-20 21:19:44 +0000971 OutStreamer.SwitchSection(StubSection);
972 EmitAlignment(4);
Chris Lattner9d7efd32010-04-04 07:05:53 +0000973 OutStreamer.EmitLabel(Stub);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000974 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +0000975 // lis r11, ha16(LazyPtr)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000976 const MCExpr *LazyPtrHa16 =
977 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_HA16,
978 OutContext);
Benjamin Kramered9e4422012-11-26 18:05:52 +0000979 OutStreamer.EmitInstruction(MCInstBuilder(PPC::LIS)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000980 .addReg(PPC::R11)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000981 .addExpr(LazyPtrHa16));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000982
Benjamin Kramer391271f2012-11-26 13:34:22 +0000983 const MCExpr *LazyPtrLo16 =
984 MCSymbolRefExpr::Create(LazyPtr, MCSymbolRefExpr::VK_PPC_DARWIN_LO16,
985 OutContext);
986 // ldu r12, lo16(LazyPtr)(r11)
987 // lwzu r12, lo16(LazyPtr)(r11)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000988 OutStreamer.EmitInstruction(MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU)
Benjamin Kramer391271f2012-11-26 13:34:22 +0000989 .addReg(PPC::R12)
990 .addExpr(LazyPtrLo16).addExpr(LazyPtrLo16)
Benjamin Kramered9e4422012-11-26 18:05:52 +0000991 .addReg(PPC::R11));
Benjamin Kramer391271f2012-11-26 13:34:22 +0000992
Benjamin Kramer915558e2012-11-24 13:18:25 +0000993 // mtctr r12
Benjamin Kramered9e4422012-11-26 18:05:52 +0000994 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MTCTR).addReg(PPC::R12));
Benjamin Kramer915558e2012-11-24 13:18:25 +0000995 // bctr
Benjamin Kramered9e4422012-11-26 18:05:52 +0000996 OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCTR));
Benjamin Kramer391271f2012-11-26 13:34:22 +0000997
Chris Lattner917d6282010-01-20 21:19:44 +0000998 OutStreamer.SwitchSection(LSPSection);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000999 OutStreamer.EmitLabel(LazyPtr);
1000 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Benjamin Kramer915558e2012-11-24 13:18:25 +00001001
1002 MCSymbol *DyldStubBindingHelper =
1003 OutContext.GetOrCreateSymbol(StringRef("dyld_stub_binding_helper"));
1004 if (isPPC64) {
1005 // .quad dyld_stub_binding_helper
1006 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 8);
1007 } else {
1008 // .long dyld_stub_binding_helper
1009 OutStreamer.EmitSymbolValue(DyldStubBindingHelper, 4);
1010 }
Chris Lattner917d6282010-01-20 21:19:44 +00001011 }
1012
Chris Lattner2dc6fa62010-04-04 07:12:28 +00001013 OutStreamer.AddBlankLine();
Chris Lattner917d6282010-01-20 21:19:44 +00001014}
1015
1016
1017bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001018 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64;
Chris Lattner917d6282010-01-20 21:19:44 +00001019
1020 // Darwin/PPC always uses mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +00001021 const TargetLoweringObjectFileMachO &TLOFMacho =
1022 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner917d6282010-01-20 21:19:44 +00001023 MachineModuleInfoMachO &MMIMacho =
1024 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1025
Chris Lattner73a1aa02010-01-20 21:36:48 +00001026 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
1027 if (!Stubs.empty())
1028 EmitFunctionStubs(Stubs);
Misha Brukmanda2b13f2004-07-16 20:29:04 +00001029
Chris Lattner33adcfb2009-08-22 21:43:10 +00001030 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001031 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +00001032 // Only referenced functions get into the Personalities list.
Dan Gohman46510a72010-04-15 01:51:59 +00001033 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
1034 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +00001035 E = Personalities.end(); I != E; ++I) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +00001036 if (*I) {
Chris Lattnerd269a6e2010-02-03 06:18:30 +00001037 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +00001038 MachineModuleInfoImpl::StubValueTy &StubSym =
1039 MMIMacho.getGVStubEntry(NLPSym);
Chris Lattnerd62f1b42010-03-12 21:19:23 +00001040 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +00001041 }
Chris Lattner8f831cb2009-07-15 01:14:44 +00001042 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +00001043 }
1044
Chris Lattnerd3ec0b52010-01-20 21:16:14 +00001045 // Output stubs for dynamically-linked functions.
Chris Lattner73a1aa02010-01-20 21:36:48 +00001046 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerd3ec0b52010-01-20 21:16:14 +00001047
Chris Lattner2dfddee2009-08-03 22:52:21 +00001048 // Output macho stubs for external and common global variables.
Chris Lattnerd3ec0b52010-01-20 21:16:14 +00001049 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +00001050 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001051 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnere1e8a822009-08-10 17:58:51 +00001052 EmitAlignment(isPPC64 ? 3 : 2);
1053
Chris Lattnerd3ec0b52010-01-20 21:16:14 +00001054 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +00001055 // L_foo$stub:
1056 OutStreamer.EmitLabel(Stubs[i].first);
1057 // .indirect_symbol _foo
1058 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1059 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendling53351a12010-03-12 02:00:43 +00001060
1061 if (MCSym.getInt())
1062 // External to current translation unit.
Eric Christopher68ca5622013-01-09 01:57:54 +00001063 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/);
Bill Wendling53351a12010-03-12 02:00:43 +00001064 else
1065 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +00001066 //
1067 // When we place the LSDA into the TEXT section, the type info pointers
1068 // need to be indirect and pc-rel. We accomplish this by using NLPs.
1069 // However, sometimes the types are local to the file. So we need to
1070 // fill in the value for the NLP in those cases.
Bill Wendling53351a12010-03-12 02:00:43 +00001071 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
1072 OutContext),
Eric Christopher68ca5622013-01-09 01:57:54 +00001073 isPPC64 ? 8 : 4/*size*/);
Chris Lattnerdeea4162005-12-13 04:33:58 +00001074 }
Bill Wendling08d726c2010-03-11 23:39:44 +00001075
1076 Stubs.clear();
1077 OutStreamer.AddBlankLine();
Nate Begemane59bf592004-08-14 22:09:10 +00001078 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +00001079
Chris Lattnerd3ec0b52010-01-20 21:16:14 +00001080 Stubs = MMIMacho.GetHiddenGVStubList();
1081 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001082 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +00001083 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +00001084
1085 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +00001086 // L_foo$stub:
1087 OutStreamer.EmitLabel(Stubs[i].first);
1088 // .long _foo
1089 OutStreamer.EmitValue(MCSymbolRefExpr::
1090 Create(Stubs[i].second.getPointer(),
1091 OutContext),
Eric Christopher68ca5622013-01-09 01:57:54 +00001092 isPPC64 ? 8 : 4/*size*/);
Evan Chengae94e592008-12-05 01:06:39 +00001093 }
Bill Wendling08d726c2010-03-11 23:39:44 +00001094
1095 Stubs.clear();
1096 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +00001097 }
1098
Chris Lattner5b0ac992005-11-01 00:12:36 +00001099 // Funny Darwin hack: This flag tells the linker that no global symbols
1100 // contain code that falls through to other global symbols (e.g. the obvious
1101 // implementation of multiple entry points). If this doesn't occur, the
1102 // linker can safely perform dead code stripping. Since LLVM never generates
1103 // code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001104 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Chris Lattner5b0ac992005-11-01 00:12:36 +00001105
Dan Gohmanb8275a32007-07-25 19:33:14 +00001106 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +00001107}
Nate Begemaned428532004-09-04 05:00:00 +00001108
Jim Laskeyb608a4d2006-12-20 20:56:46 +00001109/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1110/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +00001111/// Darwin assembler can deal with.
1112///
Chris Lattnerb23569a2010-04-04 08:18:47 +00001113static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
Chris Lattner11d53c12010-03-13 20:55:24 +00001114 MCStreamer &Streamer) {
Jim Laskeybf111822006-12-21 20:26:09 +00001115 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
1116
Chris Lattner40bbebd2009-07-21 18:38:57 +00001117 if (Subtarget->isDarwin())
Chris Lattnerb23569a2010-04-04 08:18:47 +00001118 return new PPCDarwinAsmPrinter(tm, Streamer);
1119 return new PPCLinuxAsmPrinter(tm, Streamer);
Chris Lattner4da1c822006-09-20 17:12:19 +00001120}
Anton Korobeynikov06be9972008-08-17 13:54:28 +00001121
Bob Wilsona96751f2009-06-23 23:59:40 +00001122// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001123extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001124 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbar51b198a2009-07-15 20:24:03 +00001125 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
1126}