blob: 40d6778b646e6d214259b47e06dac3e0ad0fa036 [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 Lattnerd3ec0b52010-01-20 21:16:14 +000059 DenseMap<const MCSymbol*, const 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 Lattner56591ab2010-02-02 23:37:42 +000064 MCContext &Ctx, MCStreamer &Streamer,
65 const MCAsmInfo *T)
66 : AsmPrinter(O, TM, Ctx, Streamer, T),
Tilmann Scheller6b16eff2009-08-15 11:54:46 +000067 Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +000068
Misha Brukman5dfe3a92004-06-21 16:55:25 +000069 virtual const char *getPassName() const {
Nate Begemaned428532004-09-04 05:00:00 +000070 return "PowerPC Assembly Printer";
Misha Brukman5dfe3a92004-06-21 16:55:25 +000071 }
72
Nate Begeman21e463b2005-10-16 05:39:50 +000073 PPCTargetMachine &getTM() {
74 return static_cast<PPCTargetMachine&>(TM);
Chris Lattnera3840792004-08-16 23:25:21 +000075 }
76
Nate Begemanadeb43d2005-07-20 22:42:00 +000077 unsigned enumRegToMachineReg(unsigned enumReg) {
78 switch (enumReg) {
Torok Edwinc23197a2009-07-14 16:55:14 +000079 default: llvm_unreachable("Unhandled register!");
Nate Begemanadeb43d2005-07-20 22:42:00 +000080 case PPC::CR0: return 0;
81 case PPC::CR1: return 1;
82 case PPC::CR2: return 2;
83 case PPC::CR3: return 3;
84 case PPC::CR4: return 4;
85 case PPC::CR5: return 5;
86 case PPC::CR6: return 6;
87 case PPC::CR7: return 7;
88 }
Torok Edwinc23197a2009-07-14 16:55:14 +000089 llvm_unreachable(0);
Nate Begemanadeb43d2005-07-20 22:42:00 +000090 }
91
Nate Begemane59bf592004-08-14 22:09:10 +000092 /// printInstruction - This method is automatically generated by tablegen
93 /// from the instruction set description. This method returns true if the
94 /// machine instruction was sufficiently described to print it, otherwise it
95 /// returns false.
Chris Lattner41aefdc2009-08-08 01:32:19 +000096 void printInstruction(const MachineInstr *MI);
Chris Lattnerd95148f2009-09-13 20:19:22 +000097 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000098
Nate Begemane59bf592004-08-14 22:09:10 +000099
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000100 virtual void EmitInstruction(const MachineInstr *MI);
Chris Lattner9ba13e42005-11-17 19:25:59 +0000101 void printOp(const MachineOperand &MO);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000102
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000103 /// stripRegisterPrefix - This method strips the character prefix from a
104 /// register name so that only the number is left. Used by for linux asm.
Jim Laskey40ee69f2006-12-20 21:33:34 +0000105 const char *stripRegisterPrefix(const char *RegName) {
106 switch (RegName[0]) {
107 case 'r':
108 case 'f':
109 case 'v': return RegName + 1;
Jim Laskey2aa14aa2006-12-20 21:35:00 +0000110 case 'c': if (RegName[1] == 'r') return RegName + 2;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000111 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000112
Jim Laskey40ee69f2006-12-20 21:33:34 +0000113 return RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000114 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000115
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000116 /// printRegister - Print register according to target requirements.
117 ///
118 void printRegister(const MachineOperand &MO, bool R0AsZero) {
119 unsigned RegNo = MO.getReg();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000120 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000121
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000122 // If we should use 0 for R0.
123 if (R0AsZero && RegNo == PPC::R0) {
124 O << "0";
125 return;
126 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000127
Chris Lattner762ccea2009-09-13 20:31:40 +0000128 const char *RegName = getRegisterName(RegNo);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000129 // Linux assembler (Others?) does not take register mnemonics.
130 // FIXME - What about special registers used in mfspr/mtspr?
Jim Laskey40ee69f2006-12-20 21:33:34 +0000131 if (!Subtarget.isDarwin()) RegName = stripRegisterPrefix(RegName);
132 O << RegName;
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000133 }
Chris Lattner7bb424f2004-08-14 23:27:29 +0000134
Chris Lattner58873272006-02-01 22:38:46 +0000135 void printOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner7bb424f2004-08-14 23:27:29 +0000136 const MachineOperand &MO = MI->getOperand(OpNo);
Dan Gohmand735b802008-10-03 15:45:36 +0000137 if (MO.isReg()) {
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000138 printRegister(MO, false);
Dan Gohmand735b802008-10-03 15:45:36 +0000139 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000140 O << MO.getImm();
Chris Lattner7bb424f2004-08-14 23:27:29 +0000141 } else {
142 printOp(MO);
143 }
144 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000145
Chris Lattner58873272006-02-01 22:38:46 +0000146 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnere3f01572006-02-23 19:31:10 +0000147 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner2c003e22006-02-24 20:27:40 +0000148 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
149 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000150
151
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000152 void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000153 char value = MI->getOperand(OpNo).getImm();
Chris Lattner9c61dcf2006-03-25 06:12:06 +0000154 value = (value << (32-5)) >> (32-5);
155 O << (int)value;
156 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000157 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000158 unsigned char value = MI->getOperand(OpNo).getImm();
Chris Lattner12585ba2004-08-21 19:11:03 +0000159 assert(value <= 31 && "Invalid u5imm argument!");
Nate Begemanc3306122004-08-21 05:56:39 +0000160 O << (unsigned int)value;
161 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000162 void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000163 unsigned char value = MI->getOperand(OpNo).getImm();
Nate Begeman07aada82004-08-30 02:28:06 +0000164 assert(value <= 63 && "Invalid u6imm argument!");
165 O << (unsigned int)value;
166 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000167 void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000168 O << (short)MI->getOperand(OpNo).getImm();
Nate Begemaned428532004-09-04 05:00:00 +0000169 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000170 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000171 O << (unsigned short)MI->getOperand(OpNo).getImm();
Chris Lattner97b2a2e2004-08-15 05:20:16 +0000172 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000173 void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000174 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000175 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000176 } else {
177 O << "lo16(";
178 printOp(MI->getOperand(OpNo));
179 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000180 O << "-\"L" << getFunctionNumber() << "$pb\")";
Chris Lattner1b0a2d82006-11-16 21:45:30 +0000181 else
182 O << ')';
183 }
Chris Lattner841d12d2005-10-18 16:51:22 +0000184 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000185 void printBranchOperand(const MachineInstr *MI, unsigned OpNo) {
Nate Begemaned428532004-09-04 05:00:00 +0000186 // Branches can take an immediate operand. This is used by the branch
187 // selection pass to print $+8, an eight byte displacement from the PC.
Dan Gohmand735b802008-10-03 15:45:36 +0000188 if (MI->getOperand(OpNo).isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000189 O << "$+" << MI->getOperand(OpNo).getImm()*4;
Nate Begemaned428532004-09-04 05:00:00 +0000190 } else {
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000191 printOp(MI->getOperand(OpNo));
Nate Begemaned428532004-09-04 05:00:00 +0000192 }
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000193 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000194 void printCallOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9ba13e42005-11-17 19:25:59 +0000195 const MachineOperand &MO = MI->getOperand(OpNo);
Evan Cheng4c1aa862006-02-22 20:19:42 +0000196 if (TM.getRelocationModel() != Reloc::Static) {
Chris Lattnera637c322005-12-16 00:22:14 +0000197 if (MO.getType() == MachineOperand::MO_GlobalAddress) {
198 GlobalValue *GV = MO.getGlobal();
Chris Lattner20674332009-07-02 16:08:53 +0000199 if (GV->isDeclaration() || GV->isWeakForLinker()) {
Chris Lattnera637c322005-12-16 00:22:14 +0000200 // Dynamically-resolved functions need a stub for the function.
Chris Lattner73a1aa02010-01-20 21:36:48 +0000201 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$stub");
Bill Wendlingcebae362010-03-10 22:34:10 +0000202 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner73a1aa02010-01-20 21:36:48 +0000203 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000204 if (StubSym.getPointer() == 0)
205 StubSym = MachineModuleInfoImpl::
206 StubValueTy(GetGlobalValueSymbol(GV),!GV->hasInternalLinkage());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000207 O << *Sym;
Chris Lattnera637c322005-12-16 00:22:14 +0000208 return;
209 }
210 }
Chris Lattner9542f9712005-11-17 19:40:30 +0000211 if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
Chris Lattner73a1aa02010-01-20 21:36:48 +0000212 SmallString<128> TempNameStr;
213 TempNameStr += StringRef(MO.getSymbolName());
214 TempNameStr += StringRef("$stub");
215
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000216 MCSymbol *Sym = GetExternalSymbolSymbol(TempNameStr.str());
Bill Wendlingcebae362010-03-10 22:34:10 +0000217 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner73a1aa02010-01-20 21:36:48 +0000218 MMI->getObjFileInfo<MachineModuleInfoMachO>().getFnStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000219 if (StubSym.getPointer() == 0)
220 StubSym = MachineModuleInfoImpl::
221 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000222 O << *Sym;
Chris Lattner9542f9712005-11-17 19:40:30 +0000223 return;
Chris Lattner9542f9712005-11-17 19:40:30 +0000224 }
Chris Lattner9ba13e42005-11-17 19:25:59 +0000225 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000226
Chris Lattner9542f9712005-11-17 19:40:30 +0000227 printOp(MI->getOperand(OpNo));
Chris Lattner3e7f86a2005-11-17 19:16:08 +0000228 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000229 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000230 O << (int)MI->getOperand(OpNo).getImm()*4;
Nate Begeman422b0ce2005-11-16 00:48:01 +0000231 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000232 void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000233 O << "\"L" << getFunctionNumber() << "$pb\"\n";
234 O << "\"L" << getFunctionNumber() << "$pb\":";
Nate Begemanb7a8f2c2004-09-02 08:13:00 +0000235 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000236 void printSymbolHi(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000237 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000238 printS16ImmOperand(MI, OpNo);
Nate Begeman2497e632005-07-21 20:44:43 +0000239 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000240 if (Subtarget.isDarwin()) O << "ha16(";
Nate Begeman2497e632005-07-21 20:44:43 +0000241 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000242 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000243 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000244 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000245 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000246 else
247 O << "@ha";
Nate Begeman2497e632005-07-21 20:44:43 +0000248 }
Nate Begemaned428532004-09-04 05:00:00 +0000249 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000250 void printSymbolLo(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000251 if (MI->getOperand(OpNo).isImm()) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000252 printS16ImmOperand(MI, OpNo);
Nate Begemaned428532004-09-04 05:00:00 +0000253 } else {
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000254 if (Subtarget.isDarwin()) O << "lo16(";
Nate Begemand4c8bea2004-11-25 07:09:01 +0000255 printOp(MI->getOperand(OpNo));
Chris Lattner35d86fe2006-07-26 21:12:04 +0000256 if (TM.getRelocationModel() == Reloc::PIC_)
Evan Cheng347d39f2007-10-14 05:57:21 +0000257 O << "-\"L" << getFunctionNumber() << "$pb\"";
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000258 if (Subtarget.isDarwin())
Nate Begeman2497e632005-07-21 20:44:43 +0000259 O << ')';
Nick Lewyckyf54949d2007-03-03 05:29:51 +0000260 else
261 O << "@l";
Nate Begemaned428532004-09-04 05:00:00 +0000262 }
263 }
Nate Begeman391c5d22005-11-30 18:54:35 +0000264 void printcrbitm(const MachineInstr *MI, unsigned OpNo) {
Nate Begemanadeb43d2005-07-20 22:42:00 +0000265 unsigned CCReg = MI->getOperand(OpNo).getReg();
266 unsigned RegNo = enumRegToMachineReg(CCReg);
267 O << (0x80 >> RegNo);
268 }
Chris Lattner2c003e22006-02-24 20:27:40 +0000269 // The new addressing mode printers.
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000270 void printMemRegImm(const MachineInstr *MI, unsigned OpNo) {
271 printSymbolLo(MI, OpNo);
272 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000273 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattner13feb582006-03-21 17:21:13 +0000274 MI->getOperand(OpNo+1).getReg() == PPC::R0)
275 O << "0";
276 else
277 printOperand(MI, OpNo+1);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000278 O << ')';
279 }
Chris Lattnere5ba5802006-03-22 05:26:03 +0000280 void printMemRegImmShifted(const MachineInstr *MI, unsigned OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +0000281 if (MI->getOperand(OpNo).isImm())
Chris Lattnere5ba5802006-03-22 05:26:03 +0000282 printS16X4ImmOperand(MI, OpNo);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000283 else
Chris Lattnere5ba5802006-03-22 05:26:03 +0000284 printSymbolLo(MI, OpNo);
285 O << '(';
Dan Gohmand735b802008-10-03 15:45:36 +0000286 if (MI->getOperand(OpNo+1).isReg() &&
Chris Lattnere5ba5802006-03-22 05:26:03 +0000287 MI->getOperand(OpNo+1).getReg() == PPC::R0)
288 O << "0";
289 else
290 printOperand(MI, OpNo+1);
291 O << ')';
292 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000293
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000294 void printMemRegReg(const MachineInstr *MI, unsigned OpNo) {
Nate Begeman88276b82005-12-19 23:40:42 +0000295 // When used as the base register, r0 reads constant zero rather than
296 // the value contained in the register. For this reason, the darwin
297 // assembler requires that we print r0 as 0 (no r) when used as the base.
298 const MachineOperand &MO = MI->getOperand(OpNo);
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000299 printRegister(MO, true);
Nate Begeman7fd1edd2005-12-19 23:25:09 +0000300 O << ", ";
301 printOperand(MI, OpNo+1);
302 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000303
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000304 void printTOCEntryLabel(const MachineInstr *MI, unsigned OpNo) {
305 const MachineOperand &MO = MI->getOperand(OpNo);
306
307 assert(MO.getType() == MachineOperand::MO_GlobalAddress);
308
Chris Lattner75abc682010-01-16 02:09:06 +0000309 const MCSymbol *Sym = GetGlobalValueSymbol(MO.getGlobal());
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000310
311 // Map symbol -> label of TOC entry.
Chris Lattner75abc682010-01-16 02:09:06 +0000312 const MCSymbol *&TOCEntry = TOC[Sym];
313 if (TOCEntry == 0)
314 TOCEntry = OutContext.
Chris Lattner98cdab52010-03-10 02:25:11 +0000315 GetOrCreateTemporarySymbol(StringRef(MAI->getPrivateGlobalPrefix()) +
316 "C" + Twine(LabelID++));
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000317
Chris Lattner10b318b2010-01-17 21:43:43 +0000318 O << *TOCEntry << "@toc";
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000319 }
320
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000321 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000322 const char *Modifier);
Nate Begemaned428532004-09-04 05:00:00 +0000323 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000324
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000325 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000326 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
Bill Wendling57f0db82009-02-24 08:30:20 +0000327 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000328 explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner56591ab2010-02-02 23:37:42 +0000329 MCContext &Ctx, MCStreamer &Streamer,
330 const MCAsmInfo *T)
331 : PPCAsmPrinter(O, TM, Ctx, Streamer, T) {}
Jim Laskeybf111822006-12-21 20:26:09 +0000332
333 virtual const char *getPassName() const {
334 return "Linux PPC Assembly Printer";
335 }
336
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000337 bool doFinalization(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000338
Chris Lattner2cf72512010-01-27 07:21:55 +0000339 virtual void EmitFunctionEntryLabel();
340
Jim Laskeybf111822006-12-21 20:26:09 +0000341 void getAnalysisUsage(AnalysisUsage &AU) const {
342 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000343 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000344 AU.addRequired<DwarfWriter>();
Jim Laskeybf111822006-12-21 20:26:09 +0000345 PPCAsmPrinter::getAnalysisUsage(AU);
346 }
Jim Laskeybf111822006-12-21 20:26:09 +0000347 };
348
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000349 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
350 /// OS X
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000351 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
David Greene71847812009-07-14 20:18:05 +0000352 formatted_raw_ostream &OS;
Bill Wendling57f0db82009-02-24 08:30:20 +0000353 public:
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000354 explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner56591ab2010-02-02 23:37:42 +0000355 MCContext &Ctx, MCStreamer &Streamer,
356 const MCAsmInfo *T)
357 : PPCAsmPrinter(O, TM, Ctx, Streamer, T), OS(O) {}
Nate Begemaned428532004-09-04 05:00:00 +0000358
359 virtual const char *getPassName() const {
360 return "Darwin PPC Assembly Printer";
361 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000362
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000363 bool doFinalization(Module &M);
Bob Wilson812209a2009-09-30 22:06:26 +0000364 void EmitStartOfAsmFile(Module &M);
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000365
Chris Lattner73a1aa02010-01-20 21:36:48 +0000366 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs);
Chris Lattner917d6282010-01-20 21:19:44 +0000367
Jim Laskeyb2efb852006-01-04 22:28:25 +0000368 void getAnalysisUsage(AnalysisUsage &AU) const {
Jim Laskeyabf6d172006-01-05 01:25:28 +0000369 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000370 AU.addRequired<MachineModuleInfo>();
Devang Pateleb3fc282009-01-08 23:40:34 +0000371 AU.addRequired<DwarfWriter>();
Jim Laskeyabf6d172006-01-05 01:25:28 +0000372 PPCAsmPrinter::getAnalysisUsage(AU);
Jim Laskeyb2efb852006-01-04 22:28:25 +0000373 }
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000374 };
375} // end of anonymous namespace
376
Nate Begemane59bf592004-08-14 22:09:10 +0000377// Include the auto-generated portion of the assembly writer
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000378#include "PPCGenAsmWriter.inc"
Nate Begemane59bf592004-08-14 22:09:10 +0000379
Chris Lattner9ba13e42005-11-17 19:25:59 +0000380void PPCAsmPrinter::printOp(const MachineOperand &MO) {
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000381 switch (MO.getType()) {
Chris Lattner63b3d712006-05-04 17:21:20 +0000382 case MachineOperand::MO_Immediate:
Torok Edwinc23197a2009-07-14 16:55:14 +0000383 llvm_unreachable("printOp() does not handle immediate values");
Misha Brukman97a296f2004-07-21 20:11:11 +0000384
Nate Begeman37efe672006-04-22 18:53:45 +0000385 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +0000386 O << *MO.getMBB()->getSymbol(OutContext);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000387 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000388 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000389 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000390 << '_' << MO.getIndex();
Nate Begeman37efe672006-04-22 18:53:45 +0000391 // FIXME: PIC relocation model
392 return;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000393 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000394 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000395 << '_' << MO.getIndex();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000396 return;
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000397 case MachineOperand::MO_BlockAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +0000398 O << *GetBlockAddressSymbol(MO.getBlockAddress());
Bob Wilson3d90dbe2009-11-04 21:31:18 +0000399 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000400 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera637c322005-12-16 00:22:14 +0000401 // Computing the address of an external symbol, not calling it.
Chris Lattner75abc682010-01-16 02:09:06 +0000402 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000403 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner75abc682010-01-16 02:09:06 +0000404 return;
Chris Lattnera637c322005-12-16 00:22:14 +0000405 }
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000406
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000407 MCSymbol *NLPSym =
Chris Lattner75abc682010-01-16 02:09:06 +0000408 OutContext.GetOrCreateSymbol(StringRef(MAI->getGlobalPrefix())+
409 MO.getSymbolName()+"$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000410 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000411 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(NLPSym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000412 if (StubSym.getPointer() == 0)
413 StubSym = MachineModuleInfoImpl::
414 StubValueTy(GetExternalSymbolSymbol(MO.getSymbolName()), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000415
Chris Lattner10b318b2010-01-17 21:43:43 +0000416 O << *NLPSym;
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000417 return;
Chris Lattner8f831cb2009-07-15 01:14:44 +0000418 }
Nate Begemanb73a7112004-08-13 09:32:01 +0000419 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera637c322005-12-16 00:22:14 +0000420 // Computing the address of a global symbol, not calling it.
Nate Begemanb73a7112004-08-13 09:32:01 +0000421 GlobalValue *GV = MO.getGlobal();
Chris Lattnera40128c2010-01-16 02:00:23 +0000422 MCSymbol *SymToPrint;
Misha Brukmane2eceb52004-07-23 16:08:20 +0000423
Nate Begemanfcf4a422004-10-17 23:01:34 +0000424 // External or weakly linked global variables need non-lazily-resolved stubs
Chris Lattner8f831cb2009-07-15 01:14:44 +0000425 if (TM.getRelocationModel() != Reloc::Static &&
426 (GV->isDeclaration() || GV->isWeakForLinker())) {
427 if (!GV->hasHiddenVisibility()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000428 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000429 MachineModuleInfoImpl::StubValueTy &StubSym =
430 MMI->getObjFileInfo<MachineModuleInfoMachO>()
431 .getGVStubEntry(SymToPrint);
432 if (StubSym.getPointer() == 0)
433 StubSym = MachineModuleInfoImpl::
434 StubValueTy(GetGlobalValueSymbol(GV), !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000435 } else if (GV->isDeclaration() || GV->hasCommonLinkage() ||
436 GV->hasAvailableExternallyLinkage()) {
Chris Lattner7a2ba942010-01-16 18:37:32 +0000437 SymToPrint = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000438
Bill Wendlingcebae362010-03-10 22:34:10 +0000439 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000440 MMI->getObjFileInfo<MachineModuleInfoMachO>().
441 getHiddenGVStubEntry(SymToPrint);
Bill Wendlingcebae362010-03-10 22:34:10 +0000442 if (StubSym.getPointer() == 0)
443 StubSym = MachineModuleInfoImpl::
444 StubValueTy(GetGlobalValueSymbol(GV),
445 !GV->hasInternalLinkage());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000446 } else {
Chris Lattnera40128c2010-01-16 02:00:23 +0000447 SymToPrint = GetGlobalValueSymbol(GV);
Chris Lattnera637c322005-12-16 00:22:14 +0000448 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000449 } else {
Chris Lattnera40128c2010-01-16 02:00:23 +0000450 SymToPrint = GetGlobalValueSymbol(GV);
Nate Begemanb73a7112004-08-13 09:32:01 +0000451 }
Chris Lattnera40128c2010-01-16 02:00:23 +0000452
Chris Lattner10b318b2010-01-17 21:43:43 +0000453 O << *SymToPrint;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000454
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000455 printOffset(MO.getOffset());
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000456 return;
Nate Begemanb73a7112004-08-13 09:32:01 +0000457 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000458
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000459 default:
Misha Brukman05fcd0c2004-07-08 17:58:04 +0000460 O << "<unknown operand type: " << MO.getType() << ">";
Misha Brukman22e12072004-06-25 15:11:34 +0000461 return;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000462 }
463}
464
Chris Lattnere3f01572006-02-23 19:31:10 +0000465/// PrintAsmOperand - Print out an operand for an inline asm expression.
466///
467bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000468 unsigned AsmVariant,
Chris Lattnere3f01572006-02-23 19:31:10 +0000469 const char *ExtraCode) {
470 // Does this asm operand have a single letter operand modifier?
471 if (ExtraCode && ExtraCode[0]) {
472 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000473
Chris Lattnere3f01572006-02-23 19:31:10 +0000474 switch (ExtraCode[0]) {
475 default: return true; // Unknown modifier.
Chris Lattner78192b62007-01-25 02:52:50 +0000476 case 'c': // Don't print "$" before a global var name or constant.
477 // PPC never has a prefix.
478 printOperand(MI, OpNo);
479 return false;
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000480 case 'L': // Write second word of DImode reference.
Chris Lattnere3f01572006-02-23 19:31:10 +0000481 // Verify that this operand has two consecutive registers.
Dan Gohmand735b802008-10-03 15:45:36 +0000482 if (!MI->getOperand(OpNo).isReg() ||
Chris Lattnere3f01572006-02-23 19:31:10 +0000483 OpNo+1 == MI->getNumOperands() ||
Dan Gohmand735b802008-10-03 15:45:36 +0000484 !MI->getOperand(OpNo+1).isReg())
Chris Lattnere3f01572006-02-23 19:31:10 +0000485 return true;
486 ++OpNo; // Return the high-part.
487 break;
Chris Lattner6c2d2602007-04-24 22:51:03 +0000488 case 'I':
489 // Write 'i' if an integer constant, otherwise nothing. Used to print
490 // addi vs add, etc.
Dan Gohmand735b802008-10-03 15:45:36 +0000491 if (MI->getOperand(OpNo).isImm())
Chris Lattner6c2d2602007-04-24 22:51:03 +0000492 O << "i";
493 return false;
Chris Lattnere3f01572006-02-23 19:31:10 +0000494 }
495 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000496
Chris Lattnere3f01572006-02-23 19:31:10 +0000497 printOperand(MI, OpNo);
498 return false;
499}
500
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000501// At the moment, all inline asm memory operands are a single register.
502// In any case, the output of this routine should always be just one
503// assembler operand.
504
Chris Lattner2c003e22006-02-24 20:27:40 +0000505bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000506 unsigned AsmVariant,
Chris Lattner2c003e22006-02-24 20:27:40 +0000507 const char *ExtraCode) {
508 if (ExtraCode && ExtraCode[0])
509 return true; // Unknown modifier.
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000510 assert (MI->getOperand(OpNo).isReg());
Dale Johannesen4e68f882009-08-26 18:10:32 +0000511 O << "0(";
Dale Johannesen5cfd4dd2009-08-18 00:18:39 +0000512 printOperand(MI, OpNo);
Dale Johannesen4e68f882009-08-26 18:10:32 +0000513 O << ")";
Chris Lattner2c003e22006-02-24 20:27:40 +0000514 return false;
515}
Chris Lattnere3f01572006-02-23 19:31:10 +0000516
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000517void PPCAsmPrinter::printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattneraf53a872006-11-04 05:27:39 +0000518 const char *Modifier) {
519 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
520 unsigned Code = MI->getOperand(OpNo).getImm();
521 if (!strcmp(Modifier, "cc")) {
522 switch ((PPC::Predicate)Code) {
523 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
524 case PPC::PRED_LT: O << "lt"; return;
525 case PPC::PRED_LE: O << "le"; return;
526 case PPC::PRED_EQ: O << "eq"; return;
527 case PPC::PRED_GE: O << "ge"; return;
528 case PPC::PRED_GT: O << "gt"; return;
529 case PPC::PRED_NE: O << "ne"; return;
530 case PPC::PRED_UN: O << "un"; return;
531 case PPC::PRED_NU: O << "nu"; return;
532 }
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000533
Chris Lattneraf53a872006-11-04 05:27:39 +0000534 } else {
535 assert(!strcmp(Modifier, "reg") &&
536 "Need to specify 'cc' or 'reg' as predicate op modifier!");
537 // Don't print the register for 'always'.
538 if (Code == PPC::PRED_ALWAYS) return;
539 printOperand(MI, OpNo+1);
540 }
541}
542
543
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000544/// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
Nate Begemane59bf592004-08-14 22:09:10 +0000545/// the current output stream.
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000546///
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000547void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Nate Begemanad5f65c2005-04-05 18:19:50 +0000548 // Check for slwi/srwi mnemonics.
549 if (MI->getOpcode() == PPC::RLWINM) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000550 unsigned char SH = MI->getOperand(2).getImm();
551 unsigned char MB = MI->getOperand(3).getImm();
552 unsigned char ME = MI->getOperand(4).getImm();
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000553 bool useSubstituteMnemonic = false;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000554 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
Dale Johannesene89c5102010-01-06 02:20:18 +0000555 O << "\tslwi "; useSubstituteMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000556 }
557 if (SH <= 31 && MB == (32-SH) && ME == 31) {
Dale Johannesene89c5102010-01-06 02:20:18 +0000558 O << "\tsrwi "; useSubstituteMnemonic = true;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000559 SH = 32-SH;
560 }
Dale Johannesene89c5102010-01-06 02:20:18 +0000561 if (useSubstituteMnemonic) {
Nate Begeman391c5d22005-11-30 18:54:35 +0000562 printOperand(MI, 0);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000563 O << ", ";
Nate Begeman391c5d22005-11-30 18:54:35 +0000564 printOperand(MI, 1);
Dale Johannesene89c5102010-01-06 02:20:18 +0000565 O << ", " << (unsigned int)SH;
Chris Lattner8e089a92010-02-10 00:36:00 +0000566 OutStreamer.AddBlankLine();
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000567 return;
Nate Begemanad5f65c2005-04-05 18:19:50 +0000568 }
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000569 }
570
571 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
572 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
573 O << "\tmr ";
574 printOperand(MI, 0);
575 O << ", ";
576 printOperand(MI, 1);
Chris Lattner8e089a92010-02-10 00:36:00 +0000577 OutStreamer.AddBlankLine();
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000578 return;
579 }
580
581 if (MI->getOpcode() == PPC::RLDICR) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000582 unsigned char SH = MI->getOperand(2).getImm();
583 unsigned char ME = MI->getOperand(3).getImm();
Chris Lattner566c1b12006-11-18 01:23:56 +0000584 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
585 if (63-SH == ME) {
Nate Begeman5a804e32008-02-05 08:49:09 +0000586 O << "\tsldi ";
Chris Lattner566c1b12006-11-18 01:23:56 +0000587 printOperand(MI, 0);
588 O << ", ";
589 printOperand(MI, 1);
Dale Johannesene89c5102010-01-06 02:20:18 +0000590 O << ", " << (unsigned int)SH;
Chris Lattner8e089a92010-02-10 00:36:00 +0000591 OutStreamer.AddBlankLine();
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000592 return;
Chris Lattner566c1b12006-11-18 01:23:56 +0000593 }
Nate Begemanad5f65c2005-04-05 18:19:50 +0000594 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000595
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000596 printInstruction(MI);
Chris Lattner8e089a92010-02-10 00:36:00 +0000597 OutStreamer.AddBlankLine();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000598}
599
Chris Lattner2cf72512010-01-27 07:21:55 +0000600void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
601 if (!Subtarget.isPPC64()) // linux/ppc32 - Normal entry label.
602 return AsmPrinter::EmitFunctionEntryLabel();
603
604 // Emit an official procedure descriptor.
605 // FIXME 64-bit SVR4: Use MCSection here!
606 O << "\t.section\t\".opd\",\"aw\"\n";
607 O << "\t.align 3\n";
608 OutStreamer.EmitLabel(CurrentFnSym);
609 O << "\t.quad .L." << *CurrentFnSym << ",.TOC.@tocbase\n";
610 O << "\t.previous\n";
611 O << ".L." << *CurrentFnSym << ":\n";
612}
613
614
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000615bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
616 const TargetData *TD = TM.getTargetData();
617
618 bool isPPC64 = TD->getPointerSizeInBits() == 64;
619
620 if (isPPC64 && !TOC.empty()) {
621 // FIXME 64-bit SVR4: Use MCSection here?
622 O << "\t.section\t\".toc\",\"aw\"\n";
623
Chris Lattnera40128c2010-01-16 02:00:23 +0000624 // FIXME: This is nondeterminstic!
Chris Lattner75abc682010-01-16 02:09:06 +0000625 for (DenseMap<const MCSymbol*, const MCSymbol*>::iterator I = TOC.begin(),
626 E = TOC.end(); I != E; ++I) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000627 O << *I->second << ":\n";
628 O << "\t.tc " << *I->first << "[TC]," << *I->first << '\n';
Tilmann Scheller6b16eff2009-08-15 11:54:46 +0000629 }
630 }
631
632 return AsmPrinter::doFinalization(M);
633}
Jim Laskeybf111822006-12-21 20:26:09 +0000634
Bob Wilson812209a2009-09-30 22:06:26 +0000635void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
Dan Gohmancfbb2f02008-03-25 21:45:14 +0000636 static const char *const CPUDirectives[] = {
Dale Johannesendb01c8b2008-02-14 23:35:16 +0000637 "",
Jim Laskeyc35010d2006-12-12 20:57:08 +0000638 "ppc",
639 "ppc601",
640 "ppc602",
641 "ppc603",
642 "ppc7400",
643 "ppc750",
644 "ppc970",
645 "ppc64"
646 };
647
648 unsigned Directive = Subtarget.getDarwinDirective();
649 if (Subtarget.isGigaProcessor() && Directive < PPC::DIR_970)
650 Directive = PPC::DIR_970;
651 if (Subtarget.hasAltivec() && Directive < PPC::DIR_7400)
652 Directive = PPC::DIR_7400;
653 if (Subtarget.isPPC64() && Directive < PPC::DIR_970)
654 Directive = PPC::DIR_64;
655 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000656 O << "\t.machine " << CPUDirectives[Directive] << '\n';
Anton Korobeynikov34da1272008-08-08 18:22:59 +0000657
Jim Laskey2ada0852006-11-28 18:21:52 +0000658 // Prime text sections so they are adjacent. This reduces the likelihood a
659 // large data or debug section causes a branch to exceed 16M limit.
Chris Lattner2dfddee2009-08-03 22:52:21 +0000660 TargetLoweringObjectFileMachO &TLOFMacho =
661 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000662 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
Jim Laskey2ada0852006-11-28 18:21:52 +0000663 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000664 OutStreamer.SwitchSection(
665 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
666 MCSectionMachO::S_SYMBOL_STUBS |
667 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
668 32, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000669 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000670 OutStreamer.SwitchSection(
671 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
672 MCSectionMachO::S_SYMBOL_STUBS |
673 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
674 16, SectionKind::getText()));
Jim Laskey2ada0852006-11-28 18:21:52 +0000675 }
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000676 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
Nate Begeman18ed0292005-07-21 01:25:49 +0000677}
678
Chris Lattner73a1aa02010-01-20 21:36:48 +0000679static const MCSymbol *GetLazyPtr(const MCSymbol *Sym, MCContext &Ctx) {
680 // Remove $stub suffix, add $lazy_ptr.
681 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end()-5);
682 TmpStr += "$lazy_ptr";
Chris Lattner98cdab52010-03-10 02:25:11 +0000683 return Ctx.GetOrCreateTemporarySymbol(TmpStr.str());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000684}
685
686static const MCSymbol *GetAnonSym(const MCSymbol *Sym, MCContext &Ctx) {
687 // Add $tmp suffix to $stub, yielding $stub$tmp.
688 SmallString<128> TmpStr(Sym->getName().begin(), Sym->getName().end());
689 TmpStr += "$tmp";
Chris Lattner98cdab52010-03-10 02:25:11 +0000690 return Ctx.GetOrCreateTemporarySymbol(TmpStr.str());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000691}
692
693void PPCDarwinAsmPrinter::
694EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) {
Chris Lattner917d6282010-01-20 21:19:44 +0000695 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
696
Chris Lattner2dfddee2009-08-03 22:52:21 +0000697 TargetLoweringObjectFileMachO &TLOFMacho =
698 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner917d6282010-01-20 21:19:44 +0000699
700 // .lazy_symbol_pointer
701 const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection();
Chris Lattnerff4bc462009-08-10 01:39:42 +0000702
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000703 // Output stubs for dynamically-linked functions
Chris Lattner917d6282010-01-20 21:19:44 +0000704 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattner2dfddee2009-08-03 22:52:21 +0000705 const MCSection *StubSection =
Chris Lattner917d6282010-01-20 21:19:44 +0000706 TLOFMacho.getMachOSection("__TEXT", "__picsymbolstub1",
707 MCSectionMachO::S_SYMBOL_STUBS |
708 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
709 32, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000710 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000711 OutStreamer.SwitchSection(StubSection);
Chris Lattner7e097e42006-06-27 01:02:25 +0000712 EmitAlignment(4);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000713
714 const MCSymbol *Stub = Stubs[i].first;
Bill Wendlingcebae362010-03-10 22:34:10 +0000715 const MCSymbol *RawSym = Stubs[i].second.getPointer();
Chris Lattner73a1aa02010-01-20 21:36:48 +0000716 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
717 const MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext);
718
719 O << *Stub << ":\n";
720 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattnerdeea4162005-12-13 04:33:58 +0000721 O << "\tmflr r0\n";
Chris Lattner73a1aa02010-01-20 21:36:48 +0000722 O << "\tbcl 20,31," << *AnonSymbol << '\n';
723 O << *AnonSymbol << ":\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000724 O << "\tmflr r11\n";
Chris Lattner73a1aa02010-01-20 21:36:48 +0000725 O << "\taddis r11,r11,ha16(" << *LazyPtr << '-' << *AnonSymbol
Chris Lattner917d6282010-01-20 21:19:44 +0000726 << ")\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000727 O << "\tmtlr r0\n";
Chris Lattner73a1aa02010-01-20 21:36:48 +0000728 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
729 << '-' << *AnonSymbol << ")(r11)\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000730 O << "\tmtctr r12\n";
731 O << "\tbctr\n";
Chris Lattnerfe2fe702009-07-16 01:23:26 +0000732
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000733 OutStreamer.SwitchSection(LSPSection);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000734 O << *LazyPtr << ":\n";
735 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattner1a87c052009-07-15 02:36:21 +0000736 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
Chris Lattnerdeea4162005-12-13 04:33:58 +0000737 }
Chris Lattner917d6282010-01-20 21:19:44 +0000738 O << '\n';
739 return;
Misha Brukman46fd00a2004-06-24 23:04:11 +0000740 }
Chris Lattner917d6282010-01-20 21:19:44 +0000741
742 const MCSection *StubSection =
743 TLOFMacho.getMachOSection("__TEXT","__symbol_stub1",
744 MCSectionMachO::S_SYMBOL_STUBS |
745 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
746 16, SectionKind::getText());
Chris Lattner73a1aa02010-01-20 21:36:48 +0000747 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
748 const MCSymbol *Stub = Stubs[i].first;
Bill Wendlingcebae362010-03-10 22:34:10 +0000749 const MCSymbol *RawSym = Stubs[i].second.getPointer();
Chris Lattner73a1aa02010-01-20 21:36:48 +0000750 const MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext);
751
Chris Lattner917d6282010-01-20 21:19:44 +0000752 OutStreamer.SwitchSection(StubSection);
753 EmitAlignment(4);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000754 O << *Stub << ":\n";
755 O << "\t.indirect_symbol " << *RawSym << '\n';
756 O << "\tlis r11,ha16(" << *LazyPtr << ")\n";
757 O << (isPPC64 ? "\tldu" : "\tlwzu") << " r12,lo16(" << *LazyPtr
Chris Lattner917d6282010-01-20 21:19:44 +0000758 << ")(r11)\n";
759 O << "\tmtctr r12\n";
760 O << "\tbctr\n";
761 OutStreamer.SwitchSection(LSPSection);
Chris Lattner73a1aa02010-01-20 21:36:48 +0000762 O << *LazyPtr << ":\n";
763 O << "\t.indirect_symbol " << *RawSym << '\n';
Chris Lattner917d6282010-01-20 21:19:44 +0000764 O << (isPPC64 ? "\t.quad" : "\t.long") << " dyld_stub_binding_helper\n";
765 }
766
Anton Korobeynikov382f0022008-08-08 18:24:10 +0000767 O << '\n';
Chris Lattner917d6282010-01-20 21:19:44 +0000768}
769
770
771bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
772 bool isPPC64 = TM.getTargetData()->getPointerSizeInBits() == 64;
773
774 // Darwin/PPC always uses mach-o.
775 TargetLoweringObjectFileMachO &TLOFMacho =
776 static_cast<TargetLoweringObjectFileMachO &>(getObjFileLowering());
777 MachineModuleInfoMachO &MMIMacho =
778 MMI->getObjFileInfo<MachineModuleInfoMachO>();
779
Chris Lattner73a1aa02010-01-20 21:36:48 +0000780 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList();
781 if (!Stubs.empty())
782 EmitFunctionStubs(Stubs);
Misha Brukmanda2b13f2004-07-16 20:29:04 +0000783
Chris Lattner33adcfb2009-08-22 21:43:10 +0000784 if (MAI->doesSupportExceptionHandling() && MMI) {
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000785 // Add the (possibly multiple) personalities to the set of global values.
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000786 // Only referenced functions get into the Personalities list.
Chris Lattner0de1fc42009-06-24 19:09:55 +0000787 const std::vector<Function *> &Personalities = MMI->getPersonalities();
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000788 for (std::vector<Function *>::const_iterator I = Personalities.begin(),
Chris Lattner8f831cb2009-07-15 01:14:44 +0000789 E = Personalities.end(); I != E; ++I) {
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000790 if (*I) {
Chris Lattnerd269a6e2010-02-03 06:18:30 +0000791 MCSymbol *NLPSym = GetSymbolWithGlobalValueBase(*I, "$non_lazy_ptr");
Bill Wendlingcebae362010-03-10 22:34:10 +0000792 MachineModuleInfoImpl::StubValueTy &StubSym =
793 MMIMacho.getGVStubEntry(NLPSym);
794 StubSym = MachineModuleInfoImpl::
795 StubValueTy(GetGlobalValueSymbol(*I), true);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000796 }
Chris Lattner8f831cb2009-07-15 01:14:44 +0000797 }
Dale Johannesen1d4ce2a2007-11-20 23:24:42 +0000798 }
799
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000800 // Output stubs for dynamically-linked functions.
Chris Lattner73a1aa02010-01-20 21:36:48 +0000801 Stubs = MMIMacho.GetGVStubList();
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000802
Chris Lattner2dfddee2009-08-03 22:52:21 +0000803 // Output macho stubs for external and common global variables.
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000804 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000805 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000806 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnere1e8a822009-08-10 17:58:51 +0000807 EmitAlignment(isPPC64 ? 3 : 2);
808
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000809 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +0000810 // L_foo$stub:
811 OutStreamer.EmitLabel(Stubs[i].first);
812 // .indirect_symbol _foo
813 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
814 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
Bill Wendling53351a12010-03-12 02:00:43 +0000815
816 if (MCSym.getInt())
817 // External to current translation unit.
818 OutStreamer.EmitIntValue(0, isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
819 else
820 // Internal to current translation unit.
821 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
822 OutContext),
823 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Chris Lattnerdeea4162005-12-13 04:33:58 +0000824 }
Bill Wendling08d726c2010-03-11 23:39:44 +0000825
826 Stubs.clear();
827 OutStreamer.AddBlankLine();
Nate Begemane59bf592004-08-14 22:09:10 +0000828 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000829
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000830 Stubs = MMIMacho.GetHiddenGVStubList();
831 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000832 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner8f831cb2009-07-15 01:14:44 +0000833 EmitAlignment(isPPC64 ? 3 : 2);
Chris Lattnerd3ec0b52010-01-20 21:16:14 +0000834
835 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendling08d726c2010-03-11 23:39:44 +0000836 // L_foo$stub:
837 OutStreamer.EmitLabel(Stubs[i].first);
838 // .long _foo
839 OutStreamer.EmitValue(MCSymbolRefExpr::
840 Create(Stubs[i].second.getPointer(),
841 OutContext),
842 isPPC64 ? 8 : 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000843 }
Bill Wendling08d726c2010-03-11 23:39:44 +0000844
845 Stubs.clear();
846 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000847 }
848
Chris Lattner5b0ac992005-11-01 00:12:36 +0000849 // Funny Darwin hack: This flag tells the linker that no global symbols
850 // contain code that falls through to other global symbols (e.g. the obvious
851 // implementation of multiple entry points). If this doesn't occur, the
852 // linker can safely perform dead code stripping. Since LLVM never generates
853 // code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000854 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Chris Lattner5b0ac992005-11-01 00:12:36 +0000855
Dan Gohmanb8275a32007-07-25 19:33:14 +0000856 return AsmPrinter::doFinalization(M);
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000857}
Nate Begemaned428532004-09-04 05:00:00 +0000858
Jim Laskeyb608a4d2006-12-20 20:56:46 +0000859/// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
860/// for a MachineFunction to the given output stream, in a format that the
Chris Lattner4da1c822006-09-20 17:12:19 +0000861/// Darwin assembler can deal with.
862///
Daniel Dunbar78945782009-08-13 23:48:47 +0000863static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
864 TargetMachine &tm,
Chris Lattner56591ab2010-02-02 23:37:42 +0000865 MCContext &Ctx, MCStreamer &Streamer,
866 const MCAsmInfo *tai) {
Jim Laskeybf111822006-12-21 20:26:09 +0000867 const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
868
Chris Lattner40bbebd2009-07-21 18:38:57 +0000869 if (Subtarget->isDarwin())
Chris Lattner56591ab2010-02-02 23:37:42 +0000870 return new PPCDarwinAsmPrinter(o, tm, Ctx, Streamer, tai);
871 return new PPCLinuxAsmPrinter(o, tm, Ctx, Streamer, tai);
Chris Lattner4da1c822006-09-20 17:12:19 +0000872}
Anton Korobeynikov06be9972008-08-17 13:54:28 +0000873
Bob Wilsona96751f2009-06-23 23:59:40 +0000874// Force static initialization.
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000875extern "C" void LLVMInitializePowerPCAsmPrinter() {
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000876 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass);
Daniel Dunbar51b198a2009-07-15 20:24:03 +0000877 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass);
878}