blob: 591ae020aa3ce5b4910aaaa0f7f97ec40a81b0a3 [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"
Evan Cheng94b95502011-07-26 00:24:13 +000023#include "MCTargetDesc/PPCPredicates.h"
Dale Johannesenefc3a632010-04-26 20:05:01 +000024#include "llvm/Analysis/DebugInfo.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Module.h"
28#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"
Chris Lattner325d3dc2009-09-13 17:14:04 +000035#include "llvm/MC/MCAsmInfo.h"
Chris Lattner0b4bad52010-01-13 19:00:57 +000036#include "llvm/MC/MCContext.h"
Bill Wendling08d726c2010-03-11 23:39:44 +000037#include "llvm/MC/MCExpr.h"
Chris Lattner293ef9a2010-11-14 19:53:02 +000038#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000039#include "llvm/MC/MCSectionMachO.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000040#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000041#include "llvm/MC/MCSymbol.h"
Roman Divacky4328f9f2012-02-27 20:20:47 +000042#include "llvm/MC/MCSectionELF.h"
Chris Lattner45111d12010-01-16 21:57:06 +000043#include "llvm/Target/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000044#include "llvm/Target/TargetRegisterInfo.h"
45#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetOptions.h"
Chris Lattner293ef9a2010-11-14 19:53:02 +000047#include "llvm/Support/CommandLine.h"
Devang Patel40e0bad2010-08-04 22:07:50 +000048#include "llvm/Support/Debug.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000049#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000050#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000051#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000052#include "llvm/Support/raw_ostream.h"
Roman Divacky4328f9f2012-02-27 20:20:47 +000053#include "llvm/Support/ELF.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000054#include "llvm/ADT/StringExtras.h"
Chris Lattner48130352010-01-13 06:38:18 +000055#include "llvm/ADT/SmallString.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000056#include "InstPrinter/PPCInstPrinter.h"
Chris Lattnera3840792004-08-16 23:25:21 +000057using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000058
Chris Lattner95b2c7d2006-12-19 22:59:26 +000059namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000060 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000061 protected:
Chris Lattner9d7efd32010-04-04 07:05:53 +000062 DenseMap<MCSymbol*, MCSymbol*> TOC;
Chris Lattnerdadceed2006-09-20 17:07:15 +000063 const PPCSubtarget &Subtarget;
Chris Lattner374c6082010-11-15 03:42:54 +000064 uint64_t TOCLabelID;
Bill Wendling57f0db82009-02-24 08:30:20 +000065 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +000066 explicit PPCAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
67 : AsmPrinter(TM, Streamer),
Chris Lattner374c6082010-11-15 03:42:54 +000068 Subtarget(TM.getSubtarget<PPCSubtarget>()), TOCLabelID(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000069
Misha Brukman5dfe3a92004-06-21 16:55:25 +000070 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000071 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000072 }
73
Nate Begemane59bf592004-08-14 22:09:10 +000074
Chris Lattnerd49fe1b2010-01-28 01:28:58 +000075 virtual void EmitInstruction(const MachineInstr *MI);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000076
Chris Lattnercb22fa62010-11-15 03:39:06 +000077 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000078
Chris Lattner58873272006-02-01 22:38:46 +000079 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000080 unsigned AsmVariant, const char *ExtraCode,
81 raw_ostream &O);
Chris Lattner2c003e22006-02-24 20:27:40 +000082 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +000083 unsigned AsmVariant, const char *ExtraCode,
84 raw_ostream &O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +000085
Devang Patel40e0bad2010-08-04 22:07:50 +000086 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
Devang Patel40e0bad2010-08-04 22:07:50 +000087 MachineLocation Location;
Chris Lattnercb22fa62010-11-15 03:39:06 +000088 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
Devang Patel40e0bad2010-08-04 22:07:50 +000089 // Frame address. Currently handles register +- offset only.
90 if (MI->getOperand(0).isReg() && MI->getOperand(2).isImm())
91 Location.set(MI->getOperand(0).getReg(), MI->getOperand(2).getImm());
92 else {
93 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
94 }
95 return Location;
96 }
Nate Begemaned428532004-09-04 05:00:00 +000097 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +000098
Anton Korobeynikov34da1272008-08-08 18:22:59 +000099 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000100 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000101 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +0000102 explicit PPCLinuxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
103 : PPCAsmPrinter(TM, Streamer) {}
Jim Laskeybf111822006-12-21 20:26:09 +0000104
105 virtual const char *getPassName() const {
106 return "Linux PPC Assembly Printer";
107 }
108
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000109 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000110
Chris Lattner2cf72512010-01-27 07:21:55 +0000111 virtual void EmitFunctionEntryLabel();
Jim Laskeybf111822006-12-21 20:26:09 +0000112 };
113
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000114 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
115 /// OS X
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000116 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000117 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +0000118 explicit PPCDarwinAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
119 : PPCAsmPrinter(TM, Streamer) {}
Nate Begemaned428532004-09-04 05:00:00 +0000120
121 virtual const char *getPassName() const {
122 return "Darwin PPC Assembly Printer";
123 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000124
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000125 bool doFinalization(Module &M);
Bob Wilson812209a2009-09-30 22:06:26 +0000126 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000127
Chris Lattner73a1aa02010-01-20 21:36:48 +0000128 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000129 };
130} // end of anonymous namespace
131
Chris Lattnercb22fa62010-11-15 03:39:06 +0000132/// stripRegisterPrefix - This method strips the character prefix from a
133/// register name so that only the number is left. Used by for linux asm.
134static const char *stripRegisterPrefix(const char *RegName) {
135 switch (RegName[0]) {
136 case 'r':
137 case 'f':
138 case 'v': return RegName + 1;
139 case 'c': if (RegName[1] == 'r') return RegName + 2;
140 }
141
142 return RegName;
143}
Nate Begemane59bf592004-08-14 22:09:10 +0000144
Chris Lattnercb22fa62010-11-15 03:39:06 +0000145void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
146 raw_ostream &O) {
147 const MachineOperand &MO = MI->getOperand(OpNo);
148
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000149 switch (MO.getType()) {
Chris Lattnercb22fa62010-11-15 03:39:06 +0000150 case MachineOperand::MO_Register: {
151 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
152 // Linux assembler (Others?) does not take register mnemonics.
153 // FIXME - What about special registers used in mfspr/mtspr?
154 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
155 O << RegName;
156 return;
157 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000158 case MachineOperand::MO_Immediate:
Chris Lattnercb22fa62010-11-15 03:39:06 +0000159 O << MO.getImm();
160 return;
Misha Brukman97a296f2004-07-21 20:11:11 +0000161
Nate Begeman37efe672006-04-22 18:53:45 +0000162 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000163 O << *MO.getMBB()->getSymbol();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000164 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000165 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000166 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000167 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000168 // FIXME: PIC relocation model
169 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000170 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000171 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000172 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000173 return;
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000174 case MachineOperand::MO_BlockAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +0000175 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000176 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000177 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000178 // Computing the address of an external symbol, not calling it.
Chris Lattner75abc682010-01-16 02:09:06 +0000179 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000180 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner75abc682010-01-16 02:09:06 +0000181 return;
Chris Lattnera637c322005-12-16 00:22:14 +0000182 }
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000183
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000184 MCSymbol *NLPSym =
Chris Lattner75abc682010-01-16 02:09:06 +0000185 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
186 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000187 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000188 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000189 if (StubSym.getPointer() == 0)
190 StubSym = MachineModuleInfoImpl::
191 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000192
Chris Lattner10b318b2010-01-17 21:43:43 +0000193 O << *NLPSym;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000194 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000195 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000196 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000197 // Computing the address of a global symbol, not calling it.
Dan Gohman46510a72010-04-15 01:51:59 +0000198 const GlobalValue *GV = MO.getGlobal();
Chris Lattnera40128c2010-01-16 02:00:23 +0000199 MCSymbol *SymToPrint;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000200
Nate Begemanfcf4a422004-10-17 23:01:34 +0000201 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000202 if (TM.getRelocationModel() != Reloc::Static &&
203 (GV->isDeclaration() || GV->isWeakForLinker())) {
204 if (!GV->hasHiddenVisibility()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000205 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000206 MachineModuleInfoImpl::StubValueTy &StubSym =
207 MMI->getObjFileInfo<MachineModuleInfoMachO>()
208 .getGVStubEntry(SymToPrint);
209 if (StubSym.getPointer() == 0)
210 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000211 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000212 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
213 GV->hasAvailableExternallyLinkage()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000214 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000215
Bill Wendlingcebae362010-03-10 22:34:10 +0000216 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000217 MMI->getObjFileInfo<MachineModuleInfoMachO>().
218 getHiddenGVStubEntry(SymToPrint);
Bill Wendlingcebae362010-03-10 22:34:10 +0000219 if (StubSym.getPointer() == 0)
220 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000221 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000222 } else {
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000223 SymToPrint = Mang->getSymbol(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000224 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000225 } else {
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000226 SymToPrint = Mang->getSymbol(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000227 }
Chris Lattnera40128c2010-01-16 02:00:23 +0000228
Chris Lattner10b318b2010-01-17 21:43:43 +0000229 O << *SymToPrint;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000230
Chris Lattner0c08d092010-04-03 22:28:33 +0000231 printOffset(MO.getOffset(), O);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000232 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000233 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000234
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000235 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000236 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000237 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000238 }
239}
240
Chris Lattnere3f01572006-02-23 19:31:10 +0000241/// PrintAsmOperand - Print out an operand for an inline asm expression.
242///
243bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000244 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000245 const char *ExtraCode, raw_ostream &O) {
Chris Lattnere3f01572006-02-23 19:31:10 +0000246 // Does this asm operand have a single letter operand modifier?
247 if (ExtraCode && ExtraCode[0]) {
248 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000249
Chris Lattnere3f01572006-02-23 19:31:10 +0000250 switch (ExtraCode[0]) {
251 default: return true; // Unknown modifier.
Chris Lattner78192b62007-01-25 02:52:50 +0000252 case 'c': // Don't print "$" before a global var name or constant.
Chris Lattnercb22fa62010-11-15 03:39:06 +0000253 break; // PPC never has a prefix.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000254 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000255 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000256 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000257 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000258 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000259 return true;
260 ++OpNo; // Return the high-part.
261 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000262 case 'I':
263 // Write 'i' if an integer constant, otherwise nothing. Used to print
264 // addi vs add, etc.
Dan Gohmand735b802008-10-03 15:45:36 +0000265 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000266 O << "i";
267 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000268 }
269 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000270
Chris Lattner35c33bd2010-04-04 04:47:45 +0000271 printOperand(MI, OpNo, O);
Chris Lattnere3f01572006-02-23 19:31:10 +0000272 return false;
273}
274
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000275// At the moment, all inline asm memory operands are a single register.
276// In any case, the output of this routine should always be just one
277// assembler operand.
278
Chris Lattner2c003e22006-02-24 20:27:40 +0000279bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000280 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000281 const char *ExtraCode,
282 raw_ostream &O) {
Chris Lattner2c003e22006-02-24 20:27:40 +0000283 if (ExtraCode && ExtraCode[0])
284 return true; // Unknown modifier.
Chris Lattnercb22fa62010-11-15 03:39:06 +0000285 assert(MI->getOperand(OpNo).isReg());
Dale Johannesen4e68f882009-08-26 18:10:32 +0000286 O << "0(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000287 printOperand(MI, OpNo, O);
Dale Johannesen4e68f882009-08-26 18:10:32 +0000288 O << ")";
Chris Lattner2c003e22006-02-24 20:27:40 +0000289 return false;
290}
Chris Lattnere3f01572006-02-23 19:31:10 +0000291
Chris Lattneraf53a872006-11-04 05:27:39 +0000292
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000293/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Nate Begemane59bf592004-08-14 22:09:10 +0000294/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000295///
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000296void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattnercb22fa62010-11-15 03:39:06 +0000297 MCInst TmpInst;
298
299 // Lower multi-instruction pseudo operations.
300 switch (MI->getOpcode()) {
301 default: break;
Chris Lattnerf3b6e062010-11-15 07:52:06 +0000302 case TargetOpcode::DBG_VALUE: {
303 if (!isVerbose() || !OutStreamer.hasRawTextSupport()) return;
304
305 SmallString<32> Str;
306 raw_svector_ostream O(Str);
307 unsigned NOps = MI->getNumOperands();
308 assert(NOps==4);
309 O << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
310 // cast away const; DIetc do not take const operands for some reason.
311 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
312 O << V.getName();
313 O << " <- ";
314 // Frame address. Currently handles register +- offset only.
315 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
316 O << '['; printOperand(MI, 0, O); O << '+'; printOperand(MI, 1, O);
317 O << ']';
318 O << "+";
319 printOperand(MI, NOps-2, O);
320 OutStreamer.EmitRawText(O.str());
321 return;
322 }
323
Chris Lattnercb22fa62010-11-15 03:39:06 +0000324 case PPC::MovePCtoLR:
325 case PPC::MovePCtoLR8: {
326 // Transform %LR = MovePCtoLR
327 // Into this, where the label is the PIC base:
328 // bl L1$pb
329 // L1$pb:
330 MCSymbol *PICBase = MF->getPICBaseSymbol();
Chris Lattner2ead4582010-11-14 22:03:15 +0000331
Chris Lattnercb22fa62010-11-15 03:39:06 +0000332 // Emit the 'bl'.
333 TmpInst.setOpcode(PPC::BL_Darwin); // Darwin vs SVR4 doesn't matter here.
334
335
336 // FIXME: We would like an efficient form for this, so we don't have to do
337 // a lot of extra uniquing.
338 TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::
339 Create(PICBase, OutContext)));
340 OutStreamer.EmitInstruction(TmpInst);
341
342 // Emit the label.
343 OutStreamer.EmitLabel(PICBase);
344 return;
345 }
346 case PPC::LDtoc: {
347 // Transform %X3 = LDtoc <ga:@min1>, %X2
Roman Divackya1000742011-06-09 20:25:38 +0000348 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Chris Lattnercb22fa62010-11-15 03:39:06 +0000349
350 // Change the opcode to LD, and the global address operand to be a
351 // reference to the TOC entry we will synthesize later.
352 TmpInst.setOpcode(PPC::LD);
353 const MachineOperand &MO = MI->getOperand(1);
354 assert(MO.isGlobal());
355
356 // Map symbol -> label of TOC entry.
357 MCSymbol *&TOCEntry = TOC[Mang->getSymbol(MO.getGlobal())];
Chris Lattner374c6082010-11-15 03:42:54 +0000358 if (TOCEntry == 0)
359 TOCEntry = GetTempSymbol("C", TOCLabelID++);
Chris Lattnercb22fa62010-11-15 03:39:06 +0000360
361 const MCExpr *Exp =
362 MCSymbolRefExpr::Create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC,
363 OutContext);
364 TmpInst.getOperand(1) = MCOperand::CreateExpr(Exp);
Chris Lattner293ef9a2010-11-14 19:53:02 +0000365 OutStreamer.EmitInstruction(TmpInst);
366 return;
367 }
Chris Lattnercb22fa62010-11-15 03:39:06 +0000368
369 case PPC::MFCRpseud:
Hal Finkel234bb382011-12-07 06:34:06 +0000370 case PPC::MFCR8pseud:
Chris Lattnercb22fa62010-11-15 03:39:06 +0000371 // Transform: %R3 = MFCRpseud %CR7
372 // Into: %R3 = MFCR ;; cr7
373 OutStreamer.AddComment(PPCInstPrinter::
374 getRegisterName(MI->getOperand(1).getReg()));
Hal Finkel234bb382011-12-07 06:34:06 +0000375 TmpInst.setOpcode(Subtarget.isPPC64() ? PPC::MFCR8 : PPC::MFCR);
Chris Lattnercb22fa62010-11-15 03:39:06 +0000376 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
377 OutStreamer.EmitInstruction(TmpInst);
Dale Johannesenefc3a632010-04-26 20:05:01 +0000378 return;
Hal Finkelc6d08f12011-10-17 04:03:49 +0000379 case PPC::SYNC:
380 // In Book E sync is called msync, handle this special case here...
381 if (Subtarget.isBookE()) {
382 OutStreamer.EmitRawText(StringRef("\tmsync"));
383 return;
384 }
Dale Johannesenefc3a632010-04-26 20:05:01 +0000385 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000386
Roman Divackya1000742011-06-09 20:25:38 +0000387 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, Subtarget.isDarwin());
Chris Lattnercb22fa62010-11-15 03:39:06 +0000388 OutStreamer.EmitInstruction(TmpInst);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000389}
390
Chris Lattner2cf72512010-01-27 07:21:55 +0000391void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
392 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
393 return AsmPrinter::EmitFunctionEntryLabel();
394
395 // Emit an official procedure descriptor.
Roman Divacky4328f9f2012-02-27 20:20:47 +0000396 const MCSection *Current = OutStreamer.getCurrentSection();
397 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".opd",
398 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
399 SectionKind::getReadOnly());
400 OutStreamer.SwitchSection(Section);
Chris Lattner2cf72512010-01-27 07:21:55 +0000401 OutStreamer.EmitLabel(CurrentFnSym);
Roman Divacky4328f9f2012-02-27 20:20:47 +0000402 OutStreamer.EmitValueToAlignment(8);
403 MCSymbol *Symbol1 =
404 OutContext.GetOrCreateSymbol(".L." + Twine(CurrentFnSym->getName()));
405 MCSymbol *Symbol2 = OutContext.GetOrCreateSymbol(StringRef(".TOC.@tocbase"));
406 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol1, OutContext),
407 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/);
408 OutStreamer.EmitValue(MCSymbolRefExpr::Create(Symbol2, OutContext),
409 Subtarget.isPPC64() ? 8 : 4/*size*/, 0/*addrspace*/);
410 OutStreamer.SwitchSection(Current);
Hal Finkeld55a2662012-02-22 21:11:47 +0000411
412 MCSymbol *RealFnSym = OutContext.GetOrCreateSymbol(
413 ".L." + Twine(CurrentFnSym->getName()));
414 OutStreamer.EmitLabel(RealFnSym);
415 CurrentFnSymForSize = RealFnSym;
Chris Lattner2cf72512010-01-27 07:21:55 +0000416}
417
418
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000419bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
420 const TargetData *TD = TM.getTargetData();
421
422 bool isPPC64 = TD->getPointerSizeInBits() == 64;
423
424 if (isPPC64 && !TOC.empty()) {
Roman Divacky21a14012012-02-28 18:15:25 +0000425 const MCSectionELF *Section = OutStreamer.getContext().getELFSection(".toc",
426 ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC,
427 SectionKind::getReadOnly());
428 OutStreamer.SwitchSection(Section);
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000429
Chris Lattnera40128c2010-01-16 02:00:23 +0000430 // FIXME: This is nondeterminstic!
Chris Lattner9d7efd32010-04-04 07:05:53 +0000431 for (DenseMap<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
Chris Lattner75abc682010-01-16 02:09:06 +0000432 E = TOC.end(); I != E; ++I) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000433 OutStreamer.EmitLabel(I->second);
434 OutStreamer.EmitRawText("\t.tc " + Twine(I->first->getName()) +
435 "[TC]," + I->first->getName());
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000436 }
437 }
438
439 return AsmPrinter::doFinalization(M);
440}
Jim Laskeybf111822006-12-21 20:26:09 +0000441
Bob Wilson812209a2009-09-30 22:06:26 +0000442void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000443 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000444 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000445 "ppc",
Hal Finkelc6d08f12011-10-17 04:03:49 +0000446 "ppc440",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000447 "ppc601",
448 "ppc602",
449 "ppc603",
450 "ppc7400",
451 "ppc750",
452 "ppc970",
453 "ppc64"
454 };
455
456 unsigned Directive = Subtarget.getDarwinDirective();
457 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
458 Directive = PPC::DIR_970;
459 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
460 Directive = PPC::DIR_7400;
Eli Friedman32e698c2011-12-01 01:43:47 +0000461 if (Subtarget.isPPC64() && Directive < PPC::DIR_64)
Jim Laskeyc35010d2006-12-12 20:57:08 +0000462 Directive = PPC::DIR_64;
463 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Chris Lattnerb46443a2010-11-15 08:49:58 +0000464
465 // FIXME: This is a total hack, finish mc'izing the PPC backend.
466 if (OutStreamer.hasRawTextSupport())
467 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000468
Jim Laskey2ada0852006-11-28 18:21:52 +0000469 // Prime text sections so they are adjacent. This reduces the likelihood a
470 // large data or debug section causes a branch to exceed 16M limit.
Dan Gohman0d805c32010-04-17 16:44:48 +0000471 const TargetLoweringObjectFileMachO &TLOFMacho =
472 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000473 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskey2ada0852006-11-28 18:21:52 +0000474 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000475 OutStreamer.SwitchSection(
Chris Lattner22772212010-04-08 20:40:11 +0000476 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000477 MCSectionMachO::S_SYMBOL_STUBS |
478 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
479 32, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000480 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000481 OutStreamer.SwitchSection(
Chris Lattner22772212010-04-08 20:40:11 +0000482 OutContext.getMachOSection("__TEXT","__symbol_stub1",
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000483 MCSectionMachO::S_SYMBOL_STUBS |
484 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
485 16, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000486 }
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000487 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman18ed0292005-07-21 01:25:49 +0000488}
489
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000490static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000491 // Remove $stub suffix, add $lazy_ptr.
492 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
493 TmpStr += "$lazy_ptr";
Chris Lattner9b97a732010-03-30 18:10:53 +0000494 return Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000495}
496
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000497static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000498 // Add $tmp suffix to $stub, yielding $stub$tmp.
499 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
500 TmpStr += "$tmp";
Chris Lattner9b97a732010-03-30 18:10:53 +0000501 return Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000502}
503
504void PPCDarwinAsmPrinter::
505EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chris Lattner917d6282010-01-20 21:19:44 +0000506 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
507
Dan Gohman0d805c32010-04-17 16:44:48 +0000508 const TargetLoweringObjectFileMachO &TLOFMacho =
509 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner917d6282010-01-20 21:19:44 +0000510
511 // .lazy_symbol_pointer
512 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattnerff4bc462009-08-10 01:39:42 +0000513
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000514 // Output stubs for dynamically-linked functions
Chris Lattner917d6282010-01-20 21:19:44 +0000515 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2dfddee2009-08-03 22:52:21 +0000516 const MCSection *StubSection =
Chris Lattner22772212010-04-08 20:40:11 +0000517 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
518 MCSectionMachO::S_SYMBOL_STUBS |
519 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
520 32, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000521 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000522 OutStreamer.SwitchSection(StubSection);
Chris Lattner7e097e42006-06-27 01:02:25 +0000523 EmitAlignment(4);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000524
Chris Lattner9d7efd32010-04-04 07:05:53 +0000525 MCSymbol *Stub = Stubs[i].first;
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000526 MCSymbol *RawSym = Stubs[i].second.getPointer();
527 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
528 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000529
Chris Lattner9d7efd32010-04-04 07:05:53 +0000530 OutStreamer.EmitLabel(Stub);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000531 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
532 // FIXME: MCize this.
Chris Lattner1841b342010-04-04 07:17:25 +0000533 OutStreamer.EmitRawText(StringRef("\tmflr r0"));
534 OutStreamer.EmitRawText("\tbcl 20,31," + Twine(AnonSymbol->getName()));
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000535 OutStreamer.EmitLabel(AnonSymbol);
Chris Lattner1841b342010-04-04 07:17:25 +0000536 OutStreamer.EmitRawText(StringRef("\tmflr r11"));
537 OutStreamer.EmitRawText("\taddis r11,r11,ha16("+Twine(LazyPtr->getName())+
538 "-" + AnonSymbol->getName() + ")");
539 OutStreamer.EmitRawText(StringRef("\tmtlr r0"));
540
541 if (isPPC64)
542 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
543 "-" + AnonSymbol->getName() + ")(r11)");
544 else
545 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
546 "-" + AnonSymbol->getName() + ")(r11)");
547 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
548 OutStreamer.EmitRawText(StringRef("\tbctr"));
Chris Lattnerfe2fe702009-07-16 01:23:26 +0000549
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000550 OutStreamer.SwitchSection(LSPSection);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000551 OutStreamer.EmitLabel(LazyPtr);
552 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Chris Lattner1841b342010-04-04 07:17:25 +0000553
554 if (isPPC64)
555 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
556 else
557 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
Chris Lattnerdeea4162005-12-13 04:33:58 +0000558 }
Chris Lattner9d7efd32010-04-04 07:05:53 +0000559 OutStreamer.AddBlankLine();
Chris Lattner917d6282010-01-20 21:19:44 +0000560 return;
Misha Brukman46fd00a2004-06-24 23:04:11 +0000561 }
Chris Lattner917d6282010-01-20 21:19:44 +0000562
563 const MCSection *StubSection =
Chris Lattner22772212010-04-08 20:40:11 +0000564 OutContext.getMachOSection("__TEXT","__symbol_stub1",
565 MCSectionMachO::S_SYMBOL_STUBS |
566 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
567 16, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000568 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000569 MCSymbol *Stub = Stubs[i].first;
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000570 MCSymbol *RawSym = Stubs[i].second.getPointer();
571 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000572
Chris Lattner917d6282010-01-20 21:19:44 +0000573 OutStreamer.SwitchSection(StubSection);
574 EmitAlignment(4);
Chris Lattner9d7efd32010-04-04 07:05:53 +0000575 OutStreamer.EmitLabel(Stub);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000576 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Chris Lattner1841b342010-04-04 07:17:25 +0000577 OutStreamer.EmitRawText("\tlis r11,ha16(" + Twine(LazyPtr->getName()) +")");
578 if (isPPC64)
579 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
580 ")(r11)");
581 else
582 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
583 ")(r11)");
584 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
585 OutStreamer.EmitRawText(StringRef("\tbctr"));
Chris Lattner917d6282010-01-20 21:19:44 +0000586 OutStreamer.SwitchSection(LSPSection);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000587 OutStreamer.EmitLabel(LazyPtr);
588 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Chris Lattner1841b342010-04-04 07:17:25 +0000589
590 if (isPPC64)
591 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
592 else
593 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
Chris Lattner917d6282010-01-20 21:19:44 +0000594 }
595
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000596 OutStreamer.AddBlankLine();
Chris Lattner917d6282010-01-20 21:19:44 +0000597}
598
599
600bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
601 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
602
603 // Darwin/PPC always uses mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000604 const TargetLoweringObjectFileMachO &TLOFMacho =
605 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner917d6282010-01-20 21:19:44 +0000606 MachineModuleInfoMachO &MMIMacho =
607 MMI->getObjFileInfo<MachineModuleInfoMachO>();
608
Chris Lattner73a1aa02010-01-20 21:36:48 +0000609 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
610 if (!Stubs.empty())
611 EmitFunctionStubs(Stubs);
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000612
Chris Lattner33adcfb2009-08-22 21:43:10 +0000613 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000614 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000615 // Only referenced functions get into the Personalities list.
Dan Gohman46510a72010-04-15 01:51:59 +0000616 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
617 for (std::vector<const Function*>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +0000618 E = Personalities.end(); I != E; ++I) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000619 if (*I) {
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000620 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000621 MachineModuleInfoImpl::StubValueTy &StubSym =
622 MMIMacho.getGVStubEntry(NLPSym);
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000623 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000624 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000625 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000626 }
627
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000628 // Output stubs for dynamically-linked functions.
Chris Lattner73a1aa02010-01-20 21:36:48 +0000629 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000630
Chris Lattner2dfddee2009-08-03 22:52:21 +0000631 // Output macho stubs for external and common global variables.
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000632 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000633 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000634 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnere1e8a822009-08-10 17:58:51 +0000635 EmitAlignment(isPPC64 ? 3 : 2);
636
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000637 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +0000638 // L_foo$stub:
639 OutStreamer.EmitLabel(Stubs[i].first);
640 // .indirect_symbol _foo
641 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
642 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendling53351a12010-03-12 02:00:43 +0000643
644 if (MCSym.getInt())
645 // External to current translation unit.
646 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
647 else
648 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000649 //
650 // When we place the LSDA into the TEXT section, the type info pointers
651 // need to be indirect and pc-rel. We accomplish this by using NLPs.
652 // However, sometimes the types are local to the file. So we need to
653 // fill in the value for the NLP in those cases.
Bill Wendling53351a12010-03-12 02:00:43 +0000654 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
655 OutContext),
656 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000657 }
Bill Wendling08d726c2010-03-11 23:39:44 +0000658
659 Stubs.clear();
660 OutStreamer.AddBlankLine();
Nate Begemane59bf592004-08-14 22:09:10 +0000661 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000662
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000663 Stubs = MMIMacho.GetHiddenGVStubList();
664 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000665 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000666 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000667
668 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +0000669 // L_foo$stub:
670 OutStreamer.EmitLabel(Stubs[i].first);
671 // .long _foo
672 OutStreamer.EmitValue(MCSymbolRefExpr::
673 Create(Stubs[i].second.getPointer(),
674 OutContext),
675 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000676 }
Bill Wendling08d726c2010-03-11 23:39:44 +0000677
678 Stubs.clear();
679 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000680 }
681
Chris Lattner5b0ac992005-11-01 00:12:36 +0000682 // Funny Darwin hack: This flag tells the linker that no global symbols
683 // contain code that falls through to other global symbols (e.g. the obvious
684 // implementation of multiple entry points). If this doesn't occur, the
685 // linker can safely perform dead code stripping. Since LLVM never generates
686 // code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000687 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Chris Lattner5b0ac992005-11-01 00:12:36 +0000688
Dan Gohmanb8275a32007-07-25 19:33:14 +0000689 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000690}
Nate Begemaned428532004-09-04 05:00:00 +0000691
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000692/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
693/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +0000694/// Darwin assembler can deal with.
695///
Chris Lattnerb23569a2010-04-04 08:18:47 +0000696static AsmPrinter *createPPCAsmPrinterPass(TargetMachine &tm,
Chris Lattner11d53c12010-03-13 20:55:24 +0000697 MCStreamer &Streamer) {
Jim Laskeybf111822006-12-21 20:26:09 +0000698 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
699
Chris Lattner40bbebd2009-07-21 18:38:57 +0000700 if (Subtarget->isDarwin())
Chris Lattnerb23569a2010-04-04 08:18:47 +0000701 return new PPCDarwinAsmPrinter(tm, Streamer);
702 return new PPCLinuxAsmPrinter(tm, Streamer);
Chris Lattner4da1c822006-09-20 17:12:19 +0000703}
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000704
Bob Wilsona96751f2009-06-23 23:59:40 +0000705// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000706extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000707 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000708 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
709}