blob: e31e490e8d221d33c98c13542f176c85af24472b [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +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 Lattnerdf4ed632006-11-17 22:10:59 +000021#include "PPCPredicates.h"
Chris Lattner16e71f22005-10-14 23:59:06 +000022#include "PPCTargetMachine.h"
Chris Lattner26689592005-10-14 23:51:18 +000023#include "PPCSubtarget.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
27#include "llvm/Assembly/Writer.h"
Chris Lattnera3840792004-08-16 23:25:21 +000028#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskeya7cea6f2006-01-04 13:52:30 +000029#include "llvm/CodeGen/DwarfWriter.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 Lattnerf9bdedd2009-08-10 18:15:01 +000038#include "llvm/MC/MCSectionMachO.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000039#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000041#include "llvm/Target/Mangler.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000042#include "llvm/Target/TargetRegisterInfo.h"
43#include "llvm/Target/TargetInstrInfo.h"
44#include "llvm/Target/TargetOptions.h"
45#include "llvm/Target/TargetRegistry.h"
Nate Begemana11c2e82004-09-04 14:51:26 +000046#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000047#include "llvm/Support/CommandLine.h"
48#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000049#include "llvm/Support/ErrorHandling.h"
David Greene71847812009-07-14 20:18:05 +000050#include "llvm/Support/FormattedStream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000051#include "llvm/ADT/StringExtras.h"
Evan Chengae94e592008-12-05 01:06:39 +000052#include "llvm/ADT/StringSet.h"
Chris Lattner48130352010-01-13 06:38:18 +000053#include "llvm/ADT/SmallString.h"
Chris Lattnera3840792004-08-16 23:25:21 +000054using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000055
Chris Lattner95b2c7d2006-12-19 22:59:26 +000056namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000057 class PPCAsmPrinter : public AsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +000058 protected:
Chris Lattner9d7efd32010-04-04 07:05:53 +000059 DenseMap<MCSymbol*, MCSymbol*> TOC;
Chris Lattnerdadceed2006-09-20 17:07:15 +000060 const PPCSubtarget &Subtarget;
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000061 uint64_t LabelID;
Bill Wendling57f0db82009-02-24 08:30:20 +000062 public:
David Greene71847812009-07-14 20:18:05 +000063 explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner11d53c12010-03-13 20:55:24 +000064 MCStreamer &Streamer)
65 : AsmPrinter(O, TM, Streamer),
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000066 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000067
Misha Brukman5dfe3a92004-06-21 16:55:25 +000068 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000069 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000070 }
71
Nate Begeman21e463b2005-10-16 05:39:50 +000072 PPCTargetMachine &getTM() {
73 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000074 }
75
Nate Begemanadeb43d2005-07-20 22:42:00 +000076 unsigned enumRegToMachineReg(unsigned enumReg) {
77 switch (enumReg) {
Torok Edwinc23197a2009-07-14 16:55:14 +000078 default: llvm_unreachable("Unhandled register!");
Nate Begemanadeb43d2005-07-20 22:42:00 +000079 case PPC::CR0: return 0;
80 case PPC::CR1: return 1;
81 case PPC::CR2: return 2;
82 case PPC::CR3: return 3;
83 case PPC::CR4: return 4;
84 case PPC::CR5: return 5;
85 case PPC::CR6: return 6;
86 case PPC::CR7: return 7;
87 }
Torok Edwinc23197a2009-07-14 16:55:14 +000088 llvm_unreachable(0);
Nate Begemanadeb43d2005-07-20 22:42:00 +000089 }
90
Nate Begemane59bf592004-08-14 22:09:10 +000091 /// printInstruction - This method is automatically generated by tablegen
92 /// from the instruction set description. This method returns true if the
93 /// machine instruction was sufficiently described to print it, otherwise it
94 /// returns false.
Chris Lattner35c33bd2010-04-04 04:47:45 +000095 void printInstruction(const MachineInstr *MI, raw_ostream &O);
Chris Lattnerd95148f2009-09-13 20:19:22 +000096 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000097
Nate Begemane59bf592004-08-14 22:09:10 +000098
Chris Lattnerd49fe1b2010-01-28 01:28:58 +000099 virtual void EmitInstruction(const MachineInstr *MI);
Chris Lattner35c33bd2010-04-04 04:47:45 +0000100 void printOp(const MachineOperand &MO, raw_ostream &O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000101
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000102 /// stripRegisterPrefix - This method strips the character prefix from a
103 /// register name so that only the number is left. Used by for linux asm.
Jim Laskey40ee69f2006-12-20 21:33:34 +0000104 const char *stripRegisterPrefix(const char *RegName) {
105 switch (RegName[0]) {
106 case 'r':
107 case 'f':
108 case 'v': return RegName + 1;
Jim Laskey2aa14aa2006-12-20 21:35:00 +0000109 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000110 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000111
Jim Laskey40ee69f2006-12-20 21:33:34 +0000112 return RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000113 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000114
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000115 /// printRegister - Print register according to target requirements.
116 ///
Chris Lattner35c33bd2010-04-04 04:47:45 +0000117 void printRegister(const MachineOperand &MO, bool R0AsZero, raw_ostream &O){
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000118 unsigned RegNo = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000119 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000120
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000121 // If we should use 0 for R0.
122 if (R0AsZero && RegNo == PPC::R0) {
123 O << "0";
124 return;
125 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000126
Chris Lattner762ccea2009-09-13 20:31:40 +0000127 const char *RegName = getRegisterName(RegNo);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000128 // Linux assembler (Others?) does not take register mnemonics.
129 // FIXME - What about special registers used in mfspr/mtspr?
Jim Laskey40ee69f2006-12-20 21:33:34 +0000130 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
131 O << RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000132 }
Chris Lattner7bb424f2004-08-14 23:27:29 +0000133
Chris Lattner35c33bd2010-04-04 04:47:45 +0000134 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Chris Lattner7bb424f2004-08-14 23:27:29 +0000135 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmand735b802008-10-03 15:45:36 +0000136 if (MO.isReg()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000137 printRegister(MO, false, O);
Dan Gohmand735b802008-10-03 15:45:36 +0000138 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000139 O << MO.getImm();
Chris Lattner7bb424f2004-08-14 23:27:29 +0000140 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000141 printOp(MO, O);
Chris Lattner7bb424f2004-08-14 23:27:29 +0000142 }
143 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000144
Chris Lattner58873272006-02-01 22:38:46 +0000145 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000146 unsigned AsmVariant, const char *ExtraCode,
147 raw_ostream &O);
Chris Lattner2c003e22006-02-24 20:27:40 +0000148 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000149 unsigned AsmVariant, const char *ExtraCode,
150 raw_ostream &O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000151
152
Chris Lattner35c33bd2010-04-04 04:47:45 +0000153 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo,
154 raw_ostream &O) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000155 char value = MI->getOperand(OpNo).getImm();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000156 value = (value << (32-5)) >> (32-5);
157 O << (int)value;
158 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000159 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo,
160 raw_ostream &O) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000161 unsigned char value = MI->getOperand(OpNo).getImm();
Chris Lattner12585ba2004-08-21 19:11:03 +0000162 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000163 O << (unsigned int)value;
164 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000165 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo,
166 raw_ostream &O) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000167 unsigned char value = MI->getOperand(OpNo).getImm();
Nate Begeman07aada82004-08-30 02:28:06 +0000168 assert(value <= 63 && "Invalid u6imm argument!");
169 O << (unsigned int)value;
170 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000171 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo,
172 raw_ostream &O) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000173 O << (short)MI->getOperand(OpNo).getImm();
Nate Begemaned428532004-09-04 05:00:00 +0000174 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000175 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo,
176 raw_ostream &O) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000177 O << (unsigned short)MI->getOperand(OpNo).getImm();
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000178 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000179 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo,
180 raw_ostream &O) {
Dan Gohmand735b802008-10-03 15:45:36 +0000181 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000182 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000183 } else {
184 O << "lo16(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000185 printOp(MI->getOperand(OpNo), O);
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000186 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000187 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000188 else
189 O << ')';
190 }
Chris Lattner841d12d2005-10-18 16:51:22 +0000191 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000192 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
193 raw_ostream &O) {
Nate Begemaned428532004-09-04 05:00:00 +0000194 // Branches can take an immediate operand. This is used by the branch
195 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmand735b802008-10-03 15:45:36 +0000196 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000197 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Nate Begemaned428532004-09-04 05:00:00 +0000198 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000199 printOp(MI->getOperand(OpNo), O);
Nate Begemaned428532004-09-04 05:00:00 +0000200 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000201 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000202 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
203 raw_ostream &O) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000204 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000205 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000206 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
207 GlobalValue *GV = MO.getGlobal();
Chris Lattner20674332009-07-02 16:08:53 +0000208 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Chris Lattnera637c322005-12-16 00:22:14 +0000209 // Dynamically-resolved functions need a stub for the function.
Chris Lattner73a1aa02010-01-20 21:36:48 +0000210 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingcebae362010-03-10 22:34:10 +0000211 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner73a1aa02010-01-20 21:36:48 +0000212 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000213 if (StubSym.getPointer() == 0)
214 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000215 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000216 O << *Sym;
Chris Lattnera637c322005-12-16 00:22:14 +0000217 return;
218 }
219 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000220 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000221 SmallString<128> TempNameStr;
222 TempNameStr += StringRef(MO.getSymbolName());
223 TempNameStr += StringRef("$stub");
224
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000225 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Bill Wendlingcebae362010-03-10 22:34:10 +0000226 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner73a1aa02010-01-20 21:36:48 +0000227 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000228 if (StubSym.getPointer() == 0)
229 StubSym = MachineModuleInfoImpl::
230 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000231 O << *Sym;
Chris Lattner9542f9712005-11-17 19:40:30 +0000232 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000233 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000234 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000235
Chris Lattner35c33bd2010-04-04 04:47:45 +0000236 printOp(MI->getOperand(OpNo), O);
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000237 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000238 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
239 raw_ostream &O) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000240 O << (int)MI->getOperand(OpNo).getImm()*4;
Nate Begeman422b0ce2005-11-16 00:48:01 +0000241 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000242 void printPICLabel(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000243 O << "\"L" << getFunctionNumber() << "$pb\"\n";
244 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000245 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000246 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmand735b802008-10-03 15:45:36 +0000247 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000248 printS16ImmOperand(MI, OpNo, O);
Nate Begeman2497e632005-07-21 20:44:43 +0000249 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000250 if (Subtarget.isDarwin()) O << "ha16(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000251 printOp(MI->getOperand(OpNo), O);
Chris Lattner35d86fe2006-07-26 21:12:04 +0000252 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000253 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000254 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000255 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000256 else
257 O << "@ha";
Nate Begeman2497e632005-07-21 20:44:43 +0000258 }
Nate Begemaned428532004-09-04 05:00:00 +0000259 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000260 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Dan Gohmand735b802008-10-03 15:45:36 +0000261 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000262 printS16ImmOperand(MI, OpNo, O);
Nate Begemaned428532004-09-04 05:00:00 +0000263 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000264 if (Subtarget.isDarwin()) O << "lo16(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000265 printOp(MI->getOperand(OpNo), O);
Chris Lattner35d86fe2006-07-26 21:12:04 +0000266 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000267 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000268 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000269 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000270 else
271 O << "@l";
Nate Begemaned428532004-09-04 05:00:00 +0000272 }
273 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000274 void printcrbitm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000275 unsigned CCReg = MI->getOperand(OpNo).getReg();
276 unsigned RegNo = enumRegToMachineReg(CCReg);
277 O << (0x80 >> RegNo);
278 }
Chris Lattner2c003e22006-02-24 20:27:40 +0000279 // The new addressing mode printers.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000280 void printMemRegImm(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
281 printSymbolLo(MI, OpNo, O);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000282 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000283 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattner13feb582006-03-21 17:21:13 +0000284 MI->getOperand(OpNo+1).getReg() == PPC::R0)
285 O << "0";
286 else
Chris Lattner35c33bd2010-04-04 04:47:45 +0000287 printOperand(MI, OpNo+1, O);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000288 O << ')';
289 }
Chris Lattner35c33bd2010-04-04 04:47:45 +0000290 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo,
291 raw_ostream &O) {
Dan Gohmand735b802008-10-03 15:45:36 +0000292 if (MI->getOperand(OpNo).isImm())
Chris Lattner35c33bd2010-04-04 04:47:45 +0000293 printS16X4ImmOperand(MI, OpNo, O);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000294 else
Chris Lattner35c33bd2010-04-04 04:47:45 +0000295 printSymbolLo(MI, OpNo, O);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000296 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000297 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattnere5ba5802006-03-22 05:26:03 +0000298 MI->getOperand(OpNo+1).getReg() == PPC::R0)
299 O << "0";
300 else
Chris Lattner35c33bd2010-04-04 04:47:45 +0000301 printOperand(MI, OpNo+1, O);
Chris Lattnere5ba5802006-03-22 05:26:03 +0000302 O << ')';
303 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000304
Chris Lattner35c33bd2010-04-04 04:47:45 +0000305 void printMemRegReg(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
Nate Begeman88276b82005-12-19 23:40:42 +0000306 // When used as the base register, r0 reads constant zero rather than
307 // the value contained in the register. For this reason, the darwin
308 // assembler requires that we print r0 as 0 (no r) when used as the base.
309 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner35c33bd2010-04-04 04:47:45 +0000310 printRegister(MO, true, O);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000311 O << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000312 printOperand(MI, OpNo+1, O);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000313 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000314
Chris Lattner35c33bd2010-04-04 04:47:45 +0000315 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo,
316 raw_ostream &O) {
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000317 const MachineOperand &MO = MI->getOperand(OpNo);
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000318 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
Chris Lattner9d7efd32010-04-04 07:05:53 +0000319 MCSymbol *Sym = Mang->getSymbol(MO.getGlobal());
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000320
321 // Map symbol -> label of TOC entry.
Chris Lattner9d7efd32010-04-04 07:05:53 +0000322 MCSymbol *&TOCEntry = TOC[Sym];
Chris Lattner75abc682010-01-16 02:09:06 +0000323 if (TOCEntry == 0)
324 TOCEntry = OutContext.
Chris Lattner9b97a732010-03-30 18:10:53 +0000325 GetOrCreateSymbol(StringRef(MAI->getPrivateGlobalPrefix()) +
326 "C" + Twine(LabelID++));
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000327
Chris Lattner10b318b2010-01-17 21:43:43 +0000328 O << *TOCEntry << "@toc";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000329 }
330
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000331 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000332 raw_ostream &O, const char *Modifier);
Nate Begemaned428532004-09-04 05:00:00 +0000333 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000334
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000335 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000336 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000337 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000338 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner11d53c12010-03-13 20:55:24 +0000339 MCStreamer &Streamer)
340 : PPCAsmPrinter(O, TM, Streamer) {}
Jim Laskeybf111822006-12-21 20:26:09 +0000341
342 virtual const char *getPassName() const {
343 return "Linux PPC Assembly Printer";
344 }
345
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000346 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000347
Chris Lattner2cf72512010-01-27 07:21:55 +0000348 virtual void EmitFunctionEntryLabel();
349
Jim Laskeybf111822006-12-21 20:26:09 +0000350 void getAnalysisUsage(AnalysisUsage &AU) const {
351 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000352 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000353 AU.addRequired<DwarfWriter>();
Jim Laskeybf111822006-12-21 20:26:09 +0000354 PPCAsmPrinter::getAnalysisUsage(AU);
355 }
Jim Laskeybf111822006-12-21 20:26:09 +0000356 };
357
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000358 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
359 /// OS X
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000360 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene71847812009-07-14 20:18:05 +0000361 formatted_raw_ostream &OS;
Bill Wendling57f0db82009-02-24 08:30:20 +0000362 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000363 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner11d53c12010-03-13 20:55:24 +0000364 MCStreamer &Streamer)
365 : PPCAsmPrinter(O, TM, Streamer), OS(O) {}
Nate Begemaned428532004-09-04 05:00:00 +0000366
367 virtual const char *getPassName() const {
368 return "Darwin PPC Assembly Printer";
369 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000370
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000371 bool doFinalization(Module &M);
Bob Wilson812209a2009-09-30 22:06:26 +0000372 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000373
Chris Lattner73a1aa02010-01-20 21:36:48 +0000374 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Chris Lattner917d6282010-01-20 21:19:44 +0000375
Jim Laskeyb2efb852006-01-04 22:28:25 +0000376 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000377 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000378 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000379 AU.addRequired<DwarfWriter>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000380 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000381 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000382 };
383} // end of anonymous namespace
384
Nate Begemane59bf592004-08-14 22:09:10 +0000385// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000386#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000387
Chris Lattner35c33bd2010-04-04 04:47:45 +0000388void PPCAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000389 switch (MO.getType()) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000390 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +0000391 llvm_unreachable("printOp() does not handle immediate values");
Misha Brukman97a296f2004-07-21 20:11:11 +0000392
Nate Begeman37efe672006-04-22 18:53:45 +0000393 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000394 O << *MO.getMBB()->getSymbol();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000395 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000396 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000397 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000398 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000399 // FIXME: PIC relocation model
400 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000401 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000402 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000403 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000404 return;
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000405 case MachineOperand::MO_BlockAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +0000406 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000407 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000408 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000409 // Computing the address of an external symbol, not calling it.
Chris Lattner75abc682010-01-16 02:09:06 +0000410 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000411 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner75abc682010-01-16 02:09:06 +0000412 return;
Chris Lattnera637c322005-12-16 00:22:14 +0000413 }
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000414
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000415 MCSymbol *NLPSym =
Chris Lattner75abc682010-01-16 02:09:06 +0000416 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
417 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000418 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000419 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000420 if (StubSym.getPointer() == 0)
421 StubSym = MachineModuleInfoImpl::
422 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000423
Chris Lattner10b318b2010-01-17 21:43:43 +0000424 O << *NLPSym;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000425 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000426 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000427 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000428 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000429 GlobalValue *GV = MO.getGlobal();
Chris Lattnera40128c2010-01-16 02:00:23 +0000430 MCSymbol *SymToPrint;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000431
Nate Begemanfcf4a422004-10-17 23:01:34 +0000432 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000433 if (TM.getRelocationModel() != Reloc::Static &&
434 (GV->isDeclaration() || GV->isWeakForLinker())) {
435 if (!GV->hasHiddenVisibility()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000436 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000437 MachineModuleInfoImpl::StubValueTy &StubSym =
438 MMI->getObjFileInfo<MachineModuleInfoMachO>()
439 .getGVStubEntry(SymToPrint);
440 if (StubSym.getPointer() == 0)
441 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000442 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000443 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
444 GV->hasAvailableExternallyLinkage()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000445 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000446
Bill Wendlingcebae362010-03-10 22:34:10 +0000447 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000448 MMI->getObjFileInfo<MachineModuleInfoMachO>().
449 getHiddenGVStubEntry(SymToPrint);
Bill Wendlingcebae362010-03-10 22:34:10 +0000450 if (StubSym.getPointer() == 0)
451 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000452 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000453 } else {
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000454 SymToPrint = Mang->getSymbol(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000455 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000456 } else {
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000457 SymToPrint = Mang->getSymbol(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000458 }
Chris Lattnera40128c2010-01-16 02:00:23 +0000459
Chris Lattner10b318b2010-01-17 21:43:43 +0000460 O << *SymToPrint;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000461
Chris Lattner0c08d092010-04-03 22:28:33 +0000462 printOffset(MO.getOffset(), O);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000463 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000464 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000465
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000466 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000467 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000468 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000469 }
470}
471
Chris Lattnere3f01572006-02-23 19:31:10 +0000472/// PrintAsmOperand - Print out an operand for an inline asm expression.
473///
474bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000475 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000476 const char *ExtraCode, raw_ostream &O) {
Chris Lattnere3f01572006-02-23 19:31:10 +0000477 // Does this asm operand have a single letter operand modifier?
478 if (ExtraCode && ExtraCode[0]) {
479 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000480
Chris Lattnere3f01572006-02-23 19:31:10 +0000481 switch (ExtraCode[0]) {
482 default: return true; // Unknown modifier.
Chris Lattner78192b62007-01-25 02:52:50 +0000483 case 'c': // Don't print "$" before a global var name or constant.
484 // PPC never has a prefix.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000485 printOperand(MI, OpNo, O);
Chris Lattner78192b62007-01-25 02:52:50 +0000486 return false;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000487 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000488 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000489 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000490 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000491 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000492 return true;
493 ++OpNo; // Return the high-part.
494 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000495 case 'I':
496 // Write 'i' if an integer constant, otherwise nothing. Used to print
497 // addi vs add, etc.
Dan Gohmand735b802008-10-03 15:45:36 +0000498 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000499 O << "i";
500 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000501 }
502 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000503
Chris Lattner35c33bd2010-04-04 04:47:45 +0000504 printOperand(MI, OpNo, O);
Chris Lattnere3f01572006-02-23 19:31:10 +0000505 return false;
506}
507
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000508// At the moment, all inline asm memory operands are a single register.
509// In any case, the output of this routine should always be just one
510// assembler operand.
511
Chris Lattner2c003e22006-02-24 20:27:40 +0000512bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000513 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000514 const char *ExtraCode,
515 raw_ostream &O) {
Chris Lattner2c003e22006-02-24 20:27:40 +0000516 if (ExtraCode && ExtraCode[0])
517 return true; // Unknown modifier.
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000518 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen4e68f882009-08-26 18:10:32 +0000519 O << "0(";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000520 printOperand(MI, OpNo, O);
Dale Johannesen4e68f882009-08-26 18:10:32 +0000521 O << ")";
Chris Lattner2c003e22006-02-24 20:27:40 +0000522 return false;
523}
Chris Lattnere3f01572006-02-23 19:31:10 +0000524
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000525void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000526 raw_ostream &O, const char *Modifier){
Chris Lattneraf53a872006-11-04 05:27:39 +0000527 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
528 unsigned Code = MI->getOperand(OpNo).getImm();
529 if (!strcmp(Modifier, "cc")) {
530 switch ((PPC::Predicate)Code) {
531 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
532 case PPC::PRED_LT: O << "lt"; return;
533 case PPC::PRED_LE: O << "le"; return;
534 case PPC::PRED_EQ: O << "eq"; return;
535 case PPC::PRED_GE: O << "ge"; return;
536 case PPC::PRED_GT: O << "gt"; return;
537 case PPC::PRED_NE: O << "ne"; return;
538 case PPC::PRED_UN: O << "un"; return;
539 case PPC::PRED_NU: O << "nu"; return;
540 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000541
Chris Lattneraf53a872006-11-04 05:27:39 +0000542 } else {
543 assert(!strcmp(Modifier, "reg") &&
544 "Need to specify 'cc' or 'reg' as predicate op modifier!");
545 // Don't print the register for 'always'.
546 if (Code == PPC::PRED_ALWAYS) return;
Chris Lattner35c33bd2010-04-04 04:47:45 +0000547 printOperand(MI, OpNo+1, O);
Chris Lattneraf53a872006-11-04 05:27:39 +0000548 }
549}
550
551
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000552/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Nate Begemane59bf592004-08-14 22:09:10 +0000553/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000554///
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000555void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000556 SmallString<128> Str;
557 raw_svector_ostream O(Str);
558
Nate Begemanad5f65c2005-04-05 18:19:50 +0000559 // Check for slwi/srwi mnemonics.
560 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000561 unsigned char SH = MI->getOperand(2).getImm();
562 unsigned char MB = MI->getOperand(3).getImm();
563 unsigned char ME = MI->getOperand(4).getImm();
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000564 bool useSubstituteMnemonic = false;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000565 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesene89c5102010-01-06 02:20:18 +0000566 O << "\tslwi "; useSubstituteMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000567 }
568 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesene89c5102010-01-06 02:20:18 +0000569 O << "\tsrwi "; useSubstituteMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000570 SH = 32-SH;
571 }
Dale Johannesene89c5102010-01-06 02:20:18 +0000572 if (useSubstituteMnemonic) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000573 printOperand(MI, 0, O);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000574 O << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000575 printOperand(MI, 1, O);
Dale Johannesene89c5102010-01-06 02:20:18 +0000576 O << ", " << (unsigned int)SH;
Chris Lattner9d7efd32010-04-04 07:05:53 +0000577 OutStreamer.EmitRawText(O.str());
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000578 return;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000579 }
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000580 }
581
582 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
583 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
584 O << "\tmr ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000585 printOperand(MI, 0, O);
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000586 O << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000587 printOperand(MI, 1, O);
Chris Lattner9d7efd32010-04-04 07:05:53 +0000588 OutStreamer.EmitRawText(O.str());
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000589 return;
590 }
591
592 if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000593 unsigned char SH = MI->getOperand(2).getImm();
594 unsigned char ME = MI->getOperand(3).getImm();
Chris Lattner566c1b12006-11-18 01:23:56 +0000595 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
596 if (63-SH == ME) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000597 O << "\tsldi ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000598 printOperand(MI, 0, O);
Chris Lattner566c1b12006-11-18 01:23:56 +0000599 O << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000600 printOperand(MI, 1, O);
Dale Johannesene89c5102010-01-06 02:20:18 +0000601 O << ", " << (unsigned int)SH;
Chris Lattner9d7efd32010-04-04 07:05:53 +0000602 OutStreamer.EmitRawText(O.str());
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000603 return;
Chris Lattner566c1b12006-11-18 01:23:56 +0000604 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000605 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000606
Chris Lattner9d7efd32010-04-04 07:05:53 +0000607 printInstruction(MI, O);
608 OutStreamer.EmitRawText(O.str());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000609}
610
Chris Lattner2cf72512010-01-27 07:21:55 +0000611void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
612 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
613 return AsmPrinter::EmitFunctionEntryLabel();
614
615 // Emit an official procedure descriptor.
616 // FIXME 64-bit SVR4: Use MCSection here!
Chris Lattner9d7efd32010-04-04 07:05:53 +0000617 OutStreamer.EmitRawText(StringRef("\t.section\t\".opd\",\"aw\""));
618 OutStreamer.EmitRawText(StringRef("\t.align 3"));
Chris Lattner2cf72512010-01-27 07:21:55 +0000619 OutStreamer.EmitLabel(CurrentFnSym);
Chris Lattner9d7efd32010-04-04 07:05:53 +0000620 OutStreamer.EmitRawText("\t.quad .L." + Twine(CurrentFnSym->getName()) +
621 ",.TOC.@tocbase");
622 OutStreamer.EmitRawText(StringRef("\t.previous"));
623 OutStreamer.EmitRawText(".L." + Twine(CurrentFnSym->getName()) + ":");
Chris Lattner2cf72512010-01-27 07:21:55 +0000624}
625
626
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000627bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
628 const TargetData *TD = TM.getTargetData();
629
630 bool isPPC64 = TD->getPointerSizeInBits() == 64;
631
632 if (isPPC64 && !TOC.empty()) {
633 // FIXME 64-bit SVR4: Use MCSection here?
Chris Lattner9d7efd32010-04-04 07:05:53 +0000634 OutStreamer.EmitRawText(StringRef("\t.section\t\".toc\",\"aw\""));
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000635
Chris Lattnera40128c2010-01-16 02:00:23 +0000636 // FIXME: This is nondeterminstic!
Chris Lattner9d7efd32010-04-04 07:05:53 +0000637 for (DenseMap<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
Chris Lattner75abc682010-01-16 02:09:06 +0000638 E = TOC.end(); I != E; ++I) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000639 OutStreamer.EmitLabel(I->second);
640 OutStreamer.EmitRawText("\t.tc " + Twine(I->first->getName()) +
641 "[TC]," + I->first->getName());
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000642 }
643 }
644
645 return AsmPrinter::doFinalization(M);
646}
Jim Laskeybf111822006-12-21 20:26:09 +0000647
Bob Wilson812209a2009-09-30 22:06:26 +0000648void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000649 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000650 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000651 "ppc",
652 "ppc601",
653 "ppc602",
654 "ppc603",
655 "ppc7400",
656 "ppc750",
657 "ppc970",
658 "ppc64"
659 };
660
661 unsigned Directive = Subtarget.getDarwinDirective();
662 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
663 Directive = PPC::DIR_970;
664 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
665 Directive = PPC::DIR_7400;
666 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
667 Directive = PPC::DIR_64;
668 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Chris Lattner9d7efd32010-04-04 07:05:53 +0000669 OutStreamer.EmitRawText("\t.machine " + Twine(CPUDirectives[Directive]));
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000670
Jim Laskey2ada0852006-11-28 18:21:52 +0000671 // Prime text sections so they are adjacent. This reduces the likelihood a
672 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattner2dfddee2009-08-03 22:52:21 +0000673 TargetLoweringObjectFileMachO &TLOFMacho =
674 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000675 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskey2ada0852006-11-28 18:21:52 +0000676 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000677 OutStreamer.SwitchSection(
678 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
679 MCSectionMachO::S_SYMBOL_STUBS |
680 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
681 32, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000682 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000683 OutStreamer.SwitchSection(
684 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
685 MCSectionMachO::S_SYMBOL_STUBS |
686 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
687 16, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000688 }
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000689 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman18ed0292005-07-21 01:25:49 +0000690}
691
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000692static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000693 // Remove $stub suffix, add $lazy_ptr.
694 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
695 TmpStr += "$lazy_ptr";
Chris Lattner9b97a732010-03-30 18:10:53 +0000696 return Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000697}
698
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000699static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000700 // Add $tmp suffix to $stub, yielding $stub$tmp.
701 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
702 TmpStr += "$tmp";
Chris Lattner9b97a732010-03-30 18:10:53 +0000703 return Ctx.GetOrCreateSymbol(TmpStr.str());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000704}
705
706void PPCDarwinAsmPrinter::
707EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chris Lattner917d6282010-01-20 21:19:44 +0000708 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
709
Chris Lattner2dfddee2009-08-03 22:52:21 +0000710 TargetLoweringObjectFileMachO &TLOFMacho =
711 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner917d6282010-01-20 21:19:44 +0000712
713 // .lazy_symbol_pointer
714 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattnerff4bc462009-08-10 01:39:42 +0000715
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000716 // Output stubs for dynamically-linked functions
Chris Lattner917d6282010-01-20 21:19:44 +0000717 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2dfddee2009-08-03 22:52:21 +0000718 const MCSection *StubSection =
Chris Lattner917d6282010-01-20 21:19:44 +0000719 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
720 MCSectionMachO::S_SYMBOL_STUBS |
721 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
722 32, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000723 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000724 OutStreamer.SwitchSection(StubSection);
Chris Lattner7e097e42006-06-27 01:02:25 +0000725 EmitAlignment(4);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000726
Chris Lattner9d7efd32010-04-04 07:05:53 +0000727 MCSymbol *Stub = Stubs[i].first;
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000728 MCSymbol *RawSym = Stubs[i].second.getPointer();
729 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
730 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000731
Chris Lattner9d7efd32010-04-04 07:05:53 +0000732 OutStreamer.EmitLabel(Stub);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000733 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
734 // FIXME: MCize this.
Chris Lattner1841b342010-04-04 07:17:25 +0000735 OutStreamer.EmitRawText(StringRef("\tmflr r0"));
736 OutStreamer.EmitRawText("\tbcl 20,31," + Twine(AnonSymbol->getName()));
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000737 OutStreamer.EmitLabel(AnonSymbol);
Chris Lattner1841b342010-04-04 07:17:25 +0000738 OutStreamer.EmitRawText(StringRef("\tmflr r11"));
739 OutStreamer.EmitRawText("\taddis r11,r11,ha16("+Twine(LazyPtr->getName())+
740 "-" + AnonSymbol->getName() + ")");
741 OutStreamer.EmitRawText(StringRef("\tmtlr r0"));
742
743 if (isPPC64)
744 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
745 "-" + AnonSymbol->getName() + ")(r11)");
746 else
747 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
748 "-" + AnonSymbol->getName() + ")(r11)");
749 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
750 OutStreamer.EmitRawText(StringRef("\tbctr"));
Chris Lattnerfe2fe702009-07-16 01:23:26 +0000751
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000752 OutStreamer.SwitchSection(LSPSection);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000753 OutStreamer.EmitLabel(LazyPtr);
754 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Chris Lattner1841b342010-04-04 07:17:25 +0000755
756 if (isPPC64)
757 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
758 else
759 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
Chris Lattnerdeea4162005-12-13 04:33:58 +0000760 }
Chris Lattner9d7efd32010-04-04 07:05:53 +0000761 OutStreamer.AddBlankLine();
Chris Lattner917d6282010-01-20 21:19:44 +0000762 return;
Misha Brukman46fd00a2004-06-24 23:04:11 +0000763 }
Chris Lattner917d6282010-01-20 21:19:44 +0000764
765 const MCSection *StubSection =
766 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
767 MCSectionMachO::S_SYMBOL_STUBS |
768 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
769 16, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000770 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000771 MCSymbol *Stub = Stubs[i].first;
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000772 MCSymbol *RawSym = Stubs[i].second.getPointer();
773 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000774
Chris Lattner917d6282010-01-20 21:19:44 +0000775 OutStreamer.SwitchSection(StubSection);
776 EmitAlignment(4);
Chris Lattner9d7efd32010-04-04 07:05:53 +0000777 OutStreamer.EmitLabel(Stub);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000778 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Chris Lattner1841b342010-04-04 07:17:25 +0000779 OutStreamer.EmitRawText("\tlis r11,ha16(" + Twine(LazyPtr->getName()) +")");
780 if (isPPC64)
781 OutStreamer.EmitRawText("\tldu r12,lo16(" + Twine(LazyPtr->getName()) +
782 ")(r11)");
783 else
784 OutStreamer.EmitRawText("\tlwzu r12,lo16(" + Twine(LazyPtr->getName()) +
785 ")(r11)");
786 OutStreamer.EmitRawText(StringRef("\tmtctr r12"));
787 OutStreamer.EmitRawText(StringRef("\tbctr"));
Chris Lattner917d6282010-01-20 21:19:44 +0000788 OutStreamer.SwitchSection(LSPSection);
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000789 OutStreamer.EmitLabel(LazyPtr);
790 OutStreamer.EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol);
Chris Lattner1841b342010-04-04 07:17:25 +0000791
792 if (isPPC64)
793 OutStreamer.EmitRawText(StringRef("\t.quad dyld_stub_binding_helper"));
794 else
795 OutStreamer.EmitRawText(StringRef("\t.long dyld_stub_binding_helper"));
Chris Lattner917d6282010-01-20 21:19:44 +0000796 }
797
Chris Lattner2dc6fa62010-04-04 07:12:28 +0000798 OutStreamer.AddBlankLine();
Chris Lattner917d6282010-01-20 21:19:44 +0000799}
800
801
802bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
803 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
804
805 // Darwin/PPC always uses mach-o.
806 TargetLoweringObjectFileMachO &TLOFMacho =
807 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
808 MachineModuleInfoMachO &MMIMacho =
809 MMI->getObjFileInfo<MachineModuleInfoMachO>();
810
Chris Lattner73a1aa02010-01-20 21:36:48 +0000811 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
812 if (!Stubs.empty())
813 EmitFunctionStubs(Stubs);
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000814
Chris Lattner33adcfb2009-08-22 21:43:10 +0000815 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000816 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000817 // Only referenced functions get into the Personalities list.
Chris Lattner0de1fc42009-06-24 19:09:55 +0000818 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000819 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +0000820 E = Personalities.end(); I != E; ++I) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000821 if (*I) {
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000822 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000823 MachineModuleInfoImpl::StubValueTy &StubSym =
824 MMIMacho.getGVStubEntry(NLPSym);
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000825 StubSym = MachineModuleInfoImpl::StubValueTy(Mang->getSymbol(*I), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000826 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000827 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000828 }
829
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000830 // Output stubs for dynamically-linked functions.
Chris Lattner73a1aa02010-01-20 21:36:48 +0000831 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000832
Chris Lattner2dfddee2009-08-03 22:52:21 +0000833 // Output macho stubs for external and common global variables.
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000834 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000835 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000836 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnere1e8a822009-08-10 17:58:51 +0000837 EmitAlignment(isPPC64 ? 3 : 2);
838
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000839 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +0000840 // L_foo$stub:
841 OutStreamer.EmitLabel(Stubs[i].first);
842 // .indirect_symbol _foo
843 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
844 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendling53351a12010-03-12 02:00:43 +0000845
846 if (MCSym.getInt())
847 // External to current translation unit.
848 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
849 else
850 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000851 //
852 // When we place the LSDA into the TEXT section, the type info pointers
853 // need to be indirect and pc-rel. We accomplish this by using NLPs.
854 // However, sometimes the types are local to the file. So we need to
855 // fill in the value for the NLP in those cases.
Bill Wendling53351a12010-03-12 02:00:43 +0000856 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
857 OutContext),
858 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000859 }
Bill Wendling08d726c2010-03-11 23:39:44 +0000860
861 Stubs.clear();
862 OutStreamer.AddBlankLine();
Nate Begemane59bf592004-08-14 22:09:10 +0000863 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000864
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000865 Stubs = MMIMacho.GetHiddenGVStubList();
866 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000867 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000868 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000869
870 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +0000871 // L_foo$stub:
872 OutStreamer.EmitLabel(Stubs[i].first);
873 // .long _foo
874 OutStreamer.EmitValue(MCSymbolRefExpr::
875 Create(Stubs[i].second.getPointer(),
876 OutContext),
877 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000878 }
Bill Wendling08d726c2010-03-11 23:39:44 +0000879
880 Stubs.clear();
881 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000882 }
883
Chris Lattner5b0ac992005-11-01 00:12:36 +0000884 // Funny Darwin hack: This flag tells the linker that no global symbols
885 // contain code that falls through to other global symbols (e.g. the obvious
886 // implementation of multiple entry points). If this doesn't occur, the
887 // linker can safely perform dead code stripping. Since LLVM never generates
888 // code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000889 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Chris Lattner5b0ac992005-11-01 00:12:36 +0000890
Dan Gohmanb8275a32007-07-25 19:33:14 +0000891 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000892}
Nate Begemaned428532004-09-04 05:00:00 +0000893
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000894/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
895/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +0000896/// Darwin assembler can deal with.
897///
Daniel Dunbar78945782009-08-13 23:48:47 +0000898static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
899 TargetMachine &tm,
Chris Lattner11d53c12010-03-13 20:55:24 +0000900 MCStreamer &Streamer) {
Jim Laskeybf111822006-12-21 20:26:09 +0000901 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
902
Chris Lattner40bbebd2009-07-21 18:38:57 +0000903 if (Subtarget->isDarwin())
Chris Lattner11d53c12010-03-13 20:55:24 +0000904 return new PPCDarwinAsmPrinter(o, tm, Streamer);
905 return new PPCLinuxAsmPrinter(o, tm, Streamer);
Chris Lattner4da1c822006-09-20 17:12:19 +0000906}
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000907
Bob Wilsona96751f2009-06-23 23:59:40 +0000908// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000909extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000910 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000911 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
912}